blob: 03d481cd06292274cbba22952b69420ce8e5a8d6 [file] [log] [blame]
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +00001/*
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 "webrtc/common_audio/resampler/include/push_resampler.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <string.h>
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000014
Tommif4fc0ff2016-05-26 22:40:09 +020015#include "webrtc/base/checks.h"
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000016#include "webrtc/common_audio/include/audio_util.h"
17#include "webrtc/common_audio/resampler/include/resampler.h"
18#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
19
20namespace webrtc {
Tommif4fc0ff2016-05-26 22:40:09 +020021namespace {
22// These checks were factored out into a non-templatized function
23// due to problems with clang on Windows in debug builds.
24// For some reason having the DCHECKs inline in the template code
25// caused the compiler to generate code that threw off the linker.
26void CheckValidInitParams(int src_sample_rate_hz, int dst_sample_rate_hz,
27 size_t num_channels) {
28 RTC_DCHECK_GT(src_sample_rate_hz, 0);
29 RTC_DCHECK_GT(dst_sample_rate_hz, 0);
30 RTC_DCHECK_GT(num_channels, 0u);
31 RTC_DCHECK_LE(num_channels, 2u);
32}
33
34void CheckExpectedBufferSizes(size_t num_channels, int src_sample_rate,
35 int dst_sample_rate) {
36 const size_t src_size_10ms = src_sample_rate * num_channels / 100;
37 const size_t dst_size_10ms = dst_sample_rate * num_channels / 100;
38 RTC_CHECK_EQ(src_length, src_size_10ms);
39 RTC_CHECK_GE(dst_capacity, dst_size_10ms);
40}
41}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000042
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000043template <typename T>
44PushResampler<T>::PushResampler()
andrew@webrtc.org31628aa2013-10-22 12:50:00 +000045 : src_sample_rate_hz_(0),
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000046 dst_sample_rate_hz_(0),
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000047 num_channels_(0) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000048}
49
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000050template <typename T>
51PushResampler<T>::~PushResampler() {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000052}
53
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000054template <typename T>
55int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
56 int dst_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080057 size_t num_channels) {
Tommif4fc0ff2016-05-26 22:40:09 +020058 CheckValidInitParams(src_sample_rate_hz, dst_sample_rate_hz, num_channels);
59
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000060 if (src_sample_rate_hz == src_sample_rate_hz_ &&
61 dst_sample_rate_hz == dst_sample_rate_hz_ &&
Tommif4fc0ff2016-05-26 22:40:09 +020062 num_channels == num_channels_) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000063 // No-op if settings haven't changed.
64 return 0;
Tommif4fc0ff2016-05-26 22:40:09 +020065 }
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000066
Tommif4fc0ff2016-05-26 22:40:09 +020067 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 ||
68 num_channels > 2) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000069 return -1;
Tommif4fc0ff2016-05-26 22:40:09 +020070 }
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000071
72 src_sample_rate_hz_ = src_sample_rate_hz;
73 dst_sample_rate_hz_ = dst_sample_rate_hz;
74 num_channels_ = num_channels;
75
Peter Kastingdce40cf2015-08-24 14:52:23 -070076 const size_t src_size_10ms_mono =
77 static_cast<size_t>(src_sample_rate_hz / 100);
78 const size_t dst_size_10ms_mono =
79 static_cast<size_t>(dst_sample_rate_hz / 100);
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000080 sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono,
81 dst_size_10ms_mono));
82 if (num_channels_ == 2) {
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000083 src_left_.reset(new T[src_size_10ms_mono]);
84 src_right_.reset(new T[src_size_10ms_mono]);
85 dst_left_.reset(new T[dst_size_10ms_mono]);
86 dst_right_.reset(new T[dst_size_10ms_mono]);
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000087 sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono,
88 dst_size_10ms_mono));
89 }
90
91 return 0;
92}
93
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000094template <typename T>
Peter Kastingdce40cf2015-08-24 14:52:23 -070095int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst,
96 size_t dst_capacity) {
Tommif4fc0ff2016-05-26 22:40:09 +020097 CheckExpectedBufferSizes(num_channels_, src_sample_rate_hz_,
98 dst_sample_rate_hz_)
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000099
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000100 if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
101 // The old resampler provides this memcpy facility in the case of matching
102 // sample rates, so reproduce it here for the sinc resampler.
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000103 memcpy(dst, src, src_length * sizeof(T));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700104 return static_cast<int>(src_length);
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000105 }
106 if (num_channels_ == 2) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700107 const size_t src_length_mono = src_length / num_channels_;
108 const size_t dst_capacity_mono = dst_capacity / num_channels_;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000109 T* deinterleaved[] = {src_left_.get(), src_right_.get()};
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000110 Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
111
Peter Kastingdce40cf2015-08-24 14:52:23 -0700112 size_t dst_length_mono =
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000113 sinc_resampler_->Resample(src_left_.get(), src_length_mono,
114 dst_left_.get(), dst_capacity_mono);
115 sinc_resampler_right_->Resample(src_right_.get(), src_length_mono,
116 dst_right_.get(), dst_capacity_mono);
117
118 deinterleaved[0] = dst_left_.get();
119 deinterleaved[1] = dst_right_.get();
120 Interleave(deinterleaved, dst_length_mono, num_channels_, dst);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700121 return static_cast<int>(dst_length_mono * num_channels_);
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000122 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700123 return static_cast<int>(
124 sinc_resampler_->Resample(src, src_length, dst, dst_capacity));
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000125 }
126}
127
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000128// Explictly generate required instantiations.
129template class PushResampler<int16_t>;
130template class PushResampler<float>;
131
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000132} // namespace webrtc