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