blob: 9275cb34b7d9ef9ce2f42d23cd09fb5bc0a00635 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
12#define MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <string.h> // Access to size_t.
kwiberg2d0c3322016-02-14 09:28:33 -080015#include <memory>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/constructormagic.h"
19#include "typedefs.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020
21namespace webrtc {
22
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023class AudioVector {
24 public:
25 // Creates an empty AudioVector.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020026 AudioVector();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027
28 // Creates an AudioVector with an initial size.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020029 explicit AudioVector(size_t initial_size);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020031 virtual ~AudioVector();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032
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.orgf6ab6f82014-09-04 10:58:43 +000039 virtual void CopyTo(AudioVector* copy_to) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
minyue-webrtc79553cb2016-05-10 19:55:56 +020041 // 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.orgd94659d2013-01-29 12:09:21 +000044 // 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.org1871dd22013-10-14 20:33:25 +000046 virtual void PushFront(const AudioVector& prepend_this);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
48 // Same as above, but with an array |prepend_this| with |length| elements as
49 // source.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000050 virtual void PushFront(const int16_t* prepend_this, size_t length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051
52 // Same as PushFront but will append to the end of this object.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000053 virtual void PushBack(const AudioVector& append_this);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054
minyue-webrtc79553cb2016-05-10 19:55:56 +020055 // 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.orgd94659d2013-01-29 12:09:21 +000061 // Same as PushFront but will append to the end of this object.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000062 virtual void PushBack(const int16_t* append_this, size_t length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063
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.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000078 virtual void InsertAt(const int16_t* insert_this, size_t length,
79 size_t position);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080
81 // Like InsertAt, but inserts |length| zero elements at |position|.
82 virtual void InsertZerosAt(size_t length, size_t position);
83
minyue-webrtc79553cb2016-05-10 19:55:56 +020084 // Overwrites |length| elements of this AudioVector starting from |position|
85 // with first values in |AudioVector|. The definition of |position|
86 // is the same as for InsertAt(). If |length| and |position| are selected
87 // such that the new data extends beyond the end of the current AudioVector,
88 // the vector is extended to accommodate the new data.
89 virtual void OverwriteAt(const AudioVector& insert_this,
90 size_t length,
91 size_t position);
92
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093 // Overwrites |length| elements of this AudioVector with values taken from the
94 // array |insert_this|, starting at |position|. The definition of |position|
95 // is the same as for InsertAt(). If |length| and |position| are selected
96 // such that the new data extends beyond the end of the current AudioVector,
97 // the vector is extended to accommodate the new data.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000098 virtual void OverwriteAt(const int16_t* insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099 size_t length,
100 size_t position);
101
102 // Appends |append_this| to the end of the current vector. Lets the two
103 // vectors overlap by |fade_length| samples, and cross-fade linearly in this
104 // region.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +0000105 virtual void CrossFade(const AudioVector& append_this, size_t fade_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106
107 // Returns the number of elements in this AudioVector.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200108 virtual size_t Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109
110 // Returns true if this AudioVector is empty.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200111 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112
113 // Accesses and modifies an element of AudioVector.
henrik.lundin280eb222017-02-15 02:53:05 -0800114 inline const int16_t& operator[](size_t index) const {
115 return array_[WrapIndex(index, begin_index_, capacity_)];
116 }
117
118 inline int16_t& operator[](size_t index) {
119 return array_[WrapIndex(index, begin_index_, capacity_)];
120 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121
122 private:
henrik.lundin@webrtc.orge8433eb2013-11-12 13:15:02 +0000123 static const size_t kDefaultInitialSize = 10;
124
henrik.lundin280eb222017-02-15 02:53:05 -0800125 // This method is used by the [] operators to calculate an index within the
126 // capacity of the array, but without using the modulo operation (%).
127 static inline size_t WrapIndex(size_t index,
128 size_t begin_index,
129 size_t capacity) {
henrik.lundin5650a7d2017-02-22 03:45:40 -0800130 RTC_DCHECK_LT(index, capacity);
131 RTC_DCHECK_LT(begin_index, capacity);
132 size_t ix = begin_index + index;
133 RTC_DCHECK_GE(ix, index); // Check for overflow.
134 if (ix >= capacity) {
135 ix -= capacity;
136 }
henrik.lundin280eb222017-02-15 02:53:05 -0800137 RTC_DCHECK_LT(ix, capacity);
138 return ix;
139 }
140
henrik.lundin@webrtc.orge8433eb2013-11-12 13:15:02 +0000141 void Reserve(size_t n);
142
minyue-webrtc79553cb2016-05-10 19:55:56 +0200143 void InsertByPushBack(const int16_t* insert_this, size_t length,
144 size_t position);
145
146 void InsertByPushFront(const int16_t* insert_this, size_t length,
147 size_t position);
148
149 void InsertZerosByPushBack(size_t length, size_t position);
150
151 void InsertZerosByPushFront(size_t length, size_t position);
152
kwiberg2d0c3322016-02-14 09:28:33 -0800153 std::unique_ptr<int16_t[]> array_;
minyue-webrtc79553cb2016-05-10 19:55:56 +0200154
henrik.lundin@webrtc.orge8433eb2013-11-12 13:15:02 +0000155 size_t capacity_; // Allocated number of samples in the array.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156
minyue-webrtc79553cb2016-05-10 19:55:56 +0200157 // The index of the first sample in |array_|, except when
158 // |begin_index_ == end_index_|, which indicates an empty buffer.
159 size_t begin_index_;
160
161 // The index of the sample after the last sample in |array_|.
162 size_t end_index_;
163
henrikg3c089d72015-09-16 05:37:44 -0700164 RTC_DISALLOW_COPY_AND_ASSIGN(AudioVector);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000165};
166
167} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200168#endif // MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_