According to Baidu's speech recognition example, show C how to call C + + library

Source: Internet
Author: User

Just bored fiddling with a bit of Baidu's speech recognition Restapi, see the speech recognition sample is written in C + +, a total of three components, LIBCURL.A, Libjson.a, Base64.cpp, at compile time Json::reader undefined reference error. Think of using C to rewrite this example. Restapi is called by first using Skey to obtain tokens, and then the audio (for example: TEST.PCM) by BASE64 encoded into a string appended to the address, via curl to post to the speech recognition platform, return JSON to get audio results. When the author rewrites the code, the JSON component does not use LIBJSON.A, but instead uses the Cjson component, which in another article shows how to use the Cjson. The BASE64 encoding function is also used to Base64.cpp this file.

Because C does not have the concept of a class, C code calls the C + + compiled library to add a section of the C + + code that can be called by C. What you need to encode your audio is

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

This method, see is a string type, C standard does not have this type, so we write the method called by C to convert the string into char*, the author is not very likely to say, so the code to speak it.

#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

In the Base64.cpp file, add the above code, the above code is defined can be called by the C language method, the method at the beginning of the definition is char*, because the latter part of the code gets to the encoded value after the

Char *c;

const int len = Ret.length ();

c = new Char[len + 1];

strcpy (c, Ret.c_str ());

return C;

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

Then add 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

Method, refer to # include <base64.h> this header file in C code, and then pass

g++ Base64.cpp-fpic-shared-o libase64.so

Compile the base64 code into a libase64.so link library file and pass

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 speech value of JSON is the string after which the audio is encoded.

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

According to Baidu's speech recognition example, show C how to call C + + library

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.