Ubuntu 创建微软Azure TTS服务
- 作者帖子
- Up::1
运行环境:Ubuntu+Python
1. 到微软官方创建一个Azure 免费账户,需要信用卡认证
2、Ubuntu端配置:
# sudo apt-get update
# sudo apt-get install python libssl1.0.0 libasound2
# pip install azure-cognitiveservices-speech3. 语音合成python源码
## 输入文件text.txt
## 输出文件file.wavfrom azure.cognitiveservices.speech import AudioDataStream, SpeechConfig, SpeechSynthesizer, SpeechSynthesisOutputFormat
from azure.cognitiveservices.speech.audio import AudioOutputConfig
import azure.cognitiveservices.speech as speechsdkspeech_key, service_region = “xxxxxxxxxxxxxxxxxxxxxxxxx”, “xxxxxx”
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)speech_config.speech_synthesis_language = “zh-CN”
speech_config.speech_synthesis_voice_name =”zh-CN-XiaoyouNeural”audio_config = AudioOutputConfig(filename=”file.wav”)
synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)with open(‘text.txt’, ‘r’,encoding=’utf-8′,errors=’ignore’) as f:
text = f.read()result = synthesizer.speak_text_async(text).get()
stream = AudioDataStream(result)
stream.save_to_wav_file(“file.wav”)4. 运行方式
# python test.py
# ./test.py - Up::0
python 脚本
平台环境: mac os 或linux
python 版本: python 3,安装pycharm ide客户端
#! /usr/bin/python3
# -*- coding: utf-8 -*-
# 微软人工智能azure 文本转真人语音
###
# Copyright (c) icrosoft Corporation
# All rights reserved.
# MIT License
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “”Software””), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###
import http.client, urllib.parse, json
from xml.etree import ElementTree
import wave# Note: new unified SpeechService API key and issue token uri is per region
# New unified SpeechService key
# Free: https://azure.microsoft.com/en-us/try/cognitive-services/?api=speech-services
# Paid: https://go.microsoft.com/fwlink/?LinkId=872236apiKey = “yourkey here”
params = “”
headers = {“Ocp-Apim-Subscription-Key”: apiKey}# AccessTokenUri = “https://eastus.api.cognitive.microsoft.com/sts/v1.0/issueToken”;
AccessTokenHost = “eastus.api.cognitive.microsoft.com”
path = “/sts/v1.0/issueToken”# Connect to server to get the Access Token
print(“Connect to server to get the Access Token”)
conn = http.client.HTTPSConnection(AccessTokenHost)
conn.request(“POST”, path, params, headers)
response = conn.getresponse()
print(response.status, response.reason)data = response.read()
conn.close()accesstoken = data.decode(“UTF-8”)
print(“Access Token: ” + accesstoken)#ShortName = ‘zh-CN-XiaoxiaoNeural’ # 每月 5000个 字符免费
ShortName =’zh-CN-YunyeNeural’ # 真人云野
body = ElementTree.Element(‘speak’, version=’1.0′)
body.set(‘{http://www.w3.org/XML/1998/namespace}lang’, ‘en-us’)
voice = ElementTree.SubElement(body, ‘voice’)
voice.set(‘{http://www.w3.org/XML/1998/namespace}lang’, ‘en-US’)
voice.set(‘{http://www.w3.org/XML/1998/namespace}gender’, ‘Male’)
#voice.set(‘name’, ‘Microsoft Server Speech Text to Speech Voice (en-US, Guy24KRUS)’)
voice.set(‘name’, ShortName)
#voice.text = ‘This is a demo to call microsoft text to speech service in Python.’#直接读取文件内容,用read() 方法能直接生成字符串。
# ubuntu 端自定义原始text文件路径:/home/yourname/Desktop/tts-source.txt
# mac 端如下
with open(‘/Users/yourname/PycharmProjects/text_source/tts-source.txt’,’r’,encoding=’utf-8′) as f:
voice.text = f.read()headers = {“Content-type”: “application/ssml+xml”,
“X-Microsoft-OutputFormat”: “riff-24khz-16bit-mono-pcm”,
“Authorization”: “Bearer ” + accesstoken,
“X-Search-AppId”: “07D3234E49CE426DAA29772419F436CA”,
“X-Search-ClientID”: “1ECFAE91408841A480F00935DC390960”,
“User-Agent”: “TTSForPython”}# Connect to server to synthesize the wave
print(“\nConnect to server to synthesize the wave”)
conn = http.client.HTTPSConnection(“eastus.tts.speech.microsoft.com”)
conn.request(“POST”, “/cognitiveservices/v1”, ElementTree.tostring(body), headers)
response = conn.getresponse()
print(response.status, response.reason)data = response.read()
conn.close()
print(“The synthesized wave length: %d” % (len(data)))f = wave.open(r”output.wav”, “wb”)
f.setnchannels(1)#单声道
f.setframerate(24000)#采样率
f.setsampwidth(2) # 2byte 16bit位宽
f.writeframes(data)
f.close()参考:
官方示例参考文档: link
- Up::0
1. 创建免费的tts 文本转语音服务,需要先创建Azure 免费账户: 点击这里
网页版tts的使用入口在这里: https://speech.microsoft.com/portal
选择speech audio–>voice gallery ,进行使用
2. 获取秘钥
2.1 搜索speech, 选择speech services
2.2 创建一个text-to-voice的免费服务
1> 选择 “认知服务”,添加一个新的 Speech 订阅 ,名称随便起
2> 位置选择 美东或者东南亚
3> 定价层选择 F0, 详见的定价规则: 参考2.3 管理密钥
2.4 复制密钥 和 endpoint
2.5 配置python源代码,参考上面的python代码。
- 配置密钥
- 配置endpoint
- 配置播音员的语音名字
选择播音员语音类型名字,参考如下:
因为xiaoxiao的语音属于神经网络语音
所以选择地区时要选择 神经网络可用区域 才能使用微软xiaoxiao语音
- 作者帖子
- 哎呀,回复话题必需登录。