cognitive-services-speech-sdk: Windows: Calling `SpeechSynthesizer.StopSpeakingAsync()` does not stop synthesis

Describe the bug

A call to SpeechSynthesizer.StopSpeakingAsync() does not stop synthesis for a very long time, up to 30 seconds. The log file is here: speech.log

This issue was previously reported without action at https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/1836 and https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/2264

To Reproduce

We are building a node.js binding for Speech SDK and the C++ sources mimic the samples. The synthesis is implemented here: https://github.com/microsoft/node-speech/blob/967976ce0f4887a2b5b27f486e5209a51588516f/src/main.cc#L477

The call to StopSpeakingAsync here: https://github.com/microsoft/node-speech/blob/967976ce0f4887a2b5b27f486e5209a51588516f/src/main.cc#L539

To reproduce from that module:

  • using node.js 18.x on the system
  • git clone https://github.com/microsoft/node-speech.git
  • open index.ts and append the snippet [1] at the end
  • from a terminal cd into the workspace and run npm i
  • run node index.js

[1]

const t = createSynthesizer({
  modelPath: '<path to TTS model>',
  modelName: 'Microsoft Server Speech Text to Speech Voice (en-US, AriaNeural)',
  modelKey: '<model key>',
}, (error, result) => {
  if (error) {
    console.error(error);
  } else {
    console.log(result);
  }
});
t.synthesize(`
Now more than ever, developers are expected to build voice-enabled applications that can reach a global audience. With the same voice persona across languages, organizations can keep their brand image more consistent. To support the growing need for a single voice to speak multiple languages, particularly in scenarios such as localization and translation, a multi-lingual neural TTS voice is brought out in public preview.



This new Jenny Multilingual voice (preview), with US English as the primary/default language, can speak 13 secondary languages, each at the fluent level: German (Germany), English (Australia), English (Canada), English (Canada), Spanish (Spain), Spanish (Mexico), French (Canada), French (France), Italian (Italy), Japanese (Japan), Korean (Korea), Portuguese (Brazil), Chinese (Mandarin, Simplified).
`);
setTimeout(() => t.stop(), 5000);

Expected behavior

Calling SpeechSynthesizer.StopSpeakingAsync immediately stops synthesis.

Version of the Cognitive Services Speech SDK

1.37.0

Platform, Operating System, and Programming Language

  • OS: Windows 11 (24H2)
  • Hardware: ARM
  • Programming language: C++

Additional context

This issue does not reproduce on macOS or Linux!

About this issue

Most upvoted comments

connection from Connection.fromSynthesizer

Here is a example: stopSynthesize

Ah, thanks. It’s a bit faster with cancellation than synthesizer.close(), but still the audio already buffered plays several seconds. I now found a workaround by accessing the private audio object:

//DANGER!FRAGILE uses private objects to work around issue: https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/2350
   function KillAudio(synthesizer: sdk.SpeechSynthesizer) {      
        // kill the audio
        const audio: HTMLAudioElement | undefined = synthesizer.privAdapter?.privSessionAudioDestination?.privDestination?.privAudio;
        if(audio)
        {
            audio.pause();
            audio.currentTime = 0;
        }
    }

(this immediatelly stops the audio playback) and then I call synthesizer.close(). But this is fragile code accessing private objects, I need to find a way to access that audio object in an official way.