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

Side by Side Diff: webrtc/modules/audio_processing/audio_buffer.cc

Issue 1316523002: Convert channel counts to size_t. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Fix compile Created 4 years, 11 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 26 matching lines...) Expand all
37 if (num_frames == kSamplesPer32kHzChannel || 37 if (num_frames == kSamplesPer32kHzChannel ||
38 num_frames == kSamplesPer48kHzChannel) { 38 num_frames == kSamplesPer48kHzChannel) {
39 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel); 39 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel);
40 } 40 }
41 return num_bands; 41 return num_bands;
42 } 42 }
43 43
44 } // namespace 44 } // namespace
45 45
46 AudioBuffer::AudioBuffer(size_t input_num_frames, 46 AudioBuffer::AudioBuffer(size_t input_num_frames,
47 int num_input_channels, 47 size_t num_input_channels,
48 size_t process_num_frames, 48 size_t process_num_frames,
49 int num_process_channels, 49 size_t num_process_channels,
50 size_t output_num_frames) 50 size_t output_num_frames)
51 : input_num_frames_(input_num_frames), 51 : input_num_frames_(input_num_frames),
52 num_input_channels_(num_input_channels), 52 num_input_channels_(num_input_channels),
53 proc_num_frames_(process_num_frames), 53 proc_num_frames_(process_num_frames),
54 num_proc_channels_(num_process_channels), 54 num_proc_channels_(num_process_channels),
55 output_num_frames_(output_num_frames), 55 output_num_frames_(output_num_frames),
56 num_channels_(num_process_channels), 56 num_channels_(num_process_channels),
57 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), 57 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
58 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), 58 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
59 mixed_low_pass_valid_(false), 59 mixed_low_pass_valid_(false),
60 reference_copied_(false), 60 reference_copied_(false),
61 activity_(AudioFrame::kVadUnknown), 61 activity_(AudioFrame::kVadUnknown),
62 keyboard_data_(NULL), 62 keyboard_data_(NULL),
63 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { 63 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) {
64 assert(input_num_frames_ > 0); 64 assert(input_num_frames_ > 0);
65 assert(proc_num_frames_ > 0); 65 assert(proc_num_frames_ > 0);
66 assert(output_num_frames_ > 0); 66 assert(output_num_frames_ > 0);
67 assert(num_input_channels_ > 0); 67 assert(num_input_channels_ > 0);
68 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_); 68 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_);
69 69
70 if (input_num_frames_ != proc_num_frames_ || 70 if (input_num_frames_ != proc_num_frames_ ||
71 output_num_frames_ != proc_num_frames_) { 71 output_num_frames_ != proc_num_frames_) {
72 // Create an intermediate buffer for resampling. 72 // Create an intermediate buffer for resampling.
73 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, 73 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_,
74 num_proc_channels_)); 74 num_proc_channels_));
75 75
76 if (input_num_frames_ != proc_num_frames_) { 76 if (input_num_frames_ != proc_num_frames_) {
77 for (int i = 0; i < num_proc_channels_; ++i) { 77 for (size_t i = 0; i < num_proc_channels_; ++i) {
78 input_resamplers_.push_back( 78 input_resamplers_.push_back(
79 new PushSincResampler(input_num_frames_, 79 new PushSincResampler(input_num_frames_,
80 proc_num_frames_)); 80 proc_num_frames_));
81 } 81 }
82 } 82 }
83 83
84 if (output_num_frames_ != proc_num_frames_) { 84 if (output_num_frames_ != proc_num_frames_) {
85 for (int i = 0; i < num_proc_channels_; ++i) { 85 for (size_t i = 0; i < num_proc_channels_; ++i) {
86 output_resamplers_.push_back( 86 output_resamplers_.push_back(
87 new PushSincResampler(proc_num_frames_, 87 new PushSincResampler(proc_num_frames_,
88 output_num_frames_)); 88 output_num_frames_));
89 } 89 }
90 } 90 }
91 } 91 }
92 92
93 if (num_bands_ > 1) { 93 if (num_bands_ > 1) {
94 split_data_.reset(new IFChannelBuffer(proc_num_frames_, 94 split_data_.reset(new IFChannelBuffer(proc_num_frames_,
95 num_proc_channels_, 95 num_proc_channels_,
(...skipping 27 matching lines...) Expand all
123 // Downmix. 123 // Downmix.
124 const float* const* data_ptr = data; 124 const float* const* data_ptr = data;
125 if (need_to_downmix) { 125 if (need_to_downmix) {
126 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_, 126 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
127 input_buffer_->fbuf()->channels()[0]); 127 input_buffer_->fbuf()->channels()[0]);
128 data_ptr = input_buffer_->fbuf_const()->channels(); 128 data_ptr = input_buffer_->fbuf_const()->channels();
129 } 129 }
130 130
131 // Resample. 131 // Resample.
132 if (input_num_frames_ != proc_num_frames_) { 132 if (input_num_frames_ != proc_num_frames_) {
133 for (int i = 0; i < num_proc_channels_; ++i) { 133 for (size_t i = 0; i < num_proc_channels_; ++i) {
134 input_resamplers_[i]->Resample(data_ptr[i], 134 input_resamplers_[i]->Resample(data_ptr[i],
135 input_num_frames_, 135 input_num_frames_,
136 process_buffer_->channels()[i], 136 process_buffer_->channels()[i],
137 proc_num_frames_); 137 proc_num_frames_);
138 } 138 }
139 data_ptr = process_buffer_->channels(); 139 data_ptr = process_buffer_->channels();
140 } 140 }
141 141
142 // Convert to the S16 range. 142 // Convert to the S16 range.
143 for (int i = 0; i < num_proc_channels_; ++i) { 143 for (size_t i = 0; i < num_proc_channels_; ++i) {
144 FloatToFloatS16(data_ptr[i], 144 FloatToFloatS16(data_ptr[i],
145 proc_num_frames_, 145 proc_num_frames_,
146 data_->fbuf()->channels()[i]); 146 data_->fbuf()->channels()[i]);
147 } 147 }
148 } 148 }
149 149
150 void AudioBuffer::CopyTo(const StreamConfig& stream_config, 150 void AudioBuffer::CopyTo(const StreamConfig& stream_config,
151 float* const* data) { 151 float* const* data) {
152 assert(stream_config.num_frames() == output_num_frames_); 152 assert(stream_config.num_frames() == output_num_frames_);
153 assert(stream_config.num_channels() == num_channels_ || num_channels_ == 1); 153 assert(stream_config.num_channels() == num_channels_ || num_channels_ == 1);
154 154
155 // Convert to the float range. 155 // Convert to the float range.
156 float* const* data_ptr = data; 156 float* const* data_ptr = data;
157 if (output_num_frames_ != proc_num_frames_) { 157 if (output_num_frames_ != proc_num_frames_) {
158 // Convert to an intermediate buffer for subsequent resampling. 158 // Convert to an intermediate buffer for subsequent resampling.
159 data_ptr = process_buffer_->channels(); 159 data_ptr = process_buffer_->channels();
160 } 160 }
161 for (int i = 0; i < num_channels_; ++i) { 161 for (size_t i = 0; i < num_channels_; ++i) {
162 FloatS16ToFloat(data_->fbuf()->channels()[i], 162 FloatS16ToFloat(data_->fbuf()->channels()[i],
163 proc_num_frames_, 163 proc_num_frames_,
164 data_ptr[i]); 164 data_ptr[i]);
165 } 165 }
166 166
167 // Resample. 167 // Resample.
168 if (output_num_frames_ != proc_num_frames_) { 168 if (output_num_frames_ != proc_num_frames_) {
169 for (int i = 0; i < num_channels_; ++i) { 169 for (size_t i = 0; i < num_channels_; ++i) {
170 output_resamplers_[i]->Resample(data_ptr[i], 170 output_resamplers_[i]->Resample(data_ptr[i],
171 proc_num_frames_, 171 proc_num_frames_,
172 data[i], 172 data[i],
173 output_num_frames_); 173 output_num_frames_);
174 } 174 }
175 } 175 }
176 176
177 // Upmix. 177 // Upmix.
178 for (int i = num_channels_; i < stream_config.num_channels(); ++i) { 178 for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
179 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data)); 179 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
180 } 180 }
181 } 181 }
182 182
183 void AudioBuffer::InitForNewData() { 183 void AudioBuffer::InitForNewData() {
184 keyboard_data_ = NULL; 184 keyboard_data_ = NULL;
185 mixed_low_pass_valid_ = false; 185 mixed_low_pass_valid_ = false;
186 reference_copied_ = false; 186 reference_copied_ = false;
187 activity_ = AudioFrame::kVadUnknown; 187 activity_ = AudioFrame::kVadUnknown;
188 num_channels_ = num_proc_channels_; 188 num_channels_ = num_proc_channels_;
189 } 189 }
190 190
191 const int16_t* const* AudioBuffer::channels_const() const { 191 const int16_t* const* AudioBuffer::channels_const() const {
192 return data_->ibuf_const()->channels(); 192 return data_->ibuf_const()->channels();
193 } 193 }
194 194
195 int16_t* const* AudioBuffer::channels() { 195 int16_t* const* AudioBuffer::channels() {
196 mixed_low_pass_valid_ = false; 196 mixed_low_pass_valid_ = false;
197 return data_->ibuf()->channels(); 197 return data_->ibuf()->channels();
198 } 198 }
199 199
200 const int16_t* const* AudioBuffer::split_bands_const(int channel) const { 200 const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const {
201 return split_data_.get() ? 201 return split_data_.get() ?
202 split_data_->ibuf_const()->bands(channel) : 202 split_data_->ibuf_const()->bands(channel) :
203 data_->ibuf_const()->bands(channel); 203 data_->ibuf_const()->bands(channel);
204 } 204 }
205 205
206 int16_t* const* AudioBuffer::split_bands(int channel) { 206 int16_t* const* AudioBuffer::split_bands(size_t channel) {
207 mixed_low_pass_valid_ = false; 207 mixed_low_pass_valid_ = false;
208 return split_data_.get() ? 208 return split_data_.get() ?
209 split_data_->ibuf()->bands(channel) : 209 split_data_->ibuf()->bands(channel) :
210 data_->ibuf()->bands(channel); 210 data_->ibuf()->bands(channel);
211 } 211 }
212 212
213 const int16_t* const* AudioBuffer::split_channels_const(Band band) const { 213 const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
214 if (split_data_.get()) { 214 if (split_data_.get()) {
215 return split_data_->ibuf_const()->channels(band); 215 return split_data_->ibuf_const()->channels(band);
216 } else { 216 } else {
(...skipping 30 matching lines...) Expand all
247 247
248 const float* const* AudioBuffer::channels_const_f() const { 248 const float* const* AudioBuffer::channels_const_f() const {
249 return data_->fbuf_const()->channels(); 249 return data_->fbuf_const()->channels();
250 } 250 }
251 251
252 float* const* AudioBuffer::channels_f() { 252 float* const* AudioBuffer::channels_f() {
253 mixed_low_pass_valid_ = false; 253 mixed_low_pass_valid_ = false;
254 return data_->fbuf()->channels(); 254 return data_->fbuf()->channels();
255 } 255 }
256 256
257 const float* const* AudioBuffer::split_bands_const_f(int channel) const { 257 const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
258 return split_data_.get() ? 258 return split_data_.get() ?
259 split_data_->fbuf_const()->bands(channel) : 259 split_data_->fbuf_const()->bands(channel) :
260 data_->fbuf_const()->bands(channel); 260 data_->fbuf_const()->bands(channel);
261 } 261 }
262 262
263 float* const* AudioBuffer::split_bands_f(int channel) { 263 float* const* AudioBuffer::split_bands_f(size_t channel) {
264 mixed_low_pass_valid_ = false; 264 mixed_low_pass_valid_ = false;
265 return split_data_.get() ? 265 return split_data_.get() ?
266 split_data_->fbuf()->bands(channel) : 266 split_data_->fbuf()->bands(channel) :
267 data_->fbuf()->bands(channel); 267 data_->fbuf()->bands(channel);
268 } 268 }
269 269
270 const float* const* AudioBuffer::split_channels_const_f(Band band) const { 270 const float* const* AudioBuffer::split_channels_const_f(Band band) const {
271 if (split_data_.get()) { 271 if (split_data_.get()) {
272 return split_data_->fbuf_const()->channels(band); 272 return split_data_->fbuf_const()->channels(band);
273 } else { 273 } else {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 } 334 }
335 335
336 void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { 336 void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
337 activity_ = activity; 337 activity_ = activity;
338 } 338 }
339 339
340 AudioFrame::VADActivity AudioBuffer::activity() const { 340 AudioFrame::VADActivity AudioBuffer::activity() const {
341 return activity_; 341 return activity_;
342 } 342 }
343 343
344 int AudioBuffer::num_channels() const { 344 size_t AudioBuffer::num_channels() const {
345 return num_channels_; 345 return num_channels_;
346 } 346 }
347 347
348 void AudioBuffer::set_num_channels(int num_channels) { 348 void AudioBuffer::set_num_channels(size_t num_channels) {
349 num_channels_ = num_channels; 349 num_channels_ = num_channels;
350 } 350 }
351 351
352 size_t AudioBuffer::num_frames() const { 352 size_t AudioBuffer::num_frames() const {
353 return proc_num_frames_; 353 return proc_num_frames_;
354 } 354 }
355 355
356 size_t AudioBuffer::num_frames_per_band() const { 356 size_t AudioBuffer::num_frames_per_band() const {
357 return num_split_frames_; 357 return num_split_frames_;
358 } 358 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 } else { 391 } else {
392 assert(num_proc_channels_ == num_input_channels_); 392 assert(num_proc_channels_ == num_input_channels_);
393 Deinterleave(frame->data_, 393 Deinterleave(frame->data_,
394 input_num_frames_, 394 input_num_frames_,
395 num_proc_channels_, 395 num_proc_channels_,
396 deinterleaved); 396 deinterleaved);
397 } 397 }
398 398
399 // Resample. 399 // Resample.
400 if (input_num_frames_ != proc_num_frames_) { 400 if (input_num_frames_ != proc_num_frames_) {
401 for (int i = 0; i < num_proc_channels_; ++i) { 401 for (size_t i = 0; i < num_proc_channels_; ++i) {
402 input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i], 402 input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i],
403 input_num_frames_, 403 input_num_frames_,
404 data_->fbuf()->channels()[i], 404 data_->fbuf()->channels()[i],
405 proc_num_frames_); 405 proc_num_frames_);
406 } 406 }
407 } 407 }
408 } 408 }
409 409
410 void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) { 410 void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) {
411 frame->vad_activity_ = activity_; 411 frame->vad_activity_ = activity_;
412 if (!data_changed) { 412 if (!data_changed) {
413 return; 413 return;
414 } 414 }
415 415
416 assert(frame->num_channels_ == num_channels_ || num_channels_ == 1); 416 assert(frame->num_channels_ == num_channels_ || num_channels_ == 1);
417 assert(frame->samples_per_channel_ == output_num_frames_); 417 assert(frame->samples_per_channel_ == output_num_frames_);
418 418
419 // Resample if necessary. 419 // Resample if necessary.
420 IFChannelBuffer* data_ptr = data_.get(); 420 IFChannelBuffer* data_ptr = data_.get();
421 if (proc_num_frames_ != output_num_frames_) { 421 if (proc_num_frames_ != output_num_frames_) {
422 if (!output_buffer_) { 422 if (!output_buffer_) {
423 output_buffer_.reset( 423 output_buffer_.reset(
424 new IFChannelBuffer(output_num_frames_, num_channels_)); 424 new IFChannelBuffer(output_num_frames_, num_channels_));
425 } 425 }
426 for (int i = 0; i < num_channels_; ++i) { 426 for (size_t i = 0; i < num_channels_; ++i) {
427 output_resamplers_[i]->Resample( 427 output_resamplers_[i]->Resample(
428 data_->fbuf()->channels()[i], proc_num_frames_, 428 data_->fbuf()->channels()[i], proc_num_frames_,
429 output_buffer_->fbuf()->channels()[i], output_num_frames_); 429 output_buffer_->fbuf()->channels()[i], output_num_frames_);
430 } 430 }
431 data_ptr = output_buffer_.get(); 431 data_ptr = output_buffer_.get();
432 } 432 }
433 433
434 if (frame->num_channels_ == num_channels_) { 434 if (frame->num_channels_ == num_channels_) {
435 Interleave(data_ptr->ibuf()->channels(), proc_num_frames_, num_channels_, 435 Interleave(data_ptr->ibuf()->channels(), proc_num_frames_, num_channels_,
436 frame->data_); 436 frame->data_);
437 } else { 437 } else {
438 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], proc_num_frames_, 438 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], proc_num_frames_,
439 frame->num_channels_, frame->data_); 439 frame->num_channels_, frame->data_);
440 } 440 }
441 } 441 }
442 442
443 void AudioBuffer::CopyLowPassToReference() { 443 void AudioBuffer::CopyLowPassToReference() {
444 reference_copied_ = true; 444 reference_copied_ = true;
445 if (!low_pass_reference_channels_.get() || 445 if (!low_pass_reference_channels_.get() ||
446 low_pass_reference_channels_->num_channels() != num_channels_) { 446 low_pass_reference_channels_->num_channels() != num_channels_) {
447 low_pass_reference_channels_.reset( 447 low_pass_reference_channels_.reset(
448 new ChannelBuffer<int16_t>(num_split_frames_, 448 new ChannelBuffer<int16_t>(num_split_frames_,
449 num_proc_channels_)); 449 num_proc_channels_));
450 } 450 }
451 for (int i = 0; i < num_proc_channels_; i++) { 451 for (size_t i = 0; i < num_proc_channels_; i++) {
452 memcpy(low_pass_reference_channels_->channels()[i], 452 memcpy(low_pass_reference_channels_->channels()[i],
453 split_bands_const(i)[kBand0To8kHz], 453 split_bands_const(i)[kBand0To8kHz],
454 low_pass_reference_channels_->num_frames_per_band() * 454 low_pass_reference_channels_->num_frames_per_band() *
455 sizeof(split_bands_const(i)[kBand0To8kHz][0])); 455 sizeof(split_bands_const(i)[kBand0To8kHz][0]));
456 } 456 }
457 } 457 }
458 458
459 void AudioBuffer::SplitIntoFrequencyBands() { 459 void AudioBuffer::SplitIntoFrequencyBands() {
460 splitting_filter_->Analysis(data_.get(), split_data_.get()); 460 splitting_filter_->Analysis(data_.get(), split_data_.get());
461 } 461 }
462 462
463 void AudioBuffer::MergeFrequencyBands() { 463 void AudioBuffer::MergeFrequencyBands() {
464 splitting_filter_->Synthesis(split_data_.get(), data_.get()); 464 splitting_filter_->Synthesis(split_data_.get(), data_.get());
465 } 465 }
466 466
467 } // namespace webrtc 467 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/audio_buffer.h ('k') | webrtc/modules/audio_processing/audio_processing_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698