更新时间:2023-11-20 GMT+08:00
分享

语音合成

前提条件

初始化Client

初始化TtsCustomizationClient详见表 TtsCustomizationClient初始化参数

表1 TtsCustomizationClient初始化参数

参数名称

是否必选

参数类型

描述

ak

String

用户的ak,可参考AK/SK认证

sk

String

用户的sk,可参考AK/SK认证

region

String

区域,如:cn-north-4。具体请参考终端节点

project_id

String

项目ID,同region一一对应,参考获取项目ID

service_endpoint

String

终端节点,一般使用默认即可。

sis_config

Object

详见表2

表2 SisConfig

参数名称

是否必选

参数类型

描述

connect_timeout

String

连接超时,默认10,单位s。

read_timeout

String

读取超时,默认10,单位s。

proxy

List

[host, port] 或 [host, port, username, password]。

请求参数

请求类为TtsCustomRequest,详见表3

表3 TtsCustomRequest

参数名称

是否必选

参数类型

描述

text

String

待合成的文本。

audio_format

String

待合成的音频格式,可选mp3,wav等,默认wav。具体信息请参见《API参考》语音合成章节。

pitch

Integer

音高,[-500,500] ,默认是0。

speed

Integer

语速,[-500,500] ,默认是0。

volume

Integer

音量,[0,100],默认是50。

sample_rate

String

采样率,支持“8000”、“16000”,默认“8000”。

model_property

String

特征字符串,{language}_{speaker}_{domain},默认chinese_xiaoyan_common。具体信息请参见《API参考》中语音合成章节。

saved

Boolean

是否选择合成的音频数据保存到本地,默认不保存。

saved_path

String

选择保存到本地的路径,需要具体到音频文件,如D:/test.wav。

响应参数

Python SDK响应结果为Json格式,详见表4。调用失败处理方法请参见错误码
表4 响应结果

参数名称

是否必选

参数类型

描述

result

Object

调用成功时为合成语音内容,请参考表5

trace_id

String

用于后台日志问题追溯。

is_saved

Boolean

是否保存为本地音频。

saved_path

String

保存音频的本地路径,只有在请求时saved参数设置为true才生效。

表5 Result

参数名称

是否必选

参数类型

说明

data

String

合成后生成的语音数据,以Base64编码格式返回。

代码示例

如下示例仅供参考,最新代码请前往获取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
# 鉴权参数
# 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; 
# 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SIS_AK/HUAWEICLOUD_SIS_SK/HUAWEICLOUD_SIS_PROJECT_ID。
ak = os.getenv("HUAWEICLOUD_SIS_AK")             # 从环境变量获取ak 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert ak is not None, "Please add ak in your develop environment"
sk = os.getenv("HUAWEICLOUD_SIS_SK")             # 从环境变量获取sk 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert sk is not None, "Please add sk in your develop environment"
project_id = ""     # project id 同region一一对应,参考https://support.huaweicloud.com/api-sis/sis_03_0008.html

def ttsc_example():
    """ 语音合成demo """
    

    region = ''         # region,如cn-north-4
    text = ''           # 待合成文本,不超过500字
    path = ''           # 保存路径,如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, 参考api文档
    ttsc_request.set_property('chinese_xiaoyan_common')
    # 设置音频格式,默认wav,可选mp3和pcm
    ttsc_request.set_audio_format('wav')
    # 设置采样率,8000 or 16000, 默认8000
    ttsc_request.set_sample_rate('8000')
    # 设置音量,[0, 100],默认50
    ttsc_request.set_volume(50)
    # 设置音高, [-500, 500], 默认0
    ttsc_request.set_pitch(0)
    # 设置音速, [-500, 500], 默认0
    ttsc_request.set_speed(0)
    # 设置是否保存,默认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()
    except ClientException as e:
        print(e)
    except ServerException as e:
        print(e)
分享:

    相关文档

    相关产品