blob: 41d6dd1e18d25f89e3e408d34f337eed771028b1 [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");
peahca4cac72016-06-29 15:26:12 -0700116DEFINE_int32(lc,
117 kParameterNotSpecifiedValue,
118 "Activate (1) or deactivate(0) the level control");
peah60a189f2016-05-24 20:54:40 -0700119DEFINE_int32(
120 refined_adaptive_filter,
121 kParameterNotSpecifiedValue,
122 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
123DEFINE_int32(aecm_routing_mode,
124 kParameterNotSpecifiedValue,
125 "Specify the AECM routing mode (0-4)");
126DEFINE_int32(aecm_comfort_noise,
127 kParameterNotSpecifiedValue,
128 "Activate (1) or deactivate(0) the AECM comfort noise");
129DEFINE_int32(agc_mode,
130 kParameterNotSpecifiedValue,
131 "Specify the AGC mode (0-2)");
132DEFINE_int32(agc_target_level,
133 kParameterNotSpecifiedValue,
134 "Specify the AGC target level (0-31)");
135DEFINE_int32(agc_limiter,
136 kParameterNotSpecifiedValue,
137 "Activate (1) or deactivate(0) the level estimator");
138DEFINE_int32(agc_compression_gain,
139 kParameterNotSpecifiedValue,
140 "Specify the AGC compression gain (0-90)");
141DEFINE_int32(vad_likelihood,
142 kParameterNotSpecifiedValue,
143 "Specify the VAD likelihood (0-3)");
144DEFINE_int32(ns_level,
145 kParameterNotSpecifiedValue,
146 "Specify the NS level (0-3)");
147DEFINE_int32(stream_delay,
148 kParameterNotSpecifiedValue,
149 "Specify the stream delay in ms to use");
150DEFINE_int32(stream_drift_samples,
151 kParameterNotSpecifiedValue,
152 "Specify the number of stream drift samples to use");
153DEFINE_bool(performance_report, false, "Report the APM performance ");
154DEFINE_bool(verbose, false, "Produce verbose output");
155DEFINE_bool(bitexactness_report,
156 false,
157 "Report bitexactness for aec dump result reproduction");
158DEFINE_bool(discard_settings_in_aecdump,
159 false,
160 "Discard any config settings specified in the aec dump");
161DEFINE_bool(store_intermediate_output,
162 false,
163 "Creates new output files after each init");
164
165void SetSettingIfSpecified(const std::string value,
166 rtc::Optional<std::string>* parameter) {
167 if (value.compare("") != 0) {
168 *parameter = rtc::Optional<std::string>(value);
169 }
170}
171
172void SetSettingIfSpecified(int value, rtc::Optional<int>* parameter) {
173 if (value != kParameterNotSpecifiedValue) {
174 *parameter = rtc::Optional<int>(value);
175 }
176}
177
178void SetSettingIfFlagSet(int32_t flag, rtc::Optional<bool>* parameter) {
179 if (flag == 0) {
180 *parameter = rtc::Optional<bool>(false);
181 } else if (flag == 1) {
182 *parameter = rtc::Optional<bool>(true);
183 }
184}
185
186SimulationSettings CreateSettings() {
187 SimulationSettings settings;
188 if (FLAGS_all_default) {
189 settings.use_le = rtc::Optional<bool>(true);
190 settings.use_vad = rtc::Optional<bool>(true);
191 settings.use_ie = rtc::Optional<bool>(false);
192 settings.use_bf = rtc::Optional<bool>(false);
193 settings.use_ts = rtc::Optional<bool>(true);
194 settings.use_ns = rtc::Optional<bool>(true);
195 settings.use_hpf = rtc::Optional<bool>(true);
196 settings.use_agc = rtc::Optional<bool>(true);
197 settings.use_aec = rtc::Optional<bool>(true);
198 settings.use_aecm = rtc::Optional<bool>(false);
199 }
200 SetSettingIfSpecified(FLAGS_dump_input, &settings.aec_dump_input_filename);
201 SetSettingIfSpecified(FLAGS_dump_output, &settings.aec_dump_output_filename);
202 SetSettingIfSpecified(FLAGS_i, &settings.input_filename);
203 SetSettingIfSpecified(FLAGS_o, &settings.output_filename);
204 SetSettingIfSpecified(FLAGS_ri, &settings.reverse_input_filename);
205 SetSettingIfSpecified(FLAGS_ro, &settings.reverse_output_filename);
206 SetSettingIfSpecified(FLAGS_output_num_channels,
207 &settings.output_num_channels);
208 SetSettingIfSpecified(FLAGS_reverse_output_num_channels,
209 &settings.reverse_output_num_channels);
210 SetSettingIfSpecified(FLAGS_output_sample_rate_hz,
211 &settings.output_sample_rate_hz);
212 SetSettingIfSpecified(FLAGS_reverse_output_sample_rate_hz,
213 &settings.reverse_output_sample_rate_hz);
214 SetSettingIfSpecified(FLAGS_mic_positions, &settings.microphone_positions);
215 settings.target_angle_degrees = FLAGS_target_angle_degrees;
216 SetSettingIfFlagSet(FLAGS_aec, &settings.use_aec);
217 SetSettingIfFlagSet(FLAGS_aecm, &settings.use_aecm);
218 SetSettingIfFlagSet(FLAGS_agc, &settings.use_agc);
219 SetSettingIfFlagSet(FLAGS_hpf, &settings.use_hpf);
220 SetSettingIfFlagSet(FLAGS_ns, &settings.use_ns);
221 SetSettingIfFlagSet(FLAGS_ts, &settings.use_ts);
222 SetSettingIfFlagSet(FLAGS_bf, &settings.use_bf);
223 SetSettingIfFlagSet(FLAGS_ie, &settings.use_ie);
224 SetSettingIfFlagSet(FLAGS_vad, &settings.use_vad);
225 SetSettingIfFlagSet(FLAGS_le, &settings.use_le);
226 SetSettingIfSpecified(FLAGS_aec_suppression_level,
227 &settings.aec_suppression_level);
228 SetSettingIfFlagSet(FLAGS_delay_agnostic, &settings.use_delay_agnostic);
229 SetSettingIfFlagSet(FLAGS_extended_filter, &settings.use_extended_filter);
230 SetSettingIfFlagSet(FLAGS_drift_compensation,
231 &settings.use_drift_compensation);
232 SetSettingIfFlagSet(FLAGS_refined_adaptive_filter,
233 &settings.use_refined_adaptive_filter);
234
235 SetSettingIfFlagSet(FLAGS_aec3, &settings.use_aec3);
peahca4cac72016-06-29 15:26:12 -0700236 SetSettingIfFlagSet(FLAGS_lc, &settings.use_lc);
peah60a189f2016-05-24 20:54:40 -0700237 SetSettingIfSpecified(FLAGS_aecm_routing_mode, &settings.aecm_routing_mode);
238 SetSettingIfFlagSet(FLAGS_aecm_comfort_noise,
239 &settings.use_aecm_comfort_noise);
240 SetSettingIfSpecified(FLAGS_agc_mode, &settings.agc_mode);
241 SetSettingIfSpecified(FLAGS_agc_target_level, &settings.agc_target_level);
242 SetSettingIfFlagSet(FLAGS_agc_limiter, &settings.use_agc_limiter);
243 SetSettingIfSpecified(FLAGS_agc_compression_gain,
244 &settings.agc_compression_gain);
245 SetSettingIfSpecified(FLAGS_vad_likelihood, &settings.vad_likelihood);
246 SetSettingIfSpecified(FLAGS_ns_level, &settings.ns_level);
247 SetSettingIfSpecified(FLAGS_stream_delay, &settings.stream_delay);
248 SetSettingIfSpecified(FLAGS_stream_drift_samples,
249 &settings.stream_drift_samples);
250 settings.report_performance = FLAGS_performance_report;
251 settings.use_verbose_logging = FLAGS_verbose;
252 settings.report_bitexactness = FLAGS_bitexactness_report;
253 settings.discard_all_settings_in_aecdump = FLAGS_discard_settings_in_aecdump;
254 settings.fixed_interface = FLAGS_fixed_interface;
255 settings.store_intermediate_output = FLAGS_store_intermediate_output;
256
257 return settings;
258}
259
260void ReportConditionalErrorAndExit(bool condition, std::string message) {
261 if (condition) {
262 std::cerr << message << std::endl;
263 exit(1);
264 }
265}
266
267void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
268 if (settings.input_filename || settings.reverse_input_filename) {
269 ReportConditionalErrorAndExit(!!settings.aec_dump_input_filename,
270 "Error: The aec dump cannot be specified "
271 "together with input wav files!\n");
272
273 ReportConditionalErrorAndExit(!settings.input_filename,
274 "Error: When operating at wav files, the "
275 "input wav filename must be "
276 "specified!\n");
277
278 ReportConditionalErrorAndExit(
279 settings.reverse_output_filename && !settings.reverse_input_filename,
280 "Error: When operating at wav files, the reverse input wav filename "
281 "must be specified if the reverse output wav filename is specified!\n");
282 } else {
283 ReportConditionalErrorAndExit(!settings.aec_dump_input_filename,
284 "Error: Either the aec dump or the wav "
285 "input files must be specified!\n");
286 }
287
288 ReportConditionalErrorAndExit(
289 settings.use_aec && *settings.use_aec && settings.use_aecm &&
290 *settings.use_aecm,
291 "Error: The AEC and the AECM cannot be activated at the same time!\n");
292
293 ReportConditionalErrorAndExit(
294 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
295 "Error: --output_sample_rate_hz must be positive!\n");
296
297 ReportConditionalErrorAndExit(
298 settings.reverse_output_sample_rate_hz &&
299 settings.output_sample_rate_hz &&
300 *settings.output_sample_rate_hz <= 0,
301 "Error: --reverse_output_sample_rate_hz must be positive!\n");
302
303 ReportConditionalErrorAndExit(
304 settings.output_num_channels && *settings.output_num_channels <= 0,
305 "Error: --output_num_channels must be positive!\n");
306
307 ReportConditionalErrorAndExit(
308 settings.reverse_output_num_channels &&
309 *settings.reverse_output_num_channels <= 0,
310 "Error: --reverse_output_num_channels must be positive!\n");
311
312 ReportConditionalErrorAndExit(
313 settings.use_bf && *settings.use_bf && !settings.microphone_positions,
314 "Error: --mic_positions must be specified when the beamformer is "
315 "activated.\n");
316
317 ReportConditionalErrorAndExit(
318 settings.target_angle_degrees < 0 || settings.target_angle_degrees > 359,
319 "Error: -target_angle_degrees must be specified between 0 and 359.\n");
320
321 ReportConditionalErrorAndExit(
322 settings.aec_suppression_level &&
323 ((*settings.aec_suppression_level) < 0 ||
324 (*settings.aec_suppression_level) > 2),
325 "Error: --aec_suppression_level must be specified between 0 and 2.\n");
326
327 ReportConditionalErrorAndExit(
328 settings.aecm_routing_mode && ((*settings.aecm_routing_mode) < 0 ||
329 (*settings.aecm_routing_mode) > 4),
330 "Error: --aecm_routing_mode must be specified between 0 and 4.\n");
331
332 ReportConditionalErrorAndExit(
333 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
334 (*settings.agc_target_level) > 31),
335 "Error: --agc_target_level must be specified between 0 and 31.\n");
336
337 ReportConditionalErrorAndExit(
338 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
339 (*settings.agc_compression_gain) > 90),
340 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
341
342 ReportConditionalErrorAndExit(
343 settings.vad_likelihood &&
344 ((*settings.vad_likelihood) < 0 || (*settings.vad_likelihood) > 3),
345 "Error: --vad_likelihood must be specified between 0 and 3.\n");
346
347 ReportConditionalErrorAndExit(
348 settings.ns_level &&
349 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
350 "Error: --ns_level must be specified between 0 and 3.\n");
351
352 ReportConditionalErrorAndExit(
353 settings.report_bitexactness && !settings.aec_dump_input_filename,
354 "Error: --bitexactness_report can only be used when operating on an "
355 "aecdump\n");
356
357 auto valid_wav_name = [](const std::string& wav_file_name) {
358 if (wav_file_name.size() < 5) {
359 return false;
360 }
361 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
362 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
363 return true;
364 }
365 return false;
366 };
367
368 ReportConditionalErrorAndExit(
369 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
370 "Error: --i must be a valid .wav file name.\n");
371
372 ReportConditionalErrorAndExit(
373 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
374 "Error: --o must be a valid .wav file name.\n");
375
376 ReportConditionalErrorAndExit(
377 settings.reverse_input_filename &&
378 (!valid_wav_name(*settings.reverse_input_filename)),
379 "Error: --ri must be a valid .wav file name.\n");
380
381 ReportConditionalErrorAndExit(
382 settings.reverse_output_filename &&
383 (!valid_wav_name(*settings.reverse_output_filename)),
384 "Error: --ro must be a valid .wav file name.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800385}
386
387} // namespace
388
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000389int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700390 google::SetUsageMessage(kUsageDescription);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000391 google::ParseCommandLineFlags(&argc, &argv, true);
392
peah60a189f2016-05-24 20:54:40 -0700393 SimulationSettings settings = CreateSettings();
394 PerformBasicParameterSanityChecks(settings);
395 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000396
peah60a189f2016-05-24 20:54:40 -0700397 if (settings.aec_dump_input_filename) {
398 processor.reset(new AecDumpBasedSimulator(settings));
aluebsb0ad43b2015-11-20 00:11:53 -0800399 } else {
peah60a189f2016-05-24 20:54:40 -0700400 processor.reset(new WavBasedSimulator(settings));
andrewbdafe312015-10-29 23:42:54 -0700401 }
402
peah60a189f2016-05-24 20:54:40 -0700403 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800404
peah60a189f2016-05-24 20:54:40 -0700405 if (settings.report_performance) {
aluebsb0ad43b2015-11-20 00:11:53 -0800406 const auto& proc_time = processor->proc_time();
Niels Möllerd28db7f2016-05-10 16:31:47 +0200407 int64_t exec_time_us = proc_time.sum / rtc::kNumNanosecsPerMicrosec;
peah60a189f2016-05-24 20:54:40 -0700408 std::cout << std::endl
409 << "Execution time: " << exec_time_us * 1e-6 << " s, File time: "
410 << processor->get_num_process_stream_calls() * 1.f /
411 AudioProcessingSimulator::kChunksPerSecond
412 << std::endl
413 << "Time per fwd stream chunk (mean, max, min): " << std::endl
414 << exec_time_us * 1.f / processor->get_num_process_stream_calls()
415 << " us, " << 1.f * proc_time.max / rtc::kNumNanosecsPerMicrosec
416 << " us, " << 1.f * proc_time.min / rtc::kNumNanosecsPerMicrosec
417 << " us" << std::endl;
418 }
419
420 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
421 if (processor->OutputWasBitexact()) {
422 std::cout << "The processing was bitexact.";
423 } else {
424 std::cout << "The processing was not bitexact.";
425 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700426 }
aluebsb0ad43b2015-11-20 00:11:53 -0800427
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000428 return 0;
429}
430
peah60a189f2016-05-24 20:54:40 -0700431} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000432} // namespace webrtc
433
434int main(int argc, char* argv[]) {
peah60a189f2016-05-24 20:54:40 -0700435 return webrtc::test::main(argc, argv);
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000436}