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" |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 20 | #include "webrtc/common_audio/include/audio_util.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 21 | #include "webrtc/common_audio/window_generator.h" |
| 22 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | |
| 25 | namespace { |
| 26 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 27 | const size_t kErbResolution = 2; |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 28 | const int kWindowSizeMs = 16; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 29 | const int kChunkSizeMs = 10; // Size provided by APM. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 30 | const float kClipFreqKhz = 0.2f; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 31 | const float kKbdAlpha = 1.5f; |
| 32 | const float kLambdaBot = -1.0f; // Extreme values in bisection |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 33 | const float kLambdaTop = -1e-5f; // search for lamda. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 34 | const float kVoiceProbabilityThreshold = 0.02f; |
| 35 | // Number of chunks after voice activity which is still considered speech. |
| 36 | const size_t kSpeechOffsetDelay = 80; |
| 37 | const float kDecayRate = 0.98f; // Power estimation decay rate. |
| 38 | const float kMaxRelativeGainChange = 0.04f; // Maximum relative change in gain. |
| 39 | const float kRho = 0.0004f; // Default production and interpretation SNR. |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 40 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 41 | // Returns dot product of vectors |a| and |b| with size |length|. |
| 42 | float DotProduct(const float* a, const float* b, size_t length) { |
| 43 | float ret = 0.f; |
| 44 | for (size_t i = 0; i < length; ++i) { |
aluebs | a2abdf2 | 2016-03-02 18:36:49 -0800 | [diff] [blame] | 45 | ret += a[i] * b[i]; |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 46 | } |
| 47 | return ret; |
| 48 | } |
| 49 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 50 | // Computes the power across ERB bands from the power spectral density |pow|. |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 51 | // Stores it in |result|. |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 52 | void MapToErbBands(const float* pow, |
| 53 | const std::vector<std::vector<float>>& filter_bank, |
| 54 | float* result) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 55 | for (size_t i = 0; i < filter_bank.size(); ++i) { |
| 56 | RTC_DCHECK_GT(filter_bank[i].size(), 0u); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 57 | result[i] = DotProduct(filter_bank[i].data(), pow, filter_bank[i].size()); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 61 | } // namespace |
| 62 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 63 | IntelligibilityEnhancer::IntelligibilityEnhancer(int sample_rate_hz, |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 64 | size_t num_render_channels, |
| 65 | size_t num_noise_bins) |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 66 | : freqs_(RealFourier::ComplexLength( |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 67 | RealFourier::FftOrder(sample_rate_hz * kWindowSizeMs / 1000))), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 68 | num_noise_bins_(num_noise_bins), |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 69 | chunk_length_(static_cast<size_t>(sample_rate_hz * kChunkSizeMs / 1000)), |
| 70 | bank_size_(GetBankSize(sample_rate_hz, kErbResolution)), |
| 71 | sample_rate_hz_(sample_rate_hz), |
| 72 | num_render_channels_(num_render_channels), |
| 73 | clear_power_estimator_(freqs_, kDecayRate), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 74 | noise_power_estimator_(num_noise_bins, kDecayRate), |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 75 | filtered_clear_pow_(bank_size_, 0.f), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 76 | filtered_noise_pow_(num_noise_bins, 0.f), |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 77 | center_freqs_(bank_size_), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 78 | capture_filter_bank_(CreateErbBank(num_noise_bins)), |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 79 | render_filter_bank_(CreateErbBank(freqs_)), |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 80 | gains_eq_(bank_size_), |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 81 | gain_applier_(freqs_, kMaxRelativeGainChange), |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 82 | audio_s16_(chunk_length_), |
| 83 | chunks_since_voice_(kSpeechOffsetDelay), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 84 | is_speech_(false), |
| 85 | noise_estimation_buffer_(num_noise_bins), |
| 86 | noise_estimation_queue_(kMaxNumNoiseEstimatesToBuffer, |
| 87 | std::vector<float>(num_noise_bins), |
| 88 | RenderQueueItemVerifier<float>(num_noise_bins)) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 89 | RTC_DCHECK_LE(kRho, 1.f); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 90 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 91 | const size_t erb_index = static_cast<size_t>( |
| 92 | ceilf(11.17f * logf((kClipFreqKhz + 0.312f) / (kClipFreqKhz + 14.6575f)) + |
| 93 | 43.f)); |
| 94 | start_freq_ = std::max(static_cast<size_t>(1), erb_index * kErbResolution); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 95 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 96 | size_t window_size = static_cast<size_t>(1 << RealFourier::FftOrder(freqs_)); |
| 97 | std::vector<float> kbd_window(window_size); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 98 | WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size, |
| 99 | kbd_window.data()); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 100 | render_mangler_.reset(new LappedTransform( |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 101 | num_render_channels_, num_render_channels_, chunk_length_, |
| 102 | kbd_window.data(), window_size, window_size / 2, this)); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void IntelligibilityEnhancer::SetCaptureNoiseEstimate( |
| 106 | std::vector<float> noise) { |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 107 | RTC_DCHECK_EQ(noise.size(), num_noise_bins_); |
| 108 | // Disregarding return value since buffer overflow is acceptable, because it |
| 109 | // is not critical to get each noise estimate. |
| 110 | if (noise_estimation_queue_.Insert(&noise)) { |
| 111 | }; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 112 | } |
| 113 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 114 | void IntelligibilityEnhancer::ProcessRenderAudio(float* const* audio, |
| 115 | int sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 116 | size_t num_channels) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 117 | RTC_CHECK_EQ(sample_rate_hz_, sample_rate_hz); |
| 118 | RTC_CHECK_EQ(num_render_channels_, num_channels); |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 119 | while (noise_estimation_queue_.Remove(&noise_estimation_buffer_)) { |
| 120 | noise_power_estimator_.Step(noise_estimation_buffer_.data()); |
| 121 | } |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 122 | is_speech_ = IsSpeech(audio[0]); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 123 | render_mangler_->ProcessChunk(audio, audio); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 124 | } |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 125 | |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 126 | void IntelligibilityEnhancer::ProcessAudioBlock( |
| 127 | const std::complex<float>* const* in_block, |
| 128 | size_t in_channels, |
| 129 | size_t frames, |
| 130 | size_t /* out_channels */, |
| 131 | std::complex<float>* const* out_block) { |
| 132 | RTC_DCHECK_EQ(freqs_, frames); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 133 | if (is_speech_) { |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 134 | clear_power_estimator_.Step(in_block[0]); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 135 | } |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 136 | const std::vector<float>& clear_power = clear_power_estimator_.power(); |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame^] | 137 | const std::vector<float>& noise_power = noise_power_estimator_.power(); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 138 | MapToErbBands(clear_power.data(), render_filter_bank_, |
| 139 | filtered_clear_pow_.data()); |
| 140 | MapToErbBands(noise_power.data(), capture_filter_bank_, |
| 141 | filtered_noise_pow_.data()); |
| 142 | SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.data()); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 143 | const float power_target = |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 144 | std::accumulate(clear_power.data(), clear_power.data() + freqs_, 0.f); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 145 | const float power_top = |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 146 | DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
| 147 | SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.data()); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 148 | const float power_bot = |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 149 | DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 150 | if (power_target >= power_bot && power_target <= power_top) { |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 151 | SolveForLambda(power_target); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 152 | UpdateErbGains(); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 153 | } // Else experiencing power underflow, so do nothing. |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 154 | for (size_t i = 0; i < in_channels; ++i) { |
| 155 | gain_applier_.Apply(in_block[i], out_block[i]); |
| 156 | } |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 157 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 158 | |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 159 | void IntelligibilityEnhancer::SolveForLambda(float power_target) { |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 160 | const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values |
| 161 | const int kMaxIters = 100; // for these, based on experiments. |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 162 | |
Alejandro Luebs | 66d2481 | 2016-02-11 10:37:12 -0800 | [diff] [blame] | 163 | const float reciprocal_power_target = |
| 164 | 1.f / (power_target + std::numeric_limits<float>::epsilon()); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 165 | float lambda_bot = kLambdaBot; |
| 166 | float lambda_top = kLambdaTop; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 167 | float power_ratio = 2.f; // Ratio of achieved power to target power. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 168 | int iters = 0; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 169 | while (std::fabs(power_ratio - 1.f) > kConvergeThresh && iters <= kMaxIters) { |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 170 | const float lambda = (lambda_bot + lambda_top) / 2.f; |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 171 | SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.data()); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 172 | const float power = |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 173 | DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 174 | if (power < power_target) { |
| 175 | lambda_bot = lambda; |
| 176 | } else { |
| 177 | lambda_top = lambda; |
| 178 | } |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 179 | power_ratio = std::fabs(power * reciprocal_power_target); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 180 | ++iters; |
| 181 | } |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 182 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 183 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 184 | void IntelligibilityEnhancer::UpdateErbGains() { |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 185 | // (ERB gain) = filterbank' * (freq gain) |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 186 | float* gains = gain_applier_.target(); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 187 | for (size_t i = 0; i < freqs_; ++i) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 188 | gains[i] = 0.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 189 | for (size_t j = 0; j < bank_size_; ++j) { |
aluebs | a2abdf2 | 2016-03-02 18:36:49 -0800 | [diff] [blame] | 190 | gains[i] += render_filter_bank_[j][i] * gains_eq_[j]; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 195 | size_t IntelligibilityEnhancer::GetBankSize(int sample_rate, |
| 196 | size_t erb_resolution) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 197 | float freq_limit = sample_rate / 2000.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 198 | size_t erb_scale = static_cast<size_t>(ceilf( |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 199 | 11.17f * logf((freq_limit + 0.312f) / (freq_limit + 14.6575f)) + 43.f)); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 200 | return erb_scale * erb_resolution; |
| 201 | } |
| 202 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 203 | std::vector<std::vector<float>> IntelligibilityEnhancer::CreateErbBank( |
| 204 | size_t num_freqs) { |
| 205 | std::vector<std::vector<float>> filter_bank(bank_size_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 206 | size_t lf = 1, rf = 4; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 207 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 208 | for (size_t i = 0; i < bank_size_; ++i) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 209 | float abs_temp = fabsf((i + 1.f) / static_cast<float>(kErbResolution)); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 210 | center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp)); |
| 211 | center_freqs_[i] -= 14678.49f; |
| 212 | } |
| 213 | float last_center_freq = center_freqs_[bank_size_ - 1]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 214 | for (size_t i = 0; i < bank_size_; ++i) { |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 215 | center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq; |
| 216 | } |
| 217 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 218 | for (size_t i = 0; i < bank_size_; ++i) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 219 | filter_bank[i].resize(num_freqs); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 222 | for (size_t i = 1; i <= bank_size_; ++i) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 223 | static const size_t kOne = 1; // Avoids repeated static_cast<>s below. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 224 | size_t lll = |
| 225 | static_cast<size_t>(round(center_freqs_[std::max(kOne, i - lf) - 1] * |
| 226 | num_freqs / (0.5f * sample_rate_hz_))); |
| 227 | size_t ll = static_cast<size_t>(round(center_freqs_[std::max(kOne, i) - 1] * |
| 228 | num_freqs / (0.5f * sample_rate_hz_))); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 229 | lll = std::min(num_freqs, std::max(lll, kOne)) - 1; |
| 230 | ll = std::min(num_freqs, std::max(ll, kOne)) - 1; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 231 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 232 | size_t rrr = static_cast<size_t>( |
| 233 | round(center_freqs_[std::min(bank_size_, i + rf) - 1] * num_freqs / |
| 234 | (0.5f * sample_rate_hz_))); |
| 235 | size_t rr = static_cast<size_t>( |
| 236 | round(center_freqs_[std::min(bank_size_, i + 1) - 1] * num_freqs / |
| 237 | (0.5f * sample_rate_hz_))); |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 238 | rrr = std::min(num_freqs, std::max(rrr, kOne)) - 1; |
| 239 | rr = std::min(num_freqs, std::max(rr, kOne)) - 1; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 240 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 241 | float step = ll == lll ? 0.f : 1.f / (ll - lll); |
| 242 | float element = 0.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 243 | for (size_t j = lll; j <= ll; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 244 | filter_bank[i - 1][j] = element; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 245 | element += step; |
| 246 | } |
Alejandro Luebs | 66d2481 | 2016-02-11 10:37:12 -0800 | [diff] [blame] | 247 | step = rr == rrr ? 0.f : 1.f / (rrr - rr); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 248 | element = 1.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 249 | for (size_t j = rr; j <= rrr; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 250 | filter_bank[i - 1][j] = element; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 251 | element -= step; |
| 252 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 253 | for (size_t j = ll; j <= rr; ++j) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 254 | filter_bank[i - 1][j] = 1.f; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 258 | for (size_t i = 0; i < num_freqs; ++i) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 259 | float sum = 0.f; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 260 | for (size_t j = 0; j < bank_size_; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 261 | sum += filter_bank[j][i]; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 262 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 263 | for (size_t j = 0; j < bank_size_; ++j) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 264 | filter_bank[j][i] /= sum; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 267 | return filter_bank; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 268 | } |
| 269 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 270 | void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 271 | size_t start_freq, |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 272 | float* sols) { |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 273 | const float kMinPower = 1e-5f; |
| 274 | |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 275 | const float* pow_x0 = filtered_clear_pow_.data(); |
| 276 | const float* pow_n0 = filtered_noise_pow_.data(); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 277 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 278 | for (size_t n = 0; n < start_freq; ++n) { |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 279 | sols[n] = 1.f; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 280 | } |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 281 | |
| 282 | // Analytic solution for optimal gains. See paper for derivation. |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 283 | for (size_t n = start_freq; n < bank_size_; ++n) { |
| 284 | if (pow_x0[n] < kMinPower || pow_n0[n] < kMinPower) { |
| 285 | sols[n] = 1.f; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 286 | } else { |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 287 | const float gamma0 = 0.5f * kRho * pow_x0[n] * pow_n0[n] + |
| 288 | lambda * pow_x0[n] * pow_n0[n] * pow_n0[n]; |
| 289 | const float beta0 = |
| 290 | lambda * pow_x0[n] * (2.f - kRho) * pow_x0[n] * pow_n0[n]; |
| 291 | const float alpha0 = |
| 292 | lambda * pow_x0[n] * (1.f - kRho) * pow_x0[n] * pow_x0[n]; |
| 293 | RTC_DCHECK_LT(alpha0, 0.f); |
| 294 | // The quadratic equation should always have real roots, but to guard |
| 295 | // against numerical errors we limit it to a minimum of zero. |
| 296 | sols[n] = std::max( |
| 297 | 0.f, (-beta0 - std::sqrt(std::max( |
| 298 | 0.f, beta0 * beta0 - 4.f * alpha0 * gamma0))) / |
| 299 | (2.f * alpha0)); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 300 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 304 | bool IntelligibilityEnhancer::IsSpeech(const float* audio) { |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 305 | FloatToS16(audio, chunk_length_, audio_s16_.data()); |
| 306 | vad_.ProcessChunk(audio_s16_.data(), chunk_length_, sample_rate_hz_); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 307 | if (vad_.last_voice_probability() > kVoiceProbabilityThreshold) { |
| 308 | chunks_since_voice_ = 0; |
| 309 | } else if (chunks_since_voice_ < kSpeechOffsetDelay) { |
| 310 | ++chunks_since_voice_; |
| 311 | } |
| 312 | return chunks_since_voice_ < kSpeechOffsetDelay; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 313 | } |
| 314 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 315 | } // namespace webrtc |