使用实时语音合成
前提条件
- 确保已按照配置CPP环境(Windows)配置完毕。
- 请参考SDK(websocket)获取最新版本SDK包。
初始化Client
初始化RttsClient,其参数包括AuthInfo
请求参数
请求类为RttsRequest,详见表 RttsRequest。
通过set方法可以设置具体参数,详见表 RttsRequest设置参数
方法名称 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
SetAudioFormat |
否 |
String |
设置语音格式,默认pcm。 |
SetAudioProperty |
否 |
String |
设置语音合成特征字符串,{language}_{speaker}_{domain},即“语种_人员标识_领域”。默认chinese_xiaoyan_common 。详见API文档。 |
SetSampleRate |
否 |
String |
设置采样率:8000、16000,默认8000。 |
SetPitch |
否 |
Integer |
设置音高,-500~500,默认0。 |
SetVolume |
否 |
Integer |
设置音量,0~100,默认50。 |
SetSpeed |
否 |
Integer |
设置语速,-500~500,默认0。 |
示例代码
如下示例仅供参考,最新代码请前往SDK(websocket)章节获取并运行。
#include "IoUtil.h" #include "RttsClient.h" #include "RttsRequest.h" void OnRttsConnect() { std::cout << "now rtts client Connect success" << std::endl; } void OnRttsStart(std::string text) { std::cout << "now rtts client receive start response: " << text << std::endl; } void OnRttsEnd(std::string text) { std::cout << "now rtts client receive end response: " << text << std::endl; } void OnRttsClose() { std::cout << "now rtts client receive Close" << std::endl; } void OnRttsError(std::string text) { std::cout << "now rtts client receive error: " << text << std::endl; } void OnRttsBinary(std::string binaryData) { // data content can be avaliable by data() method, data length can be avaliable by size() method // const char* data = binaryData.data(); // int dataLength = binaryData.size(); std::cout << "now rtts client receive binary data " << binaryData.size() << std::endl; } void RttsTest() { // 1. config parameter // 1.1 init authInfo // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK std::string ak = GetEnv("HUAWEICLOUD_SDK_AK"); std::string sk = GetEnv("HUAWEICLOUD_SDK_SK"); std::string region = ""; std::string projectId = ""; AuthInfo authInfo(ak, sk, region, projectId); // 1.2 config Connect parameter HttpConfig httpConfig; httpConfig.SetReadTimeout(20000); httpConfig.SetConnectTimeout(20000); // 1.3 config callback, callback function are optional, if not set, it will use function in RttsListner WebsocketService::ptr websocketServicePtr = websocketpp::lib::make_shared<WebsocketService>(); websocketServicePtr->SetOnConnectFunc(OnRttsConnect); // Connect success callback websocketServicePtr->SetOnStartFunc(OnRttsStart); // receive start response callback websocketServicePtr->SetOnEndFunc(OnRttsEnd); // receive end response callback websocketServicePtr->SetOnCloseFunc(OnRttsClose); // Close callback websocketServicePtr->SetOnErrorFunc(OnRttsError); // receive error callback websocketServicePtr->SetOnBinaryFunc(OnRttsBinary); // receive binary callback // 1.3 option, use RttsListener, which can save file; You can edit RttsListener.h to finish your own business //WebsocketService::ptr websocketServicePtr = websocketpp::lib::make_shared<WebsocketService>(); //RttsListener rttsListener; //rttsListener.SetSaved(true); //rttsListener.SetFilePath("d:/test5.pcm"); //websocketServicePtr->SetRttsListener(rttsListener); // 1.4 config request parameter std::string text = AnsiToUtf8("华为致力于把数字世界带入每个人每个家庭每个组织,构建万物互联的智能世界。"); RttsRequest request(text); request.SetAudioFormat("pcm"); request.SetVolume(50); request.SetSpeed(0); request.SetPitch(0); request.SetSampleRate("8000"); request.SetAudioProperty("chinese_xiaoyan_common"); // 2. init client RttsClient* rttsClient = new RttsClient(authInfo, websocketServicePtr, httpConfig); // 3. send request rttsClient->Synthesis(request); // wait for save file, if setSaved false in rttsListener or don't use rttsListener, it can be removed. std::this_thread::sleep_for(std::chrono::milliseconds(2000)); delete rttsClient; } int main(){ RttsTest(); return 0; }