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

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

  1. 史上最强Java NIO入门:担心从入门到放弃的,请读这篇!

    本文原题“<NIO 入门>,作者为“Gregory M. Travis”,他是<JDK 1.4 Tutorial>等书籍的作者. 1.引言 Java NIO是Java 1.4版 ...

  2. JVM面试题(史上最强、持续更新、吐血推荐)

    文章很长而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三部 ...

  3. Webbrowser控件史上最强技巧全集

    原文:Webbrowser控件史上最强技巧全集 Webbrowser控件史上最强技巧全集 VB调用webbrowser技巧集 1.获得浏览器信息: Private Sub Command1_Click ...

  4. 史上最强php生成pdf文件,html转pdf文件方法

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  5. 史上最强maven配置详情

    史上最强maven配置详情 优点 对第三方依赖库进行了统一的版本管理 统一了构建过程 统一了项目的目录结构 构建 清理 : mvn clear 编译 : mvn compile 测试 : mvn te ...

  6. 一文深入了解史上最强的Java堆内缓存框架Caffeine

    它提供了一个近乎最佳的命中率.从性能上秒杀其他一堆进程内缓存框架,Spring5更是为了它放弃了使用多年的GuavaCache 缓存,在我们的日常开发中用的非常多,是我们应对各种性能问题支持高并发的一 ...

  7. Java算法面试题(史上最强、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  8. 金九银十,史上最强 Java 面试题整理。

    以下会重新整理所有 Java 系列面试题答案.及各大互联网公司的面试经验,会从以下几个方面汇总,本文会长期更新. Java 面试篇 史上最全 Java 面试题,带全部答案 史上最全 69 道 Spri ...

  9. Android, IOS 史上最强多语言国际化,不仅第一次会尾随系统,并且会保存用户的语言设置

    劲爆消息,我提供源代码了.你能够先看完再下载.也能够先下载再看完, android源代码地址: https://github.com/hebiao6446/------Bluetooth-Androi ...

  10. 史上最强vue总结~万字长文---面试开发全靠它了

    vue框架篇 vue的优点 轻量级框架:只关注视图层,是一个构建数据的视图集合,大小只有几十kb: 简单易学:国人开发,中文文档,不存在语言障碍 ,易于理解和学习: 双向数据绑定:保留了angular ...

随机推荐

  1. P8548 小挖的买花 方法记录

    原题链接 小挖的买花 题目背景 小挖喜欢买花,但是 ta 太懒了!所以这个任务全权交给了你. 题目描述 花店里只有 \(n\) 株花,每一株花都有三个属性:价格 \(cost_i\).美丽度 \(be ...

  2. 我说HashMap初始容量是16,面试官让我回去等通知

    众所周知HashMap是工作和面试中最常遇到的数据类型,但很多人对HashMap的知识止步于会用的程度,对它的底层实现原理一知半解,了解过很多HashMap的知识点,却都是散乱不成体系,今天一灯带你一 ...

  3. MySQL 主从复制一主两从环境配置实战

    MySQL 初始化 MySQL 主从复制是指数据可以从一个 MySQL 数据库服务器主节点复制到一个或多个从节点.MySQL 默认采用异步复制方式;从节点可以复制主数据库中的所有数据库或者特定的数据库 ...

  4. 27.路由器Routers

    像一些reils这样的web框架提供自动生成urls的功能,但是Django没有 rest framework为django添加了这一功能,以一种简单.快速.一致的方式 routers必须配合view ...

  5. 9.异步redis

    在使用Python代码操作redis时候,连接.操作.断开都是网络IO #安装支持异步redis的模块 pip3 install aioredis async def execute(address, ...

  6. 2.签名&初始化&提交

      Git设置签名 签名的作用是区分不同操作者的身份,用户的签名信息在每一个版本的提交信息中能够看到, 以此确认本次提交是谁做的,git首次安装必须设置用户签名,否则无法提交代码 这里设置的用户签名和 ...

  7. vue2和vue3组合使用教程地址

    https://cn.vuejs.org/guide/essentials/watchers.html#eager-watchers

  8. python tcp select 多路复用

    1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # 文件名:tcpserver.py 4 5 import socket 6 import time 7 ...

  9. C# 多线程访问之 SemaphoreSlim(信号量)【C# 进阶】

    SemaphoreSlim 是对可同时访问某一共享资源或资源池的线程数加以限制的 Semaphore 的轻量替代,也可在等待时间预计很短的情况下用于在单个进程内等待. 由于 SemaphoreSlim ...

  10. 硬核剖析Java锁底层AQS源码,深入理解底层架构设计

    我们常见的并发锁ReentrantLock.CountDownLatch.Semaphore.CyclicBarrier都是基于AQS实现的,所以说不懂AQS实现原理的,就不能说了解Java锁. 上篇 ...