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

相关推荐
Leinwin15 小时前
Microsoft Azure 服务4月更新告示
人工智能·azure
TIF星空1 天前
【使用 C# 获取 USB 设备信息及进行通信】
开发语言·经验分享·笔记·学习·microsoft·c#
Anchenry2 天前
【QGIS二次开发】地图显示与交互-03
microsoft·交互
binggoling2 天前
实用工具:微软软件PowerToys(完全免费),实现多台电脑共享鼠标和键盘(支持window系统)
microsoft·计算机外设·电脑
rrokoko3 天前
微软向现实低头:悄悄延长Windows 10的Microsoft 365支持
microsoft
编程乐趣3 天前
推荐一个Winform开源的UI工具包
microsoft·ui·开源
-曾牛3 天前
Spring AI 集成 Mistral AI:构建高效多语言对话助手的实战指南
java·人工智能·后端·spring·microsoft·spring ai
云攀登者-望正茂4 天前
Azure 应用的托管身份与服务主体
azure·devops
溪饱鱼4 天前
第6章: SEO与交互指标
服务器·前端·microsoft
Robot2514 天前
「华为」人形机器人赛道投资首秀!
大数据·人工智能·科技·microsoft·华为·机器人