OLD | NEW |
(Empty) | |
| 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 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); |
| 72 RTC_DCHECK_EQ(static_cast<size_t>(sample_rate_hz_ * |
| 73 AudioProcessing::kChunkSizeMs / 1000), |
| 74 in.size()); |
| 75 RTC_DCHECK_EQ(static_cast<size_t>(AudioProcessing::kSampleRate8kHz * |
| 76 AudioProcessing::kChunkSizeMs / 1000), |
| 77 out.size()); |
| 78 const size_t kMaxNumFrames = |
| 79 AudioProcessing::kSampleRate48kHz * AudioProcessing::kChunkSizeMs / 1000; |
| 80 float x[kMaxNumFrames]; |
| 81 |
| 82 // Band-limit the signal to 4 kHz. |
| 83 if (sample_rate_hz_ != AudioProcessing::kSampleRate8kHz) { |
| 84 low_pass_filter_.Process(in, rtc::ArrayView<float>(x, in.size())); |
| 85 |
| 86 // Downsample the signal. |
| 87 size_t k = 0; |
| 88 for (size_t j = 0; j < out.size(); ++j) { |
| 89 RTC_DCHECK_GT(kMaxNumFrames, k); |
| 90 out[j] = x[k]; |
| 91 k += down_sampling_factor_; |
| 92 } |
| 93 } else { |
| 94 std::copy(in.data(), in.data() + in.size(), out.data()); |
| 95 } |
| 96 |
| 97 data_dumper_->DumpWav("lc_down_sampler_output", out, |
| 98 AudioProcessing::kSampleRate8kHz, 1); |
| 99 } |
| 100 |
| 101 } // namespace webrtc |
OLD | NEW |