常用的TTS文字转语音有微软、谷歌、科大讯飞,这里分享一下微软的方法。
1、注册账号
地址:https://azure.microsoft.com/zh-cn/services/cognitive-services/text-to-speech/
2、得到终结点和秘钥
3、下载代码调取API
调用方式:本地发送HTTP请求--》微软服务器,认证后---》返回转换的音频到本地。
调用程序:https://github.com/Azure-Samples/Cognitive-Speech-TTS
Samples-Http文件夹下有各种语言如Android, C#, Java, Node.js, PHP, Python,Ruby
这里以Python举例
修改TTSSample.py文件
(1)apiKey = "Your api key goes here",把引号中的内容替换为STEP2图片中的**1或者**2。
(2)检查代码中终结点是否与STEP2图片中的终结点相同。比如程序中的AccessTokenHost = "westus.api.cognitive.microsoft.com",其中的“westus”就是终结点。再查看您账户中申请时分配的终结点名称,如STEP2图中的“westus”。若二者一致,则跳至下一步;若不一致,需要修改(只修改代码中的“westus”就行,“.api.com.......”不需要修改)。
同理,程序中“conn = http.client.HTTPSConnection("westus.tts.speech.microsoft.com")”做同样的修改。若您的终结点不是westus,则把程序中的westus替换为您的终结点名称。
终结点有3个:
美国西部 | https://westus.tts.speech.microsoft.com/cognitiveservices/v1 |
东亚 | https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1 |
北欧 | https://northeurope.tts.speech.microsoft.com/cognitiveservices/v1 |
这一条若不理解,可参看https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/how-to-text-to-speech。
(3)根据https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/rest-apis#authentication的说明,修改程序中X-Microsoft-OutputFormat和输出音频的属性(男声还是女声)。暂时输出音频只支持24khz的采样率。
(4)程序中的“voice.text”后的内容就是希望转成音频的文字内容,可以根据实际需求做修改。
(5)这时就能运行TTSSample.py。程序正常运行的返回值应该为"200 OK"。如果发生错误,则会有以下状态代码:
代码 | Description | 问题 |
---|---|---|
400 | 错误的请求 | 必需参数缺失、为空或为 null。 或者,传递给必需参数或可选参数的值无效。 常见问题是标头太长。 |
401 | 未授权 | 请求未经授权。 请检查确保订阅**或令牌有效。 |
413 | 请求实体太大 | SSML 输入超过了 1024 个字符。 |
502 | 错误的网关 | 网络或服务器端问题。 也可能表示标头无效。 |
假设前面的操作都没问题,也得到了200的返回值,那么转换后的音频在哪?答案是在程序的"data"这个变量中。"data"中的数据就是TTS转换后的音频,我们需要把它写为wav格式才能得到最终的音频。具体的操作见文末的代码。
附件:修改后的TTSSample.py文件(当然,您需要把apiKey修改为您的**;检查Python是否含有"wave"这个包),output.wav就是文本转换后的音频
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
###
#Copyright (c) Microsoft 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=872236
apiKey = "Your api key goes here"
params = ""
headers = {"Ocp-Apim-Subscription-Key": apiKey}
#AccessTokenUri = "https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken";
AccessTokenHost = "westus.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)
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.text = 'This is a demo to call microsoft text to speech service in Python.'
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("westus.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)#sample width 2 bytes(16 bits)
f.writeframes(data)
f.close()
文章评论