-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
79 lines (62 loc) · 2.6 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js" integrity="sha384-2huaZvOR9iDzHqslqwpR87isEmrfxqyWOF7hr7BY6KG0+hVKLoEXMPUJw3ynWuhO" crossorigin="anonymous"></script>
<script src="./recognition.js"></script>
<title>Document</title>
</head>
<body>
<button id="record-audio-btn">Record</button>
<button id="stop-recording-btn">Stop</button>
<p id="output"></p>
<script>
const socket = io("ws://localhost:3000");
const output = document.getElementById("output");
const recordBtn = document.getElementById("record-audio-btn");
const stopRecrdingBtn = document.getElementById("stop-recording-btn");
const recognition = new SpeechRecognizer(output)
if(stopRecrdingBtn) {
stopRecrdingBtn.disabled = true;
}
let mediaRecorder;
let audioChunks = [];
recordBtn.onclick = () => {
recordBtn.disabled = true;
stopRecrdingBtn.disabled = false;
navigator.mediaDevices
.getUserMedia({ audio: true })
.then((stream) => {
mediaRecorder = new MediaRecorder(stream);
recognition.start()
mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
const reader = new FileReader();
reader.readAsDataURL(audioBlob);
reader.onloadend = () => {
const base64data = reader.result;
socket.emit("transcribe", base64data);
console.log("********** Audio Sent **************");
};
};
mediaRecorder.start();
console.log("********** Audio Rec started **************");
})
.catch((err) => console.log("ERROR: >", err));
};
stopRecrdingBtn.onclick = () => {
recordBtn.disabled = false;
recognition.stop()
console.log("************ stopped ***********");
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
audioChunks = [];
}
};
</script>
</body>
</html>