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