含辭未吐,聲若幽蘭,史上最強免費人工智能AI語音合成TTS服務微軟Azure(Python3.10接入)

語言: CN / TW / HK

本文正在參加「金石計劃 . 瓜分6萬現金大獎」

所謂文無第一,武無第二,雲原生人工智能技術目前呈現三足鼎立的態勢,微軟,谷歌以及亞馬遜三大巨頭各擅勝場,不分伯仲,但目前微軟Azure平台不僅僅只是一個PaaS平台,相比AWS,以及GAE,它應該是目前提供雲計算人工智能服務最全面的一個平台,尤其是語音合成領域,論AI語音的平順、自然以及擬真性,無平台能出其右。

本次,我們通過Python3.10版本接入Azure平台語音合成接口,打造一款本地的TTS服務(文本轉語音:Text To Speech)。

準備工作

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

在平台上創建免費訂閲服務:http://azure.microsoft.com/zh-cn/free/cognitive-services/

免費訂閲成功後,進入資源創建環節,這裏我們訪問網址,創建免費的語音資源:http://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是選擇當前計算機默認的音箱進行輸出操作。

接着,根據官方文檔的配置,選擇一個語音機器人:http://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官方文檔:http://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 = """




hello 大家好,這裏是劉悦的技術博客
大江東去,浪淘盡,千古風流人物。
故壘西邊,人道是,三國周郎赤壁。
亂石穿空,驚濤拍岸,捲起千堆雪。
江山如畫,一時多少豪傑。



"""

result = speech_synthesizer.speak_ssml_async(ssml=text).get() ```

通過使用style和role標記進行定製,同時使用rate屬性來提升百分之十二的語速,從而讓AI語音更加連貫順暢。注意這裏使用ssml=text來聲明ssml格式的文本。

結語

人工智能AI語音系統完成了人工智能在語音合成這個細分市場的落地應用,為互聯網領域內許多需要配音的業務節約了成本和時間。

「其他文章」