andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [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 | |
| 11 | #include "webrtc/common_audio/resampler/include/push_resampler.h" |
| 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <string.h> |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 14 | |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 16 | #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 | |
| 20 | namespace webrtc { |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 21 | namespace { |
| 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. |
| 26 | void 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 | |
Tommi | c47f009 | 2016-05-26 22:55:35 +0200 | [diff] [blame^] | 34 | void CheckExpectedBufferSizes(size_t src_length, |
| 35 | size_t dst_capacity, |
| 36 | size_t num_channels, |
| 37 | int src_sample_rate, |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 38 | int dst_sample_rate) { |
| 39 | const size_t src_size_10ms = src_sample_rate * num_channels / 100; |
| 40 | const size_t dst_size_10ms = dst_sample_rate * num_channels / 100; |
| 41 | RTC_CHECK_EQ(src_length, src_size_10ms); |
| 42 | RTC_CHECK_GE(dst_capacity, dst_size_10ms); |
| 43 | } |
| 44 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 45 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 46 | template <typename T> |
| 47 | PushResampler<T>::PushResampler() |
andrew@webrtc.org | 31628aa | 2013-10-22 12:50:00 +0000 | [diff] [blame] | 48 | : src_sample_rate_hz_(0), |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 49 | dst_sample_rate_hz_(0), |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 50 | num_channels_(0) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 51 | } |
| 52 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 53 | template <typename T> |
| 54 | PushResampler<T>::~PushResampler() { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 57 | template <typename T> |
| 58 | int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz, |
| 59 | int dst_sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 60 | size_t num_channels) { |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 61 | CheckValidInitParams(src_sample_rate_hz, dst_sample_rate_hz, num_channels); |
| 62 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 63 | if (src_sample_rate_hz == src_sample_rate_hz_ && |
| 64 | dst_sample_rate_hz == dst_sample_rate_hz_ && |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 65 | num_channels == num_channels_) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 66 | // No-op if settings haven't changed. |
| 67 | return 0; |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 68 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 69 | |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 70 | if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 || |
| 71 | num_channels > 2) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 72 | return -1; |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 73 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 74 | |
| 75 | src_sample_rate_hz_ = src_sample_rate_hz; |
| 76 | dst_sample_rate_hz_ = dst_sample_rate_hz; |
| 77 | num_channels_ = num_channels; |
| 78 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 79 | const size_t src_size_10ms_mono = |
| 80 | static_cast<size_t>(src_sample_rate_hz / 100); |
| 81 | const size_t dst_size_10ms_mono = |
| 82 | static_cast<size_t>(dst_sample_rate_hz / 100); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 83 | sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono, |
| 84 | dst_size_10ms_mono)); |
| 85 | if (num_channels_ == 2) { |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 86 | src_left_.reset(new T[src_size_10ms_mono]); |
| 87 | src_right_.reset(new T[src_size_10ms_mono]); |
| 88 | dst_left_.reset(new T[dst_size_10ms_mono]); |
| 89 | dst_right_.reset(new T[dst_size_10ms_mono]); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 90 | sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono, |
| 91 | dst_size_10ms_mono)); |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 97 | template <typename T> |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 98 | int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst, |
| 99 | size_t dst_capacity) { |
Tommi | c47f009 | 2016-05-26 22:55:35 +0200 | [diff] [blame^] | 100 | CheckExpectedBufferSizes(src_length, dst_capacity, num_channels_, |
| 101 | src_sample_rate_hz_, dst_sample_rate_hz_); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 102 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 103 | if (src_sample_rate_hz_ == dst_sample_rate_hz_) { |
| 104 | // The old resampler provides this memcpy facility in the case of matching |
| 105 | // sample rates, so reproduce it here for the sinc resampler. |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 106 | memcpy(dst, src, src_length * sizeof(T)); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 107 | return static_cast<int>(src_length); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 108 | } |
| 109 | if (num_channels_ == 2) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 110 | const size_t src_length_mono = src_length / num_channels_; |
| 111 | const size_t dst_capacity_mono = dst_capacity / num_channels_; |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 112 | T* deinterleaved[] = {src_left_.get(), src_right_.get()}; |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 113 | Deinterleave(src, src_length_mono, num_channels_, deinterleaved); |
| 114 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 115 | size_t dst_length_mono = |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 116 | sinc_resampler_->Resample(src_left_.get(), src_length_mono, |
| 117 | dst_left_.get(), dst_capacity_mono); |
| 118 | sinc_resampler_right_->Resample(src_right_.get(), src_length_mono, |
| 119 | dst_right_.get(), dst_capacity_mono); |
| 120 | |
| 121 | deinterleaved[0] = dst_left_.get(); |
| 122 | deinterleaved[1] = dst_right_.get(); |
| 123 | Interleave(deinterleaved, dst_length_mono, num_channels_, dst); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 124 | return static_cast<int>(dst_length_mono * num_channels_); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 125 | } else { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 126 | return static_cast<int>( |
| 127 | sinc_resampler_->Resample(src, src_length, dst, dst_capacity)); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 131 | // Explictly generate required instantiations. |
| 132 | template class PushResampler<int16_t>; |
| 133 | template class PushResampler<float>; |
| 134 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 135 | } // namespace webrtc |