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

相关推荐
小满Autumn1 天前
WPF 入门:XAML 语法、布局与数据绑定
microsoft·c#·.net·wpf
VOOHU-沃虎1 天前
音频变压器选型指南:阻抗匹配、隔离耐压与低失真设计的工程实践
网络·音频
步步为营DotNet1 天前
解密.NET 11:C# 14 在客户端响应式编程的突破与实践
microsoft·c#·.net
问今域中1 天前
Spring AI结构化输出
windows·microsoft
189228048611 天前
NQ486固态MT29F16T08GSLDHL8-QM:D
大数据·人工智能·科技·microsoft·缓存
IT技术分享社区1 天前
微软Office 2024离线版安装指南与功能亮点介绍
microsoft·微软技术·office·电脑干货
郑寿昌2 天前
2026 全球 AI 工厂市场格局与发展趋势
大数据·人工智能·microsoft
qq_283720052 天前
RAG 重排序(Rerank)优化:BGE-Reranker、Cross-Encoder 实战详解
microsoft
Lucky_Turtle2 天前
【Azure】微软云文件存储
microsoft·flask·azure
松果财经2 天前
从“单点收付”到“跨国司库”,金融为何是出海深水区的关键变量?
人工智能·microsoft·金融