All Products
Search
Document Center

:SDK for C++

Last Updated:Apr 27, 2023

The recording file recognition service provides an SDK for C++. This topic describes how to install the SDK for C++ and provides sample code for you to use the SDK.

Note
  • The latest version of the SDK for C++ is 1.2.2, which was released on November 14, 2018.

  • Before you use this SDK, make sure that you understand how the SDK works. For more information, see Overview.

  • The SDK in this topic applies only to Intelligent Speech Interaction 2.0. For more information about how to activate Intelligent Speech Interaction 2.0 and obtain the AccessKey ID and AccessKey secret of your Alibaba Cloud account, see Activate Intelligent Speech Interaction.

SDK description

In the C++ demo of the recording file recognition service, the AlibabaNlsCommon::FileTrans method in the NlsCommonSdk.zip package is used to send Alibaba Cloud pctowap open platform (POP) API requests in a remote procedure call (RPC) style. You can send a recording file recognition request and query the recording file recognition result.

Download and installation

Download the SDK

To download the SDK for C++. The compressed package contains the following files or folders:

  • CMakeLists.txt: the CMakeList file of the demo project.

  • readme.txt: the SDK description.

  • release.log: the release notes.

  • version: the version number.

  • build.sh: the demo compilation script.

  • demo: the folder that contains demo.cpp files. The following table describes the file that is contained in the folder.

    File name

    Description

    fileTransDemo.cpp

    The demo of recording file recognition.

  • include: the folder that contains SDK header files and some third-party header files. The following table describes the files that are contained in the folder.

    File name

    Description

    openssl

    The folder that contains OpenSSL header files for Linux.

    curl

    The folder that contains curl header files.

    uuid

    The folder that contains UUID header files for Linux.

    json

    The folder that contains jsoncpp header files.

    Global.h

    The header file of global declaration.

    FileTrans.h

    The header file of recording file recognition.

  • lib: the folder that contains the curl and jsoncpp dynamic libraries.

    You can use the following software versions to load the libraries based on the operating system:

    • glibc 2.5 or later, and GCC 4 or GCC 5 for Linux

    • Visual Studio 2013 or Visual Studio 2015 for Windows

Compile and run the demo project

Note
  • In Linux, prepare the following runtime environment:

    • glibc 2.5 or later

    • GCC 4 or GCC 5

  • In Windows, you must build your own demo project and convert demo files to UTF-8 encoding with byte order mark (BOM). You can use the following dependent libraries:

    • openssl-1.0.2j

    • libuuid-1.0.3

    • curl-7.60.0

    • jsoncpp-0.10.6

Assume that the demo is decompressed to the path/to directory. Run the following commands on the Linux terminal to compile and run the program.

  • If the operating system supports CMake:

    1. Make sure that CMake 2.4 or later is installed in the local operating system.

    2. Run the cd path/to/sdk/lib command.

    3. Run the tar -zxvpf linux.tar.gz command.

    4. Run the cd path/to/sdk command.

    5. Run the ./build.sh command.

    6. Run the cd path/to/sdk/demo command.

    7. Run the ./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey command.

  • If the operating system does not support CMake, run the following commands in sequence:

    1. cd path/to/sdk/lib

    2. tar -zxvpf linux.tar.gz

    3. cd path/to/sdk/demo

    4. g++ -o fileTransDemo fileTransDemo.cpp -I path/to/sdk/include/ -L path/to/sdk/lib/linux -ljsoncpp -lssl -lcrypto -lcurl -luuid -lnlsCommonSdk -D_GLIBCXX_USE_CXX11_ABI=0

    5. export LD_LIBRARY_PATH=path/to/sdk/lib/linux/

    6. ./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey

Key object

FileTrans: the object of recording file recognition. You can use this object to obtain the recording file recognition result and error message.

Sample code

Note

Download the nls-sample-16k.wav file.

The recording file is a pulse-code modulation (PCM) encoded file with an audio sampling rate of 16,000 Hz. The demo uses a universal model in the Intelligent Speech Interaction console. If you use your own recording file, specify its audio coding format and audio sampling rate and select an appropriate model in the Intelligent Speech Interaction console. For more information about model setting, 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

// Send a recording file recognition request.
int fileTrans(const char* accessKeyId, const char* accessKeySecret, const char* appKey, const char* fileLink) {
    FileTrans request;

    /* Specify the AccessKey ID of your Alibaba Cloud account. */
    request.setAccessKeyId(accessKeyId);
    /* Specify the AccessKey secret of your Alibaba Cloud account. */
    request.setKeySecret(accessKeySecret);
    /* Specify the appkey of your project. */
    request.setAppKey(appKey);
    /* Specify the URL of the recording file. */
    request.setFileLinkUrl(fileLink);

    * Start the recording file recognition task. If the request is successful, 0 is returned. If the request fails, -1 is returned. */
    int ret = request.applyFileTrans();
    if (-1 == ret) {
        cout << "FileTrans failed: " << request.getErrorMsg() << endl; /* The error message. */
        return -1;
    } else {
        string result = request.getResult();

#ifdef _WIN32
        result = UTF8ToGBK(result);
#endif

                cout << "FileTrans successed: " << result << endl;

                return 0;
    }
}

int main(int argc, char* argv[]) {
    if (argc < 4) {
        cout << "FileTransDemo need 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://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav";

    fileTrans(accessKeyId, accessKeySecret, appKey, fileLink);

    return 0;
}