blob: 566acdeaa3c7ac670a768104f62a33546830c7c2 [file] [log] [blame]
andrew@webrtc.org50b2efe2013-04-29 17:27:291/*
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:5313#include <string.h>
andrew@webrtc.org50b2efe2013-04-29 17:27:2914
15#include "webrtc/common_audio/include/audio_util.h"
16#include "webrtc/common_audio/resampler/include/resampler.h"
17#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
18
19namespace webrtc {
20
andrew@webrtc.orgf5a33f12014-04-19 00:32:0721template <typename T>
22PushResampler<T>::PushResampler()
andrew@webrtc.org31628aa2013-10-22 12:50:0023 : src_sample_rate_hz_(0),
andrew@webrtc.org50b2efe2013-04-29 17:27:2924 dst_sample_rate_hz_(0),
andrew@webrtc.orgf5a33f12014-04-19 00:32:0725 num_channels_(0) {
andrew@webrtc.org50b2efe2013-04-29 17:27:2926}
27
andrew@webrtc.orgf5a33f12014-04-19 00:32:0728template <typename T>
29PushResampler<T>::~PushResampler() {
andrew@webrtc.org50b2efe2013-04-29 17:27:2930}
31
andrew@webrtc.orgf5a33f12014-04-19 00:32:0732template <typename T>
33int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
34 int dst_sample_rate_hz,
35 int num_channels) {
andrew@webrtc.org50b2efe2013-04-29 17:27:2936 if (src_sample_rate_hz == src_sample_rate_hz_ &&
37 dst_sample_rate_hz == dst_sample_rate_hz_ &&
andrew@webrtc.orgb86fbaf2013-07-25 22:04:3038 num_channels == num_channels_)
andrew@webrtc.org50b2efe2013-04-29 17:27:2939 // No-op if settings haven't changed.
40 return 0;
andrew@webrtc.org50b2efe2013-04-29 17:27:2941
42 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 ||
andrew@webrtc.orgb86fbaf2013-07-25 22:04:3043 num_channels <= 0 || num_channels > 2)
andrew@webrtc.org50b2efe2013-04-29 17:27:2944 return -1;
andrew@webrtc.org50b2efe2013-04-29 17:27:2945
46 src_sample_rate_hz_ = src_sample_rate_hz;
47 dst_sample_rate_hz_ = dst_sample_rate_hz;
48 num_channels_ = num_channels;
49
Peter Kastingdce40cf2015-08-24 21:52:2350 const size_t src_size_10ms_mono =
51 static_cast<size_t>(src_sample_rate_hz / 100);
52 const size_t dst_size_10ms_mono =
53 static_cast<size_t>(dst_sample_rate_hz / 100);
andrew@webrtc.org50b2efe2013-04-29 17:27:2954 sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono,
55 dst_size_10ms_mono));
56 if (num_channels_ == 2) {
andrew@webrtc.orgf5a33f12014-04-19 00:32:0757 src_left_.reset(new T[src_size_10ms_mono]);
58 src_right_.reset(new T[src_size_10ms_mono]);
59 dst_left_.reset(new T[dst_size_10ms_mono]);
60 dst_right_.reset(new T[dst_size_10ms_mono]);
andrew@webrtc.org50b2efe2013-04-29 17:27:2961 sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono,
62 dst_size_10ms_mono));
63 }
64
65 return 0;
66}
67
andrew@webrtc.orgf5a33f12014-04-19 00:32:0768template <typename T>
Peter Kastingdce40cf2015-08-24 21:52:2369int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst,
70 size_t dst_capacity) {
71 const size_t src_size_10ms =
72 static_cast<size_t>(src_sample_rate_hz_ * num_channels_ / 100);
73 const size_t dst_size_10ms =
74 static_cast<size_t>(dst_sample_rate_hz_ * num_channels_ / 100);
andrew@webrtc.orgb86fbaf2013-07-25 22:04:3075 if (src_length != src_size_10ms || dst_capacity < dst_size_10ms)
andrew@webrtc.org50b2efe2013-04-29 17:27:2976 return -1;
andrew@webrtc.org50b2efe2013-04-29 17:27:2977
andrew@webrtc.org50b2efe2013-04-29 17:27:2978 if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
79 // The old resampler provides this memcpy facility in the case of matching
80 // sample rates, so reproduce it here for the sinc resampler.
andrew@webrtc.orgf5a33f12014-04-19 00:32:0781 memcpy(dst, src, src_length * sizeof(T));
Peter Kastingdce40cf2015-08-24 21:52:2382 return static_cast<int>(src_length);
andrew@webrtc.org50b2efe2013-04-29 17:27:2983 }
84 if (num_channels_ == 2) {
Peter Kastingdce40cf2015-08-24 21:52:2385 const size_t src_length_mono = src_length / num_channels_;
86 const size_t dst_capacity_mono = dst_capacity / num_channels_;
andrew@webrtc.orgf5a33f12014-04-19 00:32:0787 T* deinterleaved[] = {src_left_.get(), src_right_.get()};
andrew@webrtc.org50b2efe2013-04-29 17:27:2988 Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
89
Peter Kastingdce40cf2015-08-24 21:52:2390 size_t dst_length_mono =
andrew@webrtc.org50b2efe2013-04-29 17:27:2991 sinc_resampler_->Resample(src_left_.get(), src_length_mono,
92 dst_left_.get(), dst_capacity_mono);
93 sinc_resampler_right_->Resample(src_right_.get(), src_length_mono,
94 dst_right_.get(), dst_capacity_mono);
95
96 deinterleaved[0] = dst_left_.get();
97 deinterleaved[1] = dst_right_.get();
98 Interleave(deinterleaved, dst_length_mono, num_channels_, dst);
Peter Kastingdce40cf2015-08-24 21:52:2399 return static_cast<int>(dst_length_mono * num_channels_);
andrew@webrtc.org50b2efe2013-04-29 17:27:29100 } else {
Peter Kastingdce40cf2015-08-24 21:52:23101 return static_cast<int>(
102 sinc_resampler_->Resample(src, src_length, dst, dst_capacity));
andrew@webrtc.org50b2efe2013-04-29 17:27:29103 }
104}
105
andrew@webrtc.orgf5a33f12014-04-19 00:32:07106// Explictly generate required instantiations.
107template class PushResampler<int16_t>;
108template class PushResampler<float>;
109
andrew@webrtc.org50b2efe2013-04-29 17:27:29110} // namespace webrtc