blob: 4902acb9ee17eca2160da3e70c824c41523ba34e [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,
105 vad,
106 kParameterNotSpecifiedValue,
107 "Activate (1) or deactivate(0) the voice activity detector");
108ABSL_FLAG(int,
109 le,
110 kParameterNotSpecifiedValue,
111 "Activate (1) or deactivate(0) the level estimator");
112ABSL_FLAG(bool,
113 all_default,
114 false,
115 "Activate all of the default components (will be overridden by any "
116 "other settings)");
117ABSL_FLAG(int,
118 aec_suppression_level,
119 kParameterNotSpecifiedValue,
120 "Set the aec suppression level (0-2)");
121ABSL_FLAG(int,
122 delay_agnostic,
123 kParameterNotSpecifiedValue,
124 "Activate (1) or deactivate(0) the AEC delay agnostic mode");
125ABSL_FLAG(int,
126 extended_filter,
127 kParameterNotSpecifiedValue,
128 "Activate (1) or deactivate(0) the AEC extended filter mode");
129ABSL_FLAG(int,
130 use_legacy_aec,
131 kParameterNotSpecifiedValue,
132 "Activate (1) or deactivate(0) the legacy AEC");
133ABSL_FLAG(int,
Per Åhgren0cbb58e2019-10-29 22:59:44 +0100134 use_legacy_ns,
135 kParameterNotSpecifiedValue,
136 "Activate (1) or deactivate(0) the legacy AEC");
137ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200138 experimental_agc,
139 kParameterNotSpecifiedValue,
140 "Activate (1) or deactivate(0) the experimental AGC");
141ABSL_FLAG(int,
142 experimental_agc_disable_digital_adaptive,
143 kParameterNotSpecifiedValue,
144 "Force-deactivate (1) digital adaptation in "
145 "experimental AGC. Digital adaptation is active by default (0).");
146ABSL_FLAG(int,
147 experimental_agc_analyze_before_aec,
148 kParameterNotSpecifiedValue,
149 "Make level estimation happen before AEC"
150 " in the experimental AGC. After AEC is the default (0)");
151ABSL_FLAG(int,
152 experimental_agc_agc2_level_estimator,
153 kParameterNotSpecifiedValue,
154 "AGC2 level estimation"
155 " in the experimental AGC. AGC1 level estimation is the default (0)");
156ABSL_FLAG(
157 int,
158 refined_adaptive_filter,
159 kParameterNotSpecifiedValue,
160 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
161ABSL_FLAG(int,
162 agc_mode,
163 kParameterNotSpecifiedValue,
164 "Specify the AGC mode (0-2)");
165ABSL_FLAG(int,
166 agc_target_level,
167 kParameterNotSpecifiedValue,
168 "Specify the AGC target level (0-31)");
169ABSL_FLAG(int,
170 agc_limiter,
171 kParameterNotSpecifiedValue,
172 "Activate (1) or deactivate(0) the level estimator");
173ABSL_FLAG(int,
174 agc_compression_gain,
175 kParameterNotSpecifiedValue,
176 "Specify the AGC compression gain (0-90)");
177ABSL_FLAG(int,
178 agc2_enable_adaptive_gain,
179 kParameterNotSpecifiedValue,
180 "Activate (1) or deactivate(0) the AGC2 adaptive gain");
181ABSL_FLAG(float,
182 agc2_fixed_gain_db,
183 kParameterNotSpecifiedValue,
184 "AGC2 fixed gain (dB) to apply");
185ABSL_FLAG(std::string,
186 agc2_adaptive_level_estimator,
187 "RMS",
188 "AGC2 adaptive digital level estimator to use [RMS, peak]");
189ABSL_FLAG(float,
190 pre_amplifier_gain_factor,
191 kParameterNotSpecifiedValue,
192 "Pre-amplifier gain factor (linear) to apply");
193ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200194 ns_level,
195 kParameterNotSpecifiedValue,
196 "Specify the NS level (0-3)");
197ABSL_FLAG(int,
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200198 maximum_internal_processing_rate,
199 kParameterNotSpecifiedValue,
200 "Set a maximum internal processing rate (32000 or 48000) to override "
201 "the default rate");
202ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200203 stream_delay,
204 kParameterNotSpecifiedValue,
205 "Specify the stream delay in ms to use");
206ABSL_FLAG(int,
207 use_stream_delay,
208 kParameterNotSpecifiedValue,
209 "Activate (1) or deactivate(0) reporting the stream delay");
210ABSL_FLAG(int,
211 stream_drift_samples,
212 kParameterNotSpecifiedValue,
213 "Specify the number of stream drift samples to use");
214ABSL_FLAG(int, initial_mic_level, 100, "Initial mic level (0-255)");
215ABSL_FLAG(int,
216 simulate_mic_gain,
217 0,
218 "Activate (1) or deactivate(0) the analog mic gain simulation");
219ABSL_FLAG(int,
Per Åhgrene14cb992019-11-27 09:34:22 +0100220 multi_channel_render,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200221 kParameterNotSpecifiedValue,
Per Åhgrene14cb992019-11-27 09:34:22 +0100222 "Activate (1) or deactivate(0) multi-channel render processing in "
223 "APM pipeline");
224ABSL_FLAG(int,
225 multi_channel_capture,
226 kParameterNotSpecifiedValue,
227 "Activate (1) or deactivate(0) multi-channel capture processing in "
228 "APM pipeline");
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200229ABSL_FLAG(int,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200230 simulated_mic_kind,
231 kParameterNotSpecifiedValue,
232 "Specify which microphone kind to use for microphone simulation");
233ABSL_FLAG(bool, performance_report, false, "Report the APM performance ");
234ABSL_FLAG(std::string,
235 performance_report_output_file,
236 "",
237 "Generate a CSV file with the API call durations");
238ABSL_FLAG(bool, verbose, false, "Produce verbose output");
239ABSL_FLAG(bool,
240 quiet,
241 false,
242 "Avoid producing information about the progress.");
243ABSL_FLAG(bool,
244 bitexactness_report,
245 false,
246 "Report bitexactness for aec dump result reproduction");
247ABSL_FLAG(bool,
248 discard_settings_in_aecdump,
249 false,
250 "Discard any config settings specified in the aec dump");
251ABSL_FLAG(bool,
252 store_intermediate_output,
253 false,
254 "Creates new output files after each init");
255ABSL_FLAG(std::string,
256 custom_call_order_file,
257 "",
258 "Custom process API call order file");
259ABSL_FLAG(std::string,
260 output_custom_call_order_file,
261 "",
262 "Generate custom process API call order file from AEC dump");
263ABSL_FLAG(bool,
264 print_aec_parameter_values,
265 false,
266 "Print parameter values used in AEC in JSON-format");
267ABSL_FLAG(std::string,
268 aec_settings,
269 "",
270 "File in JSON-format with custom AEC settings");
271ABSL_FLAG(bool,
272 dump_data,
273 false,
274 "Dump internal data during the call (requires build flag)");
275ABSL_FLAG(std::string,
276 dump_data_output_dir,
277 "",
278 "Internal data dump output directory");
279
peah60a189f2016-05-24 20:54:40 -0700280namespace webrtc {
281namespace test {
Peter Kasting69558702016-01-12 16:26:35 -0800282namespace {
283
peah60a189f2016-05-24 20:54:40 -0700284const char kUsageDescription[] =
285 "Usage: audioproc_f [options] -i <input.wav>\n"
286 " or\n"
287 " audioproc_f [options] -dump_input <aec_dump>\n"
288 "\n\n"
289 "Command-line tool to simulate a call using the audio "
290 "processing module, either based on wav files or "
oprypin6e09d872017-08-31 03:21:39 -0700291 "protobuf debug dump recordings.\n";
peah60a189f2016-05-24 20:54:40 -0700292
Alessio Bazzica68170382018-11-20 12:27:20 +0100293std::vector<std::string> GetAgc2AdaptiveLevelEstimatorNames() {
294 return {"RMS", "peak"};
295}
peah60a189f2016-05-24 20:54:40 -0700296
Alex Loiko890988c2017-08-31 10:25:48 +0200297void SetSettingIfSpecified(const std::string& value,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200298 absl::optional<std::string>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700299 if (value.compare("") != 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100300 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700301 }
302}
303
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200304void SetSettingIfSpecified(int value, absl::optional<int>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700305 if (value != kParameterNotSpecifiedValue) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100306 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700307 }
308}
309
Per Åhgrenef349602019-04-12 16:26:34 +0200310void SetSettingIfSpecified(float value, absl::optional<float>* parameter) {
311 constexpr float kFloatParameterNotSpecifiedValue =
312 kParameterNotSpecifiedValue;
313 if (value != kFloatParameterNotSpecifiedValue) {
314 *parameter = value;
315 }
316}
317
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200318void SetSettingIfFlagSet(int32_t flag, absl::optional<bool>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700319 if (flag == 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100320 *parameter = false;
peah60a189f2016-05-24 20:54:40 -0700321 } else if (flag == 1) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100322 *parameter = true;
peah60a189f2016-05-24 20:54:40 -0700323 }
324}
325
Alessio Bazzica68170382018-11-20 12:27:20 +0100326AudioProcessing::Config::GainController2::LevelEstimator
327MapAgc2AdaptiveLevelEstimator(absl::string_view name) {
328 if (name.compare("RMS") == 0) {
329 return AudioProcessing::Config::GainController2::LevelEstimator::kRms;
330 }
331 if (name.compare("peak") == 0) {
332 return AudioProcessing::Config::GainController2::LevelEstimator::kPeak;
333 }
334 auto concat_strings =
335 [](const std::vector<std::string>& strings) -> std::string {
336 rtc::StringBuilder ss;
337 for (const auto& s : strings) {
338 ss << " " << s;
339 }
340 return ss.Release();
341 };
342 RTC_CHECK(false)
343 << "Invalid value for agc2_adaptive_level_estimator, valid options:"
344 << concat_strings(GetAgc2AdaptiveLevelEstimatorNames()) << ".";
345}
346
peah60a189f2016-05-24 20:54:40 -0700347SimulationSettings CreateSettings() {
348 SimulationSettings settings;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200349 if (absl::GetFlag(FLAGS_all_default)) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100350 settings.use_le = true;
351 settings.use_vad = true;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100352 settings.use_ts = true;
353 settings.use_ns = true;
354 settings.use_hpf = true;
355 settings.use_agc = true;
356 settings.use_agc2 = false;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200357 settings.use_pre_amplifier = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100358 settings.use_aec = true;
359 settings.use_aecm = false;
360 settings.use_ed = false;
peah60a189f2016-05-24 20:54:40 -0700361 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200362 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_input),
363 &settings.aec_dump_input_filename);
364 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_output),
365 &settings.aec_dump_output_filename);
366 SetSettingIfSpecified(absl::GetFlag(FLAGS_i), &settings.input_filename);
367 SetSettingIfSpecified(absl::GetFlag(FLAGS_o), &settings.output_filename);
368 SetSettingIfSpecified(absl::GetFlag(FLAGS_ri),
369 &settings.reverse_input_filename);
370 SetSettingIfSpecified(absl::GetFlag(FLAGS_ro),
371 &settings.reverse_output_filename);
372 SetSettingIfSpecified(absl::GetFlag(FLAGS_artificial_nearend),
peahdf80fd12016-12-09 02:43:40 -0800373 &settings.artificial_nearend_filename);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100374 SetSettingIfSpecified(absl::GetFlag(FLAGS_linear_aec_output),
375 &settings.linear_aec_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200376 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700377 &settings.output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200378 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_num_channels),
peah60a189f2016-05-24 20:54:40 -0700379 &settings.reverse_output_num_channels);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200380 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700381 &settings.output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200382 SetSettingIfSpecified(absl::GetFlag(FLAGS_reverse_output_sample_rate_hz),
peah60a189f2016-05-24 20:54:40 -0700383 &settings.reverse_output_sample_rate_hz);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200384 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aec), &settings.use_aec);
385 SetSettingIfFlagSet(absl::GetFlag(FLAGS_aecm), &settings.use_aecm);
386 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ed), &settings.use_ed);
387 SetSettingIfSpecified(absl::GetFlag(FLAGS_ed_graph),
388 &settings.ed_graph_output_filename);
389 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc), &settings.use_agc);
390 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2), &settings.use_agc2);
391 SetSettingIfFlagSet(absl::GetFlag(FLAGS_pre_amplifier),
392 &settings.use_pre_amplifier);
393 SetSettingIfFlagSet(absl::GetFlag(FLAGS_hpf), &settings.use_hpf);
394 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ns), &settings.use_ns);
395 SetSettingIfFlagSet(absl::GetFlag(FLAGS_ts), &settings.use_ts);
396 SetSettingIfFlagSet(absl::GetFlag(FLAGS_vad), &settings.use_vad);
397 SetSettingIfFlagSet(absl::GetFlag(FLAGS_le), &settings.use_le);
398 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_suppression_level),
peah60a189f2016-05-24 20:54:40 -0700399 &settings.aec_suppression_level);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200400 SetSettingIfFlagSet(absl::GetFlag(FLAGS_delay_agnostic),
401 &settings.use_delay_agnostic);
402 SetSettingIfFlagSet(absl::GetFlag(FLAGS_extended_filter),
403 &settings.use_extended_filter);
404 SetSettingIfFlagSet(absl::GetFlag(FLAGS_refined_adaptive_filter),
peah60a189f2016-05-24 20:54:40 -0700405 &settings.use_refined_adaptive_filter);
406
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200407 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_legacy_aec),
408 &settings.use_legacy_aec);
Per Åhgren0cbb58e2019-10-29 22:59:44 +0100409 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_legacy_ns),
410 &settings.use_legacy_ns);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200411 SetSettingIfFlagSet(absl::GetFlag(FLAGS_experimental_agc),
412 &settings.use_experimental_agc);
413 SetSettingIfFlagSet(
414 absl::GetFlag(FLAGS_experimental_agc_disable_digital_adaptive),
415 &settings.experimental_agc_disable_digital_adaptive);
416 SetSettingIfFlagSet(absl::GetFlag(FLAGS_experimental_agc_analyze_before_aec),
Alex Loikod9342442018-09-10 13:59:41 +0200417 &settings.experimental_agc_analyze_before_aec);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200418 SetSettingIfFlagSet(
419 absl::GetFlag(FLAGS_experimental_agc_agc2_level_estimator),
420 &settings.use_experimental_agc_agc2_level_estimator);
421 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_mode), &settings.agc_mode);
422 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_target_level),
423 &settings.agc_target_level);
424 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc_limiter),
425 &settings.use_agc_limiter);
426 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc_compression_gain),
peah60a189f2016-05-24 20:54:40 -0700427 &settings.agc_compression_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200428 SetSettingIfFlagSet(absl::GetFlag(FLAGS_agc2_enable_adaptive_gain),
Per Åhgrene4d23b12018-10-02 23:40:07 +0200429 &settings.agc2_use_adaptive_gain);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200430 SetSettingIfSpecified(absl::GetFlag(FLAGS_agc2_fixed_gain_db),
431 &settings.agc2_fixed_gain_db);
432 settings.agc2_adaptive_level_estimator = MapAgc2AdaptiveLevelEstimator(
433 absl::GetFlag(FLAGS_agc2_adaptive_level_estimator));
434 SetSettingIfSpecified(absl::GetFlag(FLAGS_pre_amplifier_gain_factor),
Per Åhgrenef349602019-04-12 16:26:34 +0200435 &settings.pre_amplifier_gain_factor);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200436 SetSettingIfSpecified(absl::GetFlag(FLAGS_ns_level), &settings.ns_level);
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200437 SetSettingIfSpecified(absl::GetFlag(FLAGS_maximum_internal_processing_rate),
438 &settings.maximum_internal_processing_rate);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200439 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_delay),
440 &settings.stream_delay);
441 SetSettingIfFlagSet(absl::GetFlag(FLAGS_use_stream_delay),
442 &settings.use_stream_delay);
443 SetSettingIfSpecified(absl::GetFlag(FLAGS_stream_drift_samples),
peah60a189f2016-05-24 20:54:40 -0700444 &settings.stream_drift_samples);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200445 SetSettingIfSpecified(absl::GetFlag(FLAGS_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100446 &settings.call_order_input_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200447 SetSettingIfSpecified(absl::GetFlag(FLAGS_output_custom_call_order_file),
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100448 &settings.call_order_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200449 SetSettingIfSpecified(absl::GetFlag(FLAGS_aec_settings),
450 &settings.aec_settings_filename);
451 settings.initial_mic_level = absl::GetFlag(FLAGS_initial_mic_level);
Per Åhgrene14cb992019-11-27 09:34:22 +0100452 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_render),
453 &settings.multi_channel_render);
454 SetSettingIfFlagSet(absl::GetFlag(FLAGS_multi_channel_capture),
455 &settings.multi_channel_capture);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200456 settings.simulate_mic_gain = absl::GetFlag(FLAGS_simulate_mic_gain);
457 SetSettingIfSpecified(absl::GetFlag(FLAGS_simulated_mic_kind),
458 &settings.simulated_mic_kind);
459 settings.report_performance = absl::GetFlag(FLAGS_performance_report);
460 SetSettingIfSpecified(absl::GetFlag(FLAGS_performance_report_output_file),
Per Åhgrenada9b892019-04-03 16:06:42 +0200461 &settings.performance_report_output_filename);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200462 settings.use_verbose_logging = absl::GetFlag(FLAGS_verbose);
463 settings.use_quiet_output = absl::GetFlag(FLAGS_quiet);
464 settings.report_bitexactness = absl::GetFlag(FLAGS_bitexactness_report);
465 settings.discard_all_settings_in_aecdump =
466 absl::GetFlag(FLAGS_discard_settings_in_aecdump);
467 settings.fixed_interface = absl::GetFlag(FLAGS_fixed_interface);
468 settings.store_intermediate_output =
469 absl::GetFlag(FLAGS_store_intermediate_output);
470 settings.print_aec_parameter_values =
471 absl::GetFlag(FLAGS_print_aec_parameter_values);
472 settings.dump_internal_data = absl::GetFlag(FLAGS_dump_data);
473 SetSettingIfSpecified(absl::GetFlag(FLAGS_dump_data_output_dir),
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100474 &settings.dump_internal_data_output_dir);
peah60a189f2016-05-24 20:54:40 -0700475
476 return settings;
477}
478
Alex Loiko890988c2017-08-31 10:25:48 +0200479void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
peah60a189f2016-05-24 20:54:40 -0700480 if (condition) {
481 std::cerr << message << std::endl;
482 exit(1);
483 }
484}
485
486void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
487 if (settings.input_filename || settings.reverse_input_filename) {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200488 ReportConditionalErrorAndExit(
489 !!settings.aec_dump_input_filename,
490 "Error: The aec dump file cannot be specified "
491 "together with input wav files!\n");
492
493 ReportConditionalErrorAndExit(
494 !!settings.aec_dump_input_string,
495 "Error: The aec dump input string cannot be specified "
496 "together with input wav files!\n");
peah60a189f2016-05-24 20:54:40 -0700497
peahdf80fd12016-12-09 02:43:40 -0800498 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
499 "Error: The artificial nearend cannot be "
500 "specified together with input wav files!\n");
501
peah60a189f2016-05-24 20:54:40 -0700502 ReportConditionalErrorAndExit(!settings.input_filename,
503 "Error: When operating at wav files, the "
504 "input wav filename must be "
505 "specified!\n");
506
507 ReportConditionalErrorAndExit(
508 settings.reverse_output_filename && !settings.reverse_input_filename,
509 "Error: When operating at wav files, the reverse input wav filename "
510 "must be specified if the reverse output wav filename is specified!\n");
511 } else {
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200512 ReportConditionalErrorAndExit(
513 !settings.aec_dump_input_filename && !settings.aec_dump_input_string,
514 "Error: Either the aec dump input file, the wav "
515 "input file or the aec dump input string must be specified!\n");
516 ReportConditionalErrorAndExit(
517 settings.aec_dump_input_filename && settings.aec_dump_input_string,
518 "Error: The aec dump input file cannot be specified together with the "
519 "aec dump input string!\n");
peah60a189f2016-05-24 20:54:40 -0700520 }
521
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100522 ReportConditionalErrorAndExit(settings.use_aec && !(*settings.use_aec) &&
523 settings.linear_aec_output_filename,
524 "Error: The linear AEC ouput filename cannot "
525 "be specified without the AEC being active");
526
527 ReportConditionalErrorAndExit(
528 ((settings.use_aec && *settings.use_aec && settings.use_legacy_aec &&
529 *settings.use_legacy_aec) ||
530 (settings.use_aecm && *settings.use_aecm)) &&
531 !!settings.linear_aec_output_filename,
532 "Error: The linear AEC ouput filename cannot be specified when the "
533 "legacy AEC or the AECm are used");
534
peah60a189f2016-05-24 20:54:40 -0700535 ReportConditionalErrorAndExit(
536 settings.use_aec && *settings.use_aec && settings.use_aecm &&
537 *settings.use_aecm,
538 "Error: The AEC and the AECM cannot be activated at the same time!\n");
539
540 ReportConditionalErrorAndExit(
541 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
542 "Error: --output_sample_rate_hz must be positive!\n");
543
544 ReportConditionalErrorAndExit(
545 settings.reverse_output_sample_rate_hz &&
546 settings.output_sample_rate_hz &&
547 *settings.output_sample_rate_hz <= 0,
548 "Error: --reverse_output_sample_rate_hz must be positive!\n");
549
550 ReportConditionalErrorAndExit(
551 settings.output_num_channels && *settings.output_num_channels <= 0,
552 "Error: --output_num_channels must be positive!\n");
553
554 ReportConditionalErrorAndExit(
555 settings.reverse_output_num_channels &&
556 *settings.reverse_output_num_channels <= 0,
557 "Error: --reverse_output_num_channels must be positive!\n");
558
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200559 ReportConditionalErrorAndExit(settings.aec_suppression_level &&
560 ((*settings.aec_suppression_level) < 1 ||
561 (*settings.aec_suppression_level) > 2),
562 "Error: --aec_suppression_level must be "
563 "specified between 1 and 2. 0 is "
564 "deprecated.\n");
peah60a189f2016-05-24 20:54:40 -0700565
566 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700567 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
568 (*settings.agc_target_level) > 31),
569 "Error: --agc_target_level must be specified between 0 and 31.\n");
570
571 ReportConditionalErrorAndExit(
572 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
573 (*settings.agc_compression_gain) > 90),
574 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
575
576 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200577 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
578 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200579 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
580
581 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700582 settings.ns_level &&
583 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
584 "Error: --ns_level must be specified between 0 and 3.\n");
585
586 ReportConditionalErrorAndExit(
587 settings.report_bitexactness && !settings.aec_dump_input_filename,
588 "Error: --bitexactness_report can only be used when operating on an "
589 "aecdump\n");
590
peah5ad5de32016-12-09 03:18:22 -0800591 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100592 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800593 "Error: --custom_call_order_file cannot be used when operating on an "
594 "aecdump\n");
595
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200596 ReportConditionalErrorAndExit(
597 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
598 "Error: --initial_mic_level must be specified between 0 and 255.\n");
599
600 ReportConditionalErrorAndExit(
601 settings.simulated_mic_kind && !settings.simulate_mic_gain,
602 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
603 "disabled\n");
604
605 ReportConditionalErrorAndExit(
606 !settings.simulated_mic_kind && settings.simulate_mic_gain,
607 "Error: --simulated_mic_kind must be specified when mic simulation is "
608 "enabled\n");
609
peah60a189f2016-05-24 20:54:40 -0700610 auto valid_wav_name = [](const std::string& wav_file_name) {
611 if (wav_file_name.size() < 5) {
612 return false;
613 }
614 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
615 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
616 return true;
617 }
618 return false;
619 };
620
621 ReportConditionalErrorAndExit(
622 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
623 "Error: --i must be a valid .wav file name.\n");
624
625 ReportConditionalErrorAndExit(
626 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
627 "Error: --o must be a valid .wav file name.\n");
628
629 ReportConditionalErrorAndExit(
630 settings.reverse_input_filename &&
631 (!valid_wav_name(*settings.reverse_input_filename)),
632 "Error: --ri must be a valid .wav file name.\n");
633
634 ReportConditionalErrorAndExit(
635 settings.reverse_output_filename &&
636 (!valid_wav_name(*settings.reverse_output_filename)),
637 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800638
639 ReportConditionalErrorAndExit(
640 settings.artificial_nearend_filename &&
641 !valid_wav_name(*settings.artificial_nearend_filename),
642 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200643
644 ReportConditionalErrorAndExit(
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100645 settings.linear_aec_output_filename &&
646 (!valid_wav_name(*settings.linear_aec_output_filename)),
647 "Error: --linear_aec_output must be a valid .wav file name.\n");
648
649 ReportConditionalErrorAndExit(
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200650 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
651 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100652
653 ReportConditionalErrorAndExit(
654 !settings.dump_internal_data &&
655 settings.dump_internal_data_output_dir.has_value(),
656 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100657
658 ReportConditionalErrorAndExit(
659 !settings.aec_dump_input_filename &&
660 settings.call_order_output_filename.has_value(),
661 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200662
663 ReportConditionalErrorAndExit(
664 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
665 settings.pre_amplifier_gain_factor.has_value(),
666 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
667 "specified and set.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800668}
669
670} // namespace
671
Ivo Creusen2cb41052018-03-15 12:22:52 +0100672int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
673 int argc,
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200674 char* argv[],
675 absl::string_view input_aecdump,
676 std::vector<float>* processed_capture_samples) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200677 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
678 if (args.size() != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700679 printf("%s", kUsageDescription);
oprypin6e09d872017-08-31 03:21:39 -0700680 return 1;
681 }
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000682
peah60a189f2016-05-24 20:54:40 -0700683 SimulationSettings settings = CreateSettings();
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200684 if (!input_aecdump.empty()) {
685 settings.aec_dump_input_string = input_aecdump;
686 settings.processed_capture_samples = processed_capture_samples;
687 RTC_CHECK(settings.processed_capture_samples);
688 }
peah60a189f2016-05-24 20:54:40 -0700689 PerformBasicParameterSanityChecks(settings);
690 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000691
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200692 if (settings.aec_dump_input_filename || settings.aec_dump_input_string) {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100693 processor.reset(new AecDumpBasedSimulator(settings, std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800694 } else {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100695 processor.reset(new WavBasedSimulator(settings, std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700696 }
697
peah60a189f2016-05-24 20:54:40 -0700698 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800699
peah60a189f2016-05-24 20:54:40 -0700700 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200701 processor->GetApiCallStatistics().PrintReport();
702 }
703 if (settings.performance_report_output_filename) {
704 processor->GetApiCallStatistics().WriteReportToFile(
705 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700706 }
707
708 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
709 if (processor->OutputWasBitexact()) {
710 std::cout << "The processing was bitexact.";
711 } else {
712 std::cout << "The processing was not bitexact.";
713 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700714 }
aluebsb0ad43b2015-11-20 00:11:53 -0800715
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000716 return 0;
717}
718
peah60a189f2016-05-24 20:54:40 -0700719} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000720} // namespace webrtc