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_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 inline bool IsAligned(void* ptr) { | |
|
scherkus (not reviewing)
2012/08/07 02:51:16
don't needlessly inline -- this is for a DCHECK()
DaleCurtis
2012/08/07 18:29:45
Done.
| |
| 19 return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; | |
| 20 } | |
| 21 | |
| 22 AudioBus::AudioBus(int channels, int frames) | |
| 23 : frames_(frames) { | |
| 24 // Sanity check values, use CHECK to prevent any overflow attempts. | |
|
scherkus (not reviewing)
2012/08/07 02:51:16
comment ain't needed
DaleCurtis
2012/08/07 18:29:45
Done.
| |
| 25 CHECK_GT(frames, 0); | |
| 26 CHECK_LE(frames, limits::kMaxSamplesPerPacket); | |
| 27 CHECK_GT(channels, 0); | |
| 28 CHECK_LE(channels, limits::kMaxChannels); | |
| 29 CHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, | |
|
scherkus (not reviewing)
2012/08/07 02:51:16
this CHECK seems redundant given:
* kMaxSamplesPe
DaleCurtis
2012/08/07 18:29:45
Correct, but this part of the code doesn't know th
| |
| 30 std::numeric_limits<int>::max()); | |
| 31 | |
| 32 // Choose a size such that each channel is aligned by kChannelAlignment. | |
| 33 int aligned_frames = | |
| 34 (frames_ + kChannelAlignment - 1) & ~(kChannelAlignment - 1); | |
| 35 data_size_ = sizeof(float) * channels * aligned_frames; | |
| 36 | |
| 37 data_.reset(static_cast<float*>(base::AlignedAlloc( | |
| 38 data_size_, kChannelAlignment))); | |
| 39 | |
| 40 // Separate audio data out into channels for easy lookup later. | |
| 41 channel_data_.reserve(channels); | |
| 42 for (int i = 0; i < channels; ++i) | |
| 43 channel_data_.push_back(data_.get() + i * aligned_frames); | |
| 44 } | |
| 45 | |
| 46 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) | |
| 47 : data_size_(0), | |
| 48 channel_data_(channel_data), | |
| 49 frames_(frames) { | |
| 50 // Sanity check wrapped vector for alignment and channel count. | |
| 51 for (size_t i = 0; i < channel_data_.size(); ++i) | |
| 52 DCHECK(IsAligned(channel_data_[i])); | |
| 53 } | |
| 54 | |
| 55 AudioBus::~AudioBus() {} | |
| 56 | |
| 57 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { | |
| 58 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); | |
| 59 } | |
| 60 | |
| 61 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { | |
| 62 return scoped_ptr<AudioBus>(new AudioBus( | |
| 63 params.channels(), params.frames_per_buffer())); | |
| 64 } | |
| 65 | |
| 66 scoped_ptr<AudioBus> AudioBus::WrapVector( | |
| 67 int frames, const std::vector<float*>& channel_data) { | |
| 68 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); | |
| 69 } | |
| 70 | |
| 71 void* AudioBus::data() const { | |
| 72 DCHECK(data_.get()); | |
| 73 return data_.get(); | |
| 74 } | |
| 75 | |
| 76 int AudioBus::data_size() const { | |
| 77 DCHECK(data_.get()); | |
| 78 return data_size_; | |
| 79 } | |
| 80 | |
| 81 void AudioBus::ZeroFrames(int frames) const { | |
| 82 DCHECK_LE(frames, frames_); | |
| 83 for (size_t i = 0; i < channel_data_.size(); ++i) | |
| 84 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); | |
| 85 } | |
| 86 | |
| 87 } // namespace media | |
| OLD | NEW |