niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +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 | #include "modules/audio_processing/gain_control_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <cstdint> |
| 14 | |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 15 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/audio_processing/agc/legacy/gain_control.h" |
| 17 | #include "modules/audio_processing/audio_buffer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/include/audio_processing.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/constructor_magic.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | |
| 25 | typedef void Handle; |
| 26 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | namespace { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 28 | int16_t MapSetting(GainControl::Mode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | switch (mode) { |
| 30 | case GainControl::kAdaptiveAnalog: |
| 31 | return kAgcModeAdaptiveAnalog; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | case GainControl::kAdaptiveDigital: |
| 33 | return kAgcModeAdaptiveDigital; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | case GainControl::kFixedDigital: |
| 35 | return kAgcModeFixedDigital; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 37 | RTC_NOTREACHED(); |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 38 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
peah | 2446e5a | 2015-11-18 06:11:13 -0800 | [diff] [blame] | 41 | } // namespace |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 42 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 43 | class GainControlImpl::GainController { |
| 44 | public: |
| 45 | explicit GainController() { |
| 46 | state_ = WebRtcAgc_Create(); |
| 47 | RTC_CHECK(state_); |
| 48 | } |
| 49 | |
| 50 | ~GainController() { |
| 51 | RTC_DCHECK(state_); |
| 52 | WebRtcAgc_Free(state_); |
| 53 | } |
| 54 | |
| 55 | Handle* state() { |
| 56 | RTC_DCHECK(state_); |
| 57 | return state_; |
| 58 | } |
| 59 | |
| 60 | void Initialize(int minimum_capture_level, |
| 61 | int maximum_capture_level, |
| 62 | Mode mode, |
| 63 | int sample_rate_hz, |
| 64 | int capture_level) { |
| 65 | RTC_DCHECK(state_); |
| 66 | int error = |
| 67 | WebRtcAgc_Init(state_, minimum_capture_level, maximum_capture_level, |
| 68 | MapSetting(mode), sample_rate_hz); |
| 69 | RTC_DCHECK_EQ(0, error); |
| 70 | |
| 71 | set_capture_level(capture_level); |
| 72 | } |
| 73 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 74 | void set_capture_level(int capture_level) { capture_level_ = capture_level; } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 75 | |
| 76 | int get_capture_level() { |
| 77 | RTC_DCHECK(capture_level_); |
| 78 | return *capture_level_; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | Handle* state_; |
| 83 | // TODO(peah): Remove the optional once the initialization is moved into the |
| 84 | // ctor. |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 85 | absl::optional<int> capture_level_; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 86 | |
| 87 | RTC_DISALLOW_COPY_AND_ASSIGN(GainController); |
| 88 | }; |
| 89 | |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 90 | int GainControlImpl::instance_counter_ = 0; |
| 91 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 92 | GainControlImpl::GainControlImpl() |
| 93 | : data_dumper_(new ApmDataDumper(instance_counter_)), |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 94 | mode_(kAdaptiveAnalog), |
| 95 | minimum_capture_level_(0), |
| 96 | maximum_capture_level_(255), |
| 97 | limiter_enabled_(true), |
| 98 | target_level_dbfs_(3), |
| 99 | compression_gain_db_(9), |
| 100 | analog_capture_level_(0), |
| 101 | was_analog_level_set_(false), |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 102 | stream_is_saturated_(false) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | |
| 104 | GainControlImpl::~GainControlImpl() {} |
| 105 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 106 | void GainControlImpl::ProcessRenderAudio( |
| 107 | rtc::ArrayView<const int16_t> packed_render_audio) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 108 | if (!enabled_) { |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 112 | for (auto& gain_controller : gain_controllers_) { |
| 113 | WebRtcAgc_AddFarend(gain_controller->state(), packed_render_audio.data(), |
| 114 | packed_render_audio.size()); |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 118 | void GainControlImpl::PackRenderAudioBuffer( |
| 119 | AudioBuffer* audio, |
| 120 | std::vector<int16_t>* packed_buffer) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 121 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 122 | |
| 123 | packed_buffer->clear(); |
| 124 | packed_buffer->insert( |
| 125 | packed_buffer->end(), audio->mixed_low_pass_data(), |
| 126 | (audio->mixed_low_pass_data() + audio->num_frames_per_band())); |
| 127 | } |
| 128 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 130 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 131 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | } |
| 133 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 134 | RTC_DCHECK(num_proc_channels_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 135 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 136 | RTC_DCHECK_EQ(audio->num_channels(), *num_proc_channels_); |
| 137 | RTC_DCHECK_LE(*num_proc_channels_, gain_controllers_.size()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
| 139 | if (mode_ == kAdaptiveAnalog) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 140 | int capture_channel = 0; |
| 141 | for (auto& gain_controller : gain_controllers_) { |
| 142 | gain_controller->set_capture_level(analog_capture_level_); |
| 143 | int err = WebRtcAgc_AddMic( |
| 144 | gain_controller->state(), audio->split_bands(capture_channel), |
| 145 | audio->num_bands(), audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 147 | if (err != AudioProcessing::kNoError) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 148 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 150 | ++capture_channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | } |
| 152 | } else if (mode_ == kAdaptiveDigital) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 153 | int capture_channel = 0; |
| 154 | for (auto& gain_controller : gain_controllers_) { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 155 | int32_t capture_level_out = 0; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 156 | int err = WebRtcAgc_VirtualMic( |
| 157 | gain_controller->state(), audio->split_bands(capture_channel), |
| 158 | audio->num_bands(), audio->num_frames_per_band(), |
| 159 | analog_capture_level_, &capture_level_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 161 | gain_controller->set_capture_level(capture_level_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 163 | if (err != AudioProcessing::kNoError) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 164 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 166 | ++capture_channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 170 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | } |
| 172 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 173 | int GainControlImpl::ProcessCaptureAudio(AudioBuffer* audio, |
| 174 | bool stream_has_echo) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 175 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 176 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (mode_ == kAdaptiveAnalog && !was_analog_level_set_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 180 | return AudioProcessing::kStreamParameterNotSetError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | } |
| 182 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 183 | RTC_DCHECK(num_proc_channels_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 184 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 185 | RTC_DCHECK_EQ(audio->num_channels(), *num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | |
| 187 | stream_is_saturated_ = false; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 188 | int capture_channel = 0; |
| 189 | for (auto& gain_controller : gain_controllers_) { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 190 | int32_t capture_level_out = 0; |
| 191 | uint8_t saturation_warning = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 192 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 193 | // The call to stream_has_echo() is ok from a deadlock perspective |
| 194 | // as the capture lock is allready held. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | int err = WebRtcAgc_Process( |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 196 | gain_controller->state(), audio->split_bands_const(capture_channel), |
| 197 | audio->num_bands(), audio->num_frames_per_band(), |
| 198 | audio->split_bands(capture_channel), |
| 199 | gain_controller->get_capture_level(), &capture_level_out, |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 200 | stream_has_echo, &saturation_warning); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 201 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 202 | if (err != AudioProcessing::kNoError) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 203 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 206 | gain_controller->set_capture_level(capture_level_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | if (saturation_warning == 1) { |
| 208 | stream_is_saturated_ = true; |
| 209 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 210 | |
| 211 | ++capture_channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | } |
| 213 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 214 | RTC_DCHECK_LT(0ul, *num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | if (mode_ == kAdaptiveAnalog) { |
| 216 | // Take the analog level to be the average across the handles. |
| 217 | analog_capture_level_ = 0; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 218 | for (auto& gain_controller : gain_controllers_) { |
| 219 | analog_capture_level_ += gain_controller->get_capture_level(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | } |
| 221 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 222 | analog_capture_level_ /= (*num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | was_analog_level_set_ = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 226 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | } |
| 228 | |
aluebs | 11d4a42 | 2016-04-28 14:58:32 -0700 | [diff] [blame] | 229 | int GainControlImpl::compression_gain_db() const { |
aluebs | 11d4a42 | 2016-04-28 14:58:32 -0700 | [diff] [blame] | 230 | return compression_gain_db_; |
| 231 | } |
| 232 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 233 | // TODO(ajm): ensure this is called under kAdaptiveAnalog. |
| 234 | int GainControlImpl::set_stream_analog_level(int level) { |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 235 | data_dumper_->DumpRaw("gain_control_set_stream_analog_level", 1, &level); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 236 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | was_analog_level_set_ = true; |
| 238 | if (level < minimum_capture_level_ || level > maximum_capture_level_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 239 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | analog_capture_level_ = level; |
| 242 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 243 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Sam Zackrisson | f0d1c03 | 2019-03-27 13:28:08 +0100 | [diff] [blame] | 246 | int GainControlImpl::stream_analog_level() const { |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 247 | data_dumper_->DumpRaw("gain_control_stream_analog_level", 1, |
| 248 | &analog_capture_level_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | // TODO(ajm): enable this assertion? |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 250 | // RTC_DCHECK_EQ(kAdaptiveAnalog, mode_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | |
| 252 | return analog_capture_level_; |
| 253 | } |
| 254 | |
| 255 | int GainControlImpl::Enable(bool enable) { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 256 | if (enable && !enabled_) { |
| 257 | enabled_ = enable; // Must be set before Initialize() is called. |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 258 | |
| 259 | RTC_DCHECK(num_proc_channels_); |
| 260 | RTC_DCHECK(sample_rate_hz_); |
| 261 | Initialize(*num_proc_channels_, *sample_rate_hz_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 262 | } else { |
| 263 | enabled_ = enable; |
| 264 | } |
| 265 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | bool GainControlImpl::is_enabled() const { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 269 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int GainControlImpl::set_mode(Mode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | if (MapSetting(mode) == -1) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 274 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | mode_ = mode; |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 278 | RTC_DCHECK(num_proc_channels_); |
| 279 | RTC_DCHECK(sample_rate_hz_); |
| 280 | Initialize(*num_proc_channels_, *sample_rate_hz_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 281 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | GainControl::Mode GainControlImpl::mode() const { |
| 285 | return mode_; |
| 286 | } |
| 287 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 288 | int GainControlImpl::set_analog_level_limits(int minimum, int maximum) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | if (minimum < 0) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 290 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (maximum > 65535) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 294 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if (maximum < minimum) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 298 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 299 | } |
| 300 | |
peah | 7c931ad | 2016-03-24 12:52:02 -0700 | [diff] [blame] | 301 | size_t num_proc_channels_local = 0u; |
| 302 | int sample_rate_hz_local = 0; |
| 303 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 304 | |
peah | 7c931ad | 2016-03-24 12:52:02 -0700 | [diff] [blame] | 305 | minimum_capture_level_ = minimum; |
| 306 | maximum_capture_level_ = maximum; |
| 307 | |
| 308 | RTC_DCHECK(num_proc_channels_); |
| 309 | RTC_DCHECK(sample_rate_hz_); |
| 310 | num_proc_channels_local = *num_proc_channels_; |
| 311 | sample_rate_hz_local = *sample_rate_hz_; |
| 312 | } |
| 313 | Initialize(num_proc_channels_local, sample_rate_hz_local); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 314 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | int GainControlImpl::analog_level_minimum() const { |
| 318 | return minimum_capture_level_; |
| 319 | } |
| 320 | |
| 321 | int GainControlImpl::analog_level_maximum() const { |
| 322 | return maximum_capture_level_; |
| 323 | } |
| 324 | |
| 325 | bool GainControlImpl::stream_is_saturated() const { |
| 326 | return stream_is_saturated_; |
| 327 | } |
| 328 | |
| 329 | int GainControlImpl::set_target_level_dbfs(int level) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 330 | if (level > 31 || level < 0) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 331 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 332 | } |
Sam Zackrisson | 421c859 | 2019-02-11 13:39:46 +0100 | [diff] [blame] | 333 | target_level_dbfs_ = level; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 334 | return Configure(); |
| 335 | } |
| 336 | |
| 337 | int GainControlImpl::target_level_dbfs() const { |
| 338 | return target_level_dbfs_; |
| 339 | } |
| 340 | |
| 341 | int GainControlImpl::set_compression_gain_db(int gain) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 342 | if (gain < 0 || gain > 90) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 343 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 344 | } |
Sam Zackrisson | 421c859 | 2019-02-11 13:39:46 +0100 | [diff] [blame] | 345 | compression_gain_db_ = gain; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 346 | return Configure(); |
| 347 | } |
| 348 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 349 | int GainControlImpl::enable_limiter(bool enable) { |
Sam Zackrisson | 421c859 | 2019-02-11 13:39:46 +0100 | [diff] [blame] | 350 | limiter_enabled_ = enable; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 351 | return Configure(); |
| 352 | } |
| 353 | |
| 354 | bool GainControlImpl::is_limiter_enabled() const { |
| 355 | return limiter_enabled_; |
| 356 | } |
| 357 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 358 | void GainControlImpl::Initialize(size_t num_proc_channels, int sample_rate_hz) { |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 359 | data_dumper_->InitiateNewSetOfRecordings(); |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 360 | |
Oskar Sundbom | aa8b67d | 2017-11-17 14:34:48 +0100 | [diff] [blame] | 361 | num_proc_channels_ = num_proc_channels; |
| 362 | sample_rate_hz_ = sample_rate_hz; |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 363 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 364 | if (!enabled_) { |
| 365 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 366 | } |
| 367 | |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 368 | gain_controllers_.resize(*num_proc_channels_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 369 | for (auto& gain_controller : gain_controllers_) { |
| 370 | if (!gain_controller) { |
| 371 | gain_controller.reset(new GainController()); |
| 372 | } |
| 373 | gain_controller->Initialize(minimum_capture_level_, maximum_capture_level_, |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 374 | mode_, *sample_rate_hz_, analog_capture_level_); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | Configure(); |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 378 | } |
| 379 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 380 | int GainControlImpl::Configure() { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 381 | WebRtcAgcConfig config; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | // TODO(ajm): Flip the sign here (since AGC expects a positive value) if we |
| 383 | // change the interface. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 384 | // RTC_DCHECK_LE(target_level_dbfs_, 0); |
| 385 | // config.targetLevelDbfs = static_cast<int16_t>(-target_level_dbfs_); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 386 | config.targetLevelDbfs = static_cast<int16_t>(target_level_dbfs_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 387 | config.compressionGaindB = static_cast<int16_t>(compression_gain_db_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 388 | config.limiterEnable = limiter_enabled_; |
| 389 | |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 390 | int error = AudioProcessing::kNoError; |
| 391 | for (auto& gain_controller : gain_controllers_) { |
| 392 | const int handle_error = |
| 393 | WebRtcAgc_set_config(gain_controller->state(), config); |
| 394 | if (handle_error != AudioProcessing::kNoError) { |
| 395 | error = handle_error; |
| 396 | } |
| 397 | } |
| 398 | return error; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 400 | } // namespace webrtc |