Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Side by Side Diff: media/base/audio_bus.cc

Issue 10909185: Add Mac OS X synchronized audio I/O back-end (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "media/base/audio_bus.h" 5 #include "media/base/audio_bus.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "media/audio/audio_parameters.h" 10 #include "media/audio/audio_parameters.h"
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 static void CheckOverflow(int start_frame, int frames, int total_frames) { 98 static void CheckOverflow(int start_frame, int frames, int total_frames) {
99 CHECK_GE(start_frame, 0); 99 CHECK_GE(start_frame, 0);
100 CHECK_GE(frames, 0); 100 CHECK_GE(frames, 0);
101 CHECK_GT(total_frames, 0); 101 CHECK_GT(total_frames, 0);
102 int sum = start_frame + frames; 102 int sum = start_frame + frames;
103 CHECK_LE(sum, total_frames); 103 CHECK_LE(sum, total_frames);
104 CHECK_GE(sum, 0); 104 CHECK_GE(sum, 0);
105 } 105 }
106 106
107 AudioBus::AudioBus(int channels, int frames) 107 AudioBus::AudioBus(int channels, int frames)
108 : frames_(frames) { 108 : frames_(frames),
109 can_set_channel_data_(false) {
109 ValidateConfig(channels, frames_); 110 ValidateConfig(channels, frames_);
110 111
111 int aligned_frames = 0; 112 int aligned_frames = 0;
112 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); 113 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames);
113 114
114 data_.reset(static_cast<float*>(base::AlignedAlloc( 115 data_.reset(static_cast<float*>(base::AlignedAlloc(
115 size, AudioBus::kChannelAlignment))); 116 size, AudioBus::kChannelAlignment)));
116 117
117 BuildChannelData(channels, aligned_frames, data_.get()); 118 BuildChannelData(channels, aligned_frames, data_.get());
118 } 119 }
119 120
120 AudioBus::AudioBus(int channels, int frames, float* data) 121 AudioBus::AudioBus(int channels, int frames, float* data)
121 : frames_(frames) { 122 : frames_(frames),
123 can_set_channel_data_(false) {
122 ValidateConfig(channels, frames_); 124 ValidateConfig(channels, frames_);
123 125
124 int aligned_frames = 0; 126 int aligned_frames = 0;
125 CalculateMemorySizeInternal(channels, frames, &aligned_frames); 127 CalculateMemorySizeInternal(channels, frames, &aligned_frames);
126 128
127 BuildChannelData(channels, aligned_frames, data); 129 BuildChannelData(channels, aligned_frames, data);
128 } 130 }
129 131
130 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) 132 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data)
131 : channel_data_(channel_data), 133 : channel_data_(channel_data),
132 frames_(frames) { 134 frames_(frames),
135 can_set_channel_data_(false) {
133 ValidateConfig(channel_data_.size(), frames_); 136 ValidateConfig(channel_data_.size(), frames_);
134 137
135 // Sanity check wrapped vector for alignment and channel count. 138 // Sanity check wrapped vector for alignment and channel count.
136 for (size_t i = 0; i < channel_data_.size(); ++i) 139 for (size_t i = 0; i < channel_data_.size(); ++i)
137 DCHECK(IsAligned(channel_data_[i])); 140 DCHECK(IsAligned(channel_data_[i]));
138 } 141 }
139 142
143 AudioBus::AudioBus(int channels)
144 : channel_data_(channels),
145 frames_(0),
146 can_set_channel_data_(true) {
147 for (size_t i = 0; i < channel_data_.size(); ++i)
148 channel_data_[i] = NULL;
149 }
150
140 AudioBus::~AudioBus() {} 151 AudioBus::~AudioBus() {}
141 152
142 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { 153 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) {
143 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); 154 return scoped_ptr<AudioBus>(new AudioBus(channels, frames));
144 } 155 }
145 156
146 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { 157 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) {
147 return scoped_ptr<AudioBus>(new AudioBus( 158 return scoped_ptr<AudioBus>(new AudioBus(
148 params.channels(), params.frames_per_buffer())); 159 params.channels(), params.frames_per_buffer()));
149 } 160 }
150 161
162 scoped_ptr<AudioBus> AudioBus::CreateWrapper(int channels) {
163 return scoped_ptr<AudioBus>(new AudioBus(channels));
164 }
165
151 scoped_ptr<AudioBus> AudioBus::WrapVector( 166 scoped_ptr<AudioBus> AudioBus::WrapVector(
152 int frames, const std::vector<float*>& channel_data) { 167 int frames, const std::vector<float*>& channel_data) {
153 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); 168 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data));
154 } 169 }
155 170
156 scoped_ptr<AudioBus> AudioBus::WrapMemory(int channels, int frames, 171 scoped_ptr<AudioBus> AudioBus::WrapMemory(int channels, int frames,
157 void* data) { 172 void* data) {
158 // |data| must be aligned by AudioBus::kChannelAlignment. 173 // |data| must be aligned by AudioBus::kChannelAlignment.
159 CHECK(IsAligned(data)); 174 CHECK(IsAligned(data));
160 return scoped_ptr<AudioBus>(new AudioBus( 175 return scoped_ptr<AudioBus>(new AudioBus(
161 channels, frames, static_cast<float*>(data))); 176 channels, frames, static_cast<float*>(data)));
162 } 177 }
163 178
164 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params, 179 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params,
165 void* data) { 180 void* data) {
166 // |data| must be aligned by AudioBus::kChannelAlignment. 181 // |data| must be aligned by AudioBus::kChannelAlignment.
167 CHECK(IsAligned(data)); 182 CHECK(IsAligned(data));
168 return scoped_ptr<AudioBus>(new AudioBus( 183 return scoped_ptr<AudioBus>(new AudioBus(
169 params.channels(), params.frames_per_buffer(), 184 params.channels(), params.frames_per_buffer(),
170 static_cast<float*>(data))); 185 static_cast<float*>(data)));
171 } 186 }
172 187
188 void AudioBus::SetChannelData(int channel, float* data) {
189 CHECK(can_set_channel_data_);
190 CHECK_GE(channel, 0);
191 CHECK_LT(static_cast<size_t>(channel), channel_data_.size());
192 DCHECK(IsAligned(data));
193 channel_data_[channel] = data;
194 }
195
196 void AudioBus::set_frames(int frames) {
197 CHECK(can_set_channel_data_);
198 frames_ = frames;
199 }
200
173 void AudioBus::ZeroFramesPartial(int start_frame, int frames) { 201 void AudioBus::ZeroFramesPartial(int start_frame, int frames) {
174 CheckOverflow(start_frame, frames, frames_); 202 CheckOverflow(start_frame, frames, frames_);
175 203
176 if (frames <= 0) 204 if (frames <= 0)
177 return; 205 return;
178 206
179 for (size_t i = 0; i < channel_data_.size(); ++i) { 207 for (size_t i = 0; i < channel_data_.size(); ++i) {
180 memset(channel_data_[i] + start_frame, 0, 208 memset(channel_data_[i] + start_frame, 0,
181 frames * sizeof(*channel_data_[i])); 209 frames * sizeof(*channel_data_[i]));
182 } 210 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 DCHECK_EQ(channels(), dest->channels()); 293 DCHECK_EQ(channels(), dest->channels());
266 DCHECK_EQ(frames(), dest->frames()); 294 DCHECK_EQ(frames(), dest->frames());
267 295
268 // Since we don't know if the other AudioBus is wrapped or not (and we don't 296 // Since we don't know if the other AudioBus is wrapped or not (and we don't
269 // want to care), just copy using the public channel() accessors. 297 // want to care), just copy using the public channel() accessors.
270 for (int i = 0; i < channels(); ++i) 298 for (int i = 0; i < channels(); ++i)
271 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); 299 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames());
272 } 300 }
273 301
274 } // namespace media 302 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698