high_pass_filter_impl.cc revision a7384a1126cda7ce726f73b023bad997627fc138
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/interface/critical_section_wrapper.h"
18#include "webrtc/typedefs.h"
19
20
21namespace webrtc {
22namespace {
23const int16_t kFilterCoefficients8kHz[5] =
24    {3798, -7596, 3798, 7807, -3733};
25
26const int16_t kFilterCoefficients[5] =
27    {4012, -8024, 4012, 8002, -3913};
28
29struct FilterState {
30  int16_t y[4];
31  int16_t x[2];
32  const int16_t* ba;
33};
34
35int 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
50int Filter(FilterState* hpf, int16_t* data, int 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 (int 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 =
63        WEBRTC_SPL_MUL_16_16(y[1], ba[3]); // -a[1] * y[i-1] (low part)
64    tmp_int32 +=
65        WEBRTC_SPL_MUL_16_16(y[3], ba[4]); // -a[2] * y[i-2] (low part)
66    tmp_int32 = (tmp_int32 >> 15);
67    tmp_int32 +=
68        WEBRTC_SPL_MUL_16_16(y[0], ba[3]); // -a[1] * y[i-1] (high part)
69    tmp_int32 +=
70        WEBRTC_SPL_MUL_16_16(y[2], ba[4]); // -a[2] * y[i-2] (high part)
71    tmp_int32 = (tmp_int32 << 1);
72
73    tmp_int32 += WEBRTC_SPL_MUL_16_16(data[i], ba[0]); // b[0]*x[0]
74    tmp_int32 += WEBRTC_SPL_MUL_16_16(x[0], ba[1]);    // b[1]*x[i-1]
75    tmp_int32 += WEBRTC_SPL_MUL_16_16(x[1], ba[2]);    // b[2]*x[i-2]
76
77    // Update state (input part)
78    x[1] = x[0];
79    x[0] = data[i];
80
81    // Update state (filtered part)
82    y[2] = y[0];
83    y[3] = y[1];
84    y[0] = static_cast<int16_t>(tmp_int32 >> 13);
85    y[1] = static_cast<int16_t>(
86        (tmp_int32 - (static_cast<int32_t>(y[0]) << 13)) << 2);
87
88    // Rounding in Q12, i.e. add 2^11
89    tmp_int32 += 2048;
90
91    // Saturate (to 2^27) so that the HP filtered signal does not overflow
92    tmp_int32 = WEBRTC_SPL_SAT(static_cast<int32_t>(134217727),
93                               tmp_int32,
94                               static_cast<int32_t>(-134217728));
95
96    // Convert back to Q0 and use rounding.
97    data[i] = (int16_t)(tmp_int32 >> 12);
98  }
99
100  return AudioProcessing::kNoError;
101}
102}  // namespace
103
104typedef FilterState Handle;
105
106HighPassFilterImpl::HighPassFilterImpl(const AudioProcessing* apm,
107                                       CriticalSectionWrapper* crit)
108  : ProcessingComponent(),
109    apm_(apm),
110    crit_(crit) {}
111
112HighPassFilterImpl::~HighPassFilterImpl() {}
113
114int HighPassFilterImpl::ProcessCaptureAudio(AudioBuffer* audio) {
115  int err = apm_->kNoError;
116
117  if (!is_component_enabled()) {
118    return apm_->kNoError;
119  }
120
121  assert(audio->samples_per_split_channel() <= 160);
122
123  for (int i = 0; i < num_handles(); i++) {
124    Handle* my_handle = static_cast<Handle*>(handle(i));
125    err = Filter(my_handle,
126                 audio->split_data(i, kBand0To8kHz),
127                 audio->samples_per_split_channel());
128
129    if (err != apm_->kNoError) {
130      return GetHandleError(my_handle);
131    }
132  }
133
134  return apm_->kNoError;
135}
136
137int HighPassFilterImpl::Enable(bool enable) {
138  CriticalSectionScoped crit_scoped(crit_);
139  return EnableComponent(enable);
140}
141
142bool HighPassFilterImpl::is_enabled() const {
143  return is_component_enabled();
144}
145
146void* HighPassFilterImpl::CreateHandle() const {
147  return new FilterState;
148}
149
150void HighPassFilterImpl::DestroyHandle(void* handle) const {
151  delete static_cast<Handle*>(handle);
152}
153
154int HighPassFilterImpl::InitializeHandle(void* handle) const {
155  return InitializeFilter(static_cast<Handle*>(handle),
156                          apm_->proc_sample_rate_hz());
157}
158
159int HighPassFilterImpl::ConfigureHandle(void* /*handle*/) const {
160  return apm_->kNoError; // Not configurable.
161}
162
163int HighPassFilterImpl::num_handles_required() const {
164  return apm_->num_output_channels();
165}
166
167int HighPassFilterImpl::GetHandleError(void* handle) const {
168  // The component has no detailed errors.
169  assert(handle != NULL);
170  return apm_->kUnspecifiedError;
171}
172}  // namespace webrtc
173