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

相关推荐
※※冰馨※※7 小时前
【C#】无法安装程序包“DotSpatial.Symbology 4.0.656”
windows·microsoft·c#
电手8 小时前
微软宣布 Windows 11 将不再免费升级:升级需趁早
windows·microsoft
猿大师办公助手17 小时前
Weboffice在线Word权限控制:限制编辑,只读、修订、禁止复制等
vue.js·chrome·microsoft·word
labview_自动化1 天前
C#功能测试
windows·microsoft·c#
三月七(爱看动漫的程序员)2 天前
工具包组件和内置Agent组件
开发语言·前端·javascript·microsoft·ecmascript·facebook·新浪微博
觅远2 天前
python+Ollama库实现简单的AI模型问答交互
python·microsoft·交互
鲤籽鲲2 天前
C# ConcurrentBag 使用详解
windows·microsoft·c#·多线程
努力的小好3 天前
【Python 打造高效文件分类工具】
开发语言·python·microsoft
来自于狂人3 天前
解决 `pip install open-webui` 时的编译错误:Microsoft Visual C++ 14.0 或更高版本缺失
c++·microsoft·pip
weixin_307779133 天前
Azure Synapse Dedicated SQL Pool统计指定表中各字段的空值、空字符串或零值比例
数据仓库·sql·云计算·azure