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

相关推荐
枫叶丹42 小时前
【HarmonyOS 6.0】ArkUI Scroll组件新特性:手势缩放能力全解析
microsoft·华为·harmonyos
iceslime2 小时前
Windows10系统静音修复相关
windows·音频·修复
IOFsmLtzR5 小时前
Flink Agents 源码解读 --- (5) --- ActionExecutionOperator
microsoft·flink·wpf
小邓睡不饱耶7 小时前
花店花品信息管理系统开发实战:Python实现简易门店管理系统
服务器·python·microsoft
竹之却9 小时前
【Microsoft Store】解决微软商店无法打开,MicrosoftStore 初始化失败,请尝试刷新 或稍后返回
microsoft·微软商店·microsoft store
Wave84510 小时前
LVGL—组件介绍
microsoft
Ashmcracker11 小时前
Codex Desktop如何接入Azure OpenAI?AI Foundry部署GPT‑5.3‑codex 实操
人工智能·gpt·microsoft·azure
公子小六11 小时前
基于.NET的Windows窗体编程之WinForms布局简介
windows·microsoft·c#·.net
weitingfu12 小时前
Excel VBA 入门到精通(一):宏录制与 VBE 环境详解
microsoft
zhengyquan12 小时前
微软砸1.6万亿日元布局日本AI!
人工智能·microsoft