| | |
| | | #include "precomp.h" |
| | | #include <vector> |
| | | #ifdef __cplusplus |
| | | |
| | | extern "C" { |
| | |
| | | } |
| | | |
| | | // APIs for Offline-stream Infer |
| | | _FUNASRAPI FUNASR_RESULT FunOfflineInferBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, FUNASR_MODE mode, QM_CALLBACK fn_callback, int sampling_rate, std::string wav_format) |
| | | _FUNASRAPI FUNASR_RESULT FunOfflineInferBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, FUNASR_MODE mode, QM_CALLBACK fn_callback, const std::vector<std::vector<float>> &hw_emb, int sampling_rate, std::string wav_format) |
| | | { |
| | | funasr::OfflineStream* offline_stream = (funasr::OfflineStream*)handle; |
| | | if (!offline_stream) |
| | |
| | | int n_total = audio.GetQueueSize(); |
| | | float start_time = 0.0; |
| | | while (audio.Fetch(buff, len, flag, start_time) > 0) { |
| | | string msg = (offline_stream->asr_handle)->Forward(buff, len, true); |
| | | string msg = (offline_stream->asr_handle)->Forward(buff, len, true, hw_emb); |
| | | std::vector<std::string> msg_vec = funasr::split(msg, '|'); |
| | | p_result->msg += msg_vec[0]; |
| | | //timestamp |
| | | if(msg_vec.size() > 1){ |
| | | std::vector<std::string> msg_stamp = funasr::split(msg_vec[1], ','); |
| | | std::string cur_stamp = ""; |
| | | std::string cur_stamp = "["; |
| | | for(int i=0; i<msg_stamp.size()-1; i+=2){ |
| | | float begin = std::stof(msg_stamp[i])+start_time; |
| | | float end = std::stof(msg_stamp[i+1])+start_time; |
| | | cur_stamp += "["+std::to_string(begin)+","+std::to_string(end)+"],"; |
| | | cur_stamp += "["+std::to_string((int)(1000*begin))+","+std::to_string((int)(1000*end))+"]"; |
| | | if(i != msg_stamp.size()-2){ |
| | | cur_stamp +=","; |
| | | } |
| | | } |
| | | p_result->stamp += cur_stamp; |
| | | p_result->stamp += cur_stamp + "]"; |
| | | } |
| | | n_step++; |
| | | if (fn_callback) |
| | |
| | | return p_result; |
| | | } |
| | | |
| | | _FUNASRAPI FUNASR_RESULT FunOfflineInfer(FUNASR_HANDLE handle, const char* sz_filename, FUNASR_MODE mode, QM_CALLBACK fn_callback, int sampling_rate) |
| | | _FUNASRAPI FUNASR_RESULT FunOfflineInfer(FUNASR_HANDLE handle, const char* sz_filename, FUNASR_MODE mode, QM_CALLBACK fn_callback, const std::vector<std::vector<float>> &hw_emb, int sampling_rate) |
| | | { |
| | | funasr::OfflineStream* offline_stream = (funasr::OfflineStream*)handle; |
| | | if (!offline_stream) |
| | |
| | | int n_total = audio.GetQueueSize(); |
| | | float start_time = 0.0; |
| | | while (audio.Fetch(buff, len, flag, start_time) > 0) { |
| | | string msg = (offline_stream->asr_handle)->Forward(buff, len, true); |
| | | string msg = (offline_stream->asr_handle)->Forward(buff, len, true, hw_emb); |
| | | std::vector<std::string> msg_vec = funasr::split(msg, '|'); |
| | | p_result->msg += msg_vec[0]; |
| | | //timestamp |
| | | if(msg_vec.size() > 1){ |
| | | std::vector<std::string> msg_stamp = funasr::split(msg_vec[1], ','); |
| | | std::string cur_stamp = ""; |
| | | std::string cur_stamp = "["; |
| | | for(int i=0; i<msg_stamp.size()-1; i+=2){ |
| | | float begin = std::stof(msg_stamp[i])+start_time; |
| | | float end = std::stof(msg_stamp[i+1])+start_time; |
| | | cur_stamp += "["+std::to_string(begin)+","+std::to_string(end)+"],"; |
| | | cur_stamp += "["+std::to_string((int)(1000*begin))+","+std::to_string((int)(1000*end))+"]"; |
| | | if(i != msg_stamp.size()-2){ |
| | | cur_stamp +=","; |
| | | } |
| | | } |
| | | p_result->stamp += cur_stamp; |
| | | p_result->stamp += cur_stamp + "]"; |
| | | } |
| | | |
| | | n_step++; |
| | | if (fn_callback) |
| | | fn_callback(n_step, n_total); |
| | |
| | | return p_result; |
| | | } |
| | | |
| | | _FUNASRAPI const std::vector<std::vector<float>> CompileHotwordEmbedding(FUNASR_HANDLE handle, std::string &hotwords) { |
| | | funasr::OfflineStream* offline_stream = (funasr::OfflineStream*)handle; |
| | | std::vector<std::vector<float>> emb; |
| | | if (!offline_stream) |
| | | return emb; |
| | | return (offline_stream->asr_handle)->CompileHotwordEmbedding(hotwords); |
| | | } |
| | | |
| | | // APIs for 2pass-stream Infer |
| | | _FUNASRAPI FUNASR_RESULT FunTpassInferBuffer(FUNASR_HANDLE handle, FUNASR_HANDLE online_handle, const char* sz_buf, int n_len, std::vector<std::vector<std::string>> &punc_cache, bool input_finished, int sampling_rate, std::string wav_format, ASR_TYPE mode) |
| | | { |