blob: edc775986f74183960f0dca71cf9999a00c9d32d [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");
peahdf80fd12016-12-09 02:43:40 -080043DEFINE_string(artificial_nearend, "", "Artificial nearend wav filename");
peah60a189f2016-05-24 20:54:40 -070044DEFINE_int32(output_num_channels,
45 kParameterNotSpecifiedValue,
46 "Number of forward stream output channels");
47DEFINE_int32(reverse_output_num_channels,
48 kParameterNotSpecifiedValue,
49 "Number of Reverse stream output channels");
50DEFINE_int32(output_sample_rate_hz,
51 kParameterNotSpecifiedValue,
52 "Forward stream output sample rate in Hz");
53DEFINE_int32(reverse_output_sample_rate_hz,
54 kParameterNotSpecifiedValue,
55 "Reverse stream output sample rate in Hz");
56DEFINE_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\"");
61DEFINE_int32(target_angle_degrees,
62 90,
63 "The azimuth of the target in degrees (0-359). Only applies to "
64 "beamforming.");
65DEFINE_bool(fixed_interface,
66 false,
67 "Use the fixed interface when operating on wav files");
68DEFINE_int32(aec,
69 kParameterNotSpecifiedValue,
70 "Activate (1) or deactivate(0) the echo canceller");
71DEFINE_int32(aecm,
72 kParameterNotSpecifiedValue,
73 "Activate (1) or deactivate(0) the mobile echo controller");
ivoc0bff12a2016-11-17 00:55:43 -080074DEFINE_int32(ed,
ivoc87d1a782016-11-14 07:55:03 -080075 kParameterNotSpecifiedValue,
76 "Activate (1) or deactivate (0) the residual echo detector");
ivoc0bff12a2016-11-17 00:55:43 -080077DEFINE_string(ed_graph, "", "Output filename for graph of echo likelihood");
peah60a189f2016-05-24 20:54:40 -070078DEFINE_int32(agc,
79 kParameterNotSpecifiedValue,
80 "Activate (1) or deactivate(0) the AGC");
81DEFINE_int32(hpf,
82 kParameterNotSpecifiedValue,
83 "Activate (1) or deactivate(0) the high-pass filter");
84DEFINE_int32(ns,
85 kParameterNotSpecifiedValue,
86 "Activate (1) or deactivate(0) the noise suppressor");
87DEFINE_int32(ts,
88 kParameterNotSpecifiedValue,
89 "Activate (1) or deactivate(0) the transient suppressor");
90DEFINE_int32(bf,
91 kParameterNotSpecifiedValue,
92 "Activate (1) or deactivate(0) the beamformer");
93DEFINE_int32(ie,
94 kParameterNotSpecifiedValue,
95 "Activate (1) or deactivate(0) the intelligibility enhancer");
96DEFINE_int32(vad,
97 kParameterNotSpecifiedValue,
98 "Activate (1) or deactivate(0) the voice activity detector");
99DEFINE_int32(le,
100 kParameterNotSpecifiedValue,
101 "Activate (1) or deactivate(0) the level estimator");
102DEFINE_bool(all_default,
103 false,
104 "Activate all of the default components (will be overridden by any "
105 "other settings)");
106DEFINE_int32(aec_suppression_level,
107 kParameterNotSpecifiedValue,
108 "Set the aec suppression level (0-2)");
109DEFINE_int32(delay_agnostic,
110 kParameterNotSpecifiedValue,
111 "Activate (1) or deactivate(0) the AEC delay agnostic mode");
112DEFINE_int32(extended_filter,
113 kParameterNotSpecifiedValue,
114 "Activate (1) or deactivate(0) the AEC extended filter mode");
115DEFINE_int32(drift_compensation,
116 kParameterNotSpecifiedValue,
117 "Activate (1) or deactivate(0) the drift compensation");
118DEFINE_int32(aec3,
119 kParameterNotSpecifiedValue,
120 "Activate (1) or deactivate(0) the experimental AEC mode AEC3");
peahca4cac72016-06-29 15:26:12 -0700121DEFINE_int32(lc,
122 kParameterNotSpecifiedValue,
123 "Activate (1) or deactivate(0) the level control");
peah60a189f2016-05-24 20:54:40 -0700124DEFINE_int32(
125 refined_adaptive_filter,
126 kParameterNotSpecifiedValue,
127 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
128DEFINE_int32(aecm_routing_mode,
129 kParameterNotSpecifiedValue,
130 "Specify the AECM routing mode (0-4)");
131DEFINE_int32(aecm_comfort_noise,
132 kParameterNotSpecifiedValue,
133 "Activate (1) or deactivate(0) the AECM comfort noise");
134DEFINE_int32(agc_mode,
135 kParameterNotSpecifiedValue,
136 "Specify the AGC mode (0-2)");
137DEFINE_int32(agc_target_level,
138 kParameterNotSpecifiedValue,
139 "Specify the AGC target level (0-31)");
140DEFINE_int32(agc_limiter,
141 kParameterNotSpecifiedValue,
142 "Activate (1) or deactivate(0) the level estimator");
143DEFINE_int32(agc_compression_gain,
144 kParameterNotSpecifiedValue,
145 "Specify the AGC compression gain (0-90)");
146DEFINE_int32(vad_likelihood,
147 kParameterNotSpecifiedValue,
148 "Specify the VAD likelihood (0-3)");
149DEFINE_int32(ns_level,
150 kParameterNotSpecifiedValue,
151 "Specify the NS level (0-3)");
152DEFINE_int32(stream_delay,
153 kParameterNotSpecifiedValue,
154 "Specify the stream delay in ms to use");
155DEFINE_int32(stream_drift_samples,
156 kParameterNotSpecifiedValue,
157 "Specify the number of stream drift samples to use");
158DEFINE_bool(performance_report, false, "Report the APM performance ");
159DEFINE_bool(verbose, false, "Produce verbose output");
160DEFINE_bool(bitexactness_report,
161 false,
162 "Report bitexactness for aec dump result reproduction");
163DEFINE_bool(discard_settings_in_aecdump,
164 false,
165 "Discard any config settings specified in the aec dump");
166DEFINE_bool(store_intermediate_output,
167 false,
168 "Creates new output files after each init");
169
170void SetSettingIfSpecified(const std::string value,
171 rtc::Optional<std::string>* parameter) {
172 if (value.compare("") != 0) {
173 *parameter = rtc::Optional<std::string>(value);
174 }
175}
176
177void SetSettingIfSpecified(int value, rtc::Optional<int>* parameter) {
178 if (value != kParameterNotSpecifiedValue) {
179 *parameter = rtc::Optional<int>(value);
180 }
181}
182
183void SetSettingIfFlagSet(int32_t flag, rtc::Optional<bool>* parameter) {
184 if (flag == 0) {
185 *parameter = rtc::Optional<bool>(false);
186 } else if (flag == 1) {
187 *parameter = rtc::Optional<bool>(true);
188 }
189}
190
191SimulationSettings CreateSettings() {
192 SimulationSettings settings;
193 if (FLAGS_all_default) {
194 settings.use_le = rtc::Optional<bool>(true);
195 settings.use_vad = rtc::Optional<bool>(true);
196 settings.use_ie = rtc::Optional<bool>(false);
197 settings.use_bf = rtc::Optional<bool>(false);
198 settings.use_ts = rtc::Optional<bool>(true);
199 settings.use_ns = rtc::Optional<bool>(true);
200 settings.use_hpf = rtc::Optional<bool>(true);
201 settings.use_agc = rtc::Optional<bool>(true);
202 settings.use_aec = rtc::Optional<bool>(true);
203 settings.use_aecm = rtc::Optional<bool>(false);
ivoc0bff12a2016-11-17 00:55:43 -0800204 settings.use_ed = rtc::Optional<bool>(false);
peah60a189f2016-05-24 20:54:40 -0700205 }
206 SetSettingIfSpecified(FLAGS_dump_input, &settings.aec_dump_input_filename);
207 SetSettingIfSpecified(FLAGS_dump_output, &settings.aec_dump_output_filename);
208 SetSettingIfSpecified(FLAGS_i, &settings.input_filename);
209 SetSettingIfSpecified(FLAGS_o, &settings.output_filename);
210 SetSettingIfSpecified(FLAGS_ri, &settings.reverse_input_filename);
211 SetSettingIfSpecified(FLAGS_ro, &settings.reverse_output_filename);
peahdf80fd12016-12-09 02:43:40 -0800212 SetSettingIfSpecified(FLAGS_artificial_nearend,
213 &settings.artificial_nearend_filename);
peah60a189f2016-05-24 20:54:40 -0700214 SetSettingIfSpecified(FLAGS_output_num_channels,
215 &settings.output_num_channels);
216 SetSettingIfSpecified(FLAGS_reverse_output_num_channels,
217 &settings.reverse_output_num_channels);
218 SetSettingIfSpecified(FLAGS_output_sample_rate_hz,
219 &settings.output_sample_rate_hz);
220 SetSettingIfSpecified(FLAGS_reverse_output_sample_rate_hz,
221 &settings.reverse_output_sample_rate_hz);
222 SetSettingIfSpecified(FLAGS_mic_positions, &settings.microphone_positions);
223 settings.target_angle_degrees = FLAGS_target_angle_degrees;
224 SetSettingIfFlagSet(FLAGS_aec, &settings.use_aec);
225 SetSettingIfFlagSet(FLAGS_aecm, &settings.use_aecm);
ivoc0bff12a2016-11-17 00:55:43 -0800226 SetSettingIfFlagSet(FLAGS_ed, &settings.use_ed);
227 SetSettingIfSpecified(FLAGS_ed_graph, &settings.ed_graph_output_filename);
peah60a189f2016-05-24 20:54:40 -0700228 SetSettingIfFlagSet(FLAGS_agc, &settings.use_agc);
229 SetSettingIfFlagSet(FLAGS_hpf, &settings.use_hpf);
230 SetSettingIfFlagSet(FLAGS_ns, &settings.use_ns);
231 SetSettingIfFlagSet(FLAGS_ts, &settings.use_ts);
232 SetSettingIfFlagSet(FLAGS_bf, &settings.use_bf);
233 SetSettingIfFlagSet(FLAGS_ie, &settings.use_ie);
234 SetSettingIfFlagSet(FLAGS_vad, &settings.use_vad);
235 SetSettingIfFlagSet(FLAGS_le, &settings.use_le);
236 SetSettingIfSpecified(FLAGS_aec_suppression_level,
237 &settings.aec_suppression_level);
238 SetSettingIfFlagSet(FLAGS_delay_agnostic, &settings.use_delay_agnostic);
239 SetSettingIfFlagSet(FLAGS_extended_filter, &settings.use_extended_filter);
240 SetSettingIfFlagSet(FLAGS_drift_compensation,
241 &settings.use_drift_compensation);
242 SetSettingIfFlagSet(FLAGS_refined_adaptive_filter,
243 &settings.use_refined_adaptive_filter);
244
245 SetSettingIfFlagSet(FLAGS_aec3, &settings.use_aec3);
peahca4cac72016-06-29 15:26:12 -0700246 SetSettingIfFlagSet(FLAGS_lc, &settings.use_lc);
peah60a189f2016-05-24 20:54:40 -0700247 SetSettingIfSpecified(FLAGS_aecm_routing_mode, &settings.aecm_routing_mode);
248 SetSettingIfFlagSet(FLAGS_aecm_comfort_noise,
249 &settings.use_aecm_comfort_noise);
250 SetSettingIfSpecified(FLAGS_agc_mode, &settings.agc_mode);
251 SetSettingIfSpecified(FLAGS_agc_target_level, &settings.agc_target_level);
252 SetSettingIfFlagSet(FLAGS_agc_limiter, &settings.use_agc_limiter);
253 SetSettingIfSpecified(FLAGS_agc_compression_gain,
254 &settings.agc_compression_gain);
255 SetSettingIfSpecified(FLAGS_vad_likelihood, &settings.vad_likelihood);
256 SetSettingIfSpecified(FLAGS_ns_level, &settings.ns_level);
257 SetSettingIfSpecified(FLAGS_stream_delay, &settings.stream_delay);
258 SetSettingIfSpecified(FLAGS_stream_drift_samples,
259 &settings.stream_drift_samples);
260 settings.report_performance = FLAGS_performance_report;
261 settings.use_verbose_logging = FLAGS_verbose;
262 settings.report_bitexactness = FLAGS_bitexactness_report;
263 settings.discard_all_settings_in_aecdump = FLAGS_discard_settings_in_aecdump;
264 settings.fixed_interface = FLAGS_fixed_interface;
265 settings.store_intermediate_output = FLAGS_store_intermediate_output;
266
267 return settings;
268}
269
270void ReportConditionalErrorAndExit(bool condition, std::string message) {
271 if (condition) {
272 std::cerr << message << std::endl;
273 exit(1);
274 }
275}
276
277void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
278 if (settings.input_filename || settings.reverse_input_filename) {
279 ReportConditionalErrorAndExit(!!settings.aec_dump_input_filename,
280 "Error: The aec dump cannot be specified "
281 "together with input wav files!\n");
282
peahdf80fd12016-12-09 02:43:40 -0800283 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
284 "Error: The artificial nearend cannot be "
285 "specified together with input wav files!\n");
286
peah60a189f2016-05-24 20:54:40 -0700287 ReportConditionalErrorAndExit(!settings.input_filename,
288 "Error: When operating at wav files, the "
289 "input wav filename must be "
290 "specified!\n");
291
292 ReportConditionalErrorAndExit(
293 settings.reverse_output_filename && !settings.reverse_input_filename,
294 "Error: When operating at wav files, the reverse input wav filename "
295 "must be specified if the reverse output wav filename is specified!\n");
296 } else {
297 ReportConditionalErrorAndExit(!settings.aec_dump_input_filename,
298 "Error: Either the aec dump or the wav "
299 "input files must be specified!\n");
300 }
301
302 ReportConditionalErrorAndExit(
303 settings.use_aec && *settings.use_aec && settings.use_aecm &&
304 *settings.use_aecm,
305 "Error: The AEC and the AECM cannot be activated at the same time!\n");
306
307 ReportConditionalErrorAndExit(
308 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
309 "Error: --output_sample_rate_hz must be positive!\n");
310
311 ReportConditionalErrorAndExit(
312 settings.reverse_output_sample_rate_hz &&
313 settings.output_sample_rate_hz &&
314 *settings.output_sample_rate_hz <= 0,
315 "Error: --reverse_output_sample_rate_hz must be positive!\n");
316
317 ReportConditionalErrorAndExit(
318 settings.output_num_channels && *settings.output_num_channels <= 0,
319 "Error: --output_num_channels must be positive!\n");
320
321 ReportConditionalErrorAndExit(
322 settings.reverse_output_num_channels &&
323 *settings.reverse_output_num_channels <= 0,
324 "Error: --reverse_output_num_channels must be positive!\n");
325
326 ReportConditionalErrorAndExit(
327 settings.use_bf && *settings.use_bf && !settings.microphone_positions,
328 "Error: --mic_positions must be specified when the beamformer is "
329 "activated.\n");
330
331 ReportConditionalErrorAndExit(
332 settings.target_angle_degrees < 0 || settings.target_angle_degrees > 359,
333 "Error: -target_angle_degrees must be specified between 0 and 359.\n");
334
335 ReportConditionalErrorAndExit(
336 settings.aec_suppression_level &&
337 ((*settings.aec_suppression_level) < 0 ||
338 (*settings.aec_suppression_level) > 2),
339 "Error: --aec_suppression_level must be specified between 0 and 2.\n");
340
341 ReportConditionalErrorAndExit(
342 settings.aecm_routing_mode && ((*settings.aecm_routing_mode) < 0 ||
343 (*settings.aecm_routing_mode) > 4),
344 "Error: --aecm_routing_mode must be specified between 0 and 4.\n");
345
346 ReportConditionalErrorAndExit(
347 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
348 (*settings.agc_target_level) > 31),
349 "Error: --agc_target_level must be specified between 0 and 31.\n");
350
351 ReportConditionalErrorAndExit(
352 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
353 (*settings.agc_compression_gain) > 90),
354 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
355
356 ReportConditionalErrorAndExit(
357 settings.vad_likelihood &&
358 ((*settings.vad_likelihood) < 0 || (*settings.vad_likelihood) > 3),
359 "Error: --vad_likelihood must be specified between 0 and 3.\n");
360
361 ReportConditionalErrorAndExit(
362 settings.ns_level &&
363 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
364 "Error: --ns_level must be specified between 0 and 3.\n");
365
366 ReportConditionalErrorAndExit(
367 settings.report_bitexactness && !settings.aec_dump_input_filename,
368 "Error: --bitexactness_report can only be used when operating on an "
369 "aecdump\n");
370
371 auto valid_wav_name = [](const std::string& wav_file_name) {
372 if (wav_file_name.size() < 5) {
373 return false;
374 }
375 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
376 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
377 return true;
378 }
379 return false;
380 };
381
382 ReportConditionalErrorAndExit(
383 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
384 "Error: --i must be a valid .wav file name.\n");
385
386 ReportConditionalErrorAndExit(
387 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
388 "Error: --o must be a valid .wav file name.\n");
389
390 ReportConditionalErrorAndExit(
391 settings.reverse_input_filename &&
392 (!valid_wav_name(*settings.reverse_input_filename)),
393 "Error: --ri must be a valid .wav file name.\n");
394
395 ReportConditionalErrorAndExit(
396 settings.reverse_output_filename &&
397 (!valid_wav_name(*settings.reverse_output_filename)),
398 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800399
400 ReportConditionalErrorAndExit(
401 settings.artificial_nearend_filename &&
402 !valid_wav_name(*settings.artificial_nearend_filename),
403 "Error: --artifical_nearend must be a valid .wav file name.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800404}
405
406} // namespace
407
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000408int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700409 google::SetUsageMessage(kUsageDescription);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000410 google::ParseCommandLineFlags(&argc, &argv, true);
411
peah60a189f2016-05-24 20:54:40 -0700412 SimulationSettings settings = CreateSettings();
413 PerformBasicParameterSanityChecks(settings);
414 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000415
peah60a189f2016-05-24 20:54:40 -0700416 if (settings.aec_dump_input_filename) {
417 processor.reset(new AecDumpBasedSimulator(settings));
aluebsb0ad43b2015-11-20 00:11:53 -0800418 } else {
peah60a189f2016-05-24 20:54:40 -0700419 processor.reset(new WavBasedSimulator(settings));
andrewbdafe312015-10-29 23:42:54 -0700420 }
421
peah60a189f2016-05-24 20:54:40 -0700422 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800423
peah60a189f2016-05-24 20:54:40 -0700424 if (settings.report_performance) {
aluebsb0ad43b2015-11-20 00:11:53 -0800425 const auto& proc_time = processor->proc_time();
Niels Möllerd28db7f2016-05-10 16:31:47 +0200426 int64_t exec_time_us = proc_time.sum / rtc::kNumNanosecsPerMicrosec;
peah60a189f2016-05-24 20:54:40 -0700427 std::cout << std::endl
428 << "Execution time: " << exec_time_us * 1e-6 << " s, File time: "
429 << processor->get_num_process_stream_calls() * 1.f /
430 AudioProcessingSimulator::kChunksPerSecond
431 << std::endl
432 << "Time per fwd stream chunk (mean, max, min): " << std::endl
433 << exec_time_us * 1.f / processor->get_num_process_stream_calls()
434 << " us, " << 1.f * proc_time.max / rtc::kNumNanosecsPerMicrosec
435 << " us, " << 1.f * proc_time.min / rtc::kNumNanosecsPerMicrosec
436 << " us" << std::endl;
437 }
438
439 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
440 if (processor->OutputWasBitexact()) {
441 std::cout << "The processing was bitexact.";
442 } else {
443 std::cout << "The processing was not bitexact.";
444 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700445 }
aluebsb0ad43b2015-11-20 00:11:53 -0800446
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000447 return 0;
448}
449
peah60a189f2016-05-24 20:54:40 -0700450} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000451} // namespace webrtc
452
453int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700454 return webrtc::test::main(argc, argv);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000455}