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/audio_transform.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 AudioTransform::AudioTransform(const AudioParameters& input_params, | |
|
DaleCurtis
2012/11/12 20:15:23
I'll add micro-benchmark and run it against the ol
| |
| 19 const AudioParameters& output_params) | |
| 20 : downmix_early_(false) { | |
| 21 CHECK(input_params.IsValid()); | |
| 22 CHECK(output_params.IsValid()); | |
| 23 | |
| 24 // Handle different input and output channel layouts. | |
| 25 if (input_params.channel_layout() != output_params.channel_layout()) { | |
| 26 DVLOG(1) << "Remixing channel layout from " << input_params.channel_layout() | |
| 27 << " to " << output_params.channel_layout() << "; from " | |
| 28 << input_params.channels() << " channels to " | |
| 29 << output_params.channels() << " channels."; | |
| 30 channel_mixer_.reset(new ChannelMixer( | |
| 31 input_params.channel_layout(), output_params.channel_layout())); | |
| 32 | |
| 33 // Pare off data as early as we can for efficiency. | |
| 34 downmix_early_ = input_params.channels() > output_params.channels(); | |
| 35 if (downmix_early_) { | |
| 36 DVLOG(1) << "Remixing channel layout prior to resampling."; | |
| 37 // If we're downmixing early we need a temporary AudioBus which matches | |
| 38 // the the input channel count and input frame size since we're passing | |
| 39 // |unmixed_audio_| directly to the |source_callback_|. | |
| 40 unmixed_audio_ = AudioBus::Create(input_params); | |
| 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 &AudioTransform::ProvideInput, base::Unretained(this)))); | |
| 61 } | |
| 62 | |
| 63 // Since the resampler / output device may want a different buffer size than | |
| 64 // the caller asked for, we need to use a FIFO to ensure that both sides | |
| 65 // read in chunk sizes they're configured for. Clients may optionally choose | |
| 66 // to support arbitrary downstream requests by disabling rebuffering. | |
| 67 if (resampler_.get() || | |
| 68 input_params.frames_per_buffer() != output_params.frames_per_buffer()) { | |
| 69 DVLOG(1) << "Rebuffering from " << input_params.frames_per_buffer() | |
| 70 << " to " << output_params.frames_per_buffer(); | |
| 71 audio_fifo_.reset(new AudioPullFifo( | |
| 72 downmix_early_ ? output_params.channels() : | |
| 73 input_params.channels(), | |
| 74 input_params.frames_per_buffer(), base::Bind( | |
| 75 &AudioTransform::SourceCallback, | |
| 76 base::Unretained(this)))); | |
| 77 } | |
| 78 | |
| 79 // Temporary AudioBus for mixing inputs together. | |
| 80 mixer_input_audio_bus_ = AudioBus::Create(input_params); | |
| 81 | |
| 82 input_frame_duration_ = base::TimeDelta::FromMicroseconds( | |
| 83 base::Time::kMicrosecondsPerSecond / | |
| 84 static_cast<double>(input_params.sample_rate())); | |
| 85 output_frame_duration_ = base::TimeDelta::FromMicroseconds( | |
| 86 base::Time::kMicrosecondsPerSecond / | |
| 87 static_cast<double>(output_params.sample_rate())); | |
| 88 } | |
| 89 | |
| 90 AudioTransform::~AudioTransform() {} | |
| 91 | |
| 92 void AudioTransform::AddInput(AudioTransformInput* input) { | |
|
DaleCurtis
2012/11/12 20:15:23
What do you think about making AudioTransform thre
miu
2012/11/12 20:51:59
I'm not familiar with how this all works at the hi
DaleCurtis
2012/11/12 21:23:53
I'm not sure I'll be in KIR this Wednesday, but we
DaleCurtis
2012/11/16 23:51:05
I'll add the Map() operation in a follow up CL and
| |
| 93 transform_inputs_.push_back(input); | |
| 94 } | |
| 95 | |
| 96 void AudioTransform::RemoveInput(AudioTransformInput* input) { | |
| 97 DCHECK(std::find(transform_inputs_.begin(), transform_inputs_.end(), input) != | |
| 98 transform_inputs_.end()); | |
| 99 transform_inputs_.remove(input); | |
| 100 | |
| 101 if (transform_inputs_.empty()) | |
| 102 Reset(); | |
| 103 } | |
| 104 | |
| 105 void AudioTransform::Reset() { | |
| 106 if (audio_fifo_) | |
| 107 audio_fifo_->Clear(); | |
| 108 if (resampler_) | |
| 109 resampler_->Flush(); | |
| 110 } | |
| 111 | |
| 112 void AudioTransform::Transform(AudioBus* dest) { | |
| 113 if (transform_inputs_.empty()) { | |
| 114 dest->Zero(); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 bool needs_mixing = channel_mixer_ && !downmix_early_; | |
| 119 AudioBus* temp_dest = needs_mixing ? unmixed_audio_.get() : dest; | |
| 120 | |
| 121 if (!resampler_ && !audio_fifo_) { | |
| 122 SourceCallback(temp_dest); | |
| 123 } else { | |
| 124 if (resampler_) | |
| 125 resampler_->Resample(temp_dest, temp_dest->frames()); | |
| 126 else | |
| 127 ProvideInput(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 AudioTransform::SourceCallback(AudioBus* dest) { | |
| 137 bool needs_downmix = channel_mixer_ && downmix_early_; | |
| 138 AudioBus* temp_dest = needs_downmix ? unmixed_audio_.get() : dest; | |
|
DaleCurtis
2012/11/12 20:15:23
Next CL Optimization: Make FIFO optional for clien
| |
| 139 | |
| 140 // Sanity check our inputs. | |
| 141 DCHECK_EQ(temp_dest->frames(), mixer_input_audio_bus_->frames()); | |
| 142 DCHECK_EQ(temp_dest->channels(), mixer_input_audio_bus_->channels()); | |
| 143 | |
| 144 // Calculate the buffer delay for this callback. | |
| 145 base::TimeDelta buffer_delay; | |
| 146 if (resampler_) { | |
| 147 buffer_delay += base::TimeDelta::FromMicroseconds( | |
| 148 resampler_->output_frames_ready() * | |
| 149 output_frame_duration_.InMicroseconds()); | |
| 150 } | |
| 151 if (audio_fifo_) { | |
| 152 buffer_delay += base::TimeDelta::FromMicroseconds( | |
| 153 audio_fifo_->output_frames_ready() * | |
| 154 input_frame_duration_.InMicroseconds()); | |
| 155 } | |
| 156 | |
| 157 // Have each mixer render its data into an output buffer then mix the result. | |
| 158 for (AudioTransformInputSet::iterator it = transform_inputs_.begin(); | |
| 159 it != transform_inputs_.end(); ++it) { | |
| 160 AudioTransformInput* input = *it; | |
| 161 | |
| 162 float volume = input->ProvideAudioTransformInput( | |
|
DaleCurtis
2012/11/12 20:15:23
Next CL Optimization: Write directly to output bus
DaleCurtis
2012/11/16 23:51:05
Tried this and it didn't make a huge impact. ~100m
| |
| 163 mixer_input_audio_bus_.get(), buffer_delay); | |
| 164 | |
| 165 // Optimize the most common single input, full volume case. | |
| 166 if (it == transform_inputs_.begin()) { | |
| 167 if (volume == 1.0f) { | |
| 168 mixer_input_audio_bus_->CopyTo(temp_dest); | |
| 169 continue; | |
| 170 } | |
| 171 | |
| 172 // Zero |temp_dest| otherwise, so we're mixing into a clean buffer. | |
| 173 temp_dest->Zero(); | |
| 174 } | |
| 175 | |
| 176 // Volume adjust and mix each mixer input into |temp_dest| after rendering. | |
| 177 if (volume > 0) { | |
| 178 for (int i = 0; i < mixer_input_audio_bus_->channels(); ++i) { | |
| 179 vector_math::FMAC( | |
| 180 mixer_input_audio_bus_->channel(i), volume, | |
| 181 mixer_input_audio_bus_->frames(), temp_dest->channel(i)); | |
| 182 } | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 if (needs_downmix) { | |
| 187 DCHECK_EQ(temp_dest->frames(), dest->frames()); | |
| 188 channel_mixer_->Transform(temp_dest, dest); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 void AudioTransform::ProvideInput(AudioBus* dest) { | |
| 193 audio_fifo_->Consume(dest, dest->frames()); | |
| 194 } | |
| 195 | |
| 196 } // namespace media | |
| OLD | NEW |