Azure-Samples / cognitive-services-speech-sdk

Sample code for the Microsoft Cognitive Services Speech SDK
MIT License
2.89k stars 1.85k forks source link

How to stop the Text-To-Speech Azure SDK #2647

Open Roopesh-Bharatwaj-K-R opened 5 days ago

Roopesh-Bharatwaj-K-R commented 5 days ago

Hi Azure,

I have created a React app that utilises the Azure Speech (TTS) solution and, it's working fine.

I have used the Javascript and followed the JS Speech Synthesis documentation from Azure:

[https://learn.microsoft.com/en-us/azure/ai-services/speech-service/how-to-speech-synthesis?pivots=programming-language-javascript&tabs=browserjs%2Cterminal#synthesize-speech-to-a-file]

But the issue popped when we try to stop the speaking, we could not stop the speech from the azure, and when i checked the code, there was no methods which calls direct stop, instead i could see, Close(), Cancel().

Which was not working fine and i did check the couple of issues related to the same. [SpeechSynthesizer.StopSpeakingAsync()] which was also not working fine.

  1. https://github.com/microsoft/cognitive-services-speech-sdk-js/issues/608
  2. https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/2367
  3. https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/2350

I tried other approaches to do refreshing of the audio, and synthesis to Null. But it was not working fine

kindly suggest to me the best way how stop the audio from the azure TTS. Kindly share some of the Notebooks and code examples to resolve the stopping effectively.

Thanks in Advance for your kind suggestions.

Best, Roopesh

aman-vohra-007 commented 3 hours ago

Hey, @Roopesh-Bharatwaj-K-R . Hope this helps

I have used the microsoft-cognitiveservices-speech-sdk for viseme so I have used ref in ReactJS for the synthesizer.

import * as sdk from "microsoft-cognitiveservices-speech-sdk"

const synthesizeSpeech = text => { return new Promise((resolve, reject) => { if (!speechSynthesizerRef.current) { const speechConfig = sdk.SpeechConfig.fromSubscription( import.meta.env.VITE_SPEECH_KEY, import.meta.env.VITE_SPEECH_REGION ) speechSynthesizerRef.current = new sdk.SpeechSynthesizer(speechConfig) let speechStarted = false ..... }

And to stop the speech, I did this const stopSpeech = () => { try { setImageIndex(0) setIsAudioPlaying(false) if (speechSynthesizerRef.current) { const audio = speechSynthesizerRef.current.privAdapter?.privSessionAudioDestination?.privDestination ?.privAudio if (audio) { audio.pause() audio.currentTime = 0 speechSynthesizerRef.current.close() speechSynthesizerRef.current = null } } } catch (e) { console.error("Error in stopSpeech:", e) } }

This helped in stopping the speech as well as resetting the synthesis, so if you play it again, the audio starts too.