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

相关推荐
嵌入式×边缘AI:打怪升级日志1 小时前
韦东山STM32_HAl库入门教程(SPI)学习笔记[09]内容
stm32·嵌入式硬件·microsoft
CodeCraft Studio1 小时前
图像处理控件Aspose.Imaging教程:使用 C# 将 SVG 转换为 EMF
图像处理·microsoft·c#·svg·aspose·图片格式转换·emf
初九之潜龙勿用5 小时前
技术与情感交织的一生 (十一)
服务器·笔记·microsoft·印象笔记
卓豪终端管理18 小时前
微软WSUS替代方案
microsoft
云攀登者-望正茂20 小时前
Azure DevOps — Kubernetes 上的自托管代理 — 第 5 部分
kubernetes·azure·devops
SomeOtherTime2 天前
操作系统数据格式相关(AI回答)
服务器·windows·microsoft
张人玉3 天前
WinForm之ListBox 控件
服务器·windows·microsoft
xiaopengbc3 天前
[Windows] 微软.Net运行库离线合集包 Microsoft .Net Packages AIO v13.05.25
windows·microsoft·.net
qq_316837753 天前
String boot 接入 azure云TTS
python·flask·azure
Dxy12393102164 天前
报错:selenium.common.exceptions.ElementNotInteractableException: Message
selenium·测试工具·microsoft