Peiiiiiiiiru's picture
Create static/script.js
5b8529f verified
let mediaRecorder;
let audioChunks = [];
function startRecording() {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
document.getElementById("audioPlayback").src = URL.createObjectURL(audioBlob);
audioChunks = [];
};
});
}
function stopRecording() {
mediaRecorder.stop();
}
function uploadAudio() {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
const formData = new FormData();
formData.append("file", audioBlob, "recording.wav");
fetch("/upload_audio", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
document.getElementById("result").innerHTML = `
<p><strong>Transcription:</strong> ${data.transcription}</p>
<p><strong>Emotions:</strong> ${JSON.stringify(data.emotions)}</p>
<p><strong>Advice:</strong> ${data.advice_text}</p>
<audio controls src="${data.advice_audio}"></audio>
`;
})
.catch(err => console.error(err));
}