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

相关推荐
小陈phd1 小时前
大语言模型实战(五)——大模型开发范式演进:从“调用API”到“多Agent复杂目标”
服务器·microsoft·语言模型
lzhdim2 小时前
C#应用程序取得当前目录和退出
开发语言·数据库·microsoft·c#
Filotimo_3 小时前
在java开发中:JSON序列化和JSON反序列化
java·microsoft·json
子春一24 小时前
Flutter 2025 可访问性(Accessibility)工程体系:从合规达标到包容设计,打造人人可用的无障碍应用
javascript·flutter·microsoft
YHPsophie6 小时前
HT4125低压CMOS缓冲门器件特性及应用深度解析
microsoft
CodeLinghu1 天前
路由:Agent能够根据条件动态决定工作流的下一步
人工智能·microsoft·ai·llm
渡我白衣1 天前
导论:什么是机器学习?——破除迷思,建立全景地图
人工智能·深度学习·神经网络·目标检测·microsoft·机器学习·自然语言处理
天庭鸡腿哥2 天前
国外软件,安装即时专业版!
stm32·microsoft·macos·everything
小尧嵌入式2 天前
Linux进程线程与进程间通信
linux·运维·服务器·c语言·开发语言·数据结构·microsoft
切糕师学AI2 天前
Azure RTOS ThreadX 简介
microsoft·嵌入式·azure·rtos