andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "common_audio/resampler/include/push_resampler.h" |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 13 | #include <stdint.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 | [diff] [blame] | 14 | #include <string.h> |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 15 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "common_audio/include/audio_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "common_audio/resampler/push_sinc_resampler.h" |
| 20 | #include "rtc_base/checks.h" |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 24 | template <typename T> |
| 25 | PushResampler<T>::PushResampler() |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 26 | : src_sample_rate_hz_(0), dst_sample_rate_hz_(0), num_channels_(0) {} |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 27 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 28 | template <typename T> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 29 | PushResampler<T>::~PushResampler() {} |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 30 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 31 | template <typename T> |
| 32 | int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz, |
| 33 | int dst_sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 34 | size_t num_channels) { |
Sam Zackrisson | 3bfafb5 | 2022-08-01 09:30:48 | [diff] [blame] | 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); |
Tommi | f4fc0ff | 2016-05-26 20:40:09 | [diff] [blame] | 40 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 41 | if (src_sample_rate_hz == src_sample_rate_hz_ && |
| 42 | dst_sample_rate_hz == dst_sample_rate_hz_ && |
Tommi | f4fc0ff | 2016-05-26 20:40:09 | [diff] [blame] | 43 | num_channels == num_channels_) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 44 | // No-op if settings haven't changed. |
| 45 | return 0; |
Tommi | f4fc0ff | 2016-05-26 20:40:09 | [diff] [blame] | 46 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 47 | |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 48 | if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 49 | return -1; |
Tommi | f4fc0ff | 2016-05-26 20:40:09 | [diff] [blame] | 50 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 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 | |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 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); |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 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(); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 64 | channel_resampler->resampler = std::make_unique<PushSincResampler>( |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 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); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 68 | } |
| 69 | |
Per Åhgren | 3fdb3cb | 2019-12-11 09:57:13 | [diff] [blame] | 70 | channel_data_array_.resize(num_channels_); |
| 71 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 75 | template <typename T> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 76 | int PushResampler<T>::Resample(const T* src, |
| 77 | size_t src_length, |
| 78 | T* dst, |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 79 | size_t dst_capacity) { |
Sam Zackrisson | 3bfafb5 | 2022-08-01 09:30:48 | [diff] [blame] | 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); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 86 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 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. |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 90 | memcpy(dst, src, src_length * sizeof(T)); |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 91 | return static_cast<int>(src_length); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 92 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 93 | |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 94 | const size_t src_length_mono = src_length / num_channels_; |
| 95 | const size_t dst_capacity_mono = dst_capacity / num_channels_; |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 96 | |
Per Åhgren | 3fdb3cb | 2019-12-11 09:57:13 | [diff] [blame] | 97 | for (size_t ch = 0; ch < num_channels_; ++ch) { |
| 98 | channel_data_array_[ch] = channel_resamplers_[ch].source.data(); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 99 | } |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 100 | |
Per Åhgren | 3fdb3cb | 2019-12-11 09:57:13 | [diff] [blame] | 101 | Deinterleave(src, src_length_mono, num_channels_, channel_data_array_.data()); |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 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 | |
Per Åhgren | 3fdb3cb | 2019-12-11 09:57:13 | [diff] [blame] | 111 | for (size_t ch = 0; ch < num_channels_; ++ch) { |
| 112 | channel_data_array_[ch] = channel_resamplers_[ch].destination.data(); |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 113 | } |
| 114 | |
Per Åhgren | 3fdb3cb | 2019-12-11 09:57:13 | [diff] [blame] | 115 | Interleave(channel_data_array_.data(), dst_length_mono, num_channels_, dst); |
Alex Loiko | 3fc5a20 | 2018-10-02 12:09:46 | [diff] [blame] | 116 | return static_cast<int>(dst_length_mono * num_channels_); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 117 | } |
| 118 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 119 | // Explictly generate required instantiations. |
| 120 | template class PushResampler<int16_t>; |
| 121 | template class PushResampler<float>; |
| 122 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 | [diff] [blame] | 123 | } // namespace webrtc |