| 1 | /* | 
| 2 | Copyright 2018 Google Inc. All Rights Reserved. | 
| 3 |  | 
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); | 
| 5 | you may not use this file except in compliance with the License. | 
| 6 | You may obtain a copy of the License at | 
| 7 |  | 
| 8 |     http://www.apache.org/licenses/LICENSE-2.0 | 
| 9 |  | 
| 10 | Unless required by applicable law or agreed to in writing, software | 
| 11 | distributed under the License is distributed on an "AS-IS" BASIS, | 
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 13 | See the License for the specific language governing permissions and | 
| 14 | limitations under the License. | 
| 15 | */ | 
| 16 |  | 
| 17 | #include "base/audio_buffer.h" | 
| 18 |  | 
| 19 | namespace vraudio { | 
| 20 |  | 
| 21 | AudioBuffer::AudioBuffer() : num_frames_(0), source_id_(kInvalidSourceId) {} | 
| 22 |  | 
| 23 | AudioBuffer::AudioBuffer(size_t num_channels, size_t num_frames) | 
| 24 |     : num_frames_(num_frames), source_id_(kInvalidSourceId) { | 
| 25 |  | 
| 26 |   InitChannelViews(num_channels); | 
| 27 | } | 
| 28 |  | 
| 29 | // Copy assignment from AudioBuffer. | 
| 30 | AudioBuffer& AudioBuffer::operator=(const AudioBuffer& other) { | 
| 31 |   if (this != &other) { | 
| 32 |     num_frames_ = other.num_frames_; | 
| 33 |     source_id_ = other.source_id_; | 
| 34 |     InitChannelViews(num_channels: other.num_channels()); | 
| 35 |     for (size_t i = 0; i < num_channels(); ++i) { | 
| 36 |       channel_views_[i] = other.channel_views_[i]; | 
| 37 |     } | 
| 38 |   } | 
| 39 |   return *this; | 
| 40 | } | 
| 41 |  | 
| 42 | AudioBuffer::AudioBuffer(AudioBuffer&& other) { | 
| 43 |   num_frames_ = other.num_frames_; | 
| 44 |   other.num_frames_ = 0; | 
| 45 |   data_ = std::move(other.data_); | 
| 46 |   data_size_ = other.data_size_; | 
| 47 |   other.data_size_ = 0; | 
| 48 |   channel_views_ = std::move(other.channel_views_); | 
| 49 |   source_id_ = other.source_id_; | 
| 50 |   other.source_id_ = kInvalidSourceId; | 
| 51 | } | 
| 52 |  | 
| 53 | void AudioBuffer::InitChannelViews(size_t num_channels) { | 
| 54 |  | 
| 55 |  | 
| 56 |   const size_t num_frames_to_next_channel = FindNextAlignedArrayIndex( | 
| 57 |       length: num_frames_, type_size_bytes: sizeof(float), memory_alignment_bytes: kMemoryAlignmentBytes); | 
| 58 |  | 
| 59 |   data_size_ = num_channels * num_frames_to_next_channel; | 
| 60 |   data_.resize(new_size: data_size_); | 
| 61 |  | 
| 62 |   channel_views_.clear(); | 
| 63 |   channel_views_.reserve(n: num_channels); | 
| 64 |  | 
| 65 |   float* itr = data_.data(); | 
| 66 |  | 
| 67 |   for (size_t i = 0; i < num_channels; ++i) { | 
| 68 |     ChannelView new_channel_view(itr, num_frames_); | 
| 69 |     channel_views_.push_back(x: new_channel_view); | 
| 70 |     itr += num_frames_to_next_channel; | 
| 71 |   } | 
| 72 | } | 
| 73 |  | 
| 74 | }  // namespace vraudio | 
| 75 |  |