含辞未吐,声若幽兰,史上最强免费人工智能AI语音合成TTS服务微软Azure(Python3.10接入)

简介: 所谓文无第一,武无第二,云原生人工智能技术目前呈现三足鼎立的态势,微软,谷歌以及亚马逊三大巨头各擅胜场,不分伯仲,但目前微软Azure平台不仅仅只是一个PaaS平台,相比AWS,以及GAE,它应该是目前提供云计算人工智能服务最全面的一个平台,尤其是语音合成领域,论AI语音的平顺、自然以及拟真性,无平台能出其右。

所谓文无第一,武无第二,云原生人工智能技术目前呈现三足鼎立的态势,微软,谷歌以及亚马逊三大巨头各擅胜场,不分伯仲,但目前微软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语音系统完成了人工智能在语音合成这个细分市场的落地应用,为互联网领域内许多需要配音的业务节约了成本和时间。

相关文章
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
详解微软Copilot AI助手:Copilot官网入口_Copilot国内网站入口
微软 Copilot 是一种集成在多种 Microsoft 产品中的人工智能助手,旨在提高工作效率和创造力。它利用强大的机器学习和自然语言处理技术,能够理解用户的需求并提供实时的帮助和建议。以下是对微软 Copilot 的详细介绍。
|
16天前
|
存储 人工智能 自然语言处理
新手指南:微软ai助手Copilot国内如何使用?
微软 Copilot 是一款强大的 AI 助手,掌握一些技巧可以让你更好地利用它,提高效率和创造力,让你的工作和生活更加精彩!
|
23天前
|
机器学习/深度学习 人工智能 TensorFlow
神经网络深度剖析:Python带你潜入AI大脑,揭秘智能背后的秘密神经元
【9月更文挑战第12天】在当今科技飞速发展的时代,人工智能(AI)已深入我们的生活,从智能助手到自动驾驶,从医疗诊断到金融分析,其力量无处不在。这一切的核心是神经网络。本文将带领您搭乘Python的航船,深入AI的大脑,揭秘智能背后的秘密神经元。通过构建神经网络模型,我们可以模拟并学习复杂的数据模式。以下是一个使用Python和TensorFlow搭建的基本神经网络示例,用于解决简单的分类问题。
38 10
|
17天前
|
人工智能 自然语言处理 前端开发
基于ChatGPT开发人工智能服务平台
### 简介 ChatGPT 初期作为问答机器人,现已拓展出多种功能,如模拟面试及智能客服等。模拟面试功能涵盖个性化问题生成、实时反馈等;智能客服则提供全天候支持、多渠道服务等功能。借助人工智能技术,这些应用能显著提升面试准备效果及客户服务效率。 ### 智能平台的使用价值 通过自动化流程,帮助用户提升面试准备效果及提高客户服务效率。 ### 实现思路 1. **需求功能设计**:提问与接收回复。 2. **技术架构设计**:搭建整体框架。 3. **技术选型**:示例采用 `Flask + Template + HTML/CSS`。 4. **技术实现**:前端界面与后端服务实现。
36 1
|
21天前
|
Unix Shell Linux
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
9 1
|
24天前
|
机器学习/深度学习 人工智能 TensorFlow
深入骨髓的解析:Python中神经网络如何学会‘思考’,解锁AI新纪元
【9月更文挑战第11天】随着科技的发展,人工智能(AI)成为推动社会进步的关键力量,而神经网络作为AI的核心,正以其强大的学习和模式识别能力开启AI新纪元。本文将探讨Python中神经网络的工作原理,并通过示例代码展示其“思考”过程。神经网络模仿生物神经系统,通过加权连接传递信息并优化输出。Python凭借其丰富的科学计算库如TensorFlow和PyTorch,成为神经网络研究的首选语言。
27 1
|
2月前
|
人工智能 JavaScript Python
微软会用Python替代VBA吗?
微软会用Python替代VBA吗?
|
2月前
|
数据采集 机器学习/深度学习 人工智能
Python爬虫入门指南探索AI的无限可能:深度学习与神经网络的魅力
【8月更文挑战第27天】本文将带你走进Python爬虫的世界,从基础的爬虫概念到实战操作,你将学会如何利用Python进行网页数据的抓取。我们将一起探索requests库和BeautifulSoup库的使用,以及反爬策略的应对方法。无论你是编程新手还是有一定经验的开发者,这篇文章都将为你打开一扇通往数据抓取世界的大门。
|
25天前
|
机器学习/深度学习 人工智能 TensorFlow
神经网络入门到精通:Python带你搭建AI思维,解锁机器学习的无限可能
【9月更文挑战第10天】神经网络是开启人工智能大门的钥匙,不仅是一种技术,更是模仿人脑思考的奇迹。本文从基础概念入手,通过Python和TensorFlow搭建手写数字识别的神经网络,逐步解析数据加载、模型定义、训练及评估的全过程。随着学习深入,我们将探索深度神经网络、卷积神经网络等高级话题,并掌握优化模型性能的方法。通过不断实践,你将能构建自己的AI系统,解锁机器学习的无限潜能。
21 0
|
25天前
|
机器学习/深度学习 数据挖掘 TensorFlow
从数据小白到AI专家:Python数据分析与TensorFlow/PyTorch深度学习的蜕变之路
【9月更文挑战第10天】从数据新手成长为AI专家,需先掌握Python基础语法,并学会使用NumPy和Pandas进行数据分析。接着,通过Matplotlib和Seaborn实现数据可视化,最后利用TensorFlow或PyTorch探索深度学习。这一过程涉及从数据清洗、可视化到构建神经网络的多个步骤,每一步都需不断实践与学习。借助Python的强大功能及各类库的支持,你能逐步解锁数据的深层价值。
46 0

热门文章

最新文章

下一篇
无影云桌面