OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" | 11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" |
12 | 12 |
13 #include <math.h> | 13 #include <math.h> |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
15 #include <algorithm> | 15 #include <algorithm> |
16 #include <limits> | 16 #include <limits> |
17 #include <numeric> | 17 #include <numeric> |
18 | 18 |
19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/logging.h" |
20 #include "webrtc/common_audio/include/audio_util.h" | 21 #include "webrtc/common_audio/include/audio_util.h" |
21 #include "webrtc/common_audio/window_generator.h" | 22 #include "webrtc/common_audio/window_generator.h" |
22 | 23 |
23 namespace webrtc { | 24 namespace webrtc { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 const size_t kErbResolution = 2; | 28 const size_t kErbResolution = 2; |
28 const int kWindowSizeMs = 16; | 29 const int kWindowSizeMs = 16; |
29 const int kChunkSizeMs = 10; // Size provided by APM. | 30 const int kChunkSizeMs = 10; // Size provided by APM. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 capture_filter_bank_(CreateErbBank(num_noise_bins)), | 84 capture_filter_bank_(CreateErbBank(num_noise_bins)), |
84 render_filter_bank_(CreateErbBank(freqs_)), | 85 render_filter_bank_(CreateErbBank(freqs_)), |
85 gains_eq_(bank_size_), | 86 gains_eq_(bank_size_), |
86 gain_applier_(freqs_, kMaxRelativeGainChange), | 87 gain_applier_(freqs_, kMaxRelativeGainChange), |
87 audio_s16_(chunk_length_), | 88 audio_s16_(chunk_length_), |
88 chunks_since_voice_(kSpeechOffsetDelay), | 89 chunks_since_voice_(kSpeechOffsetDelay), |
89 is_speech_(false), | 90 is_speech_(false), |
90 snr_(kMaxActiveSNR), | 91 snr_(kMaxActiveSNR), |
91 is_active_(false), | 92 is_active_(false), |
92 num_chunks_(0u), | 93 num_chunks_(0u), |
| 94 num_active_chunks_(0u), |
93 noise_estimation_buffer_(num_noise_bins), | 95 noise_estimation_buffer_(num_noise_bins), |
94 noise_estimation_queue_(kMaxNumNoiseEstimatesToBuffer, | 96 noise_estimation_queue_(kMaxNumNoiseEstimatesToBuffer, |
95 std::vector<float>(num_noise_bins), | 97 std::vector<float>(num_noise_bins), |
96 RenderQueueItemVerifier<float>(num_noise_bins)) { | 98 RenderQueueItemVerifier<float>(num_noise_bins)) { |
97 RTC_DCHECK_LE(kRho, 1.f); | 99 RTC_DCHECK_LE(kRho, 1.f); |
98 | 100 |
99 const size_t erb_index = static_cast<size_t>( | 101 const size_t erb_index = static_cast<size_t>( |
100 ceilf(11.17f * logf((kClipFreqKhz + 0.312f) / (kClipFreqKhz + 14.6575f)) + | 102 ceilf(11.17f * logf((kClipFreqKhz + 0.312f) / (kClipFreqKhz + 14.6575f)) + |
101 43.f)); | 103 43.f)); |
102 start_freq_ = std::max(static_cast<size_t>(1), erb_index * kErbResolution); | 104 start_freq_ = std::max(static_cast<size_t>(1), erb_index * kErbResolution); |
103 | 105 |
104 size_t window_size = static_cast<size_t>(1) << RealFourier::FftOrder(freqs_); | 106 size_t window_size = static_cast<size_t>(1) << RealFourier::FftOrder(freqs_); |
105 std::vector<float> kbd_window(window_size); | 107 std::vector<float> kbd_window(window_size); |
106 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size, | 108 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size, |
107 kbd_window.data()); | 109 kbd_window.data()); |
108 render_mangler_.reset(new LappedTransform( | 110 render_mangler_.reset(new LappedTransform( |
109 num_render_channels_, num_render_channels_, chunk_length_, | 111 num_render_channels_, num_render_channels_, chunk_length_, |
110 kbd_window.data(), window_size, window_size / 2, this)); | 112 kbd_window.data(), window_size, window_size / 2, this)); |
111 } | 113 } |
112 | 114 |
| 115 IntelligibilityEnhancer::~IntelligibilityEnhancer() { |
| 116 // Don't rely on this log, since the destructor isn't called when the app/tab |
| 117 // is killed. |
| 118 LOG(LS_INFO) << "Intelligibility Enhancer was active for " |
| 119 << static_cast<float>(num_active_chunks_) / num_chunks_ |
| 120 << "% of the call."; |
| 121 } |
| 122 |
113 void IntelligibilityEnhancer::SetCaptureNoiseEstimate( | 123 void IntelligibilityEnhancer::SetCaptureNoiseEstimate( |
114 std::vector<float> noise, float gain) { | 124 std::vector<float> noise, float gain) { |
115 RTC_DCHECK_EQ(noise.size(), num_noise_bins_); | 125 RTC_DCHECK_EQ(noise.size(), num_noise_bins_); |
116 for (auto& bin : noise) { | 126 for (auto& bin : noise) { |
117 bin *= gain; | 127 bin *= gain; |
118 } | 128 } |
119 // Disregarding return value since buffer overflow is acceptable, because it | 129 // Disregarding return value since buffer overflow is acceptable, because it |
120 // is not critical to get each noise estimate. | 130 // is not critical to get each noise estimate. |
121 if (noise_estimation_queue_.Insert(&noise)) { | 131 if (noise_estimation_queue_.Insert(&noise)) { |
122 }; | 132 }; |
(...skipping 15 matching lines...) Expand all Loading... |
138 const std::complex<float>* const* in_block, | 148 const std::complex<float>* const* in_block, |
139 size_t in_channels, | 149 size_t in_channels, |
140 size_t frames, | 150 size_t frames, |
141 size_t /* out_channels */, | 151 size_t /* out_channels */, |
142 std::complex<float>* const* out_block) { | 152 std::complex<float>* const* out_block) { |
143 RTC_DCHECK_EQ(freqs_, frames); | 153 RTC_DCHECK_EQ(freqs_, frames); |
144 if (is_speech_) { | 154 if (is_speech_) { |
145 clear_power_estimator_.Step(in_block[0]); | 155 clear_power_estimator_.Step(in_block[0]); |
146 } | 156 } |
147 SnrBasedEffectActivation(); | 157 SnrBasedEffectActivation(); |
148 if (is_active_ && num_chunks_++ % kGainUpdatePeriod == 0) { | 158 ++num_chunks_; |
149 MapToErbBands(clear_power_estimator_.power().data(), render_filter_bank_, | 159 if (is_active_) { |
150 filtered_clear_pow_.data()); | 160 ++num_active_chunks_; |
151 MapToErbBands(noise_power_estimator_.power().data(), capture_filter_bank_, | 161 if (num_chunks_ % kGainUpdatePeriod == 0) { |
152 filtered_noise_pow_.data()); | 162 MapToErbBands(clear_power_estimator_.power().data(), render_filter_bank_, |
153 SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.data()); | 163 filtered_clear_pow_.data()); |
154 const float power_target = std::accumulate( | 164 MapToErbBands(noise_power_estimator_.power().data(), capture_filter_bank_, |
155 filtered_clear_pow_.data(), | 165 filtered_noise_pow_.data()); |
156 filtered_clear_pow_.data() + bank_size_, | 166 SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.data()); |
157 0.f); | 167 const float power_target = std::accumulate( |
158 const float power_top = | 168 filtered_clear_pow_.data(), |
159 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); | 169 filtered_clear_pow_.data() + bank_size_, |
160 SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.data()); | 170 0.f); |
161 const float power_bot = | 171 const float power_top = |
162 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); | 172 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
163 if (power_target >= power_bot && power_target <= power_top) { | 173 SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.data()); |
164 SolveForLambda(power_target); | 174 const float power_bot = |
165 UpdateErbGains(); | 175 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
166 } // Else experiencing power underflow, so do nothing. | 176 if (power_target >= power_bot && power_target <= power_top) { |
| 177 SolveForLambda(power_target); |
| 178 UpdateErbGains(); |
| 179 } // Else experiencing power underflow, so do nothing. |
| 180 } |
167 } | 181 } |
168 for (size_t i = 0; i < in_channels; ++i) { | 182 for (size_t i = 0; i < in_channels; ++i) { |
169 gain_applier_.Apply(in_block[i], out_block[i]); | 183 gain_applier_.Apply(in_block[i], out_block[i]); |
170 } | 184 } |
171 } | 185 } |
172 | 186 |
173 void IntelligibilityEnhancer::SnrBasedEffectActivation() { | 187 void IntelligibilityEnhancer::SnrBasedEffectActivation() { |
174 const float* clear_psd = clear_power_estimator_.power().data(); | 188 const float* clear_psd = clear_power_estimator_.power().data(); |
175 const float* noise_psd = noise_power_estimator_.power().data(); | 189 const float* noise_psd = noise_power_estimator_.power().data(); |
176 const float clear_power = | 190 const float clear_power = |
177 std::accumulate(clear_psd, clear_psd + freqs_, 0.f); | 191 std::accumulate(clear_psd, clear_psd + freqs_, 0.f); |
178 const float noise_power = | 192 const float noise_power = |
179 std::accumulate(noise_psd, noise_psd + freqs_, 0.f); | 193 std::accumulate(noise_psd, noise_psd + freqs_, 0.f); |
180 snr_ = kDecayRate * snr_ + (1.f - kDecayRate) * clear_power / | 194 snr_ = kDecayRate * snr_ + (1.f - kDecayRate) * clear_power / |
181 (noise_power + std::numeric_limits<float>::epsilon()); | 195 (noise_power + std::numeric_limits<float>::epsilon()); |
182 if (is_active_) { | 196 if (is_active_) { |
183 if (snr_ > kMaxActiveSNR) { | 197 if (snr_ > kMaxActiveSNR) { |
| 198 LOG(LS_INFO) << "Intelligibility Enhancer was deactivated at chunk " |
| 199 << num_chunks_; |
184 is_active_ = false; | 200 is_active_ = false; |
185 // Set the target gains to unity. | 201 // Set the target gains to unity. |
186 float* gains = gain_applier_.target(); | 202 float* gains = gain_applier_.target(); |
187 for (size_t i = 0; i < freqs_; ++i) { | 203 for (size_t i = 0; i < freqs_; ++i) { |
188 gains[i] = 1.f; | 204 gains[i] = 1.f; |
189 } | 205 } |
190 } | 206 } |
191 } else { | 207 } else { |
192 is_active_ = snr_ < kMinInactiveSNR; | 208 if (snr_ < kMinInactiveSNR) { |
| 209 LOG(LS_INFO) << "Intelligibility Enhancer was activated at chunk " |
| 210 << num_chunks_; |
| 211 is_active_ = true; |
| 212 } |
193 } | 213 } |
194 } | 214 } |
195 | 215 |
196 void IntelligibilityEnhancer::SolveForLambda(float power_target) { | 216 void IntelligibilityEnhancer::SolveForLambda(float power_target) { |
197 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values | 217 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values |
198 const int kMaxIters = 100; // for these, based on experiments. | 218 const int kMaxIters = 100; // for these, based on experiments. |
199 | 219 |
200 const float reciprocal_power_target = | 220 const float reciprocal_power_target = |
201 1.f / (power_target + std::numeric_limits<float>::epsilon()); | 221 1.f / (power_target + std::numeric_limits<float>::epsilon()); |
202 float lambda_bot = kLambdaBot; | 222 float lambda_bot = kLambdaBot; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 vad_.ProcessChunk(audio_s16_.data(), chunk_length_, sample_rate_hz_); | 363 vad_.ProcessChunk(audio_s16_.data(), chunk_length_, sample_rate_hz_); |
344 if (vad_.last_voice_probability() > kVoiceProbabilityThreshold) { | 364 if (vad_.last_voice_probability() > kVoiceProbabilityThreshold) { |
345 chunks_since_voice_ = 0; | 365 chunks_since_voice_ = 0; |
346 } else if (chunks_since_voice_ < kSpeechOffsetDelay) { | 366 } else if (chunks_since_voice_ < kSpeechOffsetDelay) { |
347 ++chunks_since_voice_; | 367 ++chunks_since_voice_; |
348 } | 368 } |
349 return chunks_since_voice_ < kSpeechOffsetDelay; | 369 return chunks_since_voice_ < kSpeechOffsetDelay; |
350 } | 370 } |
351 | 371 |
352 } // namespace webrtc | 372 } // namespace webrtc |
OLD | NEW |