Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/multi_channel_resampler.h" | 5 #include "media/base/multi_channel_resampler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/base/audio_bus.h" | 10 #include "media/base/audio_bus.h" |
| 11 | 11 |
| 12 namespace media { | 12 namespace media { |
| 13 | 13 |
| 14 MultiChannelResampler::MultiChannelResampler(int channels, | 14 MultiChannelResampler::MultiChannelResampler(int channels, |
| 15 double io_sample_rate_ratio, | 15 double io_sample_rate_ratio, |
| 16 const ReadCB& read_cb) | 16 const ReadCB& read_cb) |
| 17 : last_frame_count_(0), | 17 : last_frame_count_(0), |
| 18 read_cb_(read_cb) { | 18 read_cb_(read_cb), |
| 19 output_frames_ready_(0) { | |
| 19 // Allocate each channel's resampler. | 20 // Allocate each channel's resampler. |
| 20 resamplers_.reserve(channels); | 21 resamplers_.reserve(channels); |
| 21 for (int i = 0; i < channels; ++i) { | 22 for (int i = 0; i < channels; ++i) { |
| 22 resamplers_.push_back(new SincResampler(io_sample_rate_ratio, base::Bind( | 23 resamplers_.push_back(new SincResampler(io_sample_rate_ratio, base::Bind( |
| 23 &MultiChannelResampler::ProvideInput, base::Unretained(this), i))); | 24 &MultiChannelResampler::ProvideInput, base::Unretained(this), i))); |
| 24 } | 25 } |
| 25 } | 26 } |
| 26 | 27 |
| 27 MultiChannelResampler::~MultiChannelResampler() {} | 28 MultiChannelResampler::~MultiChannelResampler() {} |
| 28 | 29 |
| 29 void MultiChannelResampler::Resample(AudioBus* audio_bus, int frames) { | 30 void MultiChannelResampler::Resample(AudioBus* audio_bus, int frames) { |
| 30 DCHECK_EQ(static_cast<size_t>(audio_bus->channels()), resamplers_.size()); | 31 DCHECK_EQ(static_cast<size_t>(audio_bus->channels()), resamplers_.size()); |
| 31 | 32 |
| 32 // We need to ensure that SincResampler only calls ProvideInput once for each | 33 // We need to ensure that SincResampler only calls ProvideInput once for each |
| 33 // channel. To ensure this, we chunk the number of requested frames into | 34 // channel. To ensure this, we chunk the number of requested frames into |
| 34 // SincResampler::ChunkSize() sized chunks. SincResampler guarantees it will | 35 // SincResampler::ChunkSize() sized chunks. SincResampler guarantees it will |
| 35 // only call ProvideInput() once when we resample this way. | 36 // only call ProvideInput() once when we resample this way. |
| 36 int frames_done = 0; | 37 output_frames_ready_ = 0; |
|
Chris Rogers
2012/11/14 23:50:49
As we discussed, I think we need much better comme
| |
| 37 int chunk_size = resamplers_[0]->ChunkSize(); | 38 int chunk_size = resamplers_[0]->ChunkSize(); |
| 38 while (frames_done < frames) { | 39 while (output_frames_ready_ < frames) { |
| 39 int frames_this_time = std::min(frames - frames_done, chunk_size); | 40 int frames_this_time = std::min(frames - output_frames_ready_, chunk_size); |
| 40 | 41 |
| 41 // Resample each channel. | 42 // Resample each channel. |
| 42 for (size_t i = 0; i < resamplers_.size(); ++i) { | 43 for (size_t i = 0; i < resamplers_.size(); ++i) { |
| 43 DCHECK_EQ(chunk_size, resamplers_[i]->ChunkSize()); | 44 DCHECK_EQ(chunk_size, resamplers_[i]->ChunkSize()); |
| 44 | 45 |
| 45 // Depending on the sample-rate scale factor, and the internal buffering | 46 // Depending on the sample-rate scale factor, and the internal buffering |
| 46 // used in a SincResampler kernel, this call to Resample() will only | 47 // used in a SincResampler kernel, this call to Resample() will only |
| 47 // sometimes call ProvideInput(). However, if it calls ProvideInput() for | 48 // sometimes call ProvideInput(). However, if it calls ProvideInput() for |
| 48 // the first channel, then it will call it for the remaining channels, | 49 // the first channel, then it will call it for the remaining channels, |
| 49 // since they all buffer in the same way and are processing the same | 50 // since they all buffer in the same way and are processing the same |
| 50 // number of frames. | 51 // number of frames. |
| 51 resamplers_[i]->Resample( | 52 resamplers_[i]->Resample( |
| 52 audio_bus->channel(i) + frames_done, frames_this_time); | 53 audio_bus->channel(i) + output_frames_ready_, frames_this_time); |
| 53 } | 54 } |
| 54 | 55 |
| 55 frames_done += frames_this_time; | 56 output_frames_ready_ += frames_this_time; |
|
Chris Rogers
2012/11/14 23:50:49
I don't understand how at the end of this loop tha
DaleCurtis
2012/11/15 00:30:59
Queried upstream only in the middle of this loop.
| |
| 56 } | 57 } |
| 57 } | 58 } |
| 58 | 59 |
| 59 void MultiChannelResampler::ProvideInput(int channel, float* destination, | 60 void MultiChannelResampler::ProvideInput(int channel, float* destination, |
| 60 int frames) { | 61 int frames) { |
| 61 // Get the data from the multi-channel provider when the first channel asks | 62 // Get the data from the multi-channel provider when the first channel asks |
| 62 // for it. For subsequent channels, we can just dish out the channel data | 63 // for it. For subsequent channels, we can just dish out the channel data |
| 63 // from that (stored in |resampler_audio_bus_|). | 64 // from that (stored in |resampler_audio_bus_|). |
| 64 if (channel == 0) { | 65 if (channel == 0) { |
| 65 // Allocate staging arrays on the first request and if the frame size or | 66 // Allocate staging arrays on the first request and if the frame size or |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 94 } | 95 } |
| 95 } | 96 } |
| 96 | 97 |
| 97 void MultiChannelResampler::Flush() { | 98 void MultiChannelResampler::Flush() { |
| 98 last_frame_count_ = 0; | 99 last_frame_count_ = 0; |
| 99 for (size_t i = 0; i < resamplers_.size(); ++i) | 100 for (size_t i = 0; i < resamplers_.size(); ++i) |
| 100 resamplers_[i]->Flush(); | 101 resamplers_[i]->Flush(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 } // namespace media | 104 } // namespace media |
| OLD | NEW |