blob: 0c08349e25844cc1e5f7bdf0b5f9ab00741ab0f5 [file] [log] [blame]
andrew@webrtc.org08df9b22014-12-16 20:57:15 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Per Åhgren6ee75fd2019-04-26 11:33:37 +020011#include "modules/audio_processing/test/audioproc_float_impl.h"
12
Ivo Creusen2cb41052018-03-15 12:22:52 +010013#include <string.h>
14
aluebsb0ad43b2015-11-20 00:11:53 -080015#include <iostream>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
Ivo Creusen2cb41052018-03-15 12:22:52 +010017#include <string>
18#include <utility>
Alessio Bazzica68170382018-11-20 12:27:20 +010019#include <vector>
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000020
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020021#include "absl/flags/flag.h"
22#include "absl/flags/parse.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010023#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/include/audio_processing.h"
25#include "modules/audio_processing/test/aec_dump_based_simulator.h"
26#include "modules/audio_processing/test/audio_processing_simulator.h"
27#include "modules/audio_processing/test/wav_based_simulator.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010028#include "rtc_base/checks.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010029#include "rtc_base/strings/string_builder.h"
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000030
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020031constexpr int kParameterNotSpecifiedValue = -10000;
32
33ABSL_FLAG(std::string, dump_input, "", "Aec dump input filename");
34ABSL_FLAG(std::string, dump_output, "", "Aec dump output filename");
35ABSL_FLAG(std::string, i, "", "Forward stream input wav filename");
36ABSL_FLAG(std::string, o, "", "Forward stream output wav filename");
37ABSL_FLAG(std::string, ri, "", "Reverse stream input wav filename");
38ABSL_FLAG(std::string, ro, "", "Reverse stream output wav filename");
39ABSL_FLAG(std::string,
40 artificial_nearend,
41 "",
42 "Artificial nearend wav filename");
Per Åhgrenc20a19c2019-11-13 11:12:29 +010043ABSL_FLAG(std::string, linear_aec_output, "", "Linear AEC output wav filename");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020044ABSL_FLAG(int,
45 output_num_channels,
46 kParameterNotSpecifiedValue,
47 "Number of forward stream output channels");
48ABSL_FLAG(int,
49 reverse_output_num_channels,
50 kParameterNotSpecifiedValue,
51 "Number of Reverse stream output channels");
52ABSL_FLAG(int,
53 output_sample_rate_hz,
54 kParameterNotSpecifiedValue,
55 "Forward stream output sample rate in Hz");
56ABSL_FLAG(int,
57 reverse_output_sample_rate_hz,
58 kParameterNotSpecifiedValue,
59 "Reverse stream output sample rate in Hz");
60ABSL_FLAG(bool,
61 fixed_interface,
62 false,
63 "Use the fixed interface when operating on wav files");
64ABSL_FLAG(int,
65 aec,
66 kParameterNotSpecifiedValue,
67 "Activate (1) or deactivate(0) the echo canceller");
68ABSL_FLAG(int,
69 aecm,
70 kParameterNotSpecifiedValue,
71 "Activate (1) or deactivate(0) the mobile echo controller");
72ABSL_FLAG(int,
73 ed,
74 kParameterNotSpecifiedValue,
75 "Activate (1) or deactivate (0) the residual echo detector");
76ABSL_FLAG(std::string,
77 ed_graph,
78 "",
79 "Output filename for graph of echo likelihood");
80ABSL_FLAG(int,
81 agc,
82 kParameterNotSpecifiedValue,
83 "Activate (1) or deactivate(0) the AGC");
84ABSL_FLAG(int,
85 agc2,
86 kParameterNotSpecifiedValue,
87 "Activate (1) or deactivate(0) the AGC2");
88ABSL_FLAG(int,
89 pre_amplifier,
90 kParameterNotSpecifiedValue,
91 "Activate (1) or deactivate(0) the pre amplifier");
92ABSL_FLAG(int,
93 hpf,
94 kParameterNotSpecifiedValue,
95 "Activate (1) or deactivate(0) the high-pass filter");
96ABSL_FLAG(int,
97 ns,
98 kParameterNotSpecifiedValue,
99 "Activate (1) or deactivate(0) the noise suppressor");
100ABSL_FLAG(int,
101 ts,
102 kParameterNotSpecifiedValue,
103 "Activate (1) or deactivate(0) the transient suppressor");
104ABSL_FLAG(int,
Per Åhgren0695df12020-01-13 14:43:13 +0100105 analog_agc,
106 kParameterNotSpecifiedValue,
107 "Activate (1) or deactivate(0) the transient suppressor");
108ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200109 vad,
110 kParameterNotSpecifiedValue,
111 "Activate (1) or deactivate(0) the voice activity detector");
112ABSL_FLAG(int,
113 le,
114 kParameterNotSpecifiedValue,
115 "Activate (1) or deactivate(0) the level estimator");
116ABSL_FLAG(bool,
117 all_default,
118 false,
119 "Activate all of the default components (will be overridden by any "
120 "other settings)");
121ABSL_FLAG(int,
Per Åhgren0cbb58e2019-10-29 22:59:44 +0100122 use_legacy_ns,
123 kParameterNotSpecifiedValue,
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100124 "Activate (1) or deactivate(0) the legacy NS");
Per Åhgren0cbb58e2019-10-29 22:59:44 +0100125ABSL_FLAG(int,
Per Åhgren0695df12020-01-13 14:43:13 +0100126 analog_agc_disable_digital_adaptive,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200127 kParameterNotSpecifiedValue,
128 "Force-deactivate (1) digital adaptation in "
129 "experimental AGC. Digital adaptation is active by default (0).");
130ABSL_FLAG(int,
Per Åhgren0695df12020-01-13 14:43:13 +0100131 analog_agc_agc2_level_estimator,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200132 kParameterNotSpecifiedValue,
133 "AGC2 level estimation"
134 " in the experimental AGC. AGC1 level estimation is the default (0)");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200135ABSL_FLAG(int,
136 agc_mode,
137 kParameterNotSpecifiedValue,
138 "Specify the AGC mode (0-2)");
139ABSL_FLAG(int,
140 agc_target_level,
141 kParameterNotSpecifiedValue,
142 "Specify the AGC target level (0-31)");
143ABSL_FLAG(int,
144 agc_limiter,
145 kParameterNotSpecifiedValue,
146 "Activate (1) or deactivate(0) the level estimator");
147ABSL_FLAG(int,
148 agc_compression_gain,
149 kParameterNotSpecifiedValue,
150 "Specify the AGC compression gain (0-90)");
151ABSL_FLAG(int,
152 agc2_enable_adaptive_gain,
153 kParameterNotSpecifiedValue,
154 "Activate (1) or deactivate(0) the AGC2 adaptive gain");
155ABSL_FLAG(float,
156 agc2_fixed_gain_db,
157 kParameterNotSpecifiedValue,
158 "AGC2 fixed gain (dB) to apply");
159ABSL_FLAG(std::string,
160 agc2_adaptive_level_estimator,
161 "RMS",
162 "AGC2 adaptive digital level estimator to use [RMS, peak]");
163ABSL_FLAG(float,
164 pre_amplifier_gain_factor,
165 kParameterNotSpecifiedValue,
166 "Pre-amplifier gain factor (linear) to apply");
167ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200168 ns_level,
169 kParameterNotSpecifiedValue,
170 "Specify the NS level (0-3)");
171ABSL_FLAG(int,
Per Åhgren2e8e1c62019-12-20 00:42:22 +0100172 ns_analysis_on_linear_aec_output,
173 kParameterNotSpecifiedValue,
174 "Specifies whether the noise suppression analysis is done on the "
175 "linear AEC output");
176ABSL_FLAG(int,
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200177 maximum_internal_processing_rate,
178 kParameterNotSpecifiedValue,
179 "Set a maximum internal processing rate (32000 or 48000) to override "
180 "the default rate");
181ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200182 stream_delay,
183 kParameterNotSpecifiedValue,
184 "Specify the stream delay in ms to use");
185ABSL_FLAG(int,
186 use_stream_delay,
187 kParameterNotSpecifiedValue,
188 "Activate (1) or deactivate(0) reporting the stream delay");
189ABSL_FLAG(int,
190 stream_drift_samples,
191 kParameterNotSpecifiedValue,
192 "Specify the number of stream drift samples to use");
193ABSL_FLAG(int, initial_mic_level, 100, "Initial mic level (0-255)");
194ABSL_FLAG(int,
195 simulate_mic_gain,
196 0,
197 "Activate (1) or deactivate(0) the analog mic gain simulation");
198ABSL_FLAG(int,
Per Åhgrene14cb992019-11-27 09:34:22 +0100199 multi_channel_render,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200200 kParameterNotSpecifiedValue,
Per Åhgrene14cb992019-11-27 09:34:22 +0100201 "Activate (1) or deactivate(0) multi-channel render processing in "
202 "APM pipeline");
203ABSL_FLAG(int,
204 multi_channel_capture,
205 kParameterNotSpecifiedValue,
206 "Activate (1) or deactivate(0) multi-channel capture processing in "
207 "APM pipeline");
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200208ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200209 simulated_mic_kind,
210 kParameterNotSpecifiedValue,
211 "Specify which microphone kind to use for microphone simulation");
212ABSL_FLAG(bool, performance_report, false, "Report the APM performance ");
213ABSL_FLAG(std::string,
214 performance_report_output_file,
215 "",
216 "Generate a CSV file with the API call durations");
217ABSL_FLAG(bool, verbose, false, "Produce verbose output");
218ABSL_FLAG(bool,
219 quiet,
220 false,
221 "Avoid producing information about the progress.");
222ABSL_FLAG(bool,
223 bitexactness_report,
224 false,
225 "Report bitexactness for aec dump result reproduction");
226ABSL_FLAG(bool,
227 discard_settings_in_aecdump,
228 false,
229 "Discard any config settings specified in the aec dump");
230ABSL_FLAG(bool,
231 store_intermediate_output,
232 false,
233 "Creates new output files after each init");
234ABSL_FLAG(std::string,
235 custom_call_order_file,
236 "",
237 "Custom process API call order file");
238ABSL_FLAG(std::string,
239 output_custom_call_order_file,
240 "",
241 "Generate custom process API call order file from AEC dump");
242ABSL_FLAG(bool,
243 print_aec_parameter_values,
244 false,
245 "Print parameter values used in AEC in JSON-format");
246ABSL_FLAG(std::string,
247 aec_settings,
248 "",
249 "File in JSON-format with custom AEC settings");
250ABSL_FLAG(bool,
251 dump_data,
252 false,
253 "Dump internal data during the call (requires build flag)");
254ABSL_FLAG(std::string,
255 dump_data_output_dir,
256 "",
257 "Internal data dump output directory");
Per Åhgren5dca3f12020-01-28 09:08:11 +0100258ABSL_FLAG(bool,
259 float_wav_output,
260 false,
261 "Produce floating point wav output files.");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200262
peah60a189f2016-05-24 20:54:40 -0700263namespace webrtc {
264namespace test {
Peter Kasting69558702016-01-12 16:26:35 -0800265namespace {
266
peah60a189f2016-05-24 20:54:40 -0700267const char kUsageDescription[] =
268 "Usage: audioproc_f [options] -i <input.wav>\n"
269 " or\n"
270 " audioproc_f [options] -dump_input <aec_dump>\n"
271 "\n\n"
272 "Command-line tool to simulate a call using the audio "
273 "processing module, either based on wav files or "
oprypin6e09d872017-08-31 03:21:39 -0700274 "protobuf debug dump recordings.\n";
peah60a189f2016-05-24 20:54:40 -0700275
Alessio Bazzica68170382018-11-20 12:27:20 +0100276std::vector<std::string> GetAgc2AdaptiveLevelEstimatorNames() {
277 return {"RMS", "peak"};
278}
peah60a189f2016-05-24 20:54:40 -0700279
Alex Loiko890988c2017-08-31 10:25:48 +0200280void SetSettingIfSpecified(const std::string& value,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200281 absl::optional<std::string>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700282 if (value.compare("") != 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100283 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700284 }
285}
286
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200287void SetSettingIfSpecified(int value, absl::optional<int>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700288 if (value != kParameterNotSpecifiedValue) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100289 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700290 }
291}
292
Per Åhgrenef349602019-04-12 16:26:34 +0200293void SetSettingIfSpecified(float value, absl::optional<float>* parameter) {
294 constexpr float kFloatParameterNotSpecifiedValue =
295 kParameterNotSpecifiedValue;
296 if (value != kFloatParameterNotSpecifiedValue) {
297 *parameter = value;
298 }
299}
300
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200301void SetSettingIfFlagSet(int32_t flag, absl::optional<bool>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700302 if (flag == 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100303 *parameter = false;
peah60a189f2016-05-24 20:54:40 -0700304 } else if (flag == 1) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100305 *parameter = true;
peah60a189f2016-05-24 20:54:40 -0700306 }
307}
308
Alessio Bazzica68170382018-11-20 12:27:20 +0100309AudioProcessing::Config::GainController2::LevelEstimator
310MapAgc2AdaptiveLevelEstimator(absl::string_view name) {
311 if (name.compare("RMS") == 0) {
312 return AudioProcessing::Config::GainController2::LevelEstimator::kRms;
313 }
314 if (name.compare("peak") == 0) {
315 return AudioProcessing::Config::GainController2::LevelEstimator::kPeak;
316 }
317 auto concat_strings =
318 [](const std::vector<std::string>& strings) -> std::string {
319 rtc::StringBuilder ss;
320 for (const auto& s : strings) {
321 ss << " " << s;
322 }
323 return ss.Release();
324 };
325 RTC_CHECK(false)
326 << "Invalid value for agc2_adaptive_level_estimator, valid options:"
327 << concat_strings(GetAgc2AdaptiveLevelEstimatorNames()) << ".";
328}
329
peah60a189f2016-05-24 20:54:40 -0700330SimulationSettings CreateSettings() {
331 SimulationSettings settings;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200332 if (absl::GetFlag(FLAGS_all_default)) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100333 settings.use_le = true;
334 settings.use_vad = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100335 settings.use_ts = true;
Per Åhgren0695df12020-01-13 14:43:13 +0100336 settings.use_analog_agc = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100337 settings.use_ns = true;
338 settings.use_hpf = true;
339 settings.use_agc = true;
340 settings.use_agc2 = false;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200341 settings.use_pre_amplifier = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100342 settings.use_aec = true;
343 settings.use_aecm = false;
344 settings.use_ed = false;
peah60a189f2016-05-24 20:54:40 -0700345 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200346 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_input),
347 &settings.aec_dump_input_filename);
348 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_output),
349 &settings.aec_dump_output_filename);
350 SetSettingIfSpecified(absl::GetFlag(FLAGS_i), &settings.input_filename);
351 SetSettingIfSpecified(absl::GetFlag(FLAGS_o), &settings.output_filename);
352 SetSettingIfSpecified(absl::GetFlag(FLAGS_ri),
353 &settings.reverse_input_filename);
354 SetSettingIfSpecified(absl::GetFlag(FLAGS_ro),
355 &settings.reverse_output_filename);
356 SetSettingIfSpecified(absl::GetFlag(FLAGS_artificial_nearend),
peahdf80fd12016-12-09 02:43:40 -0800357 &settings.artificial_nearend_filename);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100358 SetSettingIfSpecified(absl::GetFlag(FLAGS_linear_aec_output),
359 &settings.linear_aec_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200360 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700361 &settings.output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200362 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700363 &settings.reverse_output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200364 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700365 &settings.output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200366 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700367 &settings.reverse_output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200368 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aec), &settings.use_aec);
369 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aecm), &settings.use_aecm);
370 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ed), &settings.use_ed);
371 SetSettingIfSpecified(absl::GetFlag(FLAGS_ed_graph),
372 &settings.ed_graph_output_filename);
373 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc), &settings.use_agc);
374 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2), &settings.use_agc2);
375 SetSettingIfFlagSet(absl::GetFlag(FLAGS_pre_amplifier),
376 &settings.use_pre_amplifier);
377 SetSettingIfFlagSet(absl::GetFlag(FLAGS_hpf), &settings.use_hpf);
378 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns), &settings.use_ns);
379 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ts), &settings.use_ts);
Per Åhgren0695df12020-01-13 14:43:13 +0100380 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc),
381 &settings.use_analog_agc);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200382 SetSettingIfFlagSet(absl::GetFlag(FLAGS_vad), &settings.use_vad);
383 SetSettingIfFlagSet(absl::GetFlag(FLAGS_le), &settings.use_le);
Per Åhgren0cbb58e2019-10-29 22:59:44 +0100384 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_legacy_ns),
385 &settings.use_legacy_ns);
Per Åhgren0695df12020-01-13 14:43:13 +0100386 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_disable_digital_adaptive),
387 &settings.analog_agc_disable_digital_adaptive);
388 SetSettingIfFlagSet(absl::GetFlag(FLAGS_analog_agc_agc2_level_estimator),
389 &settings.use_analog_agc_agc2_level_estimator);
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 Åhgren5dca3f12020-01-28 09:08:11 +0100444 settings.wav_output_format = absl::GetFlag(FLAGS_float_wav_output)
445 ? WavFile::SampleFormat::kFloat
446 : WavFile::SampleFormat::kInt16;
peah60a189f2016-05-24 20:54:40 -0700447
448 return settings;
449}
450
Alex Loiko890988c2017-08-31 10:25:48 +0200451void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
peah60a189f2016-05-24 20:54:40 -0700452 if (condition) {
453 std::cerr << message << std::endl;
454 exit(1);
455 }
456}
457
458void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
459 if (settings.input_filename || settings.reverse_input_filename) {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200460 ReportConditionalErrorAndExit(
461 !!settings.aec_dump_input_filename,
462 "Error: The aec dump file cannot be specified "
463 "together with input wav files!\n");
464
465 ReportConditionalErrorAndExit(
466 !!settings.aec_dump_input_string,
467 "Error: The aec dump input string cannot be specified "
468 "together with input wav files!\n");
peah60a189f2016-05-24 20:54:40 -0700469
peahdf80fd12016-12-09 02:43:40 -0800470 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
471 "Error: The artificial nearend cannot be "
472 "specified together with input wav files!\n");
473
peah60a189f2016-05-24 20:54:40 -0700474 ReportConditionalErrorAndExit(!settings.input_filename,
475 "Error: When operating at wav files, the "
476 "input wav filename must be "
477 "specified!\n");
478
479 ReportConditionalErrorAndExit(
480 settings.reverse_output_filename && !settings.reverse_input_filename,
481 "Error: When operating at wav files, the reverse input wav filename "
482 "must be specified if the reverse output wav filename is specified!\n");
483 } else {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200484 ReportConditionalErrorAndExit(
485 !settings.aec_dump_input_filename && !settings.aec_dump_input_string,
486 "Error: Either the aec dump input file, the wav "
487 "input file or the aec dump input string must be specified!\n");
488 ReportConditionalErrorAndExit(
489 settings.aec_dump_input_filename && settings.aec_dump_input_string,
490 "Error: The aec dump input file cannot be specified together with the "
491 "aec dump input string!\n");
peah60a189f2016-05-24 20:54:40 -0700492 }
493
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100494 ReportConditionalErrorAndExit(settings.use_aec && !(*settings.use_aec) &&
495 settings.linear_aec_output_filename,
496 "Error: The linear AEC ouput filename cannot "
497 "be specified without the AEC being active");
498
499 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700500 settings.use_aec && *settings.use_aec && settings.use_aecm &&
501 *settings.use_aecm,
502 "Error: The AEC and the AECM cannot be activated at the same time!\n");
503
504 ReportConditionalErrorAndExit(
505 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
506 "Error: --output_sample_rate_hz must be positive!\n");
507
508 ReportConditionalErrorAndExit(
509 settings.reverse_output_sample_rate_hz &&
510 settings.output_sample_rate_hz &&
511 *settings.output_sample_rate_hz <= 0,
512 "Error: --reverse_output_sample_rate_hz must be positive!\n");
513
514 ReportConditionalErrorAndExit(
515 settings.output_num_channels && *settings.output_num_channels <= 0,
516 "Error: --output_num_channels must be positive!\n");
517
518 ReportConditionalErrorAndExit(
519 settings.reverse_output_num_channels &&
520 *settings.reverse_output_num_channels <= 0,
521 "Error: --reverse_output_num_channels must be positive!\n");
522
peah60a189f2016-05-24 20:54:40 -0700523 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700524 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
525 (*settings.agc_target_level) > 31),
526 "Error: --agc_target_level must be specified between 0 and 31.\n");
527
528 ReportConditionalErrorAndExit(
529 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
530 (*settings.agc_compression_gain) > 90),
531 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
532
533 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200534 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
535 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200536 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
537
538 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700539 settings.ns_level &&
540 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
541 "Error: --ns_level must be specified between 0 and 3.\n");
542
543 ReportConditionalErrorAndExit(
544 settings.report_bitexactness && !settings.aec_dump_input_filename,
545 "Error: --bitexactness_report can only be used when operating on an "
546 "aecdump\n");
547
peah5ad5de32016-12-09 03:18:22 -0800548 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100549 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800550 "Error: --custom_call_order_file cannot be used when operating on an "
551 "aecdump\n");
552
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200553 ReportConditionalErrorAndExit(
554 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
555 "Error: --initial_mic_level must be specified between 0 and 255.\n");
556
557 ReportConditionalErrorAndExit(
558 settings.simulated_mic_kind && !settings.simulate_mic_gain,
559 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
560 "disabled\n");
561
562 ReportConditionalErrorAndExit(
563 !settings.simulated_mic_kind && settings.simulate_mic_gain,
564 "Error: --simulated_mic_kind must be specified when mic simulation is "
565 "enabled\n");
566
peah60a189f2016-05-24 20:54:40 -0700567 auto valid_wav_name = [](const std::string& wav_file_name) {
568 if (wav_file_name.size() < 5) {
569 return false;
570 }
571 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
572 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
573 return true;
574 }
575 return false;
576 };
577
578 ReportConditionalErrorAndExit(
579 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
580 "Error: --i must be a valid .wav file name.\n");
581
582 ReportConditionalErrorAndExit(
583 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
584 "Error: --o must be a valid .wav file name.\n");
585
586 ReportConditionalErrorAndExit(
587 settings.reverse_input_filename &&
588 (!valid_wav_name(*settings.reverse_input_filename)),
589 "Error: --ri must be a valid .wav file name.\n");
590
591 ReportConditionalErrorAndExit(
592 settings.reverse_output_filename &&
593 (!valid_wav_name(*settings.reverse_output_filename)),
594 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800595
596 ReportConditionalErrorAndExit(
597 settings.artificial_nearend_filename &&
598 !valid_wav_name(*settings.artificial_nearend_filename),
599 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200600
601 ReportConditionalErrorAndExit(
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100602 settings.linear_aec_output_filename &&
603 (!valid_wav_name(*settings.linear_aec_output_filename)),
604 "Error: --linear_aec_output must be a valid .wav file name.\n");
605
606 ReportConditionalErrorAndExit(
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200607 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
608 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100609
610 ReportConditionalErrorAndExit(
611 !settings.dump_internal_data &&
612 settings.dump_internal_data_output_dir.has_value(),
613 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100614
615 ReportConditionalErrorAndExit(
616 !settings.aec_dump_input_filename &&
617 settings.call_order_output_filename.has_value(),
618 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200619
620 ReportConditionalErrorAndExit(
621 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
622 settings.pre_amplifier_gain_factor.has_value(),
623 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
624 "specified and set.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800625}
626
627} // namespace
628
Ivo Creusen2cb41052018-03-15 12:22:52 +0100629int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
630 int argc,
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200631 char* argv[],
632 absl::string_view input_aecdump,
633 std::vector<float>* processed_capture_samples) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200634 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
635 if (args.size() != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700636 printf("%s", kUsageDescription);
oprypin6e09d872017-08-31 03:21:39 -0700637 return 1;
638 }
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000639
peah60a189f2016-05-24 20:54:40 -0700640 SimulationSettings settings = CreateSettings();
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200641 if (!input_aecdump.empty()) {
642 settings.aec_dump_input_string = input_aecdump;
643 settings.processed_capture_samples = processed_capture_samples;
644 RTC_CHECK(settings.processed_capture_samples);
645 }
peah60a189f2016-05-24 20:54:40 -0700646 PerformBasicParameterSanityChecks(settings);
647 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000648
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200649 if (settings.aec_dump_input_filename || settings.aec_dump_input_string) {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100650 processor.reset(new AecDumpBasedSimulator(settings, std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800651 } else {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100652 processor.reset(new WavBasedSimulator(settings, std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700653 }
654
peah60a189f2016-05-24 20:54:40 -0700655 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800656
peah60a189f2016-05-24 20:54:40 -0700657 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200658 processor->GetApiCallStatistics().PrintReport();
659 }
660 if (settings.performance_report_output_filename) {
661 processor->GetApiCallStatistics().WriteReportToFile(
662 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700663 }
664
665 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
666 if (processor->OutputWasBitexact()) {
667 std::cout << "The processing was bitexact.";
668 } else {
669 std::cout << "The processing was not bitexact.";
670 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700671 }
aluebsb0ad43b2015-11-20 00:11:53 -0800672
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000673 return 0;
674}
675
peah60a189f2016-05-24 20:54:40 -0700676} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000677} // namespace webrtc