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

相关推荐
编码者卢布1 天前
【Azure Service Fabric】如何在不删除应用的情况下“停止“某个服务,并在需要时快速恢复
microsoft·azure·fabric
Leinwin1 天前
技术视角解析 Microsoft 365 Copilot Standalone:无底座许可与跨平台集成方案
microsoft·copilot
炸薯条!2 天前
Streamlit 实战:AI智能伴侣侧边栏设置与多会话管理
microsoft
总要学点什么3 天前
第128篇:镜头光晕与高光——把逆光太阳晕成会躲山的光斑
开发语言·javascript·数码相机·microsoft·材质·cesium
ShineWinsu3 天前
对于Qt:QSound的解析
c++·qt·面试·音频·笔试·客户端·qsound
sean9083 天前
macOS Outlook(或者是Office系列?) App 打开时自动启动 Microsoft Autoupdate 处理
microsoft·macos·outlook·自动更新
刘大望3 天前
PCM 基础:声音如何变成数字
ffmpeg·音频·pcm·数字信号处理·嵌入式音频
总要学点什么3 天前
第127篇:河道流场水面——把整条河的流向烤进一张贴图
开发语言·javascript·microsoft·材质·贴图·cesium
保卫大狮兄8 天前
2026中小制造企业轻量化ERP选型观察:产供销协同路线、上线周期与ROI分析
windows·microsoft·erp·生产管理
钱栈up9 天前
微软商店把《我的世界》国区从 89 涨到 110.99:一个「小调价」里的平台定价权博弈
游戏·microsoft·raft