From 6c71422bdccf8d243bbc73fffca032599e7dd502 Mon Sep 17 00:00:00 2001 From: Niek van der Maas Date: Tue, 7 Mar 2023 16:48:20 +0100 Subject: [PATCH] Small fixes for speech recognition --- src/lib/Chat.svelte | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/lib/Chat.svelte b/src/lib/Chat.svelte index 49d20ca..9b7a275 100644 --- a/src/lib/Chat.svelte +++ b/src/lib/Chat.svelte @@ -63,15 +63,30 @@ // Try to detect speech recognition support if ("SpeechRecognition" in window) { + // @ts-ignore recognition = new SpeechRecognition(); } else if ("webkitSpeechRecognition" in window) { + // @ts-ignore 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 @@ -231,24 +246,11 @@ }; const recordToggle = () => { - // Check if already recording - if so, stop + // Check if already recording - if so, stop - else start if (recording) { recognition?.stop(); recording = false; } 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(); } };