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

相关推荐
合作小小程序员小小店1 天前
桌面开发,在线%信息管理%系统,基于vs2022,c#,winform,sql server数据。
开发语言·数据库·sql·microsoft·c#
合作小小程序员小小店1 天前
桌面开发,物业管理系统开发,基于C#,winform,mysql数据库
开发语言·数据库·sql·mysql·microsoft·c#
Azure DevOps1 天前
Azure DevOps Server 2022.2 补丁(Patch 7)
运维·microsoft·azure·devops
kaozhengpro1 天前
Microsoft AZ-104 認試介紹|Azure Administrator(完整指南 + 最新考綱)
microsoft·azure
合作小小程序员小小店1 天前
桌面开发,在线%物品代送,代接管理%系统,基于vs2022,c#,winform,sql server数据。
开发语言·数据库·sql·microsoft·c#
合作小小程序员小小店2 天前
桌面开发,点餐管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·c#
phyit2 天前
外媒实测微软 Win11 文件管理器预加载:35MB 内存换极速启动
microsoft
合作小小程序员小小店2 天前
桌面开发,在线%医院管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·sql·microsoft·c#
合作小小程序员小小店2 天前
桌面开发,下午茶甜品管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·c#
z***67772 天前
【Golang】——Gin 框架中的表单处理与数据绑定
microsoft·golang·gin