andrew@webrtc.org | dea537b | 2013-04-26 14:56:51 | [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 | |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 11 | #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
| 12 | |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 13 | #include <cstring> |
| 14 | |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 15 | #include "webrtc/common_audio/include/audio_util.h" |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 16 | #include "webrtc/rtc_base/checks.h" |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 17 | |
andrew@webrtc.org | dea537b | 2013-04-26 14:56:51 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 20 | PushSincResampler::PushSincResampler(size_t source_frames, |
| 21 | size_t destination_frames) |
andrew@webrtc.org | 221798a | 2013-10-22 12:50:00 | [diff] [blame] | 22 | : resampler_(new SincResampler(source_frames * 1.0 / destination_frames, |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 23 | source_frames, |
| 24 | this)), |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 25 | source_ptr_(nullptr), |
| 26 | source_ptr_int_(nullptr), |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 27 | destination_frames_(destination_frames), |
| 28 | first_pass_(true), |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 29 | source_available_(0) {} |
andrew@webrtc.org | dea537b | 2013-04-26 14:56:51 | [diff] [blame] | 30 | |
| 31 | PushSincResampler::~PushSincResampler() { |
| 32 | } |
| 33 | |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 34 | size_t PushSincResampler::Resample(const int16_t* source, |
| 35 | size_t source_length, |
| 36 | int16_t* destination, |
| 37 | size_t destination_capacity) { |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 38 | if (!float_buffer_.get()) |
| 39 | float_buffer_.reset(new float[destination_frames_]); |
| 40 | |
| 41 | source_ptr_int_ = source; |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 42 | // Pass nullptr as the float source to have Run() read from the int16 source. |
| 43 | Resample(nullptr, source_length, float_buffer_.get(), destination_frames_); |
andrew@webrtc.org | bce1329 | 2014-10-30 03:40:10 | [diff] [blame] | 44 | FloatS16ToS16(float_buffer_.get(), destination_frames_, destination); |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 45 | source_ptr_int_ = nullptr; |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 46 | return destination_frames_; |
| 47 | } |
| 48 | |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 49 | size_t PushSincResampler::Resample(const float* source, |
| 50 | size_t source_length, |
| 51 | float* destination, |
| 52 | size_t destination_capacity) { |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 53 | RTC_CHECK_EQ(source_length, resampler_->request_frames()); |
| 54 | RTC_CHECK_GE(destination_capacity, destination_frames_); |
andrew@webrtc.org | dea537b | 2013-04-26 14:56:51 | [diff] [blame] | 55 | // Cache the source pointer. Calling Resample() will immediately trigger |
| 56 | // the Run() callback whereupon we provide the cached value. |
| 57 | source_ptr_ = source; |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 58 | source_available_ = source_length; |
| 59 | |
| 60 | // On the first pass, we call Resample() twice. During the first call, we |
| 61 | // provide dummy input and discard the output. This is done to prime the |
| 62 | // SincResampler buffer with the correct delay (half the kernel size), thereby |
| 63 | // ensuring that all later Resample() calls will only result in one input |
| 64 | // request through Run(). |
| 65 | // |
| 66 | // If this wasn't done, SincResampler would call Run() twice on the first |
| 67 | // pass, and we'd have to introduce an entire |source_frames| of delay, rather |
| 68 | // than the minimum half kernel. |
| 69 | // |
| 70 | // It works out that ChunkSize() is exactly the amount of output we need to |
| 71 | // request in order to prime the buffer with a single Run() request for |
| 72 | // |source_frames|. |
| 73 | if (first_pass_) |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 74 | resampler_->Resample(resampler_->ChunkSize(), destination); |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 75 | |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 76 | resampler_->Resample(destination_frames_, destination); |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 77 | source_ptr_ = nullptr; |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 78 | return destination_frames_; |
andrew@webrtc.org | dea537b | 2013-04-26 14:56:51 | [diff] [blame] | 79 | } |
| 80 | |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 81 | void PushSincResampler::Run(size_t frames, float* destination) { |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 82 | // Ensure we are only asked for the available samples. This would fail if |
| 83 | // Run() was triggered more than once per Resample() call. |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 84 | RTC_CHECK_EQ(source_available_, frames); |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 85 | |
| 86 | if (first_pass_) { |
| 87 | // Provide dummy input on the first pass, the output of which will be |
| 88 | // discarded, as described in Resample(). |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 89 | std::memset(destination, 0, frames * sizeof(*destination)); |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 90 | first_pass_ = false; |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (source_ptr_) { |
andrew@webrtc.org | 4315704 | 2015-02-11 01:09:50 | [diff] [blame] | 95 | std::memcpy(destination, source_ptr_, frames * sizeof(*destination)); |
andrew@webrtc.org | 05f2131 | 2013-07-25 22:04:30 | [diff] [blame] | 96 | } else { |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 97 | for (size_t i = 0; i < frames; ++i) |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 98 | destination[i] = static_cast<float>(source_ptr_int_[i]); |
andrew@webrtc.org | dea537b | 2013-04-26 14:56:51 | [diff] [blame] | 99 | } |
andrew@webrtc.org | 6a5ccc5 | 2014-03-10 18:51:42 | [diff] [blame] | 100 | source_available_ -= frames; |
andrew@webrtc.org | dea537b | 2013-04-26 14:56:51 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace webrtc |