funasr-onnx-online-vad.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
  70. cmd.add(model_dir);
  71. cmd.add(quantize);
  72. cmd.add(wav_path);
  73. cmd.add(audio_fs);
  74. cmd.parse(argc, argv);
  75. std::map<std::string, std::string> model_path;
  76. GetValue(model_dir, MODEL_DIR, model_path);
  77. GetValue(quantize, QUANTIZE, model_path);
  78. GetValue(wav_path, WAV_PATH, model_path);
  79. struct timeval start, end;
  80. gettimeofday(&start, nullptr);
  81. int thread_num = 1;
  82. FUNASR_HANDLE vad_hanlde=FsmnVadInit(model_path, thread_num);
  83. if (!vad_hanlde)
  84. {
  85. LOG(ERROR) << "FunVad init failed";
  86. exit(-1);
  87. }
  88. gettimeofday(&end, nullptr);
  89. long seconds = (end.tv_sec - start.tv_sec);
  90. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  91. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  92. // read wav_path
  93. vector<string> wav_list;
  94. vector<string> wav_ids;
  95. string default_id = "wav_default_id";
  96. string wav_path_ = model_path.at(WAV_PATH);
  97. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  98. wav_list.emplace_back(wav_path_);
  99. wav_ids.emplace_back(default_id);
  100. }
  101. else if(is_target_file(wav_path_, "scp")){
  102. ifstream in(wav_path_);
  103. if (!in.is_open()) {
  104. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  105. return 0;
  106. }
  107. string line;
  108. while(getline(in, line))
  109. {
  110. istringstream iss(line);
  111. string column1, column2;
  112. iss >> column1 >> column2;
  113. wav_list.emplace_back(column2);
  114. wav_ids.emplace_back(column1);
  115. }
  116. in.close();
  117. }else{
  118. LOG(ERROR)<<"Please check the wav extension!";
  119. exit(-1);
  120. }
  121. // init online features
  122. FUNASR_HANDLE online_hanlde=FsmnVadOnlineInit(vad_hanlde);
  123. float snippet_time = 0.0f;
  124. long taking_micros = 0;
  125. for (int i = 0; i < wav_list.size(); i++) {
  126. auto& wav_file = wav_list[i];
  127. auto& wav_id = wav_ids[i];
  128. int32_t sampling_rate_ = audio_fs.getValue();
  129. funasr::Audio audio(1);
  130. if(is_target_file(wav_file.c_str(), "wav")){
  131. if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
  132. LOG(ERROR)<<"Failed to load "<< wav_file;
  133. exit(-1);
  134. }
  135. }else if(is_target_file(wav_file.c_str(), "pcm")){
  136. if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
  137. LOG(ERROR)<<"Failed to load "<< wav_file;
  138. exit(-1);
  139. }
  140. }else{
  141. LOG(ERROR)<<"Wrong wav extension";
  142. exit(-1);
  143. }
  144. char* speech_buff = audio.GetSpeechChar();
  145. int buff_len = audio.GetSpeechLen()*2;
  146. int step = 800*2;
  147. bool is_final = false;
  148. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  149. if (sample_offset + step >= buff_len - 1) {
  150. step = buff_len - sample_offset;
  151. is_final = true;
  152. } else {
  153. is_final = false;
  154. }
  155. gettimeofday(&start, nullptr);
  156. FUNASR_RESULT result = FsmnVadInferBuffer(online_hanlde, speech_buff+sample_offset, step, nullptr, is_final, sampling_rate_);
  157. gettimeofday(&end, nullptr);
  158. seconds = (end.tv_sec - start.tv_sec);
  159. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  160. if (result)
  161. {
  162. vector<std::vector<int>>* vad_segments = FsmnVadGetResult(result, 0);
  163. print_segs(vad_segments, wav_id);
  164. snippet_time += FsmnVadGetRetSnippetTime(result);
  165. FsmnVadFreeResult(result);
  166. }
  167. else
  168. {
  169. LOG(ERROR) << ("No return data!\n");
  170. }
  171. }
  172. }
  173. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  174. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  175. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  176. FsmnVadUninit(online_hanlde);
  177. FsmnVadUninit(vad_hanlde);
  178. return 0;
  179. }