blob: 588310a8b7cd28c6cbe0f676a267393e9ec6963a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/intelligibility/intelligibility_enhancer.h"
ekm030249d2015-06-15 13:02:24 -070012
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_audio/include/audio_util.h"
20#include "common_audio/window_generator.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "rtc_base/safe_minmax.h"
ekm030249d2015-06-15 13:02:24 -070024
ekm35b72fb2015-07-10 14:11:52 -070025namespace webrtc {
26
27namespace {
28
Peter Kastingdce40cf2015-08-24 14:52:23 -070029const size_t kErbResolution = 2;
Alejandro Luebs32348192016-02-17 20:04:19 -080030const int kWindowSizeMs = 16;
ekm35b72fb2015-07-10 14:11:52 -070031const int kChunkSizeMs = 10; // Size provided by APM.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080032const float kClipFreqKhz = 0.2f;
ekm35b72fb2015-07-10 14:11:52 -070033const float kKbdAlpha = 1.5f;
Alejandro Luebs3b149962016-04-01 13:54:36 -070034const float kLambdaBot = -1.f; // Extreme values in bisection
Alejandro Luebsdd56fa82016-03-29 13:05:40 -070035const float kLambdaTop = -1e-5f; // search for lamda.
aluebs7bd5f252016-06-21 11:30:25 -070036const float kVoiceProbabilityThreshold = 0.5f;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080037// Number of chunks after voice activity which is still considered speech.
aluebs7bd5f252016-06-21 11:30:25 -070038const size_t kSpeechOffsetDelay = 10;
39const float kDecayRate = 0.995f; // Power estimation decay rate.
40const float kMaxRelativeGainChange = 0.005f;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080041const float kRho = 0.0004f; // Default production and interpretation SNR.
Alejandro Luebs3b149962016-04-01 13:54:36 -070042const float kPowerNormalizationFactor = 1.f / (1 << 30);
aluebs2fae89e2016-04-13 11:24:06 -070043const float kMaxActiveSNR = 128.f; // 21dB
44const float kMinInactiveSNR = 32.f; // 15dB
aluebse8f8f602016-06-08 09:53:18 -070045const size_t kGainUpdatePeriod = 10u;
ekm35b72fb2015-07-10 14:11:52 -070046
aluebsc466bad2016-02-10 12:03:00 -080047// Returns dot product of vectors |a| and |b| with size |length|.
48float DotProduct(const float* a, const float* b, size_t length) {
49 float ret = 0.f;
50 for (size_t i = 0; i < length; ++i) {
aluebsa2abdf22016-03-02 18:36:49 -080051 ret += a[i] * b[i];
aluebsc466bad2016-02-10 12:03:00 -080052 }
53 return ret;
54}
55
Alejandro Luebs32348192016-02-17 20:04:19 -080056// Computes the power across ERB bands from the power spectral density |pow|.
aluebsc466bad2016-02-10 12:03:00 -080057// Stores it in |result|.
Alejandro Luebs32348192016-02-17 20:04:19 -080058void MapToErbBands(const float* pow,
59 const std::vector<std::vector<float>>& filter_bank,
60 float* result) {
aluebsc466bad2016-02-10 12:03:00 -080061 for (size_t i = 0; i < filter_bank.size(); ++i) {
kwibergaf476c72016-11-28 15:21:39 -080062 RTC_DCHECK_GT(filter_bank[i].size(), 0);
Alejandro Luebs3b149962016-04-01 13:54:36 -070063 result[i] = kPowerNormalizationFactor *
64 DotProduct(filter_bank[i].data(), pow, filter_bank[i].size());
aluebsc466bad2016-02-10 12:03:00 -080065 }
66}
67
ekm35b72fb2015-07-10 14:11:52 -070068} // namespace
69
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080070IntelligibilityEnhancer::IntelligibilityEnhancer(int sample_rate_hz,
Alex Luebs57ae8292016-03-09 16:24:34 +010071 size_t num_render_channels,
Alejandro Luebsef009252016-09-20 14:51:56 -070072 size_t num_bands,
Alex Luebs57ae8292016-03-09 16:24:34 +010073 size_t num_noise_bins)
ekmdb4fecf2015-06-22 17:49:08 -070074 : freqs_(RealFourier::ComplexLength(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080075 RealFourier::FftOrder(sample_rate_hz * kWindowSizeMs / 1000))),
Alex Luebs57ae8292016-03-09 16:24:34 +010076 num_noise_bins_(num_noise_bins),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080077 chunk_length_(static_cast<size_t>(sample_rate_hz * kChunkSizeMs / 1000)),
78 bank_size_(GetBankSize(sample_rate_hz, kErbResolution)),
79 sample_rate_hz_(sample_rate_hz),
80 num_render_channels_(num_render_channels),
81 clear_power_estimator_(freqs_, kDecayRate),
Alex Luebs57ae8292016-03-09 16:24:34 +010082 noise_power_estimator_(num_noise_bins, kDecayRate),
aluebs0a007592016-02-26 17:17:38 -080083 filtered_clear_pow_(bank_size_, 0.f),
Alex Luebs57ae8292016-03-09 16:24:34 +010084 filtered_noise_pow_(num_noise_bins, 0.f),
aluebs0a007592016-02-26 17:17:38 -080085 center_freqs_(bank_size_),
Alex Luebs57ae8292016-03-09 16:24:34 +010086 capture_filter_bank_(CreateErbBank(num_noise_bins)),
aluebsc466bad2016-02-10 12:03:00 -080087 render_filter_bank_(CreateErbBank(freqs_)),
aluebs0a007592016-02-26 17:17:38 -080088 gains_eq_(bank_size_),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080089 gain_applier_(freqs_, kMaxRelativeGainChange),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080090 audio_s16_(chunk_length_),
91 chunks_since_voice_(kSpeechOffsetDelay),
Alex Luebs57ae8292016-03-09 16:24:34 +010092 is_speech_(false),
aluebs2fae89e2016-04-13 11:24:06 -070093 snr_(kMaxActiveSNR),
94 is_active_(false),
aluebse8f8f602016-06-08 09:53:18 -070095 num_chunks_(0u),
Alejandro Luebs1aa82192016-07-01 17:16:01 -070096 num_active_chunks_(0u),
Alex Luebs57ae8292016-03-09 16:24:34 +010097 noise_estimation_buffer_(num_noise_bins),
98 noise_estimation_queue_(kMaxNumNoiseEstimatesToBuffer,
99 std::vector<float>(num_noise_bins),
100 RenderQueueItemVerifier<float>(num_noise_bins)) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800101 RTC_DCHECK_LE(kRho, 1.f);
ekm030249d2015-06-15 13:02:24 -0700102
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800103 const size_t erb_index = static_cast<size_t>(
104 ceilf(11.17f * logf((kClipFreqKhz + 0.312f) / (kClipFreqKhz + 14.6575f)) +
105 43.f));
106 start_freq_ = std::max(static_cast<size_t>(1), erb_index * kErbResolution);
ekm030249d2015-06-15 13:02:24 -0700107
brucedawsond81dc492016-04-01 10:16:14 -0700108 size_t window_size = static_cast<size_t>(1) << RealFourier::FftOrder(freqs_);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800109 std::vector<float> kbd_window(window_size);
aluebs0a007592016-02-26 17:17:38 -0800110 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size,
111 kbd_window.data());
ekmdb4fecf2015-06-22 17:49:08 -0700112 render_mangler_.reset(new LappedTransform(
aluebs0a007592016-02-26 17:17:38 -0800113 num_render_channels_, num_render_channels_, chunk_length_,
114 kbd_window.data(), window_size, window_size / 2, this));
Alejandro Luebsef009252016-09-20 14:51:56 -0700115
116 const size_t initial_delay = render_mangler_->initial_delay();
117 for (size_t i = 0u; i < num_bands - 1; ++i) {
118 high_bands_buffers_.push_back(std::unique_ptr<intelligibility::DelayBuffer>(
119 new intelligibility::DelayBuffer(initial_delay, num_render_channels_)));
120 }
aluebsc466bad2016-02-10 12:03:00 -0800121}
122
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700123IntelligibilityEnhancer::~IntelligibilityEnhancer() {
Alejandro Luebsef009252016-09-20 14:51:56 -0700124 // Don't rely on this log, since the destructor isn't called when the
125 // app/tab is killed.
126 if (num_chunks_ > 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100127 RTC_LOG(LS_INFO) << "Intelligibility Enhancer was active for "
128 << 100.f * static_cast<float>(num_active_chunks_) /
129 num_chunks_
130 << "% of the call.";
Alejandro Luebsef009252016-09-20 14:51:56 -0700131 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100132 RTC_LOG(LS_INFO) << "Intelligibility Enhancer processed no chunk.";
Alejandro Luebsef009252016-09-20 14:51:56 -0700133 }
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700134}
135
aluebsc466bad2016-02-10 12:03:00 -0800136void IntelligibilityEnhancer::SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -0700137 std::vector<float> noise, float gain) {
Alex Luebs57ae8292016-03-09 16:24:34 +0100138 RTC_DCHECK_EQ(noise.size(), num_noise_bins_);
aluebs11d4a422016-04-28 14:58:32 -0700139 for (auto& bin : noise) {
140 bin *= gain;
141 }
Alex Luebs57ae8292016-03-09 16:24:34 +0100142 // Disregarding return value since buffer overflow is acceptable, because it
143 // is not critical to get each noise estimate.
144 if (noise_estimation_queue_.Insert(&noise)) {
145 };
ekm030249d2015-06-15 13:02:24 -0700146}
147
Alejandro Luebsef009252016-09-20 14:51:56 -0700148void IntelligibilityEnhancer::ProcessRenderAudio(AudioBuffer* audio) {
149 RTC_DCHECK_EQ(num_render_channels_, audio->num_channels());
Alex Luebs57ae8292016-03-09 16:24:34 +0100150 while (noise_estimation_queue_.Remove(&noise_estimation_buffer_)) {
151 noise_power_estimator_.Step(noise_estimation_buffer_.data());
152 }
Alejandro Luebsef009252016-09-20 14:51:56 -0700153 float* const* low_band = audio->split_channels_f(kBand0To8kHz);
154 is_speech_ = IsSpeech(low_band[0]);
155 render_mangler_->ProcessChunk(low_band, low_band);
156 DelayHighBands(audio);
ekmeyerson60d9b332015-08-14 10:35:55 -0700157}
ekmdb4fecf2015-06-22 17:49:08 -0700158
aluebs0a007592016-02-26 17:17:38 -0800159void IntelligibilityEnhancer::ProcessAudioBlock(
160 const std::complex<float>* const* in_block,
161 size_t in_channels,
162 size_t frames,
163 size_t /* out_channels */,
164 std::complex<float>* const* out_block) {
165 RTC_DCHECK_EQ(freqs_, frames);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800166 if (is_speech_) {
aluebs0a007592016-02-26 17:17:38 -0800167 clear_power_estimator_.Step(in_block[0]);
ekm030249d2015-06-15 13:02:24 -0700168 }
aluebs2fae89e2016-04-13 11:24:06 -0700169 SnrBasedEffectActivation();
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700170 ++num_chunks_;
171 if (is_active_) {
172 ++num_active_chunks_;
173 if (num_chunks_ % kGainUpdatePeriod == 0) {
174 MapToErbBands(clear_power_estimator_.power().data(), render_filter_bank_,
175 filtered_clear_pow_.data());
176 MapToErbBands(noise_power_estimator_.power().data(), capture_filter_bank_,
177 filtered_noise_pow_.data());
178 SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.data());
179 const float power_target = std::accumulate(
180 filtered_clear_pow_.data(),
181 filtered_clear_pow_.data() + bank_size_,
182 0.f);
183 const float power_top =
184 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
185 SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.data());
186 const float power_bot =
187 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
188 if (power_target >= power_bot && power_target <= power_top) {
189 SolveForLambda(power_target);
190 UpdateErbGains();
191 } // Else experiencing power underflow, so do nothing.
192 }
aluebs2fae89e2016-04-13 11:24:06 -0700193 }
aluebs0a007592016-02-26 17:17:38 -0800194 for (size_t i = 0; i < in_channels; ++i) {
195 gain_applier_.Apply(in_block[i], out_block[i]);
196 }
ekm35b72fb2015-07-10 14:11:52 -0700197}
ekm030249d2015-06-15 13:02:24 -0700198
aluebs2fae89e2016-04-13 11:24:06 -0700199void IntelligibilityEnhancer::SnrBasedEffectActivation() {
200 const float* clear_psd = clear_power_estimator_.power().data();
201 const float* noise_psd = noise_power_estimator_.power().data();
202 const float clear_power =
203 std::accumulate(clear_psd, clear_psd + freqs_, 0.f);
204 const float noise_power =
205 std::accumulate(noise_psd, noise_psd + freqs_, 0.f);
206 snr_ = kDecayRate * snr_ + (1.f - kDecayRate) * clear_power /
207 (noise_power + std::numeric_limits<float>::epsilon());
208 if (is_active_) {
209 if (snr_ > kMaxActiveSNR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100210 RTC_LOG(LS_INFO) << "Intelligibility Enhancer was deactivated at chunk "
211 << num_chunks_;
aluebs2fae89e2016-04-13 11:24:06 -0700212 is_active_ = false;
213 // Set the target gains to unity.
214 float* gains = gain_applier_.target();
215 for (size_t i = 0; i < freqs_; ++i) {
216 gains[i] = 1.f;
217 }
218 }
219 } else {
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700220 if (snr_ < kMinInactiveSNR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100221 RTC_LOG(LS_INFO) << "Intelligibility Enhancer was activated at chunk "
222 << num_chunks_;
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700223 is_active_ = true;
224 }
aluebs2fae89e2016-04-13 11:24:06 -0700225 }
226}
227
aluebsf99af6b2016-02-24 17:25:42 -0800228void IntelligibilityEnhancer::SolveForLambda(float power_target) {
ekmdb4fecf2015-06-22 17:49:08 -0700229 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values
230 const int kMaxIters = 100; // for these, based on experiments.
ekm35b72fb2015-07-10 14:11:52 -0700231
Alejandro Luebs66d24812016-02-11 10:37:12 -0800232 const float reciprocal_power_target =
233 1.f / (power_target + std::numeric_limits<float>::epsilon());
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700234 float lambda_bot = kLambdaBot;
235 float lambda_top = kLambdaTop;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800236 float power_ratio = 2.f; // Ratio of achieved power to target power.
ekm030249d2015-06-15 13:02:24 -0700237 int iters = 0;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800238 while (std::fabs(power_ratio - 1.f) > kConvergeThresh && iters <= kMaxIters) {
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700239 const float lambda = (lambda_bot + lambda_top) / 2.f;
aluebs0a007592016-02-26 17:17:38 -0800240 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.data());
ekm35b72fb2015-07-10 14:11:52 -0700241 const float power =
aluebs0a007592016-02-26 17:17:38 -0800242 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
ekm030249d2015-06-15 13:02:24 -0700243 if (power < power_target) {
244 lambda_bot = lambda;
245 } else {
246 lambda_top = lambda;
247 }
ekm35b72fb2015-07-10 14:11:52 -0700248 power_ratio = std::fabs(power * reciprocal_power_target);
ekm030249d2015-06-15 13:02:24 -0700249 ++iters;
250 }
ekm35b72fb2015-07-10 14:11:52 -0700251}
ekm030249d2015-06-15 13:02:24 -0700252
ekm35b72fb2015-07-10 14:11:52 -0700253void IntelligibilityEnhancer::UpdateErbGains() {
ekmdb4fecf2015-06-22 17:49:08 -0700254 // (ERB gain) = filterbank' * (freq gain)
ekm030249d2015-06-15 13:02:24 -0700255 float* gains = gain_applier_.target();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700256 for (size_t i = 0; i < freqs_; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800257 gains[i] = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700258 for (size_t j = 0; j < bank_size_; ++j) {
aluebsa2abdf22016-03-02 18:36:49 -0800259 gains[i] += render_filter_bank_[j][i] * gains_eq_[j];
ekm030249d2015-06-15 13:02:24 -0700260 }
261 }
262}
263
Peter Kastingdce40cf2015-08-24 14:52:23 -0700264size_t IntelligibilityEnhancer::GetBankSize(int sample_rate,
265 size_t erb_resolution) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800266 float freq_limit = sample_rate / 2000.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700267 size_t erb_scale = static_cast<size_t>(ceilf(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800268 11.17f * logf((freq_limit + 0.312f) / (freq_limit + 14.6575f)) + 43.f));
ekm030249d2015-06-15 13:02:24 -0700269 return erb_scale * erb_resolution;
270}
271
aluebsc466bad2016-02-10 12:03:00 -0800272std::vector<std::vector<float>> IntelligibilityEnhancer::CreateErbBank(
273 size_t num_freqs) {
274 std::vector<std::vector<float>> filter_bank(bank_size_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700275 size_t lf = 1, rf = 4;
ekm030249d2015-06-15 13:02:24 -0700276
Peter Kastingdce40cf2015-08-24 14:52:23 -0700277 for (size_t i = 0; i < bank_size_; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800278 float abs_temp = fabsf((i + 1.f) / static_cast<float>(kErbResolution));
ekm030249d2015-06-15 13:02:24 -0700279 center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp));
280 center_freqs_[i] -= 14678.49f;
281 }
282 float last_center_freq = center_freqs_[bank_size_ - 1];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700283 for (size_t i = 0; i < bank_size_; ++i) {
ekm030249d2015-06-15 13:02:24 -0700284 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq;
285 }
286
Peter Kastingdce40cf2015-08-24 14:52:23 -0700287 for (size_t i = 0; i < bank_size_; ++i) {
aluebsc466bad2016-02-10 12:03:00 -0800288 filter_bank[i].resize(num_freqs);
ekm030249d2015-06-15 13:02:24 -0700289 }
290
Peter Kastingdce40cf2015-08-24 14:52:23 -0700291 for (size_t i = 1; i <= bank_size_; ++i) {
kwiberg07038562017-06-12 11:40:47 -0700292 size_t lll = static_cast<size_t>(
293 round(center_freqs_[rtc::SafeMax<size_t>(1, i - lf) - 1] * num_freqs /
294 (0.5f * sample_rate_hz_)));
295 size_t ll = static_cast<size_t>(
296 round(center_freqs_[rtc::SafeMax<size_t>(1, i) - 1] * num_freqs /
297 (0.5f * sample_rate_hz_)));
298 lll = rtc::SafeClamp<size_t>(lll, 1, num_freqs) - 1;
299 ll = rtc::SafeClamp<size_t>(ll, 1, num_freqs) - 1;
ekm030249d2015-06-15 13:02:24 -0700300
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800301 size_t rrr = static_cast<size_t>(
kwiberg07038562017-06-12 11:40:47 -0700302 round(center_freqs_[rtc::SafeMin<size_t>(bank_size_, i + rf) - 1] *
303 num_freqs / (0.5f * sample_rate_hz_)));
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800304 size_t rr = static_cast<size_t>(
kwiberg07038562017-06-12 11:40:47 -0700305 round(center_freqs_[rtc::SafeMin<size_t>(bank_size_, i + 1) - 1] *
306 num_freqs / (0.5f * sample_rate_hz_)));
307 rrr = rtc::SafeClamp<size_t>(rrr, 1, num_freqs) - 1;
308 rr = rtc::SafeClamp<size_t>(rr, 1, num_freqs) - 1;
ekm030249d2015-06-15 13:02:24 -0700309
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800310 float step = ll == lll ? 0.f : 1.f / (ll - lll);
311 float element = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700312 for (size_t j = lll; j <= ll; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800313 filter_bank[i - 1][j] = element;
ekm030249d2015-06-15 13:02:24 -0700314 element += step;
315 }
Alejandro Luebs66d24812016-02-11 10:37:12 -0800316 step = rr == rrr ? 0.f : 1.f / (rrr - rr);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800317 element = 1.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700318 for (size_t j = rr; j <= rrr; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800319 filter_bank[i - 1][j] = element;
ekm030249d2015-06-15 13:02:24 -0700320 element -= step;
321 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700322 for (size_t j = ll; j <= rr; ++j) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800323 filter_bank[i - 1][j] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700324 }
325 }
326
aluebsc466bad2016-02-10 12:03:00 -0800327 for (size_t i = 0; i < num_freqs; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800328 float sum = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700329 for (size_t j = 0; j < bank_size_; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800330 sum += filter_bank[j][i];
ekm030249d2015-06-15 13:02:24 -0700331 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700332 for (size_t j = 0; j < bank_size_; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800333 filter_bank[j][i] /= sum;
ekm030249d2015-06-15 13:02:24 -0700334 }
335 }
aluebsc466bad2016-02-10 12:03:00 -0800336 return filter_bank;
ekm030249d2015-06-15 13:02:24 -0700337}
338
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700339void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700340 size_t start_freq,
ekmdb4fecf2015-06-22 17:49:08 -0700341 float* sols) {
aluebsf99af6b2016-02-24 17:25:42 -0800342 const float kMinPower = 1e-5f;
343
aluebs0a007592016-02-26 17:17:38 -0800344 const float* pow_x0 = filtered_clear_pow_.data();
345 const float* pow_n0 = filtered_noise_pow_.data();
ekm030249d2015-06-15 13:02:24 -0700346
Peter Kastingdce40cf2015-08-24 14:52:23 -0700347 for (size_t n = 0; n < start_freq; ++n) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800348 sols[n] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700349 }
ekmdb4fecf2015-06-22 17:49:08 -0700350
351 // Analytic solution for optimal gains. See paper for derivation.
aluebsf99af6b2016-02-24 17:25:42 -0800352 for (size_t n = start_freq; n < bank_size_; ++n) {
353 if (pow_x0[n] < kMinPower || pow_n0[n] < kMinPower) {
354 sols[n] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700355 } else {
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700356 const float gamma0 = 0.5f * kRho * pow_x0[n] * pow_n0[n] +
aluebsf99af6b2016-02-24 17:25:42 -0800357 lambda * pow_x0[n] * pow_n0[n] * pow_n0[n];
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700358 const float beta0 =
359 lambda * pow_x0[n] * (2.f - kRho) * pow_x0[n] * pow_n0[n];
360 const float alpha0 =
361 lambda * pow_x0[n] * (1.f - kRho) * pow_x0[n] * pow_x0[n];
362 RTC_DCHECK_LT(alpha0, 0.f);
aluebsf99af6b2016-02-24 17:25:42 -0800363 // The quadratic equation should always have real roots, but to guard
364 // against numerical errors we limit it to a minimum of zero.
365 sols[n] = std::max(
Alejandro Luebsdd56fa82016-03-29 13:05:40 -0700366 0.f, (-beta0 - std::sqrt(std::max(
367 0.f, beta0 * beta0 - 4.f * alpha0 * gamma0))) /
368 (2.f * alpha0));
ekm030249d2015-06-15 13:02:24 -0700369 }
ekm030249d2015-06-15 13:02:24 -0700370 }
371}
372
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800373bool IntelligibilityEnhancer::IsSpeech(const float* audio) {
aluebs0a007592016-02-26 17:17:38 -0800374 FloatToS16(audio, chunk_length_, audio_s16_.data());
375 vad_.ProcessChunk(audio_s16_.data(), chunk_length_, sample_rate_hz_);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800376 if (vad_.last_voice_probability() > kVoiceProbabilityThreshold) {
377 chunks_since_voice_ = 0;
378 } else if (chunks_since_voice_ < kSpeechOffsetDelay) {
379 ++chunks_since_voice_;
380 }
381 return chunks_since_voice_ < kSpeechOffsetDelay;
ekmeyerson60d9b332015-08-14 10:35:55 -0700382}
383
Alejandro Luebsef009252016-09-20 14:51:56 -0700384void IntelligibilityEnhancer::DelayHighBands(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -0800385 RTC_DCHECK_EQ(audio->num_bands(), high_bands_buffers_.size() + 1);
Alejandro Luebsef009252016-09-20 14:51:56 -0700386 for (size_t i = 0u; i < high_bands_buffers_.size(); ++i) {
387 Band band = static_cast<Band>(i + 1);
388 high_bands_buffers_[i]->Delay(audio->split_channels_f(band), chunk_length_);
389 }
390}
391
ekm030249d2015-06-15 13:02:24 -0700392} // namespace webrtc