Chromium Code Reviews| Index: media/base/multi_channel_resampler.cc |
| diff --git a/media/base/multi_channel_resampler.cc b/media/base/multi_channel_resampler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..23479b13b7be4574edf73a1e51b19b22b40cd56c |
| --- /dev/null |
| +++ b/media/base/multi_channel_resampler.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/base/multi_channel_resampler.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/logging.h" |
| + |
| +namespace media { |
| + |
| +MultiChannelResampler::MultiChannelResampler(int channels, |
| + double io_sample_rate_ratio, |
| + const ReadCB& read_cb) |
| + : last_frame_count_(0), |
| + callback_count_(0), |
| + read_cb_(read_cb) { |
| + // Allocate each channel's resampler. |
| + resamplers_.reserve(channels); |
| + for (int i = 0; i < channels; ++i) |
| + resamplers_.push_back(new SincResampler( |
|
Ami GONE FROM CHROMIUM
2012/07/03 18:53:12
multi-line for bodies require braces
DaleCurtis
2012/07/04 00:56:19
Done.
|
| + base::Bind( |
|
Ami GONE FROM CHROMIUM
2012/07/03 18:53:12
could go on prev line
DaleCurtis
2012/07/04 00:56:19
Done.
|
| + &MultiChannelResampler::ProvideInput, base::Unretained(this), i), |
| + io_sample_rate_ratio)); |
| + |
| + // Preallocate staging arrays based on SincResampler's buffer size. |
| + resampler_audio_data_.reserve(channels); |
| + for (int i = 0; i < channels; ++i) |
| + resampler_audio_data_.push_back(new float[SincResampler::kBufferSize]); |
| +} |
| + |
| +MultiChannelResampler::~MultiChannelResampler() { |
| + // Clean up |resampler_audio_data_|. |
| + for (size_t i = 0; i < resampler_audio_data_.size(); ++i) |
| + delete [] resampler_audio_data_[i]; |
| + resampler_audio_data_.clear(); |
| +} |
| + |
| +void MultiChannelResampler::Resample(const std::vector<float*>& destination, |
| + int frames) { |
| + DCHECK_EQ(destination.size(), resamplers_.size()); |
| + DCHECK_EQ(destination.size(), resampler_audio_data_.size()); |
| + |
| + // We need to ensure that SincResampler only calls ProvideInput once for each |
| + // channel. To ensure this, we chunk the number of requested frames into |
| + // SincResampler::kBlockSize chunks for which SincResampler guarantees it will |
| + // only call ProvideInput() once. |
| + int frames_done = 0; |
| + while (frames_done < frames) { |
| + int frames_this_time = std::min( |
| + frames - frames_done, static_cast<int>(SincResampler::kBlockSize)); |
| + |
| + // Sanity check to ensure ProvideInput() is only called once per channel. |
| + callback_count_ = 0; |
| + |
| + // Resample each channel. |
| + for (size_t i = 0; i < resamplers_.size(); ++i) { |
| + // Depending on the sample-rate scale factor, and the internal buffering |
| + // used in a SincResampler kernel, this call to Resample() will only |
| + // sometimes call ProvideInput(). However, if it calls ProvideInput() for |
| + // the first channel, then it will call it for the remaining channels, |
| + // since they all buffer in the same way and are processing the same |
| + // number of frames. |
| + resamplers_[i]->Resample(destination[i] + frames_done, frames_this_time); |
| + } |
| + |
| + frames_done += frames_this_time; |
| + } |
| +} |
| + |
| +void MultiChannelResampler::ProvideInput(int channel, float* destination, |
| + int frames) { |
| + // Get the data from the multi-channel provider when the first channel asks |
| + // for it. For subsequent channels, we can just dish out the channel data |
| + // from that (stored in |resampler_audio_data_|). |
| + if (channel == 0) { |
| + DCHECK_LE(frames, SincResampler::kBufferSize); |
| + last_frame_count_ = frames; |
| + read_cb_.Run(resampler_audio_data_, frames); |
| + } |
| + |
| + // All channels must ask for the same amount. This should always be the case, |
| + // but let's just make sure. |
| + DCHECK_EQ(frames, last_frame_count_); |
| + DCHECK_EQ(channel, callback_count_++); |
|
Ami GONE FROM CHROMIUM
2012/07/03 18:53:12
I think it's poor form to have a variable that sit
DaleCurtis
2012/07/04 00:56:19
Switched to DCHECK and moved ++ out.
|
| + |
| + // Copy the channel data from what we received from |provider_|. |
| + memcpy(destination, resampler_audio_data_[channel], |
|
Ami GONE FROM CHROMIUM
2012/07/03 18:53:12
It'd be nicer to avoid this copy, but I guess that
|
| + sizeof(*resampler_audio_data_[channel]) * frames); |
| +} |
| + |
| +} // namespace media |