C++ demo
This topic describes how to use the C++ software development kit (SDK) for Alibaba Cloud Intelligent Speech Interaction. It provides installation instructions and code examples.
Prerequisites
The latest version is 1.2.2, which was released on November 14, 2018.
Before you use the SDK, read the API reference. For more information, see API reference.
You have activated Intelligent Speech Interaction and obtained an AccessKey ID and AccessKey secret. For more information, see Getting started.
Example description
The audio file recognition example uses the AlibabaNlsCommon::FileTrans object of the nlsCommonSDK to submit recognition requests and query results. The example uses Remote Procedure Call (RPC)-style POP API calls.
Download and installation
Download the nlsCommonSDK. The downloaded file contains the following items:
CMakeLists.txt: The CMakeLists file for the sample code-based project.
readme.txt: The SDK instructions.
release.log: The update history.
version: The version number.
build.sh: The compilation script for the demo.
demo: The sample file.
File name
Description
fileTransDemo.cpp
An audio file recognition example.
include the following: Contains the SDK header files and some third-party header files. The files are described in the following table.
File name
Description
openssl
The OpenSSL header file folder for Linux.
curl
The curl header file folder.
uuid
The UUID header file folder for Linux.
json
The jsoncpp header file folder.
Global.h
The global declaration header file.
FileTrans.h
The audio file recognition header file.
lib: Contains the curl and jsoncpp dynamic libraries.
The software versions required to load the library files vary based on your platform:
Linux (Glibc 2.5 or later, Gcc4 or Gcc5)
Windows (VS2013, VS2015)
To compile and run the program:
ImportantThe following tools are required to install the SDK on Linux:
Glibc 2.5 or later
Gcc4 or Gcc5
On Windows, you must build the sample project. Change the encoding format of the sample file to
UTF-8with a bill of materials (BOM). The following libraries are dependencies:openssl-1.0.2j
libuuid-1.0.3
curl-7.60.0
jsoncpp-0.10.6
Assume that the sample file is decompressed to the
path/todirectory. To compile and run the program, run the following commands in sequence in the Linux terminal.CMake support:
Make sure that CMake 2.4 or later is installed on your local system.
Switch to the directory:
cd path/to/sdk/lib.Decompress the file:
tar -zxvpf linux.tar.gz.Switch to the directory:
cd path/to/sdk.Run the compilation script:
./build.sh.Switch to the directory:
cd path/to/sdk/demo.You can run the file recognition sample program:
./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey.
CMake is not supported.
Switch to the directory:
cd path/to/sdk/lib.Decompress the file:
tar -zxvpf linux.tar.gz.Switch to the directory:
cd path/to/sdk/demo.Use the g++ command to compile the sample program:
g++ -o fileTransDemo fileTransDemo.cpp -I path/to/sdk/include/ -L path/to/sdk/lib/linux -ljsoncpp -lssl -lcrypto -lcurl -luuid -lalibabacloud-idst-common -D_GLIBCXX_USE_CXX11_ABI=0.Specify the library path:
export LD_LIBRARY_PATH=path/to/sdk/lib/linux/.Run the file recognition sample program:
./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey.
Key interface
FileTrans: The audio file recognition object. You can use this object to obtain recognition results, failure information, and other data.
Code example
Download nls-sample-16k.wav. The sample audio file uses the PCM encoding format with a 16000 Hz sample rate. In the console, the model is set to the general-purpose model. If you use a different audio file, you must enter the corresponding encoding format and sample rate, and set the corresponding model in the console. For more information about model settings, see Manage projects.
#include <iostream>
#include <string>
#include "FileTrans.h"
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
using std::string;
using std::cout;
using std::endl;
using namespace AlibabaNlsCommon;
#if defined _WIN32
string UTF8ToGBK(const string &strUTF8) {
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (char*)strUTF8.c_str(), -1, (wchar_t*)wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, (wchar_t*)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, (wchar_t*)wszGBK, -1, szGBK, len, NULL, NULL);
string strTemp(szGBK);
delete[] szGBK;
delete[] wszGBK;
return strTemp;
}
#endif
// Audio file recognition
int fileTrans(const char* accessKeyId, const char* accessKeySecret, const char* appKey, const char* fileLink) {
FileTrans request;
/* Set the AccessKey ID of your Alibaba Cloud account. */
request.setAccessKeyId(accessKeyId);
/* Set the AccessKey secret of your Alibaba Cloud account. */
request.setKeySecret(accessKeySecret);
/* Set the AppKey of your Alibaba Cloud account. */
request.setAppKey(appKey);
/* Set the URL of the audio file. */
request.setFileLinkUrl(fileLink);
/* Set the domain name for the Lite version. The default is the China (Shanghai) region. To use other regions, see the POP call parameters for each region. */
request.setDomain("speechfiletranscriberlite.cn-shanghai.aliyuncs.com");
/* Set the version for the Lite version connection. */
request.setServerVersion("2021-12-21");
/* Set the connection region. */
request.setRegionId("cn-shanghai");
/* Start audio file recognition. 0 is returned on success, and -1 on failure. */
int ret = request.applyFileTrans();
if (-1 == ret) {
cout << "FileTrans failed: " << request.getErrorMsg() << endl; /* Get the cause of the failure. */
return -1;
} else {
string result = request.getResult();
#ifdef _WIN32
result = UTF8ToGBK(result);
#endif
cout << "FileTrans succeeded: " << result << endl;
return 0;
}
}
int main(int argc, char* argv[]) {
if (argc < 4) {
cout << "FileTransDemo needs params: <AccessKey Id> <AccessKey Secret> <app-key>" << endl;
return -1;
}
const char* accessKeyId = argv[1];
const char* accessKeySecret = argv[2];
const char* appKey = argv[3];
const char* fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
fileTrans(accessKeyId, accessKeySecret, appKey, fileLink);
return 0;
}