blob: f3d023ec620212142d41458e9c04070ad3dd1aae [file] [log] [blame]
ekm030249d2015-06-15 13:02:24 -07001/*
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
ekm35b72fb2015-07-10 14:11:52 -070013#include <math.h>
14#include <stdlib.h>
ekm030249d2015-06-15 13:02:24 -070015#include <algorithm>
Alejandro Luebs66d24812016-02-11 10:37:12 -080016#include <limits>
ekmdb4fecf2015-06-22 17:49:08 -070017#include <numeric>
ekm030249d2015-06-15 13:02:24 -070018
19#include "webrtc/base/checks.h"
Alejandro Luebs1aa82192016-07-01 17:16:01 -070020#include "webrtc/base/logging.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070021#include "webrtc/common_audio/include/audio_util.h"
ekm030249d2015-06-15 13:02:24 -070022#include "webrtc/common_audio/window_generator.h"
23
ekm35b72fb2015-07-10 14:11:52 -070024namespace webrtc {
25
26namespace {
27
Peter Kastingdce40cf2015-08-24 14:52:23 -070028const size_t kErbResolution = 2;
Alejandro Luebs32348192016-02-17 20:04:19 -080029const int kWindowSizeMs = 16;
ekm35b72fb2015-07-10 14:11:52 -070030const int kChunkSizeMs = 10; // Size provided by APM.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080031const float kClipFreqKhz = 0.2f;
ekm35b72fb2015-07-10 14:11:52 -070032const float kKbdAlpha = 1.5f;
Alejandro Luebs3b149962016-04-01 13:54:36 -070033const float kLambdaBot = -1.f; // Extreme values in bisection
Alejandro Luebsdd56fa82016-03-29 13:05:40 -070034const float kLambdaTop = -1e-5f; // search for lamda.
aluebs7bd5f252016-06-21 11:30:25 -070035const float kVoiceProbabilityThreshold = 0.5f;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080036// Number of chunks after voice activity which is still considered speech.
aluebs7bd5f252016-06-21 11:30:25 -070037const size_t kSpeechOffsetDelay = 10;
38const float kDecayRate = 0.995f; // Power estimation decay rate.
39const float kMaxRelativeGainChange = 0.005f;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080040const float kRho = 0.0004f; // Default production and interpretation SNR.
Alejandro Luebs3b149962016-04-01 13:54:36 -070041const float kPowerNormalizationFactor = 1.f / (1 << 30);
aluebs2fae89e2016-04-13 11:24:06 -070042const float kMaxActiveSNR = 128.f; // 21dB
43const float kMinInactiveSNR = 32.f; // 15dB
aluebse8f8f602016-06-08 09:53:18 -070044const size_t kGainUpdatePeriod = 10u;
ekm35b72fb2015-07-10 14:11:52 -070045
aluebsc466bad2016-02-10 12:03:00 -080046// Returns dot product of vectors |a| and |b| with size |length|.
47float DotProduct(const float* a, const float* b, size_t length) {
48 float ret = 0.f;
49 for (size_t i = 0; i < length; ++i) {
aluebsa2abdf22016-03-02 18:36:49 -080050 ret += a[i] * b[i];
aluebsc466bad2016-02-10 12:03:00 -080051 }
52 return ret;
53}
54
Alejandro Luebs32348192016-02-17 20:04:19 -080055// Computes the power across ERB bands from the power spectral density |pow|.
aluebsc466bad2016-02-10 12:03:00 -080056// Stores it in |result|.
Alejandro Luebs32348192016-02-17 20:04:19 -080057void MapToErbBands(const float* pow,
58 const std::vector<std::vector<float>>& filter_bank,
59 float* result) {
aluebsc466bad2016-02-10 12:03:00 -080060 for (size_t i = 0; i < filter_bank.size(); ++i) {
61 RTC_DCHECK_GT(filter_bank[i].size(), 0u);
Alejandro Luebs3b149962016-04-01 13:54:36 -070062 result[i] = kPowerNormalizationFactor *
63 DotProduct(filter_bank[i].data(), pow, filter_bank[i].size());
aluebsc466bad2016-02-10 12:03:00 -080064 }
65}
66
ekm35b72fb2015-07-10 14:11:52 -070067} // namespace
68
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080069IntelligibilityEnhancer::IntelligibilityEnhancer(int sample_rate_hz,
Alex Luebs57ae8292016-03-09 16:24:34 +010070 size_t num_render_channels,
71 size_t num_noise_bins)
ekmdb4fecf2015-06-22 17:49:08 -070072 : freqs_(RealFourier::ComplexLength(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080073 RealFourier::FftOrder(sample_rate_hz * kWindowSizeMs / 1000))),
Alex Luebs57ae8292016-03-09 16:24:34 +010074 num_noise_bins_(num_noise_bins),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080075 chunk_length_(static_cast<size_t>(sample_rate_hz * kChunkSizeMs / 1000)),
76 bank_size_(GetBankSize(sample_rate_hz, kErbResolution)),
77 sample_rate_hz_(sample_rate_hz),
78 num_render_channels_(num_render_channels),
79 clear_power_estimator_(freqs_, kDecayRate),
Alex Luebs57ae8292016-03-09 16:24:34 +010080 noise_power_estimator_(num_noise_bins, kDecayRate),
aluebs0a007592016-02-26 17:17:38 -080081 filtered_clear_pow_(bank_size_, 0.f),
Alex Luebs57ae8292016-03-09 16:24:34 +010082 filtered_noise_pow_(num_noise_bins, 0.f),
aluebs0a007592016-02-26 17:17:38 -080083 center_freqs_(bank_size_),
Alex Luebs57ae8292016-03-09 16:24:34 +010084 capture_filter_bank_(CreateErbBank(num_noise_bins)),
aluebsc466bad2016-02-10 12:03:00 -080085 render_filter_bank_(CreateErbBank(freqs_)),
aluebs0a007592016-02-26 17:17:38 -080086 gains_eq_(bank_size_),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080087 gain_applier_(freqs_, kMaxRelativeGainChange),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080088 audio_s16_(chunk_length_),
89 chunks_since_voice_(kSpeechOffsetDelay),
Alex Luebs57ae8292016-03-09 16:24:34 +010090 is_speech_(false),
aluebs2fae89e2016-04-13 11:24:06 -070091 snr_(kMaxActiveSNR),
92 is_active_(false),
aluebse8f8f602016-06-08 09:53:18 -070093 num_chunks_(0u),
Alejandro Luebs1aa82192016-07-01 17:16:01 -070094 num_active_chunks_(0u),
Alex Luebs57ae8292016-03-09 16:24:34 +010095 noise_estimation_buffer_(num_noise_bins),
96 noise_estimation_queue_(kMaxNumNoiseEstimatesToBuffer,
97 std::vector<float>(num_noise_bins),
98 RenderQueueItemVerifier<float>(num_noise_bins)) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080099 RTC_DCHECK_LE(kRho, 1.f);
ekm030249d2015-06-15 13:02:24 -0700100
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800101 const size_t erb_index = static_cast<size_t>(
102 ceilf(11.17f * logf((kClipFreqKhz + 0.312f) / (kClipFreqKhz + 14.6575f)) +
103 43.f));
104 start_freq_ = std::max(static_cast<size_t>(1), erb_index * kErbResolution);
ekm030249d2015-06-15 13:02:24 -0700105
brucedawsond81dc492016-04-01 10:16:14 -0700106 size_t window_size = static_cast<size_t>(1) << RealFourier::FftOrder(freqs_);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800107 std::vector<float> kbd_window(window_size);
aluebs0a007592016-02-26 17:17:38 -0800108 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size,
109 kbd_window.data());
ekmdb4fecf2015-06-22 17:49:08 -0700110 render_mangler_.reset(new LappedTransform(
aluebs0a007592016-02-26 17:17:38 -0800111 num_render_channels_, num_render_channels_, chunk_length_,
112 kbd_window.data(), window_size, window_size / 2, this));
aluebsc466bad2016-02-10 12:03:00 -0800113}
114
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700115IntelligibilityEnhancer::~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
aluebsc466bad2016-02-10 12:03:00 -0800123void IntelligibilityEnhancer::SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -0700124 std::vector<float> noise, float gain) {
Alex Luebs57ae8292016-03-09 16:24:34 +0100125 RTC_DCHECK_EQ(noise.size(), num_noise_bins_);
aluebs11d4a422016-04-28 14:58:32 -0700126 for (auto& bin : noise) {
127 bin *= gain;
128 }
Alex Luebs57ae8292016-03-09 16:24:34 +0100129 // Disregarding return value since buffer overflow is acceptable, because it
130 // is not critical to get each noise estimate.
131 if (noise_estimation_queue_.Insert(&noise)) {
132 };
ekm030249d2015-06-15 13:02:24 -0700133}
134
ekmeyerson60d9b332015-08-14 10:35:55 -0700135void IntelligibilityEnhancer::ProcessRenderAudio(float* const* audio,
136 int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800137 size_t num_channels) {
henrikg91d6ede2015-09-17 00:24:34 -0700138 RTC_CHECK_EQ(sample_rate_hz_, sample_rate_hz);
139 RTC_CHECK_EQ(num_render_channels_, num_channels);
Alex Luebs57ae8292016-03-09 16:24:34 +0100140 while (noise_estimation_queue_.Remove(&noise_estimation_buffer_)) {
141 noise_power_estimator_.Step(noise_estimation_buffer_.data());
142 }
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800143 is_speech_ = IsSpeech(audio[0]);
aluebs0a007592016-02-26 17:17:38 -0800144 render_mangler_->ProcessChunk(audio, audio);
ekmeyerson60d9b332015-08-14 10:35:55 -0700145}
ekmdb4fecf2015-06-22 17:49:08 -0700146
aluebs0a007592016-02-26 17:17:38 -0800147void IntelligibilityEnhancer::ProcessAudioBlock(
148 const std::complex<float>* const* in_block,
149 size_t in_channels,
150 size_t frames,
151 size_t /* out_channels */,
152 std::complex<float>* const* out_block) {
153 RTC_DCHECK_EQ(freqs_, frames);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800154 if (is_speech_) {
aluebs0a007592016-02-26 17:17:38 -0800155 clear_power_estimator_.Step(in_block[0]);
ekm030249d2015-06-15 13:02:24 -0700156 }
aluebs2fae89e2016-04-13 11:24:06 -0700157 SnrBasedEffectActivation();
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700158 ++num_chunks_;
159 if (is_active_) {
160 ++num_active_chunks_;
161 if (num_chunks_ % kGainUpdatePeriod == 0) {
162 MapToErbBands(clear_power_estimator_.power().data(), render_filter_bank_,
163 filtered_clear_pow_.data());
164 MapToErbBands(noise_power_estimator_.power().data(), capture_filter_bank_,
165 filtered_noise_pow_.data());
166 SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.data());
167 const float power_target = std::accumulate(
168 filtered_clear_pow_.data(),
169 filtered_clear_pow_.data() + bank_size_,
170 0.f);
171 const float power_top =
172 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
173 SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.data());
174 const float power_bot =
175 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
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 }
aluebs2fae89e2016-04-13 11:24:06 -0700181 }
aluebs0a007592016-02-26 17:17:38 -0800182 for (size_t i = 0; i < in_channels; ++i) {
183 gain_applier_.Apply(in_block[i], out_block[i]);
184 }
ekm35b72fb2015-07-10 14:11:52 -0700185}
ekm030249d2015-06-15 13:02:24 -0700186
aluebs2fae89e2016-04-13 11:24:06 -0700187void IntelligibilityEnhancer::SnrBasedEffectActivation() {
188 const float* clear_psd = clear_power_estimator_.power().data();
189 const float* noise_psd = noise_power_estimator_.power().data();
190 const float clear_power =
191 std::accumulate(clear_psd, clear_psd + freqs_, 0.f);
192 const float noise_power =
193 std::accumulate(noise_psd, noise_psd + freqs_, 0.f);
194 snr_ = kDecayRate * snr_ + (1.f - kDecayRate) * clear_power /
195 (noise_power + std::numeric_limits<float>::epsilon());
196 if (is_active_) {
197 if (snr_ > kMaxActiveSNR) {
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700198 LOG(LS_INFO) << "Intelligibility Enhancer was deactivated at chunk "
199 << num_chunks_;
aluebs2fae89e2016-04-13 11:24:06 -0700200 is_active_ = false;
201 // Set the target gains to unity.
202 float* gains = gain_applier_.target();
203 for (size_t i = 0; i < freqs_; ++i) {
204 gains[i] = 1.f;
205 }
206 }
207 } else {
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700208 if (snr_ < kMinInactiveSNR) {
209 LOG(LS_INFO) << "Intelligibility Enhancer was activated at chunk "
210 << num_chunks_;
211 is_active_ = true;
212 }
aluebs2fae89e2016-04-13 11:24:06 -0700213 }
214}
215
aluebsf99af6b2016-02-24 17:25:42 -0800216void IntelligibilityEnhancer::SolveForLambda(float power_target) {
ekmdb4fecf2015-06-22 17:49:08 -0700217 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values
218 const int kMaxIters = 100; // for these, based on experiments.
ekm35b72fb2015-07-10 14:11:52 -0700219
Alejandro Luebs66d24812016-02-11 10:37:12 -0800220 const float reciprocal_power_target =
221 1.f / (power_target + std::numeric_limits<float>::epsilon());
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700222 float lambda_bot = kLambdaBot;
223 float lambda_top = kLambdaTop;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800224 float power_ratio = 2.f; // Ratio of achieved power to target power.
ekm030249d2015-06-15 13:02:24 -0700225 int iters = 0;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800226 while (std::fabs(power_ratio - 1.f) > kConvergeThresh && iters <= kMaxIters) {
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700227 const float lambda = (lambda_bot + lambda_top) / 2.f;
aluebs0a007592016-02-26 17:17:38 -0800228 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.data());
ekm35b72fb2015-07-10 14:11:52 -0700229 const float power =
aluebs0a007592016-02-26 17:17:38 -0800230 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
ekm030249d2015-06-15 13:02:24 -0700231 if (power < power_target) {
232 lambda_bot = lambda;
233 } else {
234 lambda_top = lambda;
235 }
ekm35b72fb2015-07-10 14:11:52 -0700236 power_ratio = std::fabs(power * reciprocal_power_target);
ekm030249d2015-06-15 13:02:24 -0700237 ++iters;
238 }
ekm35b72fb2015-07-10 14:11:52 -0700239}
ekm030249d2015-06-15 13:02:24 -0700240
ekm35b72fb2015-07-10 14:11:52 -0700241void IntelligibilityEnhancer::UpdateErbGains() {
ekmdb4fecf2015-06-22 17:49:08 -0700242 // (ERB gain) = filterbank' * (freq gain)
ekm030249d2015-06-15 13:02:24 -0700243 float* gains = gain_applier_.target();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700244 for (size_t i = 0; i < freqs_; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800245 gains[i] = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700246 for (size_t j = 0; j < bank_size_; ++j) {
aluebsa2abdf22016-03-02 18:36:49 -0800247 gains[i] += render_filter_bank_[j][i] * gains_eq_[j];
ekm030249d2015-06-15 13:02:24 -0700248 }
249 }
250}
251
Peter Kastingdce40cf2015-08-24 14:52:23 -0700252size_t IntelligibilityEnhancer::GetBankSize(int sample_rate,
253 size_t erb_resolution) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800254 float freq_limit = sample_rate / 2000.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700255 size_t erb_scale = static_cast<size_t>(ceilf(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800256 11.17f * logf((freq_limit + 0.312f) / (freq_limit + 14.6575f)) + 43.f));
ekm030249d2015-06-15 13:02:24 -0700257 return erb_scale * erb_resolution;
258}
259
aluebsc466bad2016-02-10 12:03:00 -0800260std::vector<std::vector<float>> IntelligibilityEnhancer::CreateErbBank(
261 size_t num_freqs) {
262 std::vector<std::vector<float>> filter_bank(bank_size_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700263 size_t lf = 1, rf = 4;
ekm030249d2015-06-15 13:02:24 -0700264
Peter Kastingdce40cf2015-08-24 14:52:23 -0700265 for (size_t i = 0; i < bank_size_; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800266 float abs_temp = fabsf((i + 1.f) / static_cast<float>(kErbResolution));
ekm030249d2015-06-15 13:02:24 -0700267 center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp));
268 center_freqs_[i] -= 14678.49f;
269 }
270 float last_center_freq = center_freqs_[bank_size_ - 1];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 for (size_t i = 0; i < bank_size_; ++i) {
ekm030249d2015-06-15 13:02:24 -0700272 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq;
273 }
274
Peter Kastingdce40cf2015-08-24 14:52:23 -0700275 for (size_t i = 0; i < bank_size_; ++i) {
aluebsc466bad2016-02-10 12:03:00 -0800276 filter_bank[i].resize(num_freqs);
ekm030249d2015-06-15 13:02:24 -0700277 }
278
Peter Kastingdce40cf2015-08-24 14:52:23 -0700279 for (size_t i = 1; i <= bank_size_; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700280 static const size_t kOne = 1; // Avoids repeated static_cast<>s below.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800281 size_t lll =
282 static_cast<size_t>(round(center_freqs_[std::max(kOne, i - lf) - 1] *
283 num_freqs / (0.5f * sample_rate_hz_)));
284 size_t ll = static_cast<size_t>(round(center_freqs_[std::max(kOne, i) - 1] *
285 num_freqs / (0.5f * sample_rate_hz_)));
Alejandro Luebs32348192016-02-17 20:04:19 -0800286 lll = std::min(num_freqs, std::max(lll, kOne)) - 1;
287 ll = std::min(num_freqs, std::max(ll, kOne)) - 1;
ekm030249d2015-06-15 13:02:24 -0700288
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800289 size_t rrr = static_cast<size_t>(
290 round(center_freqs_[std::min(bank_size_, i + rf) - 1] * num_freqs /
291 (0.5f * sample_rate_hz_)));
292 size_t rr = static_cast<size_t>(
293 round(center_freqs_[std::min(bank_size_, i + 1) - 1] * num_freqs /
294 (0.5f * sample_rate_hz_)));
Alejandro Luebs32348192016-02-17 20:04:19 -0800295 rrr = std::min(num_freqs, std::max(rrr, kOne)) - 1;
296 rr = std::min(num_freqs, std::max(rr, kOne)) - 1;
ekm030249d2015-06-15 13:02:24 -0700297
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800298 float step = ll == lll ? 0.f : 1.f / (ll - lll);
299 float element = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700300 for (size_t j = lll; j <= ll; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800301 filter_bank[i - 1][j] = element;
ekm030249d2015-06-15 13:02:24 -0700302 element += step;
303 }
Alejandro Luebs66d24812016-02-11 10:37:12 -0800304 step = rr == rrr ? 0.f : 1.f / (rrr - rr);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800305 element = 1.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700306 for (size_t j = rr; j <= rrr; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800307 filter_bank[i - 1][j] = element;
ekm030249d2015-06-15 13:02:24 -0700308 element -= step;
309 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700310 for (size_t j = ll; j <= rr; ++j) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800311 filter_bank[i - 1][j] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700312 }
313 }
314
aluebsc466bad2016-02-10 12:03:00 -0800315 for (size_t i = 0; i < num_freqs; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800316 float sum = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700317 for (size_t j = 0; j < bank_size_; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800318 sum += filter_bank[j][i];
ekm030249d2015-06-15 13:02:24 -0700319 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700320 for (size_t j = 0; j < bank_size_; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800321 filter_bank[j][i] /= sum;
ekm030249d2015-06-15 13:02:24 -0700322 }
323 }
aluebsc466bad2016-02-10 12:03:00 -0800324 return filter_bank;
ekm030249d2015-06-15 13:02:24 -0700325}
326
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700327void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700328 size_t start_freq,
ekmdb4fecf2015-06-22 17:49:08 -0700329 float* sols) {
aluebsf99af6b2016-02-24 17:25:42 -0800330 const float kMinPower = 1e-5f;
331
aluebs0a007592016-02-26 17:17:38 -0800332 const float* pow_x0 = filtered_clear_pow_.data();
333 const float* pow_n0 = filtered_noise_pow_.data();
ekm030249d2015-06-15 13:02:24 -0700334
Peter Kastingdce40cf2015-08-24 14:52:23 -0700335 for (size_t n = 0; n < start_freq; ++n) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800336 sols[n] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700337 }
ekmdb4fecf2015-06-22 17:49:08 -0700338
339 // Analytic solution for optimal gains. See paper for derivation.
aluebsf99af6b2016-02-24 17:25:42 -0800340 for (size_t n = start_freq; n < bank_size_; ++n) {
341 if (pow_x0[n] < kMinPower || pow_n0[n] < kMinPower) {
342 sols[n] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700343 } else {
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700344 const float gamma0 = 0.5f * kRho * pow_x0[n] * pow_n0[n] +
aluebsf99af6b2016-02-24 17:25:42 -0800345 lambda * pow_x0[n] * pow_n0[n] * pow_n0[n];
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700346 const float beta0 =
347 lambda * pow_x0[n] * (2.f - kRho) * pow_x0[n] * pow_n0[n];
348 const float alpha0 =
349 lambda * pow_x0[n] * (1.f - kRho) * pow_x0[n] * pow_x0[n];
350 RTC_DCHECK_LT(alpha0, 0.f);
aluebsf99af6b2016-02-24 17:25:42 -0800351 // The quadratic equation should always have real roots, but to guard
352 // against numerical errors we limit it to a minimum of zero.
353 sols[n] = std::max(
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700354 0.f, (-beta0 - std::sqrt(std::max(
355 0.f, beta0 * beta0 - 4.f * alpha0 * gamma0))) /
356 (2.f * alpha0));
ekm030249d2015-06-15 13:02:24 -0700357 }
ekm030249d2015-06-15 13:02:24 -0700358 }
359}
360
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800361bool IntelligibilityEnhancer::IsSpeech(const float* audio) {
aluebs0a007592016-02-26 17:17:38 -0800362 FloatToS16(audio, chunk_length_, audio_s16_.data());
363 vad_.ProcessChunk(audio_s16_.data(), chunk_length_, sample_rate_hz_);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800364 if (vad_.last_voice_probability() > kVoiceProbabilityThreshold) {
365 chunks_since_voice_ = 0;
366 } else if (chunks_since_voice_ < kSpeechOffsetDelay) {
367 ++chunks_since_voice_;
368 }
369 return chunks_since_voice_ < kSpeechOffsetDelay;
ekmeyerson60d9b332015-08-14 10:35:55 -0700370}
371
ekm030249d2015-06-15 13:02:24 -0700372} // namespace webrtc