本次,我们通过Python3.10版本接入Azure平台语音合成接口,打造一款本地的TTS做事(文本转语音:Text To Speech)。

准备事情

史上最强免费人工智能AI语音合成TTS做事微软Azure(Python3.10)_不支撑_语音 云服务

首先根据Azure平台官方文档:https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/get-started-text-to-speech?tabs=macos%2Cterminal&pivots=programming-language-python

在平台上创建免费订阅做事:https://azure.microsoft.com/zh-cn/free/cognitive-services/

免费订阅成功后,进入资源创建环节,这里我们访问网址,创建免费的语音资源:https://portal.azure.com/#create/Microsoft.CognitiveServicesSpeechServices

这里把稳订阅选择免费试用,利用区域选择东亚,如果在国外可以选择国外的对应区域。

创建语音做事资源成功后,转到资源组列表,点击获取资源秘钥:

须要把稳的是,任何时候都不要将秘钥进行传播,或者将秘钥写入代码并且提交版本。

这里相对稳妥的办法是将秘钥写入本地系统的环境变量中。

Windows系统利用如下命令:

setx COGNITIVE_SERVICE_KEY 您的秘钥

Linux系统利用如下命令:

export COGNITIVE_SERVICE_KEY=您的秘钥

Mac系统的bash终端:

编辑 ~/.bash_profile,然后添加环境变量

export COGNITIVE_SERVICE_KEY=您的秘钥

添加环境变量后,请从掌握台窗口运行 source ~/.bash_profile,使变动生效。

Mac系统的zsh终端:

编辑 ~/.zshrc,然后添加环境变量

export COGNITIVE_SERVICE_KEY=您的秘钥

如此,前期准备事情就完成了。

本地接入

确保本地Python环境版本3.10以上,然后安装Azure平台sdk:

pip3 install azure-cognitiveservices-speech

创建test.py文件:

import azure.cognitiveservices.speech as speechsdkimport osspeech_config = speechsdk.SpeechConfig(subscription=os.environ.get('KEY'), region="eastasia")audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)

这里定义语音的配置文件,通过os模块将上文环境变量中的秘钥取出利用,region便是新建语音资源时选择的地区,audio_config是选择当前打算机默认的音箱进行输出操作。

接着,根据官方文档的配置,选择一个语音机器人:https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=stt-tts#prebuilt-neural-voices

纯文本wuu-CN-XiaotongNeural1(女)wuu-CN-YunzheNeural1(男)不支持yue-CN中文(粤语,简体)yue-CN纯文本yue-CN-XiaoMinNeural1(女)yue-CN-YunSongNeural1(男)不支持zh-CN中文(普通话,简体)zh-CN音频 + 人工标记的脚本纯文本构造化文本短语列表zh-CN-XiaochenNeural4、5、6(女)zh-CN-XiaohanNeural2、4、5、6(女)zh-CN-XiaomengNeural1、2、4、5、6(女)zh-CN-XiaomoNeural2、3、4、5、6(女)zh-CN-XiaoqiuNeural4、5、6(女)zh-CN-XiaoruiNeural2、4、5、6(女)zh-CN-XiaoshuangNeural2、4、5、6、8(女)zh-CN-XiaoxiaoNeural2、4、5、6(女)zh-CN-XiaoxuanNeural2、3、4、5、6(女)zh-CN-XiaoyanNeural4、5、6(女)zh-CN-XiaoyiNeural1、2、4、5、6(女)zh-CN-XiaoyouNeural4、5、6、8(女)zh-CN-XiaozhenNeural1、2、4、5、6(女)zh-CN-YunfengNeural1、2、4、5、6(男)zh-CN-YunhaoNeural1、2、4、5、6(男)zh-CN-YunjianNeural1、2、4、5、6(男)zh-CN-YunxiaNeural1、2、4、5、6(男)zh-CN-YunxiNeural2、3、4、5、6(男)zh-CN-YunyangNeural2、4、5、6(男)zh-CN-YunyeNeural2、3、4、5、6(男)zh-CN-YunzeNeural1、2、3、4、5、6(男)神经网络定制声音专业版神经网络定制声音精简版(预览版)跨措辞语音(预览版)zh-CN-henan中文(中原河南普通话,中国大陆)不支持不支持zh-CN-henan-YundengNeural1(男)不支持zh-CN-liaoning中文(东北普通话,中国大陆)不支持不支持zh-CN-liaoning-XiaobeiNeural1(女)不支持zh-CN-shaanxi中文(中原陕西普通话,中国大陆)不支持不支持zh-CN-shaanxi-XiaoniNeural1(女)不支持zh-CN-shandong中文(冀鲁普通话,中国大陆)不支持不支持zh-CN-shandong-YunxiangNeural1(男)不支持zh-CN-sichuan中文(西南普通话,简体)zh-CN-sichuan纯文本zh-CN-sichuan-YunxiNeural1(男)不支持zh-HK中文(粤语,繁体)zh-HK纯文本zh-HK-HiuGaaiNeural4、5、6(女)zh-HK-HiuMaanNeural4、5、6(女)zh-HK-WanLungNeural1、4、5、6(男)神经网络定制声音专业版zh-TW中文(***普通话)zh-TW纯文本zh-TW-HsiaoChenNeural4、5、6(女)zh-TW-HsiaoYuNeural4、5、6(女)zh-TW-YunJheNeural4、5、6(男)神经网络定制声音专业版

单以中文语音论,可选择的范围还是相称广泛的。

连续编辑代码:

import azure.cognitiveservices.speech as speechsdkimport osspeech_config = speechsdk.SpeechConfig(subscription=os.environ.get('KEY'), region="eastasia")audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)speech_config.speech_synthesis_voice_name='zh-CN-XiaomoNeural'speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)text = "hello 大家好,这里是人工智能AI机器人在说话"speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()

这里我们选择zh-CN-XiaomoNeural作为默认AI语音,并且将text文本变量中的内容通过音箱进行输出。

如果乐意,我们也可以将语音输出为实体文件进行存储:

import azure.cognitiveservices.speech as speechsdkimport osspeech_config = speechsdk.SpeechConfig(subscription=os.environ.get('KEY'), region="eastasia")audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)file_config = speechsdk.audio.AudioOutputConfig(filename="./output.wav")speech_config.speech_synthesis_voice_name='zh-CN-XiaomoNeural'speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=file_config)text = "hello 大家好,这里是人工智能AI机器人在说话"speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()

这里指定file_config配置为脚本相对路径下的output.wav文件:

lsoutput.wav

如此,音频文件就可以被保存起来,留作往后利用了。

语腔调优

默认AI语音听多了,难免会有些索然寡味之感,幸运的是,Azure平台供应了语音合成标记措辞 (SSML) ,它可以改进合针言音的听感。

根据Azure官方文档:https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/speech-synthesis-markup

通过调度语音的角色以及样式来获取定制化的声音:

语音样式角色en-GB-RyanNeural1cheerful, chat不支持en-GB-SoniaNeural1cheerful, sad不支持en-US-AriaNeuralchat, customerservice, narration-professional, newscast-casual, newscast-formal, cheerful, empathetic, angry, sad, excited, friendly, terrified, shouting, unfriendly, whispering, hopeful不支持en-US-DavisNeuralchat, angry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering不支持en-US-GuyNeuralnewscast, angry, cheerful, sad, excited, friendly, terrified, shouting, unfriendly, whispering, hopeful不支持en-US-JaneNeuralangry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering不支持en-US-JasonNeuralangry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering不支持en-US-JennyNeuralassistant, chat, customerservice, newscast, angry, cheerful, sad, excited, friendly, terrified, shouting, unfriendly, whispering, hopeful不支持en-US-NancyNeuralangry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering不支持en-US-SaraNeuralangry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering不支持en-US-TonyNeuralangry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering不支持es-MX-JorgeNeural1cheerful, chat不支持fr-FR-DeniseNeural1cheerful, sad不支持fr-FR-HenriNeural1cheerful, sad不支持it-IT-IsabellaNeural1cheerful, chat不支持ja-JP-NanamiNeuralchat, customerservice, cheerful不支持pt-BR-FranciscaNeuralcalm不支持zh-CN-XiaohanNeural5calm, fearful, cheerful, disgruntled, serious, angry, sad, gentle, affectionate, embarrassed不支持zh-CN-XiaomengNeural1、5chat不支持zh-CN-XiaomoNeural5embarrassed, calm, fearful, cheerful, disgruntled, serious, angry, sad, depressed, affectionate, gentle, enviousYoungAdultFemale, YoungAdultMale, OlderAdultFemale, OlderAdultMale, SeniorFemale, SeniorMale, Girl, Boyzh-CN-XiaoruiNeural5calm, fearful, angry, sad不支持zh-CN-XiaoshuangNeural5chat不支持zh-CN-XiaoxiaoNeural5assistant, chat, customerservice, newscast, affectionate, angry, calm, cheerful, disgruntled, fearful, gentle, lyrical, sad, serious, poetry-reading不支持zh-CN-XiaoxuanNeural5calm, fearful, cheerful, disgruntled, serious, angry, gentle, depressedYoungAdultFemale, YoungAdultMale, OlderAdultFemale, OlderAdultMale, SeniorFemale, SeniorMale, Girl, Boyzh-CN-XiaoyiNeural1、5angry, disgruntled, affectionate, cheerful, fearful, sad, embarrassed, serious, gentle不支持zh-CN-XiaozhenNeural1、5angry, disgruntled, cheerful, fearful, sad, serious不支持zh-CN-YunfengNeural1、5angry, disgruntled, cheerful, fearful, sad, serious, depressed不支持zh-CN-YunhaoNeural1、2、5advertisement-upbeat不支持zh-CN-YunjianNeural1、3、4、5Narration-relaxed, Sports_commentary, Sports_commentary_excited不支持zh-CN-YunxiaNeural1、5calm, fearful, cheerful, angry, sad不支持zh-CN-YunxiNeural5narration-relaxed, embarrassed, fearful, cheerful, disgruntled, serious, angry, sad, depressed, chat, assistant, newscastNarrator, YoungAdultMale, Boyzh-CN-YunyangNeural5customerservice, narration-professional, newscast-casual不支持zh-CN-YunyeNeural5embarrassed, calm, fearful, cheerful, disgruntled, serious, angry, sadYoungAdultFemale, YoungAdultMale, OlderAdultFemale, OlderAdultMale, SeniorFemale, SeniorMale, Girl, Boyzh-CN-YunzeNeural1、5calm, fearful, cheerful, disgruntled, serious, angry, sad, depressed, documentary-narrationOlderAdultMale, SeniorMale

这里将语音文本改造为SSML的配置格式:

import osimport azure.cognitiveservices.speech as speechsdkspeech_config = speechsdk.SpeechConfig(subscription=os.environ.get('KEY'), region="eastasia")audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)file_config = speechsdk.audio.AudioOutputConfig(filename="./output.wav")speech_config.speech_synthesis_voice_name='zh-CN-XiaomoNeural'speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=file_config)#text = "hello 大家好,这里是人工智能AI机器人在说话"#speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()text = """ <speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="zh-CN"> <voice name="zh-CN-XiaoxiaoNeural"> <mstts:express-as style="lyrical" role="YoungAdultFemale" > <prosody rate="+12.00%"> hello 大家好,这里是刘悦的技能博客 大江东去,浪淘尽,千古风骚人物。
故垒西边,人性是,三国周郎赤壁。
乱石穿空,惊涛拍岸,卷起千堆雪。
江山如画,一时多少豪杰。
</prosody> </mstts:express-as> </voice> </speak>""" result = speech_synthesizer.speak_ssml_async(ssml=text).get()

通过利用style和role标记进行定制,同时利用rate属性来提升百分之十二的语速,从而让AI语音更加连贯顺畅。
把稳这里利用ssml=text来声明ssml格式的文本。

结语

人工智能AI语音系统完成了人工智能在语音合成这个细分市场的落地运用,为互联网领域内许多须要配音的业务节约了本钱和韶光。