blob: d9a4227eb721216e7da83954b5d4becd03bfd06d [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"
saza0cf99ce2020-04-01 17:42:01 +020030#include "system_wrappers/include/field_trial.h"
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000031
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020032constexpr int kParameterNotSpecifiedValue = -10000;
33
34ABSL_FLAG(std::string, dump_input, "", "Aec dump input filename");
35ABSL_FLAG(std::string, dump_output, "", "Aec dump output filename");
36ABSL_FLAG(std::string, i, "", "Forward stream input wav filename");
37ABSL_FLAG(std::string, o, "", "Forward stream output wav filename");
38ABSL_FLAG(std::string, ri, "", "Reverse stream input wav filename");
39ABSL_FLAG(std::string, ro, "", "Reverse stream output wav filename");
40ABSL_FLAG(std::string,
41 artificial_nearend,
42 "",
43 "Artificial nearend wav filename");
Per Åhgrenc20a19c2019-11-13 11:12:29 +010044ABSL_FLAG(std::string, linear_aec_output, "", "Linear AEC output wav filename");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020045ABSL_FLAG(int,
46 output_num_channels,
47 kParameterNotSpecifiedValue,
48 "Number of forward stream output channels");
49ABSL_FLAG(int,
50 reverse_output_num_channels,
51 kParameterNotSpecifiedValue,
52 "Number of Reverse stream output channels");
53ABSL_FLAG(int,
54 output_sample_rate_hz,
55 kParameterNotSpecifiedValue,
56 "Forward stream output sample rate in Hz");
57ABSL_FLAG(int,
58 reverse_output_sample_rate_hz,
59 kParameterNotSpecifiedValue,
60 "Reverse stream output sample rate in Hz");
61ABSL_FLAG(bool,
62 fixed_interface,
63 false,
64 "Use the fixed interface when operating on wav files");
65ABSL_FLAG(int,
66 aec,
67 kParameterNotSpecifiedValue,
68 "Activate (1) or deactivate(0) the echo canceller");
69ABSL_FLAG(int,
70 aecm,
71 kParameterNotSpecifiedValue,
72 "Activate (1) or deactivate(0) the mobile echo controller");
73ABSL_FLAG(int,
74 ed,
75 kParameterNotSpecifiedValue,
76 "Activate (1) or deactivate (0) the residual echo detector");
77ABSL_FLAG(std::string,
78 ed_graph,
79 "",
80 "Output filename for graph of echo likelihood");
81ABSL_FLAG(int,
82 agc,
83 kParameterNotSpecifiedValue,
84 "Activate (1) or deactivate(0) the AGC");
85ABSL_FLAG(int,
86 agc2,
87 kParameterNotSpecifiedValue,
88 "Activate (1) or deactivate(0) the AGC2");
89ABSL_FLAG(int,
90 pre_amplifier,
91 kParameterNotSpecifiedValue,
92 "Activate (1) or deactivate(0) the pre amplifier");
93ABSL_FLAG(int,
94 hpf,
95 kParameterNotSpecifiedValue,
96 "Activate (1) or deactivate(0) the high-pass filter");
97ABSL_FLAG(int,
98 ns,
99 kParameterNotSpecifiedValue,
100 "Activate (1) or deactivate(0) the noise suppressor");
101ABSL_FLAG(int,
102 ts,
103 kParameterNotSpecifiedValue,
104 "Activate (1) or deactivate(0) the transient suppressor");
105ABSL_FLAG(int,
Per Åhgren0695df12020-01-13 14:43:13 +0100106 analog_agc,
107 kParameterNotSpecifiedValue,
108 "Activate (1) or deactivate(0) the transient suppressor");
109ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200110 vad,
111 kParameterNotSpecifiedValue,
112 "Activate (1) or deactivate(0) the voice activity detector");
113ABSL_FLAG(int,
114 le,
115 kParameterNotSpecifiedValue,
116 "Activate (1) or deactivate(0) the level estimator");
117ABSL_FLAG(bool,
118 all_default,
119 false,
120 "Activate all of the default components (will be overridden by any "
121 "other settings)");
122ABSL_FLAG(int,
Per Åhgren0695df12020-01-13 14:43:13 +0100123 analog_agc_disable_digital_adaptive,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200124 kParameterNotSpecifiedValue,
125 "Force-deactivate (1) digital adaptation in "
126 "experimental AGC. Digital adaptation is active by default (0).");
127ABSL_FLAG(int,
Per Åhgren0695df12020-01-13 14:43:13 +0100128 analog_agc_agc2_level_estimator,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200129 kParameterNotSpecifiedValue,
130 "AGC2 level estimation"
131 " in the experimental AGC. AGC1 level estimation is the default (0)");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200132ABSL_FLAG(int,
133 agc_mode,
134 kParameterNotSpecifiedValue,
135 "Specify the AGC mode (0-2)");
136ABSL_FLAG(int,
137 agc_target_level,
138 kParameterNotSpecifiedValue,
139 "Specify the AGC target level (0-31)");
140ABSL_FLAG(int,
141 agc_limiter,
142 kParameterNotSpecifiedValue,
143 "Activate (1) or deactivate(0) the level estimator");
144ABSL_FLAG(int,
145 agc_compression_gain,
146 kParameterNotSpecifiedValue,
147 "Specify the AGC compression gain (0-90)");
148ABSL_FLAG(int,
149 agc2_enable_adaptive_gain,
150 kParameterNotSpecifiedValue,
151 "Activate (1) or deactivate(0) the AGC2 adaptive gain");
152ABSL_FLAG(float,
153 agc2_fixed_gain_db,
154 kParameterNotSpecifiedValue,
155 "AGC2 fixed gain (dB) to apply");
156ABSL_FLAG(std::string,
157 agc2_adaptive_level_estimator,
158 "RMS",
159 "AGC2 adaptive digital level estimator to use [RMS, peak]");
160ABSL_FLAG(float,
161 pre_amplifier_gain_factor,
162 kParameterNotSpecifiedValue,
163 "Pre-amplifier gain factor (linear) to apply");
164ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200165 ns_level,
166 kParameterNotSpecifiedValue,
167 "Specify the NS level (0-3)");
168ABSL_FLAG(int,
Per Åhgren2e8e1c62019-12-20 00:42:22 +0100169 ns_analysis_on_linear_aec_output,
170 kParameterNotSpecifiedValue,
171 "Specifies whether the noise suppression analysis is done on the "
172 "linear AEC output");
173ABSL_FLAG(int,
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200174 maximum_internal_processing_rate,
175 kParameterNotSpecifiedValue,
176 "Set a maximum internal processing rate (32000 or 48000) to override "
177 "the default rate");
178ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200179 stream_delay,
180 kParameterNotSpecifiedValue,
181 "Specify the stream delay in ms to use");
182ABSL_FLAG(int,
183 use_stream_delay,
184 kParameterNotSpecifiedValue,
185 "Activate (1) or deactivate(0) reporting the stream delay");
186ABSL_FLAG(int,
187 stream_drift_samples,
188 kParameterNotSpecifiedValue,
189 "Specify the number of stream drift samples to use");
190ABSL_FLAG(int, initial_mic_level, 100, "Initial mic level (0-255)");
191ABSL_FLAG(int,
192 simulate_mic_gain,
193 0,
194 "Activate (1) or deactivate(0) the analog mic gain simulation");
195ABSL_FLAG(int,
Per Åhgrene14cb992019-11-27 09:34:22 +0100196 multi_channel_render,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200197 kParameterNotSpecifiedValue,
Per Åhgrene14cb992019-11-27 09:34:22 +0100198 "Activate (1) or deactivate(0) multi-channel render processing in "
199 "APM pipeline");
200ABSL_FLAG(int,
201 multi_channel_capture,
202 kParameterNotSpecifiedValue,
203 "Activate (1) or deactivate(0) multi-channel capture processing in "
204 "APM pipeline");
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200205ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200206 simulated_mic_kind,
207 kParameterNotSpecifiedValue,
208 "Specify which microphone kind to use for microphone simulation");
209ABSL_FLAG(bool, performance_report, false, "Report the APM performance ");
210ABSL_FLAG(std::string,
211 performance_report_output_file,
212 "",
213 "Generate a CSV file with the API call durations");
214ABSL_FLAG(bool, verbose, false, "Produce verbose output");
215ABSL_FLAG(bool,
216 quiet,
217 false,
218 "Avoid producing information about the progress.");
219ABSL_FLAG(bool,
220 bitexactness_report,
221 false,
222 "Report bitexactness for aec dump result reproduction");
223ABSL_FLAG(bool,
224 discard_settings_in_aecdump,
225 false,
226 "Discard any config settings specified in the aec dump");
227ABSL_FLAG(bool,
228 store_intermediate_output,
229 false,
230 "Creates new output files after each init");
231ABSL_FLAG(std::string,
232 custom_call_order_file,
233 "",
234 "Custom process API call order file");
235ABSL_FLAG(std::string,
236 output_custom_call_order_file,
237 "",
238 "Generate custom process API call order file from AEC dump");
239ABSL_FLAG(bool,
240 print_aec_parameter_values,
241 false,
242 "Print parameter values used in AEC in JSON-format");
243ABSL_FLAG(std::string,
244 aec_settings,
245 "",
246 "File in JSON-format with custom AEC settings");
247ABSL_FLAG(bool,
248 dump_data,
249 false,
250 "Dump internal data during the call (requires build flag)");
251ABSL_FLAG(std::string,
252 dump_data_output_dir,
253 "",
254 "Internal data dump output directory");
Per Åhgren5dca3f12020-01-28 09:08:11 +0100255ABSL_FLAG(bool,
256 float_wav_output,
257 false,
258 "Produce floating point wav output files.");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200259
saza0cf99ce2020-04-01 17:42:01 +0200260ABSL_FLAG(std::string,
261 force_fieldtrials,
262 "",
263 "Field trials control experimental feature code which can be forced. "
264 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
265 " will assign the group Enable to field trial WebRTC-FooFeature.");
266
peah60a189f2016-05-24 20:54:40 -0700267namespace webrtc {
268namespace test {
Peter Kasting69558702016-01-12 16:26:35 -0800269namespace {
270
peah60a189f2016-05-24 20:54:40 -0700271const char kUsageDescription[] =
272 "Usage: audioproc_f [options] -i <input.wav>\n"
273 " or\n"
274 " audioproc_f [options] -dump_input <aec_dump>\n"
275 "\n\n"
276 "Command-line tool to simulate a call using the audio "
277 "processing module, either based on wav files or "
oprypin6e09d872017-08-31 03:21:39 -0700278 "protobuf debug dump recordings.\n";
peah60a189f2016-05-24 20:54:40 -0700279
Alessio Bazzica68170382018-11-20 12:27:20 +0100280std::vector<std::string> GetAgc2AdaptiveLevelEstimatorNames() {
281 return {"RMS", "peak"};
282}
peah60a189f2016-05-24 20:54:40 -0700283
Alex Loiko890988c2017-08-31 10:25:48 +0200284void SetSettingIfSpecified(const std::string& value,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200285 absl::optional<std::string>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700286 if (value.compare("") != 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100287 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700288 }
289}
290
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200291void SetSettingIfSpecified(int value, absl::optional<int>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700292 if (value != kParameterNotSpecifiedValue) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100293 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700294 }
295}
296
Per Åhgrenef349602019-04-12 16:26:34 +0200297void SetSettingIfSpecified(float value, absl::optional<float>* parameter) {
298 constexpr float kFloatParameterNotSpecifiedValue =
299 kParameterNotSpecifiedValue;
300 if (value != kFloatParameterNotSpecifiedValue) {
301 *parameter = value;
302 }
303}
304
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200305void SetSettingIfFlagSet(int32_t flag, absl::optional<bool>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700306 if (flag == 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100307 *parameter = false;
peah60a189f2016-05-24 20:54:40 -0700308 } else if (flag == 1) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100309 *parameter = true;
peah60a189f2016-05-24 20:54:40 -0700310 }
311}
312
Alessio Bazzica68170382018-11-20 12:27:20 +0100313AudioProcessing::Config::GainController2::LevelEstimator
314MapAgc2AdaptiveLevelEstimator(absl::string_view name) {
315 if (name.compare("RMS") == 0) {
316 return AudioProcessing::Config::GainController2::LevelEstimator::kRms;
317 }
318 if (name.compare("peak") == 0) {
319 return AudioProcessing::Config::GainController2::LevelEstimator::kPeak;
320 }
321 auto concat_strings =
322 [](const std::vector<std::string>& strings) -> std::string {
323 rtc::StringBuilder ss;
324 for (const auto& s : strings) {
325 ss << " " << s;
326 }
327 return ss.Release();
328 };
329 RTC_CHECK(false)
330 << "Invalid value for agc2_adaptive_level_estimator, valid options:"
331 << concat_strings(GetAgc2AdaptiveLevelEstimatorNames()) << ".";
332}
333
peah60a189f2016-05-24 20:54:40 -0700334SimulationSettings CreateSettings() {
335 SimulationSettings settings;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200336 if (absl::GetFlag(FLAGS_all_default)) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100337 settings.use_le = true;
338 settings.use_vad = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100339 settings.use_ts = true;
Per Åhgren0695df12020-01-13 14:43:13 +0100340 settings.use_analog_agc = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100341 settings.use_ns = true;
342 settings.use_hpf = true;
343 settings.use_agc = true;
344 settings.use_agc2 = false;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200345 settings.use_pre_amplifier = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100346 settings.use_aec = true;
347 settings.use_aecm = false;
348 settings.use_ed = false;
peah60a189f2016-05-24 20:54:40 -0700349 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200350 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_input),
351 &settings.aec_dump_input_filename);
352 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_output),
353 &settings.aec_dump_output_filename);
354 SetSettingIfSpecified(absl::GetFlag(FLAGS_i), &settings.input_filename);
355 SetSettingIfSpecified(absl::GetFlag(FLAGS_o), &settings.output_filename);
356 SetSettingIfSpecified(absl::GetFlag(FLAGS_ri),
357 &settings.reverse_input_filename);
358 SetSettingIfSpecified(absl::GetFlag(FLAGS_ro),
359 &settings.reverse_output_filename);
360 SetSettingIfSpecified(absl::GetFlag(FLAGS_artificial_nearend),
peahdf80fd12016-12-09 02:43:40 -0800361 &settings.artificial_nearend_filename);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100362 SetSettingIfSpecified(absl::GetFlag(FLAGS_linear_aec_output),
363 &settings.linear_aec_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200364 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700365 &settings.output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200366 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700367 &settings.reverse_output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200368 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700369 &settings.output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200370 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700371 &settings.reverse_output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200372 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aec), &settings.use_aec);
373 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aecm), &settings.use_aecm);
374 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ed), &settings.use_ed);
375 SetSettingIfSpecified(absl::GetFlag(FLAGS_ed_graph),
376 &settings.ed_graph_output_filename);
377 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc), &settings.use_agc);
378 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2), &settings.use_agc2);
379 SetSettingIfFlagSet(absl::GetFlag(FLAGS_pre_amplifier),
380 &settings.use_pre_amplifier);
381 SetSettingIfFlagSet(absl::GetFlag(FLAGS_hpf), &settings.use_hpf);
382 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns), &settings.use_ns);
383 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ts), &settings.use_ts);
Per Åhgren0695df12020-01-13 14:43:13 +0100384 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc),
385 &settings.use_analog_agc);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200386 SetSettingIfFlagSet(absl::GetFlag(FLAGS_vad), &settings.use_vad);
387 SetSettingIfFlagSet(absl::GetFlag(FLAGS_le), &settings.use_le);
Per Åhgren0695df12020-01-13 14:43:13 +0100388 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_disable_digital_adaptive),
389 &settings.analog_agc_disable_digital_adaptive);
390 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_agc2_level_estimator),
391 &settings.use_analog_agc_agc2_level_estimator);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200392 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_mode), &settings.agc_mode);
393 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_target_level),
394 &settings.agc_target_level);
395 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc_limiter),
396 &settings.use_agc_limiter);
397 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_compression_gain),
peah60a189f2016-05-24 20:54:40 -0700398 &settings.agc_compression_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200399 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2_enable_adaptive_gain),
Per Åhgrene4d23b12018-10-02 23:40:07 +0200400 &settings.agc2_use_adaptive_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200401 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc2_fixed_gain_db),
402 &settings.agc2_fixed_gain_db);
403 settings.agc2_adaptive_level_estimator = MapAgc2AdaptiveLevelEstimator(
404 absl::GetFlag(FLAGS_agc2_adaptive_level_estimator));
405 SetSettingIfSpecified(absl::GetFlag(FLAGS_pre_amplifier_gain_factor),
Per Åhgrenef349602019-04-12 16:26:34 +0200406 &settings.pre_amplifier_gain_factor);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200407 SetSettingIfSpecified(absl::GetFlag(FLAGS_ns_level), &settings.ns_level);
Per Åhgren2e8e1c62019-12-20 00:42:22 +0100408 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns_analysis_on_linear_aec_output),
409 &settings.ns_analysis_on_linear_aec_output);
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200410 SetSettingIfSpecified(absl::GetFlag(FLAGS_maximum_internal_processing_rate),
411 &settings.maximum_internal_processing_rate);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200412 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_delay),
413 &settings.stream_delay);
414 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_stream_delay),
415 &settings.use_stream_delay);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200416 SetSettingIfSpecified(absl::GetFlag(FLAGS_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100417 &settings.call_order_input_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200418 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100419 &settings.call_order_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200420 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_settings),
421 &settings.aec_settings_filename);
422 settings.initial_mic_level = absl::GetFlag(FLAGS_initial_mic_level);
Per Åhgrene14cb992019-11-27 09:34:22 +0100423 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_render),
424 &settings.multi_channel_render);
425 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_capture),
426 &settings.multi_channel_capture);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200427 settings.simulate_mic_gain = absl::GetFlag(FLAGS_simulate_mic_gain);
428 SetSettingIfSpecified(absl::GetFlag(FLAGS_simulated_mic_kind),
429 &settings.simulated_mic_kind);
430 settings.report_performance = absl::GetFlag(FLAGS_performance_report);
431 SetSettingIfSpecified(absl::GetFlag(FLAGS_performance_report_output_file),
Per Åhgrenada9b892019-04-03 16:06:42 +0200432 &settings.performance_report_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200433 settings.use_verbose_logging = absl::GetFlag(FLAGS_verbose);
434 settings.use_quiet_output = absl::GetFlag(FLAGS_quiet);
435 settings.report_bitexactness = absl::GetFlag(FLAGS_bitexactness_report);
436 settings.discard_all_settings_in_aecdump =
437 absl::GetFlag(FLAGS_discard_settings_in_aecdump);
438 settings.fixed_interface = absl::GetFlag(FLAGS_fixed_interface);
439 settings.store_intermediate_output =
440 absl::GetFlag(FLAGS_store_intermediate_output);
441 settings.print_aec_parameter_values =
442 absl::GetFlag(FLAGS_print_aec_parameter_values);
443 settings.dump_internal_data = absl::GetFlag(FLAGS_dump_data);
444 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_data_output_dir),
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100445 &settings.dump_internal_data_output_dir);
Per Åhgren5dca3f12020-01-28 09:08:11 +0100446 settings.wav_output_format = absl::GetFlag(FLAGS_float_wav_output)
447 ? WavFile::SampleFormat::kFloat
448 : WavFile::SampleFormat::kInt16;
peah60a189f2016-05-24 20:54:40 -0700449
450 return settings;
451}
452
Alex Loiko890988c2017-08-31 10:25:48 +0200453void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
peah60a189f2016-05-24 20:54:40 -0700454 if (condition) {
455 std::cerr << message << std::endl;
456 exit(1);
457 }
458}
459
460void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
461 if (settings.input_filename || settings.reverse_input_filename) {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200462 ReportConditionalErrorAndExit(
463 !!settings.aec_dump_input_filename,
464 "Error: The aec dump file cannot be specified "
465 "together with input wav files!\n");
466
467 ReportConditionalErrorAndExit(
468 !!settings.aec_dump_input_string,
469 "Error: The aec dump input string cannot be specified "
470 "together with input wav files!\n");
peah60a189f2016-05-24 20:54:40 -0700471
peahdf80fd12016-12-09 02:43:40 -0800472 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
473 "Error: The artificial nearend cannot be "
474 "specified together with input wav files!\n");
475
peah60a189f2016-05-24 20:54:40 -0700476 ReportConditionalErrorAndExit(!settings.input_filename,
477 "Error: When operating at wav files, the "
478 "input wav filename must be "
479 "specified!\n");
480
481 ReportConditionalErrorAndExit(
482 settings.reverse_output_filename && !settings.reverse_input_filename,
483 "Error: When operating at wav files, the reverse input wav filename "
484 "must be specified if the reverse output wav filename is specified!\n");
485 } else {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200486 ReportConditionalErrorAndExit(
487 !settings.aec_dump_input_filename && !settings.aec_dump_input_string,
488 "Error: Either the aec dump input file, the wav "
489 "input file or the aec dump input string must be specified!\n");
490 ReportConditionalErrorAndExit(
491 settings.aec_dump_input_filename && settings.aec_dump_input_string,
492 "Error: The aec dump input file cannot be specified together with the "
493 "aec dump input string!\n");
peah60a189f2016-05-24 20:54:40 -0700494 }
495
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100496 ReportConditionalErrorAndExit(settings.use_aec && !(*settings.use_aec) &&
497 settings.linear_aec_output_filename,
498 "Error: The linear AEC ouput filename cannot "
499 "be specified without the AEC being active");
500
501 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700502 settings.use_aec && *settings.use_aec && settings.use_aecm &&
503 *settings.use_aecm,
504 "Error: The AEC and the AECM cannot be activated at the same time!\n");
505
506 ReportConditionalErrorAndExit(
507 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
508 "Error: --output_sample_rate_hz must be positive!\n");
509
510 ReportConditionalErrorAndExit(
511 settings.reverse_output_sample_rate_hz &&
512 settings.output_sample_rate_hz &&
513 *settings.output_sample_rate_hz <= 0,
514 "Error: --reverse_output_sample_rate_hz must be positive!\n");
515
516 ReportConditionalErrorAndExit(
517 settings.output_num_channels && *settings.output_num_channels <= 0,
518 "Error: --output_num_channels must be positive!\n");
519
520 ReportConditionalErrorAndExit(
521 settings.reverse_output_num_channels &&
522 *settings.reverse_output_num_channels <= 0,
523 "Error: --reverse_output_num_channels must be positive!\n");
524
peah60a189f2016-05-24 20:54:40 -0700525 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700526 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
527 (*settings.agc_target_level) > 31),
528 "Error: --agc_target_level must be specified between 0 and 31.\n");
529
530 ReportConditionalErrorAndExit(
531 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
532 (*settings.agc_compression_gain) > 90),
533 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
534
535 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200536 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
537 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200538 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
539
540 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700541 settings.ns_level &&
542 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
543 "Error: --ns_level must be specified between 0 and 3.\n");
544
545 ReportConditionalErrorAndExit(
546 settings.report_bitexactness && !settings.aec_dump_input_filename,
547 "Error: --bitexactness_report can only be used when operating on an "
548 "aecdump\n");
549
peah5ad5de32016-12-09 03:18:22 -0800550 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100551 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800552 "Error: --custom_call_order_file cannot be used when operating on an "
553 "aecdump\n");
554
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200555 ReportConditionalErrorAndExit(
556 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
557 "Error: --initial_mic_level must be specified between 0 and 255.\n");
558
559 ReportConditionalErrorAndExit(
560 settings.simulated_mic_kind && !settings.simulate_mic_gain,
561 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
562 "disabled\n");
563
564 ReportConditionalErrorAndExit(
565 !settings.simulated_mic_kind && settings.simulate_mic_gain,
566 "Error: --simulated_mic_kind must be specified when mic simulation is "
567 "enabled\n");
568
peah60a189f2016-05-24 20:54:40 -0700569 auto valid_wav_name = [](const std::string& wav_file_name) {
570 if (wav_file_name.size() < 5) {
571 return false;
572 }
573 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
574 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
575 return true;
576 }
577 return false;
578 };
579
580 ReportConditionalErrorAndExit(
581 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
582 "Error: --i must be a valid .wav file name.\n");
583
584 ReportConditionalErrorAndExit(
585 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
586 "Error: --o must be a valid .wav file name.\n");
587
588 ReportConditionalErrorAndExit(
589 settings.reverse_input_filename &&
590 (!valid_wav_name(*settings.reverse_input_filename)),
591 "Error: --ri must be a valid .wav file name.\n");
592
593 ReportConditionalErrorAndExit(
594 settings.reverse_output_filename &&
595 (!valid_wav_name(*settings.reverse_output_filename)),
596 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800597
598 ReportConditionalErrorAndExit(
599 settings.artificial_nearend_filename &&
600 !valid_wav_name(*settings.artificial_nearend_filename),
601 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200602
603 ReportConditionalErrorAndExit(
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100604 settings.linear_aec_output_filename &&
605 (!valid_wav_name(*settings.linear_aec_output_filename)),
606 "Error: --linear_aec_output must be a valid .wav file name.\n");
607
608 ReportConditionalErrorAndExit(
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200609 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
610 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100611
612 ReportConditionalErrorAndExit(
613 !settings.dump_internal_data &&
614 settings.dump_internal_data_output_dir.has_value(),
615 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100616
617 ReportConditionalErrorAndExit(
618 !settings.aec_dump_input_filename &&
619 settings.call_order_output_filename.has_value(),
620 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200621
622 ReportConditionalErrorAndExit(
623 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
624 settings.pre_amplifier_gain_factor.has_value(),
625 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
626 "specified and set.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800627}
628
629} // namespace
630
Ivo Creusen2cb41052018-03-15 12:22:52 +0100631int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
632 int argc,
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200633 char* argv[],
634 absl::string_view input_aecdump,
635 std::vector<float>* processed_capture_samples) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200636 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
637 if (args.size() != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700638 printf("%s", kUsageDescription);
oprypin6e09d872017-08-31 03:21:39 -0700639 return 1;
640 }
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000641
saza0cf99ce2020-04-01 17:42:01 +0200642 // InitFieldTrialsFromString stores the char*, so the char array must
643 // outlive the application.
644 const std::string field_trials = absl::GetFlag(FLAGS_force_fieldtrials);
645 webrtc::field_trial::InitFieldTrialsFromString(field_trials.c_str());
646
peah60a189f2016-05-24 20:54:40 -0700647 SimulationSettings settings = CreateSettings();
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200648 if (!input_aecdump.empty()) {
649 settings.aec_dump_input_string = input_aecdump;
650 settings.processed_capture_samples = processed_capture_samples;
651 RTC_CHECK(settings.processed_capture_samples);
652 }
peah60a189f2016-05-24 20:54:40 -0700653 PerformBasicParameterSanityChecks(settings);
654 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000655
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200656 if (settings.aec_dump_input_filename || settings.aec_dump_input_string) {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100657 processor.reset(new AecDumpBasedSimulator(settings, std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800658 } else {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100659 processor.reset(new WavBasedSimulator(settings, std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700660 }
661
peah60a189f2016-05-24 20:54:40 -0700662 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800663
peah60a189f2016-05-24 20:54:40 -0700664 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200665 processor->GetApiCallStatistics().PrintReport();
666 }
667 if (settings.performance_report_output_filename) {
668 processor->GetApiCallStatistics().WriteReportToFile(
669 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700670 }
671
672 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
673 if (processor->OutputWasBitexact()) {
674 std::cout << "The processing was bitexact.";
675 } else {
676 std::cout << "The processing was not bitexact.";
677 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700678 }
aluebsb0ad43b2015-11-20 00:11:53 -0800679
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000680 return 0;
681}
682
peah60a189f2016-05-24 20:54:40 -0700683} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000684} // namespace webrtc