blob: 810d7789933044a90450c68d54c19347699ce83c [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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "common_audio/resampler/include/push_resampler.h"
andrew@webrtc.org50b2efe2013-04-29 17:27:2912
Yves Gerey988cc082018-10-23 10:03:0113#include <stdint.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:5314#include <string.h>
andrew@webrtc.org50b2efe2013-04-29 17:27:2915
Mirko Bonadei317a1f02019-09-17 15:06:1816#include <memory>
17
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "common_audio/include/audio_util.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "common_audio/resampler/push_sinc_resampler.h"
20#include "rtc_base/checks.h"
andrew@webrtc.org50b2efe2013-04-29 17:27:2921
22namespace webrtc {
23
andrew@webrtc.orgf5a33f12014-04-19 00:32:0724template <typename T>
25PushResampler<T>::PushResampler()
Yves Gerey665174f2018-06-19 13:03:0526 : src_sample_rate_hz_(0), dst_sample_rate_hz_(0), num_channels_(0) {}
andrew@webrtc.org50b2efe2013-04-29 17:27:2927
andrew@webrtc.orgf5a33f12014-04-19 00:32:0728template <typename T>
Yves Gerey665174f2018-06-19 13:03:0529PushResampler<T>::~PushResampler() {}
andrew@webrtc.org50b2efe2013-04-29 17:27:2930
andrew@webrtc.orgf5a33f12014-04-19 00:32:0731template <typename T>
32int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
33 int dst_sample_rate_hz,
Peter Kasting69558702016-01-13 00:26:3534 size_t num_channels) {
Sam Zackrisson3bfafb52022-08-01 09:30:4835 // 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);
Tommif4fc0ff2016-05-26 20:40:0940
andrew@webrtc.org50b2efe2013-04-29 17:27:2941 if (src_sample_rate_hz == src_sample_rate_hz_ &&
42 dst_sample_rate_hz == dst_sample_rate_hz_ &&
Tommif4fc0ff2016-05-26 20:40:0943 num_channels == num_channels_) {
andrew@webrtc.org50b2efe2013-04-29 17:27:2944 // No-op if settings haven't changed.
45 return 0;
Tommif4fc0ff2016-05-26 20:40:0946 }
andrew@webrtc.org50b2efe2013-04-29 17:27:2947
Alex Loiko3fc5a202018-10-02 12:09:4648 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0) {
andrew@webrtc.org50b2efe2013-04-29 17:27:2949 return -1;
Tommif4fc0ff2016-05-26 20:40:0950 }
andrew@webrtc.org50b2efe2013-04-29 17:27:2951
52 src_sample_rate_hz_ = src_sample_rate_hz;
53 dst_sample_rate_hz_ = dst_sample_rate_hz;
54 num_channels_ = num_channels;
55
Peter Kastingdce40cf2015-08-24 21:52:2356 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);
Alex Loiko3fc5a202018-10-02 12:09:4660 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();
Mirko Bonadei317a1f02019-09-17 15:06:1864 channel_resampler->resampler = std::make_unique<PushSincResampler>(
Alex Loiko3fc5a202018-10-02 12:09:4665 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);
andrew@webrtc.org50b2efe2013-04-29 17:27:2968 }
69
Per Åhgren3fdb3cb2019-12-11 09:57:1370 channel_data_array_.resize(num_channels_);
71
andrew@webrtc.org50b2efe2013-04-29 17:27:2972 return 0;
73}
74
andrew@webrtc.orgf5a33f12014-04-19 00:32:0775template <typename T>
Yves Gerey665174f2018-06-19 13:03:0576int PushResampler<T>::Resample(const T* src,
77 size_t src_length,
78 T* dst,
Peter Kastingdce40cf2015-08-24 21:52:2379 size_t dst_capacity) {
Sam Zackrisson3bfafb52022-08-01 09:30:4880 // 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);
andrew@webrtc.org50b2efe2013-04-29 17:27:2986
andrew@webrtc.org50b2efe2013-04-29 17:27:2987 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.
andrew@webrtc.orgf5a33f12014-04-19 00:32:0790 memcpy(dst, src, src_length * sizeof(T));
Peter Kastingdce40cf2015-08-24 21:52:2391 return static_cast<int>(src_length);
andrew@webrtc.org50b2efe2013-04-29 17:27:2992 }
andrew@webrtc.org50b2efe2013-04-29 17:27:2993
Alex Loiko3fc5a202018-10-02 12:09:4694 const size_t src_length_mono = src_length / num_channels_;
95 const size_t dst_capacity_mono = dst_capacity / num_channels_;
andrew@webrtc.org50b2efe2013-04-29 17:27:2996
Per Åhgren3fdb3cb2019-12-11 09:57:1397 for (size_t ch = 0; ch < num_channels_; ++ch) {
98 channel_data_array_[ch] = channel_resamplers_[ch].source.data();
andrew@webrtc.org50b2efe2013-04-29 17:27:2999 }
Alex Loiko3fc5a202018-10-02 12:09:46100
Per Åhgren3fdb3cb2019-12-11 09:57:13101 Deinterleave(src, src_length_mono, num_channels_, channel_data_array_.data());
Alex Loiko3fc5a202018-10-02 12:09:46102
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
Per Åhgren3fdb3cb2019-12-11 09:57:13111 for (size_t ch = 0; ch < num_channels_; ++ch) {
112 channel_data_array_[ch] = channel_resamplers_[ch].destination.data();
Alex Loiko3fc5a202018-10-02 12:09:46113 }
114
Per Åhgren3fdb3cb2019-12-11 09:57:13115 Interleave(channel_data_array_.data(), dst_length_mono, num_channels_, dst);
Alex Loiko3fc5a202018-10-02 12:09:46116 return static_cast<int>(dst_length_mono * num_channels_);
andrew@webrtc.org50b2efe2013-04-29 17:27:29117}
118
andrew@webrtc.orgf5a33f12014-04-19 00:32:07119// Explictly generate required instantiations.
120template class PushResampler<int16_t>;
121template class PushResampler<float>;
122
andrew@webrtc.org50b2efe2013-04-29 17:27:29123} // namespace webrtc