Chromium Code Reviews| Index: webrtc/modules/audio_processing/level_controller/biquad_filter.cc |
| diff --git a/webrtc/modules/audio_processing/level_controller/biquad_filter.cc b/webrtc/modules/audio_processing/level_controller/biquad_filter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f459ad85d9f283ce61bfefa9b1d06dd6f7e6d56b |
| --- /dev/null |
| +++ b/webrtc/modules/audio_processing/level_controller/biquad_filter.cc |
| @@ -0,0 +1,56 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/audio_processing/level_controller/biquad_filter.h" |
| + |
| +#include <string.h> |
| + |
| +namespace webrtc { |
| + |
| +BiQuadFilter::BiQuadFilter(const BiQuadCoefficients& coefficients, |
| + int num_biquads) |
| + : num_biquads_(num_biquads), |
| + biquad_states_(new std::vector<BiQuadState>(num_biquads_)) { |
|
hlundin-webrtc
2016/06/27 11:21:13
If biquad_states_ was just an std::vector, this wo
peah-webrtc
2016/06/27 22:51:46
Done.
|
| + for (auto& biquad_state : *biquad_states_) { |
| + biquad_state.Reset(); |
| + } |
| + coefficients_ = coefficients; |
|
hlundin-webrtc
2016/06/27 11:21:13
Can't you initialize this in the initializer list?
peah-webrtc
2016/06/27 22:51:46
Done.
|
| +} |
| + |
| +void BiQuadFilter::Process(rtc::ArrayView<const float> x, |
| + rtc::ArrayView<float> y) { |
| + ApplyBiQuad(x, y, &(*biquad_states_)[0]); |
|
hlundin-webrtc
2016/06/27 11:21:13
If the size is always 1 (per offline discussion),
peah-webrtc
2016/06/27 22:51:46
Done.
|
| + for (size_t k = 1; k < biquad_states_->size(); ++k) { |
| + ApplyBiQuad(y, y, &(*biquad_states_)[k]); |
| + } |
| +} |
| + |
| +void BiQuadFilter::ApplyBiQuad(rtc::ArrayView<const float> x, |
|
hlundin-webrtc
2016/06/27 11:21:13
I wouldn't mind a function comment explaining in s
peah-webrtc
2016/06/27 22:51:46
Done.
|
| + rtc::ArrayView<float> y, |
| + BiQuadState* biquad_state) { |
| + const auto coefficients_b = coefficients_.b; |
|
hlundin-webrtc
2016/06/27 11:21:13
There is a very marginal win in defining these loc
peah-webrtc
2016/06/27 22:51:46
Done.
|
| + const auto coefficients_a = coefficients_.a; |
| + auto memory_b = biquad_state->b; |
| + auto memory_a = biquad_state->a; |
| + for (size_t k = 0; k < x.size(); ++k) { |
| + // Use temporary variable for x[k] to allow in-place function call |
| + // (that x and y refer to the same array). |
| + const float tmp = x[k]; |
| + y[k] = coefficients_b[0] * tmp + coefficients_b[1] * memory_b[0] + |
| + coefficients_b[2] * memory_b[1] - coefficients_a[0] * memory_a[0] - |
| + coefficients_a[1] * memory_a[1]; |
| + memory_b[1] = memory_b[0]; |
| + memory_b[0] = tmp; |
| + memory_a[1] = memory_a[0]; |
| + memory_a[0] = y[k]; |
| + } |
| +} |
| + |
| +} // namespace webrtc |