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_bus.h" |
| 6 |
| 7 #include <limits> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "media/audio/audio_parameters.h" |
| 11 #include "media/base/limits.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 // Ensure each channel is 16-byte aligned for easy SSE optimizations. |
| 16 static const int kChannelAlignment = 16; |
| 17 |
| 18 static bool IsAligned(void* ptr) { |
| 19 return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; |
| 20 } |
| 21 |
| 22 AudioBus::AudioBus(int channels, int frames) |
| 23 : frames_(frames) { |
| 24 CHECK_GT(frames, 0); |
| 25 CHECK_LE(frames, limits::kMaxSamplesPerPacket); |
| 26 CHECK_GT(channels, 0); |
| 27 CHECK_LE(channels, limits::kMaxChannels); |
| 28 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, |
| 29 std::numeric_limits<int>::max()); |
| 30 |
| 31 // Choose a size such that each channel is aligned by kChannelAlignment. |
| 32 int aligned_frames = |
| 33 (frames_ + kChannelAlignment - 1) & ~(kChannelAlignment - 1); |
| 34 data_size_ = sizeof(float) * channels * aligned_frames; |
| 35 |
| 36 data_.reset(static_cast<float*>(base::AlignedAlloc( |
| 37 data_size_, kChannelAlignment))); |
| 38 |
| 39 // Separate audio data out into channels for easy lookup later. |
| 40 channel_data_.reserve(channels); |
| 41 for (int i = 0; i < channels; ++i) |
| 42 channel_data_.push_back(data_.get() + i * aligned_frames); |
| 43 } |
| 44 |
| 45 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) |
| 46 : data_size_(0), |
| 47 channel_data_(channel_data), |
| 48 frames_(frames) { |
| 49 // Sanity check wrapped vector for alignment and channel count. |
| 50 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 51 DCHECK(IsAligned(channel_data_[i])); |
| 52 } |
| 53 |
| 54 AudioBus::~AudioBus() {} |
| 55 |
| 56 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { |
| 57 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); |
| 58 } |
| 59 |
| 60 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { |
| 61 return scoped_ptr<AudioBus>(new AudioBus( |
| 62 params.channels(), params.frames_per_buffer())); |
| 63 } |
| 64 |
| 65 scoped_ptr<AudioBus> AudioBus::WrapVector( |
| 66 int frames, const std::vector<float*>& channel_data) { |
| 67 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); |
| 68 } |
| 69 |
| 70 void* AudioBus::data() { |
| 71 DCHECK(data_.get()); |
| 72 return data_.get(); |
| 73 } |
| 74 |
| 75 int AudioBus::data_size() const { |
| 76 DCHECK(data_.get()); |
| 77 return data_size_; |
| 78 } |
| 79 |
| 80 void AudioBus::ZeroFrames(int frames) { |
| 81 DCHECK_LE(frames, frames_); |
| 82 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 83 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); |
| 84 } |
| 85 |
| 86 void AudioBus::Zero() { |
| 87 ZeroFrames(frames_); |
| 88 } |
| 89 |
| 90 } // namespace media |
OLD | NEW |