ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 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 | |
| 11 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h" |
| 12 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 13 | #include <math.h> |
| 14 | #include <stdlib.h> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 15 | #include <algorithm> |
Alejandro Luebs | 66d2481 | 2016-02-11 10:37:12 -0800 | [diff] [blame] | 16 | #include <limits> |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 17 | #include <numeric> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 18 | |
| 19 | #include "webrtc/base/checks.h" |
Alejandro Luebs | 1aa8219 | 2016-07-01 17:16:01 -0700 | [diff] [blame] | 20 | #include "webrtc/base/logging.h" |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 21 | #include "webrtc/common_audio/include/audio_util.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 22 | #include "webrtc/common_audio/window_generator.h" |
| 23 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
| 26 | namespace { |
| 27 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 28 | const size_t kErbResolution = 2; |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 29 | const int kWindowSizeMs = 16; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 30 | const int kChunkSizeMs = 10; // Size provided by APM. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 31 | const float kClipFreqKhz = 0.2f; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 32 | const float kKbdAlpha = 1.5f; |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 33 | const float kLambdaBot = -1.f; // Extreme values in bisection |
Alejandro Luebs | dd56fa8 | 2016-03-29 13:05:40 -0700 | [diff] [blame] | 34 | const float kLambdaTop = -1e-5f; // search for lamda. |
aluebs | 7bd5f25 | 2016-06-21 11:30:25 -0700 | [diff] [blame] | 35 | const float kVoiceProbabilityThreshold = 0.5f; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 36 | // Number of chunks after voice activity which is still considered speech. |
aluebs | 7bd5f25 | 2016-06-21 11:30:25 -0700 | [diff] [blame] | 37 | const size_t kSpeechOffsetDelay = 10; |
| 38 | const float kDecayRate = 0.995f; // Power estimation decay rate. |
| 39 | const float kMaxRelativeGainChange = 0.005f; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 40 | const float kRho = 0.0004f; // Default production and interpretation SNR. |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 41 | const float kPowerNormalizationFactor = 1.f / (1 << 30); |
aluebs | 2fae89e | 2016-04-13 11:24:06 -0700 | [diff] [blame] | 42 | const float kMaxActiveSNR = 128.f; // 21dB |
| 43 | const float kMinInactiveSNR = 32.f; // 15dB |
aluebs | e8f8f60 | 2016-06-08 09:53:18 -0700 | [diff] [blame] | 44 | const size_t kGainUpdatePeriod = 10u; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 45 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 46 | // Returns dot product of vectors |a| and |b| with size |length|. |
| 47 | float DotProduct(const float* a, const float* b, size_t length) { |
| 48 | float ret = 0.f; |
| 49 | for (size_t i = 0; i < length; ++i) { |
aluebs | a2abdf2 | 2016-03-02 18:36:49 -0800 | [diff] [blame] | 50 | ret += a[i] * b[i]; |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 51 | } |
| 52 | return ret; |
| 53 | } |
| 54 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 55 | // Computes the power across ERB bands from the power spectral density |pow|. |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 56 | // Stores it in |result|. |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 57 | void MapToErbBands(const float* pow, |
| 58 | const std::vector<std::vector<float>>& filter_bank, |
| 59 | float* result) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 60 | for (size_t i = 0; i < filter_bank.size(); ++i) { |
| 61 | RTC_DCHECK_GT(filter_bank[i].size(), 0u); |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 62 | result[i] = kPowerNormalizationFactor * |
| 63 | DotProduct(filter_bank[i].data(), pow, filter_bank[i].size()); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 67 | } // namespace |
| 68 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 69 | IntelligibilityEnhancer::IntelligibilityEnhancer(int sample_rate_hz, |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 70 | size_t num_render_channels, |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 71 | size_t num_bands, |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 72 | size_t num_noise_bins) |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 73 | : freqs_(RealFourier::ComplexLength( |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 74 | RealFourier::FftOrder(sample_rate_hz * kWindowSizeMs / 1000))), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 75 | num_noise_bins_(num_noise_bins), |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 76 | chunk_length_(static_cast<size_t>(sample_rate_hz * kChunkSizeMs / 1000)), |
| 77 | bank_size_(GetBankSize(sample_rate_hz, kErbResolution)), |
| 78 | sample_rate_hz_(sample_rate_hz), |
| 79 | num_render_channels_(num_render_channels), |
| 80 | clear_power_estimator_(freqs_, kDecayRate), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 81 | noise_power_estimator_(num_noise_bins, kDecayRate), |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 82 | filtered_clear_pow_(bank_size_, 0.f), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 83 | filtered_noise_pow_(num_noise_bins, 0.f), |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 84 | center_freqs_(bank_size_), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 85 | capture_filter_bank_(CreateErbBank(num_noise_bins)), |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 86 | render_filter_bank_(CreateErbBank(freqs_)), |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 87 | gains_eq_(bank_size_), |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 88 | gain_applier_(freqs_, kMaxRelativeGainChange), |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 89 | audio_s16_(chunk_length_), |
| 90 | chunks_since_voice_(kSpeechOffsetDelay), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 91 | is_speech_(false), |
aluebs | 2fae89e | 2016-04-13 11:24:06 -0700 | [diff] [blame] | 92 | snr_(kMaxActiveSNR), |
| 93 | is_active_(false), |
aluebs | e8f8f60 | 2016-06-08 09:53:18 -0700 | [diff] [blame] | 94 | num_chunks_(0u), |
Alejandro Luebs | 1aa8219 | 2016-07-01 17:16:01 -0700 | [diff] [blame] | 95 | num_active_chunks_(0u), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 96 | noise_estimation_buffer_(num_noise_bins), |
| 97 | noise_estimation_queue_(kMaxNumNoiseEstimatesToBuffer, |
| 98 | std::vector<float>(num_noise_bins), |
| 99 | RenderQueueItemVerifier<float>(num_noise_bins)) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 100 | RTC_DCHECK_LE(kRho, 1.f); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 101 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 102 | const size_t erb_index = static_cast<size_t>( |
| 103 | ceilf(11.17f * logf((kClipFreqKhz + 0.312f) / (kClipFreqKhz + 14.6575f)) + |
| 104 | 43.f)); |
| 105 | start_freq_ = std::max(static_cast<size_t>(1), erb_index * kErbResolution); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 106 | |
brucedawson | d81dc49 | 2016-04-01 10:16:14 -0700 | [diff] [blame] | 107 | size_t window_size = static_cast<size_t>(1) << RealFourier::FftOrder(freqs_); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 108 | std::vector<float> kbd_window(window_size); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 109 | WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size, |
| 110 | kbd_window.data()); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 111 | render_mangler_.reset(new LappedTransform( |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 112 | num_render_channels_, num_render_channels_, chunk_length_, |
| 113 | kbd_window.data(), window_size, window_size / 2, this)); |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 114 | |
| 115 | const size_t initial_delay = render_mangler_->initial_delay(); |
| 116 | for (size_t i = 0u; i < num_bands - 1; ++i) { |
| 117 | high_bands_buffers_.push_back(std::unique_ptr<intelligibility::DelayBuffer>( |
| 118 | new intelligibility::DelayBuffer(initial_delay, num_render_channels_))); |
| 119 | } |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alejandro Luebs | 1aa8219 | 2016-07-01 17:16:01 -0700 | [diff] [blame] | 122 | IntelligibilityEnhancer::~IntelligibilityEnhancer() { |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 123 | // Don't rely on this log, since the destructor isn't called when the |
| 124 | // app/tab is killed. |
| 125 | if (num_chunks_ > 0) { |
| 126 | LOG(LS_INFO) << "Intelligibility Enhancer was active for " |
| 127 | << 100.f * static_cast<float>(num_active_chunks_) / num_chunks_ |
| 128 | << "% of the call."; |
| 129 | } else { |
| 130 | LOG(LS_INFO) << "Intelligibility Enhancer processed no chunk."; |
| 131 | } |
Alejandro Luebs | 1aa8219 | 2016-07-01 17:16:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 134 | void IntelligibilityEnhancer::SetCaptureNoiseEstimate( |
Alejandro Luebs | 5041110 | 2016-06-30 15:35:41 -0700 | [diff] [blame] | 135 | std::vector<float> noise, float gain) { |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 136 | RTC_DCHECK_EQ(noise.size(), num_noise_bins_); |
aluebs | 11d4a42 | 2016-04-28 14:58:32 -0700 | [diff] [blame] | 137 | for (auto& bin : noise) { |
| 138 | bin *= gain; |
| 139 | } |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 140 | // Disregarding return value since buffer overflow is acceptable, because it |
| 141 | // is not critical to get each noise estimate. |
| 142 | if (noise_estimation_queue_.Insert(&noise)) { |
| 143 | }; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 146 | void IntelligibilityEnhancer::ProcessRenderAudio(AudioBuffer* audio) { |
| 147 | RTC_DCHECK_EQ(num_render_channels_, audio->num_channels()); |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 148 | while (noise_estimation_queue_.Remove(&noise_estimation_buffer_)) { |
| 149 | noise_power_estimator_.Step(noise_estimation_buffer_.data()); |
| 150 | } |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 151 | float* const* low_band = audio->split_channels_f(kBand0To8kHz); |
| 152 | is_speech_ = IsSpeech(low_band[0]); |
| 153 | render_mangler_->ProcessChunk(low_band, low_band); |
| 154 | DelayHighBands(audio); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 155 | } |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 156 | |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 157 | void IntelligibilityEnhancer::ProcessAudioBlock( |
| 158 | const std::complex<float>* const* in_block, |
| 159 | size_t in_channels, |
| 160 | size_t frames, |
| 161 | size_t /* out_channels */, |
| 162 | std::complex<float>* const* out_block) { |
| 163 | RTC_DCHECK_EQ(freqs_, frames); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 164 | if (is_speech_) { |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 165 | clear_power_estimator_.Step(in_block[0]); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 166 | } |
aluebs | 2fae89e | 2016-04-13 11:24:06 -0700 | [diff] [blame] | 167 | SnrBasedEffectActivation(); |
Alejandro Luebs | 1aa8219 | 2016-07-01 17:16:01 -0700 | [diff] [blame] | 168 | ++num_chunks_; |
| 169 | if (is_active_) { |
| 170 | ++num_active_chunks_; |
| 171 | if (num_chunks_ % kGainUpdatePeriod == 0) { |
| 172 | MapToErbBands(clear_power_estimator_.power().data(), render_filter_bank_, |
| 173 | filtered_clear_pow_.data()); |
| 174 | MapToErbBands(noise_power_estimator_.power().data(), capture_filter_bank_, |
| 175 | filtered_noise_pow_.data()); |
| 176 | SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.data()); |
| 177 | const float power_target = std::accumulate( |
| 178 | filtered_clear_pow_.data(), |
| 179 | filtered_clear_pow_.data() + bank_size_, |
| 180 | 0.f); |
| 181 | const float power_top = |
| 182 | DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
| 183 | SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.data()); |
| 184 | const float power_bot = |
| 185 | DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
| 186 | if (power_target >= power_bot && power_target <= power_top) { |
| 187 | SolveForLambda(power_target); |
| 188 | UpdateErbGains(); |
| 189 | } // Else experiencing power underflow, so do nothing. |
| 190 | } |
aluebs | 2fae89e | 2016-04-13 11:24:06 -0700 | [diff] [blame] | 191 | } |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 192 | for (size_t i = 0; i < in_channels; ++i) { |
| 193 | gain_applier_.Apply(in_block[i], out_block[i]); |
| 194 | } |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 195 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 196 | |
aluebs | 2fae89e | 2016-04-13 11:24:06 -0700 | [diff] [blame] | 197 | void IntelligibilityEnhancer::SnrBasedEffectActivation() { |
| 198 | const float* clear_psd = clear_power_estimator_.power().data(); |
| 199 | const float* noise_psd = noise_power_estimator_.power().data(); |
| 200 | const float clear_power = |
| 201 | std::accumulate(clear_psd, clear_psd + freqs_, 0.f); |
| 202 | const float noise_power = |
| 203 | std::accumulate(noise_psd, noise_psd + freqs_, 0.f); |
| 204 | snr_ = kDecayRate * snr_ + (1.f - kDecayRate) * clear_power / |
| 205 | (noise_power + std::numeric_limits<float>::epsilon()); |
| 206 | if (is_active_) { |
| 207 | if (snr_ > kMaxActiveSNR) { |
Alejandro Luebs | 1aa8219 | 2016-07-01 17:16:01 -0700 | [diff] [blame] | 208 | LOG(LS_INFO) << "Intelligibility Enhancer was deactivated at chunk " |
| 209 | << num_chunks_; |
aluebs | 2fae89e | 2016-04-13 11:24:06 -0700 | [diff] [blame] | 210 | is_active_ = false; |
| 211 | // Set the target gains to unity. |
| 212 | float* gains = gain_applier_.target(); |
| 213 | for (size_t i = 0; i < freqs_; ++i) { |
| 214 | gains[i] = 1.f; |
| 215 | } |
| 216 | } |
| 217 | } else { |
Alejandro Luebs | 1aa8219 | 2016-07-01 17:16:01 -0700 | [diff] [blame] | 218 | if (snr_ < kMinInactiveSNR) { |
| 219 | LOG(LS_INFO) << "Intelligibility Enhancer was activated at chunk " |
| 220 | << num_chunks_; |
| 221 | is_active_ = true; |
| 222 | } |
aluebs | 2fae89e | 2016-04-13 11:24:06 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 226 | void IntelligibilityEnhancer::SolveForLambda(float power_target) { |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 227 | const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values |
| 228 | const int kMaxIters = 100; // for these, based on experiments. |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 229 | |
Alejandro Luebs | 66d2481 | 2016-02-11 10:37:12 -0800 | [diff] [blame] | 230 | const float reciprocal_power_target = |
| 231 | 1.f / (power_target + std::numeric_limits<float>::epsilon()); |
Alejandro Luebs | dd56fa8 | 2016-03-29 13:05:40 -0700 | [diff] [blame] | 232 | float lambda_bot = kLambdaBot; |
| 233 | float lambda_top = kLambdaTop; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 234 | float power_ratio = 2.f; // Ratio of achieved power to target power. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 235 | int iters = 0; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 236 | while (std::fabs(power_ratio - 1.f) > kConvergeThresh && iters <= kMaxIters) { |
Alejandro Luebs | dd56fa8 | 2016-03-29 13:05:40 -0700 | [diff] [blame] | 237 | const float lambda = (lambda_bot + lambda_top) / 2.f; |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 238 | SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.data()); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 239 | const float power = |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 240 | DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 241 | if (power < power_target) { |
| 242 | lambda_bot = lambda; |
| 243 | } else { |
| 244 | lambda_top = lambda; |
| 245 | } |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 246 | power_ratio = std::fabs(power * reciprocal_power_target); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 247 | ++iters; |
| 248 | } |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 249 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 250 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 251 | void IntelligibilityEnhancer::UpdateErbGains() { |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 252 | // (ERB gain) = filterbank' * (freq gain) |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 253 | float* gains = gain_applier_.target(); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 254 | for (size_t i = 0; i < freqs_; ++i) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 255 | gains[i] = 0.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 256 | for (size_t j = 0; j < bank_size_; ++j) { |
aluebs | a2abdf2 | 2016-03-02 18:36:49 -0800 | [diff] [blame] | 257 | gains[i] += render_filter_bank_[j][i] * gains_eq_[j]; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 262 | size_t IntelligibilityEnhancer::GetBankSize(int sample_rate, |
| 263 | size_t erb_resolution) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 264 | float freq_limit = sample_rate / 2000.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 265 | size_t erb_scale = static_cast<size_t>(ceilf( |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 266 | 11.17f * logf((freq_limit + 0.312f) / (freq_limit + 14.6575f)) + 43.f)); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 267 | return erb_scale * erb_resolution; |
| 268 | } |
| 269 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 270 | std::vector<std::vector<float>> IntelligibilityEnhancer::CreateErbBank( |
| 271 | size_t num_freqs) { |
| 272 | std::vector<std::vector<float>> filter_bank(bank_size_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 273 | size_t lf = 1, rf = 4; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 274 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 275 | for (size_t i = 0; i < bank_size_; ++i) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 276 | float abs_temp = fabsf((i + 1.f) / static_cast<float>(kErbResolution)); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 277 | center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp)); |
| 278 | center_freqs_[i] -= 14678.49f; |
| 279 | } |
| 280 | float last_center_freq = center_freqs_[bank_size_ - 1]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 281 | for (size_t i = 0; i < bank_size_; ++i) { |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 282 | center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq; |
| 283 | } |
| 284 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 285 | for (size_t i = 0; i < bank_size_; ++i) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 286 | filter_bank[i].resize(num_freqs); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 289 | for (size_t i = 1; i <= bank_size_; ++i) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 290 | static const size_t kOne = 1; // Avoids repeated static_cast<>s below. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 291 | size_t lll = |
| 292 | static_cast<size_t>(round(center_freqs_[std::max(kOne, i - lf) - 1] * |
| 293 | num_freqs / (0.5f * sample_rate_hz_))); |
| 294 | size_t ll = static_cast<size_t>(round(center_freqs_[std::max(kOne, i) - 1] * |
| 295 | num_freqs / (0.5f * sample_rate_hz_))); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 296 | lll = std::min(num_freqs, std::max(lll, kOne)) - 1; |
| 297 | ll = std::min(num_freqs, std::max(ll, kOne)) - 1; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 298 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 299 | size_t rrr = static_cast<size_t>( |
| 300 | round(center_freqs_[std::min(bank_size_, i + rf) - 1] * num_freqs / |
| 301 | (0.5f * sample_rate_hz_))); |
| 302 | size_t rr = static_cast<size_t>( |
| 303 | round(center_freqs_[std::min(bank_size_, i + 1) - 1] * num_freqs / |
| 304 | (0.5f * sample_rate_hz_))); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 305 | rrr = std::min(num_freqs, std::max(rrr, kOne)) - 1; |
| 306 | rr = std::min(num_freqs, std::max(rr, kOne)) - 1; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 307 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 308 | float step = ll == lll ? 0.f : 1.f / (ll - lll); |
| 309 | float element = 0.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 310 | for (size_t j = lll; j <= ll; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 311 | filter_bank[i - 1][j] = element; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 312 | element += step; |
| 313 | } |
Alejandro Luebs | 66d2481 | 2016-02-11 10:37:12 -0800 | [diff] [blame] | 314 | step = rr == rrr ? 0.f : 1.f / (rrr - rr); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 315 | element = 1.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 316 | for (size_t j = rr; j <= rrr; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 317 | filter_bank[i - 1][j] = element; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 318 | element -= step; |
| 319 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 320 | for (size_t j = ll; j <= rr; ++j) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 321 | filter_bank[i - 1][j] = 1.f; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 325 | for (size_t i = 0; i < num_freqs; ++i) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 326 | float sum = 0.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 327 | for (size_t j = 0; j < bank_size_; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 328 | sum += filter_bank[j][i]; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 329 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 330 | for (size_t j = 0; j < bank_size_; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 331 | filter_bank[j][i] /= sum; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 334 | return filter_bank; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Alejandro Luebs | dd56fa8 | 2016-03-29 13:05:40 -0700 | [diff] [blame] | 337 | void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 338 | size_t start_freq, |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 339 | float* sols) { |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 340 | const float kMinPower = 1e-5f; |
| 341 | |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 342 | const float* pow_x0 = filtered_clear_pow_.data(); |
| 343 | const float* pow_n0 = filtered_noise_pow_.data(); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 344 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 345 | for (size_t n = 0; n < start_freq; ++n) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 346 | sols[n] = 1.f; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 347 | } |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 348 | |
| 349 | // Analytic solution for optimal gains. See paper for derivation. |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 350 | for (size_t n = start_freq; n < bank_size_; ++n) { |
| 351 | if (pow_x0[n] < kMinPower || pow_n0[n] < kMinPower) { |
| 352 | sols[n] = 1.f; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 353 | } else { |
Alejandro Luebs | dd56fa8 | 2016-03-29 13:05:40 -0700 | [diff] [blame] | 354 | const float gamma0 = 0.5f * kRho * pow_x0[n] * pow_n0[n] + |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 355 | lambda * pow_x0[n] * pow_n0[n] * pow_n0[n]; |
Alejandro Luebs | dd56fa8 | 2016-03-29 13:05:40 -0700 | [diff] [blame] | 356 | const float beta0 = |
| 357 | lambda * pow_x0[n] * (2.f - kRho) * pow_x0[n] * pow_n0[n]; |
| 358 | const float alpha0 = |
| 359 | lambda * pow_x0[n] * (1.f - kRho) * pow_x0[n] * pow_x0[n]; |
| 360 | RTC_DCHECK_LT(alpha0, 0.f); |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 361 | // The quadratic equation should always have real roots, but to guard |
| 362 | // against numerical errors we limit it to a minimum of zero. |
| 363 | sols[n] = std::max( |
Alejandro Luebs | dd56fa8 | 2016-03-29 13:05:40 -0700 | [diff] [blame] | 364 | 0.f, (-beta0 - std::sqrt(std::max( |
| 365 | 0.f, beta0 * beta0 - 4.f * alpha0 * gamma0))) / |
| 366 | (2.f * alpha0)); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 367 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 371 | bool IntelligibilityEnhancer::IsSpeech(const float* audio) { |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 372 | FloatToS16(audio, chunk_length_, audio_s16_.data()); |
| 373 | vad_.ProcessChunk(audio_s16_.data(), chunk_length_, sample_rate_hz_); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 374 | if (vad_.last_voice_probability() > kVoiceProbabilityThreshold) { |
| 375 | chunks_since_voice_ = 0; |
| 376 | } else if (chunks_since_voice_ < kSpeechOffsetDelay) { |
| 377 | ++chunks_since_voice_; |
| 378 | } |
| 379 | return chunks_since_voice_ < kSpeechOffsetDelay; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 382 | void IntelligibilityEnhancer::DelayHighBands(AudioBuffer* audio) { |
| 383 | RTC_DCHECK_EQ(audio->num_bands(), high_bands_buffers_.size() + 1u); |
| 384 | for (size_t i = 0u; i < high_bands_buffers_.size(); ++i) { |
| 385 | Band band = static_cast<Band>(i + 1); |
| 386 | high_bands_buffers_[i]->Delay(audio->split_channels_f(band), chunk_length_); |
| 387 | } |
| 388 | } |
| 389 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 390 | } // namespace webrtc |