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..0236aa59c4f0362be5a7c2198a730a42a0b46694 |
--- /dev/null |
+++ b/media/base/multi_channel_resampler.cc |
@@ -0,0 +1,98 @@ |
+// 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/logging.h" |
+ |
+namespace media { |
+ |
+MultiChannelResampler::MultiChannelResampler( |
+ MultiChannelAudioSourceProvider* provider, double scale_factor, |
+ int number_of_channels) |
+ : number_of_channels_(number_of_channels), |
+ channel_index_(0), |
+ last_frame_count_(0), |
+ callback_count_(0), |
+ provider_(provider) { |
+ DCHECK_GT(number_of_channels, 0); |
+ |
+ // Preallocate staging arrays based on SincResampler's buffer size. |
+ resampler_audio_data_.reserve(number_of_channels); |
+ for (int i = 0; i < number_of_channels; ++i) |
+ resampler_audio_data_.push_back(new float[SincResampler::kBufferSize]); |
+ |
+ // Create each channel's resampler. |
+ resamplers_.reserve(number_of_channels); |
+ for (int i = 0; i < number_of_channels; ++i) |
+ resamplers_.push_back(new SincResampler(this, scale_factor)); |
+} |
+ |
+MultiChannelResampler::~MultiChannelResampler() { |
+ // Clean up |resampler_audio_data_|. |
Ami GONE FROM CHROMIUM
2012/06/30 20:12:14
With my scoped* suggestions in the .h file, this d
DaleCurtis
2012/07/03 03:02:57
Removed clean up of resamplers_
|
+ for (size_t i = 0; i < resampler_audio_data_.size(); ++i) |
+ delete [] resampler_audio_data_[i]; |
+ resampler_audio_data_.clear(); |
+ |
+ // Clean up |resamplers_|. |
+ for (size_t i = 0; i < resamplers_.size(); ++i) |
+ delete resamplers_[i]; |
+ resamplers_.clear(); |
+} |
+ |
+void MultiChannelResampler::Resample(const std::vector<float*>& audio_data, |
+ int number_of_frames) { |
+ // We need to ensure that SincResampler only calls ProvideInput once for each |
Ami GONE FROM CHROMIUM
2012/06/30 20:12:14
An alternative, of course, is to allow SR to call
DaleCurtis
2012/07/01 23:27:46
Yeah, I'm not super happy with the chunking design
Ami GONE FROM CHROMIUM
2012/07/01 23:44:44
I don't follow. Why would you need to reallocate
DaleCurtis
2012/07/02 17:43:45
If SR calls PI as many times as it wants for a sin
DaleCurtis
2012/07/03 03:02:57
After discussion and partial implementation this i
|
+ // 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 < number_of_frames) { |
+ int frames_this_time = std::min( |
+ number_of_frames - frames_done, SincResampler::kBlockSize); |
+ |
+ // Sanity check to ensure ProvideInput is only called once per channel. The |
+ // value should track |channel_index_| when checked inside ProvideInput(). |
+ callback_count_ = 0; |
+ |
+ // Resample each channel. |
+ for (channel_index_ = 0; channel_index_ < resamplers_.size(); |
+ ++channel_index_) { |
+ // 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_[channel_index_]->Resample( |
+ audio_data[channel_index_] + frames_done, frames_this_time); |
+ } |
+ |
+ frames_done += frames_this_time; |
+ } |
+} |
+ |
+void MultiChannelResampler::ProvideInput(float* destination, |
+ int number_of_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_index_) { |
+ DCHECK_LE(number_of_frames, SincResampler::kBufferSize); |
+ last_frame_count_ = number_of_frames; |
+ provider_->ProvideInput(resampler_audio_data_, number_of_frames); |
+ } |
+ |
+ // All channels must ask for the same amount. This should always be the case, |
+ // but let's just make sure. |
+ DCHECK_EQ(number_of_frames, last_frame_count_); |
+ |
+ // Copy the channel data from what we received from |provider_|. |
+ DCHECK_LT(channel_index_, static_cast<size_t>(number_of_channels_)); |
+ DCHECK_EQ(channel_index_, callback_count_++); |
Ami GONE FROM CHROMIUM
2012/06/30 20:12:14
Bug: this line is compiled out of Release builds,
DaleCurtis
2012/07/01 23:27:46
Whoops! I wonder if that's the source of my memory
DaleCurtis
2012/07/03 03:02:57
Actually this doesn't matter as it's only used for
|
+ memcpy(destination, resampler_audio_data_[channel_index_], |
+ sizeof(*resampler_audio_data_[0]) * number_of_frames); |
+} |
+ |
+} // namespace media |