| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/common_audio/resampler/include/push_resampler.h" | 11 #include "webrtc/common_audio/resampler/include/push_resampler.h" |
| 12 | 12 |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | |
| 16 #include "webrtc/common_audio/include/audio_util.h" | 15 #include "webrtc/common_audio/include/audio_util.h" |
| 17 #include "webrtc/common_audio/resampler/include/resampler.h" | 16 #include "webrtc/common_audio/resampler/include/resampler.h" |
| 18 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" | 17 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
| 19 | 18 |
| 20 namespace webrtc { | 19 namespace webrtc { |
| 21 | 20 |
| 22 template <typename T> | 21 template <typename T> |
| 23 PushResampler<T>::PushResampler() | 22 PushResampler<T>::PushResampler() |
| 24 : src_sample_rate_hz_(0), | 23 : src_sample_rate_hz_(0), |
| 25 dst_sample_rate_hz_(0), | 24 dst_sample_rate_hz_(0), |
| 26 num_channels_(0) { | 25 num_channels_(0) { |
| 27 } | 26 } |
| 28 | 27 |
| 29 template <typename T> | 28 template <typename T> |
| 30 PushResampler<T>::~PushResampler() { | 29 PushResampler<T>::~PushResampler() { |
| 31 } | 30 } |
| 32 | 31 |
| 33 template <typename T> | 32 template <typename T> |
| 34 int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz, | 33 int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz, |
| 35 int dst_sample_rate_hz, | 34 int dst_sample_rate_hz, |
| 36 size_t num_channels) { | 35 size_t num_channels) { |
| 37 RTC_DCHECK_GT(src_sample_rate_hz, 0); | |
| 38 RTC_DCHECK_GT(dst_sample_rate_hz, 0); | |
| 39 RTC_DCHECK_GT(num_channels, 0u); | |
| 40 RTC_DCHECK_LE(num_channels, 2u); | |
| 41 | |
| 42 if (src_sample_rate_hz == src_sample_rate_hz_ && | 36 if (src_sample_rate_hz == src_sample_rate_hz_ && |
| 43 dst_sample_rate_hz == dst_sample_rate_hz_ && | 37 dst_sample_rate_hz == dst_sample_rate_hz_ && |
| 44 num_channels == num_channels_) { | 38 num_channels == num_channels_) |
| 45 // No-op if settings haven't changed. | 39 // No-op if settings haven't changed. |
| 46 return 0; | 40 return 0; |
| 47 } | |
| 48 | 41 |
| 49 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 || | 42 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || |
| 50 num_channels > 2) { | 43 num_channels <= 0 || num_channels > 2) |
| 51 return -1; | 44 return -1; |
| 52 } | |
| 53 | 45 |
| 54 src_sample_rate_hz_ = src_sample_rate_hz; | 46 src_sample_rate_hz_ = src_sample_rate_hz; |
| 55 dst_sample_rate_hz_ = dst_sample_rate_hz; | 47 dst_sample_rate_hz_ = dst_sample_rate_hz; |
| 56 num_channels_ = num_channels; | 48 num_channels_ = num_channels; |
| 57 | 49 |
| 58 const size_t src_size_10ms_mono = | 50 const size_t src_size_10ms_mono = |
| 59 static_cast<size_t>(src_sample_rate_hz / 100); | 51 static_cast<size_t>(src_sample_rate_hz / 100); |
| 60 const size_t dst_size_10ms_mono = | 52 const size_t dst_size_10ms_mono = |
| 61 static_cast<size_t>(dst_sample_rate_hz / 100); | 53 static_cast<size_t>(dst_sample_rate_hz / 100); |
| 62 sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono, | 54 sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono, |
| 63 dst_size_10ms_mono)); | 55 dst_size_10ms_mono)); |
| 64 if (num_channels_ == 2) { | 56 if (num_channels_ == 2) { |
| 65 src_left_.reset(new T[src_size_10ms_mono]); | 57 src_left_.reset(new T[src_size_10ms_mono]); |
| 66 src_right_.reset(new T[src_size_10ms_mono]); | 58 src_right_.reset(new T[src_size_10ms_mono]); |
| 67 dst_left_.reset(new T[dst_size_10ms_mono]); | 59 dst_left_.reset(new T[dst_size_10ms_mono]); |
| 68 dst_right_.reset(new T[dst_size_10ms_mono]); | 60 dst_right_.reset(new T[dst_size_10ms_mono]); |
| 69 sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono, | 61 sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono, |
| 70 dst_size_10ms_mono)); | 62 dst_size_10ms_mono)); |
| 71 } | 63 } |
| 72 | 64 |
| 73 return 0; | 65 return 0; |
| 74 } | 66 } |
| 75 | 67 |
| 76 template <typename T> | 68 template <typename T> |
| 77 int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst, | 69 int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst, |
| 78 size_t dst_capacity) { | 70 size_t dst_capacity) { |
| 79 const size_t src_size_10ms = src_sample_rate_hz_ * num_channels_ / 100; | 71 const size_t src_size_10ms = src_sample_rate_hz_ * num_channels_ / 100; |
| 80 const size_t dst_size_10ms = dst_sample_rate_hz_ * num_channels_ / 100; | 72 const size_t dst_size_10ms = dst_sample_rate_hz_ * num_channels_ / 100; |
| 81 RTC_CHECK_EQ(src_length, src_size_10ms); | |
| 82 RTC_CHECK_GE(dst_capacity, dst_size_10ms); | |
| 83 if (src_length != src_size_10ms || dst_capacity < dst_size_10ms) | 73 if (src_length != src_size_10ms || dst_capacity < dst_size_10ms) |
| 84 return -1; | 74 return -1; |
| 85 | 75 |
| 86 if (src_sample_rate_hz_ == dst_sample_rate_hz_) { | 76 if (src_sample_rate_hz_ == dst_sample_rate_hz_) { |
| 87 // The old resampler provides this memcpy facility in the case of matching | 77 // The old resampler provides this memcpy facility in the case of matching |
| 88 // sample rates, so reproduce it here for the sinc resampler. | 78 // sample rates, so reproduce it here for the sinc resampler. |
| 89 memcpy(dst, src, src_length * sizeof(T)); | 79 memcpy(dst, src, src_length * sizeof(T)); |
| 90 return static_cast<int>(src_length); | 80 return static_cast<int>(src_length); |
| 91 } | 81 } |
| 92 if (num_channels_ == 2) { | 82 if (num_channels_ == 2) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 109 return static_cast<int>( | 99 return static_cast<int>( |
| 110 sinc_resampler_->Resample(src, src_length, dst, dst_capacity)); | 100 sinc_resampler_->Resample(src, src_length, dst, dst_capacity)); |
| 111 } | 101 } |
| 112 } | 102 } |
| 113 | 103 |
| 114 // Explictly generate required instantiations. | 104 // Explictly generate required instantiations. |
| 115 template class PushResampler<int16_t>; | 105 template class PushResampler<int16_t>; |
| 116 template class PushResampler<float>; | 106 template class PushResampler<float>; |
| 117 | 107 |
| 118 } // namespace webrtc | 108 } // namespace webrtc |
| OLD | NEW |