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