yvupuzol
yvupuzol

Reputation: 3

How to play audio TTS in queue?

I'm trying to make speech for twitch chat messages through Azure TTS. In this case, everything works, but the messages are played at the same time. How can I make messages play in sequence?

<html>
  <head>
    <script src="comfy.min.js"></script>
  </head>
  <body>
    <script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>
    <script type="text/javascript">
    function synthesizeSpeech(message) {
        var speechConfig = SpeechSDK.SpeechConfig.fromSubscription("AzureKey", "AzureRegion");
        speechConfig.speechSynthesisVoiceName = "en-US-Zira";
        speechConfig.SpeechSynthesisLanguage = "en-US";
        var synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig);
        synthesizer.speakTextAsync(message);  // twitch message speech
    };
    
    ComfyJS.onChat = (user, message, flags, self, extra) => {
        if( flags.broadcaster === true ) {
          console.log(message); //display message
          synthesizeSpeech(message);  // start function speech
        }
    }
      ComfyJS.Init( "TwitchChannel" );
    </script>
  </body>
</html>

Upvotes: 0

Views: 388

Answers (1)

Brian Mouncer
Brian Mouncer

Reputation: 71

I believe the issue here is that the ComfyJS.onChat/synthesizeSpeech() function is getting called multiple times on different threads, or at least multiple times without waiting for the previous speakTextAsync call to finish speaking.

I would experiment with making "var synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig)" globally scoped variable, so that you are using a single synthesizer to speak all the incoming messages, rather than a new synthesizer for each message. using a single tts engine should cause them to queue up and render in order.

Alternatively you could wait for speakTextAsync() to finish before allowing another synthesizer and message to be created and queued, but I think it would be more efficient to use a single synthesizer instance for the entire chat/conversation.

Brian.

Upvotes: 0

Related Questions