niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Alejandro Luebs | cb3f9bd | 2015-10-29 18:21:34 -0700 | [diff] [blame] | 14 | // MSVC++ requires this to be set before any other includes to get M_PI. |
Patrik Höglund | 3ff90f1 | 2017-12-12 14:41:53 +0100 | [diff] [blame] | 15 | #ifndef _USE_MATH_DEFINES |
Alejandro Luebs | cb3f9bd | 2015-10-29 18:21:34 -0700 | [diff] [blame] | 16 | #define _USE_MATH_DEFINES |
Patrik Höglund | 3ff90f1 | 2017-12-12 14:41:53 +0100 | [diff] [blame] | 17 | #endif |
Alejandro Luebs | cb3f9bd | 2015-10-29 18:21:34 -0700 | [diff] [blame] | 18 | |
| 19 | #include <math.h> |
andrew@webrtc.org | d72b3d6 | 2012-11-15 21:46:06 +0000 | [diff] [blame] | 20 | #include <stddef.h> // size_t |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 21 | #include <stdio.h> // FILE |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 22 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 23 | |
aluebs@webrtc.org | fb7a039 | 2015-01-05 21:58:58 +0000 | [diff] [blame] | 24 | #include <vector> |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 25 | |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 26 | #include "absl/types/optional.h" |
Gustaf Ullberg | bffa300 | 2018-02-14 15:12:00 +0100 | [diff] [blame] | 27 | #include "api/audio/echo_canceller3_config.h" |
Gustaf Ullberg | fd4ce50 | 2018-02-15 10:09:09 +0100 | [diff] [blame] | 28 | #include "api/audio/echo_control.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 29 | #include "api/scoped_refptr.h" |
Sam Zackrisson | 4d36449 | 2018-03-02 16:03:21 +0100 | [diff] [blame] | 30 | #include "modules/audio_processing/include/audio_generator.h" |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 31 | #include "modules/audio_processing/include/audio_processing_statistics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "modules/audio_processing/include/config.h" |
| 33 | #include "rtc_base/arraysize.h" |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 34 | #include "rtc_base/deprecation.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 35 | #include "rtc_base/ref_count.h" |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 36 | #include "rtc_base/system/rtc_export.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
| 38 | namespace webrtc { |
| 39 | |
peah | 50e21bd | 2016-03-05 08:39:21 -0800 | [diff] [blame] | 40 | struct AecCore; |
| 41 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 42 | class AecDump; |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 43 | class AudioBuffer; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | class AudioFrame; |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 45 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 46 | class StreamConfig; |
| 47 | class ProcessingConfig; |
| 48 | |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 49 | class EchoDetector; |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 50 | class CustomAudioAnalyzer; |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame] | 51 | class CustomProcessing; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 53 | // Use to enable the extended filter mode in the AEC, along with robustness |
| 54 | // measures around the reported system delays. It comes with a significant |
| 55 | // increase in AEC complexity, but is much more robust to unreliable reported |
| 56 | // delays. |
andrew@webrtc.org | 6b1e219 | 2013-09-25 23:46:20 +0000 | [diff] [blame] | 57 | // |
| 58 | // Detailed changes to the algorithm: |
| 59 | // - The filter length is changed from 48 to 128 ms. This comes with tuning of |
| 60 | // several parameters: i) filter adaptation stepsize and error threshold; |
| 61 | // ii) non-linear processing smoothing and overdrive. |
| 62 | // - Option to ignore the reported delays on platforms which we deem |
| 63 | // sufficiently unreliable. See WEBRTC_UNTRUSTED_DELAY in echo_cancellation.c. |
| 64 | // - Faster startup times by removing the excessive "startup phase" processing |
| 65 | // of reported delays. |
| 66 | // - Much more conservative adjustments to the far-end read pointer. We smooth |
| 67 | // the delay difference more heavily, and back off from the difference more. |
| 68 | // Adjustments force a readaptation of the filter, so they should be avoided |
| 69 | // except when really necessary. |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 70 | struct ExtendedFilter { |
| 71 | ExtendedFilter() : enabled(false) {} |
| 72 | explicit ExtendedFilter(bool enabled) : enabled(enabled) {} |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 73 | static const ConfigOptionID identifier = ConfigOptionID::kExtendedFilter; |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 74 | bool enabled; |
| 75 | }; |
andrew@webrtc.org | 6b1e219 | 2013-09-25 23:46:20 +0000 | [diff] [blame] | 76 | |
peah | 0332c2d | 2016-04-15 11:23:33 -0700 | [diff] [blame] | 77 | // Enables the refined linear filter adaptation in the echo canceller. |
saza | be490b2 | 2018-10-03 17:03:13 +0200 | [diff] [blame] | 78 | // This configuration only applies to non-mobile echo cancellation. |
| 79 | // It can be set in the constructor or using AudioProcessing::SetExtraOptions(). |
peah | 0332c2d | 2016-04-15 11:23:33 -0700 | [diff] [blame] | 80 | struct RefinedAdaptiveFilter { |
| 81 | RefinedAdaptiveFilter() : enabled(false) {} |
| 82 | explicit RefinedAdaptiveFilter(bool enabled) : enabled(enabled) {} |
| 83 | static const ConfigOptionID identifier = |
| 84 | ConfigOptionID::kAecRefinedAdaptiveFilter; |
| 85 | bool enabled; |
| 86 | }; |
| 87 | |
henrik.lundin | 366e952 | 2015-07-03 00:50:05 -0700 | [diff] [blame] | 88 | // Enables delay-agnostic echo cancellation. This feature relies on internally |
| 89 | // estimated delays between the process and reverse streams, thus not relying |
saza | be490b2 | 2018-10-03 17:03:13 +0200 | [diff] [blame] | 90 | // on reported system delays. This configuration only applies to non-mobile echo |
| 91 | // cancellation. It can be set in the constructor or using |
| 92 | // AudioProcessing::SetExtraOptions(). |
henrik.lundin | 0f133b9 | 2015-07-02 00:17:55 -0700 | [diff] [blame] | 93 | struct DelayAgnostic { |
| 94 | DelayAgnostic() : enabled(false) {} |
| 95 | explicit DelayAgnostic(bool enabled) : enabled(enabled) {} |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 96 | static const ConfigOptionID identifier = ConfigOptionID::kDelayAgnostic; |
henrik.lundin | 0f133b9 | 2015-07-02 00:17:55 -0700 | [diff] [blame] | 97 | bool enabled; |
| 98 | }; |
bjornv@webrtc.org | 3f83072 | 2014-06-11 04:48:11 +0000 | [diff] [blame] | 99 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 100 | // Use to enable experimental gain control (AGC). At startup the experimental |
| 101 | // AGC moves the microphone volume up to |startup_min_volume| if the current |
| 102 | // microphone volume is set too low. The value is clamped to its operating range |
| 103 | // [12, 255]. Here, 255 maps to 100%. |
| 104 | // |
Ivo Creusen | 62337e5 | 2018-01-09 14:17:33 +0100 | [diff] [blame] | 105 | // Must be provided through AudioProcessingBuilder().Create(config). |
Bjorn Volcker | fb49451 | 2015-04-22 06:39:58 +0200 | [diff] [blame] | 106 | #if defined(WEBRTC_CHROMIUM_BUILD) |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 107 | static const int kAgcStartupMinVolume = 85; |
Bjorn Volcker | fb49451 | 2015-04-22 06:39:58 +0200 | [diff] [blame] | 108 | #else |
| 109 | static const int kAgcStartupMinVolume = 0; |
| 110 | #endif // defined(WEBRTC_CHROMIUM_BUILD) |
Henrik Lundin | e3a4da9 | 2017-11-06 11:42:21 +0100 | [diff] [blame] | 111 | static constexpr int kClippedLevelMin = 70; |
andrew@webrtc.org | c7c7a53 | 2014-01-29 04:57:25 +0000 | [diff] [blame] | 112 | struct ExperimentalAgc { |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 113 | ExperimentalAgc() = default; |
| 114 | explicit ExperimentalAgc(bool enabled) : enabled(enabled) {} |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 115 | ExperimentalAgc(bool enabled, |
| 116 | bool enabled_agc2_level_estimator, |
Per Åhgren | b8c1be5 | 2019-11-07 20:35:50 +0100 | [diff] [blame] | 117 | bool digital_adaptive_disabled) |
| 118 | : enabled(enabled), |
| 119 | enabled_agc2_level_estimator(enabled_agc2_level_estimator), |
| 120 | digital_adaptive_disabled(digital_adaptive_disabled) {} |
| 121 | // Deprecated constructor: will be removed. |
| 122 | ExperimentalAgc(bool enabled, |
| 123 | bool enabled_agc2_level_estimator, |
Alex Loiko | d934244 | 2018-09-10 13:59:41 +0200 | [diff] [blame] | 124 | bool digital_adaptive_disabled, |
| 125 | bool analyze_before_aec) |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 126 | : enabled(enabled), |
| 127 | enabled_agc2_level_estimator(enabled_agc2_level_estimator), |
Per Åhgren | b8c1be5 | 2019-11-07 20:35:50 +0100 | [diff] [blame] | 128 | digital_adaptive_disabled(digital_adaptive_disabled) {} |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 129 | ExperimentalAgc(bool enabled, int startup_min_volume) |
| 130 | : enabled(enabled), startup_min_volume(startup_min_volume) {} |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 131 | ExperimentalAgc(bool enabled, int startup_min_volume, int clipped_level_min) |
| 132 | : enabled(enabled), |
| 133 | startup_min_volume(startup_min_volume), |
| 134 | clipped_level_min(clipped_level_min) {} |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 135 | static const ConfigOptionID identifier = ConfigOptionID::kExperimentalAgc; |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 136 | bool enabled = true; |
| 137 | int startup_min_volume = kAgcStartupMinVolume; |
| 138 | // Lowest microphone level that will be applied in response to clipping. |
| 139 | int clipped_level_min = kClippedLevelMin; |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 140 | bool enabled_agc2_level_estimator = false; |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 141 | bool digital_adaptive_disabled = false; |
andrew@webrtc.org | 6b1e219 | 2013-09-25 23:46:20 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
aluebs@webrtc.org | 9825afc | 2014-06-30 17:39:53 +0000 | [diff] [blame] | 144 | // Use to enable experimental noise suppression. It can be set in the |
| 145 | // constructor or using AudioProcessing::SetExtraOptions(). |
| 146 | struct ExperimentalNs { |
| 147 | ExperimentalNs() : enabled(false) {} |
| 148 | explicit ExperimentalNs(bool enabled) : enabled(enabled) {} |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 149 | static const ConfigOptionID identifier = ConfigOptionID::kExperimentalNs; |
aluebs@webrtc.org | 9825afc | 2014-06-30 17:39:53 +0000 | [diff] [blame] | 150 | bool enabled; |
| 151 | }; |
| 152 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | // The Audio Processing Module (APM) provides a collection of voice processing |
| 154 | // components designed for real-time communications software. |
| 155 | // |
| 156 | // APM operates on two audio streams on a frame-by-frame basis. Frames of the |
| 157 | // primary stream, on which all processing is applied, are passed to |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 158 | // |ProcessStream()|. Frames of the reverse direction stream are passed to |
| 159 | // |ProcessReverseStream()|. On the client-side, this will typically be the |
| 160 | // near-end (capture) and far-end (render) streams, respectively. APM should be |
| 161 | // placed in the signal chain as close to the audio hardware abstraction layer |
| 162 | // (HAL) as possible. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | // |
| 164 | // On the server-side, the reverse stream will normally not be used, with |
| 165 | // processing occurring on each incoming stream. |
| 166 | // |
| 167 | // Component interfaces follow a similar pattern and are accessed through |
| 168 | // corresponding getters in APM. All components are disabled at create-time, |
| 169 | // with default settings that are recommended for most situations. New settings |
| 170 | // can be applied without enabling a component. Enabling a component triggers |
| 171 | // memory allocation and initialization to allow it to start processing the |
| 172 | // streams. |
| 173 | // |
| 174 | // Thread safety is provided with the following assumptions to reduce locking |
| 175 | // overhead: |
| 176 | // 1. The stream getters and setters are called from the same thread as |
| 177 | // ProcessStream(). More precisely, stream functions are never called |
| 178 | // concurrently with ProcessStream(). |
| 179 | // 2. Parameter getters are never called concurrently with the corresponding |
| 180 | // setter. |
| 181 | // |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 182 | // APM accepts only linear PCM audio data in chunks of 10 ms. The int16 |
| 183 | // interfaces use interleaved data, while the float interfaces use deinterleaved |
| 184 | // data. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | // |
| 186 | // Usage example, omitting error checking: |
Ivo Creusen | 62337e5 | 2018-01-09 14:17:33 +0100 | [diff] [blame] | 187 | // AudioProcessing* apm = AudioProcessingBuilder().Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | // |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 189 | // AudioProcessing::Config config; |
Sam Zackrisson | cdf0e6d | 2018-09-17 11:05:17 +0200 | [diff] [blame] | 190 | // config.echo_canceller.enabled = true; |
| 191 | // config.echo_canceller.mobile_mode = false; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 192 | // |
| 193 | // config.gain_controller1.enabled = true; |
| 194 | // config.gain_controller1.mode = |
| 195 | // AudioProcessing::Config::GainController1::kAdaptiveAnalog; |
| 196 | // config.gain_controller1.analog_level_minimum = 0; |
| 197 | // config.gain_controller1.analog_level_maximum = 255; |
| 198 | // |
Sam Zackrisson | ab1aee0 | 2018-03-05 15:59:06 +0100 | [diff] [blame] | 199 | // config.gain_controller2.enabled = true; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 200 | // |
| 201 | // config.high_pass_filter.enabled = true; |
| 202 | // |
| 203 | // config.voice_detection.enabled = true; |
| 204 | // |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 205 | // apm->ApplyConfig(config) |
| 206 | // |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | // apm->noise_reduction()->set_level(kHighSuppression); |
| 208 | // apm->noise_reduction()->Enable(true); |
| 209 | // |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | // // Start a voice call... |
| 211 | // |
| 212 | // // ... Render frame arrives bound for the audio HAL ... |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 213 | // apm->ProcessReverseStream(render_frame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | // |
| 215 | // // ... Capture frame arrives from the audio HAL ... |
| 216 | // // Call required set_stream_ functions. |
| 217 | // apm->set_stream_delay_ms(delay_ms); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 218 | // apm->set_stream_analog_level(analog_level); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | // |
| 220 | // apm->ProcessStream(capture_frame); |
| 221 | // |
| 222 | // // Call required stream_ functions. |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 223 | // analog_level = apm->recommended_stream_analog_level(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | // has_voice = apm->stream_has_voice(); |
| 225 | // |
| 226 | // // Repeate render and capture processing for the duration of the call... |
| 227 | // // Start a new call... |
| 228 | // apm->Initialize(); |
| 229 | // |
| 230 | // // Close the application... |
andrew@webrtc.org | f3930e9 | 2013-09-18 22:37:32 +0000 | [diff] [blame] | 231 | // delete apm; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | // |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 233 | class RTC_EXPORT AudioProcessing : public rtc::RefCountInterface { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 234 | public: |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 235 | // The struct below constitutes the new parameter scheme for the audio |
| 236 | // processing. It is being introduced gradually and until it is fully |
| 237 | // introduced, it is prone to change. |
| 238 | // TODO(peah): Remove this comment once the new config scheme is fully rolled |
| 239 | // out. |
| 240 | // |
| 241 | // The parameters and behavior of the audio processing module are controlled |
| 242 | // by changing the default values in the AudioProcessing::Config struct. |
| 243 | // The config is applied by passing the struct to the ApplyConfig method. |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 244 | // |
| 245 | // This config is intended to be used during setup, and to enable/disable |
| 246 | // top-level processing effects. Use during processing may cause undesired |
| 247 | // submodule resets, affecting the audio quality. Use the RuntimeSetting |
| 248 | // construct for runtime configuration. |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame^] | 249 | struct RTC_EXPORT Config { |
Per Åhgren | fcbe407 | 2019-09-15 00:27:58 +0200 | [diff] [blame] | 250 | // Sets the properties of the audio processing pipeline. |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame^] | 251 | struct RTC_EXPORT Pipeline { |
Per Åhgren | fcbe407 | 2019-09-15 00:27:58 +0200 | [diff] [blame] | 252 | Pipeline(); |
| 253 | |
| 254 | // Maximum allowed processing rate used internally. May only be set to |
| 255 | // 32000 or 48000 and any differing values will be treated as 48000. The |
| 256 | // default rate is currently selected based on the CPU architecture, but |
| 257 | // that logic may change. |
| 258 | int maximum_internal_processing_rate; |
Sam Zackrisson | feee1e4 | 2019-09-20 07:50:35 +0200 | [diff] [blame] | 259 | // Force multi-channel processing on playout and capture audio. This is an |
| 260 | // experimental feature, and is likely to change without warning. |
| 261 | bool experimental_multi_channel = false; |
Per Åhgren | fcbe407 | 2019-09-15 00:27:58 +0200 | [diff] [blame] | 262 | } pipeline; |
| 263 | |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 264 | // Enabled the pre-amplifier. It amplifies the capture signal |
| 265 | // before any other processing is done. |
| 266 | struct PreAmplifier { |
| 267 | bool enabled = false; |
| 268 | float fixed_gain_factor = 1.f; |
| 269 | } pre_amplifier; |
| 270 | |
| 271 | struct HighPassFilter { |
| 272 | bool enabled = false; |
| 273 | } high_pass_filter; |
| 274 | |
Sam Zackrisson | 8b5d2cc | 2018-07-27 13:27:23 +0200 | [diff] [blame] | 275 | struct EchoCanceller { |
| 276 | bool enabled = false; |
| 277 | bool mobile_mode = false; |
Sam Zackrisson | a955849 | 2018-08-15 13:44:12 +0200 | [diff] [blame] | 278 | // Recommended not to use. Will be removed in the future. |
| 279 | // APM components are not fine-tuned for legacy suppression levels. |
| 280 | bool legacy_moderate_suppression_level = false; |
Per Åhgren | 03257b0 | 2019-02-28 10:52:26 +0100 | [diff] [blame] | 281 | // Recommended not to use. Will be removed in the future. |
| 282 | bool use_legacy_aec = false; |
Sam Zackrisson | 8b5d2cc | 2018-07-27 13:27:23 +0200 | [diff] [blame] | 283 | } echo_canceller; |
| 284 | |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 285 | // Enables background noise suppression. |
| 286 | struct NoiseSuppression { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 287 | bool enabled = false; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 288 | enum Level { kLow, kModerate, kHigh, kVeryHigh }; |
| 289 | Level level = kModerate; |
Per Åhgren | 0cbb58e | 2019-10-29 22:59:44 +0100 | [diff] [blame] | 290 | // Recommended not to use. Will be removed in the future. |
| 291 | bool use_legacy_ns = false; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 292 | } noise_suppression; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 293 | |
Sam Zackrisson | 0824c6f | 2019-10-07 14:03:56 +0200 | [diff] [blame] | 294 | // Enables reporting of |voice_detected| in webrtc::AudioProcessingStats. |
| 295 | // In addition to |voice_detected|, VAD decision is provided through the |
| 296 | // |AudioFrame| passed to |ProcessStream()|. The |vad_activity_| member will |
| 297 | // be modified to reflect the current decision. |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 298 | struct VoiceDetection { |
Alex Loiko | 5feb30e | 2018-04-16 13:52:32 +0200 | [diff] [blame] | 299 | bool enabled = false; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 300 | } voice_detection; |
Alex Loiko | 5feb30e | 2018-04-16 13:52:32 +0200 | [diff] [blame] | 301 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 302 | // Enables automatic gain control (AGC) functionality. |
| 303 | // The automatic gain control (AGC) component brings the signal to an |
| 304 | // appropriate range. This is done by applying a digital gain directly and, |
| 305 | // in the analog mode, prescribing an analog gain to be applied at the audio |
| 306 | // HAL. |
| 307 | // Recommended to be enabled on the client-side. |
| 308 | struct GainController1 { |
| 309 | bool enabled = false; |
| 310 | enum Mode { |
| 311 | // Adaptive mode intended for use if an analog volume control is |
| 312 | // available on the capture device. It will require the user to provide |
| 313 | // coupling between the OS mixer controls and AGC through the |
| 314 | // stream_analog_level() functions. |
| 315 | // It consists of an analog gain prescription for the audio device and a |
| 316 | // digital compression stage. |
| 317 | kAdaptiveAnalog, |
| 318 | // Adaptive mode intended for situations in which an analog volume |
| 319 | // control is unavailable. It operates in a similar fashion to the |
| 320 | // adaptive analog mode, but with scaling instead applied in the digital |
| 321 | // domain. As with the analog mode, it additionally uses a digital |
| 322 | // compression stage. |
| 323 | kAdaptiveDigital, |
| 324 | // Fixed mode which enables only the digital compression stage also used |
| 325 | // by the two adaptive modes. |
| 326 | // It is distinguished from the adaptive modes by considering only a |
| 327 | // short time-window of the input signal. It applies a fixed gain |
| 328 | // through most of the input level range, and compresses (gradually |
| 329 | // reduces gain with increasing level) the input signal at higher |
| 330 | // levels. This mode is preferred on embedded devices where the capture |
| 331 | // signal level is predictable, so that a known gain can be applied. |
| 332 | kFixedDigital |
| 333 | }; |
| 334 | Mode mode = kAdaptiveAnalog; |
| 335 | // Sets the target peak level (or envelope) of the AGC in dBFs (decibels |
| 336 | // from digital full-scale). The convention is to use positive values. For |
| 337 | // instance, passing in a value of 3 corresponds to -3 dBFs, or a target |
| 338 | // level 3 dB below full-scale. Limited to [0, 31]. |
| 339 | int target_level_dbfs = 3; |
| 340 | // Sets the maximum gain the digital compression stage may apply, in dB. A |
| 341 | // higher number corresponds to greater compression, while a value of 0 |
| 342 | // will leave the signal uncompressed. Limited to [0, 90]. |
| 343 | // For updates after APM setup, use a RuntimeSetting instead. |
| 344 | int compression_gain_db = 9; |
| 345 | // When enabled, the compression stage will hard limit the signal to the |
| 346 | // target level. Otherwise, the signal will be compressed but not limited |
| 347 | // above the target level. |
| 348 | bool enable_limiter = true; |
| 349 | // Sets the minimum and maximum analog levels of the audio capture device. |
| 350 | // Must be set if an analog mode is used. Limited to [0, 65535]. |
| 351 | int analog_level_minimum = 0; |
| 352 | int analog_level_maximum = 255; |
| 353 | } gain_controller1; |
| 354 | |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 355 | // Enables the next generation AGC functionality. This feature replaces the |
| 356 | // standard methods of gain control in the previous AGC. Enabling this |
| 357 | // submodule enables an adaptive digital AGC followed by a limiter. By |
| 358 | // setting |fixed_gain_db|, the limiter can be turned into a compressor that |
| 359 | // first applies a fixed gain. The adaptive digital AGC can be turned off by |
| 360 | // setting |adaptive_digital_mode=false|. |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 361 | struct GainController2 { |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 362 | enum LevelEstimator { kRms, kPeak }; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 363 | bool enabled = false; |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 364 | struct { |
| 365 | float gain_db = 0.f; |
| 366 | } fixed_digital; |
| 367 | struct { |
Alessio Bazzica | 8da7b35 | 2018-11-21 10:50:58 +0100 | [diff] [blame] | 368 | bool enabled = false; |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 369 | LevelEstimator level_estimator = kRms; |
| 370 | bool use_saturation_protector = true; |
| 371 | float extra_saturation_margin_db = 2.f; |
| 372 | } adaptive_digital; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 373 | } gain_controller2; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 374 | |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 375 | struct ResidualEchoDetector { |
| 376 | bool enabled = true; |
| 377 | } residual_echo_detector; |
| 378 | |
Sam Zackrisson | b24c00f | 2018-11-26 16:18:25 +0100 | [diff] [blame] | 379 | // Enables reporting of |output_rms_dbfs| in webrtc::AudioProcessingStats. |
| 380 | struct LevelEstimation { |
| 381 | bool enabled = false; |
| 382 | } level_estimation; |
| 383 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 384 | // Explicit copy assignment implementation to avoid issues with memory |
| 385 | // sanitizer complaints in case of self-assignment. |
| 386 | // TODO(peah): Add buildflag to ensure that this is only included for memory |
| 387 | // sanitizer builds. |
| 388 | Config& operator=(const Config& config) { |
| 389 | if (this != &config) { |
| 390 | memcpy(this, &config, sizeof(*this)); |
| 391 | } |
| 392 | return *this; |
| 393 | } |
Artem Titov | 59bbd65 | 2019-08-02 11:31:37 +0200 | [diff] [blame] | 394 | |
| 395 | std::string ToString() const; |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 396 | }; |
| 397 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 398 | // TODO(mgraczyk): Remove once all methods that use ChannelLayout are gone. |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 399 | enum ChannelLayout { |
| 400 | kMono, |
| 401 | // Left, right. |
| 402 | kStereo, |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 403 | // Mono, keyboard, and mic. |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 404 | kMonoAndKeyboard, |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 405 | // Left, right, keyboard, and mic. |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 406 | kStereoAndKeyboard |
| 407 | }; |
| 408 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 409 | // Specifies the properties of a setting to be passed to AudioProcessing at |
| 410 | // runtime. |
| 411 | class RuntimeSetting { |
| 412 | public: |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 413 | enum class Type { |
| 414 | kNotSpecified, |
| 415 | kCapturePreGain, |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 416 | kCaptureCompressionGain, |
Per Åhgren | 6ee75fd | 2019-04-26 11:33:37 +0200 | [diff] [blame] | 417 | kCaptureFixedPostGain, |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 418 | kPlayoutVolumeChange, |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 419 | kCustomRenderProcessingRuntimeSetting, |
| 420 | kPlayoutAudioDeviceChange |
| 421 | }; |
| 422 | |
| 423 | // Play-out audio device properties. |
| 424 | struct PlayoutAudioDeviceInfo { |
| 425 | int id; // Identifies the audio device. |
| 426 | int max_volume; // Maximum play-out volume. |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 427 | }; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 428 | |
| 429 | RuntimeSetting() : type_(Type::kNotSpecified), value_(0.f) {} |
| 430 | ~RuntimeSetting() = default; |
| 431 | |
| 432 | static RuntimeSetting CreateCapturePreGain(float gain) { |
| 433 | RTC_DCHECK_GE(gain, 1.f) << "Attenuation is not allowed."; |
| 434 | return {Type::kCapturePreGain, gain}; |
| 435 | } |
| 436 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 437 | // Corresponds to Config::GainController1::compression_gain_db, but for |
| 438 | // runtime configuration. |
| 439 | static RuntimeSetting CreateCompressionGainDb(int gain_db) { |
| 440 | RTC_DCHECK_GE(gain_db, 0); |
| 441 | RTC_DCHECK_LE(gain_db, 90); |
| 442 | return {Type::kCaptureCompressionGain, static_cast<float>(gain_db)}; |
| 443 | } |
| 444 | |
Per Åhgren | 6ee75fd | 2019-04-26 11:33:37 +0200 | [diff] [blame] | 445 | // Corresponds to Config::GainController2::fixed_digital::gain_db, but for |
| 446 | // runtime configuration. |
| 447 | static RuntimeSetting CreateCaptureFixedPostGain(float gain_db) { |
| 448 | RTC_DCHECK_GE(gain_db, 0.f); |
| 449 | RTC_DCHECK_LE(gain_db, 90.f); |
| 450 | return {Type::kCaptureFixedPostGain, gain_db}; |
| 451 | } |
| 452 | |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 453 | // Creates a runtime setting to notify play-out (aka render) audio device |
| 454 | // changes. |
| 455 | static RuntimeSetting CreatePlayoutAudioDeviceChange( |
| 456 | PlayoutAudioDeviceInfo audio_device) { |
| 457 | return {Type::kPlayoutAudioDeviceChange, audio_device}; |
| 458 | } |
| 459 | |
| 460 | // Creates a runtime setting to notify play-out (aka render) volume changes. |
| 461 | // |volume| is the unnormalized volume, the maximum of which |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 462 | static RuntimeSetting CreatePlayoutVolumeChange(int volume) { |
| 463 | return {Type::kPlayoutVolumeChange, volume}; |
| 464 | } |
| 465 | |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 466 | static RuntimeSetting CreateCustomRenderSetting(float payload) { |
| 467 | return {Type::kCustomRenderProcessingRuntimeSetting, payload}; |
| 468 | } |
| 469 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 470 | Type type() const { return type_; } |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 471 | // Getters do not return a value but instead modify the argument to protect |
| 472 | // from implicit casting. |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 473 | void GetFloat(float* value) const { |
| 474 | RTC_DCHECK(value); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 475 | *value = value_.float_value; |
| 476 | } |
| 477 | void GetInt(int* value) const { |
| 478 | RTC_DCHECK(value); |
| 479 | *value = value_.int_value; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 480 | } |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 481 | void GetPlayoutAudioDeviceInfo(PlayoutAudioDeviceInfo* value) const { |
| 482 | RTC_DCHECK(value); |
| 483 | *value = value_.playout_audio_device_info; |
| 484 | } |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 485 | |
| 486 | private: |
| 487 | RuntimeSetting(Type id, float value) : type_(id), value_(value) {} |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 488 | RuntimeSetting(Type id, int value) : type_(id), value_(value) {} |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 489 | RuntimeSetting(Type id, PlayoutAudioDeviceInfo value) |
| 490 | : type_(id), value_(value) {} |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 491 | Type type_; |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 492 | union U { |
| 493 | U() {} |
| 494 | U(int value) : int_value(value) {} |
| 495 | U(float value) : float_value(value) {} |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 496 | U(PlayoutAudioDeviceInfo value) : playout_audio_device_info(value) {} |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 497 | float float_value; |
| 498 | int int_value; |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 499 | PlayoutAudioDeviceInfo playout_audio_device_info; |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 500 | } value_; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 501 | }; |
| 502 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 503 | ~AudioProcessing() override {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 504 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 505 | // Initializes internal states, while retaining all user settings. This |
| 506 | // should be called before beginning to process a new audio stream. However, |
| 507 | // it is not necessary to call before processing the first stream after |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 508 | // creation. |
| 509 | // |
| 510 | // It is also not necessary to call if the audio parameters (sample |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 511 | // rate and number of channels) have changed. Passing updated parameters |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 512 | // directly to |ProcessStream()| and |ProcessReverseStream()| is permissible. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 513 | // If the parameters are known at init-time though, they may be provided. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 514 | virtual int Initialize() = 0; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 515 | |
| 516 | // The int16 interfaces require: |
| 517 | // - only |NativeRate|s be used |
| 518 | // - that the input, output and reverse rates must match |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 519 | // - that |processing_config.output_stream()| matches |
| 520 | // |processing_config.input_stream()|. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 521 | // |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 522 | // The float interfaces accept arbitrary rates and support differing input and |
| 523 | // output layouts, but the output must have either one channel or the same |
| 524 | // number of channels as the input. |
| 525 | virtual int Initialize(const ProcessingConfig& processing_config) = 0; |
| 526 | |
| 527 | // Initialize with unpacked parameters. See Initialize() above for details. |
| 528 | // |
| 529 | // TODO(mgraczyk): Remove once clients are updated to use the new interface. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 530 | virtual int Initialize(int capture_input_sample_rate_hz, |
| 531 | int capture_output_sample_rate_hz, |
| 532 | int render_sample_rate_hz, |
| 533 | ChannelLayout capture_input_layout, |
| 534 | ChannelLayout capture_output_layout, |
| 535 | ChannelLayout render_input_layout) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 536 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 537 | // TODO(peah): This method is a temporary solution used to take control |
| 538 | // over the parameters in the audio processing module and is likely to change. |
| 539 | virtual void ApplyConfig(const Config& config) = 0; |
| 540 | |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 541 | // Pass down additional options which don't have explicit setters. This |
| 542 | // ensures the options are applied immediately. |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 543 | virtual void SetExtraOptions(const webrtc::Config& config) = 0; |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 544 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 545 | // TODO(ajm): Only intended for internal use. Make private and friend the |
| 546 | // necessary classes? |
| 547 | virtual int proc_sample_rate_hz() const = 0; |
| 548 | virtual int proc_split_sample_rate_hz() const = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 549 | virtual size_t num_input_channels() const = 0; |
| 550 | virtual size_t num_proc_channels() const = 0; |
| 551 | virtual size_t num_output_channels() const = 0; |
| 552 | virtual size_t num_reverse_channels() const = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 553 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 554 | // Set to true when the output of AudioProcessing will be muted or in some |
| 555 | // other way not used. Ideally, the captured audio would still be processed, |
| 556 | // but some components may change behavior based on this information. |
| 557 | // Default false. |
| 558 | virtual void set_output_will_be_muted(bool muted) = 0; |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 559 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 560 | // Enqueue a runtime setting. |
| 561 | virtual void SetRuntimeSetting(RuntimeSetting setting) = 0; |
| 562 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 563 | // Processes a 10 ms |frame| of the primary audio stream. On the client-side, |
| 564 | // this is the near-end (or captured) audio. |
| 565 | // |
| 566 | // If needed for enabled functionality, any function with the set_stream_ tag |
| 567 | // must be called prior to processing the current frame. Any getter function |
| 568 | // with the stream_ tag which is needed should be called after processing. |
| 569 | // |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 570 | // The |sample_rate_hz_|, |num_channels_|, and |samples_per_channel_| |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 571 | // members of |frame| must be valid. If changed from the previous call to this |
| 572 | // method, it will trigger an initialization. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 573 | virtual int ProcessStream(AudioFrame* frame) = 0; |
| 574 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 575 | // Accepts deinterleaved float audio with the range [-1, 1]. Each element of |
| 576 | // |src| points to a channel buffer, arranged according to |input_stream|. At |
| 577 | // output, the channels will be arranged according to |output_stream| in |
| 578 | // |dest|. |
| 579 | // |
| 580 | // The output must have one channel or as many channels as the input. |src| |
| 581 | // and |dest| may use the same memory, if desired. |
| 582 | virtual int ProcessStream(const float* const* src, |
| 583 | const StreamConfig& input_config, |
| 584 | const StreamConfig& output_config, |
| 585 | float* const* dest) = 0; |
| 586 | |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 587 | // Processes a 10 ms |frame| of the reverse direction audio stream. The frame |
| 588 | // may be modified. On the client-side, this is the far-end (or to be |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 589 | // rendered) audio. |
| 590 | // |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 591 | // It is necessary to provide this if echo processing is enabled, as the |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 592 | // reverse stream forms the echo reference signal. It is recommended, but not |
| 593 | // necessary, to provide if gain control is enabled. On the server-side this |
| 594 | // typically will not be used. If you're not sure what to pass in here, |
| 595 | // chances are you don't need to use it. |
| 596 | // |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 597 | // The |sample_rate_hz_|, |num_channels_|, and |samples_per_channel_| |
aluebs | da116c4 | 2016-03-17 16:43:29 -0700 | [diff] [blame] | 598 | // members of |frame| must be valid. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 599 | virtual int ProcessReverseStream(AudioFrame* frame) = 0; |
| 600 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 601 | // Accepts deinterleaved float audio with the range [-1, 1]. Each element of |
| 602 | // |data| points to a channel buffer, arranged according to |reverse_config|. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 603 | virtual int ProcessReverseStream(const float* const* src, |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 604 | const StreamConfig& input_config, |
| 605 | const StreamConfig& output_config, |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 606 | float* const* dest) = 0; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 607 | |
Gustaf Ullberg | cb30726 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 608 | // Accepts deinterleaved float audio with the range [-1, 1]. Each element |
| 609 | // of |data| points to a channel buffer, arranged according to |
| 610 | // |reverse_config|. |
| 611 | virtual int AnalyzeReverseStream(const float* const* data, |
| 612 | const StreamConfig& reverse_config) = 0; |
| 613 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 614 | // This must be called prior to ProcessStream() if and only if adaptive analog |
| 615 | // gain control is enabled, to pass the current analog level from the audio |
| 616 | // HAL. Must be within the range provided in Config::GainController1. |
| 617 | virtual void set_stream_analog_level(int level) = 0; |
| 618 | |
| 619 | // When an analog mode is set, this should be called after ProcessStream() |
| 620 | // to obtain the recommended new analog level for the audio HAL. It is the |
| 621 | // user's responsibility to apply this level. |
| 622 | virtual int recommended_stream_analog_level() const = 0; |
| 623 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 624 | // This must be called if and only if echo processing is enabled. |
| 625 | // |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 626 | // Sets the |delay| in ms between ProcessReverseStream() receiving a far-end |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 627 | // frame and ProcessStream() receiving a near-end frame containing the |
| 628 | // corresponding echo. On the client-side this can be expressed as |
| 629 | // delay = (t_render - t_analyze) + (t_process - t_capture) |
| 630 | // where, |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 631 | // - t_analyze is the time a frame is passed to ProcessReverseStream() and |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 632 | // t_render is the time the first sample of the same frame is rendered by |
| 633 | // the audio hardware. |
| 634 | // - t_capture is the time the first sample of a frame is captured by the |
alessiob | 13fc180 | 2017-04-19 05:35:51 -0700 | [diff] [blame] | 635 | // audio hardware and t_process is the time the same frame is passed to |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 636 | // ProcessStream(). |
| 637 | virtual int set_stream_delay_ms(int delay) = 0; |
| 638 | virtual int stream_delay_ms() const = 0; |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 639 | virtual bool was_stream_delay_set() const = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 640 | |
andrew@webrtc.org | 75dd288 | 2014-02-11 20:52:30 +0000 | [diff] [blame] | 641 | // Call to signal that a key press occurred (true) or did not occur (false) |
| 642 | // with this chunk of audio. |
| 643 | virtual void set_stream_key_pressed(bool key_pressed) = 0; |
andrew@webrtc.org | 75dd288 | 2014-02-11 20:52:30 +0000 | [diff] [blame] | 644 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 645 | // Sets a delay |offset| in ms to add to the values passed in through |
| 646 | // set_stream_delay_ms(). May be positive or negative. |
| 647 | // |
| 648 | // Note that this could cause an otherwise valid value passed to |
| 649 | // set_stream_delay_ms() to return an error. |
| 650 | virtual void set_delay_offset_ms(int offset) = 0; |
| 651 | virtual int delay_offset_ms() const = 0; |
| 652 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 653 | // Attaches provided webrtc::AecDump for recording debugging |
| 654 | // information. Log file and maximum file size logic is supposed to |
| 655 | // be handled by implementing instance of AecDump. Calling this |
| 656 | // method when another AecDump is attached resets the active AecDump |
| 657 | // with a new one. This causes the d-tor of the earlier AecDump to |
| 658 | // be called. The d-tor call may block until all pending logging |
| 659 | // tasks are completed. |
Alex Loiko | be767e0 | 2017-06-08 09:45:03 +0200 | [diff] [blame] | 660 | virtual void AttachAecDump(std::unique_ptr<AecDump> aec_dump) = 0; |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 661 | |
| 662 | // If no AecDump is attached, this has no effect. If an AecDump is |
| 663 | // attached, it's destructor is called. The d-tor may block until |
| 664 | // all pending logging tasks are completed. |
Alex Loiko | be767e0 | 2017-06-08 09:45:03 +0200 | [diff] [blame] | 665 | virtual void DetachAecDump() = 0; |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 666 | |
Sam Zackrisson | 4d36449 | 2018-03-02 16:03:21 +0100 | [diff] [blame] | 667 | // Attaches provided webrtc::AudioGenerator for modifying playout audio. |
| 668 | // Calling this method when another AudioGenerator is attached replaces the |
| 669 | // active AudioGenerator with a new one. |
| 670 | virtual void AttachPlayoutAudioGenerator( |
| 671 | std::unique_ptr<AudioGenerator> audio_generator) = 0; |
| 672 | |
| 673 | // If no AudioGenerator is attached, this has no effect. If an AecDump is |
| 674 | // attached, its destructor is called. |
| 675 | virtual void DetachPlayoutAudioGenerator() = 0; |
| 676 | |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 677 | // Use to send UMA histograms at end of a call. Note that all histogram |
| 678 | // specific member variables are reset. |
Per Åhgren | ea4c5df | 2019-05-03 09:00:08 +0200 | [diff] [blame] | 679 | // Deprecated. This method is deprecated and will be removed. |
| 680 | // TODO(peah): Remove this method. |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 681 | virtual void UpdateHistogramsOnCallEnd() = 0; |
| 682 | |
Sam Zackrisson | 2812763 | 2018-11-01 11:37:15 +0100 | [diff] [blame] | 683 | // Get audio processing statistics. The |has_remote_tracks| argument should be |
| 684 | // set if there are active remote tracks (this would usually be true during |
| 685 | // a call). If there are no remote tracks some of the stats will not be set by |
| 686 | // AudioProcessing, because they only make sense if there is at least one |
| 687 | // remote track. |
| 688 | virtual AudioProcessingStats GetStatistics(bool has_remote_tracks) const = 0; |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 689 | |
henrik.lundin | adf0635 | 2017-04-05 05:48:24 -0700 | [diff] [blame] | 690 | // Returns the last applied configuration. |
henrik.lundin | 7749286 | 2017-04-06 23:28:09 -0700 | [diff] [blame] | 691 | virtual AudioProcessing::Config GetConfig() const = 0; |
henrik.lundin | adf0635 | 2017-04-05 05:48:24 -0700 | [diff] [blame] | 692 | |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 693 | enum Error { |
| 694 | // Fatal errors. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 695 | kNoError = 0, |
| 696 | kUnspecifiedError = -1, |
| 697 | kCreationFailedError = -2, |
| 698 | kUnsupportedComponentError = -3, |
| 699 | kUnsupportedFunctionError = -4, |
| 700 | kNullPointerError = -5, |
| 701 | kBadParameterError = -6, |
| 702 | kBadSampleRateError = -7, |
| 703 | kBadDataLengthError = -8, |
| 704 | kBadNumberChannelsError = -9, |
| 705 | kFileError = -10, |
| 706 | kStreamParameterNotSetError = -11, |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 707 | kNotEnabledError = -12, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 708 | |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 709 | // Warnings are non-fatal. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 710 | // This results when a set_stream_ parameter is out of range. Processing |
| 711 | // will continue, but the parameter may have been truncated. |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 712 | kBadStreamParameterWarning = -13 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 713 | }; |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 714 | |
Per Åhgren | c8626b6 | 2019-08-23 15:49:51 +0200 | [diff] [blame] | 715 | // Native rates supported by the AudioFrame interfaces. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 716 | enum NativeRate { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 717 | kSampleRate8kHz = 8000, |
| 718 | kSampleRate16kHz = 16000, |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 719 | kSampleRate32kHz = 32000, |
| 720 | kSampleRate48kHz = 48000 |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 721 | }; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 722 | |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 723 | // TODO(kwiberg): We currently need to support a compiler (Visual C++) that |
| 724 | // complains if we don't explicitly state the size of the array here. Remove |
| 725 | // the size when that's no longer the case. |
| 726 | static constexpr int kNativeSampleRatesHz[4] = { |
| 727 | kSampleRate8kHz, kSampleRate16kHz, kSampleRate32kHz, kSampleRate48kHz}; |
| 728 | static constexpr size_t kNumNativeSampleRates = |
| 729 | arraysize(kNativeSampleRatesHz); |
| 730 | static constexpr int kMaxNativeSampleRateHz = |
| 731 | kNativeSampleRatesHz[kNumNativeSampleRates - 1]; |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 732 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 733 | static const int kChunkSizeMs = 10; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 736 | class RTC_EXPORT AudioProcessingBuilder { |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 737 | public: |
| 738 | AudioProcessingBuilder(); |
| 739 | ~AudioProcessingBuilder(); |
| 740 | // The AudioProcessingBuilder takes ownership of the echo_control_factory. |
| 741 | AudioProcessingBuilder& SetEchoControlFactory( |
| 742 | std::unique_ptr<EchoControlFactory> echo_control_factory); |
| 743 | // The AudioProcessingBuilder takes ownership of the capture_post_processing. |
| 744 | AudioProcessingBuilder& SetCapturePostProcessing( |
| 745 | std::unique_ptr<CustomProcessing> capture_post_processing); |
| 746 | // The AudioProcessingBuilder takes ownership of the render_pre_processing. |
| 747 | AudioProcessingBuilder& SetRenderPreProcessing( |
| 748 | std::unique_ptr<CustomProcessing> render_pre_processing); |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 749 | // The AudioProcessingBuilder takes ownership of the echo_detector. |
| 750 | AudioProcessingBuilder& SetEchoDetector( |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 751 | rtc::scoped_refptr<EchoDetector> echo_detector); |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 752 | // The AudioProcessingBuilder takes ownership of the capture_analyzer. |
| 753 | AudioProcessingBuilder& SetCaptureAnalyzer( |
| 754 | std::unique_ptr<CustomAudioAnalyzer> capture_analyzer); |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 755 | // This creates an APM instance using the previously set components. Calling |
| 756 | // the Create function resets the AudioProcessingBuilder to its initial state. |
| 757 | AudioProcessing* Create(); |
| 758 | AudioProcessing* Create(const webrtc::Config& config); |
| 759 | |
| 760 | private: |
| 761 | std::unique_ptr<EchoControlFactory> echo_control_factory_; |
| 762 | std::unique_ptr<CustomProcessing> capture_post_processing_; |
| 763 | std::unique_ptr<CustomProcessing> render_pre_processing_; |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 764 | rtc::scoped_refptr<EchoDetector> echo_detector_; |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 765 | std::unique_ptr<CustomAudioAnalyzer> capture_analyzer_; |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 766 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioProcessingBuilder); |
| 767 | }; |
| 768 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 769 | class StreamConfig { |
| 770 | public: |
| 771 | // sample_rate_hz: The sampling rate of the stream. |
| 772 | // |
| 773 | // num_channels: The number of audio channels in the stream, excluding the |
| 774 | // keyboard channel if it is present. When passing a |
| 775 | // StreamConfig with an array of arrays T*[N], |
| 776 | // |
| 777 | // N == {num_channels + 1 if has_keyboard |
| 778 | // {num_channels if !has_keyboard |
| 779 | // |
| 780 | // has_keyboard: True if the stream has a keyboard channel. When has_keyboard |
| 781 | // is true, the last channel in any corresponding list of |
| 782 | // channels is the keyboard channel. |
| 783 | StreamConfig(int sample_rate_hz = 0, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 784 | size_t num_channels = 0, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 785 | bool has_keyboard = false) |
| 786 | : sample_rate_hz_(sample_rate_hz), |
| 787 | num_channels_(num_channels), |
| 788 | has_keyboard_(has_keyboard), |
| 789 | num_frames_(calculate_frames(sample_rate_hz)) {} |
| 790 | |
| 791 | void set_sample_rate_hz(int value) { |
| 792 | sample_rate_hz_ = value; |
| 793 | num_frames_ = calculate_frames(value); |
| 794 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 795 | void set_num_channels(size_t value) { num_channels_ = value; } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 796 | void set_has_keyboard(bool value) { has_keyboard_ = value; } |
| 797 | |
| 798 | int sample_rate_hz() const { return sample_rate_hz_; } |
| 799 | |
| 800 | // The number of channels in the stream, not including the keyboard channel if |
| 801 | // present. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 802 | size_t num_channels() const { return num_channels_; } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 803 | |
| 804 | bool has_keyboard() const { return has_keyboard_; } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 805 | size_t num_frames() const { return num_frames_; } |
| 806 | size_t num_samples() const { return num_channels_ * num_frames_; } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 807 | |
| 808 | bool operator==(const StreamConfig& other) const { |
| 809 | return sample_rate_hz_ == other.sample_rate_hz_ && |
| 810 | num_channels_ == other.num_channels_ && |
| 811 | has_keyboard_ == other.has_keyboard_; |
| 812 | } |
| 813 | |
| 814 | bool operator!=(const StreamConfig& other) const { return !(*this == other); } |
| 815 | |
| 816 | private: |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 817 | static size_t calculate_frames(int sample_rate_hz) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 818 | return static_cast<size_t>(AudioProcessing::kChunkSizeMs * sample_rate_hz / |
| 819 | 1000); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | int sample_rate_hz_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 823 | size_t num_channels_; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 824 | bool has_keyboard_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 825 | size_t num_frames_; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | class ProcessingConfig { |
| 829 | public: |
| 830 | enum StreamName { |
| 831 | kInputStream, |
| 832 | kOutputStream, |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 833 | kReverseInputStream, |
| 834 | kReverseOutputStream, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 835 | kNumStreamNames, |
| 836 | }; |
| 837 | |
| 838 | const StreamConfig& input_stream() const { |
| 839 | return streams[StreamName::kInputStream]; |
| 840 | } |
| 841 | const StreamConfig& output_stream() const { |
| 842 | return streams[StreamName::kOutputStream]; |
| 843 | } |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 844 | const StreamConfig& reverse_input_stream() const { |
| 845 | return streams[StreamName::kReverseInputStream]; |
| 846 | } |
| 847 | const StreamConfig& reverse_output_stream() const { |
| 848 | return streams[StreamName::kReverseOutputStream]; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | StreamConfig& input_stream() { return streams[StreamName::kInputStream]; } |
| 852 | StreamConfig& output_stream() { return streams[StreamName::kOutputStream]; } |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 853 | StreamConfig& reverse_input_stream() { |
| 854 | return streams[StreamName::kReverseInputStream]; |
| 855 | } |
| 856 | StreamConfig& reverse_output_stream() { |
| 857 | return streams[StreamName::kReverseOutputStream]; |
| 858 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 859 | |
| 860 | bool operator==(const ProcessingConfig& other) const { |
| 861 | for (int i = 0; i < StreamName::kNumStreamNames; ++i) { |
| 862 | if (this->streams[i] != other.streams[i]) { |
| 863 | return false; |
| 864 | } |
| 865 | } |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | bool operator!=(const ProcessingConfig& other) const { |
| 870 | return !(*this == other); |
| 871 | } |
| 872 | |
| 873 | StreamConfig streams[StreamName::kNumStreamNames]; |
| 874 | }; |
| 875 | |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 876 | // Experimental interface for a custom analysis submodule. |
| 877 | class CustomAudioAnalyzer { |
| 878 | public: |
| 879 | // (Re-) Initializes the submodule. |
| 880 | virtual void Initialize(int sample_rate_hz, int num_channels) = 0; |
| 881 | // Analyzes the given capture or render signal. |
| 882 | virtual void Analyze(const AudioBuffer* audio) = 0; |
| 883 | // Returns a string representation of the module state. |
| 884 | virtual std::string ToString() const = 0; |
| 885 | |
| 886 | virtual ~CustomAudioAnalyzer() {} |
| 887 | }; |
| 888 | |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame] | 889 | // Interface for a custom processing submodule. |
| 890 | class CustomProcessing { |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 891 | public: |
| 892 | // (Re-)Initializes the submodule. |
| 893 | virtual void Initialize(int sample_rate_hz, int num_channels) = 0; |
| 894 | // Processes the given capture or render signal. |
| 895 | virtual void Process(AudioBuffer* audio) = 0; |
| 896 | // Returns a string representation of the module state. |
| 897 | virtual std::string ToString() const = 0; |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 898 | // Handles RuntimeSettings. TODO(webrtc:9262): make pure virtual |
| 899 | // after updating dependencies. |
| 900 | virtual void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting); |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 901 | |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame] | 902 | virtual ~CustomProcessing() {} |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 903 | }; |
| 904 | |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 905 | // Interface for an echo detector submodule. |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 906 | class EchoDetector : public rtc::RefCountInterface { |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 907 | public: |
| 908 | // (Re-)Initializes the submodule. |
Ivo Creusen | 647ef09 | 2018-03-14 17:13:48 +0100 | [diff] [blame] | 909 | virtual void Initialize(int capture_sample_rate_hz, |
| 910 | int num_capture_channels, |
| 911 | int render_sample_rate_hz, |
| 912 | int num_render_channels) = 0; |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 913 | |
| 914 | // Analysis (not changing) of the render signal. |
| 915 | virtual void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) = 0; |
| 916 | |
| 917 | // Analysis (not changing) of the capture signal. |
| 918 | virtual void AnalyzeCaptureAudio( |
| 919 | rtc::ArrayView<const float> capture_audio) = 0; |
| 920 | |
| 921 | // Pack an AudioBuffer into a vector<float>. |
| 922 | static void PackRenderAudioBuffer(AudioBuffer* audio, |
| 923 | std::vector<float>* packed_buffer); |
| 924 | |
| 925 | struct Metrics { |
| 926 | double echo_likelihood; |
| 927 | double echo_likelihood_recent_max; |
| 928 | }; |
| 929 | |
| 930 | // Collect current metrics from the echo detector. |
| 931 | virtual Metrics GetMetrics() const = 0; |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 932 | }; |
| 933 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 934 | } // namespace webrtc |
| 935 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 936 | #endif // MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_ |