File size: 2,291 Bytes
47c8182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e119305
 
47c8182
 
 
e119305
47c8182
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Voice Chat Interface</title>
</head>
<body>
    <h1>Voice Chat</h1>
    <button id="recordButton">Start Listening</button>
    <button id="generateButton">Generate Speech</button>
    <audio id="audioPlayer" controls></audio>

    <script>
        let mediaRecorder;
        let audioChunks = [];
        
        document.getElementById("recordButton").addEventListener("click", async () => {
            if (!mediaRecorder || mediaRecorder.state === "inactive") {
                const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                mediaRecorder = new MediaRecorder(stream);
                mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
                mediaRecorder.onstop = async () => {
                    const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
                    const formData = new FormData();
                    formData.append("file", audioBlob, "recording.wav");
                    
                    const response = await fetch("/chat/", {
                        method: "POST",
                        body: formData
                    });
                    const audioData = await response.blob();
                    document.getElementById("audioPlayer").src = URL.createObjectURL(audioData);
                };
                audioChunks = [];
                mediaRecorder.start();
                setTimeout(() => mediaRecorder.stop(), 5000);
            }
        });
        
        document.getElementById("generateButton").addEventListener("click", async () => {
            const inputText = prompt("Enter text to convert to speech:");
            if (inputText) {
                const response = await fetch("/tts/", {
                    method: "POST",
                    headers: { "Content-Type": "application/json" },
                    body: JSON.stringify({ text:inputText})
                });
                const audioData = await response.blob();
                document.getElementById("audioPlayer").src = URL.createObjectURL(audioData);
            }
        });
    </script>
</body>
</html>