1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
12 
13 #include <assert.h>
14 
15 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
16 #include "webrtc/modules/audio_processing/audio_buffer.h"
17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
18 #include "webrtc/typedefs.h"
19 
20 
21 namespace webrtc {
22 namespace {
23 const int16_t kFilterCoefficients8kHz[5] =
24     {3798, -7596, 3798, 7807, -3733};
25 
26 const int16_t kFilterCoefficients[5] =
27     {4012, -8024, 4012, 8002, -3913};
28 
29 struct FilterState {
30   int16_t y[4];
31   int16_t x[2];
32   const int16_t* ba;
33 };
34 
InitializeFilter(FilterState * hpf,int sample_rate_hz)35 int InitializeFilter(FilterState* hpf, int sample_rate_hz) {
36   assert(hpf != NULL);
37 
38   if (sample_rate_hz == AudioProcessing::kSampleRate8kHz) {
39     hpf->ba = kFilterCoefficients8kHz;
40   } else {
41     hpf->ba = kFilterCoefficients;
42   }
43 
44   WebRtcSpl_MemSetW16(hpf->x, 0, 2);
45   WebRtcSpl_MemSetW16(hpf->y, 0, 4);
46 
47   return AudioProcessing::kNoError;
48 }
49 
Filter(FilterState * hpf,int16_t * data,size_t length)50 int Filter(FilterState* hpf, int16_t* data, size_t length) {
51   assert(hpf != NULL);
52 
53   int32_t tmp_int32 = 0;
54   int16_t* y = hpf->y;
55   int16_t* x = hpf->x;
56   const int16_t* ba = hpf->ba;
57 
58   for (size_t i = 0; i < length; i++) {
59     //  y[i] = b[0] * x[i] + b[1] * x[i-1] + b[2] * x[i-2]
60     //         + -a[1] * y[i-1] + -a[2] * y[i-2];
61 
62     tmp_int32 = y[1] * ba[3];  // -a[1] * y[i-1] (low part)
63     tmp_int32 += y[3] * ba[4];  // -a[2] * y[i-2] (low part)
64     tmp_int32 = (tmp_int32 >> 15);
65     tmp_int32 += y[0] * ba[3];  // -a[1] * y[i-1] (high part)
66     tmp_int32 += y[2] * ba[4];  // -a[2] * y[i-2] (high part)
67     tmp_int32 = (tmp_int32 << 1);
68 
69     tmp_int32 += data[i] * ba[0];  // b[0]*x[0]
70     tmp_int32 += x[0] * ba[1];  // b[1]*x[i-1]
71     tmp_int32 += x[1] * ba[2];  // b[2]*x[i-2]
72 
73     // Update state (input part)
74     x[1] = x[0];
75     x[0] = data[i];
76 
77     // Update state (filtered part)
78     y[2] = y[0];
79     y[3] = y[1];
80     y[0] = static_cast<int16_t>(tmp_int32 >> 13);
81     y[1] = static_cast<int16_t>(
82         (tmp_int32 - (static_cast<int32_t>(y[0]) << 13)) << 2);
83 
84     // Rounding in Q12, i.e. add 2^11
85     tmp_int32 += 2048;
86 
87     // Saturate (to 2^27) so that the HP filtered signal does not overflow
88     tmp_int32 = WEBRTC_SPL_SAT(static_cast<int32_t>(134217727),
89                                tmp_int32,
90                                static_cast<int32_t>(-134217728));
91 
92     // Convert back to Q0 and use rounding.
93     data[i] = (int16_t)(tmp_int32 >> 12);
94   }
95 
96   return AudioProcessing::kNoError;
97 }
98 }  // namespace
99 
100 typedef FilterState Handle;
101 
HighPassFilterImpl(const AudioProcessing * apm,CriticalSectionWrapper * crit)102 HighPassFilterImpl::HighPassFilterImpl(const AudioProcessing* apm,
103                                        CriticalSectionWrapper* crit)
104   : ProcessingComponent(),
105     apm_(apm),
106     crit_(crit) {}
107 
~HighPassFilterImpl()108 HighPassFilterImpl::~HighPassFilterImpl() {}
109 
ProcessCaptureAudio(AudioBuffer * audio)110 int HighPassFilterImpl::ProcessCaptureAudio(AudioBuffer* audio) {
111   int err = apm_->kNoError;
112 
113   if (!is_component_enabled()) {
114     return apm_->kNoError;
115   }
116 
117   assert(audio->num_frames_per_band() <= 160);
118 
119   for (int i = 0; i < num_handles(); i++) {
120     Handle* my_handle = static_cast<Handle*>(handle(i));
121     err = Filter(my_handle,
122                  audio->split_bands(i)[kBand0To8kHz],
123                  audio->num_frames_per_band());
124 
125     if (err != apm_->kNoError) {
126       return GetHandleError(my_handle);
127     }
128   }
129 
130   return apm_->kNoError;
131 }
132 
Enable(bool enable)133 int HighPassFilterImpl::Enable(bool enable) {
134   CriticalSectionScoped crit_scoped(crit_);
135   return EnableComponent(enable);
136 }
137 
is_enabled() const138 bool HighPassFilterImpl::is_enabled() const {
139   return is_component_enabled();
140 }
141 
CreateHandle() const142 void* HighPassFilterImpl::CreateHandle() const {
143   return new FilterState;
144 }
145 
DestroyHandle(void * handle) const146 void HighPassFilterImpl::DestroyHandle(void* handle) const {
147   delete static_cast<Handle*>(handle);
148 }
149 
InitializeHandle(void * handle) const150 int HighPassFilterImpl::InitializeHandle(void* handle) const {
151   return InitializeFilter(static_cast<Handle*>(handle),
152                           apm_->proc_sample_rate_hz());
153 }
154 
ConfigureHandle(void *) const155 int HighPassFilterImpl::ConfigureHandle(void* /*handle*/) const {
156   return apm_->kNoError; // Not configurable.
157 }
158 
num_handles_required() const159 int HighPassFilterImpl::num_handles_required() const {
160   return apm_->num_output_channels();
161 }
162 
GetHandleError(void * handle) const163 int HighPassFilterImpl::GetHandleError(void* handle) const {
164   // The component has no detailed errors.
165   assert(handle != NULL);
166   return apm_->kUnspecifiedError;
167 }
168 }  // namespace webrtc
169