Azure Speech_synthesizer.speak_text_async() 真的异步执行吗?

问题描述 投票:0回答:1

我正在 python 中使用 Azure SpeechSynthesizer 库。我已经编写了将一些文本翻译成语音的代码。我发现您需要对结果进行 get() 调用才能真正让它进行任何语音合成。但这个 get() 调用本质上是阻塞的。

pull_stream = speechsdk.audio.PullAudioOutputStream()
stream_config = speechsdk.audio.AudioOutputConfig(stream=pull_stream)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=stream_config)

result = speech_synthesizer.speak_text_async(text)
result.get()
del speech_synthesizer

如果我不调用 result.get(),我无法从流中提取任何数据。但是当我调用 result.get() 时,它会在将文本翻译为语音时阻塞几秒钟。我已经使用文件名的 AudioOutputConfig 运行它,将其保存到波形文件中,并且时间大致相同。所以我知道无论我将输出作为流还是文件,它都在做相同的工作。

有关如何让它实际异步工作的任何指示,以便我可以在翻译时从流中提取数据,而不必等到它完成?

azure speech-synthesis azure-speech
1个回答
0
投票

我尝试使用以下代码将文本转换为语音:使用 result = voice_synthesizer.speak_text_async(text).get().wav 文件,并成功将文本转换为语音。

代码:

import azure.cognitiveservices.speech as speechsdk
import threading

subscription_key = "<speech_key>"
region = "<speech_key>"
text = "Hello Kamali,welcome."
output_file = "output.wav"  

def synthesis_callback(evt):
    """
    Callback function to handle speech synthesis events.
    """
    if evt.result.reason == speechsdk.ResultReason.SynthesizingAudio:
        audio_data = evt.result.audio_data
        with open(output_file, "ab") as f:
            f.write(audio_data)
    elif evt.result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
        print("Speech synthesis completed.")
pull_stream = speechsdk.audio.PullAudioOutputStream()
stream_config = speechsdk.audio.AudioOutputConfig(stream=pull_stream)
speech_config = speechsdk.SpeechConfig(subscription=subscription_key, region=region)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=stream_config)

speech_synthesizer.synthesis_started.connect(lambda evt: print("Synthesis started"))
speech_synthesizer.synthesizing.connect(synthesis_callback)
speech_synthesizer.synthesis_completed.connect(lambda evt: print("Synthesis completed"))

result = speech_synthesizer.speak_text_async(text).get()  

del speech_synthesizer
print(f"Audio saved to {output_file}")

输出:

下面的代码成功地将文本转换为语音输出,如下。

C:\Users\xxxxxxxxx\kamali> python test.py
Synthesis started
Audio saved to output.wav

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.