1 /*
2 * Copyright (c) 2016 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 "modules/audio_processing/agc2/down_sampler.h"
12
13 #include <string.h>
14
15 #include <algorithm>
16
17 #include "modules/audio_processing/agc2/biquad_filter.h"
18 #include "modules/audio_processing/logging/apm_data_dumper.h"
19 #include "rtc_base/checks.h"
20
21 namespace webrtc {
22 namespace {
23
24 constexpr int kChunkSizeMs = 10;
25 constexpr int kSampleRate8kHz = 8000;
26 constexpr int kSampleRate16kHz = 16000;
27 constexpr int kSampleRate32kHz = 32000;
28 constexpr int kSampleRate48kHz = 48000;
29
30 // Bandlimiter coefficients computed based on that only
31 // the first 40 bins of the spectrum for the downsampled
32 // signal are used.
33 // [B,A] = butter(2,(41/64*4000)/8000)
34 const BiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients_16kHz = {
35 {0.1455f, 0.2911f, 0.1455f},
36 {-0.6698f, 0.2520f}};
37
38 // [B,A] = butter(2,(41/64*4000)/16000)
39 const BiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients_32kHz = {
40 {0.0462f, 0.0924f, 0.0462f},
41 {-1.3066f, 0.4915f}};
42
43 // [B,A] = butter(2,(41/64*4000)/24000)
44 const BiQuadFilter::BiQuadCoefficients kLowPassFilterCoefficients_48kHz = {
45 {0.0226f, 0.0452f, 0.0226f},
46 {-1.5320f, 0.6224f}};
47
48 } // namespace
49
DownSampler(ApmDataDumper * data_dumper)50 DownSampler::DownSampler(ApmDataDumper* data_dumper)
51 : data_dumper_(data_dumper) {
52 Initialize(48000);
53 }
Initialize(int sample_rate_hz)54 void DownSampler::Initialize(int sample_rate_hz) {
55 RTC_DCHECK(
56 sample_rate_hz == kSampleRate8kHz || sample_rate_hz == kSampleRate16kHz ||
57 sample_rate_hz == kSampleRate32kHz || sample_rate_hz == kSampleRate48kHz);
58
59 sample_rate_hz_ = sample_rate_hz;
60 down_sampling_factor_ = rtc::CheckedDivExact(sample_rate_hz_, 8000);
61
62 /// Note that the down sampling filter is not used if the sample rate is 8
63 /// kHz.
64 if (sample_rate_hz_ == kSampleRate16kHz) {
65 low_pass_filter_.Initialize(kLowPassFilterCoefficients_16kHz);
66 } else if (sample_rate_hz_ == kSampleRate32kHz) {
67 low_pass_filter_.Initialize(kLowPassFilterCoefficients_32kHz);
68 } else if (sample_rate_hz_ == kSampleRate48kHz) {
69 low_pass_filter_.Initialize(kLowPassFilterCoefficients_48kHz);
70 }
71 }
72
DownSample(rtc::ArrayView<const float> in,rtc::ArrayView<float> out)73 void DownSampler::DownSample(rtc::ArrayView<const float> in,
74 rtc::ArrayView<float> out) {
75 data_dumper_->DumpWav("lc_down_sampler_input", in, sample_rate_hz_, 1);
76 RTC_DCHECK_EQ(sample_rate_hz_ * kChunkSizeMs / 1000, in.size());
77 RTC_DCHECK_EQ(kSampleRate8kHz * kChunkSizeMs / 1000, out.size());
78 const size_t kMaxNumFrames = kSampleRate48kHz * kChunkSizeMs / 1000;
79 float x[kMaxNumFrames];
80
81 // Band-limit the signal to 4 kHz.
82 if (sample_rate_hz_ != kSampleRate8kHz) {
83 low_pass_filter_.Process(in, rtc::ArrayView<float>(x, in.size()));
84
85 // Downsample the signal.
86 size_t k = 0;
87 for (size_t j = 0; j < out.size(); ++j) {
88 RTC_DCHECK_GT(kMaxNumFrames, k);
89 out[j] = x[k];
90 k += down_sampling_factor_;
91 }
92 } else {
93 std::copy(in.data(), in.data() + in.size(), out.data());
94 }
95
96 data_dumper_->DumpWav("lc_down_sampler_output", out, kSampleRate8kHz, 1);
97 }
98
99 } // namespace webrtc
100