blob: 4df5e2ea5eeda434d2ccda56d6bc762b77f214a5 [file] [log] [blame]
andrew@webrtc.org08df9b22014-12-16 20:57:15 +00001/*
2 * Copyright (c) 2014 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
aluebsb0ad43b2015-11-20 00:11:53 -080011#include <iostream>
kwiberg62eaacf2016-02-17 06:39:05 -080012#include <memory>
peah60a189f2016-05-24 20:54:40 -070013
14#include <string.h>
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000015
16#include "gflags/gflags.h"
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000017#include "webrtc/modules/audio_processing/include/audio_processing.h"
peah60a189f2016-05-24 20:54:40 -070018#include "webrtc/modules/audio_processing/test/aec_dump_based_simulator.h"
19#include "webrtc/modules/audio_processing/test/audio_processing_simulator.h"
20#include "webrtc/modules/audio_processing/test/wav_based_simulator.h"
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000021
peah60a189f2016-05-24 20:54:40 -070022namespace webrtc {
23namespace test {
Peter Kasting69558702016-01-12 16:26:35 -080024namespace {
25
peah60a189f2016-05-24 20:54:40 -070026const int kParameterNotSpecifiedValue = -10000;
27
28const char kUsageDescription[] =
29 "Usage: audioproc_f [options] -i <input.wav>\n"
30 " or\n"
31 " audioproc_f [options] -dump_input <aec_dump>\n"
32 "\n\n"
33 "Command-line tool to simulate a call using the audio "
34 "processing module, either based on wav files or "
35 "protobuf debug dump recordings.";
36
37DEFINE_string(dump_input, "", "Aec dump input filename");
38DEFINE_string(dump_output, "", "Aec dump output filename");
39DEFINE_string(i, "", "Forward stream input wav filename");
40DEFINE_string(o, "", "Forward stream output wav filename");
41DEFINE_string(ri, "", "Reverse stream input wav filename");
42DEFINE_string(ro, "", "Reverse stream output wav filename");
43DEFINE_int32(output_num_channels,
44 kParameterNotSpecifiedValue,
45 "Number of forward stream output channels");
46DEFINE_int32(reverse_output_num_channels,
47 kParameterNotSpecifiedValue,
48 "Number of Reverse stream output channels");
49DEFINE_int32(output_sample_rate_hz,
50 kParameterNotSpecifiedValue,
51 "Forward stream output sample rate in Hz");
52DEFINE_int32(reverse_output_sample_rate_hz,
53 kParameterNotSpecifiedValue,
54 "Reverse stream output sample rate in Hz");
55DEFINE_string(mic_positions,
56 "",
57 "Space delimited cartesian coordinates of microphones in "
58 "meters. The coordinates of each point are contiguous. For a "
59 "two element array: \"x1 y1 z1 x2 y2 z2\"");
60DEFINE_int32(target_angle_degrees,
61 90,
62 "The azimuth of the target in degrees (0-359). Only applies to "
63 "beamforming.");
64DEFINE_bool(fixed_interface,
65 false,
66 "Use the fixed interface when operating on wav files");
67DEFINE_int32(aec,
68 kParameterNotSpecifiedValue,
69 "Activate (1) or deactivate(0) the echo canceller");
70DEFINE_int32(aecm,
71 kParameterNotSpecifiedValue,
72 "Activate (1) or deactivate(0) the mobile echo controller");
73DEFINE_int32(agc,
74 kParameterNotSpecifiedValue,
75 "Activate (1) or deactivate(0) the AGC");
76DEFINE_int32(hpf,
77 kParameterNotSpecifiedValue,
78 "Activate (1) or deactivate(0) the high-pass filter");
79DEFINE_int32(ns,
80 kParameterNotSpecifiedValue,
81 "Activate (1) or deactivate(0) the noise suppressor");
82DEFINE_int32(ts,
83 kParameterNotSpecifiedValue,
84 "Activate (1) or deactivate(0) the transient suppressor");
85DEFINE_int32(bf,
86 kParameterNotSpecifiedValue,
87 "Activate (1) or deactivate(0) the beamformer");
88DEFINE_int32(ie,
89 kParameterNotSpecifiedValue,
90 "Activate (1) or deactivate(0) the intelligibility enhancer");
91DEFINE_int32(vad,
92 kParameterNotSpecifiedValue,
93 "Activate (1) or deactivate(0) the voice activity detector");
94DEFINE_int32(le,
95 kParameterNotSpecifiedValue,
96 "Activate (1) or deactivate(0) the level estimator");
97DEFINE_bool(all_default,
98 false,
99 "Activate all of the default components (will be overridden by any "
100 "other settings)");
101DEFINE_int32(aec_suppression_level,
102 kParameterNotSpecifiedValue,
103 "Set the aec suppression level (0-2)");
104DEFINE_int32(delay_agnostic,
105 kParameterNotSpecifiedValue,
106 "Activate (1) or deactivate(0) the AEC delay agnostic mode");
107DEFINE_int32(extended_filter,
108 kParameterNotSpecifiedValue,
109 "Activate (1) or deactivate(0) the AEC extended filter mode");
110DEFINE_int32(drift_compensation,
111 kParameterNotSpecifiedValue,
112 "Activate (1) or deactivate(0) the drift compensation");
113DEFINE_int32(aec3,
114 kParameterNotSpecifiedValue,
115 "Activate (1) or deactivate(0) the experimental AEC mode AEC3");
116DEFINE_int32(
117 refined_adaptive_filter,
118 kParameterNotSpecifiedValue,
119 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
120DEFINE_int32(aecm_routing_mode,
121 kParameterNotSpecifiedValue,
122 "Specify the AECM routing mode (0-4)");
123DEFINE_int32(aecm_comfort_noise,
124 kParameterNotSpecifiedValue,
125 "Activate (1) or deactivate(0) the AECM comfort noise");
126DEFINE_int32(agc_mode,
127 kParameterNotSpecifiedValue,
128 "Specify the AGC mode (0-2)");
129DEFINE_int32(agc_target_level,
130 kParameterNotSpecifiedValue,
131 "Specify the AGC target level (0-31)");
132DEFINE_int32(agc_limiter,
133 kParameterNotSpecifiedValue,
134 "Activate (1) or deactivate(0) the level estimator");
135DEFINE_int32(agc_compression_gain,
136 kParameterNotSpecifiedValue,
137 "Specify the AGC compression gain (0-90)");
138DEFINE_int32(vad_likelihood,
139 kParameterNotSpecifiedValue,
140 "Specify the VAD likelihood (0-3)");
141DEFINE_int32(ns_level,
142 kParameterNotSpecifiedValue,
143 "Specify the NS level (0-3)");
144DEFINE_int32(stream_delay,
145 kParameterNotSpecifiedValue,
146 "Specify the stream delay in ms to use");
147DEFINE_int32(stream_drift_samples,
148 kParameterNotSpecifiedValue,
149 "Specify the number of stream drift samples to use");
150DEFINE_bool(performance_report, false, "Report the APM performance ");
151DEFINE_bool(verbose, false, "Produce verbose output");
152DEFINE_bool(bitexactness_report,
153 false,
154 "Report bitexactness for aec dump result reproduction");
155DEFINE_bool(discard_settings_in_aecdump,
156 false,
157 "Discard any config settings specified in the aec dump");
158DEFINE_bool(store_intermediate_output,
159 false,
160 "Creates new output files after each init");
161
162void SetSettingIfSpecified(const std::string value,
163 rtc::Optional<std::string>* parameter) {
164 if (value.compare("") != 0) {
165 *parameter = rtc::Optional<std::string>(value);
166 }
167}
168
169void SetSettingIfSpecified(int value, rtc::Optional<int>* parameter) {
170 if (value != kParameterNotSpecifiedValue) {
171 *parameter = rtc::Optional<int>(value);
172 }
173}
174
175void SetSettingIfFlagSet(int32_t flag, rtc::Optional<bool>* parameter) {
176 if (flag == 0) {
177 *parameter = rtc::Optional<bool>(false);
178 } else if (flag == 1) {
179 *parameter = rtc::Optional<bool>(true);
180 }
181}
182
183SimulationSettings CreateSettings() {
184 SimulationSettings settings;
185 if (FLAGS_all_default) {
186 settings.use_le = rtc::Optional<bool>(true);
187 settings.use_vad = rtc::Optional<bool>(true);
188 settings.use_ie = rtc::Optional<bool>(false);
189 settings.use_bf = rtc::Optional<bool>(false);
190 settings.use_ts = rtc::Optional<bool>(true);
191 settings.use_ns = rtc::Optional<bool>(true);
192 settings.use_hpf = rtc::Optional<bool>(true);
193 settings.use_agc = rtc::Optional<bool>(true);
194 settings.use_aec = rtc::Optional<bool>(true);
195 settings.use_aecm = rtc::Optional<bool>(false);
196 }
197 SetSettingIfSpecified(FLAGS_dump_input, &settings.aec_dump_input_filename);
198 SetSettingIfSpecified(FLAGS_dump_output, &settings.aec_dump_output_filename);
199 SetSettingIfSpecified(FLAGS_i, &settings.input_filename);
200 SetSettingIfSpecified(FLAGS_o, &settings.output_filename);
201 SetSettingIfSpecified(FLAGS_ri, &settings.reverse_input_filename);
202 SetSettingIfSpecified(FLAGS_ro, &settings.reverse_output_filename);
203 SetSettingIfSpecified(FLAGS_output_num_channels,
204 &settings.output_num_channels);
205 SetSettingIfSpecified(FLAGS_reverse_output_num_channels,
206 &settings.reverse_output_num_channels);
207 SetSettingIfSpecified(FLAGS_output_sample_rate_hz,
208 &settings.output_sample_rate_hz);
209 SetSettingIfSpecified(FLAGS_reverse_output_sample_rate_hz,
210 &settings.reverse_output_sample_rate_hz);
211 SetSettingIfSpecified(FLAGS_mic_positions, &settings.microphone_positions);
212 settings.target_angle_degrees = FLAGS_target_angle_degrees;
213 SetSettingIfFlagSet(FLAGS_aec, &settings.use_aec);
214 SetSettingIfFlagSet(FLAGS_aecm, &settings.use_aecm);
215 SetSettingIfFlagSet(FLAGS_agc, &settings.use_agc);
216 SetSettingIfFlagSet(FLAGS_hpf, &settings.use_hpf);
217 SetSettingIfFlagSet(FLAGS_ns, &settings.use_ns);
218 SetSettingIfFlagSet(FLAGS_ts, &settings.use_ts);
219 SetSettingIfFlagSet(FLAGS_bf, &settings.use_bf);
220 SetSettingIfFlagSet(FLAGS_ie, &settings.use_ie);
221 SetSettingIfFlagSet(FLAGS_vad, &settings.use_vad);
222 SetSettingIfFlagSet(FLAGS_le, &settings.use_le);
223 SetSettingIfSpecified(FLAGS_aec_suppression_level,
224 &settings.aec_suppression_level);
225 SetSettingIfFlagSet(FLAGS_delay_agnostic, &settings.use_delay_agnostic);
226 SetSettingIfFlagSet(FLAGS_extended_filter, &settings.use_extended_filter);
227 SetSettingIfFlagSet(FLAGS_drift_compensation,
228 &settings.use_drift_compensation);
229 SetSettingIfFlagSet(FLAGS_refined_adaptive_filter,
230 &settings.use_refined_adaptive_filter);
231
232 SetSettingIfFlagSet(FLAGS_aec3, &settings.use_aec3);
233 SetSettingIfSpecified(FLAGS_aecm_routing_mode, &settings.aecm_routing_mode);
234 SetSettingIfFlagSet(FLAGS_aecm_comfort_noise,
235 &settings.use_aecm_comfort_noise);
236 SetSettingIfSpecified(FLAGS_agc_mode, &settings.agc_mode);
237 SetSettingIfSpecified(FLAGS_agc_target_level, &settings.agc_target_level);
238 SetSettingIfFlagSet(FLAGS_agc_limiter, &settings.use_agc_limiter);
239 SetSettingIfSpecified(FLAGS_agc_compression_gain,
240 &settings.agc_compression_gain);
241 SetSettingIfSpecified(FLAGS_vad_likelihood, &settings.vad_likelihood);
242 SetSettingIfSpecified(FLAGS_ns_level, &settings.ns_level);
243 SetSettingIfSpecified(FLAGS_stream_delay, &settings.stream_delay);
244 SetSettingIfSpecified(FLAGS_stream_drift_samples,
245 &settings.stream_drift_samples);
246 settings.report_performance = FLAGS_performance_report;
247 settings.use_verbose_logging = FLAGS_verbose;
248 settings.report_bitexactness = FLAGS_bitexactness_report;
249 settings.discard_all_settings_in_aecdump = FLAGS_discard_settings_in_aecdump;
250 settings.fixed_interface = FLAGS_fixed_interface;
251 settings.store_intermediate_output = FLAGS_store_intermediate_output;
252
253 return settings;
254}
255
256void ReportConditionalErrorAndExit(bool condition, std::string message) {
257 if (condition) {
258 std::cerr << message << std::endl;
259 exit(1);
260 }
261}
262
263void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
264 if (settings.input_filename || settings.reverse_input_filename) {
265 ReportConditionalErrorAndExit(!!settings.aec_dump_input_filename,
266 "Error: The aec dump cannot be specified "
267 "together with input wav files!\n");
268
269 ReportConditionalErrorAndExit(!settings.input_filename,
270 "Error: When operating at wav files, the "
271 "input wav filename must be "
272 "specified!\n");
273
274 ReportConditionalErrorAndExit(
275 settings.reverse_output_filename && !settings.reverse_input_filename,
276 "Error: When operating at wav files, the reverse input wav filename "
277 "must be specified if the reverse output wav filename is specified!\n");
278 } else {
279 ReportConditionalErrorAndExit(!settings.aec_dump_input_filename,
280 "Error: Either the aec dump or the wav "
281 "input files must be specified!\n");
282 }
283
284 ReportConditionalErrorAndExit(
285 settings.use_aec && *settings.use_aec && settings.use_aecm &&
286 *settings.use_aecm,
287 "Error: The AEC and the AECM cannot be activated at the same time!\n");
288
289 ReportConditionalErrorAndExit(
290 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
291 "Error: --output_sample_rate_hz must be positive!\n");
292
293 ReportConditionalErrorAndExit(
294 settings.reverse_output_sample_rate_hz &&
295 settings.output_sample_rate_hz &&
296 *settings.output_sample_rate_hz <= 0,
297 "Error: --reverse_output_sample_rate_hz must be positive!\n");
298
299 ReportConditionalErrorAndExit(
300 settings.output_num_channels && *settings.output_num_channels <= 0,
301 "Error: --output_num_channels must be positive!\n");
302
303 ReportConditionalErrorAndExit(
304 settings.reverse_output_num_channels &&
305 *settings.reverse_output_num_channels <= 0,
306 "Error: --reverse_output_num_channels must be positive!\n");
307
308 ReportConditionalErrorAndExit(
309 settings.use_bf && *settings.use_bf && !settings.microphone_positions,
310 "Error: --mic_positions must be specified when the beamformer is "
311 "activated.\n");
312
313 ReportConditionalErrorAndExit(
314 settings.target_angle_degrees < 0 || settings.target_angle_degrees > 359,
315 "Error: -target_angle_degrees must be specified between 0 and 359.\n");
316
317 ReportConditionalErrorAndExit(
318 settings.aec_suppression_level &&
319 ((*settings.aec_suppression_level) < 0 ||
320 (*settings.aec_suppression_level) > 2),
321 "Error: --aec_suppression_level must be specified between 0 and 2.\n");
322
323 ReportConditionalErrorAndExit(
324 settings.aecm_routing_mode && ((*settings.aecm_routing_mode) < 0 ||
325 (*settings.aecm_routing_mode) > 4),
326 "Error: --aecm_routing_mode must be specified between 0 and 4.\n");
327
328 ReportConditionalErrorAndExit(
329 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
330 (*settings.agc_target_level) > 31),
331 "Error: --agc_target_level must be specified between 0 and 31.\n");
332
333 ReportConditionalErrorAndExit(
334 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
335 (*settings.agc_compression_gain) > 90),
336 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
337
338 ReportConditionalErrorAndExit(
339 settings.vad_likelihood &&
340 ((*settings.vad_likelihood) < 0 || (*settings.vad_likelihood) > 3),
341 "Error: --vad_likelihood must be specified between 0 and 3.\n");
342
343 ReportConditionalErrorAndExit(
344 settings.ns_level &&
345 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
346 "Error: --ns_level must be specified between 0 and 3.\n");
347
348 ReportConditionalErrorAndExit(
349 settings.report_bitexactness && !settings.aec_dump_input_filename,
350 "Error: --bitexactness_report can only be used when operating on an "
351 "aecdump\n");
352
353 auto valid_wav_name = [](const std::string& wav_file_name) {
354 if (wav_file_name.size() < 5) {
355 return false;
356 }
357 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
358 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
359 return true;
360 }
361 return false;
362 };
363
364 ReportConditionalErrorAndExit(
365 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
366 "Error: --i must be a valid .wav file name.\n");
367
368 ReportConditionalErrorAndExit(
369 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
370 "Error: --o must be a valid .wav file name.\n");
371
372 ReportConditionalErrorAndExit(
373 settings.reverse_input_filename &&
374 (!valid_wav_name(*settings.reverse_input_filename)),
375 "Error: --ri must be a valid .wav file name.\n");
376
377 ReportConditionalErrorAndExit(
378 settings.reverse_output_filename &&
379 (!valid_wav_name(*settings.reverse_output_filename)),
380 "Error: --ro must be a valid .wav file name.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800381}
382
383} // namespace
384
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000385int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700386 google::SetUsageMessage(kUsageDescription);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000387 google::ParseCommandLineFlags(&argc, &argv, true);
388
peah60a189f2016-05-24 20:54:40 -0700389 SimulationSettings settings = CreateSettings();
390 PerformBasicParameterSanityChecks(settings);
391 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000392
peah60a189f2016-05-24 20:54:40 -0700393 if (settings.aec_dump_input_filename) {
394 processor.reset(new AecDumpBasedSimulator(settings));
aluebsb0ad43b2015-11-20 00:11:53 -0800395 } else {
peah60a189f2016-05-24 20:54:40 -0700396 processor.reset(new WavBasedSimulator(settings));
andrewbdafe312015-10-29 23:42:54 -0700397 }
398
peah60a189f2016-05-24 20:54:40 -0700399 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800400
peah60a189f2016-05-24 20:54:40 -0700401 if (settings.report_performance) {
aluebsb0ad43b2015-11-20 00:11:53 -0800402 const auto& proc_time = processor->proc_time();
Niels Möllerd28db7f2016-05-10 16:31:47 +0200403 int64_t exec_time_us = proc_time.sum / rtc::kNumNanosecsPerMicrosec;
peah60a189f2016-05-24 20:54:40 -0700404 std::cout << std::endl
405 << "Execution time: " << exec_time_us * 1e-6 << " s, File time: "
406 << processor->get_num_process_stream_calls() * 1.f /
407 AudioProcessingSimulator::kChunksPerSecond
408 << std::endl
409 << "Time per fwd stream chunk (mean, max, min): " << std::endl
410 << exec_time_us * 1.f / processor->get_num_process_stream_calls()
411 << " us, " << 1.f * proc_time.max / rtc::kNumNanosecsPerMicrosec
412 << " us, " << 1.f * proc_time.min / rtc::kNumNanosecsPerMicrosec
413 << " us" << std::endl;
414 }
415
416 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
417 if (processor->OutputWasBitexact()) {
418 std::cout << "The processing was bitexact.";
419 } else {
420 std::cout << "The processing was not bitexact.";
421 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700422 }
aluebsb0ad43b2015-11-20 00:11:53 -0800423
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000424 return 0;
425}
426
peah60a189f2016-05-24 20:54:40 -0700427} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000428} // namespace webrtc
429
430int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700431 return webrtc::test::main(argc, argv);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000432}