Spaces:
Sleeping
Sleeping
Create js/script.js
Browse files- static/js/script.js +67 -0
static/js/script.js
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
let isRecording = false;
|
2 |
+
let mediaRecorder;
|
3 |
+
let recordedChunks = [];
|
4 |
+
|
5 |
+
document.getElementById('recordButton').addEventListener('click', async function () {
|
6 |
+
if (!isRecording) {
|
7 |
+
startRecording();
|
8 |
+
} else {
|
9 |
+
stopRecording();
|
10 |
+
}
|
11 |
+
});
|
12 |
+
|
13 |
+
function startRecording() {
|
14 |
+
navigator.mediaDevices.getUserMedia({ audio: true })
|
15 |
+
.then(function (stream) {
|
16 |
+
mediaRecorder = new MediaRecorder(stream);
|
17 |
+
mediaRecorder.start();
|
18 |
+
|
19 |
+
mediaRecorder.ondataavailable = function (e) {
|
20 |
+
recordedChunks.push(e.data);
|
21 |
+
};
|
22 |
+
|
23 |
+
document.getElementById('recordButton').textContent = 'Stop';
|
24 |
+
document.getElementById('recordStatus').textContent = 'Recording...';
|
25 |
+
isRecording = true;
|
26 |
+
})
|
27 |
+
.catch(function (err) {
|
28 |
+
console.log('The following error occurred: ' + err);
|
29 |
+
});
|
30 |
+
}
|
31 |
+
|
32 |
+
function stopRecording() {
|
33 |
+
mediaRecorder.stop();
|
34 |
+
|
35 |
+
mediaRecorder.onstop = function () {
|
36 |
+
const blob = new Blob(recordedChunks, { type: 'audio/webm' });
|
37 |
+
const audioURL = window.URL.createObjectURL(blob);
|
38 |
+
recordedChunks = [];
|
39 |
+
|
40 |
+
document.getElementById('recordButton').textContent = 'Start';
|
41 |
+
document.getElementById('recordStatus').textContent = 'Tap to Record';
|
42 |
+
document.getElementById('transcribeContainer').style.display = 'block';
|
43 |
+
|
44 |
+
document.getElementById('transcribeButton').addEventListener('click', function () {
|
45 |
+
transcribeAudio(blob);
|
46 |
+
});
|
47 |
+
|
48 |
+
isRecording = false;
|
49 |
+
};
|
50 |
+
}
|
51 |
+
|
52 |
+
function transcribeAudio(blob) {
|
53 |
+
const formData = new FormData();
|
54 |
+
formData.append('audio', blob, 'audio.webm');
|
55 |
+
|
56 |
+
fetch('/transcribe', {
|
57 |
+
method: 'POST',
|
58 |
+
body: formData
|
59 |
+
})
|
60 |
+
.then(response => response.json())
|
61 |
+
.then(data => {
|
62 |
+
document.getElementById('output').textContent = data.transcription;
|
63 |
+
})
|
64 |
+
.catch(error => {
|
65 |
+
console.error('Error:', error);
|
66 |
+
});
|
67 |
+
}
|