| 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 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.
h" | 11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.
h" |
| 12 | 12 |
| 13 #include <math.h> | 13 #include <math.h> |
| 14 #include <stdlib.h> | 14 #include <stdlib.h> |
| 15 #include <string.h> | 15 #include <string.h> |
| 16 #include <algorithm> | 16 #include <algorithm> |
| 17 #include <limits> |
| 17 | 18 |
| 18 namespace webrtc { | 19 namespace webrtc { |
| 19 | 20 |
| 20 namespace intelligibility { | 21 namespace intelligibility { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // Return |current| changed towards |target|, with the change being at most | 25 // Return |current| changed towards |target|, with the relative change being at |
| 25 // |limit|. | 26 // most |limit|. |
| 26 float UpdateFactor(float target, float current, float limit) { | 27 float UpdateFactor(float target, float current, float limit) { |
| 27 float delta = fabsf(target - current); | 28 float gain = target / (current + std::numeric_limits<float>::epsilon()); |
| 28 float sign = copysign(1.f, target - current); | 29 if (gain < 1.f - limit) { |
| 29 return current + sign * fminf(delta, limit); | 30 gain = 1.f - limit; |
| 31 } else if (gain > 1.f + limit) { |
| 32 gain = 1.f + limit; |
| 33 } |
| 34 return current * gain + std::numeric_limits<float>::epsilon(); |
| 30 } | 35 } |
| 31 | 36 |
| 32 } // namespace | 37 } // namespace |
| 33 | 38 |
| 34 PowerEstimator::PowerEstimator(size_t num_freqs, | 39 template<typename T> |
| 35 float decay) | 40 PowerEstimator<T>::PowerEstimator(size_t num_freqs, float decay) |
| 36 : magnitude_(new float[num_freqs]()), | 41 : power_(num_freqs, 0.f), decay_(decay) {} |
| 37 power_(new float[num_freqs]()), | |
| 38 num_freqs_(num_freqs), | |
| 39 decay_(decay) { | |
| 40 memset(magnitude_.get(), 0, sizeof(*magnitude_.get()) * num_freqs_); | |
| 41 memset(power_.get(), 0, sizeof(*power_.get()) * num_freqs_); | |
| 42 } | |
| 43 | 42 |
| 44 // Compute the magnitude from the beginning, with exponential decaying of the | 43 template<typename T> |
| 45 // series data. | 44 void PowerEstimator<T>::Step(const T* data) { |
| 46 void PowerEstimator::Step(const std::complex<float>* data) { | 45 for (size_t i = 0; i < power_.size(); ++i) { |
| 47 for (size_t i = 0; i < num_freqs_; ++i) { | 46 power_[i] = decay_ * power_[i] + |
| 48 magnitude_[i] = decay_ * magnitude_[i] + | 47 (1.f - decay_) * std::abs(data[i]) * std::abs(data[i]); |
| 49 (1.f - decay_) * std::abs(data[i]); | |
| 50 } | 48 } |
| 51 } | 49 } |
| 52 | 50 |
| 53 const float* PowerEstimator::Power() { | 51 template class PowerEstimator<float>; |
| 54 for (size_t i = 0; i < num_freqs_; ++i) { | 52 template class PowerEstimator<std::complex<float>>; |
| 55 power_[i] = magnitude_[i] * magnitude_[i]; | |
| 56 } | |
| 57 return &power_[0]; | |
| 58 } | |
| 59 | 53 |
| 60 GainApplier::GainApplier(size_t freqs, float change_limit) | 54 GainApplier::GainApplier(size_t freqs, float relative_change_limit) |
| 61 : num_freqs_(freqs), | 55 : num_freqs_(freqs), |
| 62 change_limit_(change_limit), | 56 relative_change_limit_(relative_change_limit), |
| 63 target_(new float[freqs]()), | 57 target_(new float[freqs]()), |
| 64 current_(new float[freqs]()) { | 58 current_(new float[freqs]()) { |
| 65 for (size_t i = 0; i < freqs; ++i) { | 59 for (size_t i = 0; i < freqs; ++i) { |
| 66 target_[i] = 1.f; | 60 target_[i] = 1.f; |
| 67 current_[i] = 1.f; | 61 current_[i] = 1.f; |
| 68 } | 62 } |
| 69 } | 63 } |
| 70 | 64 |
| 71 void GainApplier::Apply(const std::complex<float>* in_block, | 65 void GainApplier::Apply(const std::complex<float>* in_block, |
| 72 std::complex<float>* out_block) { | 66 std::complex<float>* out_block) { |
| 73 for (size_t i = 0; i < num_freqs_; ++i) { | 67 for (size_t i = 0; i < num_freqs_; ++i) { |
| 74 float factor = sqrtf(fabsf(current_[i])); | 68 current_[i] = UpdateFactor(target_[i], current_[i], relative_change_limit_); |
| 75 if (!std::isnormal(factor)) { | 69 out_block[i] = sqrtf(fabsf(current_[i])) * in_block[i]; |
| 76 factor = 1.f; | |
| 77 } | |
| 78 out_block[i] = factor * in_block[i]; | |
| 79 current_[i] = UpdateFactor(target_[i], current_[i], change_limit_); | |
| 80 } | 70 } |
| 81 } | 71 } |
| 82 | 72 |
| 83 } // namespace intelligibility | 73 } // namespace intelligibility |
| 84 | 74 |
| 85 } // namespace webrtc | 75 } // namespace webrtc |
| OLD | NEW |