blob: c9abd0ad6cf7e33717369816473440ad8a86715b [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");
Per Åhgren34fdc922021-03-10 09:51:52 +0000204ABSL_FLAG(int,
205 frame_for_sending_capture_output_used_false,
206 kParameterNotSpecifiedValue,
207 "Capture frame index for sending a runtime setting for that the "
208 "capture output is not used.");
209ABSL_FLAG(int,
210 frame_for_sending_capture_output_used_true,
211 kParameterNotSpecifiedValue,
212 "Capture frame index for sending a runtime setting for that the "
213 "capture output is used.");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200214ABSL_FLAG(bool, performance_report, false, "Report the APM performance ");
215ABSL_FLAG(std::string,
216 performance_report_output_file,
217 "",
218 "Generate a CSV file with the API call durations");
219ABSL_FLAG(bool, verbose, false, "Produce verbose output");
220ABSL_FLAG(bool,
221 quiet,
222 false,
223 "Avoid producing information about the progress.");
224ABSL_FLAG(bool,
225 bitexactness_report,
226 false,
227 "Report bitexactness for aec dump result reproduction");
228ABSL_FLAG(bool,
229 discard_settings_in_aecdump,
230 false,
231 "Discard any config settings specified in the aec dump");
232ABSL_FLAG(bool,
233 store_intermediate_output,
234 false,
235 "Creates new output files after each init");
236ABSL_FLAG(std::string,
237 custom_call_order_file,
238 "",
239 "Custom process API call order file");
240ABSL_FLAG(std::string,
241 output_custom_call_order_file,
242 "",
243 "Generate custom process API call order file from AEC dump");
244ABSL_FLAG(bool,
245 print_aec_parameter_values,
246 false,
247 "Print parameter values used in AEC in JSON-format");
248ABSL_FLAG(std::string,
249 aec_settings,
250 "",
251 "File in JSON-format with custom AEC settings");
252ABSL_FLAG(bool,
253 dump_data,
254 false,
255 "Dump internal data during the call (requires build flag)");
256ABSL_FLAG(std::string,
257 dump_data_output_dir,
258 "",
259 "Internal data dump output directory");
Per Åhgrenc2ae4c82021-01-20 15:42:13 +0100260ABSL_FLAG(int,
261 dump_set_to_use,
262 kParameterNotSpecifiedValue,
263 "Specifies the dump set to use (if not all the dump sets will "
264 "be used");
Per Åhgren5dca3f12020-01-28 09:08:11 +0100265ABSL_FLAG(bool,
Per Åhgren879d33b2021-01-21 10:08:15 +0100266 analyze,
267 false,
268 "Only analyze the call setup behavior (no processing)");
269ABSL_FLAG(float,
270 dump_start_seconds,
271 kParameterNotSpecifiedValue,
272 "Start of when to dump data (seconds).");
273ABSL_FLAG(float,
274 dump_end_seconds,
275 kParameterNotSpecifiedValue,
276 "End of when to dump data (seconds).");
277ABSL_FLAG(int,
278 dump_start_frame,
279 kParameterNotSpecifiedValue,
280 "Start of when to dump data (frames).");
281ABSL_FLAG(int,
282 dump_end_frame,
283 kParameterNotSpecifiedValue,
284 "End of when to dump data (frames).");
285ABSL_FLAG(int,
286 init_to_process,
287 kParameterNotSpecifiedValue,
288 "Init index to process.");
289
290ABSL_FLAG(bool,
Per Åhgren5dca3f12020-01-28 09:08:11 +0100291 float_wav_output,
292 false,
293 "Produce floating point wav output files.");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200294
saza0cf99ce2020-04-01 17:42:01 +0200295ABSL_FLAG(std::string,
296 force_fieldtrials,
297 "",
298 "Field trials control experimental feature code which can be forced. "
299 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
300 " will assign the group Enable to field trial WebRTC-FooFeature.");
301
peah60a189f2016-05-24 20:54:40 -0700302namespace webrtc {
303namespace test {
Peter Kasting69558702016-01-12 16:26:35 -0800304namespace {
305
peah60a189f2016-05-24 20:54:40 -0700306const char kUsageDescription[] =
307 "Usage: audioproc_f [options] -i <input.wav>\n"
308 " or\n"
309 " audioproc_f [options] -dump_input <aec_dump>\n"
310 "\n\n"
311 "Command-line tool to simulate a call using the audio "
312 "processing module, either based on wav files or "
oprypin6e09d872017-08-31 03:21:39 -0700313 "protobuf debug dump recordings.\n";
peah60a189f2016-05-24 20:54:40 -0700314
Alessio Bazzica68170382018-11-20 12:27:20 +0100315std::vector<std::string> GetAgc2AdaptiveLevelEstimatorNames() {
316 return {"RMS", "peak"};
317}
peah60a189f2016-05-24 20:54:40 -0700318
Alex Loiko890988c2017-08-31 10:25:48 +0200319void SetSettingIfSpecified(const std::string& value,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200320 absl::optional<std::string>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700321 if (value.compare("") != 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100322 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700323 }
324}
325
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200326void SetSettingIfSpecified(int value, absl::optional<int>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700327 if (value != kParameterNotSpecifiedValue) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100328 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700329 }
330}
331
Per Åhgrenef349602019-04-12 16:26:34 +0200332void SetSettingIfSpecified(float value, absl::optional<float>* parameter) {
333 constexpr float kFloatParameterNotSpecifiedValue =
334 kParameterNotSpecifiedValue;
335 if (value != kFloatParameterNotSpecifiedValue) {
336 *parameter = value;
337 }
338}
339
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200340void SetSettingIfFlagSet(int32_t flag, absl::optional<bool>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700341 if (flag == 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100342 *parameter = false;
peah60a189f2016-05-24 20:54:40 -0700343 } else if (flag == 1) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100344 *parameter = true;
peah60a189f2016-05-24 20:54:40 -0700345 }
346}
347
Alessio Bazzica68170382018-11-20 12:27:20 +0100348AudioProcessing::Config::GainController2::LevelEstimator
349MapAgc2AdaptiveLevelEstimator(absl::string_view name) {
350 if (name.compare("RMS") == 0) {
351 return AudioProcessing::Config::GainController2::LevelEstimator::kRms;
352 }
353 if (name.compare("peak") == 0) {
354 return AudioProcessing::Config::GainController2::LevelEstimator::kPeak;
355 }
356 auto concat_strings =
357 [](const std::vector<std::string>& strings) -> std::string {
358 rtc::StringBuilder ss;
359 for (const auto& s : strings) {
360 ss << " " << s;
361 }
362 return ss.Release();
363 };
364 RTC_CHECK(false)
365 << "Invalid value for agc2_adaptive_level_estimator, valid options:"
366 << concat_strings(GetAgc2AdaptiveLevelEstimatorNames()) << ".";
367}
368
peah60a189f2016-05-24 20:54:40 -0700369SimulationSettings CreateSettings() {
370 SimulationSettings settings;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200371 if (absl::GetFlag(FLAGS_all_default)) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100372 settings.use_le = true;
373 settings.use_vad = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100374 settings.use_ts = true;
Per Åhgren0695df12020-01-13 14:43:13 +0100375 settings.use_analog_agc = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100376 settings.use_ns = true;
377 settings.use_hpf = true;
378 settings.use_agc = true;
379 settings.use_agc2 = false;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200380 settings.use_pre_amplifier = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100381 settings.use_aec = true;
382 settings.use_aecm = false;
383 settings.use_ed = false;
peah60a189f2016-05-24 20:54:40 -0700384 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200385 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_input),
386 &settings.aec_dump_input_filename);
387 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_output),
388 &settings.aec_dump_output_filename);
389 SetSettingIfSpecified(absl::GetFlag(FLAGS_i), &settings.input_filename);
390 SetSettingIfSpecified(absl::GetFlag(FLAGS_o), &settings.output_filename);
391 SetSettingIfSpecified(absl::GetFlag(FLAGS_ri),
392 &settings.reverse_input_filename);
393 SetSettingIfSpecified(absl::GetFlag(FLAGS_ro),
394 &settings.reverse_output_filename);
395 SetSettingIfSpecified(absl::GetFlag(FLAGS_artificial_nearend),
peahdf80fd12016-12-09 02:43:40 -0800396 &settings.artificial_nearend_filename);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100397 SetSettingIfSpecified(absl::GetFlag(FLAGS_linear_aec_output),
398 &settings.linear_aec_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200399 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700400 &settings.output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200401 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700402 &settings.reverse_output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200403 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700404 &settings.output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200405 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700406 &settings.reverse_output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200407 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aec), &settings.use_aec);
408 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aecm), &settings.use_aecm);
409 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ed), &settings.use_ed);
410 SetSettingIfSpecified(absl::GetFlag(FLAGS_ed_graph),
411 &settings.ed_graph_output_filename);
412 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc), &settings.use_agc);
413 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2), &settings.use_agc2);
414 SetSettingIfFlagSet(absl::GetFlag(FLAGS_pre_amplifier),
415 &settings.use_pre_amplifier);
416 SetSettingIfFlagSet(absl::GetFlag(FLAGS_hpf), &settings.use_hpf);
417 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns), &settings.use_ns);
418 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ts), &settings.use_ts);
Per Åhgren0695df12020-01-13 14:43:13 +0100419 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc),
420 &settings.use_analog_agc);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200421 SetSettingIfFlagSet(absl::GetFlag(FLAGS_vad), &settings.use_vad);
422 SetSettingIfFlagSet(absl::GetFlag(FLAGS_le), &settings.use_le);
Per Åhgren0695df12020-01-13 14:43:13 +0100423 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_disable_digital_adaptive),
424 &settings.analog_agc_disable_digital_adaptive);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200425 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_mode), &settings.agc_mode);
426 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_target_level),
427 &settings.agc_target_level);
428 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc_limiter),
429 &settings.use_agc_limiter);
430 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_compression_gain),
peah60a189f2016-05-24 20:54:40 -0700431 &settings.agc_compression_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200432 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2_enable_adaptive_gain),
Per Åhgrene4d23b12018-10-02 23:40:07 +0200433 &settings.agc2_use_adaptive_gain);
Per Åhgren879d33b2021-01-21 10:08:15 +0100434
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200435 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc2_fixed_gain_db),
436 &settings.agc2_fixed_gain_db);
437 settings.agc2_adaptive_level_estimator = MapAgc2AdaptiveLevelEstimator(
438 absl::GetFlag(FLAGS_agc2_adaptive_level_estimator));
439 SetSettingIfSpecified(absl::GetFlag(FLAGS_pre_amplifier_gain_factor),
Per Åhgrenef349602019-04-12 16:26:34 +0200440 &settings.pre_amplifier_gain_factor);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200441 SetSettingIfSpecified(absl::GetFlag(FLAGS_ns_level), &settings.ns_level);
Per Åhgren2e8e1c62019-12-20 00:42:22 +0100442 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns_analysis_on_linear_aec_output),
443 &settings.ns_analysis_on_linear_aec_output);
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200444 SetSettingIfSpecified(absl::GetFlag(FLAGS_maximum_internal_processing_rate),
445 &settings.maximum_internal_processing_rate);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200446 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_delay),
447 &settings.stream_delay);
448 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_stream_delay),
449 &settings.use_stream_delay);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200450 SetSettingIfSpecified(absl::GetFlag(FLAGS_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100451 &settings.call_order_input_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200452 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100453 &settings.call_order_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200454 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_settings),
455 &settings.aec_settings_filename);
456 settings.initial_mic_level = absl::GetFlag(FLAGS_initial_mic_level);
Per Åhgrene14cb992019-11-27 09:34:22 +0100457 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_render),
458 &settings.multi_channel_render);
459 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_capture),
460 &settings.multi_channel_capture);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200461 settings.simulate_mic_gain = absl::GetFlag(FLAGS_simulate_mic_gain);
462 SetSettingIfSpecified(absl::GetFlag(FLAGS_simulated_mic_kind),
463 &settings.simulated_mic_kind);
Per Åhgren34fdc922021-03-10 09:51:52 +0000464 SetSettingIfSpecified(
465 absl::GetFlag(FLAGS_frame_for_sending_capture_output_used_false),
466 &settings.frame_for_sending_capture_output_used_false);
467 SetSettingIfSpecified(
468 absl::GetFlag(FLAGS_frame_for_sending_capture_output_used_true),
469 &settings.frame_for_sending_capture_output_used_true);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200470 settings.report_performance = absl::GetFlag(FLAGS_performance_report);
471 SetSettingIfSpecified(absl::GetFlag(FLAGS_performance_report_output_file),
Per Åhgrenada9b892019-04-03 16:06:42 +0200472 &settings.performance_report_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200473 settings.use_verbose_logging = absl::GetFlag(FLAGS_verbose);
474 settings.use_quiet_output = absl::GetFlag(FLAGS_quiet);
475 settings.report_bitexactness = absl::GetFlag(FLAGS_bitexactness_report);
476 settings.discard_all_settings_in_aecdump =
477 absl::GetFlag(FLAGS_discard_settings_in_aecdump);
478 settings.fixed_interface = absl::GetFlag(FLAGS_fixed_interface);
479 settings.store_intermediate_output =
480 absl::GetFlag(FLAGS_store_intermediate_output);
481 settings.print_aec_parameter_values =
482 absl::GetFlag(FLAGS_print_aec_parameter_values);
483 settings.dump_internal_data = absl::GetFlag(FLAGS_dump_data);
484 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_data_output_dir),
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100485 &settings.dump_internal_data_output_dir);
Per Åhgrenc2ae4c82021-01-20 15:42:13 +0100486 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_set_to_use),
487 &settings.dump_set_to_use);
Per Åhgren5dca3f12020-01-28 09:08:11 +0100488 settings.wav_output_format = absl::GetFlag(FLAGS_float_wav_output)
489 ? WavFile::SampleFormat::kFloat
490 : WavFile::SampleFormat::kInt16;
peah60a189f2016-05-24 20:54:40 -0700491
Per Åhgren879d33b2021-01-21 10:08:15 +0100492 settings.analysis_only = absl::GetFlag(FLAGS_analyze);
493
494 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_start_frame),
495 &settings.dump_start_frame);
496 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_end_frame),
497 &settings.dump_end_frame);
498
499 constexpr int kFramesPerSecond = 100;
500 absl::optional<float> start_seconds;
501 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_start_seconds),
502 &start_seconds);
503 if (start_seconds) {
504 settings.dump_start_frame = *start_seconds * kFramesPerSecond;
505 }
506
507 absl::optional<float> end_seconds;
508 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_end_seconds), &end_seconds);
509 if (end_seconds) {
510 settings.dump_end_frame = *end_seconds * kFramesPerSecond;
511 }
512
513 SetSettingIfSpecified(absl::GetFlag(FLAGS_init_to_process),
514 &settings.init_to_process);
515
peah60a189f2016-05-24 20:54:40 -0700516 return settings;
517}
518
Alex Loiko890988c2017-08-31 10:25:48 +0200519void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
peah60a189f2016-05-24 20:54:40 -0700520 if (condition) {
521 std::cerr << message << std::endl;
522 exit(1);
523 }
524}
525
Per Åhgrene9cd6172020-05-19 12:52:08 +0200526void PerformBasicParameterSanityChecks(
527 const SimulationSettings& settings,
528 bool pre_constructed_ap_provided,
529 bool pre_constructed_ap_builder_provided) {
peah60a189f2016-05-24 20:54:40 -0700530 if (settings.input_filename || settings.reverse_input_filename) {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200531 ReportConditionalErrorAndExit(
532 !!settings.aec_dump_input_filename,
533 "Error: The aec dump file cannot be specified "
534 "together with input wav files!\n");
535
536 ReportConditionalErrorAndExit(
537 !!settings.aec_dump_input_string,
538 "Error: The aec dump input string cannot be specified "
539 "together with input wav files!\n");
peah60a189f2016-05-24 20:54:40 -0700540
peahdf80fd12016-12-09 02:43:40 -0800541 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
542 "Error: The artificial nearend cannot be "
543 "specified together with input wav files!\n");
544
peah60a189f2016-05-24 20:54:40 -0700545 ReportConditionalErrorAndExit(!settings.input_filename,
546 "Error: When operating at wav files, the "
547 "input wav filename must be "
548 "specified!\n");
549
550 ReportConditionalErrorAndExit(
551 settings.reverse_output_filename && !settings.reverse_input_filename,
552 "Error: When operating at wav files, the reverse input wav filename "
553 "must be specified if the reverse output wav filename is specified!\n");
554 } else {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200555 ReportConditionalErrorAndExit(
556 !settings.aec_dump_input_filename && !settings.aec_dump_input_string,
557 "Error: Either the aec dump input file, the wav "
558 "input file or the aec dump input string must be specified!\n");
559 ReportConditionalErrorAndExit(
560 settings.aec_dump_input_filename && settings.aec_dump_input_string,
561 "Error: The aec dump input file cannot be specified together with the "
562 "aec dump input string!\n");
peah60a189f2016-05-24 20:54:40 -0700563 }
564
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100565 ReportConditionalErrorAndExit(settings.use_aec && !(*settings.use_aec) &&
566 settings.linear_aec_output_filename,
567 "Error: The linear AEC ouput filename cannot "
568 "be specified without the AEC being active");
569
570 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700571 settings.use_aec && *settings.use_aec && settings.use_aecm &&
572 *settings.use_aecm,
573 "Error: The AEC and the AECM cannot be activated at the same time!\n");
574
575 ReportConditionalErrorAndExit(
576 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
577 "Error: --output_sample_rate_hz must be positive!\n");
578
579 ReportConditionalErrorAndExit(
580 settings.reverse_output_sample_rate_hz &&
581 settings.output_sample_rate_hz &&
582 *settings.output_sample_rate_hz <= 0,
583 "Error: --reverse_output_sample_rate_hz must be positive!\n");
584
585 ReportConditionalErrorAndExit(
586 settings.output_num_channels && *settings.output_num_channels <= 0,
587 "Error: --output_num_channels must be positive!\n");
588
589 ReportConditionalErrorAndExit(
590 settings.reverse_output_num_channels &&
591 *settings.reverse_output_num_channels <= 0,
592 "Error: --reverse_output_num_channels must be positive!\n");
593
peah60a189f2016-05-24 20:54:40 -0700594 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700595 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
596 (*settings.agc_target_level) > 31),
597 "Error: --agc_target_level must be specified between 0 and 31.\n");
598
599 ReportConditionalErrorAndExit(
600 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
601 (*settings.agc_compression_gain) > 90),
602 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
603
604 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200605 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
606 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200607 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
608
609 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700610 settings.ns_level &&
611 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
612 "Error: --ns_level must be specified between 0 and 3.\n");
613
614 ReportConditionalErrorAndExit(
615 settings.report_bitexactness && !settings.aec_dump_input_filename,
616 "Error: --bitexactness_report can only be used when operating on an "
617 "aecdump\n");
618
peah5ad5de32016-12-09 03:18:22 -0800619 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100620 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800621 "Error: --custom_call_order_file cannot be used when operating on an "
622 "aecdump\n");
623
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200624 ReportConditionalErrorAndExit(
625 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
626 "Error: --initial_mic_level must be specified between 0 and 255.\n");
627
628 ReportConditionalErrorAndExit(
629 settings.simulated_mic_kind && !settings.simulate_mic_gain,
630 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
631 "disabled\n");
632
633 ReportConditionalErrorAndExit(
634 !settings.simulated_mic_kind && settings.simulate_mic_gain,
635 "Error: --simulated_mic_kind must be specified when mic simulation is "
636 "enabled\n");
637
peah60a189f2016-05-24 20:54:40 -0700638 auto valid_wav_name = [](const std::string& wav_file_name) {
639 if (wav_file_name.size() < 5) {
640 return false;
641 }
642 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
643 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
644 return true;
645 }
646 return false;
647 };
648
649 ReportConditionalErrorAndExit(
650 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
651 "Error: --i must be a valid .wav file name.\n");
652
653 ReportConditionalErrorAndExit(
654 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
655 "Error: --o must be a valid .wav file name.\n");
656
657 ReportConditionalErrorAndExit(
658 settings.reverse_input_filename &&
659 (!valid_wav_name(*settings.reverse_input_filename)),
660 "Error: --ri must be a valid .wav file name.\n");
661
662 ReportConditionalErrorAndExit(
663 settings.reverse_output_filename &&
664 (!valid_wav_name(*settings.reverse_output_filename)),
665 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800666
667 ReportConditionalErrorAndExit(
668 settings.artificial_nearend_filename &&
669 !valid_wav_name(*settings.artificial_nearend_filename),
670 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200671
672 ReportConditionalErrorAndExit(
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100673 settings.linear_aec_output_filename &&
674 (!valid_wav_name(*settings.linear_aec_output_filename)),
675 "Error: --linear_aec_output must be a valid .wav file name.\n");
676
677 ReportConditionalErrorAndExit(
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200678 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
679 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100680
Per Åhgren879d33b2021-01-21 10:08:15 +0100681 ReportConditionalErrorAndExit(settings.init_to_process &&
682 *settings.init_to_process != 1 &&
683 !settings.aec_dump_input_filename,
684 "Error: --init_to_process must be set to 1 for "
685 "wav-file based simulations.\n");
686
687 ReportConditionalErrorAndExit(
688 !settings.init_to_process &&
689 (settings.dump_start_frame || settings.dump_end_frame),
690 "Error: --init_to_process must be set when specifying a start and/or end "
691 "frame for when to dump internal data.\n");
692
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100693 ReportConditionalErrorAndExit(
694 !settings.dump_internal_data &&
695 settings.dump_internal_data_output_dir.has_value(),
696 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100697
698 ReportConditionalErrorAndExit(
699 !settings.aec_dump_input_filename &&
700 settings.call_order_output_filename.has_value(),
701 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200702
703 ReportConditionalErrorAndExit(
704 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
705 settings.pre_amplifier_gain_factor.has_value(),
706 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
707 "specified and set.\n");
Per Åhgrene9cd6172020-05-19 12:52:08 +0200708
709 ReportConditionalErrorAndExit(
710 pre_constructed_ap_provided && pre_constructed_ap_builder_provided,
711 "Error: The AudioProcessing and the AudioProcessingBuilder cannot both "
712 "be specified at the same time.\n");
713
714 ReportConditionalErrorAndExit(
715 settings.aec_settings_filename && pre_constructed_ap_provided,
716 "Error: The aec_settings_filename cannot be specified when a "
717 "pre-constructed audio processing object is provided.\n");
718
719 ReportConditionalErrorAndExit(
720 settings.aec_settings_filename && pre_constructed_ap_provided,
721 "Error: The print_aec_parameter_values cannot be set when a "
722 "pre-constructed audio processing object is provided.\n");
723
724 if (settings.linear_aec_output_filename && pre_constructed_ap_provided) {
725 std::cout << "Warning: For the linear AEC output to be stored, this must "
726 "be configured in the AEC that is part of the provided "
727 "AudioProcessing object."
728 << std::endl;
729 }
Peter Kasting69558702016-01-12 16:26:35 -0800730}
731
Per Åhgrene9cd6172020-05-19 12:52:08 +0200732int RunSimulation(rtc::scoped_refptr<AudioProcessing> audio_processing,
733 std::unique_ptr<AudioProcessingBuilder> ap_builder,
734 int argc,
735 char* argv[],
736 absl::string_view input_aecdump,
737 std::vector<float>* processed_capture_samples) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200738 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
739 if (args.size() != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700740 printf("%s", kUsageDescription);
oprypin6e09d872017-08-31 03:21:39 -0700741 return 1;
742 }
saza0cf99ce2020-04-01 17:42:01 +0200743 // InitFieldTrialsFromString stores the char*, so the char array must
744 // outlive the application.
745 const std::string field_trials = absl::GetFlag(FLAGS_force_fieldtrials);
746 webrtc::field_trial::InitFieldTrialsFromString(field_trials.c_str());
747
peah60a189f2016-05-24 20:54:40 -0700748 SimulationSettings settings = CreateSettings();
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200749 if (!input_aecdump.empty()) {
750 settings.aec_dump_input_string = input_aecdump;
751 settings.processed_capture_samples = processed_capture_samples;
752 RTC_CHECK(settings.processed_capture_samples);
753 }
Per Åhgrene9cd6172020-05-19 12:52:08 +0200754 PerformBasicParameterSanityChecks(settings, !!audio_processing, !!ap_builder);
peah60a189f2016-05-24 20:54:40 -0700755 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000756
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200757 if (settings.aec_dump_input_filename || settings.aec_dump_input_string) {
Per Åhgrene9cd6172020-05-19 12:52:08 +0200758 processor.reset(new AecDumpBasedSimulator(
759 settings, std::move(audio_processing), std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800760 } else {
Per Åhgrene9cd6172020-05-19 12:52:08 +0200761 processor.reset(new WavBasedSimulator(settings, std::move(audio_processing),
762 std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700763 }
764
Per Åhgren879d33b2021-01-21 10:08:15 +0100765 if (settings.analysis_only) {
766 processor->Analyze();
767 } else {
768 processor->Process();
769 }
aluebsb0ad43b2015-11-20 00:11:53 -0800770
peah60a189f2016-05-24 20:54:40 -0700771 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200772 processor->GetApiCallStatistics().PrintReport();
773 }
774 if (settings.performance_report_output_filename) {
775 processor->GetApiCallStatistics().WriteReportToFile(
776 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700777 }
778
779 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
780 if (processor->OutputWasBitexact()) {
781 std::cout << "The processing was bitexact.";
782 } else {
783 std::cout << "The processing was not bitexact.";
784 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700785 }
aluebsb0ad43b2015-11-20 00:11:53 -0800786
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000787 return 0;
788}
789
Per Åhgrene9cd6172020-05-19 12:52:08 +0200790} // namespace
791
792int AudioprocFloatImpl(rtc::scoped_refptr<AudioProcessing> audio_processing,
793 int argc,
794 char* argv[]) {
795 return RunSimulation(
796 std::move(audio_processing), /*ap_builder=*/nullptr, argc, argv,
797 /*input_aecdump=*/"", /*processed_capture_samples=*/nullptr);
798}
799
800int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
801 int argc,
802 char* argv[],
803 absl::string_view input_aecdump,
804 std::vector<float>* processed_capture_samples) {
805 return RunSimulation(/*audio_processing=*/nullptr, std::move(ap_builder),
806 argc, argv, input_aecdump, processed_capture_samples);
807}
808
peah60a189f2016-05-24 20:54:40 -0700809} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000810} // namespace webrtc