Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/multi_channel_resampler.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace media { | |
| 10 | |
| 11 MultiChannelResampler::MultiChannelResampler( | |
| 12 MultiChannelAudioSourceProvider* provider, double scale_factor, | |
| 13 int number_of_channels) | |
| 14 : number_of_channels_(number_of_channels), | |
| 15 channel_index_(0), | |
| 16 last_frame_count_(0), | |
| 17 callback_count_(0), | |
| 18 provider_(provider) { | |
| 19 DCHECK_GT(number_of_channels, 0); | |
| 20 | |
| 21 // Preallocate staging arrays based on SincResampler's buffer size. | |
| 22 resampler_audio_data_.reserve(number_of_channels); | |
| 23 for (int i = 0; i < number_of_channels; ++i) | |
| 24 resampler_audio_data_.push_back(new float[SincResampler::kBufferSize]); | |
| 25 | |
| 26 // Create each channel's resampler. | |
| 27 resamplers_.reserve(number_of_channels); | |
| 28 for (int i = 0; i < number_of_channels; ++i) | |
| 29 resamplers_.push_back(new SincResampler(this, scale_factor)); | |
| 30 } | |
| 31 | |
| 32 MultiChannelResampler::~MultiChannelResampler() { | |
| 33 // 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_
| |
| 34 for (size_t i = 0; i < resampler_audio_data_.size(); ++i) | |
| 35 delete [] resampler_audio_data_[i]; | |
| 36 resampler_audio_data_.clear(); | |
| 37 | |
| 38 // Clean up |resamplers_|. | |
| 39 for (size_t i = 0; i < resamplers_.size(); ++i) | |
| 40 delete resamplers_[i]; | |
| 41 resamplers_.clear(); | |
| 42 } | |
| 43 | |
| 44 void MultiChannelResampler::Resample(const std::vector<float*>& audio_data, | |
| 45 int number_of_frames) { | |
| 46 // 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
| |
| 47 // channel. To ensure this, we chunk the number of requested frames into | |
| 48 // SincResampler::kBlockSize chunks for which SincResampler guarantees it will | |
| 49 // only call ProvideInput() once. | |
| 50 int frames_done = 0; | |
| 51 while (frames_done < number_of_frames) { | |
| 52 int frames_this_time = std::min( | |
| 53 number_of_frames - frames_done, SincResampler::kBlockSize); | |
| 54 | |
| 55 // Sanity check to ensure ProvideInput is only called once per channel. The | |
| 56 // value should track |channel_index_| when checked inside ProvideInput(). | |
| 57 callback_count_ = 0; | |
| 58 | |
| 59 // Resample each channel. | |
| 60 for (channel_index_ = 0; channel_index_ < resamplers_.size(); | |
| 61 ++channel_index_) { | |
| 62 // Depending on the sample-rate scale factor, and the internal buffering | |
| 63 // used in a SincResampler kernel, this call to Resample() will only | |
| 64 // sometimes call ProvideInput(). However, if it calls ProvideInput() for | |
| 65 // the first channel, then it will call it for the remaining channels, | |
| 66 // since they all buffer in the same way and are processing the same | |
| 67 // number of frames. | |
| 68 resamplers_[channel_index_]->Resample( | |
| 69 audio_data[channel_index_] + frames_done, frames_this_time); | |
| 70 } | |
| 71 | |
| 72 frames_done += frames_this_time; | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void MultiChannelResampler::ProvideInput(float* destination, | |
| 77 int number_of_frames) { | |
| 78 // Get the data from the multi-channel provider when the first channel asks | |
| 79 // for it. For subsequent channels, we can just dish out the channel data | |
| 80 // from that (stored in |resampler_audio_data_|). | |
| 81 if (!channel_index_) { | |
| 82 DCHECK_LE(number_of_frames, SincResampler::kBufferSize); | |
| 83 last_frame_count_ = number_of_frames; | |
| 84 provider_->ProvideInput(resampler_audio_data_, number_of_frames); | |
| 85 } | |
| 86 | |
| 87 // All channels must ask for the same amount. This should always be the case, | |
| 88 // but let's just make sure. | |
| 89 DCHECK_EQ(number_of_frames, last_frame_count_); | |
| 90 | |
| 91 // Copy the channel data from what we received from |provider_|. | |
| 92 DCHECK_LT(channel_index_, static_cast<size_t>(number_of_channels_)); | |
| 93 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
| |
| 94 memcpy(destination, resampler_audio_data_[channel_index_], | |
| 95 sizeof(*resampler_audio_data_[0]) * number_of_frames); | |
| 96 } | |
| 97 | |
| 98 } // namespace media | |
| OLD | NEW |