Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Unified Diff: media/base/multi_channel_resampler.cc

Issue 10701049: Add MultiChannelResampler wrapper for SincResampler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments! Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/multi_channel_resampler.h ('k') | media/base/multi_channel_resampler_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « media/base/multi_channel_resampler.h ('k') | media/base/multi_channel_resampler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698