1 /*
2 * Copyright (c) 2013 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 "common_audio/resampler/include/push_resampler.h"
12
13 #include <stdint.h>
14 #include <string.h>
15
16 #include <memory>
17
18 #include "common_audio/include/audio_util.h"
19 #include "common_audio/resampler/push_sinc_resampler.h"
20 #include "rtc_base/checks.h"
21
22 namespace webrtc {
23
24 template <typename T>
PushResampler()25 PushResampler<T>::PushResampler()
26 : src_sample_rate_hz_(0), dst_sample_rate_hz_(0), num_channels_(0) {}
27
28 template <typename T>
~PushResampler()29 PushResampler<T>::~PushResampler() {}
30
31 template <typename T>
InitializeIfNeeded(int src_sample_rate_hz,int dst_sample_rate_hz,size_t num_channels)32 int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
33 int dst_sample_rate_hz,
34 size_t num_channels) {
35 // These checks used to be factored out of this template function due to
36 // Windows debug build issues with clang. http://crbug.com/615050
37 RTC_DCHECK_GT(src_sample_rate_hz, 0);
38 RTC_DCHECK_GT(dst_sample_rate_hz, 0);
39 RTC_DCHECK_GT(num_channels, 0);
40
41 if (src_sample_rate_hz == src_sample_rate_hz_ &&
42 dst_sample_rate_hz == dst_sample_rate_hz_ &&
43 num_channels == num_channels_) {
44 // No-op if settings haven't changed.
45 return 0;
46 }
47
48 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0) {
49 return -1;
50 }
51
52 src_sample_rate_hz_ = src_sample_rate_hz;
53 dst_sample_rate_hz_ = dst_sample_rate_hz;
54 num_channels_ = num_channels;
55
56 const size_t src_size_10ms_mono =
57 static_cast<size_t>(src_sample_rate_hz / 100);
58 const size_t dst_size_10ms_mono =
59 static_cast<size_t>(dst_sample_rate_hz / 100);
60 channel_resamplers_.clear();
61 for (size_t i = 0; i < num_channels; ++i) {
62 channel_resamplers_.push_back(ChannelResampler());
63 auto channel_resampler = channel_resamplers_.rbegin();
64 channel_resampler->resampler = std::make_unique<PushSincResampler>(
65 src_size_10ms_mono, dst_size_10ms_mono);
66 channel_resampler->source.resize(src_size_10ms_mono);
67 channel_resampler->destination.resize(dst_size_10ms_mono);
68 }
69
70 channel_data_array_.resize(num_channels_);
71
72 return 0;
73 }
74
75 template <typename T>
Resample(const T * src,size_t src_length,T * dst,size_t dst_capacity)76 int PushResampler<T>::Resample(const T* src,
77 size_t src_length,
78 T* dst,
79 size_t dst_capacity) {
80 // These checks used to be factored out of this template function due to
81 // Windows debug build issues with clang. http://crbug.com/615050
82 const size_t src_size_10ms = (src_sample_rate_hz_ / 100) * num_channels_;
83 const size_t dst_size_10ms = (dst_sample_rate_hz_ / 100) * num_channels_;
84 RTC_DCHECK_EQ(src_length, src_size_10ms);
85 RTC_DCHECK_GE(dst_capacity, dst_size_10ms);
86
87 if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
88 // The old resampler provides this memcpy facility in the case of matching
89 // sample rates, so reproduce it here for the sinc resampler.
90 memcpy(dst, src, src_length * sizeof(T));
91 return static_cast<int>(src_length);
92 }
93
94 const size_t src_length_mono = src_length / num_channels_;
95 const size_t dst_capacity_mono = dst_capacity / num_channels_;
96
97 for (size_t ch = 0; ch < num_channels_; ++ch) {
98 channel_data_array_[ch] = channel_resamplers_[ch].source.data();
99 }
100
101 Deinterleave(src, src_length_mono, num_channels_, channel_data_array_.data());
102
103 size_t dst_length_mono = 0;
104
105 for (auto& resampler : channel_resamplers_) {
106 dst_length_mono = resampler.resampler->Resample(
107 resampler.source.data(), src_length_mono, resampler.destination.data(),
108 dst_capacity_mono);
109 }
110
111 for (size_t ch = 0; ch < num_channels_; ++ch) {
112 channel_data_array_[ch] = channel_resamplers_[ch].destination.data();
113 }
114
115 Interleave(channel_data_array_.data(), dst_length_mono, num_channels_, dst);
116 return static_cast<int>(dst_length_mono * num_channels_);
117 }
118
119 // Explictly generate required instantiations.
120 template class PushResampler<int16_t>;
121 template class PushResampler<float>;
122
123 } // namespace webrtc
124