funasr-onnx-online-vad.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <map>
  14. #include <vector>
  15. #include <glog/logging.h>
  16. #include "funasrruntime.h"
  17. #include "tclap/CmdLine.h"
  18. #include "com-define.h"
  19. #include "audio.h"
  20. using namespace std;
  21. bool is_target_file(const std::string& filename, const std::string target) {
  22. std::size_t pos = filename.find_last_of(".");
  23. if (pos == std::string::npos) {
  24. return false;
  25. }
  26. std::string extension = filename.substr(pos + 1);
  27. return (extension == target);
  28. }
  29. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  30. {
  31. if (value_arg.isSet()){
  32. model_path.insert({key, value_arg.getValue()});
  33. LOG(INFO)<< key << " : " << value_arg.getValue();
  34. }
  35. }
  36. void print_segs(vector<vector<int>>* vec, string &wav_id) {
  37. if((*vec).size() == 0){
  38. return;
  39. }
  40. string seg_out=wav_id + ": [";
  41. for (int i = 0; i < vec->size(); i++) {
  42. vector<int> inner_vec = (*vec)[i];
  43. if(inner_vec.size() == 0){
  44. continue;
  45. }
  46. seg_out += "[";
  47. for (int j = 0; j < inner_vec.size(); j++) {
  48. seg_out += to_string(inner_vec[j]);
  49. if (j != inner_vec.size() - 1) {
  50. seg_out += ",";
  51. }
  52. }
  53. seg_out += "]";
  54. if (i != vec->size() - 1) {
  55. seg_out += ",";
  56. }
  57. }
  58. seg_out += "]";
  59. LOG(INFO)<<seg_out;
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. google::InitGoogleLogging(argv[0]);
  64. FLAGS_logtostderr = true;
  65. TCLAP::CmdLine cmd("funasr-onnx-offline-vad", ' ', "1.0");
  66. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", true, "", "string");
  67. TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "false (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");
  68. 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");
  69. cmd.add(model_dir);
  70. cmd.add(quantize);
  71. cmd.add(wav_path);
  72. cmd.parse(argc, argv);
  73. std::map<std::string, std::string> model_path;
  74. GetValue(model_dir, MODEL_DIR, model_path);
  75. GetValue(quantize, QUANTIZE, model_path);
  76. GetValue(wav_path, WAV_PATH, model_path);
  77. struct timeval start, end;
  78. gettimeofday(&start, NULL);
  79. int thread_num = 1;
  80. FUNASR_HANDLE vad_hanlde=FsmnVadInit(model_path, thread_num);
  81. if (!vad_hanlde)
  82. {
  83. LOG(ERROR) << "FunVad init failed";
  84. exit(-1);
  85. }
  86. gettimeofday(&end, NULL);
  87. long seconds = (end.tv_sec - start.tv_sec);
  88. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  89. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  90. // read wav_path
  91. vector<string> wav_list;
  92. vector<string> wav_ids;
  93. string default_id = "wav_default_id";
  94. string wav_path_ = model_path.at(WAV_PATH);
  95. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  96. wav_list.emplace_back(wav_path_);
  97. wav_ids.emplace_back(default_id);
  98. }
  99. else if(is_target_file(wav_path_, "scp")){
  100. ifstream in(wav_path_);
  101. if (!in.is_open()) {
  102. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  103. return 0;
  104. }
  105. string line;
  106. while(getline(in, line))
  107. {
  108. istringstream iss(line);
  109. string column1, column2;
  110. iss >> column1 >> column2;
  111. wav_list.emplace_back(column2);
  112. wav_ids.emplace_back(column1);
  113. }
  114. in.close();
  115. }else{
  116. LOG(ERROR)<<"Please check the wav extension!";
  117. exit(-1);
  118. }
  119. // init online features
  120. FUNASR_HANDLE online_hanlde=FsmnVadOnlineInit(vad_hanlde);
  121. float snippet_time = 0.0f;
  122. long taking_micros = 0;
  123. for (int i = 0; i < wav_list.size(); i++) {
  124. auto& wav_file = wav_list[i];
  125. auto& wav_id = wav_ids[i];
  126. int32_t sampling_rate_ = -1;
  127. funasr::Audio audio(1);
  128. if(is_target_file(wav_file.c_str(), "wav")){
  129. int32_t sampling_rate_ = -1;
  130. if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
  131. LOG(ERROR)<<"Failed to load "<< wav_file;
  132. exit(-1);
  133. }
  134. }else if(is_target_file(wav_file.c_str(), "pcm")){
  135. if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
  136. LOG(ERROR)<<"Failed to load "<< wav_file;
  137. exit(-1);
  138. }
  139. }else{
  140. LOG(ERROR)<<"Wrong wav extension";
  141. exit(-1);
  142. }
  143. char* speech_buff = audio.GetSpeechChar();
  144. int buff_len = audio.GetSpeechLen()*2;
  145. int step = 800*2;
  146. bool is_final = false;
  147. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  148. if (sample_offset + step >= buff_len - 1) {
  149. step = buff_len - sample_offset;
  150. is_final = true;
  151. } else {
  152. is_final = false;
  153. }
  154. gettimeofday(&start, NULL);
  155. FUNASR_RESULT result = FsmnVadInferBuffer(online_hanlde, speech_buff+sample_offset, step, NULL, is_final, 16000);
  156. gettimeofday(&end, NULL);
  157. seconds = (end.tv_sec - start.tv_sec);
  158. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  159. if (result)
  160. {
  161. vector<std::vector<int>>* vad_segments = FsmnVadGetResult(result, 0);
  162. print_segs(vad_segments, wav_id);
  163. snippet_time += FsmnVadGetRetSnippetTime(result);
  164. FsmnVadFreeResult(result);
  165. }
  166. else
  167. {
  168. LOG(ERROR) << ("No return data!\n");
  169. }
  170. }
  171. }
  172. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  173. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  174. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  175. FsmnVadUninit(online_hanlde);
  176. FsmnVadUninit(vad_hanlde);
  177. return 0;
  178. }