forked from deepgram-devs/node-live-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
108 lines (87 loc) · 2.75 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const captions = window.document.getElementById("captions");
const languages = window.document.getElementById("selectLang");
let currentLang = "en";
async function getMicrophone() {
const userMedia = await navigator.mediaDevices.getUserMedia({
audio: true,
});
return new MediaRecorder(userMedia);
}
async function openMicrophone(microphone, socket) {
await microphone.start(500);
microphone.onstart = () => {
console.log("client: microphone opened");
document.body.classList.add("recording");
};
microphone.onstop = () => {
console.log("client: microphone closed");
document.body.classList.remove("recording");
};
microphone.ondataavailable = (e) => {
console.log("client: sent data to websocket");
socket.emit("packet-sent", e.data);
};
}
async function closeMicrophone(microphone) {
microphone.stop();
}
async function startRecording(socket) {
const listenButton = document.getElementById("record");
let microphone;
console.log("client: waiting to open microphone");
listenButton.addEventListener("click", async () => {
if (!microphone) {
// open and close the microphone
microphone = await getMicrophone();
await openMicrophone(microphone, socket);
} else {
await closeMicrophone(microphone);
microphone = undefined;
}
});
}
async function startRadio(socket) {
const listenButton = document.getElementById("record");
let radio;
listenButton.addEventListener("click", async () => {
if (!radio) {
radio = "ON"
await startStreaming(socket);
} else {
await stopStreaming(socket);
radio = undefined;
}
})
}
async function startStreaming(socket) {
socket.emit("startStream");
}
async function stopStreaming(socket) {
socket.emit("stopStream");
}
window.addEventListener("load", () => {
const socket = io((options = { transports: ["websocket"] }));
socket.on("connect", async () => {
console.log("client: connected to websocket");
// await startRecording(socket);
await startRadio(socket);
});
socket.on("transcript", (transcript) => {
captions.innerHTML = transcript ? `<span>${transcript}</span>` : "";
});
// Subscribe to translations
// socket.emit('subscribe', 'de');
socket.on('translated', (translated) => {
console.log(`Received translation: ${translated}`);
translations.innerHTML = translated ? `<span>${translated}</span>` : "";
});
languages.addEventListener("change", function() {
let selectedLang = languages.options[languages.selectedIndex].value;
if (selectedLang != currentLang) {
socket.emit('unsubscribe', currentLang);
currentLang = selectedLang;
}
console.log(`Selected language: ${selectedLang}`);
socket.emit("subscribe", selectedLang);
})
});