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