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 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
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" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
| 21 | typedef void Handle; |
| 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 24 | int16_t MapSetting(GainControl::Mode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | switch (mode) { |
| 26 | case GainControl::kAdaptiveAnalog: |
| 27 | return kAgcModeAdaptiveAnalog; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | case GainControl::kAdaptiveDigital: |
| 29 | return kAgcModeAdaptiveDigital; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | case GainControl::kFixedDigital: |
| 31 | return kAgcModeFixedDigital; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 33 | assert(false); |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 34 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | } // namespace |
| 37 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 38 | GainControlImpl::GainControlImpl(const AudioProcessing* apm, |
| 39 | CriticalSectionWrapper* crit) |
| 40 | : ProcessingComponent(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | apm_(apm), |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 42 | crit_(crit), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | mode_(kAdaptiveAnalog), |
| 44 | minimum_capture_level_(0), |
| 45 | maximum_capture_level_(255), |
| 46 | limiter_enabled_(true), |
| 47 | target_level_dbfs_(3), |
| 48 | compression_gain_db_(9), |
| 49 | analog_capture_level_(0), |
| 50 | was_analog_level_set_(false), |
| 51 | stream_is_saturated_(false) {} |
| 52 | |
| 53 | GainControlImpl::~GainControlImpl() {} |
| 54 | |
| 55 | int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { |
| 56 | if (!is_component_enabled()) { |
| 57 | return apm_->kNoError; |
| 58 | } |
| 59 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 60 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | for (int i = 0; i < num_handles(); i++) { |
| 63 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 64 | int err = WebRtcAgc_AddFarend( |
| 65 | my_handle, |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 66 | audio->mixed_low_pass_data(), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 67 | audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
| 69 | if (err != apm_->kNoError) { |
| 70 | return GetHandleError(my_handle); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return apm_->kNoError; |
| 75 | } |
| 76 | |
| 77 | int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
| 78 | if (!is_component_enabled()) { |
| 79 | return apm_->kNoError; |
| 80 | } |
| 81 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 82 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | assert(audio->num_channels() == num_handles()); |
| 84 | |
| 85 | int err = apm_->kNoError; |
| 86 | |
| 87 | if (mode_ == kAdaptiveAnalog) { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 88 | capture_levels_.assign(num_handles(), analog_capture_level_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | for (int i = 0; i < num_handles(); i++) { |
| 90 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 91 | err = WebRtcAgc_AddMic( |
| 92 | my_handle, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 93 | audio->split_bands(i), |
| 94 | audio->num_bands(), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 95 | audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
| 97 | if (err != apm_->kNoError) { |
| 98 | return GetHandleError(my_handle); |
| 99 | } |
| 100 | } |
| 101 | } else if (mode_ == kAdaptiveDigital) { |
| 102 | |
| 103 | for (int i = 0; i < num_handles(); i++) { |
| 104 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 105 | int32_t capture_level_out = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | |
| 107 | err = WebRtcAgc_VirtualMic( |
| 108 | my_handle, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 109 | audio->split_bands(i), |
| 110 | audio->num_bands(), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 111 | audio->num_frames_per_band(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | analog_capture_level_, |
| 113 | &capture_level_out); |
| 114 | |
| 115 | capture_levels_[i] = capture_level_out; |
| 116 | |
| 117 | if (err != apm_->kNoError) { |
| 118 | return GetHandleError(my_handle); |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return apm_->kNoError; |
| 125 | } |
| 126 | |
| 127 | int GainControlImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
| 128 | if (!is_component_enabled()) { |
| 129 | return apm_->kNoError; |
| 130 | } |
| 131 | |
| 132 | if (mode_ == kAdaptiveAnalog && !was_analog_level_set_) { |
| 133 | return apm_->kStreamParameterNotSetError; |
| 134 | } |
| 135 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 136 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | assert(audio->num_channels() == num_handles()); |
| 138 | |
| 139 | stream_is_saturated_ = false; |
| 140 | for (int i = 0; i < num_handles(); i++) { |
| 141 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 142 | int32_t capture_level_out = 0; |
| 143 | uint8_t saturation_warning = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
| 145 | int err = WebRtcAgc_Process( |
| 146 | my_handle, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 147 | audio->split_bands_const(i), |
| 148 | audio->num_bands(), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 149 | audio->num_frames_per_band(), |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 150 | audio->split_bands(i), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | capture_levels_[i], |
| 152 | &capture_level_out, |
| 153 | apm_->echo_cancellation()->stream_has_echo(), |
| 154 | &saturation_warning); |
| 155 | |
| 156 | if (err != apm_->kNoError) { |
| 157 | return GetHandleError(my_handle); |
| 158 | } |
| 159 | |
| 160 | capture_levels_[i] = capture_level_out; |
| 161 | if (saturation_warning == 1) { |
| 162 | stream_is_saturated_ = true; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (mode_ == kAdaptiveAnalog) { |
| 167 | // Take the analog level to be the average across the handles. |
| 168 | analog_capture_level_ = 0; |
| 169 | for (int i = 0; i < num_handles(); i++) { |
| 170 | analog_capture_level_ += capture_levels_[i]; |
| 171 | } |
| 172 | |
| 173 | analog_capture_level_ /= num_handles(); |
| 174 | } |
| 175 | |
| 176 | was_analog_level_set_ = false; |
| 177 | return apm_->kNoError; |
| 178 | } |
| 179 | |
| 180 | // TODO(ajm): ensure this is called under kAdaptiveAnalog. |
| 181 | int GainControlImpl::set_stream_analog_level(int level) { |
Brave Yao | 1a07a1e | 2015-05-21 12:42:40 +0800 | [diff] [blame] | 182 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | was_analog_level_set_ = true; |
| 184 | if (level < minimum_capture_level_ || level > maximum_capture_level_) { |
| 185 | return apm_->kBadParameterError; |
| 186 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | analog_capture_level_ = level; |
| 188 | |
| 189 | return apm_->kNoError; |
| 190 | } |
| 191 | |
| 192 | int GainControlImpl::stream_analog_level() { |
| 193 | // TODO(ajm): enable this assertion? |
| 194 | //assert(mode_ == kAdaptiveAnalog); |
| 195 | |
| 196 | return analog_capture_level_; |
| 197 | } |
| 198 | |
| 199 | int GainControlImpl::Enable(bool enable) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 200 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 201 | return EnableComponent(enable); |
| 202 | } |
| 203 | |
| 204 | bool GainControlImpl::is_enabled() const { |
| 205 | return is_component_enabled(); |
| 206 | } |
| 207 | |
| 208 | int GainControlImpl::set_mode(Mode mode) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 209 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | if (MapSetting(mode) == -1) { |
| 211 | return apm_->kBadParameterError; |
| 212 | } |
| 213 | |
| 214 | mode_ = mode; |
| 215 | return Initialize(); |
| 216 | } |
| 217 | |
| 218 | GainControl::Mode GainControlImpl::mode() const { |
| 219 | return mode_; |
| 220 | } |
| 221 | |
| 222 | int GainControlImpl::set_analog_level_limits(int minimum, |
| 223 | int maximum) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 224 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | if (minimum < 0) { |
| 226 | return apm_->kBadParameterError; |
| 227 | } |
| 228 | |
| 229 | if (maximum > 65535) { |
| 230 | return apm_->kBadParameterError; |
| 231 | } |
| 232 | |
| 233 | if (maximum < minimum) { |
| 234 | return apm_->kBadParameterError; |
| 235 | } |
| 236 | |
| 237 | minimum_capture_level_ = minimum; |
| 238 | maximum_capture_level_ = maximum; |
| 239 | |
| 240 | return Initialize(); |
| 241 | } |
| 242 | |
| 243 | int GainControlImpl::analog_level_minimum() const { |
| 244 | return minimum_capture_level_; |
| 245 | } |
| 246 | |
| 247 | int GainControlImpl::analog_level_maximum() const { |
| 248 | return maximum_capture_level_; |
| 249 | } |
| 250 | |
| 251 | bool GainControlImpl::stream_is_saturated() const { |
| 252 | return stream_is_saturated_; |
| 253 | } |
| 254 | |
| 255 | int GainControlImpl::set_target_level_dbfs(int level) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 256 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 257 | if (level > 31 || level < 0) { |
| 258 | return apm_->kBadParameterError; |
| 259 | } |
| 260 | |
| 261 | target_level_dbfs_ = level; |
| 262 | return Configure(); |
| 263 | } |
| 264 | |
| 265 | int GainControlImpl::target_level_dbfs() const { |
| 266 | return target_level_dbfs_; |
| 267 | } |
| 268 | |
| 269 | int GainControlImpl::set_compression_gain_db(int gain) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 270 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 271 | if (gain < 0 || gain > 90) { |
| 272 | return apm_->kBadParameterError; |
| 273 | } |
| 274 | |
| 275 | compression_gain_db_ = gain; |
| 276 | return Configure(); |
| 277 | } |
| 278 | |
| 279 | int GainControlImpl::compression_gain_db() const { |
| 280 | return compression_gain_db_; |
| 281 | } |
| 282 | |
| 283 | int GainControlImpl::enable_limiter(bool enable) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 284 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 285 | limiter_enabled_ = enable; |
| 286 | return Configure(); |
| 287 | } |
| 288 | |
| 289 | bool GainControlImpl::is_limiter_enabled() const { |
| 290 | return limiter_enabled_; |
| 291 | } |
| 292 | |
| 293 | int GainControlImpl::Initialize() { |
| 294 | int err = ProcessingComponent::Initialize(); |
| 295 | if (err != apm_->kNoError || !is_component_enabled()) { |
| 296 | return err; |
| 297 | } |
| 298 | |
andrew@webrtc.org | b6541ca | 2014-01-07 18:36:10 +0000 | [diff] [blame] | 299 | capture_levels_.assign(num_handles(), analog_capture_level_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 300 | return apm_->kNoError; |
| 301 | } |
| 302 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 303 | void* GainControlImpl::CreateHandle() const { |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 304 | return WebRtcAgc_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 305 | } |
| 306 | |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 307 | void GainControlImpl::DestroyHandle(void* handle) const { |
| 308 | WebRtcAgc_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | int GainControlImpl::InitializeHandle(void* handle) const { |
| 312 | return WebRtcAgc_Init(static_cast<Handle*>(handle), |
| 313 | minimum_capture_level_, |
| 314 | maximum_capture_level_, |
| 315 | MapSetting(mode_), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 316 | apm_->proc_sample_rate_hz()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | int GainControlImpl::ConfigureHandle(void* handle) const { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 320 | WebRtcAgcConfig config; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 321 | // TODO(ajm): Flip the sign here (since AGC expects a positive value) if we |
| 322 | // change the interface. |
| 323 | //assert(target_level_dbfs_ <= 0); |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 324 | //config.targetLevelDbfs = static_cast<int16_t>(-target_level_dbfs_); |
| 325 | config.targetLevelDbfs = static_cast<int16_t>(target_level_dbfs_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 326 | config.compressionGaindB = |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 327 | static_cast<int16_t>(compression_gain_db_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 328 | config.limiterEnable = limiter_enabled_; |
| 329 | |
| 330 | return WebRtcAgc_set_config(static_cast<Handle*>(handle), config); |
| 331 | } |
| 332 | |
| 333 | int GainControlImpl::num_handles_required() const { |
| 334 | return apm_->num_output_channels(); |
| 335 | } |
| 336 | |
| 337 | int GainControlImpl::GetHandleError(void* handle) const { |
| 338 | // The AGC has no get_error() function. |
| 339 | // (Despite listing errors in its interface...) |
| 340 | assert(handle != NULL); |
| 341 | return apm_->kUnspecifiedError; |
| 342 | } |
| 343 | } // namespace webrtc |