const {authToken, region} = await getSpeechToken(true); const speechConfig =…
- User
const {authToken, region} = await getSpeechToken(true); const speechConfig = speechsdk.SpeechConfig.fromAuthorizationToken( authToken, region, );
speechConfig.speechRecognitionLanguage = 'en-IN'; audioStream.current = speechsdk.AudioInputStream.createPushStream( speechsdk.AudioStreamFormat.getWaveFormatPCM( micSettings.sampleRate, micSettings.sampleSize, micSettings.channelCount, ), ); const audioConfig = speechsdk.AudioConfig.fromStreamInput( audioStream.current, ); const recognizer = new speechsdk.SpeechRecognizer( speechConfig, audioConfig, ); speechsdk.PhraseListGrammar.fromRecognizer(recognizer).addPhrases(phrases); recognizer.recognizing = (s, e) => { const resultJson = JSON.parse(e.result.json); const offset = resultJson.Offset; const text = resultJson.Text; const duration= resultJson.Duration; console.log('recognizing', text) }; recognizer.recognized = (s, e) => { if (e.result.reason === speechsdk.ResultReason.RecognizedSpeech) { console.log('RECOGNIZED: Text=' + e.result.text); } else if (e.result.reason === speechsdk.ResultReason.NoMatch) { console.log('NOMATCH: Speech could not be recognized.'); } }; recognizer.canceled = (s, e) => { console.log(`CANCELED: Reason=${e.reason}`); if (e.reason === speechsdk.CancellationReason.Error) { console.log(`CANCELED: ErrorCode=${e.errorCode}`); console.log(`CANCELED: ErrorDetails=${e.errorDetails}`); } }; return new Promise((resolve, reject) => { console.log('Starting continuous recognition'); recognizer.startContinuousRecognitionAsync( () => { console.log('Continuous recognition started'); recognizerRef.current = recognizer; initialized.current = true; initializing.current = false; resolve(recognizer); }, err => reject(err), ); });}
is there a way to reset the recognizing event manually while speaking?
- Assistant
In the Microsoft Azure Speech SDK, there isn't a built-in method to manually reset the
recognizingevent or the recognizer's internal state during ongoing speech recognition. Therecognizingevent is a read-only stream of intermediate recognition results provided by the SDK as it processes incoming audio.If you need to reset or clear the recognizer's state while speaking—perhaps to discard partial results and start fresh—you would typically need to stop the current recognition session and start a new one. This can be achieved by calling
stopContinuousRecognitionAsyncand thenstartContinuousRecognitionAsyncon theSpeechRecognizerinstance. Here's how you might do that:// To reset the recognizer during recognition recognizer.stopContinuousRecognitionAsync(() => { console.log('Recognition stopped for reset.'); // Optionally, you can dispose of the current recognizer and create a new one // recognizer.close(); // Initialize a new recognizer here if needed // Restart recognition recognizer.startContinuousRecognitionAsync(() => { console.log('Recognition restarted after reset.'); }, err => { console.error('Error restarting recognition: ' + err); }); }, err => { console.error('Error stopping recognition: ' + err); });However, be aware that stopping and restarting the recognizer can introduce delays and might cause some spoken words to be missed during the transition. The SDK needs to reinitialize resources, and any audio spoken while it's stopped won't be processed.
If your goal is to ignore or discard certain recognizing results based on some condition while maintaining continuous recognition, you might consider managing this within your event handler logic. For example, you can set a flag when you want to "reset" and then ignore any
recognizingevents until you're ready to process them again:let resetInProgress = false; recognizer.recognizing = (s, e) => { if (resetInProgress) { // Ignore recognizing events during reset return; } const resultJson = JSON.parse(e.result.json); const text = resultJson.Text; console.log('recognizing', text); }; // Somewhere in your code when you want to reset resetInProgress = true; // After some condition or timeout, end the reset setTimeout(() => { resetInProgress = false; }, timeoutDuration);This approach doesn't reset the recognizer's internal state, but it allows you to control how you handle recognizing events without interrupting the recognition session.
If you need more control over the recognition process—such as implementing custom reset behavior—you might need to look into more advanced features of the SDK or consider designing your application logic to accommodate the limitations.
Summary:
- No direct reset method: The SDK doesn't provide a method to reset the
recognizingevent or the recognizer's internal buffers during ongoing recognition. - Stopping and restarting: You can stop and then restart recognition using
stopContinuousRecognitionAsyncandstartContinuousRecognitionAsync, but this may lead to missed audio and delays. - Event handler logic: Manage resets within your event handlers by using flags or conditions to ignore or handle recognizing events as needed.
- Design considerations: Assess whether you can adjust your application design to work within these constraints or if you need to implement a custom solution.
Remember to handle any asynchronous operations carefully to ensure that your application remains responsive and that resources are properly managed.
- No direct reset method: The SDK doesn't provide a method to reset the