blob: dc2dcdd2610542a69238a4317c24b7a18286f50e [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"
ekmeyerson60d9b332015-08-14 10:35:55 -070020#include "webrtc/common_audio/include/audio_util.h"
ekm030249d2015-06-15 13:02:24 -070021#include "webrtc/common_audio/window_generator.h"
22
ekm35b72fb2015-07-10 14:11:52 -070023namespace webrtc {
24
25namespace {
26
Peter Kastingdce40cf2015-08-24 14:52:23 -070027const size_t kErbResolution = 2;
Alejandro Luebs32348192016-02-17 20:04:19 -080028const int kWindowSizeMs = 16;
ekm35b72fb2015-07-10 14:11:52 -070029const int kChunkSizeMs = 10; // Size provided by APM.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080030const float kClipFreqKhz = 0.2f;
ekm35b72fb2015-07-10 14:11:52 -070031const float kKbdAlpha = 1.5f;
32const float kLambdaBot = -1.0f; // Extreme values in bisection
aluebsf99af6b2016-02-24 17:25:42 -080033const float kLambdaTop = -1e-5f; // search for lamda.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080034const float kVoiceProbabilityThreshold = 0.02f;
35// Number of chunks after voice activity which is still considered speech.
36const size_t kSpeechOffsetDelay = 80;
37const float kDecayRate = 0.98f; // Power estimation decay rate.
38const float kMaxRelativeGainChange = 0.04f; // Maximum relative change in gain.
39const float kRho = 0.0004f; // Default production and interpretation SNR.
ekm35b72fb2015-07-10 14:11:52 -070040
aluebsc466bad2016-02-10 12:03:00 -080041// Returns dot product of vectors |a| and |b| with size |length|.
42float DotProduct(const float* a, const float* b, size_t length) {
43 float ret = 0.f;
44 for (size_t i = 0; i < length; ++i) {
aluebsa2abdf22016-03-02 18:36:49 -080045 ret += a[i] * b[i];
aluebsc466bad2016-02-10 12:03:00 -080046 }
47 return ret;
48}
49
Alejandro Luebs32348192016-02-17 20:04:19 -080050// Computes the power across ERB bands from the power spectral density |pow|.
aluebsc466bad2016-02-10 12:03:00 -080051// Stores it in |result|.
Alejandro Luebs32348192016-02-17 20:04:19 -080052void MapToErbBands(const float* pow,
53 const std::vector<std::vector<float>>& filter_bank,
54 float* result) {
aluebsc466bad2016-02-10 12:03:00 -080055 for (size_t i = 0; i < filter_bank.size(); ++i) {
56 RTC_DCHECK_GT(filter_bank[i].size(), 0u);
aluebs0a007592016-02-26 17:17:38 -080057 result[i] = DotProduct(filter_bank[i].data(), pow, filter_bank[i].size());
aluebsc466bad2016-02-10 12:03:00 -080058 }
59}
60
ekm35b72fb2015-07-10 14:11:52 -070061} // namespace
62
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080063IntelligibilityEnhancer::IntelligibilityEnhancer(int sample_rate_hz,
64 size_t num_render_channels)
ekmdb4fecf2015-06-22 17:49:08 -070065 : freqs_(RealFourier::ComplexLength(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080066 RealFourier::FftOrder(sample_rate_hz * kWindowSizeMs / 1000))),
67 chunk_length_(static_cast<size_t>(sample_rate_hz * kChunkSizeMs / 1000)),
68 bank_size_(GetBankSize(sample_rate_hz, kErbResolution)),
69 sample_rate_hz_(sample_rate_hz),
70 num_render_channels_(num_render_channels),
71 clear_power_estimator_(freqs_, kDecayRate),
72 noise_power_estimator_(
73 new intelligibility::PowerEstimator<float>(freqs_, kDecayRate)),
aluebs0a007592016-02-26 17:17:38 -080074 filtered_clear_pow_(bank_size_, 0.f),
75 filtered_noise_pow_(bank_size_, 0.f),
76 center_freqs_(bank_size_),
aluebsc466bad2016-02-10 12:03:00 -080077 render_filter_bank_(CreateErbBank(freqs_)),
aluebs0a007592016-02-26 17:17:38 -080078 gains_eq_(bank_size_),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080079 gain_applier_(freqs_, kMaxRelativeGainChange),
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080080 audio_s16_(chunk_length_),
81 chunks_since_voice_(kSpeechOffsetDelay),
82 is_speech_(false) {
83 RTC_DCHECK_LE(kRho, 1.f);
ekm030249d2015-06-15 13:02:24 -070084
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080085 const size_t erb_index = static_cast<size_t>(
86 ceilf(11.17f * logf((kClipFreqKhz + 0.312f) / (kClipFreqKhz + 14.6575f)) +
87 43.f));
88 start_freq_ = std::max(static_cast<size_t>(1), erb_index * kErbResolution);
ekm030249d2015-06-15 13:02:24 -070089
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080090 size_t window_size = static_cast<size_t>(1 << RealFourier::FftOrder(freqs_));
91 std::vector<float> kbd_window(window_size);
aluebs0a007592016-02-26 17:17:38 -080092 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size,
93 kbd_window.data());
ekmdb4fecf2015-06-22 17:49:08 -070094 render_mangler_.reset(new LappedTransform(
aluebs0a007592016-02-26 17:17:38 -080095 num_render_channels_, num_render_channels_, chunk_length_,
96 kbd_window.data(), window_size, window_size / 2, this));
aluebsc466bad2016-02-10 12:03:00 -080097}
98
99void IntelligibilityEnhancer::SetCaptureNoiseEstimate(
100 std::vector<float> noise) {
101 if (capture_filter_bank_.size() != bank_size_ ||
102 capture_filter_bank_[0].size() != noise.size()) {
103 capture_filter_bank_ = CreateErbBank(noise.size());
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800104 noise_power_estimator_.reset(
105 new intelligibility::PowerEstimator<float>(noise.size(), kDecayRate));
aluebsc466bad2016-02-10 12:03:00 -0800106 }
aluebs0a007592016-02-26 17:17:38 -0800107 noise_power_estimator_->Step(noise.data());
ekm030249d2015-06-15 13:02:24 -0700108}
109
ekmeyerson60d9b332015-08-14 10:35:55 -0700110void IntelligibilityEnhancer::ProcessRenderAudio(float* const* audio,
111 int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800112 size_t num_channels) {
henrikg91d6ede2015-09-17 00:24:34 -0700113 RTC_CHECK_EQ(sample_rate_hz_, sample_rate_hz);
114 RTC_CHECK_EQ(num_render_channels_, num_channels);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800115 is_speech_ = IsSpeech(audio[0]);
aluebs0a007592016-02-26 17:17:38 -0800116 render_mangler_->ProcessChunk(audio, audio);
ekmeyerson60d9b332015-08-14 10:35:55 -0700117}
ekmdb4fecf2015-06-22 17:49:08 -0700118
aluebs0a007592016-02-26 17:17:38 -0800119void IntelligibilityEnhancer::ProcessAudioBlock(
120 const std::complex<float>* const* in_block,
121 size_t in_channels,
122 size_t frames,
123 size_t /* out_channels */,
124 std::complex<float>* const* out_block) {
125 RTC_DCHECK_EQ(freqs_, frames);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800126 if (is_speech_) {
aluebs0a007592016-02-26 17:17:38 -0800127 clear_power_estimator_.Step(in_block[0]);
ekm030249d2015-06-15 13:02:24 -0700128 }
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800129 const std::vector<float>& clear_power = clear_power_estimator_.power();
130 const std::vector<float>& noise_power = noise_power_estimator_->power();
aluebs0a007592016-02-26 17:17:38 -0800131 MapToErbBands(clear_power.data(), render_filter_bank_,
132 filtered_clear_pow_.data());
133 MapToErbBands(noise_power.data(), capture_filter_bank_,
134 filtered_noise_pow_.data());
135 SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.data());
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800136 const float power_target =
aluebs0a007592016-02-26 17:17:38 -0800137 std::accumulate(clear_power.data(), clear_power.data() + freqs_, 0.f);
ekm35b72fb2015-07-10 14:11:52 -0700138 const float power_top =
aluebs0a007592016-02-26 17:17:38 -0800139 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
140 SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.data());
ekm35b72fb2015-07-10 14:11:52 -0700141 const float power_bot =
aluebs0a007592016-02-26 17:17:38 -0800142 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
ekm35b72fb2015-07-10 14:11:52 -0700143 if (power_target >= power_bot && power_target <= power_top) {
aluebsf99af6b2016-02-24 17:25:42 -0800144 SolveForLambda(power_target);
ekm35b72fb2015-07-10 14:11:52 -0700145 UpdateErbGains();
Alejandro Luebs32348192016-02-17 20:04:19 -0800146 } // Else experiencing power underflow, so do nothing.
aluebs0a007592016-02-26 17:17:38 -0800147 for (size_t i = 0; i < in_channels; ++i) {
148 gain_applier_.Apply(in_block[i], out_block[i]);
149 }
ekm35b72fb2015-07-10 14:11:52 -0700150}
ekm030249d2015-06-15 13:02:24 -0700151
aluebsf99af6b2016-02-24 17:25:42 -0800152void IntelligibilityEnhancer::SolveForLambda(float power_target) {
ekmdb4fecf2015-06-22 17:49:08 -0700153 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values
154 const int kMaxIters = 100; // for these, based on experiments.
ekm35b72fb2015-07-10 14:11:52 -0700155
Alejandro Luebs66d24812016-02-11 10:37:12 -0800156 const float reciprocal_power_target =
157 1.f / (power_target + std::numeric_limits<float>::epsilon());
ekm35b72fb2015-07-10 14:11:52 -0700158 float lambda_bot = kLambdaBot;
159 float lambda_top = kLambdaTop;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800160 float power_ratio = 2.f; // Ratio of achieved power to target power.
ekm030249d2015-06-15 13:02:24 -0700161 int iters = 0;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800162 while (std::fabs(power_ratio - 1.f) > kConvergeThresh && iters <= kMaxIters) {
aluebsf99af6b2016-02-24 17:25:42 -0800163 const float lambda = (lambda_bot + lambda_top) / 2.f;
aluebs0a007592016-02-26 17:17:38 -0800164 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.data());
ekm35b72fb2015-07-10 14:11:52 -0700165 const float power =
aluebs0a007592016-02-26 17:17:38 -0800166 DotProduct(gains_eq_.data(), filtered_clear_pow_.data(), bank_size_);
ekm030249d2015-06-15 13:02:24 -0700167 if (power < power_target) {
168 lambda_bot = lambda;
169 } else {
170 lambda_top = lambda;
171 }
ekm35b72fb2015-07-10 14:11:52 -0700172 power_ratio = std::fabs(power * reciprocal_power_target);
ekm030249d2015-06-15 13:02:24 -0700173 ++iters;
174 }
ekm35b72fb2015-07-10 14:11:52 -0700175}
ekm030249d2015-06-15 13:02:24 -0700176
ekm35b72fb2015-07-10 14:11:52 -0700177void IntelligibilityEnhancer::UpdateErbGains() {
ekmdb4fecf2015-06-22 17:49:08 -0700178 // (ERB gain) = filterbank' * (freq gain)
ekm030249d2015-06-15 13:02:24 -0700179 float* gains = gain_applier_.target();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700180 for (size_t i = 0; i < freqs_; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800181 gains[i] = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700182 for (size_t j = 0; j < bank_size_; ++j) {
aluebsa2abdf22016-03-02 18:36:49 -0800183 gains[i] += render_filter_bank_[j][i] * gains_eq_[j];
ekm030249d2015-06-15 13:02:24 -0700184 }
185 }
186}
187
Peter Kastingdce40cf2015-08-24 14:52:23 -0700188size_t IntelligibilityEnhancer::GetBankSize(int sample_rate,
189 size_t erb_resolution) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800190 float freq_limit = sample_rate / 2000.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700191 size_t erb_scale = static_cast<size_t>(ceilf(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800192 11.17f * logf((freq_limit + 0.312f) / (freq_limit + 14.6575f)) + 43.f));
ekm030249d2015-06-15 13:02:24 -0700193 return erb_scale * erb_resolution;
194}
195
aluebsc466bad2016-02-10 12:03:00 -0800196std::vector<std::vector<float>> IntelligibilityEnhancer::CreateErbBank(
197 size_t num_freqs) {
198 std::vector<std::vector<float>> filter_bank(bank_size_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700199 size_t lf = 1, rf = 4;
ekm030249d2015-06-15 13:02:24 -0700200
Peter Kastingdce40cf2015-08-24 14:52:23 -0700201 for (size_t i = 0; i < bank_size_; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800202 float abs_temp = fabsf((i + 1.f) / static_cast<float>(kErbResolution));
ekm030249d2015-06-15 13:02:24 -0700203 center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp));
204 center_freqs_[i] -= 14678.49f;
205 }
206 float last_center_freq = center_freqs_[bank_size_ - 1];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700207 for (size_t i = 0; i < bank_size_; ++i) {
ekm030249d2015-06-15 13:02:24 -0700208 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq;
209 }
210
Peter Kastingdce40cf2015-08-24 14:52:23 -0700211 for (size_t i = 0; i < bank_size_; ++i) {
aluebsc466bad2016-02-10 12:03:00 -0800212 filter_bank[i].resize(num_freqs);
ekm030249d2015-06-15 13:02:24 -0700213 }
214
Peter Kastingdce40cf2015-08-24 14:52:23 -0700215 for (size_t i = 1; i <= bank_size_; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700216 static const size_t kOne = 1; // Avoids repeated static_cast<>s below.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800217 size_t lll =
218 static_cast<size_t>(round(center_freqs_[std::max(kOne, i - lf) - 1] *
219 num_freqs / (0.5f * sample_rate_hz_)));
220 size_t ll = static_cast<size_t>(round(center_freqs_[std::max(kOne, i) - 1] *
221 num_freqs / (0.5f * sample_rate_hz_)));
Alejandro Luebs32348192016-02-17 20:04:19 -0800222 lll = std::min(num_freqs, std::max(lll, kOne)) - 1;
223 ll = std::min(num_freqs, std::max(ll, kOne)) - 1;
ekm030249d2015-06-15 13:02:24 -0700224
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800225 size_t rrr = static_cast<size_t>(
226 round(center_freqs_[std::min(bank_size_, i + rf) - 1] * num_freqs /
227 (0.5f * sample_rate_hz_)));
228 size_t rr = static_cast<size_t>(
229 round(center_freqs_[std::min(bank_size_, i + 1) - 1] * num_freqs /
230 (0.5f * sample_rate_hz_)));
Alejandro Luebs32348192016-02-17 20:04:19 -0800231 rrr = std::min(num_freqs, std::max(rrr, kOne)) - 1;
232 rr = std::min(num_freqs, std::max(rr, kOne)) - 1;
ekm030249d2015-06-15 13:02:24 -0700233
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800234 float step = ll == lll ? 0.f : 1.f / (ll - lll);
235 float element = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700236 for (size_t j = lll; j <= ll; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800237 filter_bank[i - 1][j] = element;
ekm030249d2015-06-15 13:02:24 -0700238 element += step;
239 }
Alejandro Luebs66d24812016-02-11 10:37:12 -0800240 step = rr == rrr ? 0.f : 1.f / (rrr - rr);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800241 element = 1.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700242 for (size_t j = rr; j <= rrr; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800243 filter_bank[i - 1][j] = element;
ekm030249d2015-06-15 13:02:24 -0700244 element -= step;
245 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700246 for (size_t j = ll; j <= rr; ++j) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800247 filter_bank[i - 1][j] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700248 }
249 }
250
aluebsc466bad2016-02-10 12:03:00 -0800251 for (size_t i = 0; i < num_freqs; ++i) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800252 float sum = 0.f;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700253 for (size_t j = 0; j < bank_size_; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800254 sum += filter_bank[j][i];
ekm030249d2015-06-15 13:02:24 -0700255 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700256 for (size_t j = 0; j < bank_size_; ++j) {
aluebsc466bad2016-02-10 12:03:00 -0800257 filter_bank[j][i] /= sum;
ekm030249d2015-06-15 13:02:24 -0700258 }
259 }
aluebsc466bad2016-02-10 12:03:00 -0800260 return filter_bank;
ekm030249d2015-06-15 13:02:24 -0700261}
262
ekmdb4fecf2015-06-22 17:49:08 -0700263void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700264 size_t start_freq,
ekmdb4fecf2015-06-22 17:49:08 -0700265 float* sols) {
aluebsf99af6b2016-02-24 17:25:42 -0800266 const float kMinPower = 1e-5f;
267
aluebs0a007592016-02-26 17:17:38 -0800268 const float* pow_x0 = filtered_clear_pow_.data();
269 const float* pow_n0 = filtered_noise_pow_.data();
ekm030249d2015-06-15 13:02:24 -0700270
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 for (size_t n = 0; n < start_freq; ++n) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800272 sols[n] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700273 }
ekmdb4fecf2015-06-22 17:49:08 -0700274
275 // Analytic solution for optimal gains. See paper for derivation.
aluebsf99af6b2016-02-24 17:25:42 -0800276 for (size_t n = start_freq; n < bank_size_; ++n) {
277 if (pow_x0[n] < kMinPower || pow_n0[n] < kMinPower) {
278 sols[n] = 1.f;
ekm030249d2015-06-15 13:02:24 -0700279 } else {
aluebsf99af6b2016-02-24 17:25:42 -0800280 const float gamma0 = 0.5f * kRho * pow_x0[n] * pow_n0[n] +
281 lambda * pow_x0[n] * pow_n0[n] * pow_n0[n];
282 const float beta0 =
283 lambda * pow_x0[n] * (2.f - kRho) * pow_x0[n] * pow_n0[n];
284 const float alpha0 =
285 lambda * pow_x0[n] * (1.f - kRho) * pow_x0[n] * pow_x0[n];
286 RTC_DCHECK_LT(alpha0, 0.f);
287 // The quadratic equation should always have real roots, but to guard
288 // against numerical errors we limit it to a minimum of zero.
289 sols[n] = std::max(
290 0.f, (-beta0 - std::sqrt(std::max(
291 0.f, beta0 * beta0 - 4.f * alpha0 * gamma0))) /
292 (2.f * alpha0));
ekm030249d2015-06-15 13:02:24 -0700293 }
ekm030249d2015-06-15 13:02:24 -0700294 }
295}
296
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800297bool IntelligibilityEnhancer::IsSpeech(const float* audio) {
aluebs0a007592016-02-26 17:17:38 -0800298 FloatToS16(audio, chunk_length_, audio_s16_.data());
299 vad_.ProcessChunk(audio_s16_.data(), chunk_length_, sample_rate_hz_);
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800300 if (vad_.last_voice_probability() > kVoiceProbabilityThreshold) {
301 chunks_since_voice_ = 0;
302 } else if (chunks_since_voice_ < kSpeechOffsetDelay) {
303 ++chunks_since_voice_;
304 }
305 return chunks_since_voice_ < kSpeechOffsetDelay;
ekmeyerson60d9b332015-08-14 10:35:55 -0700306}
307
ekm030249d2015-06-15 13:02:24 -0700308} // namespace webrtc