根據百度的語音識別例子,展示C如何調用C++庫


剛才無聊擺弄了一下百度的語音識別RestAPI,看到語音識別的Sample是用C++寫的,一共有三個組件,libcurl.a、libjson.a、base64.cpp,在編譯時出現Json::Reader未定義引用的錯誤。想着還是用C重寫一下這個例子吧。RestAPI的調用方式是先用過SKey獲得Token,然后把音頻(例如:test.pcm)通過Base64編碼成字符串附加到地址中,通過curl的方式post到語音識別平台,返回JSON獲取音頻結果。作者重寫代碼時json組件沒有用libjson.a,而是用的cJSON組件,會在另一篇文章展示如何用cJSON。Base64的編碼功能還是用的Base64.cpp這個文件。

因為C語言沒有類的概念,C代碼在調用C++編譯的庫的時候要在C++代碼中添加一段可以被C調用的函數。在對音頻編碼時需要用到的是

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

這個方法,看到是string類型,C標准沒有這個類型,所以我們在寫被C調用的方法時要把string轉換成char*,作者不太會說,所以還是用代碼來說話吧。


 

 

#ifdef __cplusplus

extern "C" {

#endif

//把被調用的方法放在這里

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


在base64.cpp文件中添加上面的代碼,上面的代碼就是定義可以被C語言調用的方法,方法開頭定義的是char*,因為代碼的后部分獲取到編碼的值后通過

  char *c;

  const int len = ret.length();

  c = new char[len + 1];

  strcpy(c, ret.c_str());

  return c;

這幾行代碼將string類型的字符串轉換成了char*類型的字符串。

然后在base64.h頭文件中添加


#ifdef __cplusplus

extern "C" {

#endif

 

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

 

#ifdef __cplusplus

}

#endif


上方法,在C代碼中引用#include <base64.h>這個頭文件,然后通過

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

把base64代碼編譯成libase64.so鏈接庫文件,再通過

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

我們的sample.c代碼就可以調用libase64.so的方法了,結果如下:


json的speech值就是對音頻進行編碼后的字符串。

 

微信原文地址:http://mp.weixin.qq.com/s?__biz=MzA4NTU0OTU5MA==&mid=2247483660&idx=1&sn=8c02f0084d96de49596899484bd876e9&chksm=9fd77675a8a0ff63e0f61cc1e77e0a4a6e2fcc2ed62dcf8e5d75e9d20826bbc93c4679908594#rd


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM