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