百度云语音合成的SDK用过了,感觉上面的语音还是太少了,为了找到更多的语音角色,我找了华为云语音合成。诶,你别说试了试还真可以哦 ……
我在示例代码里面做了少量改动,不过还有很多地方需要修改,一步一步来吧!
下面直接上代码!
# -*- coding: utf-8 -*-
from huaweicloud_sis.client.tts_client import TtsCustomizationClient
from huaweicloud_sis.bean.tts_request import TtsCustomRequest
from huaweicloud_sis.bean.sis_config import SisConfig
from huaweicloud_sis.exception.exceptions import ClientException
from huaweicloud_sis.exception.exceptions import ServerException
import json
def ttsc_example():
""" 语音合成demo """
ak = 'XXXXXXXXXXX'
sk = 'XXXXXXXXXX'
region = 'cn-east-3' # region,如cn-north-4
project_id = 'XXXXXXXX' # 同region一一对应。登录管理控制台,鼠标移动到右上角的用户名上,在下拉列表中选择我的凭证。在我的凭证页面,可以查看用户名、帐号名,在项目列表中查看项目。多项目时,展开“所属区域”,从“项目ID”列获取子项目ID。
###以下为添加部分可以自定义文件路径
file_path = input("请输入文件路径 \n")
with open(file_path,encoding='utf-8') as file_object:
contents = file_object.read()
###以上为读取指定路径下面TXT文本并传递给contents
text = contents # 待合成文本,不超过500字
dir_name = 'D:\\Document\\整理语音数据\\'
file_name = input('请输入要保持的文件名: \n')
#判断D盘下是否有document\整理输出数据,如何没有则创建
#以上为合成部分
path = dir_name + file_name + '.mp3' # 保存路径,需要具体到音频文件,如D:/test.wav,可在设置中选择不保存本地
# step1 初始化客户端
config = SisConfig()
config.set_connect_timeout(10) # 设置连接超时,单位s
config.set_read_timeout(10) # 设置读取超时,单位s
# 设置代理,使用代理前一定要确保代理可用。 代理格式可为[host, port] 或 [host, port, username, password]
# config.set_proxy(proxy)
ttsc_client = TtsCustomizationClient(ak, sk, region, project_id, sis_config=config)
# step2 构造请求
ttsc_request = TtsCustomRequest(text)
# 设置请求,所有参数均可不设置,使用默认参数
# 设置属性字符串, language_speaker_domain, 默认chinese_xiaoyan_common
ttsc_request.set_property('chinese_xiaoyan_common')
# 设置音频格式,默认wav,可选mp3和pcm
ttsc_request.set_audio_format('mp3')
# 设置采样率,8000 or 16000, 默认8000
ttsc_request.set_sample_rate('16000')
# 设置音量,[0, 100],默认50
ttsc_request.set_volume(50)
# 设置音高, [-500, 500], 默认0
ttsc_request.set_pitch(0)
# 设置音速, [-500, 500], 默认0
ttsc_request.set_speed(-50)
# 设置是否保存,默认False
ttsc_request.set_saved(True)
# 设置保存路径,只有设置保存,此参数才生效
ttsc_request.set_saved_path(path)
# step3 发送请求,返回结果。如果设置保存,可在指定路径里查看保存的音频。
result = ttsc_client.get_ttsc_response(ttsc_request)
#print(json.dumps(result, indent=2, ensure_ascii=False))
if __name__ == '__main__':
try:
ttsc_example()
print('转换成功!')
except ClientException as e:
print(e)
except ServerException as e:
print(e)
感觉吧,好像转化的语音比百度的好听点,还是说我有毛病!
我还取了个名字,菊花厂语音助手!加油抓紧时间改进!暂时不做操作界面了,就自己用好了