### Install Language Models Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Example demonstrating how to install language models for speech recognition. ```javascript // Install language models // ... implementation details ... ``` -------------------------------- ### SpeechRecognitionOptions Example with All Fields Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md An example demonstrating how to instantiate SpeechRecognitionOptions with all fields set. ```javascript const options = { langs: ['en-US', 'fr-FR', 'de-DE'], processLocally: true, quality: 'dictation' }; const status = await SpeechRecognition.available(options); ``` -------------------------------- ### Install On-Device Speech Recognition Resources (JavaScript) Source: https://github.com/webaudio/web-speech-api/blob/main/explainers/on-device-speech-recognition.md Call this method to install necessary resources for on-device speech recognition. It returns a promise that resolves to a boolean indicating success or failure of the installation. ```javascript const options = { langs: ['en-US'], processLocally: true }; SpeechRecognition.install(options).then((success) => { if (success) { console.log(`On-device speech recognition resources for ${options.langs.join(', ')} installed successfully.`); } else { console.error(`Unable to install on-device speech recognition resources for ${options.langs.join(', ')}. This could be due to unsupported languages or download issues.`); } }); ``` -------------------------------- ### JavaScript Usage Example for Progressive Enhancement Source: https://github.com/webaudio/web-speech-api/blob/main/explainers/quality-levels.md Demonstrates how to implement progressive enhancement for on-device speech recognition, checking availability, attempting installation, and falling back to cloud-based recognition if necessary. ```javascript const meetConfig = { langs: ['en-US'], processLocally: true, // We specifically need a model capable of handling a meeting environment quality: 'conversation' }; async function setupSpeech() { // 1. Check if a high-quality model is already available const availability = await SpeechRecognition.available(meetConfig); if (availability == 'available') { startOnDeviceSpeech(); return; } // 2. If not, try to install one try { await SpeechRecognition.install(meetConfig); startOnDeviceSpeech(); } catch (e) { // 3. Fallback: Device hardware is too weak for "conversation" quality, // or user denied the download. Fallback to Cloud to ensure transcript usability. console.log("High-quality on-device model unavailable. Using Cloud."); startCloudSpeech(); } } ``` -------------------------------- ### Interpreting 'available' Status Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md Example showing how to proceed when speech recognition is 'available'. This indicates that the recognition model is installed and ready for use, whether locally or remotely. ```javascript const status = await SpeechRecognition.available({ langs: ['en-US'], processLocally: true }); if (status === 'available') { console.log('Ready to use local speech recognition'); recognition.processLocally = true; recognition.start(); } ``` -------------------------------- ### Voice Chat Bot Implementation Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md A combined example for implementing a voice-enabled chatbot. ```javascript // Voice chat bot implementation // ... implementation details ... ``` -------------------------------- ### Install Speech Recognition Models Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md Install necessary models for speech recognition with specified options. ```javascript SpeechRecognition.install(options) ``` -------------------------------- ### Handle Audio Capture Start Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Fired when the user agent has started to capture audio. Use this to indicate recording has begun. ```javascript recognition.onaudiostart = () => { console.log('Audio capture started'); showRecordingIndicator(); }; ``` -------------------------------- ### Basic Speech Recognition Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md A fundamental example demonstrating how to perform basic speech recognition. ```javascript // Basic voice search example // ... implementation details ... ``` -------------------------------- ### Mark and Boundary Events Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Example for handling mark and boundary events during speech synthesis. ```javascript // Mark and boundary events // ... implementation details ... ``` -------------------------------- ### Basic Text-to-Speech Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md A simple example for basic text-to-speech functionality. ```javascript // Basic text-to-speech // ... implementation details ... ``` -------------------------------- ### Form Field with Voice Input Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Example showing how to integrate voice input into form fields. ```javascript // Form field with voice input // ... implementation details ... ``` -------------------------------- ### start() - Microphone Input Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Starts speech recognition directly from the device microphone. This method requests microphone permission and fires events to indicate the recognition status. ```APIDOC ## start() ### Description Starts speech recognition directly from the device microphone. ### Method `start()` ### Parameters None ### Return Type `undefined` ### Throws - `InvalidStateError`: If recognition has already been started and no error or end event has fired - `InvalidStateError`: If the document is not fully active - `InvalidStateError`: If a `MediaStreamTrack` is required but not provided ### Behavior 1. Requests microphone permission from the user 2. If permission is denied, fires an `error` event with `not-allowed` code 3. If local processing is required and unavailable, fires an `error` event with `service-not-allowed` code 4. If contextual biasing is used but not supported, fires an `error` event with `phrases-not-supported` code 5. Once the system begins listening, fires a `start` event ### Example ```javascript const recognition = new SpeechRecognition(); recognition.lang = 'en-US'; recognition.continuous = false; recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log('You said:', transcript); }; recognition.onerror = (event) => { console.error('Error:', event.error); }; // Start listening for speech recognition.start(); ``` ``` -------------------------------- ### Language Selection UI Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Example for implementing a user interface to select the recognition language. ```javascript // Language selection UI // ... implementation details ... ``` -------------------------------- ### Install Speech Recognition Language Packs Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md This snippet attempts to install speech recognition language packs for the specified languages. It returns true if all installations succeed or languages were already installed, and false otherwise. Note that `processLocally` is not used in this method. ```javascript const options = { langs: ['en-US', 'es-ES'], quality: 'command' }; const installed = await SpeechRecognition.install(options); if (installed) { console.log('Language packs ready'); recognition.processLocally = true; } else { console.log('Installation failed or not all languages supported'); } ``` -------------------------------- ### Pause and Resume Speech Synthesis Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Example showing how to pause and resume speech synthesis playback. ```javascript // Pause and resume functionality // ... implementation details ... ``` -------------------------------- ### SpeechRecognition.install Source: https://github.com/webaudio/web-speech-api/blob/main/explainers/on-device-speech-recognition.md Installs the resources required for speech recognition matching the provided SpeechRecognitionOptions. The installation process may download and configure necessary language models. ```APIDOC ## SpeechRecognition.install ### Description This method installs the resources required for speech recognition matching the provided `SpeechRecognitionOptions`. The installation process may download and configure necessary language models. ### Method `Promise install(SpeechRecognitionOptions options)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **options** (`SpeechRecognitionOptions`) - Required - Options to specify desired speech recognition capabilities, such as languages and whether to process locally. ### Request Example ```javascript // Install on-device resources for English (US) const options = { langs: ['en-US'], processLocally: true }; SpeechRecognition.install(options).then((success) => { if (success) { console.log(`On-device speech recognition resources for ${options.langs.join(', ')} installed successfully.`); } else { console.error(`Unable to install on-device speech recognition resources for ${options.langs.join(', ')}. This could be due to unsupported languages or download issues.`); } }); ``` ### Response #### Success Response (200) - **success** (`boolean`) - True if the installation was successful, false otherwise. #### Response Example ```json true ``` ``` -------------------------------- ### Start Speech Recognition (Microphone) Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Starts speech recognition directly from the device microphone. Handles permission requests and fires events for start, result, and errors. ```javascript const recognition = new SpeechRecognition(); recognition.lang = 'en-US'; recognition.continuous = false; recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log('You said:', transcript); }; recognition.onerror = (event) => { console.error('Error:', event.error); }; // Start listening for speech recognition.start(); ``` -------------------------------- ### Voice Selection and Switching Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Demonstrates how to select and switch between different available voices for speech synthesis. ```javascript // Voice selection and switching // ... implementation details ... ``` -------------------------------- ### Interpreting 'downloadable' Status Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md Example of handling the 'downloadable' status, indicating that an on-device speech recognition model can be installed. This is relevant when `processLocally` is true and the model is not yet present. ```javascript const status = await SpeechRecognition.available({ langs: ['en-GB'], processLocally: true }); if (status === 'downloadable') { console.log('British English model available for download'); const installed = await SpeechRecognition.install({ langs: ['en-GB'] }); if (installed) { console.log('Model installed successfully'); } } ``` -------------------------------- ### audiostart Event Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Fired when the user agent has started to capture audio. This event indicates the beginning of the audio input process. ```APIDOC ## audiostart ### Description Fired when the user agent has started to capture audio. ### Event Type `Event` ### Example ```javascript recognition.onaudiostart = () => { console.log('Audio capture started'); showRecordingIndicator(); }; ``` ``` -------------------------------- ### SpeechRecognition.install Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Attempts to install speech recognition language packs for all specified languages. It returns a promise that resolves to a boolean indicating whether all installations were successful. ```APIDOC ## static install(options) ### Description Attempts to install speech recognition language packs for all specified languages. ### Method static ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **options** (SpeechRecognitionOptions) - Required - Configuration with languages to install ### Request Example ```javascript const options = { langs: ['en-US', 'es-ES'], quality: 'command' }; const installed = await SpeechRecognition.install(options); if (installed) { console.log('Language packs ready'); recognition.processLocally = true; } else { console.log('Installation failed or not all languages supported'); } ``` ### Response #### Success Response (200) - **boolean** - `true` if all installations succeed, `false` otherwise #### Response Example ```json true ``` ### Throws - `InvalidStateError`: If the document is not fully active - `SyntaxError`: If any language tag in `options.langs` is not a valid BCP 47 tag ### Permissions Gated behind the "on-device-speech-recognition" policy-controlled feature with a default allowlist of `['self']` ``` -------------------------------- ### Voice Commands Recognition Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Demonstrates recognizing specific voice commands. ```javascript // Voice commands recognition // ... implementation details ... ``` -------------------------------- ### Get and Log All Available Voices Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-voice.md Retrieves all available speech synthesis voices and logs their URI and name to the console. This is a basic example to inspect available voices. ```javascript const synth = window.speechSynthesis; const voices = synth.getVoices(); voices.forEach((voice) => { console.log('Voice URI:', voice.voiceURI); console.log('Voice Name:', voice.name); }); // To use a voice, don't interact with voiceURI directly. // Instead, set the voice object itself: const utterance = new SpeechSynthesisUtterance('Hello'); utterance.voice = voices[0]; // Pass the voice object ``` -------------------------------- ### SSML Support Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Demonstrates the use of Speech Synthesis Markup Language (SSML) for advanced text-to-speech control. ```javascript // SSML support // ... implementation details ... ``` -------------------------------- ### Dynamic Voice Selection Class Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Example of a class for dynamically selecting and managing voices in speech synthesis. ```javascript // Dynamic voice selection class // ... implementation details ... ``` -------------------------------- ### Basic Voice Search with SpeechRecognition Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/usage-examples.md Capture voice input for a search form. This example sets up the SpeechRecognition API to listen for user speech, update a search input field, and submit the form when speech is finalized. It includes basic event handling for start, result, error, and end events. ```javascript const recognition = new SpeechRecognition(); recognition.lang = 'en-US'; const searchInput = document.getElementById('searchInput'); const micButton = document.getElementById('micButton'); micButton.addEventListener('click', () => { searchInput.value = ''; recognition.start(); }); recognition.onstart = () => { micButton.classList.add('listening'); }; recognition.onresult = (event) => { let transcript = ''; for (let i = event.resultIndex; i < event.results.length; i++) { transcript += event.results[i][0].transcript; } searchInput.value = transcript; if (event.results[event.results.length - 1].isFinal) { searchInput.form.submit(); } }; recognition.onerror = (event) => { console.error('Speech recognition error:', event.error); }; recognition.onend = () => { micButton.classList.remove('listening'); }; ``` -------------------------------- ### start(audioTrack) - MediaStreamTrack Input Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Starts speech recognition using audio from a provided `MediaStreamTrack`. This method does not request microphone permission as the audio source is already supplied. ```APIDOC ## start(audioTrack) ### Description Starts speech recognition using audio from a `MediaStreamTrack`. ### Method `start(MediaStreamTrack audioTrack)` ### Parameters #### Path Parameters - **audioTrack** (MediaStreamTrack) - Required - Audio track to use for recognition ### Return Type `undefined` ### Throws - `InvalidStateError`: If `audioTrack.kind` is not "audio" - `InvalidStateError`: If `audioTrack.readyState` is not "live" - `InvalidStateError`: If recognition has already been started and no error or end event has fired - `InvalidStateError`: If the document is not fully active ### Behavior 1. Validates the audio track 2. Does NOT request microphone permission (audio source is already provided) 3. Begins processing audio from the track 4. Fires a `start` event once recognition begins ### Example ```javascript const recognition = new SpeechRecognition(); // Get audio from WebRTC peer connection const audioTrack = await getAudioTrackFromPeerConnection(); recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log('Recognized:', transcript); }; recognition.start(audioTrack); ``` ``` -------------------------------- ### Start Speech Recognition (MediaStreamTrack) Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Starts speech recognition using audio from a provided MediaStreamTrack. This method does not request microphone permission as the audio source is already supplied. ```javascript const recognition = new SpeechRecognition(); // Get audio from WebRTC peer connection const audioTrack = await getAudioTrackFromPeerConnection(); recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log('Recognized:', transcript); }; recognition.start(audioTrack); ``` -------------------------------- ### start Event Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Fired when the recognition service has begun listening with the intention of recognizing speech. ```APIDOC ## start ### Description Fired when the recognition service has begun listening with the intention of recognizing speech. ### Event Type `Event` ``` -------------------------------- ### Detect Speech Start Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Fired when speech that will be used for recognition has started. This event must fire after `audiostart`. ```javascript recognition.onspeechstart = () => { console.log('Speech detected, processing'); }; ``` -------------------------------- ### Start Speech Recognition Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md Initiate the speech recognition process. ```javascript recognition.start() ``` -------------------------------- ### Start On-Device Speech Recognition Source: https://github.com/webaudio/web-speech-api/blob/main/explainers/on-device-speech-recognition.md Configure `SpeechRecognition` with `processLocally: true` and the desired language tags before starting recognition. This ensures the speech recognition runs on the user's device. ```javascript const recognition = new SpeechRecognition(); recognition.options = { langs: ['en-US'], processLocally: true }; recognition.start(); ``` -------------------------------- ### SpeechRecognitionPhrase Usage Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-grammar.md Provides a complete example of creating and using SpeechRecognitionPhrase objects for contextual biasing in speech recognition. ```javascript const recognition = new SpeechRecognition(); // Create phrases for contextual biasing const phrases = [ new SpeechRecognitionPhrase('hello', 1.5), new SpeechRecognitionPhrase('goodbye', 1.5), new SpeechRecognitionPhrase('help', 2.0), new SpeechRecognitionPhrase('next', 1.0), new SpeechRecognitionPhrase('previous', 1.0) ]; recognition.phrases = phrases; recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log('Recognized:', transcript); }; recognition.start(); ``` -------------------------------- ### N-best Results with Confidence Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Shows how to retrieve multiple recognition results with their confidence scores. ```javascript // N-best results with confidence // ... implementation details ... ``` -------------------------------- ### Continuous Dictation with Interim Results Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Example for continuous speech dictation, showing how to handle interim recognition results. ```javascript // Continuous dictation with interim results // ... implementation details ... ``` -------------------------------- ### Detect Sound Start Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Fired when some sound (possibly speech) has been detected with low latency. This event must fire after `audiostart`. ```javascript recognition.onsoundstart = () => { console.log('Sound detected'); }; ``` -------------------------------- ### speechstart Event Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Fired when speech that will be used for recognition has started. This event is specific to the detection of recognizable speech. ```APIDOC ## speechstart ### Description Fired when speech that will be used for recognition has started. ### Event Type `Event` ### Guarantee The `audiostart` event must have fired before this event. ### Example ```javascript recognition.onspeechstart = () => { console.log('Speech detected, processing'); }; ``` ``` -------------------------------- ### Access Speech Synthesis Instance Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md Get the global SpeechSynthesis instance to control text-to-speech. ```javascript const synth = window.speechSynthesis; synth.speak(utterance); ``` -------------------------------- ### Handling Utterance Start Event Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Use this snippet to detect when speech synthesis begins. It logs a message and shows a speaking indicator. ```javascript const utterance = new SpeechSynthesisUtterance('Starting to speak'); utterance.onstart = (event) => { console.log('Speaking started'); console.log('Utterance:', event.utterance.text); showSpeakingIndicator(); }; window.speechSynthesis.speak(utterance); ``` -------------------------------- ### Create SpeechSynthesisEvent Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md Example of creating a SpeechSynthesisEvent. Ensure a SpeechSynthesisUtterance object is provided as it is a required field. ```javascript const utterance = new SpeechSynthesisUtterance('Hello'); const eventInit = { utterance: utterance, charIndex: 0, charLength: 5, elapsedTime: 0, name: "" }; const event = new SpeechSynthesisEvent('start', eventInit); ``` -------------------------------- ### Check Recognition Availability Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Code to check if speech recognition is available in the browser. ```javascript // Check recognition availability // ... implementation details ... ``` -------------------------------- ### Speak Text with Speech Synthesis Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md Start speaking the provided utterance using the speech synthesis engine. ```javascript speechSynthesis.speak(utterance) ``` -------------------------------- ### Interpreting 'downloading' Status Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md Example demonstrating how to react when the speech recognition model is in the 'downloading' state. This involves informing the user and potentially retrying after a delay. ```javascript const status = await SpeechRecognition.available({ langs: ['fr-FR'], processLocally: true }); if (status === 'downloading') { console.log('French model is being downloaded...'); // Show progress, wait, then retry setTimeout(async () => { const newStatus = await SpeechRecognition.available({ langs: ['fr-FR'], processLocally: true }); if (newStatus === 'available') { console.log('Download complete!'); } }, 5000); } ``` -------------------------------- ### Creating a SpeechRecognitionEvent Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md Example of how to create a new SpeechRecognitionEvent using the SpeechRecognitionEventInit dictionary. Assumes `createResultList` is an available implementation-specific function. ```javascript const resultList = createResultList(); // Implementation-specific const eventInit = { resultIndex: 2, results: resultList }; const event = new SpeechRecognitionEvent('result', eventInit); ``` -------------------------------- ### Setting Utterance Pitch Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Demonstrates how to set the speaking pitch for an utterance, creating high-pitched, low-pitched, and normal voices. Includes an example of creating different character voices. ```javascript const synth = window.speechSynthesis; // High-pitched voice (helium effect) const highPitch = new SpeechSynthesisUtterance('High pitched voice'); highPitch.pitch = 1.8; // Low-pitched voice (bass effect) const lowPitch = new SpeechSynthesisUtterance('Low pitched voice'); lowPitch.pitch = 0.5; // Normal pitch const normal = new SpeechSynthesisUtterance('Normal pitch voice'); normal.pitch = 1.0; synth.speak(highPitch); synth.speak(lowPitch); synth.speak(normal); // Voice character selection function createCharacterVoice(text, character) { const utterance = new SpeechSynthesisUtterance(text); switch (character) { case 'child': utterance.pitch = 1.5; utterance.rate = 1.2; break; case 'narrator': utterance.pitch = 0.8; utterance.rate = 0.9; break; case 'robot': utterance.pitch = 1.0; utterance.rate = 1.3; break; } return utterance; } const childVoice = createCharacterVoice('Hello!', 'child'); synth.speak(childVoice); ``` -------------------------------- ### Check Speech Recognition Availability Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/usage-examples.md Checks the availability of speech recognition, including language model download status and installation. Logs the status and provides feedback to the user. ```javascript async function checkAvailability() { const status = await SpeechRecognition.available({ langs: ['en-US', 'fr-FR'], processLocally: false, quality: 'dictation' }); console.log('Availability:', status); switch (status) { case 'available': console.log('Ready to recognize'); break; case 'downloadable': console.log('Language models available for download'); const installed = await SpeechRecognition.install({ langs: ['en-US'] }); if (installed) { console.log('Models installed'); } break; case 'downloading': console.log('Models are being downloaded'); break; case 'unavailable': console.log('Speech recognition not available'); break; } } checkAvailability(); ``` -------------------------------- ### SpeechRecognitionOptions: Checking Command Quality Availability Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md Example of checking for speech recognition availability with the 'command' quality level, suitable for short phrases and limited vocabulary. ```javascript // For voice commands const commandAvailable = await SpeechRecognition.available({ langs: ['en-US'], quality: 'command' }); ``` -------------------------------- ### Voice Commands: New Way (Recommended) Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-grammar.md Demonstrates using `phrases` for biasing important voice commands and setting up the result handler. ```javascript const recognition = new SpeechRecognition(); recognition.lang = 'en-US'; recognition.continuous = false; // Use phrases for biasing important commands recognition.phrases = [ new SpeechRecognitionPhrase('turn on', 2.0), new SpeechRecognitionPhrase('turn off', 2.0), new SpeechRecognitionPhrase('lights', 1.5), new SpeechRecognitionPhrase('television', 1.5) ]; recognition.onresult = (event) => { const command = event.results[0][0].transcript; console.log('Command:', command); }; ``` -------------------------------- ### Handling Utterance Pause and Resume Events Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md This example shows how to manage the 'pause' and 'resume' events for speech synthesis. It logs the character index and elapsed time when pausing and resuming. ```javascript const utterance = new SpeechSynthesisUtterance('A long message...'); const synth = window.speechSynthesis; utterance.onpause = (event) => { console.log('Paused at position:', event.charIndex); console.log('Time elapsed:', event.elapsedTime, 'seconds'); }; utterance.onresume = (event) => { console.log('Resumed from position:', event.charIndex); }; synth.speak(utterance); // Pause it after 2 seconds setTimeout(() => synth.pause(), 2000); // Resume after 4 seconds setTimeout(() => synth.resume(), 4000); ``` -------------------------------- ### Initialize and Use SpeechRecognition Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md This snippet shows how to create a SpeechRecognition instance, set its properties like language and interim results, and start the recognition process. It also includes an event handler for receiving transcription results. ```javascript const recognition = new SpeechRecognition(); recognition.lang = 'en-US'; recognition.continuous = false; recognition.interimResults = true; recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log('You said:', transcript); }; recognition.start(); ``` -------------------------------- ### Best Practices Summary Pattern Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md A summary of best practices for using the Web Speech API effectively. ```javascript // Best practices summary // ... implementation details ... ``` -------------------------------- ### Initialize and Use SpeechSynthesis Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md This snippet demonstrates how to use the SpeechSynthesis interface to convert text into spoken audio. It shows how to create an utterance, set its properties like language, rate, and pitch, and then speak it. ```javascript const synth = window.speechSynthesis; const utterance = new SpeechSynthesisUtterance('Hello, world!'); utterance.lang = 'en-US'; utterance.rate = 1.0; utterance.pitch = 1.0; synth.speak(utterance); ``` -------------------------------- ### SpeechSynthesisUtterance.pitch Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Sets or gets the pitch for the speech synthesis. A value of 1.0 is normal. ```APIDOC ## SpeechSynthesisUtterance.pitch ### Description The pitch for the speech synthesis. ### Method Attribute ### Parameters #### Path Parameters - **pitch** (float) - Required - Pitch level, from 0.0 to 2.0 ### Behavior - Values range from 0.0 (lowest pitch) to 2.0 (highest pitch). - The default value is 1.0. ### Request Example ```javascript const utterance = new SpeechSynthesisUtterance('This is spoken at a higher pitch.'); utterance.pitch = 1.5; window.speechSynthesis.speak(utterance); ``` ``` -------------------------------- ### Voice Command Application with Boosted Phrases Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/contextual-biasing.md Create a voice command recognition instance by defining common commands with specific boost values to prioritize their recognition. This example sets up phrases for controlling devices and modifiers. ```javascript function createCommandRecognition() { const recognition = new SpeechRecognition(); recognition.lang = 'en-US'; recognition.continuous = false; // Define command phrases with boost values const commands = [ // High priority commands new SpeechRecognitionPhrase('turn on', 2.5), new SpeechRecognitionPhrase('turn off', 2.5), new SpeechRecognitionPhrase('help', 3.0), new SpeechRecognitionPhrase('cancel', 2.5), // Device/object names new SpeechRecognitionPhrase('lights', 2.0), new SpeechRecognitionPhrase('television', 2.0), new SpeechRecognitionPhrase('door', 2.0), new SpeechRecognitionPhrase('speaker', 2.0), // Modifiers new SpeechRecognitionPhrase('bright', 1.5), new SpeechRecognitionPhrase('dim', 1.5), new SpeechRecognitionPhrase('volume', 1.5) ]; recognition.phrases = commands; return recognition; } const recognition = createCommandRecognition(); recognition.start(); ``` -------------------------------- ### SpeechSynthesisUtterance.rate Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Sets or gets the speech synthesis rate. A value of 1.0 is normal. ```APIDOC ## SpeechSynthesisUtterance.rate ### Description The speech synthesis rate. ### Method Attribute ### Parameters #### Path Parameters - **rate** (float) - Required - Speech synthesis rate, from 0.1 to 10.0 ### Behavior - Values range from 0.1 (slowest) to 10.0 (fastest). - The default value is 1.0. ### Request Example ```javascript const utterance = new SpeechSynthesisUtterance('This is spoken at a faster rate.'); utterance.rate = 1.5; window.speechSynthesis.speak(utterance); ``` ``` -------------------------------- ### SpeechSynthesisUtterance.volume Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Sets or gets the volume for the speech synthesis. A value of 1.0 is normal. ```APIDOC ## SpeechSynthesisUtterance.volume ### Description The volume for the speech synthesis. ### Method Attribute ### Parameters #### Path Parameters - **volume** (float) - Required - Volume level, from 0.0 to 1.0 ### Behavior - Values range from 0.0 (silent) to 1.0 (loudest). - The default value is 1.0. ### Request Example ```javascript const utterance = new SpeechSynthesisUtterance('This is spoken at half volume.'); utterance.volume = 0.5; window.speechSynthesis.speak(utterance); ``` ``` -------------------------------- ### Create Speech Recognition Instance Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md Instantiate a new SpeechRecognition object to begin speech recognition. ```javascript new SpeechRecognition() ``` -------------------------------- ### Get Available Voices Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/api-overview.md Retrieve a list of available speech synthesis voices. ```javascript speechSynthesis.getVoices() ``` -------------------------------- ### soundstart Event Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-api.md Fired when some sound (possibly speech) has been detected with low latency. This event signals the detection of audible input. ```APIDOC ## soundstart ### Description Fired when some sound (possibly speech) has been detected with low latency. ### Event Type `Event` ### Guarantee The `audiostart` event must have fired before this event. ### Example ```javascript recognition.onsoundstart = () => { console.log('Sound detected'); }; ``` ``` -------------------------------- ### Create SpeechSynthesisErrorEvent Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/types-and-enums.md Example of creating a SpeechSynthesisErrorEvent. The 'error' field is required and must be a valid SpeechSynthesisErrorCode. ```javascript const utterance = new SpeechSynthesisUtterance('Hello'); const eventInit = { utterance: utterance, error: 'audio-hardware', charIndex: 0 }; const event = new SpeechSynthesisErrorEvent('error', eventInit); ``` -------------------------------- ### Accessing Speech Recognition Transcript Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-recognition-events.md This example shows how to access the transcript of the first recognition alternative from the SpeechRecognitionEvent. For continuous recognition, it demonstrates accumulating final results into a full transcript. ```javascript recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log('You said: ' + transcript); // For continuous recognition let fullTranscript = ''; for (let i = 0; i < event.results.length; i++) { if (event.results[i].isFinal) { fullTranscript += event.results[i][0].transcript; } } }; ``` -------------------------------- ### Adjustable Speech Rate and Pitch Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Code for adjusting the speech rate and pitch during text-to-speech. ```javascript // Adjustable speech rate and pitch // ... implementation details ... ``` -------------------------------- ### Initialize Web Speech API with Browser Compatibility Check Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/usage-examples.md Checks for the availability of Speech Recognition and Speech Synthesis APIs in the browser. It logs messages to the console and displays warnings on the UI if either API is not supported. Returns initialized API objects or null if not supported. ```javascript function initializeSpeechAPI() { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; const SpeechSynthesis = window.speechSynthesis; if (!SpeechRecognition) { console.error('Speech Recognition not supported'); document.getElementById('recognitionWarning').style.display = 'block'; } else { console.log('Speech Recognition is supported'); } if (!SpeechSynthesis) { console.error('Speech Synthesis not supported'); document.getElementById('synthesisWarning').style.display = 'block'; } else { console.log('Speech Synthesis is supported'); } return { recognition: SpeechRecognition ? new SpeechRecognition() : null, synthesis: SpeechSynthesis }; } const { recognition, synthesis } = initializeSpeechAPI(); ``` -------------------------------- ### SpeechSynthesisUtterance.text Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Sets or gets the text content to be synthesized for this utterance. Supports plain text and SSML. ```APIDOC ## SpeechSynthesisUtterance.text ### Description The text to be synthesized and spoken for this utterance. May be plain text or SSML. ### Method Attribute ### Parameters #### Path Parameters - **text** (DOMString) - Required - Text content to synthesize ### Behavior - May be plain text or a complete, well-formed SSML document. - For engines without SSML support, tags are stripped and only text content is spoken. - There may be a maximum length (e.g., 32,767 characters). - Changes after calling `speak()` but before `end`/`error` event may or may not be reflected in the output. ### Supported Markup SSML (Speech Synthesis Markup Language) ### Request Example ```javascript const utterance = new SpeechSynthesisUtterance(); // Plain text utterance.text = 'Hello, world!'; // SSML with prosody tags utterance.text = ` Hello, world! `; // With SSML marks for event tracking utterance.text = ` First part Second part Third part `; ``` ``` -------------------------------- ### SpeechRecognitionPhrase Constructor and Usage Source: https://github.com/webaudio/web-speech-api/blob/main/explainers/contextual-biasing.md Demonstrates how to create and use `SpeechRecognitionPhrase` objects to provide contextual hints to the `SpeechRecognition` engine. This includes creating phrases with custom boost values and dynamically adding them to the recognition session. ```APIDOC ## SpeechRecognition.phrases attribute and SpeechRecognitionPhrase interface ### Description Contextual biasing allows developers to provide hints to the speech recognition engine in the form of a list of phrases and boost values. This feature is implemented through the `phrases` attribute on the `SpeechRecognition` interface, which uses the `SpeechRecognitionPhrase` interface. ### `SpeechRecognition.phrases` attribute - Type: `ObservableArray` - Description: An array of `SpeechRecognitionPhrase` objects that provide contextual hints for the recognition session. It can be modified like a JavaScript `Array`. ### `SpeechRecognitionPhrase` interface Represents a single phrase and its associated boost value. - **constructor(DOMString phrase, optional float boost = 1.0)**: Creates a new phrase object. - `phrase`: The text string to be boosted. - `boost`: A float between 0.0 and 10.0. Higher values make the phrase more likely to be recognized. ### Example Usage ```javascript // A list of phrases relevant to our application's context. const phraseData = [ { phrase: 'Zoltan', boost: 3.0 }, { phrase: 'Grog', boost: 2.0 }, ]; // Create SpeechRecognitionPhrase objects. const phraseObjects = phraseData.map(p => new SpeechRecognitionPhrase(p.phrase, p.boost)); const recognition = new SpeechRecognition(); // Assign the phrase objects to the recognition instance. // The attribute is an ObservableArray, so we can assign an array to it. recognition.phrases = phraseObjects; // We can also dynamically add/remove phrases. recognition.phrases.push(new SpeechRecognitionPhrase('Xylia', 2.5)); // Some user agents (e.g. Chrome) might only support on-device contextual biasing. recognition.processLocally = true; recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; console.log(`Result: ${transcript}`); }; recognition.onerror = (event) => { if (event.error === 'phrases-not-supported') { console.warn('Contextual biasing is not supported by this browser/service.'); } }; // Start recognition when the user clicks a button. document.getElementById('speak-button').onclick = () => { recognition.start(); }; ``` ``` -------------------------------- ### Speech Synthesis with SSML Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Demonstrates how to use SSML markup within a SpeechSynthesisUtterance to control speech properties like pitch, rate, and breaks. Includes an example of handling the 'onmark' event. ```javascript const utterance = new SpeechSynthesisUtterance(` Welcome to the audio book. The story begins: Important events occur. `); // Engine without SSML support strips tags and speaks the text utterance.onmark = (event) => { if (event.name === 'chapter1') { console.log('Chapter 1 starting'); } }; window.speechSynthesis.speak(utterance); ``` -------------------------------- ### Accessible Text Reader Example Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/INDEX.md Code for creating an accessible text reader using speech synthesis. ```javascript // Accessible text reader // ... implementation details ... ``` -------------------------------- ### Customer Service Chatbot with Contextual Phrases Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/contextual-biasing.md Implement a chatbot that boosts common customer intents and context-specific phrases. This example dynamically sets phrases based on the provided context and handles potential 'phrases-not-supported' errors. ```javascript class ChatbotRecognition { constructor(context) { this.recognition = new SpeechRecognition(); this.recognition.lang = 'en-US'; this.recognition.continuous = false; this.recognition.interimResults = true; this.setupPhrases(context); } setupPhrases(context) { const phrases = []; // Common intents const commonIntents = [ 'I want to order', 'I have a problem', 'Where is my package', 'How much does it cost', 'Do you have stock', 'I want to return this', 'What are the hours' ]; // Context-specific high-value phrases const contextPhrases = this.getContextPhrases(context); // Add with high boost for common intents commonIntents.forEach(intent => { phrases.push(new SpeechRecognitionPhrase(intent, 2.5)); }); // Add context-specific phrases contextPhrases.forEach(phrase => { phrases.push(new SpeechRecognitionPhrase(phrase, 3.0)); }); this.recognition.phrases = phrases; } getContextPhrases(context) { const contextMap = { 'orders': ['track order', 'cancel order', 'order history'], 'billing': ['credit card', 'invoice', 'refund', 'payment'], 'support': ['technical issue', 'not working', 'error', 'help'] }; return contextMap[context] || []; } listen(callback) { this.recognition.onresult = (event) => { let transcript = ''; for (let i = event.resultIndex; i < event.results.length; i++) { transcript += event.results[i][0].transcript; } if (event.results[event.results.length - 1].isFinal) { callback(transcript); } }; this.recognition.onerror = (event) => { if (event.error === 'phrases-not-supported') { console.warn('Contextual biasing not supported, removing phrases'); this.recognition.phrases = []; } }; this.recognition.start(); } } // Usage const chatbot = new ChatbotRecognition('orders'); chatbot.listen((transcript) => { console.log('Customer said:', transcript); }); ``` -------------------------------- ### Set SpeechGrammar weight (Deprecated) Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-grammar.md Example of setting the `weight` property of a SpeechGrammar object. This has no effect on recognition. ```javascript const recognition = new SpeechRecognition(); const grammar = recognition.grammars[0]; grammar.weight = 1.5; // Increase weight // This has no effect on recognition ``` -------------------------------- ### Set SpeechGrammar src (Deprecated) Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-grammar.md Example of setting the `src` property of a SpeechGrammar object. This has no effect on recognition. ```javascript const recognition = new SpeechRecognition(); const grammar = recognition.grammars[0]; grammar.src = 'file:///path/to/grammar.jsgf'; // This has no effect on recognition ``` -------------------------------- ### SpeechSynthesisUtterance.voice Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-synthesis-utterance.md Sets or gets the SpeechSynthesisVoice to use for the utterance. If null, the user agent selects a default voice. ```APIDOC ## SpeechSynthesisUtterance.voice ### Description The voice to use for synthesis. Must be a SpeechSynthesisVoice instance from `getVoices()`. ### Method Attribute ### Parameters #### Path Parameters - **voice** (SpeechSynthesisVoice or null) - Required - Voice instance from `getVoices()` ### Behavior - Initialized to `null` when the utterance is created. - Must be one of the objects returned by `SpeechSynthesis.getVoices()`. - If set to a valid voice at `speak()` time, that voice is used. - If `null` or unset at `speak()` time, the user agent selects a default voice. - The default voice should support the current language (`lang` attribute). - Default voice can be local or remote and incorporates user agent preferences. ### Request Example ```javascript const synth = window.speechSynthesis; const utterance = new SpeechSynthesisUtterance('Hello'); // Get available voices const voices = synth.getVoices(); // Find a specific voice const preferredVoice = voices.find( (v) => v.name.includes('Google US English') && !v.localService ); if (preferredVoice) { utterance.voice = preferredVoice; } else if (voices.length > 0) { utterance.voice = voices[0]; // Use first available } synth.speak(utterance); // Also works with getting default voice for a language const frenchVoice = voices.find((v) => v.lang.startsWith('fr')); if (frenchVoice) { utterance.voice = frenchVoice; } ``` ``` -------------------------------- ### SpeechRecognitionPhrase Constructor Usage Source: https://github.com/webaudio/web-speech-api/blob/main/_autodocs/speech-grammar.md Illustrates the basic usage of the SpeechRecognitionPhrase constructor with phrase and boost parameters. ```javascript new SpeechRecognitionPhrase(phrase, boost) ```