blob: 201d0fd5d1c783934538e62e81db1a996cfbcdc1 [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");
ivoc87d1a782016-11-14 07:55:03 -080073DEFINE_int32(red,
74 kParameterNotSpecifiedValue,
75 "Activate (1) or deactivate (0) the residual echo detector");
76DEFINE_string(red_graph, "", "Output filename for graph of echo likelihood");
peah60a189f2016-05-24 20:54:40 -070077DEFINE_int32(agc,
78 kParameterNotSpecifiedValue,
79 "Activate (1) or deactivate(0) the AGC");
80DEFINE_int32(hpf,
81 kParameterNotSpecifiedValue,
82 "Activate (1) or deactivate(0) the high-pass filter");
83DEFINE_int32(ns,
84 kParameterNotSpecifiedValue,
85 "Activate (1) or deactivate(0) the noise suppressor");
86DEFINE_int32(ts,
87 kParameterNotSpecifiedValue,
88 "Activate (1) or deactivate(0) the transient suppressor");
89DEFINE_int32(bf,
90 kParameterNotSpecifiedValue,
91 "Activate (1) or deactivate(0) the beamformer");
92DEFINE_int32(ie,
93 kParameterNotSpecifiedValue,
94 "Activate (1) or deactivate(0) the intelligibility enhancer");
95DEFINE_int32(vad,
96 kParameterNotSpecifiedValue,
97 "Activate (1) or deactivate(0) the voice activity detector");
98DEFINE_int32(le,
99 kParameterNotSpecifiedValue,
100 "Activate (1) or deactivate(0) the level estimator");
101DEFINE_bool(all_default,
102 false,
103 "Activate all of the default components (will be overridden by any "
104 "other settings)");
105DEFINE_int32(aec_suppression_level,
106 kParameterNotSpecifiedValue,
107 "Set the aec suppression level (0-2)");
108DEFINE_int32(delay_agnostic,
109 kParameterNotSpecifiedValue,
110 "Activate (1) or deactivate(0) the AEC delay agnostic mode");
111DEFINE_int32(extended_filter,
112 kParameterNotSpecifiedValue,
113 "Activate (1) or deactivate(0) the AEC extended filter mode");
114DEFINE_int32(drift_compensation,
115 kParameterNotSpecifiedValue,
116 "Activate (1) or deactivate(0) the drift compensation");
117DEFINE_int32(aec3,
118 kParameterNotSpecifiedValue,
119 "Activate (1) or deactivate(0) the experimental AEC mode AEC3");
peahca4cac72016-06-29 15:26:12 -0700120DEFINE_int32(lc,
121 kParameterNotSpecifiedValue,
122 "Activate (1) or deactivate(0) the level control");
peah60a189f2016-05-24 20:54:40 -0700123DEFINE_int32(
124 refined_adaptive_filter,
125 kParameterNotSpecifiedValue,
126 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
127DEFINE_int32(aecm_routing_mode,
128 kParameterNotSpecifiedValue,
129 "Specify the AECM routing mode (0-4)");
130DEFINE_int32(aecm_comfort_noise,
131 kParameterNotSpecifiedValue,
132 "Activate (1) or deactivate(0) the AECM comfort noise");
133DEFINE_int32(agc_mode,
134 kParameterNotSpecifiedValue,
135 "Specify the AGC mode (0-2)");
136DEFINE_int32(agc_target_level,
137 kParameterNotSpecifiedValue,
138 "Specify the AGC target level (0-31)");
139DEFINE_int32(agc_limiter,
140 kParameterNotSpecifiedValue,
141 "Activate (1) or deactivate(0) the level estimator");
142DEFINE_int32(agc_compression_gain,
143 kParameterNotSpecifiedValue,
144 "Specify the AGC compression gain (0-90)");
145DEFINE_int32(vad_likelihood,
146 kParameterNotSpecifiedValue,
147 "Specify the VAD likelihood (0-3)");
148DEFINE_int32(ns_level,
149 kParameterNotSpecifiedValue,
150 "Specify the NS level (0-3)");
151DEFINE_int32(stream_delay,
152 kParameterNotSpecifiedValue,
153 "Specify the stream delay in ms to use");
154DEFINE_int32(stream_drift_samples,
155 kParameterNotSpecifiedValue,
156 "Specify the number of stream drift samples to use");
157DEFINE_bool(performance_report, false, "Report the APM performance ");
158DEFINE_bool(verbose, false, "Produce verbose output");
159DEFINE_bool(bitexactness_report,
160 false,
161 "Report bitexactness for aec dump result reproduction");
162DEFINE_bool(discard_settings_in_aecdump,
163 false,
164 "Discard any config settings specified in the aec dump");
165DEFINE_bool(store_intermediate_output,
166 false,
167 "Creates new output files after each init");
168
169void SetSettingIfSpecified(const std::string value,
170 rtc::Optional<std::string>* parameter) {
171 if (value.compare("") != 0) {
172 *parameter = rtc::Optional<std::string>(value);
173 }
174}
175
176void SetSettingIfSpecified(int value, rtc::Optional<int>* parameter) {
177 if (value != kParameterNotSpecifiedValue) {
178 *parameter = rtc::Optional<int>(value);
179 }
180}
181
182void SetSettingIfFlagSet(int32_t flag, rtc::Optional<bool>* parameter) {
183 if (flag == 0) {
184 *parameter = rtc::Optional<bool>(false);
185 } else if (flag == 1) {
186 *parameter = rtc::Optional<bool>(true);
187 }
188}
189
190SimulationSettings CreateSettings() {
191 SimulationSettings settings;
192 if (FLAGS_all_default) {
193 settings.use_le = rtc::Optional<bool>(true);
194 settings.use_vad = rtc::Optional<bool>(true);
195 settings.use_ie = rtc::Optional<bool>(false);
196 settings.use_bf = rtc::Optional<bool>(false);
197 settings.use_ts = rtc::Optional<bool>(true);
198 settings.use_ns = rtc::Optional<bool>(true);
199 settings.use_hpf = rtc::Optional<bool>(true);
200 settings.use_agc = rtc::Optional<bool>(true);
201 settings.use_aec = rtc::Optional<bool>(true);
202 settings.use_aecm = rtc::Optional<bool>(false);
ivoc87d1a782016-11-14 07:55:03 -0800203 settings.use_red = rtc::Optional<bool>(false);
peah60a189f2016-05-24 20:54:40 -0700204 }
205 SetSettingIfSpecified(FLAGS_dump_input, &settings.aec_dump_input_filename);
206 SetSettingIfSpecified(FLAGS_dump_output, &settings.aec_dump_output_filename);
207 SetSettingIfSpecified(FLAGS_i, &settings.input_filename);
208 SetSettingIfSpecified(FLAGS_o, &settings.output_filename);
209 SetSettingIfSpecified(FLAGS_ri, &settings.reverse_input_filename);
210 SetSettingIfSpecified(FLAGS_ro, &settings.reverse_output_filename);
211 SetSettingIfSpecified(FLAGS_output_num_channels,
212 &settings.output_num_channels);
213 SetSettingIfSpecified(FLAGS_reverse_output_num_channels,
214 &settings.reverse_output_num_channels);
215 SetSettingIfSpecified(FLAGS_output_sample_rate_hz,
216 &settings.output_sample_rate_hz);
217 SetSettingIfSpecified(FLAGS_reverse_output_sample_rate_hz,
218 &settings.reverse_output_sample_rate_hz);
219 SetSettingIfSpecified(FLAGS_mic_positions, &settings.microphone_positions);
220 settings.target_angle_degrees = FLAGS_target_angle_degrees;
221 SetSettingIfFlagSet(FLAGS_aec, &settings.use_aec);
222 SetSettingIfFlagSet(FLAGS_aecm, &settings.use_aecm);
ivoc87d1a782016-11-14 07:55:03 -0800223 SetSettingIfFlagSet(FLAGS_red, &settings.use_red);
224 SetSettingIfSpecified(FLAGS_red_graph, &settings.red_graph_output_filename);
peah60a189f2016-05-24 20:54:40 -0700225 SetSettingIfFlagSet(FLAGS_agc, &settings.use_agc);
226 SetSettingIfFlagSet(FLAGS_hpf, &settings.use_hpf);
227 SetSettingIfFlagSet(FLAGS_ns, &settings.use_ns);
228 SetSettingIfFlagSet(FLAGS_ts, &settings.use_ts);
229 SetSettingIfFlagSet(FLAGS_bf, &settings.use_bf);
230 SetSettingIfFlagSet(FLAGS_ie, &settings.use_ie);
231 SetSettingIfFlagSet(FLAGS_vad, &settings.use_vad);
232 SetSettingIfFlagSet(FLAGS_le, &settings.use_le);
233 SetSettingIfSpecified(FLAGS_aec_suppression_level,
234 &settings.aec_suppression_level);
235 SetSettingIfFlagSet(FLAGS_delay_agnostic, &settings.use_delay_agnostic);
236 SetSettingIfFlagSet(FLAGS_extended_filter, &settings.use_extended_filter);
237 SetSettingIfFlagSet(FLAGS_drift_compensation,
238 &settings.use_drift_compensation);
239 SetSettingIfFlagSet(FLAGS_refined_adaptive_filter,
240 &settings.use_refined_adaptive_filter);
241
242 SetSettingIfFlagSet(FLAGS_aec3, &settings.use_aec3);
peahca4cac72016-06-29 15:26:12 -0700243 SetSettingIfFlagSet(FLAGS_lc, &settings.use_lc);
peah60a189f2016-05-24 20:54:40 -0700244 SetSettingIfSpecified(FLAGS_aecm_routing_mode, &settings.aecm_routing_mode);
245 SetSettingIfFlagSet(FLAGS_aecm_comfort_noise,
246 &settings.use_aecm_comfort_noise);
247 SetSettingIfSpecified(FLAGS_agc_mode, &settings.agc_mode);
248 SetSettingIfSpecified(FLAGS_agc_target_level, &settings.agc_target_level);
249 SetSettingIfFlagSet(FLAGS_agc_limiter, &settings.use_agc_limiter);
250 SetSettingIfSpecified(FLAGS_agc_compression_gain,
251 &settings.agc_compression_gain);
252 SetSettingIfSpecified(FLAGS_vad_likelihood, &settings.vad_likelihood);
253 SetSettingIfSpecified(FLAGS_ns_level, &settings.ns_level);
254 SetSettingIfSpecified(FLAGS_stream_delay, &settings.stream_delay);
255 SetSettingIfSpecified(FLAGS_stream_drift_samples,
256 &settings.stream_drift_samples);
257 settings.report_performance = FLAGS_performance_report;
258 settings.use_verbose_logging = FLAGS_verbose;
259 settings.report_bitexactness = FLAGS_bitexactness_report;
260 settings.discard_all_settings_in_aecdump = FLAGS_discard_settings_in_aecdump;
261 settings.fixed_interface = FLAGS_fixed_interface;
262 settings.store_intermediate_output = FLAGS_store_intermediate_output;
263
264 return settings;
265}
266
267void ReportConditionalErrorAndExit(bool condition, std::string message) {
268 if (condition) {
269 std::cerr << message << std::endl;
270 exit(1);
271 }
272}
273
274void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
275 if (settings.input_filename || settings.reverse_input_filename) {
276 ReportConditionalErrorAndExit(!!settings.aec_dump_input_filename,
277 "Error: The aec dump cannot be specified "
278 "together with input wav files!\n");
279
280 ReportConditionalErrorAndExit(!settings.input_filename,
281 "Error: When operating at wav files, the "
282 "input wav filename must be "
283 "specified!\n");
284
285 ReportConditionalErrorAndExit(
286 settings.reverse_output_filename && !settings.reverse_input_filename,
287 "Error: When operating at wav files, the reverse input wav filename "
288 "must be specified if the reverse output wav filename is specified!\n");
289 } else {
290 ReportConditionalErrorAndExit(!settings.aec_dump_input_filename,
291 "Error: Either the aec dump or the wav "
292 "input files must be specified!\n");
293 }
294
295 ReportConditionalErrorAndExit(
296 settings.use_aec && *settings.use_aec && settings.use_aecm &&
297 *settings.use_aecm,
298 "Error: The AEC and the AECM cannot be activated at the same time!\n");
299
300 ReportConditionalErrorAndExit(
301 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
302 "Error: --output_sample_rate_hz must be positive!\n");
303
304 ReportConditionalErrorAndExit(
305 settings.reverse_output_sample_rate_hz &&
306 settings.output_sample_rate_hz &&
307 *settings.output_sample_rate_hz <= 0,
308 "Error: --reverse_output_sample_rate_hz must be positive!\n");
309
310 ReportConditionalErrorAndExit(
311 settings.output_num_channels && *settings.output_num_channels <= 0,
312 "Error: --output_num_channels must be positive!\n");
313
314 ReportConditionalErrorAndExit(
315 settings.reverse_output_num_channels &&
316 *settings.reverse_output_num_channels <= 0,
317 "Error: --reverse_output_num_channels must be positive!\n");
318
319 ReportConditionalErrorAndExit(
320 settings.use_bf && *settings.use_bf && !settings.microphone_positions,
321 "Error: --mic_positions must be specified when the beamformer is "
322 "activated.\n");
323
324 ReportConditionalErrorAndExit(
325 settings.target_angle_degrees < 0 || settings.target_angle_degrees > 359,
326 "Error: -target_angle_degrees must be specified between 0 and 359.\n");
327
328 ReportConditionalErrorAndExit(
329 settings.aec_suppression_level &&
330 ((*settings.aec_suppression_level) < 0 ||
331 (*settings.aec_suppression_level) > 2),
332 "Error: --aec_suppression_level must be specified between 0 and 2.\n");
333
334 ReportConditionalErrorAndExit(
335 settings.aecm_routing_mode && ((*settings.aecm_routing_mode) < 0 ||
336 (*settings.aecm_routing_mode) > 4),
337 "Error: --aecm_routing_mode must be specified between 0 and 4.\n");
338
339 ReportConditionalErrorAndExit(
340 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
341 (*settings.agc_target_level) > 31),
342 "Error: --agc_target_level must be specified between 0 and 31.\n");
343
344 ReportConditionalErrorAndExit(
345 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
346 (*settings.agc_compression_gain) > 90),
347 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
348
349 ReportConditionalErrorAndExit(
350 settings.vad_likelihood &&
351 ((*settings.vad_likelihood) < 0 || (*settings.vad_likelihood) > 3),
352 "Error: --vad_likelihood must be specified between 0 and 3.\n");
353
354 ReportConditionalErrorAndExit(
355 settings.ns_level &&
356 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
357 "Error: --ns_level must be specified between 0 and 3.\n");
358
359 ReportConditionalErrorAndExit(
360 settings.report_bitexactness && !settings.aec_dump_input_filename,
361 "Error: --bitexactness_report can only be used when operating on an "
362 "aecdump\n");
363
364 auto valid_wav_name = [](const std::string& wav_file_name) {
365 if (wav_file_name.size() < 5) {
366 return false;
367 }
368 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
369 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
370 return true;
371 }
372 return false;
373 };
374
375 ReportConditionalErrorAndExit(
376 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
377 "Error: --i must be a valid .wav file name.\n");
378
379 ReportConditionalErrorAndExit(
380 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
381 "Error: --o must be a valid .wav file name.\n");
382
383 ReportConditionalErrorAndExit(
384 settings.reverse_input_filename &&
385 (!valid_wav_name(*settings.reverse_input_filename)),
386 "Error: --ri must be a valid .wav file name.\n");
387
388 ReportConditionalErrorAndExit(
389 settings.reverse_output_filename &&
390 (!valid_wav_name(*settings.reverse_output_filename)),
391 "Error: --ro must be a valid .wav file name.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800392}
393
394} // namespace
395
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000396int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700397 google::SetUsageMessage(kUsageDescription);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000398 google::ParseCommandLineFlags(&argc, &argv, true);
399
peah60a189f2016-05-24 20:54:40 -0700400 SimulationSettings settings = CreateSettings();
401 PerformBasicParameterSanityChecks(settings);
402 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000403
peah60a189f2016-05-24 20:54:40 -0700404 if (settings.aec_dump_input_filename) {
405 processor.reset(new AecDumpBasedSimulator(settings));
aluebsb0ad43b2015-11-20 00:11:53 -0800406 } else {
peah60a189f2016-05-24 20:54:40 -0700407 processor.reset(new WavBasedSimulator(settings));
andrewbdafe312015-10-29 23:42:54 -0700408 }
409
peah60a189f2016-05-24 20:54:40 -0700410 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800411
peah60a189f2016-05-24 20:54:40 -0700412 if (settings.report_performance) {
aluebsb0ad43b2015-11-20 00:11:53 -0800413 const auto& proc_time = processor->proc_time();
Niels Möllerd28db7f2016-05-10 16:31:47 +0200414 int64_t exec_time_us = proc_time.sum / rtc::kNumNanosecsPerMicrosec;
peah60a189f2016-05-24 20:54:40 -0700415 std::cout << std::endl
416 << "Execution time: " << exec_time_us * 1e-6 << " s, File time: "
417 << processor->get_num_process_stream_calls() * 1.f /
418 AudioProcessingSimulator::kChunksPerSecond
419 << std::endl
420 << "Time per fwd stream chunk (mean, max, min): " << std::endl
421 << exec_time_us * 1.f / processor->get_num_process_stream_calls()
422 << " us, " << 1.f * proc_time.max / rtc::kNumNanosecsPerMicrosec
423 << " us, " << 1.f * proc_time.min / rtc::kNumNanosecsPerMicrosec
424 << " us" << std::endl;
425 }
426
427 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
428 if (processor->OutputWasBitexact()) {
429 std::cout << "The processing was bitexact.";
430 } else {
431 std::cout << "The processing was not bitexact.";
432 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700433 }
aluebsb0ad43b2015-11-20 00:11:53 -0800434
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000435 return 0;
436}
437
peah60a189f2016-05-24 20:54:40 -0700438} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000439} // namespace webrtc
440
441int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700442 return webrtc::test::main(argc, argv);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000443}