blob: 6b94a071f818085d4e589e2b023544dc2ca2d042 [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
Alessio Bazzica68170382018-11-20 12:27:20 +010021#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/include/audio_processing.h"
23#include "modules/audio_processing/test/aec_dump_based_simulator.h"
24#include "modules/audio_processing/test/audio_processing_simulator.h"
25#include "modules/audio_processing/test/wav_based_simulator.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010026#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/flags.h"
Alessio Bazzica68170382018-11-20 12:27:20 +010028#include "rtc_base/strings/string_builder.h"
andrew@webrtc.org08df9b22014-12-16 20:57:15 +000029
peah60a189f2016-05-24 20:54:40 -070030namespace webrtc {
31namespace test {
Peter Kasting69558702016-01-12 16:26:35 -080032namespace {
33
peah60a189f2016-05-24 20:54:40 -070034const int kParameterNotSpecifiedValue = -10000;
35
36const char kUsageDescription[] =
37 "Usage: audioproc_f [options] -i <input.wav>\n"
38 " or\n"
39 " audioproc_f [options] -dump_input <aec_dump>\n"
40 "\n\n"
41 "Command-line tool to simulate a call using the audio "
42 "processing module, either based on wav files or "
oprypin6e09d872017-08-31 03:21:39 -070043 "protobuf debug dump recordings.\n";
peah60a189f2016-05-24 20:54:40 -070044
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020045WEBRTC_DEFINE_string(dump_input, "", "Aec dump input filename");
46WEBRTC_DEFINE_string(dump_output, "", "Aec dump output filename");
47WEBRTC_DEFINE_string(i, "", "Forward stream input wav filename");
48WEBRTC_DEFINE_string(o, "", "Forward stream output wav filename");
49WEBRTC_DEFINE_string(ri, "", "Reverse stream input wav filename");
50WEBRTC_DEFINE_string(ro, "", "Reverse stream output wav filename");
51WEBRTC_DEFINE_string(artificial_nearend, "", "Artificial nearend wav filename");
52WEBRTC_DEFINE_int(output_num_channels,
53 kParameterNotSpecifiedValue,
54 "Number of forward stream output channels");
55WEBRTC_DEFINE_int(reverse_output_num_channels,
56 kParameterNotSpecifiedValue,
57 "Number of Reverse stream output channels");
58WEBRTC_DEFINE_int(output_sample_rate_hz,
59 kParameterNotSpecifiedValue,
60 "Forward stream output sample rate in Hz");
61WEBRTC_DEFINE_int(reverse_output_sample_rate_hz,
62 kParameterNotSpecifiedValue,
63 "Reverse stream output sample rate in Hz");
64WEBRTC_DEFINE_bool(fixed_interface,
65 false,
66 "Use the fixed interface when operating on wav files");
67WEBRTC_DEFINE_int(aec,
68 kParameterNotSpecifiedValue,
69 "Activate (1) or deactivate(0) the echo canceller");
70WEBRTC_DEFINE_int(aecm,
71 kParameterNotSpecifiedValue,
72 "Activate (1) or deactivate(0) the mobile echo controller");
73WEBRTC_DEFINE_int(ed,
74 kParameterNotSpecifiedValue,
75 "Activate (1) or deactivate (0) the residual echo detector");
76WEBRTC_DEFINE_string(ed_graph,
77 "",
78 "Output filename for graph of echo likelihood");
79WEBRTC_DEFINE_int(agc,
80 kParameterNotSpecifiedValue,
81 "Activate (1) or deactivate(0) the AGC");
82WEBRTC_DEFINE_int(agc2,
83 kParameterNotSpecifiedValue,
84 "Activate (1) or deactivate(0) the AGC2");
85WEBRTC_DEFINE_int(pre_amplifier,
86 kParameterNotSpecifiedValue,
87 "Activate (1) or deactivate(0) the pre amplifier");
88WEBRTC_DEFINE_int(hpf,
89 kParameterNotSpecifiedValue,
90 "Activate (1) or deactivate(0) the high-pass filter");
91WEBRTC_DEFINE_int(ns,
92 kParameterNotSpecifiedValue,
93 "Activate (1) or deactivate(0) the noise suppressor");
94WEBRTC_DEFINE_int(ts,
95 kParameterNotSpecifiedValue,
96 "Activate (1) or deactivate(0) the transient suppressor");
97WEBRTC_DEFINE_int(vad,
98 kParameterNotSpecifiedValue,
99 "Activate (1) or deactivate(0) the voice activity detector");
100WEBRTC_DEFINE_int(le,
101 kParameterNotSpecifiedValue,
102 "Activate (1) or deactivate(0) the level estimator");
103WEBRTC_DEFINE_bool(
104 all_default,
105 false,
106 "Activate all of the default components (will be overridden by any "
107 "other settings)");
108WEBRTC_DEFINE_int(aec_suppression_level,
109 kParameterNotSpecifiedValue,
110 "Set the aec suppression level (0-2)");
111WEBRTC_DEFINE_int(delay_agnostic,
112 kParameterNotSpecifiedValue,
113 "Activate (1) or deactivate(0) the AEC delay agnostic mode");
114WEBRTC_DEFINE_int(extended_filter,
115 kParameterNotSpecifiedValue,
116 "Activate (1) or deactivate(0) the AEC extended filter mode");
Per Åhgren200feba2019-03-06 04:16:46 +0100117WEBRTC_DEFINE_int(use_legacy_aec,
118 kParameterNotSpecifiedValue,
119 "Activate (1) or deactivate(0) the legacy AEC");
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200120WEBRTC_DEFINE_int(experimental_agc,
121 kParameterNotSpecifiedValue,
122 "Activate (1) or deactivate(0) the experimental AGC");
123WEBRTC_DEFINE_int(
124 experimental_agc_disable_digital_adaptive,
125 kParameterNotSpecifiedValue,
126 "Force-deactivate (1) digital adaptation in "
127 "experimental AGC. Digital adaptation is active by default (0).");
128WEBRTC_DEFINE_int(experimental_agc_analyze_before_aec,
129 kParameterNotSpecifiedValue,
130 "Make level estimation happen before AEC"
131 " in the experimental AGC. After AEC is the default (0)");
132WEBRTC_DEFINE_int(
Alex Loikod9342442018-09-10 13:59:41 +0200133 experimental_agc_agc2_level_estimator,
134 kParameterNotSpecifiedValue,
135 "AGC2 level estimation"
136 " in the experimental AGC. AGC1 level estimation is the default (0)");
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200137WEBRTC_DEFINE_int(
peah60a189f2016-05-24 20:54:40 -0700138 refined_adaptive_filter,
139 kParameterNotSpecifiedValue,
140 "Activate (1) or deactivate(0) the refined adaptive filter functionality");
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200141WEBRTC_DEFINE_int(agc_mode,
142 kParameterNotSpecifiedValue,
143 "Specify the AGC mode (0-2)");
144WEBRTC_DEFINE_int(agc_target_level,
145 kParameterNotSpecifiedValue,
146 "Specify the AGC target level (0-31)");
147WEBRTC_DEFINE_int(agc_limiter,
148 kParameterNotSpecifiedValue,
149 "Activate (1) or deactivate(0) the level estimator");
150WEBRTC_DEFINE_int(agc_compression_gain,
151 kParameterNotSpecifiedValue,
152 "Specify the AGC compression gain (0-90)");
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200153WEBRTC_DEFINE_int(agc2_enable_adaptive_gain,
154 kParameterNotSpecifiedValue,
155 "Activate (1) or deactivate(0) the AGC2 adaptive gain");
156WEBRTC_DEFINE_float(agc2_fixed_gain_db,
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200157 kParameterNotSpecifiedValue,
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200158 "AGC2 fixed gain (dB) to apply");
Alessio Bazzica68170382018-11-20 12:27:20 +0100159
160std::vector<std::string> GetAgc2AdaptiveLevelEstimatorNames() {
161 return {"RMS", "peak"};
162}
163WEBRTC_DEFINE_string(
164 agc2_adaptive_level_estimator,
165 "RMS",
166 "AGC2 adaptive digital level estimator to use [RMS, peak]");
167
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200168WEBRTC_DEFINE_float(pre_amplifier_gain_factor,
Per Åhgrenef349602019-04-12 16:26:34 +0200169 kParameterNotSpecifiedValue,
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200170 "Pre-amplifier gain factor (linear) to apply");
171WEBRTC_DEFINE_int(vad_likelihood,
172 kParameterNotSpecifiedValue,
173 "Specify the VAD likelihood (0-3)");
174WEBRTC_DEFINE_int(ns_level,
175 kParameterNotSpecifiedValue,
176 "Specify the NS level (0-3)");
177WEBRTC_DEFINE_int(stream_delay,
178 kParameterNotSpecifiedValue,
179 "Specify the stream delay in ms to use");
180WEBRTC_DEFINE_int(use_stream_delay,
181 kParameterNotSpecifiedValue,
182 "Activate (1) or deactivate(0) reporting the stream delay");
183WEBRTC_DEFINE_int(stream_drift_samples,
184 kParameterNotSpecifiedValue,
185 "Specify the number of stream drift samples to use");
186WEBRTC_DEFINE_int(initial_mic_level, 100, "Initial mic level (0-255)");
187WEBRTC_DEFINE_int(
188 simulate_mic_gain,
189 0,
190 "Activate (1) or deactivate(0) the analog mic gain simulation");
191WEBRTC_DEFINE_int(
192 simulated_mic_kind,
193 kParameterNotSpecifiedValue,
194 "Specify which microphone kind to use for microphone simulation");
195WEBRTC_DEFINE_bool(performance_report, false, "Report the APM performance ");
Per Åhgrenada9b892019-04-03 16:06:42 +0200196WEBRTC_DEFINE_string(performance_report_output_file,
197 "",
198 "Generate a CSV file with the API call durations");
199
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200200WEBRTC_DEFINE_bool(verbose, false, "Produce verbose output");
201WEBRTC_DEFINE_bool(quiet,
202 false,
203 "Avoid producing information about the progress.");
204WEBRTC_DEFINE_bool(bitexactness_report,
205 false,
206 "Report bitexactness for aec dump result reproduction");
207WEBRTC_DEFINE_bool(discard_settings_in_aecdump,
208 false,
209 "Discard any config settings specified in the aec dump");
210WEBRTC_DEFINE_bool(store_intermediate_output,
211 false,
212 "Creates new output files after each init");
213WEBRTC_DEFINE_string(custom_call_order_file,
214 "",
215 "Custom process API call order file");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100216WEBRTC_DEFINE_string(
217 output_custom_call_order_file,
218 "",
219 "Generate custom process API call order file from AEC dump");
Per Åhgren200feba2019-03-06 04:16:46 +0100220WEBRTC_DEFINE_bool(print_aec_parameter_values,
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200221 false,
Per Åhgren200feba2019-03-06 04:16:46 +0100222 "Print parameter values used in AEC in JSON-format");
223WEBRTC_DEFINE_string(aec_settings,
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200224 "",
Per Åhgren200feba2019-03-06 04:16:46 +0100225 "File in JSON-format with custom AEC settings");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200226WEBRTC_DEFINE_bool(dump_data,
227 false,
228 "Dump internal data during the call (requires build flag)");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100229WEBRTC_DEFINE_string(dump_data_output_dir,
230 "",
231 "Internal data dump output directory");
Mirko Bonadei2dfa9982018-10-18 11:35:32 +0200232WEBRTC_DEFINE_bool(help, false, "Print this message");
peah60a189f2016-05-24 20:54:40 -0700233
Alex Loiko890988c2017-08-31 10:25:48 +0200234void SetSettingIfSpecified(const std::string& value,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200235 absl::optional<std::string>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700236 if (value.compare("") != 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100237 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700238 }
239}
240
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200241void SetSettingIfSpecified(int value, absl::optional<int>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700242 if (value != kParameterNotSpecifiedValue) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100243 *parameter = value;
peah60a189f2016-05-24 20:54:40 -0700244 }
245}
246
Per Åhgrenef349602019-04-12 16:26:34 +0200247void SetSettingIfSpecified(float value, absl::optional<float>* parameter) {
248 constexpr float kFloatParameterNotSpecifiedValue =
249 kParameterNotSpecifiedValue;
250 if (value != kFloatParameterNotSpecifiedValue) {
251 *parameter = value;
252 }
253}
254
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200255void SetSettingIfFlagSet(int32_t flag, absl::optional<bool>* parameter) {
peah60a189f2016-05-24 20:54:40 -0700256 if (flag == 0) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100257 *parameter = false;
peah60a189f2016-05-24 20:54:40 -0700258 } else if (flag == 1) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100259 *parameter = true;
peah60a189f2016-05-24 20:54:40 -0700260 }
261}
262
Alessio Bazzica68170382018-11-20 12:27:20 +0100263AudioProcessing::Config::GainController2::LevelEstimator
264MapAgc2AdaptiveLevelEstimator(absl::string_view name) {
265 if (name.compare("RMS") == 0) {
266 return AudioProcessing::Config::GainController2::LevelEstimator::kRms;
267 }
268 if (name.compare("peak") == 0) {
269 return AudioProcessing::Config::GainController2::LevelEstimator::kPeak;
270 }
271 auto concat_strings =
272 [](const std::vector<std::string>& strings) -> std::string {
273 rtc::StringBuilder ss;
274 for (const auto& s : strings) {
275 ss << " " << s;
276 }
277 return ss.Release();
278 };
279 RTC_CHECK(false)
280 << "Invalid value for agc2_adaptive_level_estimator, valid options:"
281 << concat_strings(GetAgc2AdaptiveLevelEstimatorNames()) << ".";
282}
283
peah60a189f2016-05-24 20:54:40 -0700284SimulationSettings CreateSettings() {
285 SimulationSettings settings;
oprypin6e09d872017-08-31 03:21:39 -0700286 if (FLAG_all_default) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100287 settings.use_le = true;
288 settings.use_vad = true;
289 settings.use_ie = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100290 settings.use_ts = true;
291 settings.use_ns = true;
292 settings.use_hpf = true;
293 settings.use_agc = true;
294 settings.use_agc2 = false;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200295 settings.use_pre_amplifier = false;
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100296 settings.use_aec = true;
297 settings.use_aecm = false;
298 settings.use_ed = false;
peah60a189f2016-05-24 20:54:40 -0700299 }
oprypin6e09d872017-08-31 03:21:39 -0700300 SetSettingIfSpecified(FLAG_dump_input, &settings.aec_dump_input_filename);
301 SetSettingIfSpecified(FLAG_dump_output, &settings.aec_dump_output_filename);
302 SetSettingIfSpecified(FLAG_i, &settings.input_filename);
303 SetSettingIfSpecified(FLAG_o, &settings.output_filename);
304 SetSettingIfSpecified(FLAG_ri, &settings.reverse_input_filename);
305 SetSettingIfSpecified(FLAG_ro, &settings.reverse_output_filename);
306 SetSettingIfSpecified(FLAG_artificial_nearend,
peahdf80fd12016-12-09 02:43:40 -0800307 &settings.artificial_nearend_filename);
oprypin6e09d872017-08-31 03:21:39 -0700308 SetSettingIfSpecified(FLAG_output_num_channels,
peah60a189f2016-05-24 20:54:40 -0700309 &settings.output_num_channels);
oprypin6e09d872017-08-31 03:21:39 -0700310 SetSettingIfSpecified(FLAG_reverse_output_num_channels,
peah60a189f2016-05-24 20:54:40 -0700311 &settings.reverse_output_num_channels);
oprypin6e09d872017-08-31 03:21:39 -0700312 SetSettingIfSpecified(FLAG_output_sample_rate_hz,
peah60a189f2016-05-24 20:54:40 -0700313 &settings.output_sample_rate_hz);
oprypin6e09d872017-08-31 03:21:39 -0700314 SetSettingIfSpecified(FLAG_reverse_output_sample_rate_hz,
peah60a189f2016-05-24 20:54:40 -0700315 &settings.reverse_output_sample_rate_hz);
oprypin6e09d872017-08-31 03:21:39 -0700316 SetSettingIfFlagSet(FLAG_aec, &settings.use_aec);
317 SetSettingIfFlagSet(FLAG_aecm, &settings.use_aecm);
318 SetSettingIfFlagSet(FLAG_ed, &settings.use_ed);
319 SetSettingIfSpecified(FLAG_ed_graph, &settings.ed_graph_output_filename);
320 SetSettingIfFlagSet(FLAG_agc, &settings.use_agc);
321 SetSettingIfFlagSet(FLAG_agc2, &settings.use_agc2);
Alex Loiko5feb30e2018-04-16 13:52:32 +0200322 SetSettingIfFlagSet(FLAG_pre_amplifier, &settings.use_pre_amplifier);
oprypin6e09d872017-08-31 03:21:39 -0700323 SetSettingIfFlagSet(FLAG_hpf, &settings.use_hpf);
324 SetSettingIfFlagSet(FLAG_ns, &settings.use_ns);
325 SetSettingIfFlagSet(FLAG_ts, &settings.use_ts);
oprypin6e09d872017-08-31 03:21:39 -0700326 SetSettingIfFlagSet(FLAG_vad, &settings.use_vad);
327 SetSettingIfFlagSet(FLAG_le, &settings.use_le);
328 SetSettingIfSpecified(FLAG_aec_suppression_level,
peah60a189f2016-05-24 20:54:40 -0700329 &settings.aec_suppression_level);
oprypin6e09d872017-08-31 03:21:39 -0700330 SetSettingIfFlagSet(FLAG_delay_agnostic, &settings.use_delay_agnostic);
331 SetSettingIfFlagSet(FLAG_extended_filter, &settings.use_extended_filter);
oprypin6e09d872017-08-31 03:21:39 -0700332 SetSettingIfFlagSet(FLAG_refined_adaptive_filter,
peah60a189f2016-05-24 20:54:40 -0700333 &settings.use_refined_adaptive_filter);
334
Per Åhgren200feba2019-03-06 04:16:46 +0100335 SetSettingIfFlagSet(FLAG_use_legacy_aec, &settings.use_legacy_aec);
oprypin6e09d872017-08-31 03:21:39 -0700336 SetSettingIfFlagSet(FLAG_experimental_agc, &settings.use_experimental_agc);
Alex Loiko9489c3a2018-08-09 15:04:24 +0200337 SetSettingIfFlagSet(FLAG_experimental_agc_disable_digital_adaptive,
338 &settings.experimental_agc_disable_digital_adaptive);
Alex Loikod9342442018-09-10 13:59:41 +0200339 SetSettingIfFlagSet(FLAG_experimental_agc_analyze_before_aec,
340 &settings.experimental_agc_analyze_before_aec);
341 SetSettingIfFlagSet(FLAG_experimental_agc_agc2_level_estimator,
342 &settings.use_experimental_agc_agc2_level_estimator);
oprypin6e09d872017-08-31 03:21:39 -0700343 SetSettingIfSpecified(FLAG_agc_mode, &settings.agc_mode);
344 SetSettingIfSpecified(FLAG_agc_target_level, &settings.agc_target_level);
345 SetSettingIfFlagSet(FLAG_agc_limiter, &settings.use_agc_limiter);
346 SetSettingIfSpecified(FLAG_agc_compression_gain,
peah60a189f2016-05-24 20:54:40 -0700347 &settings.agc_compression_gain);
Per Åhgrene4d23b12018-10-02 23:40:07 +0200348 SetSettingIfFlagSet(FLAG_agc2_enable_adaptive_gain,
349 &settings.agc2_use_adaptive_gain);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200350 SetSettingIfSpecified(FLAG_agc2_fixed_gain_db, &settings.agc2_fixed_gain_db);
Alessio Bazzica68170382018-11-20 12:27:20 +0100351 settings.agc2_adaptive_level_estimator =
352 MapAgc2AdaptiveLevelEstimator(FLAG_agc2_adaptive_level_estimator);
Per Åhgrenef349602019-04-12 16:26:34 +0200353 SetSettingIfSpecified(FLAG_pre_amplifier_gain_factor,
354 &settings.pre_amplifier_gain_factor);
oprypin6e09d872017-08-31 03:21:39 -0700355 SetSettingIfSpecified(FLAG_vad_likelihood, &settings.vad_likelihood);
356 SetSettingIfSpecified(FLAG_ns_level, &settings.ns_level);
357 SetSettingIfSpecified(FLAG_stream_delay, &settings.stream_delay);
Per Åhgrenc5efb0c2018-05-28 09:10:59 +0200358 SetSettingIfFlagSet(FLAG_use_stream_delay, &settings.use_stream_delay);
oprypin6e09d872017-08-31 03:21:39 -0700359 SetSettingIfSpecified(FLAG_stream_drift_samples,
peah60a189f2016-05-24 20:54:40 -0700360 &settings.stream_drift_samples);
oprypin6e09d872017-08-31 03:21:39 -0700361 SetSettingIfSpecified(FLAG_custom_call_order_file,
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100362 &settings.call_order_input_filename);
363 SetSettingIfSpecified(FLAG_output_custom_call_order_file,
364 &settings.call_order_output_filename);
Per Åhgren200feba2019-03-06 04:16:46 +0100365 SetSettingIfSpecified(FLAG_aec_settings, &settings.aec_settings_filename);
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200366 settings.initial_mic_level = FLAG_initial_mic_level;
367 settings.simulate_mic_gain = FLAG_simulate_mic_gain;
368 SetSettingIfSpecified(FLAG_simulated_mic_kind, &settings.simulated_mic_kind);
oprypin6e09d872017-08-31 03:21:39 -0700369 settings.report_performance = FLAG_performance_report;
Per Åhgrenada9b892019-04-03 16:06:42 +0200370 SetSettingIfSpecified(FLAG_performance_report_output_file,
371 &settings.performance_report_output_filename);
oprypin6e09d872017-08-31 03:21:39 -0700372 settings.use_verbose_logging = FLAG_verbose;
Per Åhgren5a72a5e2018-08-24 07:18:24 +0200373 settings.use_quiet_output = FLAG_quiet;
oprypin6e09d872017-08-31 03:21:39 -0700374 settings.report_bitexactness = FLAG_bitexactness_report;
375 settings.discard_all_settings_in_aecdump = FLAG_discard_settings_in_aecdump;
376 settings.fixed_interface = FLAG_fixed_interface;
377 settings.store_intermediate_output = FLAG_store_intermediate_output;
Per Åhgren200feba2019-03-06 04:16:46 +0100378 settings.print_aec_parameter_values = FLAG_print_aec_parameter_values;
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200379 settings.dump_internal_data = FLAG_dump_data;
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100380 SetSettingIfSpecified(FLAG_dump_data_output_dir,
381 &settings.dump_internal_data_output_dir);
peah60a189f2016-05-24 20:54:40 -0700382
383 return settings;
384}
385
Alex Loiko890988c2017-08-31 10:25:48 +0200386void ReportConditionalErrorAndExit(bool condition, const std::string& message) {
peah60a189f2016-05-24 20:54:40 -0700387 if (condition) {
388 std::cerr << message << std::endl;
389 exit(1);
390 }
391}
392
393void PerformBasicParameterSanityChecks(const SimulationSettings& settings) {
394 if (settings.input_filename || settings.reverse_input_filename) {
395 ReportConditionalErrorAndExit(!!settings.aec_dump_input_filename,
396 "Error: The aec dump cannot be specified "
397 "together with input wav files!\n");
398
peahdf80fd12016-12-09 02:43:40 -0800399 ReportConditionalErrorAndExit(!!settings.artificial_nearend_filename,
400 "Error: The artificial nearend cannot be "
401 "specified together with input wav files!\n");
402
peah60a189f2016-05-24 20:54:40 -0700403 ReportConditionalErrorAndExit(!settings.input_filename,
404 "Error: When operating at wav files, the "
405 "input wav filename must be "
406 "specified!\n");
407
408 ReportConditionalErrorAndExit(
409 settings.reverse_output_filename && !settings.reverse_input_filename,
410 "Error: When operating at wav files, the reverse input wav filename "
411 "must be specified if the reverse output wav filename is specified!\n");
412 } else {
413 ReportConditionalErrorAndExit(!settings.aec_dump_input_filename,
414 "Error: Either the aec dump or the wav "
415 "input files must be specified!\n");
416 }
417
418 ReportConditionalErrorAndExit(
419 settings.use_aec && *settings.use_aec && settings.use_aecm &&
420 *settings.use_aecm,
421 "Error: The AEC and the AECM cannot be activated at the same time!\n");
422
423 ReportConditionalErrorAndExit(
424 settings.output_sample_rate_hz && *settings.output_sample_rate_hz <= 0,
425 "Error: --output_sample_rate_hz must be positive!\n");
426
427 ReportConditionalErrorAndExit(
428 settings.reverse_output_sample_rate_hz &&
429 settings.output_sample_rate_hz &&
430 *settings.output_sample_rate_hz <= 0,
431 "Error: --reverse_output_sample_rate_hz must be positive!\n");
432
433 ReportConditionalErrorAndExit(
434 settings.output_num_channels && *settings.output_num_channels <= 0,
435 "Error: --output_num_channels must be positive!\n");
436
437 ReportConditionalErrorAndExit(
438 settings.reverse_output_num_channels &&
439 *settings.reverse_output_num_channels <= 0,
440 "Error: --reverse_output_num_channels must be positive!\n");
441
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200442 ReportConditionalErrorAndExit(settings.aec_suppression_level &&
443 ((*settings.aec_suppression_level) < 1 ||
444 (*settings.aec_suppression_level) > 2),
445 "Error: --aec_suppression_level must be "
446 "specified between 1 and 2. 0 is "
447 "deprecated.\n");
peah60a189f2016-05-24 20:54:40 -0700448
449 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700450 settings.agc_target_level && ((*settings.agc_target_level) < 0 ||
451 (*settings.agc_target_level) > 31),
452 "Error: --agc_target_level must be specified between 0 and 31.\n");
453
454 ReportConditionalErrorAndExit(
455 settings.agc_compression_gain && ((*settings.agc_compression_gain) < 0 ||
456 (*settings.agc_compression_gain) > 90),
457 "Error: --agc_compression_gain must be specified between 0 and 90.\n");
458
459 ReportConditionalErrorAndExit(
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200460 settings.agc2_fixed_gain_db && ((*settings.agc2_fixed_gain_db) < 0 ||
461 (*settings.agc2_fixed_gain_db) > 90),
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200462 "Error: --agc2_fixed_gain_db must be specified between 0 and 90.\n");
463
464 ReportConditionalErrorAndExit(
peah60a189f2016-05-24 20:54:40 -0700465 settings.vad_likelihood &&
466 ((*settings.vad_likelihood) < 0 || (*settings.vad_likelihood) > 3),
467 "Error: --vad_likelihood must be specified between 0 and 3.\n");
468
469 ReportConditionalErrorAndExit(
470 settings.ns_level &&
471 ((*settings.ns_level) < 0 || (*settings.ns_level) > 3),
472 "Error: --ns_level must be specified between 0 and 3.\n");
473
474 ReportConditionalErrorAndExit(
475 settings.report_bitexactness && !settings.aec_dump_input_filename,
476 "Error: --bitexactness_report can only be used when operating on an "
477 "aecdump\n");
478
peah5ad5de32016-12-09 03:18:22 -0800479 ReportConditionalErrorAndExit(
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100480 settings.call_order_input_filename && settings.aec_dump_input_filename,
peah5ad5de32016-12-09 03:18:22 -0800481 "Error: --custom_call_order_file cannot be used when operating on an "
482 "aecdump\n");
483
Alessio Bazzicaca90a552017-09-27 11:51:10 +0200484 ReportConditionalErrorAndExit(
485 (settings.initial_mic_level < 0 || settings.initial_mic_level > 255),
486 "Error: --initial_mic_level must be specified between 0 and 255.\n");
487
488 ReportConditionalErrorAndExit(
489 settings.simulated_mic_kind && !settings.simulate_mic_gain,
490 "Error: --simulated_mic_kind cannot be specified when mic simulation is "
491 "disabled\n");
492
493 ReportConditionalErrorAndExit(
494 !settings.simulated_mic_kind && settings.simulate_mic_gain,
495 "Error: --simulated_mic_kind must be specified when mic simulation is "
496 "enabled\n");
497
peah60a189f2016-05-24 20:54:40 -0700498 auto valid_wav_name = [](const std::string& wav_file_name) {
499 if (wav_file_name.size() < 5) {
500 return false;
501 }
502 if ((wav_file_name.compare(wav_file_name.size() - 4, 4, ".wav") == 0) ||
503 (wav_file_name.compare(wav_file_name.size() - 4, 4, ".WAV") == 0)) {
504 return true;
505 }
506 return false;
507 };
508
509 ReportConditionalErrorAndExit(
510 settings.input_filename && (!valid_wav_name(*settings.input_filename)),
511 "Error: --i must be a valid .wav file name.\n");
512
513 ReportConditionalErrorAndExit(
514 settings.output_filename && (!valid_wav_name(*settings.output_filename)),
515 "Error: --o must be a valid .wav file name.\n");
516
517 ReportConditionalErrorAndExit(
518 settings.reverse_input_filename &&
519 (!valid_wav_name(*settings.reverse_input_filename)),
520 "Error: --ri must be a valid .wav file name.\n");
521
522 ReportConditionalErrorAndExit(
523 settings.reverse_output_filename &&
524 (!valid_wav_name(*settings.reverse_output_filename)),
525 "Error: --ro must be a valid .wav file name.\n");
peahdf80fd12016-12-09 02:43:40 -0800526
527 ReportConditionalErrorAndExit(
528 settings.artificial_nearend_filename &&
529 !valid_wav_name(*settings.artificial_nearend_filename),
530 "Error: --artifical_nearend must be a valid .wav file name.\n");
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200531
532 ReportConditionalErrorAndExit(
533 WEBRTC_APM_DEBUG_DUMP == 0 && settings.dump_internal_data,
534 "Error: --dump_data cannot be set without proper build support.\n");
Alessio Bazzica4bc60452018-11-20 12:44:15 +0100535
536 ReportConditionalErrorAndExit(
537 !settings.dump_internal_data &&
538 settings.dump_internal_data_output_dir.has_value(),
539 "Error: --dump_data_output_dir cannot be set without --dump_data.\n");
Ivo Creusen9a66d5e2019-03-13 13:55:12 +0100540
541 ReportConditionalErrorAndExit(
542 !settings.aec_dump_input_filename &&
543 settings.call_order_output_filename.has_value(),
544 "Error: --output_custom_call_order_file needs an AEC dump input file.\n");
Per Åhgrenef349602019-04-12 16:26:34 +0200545
546 ReportConditionalErrorAndExit(
547 (!settings.use_pre_amplifier || !(*settings.use_pre_amplifier)) &&
548 settings.pre_amplifier_gain_factor.has_value(),
549 "Error: --pre_amplifier_gain_factor needs --pre_amplifier to be "
550 "specified and set.\n");
Peter Kasting69558702016-01-12 16:26:35 -0800551}
552
553} // namespace
554
Ivo Creusen2cb41052018-03-15 12:22:52 +0100555int AudioprocFloatImpl(std::unique_ptr<AudioProcessingBuilder> ap_builder,
556 int argc,
557 char* argv[]) {
Yves Gerey665174f2018-06-19 15:03:05 +0200558 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true) || FLAG_help ||
559 argc != 1) {
oprypin6e09d872017-08-31 03:21:39 -0700560 printf("%s", kUsageDescription);
561 if (FLAG_help) {
562 rtc::FlagList::Print(nullptr, false);
563 return 0;
564 }
565 return 1;
566 }
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000567
peah60a189f2016-05-24 20:54:40 -0700568 SimulationSettings settings = CreateSettings();
569 PerformBasicParameterSanityChecks(settings);
570 std::unique_ptr<AudioProcessingSimulator> processor;
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000571
peah60a189f2016-05-24 20:54:40 -0700572 if (settings.aec_dump_input_filename) {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100573 processor.reset(new AecDumpBasedSimulator(settings, std::move(ap_builder)));
aluebsb0ad43b2015-11-20 00:11:53 -0800574 } else {
Ivo Creusen8c812f32018-03-09 17:33:04 +0100575 processor.reset(new WavBasedSimulator(settings, std::move(ap_builder)));
andrewbdafe312015-10-29 23:42:54 -0700576 }
577
peah60a189f2016-05-24 20:54:40 -0700578 processor->Process();
aluebsb0ad43b2015-11-20 00:11:53 -0800579
peah60a189f2016-05-24 20:54:40 -0700580 if (settings.report_performance) {
Per Åhgrenada9b892019-04-03 16:06:42 +0200581 processor->GetApiCallStatistics().PrintReport();
582 }
583 if (settings.performance_report_output_filename) {
584 processor->GetApiCallStatistics().WriteReportToFile(
585 *settings.performance_report_output_filename);
peah60a189f2016-05-24 20:54:40 -0700586 }
587
588 if (settings.report_bitexactness && settings.aec_dump_input_filename) {
589 if (processor->OutputWasBitexact()) {
590 std::cout << "The processing was bitexact.";
591 } else {
592 std::cout << "The processing was not bitexact.";
593 }
Alejandro Luebs5d22c002015-04-15 11:26:40 -0700594 }
aluebsb0ad43b2015-11-20 00:11:53 -0800595
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000596 return 0;
597}
598
peah60a189f2016-05-24 20:54:40 -0700599} // namespace test
andrew@webrtc.org08df9b22014-12-16 20:57:15 +0000600} // namespace webrtc