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 = `
Transcription: ${data.transcription}
Emotions: ${JSON.stringify(data.emotions)}
Advice: ${data.advice_text}
`; }) .catch(err => console.error(err)); }