File size: 2,549 Bytes
83a6319
 
1a08d75
 
 
83a6319
 
1a08d75
83a6319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<head>
<title>Whisper, Vicuna, & TTS Demo</title>
</head>
<body>
<div id="gradio-container"></div>
<script src="/js/index.js"></script>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
let recorder;
let chunks =[];
let audioComponent;
let recognition; // Speech recognition object

window.addEventListener('DOMContentLoaded', (event) => {
    audioComponent = document.getElementById("mic_audio");

    // Initialize speech recognition
    recognition = new webkitSpeechRecognition() || new SpeechRecognition(); // Cross-browser compatibility
    recognition.continuous = true; // Keep listening
    recognition.interimResults = false; // Don't return partial results

    recognition.onresult = event => {
        const transcript = event.results.transcript.toLowerCase(); // Get the recognized text
        console.log("Recognized:", transcript); // For debugging

        if (transcript.includes("start recording")) { // Your "start" command
            startRecording();
        } else if (transcript.includes("stop recording")) { // Your "stop" command
            stopRecording();
        }
    };

    recognition.onerror = event => {
        console.error("Speech recognition error:", event.error);
    };

    recognition.start(); // Start listening for voice commands immediately

});

function startRecording() {
    navigator.mediaDevices.getUserMedia({ audio: true })
      .then(stream => {
            recorder = new MediaRecorder(stream);
            recorder.ondataavailable = e => {
                if (e.data.size > 0) {
                    chunks.push(e.data);
                }
            };
            recorder.start();
            console.log("Recording started.");
        })
      .catch(err => console.error('Error accessing microphone:', err));
}

function stopRecording() {
    if (recorder && recorder.state === "recording") {
        recorder.stop();
        recorder.onstop = async () => {
          const blob = new Blob(chunks, { type: 'audio/wav' }); // Or appropriate type
          const file = new File([blob], "recording.wav", { type: 'audio/wav' }); // Create a File object

          const dataTransfer = new DataTransfer();
          dataTransfer.items.add(file);
          audioComponent.files = dataTransfer.files; // Set the files property

          audioComponent.dispatchEvent(new Event('change')); // Trigger change to send data

          chunks =;
          console.log("Recording stopped and data sent.");
        };
    }
}

});

</script>
</body>
</html>