All Products
Search
Document Center

C++ demo

Last Updated: Oct 11, 2019

Note:

  • Read API reference before using the SDK.
  • The SDK in this topic applies only to Intelligent Speech Interaction V2.0 that Alibaba Cloud published on July 5, 2018. If you have activated an earlier version of Intelligent Speech Interaction, activate Intelligent Speech Interaction V2.0 before using the SDK.

Demo instructions

The C++ demo of the recording file recognition service uses the AlibabaNlsCommon::FileTrans method in the NlsCommonSdk.zip package to send 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.

SDK instructions

SDK download link: nlsCommonSDK

The latest version is V1.2.2 and published on November, 14, 2018.

The SDK directory is described as follows:

  • CMakeLists.txt: the CMakeList file of the demo project.
  • demo
Name Description
fileTransDemo.cpp The demo of recording file recognition.
  • include: the folder that contains SDK header files and part of third-party header files, as described in the following table.
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 SDK libraries and dependent third-party libraries. Depending on the operating system, you can select libnlsCommonSdk.so for Linux (runtime environment: glibc 2.5 or later, and GCC 4 or GCC 5), or nlsCommonSdk.dll for Windows (runtime environment: Visual Studio 2013 or Visual Studio 2015).
  • readme.txt: the SDK description.
  • release.log: the release notes.
  • version: the version number.
  • build.sh: the demo compilation script.

Note:

  1. In Linux, the minimum runtime environment requirements are as follows: glibc 2.5 or later, and GCC 4 or GCC 5.
  2. In Windows, you need to build your own demo project.

Dependent libraries

  1. openssl-1.0.2j
  2. libuuid-1.0.3
  3. curl-7.60.0
  4. jsoncpp-0.10.6

Demo compilation in Linux

  1. 1. Check the local operating system to ensure that CMake 2.4 or later is installed.
  2. 2. cd path/to/sdk/lib
  3. 3. tar -zxvpf linux.tar.gz
  4. 4. cd path/to/sdk
  5. 5. Run the [./build.sh] command to compile the demo.
  6. 6. After compilation, go to the demo directory and run the following command:
  7. ./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey
  8. If the operating system does not support CMake, you can run the following commands to manually compile the demo:
  9. 1: cd path/to/sdk/lib
  10. 2: tar -zxvpf linux.tar.gz
  11. 3: cd path/to/sdk/demo
  12. 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
  13. 5: export LD_LIBRARY_PATH=path/to/sdk/lib/linux/
  14. 6: ./fileTransDemo your-AccessKeyId your-AccessKeySecret your-appkey
  15. In Windows, you need to build your own project and convert demo files to UTF-8 encoding with byte order mark (BOM).

Key object

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

Complete sample code

Note: The recording file in the demo is a pulse-code modulation (PCM) file sampled at 16 kHz. The demo uses a universal model in the console. If you use your own recording file, specify its audio coding format and audio sampling rate and select an appropriate model in the console. For more information about how to select a model, see Manage projects in Console User Guide.

nls-sample-16k.wav


Alibaba Cloud authentication:When using the SDK, you need to use your Alibaba Cloud account to authenticate the client for all API operations. You need to specify the AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.

Sample code

  1. #include <iostream>
  2. #include <string>
  3. #include "FileTrans.h"
  4. #ifdef _WIN32
  5. #include <windows.h>
  6. #endif // _WIN32
  7. using std::string;
  8. using std::cout;
  9. using std::endl;
  10. using namespace AlibabaNlsCommon;
  11. #if defined _WIN32
  12. string UTF8ToGBK(const string &strUTF8) {
  13. int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
  14. unsigned short * wszGBK = new unsigned short[len + 1];
  15. memset(wszGBK, 0, len * 2 + 2);
  16. MultiByteToWideChar(CP_UTF8, 0, (char*)strUTF8.c_str(), -1, (wchar_t*)wszGBK, len);
  17. len = WideCharToMultiByte(CP_ACP, 0, (wchar_t*)wszGBK, -1, NULL, 0, NULL, NULL);
  18. char *szGBK = new char[len + 1];
  19. memset(szGBK, 0, len + 1);
  20. WideCharToMultiByte(CP_ACP, 0, (wchar_t*)wszGBK, -1, szGBK, len, NULL, NULL);
  21. string strTemp(szGBK);
  22. delete[] szGBK;
  23. delete[] wszGBK;
  24. return strTemp;
  25. }
  26. #endif
  27. // Send a recording file recognition request.
  28. int fileTrans(const char* accessKeyId, const char* accessKeySecret, const char* appKey, const char* fileLink) {
  29. FileTrans request;
  30. /* Specify the AccessKey ID of your Alibaba Cloud account. */
  31. request.setAccessKeyId(accessKeyId);
  32. /* Specify the AccessKey secret of your Alibaba Cloud account. */
  33. request.setKeySecret(accessKeySecret);
  34. /* Specify the appkey of the project created in the console. */
  35. request.setAppKey(appKey);
  36. /* Specify the URL of the recording file. */
  37. request.setFileLinkUrl(fileLink);
  38. /* Start the recording file recognition task. Return 0 if the request is successful or -1 if the request fails. */
  39. int ret = request.applyFileTrans();
  40. if (-1 == ret) {
  41. cout << "FileTrans failed: " << request.getErrorMsg() << endl; /* The error message. */
  42. return -1;
  43. } else {
  44. string result = request.getResult();
  45. #ifdef _WIN32
  46. result = UTF8ToGBK(result);
  47. #endif
  48. cout << "FileTrans successed: " << result << endl;
  49. return 0;
  50. }
  51. }
  52. int main(int argc, char* argv[]) {
  53. if (argc < 4) {
  54. cout << "FileTransDemo need params : <AccessKey Id> <AccessKey Secret> <app - key>" << endl;
  55. return -1;
  56. }
  57. const char* accessKeyId = argv[1];
  58. const char* accessKeySecret = argv[2];
  59. const char* appKey = argv[3];
  60. const char* fileLink = "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav";
  61. fileTrans(accessKeyId, accessKeySecret, appKey, fileLink);
  62. return 0;
  63. }