C++ demo

更新时间:
复制 MD 格式

This topic describes how to use the C++ software development kit (SDK) for Alibaba Cloud Intelligent Speech Interaction, including installation instructions and sample code.

Prerequisites

  • Latest version: 1.2.2. Release date: November 14, 2018.

  • Before you use the SDK, familiarize yourself with the API. For more information, see API Description.

  • You have activated Intelligent Speech Interaction and obtained an AccessKey ID and AccessKey secret. For more information, see Get started.

Example description

The recording file recognition example uses the AlibabaNlsCommon::FileTrans method of the nlsCommonSDK to submit recognition requests and query recognition results. It uses the Remote Procedure Call (RPC) style to make POP API calls.

Download and installation

Download the nlsCommonSDK. The downloaded file contains the following items:

  • CMakeLists.txt: The CMakeLists.txt file for the sample code-based project.

  • readme.txt: The SDK description file.

  • release.log: The update log.

  • Version: The version number.

  • build.sh: The demo compilation script.

  • demo: A sample file.

    File name

    Description

    fileTransDemo.cpp

    An example of recording file recognition.

  • 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 folder for OpenSSL header files. Used for Linux.

    curl

    The folder for curl header files.

    uuid

    The folder for UUID header files. Used for Linux.

    json

    The folder for jsoncpp header files.

    Global.h

    The header file for global declarations.

    FileTrans.h

    The header file for recording file recognition.

  • lib: Contains the curl and jsoncpp dynamic libraries.

    Use the following software versions to load the library files based on your platform:

    • Linux (Glibc 2.5 or later, and Gcc4 or Gcc5)

    • Windows (VS2013, VS2015)

    To compile and run the demo:

    Note
    • The following tools are required for installation on Linux:

      • Glibc 2.5 or later

      • Gcc4 or Gcc5

    • On Windows, you must build your own sample project. Change the sample file to the UTF-8 encoding format with a bill of materials (BOM). The following dependency libraries are required:

      • 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/to directory. You can run the following commands in the Linux terminal to compile and run the program.

  • CMake support:

    Make sure that CMake 2.4 or later is installed on your local system.

    1. Switch to the directory: cd path/to/sdk/lib.

    2. Decompress the file: tar -zxvpf linux.tar.gz.

    3. Switch to the directory: cd path/to/sdk.

    4. Run the compilation script: ./build.sh.

    5. Switch to the directory: cd path/to/sdk/demo.

    6. Run the sample program for file recognition: ./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey.

  • CMake is not supported:

    1. Switch to the directory: cd path/to/sdk/lib.

    2. Decompress the file: tar -zxvpf linux.tar.gz.

    3. Switch to the directory: cd path/to/sdk/demo.

    4. 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.

    5. Specify the library path: export LD_LIBRARY_PATH=path/to/sdk/lib/linux/.

    6. Run the sample program for file recognition: ./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey.

Key interface

FileTrans: The object for recording file recognition. You can use this object to retrieve the recognition result, failure information, and more.

Code example

Note
  • Download nls-sample-16k.wav.

  • The sample recording is a PCM file with a sample rate of 16000 Hz. The model that is set in the console is the general-purpose model. If you use other recordings, you must specify the corresponding encoding format and sample rate, and set the corresponding model in the console. For more information about model settings, see Manage projects.

  • Before you call the API, you must configure environment variables to read the access credentials. The environment variables for the AccessKey ID, AccessKey secret, and AppKey are NLS_AK_ENV, NLS_SK_ENV, and NLS_APPKEY_ENV respectively.

#include <stdlib.h>
#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

// Recording 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 project.*/
    request.setAppKey(appKey);
    /*Set the URL of the audio file.*/
    request.setFileLinkUrl(fileLink);
    /*Set the connection domain name. The default value is for the China (Shanghai) region. To use other regions, see the POP API call parameters for each region.*/
    request.setDomain("filetrans.cn-shanghai.aliyuncs.com");
    /*Set the connection region. The default is Shanghai.*/
    request.setRegionId("cn-shanghai");
  
    /*Start file recognition. Returns 0 on success, -1 on failure.*/
    int ret = request.applyFileTrans();
    if (-1 == ret) {
        cout << "FileTrans failed: " << request.getErrorMsg() << endl; /*Get the failure reason.*/
        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[]) {
    // The environment variables for the AccessKey ID, AccessKey secret, and AppKey of Intelligent Speech Interaction are NLS_AK_ENV, NLS_SK_ENV, and NLS_APPKEY_ENV.
    const char* accessKeyId = getenv("NLS_AK_ENV");
    const char* accessKeySecret = getenv("NLS_SK_ENV");
    const char* appKey = getenv("NLS_APPKEY_ENV");
    const char* fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";

    fileTrans(accessKeyId, accessKeySecret, appKey, fileLink);

    return 0;
}