Fork me on GitHub

TTS微软Azure

微软Azure

https://portal.azure.com/ 注册后绑定visa卡
image

创建微软语音服务

创建资源
image
选择AI应用和代理 -》 语音
image
输入语音服务必填项
image
然后点击创建
image
创建完成后如下
image
点击转到资源

语音服务

image
点击转到SpeechStudio
image
语音库
image
没有有声内容创作,切换到旧外观
image
出现有声内容创作
image

SDK

开发文档:
https://learn.microsoft.com/en-us/azure/ai-services/speech-service/get-started-text-to-speech?tabs=macos&pivots=programming-language-csharp
Github示例代码:
https://github.com/Azure-Samples/cognitive-services-speech-sdk

Python代码调用

VSCode下载Azure AI Speech Toolkit
image

Python代码调用API

安装azure包

pip install azure-cognitiveservices-speech
import os
import azure.cognitiveservices.speech as speechsdk

# This example requires environment variables named "SPEECH_KEY" and "ENDPOINT"
# Replace with your own subscription key and endpoint, the endpoint is like : "https://YourServiceRegion.api.cognitive.microsoft.com"
speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), endpoint=os.environ.get('ENDPOINT'))
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='zh-CN-XiaoxiaoNeural'

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 endpoint values?")
posted @ 2025-09-08 20:26  秋夜雨巷  阅读(35)  评论(0)    收藏  举报