Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/common_audio/resampler/push_sinc_resampler.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

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