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" |
Sam Zackrisson | ab866a2 | 2020-05-07 13:07:49 +0200 | [diff] [blame] | 27 | #include "api/array_view.h" |
Gustaf Ullberg | bffa300 | 2018-02-14 15:12:00 +0100 | [diff] [blame] | 28 | #include "api/audio/echo_canceller3_config.h" |
Gustaf Ullberg | fd4ce50 | 2018-02-15 10:09:09 +0100 | [diff] [blame] | 29 | #include "api/audio/echo_control.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 30 | #include "api/scoped_refptr.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" |
Niels Möller | de95329 | 2020-09-29 09:46:21 +0200 | [diff] [blame] | 34 | #include "rtc_base/constructor_magic.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 35 | #include "rtc_base/ref_count.h" |
Per Åhgren | 09e9a83 | 2020-05-11 11:03:47 +0200 | [diff] [blame] | 36 | #include "rtc_base/system/file_wrapper.h" |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 37 | #include "rtc_base/system/rtc_export.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
Per Åhgren | 09e9a83 | 2020-05-11 11:03:47 +0200 | [diff] [blame] | 39 | namespace rtc { |
| 40 | class TaskQueue; |
| 41 | } // namespace rtc |
| 42 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | namespace webrtc { |
| 44 | |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 45 | class AecDump; |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 46 | class AudioBuffer; |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 47 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 48 | class StreamConfig; |
| 49 | class ProcessingConfig; |
| 50 | |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 51 | class EchoDetector; |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 52 | class CustomAudioAnalyzer; |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame] | 53 | class CustomProcessing; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 55 | // Use to enable experimental gain control (AGC). At startup the experimental |
| 56 | // AGC moves the microphone volume up to |startup_min_volume| if the current |
| 57 | // microphone volume is set too low. The value is clamped to its operating range |
| 58 | // [12, 255]. Here, 255 maps to 100%. |
| 59 | // |
Ivo Creusen | 62337e5 | 2018-01-09 14:17:33 +0100 | [diff] [blame] | 60 | // Must be provided through AudioProcessingBuilder().Create(config). |
Bjorn Volcker | fb49451 | 2015-04-22 06:39:58 +0200 | [diff] [blame] | 61 | #if defined(WEBRTC_CHROMIUM_BUILD) |
Hanna Silen | b8dc7fa | 2021-05-20 17:37:56 +0200 | [diff] [blame] | 62 | static constexpr int kAgcStartupMinVolume = 85; |
Bjorn Volcker | fb49451 | 2015-04-22 06:39:58 +0200 | [diff] [blame] | 63 | #else |
Hanna Silen | b8dc7fa | 2021-05-20 17:37:56 +0200 | [diff] [blame] | 64 | static constexpr int kAgcStartupMinVolume = 0; |
Bjorn Volcker | fb49451 | 2015-04-22 06:39:58 +0200 | [diff] [blame] | 65 | #endif // defined(WEBRTC_CHROMIUM_BUILD) |
Henrik Lundin | e3a4da9 | 2017-11-06 11:42:21 +0100 | [diff] [blame] | 66 | static constexpr int kClippedLevelMin = 70; |
Per Åhgren | 0695df1 | 2020-01-13 14:43:13 +0100 | [diff] [blame] | 67 | |
| 68 | // To be deprecated: Please instead use the flag in the |
| 69 | // AudioProcessing::Config::AnalogGainController. |
| 70 | // TODO(webrtc:5298): Remove. |
andrew@webrtc.org | c7c7a53 | 2014-01-29 04:57:25 +0000 | [diff] [blame] | 71 | struct ExperimentalAgc { |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 72 | ExperimentalAgc() = default; |
| 73 | explicit ExperimentalAgc(bool enabled) : enabled(enabled) {} |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 74 | ExperimentalAgc(bool enabled, int startup_min_volume) |
| 75 | : enabled(enabled), startup_min_volume(startup_min_volume) {} |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 76 | static const ConfigOptionID identifier = ConfigOptionID::kExperimentalAgc; |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 77 | bool enabled = true; |
| 78 | int startup_min_volume = kAgcStartupMinVolume; |
| 79 | // Lowest microphone level that will be applied in response to clipping. |
| 80 | int clipped_level_min = kClippedLevelMin; |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 81 | bool digital_adaptive_disabled = false; |
andrew@webrtc.org | 6b1e219 | 2013-09-25 23:46:20 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Per Åhgren | c073471 | 2020-01-02 15:15:36 +0100 | [diff] [blame] | 84 | // To be deprecated: Please instead use the flag in the |
| 85 | // AudioProcessing::Config::TransientSuppression. |
| 86 | // |
aluebs@webrtc.org | 9825afc | 2014-06-30 17:39:53 +0000 | [diff] [blame] | 87 | // Use to enable experimental noise suppression. It can be set in the |
Mirko Bonadei | c94650d | 2020-09-03 13:24:36 +0200 | [diff] [blame] | 88 | // constructor. |
Per Åhgren | c073471 | 2020-01-02 15:15:36 +0100 | [diff] [blame] | 89 | // TODO(webrtc:5298): Remove. |
aluebs@webrtc.org | 9825afc | 2014-06-30 17:39:53 +0000 | [diff] [blame] | 90 | struct ExperimentalNs { |
| 91 | ExperimentalNs() : enabled(false) {} |
| 92 | explicit ExperimentalNs(bool enabled) : enabled(enabled) {} |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 93 | static const ConfigOptionID identifier = ConfigOptionID::kExperimentalNs; |
aluebs@webrtc.org | 9825afc | 2014-06-30 17:39:53 +0000 | [diff] [blame] | 94 | bool enabled; |
| 95 | }; |
| 96 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | // The Audio Processing Module (APM) provides a collection of voice processing |
| 98 | // components designed for real-time communications software. |
| 99 | // |
| 100 | // APM operates on two audio streams on a frame-by-frame basis. Frames of the |
| 101 | // primary stream, on which all processing is applied, are passed to |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 102 | // |ProcessStream()|. Frames of the reverse direction stream are passed to |
| 103 | // |ProcessReverseStream()|. On the client-side, this will typically be the |
| 104 | // near-end (capture) and far-end (render) streams, respectively. APM should be |
| 105 | // placed in the signal chain as close to the audio hardware abstraction layer |
| 106 | // (HAL) as possible. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | // |
| 108 | // On the server-side, the reverse stream will normally not be used, with |
| 109 | // processing occurring on each incoming stream. |
| 110 | // |
| 111 | // Component interfaces follow a similar pattern and are accessed through |
| 112 | // corresponding getters in APM. All components are disabled at create-time, |
| 113 | // with default settings that are recommended for most situations. New settings |
| 114 | // can be applied without enabling a component. Enabling a component triggers |
| 115 | // memory allocation and initialization to allow it to start processing the |
| 116 | // streams. |
| 117 | // |
| 118 | // Thread safety is provided with the following assumptions to reduce locking |
| 119 | // overhead: |
| 120 | // 1. The stream getters and setters are called from the same thread as |
| 121 | // ProcessStream(). More precisely, stream functions are never called |
| 122 | // concurrently with ProcessStream(). |
| 123 | // 2. Parameter getters are never called concurrently with the corresponding |
| 124 | // setter. |
| 125 | // |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 126 | // APM accepts only linear PCM audio data in chunks of 10 ms. The int16 |
| 127 | // interfaces use interleaved data, while the float interfaces use deinterleaved |
| 128 | // data. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | // |
| 130 | // Usage example, omitting error checking: |
Ivo Creusen | 62337e5 | 2018-01-09 14:17:33 +0100 | [diff] [blame] | 131 | // AudioProcessing* apm = AudioProcessingBuilder().Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | // |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 133 | // AudioProcessing::Config config; |
Sam Zackrisson | cdf0e6d | 2018-09-17 11:05:17 +0200 | [diff] [blame] | 134 | // config.echo_canceller.enabled = true; |
| 135 | // config.echo_canceller.mobile_mode = false; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 136 | // |
| 137 | // config.gain_controller1.enabled = true; |
| 138 | // config.gain_controller1.mode = |
| 139 | // AudioProcessing::Config::GainController1::kAdaptiveAnalog; |
| 140 | // config.gain_controller1.analog_level_minimum = 0; |
| 141 | // config.gain_controller1.analog_level_maximum = 255; |
| 142 | // |
Sam Zackrisson | ab1aee0 | 2018-03-05 15:59:06 +0100 | [diff] [blame] | 143 | // config.gain_controller2.enabled = true; |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 144 | // |
| 145 | // config.high_pass_filter.enabled = true; |
| 146 | // |
| 147 | // config.voice_detection.enabled = true; |
| 148 | // |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 149 | // apm->ApplyConfig(config) |
| 150 | // |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | // apm->noise_reduction()->set_level(kHighSuppression); |
| 152 | // apm->noise_reduction()->Enable(true); |
| 153 | // |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | // // Start a voice call... |
| 155 | // |
| 156 | // // ... Render frame arrives bound for the audio HAL ... |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 157 | // apm->ProcessReverseStream(render_frame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | // |
| 159 | // // ... Capture frame arrives from the audio HAL ... |
| 160 | // // Call required set_stream_ functions. |
| 161 | // apm->set_stream_delay_ms(delay_ms); |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 162 | // apm->set_stream_analog_level(analog_level); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | // |
| 164 | // apm->ProcessStream(capture_frame); |
| 165 | // |
| 166 | // // Call required stream_ functions. |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 167 | // analog_level = apm->recommended_stream_analog_level(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | // has_voice = apm->stream_has_voice(); |
| 169 | // |
Hua, Chunbo | e61a40e | 2021-01-08 16:34:49 +0800 | [diff] [blame] | 170 | // // Repeat render and capture processing for the duration of the call... |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | // // Start a new call... |
| 172 | // apm->Initialize(); |
| 173 | // |
| 174 | // // Close the application... |
andrew@webrtc.org | f3930e9 | 2013-09-18 22:37:32 +0000 | [diff] [blame] | 175 | // delete apm; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | // |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 177 | class RTC_EXPORT AudioProcessing : public rtc::RefCountInterface { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | public: |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 179 | // The struct below constitutes the new parameter scheme for the audio |
| 180 | // processing. It is being introduced gradually and until it is fully |
| 181 | // introduced, it is prone to change. |
| 182 | // TODO(peah): Remove this comment once the new config scheme is fully rolled |
| 183 | // out. |
| 184 | // |
| 185 | // The parameters and behavior of the audio processing module are controlled |
| 186 | // by changing the default values in the AudioProcessing::Config struct. |
| 187 | // The config is applied by passing the struct to the ApplyConfig method. |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 188 | // |
| 189 | // This config is intended to be used during setup, and to enable/disable |
| 190 | // top-level processing effects. Use during processing may cause undesired |
| 191 | // submodule resets, affecting the audio quality. Use the RuntimeSetting |
| 192 | // construct for runtime configuration. |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame] | 193 | struct RTC_EXPORT Config { |
Per Åhgren | 2512604 | 2019-12-05 07:32:32 +0100 | [diff] [blame] | 194 | |
Per Åhgren | fcbe407 | 2019-09-15 00:27:58 +0200 | [diff] [blame] | 195 | // Sets the properties of the audio processing pipeline. |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame] | 196 | struct RTC_EXPORT Pipeline { |
Per Åhgren | fcbe407 | 2019-09-15 00:27:58 +0200 | [diff] [blame] | 197 | // Maximum allowed processing rate used internally. May only be set to |
Per Åhgren | 68c225d | 2021-01-21 23:03:32 +0100 | [diff] [blame] | 198 | // 32000 or 48000 and any differing values will be treated as 48000. |
| 199 | int maximum_internal_processing_rate = 48000; |
Per Åhgren | e14cb99 | 2019-11-27 09:34:22 +0100 | [diff] [blame] | 200 | // Allow multi-channel processing of render audio. |
| 201 | bool multi_channel_render = false; |
| 202 | // Allow multi-channel processing of capture audio when AEC3 is active |
| 203 | // or a custom AEC is injected.. |
| 204 | bool multi_channel_capture = false; |
Per Åhgren | fcbe407 | 2019-09-15 00:27:58 +0200 | [diff] [blame] | 205 | } pipeline; |
| 206 | |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 207 | // Enabled the pre-amplifier. It amplifies the capture signal |
| 208 | // before any other processing is done. |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 209 | // TODO(webrtc:5298): Deprecate and use the pre-gain functionality in |
| 210 | // capture_level_adjustment instead. |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 211 | struct PreAmplifier { |
| 212 | bool enabled = false; |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 213 | float fixed_gain_factor = 1.0f; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 214 | } pre_amplifier; |
| 215 | |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 216 | // Functionality for general level adjustment in the capture pipeline. This |
| 217 | // should not be used together with the legacy PreAmplifier functionality. |
| 218 | struct CaptureLevelAdjustment { |
| 219 | bool operator==(const CaptureLevelAdjustment& rhs) const; |
| 220 | bool operator!=(const CaptureLevelAdjustment& rhs) const { |
| 221 | return !(*this == rhs); |
| 222 | } |
| 223 | bool enabled = false; |
| 224 | // The `pre_gain_factor` scales the signal before any processing is done. |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 225 | float pre_gain_factor = 1.0f; |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 226 | // The `post_gain_factor` scales the signal after all processing is done. |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 227 | float post_gain_factor = 1.0f; |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 228 | struct AnalogMicGainEmulation { |
| 229 | bool operator==(const AnalogMicGainEmulation& rhs) const; |
| 230 | bool operator!=(const AnalogMicGainEmulation& rhs) const { |
| 231 | return !(*this == rhs); |
| 232 | } |
| 233 | bool enabled = false; |
| 234 | // Initial analog gain level to use for the emulated analog gain. Must |
| 235 | // be in the range [0...255]. |
| 236 | int initial_level = 255; |
| 237 | } analog_mic_gain_emulation; |
| 238 | } capture_level_adjustment; |
| 239 | |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 240 | struct HighPassFilter { |
| 241 | bool enabled = false; |
Per Åhgren | c042425 | 2019-12-10 13:04:15 +0100 | [diff] [blame] | 242 | bool apply_in_full_band = true; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 243 | } high_pass_filter; |
| 244 | |
Sam Zackrisson | 8b5d2cc | 2018-07-27 13:27:23 +0200 | [diff] [blame] | 245 | struct EchoCanceller { |
| 246 | bool enabled = false; |
| 247 | bool mobile_mode = false; |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 248 | bool export_linear_aec_output = false; |
Per Åhgren | b810646 | 2019-12-04 08:34:12 +0100 | [diff] [blame] | 249 | // Enforce the highpass filter to be on (has no effect for the mobile |
| 250 | // mode). |
Per Åhgren | bcce453 | 2019-12-03 13:52:40 +0100 | [diff] [blame] | 251 | bool enforce_high_pass_filtering = true; |
Sam Zackrisson | 8b5d2cc | 2018-07-27 13:27:23 +0200 | [diff] [blame] | 252 | } echo_canceller; |
| 253 | |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 254 | // Enables background noise suppression. |
| 255 | struct NoiseSuppression { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 256 | bool enabled = false; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 257 | enum Level { kLow, kModerate, kHigh, kVeryHigh }; |
| 258 | Level level = kModerate; |
Per Åhgren | 2e8e1c6 | 2019-12-20 00:42:22 +0100 | [diff] [blame] | 259 | bool analyze_linear_aec_output_when_available = false; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 260 | } noise_suppression; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 261 | |
Per Åhgren | c073471 | 2020-01-02 15:15:36 +0100 | [diff] [blame] | 262 | // Enables transient suppression. |
| 263 | struct TransientSuppression { |
| 264 | bool enabled = false; |
| 265 | } transient_suppression; |
| 266 | |
Sam Zackrisson | 0824c6f | 2019-10-07 14:03:56 +0200 | [diff] [blame] | 267 | // Enables reporting of |voice_detected| in webrtc::AudioProcessingStats. |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 268 | struct VoiceDetection { |
Alex Loiko | 5feb30e | 2018-04-16 13:52:32 +0200 | [diff] [blame] | 269 | bool enabled = false; |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 270 | } voice_detection; |
Alex Loiko | 5feb30e | 2018-04-16 13:52:32 +0200 | [diff] [blame] | 271 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 272 | // Enables automatic gain control (AGC) functionality. |
| 273 | // The automatic gain control (AGC) component brings the signal to an |
| 274 | // appropriate range. This is done by applying a digital gain directly and, |
| 275 | // in the analog mode, prescribing an analog gain to be applied at the audio |
| 276 | // HAL. |
| 277 | // Recommended to be enabled on the client-side. |
Alessio Bazzica | dfc11d5 | 2021-05-07 11:58:11 +0200 | [diff] [blame] | 278 | struct RTC_EXPORT GainController1 { |
Alessio Bazzica | 3438a93 | 2020-10-14 12:47:50 +0200 | [diff] [blame] | 279 | bool operator==(const GainController1& rhs) const; |
| 280 | bool operator!=(const GainController1& rhs) const { |
| 281 | return !(*this == rhs); |
| 282 | } |
| 283 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 284 | bool enabled = false; |
| 285 | enum Mode { |
| 286 | // Adaptive mode intended for use if an analog volume control is |
| 287 | // available on the capture device. It will require the user to provide |
| 288 | // coupling between the OS mixer controls and AGC through the |
| 289 | // stream_analog_level() functions. |
| 290 | // It consists of an analog gain prescription for the audio device and a |
| 291 | // digital compression stage. |
| 292 | kAdaptiveAnalog, |
| 293 | // Adaptive mode intended for situations in which an analog volume |
| 294 | // control is unavailable. It operates in a similar fashion to the |
| 295 | // adaptive analog mode, but with scaling instead applied in the digital |
| 296 | // domain. As with the analog mode, it additionally uses a digital |
| 297 | // compression stage. |
| 298 | kAdaptiveDigital, |
| 299 | // Fixed mode which enables only the digital compression stage also used |
| 300 | // by the two adaptive modes. |
| 301 | // It is distinguished from the adaptive modes by considering only a |
| 302 | // short time-window of the input signal. It applies a fixed gain |
| 303 | // through most of the input level range, and compresses (gradually |
| 304 | // reduces gain with increasing level) the input signal at higher |
| 305 | // levels. This mode is preferred on embedded devices where the capture |
| 306 | // signal level is predictable, so that a known gain can be applied. |
| 307 | kFixedDigital |
| 308 | }; |
| 309 | Mode mode = kAdaptiveAnalog; |
| 310 | // Sets the target peak level (or envelope) of the AGC in dBFs (decibels |
| 311 | // from digital full-scale). The convention is to use positive values. For |
| 312 | // instance, passing in a value of 3 corresponds to -3 dBFs, or a target |
| 313 | // level 3 dB below full-scale. Limited to [0, 31]. |
| 314 | int target_level_dbfs = 3; |
| 315 | // Sets the maximum gain the digital compression stage may apply, in dB. A |
| 316 | // higher number corresponds to greater compression, while a value of 0 |
| 317 | // will leave the signal uncompressed. Limited to [0, 90]. |
| 318 | // For updates after APM setup, use a RuntimeSetting instead. |
| 319 | int compression_gain_db = 9; |
| 320 | // When enabled, the compression stage will hard limit the signal to the |
| 321 | // target level. Otherwise, the signal will be compressed but not limited |
| 322 | // above the target level. |
| 323 | bool enable_limiter = true; |
| 324 | // Sets the minimum and maximum analog levels of the audio capture device. |
| 325 | // Must be set if an analog mode is used. Limited to [0, 65535]. |
| 326 | int analog_level_minimum = 0; |
| 327 | int analog_level_maximum = 255; |
Per Åhgren | 0695df1 | 2020-01-13 14:43:13 +0100 | [diff] [blame] | 328 | |
| 329 | // Enables the analog gain controller functionality. |
| 330 | struct AnalogGainController { |
| 331 | bool enabled = true; |
| 332 | int startup_min_volume = kAgcStartupMinVolume; |
| 333 | // Lowest analog microphone level that will be applied in response to |
| 334 | // clipping. |
| 335 | int clipped_level_min = kClippedLevelMin; |
Per Åhgren | 0695df1 | 2020-01-13 14:43:13 +0100 | [diff] [blame] | 336 | bool enable_digital_adaptive = true; |
Hanna Silen | b8dc7fa | 2021-05-20 17:37:56 +0200 | [diff] [blame] | 337 | // Amount the microphone level is lowered with every clipping event. |
| 338 | // Limited to (0, 255]. |
| 339 | int clipped_level_step = 15; |
| 340 | // Proportion of clipped samples required to declare a clipping event. |
| 341 | // Limited to (0.f, 1.f). |
| 342 | float clipped_ratio_threshold = 0.1f; |
| 343 | // Time in frames to wait after a clipping event before checking again. |
| 344 | // Limited to values higher than 0. |
| 345 | int clipped_wait_frames = 300; |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 346 | |
| 347 | // Enables clipping prediction functionality. |
| 348 | struct ClippingPredictor { |
| 349 | bool enabled = false; |
| 350 | enum Mode { |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 351 | // Clipping event prediction mode with fixed step estimation. |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 352 | kClippingEventPrediction, |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 353 | // Clipped peak estimation mode with adaptive step estimation. |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 354 | kAdaptiveStepClippingPeakPrediction, |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 355 | // Clipped peak estimation mode with fixed step estimation. |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 356 | kFixedStepClippingPeakPrediction, |
| 357 | }; |
| 358 | Mode mode = kClippingEventPrediction; |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 359 | // Number of frames in the sliding analysis window. |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 360 | int window_length = 5; |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 361 | // Number of frames in the sliding reference window. |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 362 | int reference_window_length = 5; |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 363 | // Reference window delay (unit: number of frames). |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 364 | int reference_window_delay = 5; |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 365 | // Clipping prediction threshold (dBFS). |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 366 | float clipping_threshold = -1.0f; |
| 367 | // Crest factor drop threshold (dB). |
| 368 | float crest_factor_margin = 3.0f; |
Alessio Bazzica | 42dacda | 2021-06-17 17:18:46 +0200 | [diff] [blame] | 369 | // If true, the recommended clipped level step is used to modify the |
| 370 | // analog gain. Otherwise, the predictor runs without affecting the |
| 371 | // analog gain. |
| 372 | bool use_predicted_step = true; |
Hanna Silen | a43953a | 2021-06-02 17:13:24 +0200 | [diff] [blame] | 373 | } clipping_predictor; |
Per Åhgren | 0695df1 | 2020-01-13 14:43:13 +0100 | [diff] [blame] | 374 | } analog_gain_controller; |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 375 | } gain_controller1; |
| 376 | |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 377 | // Enables the next generation AGC functionality. This feature replaces the |
| 378 | // standard methods of gain control in the previous AGC. Enabling this |
| 379 | // submodule enables an adaptive digital AGC followed by a limiter. By |
| 380 | // setting |fixed_gain_db|, the limiter can be turned into a compressor that |
| 381 | // first applies a fixed gain. The adaptive digital AGC can be turned off by |
| 382 | // setting |adaptive_digital_mode=false|. |
Alessio Bazzica | dfc11d5 | 2021-05-07 11:58:11 +0200 | [diff] [blame] | 383 | struct RTC_EXPORT GainController2 { |
Alessio Bazzica | 3438a93 | 2020-10-14 12:47:50 +0200 | [diff] [blame] | 384 | bool operator==(const GainController2& rhs) const; |
| 385 | bool operator!=(const GainController2& rhs) const { |
| 386 | return !(*this == rhs); |
| 387 | } |
| 388 | |
Alessio Bazzica | 980c460 | 2021-04-14 19:09:17 +0200 | [diff] [blame] | 389 | // TODO(crbug.com/webrtc/7494): Remove `LevelEstimator`. |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 390 | enum LevelEstimator { kRms, kPeak }; |
Alessio Bazzica | 61982a7 | 2021-04-14 16:17:09 +0200 | [diff] [blame] | 391 | enum NoiseEstimator { kStationaryNoise, kNoiseFloor }; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 392 | bool enabled = false; |
Alessio Bazzica | 253f836 | 2020-11-27 16:02:38 +0100 | [diff] [blame] | 393 | struct FixedDigital { |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 394 | float gain_db = 0.0f; |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 395 | } fixed_digital; |
Alessio Bazzica | dfc11d5 | 2021-05-07 11:58:11 +0200 | [diff] [blame] | 396 | struct RTC_EXPORT AdaptiveDigital { |
Alessio Bazzica | a2efd15 | 2021-04-29 16:17:49 +0200 | [diff] [blame] | 397 | bool operator==(const AdaptiveDigital& rhs) const; |
| 398 | bool operator!=(const AdaptiveDigital& rhs) const { |
| 399 | return !(*this == rhs); |
| 400 | } |
| 401 | |
Alessio Bazzica | 8da7b35 | 2018-11-21 10:50:58 +0100 | [diff] [blame] | 402 | bool enabled = false; |
Alessio Bazzica | a2efd15 | 2021-04-29 16:17:49 +0200 | [diff] [blame] | 403 | // Run the adaptive digital controller but the signal is not modified. |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 404 | bool dry_run = false; |
Alessio Bazzica | 61982a7 | 2021-04-14 16:17:09 +0200 | [diff] [blame] | 405 | NoiseEstimator noise_estimator = kNoiseFloor; |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 406 | int vad_reset_period_ms = 1500; |
Alessio Bazzica | 980c460 | 2021-04-14 19:09:17 +0200 | [diff] [blame] | 407 | int adjacent_speech_frames_threshold = 12; |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 408 | float max_gain_change_db_per_second = 3.0f; |
Alessio Bazzica | 980c460 | 2021-04-14 19:09:17 +0200 | [diff] [blame] | 409 | float max_output_noise_level_dbfs = -50.0f; |
Alessio Bazzica | 524f682 | 2021-01-05 10:28:24 +0100 | [diff] [blame] | 410 | bool sse2_allowed = true; |
Alessio Bazzica | 253f836 | 2020-11-27 16:02:38 +0100 | [diff] [blame] | 411 | bool avx2_allowed = true; |
Alessio Bazzica | 524f682 | 2021-01-05 10:28:24 +0100 | [diff] [blame] | 412 | bool neon_allowed = true; |
Alessio Bazzica | 980c460 | 2021-04-14 19:09:17 +0200 | [diff] [blame] | 413 | // TODO(crbug.com/webrtc/7494): Remove deprecated settings below. |
| 414 | float vad_probability_attack = 1.0f; |
| 415 | LevelEstimator level_estimator = kRms; |
| 416 | int level_estimator_adjacent_speech_frames_threshold = 12; |
| 417 | bool use_saturation_protector = true; |
| 418 | float initial_saturation_margin_db = 25.0f; |
| 419 | float extra_saturation_margin_db = 5.0f; |
| 420 | int gain_applier_adjacent_speech_frames_threshold = 12; |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 421 | } adaptive_digital; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 422 | } gain_controller2; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 423 | |
Sam Zackrisson | 2351313 | 2019-01-11 15:10:32 +0100 | [diff] [blame] | 424 | struct ResidualEchoDetector { |
| 425 | bool enabled = true; |
| 426 | } residual_echo_detector; |
| 427 | |
Sam Zackrisson | b24c00f | 2018-11-26 16:18:25 +0100 | [diff] [blame] | 428 | // Enables reporting of |output_rms_dbfs| in webrtc::AudioProcessingStats. |
| 429 | struct LevelEstimation { |
| 430 | bool enabled = false; |
| 431 | } level_estimation; |
| 432 | |
Artem Titov | 59bbd65 | 2019-08-02 11:31:37 +0200 | [diff] [blame] | 433 | std::string ToString() const; |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 434 | }; |
| 435 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 436 | // TODO(mgraczyk): Remove once all methods that use ChannelLayout are gone. |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 437 | enum ChannelLayout { |
| 438 | kMono, |
| 439 | // Left, right. |
| 440 | kStereo, |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 441 | // Mono, keyboard, and mic. |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 442 | kMonoAndKeyboard, |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 443 | // Left, right, keyboard, and mic. |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 444 | kStereoAndKeyboard |
| 445 | }; |
| 446 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 447 | // Specifies the properties of a setting to be passed to AudioProcessing at |
| 448 | // runtime. |
| 449 | class RuntimeSetting { |
| 450 | public: |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 451 | enum class Type { |
| 452 | kNotSpecified, |
| 453 | kCapturePreGain, |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 454 | kCaptureCompressionGain, |
Per Åhgren | 6ee75fd | 2019-04-26 11:33:37 +0200 | [diff] [blame] | 455 | kCaptureFixedPostGain, |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 456 | kPlayoutVolumeChange, |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 457 | kCustomRenderProcessingRuntimeSetting, |
Per Åhgren | 552d3e3 | 2020-08-12 08:46:47 +0200 | [diff] [blame] | 458 | kPlayoutAudioDeviceChange, |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 459 | kCapturePostGain, |
Per Åhgren | 552d3e3 | 2020-08-12 08:46:47 +0200 | [diff] [blame] | 460 | kCaptureOutputUsed |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | // Play-out audio device properties. |
| 464 | struct PlayoutAudioDeviceInfo { |
| 465 | int id; // Identifies the audio device. |
| 466 | int max_volume; // Maximum play-out volume. |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 467 | }; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 468 | |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 469 | RuntimeSetting() : type_(Type::kNotSpecified), value_(0.0f) {} |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 470 | ~RuntimeSetting() = default; |
| 471 | |
| 472 | static RuntimeSetting CreateCapturePreGain(float gain) { |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 473 | return {Type::kCapturePreGain, gain}; |
| 474 | } |
| 475 | |
Per Åhgren | db5d728 | 2021-03-15 16:31:04 +0000 | [diff] [blame] | 476 | static RuntimeSetting CreateCapturePostGain(float gain) { |
| 477 | return {Type::kCapturePostGain, gain}; |
| 478 | } |
| 479 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 480 | // Corresponds to Config::GainController1::compression_gain_db, but for |
| 481 | // runtime configuration. |
| 482 | static RuntimeSetting CreateCompressionGainDb(int gain_db) { |
| 483 | RTC_DCHECK_GE(gain_db, 0); |
| 484 | RTC_DCHECK_LE(gain_db, 90); |
| 485 | return {Type::kCaptureCompressionGain, static_cast<float>(gain_db)}; |
| 486 | } |
| 487 | |
Per Åhgren | 6ee75fd | 2019-04-26 11:33:37 +0200 | [diff] [blame] | 488 | // Corresponds to Config::GainController2::fixed_digital::gain_db, but for |
| 489 | // runtime configuration. |
| 490 | static RuntimeSetting CreateCaptureFixedPostGain(float gain_db) { |
Alessio Bazzica | 841d74e | 2021-03-31 15:04:03 +0200 | [diff] [blame] | 491 | RTC_DCHECK_GE(gain_db, 0.0f); |
| 492 | RTC_DCHECK_LE(gain_db, 90.0f); |
Per Åhgren | 6ee75fd | 2019-04-26 11:33:37 +0200 | [diff] [blame] | 493 | return {Type::kCaptureFixedPostGain, gain_db}; |
| 494 | } |
| 495 | |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 496 | // Creates a runtime setting to notify play-out (aka render) audio device |
| 497 | // changes. |
| 498 | static RuntimeSetting CreatePlayoutAudioDeviceChange( |
| 499 | PlayoutAudioDeviceInfo audio_device) { |
| 500 | return {Type::kPlayoutAudioDeviceChange, audio_device}; |
| 501 | } |
| 502 | |
| 503 | // Creates a runtime setting to notify play-out (aka render) volume changes. |
| 504 | // |volume| is the unnormalized volume, the maximum of which |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 505 | static RuntimeSetting CreatePlayoutVolumeChange(int volume) { |
| 506 | return {Type::kPlayoutVolumeChange, volume}; |
| 507 | } |
| 508 | |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 509 | static RuntimeSetting CreateCustomRenderSetting(float payload) { |
| 510 | return {Type::kCustomRenderProcessingRuntimeSetting, payload}; |
| 511 | } |
| 512 | |
Per Åhgren | 652ada5 | 2021-03-03 10:52:44 +0000 | [diff] [blame] | 513 | static RuntimeSetting CreateCaptureOutputUsedSetting( |
| 514 | bool capture_output_used) { |
| 515 | return {Type::kCaptureOutputUsed, capture_output_used}; |
Per Åhgren | 552d3e3 | 2020-08-12 08:46:47 +0200 | [diff] [blame] | 516 | } |
| 517 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 518 | Type type() const { return type_; } |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 519 | // Getters do not return a value but instead modify the argument to protect |
| 520 | // from implicit casting. |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 521 | void GetFloat(float* value) const { |
| 522 | RTC_DCHECK(value); |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 523 | *value = value_.float_value; |
| 524 | } |
| 525 | void GetInt(int* value) const { |
| 526 | RTC_DCHECK(value); |
| 527 | *value = value_.int_value; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 528 | } |
Per Åhgren | 552d3e3 | 2020-08-12 08:46:47 +0200 | [diff] [blame] | 529 | void GetBool(bool* value) const { |
| 530 | RTC_DCHECK(value); |
| 531 | *value = value_.bool_value; |
| 532 | } |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 533 | void GetPlayoutAudioDeviceInfo(PlayoutAudioDeviceInfo* value) const { |
| 534 | RTC_DCHECK(value); |
| 535 | *value = value_.playout_audio_device_info; |
| 536 | } |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 537 | |
| 538 | private: |
| 539 | RuntimeSetting(Type id, float value) : type_(id), value_(value) {} |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 540 | RuntimeSetting(Type id, int value) : type_(id), value_(value) {} |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 541 | RuntimeSetting(Type id, PlayoutAudioDeviceInfo value) |
| 542 | : type_(id), value_(value) {} |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 543 | Type type_; |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 544 | union U { |
| 545 | U() {} |
| 546 | U(int value) : int_value(value) {} |
| 547 | U(float value) : float_value(value) {} |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 548 | U(PlayoutAudioDeviceInfo value) : playout_audio_device_info(value) {} |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 549 | float float_value; |
| 550 | int int_value; |
Per Åhgren | 552d3e3 | 2020-08-12 08:46:47 +0200 | [diff] [blame] | 551 | bool bool_value; |
Alessio Bazzica | 7c19a70 | 2019-11-07 13:22:00 +0100 | [diff] [blame] | 552 | PlayoutAudioDeviceInfo playout_audio_device_info; |
Fredrik Hernqvist | ca36285 | 2019-05-10 15:50:02 +0200 | [diff] [blame] | 553 | } value_; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 554 | }; |
| 555 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 556 | ~AudioProcessing() override {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 557 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 558 | // Initializes internal states, while retaining all user settings. This |
| 559 | // should be called before beginning to process a new audio stream. However, |
| 560 | // 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] | 561 | // creation. |
| 562 | // |
| 563 | // 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] | 564 | // rate and number of channels) have changed. Passing updated parameters |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 565 | // directly to |ProcessStream()| and |ProcessReverseStream()| is permissible. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 566 | // If the parameters are known at init-time though, they may be provided. |
Per Åhgren | 0ade983 | 2020-09-01 23:57:20 +0200 | [diff] [blame] | 567 | // TODO(webrtc:5298): Change to return void. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 568 | virtual int Initialize() = 0; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 569 | |
| 570 | // The int16 interfaces require: |
| 571 | // - only |NativeRate|s be used |
| 572 | // - that the input, output and reverse rates must match |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 573 | // - that |processing_config.output_stream()| matches |
| 574 | // |processing_config.input_stream()|. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 575 | // |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 576 | // The float interfaces accept arbitrary rates and support differing input and |
| 577 | // output layouts, but the output must have either one channel or the same |
| 578 | // number of channels as the input. |
| 579 | virtual int Initialize(const ProcessingConfig& processing_config) = 0; |
| 580 | |
| 581 | // Initialize with unpacked parameters. See Initialize() above for details. |
| 582 | // |
| 583 | // TODO(mgraczyk): Remove once clients are updated to use the new interface. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 584 | virtual int Initialize(int capture_input_sample_rate_hz, |
| 585 | int capture_output_sample_rate_hz, |
| 586 | int render_sample_rate_hz, |
| 587 | ChannelLayout capture_input_layout, |
| 588 | ChannelLayout capture_output_layout, |
| 589 | ChannelLayout render_input_layout) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 590 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 591 | // TODO(peah): This method is a temporary solution used to take control |
| 592 | // over the parameters in the audio processing module and is likely to change. |
| 593 | virtual void ApplyConfig(const Config& config) = 0; |
| 594 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 595 | // TODO(ajm): Only intended for internal use. Make private and friend the |
| 596 | // necessary classes? |
| 597 | virtual int proc_sample_rate_hz() const = 0; |
| 598 | virtual int proc_split_sample_rate_hz() const = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 599 | virtual size_t num_input_channels() const = 0; |
| 600 | virtual size_t num_proc_channels() const = 0; |
| 601 | virtual size_t num_output_channels() const = 0; |
| 602 | virtual size_t num_reverse_channels() const = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 603 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 604 | // Set to true when the output of AudioProcessing will be muted or in some |
| 605 | // other way not used. Ideally, the captured audio would still be processed, |
| 606 | // but some components may change behavior based on this information. |
Per Åhgren | 0a144a7 | 2021-02-09 08:47:51 +0100 | [diff] [blame] | 607 | // Default false. This method takes a lock. To achieve this in a lock-less |
| 608 | // manner the PostRuntimeSetting can instead be used. |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 609 | virtual void set_output_will_be_muted(bool muted) = 0; |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 610 | |
Per Åhgren | 0a144a7 | 2021-02-09 08:47:51 +0100 | [diff] [blame] | 611 | // Enqueues a runtime setting. |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 612 | virtual void SetRuntimeSetting(RuntimeSetting setting) = 0; |
| 613 | |
Per Åhgren | 0a144a7 | 2021-02-09 08:47:51 +0100 | [diff] [blame] | 614 | // Enqueues a runtime setting. Returns a bool indicating whether the |
| 615 | // enqueueing was successfull. |
Per Åhgren | 8eea117 | 2021-02-09 23:15:07 +0100 | [diff] [blame] | 616 | virtual bool PostRuntimeSetting(RuntimeSetting setting) = 0; |
Per Åhgren | 0a144a7 | 2021-02-09 08:47:51 +0100 | [diff] [blame] | 617 | |
Per Åhgren | 645f24c | 2020-03-16 12:06:02 +0100 | [diff] [blame] | 618 | // Accepts and produces a 10 ms frame interleaved 16 bit integer audio as |
| 619 | // specified in |input_config| and |output_config|. |src| and |dest| may use |
| 620 | // the same memory, if desired. |
| 621 | virtual int ProcessStream(const int16_t* const src, |
| 622 | const StreamConfig& input_config, |
| 623 | const StreamConfig& output_config, |
Per Åhgren | dc5522b | 2020-03-19 14:55:58 +0100 | [diff] [blame] | 624 | int16_t* const dest) = 0; |
Per Åhgren | 645f24c | 2020-03-16 12:06:02 +0100 | [diff] [blame] | 625 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 626 | // Accepts deinterleaved float audio with the range [-1, 1]. Each element of |
| 627 | // |src| points to a channel buffer, arranged according to |input_stream|. At |
| 628 | // output, the channels will be arranged according to |output_stream| in |
| 629 | // |dest|. |
| 630 | // |
| 631 | // The output must have one channel or as many channels as the input. |src| |
| 632 | // and |dest| may use the same memory, if desired. |
| 633 | virtual int ProcessStream(const float* const* src, |
| 634 | const StreamConfig& input_config, |
| 635 | const StreamConfig& output_config, |
| 636 | float* const* dest) = 0; |
| 637 | |
Per Åhgren | 645f24c | 2020-03-16 12:06:02 +0100 | [diff] [blame] | 638 | // Accepts and produces a 10 ms frame of interleaved 16 bit integer audio for |
| 639 | // the reverse direction audio stream as specified in |input_config| and |
| 640 | // |output_config|. |src| and |dest| may use the same memory, if desired. |
| 641 | virtual int ProcessReverseStream(const int16_t* const src, |
| 642 | const StreamConfig& input_config, |
| 643 | const StreamConfig& output_config, |
| 644 | int16_t* const dest) = 0; |
| 645 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 646 | // Accepts deinterleaved float audio with the range [-1, 1]. Each element of |
| 647 | // |data| points to a channel buffer, arranged according to |reverse_config|. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 648 | virtual int ProcessReverseStream(const float* const* src, |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 649 | const StreamConfig& input_config, |
| 650 | const StreamConfig& output_config, |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 651 | float* const* dest) = 0; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 652 | |
Gustaf Ullberg | cb30726 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 653 | // Accepts deinterleaved float audio with the range [-1, 1]. Each element |
| 654 | // of |data| points to a channel buffer, arranged according to |
| 655 | // |reverse_config|. |
| 656 | virtual int AnalyzeReverseStream(const float* const* data, |
| 657 | const StreamConfig& reverse_config) = 0; |
| 658 | |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 659 | // Returns the most recently produced 10 ms of the linear AEC output at a rate |
| 660 | // of 16 kHz. If there is more than one capture channel, a mono representation |
| 661 | // of the input is returned. Returns true/false to indicate whether an output |
| 662 | // returned. |
| 663 | virtual bool GetLinearAecOutput( |
| 664 | rtc::ArrayView<std::array<float, 160>> linear_output) const = 0; |
| 665 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 666 | // This must be called prior to ProcessStream() if and only if adaptive analog |
| 667 | // gain control is enabled, to pass the current analog level from the audio |
| 668 | // HAL. Must be within the range provided in Config::GainController1. |
| 669 | virtual void set_stream_analog_level(int level) = 0; |
| 670 | |
| 671 | // When an analog mode is set, this should be called after ProcessStream() |
| 672 | // to obtain the recommended new analog level for the audio HAL. It is the |
| 673 | // user's responsibility to apply this level. |
| 674 | virtual int recommended_stream_analog_level() const = 0; |
| 675 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 676 | // This must be called if and only if echo processing is enabled. |
| 677 | // |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 678 | // Sets the |delay| in ms between ProcessReverseStream() receiving a far-end |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 679 | // frame and ProcessStream() receiving a near-end frame containing the |
| 680 | // corresponding echo. On the client-side this can be expressed as |
| 681 | // delay = (t_render - t_analyze) + (t_process - t_capture) |
| 682 | // where, |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 683 | // - 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] | 684 | // t_render is the time the first sample of the same frame is rendered by |
| 685 | // the audio hardware. |
| 686 | // - 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] | 687 | // 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] | 688 | // ProcessStream(). |
| 689 | virtual int set_stream_delay_ms(int delay) = 0; |
| 690 | virtual int stream_delay_ms() const = 0; |
| 691 | |
andrew@webrtc.org | 75dd288 | 2014-02-11 20:52:30 +0000 | [diff] [blame] | 692 | // Call to signal that a key press occurred (true) or did not occur (false) |
| 693 | // with this chunk of audio. |
| 694 | virtual void set_stream_key_pressed(bool key_pressed) = 0; |
andrew@webrtc.org | 75dd288 | 2014-02-11 20:52:30 +0000 | [diff] [blame] | 695 | |
Per Åhgren | 09e9a83 | 2020-05-11 11:03:47 +0200 | [diff] [blame] | 696 | // Creates and attaches an webrtc::AecDump for recording debugging |
| 697 | // information. |
| 698 | // The |worker_queue| may not be null and must outlive the created |
| 699 | // AecDump instance. |max_log_size_bytes == -1| means the log size |
| 700 | // will be unlimited. |handle| may not be null. The AecDump takes |
| 701 | // responsibility for |handle| and closes it in the destructor. A |
| 702 | // return value of true indicates that the file has been |
| 703 | // sucessfully opened, while a value of false indicates that |
| 704 | // opening the file failed. |
| 705 | virtual bool CreateAndAttachAecDump(const std::string& file_name, |
| 706 | int64_t max_log_size_bytes, |
| 707 | rtc::TaskQueue* worker_queue) = 0; |
| 708 | virtual bool CreateAndAttachAecDump(FILE* handle, |
| 709 | int64_t max_log_size_bytes, |
| 710 | rtc::TaskQueue* worker_queue) = 0; |
| 711 | |
| 712 | // TODO(webrtc:5298) Deprecated variant. |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 713 | // Attaches provided webrtc::AecDump for recording debugging |
| 714 | // information. Log file and maximum file size logic is supposed to |
| 715 | // be handled by implementing instance of AecDump. Calling this |
| 716 | // method when another AecDump is attached resets the active AecDump |
| 717 | // with a new one. This causes the d-tor of the earlier AecDump to |
| 718 | // be called. The d-tor call may block until all pending logging |
| 719 | // tasks are completed. |
Alex Loiko | be767e0 | 2017-06-08 09:45:03 +0200 | [diff] [blame] | 720 | virtual void AttachAecDump(std::unique_ptr<AecDump> aec_dump) = 0; |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 721 | |
| 722 | // If no AecDump is attached, this has no effect. If an AecDump is |
| 723 | // attached, it's destructor is called. The d-tor may block until |
| 724 | // all pending logging tasks are completed. |
Alex Loiko | be767e0 | 2017-06-08 09:45:03 +0200 | [diff] [blame] | 725 | virtual void DetachAecDump() = 0; |
aleloi | 868f32f | 2017-05-23 07:20:05 -0700 | [diff] [blame] | 726 | |
Per Åhgren | cf4c872 | 2019-12-30 14:32:14 +0100 | [diff] [blame] | 727 | // Get audio processing statistics. |
| 728 | virtual AudioProcessingStats GetStatistics() = 0; |
| 729 | // TODO(webrtc:5298) Deprecated variant. The |has_remote_tracks| argument |
| 730 | // should be set if there are active remote tracks (this would usually be true |
| 731 | // during a call). If there are no remote tracks some of the stats will not be |
| 732 | // set by AudioProcessing, because they only make sense if there is at least |
| 733 | // one remote track. |
| 734 | virtual AudioProcessingStats GetStatistics(bool has_remote_tracks) = 0; |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 735 | |
henrik.lundin | adf0635 | 2017-04-05 05:48:24 -0700 | [diff] [blame] | 736 | // Returns the last applied configuration. |
henrik.lundin | 7749286 | 2017-04-06 23:28:09 -0700 | [diff] [blame] | 737 | virtual AudioProcessing::Config GetConfig() const = 0; |
henrik.lundin | adf0635 | 2017-04-05 05:48:24 -0700 | [diff] [blame] | 738 | |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 739 | enum Error { |
| 740 | // Fatal errors. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 741 | kNoError = 0, |
| 742 | kUnspecifiedError = -1, |
| 743 | kCreationFailedError = -2, |
| 744 | kUnsupportedComponentError = -3, |
| 745 | kUnsupportedFunctionError = -4, |
| 746 | kNullPointerError = -5, |
| 747 | kBadParameterError = -6, |
| 748 | kBadSampleRateError = -7, |
| 749 | kBadDataLengthError = -8, |
| 750 | kBadNumberChannelsError = -9, |
| 751 | kFileError = -10, |
| 752 | kStreamParameterNotSetError = -11, |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 753 | kNotEnabledError = -12, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 754 | |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 755 | // Warnings are non-fatal. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 756 | // This results when a set_stream_ parameter is out of range. Processing |
| 757 | // will continue, but the parameter may have been truncated. |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 758 | kBadStreamParameterWarning = -13 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 759 | }; |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 760 | |
Per Åhgren | 2507f8c | 2020-03-19 12:33:29 +0100 | [diff] [blame] | 761 | // Native rates supported by the integer interfaces. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 762 | enum NativeRate { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 763 | kSampleRate8kHz = 8000, |
| 764 | kSampleRate16kHz = 16000, |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 765 | kSampleRate32kHz = 32000, |
| 766 | kSampleRate48kHz = 48000 |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 767 | }; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 768 | |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 769 | // TODO(kwiberg): We currently need to support a compiler (Visual C++) that |
| 770 | // complains if we don't explicitly state the size of the array here. Remove |
| 771 | // the size when that's no longer the case. |
| 772 | static constexpr int kNativeSampleRatesHz[4] = { |
| 773 | kSampleRate8kHz, kSampleRate16kHz, kSampleRate32kHz, kSampleRate48kHz}; |
| 774 | static constexpr size_t kNumNativeSampleRates = |
| 775 | arraysize(kNativeSampleRatesHz); |
| 776 | static constexpr int kMaxNativeSampleRateHz = |
| 777 | kNativeSampleRatesHz[kNumNativeSampleRates - 1]; |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 778 | |
Per Åhgren | 12dc274 | 2020-12-08 09:40:35 +0100 | [diff] [blame] | 779 | static constexpr int kChunkSizeMs = 10; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 780 | }; |
| 781 | |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 782 | class RTC_EXPORT AudioProcessingBuilder { |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 783 | public: |
| 784 | AudioProcessingBuilder(); |
| 785 | ~AudioProcessingBuilder(); |
| 786 | // The AudioProcessingBuilder takes ownership of the echo_control_factory. |
| 787 | AudioProcessingBuilder& SetEchoControlFactory( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 788 | std::unique_ptr<EchoControlFactory> echo_control_factory) { |
| 789 | echo_control_factory_ = std::move(echo_control_factory); |
| 790 | return *this; |
| 791 | } |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 792 | // The AudioProcessingBuilder takes ownership of the capture_post_processing. |
| 793 | AudioProcessingBuilder& SetCapturePostProcessing( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 794 | std::unique_ptr<CustomProcessing> capture_post_processing) { |
| 795 | capture_post_processing_ = std::move(capture_post_processing); |
| 796 | return *this; |
| 797 | } |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 798 | // The AudioProcessingBuilder takes ownership of the render_pre_processing. |
| 799 | AudioProcessingBuilder& SetRenderPreProcessing( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 800 | std::unique_ptr<CustomProcessing> render_pre_processing) { |
| 801 | render_pre_processing_ = std::move(render_pre_processing); |
| 802 | return *this; |
| 803 | } |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 804 | // The AudioProcessingBuilder takes ownership of the echo_detector. |
| 805 | AudioProcessingBuilder& SetEchoDetector( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 806 | rtc::scoped_refptr<EchoDetector> echo_detector) { |
| 807 | echo_detector_ = std::move(echo_detector); |
| 808 | return *this; |
| 809 | } |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 810 | // The AudioProcessingBuilder takes ownership of the capture_analyzer. |
| 811 | AudioProcessingBuilder& SetCaptureAnalyzer( |
Per Åhgren | cc73ed3 | 2020-04-26 23:56:17 +0200 | [diff] [blame] | 812 | std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) { |
| 813 | capture_analyzer_ = std::move(capture_analyzer); |
| 814 | return *this; |
| 815 | } |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 816 | // This creates an APM instance using the previously set components. Calling |
| 817 | // the Create function resets the AudioProcessingBuilder to its initial state. |
| 818 | AudioProcessing* Create(); |
| 819 | AudioProcessing* Create(const webrtc::Config& config); |
| 820 | |
| 821 | private: |
| 822 | std::unique_ptr<EchoControlFactory> echo_control_factory_; |
| 823 | std::unique_ptr<CustomProcessing> capture_post_processing_; |
| 824 | std::unique_ptr<CustomProcessing> render_pre_processing_; |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 825 | rtc::scoped_refptr<EchoDetector> echo_detector_; |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 826 | std::unique_ptr<CustomAudioAnalyzer> capture_analyzer_; |
Ivo Creusen | 5ec7e12 | 2017-12-22 11:35:59 +0100 | [diff] [blame] | 827 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioProcessingBuilder); |
| 828 | }; |
| 829 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 830 | class StreamConfig { |
| 831 | public: |
| 832 | // sample_rate_hz: The sampling rate of the stream. |
| 833 | // |
| 834 | // num_channels: The number of audio channels in the stream, excluding the |
| 835 | // keyboard channel if it is present. When passing a |
| 836 | // StreamConfig with an array of arrays T*[N], |
| 837 | // |
| 838 | // N == {num_channels + 1 if has_keyboard |
| 839 | // {num_channels if !has_keyboard |
| 840 | // |
| 841 | // has_keyboard: True if the stream has a keyboard channel. When has_keyboard |
| 842 | // is true, the last channel in any corresponding list of |
| 843 | // channels is the keyboard channel. |
| 844 | StreamConfig(int sample_rate_hz = 0, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 845 | size_t num_channels = 0, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 846 | bool has_keyboard = false) |
| 847 | : sample_rate_hz_(sample_rate_hz), |
| 848 | num_channels_(num_channels), |
| 849 | has_keyboard_(has_keyboard), |
| 850 | num_frames_(calculate_frames(sample_rate_hz)) {} |
| 851 | |
| 852 | void set_sample_rate_hz(int value) { |
| 853 | sample_rate_hz_ = value; |
| 854 | num_frames_ = calculate_frames(value); |
| 855 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 856 | void set_num_channels(size_t value) { num_channels_ = value; } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 857 | void set_has_keyboard(bool value) { has_keyboard_ = value; } |
| 858 | |
| 859 | int sample_rate_hz() const { return sample_rate_hz_; } |
| 860 | |
| 861 | // The number of channels in the stream, not including the keyboard channel if |
| 862 | // present. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 863 | size_t num_channels() const { return num_channels_; } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 864 | |
| 865 | bool has_keyboard() const { return has_keyboard_; } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 866 | size_t num_frames() const { return num_frames_; } |
| 867 | size_t num_samples() const { return num_channels_ * num_frames_; } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 868 | |
| 869 | bool operator==(const StreamConfig& other) const { |
| 870 | return sample_rate_hz_ == other.sample_rate_hz_ && |
| 871 | num_channels_ == other.num_channels_ && |
| 872 | has_keyboard_ == other.has_keyboard_; |
| 873 | } |
| 874 | |
| 875 | bool operator!=(const StreamConfig& other) const { return !(*this == other); } |
| 876 | |
| 877 | private: |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 878 | static size_t calculate_frames(int sample_rate_hz) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 879 | return static_cast<size_t>(AudioProcessing::kChunkSizeMs * sample_rate_hz / |
| 880 | 1000); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | int sample_rate_hz_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 884 | size_t num_channels_; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 885 | bool has_keyboard_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 886 | size_t num_frames_; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 887 | }; |
| 888 | |
| 889 | class ProcessingConfig { |
| 890 | public: |
| 891 | enum StreamName { |
| 892 | kInputStream, |
| 893 | kOutputStream, |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 894 | kReverseInputStream, |
| 895 | kReverseOutputStream, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 896 | kNumStreamNames, |
| 897 | }; |
| 898 | |
| 899 | const StreamConfig& input_stream() const { |
| 900 | return streams[StreamName::kInputStream]; |
| 901 | } |
| 902 | const StreamConfig& output_stream() const { |
| 903 | return streams[StreamName::kOutputStream]; |
| 904 | } |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 905 | const StreamConfig& reverse_input_stream() const { |
| 906 | return streams[StreamName::kReverseInputStream]; |
| 907 | } |
| 908 | const StreamConfig& reverse_output_stream() const { |
| 909 | return streams[StreamName::kReverseOutputStream]; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | StreamConfig& input_stream() { return streams[StreamName::kInputStream]; } |
| 913 | StreamConfig& output_stream() { return streams[StreamName::kOutputStream]; } |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 914 | StreamConfig& reverse_input_stream() { |
| 915 | return streams[StreamName::kReverseInputStream]; |
| 916 | } |
| 917 | StreamConfig& reverse_output_stream() { |
| 918 | return streams[StreamName::kReverseOutputStream]; |
| 919 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 920 | |
| 921 | bool operator==(const ProcessingConfig& other) const { |
| 922 | for (int i = 0; i < StreamName::kNumStreamNames; ++i) { |
| 923 | if (this->streams[i] != other.streams[i]) { |
| 924 | return false; |
| 925 | } |
| 926 | } |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | bool operator!=(const ProcessingConfig& other) const { |
| 931 | return !(*this == other); |
| 932 | } |
| 933 | |
| 934 | StreamConfig streams[StreamName::kNumStreamNames]; |
| 935 | }; |
| 936 | |
Valeriia Nemychnikova | f06eb57 | 2018-08-29 10:37:09 +0200 | [diff] [blame] | 937 | // Experimental interface for a custom analysis submodule. |
| 938 | class CustomAudioAnalyzer { |
| 939 | public: |
| 940 | // (Re-) Initializes the submodule. |
| 941 | virtual void Initialize(int sample_rate_hz, int num_channels) = 0; |
| 942 | // Analyzes the given capture or render signal. |
| 943 | virtual void Analyze(const AudioBuffer* audio) = 0; |
| 944 | // Returns a string representation of the module state. |
| 945 | virtual std::string ToString() const = 0; |
| 946 | |
| 947 | virtual ~CustomAudioAnalyzer() {} |
| 948 | }; |
| 949 | |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame] | 950 | // Interface for a custom processing submodule. |
| 951 | class CustomProcessing { |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 952 | public: |
| 953 | // (Re-)Initializes the submodule. |
| 954 | virtual void Initialize(int sample_rate_hz, int num_channels) = 0; |
| 955 | // Processes the given capture or render signal. |
| 956 | virtual void Process(AudioBuffer* audio) = 0; |
| 957 | // Returns a string representation of the module state. |
| 958 | virtual std::string ToString() const = 0; |
Alex Loiko | 73ec019 | 2018-05-15 10:52:28 +0200 | [diff] [blame] | 959 | // Handles RuntimeSettings. TODO(webrtc:9262): make pure virtual |
| 960 | // after updating dependencies. |
| 961 | virtual void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting); |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 962 | |
Alex Loiko | 5825aa6 | 2017-12-18 16:02:40 +0100 | [diff] [blame] | 963 | virtual ~CustomProcessing() {} |
Sam Zackrisson | 0beac58 | 2017-09-25 12:04:02 +0200 | [diff] [blame] | 964 | }; |
| 965 | |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 966 | // Interface for an echo detector submodule. |
Ivo Creusen | d1f970d | 2018-06-14 11:02:03 +0200 | [diff] [blame] | 967 | class EchoDetector : public rtc::RefCountInterface { |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 968 | public: |
| 969 | // (Re-)Initializes the submodule. |
Ivo Creusen | 647ef09 | 2018-03-14 17:13:48 +0100 | [diff] [blame] | 970 | virtual void Initialize(int capture_sample_rate_hz, |
| 971 | int num_capture_channels, |
| 972 | int render_sample_rate_hz, |
| 973 | int num_render_channels) = 0; |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 974 | |
| 975 | // Analysis (not changing) of the render signal. |
| 976 | virtual void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) = 0; |
| 977 | |
| 978 | // Analysis (not changing) of the capture signal. |
| 979 | virtual void AnalyzeCaptureAudio( |
| 980 | rtc::ArrayView<const float> capture_audio) = 0; |
| 981 | |
| 982 | // Pack an AudioBuffer into a vector<float>. |
| 983 | static void PackRenderAudioBuffer(AudioBuffer* audio, |
| 984 | std::vector<float>* packed_buffer); |
| 985 | |
| 986 | struct Metrics { |
Ivo Creusen | bb826c9 | 2020-04-29 14:34:48 +0200 | [diff] [blame] | 987 | absl::optional<double> echo_likelihood; |
| 988 | absl::optional<double> echo_likelihood_recent_max; |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 989 | }; |
| 990 | |
| 991 | // Collect current metrics from the echo detector. |
| 992 | virtual Metrics GetMetrics() const = 0; |
Ivo Creusen | 09fa4b0 | 2018-01-11 16:08:54 +0100 | [diff] [blame] | 993 | }; |
| 994 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 995 | } // namespace webrtc |
| 996 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 997 | #endif // MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_ |