1 /*
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
11 #include <iostream>
12 #include <memory>
13
14 #include <string.h>
15
16 #include "modules/audio_processing/include/audio_processing.h"
17 #include "modules/audio_processing/test/aec_dump_based_simulator.h"
18 #include "modules/audio_processing/test/audio_processing_simulator.h"
19 #include "modules/audio_processing/test/wav_based_simulator.h"
20 #include "rtc_base/flags.h"
21
22 namespace webrtc {
23 namespace test {
24 namespace {
25
26 const int kParameterNotSpecifiedValue = -10000;
27
28 const 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.\n";
36
37 DEFINE_string(dump_input, "", "Aec dump input filename");
38 DEFINE_string(dump_output, "", "Aec dump output filename");
39 DEFINE_string(i, "", "Forward stream input wav filename");
40 DEFINE_string(o, "", "Forward stream output wav filename");
41 DEFINE_string(ri, "", "Reverse stream input wav filename");
42 DEFINE_string(ro, "", "Reverse stream output wav filename");
43 DEFINE_string(artificial_nearend, "", "Artificial nearend wav filename");
44 DEFINE_int(output_num_channels,
45 kParameterNotSpecifiedValue,
46 "Number of forward stream output channels");
47 DEFINE_int(reverse_output_num_channels,
48 kParameterNotSpecifiedValue,
49 "Number of Reverse stream output channels");
50 DEFINE_int(output_sample_rate_hz,
51 kParameterNotSpecifiedValue,
52 "Forward stream output sample rate in Hz");
53 DEFINE_int(reverse_output_sample_rate_hz,
54 kParameterNotSpecifiedValue,
55 "Reverse stream output sample rate in Hz");
56 DEFINE_string(mic_positions,
57 "",
58 "Space delimited cartesian coordinates of microphones in "
59 "meters. The coordinates of each point are contiguous. For a "
60 "two element array: \"x1 y1 z1 x2 y2 z2\"");
61 DEFINE_int(target_angle_degrees,
62 90,
63 "The azimuth of the target in degrees (0-359). Only applies to "
64 "beamforming.");
65 DEFINE_bool(fixed_interface,
66 false,
67 "Use the fixed interface when operating on wav files");
68 DEFINE_int(aec,
69 kParameterNotSpecifiedValue,
70 "Activate (1) or deactivate(0) the echo canceller");
71 DEFINE_int(aecm,
72 kParameterNotSpecifiedValue,
73 "Activate (1) or deactivate(0) the mobile echo controller");
74 DEFINE_int(ed,
75 kParameterNotSpecifiedValue,
76 "Activate (1) or deactivate (0) the residual echo detector");
77 DEFINE_string(ed_graph, "", "Output filename for graph of echo likelihood");
78 DEFINE_int(agc,
79 kParameterNotSpecifiedValue,
80 "Activate (1) or deactivate(0) the AGC");
81 DEFINE_int(agc2,
82 kParameterNotSpecifiedValue,
83 "Activate (1) or deactivate(0) the AGC2");
84 DEFINE_int(hpf,
85 kParameterNotSpecifiedValue,
86 "Activate (1) or deactivate(0) the high-pass filter");
87 DEFINE_int(ns,
88 kParameterNotSpecifiedValue,
89 "Activate (1) or deactivate(0) the noise suppressor");
90 DEFINE_int(ts,
91 kParameterNotSpecifiedValue,
92 "Activate (1) or deactivate(0) the transient suppressor");
93 DEFINE_int(bf,
94 kParameterNotSpecifiedValue,
95 "Activate (1) or deactivate(0) the beamformer");
96 DEFINE_int(ie,
97 kParameterNotSpecifiedValue,
98 "Activate (1) or deactivate(0) the intelligibility enhancer");
99 DEFINE_int(vad,
100 kParameterNotSpecifiedValue,
101 "Activate (1) or deactivate(0) the voice activity detector");
102 DEFINE_int(le,
103 kParameterNotSpecifiedValue,
104 "Activate (1) or deactivate(0) the level estimator");
105 DEFINE_bool(all_default,
106 false,
107 "Activate all of the default components (will be overridden by any "
108 "other settings)");
109 DEFINE_int(aec_suppression_level,
110 kParameterNotSpecifiedValue,
111 "Set the aec suppression level (0-2)");
112 DEFINE_int(delay_agnostic,
113 kParameterNotSpecifiedValue,
114 "Activate (1) or deactivate(0) the AEC delay agnostic mode");
115 DEFINE_int(extended_filter,
116 kParameterNotSpecifiedValue,
117 "Activate (1) or deactivate(0) the AEC extended filter mode");
118 DEFINE_int(drift_compensation,
119 kParameterNotSpecifiedValue,
120 "Activate (1) or deactivate(0) the drift compensation");
121 DEFINE_int(aec3,
122 kParameterNotSpecifiedValue,
123 "Activate (1) or deactivate(0) the experimental AEC mode AEC3");
124 DEFINE_int(lc,
125 kParameterNotSpecifiedValue,
126 "Activate (1) or deactivate(0) the level control");
127 DEFINE_int(experimental_agc,
128 kParameterNotSpecifiedValue,
129 "Activate (1) or deactivate(0) the experimental AGC");
130 DEFINE_int(
131 refined_adaptive_filter,
132 kParameterNotSpecifiedValue,
133 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
134 DEFINE_int(aecm_routing_mode,
135 kParameterNotSpecifiedValue,
136 "Specify the AECM routing mode (0-4)");
137 DEFINE_int(aecm_comfort_noise,
138 kParameterNotSpecifiedValue,
139 "Activate (1) or deactivate(0) the AECM comfort noise");
140 DEFINE_int(agc_mode,
141 kParameterNotSpecifiedValue,
142 "Specify the AGC mode (0-2)");
143 DEFINE_int(agc_target_level,
144 kParameterNotSpecifiedValue,
145 "Specify the AGC target level (0-31)");
146 DEFINE_int(agc_limiter,
147 kParameterNotSpecifiedValue,
148 "Activate (1) or deactivate(0) the level estimator");
149 DEFINE_int(agc_compression_gain,
150 kParameterNotSpecifiedValue,
151 "Specify the AGC compression gain (0-90)");
152 DEFINE_float(agc2_fixed_gain_db, 0.f, "AGC2 fixed gain (dB) to apply");
153 DEFINE_int(vad_likelihood,
154 kParameterNotSpecifiedValue,
155 "Specify the VAD likelihood (0-3)");
156 DEFINE_int(ns_level,
157 kParameterNotSpecifiedValue,
158 "Specify the NS level (0-3)");
159 DEFINE_int(stream_delay,
160 kParameterNotSpecifiedValue,
161 "Specify the stream delay in ms to use");
162 DEFINE_int(stream_drift_samples,
163 kParameterNotSpecifiedValue,
164 "Specify the number of stream drift samples to use");
165 DEFINE_int(initial_mic_level, 100, "Initial mic level (0-255)");
166 DEFINE_int(simulate_mic_gain,
167 0,
168 "Activate (1) or deactivate(0) the analog mic gain simulation");
169 DEFINE_int(simulated_mic_kind,
170 kParameterNotSpecifiedValue,
171 "Specify which microphone kind to use for microphone simulation");
172 DEFINE_bool(performance_report, false, "Report the APM performance ");
173 DEFINE_bool(verbose, false, "Produce verbose output");
174 DEFINE_bool(bitexactness_report,
175 false,
176 "Report bitexactness for aec dump result reproduction");
177 DEFINE_bool(discard_settings_in_aecdump,
178 false,
179 "Discard any config settings specified in the aec dump");
180 DEFINE_bool(store_intermediate_output,
181 false,
182 "Creates new output files after each init");
183 DEFINE_string(custom_call_order_file, "", "Custom process API call order file");
184 DEFINE_bool(help, false, "Print this message");
185
SetSettingIfSpecified(const std::string & value,rtc::Optional<std::string> * parameter)186 void SetSettingIfSpecified(const std::string& value,
187 rtc::Optional<std::string>* parameter) {
188 if (value.compare("") != 0) {
189 *parameter = value;
190 }
191 }
192
SetSettingIfSpecified(int value,rtc::Optional<int> * parameter)193 void SetSettingIfSpecified(int value, rtc::Optional<int>* parameter) {
194 if (value != kParameterNotSpecifiedValue) {
195 *parameter = value;
196 }
197 }
198
SetSettingIfFlagSet(int32_t flag,rtc::Optional<bool> * parameter)199 void SetSettingIfFlagSet(int32_t flag, rtc::Optional<bool>* parameter) {
200 if (flag == 0) {
201 *parameter = false;
202 } else if (flag == 1) {
203 *parameter = true;
204 }
205 }
206
CreateSettings()207 SimulationSettings CreateSettings() {
208 SimulationSettings settings;
209 if (FLAG_all_default) {
210 settings.use_le = true;
211 settings.use_vad = true;
212 settings.use_ie = false;
213 settings.use_bf = false;
214 settings.use_ts = true;
215 settings.use_ns = true;
216 settings.use_hpf = true;
217 settings.use_agc = true;
218 settings.use_agc2 = false;
219 settings.use_aec = true;
220 settings.use_aecm = false;
221 settings.use_ed = false;
222 }
223 SetSettingIfSpecified(FLAG_dump_input, &settings.aec_dump_input_filename);
224 SetSettingIfSpecified(FLAG_dump_output, &settings.aec_dump_output_filename);
225 SetSettingIfSpecified(FLAG_i, &settings.input_filename);
226 SetSettingIfSpecified(FLAG_o, &settings.output_filename);
227 SetSettingIfSpecified(FLAG_ri, &settings.reverse_input_filename);
228 SetSettingIfSpecified(FLAG_ro, &settings.reverse_output_filename);
229 SetSettingIfSpecified(FLAG_artificial_nearend,
230 &settings.artificial_nearend_filename);
231 SetSettingIfSpecified(FLAG_output_num_channels,
232 &settings.output_num_channels);
233 SetSettingIfSpecified(FLAG_reverse_output_num_channels,
234 &settings.reverse_output_num_channels);
235 SetSettingIfSpecified(FLAG_output_sample_rate_hz,
236 &settings.output_sample_rate_hz);
237 SetSettingIfSpecified(FLAG_reverse_output_sample_rate_hz,
238 &settings.reverse_output_sample_rate_hz);
239 SetSettingIfSpecified(FLAG_mic_positions, &settings.microphone_positions);
240 settings.target_angle_degrees = FLAG_target_angle_degrees;
241 SetSettingIfFlagSet(FLAG_aec, &settings.use_aec);
242 SetSettingIfFlagSet(FLAG_aecm, &settings.use_aecm);
243 SetSettingIfFlagSet(FLAG_ed, &settings.use_ed);
244 SetSettingIfSpecified(FLAG_ed_graph, &settings.ed_graph_output_filename);
245 SetSettingIfFlagSet(FLAG_agc, &settings.use_agc);
246 SetSettingIfFlagSet(FLAG_agc2, &settings.use_agc2);
247 SetSettingIfFlagSet(FLAG_hpf, &settings.use_hpf);
248 SetSettingIfFlagSet(FLAG_ns, &settings.use_ns);
249 SetSettingIfFlagSet(FLAG_ts, &settings.use_ts);
250 SetSettingIfFlagSet(FLAG_bf, &settings.use_bf);
251 SetSettingIfFlagSet(FLAG_ie, &settings.use_ie);
252 SetSettingIfFlagSet(FLAG_vad, &settings.use_vad);
253 SetSettingIfFlagSet(FLAG_le, &settings.use_le);
254 SetSettingIfSpecified(FLAG_aec_suppression_level,
255 &settings.aec_suppression_level);
256 SetSettingIfFlagSet(FLAG_delay_agnostic, &settings.use_delay_agnostic);
257 SetSettingIfFlagSet(FLAG_extended_filter, &settings.use_extended_filter);
258 SetSettingIfFlagSet(FLAG_drift_compensation,
259 &settings.use_drift_compensation);
260 SetSettingIfFlagSet(FLAG_refined_adaptive_filter,
261 &settings.use_refined_adaptive_filter);
262
263 SetSettingIfFlagSet(FLAG_aec3, &settings.use_aec3);
264 SetSettingIfFlagSet(FLAG_lc, &settings.use_lc);
265 SetSettingIfFlagSet(FLAG_experimental_agc, &settings.use_experimental_agc);
266 SetSettingIfSpecified(FLAG_aecm_routing_mode, &settings.aecm_routing_mode);
267 SetSettingIfFlagSet(FLAG_aecm_comfort_noise,
268 &settings.use_aecm_comfort_noise);
269 SetSettingIfSpecified(FLAG_agc_mode, &settings.agc_mode);
270 SetSettingIfSpecified(FLAG_agc_target_level, &settings.agc_target_level);
271 SetSettingIfFlagSet(FLAG_agc_limiter, &settings.use_agc_limiter);
272 SetSettingIfSpecified(FLAG_agc_compression_gain,
273 &settings.agc_compression_gain);
274 settings.agc2_fixed_gain_db = FLAG_agc2_fixed_gain_db;
275 SetSettingIfSpecified(FLAG_vad_likelihood, &settings.vad_likelihood);
276 SetSettingIfSpecified(FLAG_ns_level, &settings.ns_level);
277 SetSettingIfSpecified(FLAG_stream_delay, &settings.stream_delay);
278 SetSettingIfSpecified(FLAG_stream_drift_samples,
279 &settings.stream_drift_samples);
280 SetSettingIfSpecified(FLAG_custom_call_order_file,
281 &settings.custom_call_order_filename);
282 settings.initial_mic_level = FLAG_initial_mic_level;
283 settings.simulate_mic_gain = FLAG_simulate_mic_gain;
284 SetSettingIfSpecified(FLAG_simulated_mic_kind, &settings.simulated_mic_kind);
285 settings.report_performance = FLAG_performance_report;
286 settings.use_verbose_logging = FLAG_verbose;
287 settings.report_bitexactness = FLAG_bitexactness_report;
288 settings.discard_all_settings_in_aecdump = FLAG_discard_settings_in_aecdump;
289 settings.fixed_interface = FLAG_fixed_interface;
290 settings.store_intermediate_output = FLAG_store_intermediate_output;
291
292 return settings;
293 }
294
ReportConditionalErrorAndExit(bool condition,const std::string & message)295 void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
296 if (condition) {
297 std::cerr << message << std::endl;
298 exit(1);
299 }
300 }
301
PerformBasicParameterSanityChecks(const SimulationSettings & settings)302 void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
303 if (settings.input_filename || settings.reverse_input_filename) {
304 ReportConditionalErrorAndExit(!!settings.aec_dump_input_filename,
305 "Error: The aec dump cannot be specified "
306 "together with input wav files!\n");
307
308 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
309 "Error: The artificial nearend cannot be "
310 "specified together with input wav files!\n");
311
312 ReportConditionalErrorAndExit(!settings.input_filename,
313 "Error: When operating at wav files, the "
314 "input wav filename must be "
315 "specified!\n");
316
317 ReportConditionalErrorAndExit(
318 settings.reverse_output_filename && !settings.reverse_input_filename,
319 "Error: When operating at wav files, the reverse input wav filename "
320 "must be specified if the reverse output wav filename is specified!\n");
321 } else {
322 ReportConditionalErrorAndExit(!settings.aec_dump_input_filename,
323 "Error: Either the aec dump or the wav "
324 "input files must be specified!\n");
325 }
326
327 ReportConditionalErrorAndExit(
328 settings.use_aec && *settings.use_aec && settings.use_aecm &&
329 *settings.use_aecm,
330 "Error: The AEC and the AECM cannot be activated at the same time!\n");
331
332 ReportConditionalErrorAndExit(
333 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
334 "Error: --output_sample_rate_hz must be positive!\n");
335
336 ReportConditionalErrorAndExit(
337 settings.reverse_output_sample_rate_hz &&
338 settings.output_sample_rate_hz &&
339 *settings.output_sample_rate_hz <= 0,
340 "Error: --reverse_output_sample_rate_hz must be positive!\n");
341
342 ReportConditionalErrorAndExit(
343 settings.output_num_channels && *settings.output_num_channels <= 0,
344 "Error: --output_num_channels must be positive!\n");
345
346 ReportConditionalErrorAndExit(
347 settings.reverse_output_num_channels &&
348 *settings.reverse_output_num_channels <= 0,
349 "Error: --reverse_output_num_channels must be positive!\n");
350
351 ReportConditionalErrorAndExit(
352 settings.use_bf && *settings.use_bf && !settings.microphone_positions,
353 "Error: --mic_positions must be specified when the beamformer is "
354 "activated.\n");
355
356 ReportConditionalErrorAndExit(
357 settings.target_angle_degrees < 0 || settings.target_angle_degrees > 359,
358 "Error: -target_angle_degrees must be specified between 0 and 359.\n");
359
360 ReportConditionalErrorAndExit(
361 settings.aec_suppression_level &&
362 ((*settings.aec_suppression_level) < 0 ||
363 (*settings.aec_suppression_level) > 2),
364 "Error: --aec_suppression_level must be specified between 0 and 2.\n");
365
366 ReportConditionalErrorAndExit(
367 settings.aecm_routing_mode && ((*settings.aecm_routing_mode) < 0 ||
368 (*settings.aecm_routing_mode) > 4),
369 "Error: --aecm_routing_mode must be specified between 0 and 4.\n");
370
371 ReportConditionalErrorAndExit(
372 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
373 (*settings.agc_target_level) > 31),
374 "Error: --agc_target_level must be specified between 0 and 31.\n");
375
376 ReportConditionalErrorAndExit(
377 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
378 (*settings.agc_compression_gain) > 90),
379 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
380
381 ReportConditionalErrorAndExit(
382 settings.use_agc && *settings.use_agc && settings.use_agc2 &&
383 *settings.use_agc2,
384 "Error: --agc and --agc2 cannot be both active.\n");
385
386 ReportConditionalErrorAndExit(
387 settings.use_agc2 && *settings.use_agc2 &&
388 ((settings.agc2_fixed_gain_db) < 0 ||
389 (settings.agc2_fixed_gain_db) > 90),
390 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
391
392 ReportConditionalErrorAndExit(
393 settings.vad_likelihood &&
394 ((*settings.vad_likelihood) < 0 || (*settings.vad_likelihood) > 3),
395 "Error: --vad_likelihood must be specified between 0 and 3.\n");
396
397 ReportConditionalErrorAndExit(
398 settings.ns_level &&
399 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
400 "Error: --ns_level must be specified between 0 and 3.\n");
401
402 ReportConditionalErrorAndExit(
403 settings.report_bitexactness && !settings.aec_dump_input_filename,
404 "Error: --bitexactness_report can only be used when operating on an "
405 "aecdump\n");
406
407 ReportConditionalErrorAndExit(
408 settings.custom_call_order_filename && settings.aec_dump_input_filename,
409 "Error: --custom_call_order_file cannot be used when operating on an "
410 "aecdump\n");
411
412 ReportConditionalErrorAndExit(
413 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
414 "Error: --initial_mic_level must be specified between 0 and 255.\n");
415
416 ReportConditionalErrorAndExit(
417 settings.simulated_mic_kind && !settings.simulate_mic_gain,
418 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
419 "disabled\n");
420
421 ReportConditionalErrorAndExit(
422 !settings.simulated_mic_kind && settings.simulate_mic_gain,
423 "Error: --simulated_mic_kind must be specified when mic simulation is "
424 "enabled\n");
425
426 auto valid_wav_name = [](const std::string& wav_file_name) {
427 if (wav_file_name.size() < 5) {
428 return false;
429 }
430 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
431 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
432 return true;
433 }
434 return false;
435 };
436
437 ReportConditionalErrorAndExit(
438 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
439 "Error: --i must be a valid .wav file name.\n");
440
441 ReportConditionalErrorAndExit(
442 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
443 "Error: --o must be a valid .wav file name.\n");
444
445 ReportConditionalErrorAndExit(
446 settings.reverse_input_filename &&
447 (!valid_wav_name(*settings.reverse_input_filename)),
448 "Error: --ri must be a valid .wav file name.\n");
449
450 ReportConditionalErrorAndExit(
451 settings.reverse_output_filename &&
452 (!valid_wav_name(*settings.reverse_output_filename)),
453 "Error: --ro must be a valid .wav file name.\n");
454
455 ReportConditionalErrorAndExit(
456 settings.artificial_nearend_filename &&
457 !valid_wav_name(*settings.artificial_nearend_filename),
458 "Error: --artifical_nearend must be a valid .wav file name.\n");
459 }
460
461 } // namespace
462
main(int argc,char * argv[])463 int main(int argc, char* argv[]) {
464 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true) ||
465 FLAG_help || argc != 1) {
466 printf("%s", kUsageDescription);
467 if (FLAG_help) {
468 rtc::FlagList::Print(nullptr, false);
469 return 0;
470 }
471 return 1;
472 }
473
474 SimulationSettings settings = CreateSettings();
475 PerformBasicParameterSanityChecks(settings);
476 std::unique_ptr<AudioProcessingSimulator> processor;
477
478 if (settings.aec_dump_input_filename) {
479 processor.reset(new AecDumpBasedSimulator(settings));
480 } else {
481 processor.reset(new WavBasedSimulator(settings));
482 }
483
484 processor->Process();
485
486 if (settings.report_performance) {
487 const auto& proc_time = processor->proc_time();
488 int64_t exec_time_us = proc_time.sum / rtc::kNumNanosecsPerMicrosec;
489 std::cout << std::endl
490 << "Execution time: " << exec_time_us * 1e-6 << " s, File time: "
491 << processor->get_num_process_stream_calls() * 1.f /
492 AudioProcessingSimulator::kChunksPerSecond
493 << std::endl
494 << "Time per fwd stream chunk (mean, max, min): " << std::endl
495 << exec_time_us * 1.f / processor->get_num_process_stream_calls()
496 << " us, " << 1.f * proc_time.max / rtc::kNumNanosecsPerMicrosec
497 << " us, " << 1.f * proc_time.min / rtc::kNumNanosecsPerMicrosec
498 << " us" << std::endl;
499 }
500
501 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
502 if (processor->OutputWasBitexact()) {
503 std::cout << "The processing was bitexact.";
504 } else {
505 std::cout << "The processing was not bitexact.";
506 }
507 }
508
509 return 0;
510 }
511
512 } // namespace test
513 } // namespace webrtc
514
main(int argc,char * argv[])515 int main(int argc, char* argv[]) {
516 return webrtc::test::main(argc, argv);
517 }
518