blob: 2d93e5f3447ddff9236a7de4c89ad650543d6fe6 [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,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200128 agc_mode,
129 kParameterNotSpecifiedValue,
130 "Specify the AGC mode (0-2)");
131ABSL_FLAG(int,
132 agc_target_level,
133 kParameterNotSpecifiedValue,
134 "Specify the AGC target level (0-31)");
135ABSL_FLAG(int,
136 agc_limiter,
137 kParameterNotSpecifiedValue,
138 "Activate (1) or deactivate(0) the level estimator");
139ABSL_FLAG(int,
140 agc_compression_gain,
141 kParameterNotSpecifiedValue,
142 "Specify the AGC compression gain (0-90)");
143ABSL_FLAG(int,
144 agc2_enable_adaptive_gain,
145 kParameterNotSpecifiedValue,
146 "Activate (1) or deactivate(0) the AGC2 adaptive gain");
147ABSL_FLAG(float,
148 agc2_fixed_gain_db,
149 kParameterNotSpecifiedValue,
150 "AGC2 fixed gain (dB) to apply");
151ABSL_FLAG(std::string,
152 agc2_adaptive_level_estimator,
153 "RMS",
154 "AGC2 adaptive digital level estimator to use [RMS, peak]");
155ABSL_FLAG(float,
156 pre_amplifier_gain_factor,
157 kParameterNotSpecifiedValue,
158 "Pre-amplifier gain factor (linear) to apply");
159ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200160 ns_level,
161 kParameterNotSpecifiedValue,
162 "Specify the NS level (0-3)");
163ABSL_FLAG(int,
Per Åhgren2e8e1c62019-12-20 00:42:22 +0100164 ns_analysis_on_linear_aec_output,
165 kParameterNotSpecifiedValue,
166 "Specifies whether the noise suppression analysis is done on the "
167 "linear AEC output");
168ABSL_FLAG(int,
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200169 maximum_internal_processing_rate,
170 kParameterNotSpecifiedValue,
171 "Set a maximum internal processing rate (32000 or 48000) to override "
172 "the default rate");
173ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200174 stream_delay,
175 kParameterNotSpecifiedValue,
176 "Specify the stream delay in ms to use");
177ABSL_FLAG(int,
178 use_stream_delay,
179 kParameterNotSpecifiedValue,
180 "Activate (1) or deactivate(0) reporting the stream delay");
181ABSL_FLAG(int,
182 stream_drift_samples,
183 kParameterNotSpecifiedValue,
184 "Specify the number of stream drift samples to use");
185ABSL_FLAG(int, initial_mic_level, 100, "Initial mic level (0-255)");
186ABSL_FLAG(int,
187 simulate_mic_gain,
188 0,
189 "Activate (1) or deactivate(0) the analog mic gain simulation");
190ABSL_FLAG(int,
Per Åhgrene14cb992019-11-27 09:34:22 +0100191 multi_channel_render,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200192 kParameterNotSpecifiedValue,
Per Åhgrene14cb992019-11-27 09:34:22 +0100193 "Activate (1) or deactivate(0) multi-channel render processing in "
194 "APM pipeline");
195ABSL_FLAG(int,
196 multi_channel_capture,
197 kParameterNotSpecifiedValue,
198 "Activate (1) or deactivate(0) multi-channel capture processing in "
199 "APM pipeline");
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200200ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200201 simulated_mic_kind,
202 kParameterNotSpecifiedValue,
203 "Specify which microphone kind to use for microphone simulation");
204ABSL_FLAG(bool, performance_report, false, "Report the APM performance ");
205ABSL_FLAG(std::string,
206 performance_report_output_file,
207 "",
208 "Generate a CSV file with the API call durations");
209ABSL_FLAG(bool, verbose, false, "Produce verbose output");
210ABSL_FLAG(bool,
211 quiet,
212 false,
213 "Avoid producing information about the progress.");
214ABSL_FLAG(bool,
215 bitexactness_report,
216 false,
217 "Report bitexactness for aec dump result reproduction");
218ABSL_FLAG(bool,
219 discard_settings_in_aecdump,
220 false,
221 "Discard any config settings specified in the aec dump");
222ABSL_FLAG(bool,
223 store_intermediate_output,
224 false,
225 "Creates new output files after each init");
226ABSL_FLAG(std::string,
227 custom_call_order_file,
228 "",
229 "Custom process API call order file");
230ABSL_FLAG(std::string,
231 output_custom_call_order_file,
232 "",
233 "Generate custom process API call order file from AEC dump");
234ABSL_FLAG(bool,
235 print_aec_parameter_values,
236 false,
237 "Print parameter values used in AEC in JSON-format");
238ABSL_FLAG(std::string,
239 aec_settings,
240 "",
241 "File in JSON-format with custom AEC settings");
242ABSL_FLAG(bool,
243 dump_data,
244 false,
245 "Dump internal data during the call (requires build flag)");
246ABSL_FLAG(std::string,
247 dump_data_output_dir,
248 "",
249 "Internal data dump output directory");
Per Åhgrenc2ae4c82021-01-20 15:42:13 +0100250ABSL_FLAG(int,
251 dump_set_to_use,
252 kParameterNotSpecifiedValue,
253 "Specifies the dump set to use (if not all the dump sets will "
254 "be used");
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);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200390 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_mode), &settings.agc_mode);
391 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_target_level),
392 &settings.agc_target_level);
393 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc_limiter),
394 &settings.use_agc_limiter);
395 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_compression_gain),
peah60a189f2016-05-24 20:54:40 -0700396 &settings.agc_compression_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200397 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2_enable_adaptive_gain),
Per Åhgrene4d23b12018-10-02 23:40:07 +0200398 &settings.agc2_use_adaptive_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200399 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc2_fixed_gain_db),
400 &settings.agc2_fixed_gain_db);
401 settings.agc2_adaptive_level_estimator = MapAgc2AdaptiveLevelEstimator(
402 absl::GetFlag(FLAGS_agc2_adaptive_level_estimator));
403 SetSettingIfSpecified(absl::GetFlag(FLAGS_pre_amplifier_gain_factor),
Per Åhgrenef349602019-04-12 16:26:34 +0200404 &settings.pre_amplifier_gain_factor);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200405 SetSettingIfSpecified(absl::GetFlag(FLAGS_ns_level), &settings.ns_level);
Per Åhgren2e8e1c62019-12-20 00:42:22 +0100406 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns_analysis_on_linear_aec_output),
407 &settings.ns_analysis_on_linear_aec_output);
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200408 SetSettingIfSpecified(absl::GetFlag(FLAGS_maximum_internal_processing_rate),
409 &settings.maximum_internal_processing_rate);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200410 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_delay),
411 &settings.stream_delay);
412 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_stream_delay),
413 &settings.use_stream_delay);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200414 SetSettingIfSpecified(absl::GetFlag(FLAGS_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100415 &settings.call_order_input_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200416 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100417 &settings.call_order_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200418 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_settings),
419 &settings.aec_settings_filename);
420 settings.initial_mic_level = absl::GetFlag(FLAGS_initial_mic_level);
Per Åhgrene14cb992019-11-27 09:34:22 +0100421 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_render),
422 &settings.multi_channel_render);
423 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_capture),
424 &settings.multi_channel_capture);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200425 settings.simulate_mic_gain = absl::GetFlag(FLAGS_simulate_mic_gain);
426 SetSettingIfSpecified(absl::GetFlag(FLAGS_simulated_mic_kind),
427 &settings.simulated_mic_kind);
428 settings.report_performance = absl::GetFlag(FLAGS_performance_report);
429 SetSettingIfSpecified(absl::GetFlag(FLAGS_performance_report_output_file),
Per Åhgrenada9b892019-04-03 16:06:42 +0200430 &settings.performance_report_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200431 settings.use_verbose_logging = absl::GetFlag(FLAGS_verbose);
432 settings.use_quiet_output = absl::GetFlag(FLAGS_quiet);
433 settings.report_bitexactness = absl::GetFlag(FLAGS_bitexactness_report);
434 settings.discard_all_settings_in_aecdump =
435 absl::GetFlag(FLAGS_discard_settings_in_aecdump);
436 settings.fixed_interface = absl::GetFlag(FLAGS_fixed_interface);
437 settings.store_intermediate_output =
438 absl::GetFlag(FLAGS_store_intermediate_output);
439 settings.print_aec_parameter_values =
440 absl::GetFlag(FLAGS_print_aec_parameter_values);
441 settings.dump_internal_data = absl::GetFlag(FLAGS_dump_data);
442 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_data_output_dir),
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100443 &settings.dump_internal_data_output_dir);
Per Åhgrenc2ae4c82021-01-20 15:42:13 +0100444 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_set_to_use),
445 &settings.dump_set_to_use);
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
Per Åhgrene9cd6172020-05-19 12:52:08 +0200460void PerformBasicParameterSanityChecks(
461 const SimulationSettings& settings,
462 bool pre_constructed_ap_provided,
463 bool pre_constructed_ap_builder_provided) {
peah60a189f2016-05-24 20:54:40 -0700464 if (settings.input_filename || settings.reverse_input_filename) {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200465 ReportConditionalErrorAndExit(
466 !!settings.aec_dump_input_filename,
467 "Error: The aec dump file cannot be specified "
468 "together with input wav files!\n");
469
470 ReportConditionalErrorAndExit(
471 !!settings.aec_dump_input_string,
472 "Error: The aec dump input string cannot be specified "
473 "together with input wav files!\n");
peah60a189f2016-05-24 20:54:40 -0700474
peahdf80fd12016-12-09 02:43:40 -0800475 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
476 "Error: The artificial nearend cannot be "
477 "specified together with input wav files!\n");
478
peah60a189f2016-05-24 20:54:40 -0700479 ReportConditionalErrorAndExit(!settings.input_filename,
480 "Error: When operating at wav files, the "
481 "input wav filename must be "
482 "specified!\n");
483
484 ReportConditionalErrorAndExit(
485 settings.reverse_output_filename && !settings.reverse_input_filename,
486 "Error: When operating at wav files, the reverse input wav filename "
487 "must be specified if the reverse output wav filename is specified!\n");
488 } else {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200489 ReportConditionalErrorAndExit(
490 !settings.aec_dump_input_filename && !settings.aec_dump_input_string,
491 "Error: Either the aec dump input file, the wav "
492 "input file or the aec dump input string must be specified!\n");
493 ReportConditionalErrorAndExit(
494 settings.aec_dump_input_filename && settings.aec_dump_input_string,
495 "Error: The aec dump input file cannot be specified together with the "
496 "aec dump input string!\n");
peah60a189f2016-05-24 20:54:40 -0700497 }
498
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100499 ReportConditionalErrorAndExit(settings.use_aec && !(*settings.use_aec) &&
500 settings.linear_aec_output_filename,
501 "Error: The linear AEC ouput filename cannot "
502 "be specified without the AEC being active");
503
504 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700505 settings.use_aec && *settings.use_aec && settings.use_aecm &&
506 *settings.use_aecm,
507 "Error: The AEC and the AECM cannot be activated at the same time!\n");
508
509 ReportConditionalErrorAndExit(
510 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
511 "Error: --output_sample_rate_hz must be positive!\n");
512
513 ReportConditionalErrorAndExit(
514 settings.reverse_output_sample_rate_hz &&
515 settings.output_sample_rate_hz &&
516 *settings.output_sample_rate_hz <= 0,
517 "Error: --reverse_output_sample_rate_hz must be positive!\n");
518
519 ReportConditionalErrorAndExit(
520 settings.output_num_channels && *settings.output_num_channels <= 0,
521 "Error: --output_num_channels must be positive!\n");
522
523 ReportConditionalErrorAndExit(
524 settings.reverse_output_num_channels &&
525 *settings.reverse_output_num_channels <= 0,
526 "Error: --reverse_output_num_channels must be positive!\n");
527
peah60a189f2016-05-24 20:54:40 -0700528 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700529 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
530 (*settings.agc_target_level) > 31),
531 "Error: --agc_target_level must be specified between 0 and 31.\n");
532
533 ReportConditionalErrorAndExit(
534 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
535 (*settings.agc_compression_gain) > 90),
536 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
537
538 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200539 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
540 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200541 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
542
543 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700544 settings.ns_level &&
545 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
546 "Error: --ns_level must be specified between 0 and 3.\n");
547
548 ReportConditionalErrorAndExit(
549 settings.report_bitexactness && !settings.aec_dump_input_filename,
550 "Error: --bitexactness_report can only be used when operating on an "
551 "aecdump\n");
552
peah5ad5de32016-12-09 03:18:22 -0800553 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100554 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800555 "Error: --custom_call_order_file cannot be used when operating on an "
556 "aecdump\n");
557
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200558 ReportConditionalErrorAndExit(
559 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
560 "Error: --initial_mic_level must be specified between 0 and 255.\n");
561
562 ReportConditionalErrorAndExit(
563 settings.simulated_mic_kind && !settings.simulate_mic_gain,
564 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
565 "disabled\n");
566
567 ReportConditionalErrorAndExit(
568 !settings.simulated_mic_kind && settings.simulate_mic_gain,
569 "Error: --simulated_mic_kind must be specified when mic simulation is "
570 "enabled\n");
571
peah60a189f2016-05-24 20:54:40 -0700572 auto valid_wav_name = [](const std::string& wav_file_name) {
573 if (wav_file_name.size() < 5) {
574 return false;
575 }
576 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
577 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
578 return true;
579 }
580 return false;
581 };
582
583 ReportConditionalErrorAndExit(
584 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
585 "Error: --i must be a valid .wav file name.\n");
586
587 ReportConditionalErrorAndExit(
588 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
589 "Error: --o must be a valid .wav file name.\n");
590
591 ReportConditionalErrorAndExit(
592 settings.reverse_input_filename &&
593 (!valid_wav_name(*settings.reverse_input_filename)),
594 "Error: --ri must be a valid .wav file name.\n");
595
596 ReportConditionalErrorAndExit(
597 settings.reverse_output_filename &&
598 (!valid_wav_name(*settings.reverse_output_filename)),
599 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800600
601 ReportConditionalErrorAndExit(
602 settings.artificial_nearend_filename &&
603 !valid_wav_name(*settings.artificial_nearend_filename),
604 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200605
606 ReportConditionalErrorAndExit(
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100607 settings.linear_aec_output_filename &&
608 (!valid_wav_name(*settings.linear_aec_output_filename)),
609 "Error: --linear_aec_output must be a valid .wav file name.\n");
610
611 ReportConditionalErrorAndExit(
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200612 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
613 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100614
615 ReportConditionalErrorAndExit(
616 !settings.dump_internal_data &&
617 settings.dump_internal_data_output_dir.has_value(),
618 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100619
620 ReportConditionalErrorAndExit(
621 !settings.aec_dump_input_filename &&
622 settings.call_order_output_filename.has_value(),
623 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200624
625 ReportConditionalErrorAndExit(
626 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
627 settings.pre_amplifier_gain_factor.has_value(),
628 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
629 "specified and set.\n");
Per Åhgrene9cd6172020-05-19 12:52:08 +0200630
631 ReportConditionalErrorAndExit(
632 pre_constructed_ap_provided && pre_constructed_ap_builder_provided,
633 "Error: The AudioProcessing and the AudioProcessingBuilder cannot both "
634 "be specified at the same time.\n");
635
636 ReportConditionalErrorAndExit(
637 settings.aec_settings_filename && pre_constructed_ap_provided,
638 "Error: The aec_settings_filename cannot be specified when a "
639 "pre-constructed audio processing object is provided.\n");
640
641 ReportConditionalErrorAndExit(
642 settings.aec_settings_filename && pre_constructed_ap_provided,
643 "Error: The print_aec_parameter_values cannot be set when a "
644 "pre-constructed audio processing object is provided.\n");
645
646 if (settings.linear_aec_output_filename && pre_constructed_ap_provided) {
647 std::cout << "Warning: For the linear AEC output to be stored, this must "
648 "be configured in the AEC that is part of the provided "
649 "AudioProcessing object."
650 << std::endl;
651 }
Peter Kasting69558702016-01-12 16:26:35 -0800652}
653
Per Åhgrene9cd6172020-05-19 12:52:08 +0200654int RunSimulation(rtc::scoped_refptr<AudioProcessing> audio_processing,
655 std::unique_ptr<AudioProcessingBuilder> ap_builder,
656 int argc,
657 char* argv[],
658 absl::string_view input_aecdump,
659 std::vector<float>* processed_capture_samples) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200660 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
661 if (args.size() != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700662 printf("%s", kUsageDescription);
oprypin6e09d872017-08-31 03:21:39 -0700663 return 1;
664 }
saza0cf99ce2020-04-01 17:42:01 +0200665 // InitFieldTrialsFromString stores the char*, so the char array must
666 // outlive the application.
667 const std::string field_trials = absl::GetFlag(FLAGS_force_fieldtrials);
668 webrtc::field_trial::InitFieldTrialsFromString(field_trials.c_str());
669
peah60a189f2016-05-24 20:54:40 -0700670 SimulationSettings settings = CreateSettings();
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200671 if (!input_aecdump.empty()) {
672 settings.aec_dump_input_string = input_aecdump;
673 settings.processed_capture_samples = processed_capture_samples;
674 RTC_CHECK(settings.processed_capture_samples);
675 }
Per Åhgrene9cd6172020-05-19 12:52:08 +0200676 PerformBasicParameterSanityChecks(settings, !!audio_processing, !!ap_builder);
peah60a189f2016-05-24 20:54:40 -0700677 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000678
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200679 if (settings.aec_dump_input_filename || settings.aec_dump_input_string) {
Per Åhgrene9cd6172020-05-19 12:52:08 +0200680 processor.reset(new AecDumpBasedSimulator(
681 settings, std::move(audio_processing), std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800682 } else {
Per Åhgrene9cd6172020-05-19 12:52:08 +0200683 processor.reset(new WavBasedSimulator(settings, std::move(audio_processing),
684 std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700685 }
686
peah60a189f2016-05-24 20:54:40 -0700687 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800688
peah60a189f2016-05-24 20:54:40 -0700689 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200690 processor->GetApiCallStatistics().PrintReport();
691 }
692 if (settings.performance_report_output_filename) {
693 processor->GetApiCallStatistics().WriteReportToFile(
694 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700695 }
696
697 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
698 if (processor->OutputWasBitexact()) {
699 std::cout << "The processing was bitexact.";
700 } else {
701 std::cout << "The processing was not bitexact.";
702 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700703 }
aluebsb0ad43b2015-11-20 00:11:53 -0800704
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000705 return 0;
706}
707
Per Åhgrene9cd6172020-05-19 12:52:08 +0200708} // namespace
709
710int AudioprocFloatImpl(rtc::scoped_refptr<AudioProcessing> audio_processing,
711 int argc,
712 char* argv[]) {
713 return RunSimulation(
714 std::move(audio_processing), /*ap_builder=*/nullptr, argc, argv,
715 /*input_aecdump=*/"", /*processed_capture_samples=*/nullptr);
716}
717
718int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
719 int argc,
720 char* argv[],
721 absl::string_view input_aecdump,
722 std::vector<float>* processed_capture_samples) {
723 return RunSimulation(/*audio_processing=*/nullptr, std::move(ap_builder),
724 argc, argv, input_aecdump, processed_capture_samples);
725}
726
peah60a189f2016-05-24 20:54:40 -0700727} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000728} // namespace webrtc