python - Azure TTS 超时 python

标签 python azure text-to-speech azure-cognitive-services

嗨,我遇到了 Azure TTS 的问题当我尝试使用 python 文本到语音快速入门示例时,我收到此错误

...> python example.py
Enter some text that you want to speak >
test
Speech synthesis canceled: CancellationReason.Error
Error details: USP error: timeout waiting for the first audio chunk
Did you set the speech resource key and region values?

使用Python 3.11.4 **Azure 语音转文本工作正常 **

最佳答案

我遵循了给定的MS Doc我得到了带有输入文本的语音输出。

代码:

import os
import azure.cognitiveservices.speech as speechsdk

speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)

speech_config.speech_synthesis_voice_name='en-US-JennyNeural'

speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

print("Enter text that you want to speak >")
text = input()

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

if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
    print("Speech synthesized for text [{}]".format(text))
elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
    cancellation_details = speech_synthesis_result.cancellation_details
    print("Speech synthesis canceled: {}".format(cancellation_details.reason))
    if cancellation_details.reason == speechsdk.CancellationReason.Error:
        if cancellation_details.error_details:
            print("Error details: {}".format(cancellation_details.error_details))
            print("Did you set speech resource key and region?")

<强>。环境:

SPEECH_KEY="<specch_key>"
SPEECH_REGION="<speech_region>"

在运行上述代码之前,您需要在系统环境变量中设置SPEECH_KEYSPEECH_REGION,如下所示,

enter image description here

输出:

运行成功如下,

enter image description here

然后,我输入一行Hi kamali, How are you。 kasimbi 怎么样?如下所示,它提供语音输出。

enter image description here

作为替代方案,您还可以尝试下面的其他文本转语音代码,

代码:

import os
from azure.cognitiveservices.speech import AudioDataStream, ResultReason, SpeechConfig, SpeechSynthesizer, SpeechSynthesisOutputFormat

subscription_key = "<speech_key>"
region = "<speech_region>"

speech_config = SpeechConfig(subscription=subscription_key, region=region)
synthesizer = SpeechSynthesizer(speech_config=speech_config)

try:
    text = input("Enter some text that you want to speak > ")
    result = synthesizer.speak_text_async(text).get()
    
    if result.reason == ResultReason.SynthesizingAudioCompleted:
        print("Speech synthesis succeeded")
    else:
        print("Speech synthesis failed: {}".format(result.reason))
    
except Exception as e:
    print("An error occurred: {}".format(e))

输出:

它成功运行并给出带有输入文本的语音输出,如下所示,

enter image description here

关于python - Azure TTS 超时 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76891591/

相关文章:

python - view.py 中的 Django PasswordChangeForm 不工作

python - 替换 Python 列表/字典中的值?

ios - 如何在Azure托管代理上使用特定的iOS版本?

.net - 保持后台任务在云服务应用程序域内运行的最佳实践是什么?

text-to-speech - 波网的输入是什么?

ios - Text 2 Speech 适用于模拟器,但不适用于真实设备

python - 插入中的 sqlalchemy 回滚

python - 有没有更快的(numpy?)方法将 pandas df int 列组合成点分隔的 str col 而不出现 TypeError

python - 在 Visual Studio Code 上调试 Azure 函数时出错 ('LogType' 不存在)

java - 如何从java发送bluemix wav流响应