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.

65 lines
2.4 KiB

  1. // Copyright (c) 2020 Mobvoi Inc (Binbin Zhang)
  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 "frontend/wav.h"
  15. #include "utils/flags.h"
  16. #include "utils/timer.h"
  17. #include "websocket/websocket_client.h"
  18. DEFINE_string(hostname, "127.0.0.1", "hostname of websocket server");
  19. DEFINE_int32(port, 10086, "port of websocket server");
  20. DEFINE_int32(nbest, 1, "n-best of decode result");
  21. DEFINE_string(wav_path, "", "test wav file path");
  22. DEFINE_bool(continuous_decoding, false, "continuous decoding mode");
  23. int main(int argc, char* argv[]) {
  24. gflags::ParseCommandLineFlags(&argc, &argv, false);
  25. google::InitGoogleLogging(argv[0]);
  26. wenet::WebSocketClient client(FLAGS_hostname, FLAGS_port);
  27. client.set_nbest(FLAGS_nbest);
  28. client.set_continuous_decoding(FLAGS_continuous_decoding);
  29. client.SendStartSignal();
  30. wenet::WavReader wav_reader(FLAGS_wav_path);
  31. const int sample_rate = 16000;
  32. // Only support 16K
  33. CHECK_EQ(wav_reader.sample_rate(), sample_rate);
  34. const int num_samples = wav_reader.num_samples();
  35. // Send data every 0.5 second
  36. const float interval = 0.5;
  37. const int sample_interval = interval * sample_rate;
  38. for (int start = 0; start < num_samples; start += sample_interval) {
  39. if (client.done()) {
  40. break;
  41. }
  42. int end = std::min(start + sample_interval, num_samples);
  43. // Convert to short
  44. std::vector<int16_t> data;
  45. data.reserve(end - start);
  46. for (int j = start; j < end; j++) {
  47. data.push_back(static_cast<int16_t>(wav_reader.data()[j]));
  48. }
  49. // TODO(Binbin Zhang): Network order?
  50. // Send PCM data
  51. client.SendBinaryData(data.data(), data.size() * sizeof(int16_t));
  52. VLOG(2) << "Send " << data.size() << " samples";
  53. std::this_thread::sleep_for(
  54. std::chrono::milliseconds(static_cast<int>(interval * 1000)));
  55. }
  56. wenet::Timer timer;
  57. client.SendEndSignal();
  58. client.Join();
  59. VLOG(2) << "Total latency: " << timer.Elapsed() << "ms.";
  60. return 0;
  61. }