14 if (doc.HasParseError())
17 std::cout <<
"JSON解析错误" << code << std::endl;
23 token_ = vConfidence.GetString();
24 std::cout <<
"token from url_response json is :" <<
token_ << std::endl;
37 curl_global_init(CURL_GLOBAL_ALL);
59 std::cout <<
"URL is : " << url << std::endl;
60 CURL* curl = curl_easy_init();
62 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
63 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);
64 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
65 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
writefunc);
67 CURLcode res_curl = curl_easy_perform(curl);
69 if (res_curl != CURLE_OK)
71 std::cout <<
"perform curl error: " << res <<
"," << curl_easy_strerror(res_curl) << std::endl;
80 curl_easy_cleanup(curl);
95 CURL* curl = curl_easy_init();
100 std::cout <<
"runAsrAndRecog:: url = :" << url << std::endl;
104 struct curl_slist* headerlist = NULL;
107 headerlist = curl_slist_append(headerlist, header.c_str());
108 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
109 curl_easy_setopt(curl, CURLOPT_POST, 1);
110 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);
111 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
112 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
114 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
pcm_data_.
size);
115 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
writefunc);
116 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &
rec_result_);
118 CURLcode res_curl = curl_easy_perform(curl);
119 std::cout <<
"Baidu online speech recognition result is :" <<
rec_result_ << std::endl;
120 curl_slist_free_all(headerlist);
123 curl_easy_cleanup(curl);
130 size_t result_len = size * nmemb;
131 int is_new = (*result == NULL);
134 *result = (
char*)malloc(result_len + 1);
137 std::cout <<
"realloc failure!\n";
140 memcpy(*result, ptr, result_len);
141 (*result)[result_len] =
'\0';
145 size_t old_size = strlen(*result);
146 *result = (
char*)realloc(*result, result_len + old_size);
149 std::cout <<
"realloc failure!\n";
152 memcpy(*result + old_size, ptr, result_len);
153 (*result)[result_len + old_size] =
'\0';
181 std::string save_file =
base_path_ +
"/cache/pcm" + save_file_name;
182 std::ofstream pcm_file(save_file, std::ofstream::binary);
189 int success_code = 0;
191 std::cout <<
"-----------Start ASR Recording Thread --------" << std::endl;
199 std::cout <<
"\nNo active record device find! " << std::endl;
203 std::cout <<
"The total number of active input devices is : " <<
getInputDeviceNum() << std::endl;
226 std::cout <<
"ERROR:buf_new realloc error!" << std::endl;
234 if (enable_audio_save)
237 save_pcm_to_file.detach();
245 if (doc.HasParseError())
248 std::cout <<
"JSON解析错误" << code << std::endl;
251 if (doc.HasMember(
"result"))
static size_t writefunc(void *ptr, size_t size, size_t nmemb, char **result)
libcurl 返回回调函数.
FileOperation pcm_file_operation
struct DataBuff getOneChannelData()
从双声道数据中分离出单声道数据.
struct DataBuff startRecord()
struct AsrConfig asr_config
void praseToken()
从json中解析出token值.
void initAndConfigAsr()
百度语音识别模块的初始化,设置asr相关参数.
std_msgs::Header * header(M &m)
struct recorder * asr_record_
void setAsrParams(const std::string base_path, const std::string pcm_file, const int channel)
设置语音模块需外部传入的路径等参数.
int setRecordDuration(const float duration_time)
std::string setFileName(std::string file_type)
void uninitAsr()
一次识别结束后释放资源.
void stopRecordThroughMIC()
关闭录音设备.
百度语音识别模块接口头文件. TODO: 还需要添加版权、版本等信息
const std::string API_TOKEN_URL
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
struct DataBuff pcm_data_
std::string resultFromJson()
从完整的json语音识别结果中解析出需要的字符串结果.
ParseErrorCode
Error code of parsing.
void initRecord(struct recorder *rec, record_dev_id dev, WAVEFORMATEX *fmt)
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
record_dev_id getDefaultInputDevice()
char * runAsrAndRecog()
将pcm_data_结构体里存储的音频用于百度在线语音识别并获取识别结果.
void speechGetToken()
获取百度在线语音识别的Token.
const std::string base_path
struct DataBuff recordThroughMIC(const float record_time, bool enable_audio_save)
录音接口.
std::string online_final_result
void saveRecordDataToFile()
把录音数据存入/cache/pcm/目录下的pcm文件里,以次序和时间命名.
common definitions and configuration
std::string pcm_file_path
void getPcmFileData()
读取pcm文件里的音频数据,并将数据的内容和数据大小存入pcm_data_结构体.
RecordAlsaAPI record_alsa
void getPCMData(struct DataBuff pcm_buff)
获取pcm数据接口.
struct DataBuff readFileAsDatabuffer(const std::string file_path)