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