OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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/high_pass_filter_impl.h" | 11 #include "webrtc/modules/audio_processing/low_cut_filter.h" |
12 | 12 |
13 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 13 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
14 #include "webrtc/modules/audio_processing/audio_buffer.h" | 14 #include "webrtc/modules/audio_processing/audio_buffer.h" |
15 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 15 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
16 | 16 |
17 namespace webrtc { | 17 namespace webrtc { |
18 namespace { | 18 namespace { |
19 const int16_t kFilterCoefficients8kHz[5] = {3798, -7596, 3798, 7807, -3733}; | 19 const int16_t kFilterCoefficients8kHz[5] = {3798, -7596, 3798, 7807, -3733}; |
20 const int16_t kFilterCoefficients[5] = {4012, -8024, 4012, 8002, -3913}; | 20 const int16_t kFilterCoefficients[5] = {4012, -8024, 4012, 8002, -3913}; |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 class HighPassFilterImpl::BiquadFilter { | 23 class LowCutFilter::BiquadFilter { |
24 public: | 24 public: |
25 explicit BiquadFilter(int sample_rate_hz) : | 25 explicit BiquadFilter(int sample_rate_hz) |
26 ba_(sample_rate_hz == AudioProcessing::kSampleRate8kHz ? | 26 : ba_(sample_rate_hz == AudioProcessing::kSampleRate8kHz |
27 kFilterCoefficients8kHz : kFilterCoefficients) | 27 ? kFilterCoefficients8kHz |
28 { | 28 : kFilterCoefficients) { |
29 Reset(); | |
30 } | |
31 | |
32 void Reset() { | |
33 std::memset(x_, 0, sizeof(x_)); | 29 std::memset(x_, 0, sizeof(x_)); |
34 std::memset(y_, 0, sizeof(y_)); | 30 std::memset(y_, 0, sizeof(y_)); |
35 } | 31 } |
36 | 32 |
37 void Process(int16_t* data, size_t length) { | 33 void Process(int16_t* data, size_t length) { |
38 const int16_t* const ba = ba_; | 34 const int16_t* const ba = ba_; |
39 int16_t* x = x_; | 35 int16_t* x = x_; |
40 int16_t* y = y_; | 36 int16_t* y = y_; |
41 int32_t tmp_int32 = 0; | 37 int32_t tmp_int32 = 0; |
42 | 38 |
43 for (size_t i = 0; i < length; i++) { | 39 for (size_t i = 0; i < length; i++) { |
44 // y[i] = b[0] * x[i] + b[1] * x[i-1] + b[2] * x[i-2] | 40 // y[i] = b[0] * x[i] + b[1] * x[i-1] + b[2] * x[i-2] |
45 // + -a[1] * y[i-1] + -a[2] * y[i-2]; | 41 // + -a[1] * y[i-1] + -a[2] * y[i-2]; |
46 | 42 |
47 tmp_int32 = y[1] * ba[3]; // -a[1] * y[i-1] (low part) | 43 tmp_int32 = y[1] * ba[3]; // -a[1] * y[i-1] (low part) |
48 tmp_int32 += y[3] * ba[4]; // -a[2] * y[i-2] (low part) | 44 tmp_int32 += y[3] * ba[4]; // -a[2] * y[i-2] (low part) |
49 tmp_int32 = (tmp_int32 >> 15); | 45 tmp_int32 = (tmp_int32 >> 15); |
50 tmp_int32 += y[0] * ba[3]; // -a[1] * y[i-1] (high part) | 46 tmp_int32 += y[0] * ba[3]; // -a[1] * y[i-1] (high part) |
51 tmp_int32 += y[2] * ba[4]; // -a[2] * y[i-2] (high part) | 47 tmp_int32 += y[2] * ba[4]; // -a[2] * y[i-2] (high part) |
52 tmp_int32 = (tmp_int32 << 1); | 48 tmp_int32 = (tmp_int32 << 1); |
53 | 49 |
54 tmp_int32 += data[i] * ba[0]; // b[0] * x[0] | 50 tmp_int32 += data[i] * ba[0]; // b[0] * x[0] |
55 tmp_int32 += x[0] * ba[1]; // b[1] * x[i-1] | 51 tmp_int32 += x[0] * ba[1]; // b[1] * x[i-1] |
56 tmp_int32 += x[1] * ba[2]; // b[2] * x[i-2] | 52 tmp_int32 += x[1] * ba[2]; // b[2] * x[i-2] |
57 | 53 |
58 // Update state (input part). | 54 // Update state (input part). |
59 x[1] = x[0]; | 55 x[1] = x[0]; |
60 x[0] = data[i]; | 56 x[0] = data[i]; |
61 | 57 |
62 // Update state (filtered part). | 58 // Update state (filtered part). |
63 y[2] = y[0]; | 59 y[2] = y[0]; |
64 y[3] = y[1]; | 60 y[3] = y[1]; |
65 y[0] = static_cast<int16_t>(tmp_int32 >> 13); | 61 y[0] = static_cast<int16_t>(tmp_int32 >> 13); |
66 y[1] = static_cast<int16_t>( | 62 y[1] = static_cast<int16_t>( |
67 (tmp_int32 - (static_cast<int32_t>(y[0]) << 13)) << 2); | 63 (tmp_int32 - (static_cast<int32_t>(y[0]) << 13)) << 2); |
68 | 64 |
69 // Rounding in Q12, i.e. add 2^11. | 65 // Rounding in Q12, i.e. add 2^11. |
70 tmp_int32 += 2048; | 66 tmp_int32 += 2048; |
71 | 67 |
72 // Saturate (to 2^27) so that the HP filtered signal does not overflow. | 68 // Saturate (to 2^27) so that the HP filtered signal does not overflow. |
73 tmp_int32 = WEBRTC_SPL_SAT(static_cast<int32_t>(134217727), | 69 tmp_int32 = WEBRTC_SPL_SAT(static_cast<int32_t>(134217727), tmp_int32, |
74 tmp_int32, | |
75 static_cast<int32_t>(-134217728)); | 70 static_cast<int32_t>(-134217728)); |
76 | 71 |
77 // Convert back to Q0 and use rounding. | 72 // Convert back to Q0 and use rounding. |
78 data[i] = static_cast<int16_t>(tmp_int32 >> 12); | 73 data[i] = static_cast<int16_t>(tmp_int32 >> 12); |
79 } | 74 } |
80 } | 75 } |
81 | 76 |
82 private: | 77 private: |
83 const int16_t* const ba_ = nullptr; | 78 const int16_t* const ba_ = nullptr; |
84 int16_t x_[2]; | 79 int16_t x_[2]; |
85 int16_t y_[4]; | 80 int16_t y_[4]; |
86 }; | 81 }; |
87 | 82 |
88 HighPassFilterImpl::HighPassFilterImpl(rtc::CriticalSection* crit) | 83 LowCutFilter::LowCutFilter(size_t channels, int sample_rate_hz) { |
89 : crit_(crit) { | 84 filters_.resize(channels); |
90 RTC_DCHECK(crit_); | 85 for (size_t i = 0; i < channels; i++) { |
| 86 filters_[i].reset(new BiquadFilter(sample_rate_hz)); |
| 87 } |
91 } | 88 } |
92 | 89 |
93 HighPassFilterImpl::~HighPassFilterImpl() {} | 90 LowCutFilter::~LowCutFilter() {} |
94 | 91 |
95 void HighPassFilterImpl::Initialize(size_t channels, int sample_rate_hz) { | 92 void LowCutFilter::Process(AudioBuffer* audio) { |
96 std::vector<std::unique_ptr<BiquadFilter>> new_filters(channels); | |
97 for (size_t i = 0; i < channels; i++) { | |
98 new_filters[i].reset(new BiquadFilter(sample_rate_hz)); | |
99 } | |
100 rtc::CritScope cs(crit_); | |
101 filters_.swap(new_filters); | |
102 } | |
103 | |
104 void HighPassFilterImpl::ProcessCaptureAudio(AudioBuffer* audio) { | |
105 RTC_DCHECK(audio); | 93 RTC_DCHECK(audio); |
106 rtc::CritScope cs(crit_); | |
107 if (!enabled_) { | |
108 return; | |
109 } | |
110 | |
111 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); | 94 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
112 RTC_DCHECK_EQ(filters_.size(), audio->num_channels()); | 95 RTC_DCHECK_EQ(filters_.size(), audio->num_channels()); |
113 for (size_t i = 0; i < filters_.size(); i++) { | 96 for (size_t i = 0; i < filters_.size(); i++) { |
114 filters_[i]->Process(audio->split_bands(i)[kBand0To8kHz], | 97 filters_[i]->Process(audio->split_bands(i)[kBand0To8kHz], |
115 audio->num_frames_per_band()); | 98 audio->num_frames_per_band()); |
116 } | 99 } |
117 } | 100 } |
118 | 101 |
119 int HighPassFilterImpl::Enable(bool enable) { | |
120 rtc::CritScope cs(crit_); | |
121 if (!enabled_ && enable) { | |
122 for (auto& filter : filters_) { | |
123 filter->Reset(); | |
124 } | |
125 } | |
126 enabled_ = enable; | |
127 return AudioProcessing::kNoError; | |
128 } | |
129 | |
130 bool HighPassFilterImpl::is_enabled() const { | |
131 rtc::CritScope cs(crit_); | |
132 return enabled_; | |
133 } | |
134 } // namespace webrtc | 102 } // namespace webrtc |
OLD | NEW |