| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // |
| 12 // Implements core class for intelligibility enhancer. |
| 13 // |
| 14 // Details of the model and algorithm can be found in the original paper: |
| 15 // http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788 |
| 16 // |
| 17 |
| 11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" | 18 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" |
| 12 | 19 |
| 13 #include <cmath> | 20 #include <cmath> |
| 14 #include <cstdlib> | 21 #include <cstdlib> |
| 15 | 22 |
| 16 #include <algorithm> | 23 #include <algorithm> |
| 24 #include <numeric> |
| 17 | 25 |
| 18 #include "webrtc/base/checks.h" | 26 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/common_audio/vad/include/webrtc_vad.h" | 27 #include "webrtc/common_audio/vad/include/webrtc_vad.h" |
| 20 #include "webrtc/common_audio/window_generator.h" | 28 #include "webrtc/common_audio/window_generator.h" |
| 21 | 29 |
| 22 using std::complex; | 30 using std::complex; |
| 23 using std::max; | 31 using std::max; |
| 24 using std::min; | 32 using std::min; |
| 25 | 33 |
| 26 namespace webrtc { | 34 namespace webrtc { |
| 27 | 35 |
| 28 const int IntelligibilityEnhancer::kErbResolution = 2; | 36 const int IntelligibilityEnhancer::kErbResolution = 2; |
| 29 const int IntelligibilityEnhancer::kWindowSizeMs = 2; | 37 const int IntelligibilityEnhancer::kWindowSizeMs = 2; |
| 30 // The size of the chunk provided by APM, in milliseconds. | 38 const int IntelligibilityEnhancer::kChunkSizeMs = 10; // Size provided by APM. |
| 31 const int IntelligibilityEnhancer::kChunkSizeMs = 10; | |
| 32 const int IntelligibilityEnhancer::kAnalyzeRate = 800; | 39 const int IntelligibilityEnhancer::kAnalyzeRate = 800; |
| 33 const int IntelligibilityEnhancer::kVarianceRate = 2; | 40 const int IntelligibilityEnhancer::kVarianceRate = 2; |
| 34 const float IntelligibilityEnhancer::kClipFreq = 200.0f; | 41 const float IntelligibilityEnhancer::kClipFreq = 200.0f; |
| 35 const float IntelligibilityEnhancer::kConfigRho = 0.02f; | 42 const float IntelligibilityEnhancer::kConfigRho = 0.02f; |
| 36 const float IntelligibilityEnhancer::kKbdAlpha = 1.5f; | 43 const float IntelligibilityEnhancer::kKbdAlpha = 1.5f; |
| 44 |
| 45 // To disable gain update smoothing, set gain limit to be VERY high. |
| 46 // TODO(ekmeyerson): Add option to disable gain smoothing altogether |
| 47 // to avoid the extra computation. |
| 37 const float IntelligibilityEnhancer::kGainChangeLimit = 0.0125f; | 48 const float IntelligibilityEnhancer::kGainChangeLimit = 0.0125f; |
| 38 | 49 |
| 39 using VarianceType = intelligibility::VarianceArray::StepType; | 50 using VarianceType = intelligibility::VarianceArray::StepType; |
| 40 | 51 |
| 41 IntelligibilityEnhancer::TransformCallback::TransformCallback( | 52 IntelligibilityEnhancer::TransformCallback::TransformCallback( |
| 42 IntelligibilityEnhancer* parent, | 53 IntelligibilityEnhancer* parent, |
| 43 IntelligibilityEnhancer::AudioSource source) | 54 IntelligibilityEnhancer::AudioSource source) |
| 44 : parent_(parent), | 55 : parent_(parent), source_(source) { |
| 45 source_(source) {} | 56 } |
| 46 | 57 |
| 47 void IntelligibilityEnhancer::TransformCallback::ProcessAudioBlock( | 58 void IntelligibilityEnhancer::TransformCallback::ProcessAudioBlock( |
| 48 const complex<float>* const* in_block, | 59 const complex<float>* const* in_block, |
| 49 int in_channels, int frames, int /* out_channels */, | 60 int in_channels, |
| 61 int frames, |
| 62 int /* out_channels */, |
| 50 complex<float>* const* out_block) { | 63 complex<float>* const* out_block) { |
| 51 DCHECK_EQ(parent_->freqs_, frames); | 64 DCHECK_EQ(parent_->freqs_, frames); |
| 52 for (int i = 0; i < in_channels; ++i) { | 65 for (int i = 0; i < in_channels; ++i) { |
| 53 parent_->DispatchAudio(source_, in_block[i], out_block[i]); | 66 parent_->DispatchAudio(source_, in_block[i], out_block[i]); |
| 54 } | 67 } |
| 55 } | 68 } |
| 56 | 69 |
| 57 IntelligibilityEnhancer::IntelligibilityEnhancer(int erb_resolution, | 70 IntelligibilityEnhancer::IntelligibilityEnhancer(int erb_resolution, |
| 58 int sample_rate_hz, | 71 int sample_rate_hz, |
| 59 int channels, | 72 int channels, |
| 60 int cv_type, float cv_alpha, | 73 int cv_type, |
| 74 float cv_alpha, |
| 61 int cv_win, | 75 int cv_win, |
| 62 int analysis_rate, | 76 int analysis_rate, |
| 63 int variance_rate, | 77 int variance_rate, |
| 64 float gain_limit) | 78 float gain_limit) |
| 65 : freqs_(RealFourier::ComplexLength(RealFourier::FftOrder( | 79 : freqs_(RealFourier::ComplexLength( |
| 66 sample_rate_hz * kWindowSizeMs / 1000))), | 80 RealFourier::FftOrder(sample_rate_hz * kWindowSizeMs / 1000))), |
| 67 window_size_(1 << RealFourier::FftOrder(freqs_)), | 81 window_size_(1 << RealFourier::FftOrder(freqs_)), |
| 68 chunk_length_(sample_rate_hz * kChunkSizeMs / 1000), | 82 chunk_length_(sample_rate_hz * kChunkSizeMs / 1000), |
| 69 bank_size_(GetBankSize(sample_rate_hz, erb_resolution)), | 83 bank_size_(GetBankSize(sample_rate_hz, erb_resolution)), |
| 70 sample_rate_hz_(sample_rate_hz), | 84 sample_rate_hz_(sample_rate_hz), |
| 71 erb_resolution_(erb_resolution), | 85 erb_resolution_(erb_resolution), |
| 72 channels_(channels), | 86 channels_(channels), |
| 73 analysis_rate_(analysis_rate), | 87 analysis_rate_(analysis_rate), |
| 74 variance_rate_(variance_rate), | 88 variance_rate_(variance_rate), |
| 75 clear_variance_(freqs_, static_cast<VarianceType>(cv_type), cv_win, | 89 clear_variance_(freqs_, |
| 90 static_cast<VarianceType>(cv_type), |
| 91 cv_win, |
| 76 cv_alpha), | 92 cv_alpha), |
| 77 noise_variance_(freqs_, VarianceType::kStepInfinite, 475, 0.01f), | 93 noise_variance_(freqs_, VarianceType::kStepInfinite, 475, 0.01f), |
| 78 filtered_clear_var_(new float[bank_size_]), | 94 filtered_clear_var_(new float[bank_size_]), |
| 79 filtered_noise_var_(new float[bank_size_]), | 95 filtered_noise_var_(new float[bank_size_]), |
| 80 filter_bank_(nullptr), | 96 filter_bank_(nullptr), |
| 81 center_freqs_(new float[bank_size_]), | 97 center_freqs_(new float[bank_size_]), |
| 82 rho_(new float[bank_size_]), | 98 rho_(new float[bank_size_]), |
| 83 gains_eq_(new float[bank_size_]), | 99 gains_eq_(new float[bank_size_]), |
| 84 gain_applier_(freqs_, gain_limit), | 100 gain_applier_(freqs_, gain_limit), |
| 85 temp_out_buffer_(nullptr), | 101 temp_out_buffer_(nullptr), |
| 86 input_audio_(new float*[channels]), | 102 input_audio_(new float* [channels]), |
| 87 kbd_window_(new float[window_size_]), | 103 kbd_window_(new float[window_size_]), |
| 88 render_callback_(this, AudioSource::kRenderStream), | 104 render_callback_(this, AudioSource::kRenderStream), |
| 89 capture_callback_(this, AudioSource::kCaptureStream), | 105 capture_callback_(this, AudioSource::kCaptureStream), |
| 90 block_count_(0), | 106 block_count_(0), |
| 91 analysis_step_(0), | 107 analysis_step_(0), |
| 92 vad_high_(nullptr), | 108 vad_high_(WebRtcVad_Create()), |
| 93 vad_low_(nullptr), | 109 vad_low_(WebRtcVad_Create()), |
| 94 vad_tmp_buffer_(new int16_t[chunk_length_]) { | 110 vad_tmp_buffer_(new int16_t[chunk_length_]) { |
| 95 DCHECK_LE(kConfigRho, 1.0f); | 111 DCHECK_LE(kConfigRho, 1.0f); |
| 96 | 112 |
| 97 CreateErbBank(); | 113 CreateErbBank(); |
| 98 | 114 |
| 99 WebRtcVad_Create(&vad_high_); | |
| 100 WebRtcVad_Init(vad_high_); | 115 WebRtcVad_Init(vad_high_); |
| 101 WebRtcVad_set_mode(vad_high_, 0); // high likelihood of speech | 116 WebRtcVad_set_mode(vad_high_, 0); // High likelihood of speech. |
| 102 WebRtcVad_Create(&vad_low_); | |
| 103 WebRtcVad_Init(vad_low_); | 117 WebRtcVad_Init(vad_low_); |
| 104 WebRtcVad_set_mode(vad_low_, 3); // low likelihood of speech | 118 WebRtcVad_set_mode(vad_low_, 3); // Low likelihood of speech. |
| 105 | 119 |
| 106 temp_out_buffer_ = static_cast<float**>(malloc( | 120 temp_out_buffer_ = static_cast<float**>( |
| 107 sizeof(*temp_out_buffer_) * channels_ + | 121 malloc(sizeof(*temp_out_buffer_) * channels_ + |
| 108 sizeof(**temp_out_buffer_) * chunk_length_ * channels_)); | 122 sizeof(**temp_out_buffer_) * chunk_length_ * channels_)); |
| 109 for (int i = 0; i < channels_; ++i) { | 123 for (int i = 0; i < channels_; ++i) { |
| 110 temp_out_buffer_[i] = reinterpret_cast<float*>(temp_out_buffer_ + channels_) | 124 temp_out_buffer_[i] = |
| 111 + chunk_length_ * i; | 125 reinterpret_cast<float*>(temp_out_buffer_ + channels_) + |
| 126 chunk_length_ * i; |
| 112 } | 127 } |
| 113 | 128 |
| 129 // Assumes all rho equal. |
| 114 for (int i = 0; i < bank_size_; ++i) { | 130 for (int i = 0; i < bank_size_; ++i) { |
| 115 rho_[i] = kConfigRho * kConfigRho; | 131 rho_[i] = kConfigRho * kConfigRho; |
| 116 } | 132 } |
| 117 | 133 |
| 118 float freqs_khz = kClipFreq / 1000.0f; | 134 float freqs_khz = kClipFreq / 1000.0f; |
| 119 int erb_index = static_cast<int>(ceilf(11.17f * logf((freqs_khz + 0.312f) / | 135 int erb_index = static_cast<int>(ceilf( |
| 120 (freqs_khz + 14.6575f)) | 136 11.17f * logf((freqs_khz + 0.312f) / (freqs_khz + 14.6575f)) + 43.0f)); |
| 121 + 43.0f)); | |
| 122 start_freq_ = max(1, erb_index * kErbResolution); | 137 start_freq_ = max(1, erb_index * kErbResolution); |
| 123 | 138 |
| 124 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size_, | 139 WindowGenerator::KaiserBesselDerived(kKbdAlpha, window_size_, |
| 125 kbd_window_.get()); | 140 kbd_window_.get()); |
| 126 render_mangler_.reset(new LappedTransform(channels_, channels_, | 141 render_mangler_.reset(new LappedTransform( |
| 127 chunk_length_, | 142 channels_, channels_, chunk_length_, kbd_window_.get(), window_size_, |
| 128 kbd_window_.get(), | 143 window_size_ / 2, &render_callback_)); |
| 129 window_size_, | 144 capture_mangler_.reset(new LappedTransform( |
| 130 window_size_ / 2, | 145 channels_, channels_, chunk_length_, kbd_window_.get(), window_size_, |
| 131 &render_callback_)); | 146 window_size_ / 2, &capture_callback_)); |
| 132 capture_mangler_.reset(new LappedTransform(channels_, channels_, | |
| 133 chunk_length_, | |
| 134 kbd_window_.get(), | |
| 135 window_size_, | |
| 136 window_size_ / 2, | |
| 137 &capture_callback_)); | |
| 138 } | 147 } |
| 139 | 148 |
| 140 IntelligibilityEnhancer::~IntelligibilityEnhancer() { | 149 IntelligibilityEnhancer::~IntelligibilityEnhancer() { |
| 141 WebRtcVad_Free(vad_low_); | 150 WebRtcVad_Free(vad_low_); |
| 142 WebRtcVad_Free(vad_high_); | 151 WebRtcVad_Free(vad_high_); |
| 143 free(filter_bank_); | 152 free(filter_bank_); |
| 144 } | 153 } |
| 145 | 154 |
| 146 void IntelligibilityEnhancer::ProcessRenderAudio(float* const* audio) { | 155 void IntelligibilityEnhancer::ProcessRenderAudio(float* const* audio) { |
| 147 for (int i = 0; i < chunk_length_; ++i) { | 156 for (int i = 0; i < chunk_length_; ++i) { |
| 148 vad_tmp_buffer_[i] = (int16_t)audio[0][i]; | 157 vad_tmp_buffer_[i] = (int16_t)audio[0][i]; |
| 149 } | 158 } |
| 150 has_voice_low_ = WebRtcVad_Process(vad_low_, sample_rate_hz_, | 159 has_voice_low_ = WebRtcVad_Process(vad_low_, sample_rate_hz_, |
| 151 vad_tmp_buffer_.get(), chunk_length_) == 1; | 160 vad_tmp_buffer_.get(), chunk_length_) == 1; |
| 152 | 161 |
| 162 // Process and enhance chunk of |audio| |
| 153 render_mangler_->ProcessChunk(audio, temp_out_buffer_); | 163 render_mangler_->ProcessChunk(audio, temp_out_buffer_); |
| 164 |
| 154 for (int i = 0; i < channels_; ++i) { | 165 for (int i = 0; i < channels_; ++i) { |
| 155 memcpy(audio[i], temp_out_buffer_[i], | 166 memcpy(audio[i], temp_out_buffer_[i], |
| 156 chunk_length_ * sizeof(**temp_out_buffer_)); | 167 chunk_length_ * sizeof(**temp_out_buffer_)); |
| 157 } | 168 } |
| 158 } | 169 } |
| 159 | 170 |
| 160 void IntelligibilityEnhancer::ProcessCaptureAudio(float* const* audio) { | 171 void IntelligibilityEnhancer::ProcessCaptureAudio(float* const* audio) { |
| 161 for (int i = 0; i < chunk_length_; ++i) { | 172 for (int i = 0; i < chunk_length_; ++i) { |
| 162 vad_tmp_buffer_[i] = (int16_t)audio[0][i]; | 173 vad_tmp_buffer_[i] = (int16_t)audio[0][i]; |
| 163 } | 174 } |
| 164 // TODO(bercic): the VAD was always detecting voice in the noise stream, | 175 // TODO(bercic): The VAD was always detecting voice in the noise stream, |
| 165 // no matter what the aggressiveness, so it was temporarily disabled here | 176 // no matter what the aggressiveness, so it was temporarily disabled here. |
| 166 | 177 |
| 167 //if (WebRtcVad_Process(vad_high_, sample_rate_hz_, vad_tmp_buffer_.get(), | 178 #if 0 |
| 168 // chunk_length_) == 1) { | 179 if (WebRtcVad_Process(vad_high_, sample_rate_hz_, vad_tmp_buffer_.get(), |
| 169 // printf("capture HAS speech\n"); | 180 chunk_length_) == 1) { |
| 170 // return; | 181 printf("capture HAS speech\n"); |
| 171 //} | 182 return; |
| 172 //printf("capture NO speech\n"); | 183 } |
| 184 printf("capture NO speech\n"); |
| 185 #endif |
| 186 |
| 173 capture_mangler_->ProcessChunk(audio, temp_out_buffer_); | 187 capture_mangler_->ProcessChunk(audio, temp_out_buffer_); |
| 174 } | 188 } |
| 175 | 189 |
| 176 void IntelligibilityEnhancer::DispatchAudio( | 190 void IntelligibilityEnhancer::DispatchAudio( |
| 177 IntelligibilityEnhancer::AudioSource source, | 191 IntelligibilityEnhancer::AudioSource source, |
| 178 const complex<float>* in_block, complex<float>* out_block) { | 192 const complex<float>* in_block, |
| 193 complex<float>* out_block) { |
| 179 switch (source) { | 194 switch (source) { |
| 180 case kRenderStream: | 195 case kRenderStream: |
| 181 ProcessClearBlock(in_block, out_block); | 196 ProcessClearBlock(in_block, out_block); |
| 182 break; | 197 break; |
| 183 case kCaptureStream: | 198 case kCaptureStream: |
| 184 ProcessNoiseBlock(in_block, out_block); | 199 ProcessNoiseBlock(in_block, out_block); |
| 185 break; | 200 break; |
| 186 } | 201 } |
| 187 } | 202 } |
| 188 | 203 |
| 189 void IntelligibilityEnhancer::ProcessClearBlock(const complex<float>* in_block, | 204 void IntelligibilityEnhancer::ProcessClearBlock(const complex<float>* in_block, |
| 190 complex<float>* out_block) { | 205 complex<float>* out_block) { |
| 191 float power_target; | 206 float power_target; |
| 192 | 207 |
| 193 if (block_count_ < 2) { | 208 if (block_count_ < 2) { |
| 194 memset(out_block, 0, freqs_ * sizeof(*out_block)); | 209 memset(out_block, 0, freqs_ * sizeof(*out_block)); |
| 195 ++block_count_; | 210 ++block_count_; |
| 196 return; | 211 return; |
| 197 } | 212 } |
| 198 | 213 |
| 214 // For now, always assumes enhancement is necessary. |
| 215 // TODO(ekmeyerson): Change to only enhance if necessary, |
| 216 // based on experiments with different cutoffs. |
| 199 if (has_voice_low_ || true) { | 217 if (has_voice_low_ || true) { |
| 200 clear_variance_.Step(in_block, false); | 218 clear_variance_.Step(in_block, false); |
| 201 power_target = std::accumulate(clear_variance_.variance(), | 219 power_target = std::accumulate(clear_variance_.variance(), |
| 202 clear_variance_.variance() + freqs_, 0.0f); | 220 clear_variance_.variance() + freqs_, 0.0f); |
| 203 | 221 |
| 204 if (block_count_ % analysis_rate_ == analysis_rate_ - 1) { | 222 if (block_count_ % analysis_rate_ == analysis_rate_ - 1) { |
| 205 AnalyzeClearBlock(power_target); | 223 AnalyzeClearBlock(power_target); |
| 206 ++analysis_step_; | 224 ++analysis_step_; |
| 207 if (analysis_step_ == variance_rate_) { | 225 if (analysis_step_ == variance_rate_) { |
| 208 analysis_step_ = 0; | 226 analysis_step_ = 0; |
| 209 clear_variance_.Clear(); | 227 clear_variance_.Clear(); |
| 210 noise_variance_.Clear(); | 228 noise_variance_.Clear(); |
| 211 } | 229 } |
| 212 } | 230 } |
| 213 ++block_count_; | 231 ++block_count_; |
| 214 } | 232 } |
| 215 | 233 |
| 216 /* efidata(n,:) = sqrt(b(n)) * fidata(n,:) */ | 234 /* efidata(n,:) = sqrt(b(n)) * fidata(n,:) */ |
| 217 gain_applier_.Apply(in_block, out_block); | 235 gain_applier_.Apply(in_block, out_block); |
| 218 } | 236 } |
| 219 | 237 |
| 220 void IntelligibilityEnhancer::AnalyzeClearBlock(float power_target) { | 238 void IntelligibilityEnhancer::AnalyzeClearBlock(float power_target) { |
| 221 FilterVariance(clear_variance_.variance(), filtered_clear_var_.get()); | 239 FilterVariance(clear_variance_.variance(), filtered_clear_var_.get()); |
| 222 FilterVariance(noise_variance_.variance(), filtered_noise_var_.get()); | 240 FilterVariance(noise_variance_.variance(), filtered_noise_var_.get()); |
| 223 | 241 |
| 224 /* lambda binary search */ | 242 // Bisection search for optimal |lambda| |
| 225 | 243 |
| 226 float lambda_bot = -1.0f, lambda_top = -10e-18f, lambda; | 244 float lambda_bot = -1.0f, lambda_top = -10e-18f, lambda; |
| 227 float power_bot, power_top, power; | 245 float power_bot, power_top, power; |
| 228 SolveEquation14(lambda_top, start_freq_, gains_eq_.get()); | 246 SolveForGainsGivenLambda(lambda_top, start_freq_, gains_eq_.get()); |
| 229 power_top = DotProduct(gains_eq_.get(), filtered_clear_var_.get(), | 247 power_top = |
| 230 bank_size_); | 248 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| 231 SolveEquation14(lambda_bot, start_freq_, gains_eq_.get()); | 249 SolveForGainsGivenLambda(lambda_bot, start_freq_, gains_eq_.get()); |
| 232 power_bot = DotProduct(gains_eq_.get(), filtered_clear_var_.get(), | 250 power_bot = |
| 233 bank_size_); | 251 DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| 234 DCHECK(power_target >= power_bot && power_target <= power_top); | 252 DCHECK(power_target >= power_bot && power_target <= power_top); |
| 235 | 253 |
| 236 float power_ratio = 2.0f; | 254 float power_ratio = 2.0f; // Ratio of achieved power to target power. |
| 255 const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values |
| 256 const int kMaxIters = 100; // for these, based on experiments. |
| 237 int iters = 0; | 257 int iters = 0; |
| 238 while (fabs(power_ratio - 1.0f) > 0.001f && iters <= 100) { | 258 while (fabs(power_ratio - 1.0f) > kConvergeThresh && iters <= kMaxIters) { |
| 239 lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; | 259 lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; |
| 240 SolveEquation14(lambda, start_freq_, gains_eq_.get()); | 260 SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.get()); |
| 241 power = DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); | 261 power = DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| 242 if (power < power_target) { | 262 if (power < power_target) { |
| 243 lambda_bot = lambda; | 263 lambda_bot = lambda; |
| 244 } else { | 264 } else { |
| 245 lambda_top = lambda; | 265 lambda_top = lambda; |
| 246 } | 266 } |
| 247 power_ratio = fabs(power / power_target); | 267 power_ratio = fabs(power / power_target); |
| 248 ++iters; | 268 ++iters; |
| 249 } | 269 } |
| 250 | 270 |
| 251 /* b = filterbank' * b */ | 271 // (ERB gain) = filterbank' * (freq gain) |
| 252 float* gains = gain_applier_.target(); | 272 float* gains = gain_applier_.target(); |
| 253 for (int i = 0; i < freqs_; ++i) { | 273 for (int i = 0; i < freqs_; ++i) { |
| 254 gains[i] = 0.0f; | 274 gains[i] = 0.0f; |
| 255 for (int j = 0; j < bank_size_; ++j) { | 275 for (int j = 0; j < bank_size_; ++j) { |
| 256 gains[i] = fmaf(filter_bank_[j][i], gains_eq_[j], gains[i]); | 276 gains[i] = fmaf(filter_bank_[j][i], gains_eq_[j], gains[i]); |
| 257 } | 277 } |
| 258 } | 278 } |
| 259 } | 279 } |
| 260 | 280 |
| 261 void IntelligibilityEnhancer::ProcessNoiseBlock(const complex<float>* in_block, | 281 void IntelligibilityEnhancer::ProcessNoiseBlock(const complex<float>* in_block, |
| 262 complex<float>* /*out_block*/) { | 282 complex<float>* /*out_block*/) { |
| 263 noise_variance_.Step(in_block); | 283 noise_variance_.Step(in_block); |
| 264 } | 284 } |
| 265 | 285 |
| 266 int IntelligibilityEnhancer::GetBankSize(int sample_rate, int erb_resolution) { | 286 int IntelligibilityEnhancer::GetBankSize(int sample_rate, int erb_resolution) { |
| 267 float freq_limit = sample_rate / 2000.0f; | 287 float freq_limit = sample_rate / 2000.0f; |
| 268 int erb_scale = ceilf(11.17f * logf((freq_limit + 0.312f) / | 288 int erb_scale = ceilf( |
| 269 (freq_limit + 14.6575f)) + 43.0f); | 289 11.17f * logf((freq_limit + 0.312f) / (freq_limit + 14.6575f)) + 43.0f); |
| 270 return erb_scale * erb_resolution; | 290 return erb_scale * erb_resolution; |
| 271 } | 291 } |
| 272 | 292 |
| 273 void IntelligibilityEnhancer::CreateErbBank() { | 293 void IntelligibilityEnhancer::CreateErbBank() { |
| 274 int lf = 1, rf = 4; | 294 int lf = 1, rf = 4; |
| 275 | 295 |
| 276 for (int i = 0; i < bank_size_; ++i) { | 296 for (int i = 0; i < bank_size_; ++i) { |
| 277 float abs_temp = fabsf((i + 1.0f) / static_cast<float>(erb_resolution_)); | 297 float abs_temp = fabsf((i + 1.0f) / static_cast<float>(erb_resolution_)); |
| 278 center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp)); | 298 center_freqs_[i] = 676170.4f / (47.06538f - expf(0.08950404f * abs_temp)); |
| 279 center_freqs_[i] -= 14678.49f; | 299 center_freqs_[i] -= 14678.49f; |
| 280 } | 300 } |
| 281 float last_center_freq = center_freqs_[bank_size_ - 1]; | 301 float last_center_freq = center_freqs_[bank_size_ - 1]; |
| 282 for (int i = 0; i < bank_size_; ++i) { | 302 for (int i = 0; i < bank_size_; ++i) { |
| 283 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq; | 303 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq; |
| 284 } | 304 } |
| 285 | 305 |
| 286 filter_bank_ = static_cast<float**>(malloc( | 306 filter_bank_ = static_cast<float**>( |
| 287 sizeof(*filter_bank_) * bank_size_ + | 307 malloc(sizeof(*filter_bank_) * bank_size_ + |
| 288 sizeof(**filter_bank_) * freqs_ * bank_size_)); | 308 sizeof(**filter_bank_) * freqs_ * bank_size_)); |
| 289 for (int i = 0; i < bank_size_; ++i) { | 309 for (int i = 0; i < bank_size_; ++i) { |
| 290 filter_bank_[i] = reinterpret_cast<float*>(filter_bank_ + bank_size_) + | 310 filter_bank_[i] = |
| 291 freqs_ * i; | 311 reinterpret_cast<float*>(filter_bank_ + bank_size_) + freqs_ * i; |
| 292 } | 312 } |
| 293 | 313 |
| 294 for (int i = 1; i <= bank_size_; ++i) { | 314 for (int i = 1; i <= bank_size_; ++i) { |
| 295 int lll, ll, rr, rrr; | 315 int lll, ll, rr, rrr; |
| 296 lll = round(center_freqs_[max(1, i - lf) - 1] * freqs_ / | 316 lll = round(center_freqs_[max(1, i - lf) - 1] * freqs_ / |
| 297 (0.5f * sample_rate_hz_)); | 317 (0.5f * sample_rate_hz_)); |
| 298 ll = round(center_freqs_[max(1, i ) - 1] * freqs_ / | 318 ll = |
| 299 (0.5f * sample_rate_hz_)); | 319 round(center_freqs_[max(1, i) - 1] * freqs_ / (0.5f * sample_rate_hz_)); |
| 300 lll = min(freqs_, max(lll, 1)) - 1; | 320 lll = min(freqs_, max(lll, 1)) - 1; |
| 301 ll = min(freqs_, max(ll, 1)) - 1; | 321 ll = min(freqs_, max(ll, 1)) - 1; |
| 302 | 322 |
| 303 rrr = round(center_freqs_[min(bank_size_, i + rf) - 1] * freqs_ / | 323 rrr = round(center_freqs_[min(bank_size_, i + rf) - 1] * freqs_ / |
| 304 (0.5f * sample_rate_hz_)); | 324 (0.5f * sample_rate_hz_)); |
| 305 rr = round(center_freqs_[min(bank_size_, i + 1) - 1] * freqs_ / | 325 rr = round(center_freqs_[min(bank_size_, i + 1) - 1] * freqs_ / |
| 306 (0.5f * sample_rate_hz_)); | 326 (0.5f * sample_rate_hz_)); |
| 307 rrr = min(freqs_, max(rrr, 1)) - 1; | 327 rrr = min(freqs_, max(rrr, 1)) - 1; |
| 308 rr = min(freqs_, max(rr, 1)) - 1; | 328 rr = min(freqs_, max(rr, 1)) - 1; |
| 309 | 329 |
| 310 float step, element; | 330 float step, element; |
| 311 | 331 |
| 312 step = 1.0f / (ll - lll); | 332 step = 1.0f / (ll - lll); |
| 313 element = 0.0f; | 333 element = 0.0f; |
| 314 for (int j = lll; j <= ll; ++j) { | 334 for (int j = lll; j <= ll; ++j) { |
| 315 filter_bank_[i - 1][j] = element; | 335 filter_bank_[i - 1][j] = element; |
| 316 element += step; | 336 element += step; |
| 317 } | 337 } |
| 318 step = 1.0f / (rrr - rr); | 338 step = 1.0f / (rrr - rr); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 331 sum = 0.0f; | 351 sum = 0.0f; |
| 332 for (int j = 0; j < bank_size_; ++j) { | 352 for (int j = 0; j < bank_size_; ++j) { |
| 333 sum += filter_bank_[j][i]; | 353 sum += filter_bank_[j][i]; |
| 334 } | 354 } |
| 335 for (int j = 0; j < bank_size_; ++j) { | 355 for (int j = 0; j < bank_size_; ++j) { |
| 336 filter_bank_[j][i] /= sum; | 356 filter_bank_[j][i] /= sum; |
| 337 } | 357 } |
| 338 } | 358 } |
| 339 } | 359 } |
| 340 | 360 |
| 341 void IntelligibilityEnhancer::SolveEquation14(float lambda, int start_freq, | 361 void IntelligibilityEnhancer::SolveForGainsGivenLambda(float lambda, |
| 342 float* sols) { | 362 int start_freq, |
| 363 float* sols) { |
| 343 bool quadratic = (kConfigRho < 1.0f); | 364 bool quadratic = (kConfigRho < 1.0f); |
| 344 const float* var_x0 = filtered_clear_var_.get(); | 365 const float* var_x0 = filtered_clear_var_.get(); |
| 345 const float* var_n0 = filtered_noise_var_.get(); | 366 const float* var_n0 = filtered_noise_var_.get(); |
| 346 | 367 |
| 347 for (int n = 0; n < start_freq; ++n) { | 368 for (int n = 0; n < start_freq; ++n) { |
| 348 sols[n] = 1.0f; | 369 sols[n] = 1.0f; |
| 349 } | 370 } |
| 371 |
| 372 // Analytic solution for optimal gains. See paper for derivation. |
| 350 for (int n = start_freq - 1; n < bank_size_; ++n) { | 373 for (int n = start_freq - 1; n < bank_size_; ++n) { |
| 351 float alpha0, beta0, gamma0; | 374 float alpha0, beta0, gamma0; |
| 352 gamma0 = 0.5f * rho_[n] * var_x0[n] * var_n0[n] + | 375 gamma0 = 0.5f * rho_[n] * var_x0[n] * var_n0[n] + |
| 353 lambda * var_x0[n] * var_n0[n] * var_n0[n]; | 376 lambda * var_x0[n] * var_n0[n] * var_n0[n]; |
| 354 beta0 = lambda * var_x0[n] * (2 - rho_[n]) * var_x0[n] * var_n0[n]; | 377 beta0 = lambda * var_x0[n] * (2 - rho_[n]) * var_x0[n] * var_n0[n]; |
| 355 if (quadratic) { | 378 if (quadratic) { |
| 356 alpha0 = lambda * var_x0[n] * (1 - rho_[n]) * var_x0[n] * var_x0[n]; | 379 alpha0 = lambda * var_x0[n] * (1 - rho_[n]) * var_x0[n] * var_x0[n]; |
| 357 sols[n] = (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0)) | 380 sols[n] = |
| 358 / (2 * alpha0); | 381 (-beta0 - sqrtf(beta0 * beta0 - 4 * alpha0 * gamma0)) / (2 * alpha0); |
| 359 } else { | 382 } else { |
| 360 sols[n] = -gamma0 / beta0; | 383 sols[n] = -gamma0 / beta0; |
| 361 } | 384 } |
| 362 sols[n] = fmax(0, sols[n]); | 385 sols[n] = fmax(0, sols[n]); |
| 363 } | 386 } |
| 364 } | 387 } |
| 365 | 388 |
| 366 void IntelligibilityEnhancer::FilterVariance(const float* var, float* result) { | 389 void IntelligibilityEnhancer::FilterVariance(const float* var, float* result) { |
| 367 for (int i = 0; i < bank_size_; ++i) { | 390 for (int i = 0; i < bank_size_; ++i) { |
| 368 result[i] = DotProduct(filter_bank_[i], var, freqs_); | 391 result[i] = DotProduct(filter_bank_[i], var, freqs_); |
| 369 } | 392 } |
| 370 } | 393 } |
| 371 | 394 |
| 372 float IntelligibilityEnhancer::DotProduct(const float* a, const float* b, | 395 float IntelligibilityEnhancer::DotProduct(const float* a, |
| 373 int length) { | 396 const float* b, |
| 397 int length) { |
| 374 float ret = 0.0f; | 398 float ret = 0.0f; |
| 375 | 399 |
| 376 for (int i = 0; i < length; ++i) { | 400 for (int i = 0; i < length; ++i) { |
| 377 ret = fmaf(a[i], b[i], ret); | 401 ret = fmaf(a[i], b[i], ret); |
| 378 } | 402 } |
| 379 return ret; | 403 return ret; |
| 380 } | 404 } |
| 381 | 405 |
| 382 } // namespace webrtc | 406 } // namespace webrtc |
| 383 | |
| OLD | NEW |