From a7420ef4e7fb656057fe497cdbed3fce57efa721 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 11 Nov 2023 13:03:54 -0800 Subject: [PATCH] feat: speech recognition support --- src/routes/+page.svelte | 150 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index e44a61fbb..30ca753f9 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -25,7 +25,12 @@ system: null, temperature: null }; - let speechRecognition = true; + + let fileUploadEnabled = false; + + let speechRecognition; + let speechRecognitionEnabled = true; + let speechRecognitionListening = false; let models = []; let chats = []; @@ -187,6 +192,63 @@ } }; + const speechRecognitionHandler = () => { + // Check if SpeechRecognition is supported + + if (speechRecognitionListening) { + speechRecognition.stop(); + } else { + if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) { + // Create a SpeechRecognition object + speechRecognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); + + // Set continuous to true for continuous recognition + speechRecognition.continuous = true; + + // Set the timeout for turning off the recognition after inactivity (in milliseconds) + const inactivityTimeout = 3000; // 3 seconds + + let timeoutId; + // Start recognition + speechRecognition.start(); + speechRecognitionListening = true; + + // Event triggered when speech is recognized + speechRecognition.onresult = function (event) { + // Clear the inactivity timeout + clearTimeout(timeoutId); + + // Handle recognized speech + console.log(event); + const transcript = event.results[Object.keys(event.results).length - 1][0].transcript; + prompt = `${prompt}${transcript}`; + + // Restart the inactivity timeout + timeoutId = setTimeout(() => { + console.log('Speech recognition turned off due to inactivity.'); + speechRecognition.stop(); + }, inactivityTimeout); + }; + + // Event triggered when recognition is ended + speechRecognition.onend = function () { + // Restart recognition after it ends + console.log('recognition ended'); + speechRecognitionListening = false; + }; + + // Event triggered when an error occurs + speechRecognition.onerror = function (event) { + console.log(event); + toast.error(`Speech recognition error: ${event.error}`); + speechRecognitionListening = false; + }; + } else { + toast.error('SpeechRecognition API is not supported in this browser.'); + } + } + }; + ////////////////////////// // Web functions ////////////////////////// @@ -1200,10 +1262,9 @@ >