| 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/audio_converter.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "media/base/audio_pull_fifo.h" |
| 12 #include "media/base/channel_mixer.h" |
| 13 #include "media/base/multi_channel_resampler.h" |
| 14 #include "media/base/vector_math.h" |
| 15 |
| 16 namespace media { |
| 17 |
| 18 AudioConverter::AudioConverter(const AudioParameters& input_params, |
| 19 const AudioParameters& output_params, |
| 20 bool disable_fifo) |
| 21 : downmix_early_(false), |
| 22 resampler_frame_delay_(0), |
| 23 input_channel_count_(input_params.channels()) { |
| 24 CHECK(input_params.IsValid()); |
| 25 CHECK(output_params.IsValid()); |
| 26 |
| 27 // Handle different input and output channel layouts. |
| 28 if (input_params.channel_layout() != output_params.channel_layout()) { |
| 29 DVLOG(1) << "Remixing channel layout from " << input_params.channel_layout() |
| 30 << " to " << output_params.channel_layout() << "; from " |
| 31 << input_params.channels() << " channels to " |
| 32 << output_params.channels() << " channels."; |
| 33 channel_mixer_.reset(new ChannelMixer( |
| 34 input_params.channel_layout(), output_params.channel_layout())); |
| 35 |
| 36 // Pare off data as early as we can for efficiency. |
| 37 downmix_early_ = input_params.channels() > output_params.channels(); |
| 38 if (downmix_early_) { |
| 39 DVLOG(1) << "Remixing channel layout prior to resampling."; |
| 40 // |unmixed_audio_| will be allocated on the fly. |
| 41 } else { |
| 42 // Instead, if we're not downmixing early we need a temporary AudioBus |
| 43 // which matches the input channel count but uses the output frame size |
| 44 // since we'll mix into the AudioBus from the output stream. |
| 45 unmixed_audio_ = AudioBus::Create( |
| 46 input_params.channels(), output_params.frames_per_buffer()); |
| 47 } |
| 48 } |
| 49 |
| 50 // Only resample if necessary since it's expensive. |
| 51 if (input_params.sample_rate() != output_params.sample_rate()) { |
| 52 DVLOG(1) << "Resampling from " << input_params.sample_rate() << " to " |
| 53 << output_params.sample_rate(); |
| 54 double io_sample_rate_ratio = input_params.sample_rate() / |
| 55 static_cast<double>(output_params.sample_rate()); |
| 56 resampler_.reset(new MultiChannelResampler( |
| 57 downmix_early_ ? output_params.channels() : |
| 58 input_params.channels(), |
| 59 io_sample_rate_ratio, base::Bind( |
| 60 &AudioConverter::ProvideInput, base::Unretained(this)))); |
| 61 } |
| 62 |
| 63 input_frame_duration_ = base::TimeDelta::FromMicroseconds( |
| 64 base::Time::kMicrosecondsPerSecond / |
| 65 static_cast<double>(input_params.sample_rate())); |
| 66 output_frame_duration_ = base::TimeDelta::FromMicroseconds( |
| 67 base::Time::kMicrosecondsPerSecond / |
| 68 static_cast<double>(output_params.sample_rate())); |
| 69 |
| 70 if (disable_fifo) |
| 71 return; |
| 72 |
| 73 // Since the resampler / output device may want a different buffer size than |
| 74 // the caller asked for, we need to use a FIFO to ensure that both sides |
| 75 // read in chunk sizes they're configured for. |
| 76 if (resampler_.get() || |
| 77 input_params.frames_per_buffer() != output_params.frames_per_buffer()) { |
| 78 DVLOG(1) << "Rebuffering from " << input_params.frames_per_buffer() |
| 79 << " to " << output_params.frames_per_buffer(); |
| 80 audio_fifo_.reset(new AudioPullFifo( |
| 81 downmix_early_ ? output_params.channels() : |
| 82 input_params.channels(), |
| 83 input_params.frames_per_buffer(), base::Bind( |
| 84 &AudioConverter::SourceCallback, |
| 85 base::Unretained(this)))); |
| 86 } |
| 87 } |
| 88 |
| 89 AudioConverter::~AudioConverter() {} |
| 90 |
| 91 void AudioConverter::AddInput(InputCallback* input) { |
| 92 transform_inputs_.push_back(input); |
| 93 } |
| 94 |
| 95 void AudioConverter::RemoveInput(InputCallback* input) { |
| 96 DCHECK(std::find(transform_inputs_.begin(), transform_inputs_.end(), input) != |
| 97 transform_inputs_.end()); |
| 98 transform_inputs_.remove(input); |
| 99 |
| 100 if (transform_inputs_.empty()) |
| 101 Reset(); |
| 102 } |
| 103 |
| 104 void AudioConverter::Reset() { |
| 105 if (audio_fifo_) |
| 106 audio_fifo_->Clear(); |
| 107 if (resampler_) |
| 108 resampler_->Flush(); |
| 109 } |
| 110 |
| 111 void AudioConverter::Convert(AudioBus* dest) { |
| 112 if (transform_inputs_.empty()) { |
| 113 dest->Zero(); |
| 114 return; |
| 115 } |
| 116 |
| 117 bool needs_mixing = channel_mixer_ && !downmix_early_; |
| 118 AudioBus* temp_dest = needs_mixing ? unmixed_audio_.get() : dest; |
| 119 DCHECK(temp_dest); |
| 120 |
| 121 if (!resampler_ && !audio_fifo_) { |
| 122 SourceCallback(0, temp_dest); |
| 123 } else { |
| 124 if (resampler_) |
| 125 resampler_->Resample(temp_dest, temp_dest->frames()); |
| 126 else |
| 127 ProvideInput(0, temp_dest); |
| 128 } |
| 129 |
| 130 if (needs_mixing) { |
| 131 DCHECK_EQ(temp_dest->frames(), dest->frames()); |
| 132 channel_mixer_->Transform(temp_dest, dest); |
| 133 } |
| 134 } |
| 135 |
| 136 void AudioConverter::SourceCallback(int fifo_frame_delay, AudioBus* dest) { |
| 137 bool needs_downmix = channel_mixer_ && downmix_early_; |
| 138 |
| 139 if (!mixer_input_audio_bus_ || |
| 140 mixer_input_audio_bus_->frames() != dest->frames()) { |
| 141 mixer_input_audio_bus_ = |
| 142 AudioBus::Create(input_channel_count_, dest->frames()); |
| 143 } |
| 144 |
| 145 if (needs_downmix && |
| 146 (!unmixed_audio_ || unmixed_audio_->frames() != dest->frames())) { |
| 147 // If we're downmixing early we need a temporary AudioBus which matches |
| 148 // the the input channel count and input frame size since we're passing |
| 149 // |unmixed_audio_| directly to the |source_callback_|. |
| 150 unmixed_audio_ = AudioBus::Create(input_channel_count_, dest->frames()); |
| 151 } |
| 152 |
| 153 AudioBus* temp_dest = needs_downmix ? unmixed_audio_.get() : dest; |
| 154 |
| 155 // Sanity check our inputs. |
| 156 DCHECK_EQ(temp_dest->frames(), mixer_input_audio_bus_->frames()); |
| 157 DCHECK_EQ(temp_dest->channels(), mixer_input_audio_bus_->channels()); |
| 158 |
| 159 // Calculate the buffer delay for this callback. |
| 160 base::TimeDelta buffer_delay; |
| 161 if (resampler_) { |
| 162 buffer_delay += base::TimeDelta::FromMicroseconds( |
| 163 resampler_frame_delay_ * output_frame_duration_.InMicroseconds()); |
| 164 } |
| 165 if (audio_fifo_) { |
| 166 buffer_delay += base::TimeDelta::FromMicroseconds( |
| 167 fifo_frame_delay * input_frame_duration_.InMicroseconds()); |
| 168 } |
| 169 |
| 170 // Have each mixer render its data into an output buffer then mix the result. |
| 171 for (InputCallbackSet::iterator it = transform_inputs_.begin(); |
| 172 it != transform_inputs_.end(); ++it) { |
| 173 InputCallback* input = *it; |
| 174 |
| 175 float volume = input->ProvideInput( |
| 176 mixer_input_audio_bus_.get(), buffer_delay); |
| 177 |
| 178 // Optimize the most common single input, full volume case. |
| 179 if (it == transform_inputs_.begin()) { |
| 180 if (volume == 1.0f) { |
| 181 mixer_input_audio_bus_->CopyTo(temp_dest); |
| 182 continue; |
| 183 } |
| 184 |
| 185 // Zero |temp_dest| otherwise, so we're mixing into a clean buffer. |
| 186 temp_dest->Zero(); |
| 187 } |
| 188 |
| 189 // Volume adjust and mix each mixer input into |temp_dest| after rendering. |
| 190 if (volume > 0) { |
| 191 for (int i = 0; i < mixer_input_audio_bus_->channels(); ++i) { |
| 192 vector_math::FMAC( |
| 193 mixer_input_audio_bus_->channel(i), volume, |
| 194 mixer_input_audio_bus_->frames(), temp_dest->channel(i)); |
| 195 } |
| 196 } |
| 197 } |
| 198 |
| 199 if (needs_downmix) { |
| 200 DCHECK_EQ(temp_dest->frames(), dest->frames()); |
| 201 channel_mixer_->Transform(temp_dest, dest); |
| 202 } |
| 203 } |
| 204 |
| 205 void AudioConverter::ProvideInput(int resampler_frame_delay, AudioBus* dest) { |
| 206 resampler_frame_delay_ = resampler_frame_delay; |
| 207 if (audio_fifo_) |
| 208 audio_fifo_->Consume(dest, dest->frames()); |
| 209 else |
| 210 SourceCallback(0, dest); |
| 211 } |
| 212 |
| 213 } // namespace media |
| OLD | NEW |