According to Baidu's Speech Recognition example, how C calls the C ++ library and calls the Speech Recognition

Source: Internet
Author: User

According to Baidu's Speech Recognition example, how C calls the C ++ library and calls the Speech Recognition

Just now, I was bored with Baidu's Speech Recognition RestAPI. I saw that the speech recognition Sample was written in C ++. There are three components in total: libcurl. a. libjson. a. base64.cpp: The Json: Reader does not reference the file during compilation. I want to rewrite this example with C. The call method of RestAPI is to first obtain the Token using the SKey, and then call the audio (for example, test. pcm) is encoded as a string and appended to the address through Base64 encoding. It is post to the speech recognition platform through curl and the audio result is returned in JSON format. When the author rewrite the code, the json component does not use libjson. a, but uses the cJSON component. It will show how to use cJSON in another article. The Base64 encoding function still uses the Base64.cpp file.

C language does not have the concept of classes. When Calling C ++ compiled libraries, C code must add a function that can be called by C in C ++ code. Which of the following is required for Audio Encoding?

Std: string base64_encode (unsigned char const * bytes_to_encode, unsigned int in_len)

This method is of the string type. The C standard does not have this type. Therefore, we need to convert string to char * When writing the method called by C. The author will not say that, so let's use the code.

 

 

# Ifdef _ cplusplus

Extern "C "{

# Endif

// Put the called method here

Char * callBase64Encode (unsigned char const * bytes_to_encode, unsigned int in_len)

{

Std: string ret;

Ret = base64_encode (bytes_to_encode, in_len );

 

Char * c;

Const int len = ret. length ();

C = new char [len + 1];

Strcpy (c, ret. c_str ());

Return c;

}

 

# Ifdef _ cplusplus

}

# Endif

Add the following code to the base64.cpp file: the code above defines the method that can be called by C language. The method starts with char *, because the code after obtaining the encoding value is passed

Char * c;

Const int len = ret. length ();

C = new char [len + 1];

Strcpy (c, ret. c_str ());

Return c;

These lines of code convert string type strings to char * type strings.

Add the following content to the base64.h header file:

# Ifdef _ cplusplus

Extern "C "{

# Endif

 

Char * callBase64Encode (unsigned char const * bytes_to_encode, unsigned int in_len );

 

# Ifdef _ cplusplus

}

# Endif

Reference the header file # include <base64.h> in the C code, and then use

G ++ base64.cpp-fPIC-shared-o libase64.so

Compile the base64 code into the libase64.so library file, and then use

Gcc cJSON. c sample. c-L.-lase64-lstdc ++-o sample-lm

Our sample. c code can call the libase64.so method. The result is as follows:


The json speech value is the encoded string of the audio.

 

Original address: http://mp.weixin.qq.com/s? _ Biz = MzA4NTU0OTU5MA ==& mid = 2247483660 & idx = 1 & sn = 8c02f0084d96de49596899484bd876e9 & chksm = workshop # rd

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.