Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

- Resolved microphone not released when `stop()` is called in continuous mode during silence, by [@Akram-Wa](https://github.com/Akram-Wa) in PR [#260](https://github.com/compulim/web-speech-cognitive-services/pull/260)

### Changed

- 👷🏻 Test framework moved to Node.js test runner, by [@compulim](https://github.com/compulim) in PR [#256](https://github.com/compulim/web-speech-cognitive-services/pull/256)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,63 @@ describe('SpeechRecognition', () => {
'webspeech:end'
]);
});

test('stop with no pending speech should release microphone immediately', async () => {
// Regression test for https://github.com/compulim/web-speech-cognitive-services/issues/166
// stop() in continuous mode must call stopContinuousRecognitionAsync immediately,
// not defer it to a recognized event that may never arrive (e.g. during silence).
speechRecognition.continuous = true;
speechRecognition.start();

const recognizer = await constructRecognizerDeferred.promise;

await recognizer.waitForStartContinuousRecognitionAsync();

// This will fire "firstAudibleChunk" on "emitRead"
await recognizer.readAudioChunk();

recognizer.audioConfig.emitEvent('AudioSourceReadyEvent');

// cognitiveservices:audioSourceReady
// webspeech:start
// webspeech:audiostart

recognizer.audioConfig.emitRead();
await soundStartEmitted;

// cognitiveservices:firstAudibleChunk
// webspeech:soundstart

speechRecognition.stop();

// cognitiveservices:stop
// stopContinuousRecognitionAsync must be called immediately here (the fix)

await recognizer.waitForStopContinuousRecognitionAsync();

recognizer.audioConfig.emitEvent('AudioSourceOffEvent');

// cognitiveservices:audioSourceOff
// webspeech:soundend
// webspeech:audioend
// webspeech:error { error: 'no-speech' }
// webspeech:end

await endEventEmitted;

expect(toSnapshot(events)).toEqual([
'cognitiveservices:audioSourceReady',
'webspeech:start',
'webspeech:audiostart',
'cognitiveservices:firstAudibleChunk',
'webspeech:soundstart',
'cognitiveservices:stop',
'cognitiveservices:audioSourceOff',
'webspeech:soundend',
'webspeech:audioend',
'webspeech:end'
]);
});
});

test('with network error', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,9 @@ export default function createSpeechRecognitionPonyfillFromRecognizer({
stopping = 'stop';
}

// Abort should not be dispatched without support of "stopContinuousRecognitionAsync".
// But for defensive purpose, we make sure "stopContinuousRecognitionAsync" is available before we can call.
if (abort && recognizer.stopContinuousRecognitionAsync) {
// Both abort and stop must call stopContinuousRecognitionAsync immediately to release the microphone.
// Without this, stop() leaves the mic active and subsequent start() calls fail until page refresh.
if (recognizer.stopContinuousRecognitionAsync) {
await cognitiveServicesAsyncToPromise<void>(recognizer.stopContinuousRecognitionAsync, recognizer)();
}
} else if (audioSourceReady) {
Expand Down Expand Up @@ -536,8 +536,8 @@ export default function createSpeechRecognitionPonyfillFromRecognizer({
}

// If it is interactive, stop after first recognition.
// If it is continuous and it is stopping, stop it too.
if ((!this.continuous || stopping === 'stop') && recognizer.stopContinuousRecognitionAsync) {
// Continuous+stopping case is already handled above when stop was first received.
if (!this.continuous && recognizer.stopContinuousRecognitionAsync) {
await cognitiveServicesAsyncToPromise<void>(recognizer.stopContinuousRecognitionAsync, recognizer)();
}

Expand Down