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

相关推荐
数据的世界0113 小时前
C#权威指南第9课:方法
microsoft·c#·.net
E_ICEBLUE21 小时前
快速合并 Excel 工作表和文件:Java 实现
java·microsoft·excel
EllenShen1231 天前
(Azure)PGSQL和redis 连通性测试 --code 备份
redis·postgresql·azure
兴趣使然黄小黄1 天前
【AI-agent】LangChain开发智能体工具流程
人工智能·microsoft·langchain
扶尔魔ocy2 天前
【QT自定义2D控件】QGraphics绘制仪表盘
数据库·qt·microsoft
YJlio2 天前
自动化实践(7.25):把 PsTools 接入 PowerShell / 批处理 / Ansible
microsoft·自动化·ansible
开开心心就好2 天前
微软官方出品:免费数据恢复工具推荐
网络·笔记·microsoft·pdf·word·音视频·symfony
Leinwin3 天前
微软加速在亚洲扩展云基础设施,推动区域数字化跨越式发展
microsoft
HelloRevit3 天前
快速入门 - Azure 数字孪生的 3D 场景工作室(预览版)入门
3d·flask·azure
天地之于壹炁兮3 天前
编程I/O入门指南:核心操作全解析
数据库·windows·microsoft