File size: 2,708 Bytes
97e7d36
 
 
6831f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Godot Export with Audio Recording</title>
</head>

<body>
    <h1>Godot Export with Microphone Recording</h1>

    <!-- Iframe for the Godot export -->
    <iframe src="godot/index.html" width="800" height="600" frameborder="0"></iframe>

    <script>
        // URL of the server to send audio chunks
        const serverUrl = "./api/process"

        // Check if browser supports audio recording
        if (!navigator.mediaDevices?.getUserMedia) {
            console.error('Your browser does not support audio recording.')
            alert('Your browser does not support audio recording. Please try using a modern browser like Chrome, Firefox, or Edge.')
            throw new Error('Audio recording not supported')
        }

        // Set up audio recording
        navigator.mediaDevices.getUserMedia({ audio: true })
            .then(stream => {
                const audioContext = new (window.AudioContext || window.webkitAudioContext)()
                const mediaRecorder = new MediaRecorder(stream)
                const audioChunks = []

                mediaRecorder.ondataavailable = event => {
                    audioChunks.push(event.data)
                }

                mediaRecorder.onstop = () => {
                    const audioBlob = new Blob(audioChunks, { type: 'audio/webm' })
                    audioChunks.length = 0 // Clear chunks after creating the Blob

                    // Send the audio chunk to the server
                    const formData = new FormData()
                    formData.append('audio', audioBlob)

                    fetch(serverUrl, {
                        method: 'POST',
                        body: formData
                    }).then(response => {
                        console.log('Audio chunk sent successfully')
                    }).catch(error => {
                        console.error('Error sending audio chunk:', error)
                    })
                }

                // Start recording in intervals
                const chunkInterval = 300 // Chunk duration in milliseconds
                setInterval(() => {
                    if (mediaRecorder.state === 'recording') {
                        mediaRecorder.stop()
                        mediaRecorder.start()
                    } else {
                        mediaRecorder.start()
                    }
                }, chunkInterval)
            })
            .catch(error => {
                console.error('Error accessing microphone:', error)
            });
    </script>
</body>

</html>