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