File size: 1,424 Bytes
5b8529f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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));
}