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

相关推荐
askah664415 小时前
mfc140u.dll丢失怎么办? mfc140u.dll文件缺失的修复技巧
windows·经验分享·游戏·microsoft·dll修复
中游鱼21 小时前
解决 Hypack 安装不能正常运行的引导及微软 VC++ 运行库 VCRedist
c++·microsoft·hypack·vc++运行库
小蜗的房子1 天前
SQL Server 2022安装要求(硬件、软件、操作系统等)
运维·windows·sql·学习·microsoft·sqlserver·操作系统
SandyXu011 天前
C#、C和C++的主要区别
c语言·c++·microsoft·c#
Ares-Wang1 天前
net core Autofac 替换默认的服务容器 DI,微软自动的容器 不支持命名选项的
android·microsoft
qq5652219612 天前
c# 判断字符串是否是 decimal
windows·microsoft·c#
HEX9CF2 天前
【Linux】安装并配置 Microsoft SQL Server 数据库(Ubuntu 22.04)
linux·数据库·microsoft
flashman9112 天前
python读word中的表格和插入表格
开发语言·python·microsoft·自动化·word
flashman9112 天前
python在word的页脚插入页码
python·microsoft·c#·自动化·word
getaxiosluo3 天前
使用uniapp使用音乐播放组件网易云
前端·vue.js·uni-app·音频