henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/audio_vector.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | |
| 15 | #include <algorithm> |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 16 | #include <memory> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 19 | #include "typedefs.h" // NOLINT(build/include) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 23 | AudioVector::AudioVector() : AudioVector(kDefaultInitialSize) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 24 | Clear(); |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | AudioVector::AudioVector(size_t initial_size) |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 28 | : array_(new int16_t[initial_size + 1]), |
| 29 | capacity_(initial_size + 1), |
| 30 | begin_index_(0), |
| 31 | end_index_(capacity_ - 1) { |
| 32 | memset(array_.get(), 0, capacity_ * sizeof(int16_t)); |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | AudioVector::~AudioVector() = default; |
| 36 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 37 | void AudioVector::Clear() { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 38 | end_index_ = begin_index_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 39 | } |
| 40 | |
henrik.lundin@webrtc.org | f6ab6f8 | 2014-09-04 10:58:43 +0000 | [diff] [blame] | 41 | void AudioVector::CopyTo(AudioVector* copy_to) const { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 42 | RTC_DCHECK(copy_to); |
| 43 | copy_to->Reserve(Size()); |
| 44 | CopyTo(Size(), 0, copy_to->array_.get()); |
| 45 | copy_to->begin_index_ = 0; |
| 46 | copy_to->end_index_ = Size(); |
| 47 | } |
| 48 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 49 | void AudioVector::CopyTo(size_t length, |
| 50 | size_t position, |
| 51 | int16_t* copy_to) const { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 52 | if (length == 0) |
| 53 | return; |
| 54 | length = std::min(length, Size() - position); |
| 55 | const size_t copy_index = (begin_index_ + position) % capacity_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 56 | const size_t first_chunk_length = std::min(length, capacity_ - copy_index); |
| 57 | memcpy(copy_to, &array_[copy_index], first_chunk_length * sizeof(int16_t)); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 58 | const size_t remaining_length = length - first_chunk_length; |
| 59 | if (remaining_length > 0) { |
| 60 | memcpy(©_to[first_chunk_length], array_.get(), |
| 61 | remaining_length * sizeof(int16_t)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 65 | void AudioVector::PushFront(const AudioVector& prepend_this) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 66 | const size_t length = prepend_this.Size(); |
| 67 | if (length == 0) |
| 68 | return; |
| 69 | |
| 70 | // Although the subsequent calling to PushFront does Reserve in it, it is |
| 71 | // always more efficient to do a big Reserve first. |
| 72 | Reserve(Size() + length); |
| 73 | |
| 74 | const size_t first_chunk_length = |
| 75 | std::min(length, prepend_this.capacity_ - prepend_this.begin_index_); |
| 76 | const size_t remaining_length = length - first_chunk_length; |
| 77 | if (remaining_length > 0) |
| 78 | PushFront(prepend_this.array_.get(), remaining_length); |
| 79 | PushFront(&prepend_this.array_[prepend_this.begin_index_], |
| 80 | first_chunk_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 81 | } |
| 82 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 83 | void AudioVector::PushFront(const int16_t* prepend_this, size_t length) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 84 | if (length == 0) |
| 85 | return; |
| 86 | Reserve(Size() + length); |
| 87 | const size_t first_chunk_length = std::min(length, begin_index_); |
| 88 | memcpy(&array_[begin_index_ - first_chunk_length], |
| 89 | &prepend_this[length - first_chunk_length], |
| 90 | first_chunk_length * sizeof(int16_t)); |
| 91 | const size_t remaining_length = length - first_chunk_length; |
| 92 | if (remaining_length > 0) { |
| 93 | memcpy(&array_[capacity_ - remaining_length], prepend_this, |
| 94 | remaining_length * sizeof(int16_t)); |
| 95 | } |
| 96 | begin_index_ = (begin_index_ + capacity_ - length) % capacity_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 97 | } |
| 98 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 99 | void AudioVector::PushBack(const AudioVector& append_this) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 100 | PushBack(append_this, append_this.Size(), 0); |
| 101 | } |
| 102 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 103 | void AudioVector::PushBack(const AudioVector& append_this, |
| 104 | size_t length, |
| 105 | size_t position) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 106 | RTC_DCHECK_LE(position, append_this.Size()); |
| 107 | RTC_DCHECK_LE(length, append_this.Size() - position); |
| 108 | |
| 109 | if (length == 0) |
| 110 | return; |
| 111 | |
| 112 | // Although the subsequent calling to PushBack does Reserve in it, it is |
| 113 | // always more efficient to do a big Reserve first. |
| 114 | Reserve(Size() + length); |
| 115 | |
| 116 | const size_t start_index = |
| 117 | (append_this.begin_index_ + position) % append_this.capacity_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 118 | const size_t first_chunk_length = |
| 119 | std::min(length, append_this.capacity_ - start_index); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 120 | PushBack(&append_this.array_[start_index], first_chunk_length); |
| 121 | |
| 122 | const size_t remaining_length = length - first_chunk_length; |
| 123 | if (remaining_length > 0) |
| 124 | PushBack(append_this.array_.get(), remaining_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 125 | } |
| 126 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 127 | void AudioVector::PushBack(const int16_t* append_this, size_t length) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 128 | if (length == 0) |
| 129 | return; |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 130 | Reserve(Size() + length); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 131 | const size_t first_chunk_length = std::min(length, capacity_ - end_index_); |
| 132 | memcpy(&array_[end_index_], append_this, |
| 133 | first_chunk_length * sizeof(int16_t)); |
| 134 | const size_t remaining_length = length - first_chunk_length; |
| 135 | if (remaining_length > 0) { |
| 136 | memcpy(array_.get(), &append_this[first_chunk_length], |
| 137 | remaining_length * sizeof(int16_t)); |
| 138 | } |
| 139 | end_index_ = (end_index_ + length) % capacity_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 140 | } |
| 141 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 142 | void AudioVector::PopFront(size_t length) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 143 | if (length == 0) |
| 144 | return; |
| 145 | length = std::min(length, Size()); |
| 146 | begin_index_ = (begin_index_ + length) % capacity_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 147 | } |
| 148 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 149 | void AudioVector::PopBack(size_t length) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 150 | if (length == 0) |
| 151 | return; |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 152 | // Never remove more than what is in the array. |
| 153 | length = std::min(length, Size()); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 154 | end_index_ = (end_index_ + capacity_ - length) % capacity_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 155 | } |
| 156 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 157 | void AudioVector::Extend(size_t extra_length) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 158 | if (extra_length == 0) |
| 159 | return; |
| 160 | InsertZerosByPushBack(extra_length, Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 161 | } |
| 162 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 163 | void AudioVector::InsertAt(const int16_t* insert_this, |
| 164 | size_t length, |
| 165 | size_t position) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 166 | if (length == 0) |
| 167 | return; |
| 168 | // Cap the insert position at the current array length. |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 169 | position = std::min(Size(), position); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 170 | |
| 171 | // When inserting to a position closer to the beginning, it is more efficient |
| 172 | // to insert by pushing front than to insert by pushing back, since less data |
| 173 | // will be moved, vice versa. |
| 174 | if (position <= Size() - position) { |
| 175 | InsertByPushFront(insert_this, length, position); |
| 176 | } else { |
| 177 | InsertByPushBack(insert_this, length, position); |
| 178 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 181 | void AudioVector::InsertZerosAt(size_t length, size_t position) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 182 | if (length == 0) |
| 183 | return; |
| 184 | // Cap the insert position at the current array length. |
| 185 | position = std::min(Size(), position); |
| 186 | |
| 187 | // When inserting to a position closer to the beginning, it is more efficient |
| 188 | // to insert by pushing front than to insert by pushing back, since less data |
| 189 | // will be moved, vice versa. |
| 190 | if (position <= Size() - position) { |
| 191 | InsertZerosByPushFront(length, position); |
| 192 | } else { |
| 193 | InsertZerosByPushBack(length, position); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void AudioVector::OverwriteAt(const AudioVector& insert_this, |
| 198 | size_t length, |
| 199 | size_t position) { |
| 200 | RTC_DCHECK_LE(length, insert_this.Size()); |
| 201 | if (length == 0) |
| 202 | return; |
| 203 | |
| 204 | // Cap the insert position at the current array length. |
| 205 | position = std::min(Size(), position); |
| 206 | |
| 207 | // Although the subsequent calling to OverwriteAt does Reserve in it, it is |
| 208 | // always more efficient to do a big Reserve first. |
| 209 | size_t new_size = std::max(Size(), position + length); |
| 210 | Reserve(new_size); |
| 211 | |
| 212 | const size_t first_chunk_length = |
| 213 | std::min(length, insert_this.capacity_ - insert_this.begin_index_); |
| 214 | OverwriteAt(&insert_this.array_[insert_this.begin_index_], first_chunk_length, |
| 215 | position); |
| 216 | const size_t remaining_length = length - first_chunk_length; |
| 217 | if (remaining_length > 0) { |
| 218 | OverwriteAt(insert_this.array_.get(), remaining_length, |
| 219 | position + first_chunk_length); |
| 220 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 221 | } |
| 222 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 223 | void AudioVector::OverwriteAt(const int16_t* insert_this, |
| 224 | size_t length, |
| 225 | size_t position) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 226 | if (length == 0) |
| 227 | return; |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 228 | // Cap the insert position at the current array length. |
| 229 | position = std::min(Size(), position); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 230 | |
| 231 | size_t new_size = std::max(Size(), position + length); |
| 232 | Reserve(new_size); |
| 233 | |
| 234 | const size_t overwrite_index = (begin_index_ + position) % capacity_; |
| 235 | const size_t first_chunk_length = |
| 236 | std::min(length, capacity_ - overwrite_index); |
| 237 | memcpy(&array_[overwrite_index], insert_this, |
| 238 | first_chunk_length * sizeof(int16_t)); |
| 239 | const size_t remaining_length = length - first_chunk_length; |
| 240 | if (remaining_length > 0) { |
| 241 | memcpy(array_.get(), &insert_this[first_chunk_length], |
| 242 | remaining_length * sizeof(int16_t)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 243 | } |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 244 | |
| 245 | end_index_ = (begin_index_ + new_size) % capacity_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 246 | } |
| 247 | |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 248 | void AudioVector::CrossFade(const AudioVector& append_this, |
| 249 | size_t fade_length) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 250 | // Fade length cannot be longer than the current vector or |append_this|. |
| 251 | assert(fade_length <= Size()); |
| 252 | assert(fade_length <= append_this.Size()); |
| 253 | fade_length = std::min(fade_length, Size()); |
| 254 | fade_length = std::min(fade_length, append_this.Size()); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 255 | size_t position = Size() - fade_length + begin_index_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 256 | // Cross fade the overlapping regions. |
| 257 | // |alpha| is the mixing factor in Q14. |
| 258 | // TODO(hlundin): Consider skipping +1 in the denominator to produce a |
| 259 | // smoother cross-fade, in particular at the end of the fade. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 260 | int alpha_step = 16384 / (static_cast<int>(fade_length) + 1); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 261 | int alpha = 16384; |
| 262 | for (size_t i = 0; i < fade_length; ++i) { |
| 263 | alpha -= alpha_step; |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 264 | array_[(position + i) % capacity_] = |
| 265 | (alpha * array_[(position + i) % capacity_] + |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 266 | (16384 - alpha) * append_this[i] + 8192) >> |
| 267 | 14; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 268 | } |
| 269 | assert(alpha >= 0); // Verify that the slope was correct. |
| 270 | // Append what is left of |append_this|. |
henrik.lundin@webrtc.org | 63464a9 | 2013-01-30 09:41:56 +0000 | [diff] [blame] | 271 | size_t samples_to_push_back = append_this.Size() - fade_length; |
| 272 | if (samples_to_push_back > 0) |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 273 | PushBack(append_this, samples_to_push_back, fade_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 276 | // Returns the number of elements in this AudioVector. |
| 277 | size_t AudioVector::Size() const { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 278 | return (end_index_ + capacity_ - begin_index_) % capacity_; |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Returns true if this AudioVector is empty. |
| 282 | bool AudioVector::Empty() const { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 283 | return begin_index_ == end_index_; |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 284 | } |
| 285 | |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 286 | void AudioVector::Reserve(size_t n) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 287 | if (capacity_ > n) |
| 288 | return; |
| 289 | const size_t length = Size(); |
| 290 | // Reserve one more sample to remove the ambiguity between empty vector and |
| 291 | // full vector. Therefore |begin_index_| == |end_index_| indicates empty |
| 292 | // vector, and |begin_index_| == (|end_index_| + 1) % capacity indicates |
| 293 | // full vector. |
| 294 | std::unique_ptr<int16_t[]> temp_array(new int16_t[n + 1]); |
| 295 | CopyTo(length, 0, temp_array.get()); |
| 296 | array_.swap(temp_array); |
| 297 | begin_index_ = 0; |
| 298 | end_index_ = length; |
| 299 | capacity_ = n + 1; |
| 300 | } |
| 301 | |
| 302 | void AudioVector::InsertByPushBack(const int16_t* insert_this, |
| 303 | size_t length, |
| 304 | size_t position) { |
| 305 | const size_t move_chunk_length = Size() - position; |
| 306 | std::unique_ptr<int16_t[]> temp_array(nullptr); |
| 307 | if (move_chunk_length > 0) { |
| 308 | // TODO(minyue): see if it is possible to avoid copying to a buffer. |
| 309 | temp_array.reset(new int16_t[move_chunk_length]); |
| 310 | CopyTo(move_chunk_length, position, temp_array.get()); |
| 311 | PopBack(move_chunk_length); |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 312 | } |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 313 | |
| 314 | Reserve(Size() + length + move_chunk_length); |
| 315 | PushBack(insert_this, length); |
| 316 | if (move_chunk_length > 0) |
| 317 | PushBack(temp_array.get(), move_chunk_length); |
| 318 | } |
| 319 | |
| 320 | void AudioVector::InsertByPushFront(const int16_t* insert_this, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 321 | size_t length, |
| 322 | size_t position) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 323 | std::unique_ptr<int16_t[]> temp_array(nullptr); |
| 324 | if (position > 0) { |
| 325 | // TODO(minyue): see if it is possible to avoid copying to a buffer. |
| 326 | temp_array.reset(new int16_t[position]); |
| 327 | CopyTo(position, 0, temp_array.get()); |
| 328 | PopFront(position); |
| 329 | } |
| 330 | |
| 331 | Reserve(Size() + length + position); |
| 332 | PushFront(insert_this, length); |
| 333 | if (position > 0) |
| 334 | PushFront(temp_array.get(), position); |
| 335 | } |
| 336 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 337 | void AudioVector::InsertZerosByPushBack(size_t length, size_t position) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 338 | const size_t move_chunk_length = Size() - position; |
| 339 | std::unique_ptr<int16_t[]> temp_array(nullptr); |
| 340 | if (move_chunk_length > 0) { |
| 341 | temp_array.reset(new int16_t[move_chunk_length]); |
| 342 | CopyTo(move_chunk_length, position, temp_array.get()); |
| 343 | PopBack(move_chunk_length); |
| 344 | } |
| 345 | |
| 346 | Reserve(Size() + length + move_chunk_length); |
| 347 | |
| 348 | const size_t first_zero_chunk_length = |
| 349 | std::min(length, capacity_ - end_index_); |
| 350 | memset(&array_[end_index_], 0, first_zero_chunk_length * sizeof(int16_t)); |
| 351 | const size_t remaining_zero_length = length - first_zero_chunk_length; |
| 352 | if (remaining_zero_length > 0) |
| 353 | memset(array_.get(), 0, remaining_zero_length * sizeof(int16_t)); |
| 354 | end_index_ = (end_index_ + length) % capacity_; |
| 355 | |
| 356 | if (move_chunk_length > 0) |
| 357 | PushBack(temp_array.get(), move_chunk_length); |
| 358 | } |
| 359 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 360 | void AudioVector::InsertZerosByPushFront(size_t length, size_t position) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 361 | std::unique_ptr<int16_t[]> temp_array(nullptr); |
| 362 | if (position > 0) { |
| 363 | temp_array.reset(new int16_t[position]); |
| 364 | CopyTo(position, 0, temp_array.get()); |
| 365 | PopFront(position); |
| 366 | } |
| 367 | |
| 368 | Reserve(Size() + length + position); |
| 369 | |
| 370 | const size_t first_zero_chunk_length = std::min(length, begin_index_); |
| 371 | memset(&array_[begin_index_ - first_zero_chunk_length], 0, |
| 372 | first_zero_chunk_length * sizeof(int16_t)); |
| 373 | const size_t remaining_zero_length = length - first_zero_chunk_length; |
| 374 | if (remaining_zero_length > 0) |
| 375 | memset(&array_[capacity_ - remaining_zero_length], 0, |
| 376 | remaining_zero_length * sizeof(int16_t)); |
| 377 | begin_index_ = (begin_index_ + capacity_ - length) % capacity_; |
| 378 | |
| 379 | if (position > 0) |
| 380 | PushFront(temp_array.get(), position); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 381 | } |
| 382 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 383 | } // namespace webrtc |