1 /*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <array>
12 #include <string>
13 #include <vector>
14
15 #include "absl/flags/flag.h"
16 #include "absl/flags/parse.h"
17 #include "common_audio/resampler/push_sinc_resampler.h"
18 #include "common_audio/wav_file.h"
19 #include "modules/audio_processing/agc2/rnn_vad/common.h"
20 #include "modules/audio_processing/agc2/rnn_vad/features_extraction.h"
21 #include "modules/audio_processing/agc2/rnn_vad/rnn.h"
22 #include "rtc_base/logging.h"
23
24 ABSL_FLAG(std::string, i, "", "Path to the input wav file");
25 ABSL_FLAG(std::string, f, "", "Path to the output features file");
26 ABSL_FLAG(std::string, o, "", "Path to the output VAD probabilities file");
27
28 namespace webrtc {
29 namespace rnn_vad {
30 namespace test {
31
main(int argc,char * argv[])32 int main(int argc, char* argv[]) {
33 absl::ParseCommandLine(argc, argv);
34 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
35
36 // Open wav input file and check properties.
37 const std::string input_wav_file = absl::GetFlag(FLAGS_i);
38 WavReader wav_reader(input_wav_file);
39 if (wav_reader.num_channels() != 1) {
40 RTC_LOG(LS_ERROR) << "Only mono wav files are supported";
41 return 1;
42 }
43 if (wav_reader.sample_rate() % 100 != 0) {
44 RTC_LOG(LS_ERROR) << "The sample rate rate must allow 10 ms frames.";
45 return 1;
46 }
47 RTC_LOG(LS_INFO) << "Input sample rate: " << wav_reader.sample_rate();
48
49 // Init output files.
50 const std::string output_vad_probs_file = absl::GetFlag(FLAGS_o);
51 FILE* vad_probs_file = fopen(output_vad_probs_file.c_str(), "wb");
52 FILE* features_file = nullptr;
53 const std::string output_feature_file = absl::GetFlag(FLAGS_f);
54 if (!output_feature_file.empty()) {
55 features_file = fopen(output_feature_file.c_str(), "wb");
56 }
57
58 // Initialize.
59 const size_t frame_size_10ms =
60 rtc::CheckedDivExact(wav_reader.sample_rate(), 100);
61 std::vector<float> samples_10ms;
62 samples_10ms.resize(frame_size_10ms);
63 std::array<float, kFrameSize10ms24kHz> samples_10ms_24kHz;
64 PushSincResampler resampler(frame_size_10ms, kFrameSize10ms24kHz);
65 FeaturesExtractor features_extractor;
66 std::array<float, kFeatureVectorSize> feature_vector;
67 RnnBasedVad rnn_vad;
68
69 // Compute VAD probabilities.
70 while (true) {
71 // Read frame at the input sample rate.
72 const auto read_samples =
73 wav_reader.ReadSamples(frame_size_10ms, samples_10ms.data());
74 if (read_samples < frame_size_10ms) {
75 break; // EOF.
76 }
77 // Resample input.
78 resampler.Resample(samples_10ms.data(), samples_10ms.size(),
79 samples_10ms_24kHz.data(), samples_10ms_24kHz.size());
80
81 // Extract features and feed the RNN.
82 bool is_silence = features_extractor.CheckSilenceComputeFeatures(
83 samples_10ms_24kHz, feature_vector);
84 float vad_probability =
85 rnn_vad.ComputeVadProbability(feature_vector, is_silence);
86 // Write voice probability.
87 RTC_DCHECK_GE(vad_probability, 0.f);
88 RTC_DCHECK_GE(1.f, vad_probability);
89 fwrite(&vad_probability, sizeof(float), 1, vad_probs_file);
90 // Write features.
91 if (features_file) {
92 const float float_is_silence = is_silence ? 1.f : 0.f;
93 fwrite(&float_is_silence, sizeof(float), 1, features_file);
94 if (is_silence) {
95 // Do not write uninitialized values.
96 feature_vector.fill(0.f);
97 }
98 fwrite(feature_vector.data(), sizeof(float), kFeatureVectorSize,
99 features_file);
100 }
101 }
102
103 // Close output file(s).
104 fclose(vad_probs_file);
105 RTC_LOG(LS_INFO) << "VAD probabilities written to " << output_vad_probs_file;
106 if (features_file) {
107 fclose(features_file);
108 RTC_LOG(LS_INFO) << "features written to " << output_feature_file;
109 }
110
111 return 0;
112 }
113
114 } // namespace test
115 } // namespace rnn_vad
116 } // namespace webrtc
117
main(int argc,char * argv[])118 int main(int argc, char* argv[]) {
119 return webrtc::rnn_vad::test::main(argc, argv);
120 }
121