Small fixes for speech recognition

This commit is contained in:
Niek van der Maas 2023-03-07 16:48:20 +01:00
parent 988b08a93c
commit 6c71422bdc
1 changed files with 20 additions and 18 deletions

View File

@ -63,15 +63,30 @@
// Try to detect speech recognition support // Try to detect speech recognition support
if ("SpeechRecognition" in window) { if ("SpeechRecognition" in window) {
// @ts-ignore
recognition = new SpeechRecognition(); recognition = new SpeechRecognition();
} else if ("webkitSpeechRecognition" in window) { } else if ("webkitSpeechRecognition" in window) {
// @ts-ignore
recognition = new webkitSpeechRecognition(); recognition = new webkitSpeechRecognition();
} else {
console.log("Speech recognition not supported");
recognition = null;
} }
recognition!.interimResults = false; if (recognition) {
recognition.interimResults = false;
recognition.onstart = () => {
recording = true;
};
recognition.onresult = (event) => {
// Stop speech recognition, submit the form and remove the pulse
const last = event.results.length - 1;
const text = event.results[last][0].transcript;
input.value = text;
recognition.stop();
recording = false;
submitForm(true);
};
} else {
console.log("Speech recognition not supported");
}
}); });
// Scroll to the bottom of the chat on update // Scroll to the bottom of the chat on update
@ -231,24 +246,11 @@
}; };
const recordToggle = () => { const recordToggle = () => {
// Check if already recording - if so, stop // Check if already recording - if so, stop - else start
if (recording) { if (recording) {
recognition?.stop(); recognition?.stop();
recording = false; recording = false;
} else { } else {
// Mark as recording
recording = true;
// Start speech recognition
recognition!.onresult = (event) => {
// Stop speech recognition, submit the form and remove the pulse
const last = event.results.length - 1;
const text = event.results[last][0].transcript;
input.value = text;
recognition.stop();
recording = false;
submitForm(true);
};
recognition?.start(); recognition?.start();
} }
}; };