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