peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 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 | |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 18 | #include "webrtc/modules/audio_processing/level_controller/biquad_filter.h" |
| 19 | #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 20 | #include "webrtc/rtc_base/checks.h" |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 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 | DownSampler::DownSampler(ApmDataDumper* data_dumper) |
| 46 | : data_dumper_(data_dumper) { |
| 47 | Initialize(48000); |
| 48 | } |
| 49 | void DownSampler::Initialize(int sample_rate_hz) { |
| 50 | RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
| 51 | sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
| 52 | sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 53 | sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
| 54 | |
| 55 | sample_rate_hz_ = sample_rate_hz; |
| 56 | 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 | if (sample_rate_hz_ == AudioProcessing::kSampleRate16kHz) { |
| 61 | low_pass_filter_.Initialize(kLowPassFilterCoefficients_16kHz); |
| 62 | } else if (sample_rate_hz_ == AudioProcessing::kSampleRate32kHz) { |
| 63 | low_pass_filter_.Initialize(kLowPassFilterCoefficients_32kHz); |
| 64 | } else if (sample_rate_hz_ == AudioProcessing::kSampleRate48kHz) { |
| 65 | low_pass_filter_.Initialize(kLowPassFilterCoefficients_48kHz); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void DownSampler::DownSample(rtc::ArrayView<const float> in, |
| 70 | rtc::ArrayView<float> out) { |
| 71 | data_dumper_->DumpWav("lc_down_sampler_input", in, sample_rate_hz_, 1); |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 72 | RTC_DCHECK_EQ(sample_rate_hz_ * AudioProcessing::kChunkSizeMs / 1000, |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 73 | in.size()); |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 74 | RTC_DCHECK_EQ( |
| 75 | AudioProcessing::kSampleRate8kHz * AudioProcessing::kChunkSizeMs / 1000, |
| 76 | out.size()); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 77 | const size_t kMaxNumFrames = |
| 78 | AudioProcessing::kSampleRate48kHz * AudioProcessing::kChunkSizeMs / 1000; |
| 79 | float x[kMaxNumFrames]; |
| 80 | |
| 81 | // Band-limit the signal to 4 kHz. |
| 82 | if (sample_rate_hz_ != AudioProcessing::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, |
| 97 | AudioProcessing::kSampleRate8kHz, 1); |
| 98 | } |
| 99 | |
| 100 | } // namespace webrtc |