Chromium Code Reviews| Index: webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.cc |
| diff --git a/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.cc b/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.cc |
| index 3029e21619a981917f377afb78075bfd66def952..c258886f598294df613c7f1800e12dba1ba7a649 100644 |
| --- a/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.cc |
| +++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.cc |
| @@ -27,26 +27,24 @@ |
| #include "webrtc/common_audio/vad/include/webrtc_vad.h" |
| #include "webrtc/common_audio/window_generator.h" |
| -using std::complex; |
| -using std::max; |
| -using std::min; |
| - |
| namespace webrtc { |
| -const int IntelligibilityEnhancer::kErbResolution = 2; |
| -const int IntelligibilityEnhancer::kWindowSizeMs = 2; |
| -const int IntelligibilityEnhancer::kChunkSizeMs = 10; // Size provided by APM. |
| -const int IntelligibilityEnhancer::kAnalyzeRate = 800; |
| -const int IntelligibilityEnhancer::kVarianceRate = 2; |
| -const float IntelligibilityEnhancer::kClipFreq = 200.0f; |
| -const float IntelligibilityEnhancer::kConfigRho = 0.02f; |
| -const float IntelligibilityEnhancer::kKbdAlpha = 1.5f; |
| +namespace { |
| + |
| +const int kErbResolution = 2; |
| +const int kWindowSizeMs = 2; |
| +const int kChunkSizeMs = 10; // Size provided by APM. |
| +const float kClipFreq = 200.0f; |
| +const float kConfigRho = 0.02f; // Default production and interpretation SNR. |
| +const float kKbdAlpha = 1.5f; |
| +const float kLambdaBot = -1.0; // Extreme values in bisection |
| +const float kLambdaTop = -10e-18f; // search for lamda. |
| -// To disable gain update smoothing, set gain limit to be VERY high. |
| -// TODO(ekmeyerson): Add option to disable gain smoothing altogether |
| -// to avoid the extra computation. |
| -const float IntelligibilityEnhancer::kGainChangeLimit = 0.0125f; |
| +} // namespace |
| +using std::complex; |
| +using std::max; |
| +using std::min; |
| using VarianceType = intelligibility::VarianceArray::StepType; |
| IntelligibilityEnhancer::TransformCallback::TransformCallback( |
| @@ -203,8 +201,6 @@ void IntelligibilityEnhancer::DispatchAudio( |
| void IntelligibilityEnhancer::ProcessClearBlock(const complex<float>* in_block, |
| complex<float>* out_block) { |
| - float power_target; |
| - |
| if (block_count_ < 2) { |
| memset(out_block, 0, freqs_ * sizeof(*out_block)); |
| ++block_count_; |
| @@ -216,8 +212,8 @@ void IntelligibilityEnhancer::ProcessClearBlock(const complex<float>* in_block, |
| // based on experiments with different cutoffs. |
| if (has_voice_low_ || true) { |
| clear_variance_.Step(in_block, false); |
| - power_target = std::accumulate(clear_variance_.variance(), |
| - clear_variance_.variance() + freqs_, 0.0f); |
| + const float power_target = std::accumulate( |
| + clear_variance_.variance(), clear_variance_.variance() + freqs_, 0.0f); |
| if (block_count_ % analysis_rate_ == analysis_rate_ - 1) { |
| AnalyzeClearBlock(power_target); |
| @@ -239,26 +235,32 @@ void IntelligibilityEnhancer::AnalyzeClearBlock(float power_target) { |
| FilterVariance(clear_variance_.variance(), filtered_clear_var_.get()); |
| FilterVariance(noise_variance_.variance(), filtered_noise_var_.get()); |
| - // Bisection search for optimal |lambda| |
| - |
| - float lambda_bot = -1.0f, lambda_top = -10e-18f, lambda; |
| - float power_bot, power_top, power; |
| - SolveForGainsGivenLambda(lambda_top, start_freq_, gains_eq_.get()); |
| - power_top = |
| + SolveForGainsGivenLambda(kLambdaTop, start_freq_, gains_eq_.get()); |
| + const float power_top = |
| DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| - SolveForGainsGivenLambda(lambda_bot, start_freq_, gains_eq_.get()); |
| - power_bot = |
| + SolveForGainsGivenLambda(kLambdaBot, start_freq_, gains_eq_.get()); |
| + const float power_bot = |
| DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| - DCHECK(power_target >= power_bot && power_target <= power_top); |
| + if (power_target >= power_bot && power_target <= power_top) { |
| + SolveForLambda(power_target, power_bot, power_top); |
| + UpdateErbGains(); |
| + } // Else experiencing variance underflow, so do nothing. |
| +} |
| +void IntelligibilityEnhancer::SolveForLambda(float power_target, |
| + float power_bot, |
| + float power_top) { |
| + float lambda_bot = kLambdaBot; |
| + float lambda_top = kLambdaTop; |
| float power_ratio = 2.0f; // Ratio of achieved power to target power. |
| const float kConvergeThresh = 0.001f; // TODO(ekmeyerson): Find best values |
| const int kMaxIters = 100; // for these, based on experiments. |
| int iters = 0; |
| while (fabs(power_ratio - 1.0f) > kConvergeThresh && iters <= kMaxIters) { |
|
Andrew MacDonald
2015/07/09 03:22:46
std::fabs
ekm
2015/07/09 18:19:22
Done.
|
| - lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; |
| + const float lambda = lambda_bot + (lambda_top - lambda_bot) / 2.0f; |
| SolveForGainsGivenLambda(lambda, start_freq_, gains_eq_.get()); |
| - power = DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| + const float power = |
| + DotProduct(gains_eq_.get(), filtered_clear_var_.get(), bank_size_); |
| if (power < power_target) { |
| lambda_bot = lambda; |
| } else { |
| @@ -267,7 +269,9 @@ void IntelligibilityEnhancer::AnalyzeClearBlock(float power_target) { |
| power_ratio = fabs(power / power_target); |
|
Andrew MacDonald
2015/07/09 03:22:46
To be sure we avoid a divide, outside the loop:
co
ekm
2015/07/09 18:19:22
Done. Nice!
|
| ++iters; |
| } |
| +} |
| +void IntelligibilityEnhancer::UpdateErbGains() { |
| // (ERB gain) = filterbank' * (freq gain) |
| float* gains = gain_applier_.target(); |
| for (int i = 0; i < freqs_; ++i) { |