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 callback_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( | |
Ami GONE FROM CHROMIUM
2012/07/03 18:53:12
multi-line for bodies require braces
DaleCurtis
2012/07/04 00:56:19
Done.
| |
23 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.
| |
24 &MultiChannelResampler::ProvideInput, base::Unretained(this), i), | |
25 io_sample_rate_ratio)); | |
26 | |
27 // Preallocate staging arrays based on SincResampler's buffer size. | |
28 resampler_audio_data_.reserve(channels); | |
29 for (int i = 0; i < channels; ++i) | |
30 resampler_audio_data_.push_back(new float[SincResampler::kBufferSize]); | |
31 } | |
32 | |
33 MultiChannelResampler::~MultiChannelResampler() { | |
34 // Clean up |resampler_audio_data_|. | |
35 for (size_t i = 0; i < resampler_audio_data_.size(); ++i) | |
36 delete [] resampler_audio_data_[i]; | |
37 resampler_audio_data_.clear(); | |
38 } | |
39 | |
40 void MultiChannelResampler::Resample(const std::vector<float*>& destination, | |
41 int frames) { | |
42 DCHECK_EQ(destination.size(), resamplers_.size()); | |
43 DCHECK_EQ(destination.size(), resampler_audio_data_.size()); | |
44 | |
45 // We need to ensure that SincResampler only calls ProvideInput once for each | |
46 // channel. To ensure this, we chunk the number of requested frames into | |
47 // SincResampler::kBlockSize chunks for which SincResampler guarantees it will | |
48 // only call ProvideInput() once. | |
49 int frames_done = 0; | |
50 while (frames_done < frames) { | |
51 int frames_this_time = std::min( | |
52 frames - frames_done, static_cast<int>(SincResampler::kBlockSize)); | |
53 | |
54 // Sanity check to ensure ProvideInput() is only called once per channel. | |
55 callback_count_ = 0; | |
56 | |
57 // Resample each channel. | |
58 for (size_t i = 0; i < resamplers_.size(); ++i) { | |
59 // Depending on the sample-rate scale factor, and the internal buffering | |
60 // used in a SincResampler kernel, this call to Resample() will only | |
61 // sometimes call ProvideInput(). However, if it calls ProvideInput() for | |
62 // the first channel, then it will call it for the remaining channels, | |
63 // since they all buffer in the same way and are processing the same | |
64 // number of frames. | |
65 resamplers_[i]->Resample(destination[i] + frames_done, frames_this_time); | |
66 } | |
67 | |
68 frames_done += frames_this_time; | |
69 } | |
70 } | |
71 | |
72 void MultiChannelResampler::ProvideInput(int channel, float* destination, | |
73 int frames) { | |
74 // Get the data from the multi-channel provider when the first channel asks | |
75 // for it. For subsequent channels, we can just dish out the channel data | |
76 // from that (stored in |resampler_audio_data_|). | |
77 if (channel == 0) { | |
78 DCHECK_LE(frames, SincResampler::kBufferSize); | |
79 last_frame_count_ = frames; | |
80 read_cb_.Run(resampler_audio_data_, frames); | |
81 } | |
82 | |
83 // All channels must ask for the same amount. This should always be the case, | |
84 // but let's just make sure. | |
85 DCHECK_EQ(frames, last_frame_count_); | |
86 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.
| |
87 | |
88 // Copy the channel data from what we received from |provider_|. | |
89 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
| |
90 sizeof(*resampler_audio_data_[channel]) * frames); | |
91 } | |
92 | |
93 } // namespace media | |
OLD | NEW |