blob: 767c93ddd8fdf728134e8edd14aef03a50b3cf8e [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,
Per Åhgren879d33b2021-01-21 10:08:15 +0100256 analyze,
257 false,
258 "Only analyze the call setup behavior (no processing)");
259ABSL_FLAG(float,
260 dump_start_seconds,
261 kParameterNotSpecifiedValue,
262 "Start of when to dump data (seconds).");
263ABSL_FLAG(float,
264 dump_end_seconds,
265 kParameterNotSpecifiedValue,
266 "End of when to dump data (seconds).");
267ABSL_FLAG(int,
268 dump_start_frame,
269 kParameterNotSpecifiedValue,
270 "Start of when to dump data (frames).");
271ABSL_FLAG(int,
272 dump_end_frame,
273 kParameterNotSpecifiedValue,
274 "End of when to dump data (frames).");
275ABSL_FLAG(int,
276 init_to_process,
277 kParameterNotSpecifiedValue,
278 "Init index to process.");
279
280ABSL_FLAG(bool,
Per Åhgren5dca3f12020-01-28 09:08:11 +0100281 float_wav_output,
282 false,
283 "Produce floating point wav output files.");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200284
saza0cf99ce2020-04-01 17:42:01 +0200285ABSL_FLAG(std::string,
286 force_fieldtrials,
287 "",
288 "Field trials control experimental feature code which can be forced. "
289 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
290 " will assign the group Enable to field trial WebRTC-FooFeature.");
291
peah60a189f2016-05-24 20:54:40 -0700292namespace webrtc {
293namespace test {
Peter Kasting69558702016-01-12 16:26:35 -0800294namespace {
295
peah60a189f2016-05-24 20:54:40 -0700296const char kUsageDescription[] =
297 "Usage: audioproc_f [options] -i <input.wav>\n"
298 " or\n"
299 " audioproc_f [options] -dump_input <aec_dump>\n"
300 "\n\n"
301 "Command-line tool to simulate a call using the audio "
302 "processing module, either based on wav files or "
oprypin6e09d872017-08-31 03:21:39 -0700303 "protobuf debug dump recordings.\n";
peah60a189f2016-05-24 20:54:40 -0700304
Alessio Bazzica68170382018-11-20 12:27:20 +0100305std::vector<std::string> GetAgc2AdaptiveLevelEstimatorNames() {
306 return {"RMS", "peak"};
307}
peah60a189f2016-05-24 20:54:40 -0700308
Alex Loiko890988c2017-08-31 10:25:48 +0200309void SetSettingIfSpecified(const std::string& value,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200310 absl::optional<std::string>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700311 if (value.compare("") != 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100312 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700313 }
314}
315
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200316void SetSettingIfSpecified(int value, absl::optional<int>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700317 if (value != kParameterNotSpecifiedValue) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100318 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700319 }
320}
321
Per Åhgrenef349602019-04-12 16:26:34 +0200322void SetSettingIfSpecified(float value, absl::optional<float>* parameter) {
323 constexpr float kFloatParameterNotSpecifiedValue =
324 kParameterNotSpecifiedValue;
325 if (value != kFloatParameterNotSpecifiedValue) {
326 *parameter = value;
327 }
328}
329
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200330void SetSettingIfFlagSet(int32_t flag, absl::optional<bool>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700331 if (flag == 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100332 *parameter = false;
peah60a189f2016-05-24 20:54:40 -0700333 } else if (flag == 1) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100334 *parameter = true;
peah60a189f2016-05-24 20:54:40 -0700335 }
336}
337
Alessio Bazzica68170382018-11-20 12:27:20 +0100338AudioProcessing::Config::GainController2::LevelEstimator
339MapAgc2AdaptiveLevelEstimator(absl::string_view name) {
340 if (name.compare("RMS") == 0) {
341 return AudioProcessing::Config::GainController2::LevelEstimator::kRms;
342 }
343 if (name.compare("peak") == 0) {
344 return AudioProcessing::Config::GainController2::LevelEstimator::kPeak;
345 }
346 auto concat_strings =
347 [](const std::vector<std::string>& strings) -> std::string {
348 rtc::StringBuilder ss;
349 for (const auto& s : strings) {
350 ss << " " << s;
351 }
352 return ss.Release();
353 };
354 RTC_CHECK(false)
355 << "Invalid value for agc2_adaptive_level_estimator, valid options:"
356 << concat_strings(GetAgc2AdaptiveLevelEstimatorNames()) << ".";
357}
358
peah60a189f2016-05-24 20:54:40 -0700359SimulationSettings CreateSettings() {
360 SimulationSettings settings;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200361 if (absl::GetFlag(FLAGS_all_default)) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100362 settings.use_le = true;
363 settings.use_vad = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100364 settings.use_ts = true;
Per Åhgren0695df12020-01-13 14:43:13 +0100365 settings.use_analog_agc = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100366 settings.use_ns = true;
367 settings.use_hpf = true;
368 settings.use_agc = true;
369 settings.use_agc2 = false;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200370 settings.use_pre_amplifier = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100371 settings.use_aec = true;
372 settings.use_aecm = false;
373 settings.use_ed = false;
peah60a189f2016-05-24 20:54:40 -0700374 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200375 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_input),
376 &settings.aec_dump_input_filename);
377 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_output),
378 &settings.aec_dump_output_filename);
379 SetSettingIfSpecified(absl::GetFlag(FLAGS_i), &settings.input_filename);
380 SetSettingIfSpecified(absl::GetFlag(FLAGS_o), &settings.output_filename);
381 SetSettingIfSpecified(absl::GetFlag(FLAGS_ri),
382 &settings.reverse_input_filename);
383 SetSettingIfSpecified(absl::GetFlag(FLAGS_ro),
384 &settings.reverse_output_filename);
385 SetSettingIfSpecified(absl::GetFlag(FLAGS_artificial_nearend),
peahdf80fd12016-12-09 02:43:40 -0800386 &settings.artificial_nearend_filename);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100387 SetSettingIfSpecified(absl::GetFlag(FLAGS_linear_aec_output),
388 &settings.linear_aec_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200389 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700390 &settings.output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200391 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700392 &settings.reverse_output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200393 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700394 &settings.output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200395 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700396 &settings.reverse_output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200397 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aec), &settings.use_aec);
398 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aecm), &settings.use_aecm);
399 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ed), &settings.use_ed);
400 SetSettingIfSpecified(absl::GetFlag(FLAGS_ed_graph),
401 &settings.ed_graph_output_filename);
402 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc), &settings.use_agc);
403 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2), &settings.use_agc2);
404 SetSettingIfFlagSet(absl::GetFlag(FLAGS_pre_amplifier),
405 &settings.use_pre_amplifier);
406 SetSettingIfFlagSet(absl::GetFlag(FLAGS_hpf), &settings.use_hpf);
407 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns), &settings.use_ns);
408 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ts), &settings.use_ts);
Per Åhgren0695df12020-01-13 14:43:13 +0100409 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc),
410 &settings.use_analog_agc);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200411 SetSettingIfFlagSet(absl::GetFlag(FLAGS_vad), &settings.use_vad);
412 SetSettingIfFlagSet(absl::GetFlag(FLAGS_le), &settings.use_le);
Per Åhgren0695df12020-01-13 14:43:13 +0100413 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_disable_digital_adaptive),
414 &settings.analog_agc_disable_digital_adaptive);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200415 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_mode), &settings.agc_mode);
416 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_target_level),
417 &settings.agc_target_level);
418 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc_limiter),
419 &settings.use_agc_limiter);
420 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_compression_gain),
peah60a189f2016-05-24 20:54:40 -0700421 &settings.agc_compression_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200422 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2_enable_adaptive_gain),
Per Åhgrene4d23b12018-10-02 23:40:07 +0200423 &settings.agc2_use_adaptive_gain);
Per Åhgren879d33b2021-01-21 10:08:15 +0100424
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200425 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc2_fixed_gain_db),
426 &settings.agc2_fixed_gain_db);
427 settings.agc2_adaptive_level_estimator = MapAgc2AdaptiveLevelEstimator(
428 absl::GetFlag(FLAGS_agc2_adaptive_level_estimator));
429 SetSettingIfSpecified(absl::GetFlag(FLAGS_pre_amplifier_gain_factor),
Per Åhgrenef349602019-04-12 16:26:34 +0200430 &settings.pre_amplifier_gain_factor);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200431 SetSettingIfSpecified(absl::GetFlag(FLAGS_ns_level), &settings.ns_level);
Per Åhgren2e8e1c62019-12-20 00:42:22 +0100432 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns_analysis_on_linear_aec_output),
433 &settings.ns_analysis_on_linear_aec_output);
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200434 SetSettingIfSpecified(absl::GetFlag(FLAGS_maximum_internal_processing_rate),
435 &settings.maximum_internal_processing_rate);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200436 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_delay),
437 &settings.stream_delay);
438 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_stream_delay),
439 &settings.use_stream_delay);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200440 SetSettingIfSpecified(absl::GetFlag(FLAGS_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100441 &settings.call_order_input_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200442 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100443 &settings.call_order_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200444 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_settings),
445 &settings.aec_settings_filename);
446 settings.initial_mic_level = absl::GetFlag(FLAGS_initial_mic_level);
Per Åhgrene14cb992019-11-27 09:34:22 +0100447 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_render),
448 &settings.multi_channel_render);
449 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_capture),
450 &settings.multi_channel_capture);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200451 settings.simulate_mic_gain = absl::GetFlag(FLAGS_simulate_mic_gain);
452 SetSettingIfSpecified(absl::GetFlag(FLAGS_simulated_mic_kind),
453 &settings.simulated_mic_kind);
454 settings.report_performance = absl::GetFlag(FLAGS_performance_report);
455 SetSettingIfSpecified(absl::GetFlag(FLAGS_performance_report_output_file),
Per Åhgrenada9b892019-04-03 16:06:42 +0200456 &settings.performance_report_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200457 settings.use_verbose_logging = absl::GetFlag(FLAGS_verbose);
458 settings.use_quiet_output = absl::GetFlag(FLAGS_quiet);
459 settings.report_bitexactness = absl::GetFlag(FLAGS_bitexactness_report);
460 settings.discard_all_settings_in_aecdump =
461 absl::GetFlag(FLAGS_discard_settings_in_aecdump);
462 settings.fixed_interface = absl::GetFlag(FLAGS_fixed_interface);
463 settings.store_intermediate_output =
464 absl::GetFlag(FLAGS_store_intermediate_output);
465 settings.print_aec_parameter_values =
466 absl::GetFlag(FLAGS_print_aec_parameter_values);
467 settings.dump_internal_data = absl::GetFlag(FLAGS_dump_data);
468 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_data_output_dir),
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100469 &settings.dump_internal_data_output_dir);
Per Åhgrenc2ae4c82021-01-20 15:42:13 +0100470 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_set_to_use),
471 &settings.dump_set_to_use);
Per Åhgren5dca3f12020-01-28 09:08:11 +0100472 settings.wav_output_format = absl::GetFlag(FLAGS_float_wav_output)
473 ? WavFile::SampleFormat::kFloat
474 : WavFile::SampleFormat::kInt16;
peah60a189f2016-05-24 20:54:40 -0700475
Per Åhgren879d33b2021-01-21 10:08:15 +0100476 settings.analysis_only = absl::GetFlag(FLAGS_analyze);
477
478 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_start_frame),
479 &settings.dump_start_frame);
480 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_end_frame),
481 &settings.dump_end_frame);
482
483 constexpr int kFramesPerSecond = 100;
484 absl::optional<float> start_seconds;
485 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_start_seconds),
486 &start_seconds);
487 if (start_seconds) {
488 settings.dump_start_frame = *start_seconds * kFramesPerSecond;
489 }
490
491 absl::optional<float> end_seconds;
492 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_end_seconds), &end_seconds);
493 if (end_seconds) {
494 settings.dump_end_frame = *end_seconds * kFramesPerSecond;
495 }
496
497 SetSettingIfSpecified(absl::GetFlag(FLAGS_init_to_process),
498 &settings.init_to_process);
499
peah60a189f2016-05-24 20:54:40 -0700500 return settings;
501}
502
Alex Loiko890988c2017-08-31 10:25:48 +0200503void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
peah60a189f2016-05-24 20:54:40 -0700504 if (condition) {
505 std::cerr << message << std::endl;
506 exit(1);
507 }
508}
509
Per Åhgrene9cd6172020-05-19 12:52:08 +0200510void PerformBasicParameterSanityChecks(
511 const SimulationSettings& settings,
512 bool pre_constructed_ap_provided,
513 bool pre_constructed_ap_builder_provided) {
peah60a189f2016-05-24 20:54:40 -0700514 if (settings.input_filename || settings.reverse_input_filename) {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200515 ReportConditionalErrorAndExit(
516 !!settings.aec_dump_input_filename,
517 "Error: The aec dump file cannot be specified "
518 "together with input wav files!\n");
519
520 ReportConditionalErrorAndExit(
521 !!settings.aec_dump_input_string,
522 "Error: The aec dump input string cannot be specified "
523 "together with input wav files!\n");
peah60a189f2016-05-24 20:54:40 -0700524
peahdf80fd12016-12-09 02:43:40 -0800525 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
526 "Error: The artificial nearend cannot be "
527 "specified together with input wav files!\n");
528
peah60a189f2016-05-24 20:54:40 -0700529 ReportConditionalErrorAndExit(!settings.input_filename,
530 "Error: When operating at wav files, the "
531 "input wav filename must be "
532 "specified!\n");
533
534 ReportConditionalErrorAndExit(
535 settings.reverse_output_filename && !settings.reverse_input_filename,
536 "Error: When operating at wav files, the reverse input wav filename "
537 "must be specified if the reverse output wav filename is specified!\n");
538 } else {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200539 ReportConditionalErrorAndExit(
540 !settings.aec_dump_input_filename && !settings.aec_dump_input_string,
541 "Error: Either the aec dump input file, the wav "
542 "input file or the aec dump input string must be specified!\n");
543 ReportConditionalErrorAndExit(
544 settings.aec_dump_input_filename && settings.aec_dump_input_string,
545 "Error: The aec dump input file cannot be specified together with the "
546 "aec dump input string!\n");
peah60a189f2016-05-24 20:54:40 -0700547 }
548
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100549 ReportConditionalErrorAndExit(settings.use_aec && !(*settings.use_aec) &&
550 settings.linear_aec_output_filename,
551 "Error: The linear AEC ouput filename cannot "
552 "be specified without the AEC being active");
553
554 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700555 settings.use_aec && *settings.use_aec && settings.use_aecm &&
556 *settings.use_aecm,
557 "Error: The AEC and the AECM cannot be activated at the same time!\n");
558
559 ReportConditionalErrorAndExit(
560 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
561 "Error: --output_sample_rate_hz must be positive!\n");
562
563 ReportConditionalErrorAndExit(
564 settings.reverse_output_sample_rate_hz &&
565 settings.output_sample_rate_hz &&
566 *settings.output_sample_rate_hz <= 0,
567 "Error: --reverse_output_sample_rate_hz must be positive!\n");
568
569 ReportConditionalErrorAndExit(
570 settings.output_num_channels && *settings.output_num_channels <= 0,
571 "Error: --output_num_channels must be positive!\n");
572
573 ReportConditionalErrorAndExit(
574 settings.reverse_output_num_channels &&
575 *settings.reverse_output_num_channels <= 0,
576 "Error: --reverse_output_num_channels must be positive!\n");
577
peah60a189f2016-05-24 20:54:40 -0700578 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700579 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
580 (*settings.agc_target_level) > 31),
581 "Error: --agc_target_level must be specified between 0 and 31.\n");
582
583 ReportConditionalErrorAndExit(
584 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
585 (*settings.agc_compression_gain) > 90),
586 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
587
588 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200589 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
590 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200591 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
592
593 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700594 settings.ns_level &&
595 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
596 "Error: --ns_level must be specified between 0 and 3.\n");
597
598 ReportConditionalErrorAndExit(
599 settings.report_bitexactness && !settings.aec_dump_input_filename,
600 "Error: --bitexactness_report can only be used when operating on an "
601 "aecdump\n");
602
peah5ad5de32016-12-09 03:18:22 -0800603 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100604 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800605 "Error: --custom_call_order_file cannot be used when operating on an "
606 "aecdump\n");
607
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200608 ReportConditionalErrorAndExit(
609 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
610 "Error: --initial_mic_level must be specified between 0 and 255.\n");
611
612 ReportConditionalErrorAndExit(
613 settings.simulated_mic_kind && !settings.simulate_mic_gain,
614 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
615 "disabled\n");
616
617 ReportConditionalErrorAndExit(
618 !settings.simulated_mic_kind && settings.simulate_mic_gain,
619 "Error: --simulated_mic_kind must be specified when mic simulation is "
620 "enabled\n");
621
peah60a189f2016-05-24 20:54:40 -0700622 auto valid_wav_name = [](const std::string& wav_file_name) {
623 if (wav_file_name.size() < 5) {
624 return false;
625 }
626 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
627 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
628 return true;
629 }
630 return false;
631 };
632
633 ReportConditionalErrorAndExit(
634 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
635 "Error: --i must be a valid .wav file name.\n");
636
637 ReportConditionalErrorAndExit(
638 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
639 "Error: --o must be a valid .wav file name.\n");
640
641 ReportConditionalErrorAndExit(
642 settings.reverse_input_filename &&
643 (!valid_wav_name(*settings.reverse_input_filename)),
644 "Error: --ri must be a valid .wav file name.\n");
645
646 ReportConditionalErrorAndExit(
647 settings.reverse_output_filename &&
648 (!valid_wav_name(*settings.reverse_output_filename)),
649 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800650
651 ReportConditionalErrorAndExit(
652 settings.artificial_nearend_filename &&
653 !valid_wav_name(*settings.artificial_nearend_filename),
654 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200655
656 ReportConditionalErrorAndExit(
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100657 settings.linear_aec_output_filename &&
658 (!valid_wav_name(*settings.linear_aec_output_filename)),
659 "Error: --linear_aec_output must be a valid .wav file name.\n");
660
661 ReportConditionalErrorAndExit(
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200662 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
663 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100664
Per Åhgren879d33b2021-01-21 10:08:15 +0100665 ReportConditionalErrorAndExit(settings.init_to_process &&
666 *settings.init_to_process != 1 &&
667 !settings.aec_dump_input_filename,
668 "Error: --init_to_process must be set to 1 for "
669 "wav-file based simulations.\n");
670
671 ReportConditionalErrorAndExit(
672 !settings.init_to_process &&
673 (settings.dump_start_frame || settings.dump_end_frame),
674 "Error: --init_to_process must be set when specifying a start and/or end "
675 "frame for when to dump internal data.\n");
676
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100677 ReportConditionalErrorAndExit(
678 !settings.dump_internal_data &&
679 settings.dump_internal_data_output_dir.has_value(),
680 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100681
682 ReportConditionalErrorAndExit(
683 !settings.aec_dump_input_filename &&
684 settings.call_order_output_filename.has_value(),
685 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200686
687 ReportConditionalErrorAndExit(
688 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
689 settings.pre_amplifier_gain_factor.has_value(),
690 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
691 "specified and set.\n");
Per Åhgrene9cd6172020-05-19 12:52:08 +0200692
693 ReportConditionalErrorAndExit(
694 pre_constructed_ap_provided && pre_constructed_ap_builder_provided,
695 "Error: The AudioProcessing and the AudioProcessingBuilder cannot both "
696 "be specified at the same time.\n");
697
698 ReportConditionalErrorAndExit(
699 settings.aec_settings_filename && pre_constructed_ap_provided,
700 "Error: The aec_settings_filename cannot be specified when a "
701 "pre-constructed audio processing object is provided.\n");
702
703 ReportConditionalErrorAndExit(
704 settings.aec_settings_filename && pre_constructed_ap_provided,
705 "Error: The print_aec_parameter_values cannot be set when a "
706 "pre-constructed audio processing object is provided.\n");
707
708 if (settings.linear_aec_output_filename && pre_constructed_ap_provided) {
709 std::cout << "Warning: For the linear AEC output to be stored, this must "
710 "be configured in the AEC that is part of the provided "
711 "AudioProcessing object."
712 << std::endl;
713 }
Peter Kasting69558702016-01-12 16:26:35 -0800714}
715
Per Åhgrene9cd6172020-05-19 12:52:08 +0200716int RunSimulation(rtc::scoped_refptr<AudioProcessing> audio_processing,
717 std::unique_ptr<AudioProcessingBuilder> ap_builder,
718 int argc,
719 char* argv[],
720 absl::string_view input_aecdump,
721 std::vector<float>* processed_capture_samples) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200722 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
723 if (args.size() != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700724 printf("%s", kUsageDescription);
oprypin6e09d872017-08-31 03:21:39 -0700725 return 1;
726 }
saza0cf99ce2020-04-01 17:42:01 +0200727 // InitFieldTrialsFromString stores the char*, so the char array must
728 // outlive the application.
729 const std::string field_trials = absl::GetFlag(FLAGS_force_fieldtrials);
730 webrtc::field_trial::InitFieldTrialsFromString(field_trials.c_str());
731
peah60a189f2016-05-24 20:54:40 -0700732 SimulationSettings settings = CreateSettings();
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200733 if (!input_aecdump.empty()) {
734 settings.aec_dump_input_string = input_aecdump;
735 settings.processed_capture_samples = processed_capture_samples;
736 RTC_CHECK(settings.processed_capture_samples);
737 }
Per Åhgrene9cd6172020-05-19 12:52:08 +0200738 PerformBasicParameterSanityChecks(settings, !!audio_processing, !!ap_builder);
peah60a189f2016-05-24 20:54:40 -0700739 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000740
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200741 if (settings.aec_dump_input_filename || settings.aec_dump_input_string) {
Per Åhgrene9cd6172020-05-19 12:52:08 +0200742 processor.reset(new AecDumpBasedSimulator(
743 settings, std::move(audio_processing), std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800744 } else {
Per Åhgrene9cd6172020-05-19 12:52:08 +0200745 processor.reset(new WavBasedSimulator(settings, std::move(audio_processing),
746 std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700747 }
748
Per Åhgren879d33b2021-01-21 10:08:15 +0100749 if (settings.analysis_only) {
750 processor->Analyze();
751 } else {
752 processor->Process();
753 }
aluebsb0ad43b2015-11-20 00:11:53 -0800754
peah60a189f2016-05-24 20:54:40 -0700755 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200756 processor->GetApiCallStatistics().PrintReport();
757 }
758 if (settings.performance_report_output_filename) {
759 processor->GetApiCallStatistics().WriteReportToFile(
760 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700761 }
762
763 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
764 if (processor->OutputWasBitexact()) {
765 std::cout << "The processing was bitexact.";
766 } else {
767 std::cout << "The processing was not bitexact.";
768 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700769 }
aluebsb0ad43b2015-11-20 00:11:53 -0800770
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000771 return 0;
772}
773
Per Åhgrene9cd6172020-05-19 12:52:08 +0200774} // namespace
775
776int AudioprocFloatImpl(rtc::scoped_refptr<AudioProcessing> audio_processing,
777 int argc,
778 char* argv[]) {
779 return RunSimulation(
780 std::move(audio_processing), /*ap_builder=*/nullptr, argc, argv,
781 /*input_aecdump=*/"", /*processed_capture_samples=*/nullptr);
782}
783
784int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
785 int argc,
786 char* argv[],
787 absl::string_view input_aecdump,
788 std::vector<float>* processed_capture_samples) {
789 return RunSimulation(/*audio_processing=*/nullptr, std::move(ap_builder),
790 argc, argv, input_aecdump, processed_capture_samples);
791}
792
peah60a189f2016-05-24 20:54:40 -0700793} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000794} // namespace webrtc