含辞未吐,声若幽兰,史上最强免费人工智能AI语音合成TTS服务微软Azure(Python3.10接入)
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
所谓文无第一武无第二云原生人工智能技术目前呈现三足鼎立的态势微软谷歌以及亚马逊三大巨头各擅胜场不分伯仲但目前微软Azure平台不仅仅只是一个PaaS平台相比AWS以及GAE它应该是目前提供云计算人工智能服务最全面的一个平台尤其是语音合成领域论AI语音的平顺、自然以及拟真性无平台能出其右。
本次我们通过Python3.10版本接入Azure平台语音合成接口打造一款本地的TTS服务(文本转语音:Text To Speech)。
准备工作
首先根据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 speechsdk
import os
speech_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 speechsdk
import os
speech_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 speechsdk
import os
speech_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文件
ls
output.wav
如此音频文件就可以被保存起来留作以后使用了。
语音调优
默认AI语音听多了难免会有些索然寡味之感幸运的是Azure平台提供了语音合成标记语言 (SSML) 它可以改善合成语音的听感。
根据Azure官方文档https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/speech-synthesis-markup
通过调整语音的角色以及样式来获取定制化的声音
语音 样式 角色
en-GB-RyanNeural1 cheerful, chat 不支持
en-GB-SoniaNeural1 cheerful, sad 不支持
en-US-AriaNeural chat, customerservice, narration-professional, newscast-casual, newscast-formal, cheerful, empathetic, angry, sad, excited, friendly, terrified, shouting, unfriendly, whispering, hopeful 不支持
en-US-DavisNeural chat, angry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering 不支持
en-US-GuyNeural newscast, angry, cheerful, sad, excited, friendly, terrified, shouting, unfriendly, whispering, hopeful 不支持
en-US-JaneNeural angry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering 不支持
en-US-JasonNeural angry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering 不支持
en-US-JennyNeural assistant, chat, customerservice, newscast, angry, cheerful, sad, excited, friendly, terrified, shouting, unfriendly, whispering, hopeful 不支持
en-US-NancyNeural angry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering 不支持
en-US-SaraNeural angry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering 不支持
en-US-TonyNeural angry, cheerful, excited, friendly, hopeful, sad, shouting, terrified, unfriendly, whispering 不支持
es-MX-JorgeNeural1 cheerful, chat 不支持
fr-FR-DeniseNeural1 cheerful, sad 不支持
fr-FR-HenriNeural1 cheerful, sad 不支持
it-IT-IsabellaNeural1 cheerful, chat 不支持
ja-JP-NanamiNeural chat, customerservice, cheerful 不支持
pt-BR-FranciscaNeural calm 不支持
zh-CN-XiaohanNeural5 calm, fearful, cheerful, disgruntled, serious, angry, sad, gentle, affectionate, embarrassed 不支持
zh-CN-XiaomengNeural1、5 chat 不支持
zh-CN-XiaomoNeural5 embarrassed, calm, fearful, cheerful, disgruntled, serious, angry, sad, depressed, affectionate, gentle, envious YoungAdultFemale, YoungAdultMale, OlderAdultFemale, OlderAdultMale, SeniorFemale, SeniorMale, Girl, Boy
zh-CN-XiaoruiNeural5 calm, fearful, angry, sad 不支持
zh-CN-XiaoshuangNeural5 chat 不支持
zh-CN-XiaoxiaoNeural5 assistant, chat, customerservice, newscast, affectionate, angry, calm, cheerful, disgruntled, fearful, gentle, lyrical, sad, serious, poetry-reading 不支持
zh-CN-XiaoxuanNeural5 calm, fearful, cheerful, disgruntled, serious, angry, gentle, depressed YoungAdultFemale, YoungAdultMale, OlderAdultFemale, OlderAdultMale, SeniorFemale, SeniorMale, Girl, Boy
zh-CN-XiaoyiNeural1、5 angry, disgruntled, affectionate, cheerful, fearful, sad, embarrassed, serious, gentle 不支持
zh-CN-XiaozhenNeural1、5 angry, disgruntled, cheerful, fearful, sad, serious 不支持
zh-CN-YunfengNeural1、5 angry, disgruntled, cheerful, fearful, sad, serious, depressed 不支持
zh-CN-YunhaoNeural1、2、5 advertisement-upbeat 不支持
zh-CN-YunjianNeural1、3、4、5 Narration-relaxed, Sports_commentary, Sports_commentary_excited 不支持
zh-CN-YunxiaNeural1、5 calm, fearful, cheerful, angry, sad 不支持
zh-CN-YunxiNeural5 narration-relaxed, embarrassed, fearful, cheerful, disgruntled, serious, angry, sad, depressed, chat, assistant, newscast Narrator, YoungAdultMale, Boy
zh-CN-YunyangNeural5 customerservice, narration-professional, newscast-casual 不支持
zh-CN-YunyeNeural5 embarrassed, calm, fearful, cheerful, disgruntled, serious, angry, sad YoungAdultFemale, YoungAdultMale, OlderAdultFemale, OlderAdultMale, SeniorFemale, SeniorMale, Girl, Boy
zh-CN-YunzeNeural1、5 calm, fearful, cheerful, disgruntled, serious, angry, sad, depressed, documentary-narration OlderAdultMale, SeniorMale
这里将语音文本改造为SSML的配置格式
import os
import azure.cognitiveservices.speech as speechsdk
speech_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语音系统完成了人工智能在语音合成这个细分市场的落地应用为互联网领域内许多需要配音的业务节约了成本和时间。
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |