更新时间:2026-03-10 GMT+08:00
分享

语音合成

前提条件

初始化Client

初始化TtsCustomizationClient,其参数包括AuthInfo和SisConfig。

表1 AuthInfo

参数名称

是否必选

参数类型

描述

ak

是

String

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

sk

是

String

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

region

是

String

区域,如cn-north-4,参考终端节点。

projectId

是

String

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

endpoint

否

String

终端节点,参考终端节点。

表2 SisConfig

参数名称

是否必选

参数类型

描述

connectionTimeout

否

Integer

连接超时,默认10000,单位ms。

readTimeout

否

Integer

读取超时,默认10000,单位ms。

请求参数

请求类为TtsCustomRequest,详见表3。

表3 TtsCustomRequest

参数名称

是否必选

参数类型

描述

text

是

String

待合成的文本,文本长度限制不大于500字符。

audioFormat

否

String

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

pitch

否

Integer

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

speed

否

Integer

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

volume

否

Integer

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

sampleRate

否

String

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

property

否

String

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

isSaved

否

Boolean

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

savePath

否

String

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

响应参数

响应类为TtsCustomResponse,详见表3。调用失败处理方法请参见错误码。

表4 TtsResponse

参数名

是否必选

参数类型

说明

isSaved

否

String

是否将响应音频保存为本地文件。

savePath

否

String

保存本地的路径,如D:/test.wav。

result

是

Object

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

调用失败时无此字段。

表5 Result

参数名

是否必选

参数类型

说明

data

是

String

合成后生成的语音数据,以Base64编码格式返回。用户如需生成音频,需要将Base64编码解码成byte数组,再保存为wav音频。

代码示例

如下示例仅供参考,最新代码请前往SDK(websocket)章节获取并运行。

import com.huawei.sis.bean.AuthInfo;
import com.huawei.sis.bean.SisConfig;
import com.huawei.sis.bean.SisConstant;
import com.huawei.sis.bean.request.TtsCustomRequest;
import com.huawei.sis.bean.response.TtsCustomResponse;
import com.huawei.sis.client.TtsCustomizationClient;
import com.huawei.sis.exception.SisException;
import com.huawei.sis.util.JsonUtils;

/**
 * 语音合成的demo
 *
 * Copyright 2021 Huawei Technologies Co.,Ltd.
 */
public class TtsCustomizationDemo {

  private static final int DEFAULT_PITCH = 0;
  private static final int DEFAULT_SPEED = 0;
  private static final int DEFAULT_VOLUME = 50;

  // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
  // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
  private String ak = System.getenv("HUAWEICLOUD_SDK_AK");
  private String sk = System.getenv("HUAWEICLOUD_SDK_SK");


  private String region = "";    // 区域,如cn-north-1、cn-north-4
  private String projectId = ""; // 项目id。登录管理控制台,鼠标移动到右上角的用户名上,在下拉列表中选择我的凭证,在项目列表中查看项目id。多项目时,展开“所属区域”,从“项目ID”列获取子项目ID。

  private String text = "";      // 待合成的文本
  private String path = "";      // 设置本地音频保存路径.可选择不保存到本地。需具体到文件,如D:/test.wav


  /**
   * 用于语音合成参数设置,例如发声人、音高、语速、音量、采样率、连接超时。所有参数均可以不设置,采用默认。
   *
   * @param request 语音合成请求
   */
  private void setParameter(TtsCustomRequest request) {

    // 设置语音格式,可选mp3,pcm等,默认wav
    request.setAudioFormat("wav");
    // 音高,[-500, 500], 默认0
    request.setPitch(DEFAULT_PITCH);
    // 语速,[-500, 500],默认0
    request.setSpeed(DEFAULT_SPEED);
    // 音量,[0, 100],默认50
    request.setVolume(DEFAULT_VOLUME);
    // 当前支持8000和16000,默认8000
    request.setSampleRate("8000");
    // 设置property,特征字符串,{language}_{speaker}_{domain}
    request.setProperty("chinese_xiaoyu_common");

    // 设置返回数据是否保存,默认不保存。若保存,则需要设置保存路径,如D:/1.wav
    request.setSaved(true);
    request.setSavePath(path);
  }

  /**
   * 定义config,所有参数可选,设置超时时间等。
   *
   * @return SisConfig
   */
  private SisConfig getConfig() {
    SisConfig config = new SisConfig();
    // 设置连接超时,默认10000ms
    config.setConnectionTimeout(SisConstant.DEFAULT_CONNECTION_TIMEOUT);
    // 设置读取超时,默认10000ms
    config.setReadTimeout(SisConstant.DEFAULT_READ_TIMEOUT);
    // 设置代理, 一定要确保代理可用才启动此设置。 代理初始化也可用不加密的代理,new ProxyHostInfo(host, port);
    // ProxyHostInfo proxy = new ProxyHostInfo(host, port, username, password);
    // config.setProxy(proxy);
    return config;
  }

  /**
   * 根据文本和api,获取生成的音频数据
   */
  private void ttsCustomDemo() {
    try {
      // 1. 初始化TtsCustomizationClient
      // 定义authInfo,根据ak,sk,region, projectId.
      AuthInfo authInfo = new AuthInfo(ak, sk, region, projectId);
      // 定义config,所有参数可选,设置超时时间。
      SisConfig config = getConfig();
      // 根据authInfo和config,构造TtsCustomizationClient
      TtsCustomizationClient tts = new TtsCustomizationClient(authInfo, config);

      // 2. 配置请求
      TtsCustomRequest request = new TtsCustomRequest(text);
      // 设置参数,所有参数均可选,如果要保存合成音频文件,需要在request设置
      setParameter(request);

      // 3. 发送请求,获取响应。具体结果可通过response.getXX获取。
      TtsCustomResponse response = tts.getTtsResponse(request);
      // 设置企业id, 可选
      // Map<String, String> headers = OKHttpClientUtils.getJsonHeaders();
      // headers.put(SisConstant.ENTERPRISE_PROJECT_ID_KEY, "your enterprise_id");
      // TtsCustomResponse response = tts.getTtsResponse(headers, request);

      System.out.println(JsonUtils.obj2Str(response, true));

    } catch (SisException e) {
      e.printStackTrace();
      System.out.println("error_code:" + e.getErrorCode() + "\nerror_msg:" + e.getErrorMsg());
    }

  }

  public static void main(String[] args) {
    TtsCustomizationDemo demo = new TtsCustomizationDemo();
    demo.ttsCustomDemo();
  }

}

相关文档