Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,16 @@ from io import BytesIO
|
|
| 6 |
from tempfile import NamedTemporaryFile
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
-
# JavaScript for recording audio
|
| 10 |
def record_audio_js():
|
| 11 |
js_code = """
|
| 12 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
async function recordAudio() {
|
|
|
|
|
|
|
| 14 |
const div = document.createElement('div');
|
| 15 |
const audio = document.createElement('audio');
|
| 16 |
const startButton = document.createElement('button');
|
|
@@ -22,38 +27,45 @@ def record_audio_js():
|
|
| 22 |
div.appendChild(startButton);
|
| 23 |
div.appendChild(audio);
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
startButton.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
recorder.ondataavailable = (event) => chunks.push(event.data);
|
| 45 |
-
recorder.onstop = async () => {
|
| 46 |
-
const blob = new Blob(chunks, { type: 'audio/wav' });
|
| 47 |
-
const reader = new FileReader();
|
| 48 |
-
reader.onloadend = function () {
|
| 49 |
-
const base64String = btoa(new Uint8Array(reader.result).reduce((data, byte) => data + String.fromCharCode(byte), ''));
|
| 50 |
-
window.parent.postMessage({func: "receiveAudio", data: base64String}, "*");
|
| 51 |
};
|
| 52 |
-
reader.readAsArrayBuffer(blob);
|
| 53 |
-
};
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
window.addEventListener('message', (event) => {
|
|
@@ -61,6 +73,12 @@ def record_audio_js():
|
|
| 61 |
recordAudio();
|
| 62 |
}
|
| 63 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
</script>
|
| 65 |
<button onclick="window.postMessage({func: 'startRecording'}, '*')">Record Audio</button>
|
| 66 |
"""
|
|
|
|
| 6 |
from tempfile import NamedTemporaryFile
|
| 7 |
import numpy as np
|
| 8 |
|
|
|
|
| 9 |
def record_audio_js():
|
| 10 |
js_code = """
|
| 11 |
<script>
|
| 12 |
+
function logMessage(message) {
|
| 13 |
+
console.log(message);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
async function recordAudio() {
|
| 17 |
+
logMessage('Starting audio recording...');
|
| 18 |
+
|
| 19 |
const div = document.createElement('div');
|
| 20 |
const audio = document.createElement('audio');
|
| 21 |
const startButton = document.createElement('button');
|
|
|
|
| 27 |
div.appendChild(startButton);
|
| 28 |
div.appendChild(audio);
|
| 29 |
|
| 30 |
+
try {
|
| 31 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 32 |
+
let recorder = new MediaRecorder(stream);
|
| 33 |
+
audio.style.display = 'block';
|
| 34 |
+
audio.srcObject = stream;
|
| 35 |
+
audio.controls = true;
|
| 36 |
+
audio.muted = true;
|
| 37 |
+
|
| 38 |
+
startButton.onclick = async () => {
|
| 39 |
+
logMessage('Start button clicked');
|
| 40 |
+
startButton.disabled = true;
|
| 41 |
+
stopButton.style.display = 'inline';
|
| 42 |
+
recorder.start();
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
stopButton.onclick = async () => {
|
| 46 |
+
logMessage('Stop button clicked');
|
| 47 |
+
stopButton.disabled = true;
|
| 48 |
+
recorder.stop();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
};
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
let chunks = [];
|
| 52 |
+
recorder.ondataavailable = (event) => chunks.push(event.data);
|
| 53 |
+
recorder.onstop = async () => {
|
| 54 |
+
logMessage('Recording stopped');
|
| 55 |
+
const blob = new Blob(chunks, { type: 'audio/wav' });
|
| 56 |
+
const reader = new FileReader();
|
| 57 |
+
reader.onloadend = function () {
|
| 58 |
+
const base64String = btoa(new Uint8Array(reader.result).reduce((data, byte) => data + String.fromCharCode(byte), ''));
|
| 59 |
+
window.parent.postMessage({ func: "receiveAudio", data: base64String }, "*");
|
| 60 |
+
};
|
| 61 |
+
reader.readAsArrayBuffer(blob);
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
stream.getAudioTracks()[0].stop();
|
| 65 |
+
div.remove();
|
| 66 |
+
} catch (error) {
|
| 67 |
+
logMessage('Error: ' + error);
|
| 68 |
+
}
|
| 69 |
}
|
| 70 |
|
| 71 |
window.addEventListener('message', (event) => {
|
|
|
|
| 73 |
recordAudio();
|
| 74 |
}
|
| 75 |
});
|
| 76 |
+
|
| 77 |
+
window.addEventListener('load', () => {
|
| 78 |
+
document.querySelector('button').addEventListener('click', () => {
|
| 79 |
+
window.postMessage({func: 'startRecording'}, '*');
|
| 80 |
+
});
|
| 81 |
+
});
|
| 82 |
</script>
|
| 83 |
<button onclick="window.postMessage({func: 'startRecording'}, '*')">Record Audio</button>
|
| 84 |
"""
|