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.

64 lines
2.4 KiB

  1. // Copyright (c) 2021 Ximalaya Speech Team (Xiang Lyu)
  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 "grpc/grpc_client.h"
  16. #include "utils/flags.h"
  17. #include "utils/timer.h"
  18. #include "gflags/gflags.h"
  19. DEFINE_string(hostname, "127.0.0.1", "hostname of websocket server");
  20. DEFINE_int32(port, 10086, "port of websocket server");
  21. DEFINE_int32(nbest, 1, "n-best of decode result");
  22. DEFINE_string(wav_path, "", "test wav file path");
  23. DEFINE_bool(continuous_decoding, false, "continuous decoding mode");
  24. int main(int argc, char* argv[]) {
  25. gflags::ParseCommandLineFlags(&argc, &argv, false);
  26. google::InitGoogleLogging(argv[0]);
  27. wenet::GrpcClient client(FLAGS_hostname, FLAGS_port, FLAGS_nbest,
  28. FLAGS_continuous_decoding);
  29. wenet::WavReader wav_reader(FLAGS_wav_path);
  30. const int sample_rate = 16000;
  31. // Only support 16K
  32. CHECK_EQ(wav_reader.sample_rate(), sample_rate);
  33. const int num_samples = wav_reader.num_samples();
  34. std::vector<float> pcm_data(wav_reader.data(),
  35. wav_reader.data() + num_samples);
  36. // Send data every 0.5 second
  37. const float interval = 0.5;
  38. const int sample_interval = interval * sample_rate;
  39. for (int start = 0; start < num_samples; start += sample_interval) {
  40. if (client.done()) {
  41. break;
  42. }
  43. int end = std::min(start + sample_interval, num_samples);
  44. // Convert to short
  45. std::vector<int16_t> data;
  46. data.reserve(end - start);
  47. for (int j = start; j < end; j++) {
  48. data.push_back(static_cast<int16_t>(pcm_data[j]));
  49. }
  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.Join();
  58. VLOG(2) << "Total latency: " << timer.Elapsed() << "ms.";
  59. return 0;
  60. }