You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.2 KiB

  1. // Copyright (c) 2020 Mobvoi Inc (Binbin Zhang, Di Wu)
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <iomanip>
  15. #include <thread>
  16. #include <utility>
  17. #include "decoder/params.h"
  18. #include "frontend/wav.h"
  19. #include "utils/flags.h"
  20. #include "utils/wn_string.h"
  21. #include "utils/thread_pool.h"
  22. #include "utils/timer.h"
  23. #include "utils/wn_utils.h"
  24. DEFINE_bool(simulate_streaming, false, "simulate streaming input");
  25. DEFINE_bool(output_nbest, false, "output n-best of decode result");
  26. DEFINE_string(wav_path, "", "single wave path");
  27. DEFINE_string(wav_scp, "", "input wav scp");
  28. DEFINE_string(result, "", "result output file");
  29. DEFINE_bool(continuous_decoding, false, "continuous decoding mode");
  30. DEFINE_int32(thread_num, 1, "num of decode thread");
  31. DEFINE_int32(warmup, 0, "num of warmup decode, 0 means no warmup");
  32. std::shared_ptr<wenet::DecodeOptions> g_decode_config;
  33. std::shared_ptr<wenet::FeaturePipelineConfig> g_feature_config;
  34. std::shared_ptr<wenet::DecodeResource> g_decode_resource;
  35. std::ofstream g_result;
  36. std::mutex g_mutex;
  37. int g_total_waves_dur = 0;
  38. int g_total_decode_time = 0;
  39. void Decode(std::pair<std::string, std::string> wav, bool warmup = false) {
  40. wenet::WavReader wav_reader(wav.second);
  41. int num_samples = wav_reader.num_samples();
  42. CHECK_EQ(wav_reader.sample_rate(), FLAGS_sample_rate);
  43. auto feature_pipeline =
  44. std::make_shared<wenet::FeaturePipeline>(*g_feature_config);
  45. feature_pipeline->AcceptWaveform(wav_reader.data(), num_samples);
  46. feature_pipeline->set_input_finished();
  47. LOG(INFO) << "num frames " << feature_pipeline->num_frames();
  48. wenet::AsrDecoder decoder(feature_pipeline, g_decode_resource,
  49. *g_decode_config);
  50. int wave_dur = static_cast<int>(static_cast<float>(num_samples) /
  51. wav_reader.sample_rate() * 1000);
  52. int decode_time = 0;
  53. std::string final_result;
  54. while (true) {
  55. wenet::Timer timer;
  56. wenet::DecodeState state = decoder.Decode();
  57. if (state == wenet::DecodeState::kEndFeats) {
  58. decoder.Rescoring();
  59. }
  60. int chunk_decode_time = timer.Elapsed();
  61. decode_time += chunk_decode_time;
  62. if (decoder.DecodedSomething()) {
  63. LOG(INFO) << "Partial result: " << decoder.result()[0].sentence;
  64. }
  65. if (FLAGS_continuous_decoding && state == wenet::DecodeState::kEndpoint) {
  66. if (decoder.DecodedSomething()) {
  67. decoder.Rescoring();
  68. LOG(INFO) << "Final result (continuous decoding): "
  69. << decoder.result()[0].sentence;
  70. final_result.append(decoder.result()[0].sentence);
  71. }
  72. decoder.ResetContinuousDecoding();
  73. }
  74. if (state == wenet::DecodeState::kEndFeats) {
  75. break;
  76. } else if (FLAGS_chunk_size > 0 && FLAGS_simulate_streaming) {
  77. float frame_shift_in_ms =
  78. static_cast<float>(g_feature_config->frame_shift) /
  79. wav_reader.sample_rate() * 1000;
  80. auto wait_time =
  81. decoder.num_frames_in_current_chunk() * frame_shift_in_ms -
  82. chunk_decode_time;
  83. if (wait_time > 0) {
  84. LOG(INFO) << "Simulate streaming, waiting for " << wait_time << "ms";
  85. std::this_thread::sleep_for(
  86. std::chrono::milliseconds(static_cast<int>(wait_time)));
  87. }
  88. }
  89. }
  90. if (decoder.DecodedSomething()) {
  91. final_result.append(decoder.result()[0].sentence);
  92. }
  93. LOG(INFO) << wav.first << " Final result: " << final_result << std::endl;
  94. LOG(INFO) << "Decoded " << wave_dur << "ms audio taken " << decode_time
  95. << "ms.";
  96. if (!warmup) {
  97. g_mutex.lock();
  98. std::ostream& buffer = FLAGS_result.empty() ? std::cout : g_result;
  99. if (!FLAGS_output_nbest) {
  100. buffer << wav.first << " " << final_result << std::endl;
  101. } else {
  102. buffer << "wav " << wav.first << std::endl;
  103. auto& results = decoder.result();
  104. for (auto& r : results) {
  105. if (r.sentence.empty()) continue;
  106. buffer << "candidate " << r.score << " " << r.sentence << std::endl;
  107. }
  108. }
  109. g_total_waves_dur += wave_dur;
  110. g_total_decode_time += decode_time;
  111. g_mutex.unlock();
  112. }
  113. }
  114. int main(int argc, char* argv[]) {
  115. gflags::ParseCommandLineFlags(&argc, &argv, false);
  116. google::InitGoogleLogging(argv[0]);
  117. g_decode_config = wenet::InitDecodeOptionsFromFlags();
  118. g_feature_config = wenet::InitFeaturePipelineConfigFromFlags();
  119. g_decode_resource = wenet::InitDecodeResourceFromFlags();
  120. if (FLAGS_wav_path.empty() && FLAGS_wav_scp.empty()) {
  121. LOG(FATAL) << "Please provide the wave path or the wav scp.";
  122. }
  123. std::vector<std::pair<std::string, std::string>> waves;
  124. if (!FLAGS_wav_path.empty()) {
  125. waves.emplace_back(make_pair("test", FLAGS_wav_path));
  126. } else {
  127. std::ifstream wav_scp(FLAGS_wav_scp);
  128. std::string line;
  129. while (getline(wav_scp, line)) {
  130. std::vector<std::string> strs;
  131. wenet::SplitString(line, &strs);
  132. CHECK_GE(strs.size(), 2);
  133. waves.emplace_back(make_pair(strs[0], strs[1]));
  134. }
  135. if (waves.empty()) {
  136. LOG(FATAL) << "Please provide non-empty wav scp.";
  137. }
  138. }
  139. if (!FLAGS_result.empty()) {
  140. g_result.open(FLAGS_result, std::ios::out);
  141. }
  142. // Warmup
  143. if (FLAGS_warmup > 0) {
  144. LOG(INFO) << "Warming up...";
  145. {
  146. ThreadPool pool(FLAGS_thread_num);
  147. auto wav = waves[0];
  148. for (int i = 0; i < FLAGS_warmup; i++) {
  149. pool.enqueue(Decode, wav, true);
  150. }
  151. }
  152. LOG(INFO) << "Warmup done.";
  153. }
  154. {
  155. ThreadPool pool(FLAGS_thread_num);
  156. for (auto& wav : waves) {
  157. pool.enqueue(Decode, wav, false);
  158. }
  159. }
  160. LOG(INFO) << "Total: decoded " << g_total_waves_dur << "ms audio taken "
  161. << g_total_decode_time << "ms.";
  162. LOG(INFO) << "RTF: " << std::setprecision(4)
  163. << static_cast<float>(g_total_decode_time) / g_total_waves_dur;
  164. return 0;
  165. }