| 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 // AudioConverter implementation. Uses MultiChannelSincResampler for resampling | 5 // AudioConverter implementation. Uses MultiChannelSincResampler for resampling |
| 6 // audio, ChannelMixer for channel mixing, and AudioPullFifo for buffering. | 6 // audio, ChannelMixer for channel mixing, and AudioPullFifo for buffering. |
| 7 // | 7 // |
| 8 // Delay estimates are provided to InputCallbacks based on the frame delay | 8 // Delay estimates are provided to InputCallbacks based on the frame delay |
| 9 // information reported via the resampler and FIFO units. | 9 // information reported via the resampler and FIFO units. |
| 10 | 10 |
| 11 #include "media/base/audio_converter.h" | 11 #include "media/base/audio_converter.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
| 17 #include "media/base/audio_bus.h" | 17 #include "media/base/audio_bus.h" |
| 18 #include "media/base/audio_pull_fifo.h" | 18 #include "media/base/audio_pull_fifo.h" |
| 19 #include "media/base/channel_mixer.h" | 19 #include "media/base/channel_mixer.h" |
| 20 #include "media/base/multi_channel_resampler.h" | 20 #include "media/base/multi_channel_resampler.h" |
| 21 #include "media/base/vector_math.h" | 21 #include "media/base/vector_math.h" |
| 22 | 22 |
| 23 namespace media { | 23 namespace media { |
| 24 | 24 |
| 25 AudioConverter::AudioConverter(const AudioParameters& input_params, | 25 AudioConverter::AudioConverter(const AudioParameters& input_params, |
| 26 const AudioParameters& output_params, | 26 const AudioParameters& output_params, |
| 27 bool disable_fifo) | 27 bool disable_fifo) |
| 28 : chunk_size_(output_params.frames_per_buffer()), | 28 : chunk_size_(input_params.frames_per_buffer()), |
| 29 downmix_early_(false), | 29 downmix_early_(false), |
| 30 resampler_frame_delay_(0), | 30 resampler_frame_delay_(0), |
| 31 input_channel_count_(input_params.channels()) { | 31 input_channel_count_(input_params.channels()) { |
| 32 CHECK(input_params.IsValid()); | 32 CHECK(input_params.IsValid()); |
| 33 CHECK(output_params.IsValid()); | 33 CHECK(output_params.IsValid()); |
| 34 | 34 |
| 35 // Handle different input and output channel layouts. | 35 // Handle different input and output channel layouts. |
| 36 if (input_params.channel_layout() != output_params.channel_layout()) { | 36 if (input_params.channel_layout() != output_params.channel_layout()) { |
| 37 DVLOG(1) << "Remixing channel layout from " << input_params.channel_layout() | 37 DVLOG(1) << "Remixing channel layout from " << input_params.channel_layout() |
| 38 << " to " << output_params.channel_layout() << "; from " | 38 << " to " << output_params.channel_layout() << "; from " |
| 39 << input_params.channels() << " channels to " | 39 << input_params.channels() << " channels to " |
| 40 << output_params.channels() << " channels."; | 40 << output_params.channels() << " channels."; |
| 41 channel_mixer_.reset(new ChannelMixer(input_params, output_params)); | 41 channel_mixer_.reset(new ChannelMixer(input_params, output_params)); |
| 42 | 42 |
| 43 // Pare off data as early as we can for efficiency. | 43 // Pare off data as early as we can for efficiency. |
| 44 downmix_early_ = input_params.channels() > output_params.channels(); | 44 downmix_early_ = input_params.channels() > output_params.channels(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 // Only resample if necessary since it's expensive. | 47 // Only resample if necessary since it's expensive. |
| 48 if (input_params.sample_rate() != output_params.sample_rate()) { | 48 if (input_params.sample_rate() != output_params.sample_rate()) { |
| 49 DVLOG(1) << "Resampling from " << input_params.sample_rate() << " to " | 49 DVLOG(1) << "Resampling from " << input_params.sample_rate() << " to " |
| 50 << output_params.sample_rate(); | 50 << output_params.sample_rate(); |
| 51 const double io_sample_rate_ratio = input_params.sample_rate() / | |
| 52 static_cast<double>(output_params.sample_rate()); | |
| 53 const int request_size = disable_fifo ? SincResampler::kDefaultRequestSize : | 51 const int request_size = disable_fifo ? SincResampler::kDefaultRequestSize : |
| 54 input_params.frames_per_buffer(); | 52 input_params.frames_per_buffer(); |
| 53 const double io_sample_rate_ratio = |
| 54 input_params.sample_rate() / |
| 55 static_cast<double>(output_params.sample_rate()); |
| 55 resampler_.reset(new MultiChannelResampler( | 56 resampler_.reset(new MultiChannelResampler( |
| 56 downmix_early_ ? output_params.channels() : | 57 downmix_early_ ? output_params.channels() : input_params.channels(), |
| 57 input_params.channels(), | 58 io_sample_rate_ratio, |
| 58 io_sample_rate_ratio, request_size, base::Bind( | 59 request_size, |
| 59 &AudioConverter::ProvideInput, base::Unretained(this)))); | 60 base::Bind(&AudioConverter::ProvideInput, base::Unretained(this)))); |
| 60 } | 61 } |
| 61 | 62 |
| 62 input_frame_duration_ = base::TimeDelta::FromMicroseconds( | 63 input_frame_duration_ = base::TimeDelta::FromMicroseconds( |
| 63 base::Time::kMicrosecondsPerSecond / | 64 base::Time::kMicrosecondsPerSecond / |
| 64 static_cast<double>(input_params.sample_rate())); | 65 static_cast<double>(input_params.sample_rate())); |
| 65 output_frame_duration_ = base::TimeDelta::FromMicroseconds( | 66 output_frame_duration_ = base::TimeDelta::FromMicroseconds( |
| 66 base::Time::kMicrosecondsPerSecond / | 67 base::Time::kMicrosecondsPerSecond / |
| 67 static_cast<double>(output_params.sample_rate())); | 68 static_cast<double>(output_params.sample_rate())); |
| 68 | 69 |
| 69 // The resampler can be configured to work with a specific request size, so a | 70 // The resampler can be configured to work with a specific request size, so a |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 else | 245 else |
| 245 SourceCallback(0, dest); | 246 SourceCallback(0, dest); |
| 246 } | 247 } |
| 247 | 248 |
| 248 void AudioConverter::CreateUnmixedAudioIfNecessary(int frames) { | 249 void AudioConverter::CreateUnmixedAudioIfNecessary(int frames) { |
| 249 if (!unmixed_audio_ || unmixed_audio_->frames() != frames) | 250 if (!unmixed_audio_ || unmixed_audio_->frames() != frames) |
| 250 unmixed_audio_ = AudioBus::Create(input_channel_count_, frames); | 251 unmixed_audio_ = AudioBus::Create(input_channel_count_, frames); |
| 251 } | 252 } |
| 252 | 253 |
| 253 } // namespace media | 254 } // namespace media |
| OLD | NEW |