blob: d24b881968b90264d417337c61b4ee364f91a60e [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
Per Åhgren6ee75fd2019-04-26 11:33:37 +020011#include "modules/audio_processing/test/audioproc_float_impl.h"
12
Ivo Creusen2cb41052018-03-15 12:22:52 +010013#include <string.h>
14
aluebsb0ad43b2015-11-20 00:11:53 -080015#include <iostream>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
Ivo Creusen2cb41052018-03-15 12:22:52 +010017#include <string>
18#include <utility>
Alessio Bazzica68170382018-11-20 12:27:20 +010019#include <vector>
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000020
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020021#include "absl/flags/flag.h"
22#include "absl/flags/parse.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010023#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/include/audio_processing.h"
25#include "modules/audio_processing/test/aec_dump_based_simulator.h"
26#include "modules/audio_processing/test/audio_processing_simulator.h"
27#include "modules/audio_processing/test/wav_based_simulator.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010028#include "rtc_base/checks.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010029#include "rtc_base/strings/string_builder.h"
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000030
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020031constexpr int kParameterNotSpecifiedValue = -10000;
32
33ABSL_FLAG(std::string, dump_input, "", "Aec dump input filename");
34ABSL_FLAG(std::string, dump_output, "", "Aec dump output filename");
35ABSL_FLAG(std::string, i, "", "Forward stream input wav filename");
36ABSL_FLAG(std::string, o, "", "Forward stream output wav filename");
37ABSL_FLAG(std::string, ri, "", "Reverse stream input wav filename");
38ABSL_FLAG(std::string, ro, "", "Reverse stream output wav filename");
39ABSL_FLAG(std::string,
40 artificial_nearend,
41 "",
42 "Artificial nearend wav filename");
43ABSL_FLAG(int,
44 output_num_channels,
45 kParameterNotSpecifiedValue,
46 "Number of forward stream output channels");
47ABSL_FLAG(int,
48 reverse_output_num_channels,
49 kParameterNotSpecifiedValue,
50 "Number of Reverse stream output channels");
51ABSL_FLAG(int,
52 output_sample_rate_hz,
53 kParameterNotSpecifiedValue,
54 "Forward stream output sample rate in Hz");
55ABSL_FLAG(int,
56 reverse_output_sample_rate_hz,
57 kParameterNotSpecifiedValue,
58 "Reverse stream output sample rate in Hz");
59ABSL_FLAG(bool,
60 fixed_interface,
61 false,
62 "Use the fixed interface when operating on wav files");
63ABSL_FLAG(int,
64 aec,
65 kParameterNotSpecifiedValue,
66 "Activate (1) or deactivate(0) the echo canceller");
67ABSL_FLAG(int,
68 aecm,
69 kParameterNotSpecifiedValue,
70 "Activate (1) or deactivate(0) the mobile echo controller");
71ABSL_FLAG(int,
72 ed,
73 kParameterNotSpecifiedValue,
74 "Activate (1) or deactivate (0) the residual echo detector");
75ABSL_FLAG(std::string,
76 ed_graph,
77 "",
78 "Output filename for graph of echo likelihood");
79ABSL_FLAG(int,
80 agc,
81 kParameterNotSpecifiedValue,
82 "Activate (1) or deactivate(0) the AGC");
83ABSL_FLAG(int,
84 agc2,
85 kParameterNotSpecifiedValue,
86 "Activate (1) or deactivate(0) the AGC2");
87ABSL_FLAG(int,
88 pre_amplifier,
89 kParameterNotSpecifiedValue,
90 "Activate (1) or deactivate(0) the pre amplifier");
91ABSL_FLAG(int,
92 hpf,
93 kParameterNotSpecifiedValue,
94 "Activate (1) or deactivate(0) the high-pass filter");
95ABSL_FLAG(int,
96 ns,
97 kParameterNotSpecifiedValue,
98 "Activate (1) or deactivate(0) the noise suppressor");
99ABSL_FLAG(int,
100 ts,
101 kParameterNotSpecifiedValue,
102 "Activate (1) or deactivate(0) the transient suppressor");
103ABSL_FLAG(int,
104 vad,
105 kParameterNotSpecifiedValue,
106 "Activate (1) or deactivate(0) the voice activity detector");
107ABSL_FLAG(int,
108 le,
109 kParameterNotSpecifiedValue,
110 "Activate (1) or deactivate(0) the level estimator");
111ABSL_FLAG(bool,
112 all_default,
113 false,
114 "Activate all of the default components (will be overridden by any "
115 "other settings)");
116ABSL_FLAG(int,
117 aec_suppression_level,
118 kParameterNotSpecifiedValue,
119 "Set the aec suppression level (0-2)");
120ABSL_FLAG(int,
121 delay_agnostic,
122 kParameterNotSpecifiedValue,
123 "Activate (1) or deactivate(0) the AEC delay agnostic mode");
124ABSL_FLAG(int,
125 extended_filter,
126 kParameterNotSpecifiedValue,
127 "Activate (1) or deactivate(0) the AEC extended filter mode");
128ABSL_FLAG(int,
129 use_legacy_aec,
130 kParameterNotSpecifiedValue,
131 "Activate (1) or deactivate(0) the legacy AEC");
132ABSL_FLAG(int,
133 experimental_agc,
134 kParameterNotSpecifiedValue,
135 "Activate (1) or deactivate(0) the experimental AGC");
136ABSL_FLAG(int,
137 experimental_agc_disable_digital_adaptive,
138 kParameterNotSpecifiedValue,
139 "Force-deactivate (1) digital adaptation in "
140 "experimental AGC. Digital adaptation is active by default (0).");
141ABSL_FLAG(int,
142 experimental_agc_analyze_before_aec,
143 kParameterNotSpecifiedValue,
144 "Make level estimation happen before AEC"
145 " in the experimental AGC. After AEC is the default (0)");
146ABSL_FLAG(int,
147 experimental_agc_agc2_level_estimator,
148 kParameterNotSpecifiedValue,
149 "AGC2 level estimation"
150 " in the experimental AGC. AGC1 level estimation is the default (0)");
151ABSL_FLAG(
152 int,
153 refined_adaptive_filter,
154 kParameterNotSpecifiedValue,
155 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
156ABSL_FLAG(int,
157 agc_mode,
158 kParameterNotSpecifiedValue,
159 "Specify the AGC mode (0-2)");
160ABSL_FLAG(int,
161 agc_target_level,
162 kParameterNotSpecifiedValue,
163 "Specify the AGC target level (0-31)");
164ABSL_FLAG(int,
165 agc_limiter,
166 kParameterNotSpecifiedValue,
167 "Activate (1) or deactivate(0) the level estimator");
168ABSL_FLAG(int,
169 agc_compression_gain,
170 kParameterNotSpecifiedValue,
171 "Specify the AGC compression gain (0-90)");
172ABSL_FLAG(int,
173 agc2_enable_adaptive_gain,
174 kParameterNotSpecifiedValue,
175 "Activate (1) or deactivate(0) the AGC2 adaptive gain");
176ABSL_FLAG(float,
177 agc2_fixed_gain_db,
178 kParameterNotSpecifiedValue,
179 "AGC2 fixed gain (dB) to apply");
180ABSL_FLAG(std::string,
181 agc2_adaptive_level_estimator,
182 "RMS",
183 "AGC2 adaptive digital level estimator to use [RMS, peak]");
184ABSL_FLAG(float,
185 pre_amplifier_gain_factor,
186 kParameterNotSpecifiedValue,
187 "Pre-amplifier gain factor (linear) to apply");
188ABSL_FLAG(int,
189 vad_likelihood,
190 kParameterNotSpecifiedValue,
191 "Specify the VAD likelihood (0-3)");
192ABSL_FLAG(int,
193 ns_level,
194 kParameterNotSpecifiedValue,
195 "Specify the NS level (0-3)");
196ABSL_FLAG(int,
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200197 maximum_internal_processing_rate,
198 kParameterNotSpecifiedValue,
199 "Set a maximum internal processing rate (32000 or 48000) to override "
200 "the default rate");
201ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200202 stream_delay,
203 kParameterNotSpecifiedValue,
204 "Specify the stream delay in ms to use");
205ABSL_FLAG(int,
206 use_stream_delay,
207 kParameterNotSpecifiedValue,
208 "Activate (1) or deactivate(0) reporting the stream delay");
209ABSL_FLAG(int,
210 stream_drift_samples,
211 kParameterNotSpecifiedValue,
212 "Specify the number of stream drift samples to use");
213ABSL_FLAG(int, initial_mic_level, 100, "Initial mic level (0-255)");
214ABSL_FLAG(int,
215 simulate_mic_gain,
216 0,
217 "Activate (1) or deactivate(0) the analog mic gain simulation");
218ABSL_FLAG(int,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200219 experimental_multi_channel,
220 kParameterNotSpecifiedValue,
221 "Activate (1) or deactivate(0) multi-channel audio in APM pipeline");
222ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200223 simulated_mic_kind,
224 kParameterNotSpecifiedValue,
225 "Specify which microphone kind to use for microphone simulation");
226ABSL_FLAG(bool, performance_report, false, "Report the APM performance ");
227ABSL_FLAG(std::string,
228 performance_report_output_file,
229 "",
230 "Generate a CSV file with the API call durations");
231ABSL_FLAG(bool, verbose, false, "Produce verbose output");
232ABSL_FLAG(bool,
233 quiet,
234 false,
235 "Avoid producing information about the progress.");
236ABSL_FLAG(bool,
237 bitexactness_report,
238 false,
239 "Report bitexactness for aec dump result reproduction");
240ABSL_FLAG(bool,
241 discard_settings_in_aecdump,
242 false,
243 "Discard any config settings specified in the aec dump");
244ABSL_FLAG(bool,
245 store_intermediate_output,
246 false,
247 "Creates new output files after each init");
248ABSL_FLAG(std::string,
249 custom_call_order_file,
250 "",
251 "Custom process API call order file");
252ABSL_FLAG(std::string,
253 output_custom_call_order_file,
254 "",
255 "Generate custom process API call order file from AEC dump");
256ABSL_FLAG(bool,
257 print_aec_parameter_values,
258 false,
259 "Print parameter values used in AEC in JSON-format");
260ABSL_FLAG(std::string,
261 aec_settings,
262 "",
263 "File in JSON-format with custom AEC settings");
264ABSL_FLAG(bool,
265 dump_data,
266 false,
267 "Dump internal data during the call (requires build flag)");
268ABSL_FLAG(std::string,
269 dump_data_output_dir,
270 "",
271 "Internal data dump output directory");
272
peah60a189f2016-05-24 20:54:40 -0700273namespace webrtc {
274namespace test {
Peter Kasting69558702016-01-12 16:26:35 -0800275namespace {
276
peah60a189f2016-05-24 20:54:40 -0700277const char kUsageDescription[] =
278 "Usage: audioproc_f [options] -i <input.wav>\n"
279 " or\n"
280 " audioproc_f [options] -dump_input <aec_dump>\n"
281 "\n\n"
282 "Command-line tool to simulate a call using the audio "
283 "processing module, either based on wav files or "
oprypin6e09d872017-08-31 03:21:39 -0700284 "protobuf debug dump recordings.\n";
peah60a189f2016-05-24 20:54:40 -0700285
Alessio Bazzica68170382018-11-20 12:27:20 +0100286std::vector<std::string> GetAgc2AdaptiveLevelEstimatorNames() {
287 return {"RMS", "peak"};
288}
peah60a189f2016-05-24 20:54:40 -0700289
Alex Loiko890988c2017-08-31 10:25:48 +0200290void SetSettingIfSpecified(const std::string& value,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200291 absl::optional<std::string>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700292 if (value.compare("") != 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100293 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700294 }
295}
296
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200297void SetSettingIfSpecified(int value, absl::optional<int>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700298 if (value != kParameterNotSpecifiedValue) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100299 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700300 }
301}
302
Per Åhgrenef349602019-04-12 16:26:34 +0200303void SetSettingIfSpecified(float value, absl::optional<float>* parameter) {
304 constexpr float kFloatParameterNotSpecifiedValue =
305 kParameterNotSpecifiedValue;
306 if (value != kFloatParameterNotSpecifiedValue) {
307 *parameter = value;
308 }
309}
310
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200311void SetSettingIfFlagSet(int32_t flag, absl::optional<bool>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700312 if (flag == 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100313 *parameter = false;
peah60a189f2016-05-24 20:54:40 -0700314 } else if (flag == 1) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100315 *parameter = true;
peah60a189f2016-05-24 20:54:40 -0700316 }
317}
318
Alessio Bazzica68170382018-11-20 12:27:20 +0100319AudioProcessing::Config::GainController2::LevelEstimator
320MapAgc2AdaptiveLevelEstimator(absl::string_view name) {
321 if (name.compare("RMS") == 0) {
322 return AudioProcessing::Config::GainController2::LevelEstimator::kRms;
323 }
324 if (name.compare("peak") == 0) {
325 return AudioProcessing::Config::GainController2::LevelEstimator::kPeak;
326 }
327 auto concat_strings =
328 [](const std::vector<std::string>& strings) -> std::string {
329 rtc::StringBuilder ss;
330 for (const auto& s : strings) {
331 ss << " " << s;
332 }
333 return ss.Release();
334 };
335 RTC_CHECK(false)
336 << "Invalid value for agc2_adaptive_level_estimator, valid options:"
337 << concat_strings(GetAgc2AdaptiveLevelEstimatorNames()) << ".";
338}
339
peah60a189f2016-05-24 20:54:40 -0700340SimulationSettings CreateSettings() {
341 SimulationSettings settings;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200342 if (absl::GetFlag(FLAGS_all_default)) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100343 settings.use_le = true;
344 settings.use_vad = true;
345 settings.use_ie = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100346 settings.use_ts = true;
347 settings.use_ns = true;
348 settings.use_hpf = true;
349 settings.use_agc = true;
350 settings.use_agc2 = false;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200351 settings.use_pre_amplifier = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100352 settings.use_aec = true;
353 settings.use_aecm = false;
354 settings.use_ed = false;
peah60a189f2016-05-24 20:54:40 -0700355 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200356 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_input),
357 &settings.aec_dump_input_filename);
358 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_output),
359 &settings.aec_dump_output_filename);
360 SetSettingIfSpecified(absl::GetFlag(FLAGS_i), &settings.input_filename);
361 SetSettingIfSpecified(absl::GetFlag(FLAGS_o), &settings.output_filename);
362 SetSettingIfSpecified(absl::GetFlag(FLAGS_ri),
363 &settings.reverse_input_filename);
364 SetSettingIfSpecified(absl::GetFlag(FLAGS_ro),
365 &settings.reverse_output_filename);
366 SetSettingIfSpecified(absl::GetFlag(FLAGS_artificial_nearend),
peahdf80fd12016-12-09 02:43:40 -0800367 &settings.artificial_nearend_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200368 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700369 &settings.output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200370 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700371 &settings.reverse_output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200372 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700373 &settings.output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200374 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700375 &settings.reverse_output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200376 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aec), &settings.use_aec);
377 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aecm), &settings.use_aecm);
378 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ed), &settings.use_ed);
379 SetSettingIfSpecified(absl::GetFlag(FLAGS_ed_graph),
380 &settings.ed_graph_output_filename);
381 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc), &settings.use_agc);
382 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2), &settings.use_agc2);
383 SetSettingIfFlagSet(absl::GetFlag(FLAGS_pre_amplifier),
384 &settings.use_pre_amplifier);
385 SetSettingIfFlagSet(absl::GetFlag(FLAGS_hpf), &settings.use_hpf);
386 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns), &settings.use_ns);
387 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ts), &settings.use_ts);
388 SetSettingIfFlagSet(absl::GetFlag(FLAGS_vad), &settings.use_vad);
389 SetSettingIfFlagSet(absl::GetFlag(FLAGS_le), &settings.use_le);
390 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_suppression_level),
peah60a189f2016-05-24 20:54:40 -0700391 &settings.aec_suppression_level);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200392 SetSettingIfFlagSet(absl::GetFlag(FLAGS_delay_agnostic),
393 &settings.use_delay_agnostic);
394 SetSettingIfFlagSet(absl::GetFlag(FLAGS_extended_filter),
395 &settings.use_extended_filter);
396 SetSettingIfFlagSet(absl::GetFlag(FLAGS_refined_adaptive_filter),
peah60a189f2016-05-24 20:54:40 -0700397 &settings.use_refined_adaptive_filter);
398
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200399 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_legacy_aec),
400 &settings.use_legacy_aec);
401 SetSettingIfFlagSet(absl::GetFlag(FLAGS_experimental_agc),
402 &settings.use_experimental_agc);
403 SetSettingIfFlagSet(
404 absl::GetFlag(FLAGS_experimental_agc_disable_digital_adaptive),
405 &settings.experimental_agc_disable_digital_adaptive);
406 SetSettingIfFlagSet(absl::GetFlag(FLAGS_experimental_agc_analyze_before_aec),
Alex Loikod9342442018-09-10 13:59:41 +0200407 &settings.experimental_agc_analyze_before_aec);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200408 SetSettingIfFlagSet(
409 absl::GetFlag(FLAGS_experimental_agc_agc2_level_estimator),
410 &settings.use_experimental_agc_agc2_level_estimator);
411 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_mode), &settings.agc_mode);
412 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_target_level),
413 &settings.agc_target_level);
414 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc_limiter),
415 &settings.use_agc_limiter);
416 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_compression_gain),
peah60a189f2016-05-24 20:54:40 -0700417 &settings.agc_compression_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200418 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2_enable_adaptive_gain),
Per Åhgrene4d23b12018-10-02 23:40:07 +0200419 &settings.agc2_use_adaptive_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200420 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc2_fixed_gain_db),
421 &settings.agc2_fixed_gain_db);
422 settings.agc2_adaptive_level_estimator = MapAgc2AdaptiveLevelEstimator(
423 absl::GetFlag(FLAGS_agc2_adaptive_level_estimator));
424 SetSettingIfSpecified(absl::GetFlag(FLAGS_pre_amplifier_gain_factor),
Per Åhgrenef349602019-04-12 16:26:34 +0200425 &settings.pre_amplifier_gain_factor);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200426 SetSettingIfSpecified(absl::GetFlag(FLAGS_vad_likelihood),
427 &settings.vad_likelihood);
428 SetSettingIfSpecified(absl::GetFlag(FLAGS_ns_level), &settings.ns_level);
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200429 SetSettingIfSpecified(absl::GetFlag(FLAGS_maximum_internal_processing_rate),
430 &settings.maximum_internal_processing_rate);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200431 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_delay),
432 &settings.stream_delay);
433 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_stream_delay),
434 &settings.use_stream_delay);
435 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_drift_samples),
peah60a189f2016-05-24 20:54:40 -0700436 &settings.stream_drift_samples);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200437 SetSettingIfSpecified(absl::GetFlag(FLAGS_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100438 &settings.call_order_input_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200439 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100440 &settings.call_order_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200441 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_settings),
442 &settings.aec_settings_filename);
443 settings.initial_mic_level = absl::GetFlag(FLAGS_initial_mic_level);
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200444 SetSettingIfFlagSet(absl::GetFlag(FLAGS_experimental_multi_channel),
445 &settings.experimental_multi_channel);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200446 settings.simulate_mic_gain = absl::GetFlag(FLAGS_simulate_mic_gain);
447 SetSettingIfSpecified(absl::GetFlag(FLAGS_simulated_mic_kind),
448 &settings.simulated_mic_kind);
449 settings.report_performance = absl::GetFlag(FLAGS_performance_report);
450 SetSettingIfSpecified(absl::GetFlag(FLAGS_performance_report_output_file),
Per Åhgrenada9b892019-04-03 16:06:42 +0200451 &settings.performance_report_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200452 settings.use_verbose_logging = absl::GetFlag(FLAGS_verbose);
453 settings.use_quiet_output = absl::GetFlag(FLAGS_quiet);
454 settings.report_bitexactness = absl::GetFlag(FLAGS_bitexactness_report);
455 settings.discard_all_settings_in_aecdump =
456 absl::GetFlag(FLAGS_discard_settings_in_aecdump);
457 settings.fixed_interface = absl::GetFlag(FLAGS_fixed_interface);
458 settings.store_intermediate_output =
459 absl::GetFlag(FLAGS_store_intermediate_output);
460 settings.print_aec_parameter_values =
461 absl::GetFlag(FLAGS_print_aec_parameter_values);
462 settings.dump_internal_data = absl::GetFlag(FLAGS_dump_data);
463 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_data_output_dir),
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100464 &settings.dump_internal_data_output_dir);
peah60a189f2016-05-24 20:54:40 -0700465
466 return settings;
467}
468
Alex Loiko890988c2017-08-31 10:25:48 +0200469void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
peah60a189f2016-05-24 20:54:40 -0700470 if (condition) {
471 std::cerr << message << std::endl;
472 exit(1);
473 }
474}
475
476void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
477 if (settings.input_filename || settings.reverse_input_filename) {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200478 ReportConditionalErrorAndExit(
479 !!settings.aec_dump_input_filename,
480 "Error: The aec dump file cannot be specified "
481 "together with input wav files!\n");
482
483 ReportConditionalErrorAndExit(
484 !!settings.aec_dump_input_string,
485 "Error: The aec dump input string cannot be specified "
486 "together with input wav files!\n");
peah60a189f2016-05-24 20:54:40 -0700487
peahdf80fd12016-12-09 02:43:40 -0800488 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
489 "Error: The artificial nearend cannot be "
490 "specified together with input wav files!\n");
491
peah60a189f2016-05-24 20:54:40 -0700492 ReportConditionalErrorAndExit(!settings.input_filename,
493 "Error: When operating at wav files, the "
494 "input wav filename must be "
495 "specified!\n");
496
497 ReportConditionalErrorAndExit(
498 settings.reverse_output_filename && !settings.reverse_input_filename,
499 "Error: When operating at wav files, the reverse input wav filename "
500 "must be specified if the reverse output wav filename is specified!\n");
501 } else {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200502 ReportConditionalErrorAndExit(
503 !settings.aec_dump_input_filename && !settings.aec_dump_input_string,
504 "Error: Either the aec dump input file, the wav "
505 "input file or the aec dump input string must be specified!\n");
506 ReportConditionalErrorAndExit(
507 settings.aec_dump_input_filename && settings.aec_dump_input_string,
508 "Error: The aec dump input file cannot be specified together with the "
509 "aec dump input string!\n");
peah60a189f2016-05-24 20:54:40 -0700510 }
511
512 ReportConditionalErrorAndExit(
513 settings.use_aec && *settings.use_aec && settings.use_aecm &&
514 *settings.use_aecm,
515 "Error: The AEC and the AECM cannot be activated at the same time!\n");
516
517 ReportConditionalErrorAndExit(
518 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
519 "Error: --output_sample_rate_hz must be positive!\n");
520
521 ReportConditionalErrorAndExit(
522 settings.reverse_output_sample_rate_hz &&
523 settings.output_sample_rate_hz &&
524 *settings.output_sample_rate_hz <= 0,
525 "Error: --reverse_output_sample_rate_hz must be positive!\n");
526
527 ReportConditionalErrorAndExit(
528 settings.output_num_channels && *settings.output_num_channels <= 0,
529 "Error: --output_num_channels must be positive!\n");
530
531 ReportConditionalErrorAndExit(
532 settings.reverse_output_num_channels &&
533 *settings.reverse_output_num_channels <= 0,
534 "Error: --reverse_output_num_channels must be positive!\n");
535
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200536 ReportConditionalErrorAndExit(settings.aec_suppression_level &&
537 ((*settings.aec_suppression_level) < 1 ||
538 (*settings.aec_suppression_level) > 2),
539 "Error: --aec_suppression_level must be "
540 "specified between 1 and 2. 0 is "
541 "deprecated.\n");
peah60a189f2016-05-24 20:54:40 -0700542
543 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700544 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
545 (*settings.agc_target_level) > 31),
546 "Error: --agc_target_level must be specified between 0 and 31.\n");
547
548 ReportConditionalErrorAndExit(
549 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
550 (*settings.agc_compression_gain) > 90),
551 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
552
553 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200554 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
555 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200556 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
557
558 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700559 settings.vad_likelihood &&
560 ((*settings.vad_likelihood) < 0 || (*settings.vad_likelihood) > 3),
561 "Error: --vad_likelihood must be specified between 0 and 3.\n");
562
563 ReportConditionalErrorAndExit(
564 settings.ns_level &&
565 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
566 "Error: --ns_level must be specified between 0 and 3.\n");
567
568 ReportConditionalErrorAndExit(
569 settings.report_bitexactness && !settings.aec_dump_input_filename,
570 "Error: --bitexactness_report can only be used when operating on an "
571 "aecdump\n");
572
peah5ad5de32016-12-09 03:18:22 -0800573 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100574 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800575 "Error: --custom_call_order_file cannot be used when operating on an "
576 "aecdump\n");
577
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200578 ReportConditionalErrorAndExit(
579 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
580 "Error: --initial_mic_level must be specified between 0 and 255.\n");
581
582 ReportConditionalErrorAndExit(
583 settings.simulated_mic_kind && !settings.simulate_mic_gain,
584 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
585 "disabled\n");
586
587 ReportConditionalErrorAndExit(
588 !settings.simulated_mic_kind && settings.simulate_mic_gain,
589 "Error: --simulated_mic_kind must be specified when mic simulation is "
590 "enabled\n");
591
peah60a189f2016-05-24 20:54:40 -0700592 auto valid_wav_name = [](const std::string& wav_file_name) {
593 if (wav_file_name.size() < 5) {
594 return false;
595 }
596 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
597 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
598 return true;
599 }
600 return false;
601 };
602
603 ReportConditionalErrorAndExit(
604 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
605 "Error: --i must be a valid .wav file name.\n");
606
607 ReportConditionalErrorAndExit(
608 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
609 "Error: --o must be a valid .wav file name.\n");
610
611 ReportConditionalErrorAndExit(
612 settings.reverse_input_filename &&
613 (!valid_wav_name(*settings.reverse_input_filename)),
614 "Error: --ri must be a valid .wav file name.\n");
615
616 ReportConditionalErrorAndExit(
617 settings.reverse_output_filename &&
618 (!valid_wav_name(*settings.reverse_output_filename)),
619 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800620
621 ReportConditionalErrorAndExit(
622 settings.artificial_nearend_filename &&
623 !valid_wav_name(*settings.artificial_nearend_filename),
624 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200625
626 ReportConditionalErrorAndExit(
627 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
628 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100629
630 ReportConditionalErrorAndExit(
631 !settings.dump_internal_data &&
632 settings.dump_internal_data_output_dir.has_value(),
633 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100634
635 ReportConditionalErrorAndExit(
636 !settings.aec_dump_input_filename &&
637 settings.call_order_output_filename.has_value(),
638 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200639
640 ReportConditionalErrorAndExit(
641 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
642 settings.pre_amplifier_gain_factor.has_value(),
643 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
644 "specified and set.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800645}
646
647} // namespace
648
Ivo Creusen2cb41052018-03-15 12:22:52 +0100649int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
650 int argc,
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200651 char* argv[],
652 absl::string_view input_aecdump,
653 std::vector<float>* processed_capture_samples) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200654 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
655 if (args.size() != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700656 printf("%s", kUsageDescription);
oprypin6e09d872017-08-31 03:21:39 -0700657 return 1;
658 }
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000659
peah60a189f2016-05-24 20:54:40 -0700660 SimulationSettings settings = CreateSettings();
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200661 if (!input_aecdump.empty()) {
662 settings.aec_dump_input_string = input_aecdump;
663 settings.processed_capture_samples = processed_capture_samples;
664 RTC_CHECK(settings.processed_capture_samples);
665 }
peah60a189f2016-05-24 20:54:40 -0700666 PerformBasicParameterSanityChecks(settings);
667 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000668
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200669 if (settings.aec_dump_input_filename || settings.aec_dump_input_string) {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100670 processor.reset(new AecDumpBasedSimulator(settings, std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800671 } else {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100672 processor.reset(new WavBasedSimulator(settings, std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700673 }
674
peah60a189f2016-05-24 20:54:40 -0700675 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800676
peah60a189f2016-05-24 20:54:40 -0700677 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200678 processor->GetApiCallStatistics().PrintReport();
679 }
680 if (settings.performance_report_output_filename) {
681 processor->GetApiCallStatistics().WriteReportToFile(
682 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700683 }
684
685 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
686 if (processor->OutputWasBitexact()) {
687 std::cout << "The processing was bitexact.";
688 } else {
689 std::cout << "The processing was not bitexact.";
690 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700691 }
aluebsb0ad43b2015-11-20 00:11:53 -0800692
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000693 return 0;
694}
695
peah60a189f2016-05-24 20:54:40 -0700696} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000697} // namespace webrtc