Azure 将文本转换为语音

注意:只需要使用 speech 语音服务,不用Azure OpenAi

环境变量

复制代码
setx SPEECH_KEY your-key
setx SPEECH_REGION your-region

需要安装库

复制代码
pip install azure-cognitiveservices-speech

import os
import azure.cognitiveservices.speech as speechsdk

# This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
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)

# The neural multilingual voice can speak different languages based on the input text.
speech_config.speech_synthesis_voice_name='en-US-AvaMultilingualNeural'

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

# Get text from the console and synthesize to the default speaker.
print("Enter some 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 the speech resource key and region values?")

参见:

文本转语音快速入门 - 语音服务 - Azure AI services | Microsoft Learn

另一篇:

Azure OpenAI 语音转语音聊天_azure openai 语音转语音聊天 restful-CSDN博客

相关推荐
为何创造硅基生物3 天前
C语言结构体
c语言·windows·microsoft
宝桥南山3 天前
Azure - Azure需要MFA login了(2025-09-30之后)
microsoft·微软·azure
zzywxc7873 天前
AI赋能千行百业:金融、医疗、教育、制造业的落地实践与未来展望
java·人工智能·python·microsoft·金融·golang·prompt
安娜的信息安全说4 天前
企业身份认证系统选型:Azure AD 与 Keycloak 功能详解
安全·microsoft·keycloak·azure ad
胡耀超4 天前
音频降噪技术:从原理到工具的完整指南(scipy librosa noisereduce soundfile pedalboard)
音视频·音频·scipy·降噪·soundfile·noisereduce·pedalboard
迎風吹頭髮4 天前
UNIX下C语言编程与实践24-UNIX 标准文件编程库:无格式读写函数族(字符、行、块)的使用
c语言·microsoft·unix
zzywxc7875 天前
大模型落地实践指南:从技术路径到企业级解决方案
java·人工智能·python·microsoft·golang·prompt
FreeBuf_5 天前
微软警示AI驱动的钓鱼攻击:LLM生成的SVG文件绕过邮件安全检测
人工智能·安全·microsoft
ITHAOGE155 天前
下载 | Win10 2021官方精简版,预装应用极少!(9月更新、Win 10 IoT LTSC 2021版、适合老电脑安装)
windows·科技·物联网·microsoft·微软·电脑
一人の梅雨6 天前
淘宝店铺全量商品接口深度开发:从分页优化到数据完整性保障
linux·windows·microsoft