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 namespace {
24 // These checks were factored out into a non-templatized function
25 // due to problems with clang on Windows in debug builds.
26 // For some reason having the DCHECKs inline in the template code
27 // caused the compiler to generate code that threw off the linker.
28 // TODO(tommi): Re-enable when we've figured out what the problem is.
29 // http://crbug.com/615050
CheckValidInitParams(int src_sample_rate_hz,int dst_sample_rate_hz,size_t num_channels)30 void CheckValidInitParams(int src_sample_rate_hz,
31 int dst_sample_rate_hz,
32 size_t num_channels) {
33 // The below checks are temporarily disabled on WEBRTC_WIN due to problems
34 // with clang debug builds.
35 #if !defined(WEBRTC_WIN) && defined(__clang__)
36 RTC_DCHECK_GT(src_sample_rate_hz, 0);
37 RTC_DCHECK_GT(dst_sample_rate_hz, 0);
38 RTC_DCHECK_GT(num_channels, 0);
39 #endif
40 }
41
CheckExpectedBufferSizes(size_t src_length,size_t dst_capacity,size_t num_channels,int src_sample_rate,int dst_sample_rate)42 void CheckExpectedBufferSizes(size_t src_length,
43 size_t dst_capacity,
44 size_t num_channels,
45 int src_sample_rate,
46 int dst_sample_rate) {
47 // The below checks are temporarily disabled on WEBRTC_WIN due to problems
48 // with clang debug builds.
49 // TODO(tommi): Re-enable when we've figured out what the problem is.
50 // http://crbug.com/615050
51 #if !defined(WEBRTC_WIN) && defined(__clang__)
52 const size_t src_size_10ms = src_sample_rate * num_channels / 100;
53 const size_t dst_size_10ms = dst_sample_rate * num_channels / 100;
54 RTC_DCHECK_EQ(src_length, src_size_10ms);
55 RTC_DCHECK_GE(dst_capacity, dst_size_10ms);
56 #endif
57 }
58 } // namespace
59
60 template <typename T>
PushResampler()61 PushResampler<T>::PushResampler()
62 : src_sample_rate_hz_(0), dst_sample_rate_hz_(0), num_channels_(0) {}
63
64 template <typename T>
~PushResampler()65 PushResampler<T>::~PushResampler() {}
66
67 template <typename T>
InitializeIfNeeded(int src_sample_rate_hz,int dst_sample_rate_hz,size_t num_channels)68 int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
69 int dst_sample_rate_hz,
70 size_t num_channels) {
71 CheckValidInitParams(src_sample_rate_hz, dst_sample_rate_hz, num_channels);
72
73 if (src_sample_rate_hz == src_sample_rate_hz_ &&
74 dst_sample_rate_hz == dst_sample_rate_hz_ &&
75 num_channels == num_channels_) {
76 // No-op if settings haven't changed.
77 return 0;
78 }
79
80 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0) {
81 return -1;
82 }
83
84 src_sample_rate_hz_ = src_sample_rate_hz;
85 dst_sample_rate_hz_ = dst_sample_rate_hz;
86 num_channels_ = num_channels;
87
88 const size_t src_size_10ms_mono =
89 static_cast<size_t>(src_sample_rate_hz / 100);
90 const size_t dst_size_10ms_mono =
91 static_cast<size_t>(dst_sample_rate_hz / 100);
92 channel_resamplers_.clear();
93 for (size_t i = 0; i < num_channels; ++i) {
94 channel_resamplers_.push_back(ChannelResampler());
95 auto channel_resampler = channel_resamplers_.rbegin();
96 channel_resampler->resampler = std::make_unique<PushSincResampler>(
97 src_size_10ms_mono, dst_size_10ms_mono);
98 channel_resampler->source.resize(src_size_10ms_mono);
99 channel_resampler->destination.resize(dst_size_10ms_mono);
100 }
101
102 channel_data_array_.resize(num_channels_);
103
104 return 0;
105 }
106
107 template <typename T>
Resample(const T * src,size_t src_length,T * dst,size_t dst_capacity)108 int PushResampler<T>::Resample(const T* src,
109 size_t src_length,
110 T* dst,
111 size_t dst_capacity) {
112 CheckExpectedBufferSizes(src_length, dst_capacity, num_channels_,
113 src_sample_rate_hz_, dst_sample_rate_hz_);
114
115 if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
116 // The old resampler provides this memcpy facility in the case of matching
117 // sample rates, so reproduce it here for the sinc resampler.
118 memcpy(dst, src, src_length * sizeof(T));
119 return static_cast<int>(src_length);
120 }
121
122 const size_t src_length_mono = src_length / num_channels_;
123 const size_t dst_capacity_mono = dst_capacity / num_channels_;
124
125 for (size_t ch = 0; ch < num_channels_; ++ch) {
126 channel_data_array_[ch] = channel_resamplers_[ch].source.data();
127 }
128
129 Deinterleave(src, src_length_mono, num_channels_, channel_data_array_.data());
130
131 size_t dst_length_mono = 0;
132
133 for (auto& resampler : channel_resamplers_) {
134 dst_length_mono = resampler.resampler->Resample(
135 resampler.source.data(), src_length_mono, resampler.destination.data(),
136 dst_capacity_mono);
137 }
138
139 for (size_t ch = 0; ch < num_channels_; ++ch) {
140 channel_data_array_[ch] = channel_resamplers_[ch].destination.data();
141 }
142
143 Interleave(channel_data_array_.data(), dst_length_mono, num_channels_, dst);
144 return static_cast<int>(dst_length_mono * num_channels_);
145 }
146
147 // Explictly generate required instantiations.
148 template class PushResampler<int16_t>;
149 template class PushResampler<float>;
150
151 } // namespace webrtc
152