gpt4 book ai didi

python - 如何为 Microsoft Azure 声音赋予不同的风格?

转载 作者:行者123 更新时间:2023-12-03 06:08:48 32 4
gpt4 key购买 nike

我无法弄清楚如何将不同的语音风格应用于 Microsoft Azure 文本转语音。

speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Note: the voice setting will not overwrite the voice element in input SSML.
speech_config.speech_synthesis_voice_name = "en-US-DavisNeural"
speech_config.speech_synthesis_voice_style = "shouting"


text = "Hi, this is Davis"


# use the default speaker as audio output.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)

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

尝试过寻找其他指南,但有些指南已被删除,并且官方文档对我没有真正的帮助。

最佳答案

要将不同的语音样式应用于 Microsoft Azure 文本转语音语音,您可以使用语音合成标记语言 (SSML)。对于您的情况,您可以修改 text 变量以包含 SSML 标记。

下面是如何使用SSML应用“shouting”和其他一些样式的示例:

import azure.cognitiveservices.speech as speechsdk


speech_key = "<API Key>"
service_region = "<Region Code>"

speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

text = """
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
<voice name="en-US-DavisNeural">
<mstts:express-as style="cheerful" styledegree="2">
That'd be just amazing!
</mstts:express-as>
<mstts:express-as style="shouting" styledegree="2">
Hi, this is Davis
</mstts:express-as>
<mstts:express-as style="sad" styledegree="2">
What's next?
</mstts:express-as>
</voice>
</speak>
"""

speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
result = speech_synthesizer.speak_ssml_async(text).get()
output_file = "output.wav"
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
audio_data = result.audio_data
with open(output_file, "wb") as file:
file.write(audio_data)
print(f"Audio saved to {output_file}")

通过上面的代码片段,您将能够获得不同的语音风格输出。有关更多详细信息和示例,请查看 Customize voice and sound with SSML Documentation.

关于python - 如何为 Microsoft Azure 声音赋予不同的风格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77063185/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com