andrew@webrtc.org | b6fadb1 | 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 | |
| 13 | #include <cstring> |
| 14 | |
| 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 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | PushResampler::PushResampler() |
andrew@webrtc.org | 92bfbbd | 2013-06-03 19:00:29 +0000 | [diff] [blame] | 22 | : sinc_resampler_(NULL), |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 23 | sinc_resampler_right_(NULL), |
| 24 | src_sample_rate_hz_(0), |
| 25 | dst_sample_rate_hz_(0), |
| 26 | num_channels_(0), |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 27 | src_left_(NULL), |
| 28 | src_right_(NULL), |
| 29 | dst_left_(NULL), |
| 30 | dst_right_(NULL) { |
| 31 | } |
| 32 | |
| 33 | PushResampler::~PushResampler() { |
| 34 | } |
| 35 | |
| 36 | int PushResampler::InitializeIfNeeded(int src_sample_rate_hz, |
| 37 | int dst_sample_rate_hz, |
| 38 | int num_channels) { |
| 39 | if (src_sample_rate_hz == src_sample_rate_hz_ && |
| 40 | dst_sample_rate_hz == dst_sample_rate_hz_ && |
| 41 | num_channels == num_channels_) { |
| 42 | // No-op if settings haven't changed. |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || |
| 47 | num_channels <= 0 || num_channels > 2) { |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | src_sample_rate_hz_ = src_sample_rate_hz; |
| 52 | dst_sample_rate_hz_ = dst_sample_rate_hz; |
| 53 | num_channels_ = num_channels; |
| 54 | |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 55 | const int src_size_10ms_mono = src_sample_rate_hz / 100; |
| 56 | const int dst_size_10ms_mono = dst_sample_rate_hz / 100; |
| 57 | sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono, |
| 58 | dst_size_10ms_mono)); |
| 59 | if (num_channels_ == 2) { |
| 60 | src_left_.reset(new int16_t[src_size_10ms_mono]); |
| 61 | src_right_.reset(new int16_t[src_size_10ms_mono]); |
| 62 | dst_left_.reset(new int16_t[dst_size_10ms_mono]); |
| 63 | dst_right_.reset(new int16_t[dst_size_10ms_mono]); |
| 64 | sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono, |
| 65 | dst_size_10ms_mono)); |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int PushResampler::Resample(const int16_t* src, int src_length, |
| 72 | int16_t* dst, int dst_capacity) { |
| 73 | const int src_size_10ms = src_sample_rate_hz_ * num_channels_ / 100; |
| 74 | const int dst_size_10ms = dst_sample_rate_hz_ * num_channels_ / 100; |
| 75 | if (src_length != src_size_10ms || dst_capacity < dst_size_10ms) { |
| 76 | return -1; |
| 77 | } |
| 78 | |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 79 | if (src_sample_rate_hz_ == dst_sample_rate_hz_) { |
| 80 | // The old resampler provides this memcpy facility in the case of matching |
| 81 | // sample rates, so reproduce it here for the sinc resampler. |
| 82 | memcpy(dst, src, src_length * sizeof(int16_t)); |
| 83 | return src_length; |
| 84 | } |
| 85 | if (num_channels_ == 2) { |
| 86 | const int src_length_mono = src_length / num_channels_; |
| 87 | const int dst_capacity_mono = dst_capacity / num_channels_; |
| 88 | int16_t* deinterleaved[] = {src_left_.get(), src_right_.get()}; |
| 89 | Deinterleave(src, src_length_mono, num_channels_, deinterleaved); |
| 90 | |
| 91 | int dst_length_mono = |
| 92 | sinc_resampler_->Resample(src_left_.get(), src_length_mono, |
| 93 | dst_left_.get(), dst_capacity_mono); |
| 94 | sinc_resampler_right_->Resample(src_right_.get(), src_length_mono, |
| 95 | dst_right_.get(), dst_capacity_mono); |
| 96 | |
| 97 | deinterleaved[0] = dst_left_.get(); |
| 98 | deinterleaved[1] = dst_right_.get(); |
| 99 | Interleave(deinterleaved, dst_length_mono, num_channels_, dst); |
| 100 | return dst_length_mono * num_channels_; |
| 101 | } else { |
| 102 | return sinc_resampler_->Resample(src, src_length, dst, dst_capacity); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } // namespace webrtc |