funasr-onnx-online-rtf.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
  3. * MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. #ifndef _WIN32
  6. #include <sys/time.h>
  7. #else
  8. #include <win_func.h>
  9. #endif
  10. #include <glog/logging.h>
  11. #include "funasrruntime.h"
  12. #include "tclap/CmdLine.h"
  13. #include "com-define.h"
  14. #include <iostream>
  15. #include <fstream>
  16. #include <sstream>
  17. #include <vector>
  18. #include <atomic>
  19. #include <mutex>
  20. #include <thread>
  21. #include <map>
  22. #include "audio.h"
  23. using namespace std;
  24. std::atomic<int> wav_index(0);
  25. std::mutex mtx;
  26. bool is_target_file(const std::string& filename, const std::string target) {
  27. std::size_t pos = filename.find_last_of(".");
  28. if (pos == std::string::npos) {
  29. return false;
  30. }
  31. std::string extension = filename.substr(pos + 1);
  32. return (extension == target);
  33. }
  34. void runReg(FUNASR_HANDLE asr_handle, vector<string> wav_list, vector<string> wav_ids, int audio_fs,
  35. float* total_length, long* total_time, int core_id) {
  36. struct timeval start, end;
  37. long seconds = 0;
  38. float n_total_length = 0.0f;
  39. long n_total_time = 0;
  40. // init online features
  41. FUNASR_HANDLE online_handle=FunASROnlineInit(asr_handle);
  42. // warm up
  43. for (size_t i = 0; i < 10; i++)
  44. {
  45. int32_t sampling_rate_ = audio_fs;
  46. funasr::Audio audio(1);
  47. if(is_target_file(wav_list[0].c_str(), "wav")){
  48. if(!audio.LoadWav2Char(wav_list[0].c_str(), &sampling_rate_)){
  49. LOG(ERROR)<<"Failed to load "<< wav_list[0];
  50. exit(-1);
  51. }
  52. }else if(is_target_file(wav_list[0].c_str(), "pcm")){
  53. if (!audio.LoadPcmwav2Char(wav_list[0].c_str(), &sampling_rate_)){
  54. LOG(ERROR)<<"Failed to load "<< wav_list[0];
  55. exit(-1);
  56. }
  57. }else{
  58. if (!audio.FfmpegLoad(wav_list[0].c_str(), true)){
  59. LOG(ERROR)<<"Failed to load "<< wav_list[0];
  60. exit(-1);
  61. }
  62. }
  63. char* speech_buff = audio.GetSpeechChar();
  64. int buff_len = audio.GetSpeechLen()*2;
  65. int step = 9600*2;
  66. bool is_final = false;
  67. string final_res="";
  68. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  69. if (sample_offset + step >= buff_len - 1) {
  70. step = buff_len - sample_offset;
  71. is_final = true;
  72. } else {
  73. is_final = false;
  74. }
  75. FUNASR_RESULT result = FunASRInferBuffer(online_handle, speech_buff+sample_offset, step, RASR_NONE, NULL, is_final, sampling_rate_);
  76. if (result)
  77. {
  78. FunASRFreeResult(result);
  79. }
  80. }
  81. }
  82. while (true) {
  83. // 使用原子变量获取索引并递增
  84. int i = wav_index.fetch_add(1);
  85. if (i >= wav_list.size()) {
  86. break;
  87. }
  88. int32_t sampling_rate_ = audio_fs;
  89. funasr::Audio audio(1);
  90. if(is_target_file(wav_list[i].c_str(), "wav")){
  91. if(!audio.LoadWav2Char(wav_list[i].c_str(), &sampling_rate_)){
  92. LOG(ERROR)<<"Failed to load "<< wav_list[i];
  93. exit(-1);
  94. }
  95. }else if(is_target_file(wav_list[i].c_str(), "pcm")){
  96. if (!audio.LoadPcmwav2Char(wav_list[i].c_str(), &sampling_rate_)){
  97. LOG(ERROR)<<"Failed to load "<< wav_list[i];
  98. exit(-1);
  99. }
  100. }else{
  101. if (!audio.FfmpegLoad(wav_list[i].c_str(), true)){
  102. LOG(ERROR)<<"Failed to load "<< wav_list[i];
  103. exit(-1);
  104. }
  105. }
  106. char* speech_buff = audio.GetSpeechChar();
  107. int buff_len = audio.GetSpeechLen()*2;
  108. int step = 9600*2;
  109. bool is_final = false;
  110. string final_res="";
  111. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  112. if (sample_offset + step >= buff_len - 1) {
  113. step = buff_len - sample_offset;
  114. is_final = true;
  115. } else {
  116. is_final = false;
  117. }
  118. gettimeofday(&start, NULL);
  119. FUNASR_RESULT result = FunASRInferBuffer(online_handle, speech_buff+sample_offset, step, RASR_NONE, NULL, is_final, sampling_rate_);
  120. gettimeofday(&end, NULL);
  121. seconds = (end.tv_sec - start.tv_sec);
  122. long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  123. n_total_time += taking_micros;
  124. if (result)
  125. {
  126. string msg = FunASRGetResult(result, 0);
  127. final_res += msg;
  128. LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << msg;
  129. float snippet_time = FunASRGetRetSnippetTime(result);
  130. n_total_length += snippet_time;
  131. FunASRFreeResult(result);
  132. }
  133. else
  134. {
  135. LOG(ERROR) << ("No return data!\n");
  136. }
  137. }
  138. LOG(INFO) << "Thread: " << this_thread::get_id() << ", Final results " << wav_ids[i] << " : " << final_res;
  139. }
  140. {
  141. lock_guard<mutex> guard(mtx);
  142. *total_length += n_total_length;
  143. if(*total_time < n_total_time){
  144. *total_time = n_total_time;
  145. }
  146. }
  147. FunASRUninit(online_handle);
  148. }
  149. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  150. {
  151. if (value_arg.isSet()){
  152. model_path.insert({key, value_arg.getValue()});
  153. LOG(INFO)<< key << " : " << value_arg.getValue();
  154. }
  155. }
  156. int main(int argc, char *argv[])
  157. {
  158. google::InitGoogleLogging(argv[0]);
  159. FLAGS_logtostderr = true;
  160. TCLAP::CmdLine cmd("funasr-onnx-online-rtf", ' ', "1.0");
  161. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  162. TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
  163. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  164. TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "true (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "true", "string");
  165. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
  166. TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "true (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "true", "string");
  167. TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
  168. TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
  169. TCLAP::ValueArg<std::int32_t> thread_num("", THREAD_NUM, "multi-thread num for rtf", true, 0, "int32_t");
  170. cmd.add(model_dir);
  171. cmd.add(quantize);
  172. cmd.add(vad_dir);
  173. cmd.add(vad_quant);
  174. cmd.add(punc_dir);
  175. cmd.add(punc_quant);
  176. cmd.add(wav_path);
  177. cmd.add(audio_fs);
  178. cmd.add(thread_num);
  179. cmd.parse(argc, argv);
  180. std::map<std::string, std::string> model_path;
  181. GetValue(model_dir, MODEL_DIR, model_path);
  182. GetValue(quantize, QUANTIZE, model_path);
  183. GetValue(vad_dir, VAD_DIR, model_path);
  184. GetValue(vad_quant, VAD_QUANT, model_path);
  185. GetValue(punc_dir, PUNC_DIR, model_path);
  186. GetValue(punc_quant, PUNC_QUANT, model_path);
  187. GetValue(wav_path, WAV_PATH, model_path);
  188. struct timeval start, end;
  189. gettimeofday(&start, NULL);
  190. FUNASR_HANDLE asr_handle=FunASRInit(model_path, 1, ASR_ONLINE);
  191. if (!asr_handle)
  192. {
  193. LOG(ERROR) << "FunASR init failed";
  194. exit(-1);
  195. }
  196. gettimeofday(&end, NULL);
  197. long seconds = (end.tv_sec - start.tv_sec);
  198. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  199. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  200. // read wav_path
  201. vector<string> wav_list;
  202. vector<string> wav_ids;
  203. string default_id = "wav_default_id";
  204. string wav_path_ = model_path.at(WAV_PATH);
  205. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  206. wav_list.emplace_back(wav_path_);
  207. wav_ids.emplace_back(default_id);
  208. }
  209. else if(is_target_file(wav_path_, "scp")){
  210. ifstream in(wav_path_);
  211. if (!in.is_open()) {
  212. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  213. return 0;
  214. }
  215. string line;
  216. while(getline(in, line))
  217. {
  218. istringstream iss(line);
  219. string column1, column2;
  220. iss >> column1 >> column2;
  221. wav_list.emplace_back(column2);
  222. wav_ids.emplace_back(column1);
  223. }
  224. in.close();
  225. }else{
  226. LOG(ERROR)<<"Please check the wav extension!";
  227. exit(-1);
  228. }
  229. // 多线程测试
  230. float total_length = 0.0f;
  231. long total_time = 0;
  232. std::vector<std::thread> threads;
  233. int rtf_threds = thread_num.getValue();
  234. for (int i = 0; i < rtf_threds; i++)
  235. {
  236. threads.emplace_back(thread(runReg, asr_handle, wav_list, wav_ids, audio_fs.getValue(), &total_length, &total_time, i));
  237. }
  238. for (auto& thread : threads)
  239. {
  240. thread.join();
  241. }
  242. LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
  243. LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
  244. LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
  245. LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
  246. FunASRUninit(asr_handle);
  247. return 0;
  248. }