Spaces:
Sleeping
Sleeping
Update static/js/script.js
Browse files- static/js/script.js +49 -27
static/js/script.js
CHANGED
@@ -2,8 +2,15 @@ let isRecording = false;
|
|
2 |
let mediaRecorder;
|
3 |
let recordedChunks = [];
|
4 |
|
5 |
-
//
|
6 |
-
document.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
if (!isRecording) {
|
8 |
startRecording();
|
9 |
} else {
|
@@ -14,19 +21,29 @@ document.getElementById('recordButton').addEventListener('click', function () {
|
|
14 |
function startRecording() {
|
15 |
navigator.mediaDevices.getUserMedia({ audio: true })
|
16 |
.then(function (stream) {
|
17 |
-
|
18 |
-
mediaRecorder
|
19 |
|
20 |
mediaRecorder.ondataavailable = function (e) {
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
};
|
23 |
|
24 |
-
|
25 |
-
document.getElementById('recordButton').textContent = 'Stop';
|
26 |
-
document.getElementById('recordButton').classList.add('recording');
|
27 |
-
document.getElementById('recordStatus').textContent = 'Recording...';
|
28 |
-
document.getElementById('transcribeContainer').style.display = 'none'; // Hide Transcribe button initially
|
29 |
-
isRecording = true;
|
30 |
})
|
31 |
.catch(function (err) {
|
32 |
console.error('The following error occurred: ' + err);
|
@@ -34,40 +51,45 @@ function startRecording() {
|
|
34 |
}
|
35 |
|
36 |
function stopRecording() {
|
37 |
-
mediaRecorder
|
|
|
|
|
38 |
|
39 |
mediaRecorder.onstop = function () {
|
40 |
-
const blob = new Blob(recordedChunks, { type: 'audio/
|
41 |
recordedChunks = [];
|
42 |
const audioURL = window.URL.createObjectURL(blob);
|
43 |
|
44 |
-
// Update
|
45 |
-
|
46 |
-
|
47 |
-
document.getElementById('recordStatus').textContent = 'Tap to Record';
|
48 |
-
document.getElementById('transcribeContainer').style.display = 'block'; // Show Transcribe button
|
49 |
|
50 |
-
//
|
51 |
-
|
52 |
-
|
53 |
-
});
|
54 |
|
|
|
|
|
55 |
isRecording = false;
|
56 |
};
|
57 |
}
|
58 |
|
59 |
-
function
|
|
|
|
|
|
|
|
|
|
|
60 |
const formData = new FormData();
|
61 |
-
formData.append('audio', blob, 'audio.
|
62 |
|
63 |
-
|
64 |
-
fetch('https://jikoni-semabox.hf.space/transcribe', { // https://tri4-semalab.hf.space/transcribe
|
65 |
method: 'POST',
|
66 |
body: formData
|
67 |
})
|
68 |
.then(response => response.json())
|
69 |
.then(data => {
|
70 |
-
|
71 |
})
|
72 |
.catch(error => {
|
73 |
console.error('Error:', error);
|
|
|
2 |
let mediaRecorder;
|
3 |
let recordedChunks = [];
|
4 |
|
5 |
+
// Get elements
|
6 |
+
const micContainer = document.getElementsByClassName('mic-container')[0];
|
7 |
+
const circle = micContainer.getElementsByClassName('circle')[0];
|
8 |
+
const outputContainer = document.getElementById('output');
|
9 |
+
const audioPlayerContainer = document.getElementById('audioPlayerContainer');
|
10 |
+
const audioPlayer = document.getElementById('audioPlayer');
|
11 |
+
|
12 |
+
// Handle click event for the microphone container
|
13 |
+
micContainer.addEventListener('click', function () {
|
14 |
if (!isRecording) {
|
15 |
startRecording();
|
16 |
} else {
|
|
|
21 |
function startRecording() {
|
22 |
navigator.mediaDevices.getUserMedia({ audio: true })
|
23 |
.then(function (stream) {
|
24 |
+
// Create a MediaRecorder instance with MIME type 'audio/wav'
|
25 |
+
mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/wav' });
|
26 |
|
27 |
mediaRecorder.ondataavailable = function (e) {
|
28 |
+
if (e.data.size > 0) {
|
29 |
+
recordedChunks.push(e.data);
|
30 |
+
}
|
31 |
+
};
|
32 |
+
|
33 |
+
mediaRecorder.onstart = function () {
|
34 |
+
// Update UI for recording
|
35 |
+
circle.classList.add('active');
|
36 |
+
outputContainer.textContent = 'Recording...';
|
37 |
+
audioPlayerContainer.style.display = 'none'; // Hide audio player initially
|
38 |
+
isRecording = true;
|
39 |
+
};
|
40 |
+
|
41 |
+
mediaRecorder.onerror = function (e) {
|
42 |
+
console.error('Error recording audio:', e);
|
43 |
+
stopRecording(); // Stop recording on error
|
44 |
};
|
45 |
|
46 |
+
mediaRecorder.start();
|
|
|
|
|
|
|
|
|
|
|
47 |
})
|
48 |
.catch(function (err) {
|
49 |
console.error('The following error occurred: ' + err);
|
|
|
51 |
}
|
52 |
|
53 |
function stopRecording() {
|
54 |
+
if (mediaRecorder) {
|
55 |
+
mediaRecorder.stop();
|
56 |
+
}
|
57 |
|
58 |
mediaRecorder.onstop = function () {
|
59 |
+
const blob = new Blob(recordedChunks, { type: 'audio/wav' });
|
60 |
recordedChunks = [];
|
61 |
const audioURL = window.URL.createObjectURL(blob);
|
62 |
|
63 |
+
// Update UI after recording
|
64 |
+
circle.classList.remove('active');
|
65 |
+
outputContainer.textContent = 'Click to Transcribe';
|
|
|
|
|
66 |
|
67 |
+
// Display audio player
|
68 |
+
audioPlayer.src = audioURL;
|
69 |
+
audioPlayerContainer.style.display = 'block';
|
|
|
70 |
|
71 |
+
// Handle transcription
|
72 |
+
outputContainer.addEventListener('click', handleTranscribeClick, { once: true });
|
73 |
isRecording = false;
|
74 |
};
|
75 |
}
|
76 |
|
77 |
+
function handleTranscribeClick() {
|
78 |
+
transcribeAudio();
|
79 |
+
}
|
80 |
+
|
81 |
+
function transcribeAudio() {
|
82 |
+
const blob = new Blob(recordedChunks, { type: 'audio/wav' });
|
83 |
const formData = new FormData();
|
84 |
+
formData.append('audio', blob, 'audio.wav');
|
85 |
|
86 |
+
fetch('https://jikoni-semabox.hf.space/transcribe', {
|
|
|
87 |
method: 'POST',
|
88 |
body: formData
|
89 |
})
|
90 |
.then(response => response.json())
|
91 |
.then(data => {
|
92 |
+
outputContainer.textContent = `Transcription: ${data.transcription || 'No transcription available'}`;
|
93 |
})
|
94 |
.catch(error => {
|
95 |
console.error('Error:', error);
|