如何在队列中播放音频 TTS?

问题描述

我正在尝试通过 Azure TTS 为 twitch 聊天消息进行演讲。在这种情况下,一切正常,但同时播放消息。如何让消息按顺序播放?

<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>

解决方法

我认为这里的问题是 ComfyJS.onChat/synthesizeSpeech() 函数在不同线程上被多次调用,或者至少多次调用,而无需等待之前的 speakTextAsync 调用结束。

我会尝试制作“var synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig)”全局范围变量,以便您使用单个合成器来朗读所有传入消息,而不是为每条消息使用新的合成器。使用单个 tts 引擎应该会导致它们排队并按顺序呈现。

或者,您可以等待 speakTextAsync() 完成,然后再允许创建另一个合成器和消息并进行排队,但我认为在整个聊天/对话中使用单个合成器实例会更有效。

布莱恩。