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/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace media { |
| 12 |
| 13 MultiChannelResampler::MultiChannelResampler(int channels, |
| 14 double io_sample_rate_ratio, |
| 15 const ReadCB& read_cb) |
| 16 : last_frame_count_(0), |
| 17 first_frame_count_(0), |
| 18 read_cb_(read_cb) { |
| 19 // Allocate each channel's resampler. |
| 20 resamplers_.reserve(channels); |
| 21 for (int i = 0; i < channels; ++i) { |
| 22 resamplers_.push_back(new SincResampler(io_sample_rate_ratio, base::Bind( |
| 23 &MultiChannelResampler::ProvideInput, base::Unretained(this), i))); |
| 24 } |
| 25 } |
| 26 |
| 27 MultiChannelResampler::~MultiChannelResampler() { |
| 28 // Clean up |resampler_audio_data_|. Skip the first channel since we never |
| 29 // allocated that, but just used the destination passed into ProvideInput(). |
| 30 for (size_t i = 1; i < resampler_audio_data_.size(); ++i) |
| 31 delete [] resampler_audio_data_[i]; |
| 32 resampler_audio_data_.clear(); |
| 33 } |
| 34 |
| 35 void MultiChannelResampler::Resample(const std::vector<float*>& destination, |
| 36 int frames) { |
| 37 DCHECK_EQ(destination.size(), resamplers_.size()); |
| 38 |
| 39 // We need to ensure that SincResampler only calls ProvideInput once for each |
| 40 // channel. To ensure this, we chunk the number of requested frames into |
| 41 // SincResampler::ChunkSize() sized chunks. SincResampler guarantees it will |
| 42 // only call ProvideInput() once when we resample this way. |
| 43 int frames_done = 0; |
| 44 int chunk_size = resamplers_[0]->ChunkSize(); |
| 45 while (frames_done < frames) { |
| 46 int frames_this_time = std::min(frames - frames_done, chunk_size); |
| 47 |
| 48 // Resample each channel. |
| 49 for (size_t i = 0; i < resamplers_.size(); ++i) { |
| 50 DCHECK_EQ(chunk_size, resamplers_[i]->ChunkSize()); |
| 51 |
| 52 // Depending on the sample-rate scale factor, and the internal buffering |
| 53 // used in a SincResampler kernel, this call to Resample() will only |
| 54 // sometimes call ProvideInput(). However, if it calls ProvideInput() for |
| 55 // the first channel, then it will call it for the remaining channels, |
| 56 // since they all buffer in the same way and are processing the same |
| 57 // number of frames. |
| 58 resamplers_[i]->Resample(destination[i] + frames_done, frames_this_time); |
| 59 } |
| 60 |
| 61 frames_done += frames_this_time; |
| 62 } |
| 63 } |
| 64 |
| 65 void MultiChannelResampler::ProvideInput(int channel, float* destination, |
| 66 int frames) { |
| 67 // Get the data from the multi-channel provider when the first channel asks |
| 68 // for it. For subsequent channels, we can just dish out the channel data |
| 69 // from that (stored in |resampler_audio_data_|). |
| 70 if (channel == 0) { |
| 71 // Allocate staging arrays on the first request. |
| 72 if (resampler_audio_data_.size() == 0) { |
| 73 first_frame_count_ = frames; |
| 74 // Skip allocation of the first buffer, since we'll use |destination| |
| 75 // directly for that. |
| 76 resampler_audio_data_.reserve(resamplers_.size()); |
| 77 resampler_audio_data_.push_back(destination); |
| 78 for (size_t i = 1; i < resamplers_.size(); ++i) |
| 79 resampler_audio_data_.push_back(new float[frames]); |
| 80 } else { |
| 81 DCHECK_LE(frames, first_frame_count_); |
| 82 resampler_audio_data_[0] = destination; |
| 83 } |
| 84 |
| 85 last_frame_count_ = frames; |
| 86 read_cb_.Run(resampler_audio_data_, frames); |
| 87 } else { |
| 88 // All channels must ask for the same amount. This should always be the |
| 89 // case, but let's just make sure. |
| 90 DCHECK_EQ(frames, last_frame_count_); |
| 91 |
| 92 // Copy the channel data from what we received from |read_cb_|. |
| 93 memcpy(destination, resampler_audio_data_[channel], |
| 94 sizeof(*resampler_audio_data_[channel]) * frames); |
| 95 } |
| 96 } |
| 97 |
| 98 } // namespace media |
OLD | NEW |