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 | #ifndef MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <string.h> |
| 15 | #include <cstdint> |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 16 | #include <memory> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 19 | #include "rtc_base/constructor_magic.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 23 | class AudioVector { |
| 24 | public: |
| 25 | // Creates an empty AudioVector. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 26 | AudioVector(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 27 | |
| 28 | // Creates an AudioVector with an initial size. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 29 | explicit AudioVector(size_t initial_size); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 30 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 31 | virtual ~AudioVector(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | |
| 33 | // Deletes all values and make the vector empty. |
| 34 | virtual void Clear(); |
| 35 | |
| 36 | // Copies all values from this vector to |copy_to|. Any contents in |copy_to| |
| 37 | // are deleted before the copy operation. After the operation is done, |
| 38 | // |copy_to| will be an exact replica of this object. |
henrik.lundin@webrtc.org | f6ab6f8 | 2014-09-04 10:58:43 +0000 | [diff] [blame] | 39 | virtual void CopyTo(AudioVector* copy_to) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 40 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 41 | // Copies |length| values from |position| in this vector to |copy_to|. |
| 42 | virtual void CopyTo(size_t length, size_t position, int16_t* copy_to) const; |
| 43 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | // Prepends the contents of AudioVector |prepend_this| to this object. The |
| 45 | // length of this object is increased with the length of |prepend_this|. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 46 | virtual void PushFront(const AudioVector& prepend_this); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 47 | |
| 48 | // Same as above, but with an array |prepend_this| with |length| elements as |
| 49 | // source. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 50 | virtual void PushFront(const int16_t* prepend_this, size_t length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 51 | |
| 52 | // Same as PushFront but will append to the end of this object. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 53 | virtual void PushBack(const AudioVector& append_this); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 55 | // Appends a segment of |append_this| to the end of this object. The segment |
| 56 | // starts from |position| and has |length| samples. |
| 57 | virtual void PushBack(const AudioVector& append_this, |
| 58 | size_t length, |
| 59 | size_t position); |
| 60 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 61 | // Same as PushFront but will append to the end of this object. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 62 | virtual void PushBack(const int16_t* append_this, size_t length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 63 | |
| 64 | // Removes |length| elements from the beginning of this object. |
| 65 | virtual void PopFront(size_t length); |
| 66 | |
| 67 | // Removes |length| elements from the end of this object. |
| 68 | virtual void PopBack(size_t length); |
| 69 | |
| 70 | // Extends this object with |extra_length| elements at the end. The new |
| 71 | // elements are initialized to zero. |
| 72 | virtual void Extend(size_t extra_length); |
| 73 | |
| 74 | // Inserts |length| elements taken from the array |insert_this| and insert |
| 75 | // them at |position|. The length of the AudioVector is increased by |length|. |
| 76 | // |position| = 0 means that the new values are prepended to the vector. |
| 77 | // |position| = Size() means that the new values are appended to the vector. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | virtual void InsertAt(const int16_t* insert_this, |
| 79 | size_t length, |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 80 | size_t position); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 81 | |
| 82 | // Like InsertAt, but inserts |length| zero elements at |position|. |
| 83 | virtual void InsertZerosAt(size_t length, size_t position); |
| 84 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 85 | // Overwrites |length| elements of this AudioVector starting from |position| |
| 86 | // with first values in |AudioVector|. The definition of |position| |
| 87 | // is the same as for InsertAt(). If |length| and |position| are selected |
| 88 | // such that the new data extends beyond the end of the current AudioVector, |
| 89 | // the vector is extended to accommodate the new data. |
| 90 | virtual void OverwriteAt(const AudioVector& insert_this, |
| 91 | size_t length, |
| 92 | size_t position); |
| 93 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | // Overwrites |length| elements of this AudioVector with values taken from the |
| 95 | // array |insert_this|, starting at |position|. The definition of |position| |
| 96 | // is the same as for InsertAt(). If |length| and |position| are selected |
| 97 | // such that the new data extends beyond the end of the current AudioVector, |
| 98 | // the vector is extended to accommodate the new data. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 99 | virtual void OverwriteAt(const int16_t* insert_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 100 | size_t length, |
| 101 | size_t position); |
| 102 | |
| 103 | // Appends |append_this| to the end of the current vector. Lets the two |
| 104 | // vectors overlap by |fade_length| samples, and cross-fade linearly in this |
| 105 | // region. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 106 | virtual void CrossFade(const AudioVector& append_this, size_t fade_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 107 | |
| 108 | // Returns the number of elements in this AudioVector. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 109 | virtual size_t Size() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 110 | |
| 111 | // Returns true if this AudioVector is empty. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 112 | virtual bool Empty() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 113 | |
| 114 | // Accesses and modifies an element of AudioVector. |
henrik.lundin | 280eb22 | 2017-02-15 02:53:05 -0800 | [diff] [blame] | 115 | inline const int16_t& operator[](size_t index) const { |
| 116 | return array_[WrapIndex(index, begin_index_, capacity_)]; |
| 117 | } |
| 118 | |
| 119 | inline int16_t& operator[](size_t index) { |
| 120 | return array_[WrapIndex(index, begin_index_, capacity_)]; |
| 121 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 122 | |
| 123 | private: |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 124 | static const size_t kDefaultInitialSize = 10; |
| 125 | |
henrik.lundin | 280eb22 | 2017-02-15 02:53:05 -0800 | [diff] [blame] | 126 | // This method is used by the [] operators to calculate an index within the |
| 127 | // capacity of the array, but without using the modulo operation (%). |
| 128 | static inline size_t WrapIndex(size_t index, |
| 129 | size_t begin_index, |
| 130 | size_t capacity) { |
henrik.lundin | 5650a7d | 2017-02-22 03:45:40 -0800 | [diff] [blame] | 131 | RTC_DCHECK_LT(index, capacity); |
| 132 | RTC_DCHECK_LT(begin_index, capacity); |
| 133 | size_t ix = begin_index + index; |
| 134 | RTC_DCHECK_GE(ix, index); // Check for overflow. |
| 135 | if (ix >= capacity) { |
| 136 | ix -= capacity; |
| 137 | } |
henrik.lundin | 280eb22 | 2017-02-15 02:53:05 -0800 | [diff] [blame] | 138 | RTC_DCHECK_LT(ix, capacity); |
| 139 | return ix; |
| 140 | } |
| 141 | |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 142 | void Reserve(size_t n); |
| 143 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 144 | void InsertByPushBack(const int16_t* insert_this, |
| 145 | size_t length, |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 146 | size_t position); |
| 147 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 148 | void InsertByPushFront(const int16_t* insert_this, |
| 149 | size_t length, |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 150 | size_t position); |
| 151 | |
| 152 | void InsertZerosByPushBack(size_t length, size_t position); |
| 153 | |
| 154 | void InsertZerosByPushFront(size_t length, size_t position); |
| 155 | |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 156 | std::unique_ptr<int16_t[]> array_; |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 157 | |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 +0000 | [diff] [blame] | 158 | size_t capacity_; // Allocated number of samples in the array. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 159 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 160 | // The index of the first sample in |array_|, except when |
| 161 | // |begin_index_ == end_index_|, which indicates an empty buffer. |
| 162 | size_t begin_index_; |
| 163 | |
| 164 | // The index of the sample after the last sample in |array_|. |
| 165 | size_t end_index_; |
| 166 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 167 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioVector); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 171 | #endif // MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_ |