blob: d68f3ec6be25180427bb794a6212aefd38fd77cf [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <cstdint>
kwiberg2d0c3322016-02-14 09:28:33 -080017#include <memory>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.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
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090033 AudioVector(const AudioVector&) = delete;
34 AudioVector& operator=(const AudioVector&) = delete;
35
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036 // Deletes all values and make the vector empty.
37 virtual void Clear();
38
Artem Titovd00ce742021-07-28 20:00:17 +020039 // Copies all values from this vector to `copy_to`. Any contents in `copy_to`
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040 // are deleted before the copy operation. After the operation is done,
Artem Titovd00ce742021-07-28 20:00:17 +020041 // `copy_to` will be an exact replica of this object.
henrik.lundin@webrtc.orgf6ab6f82014-09-04 10:58:43 +000042 virtual void CopyTo(AudioVector* copy_to) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
Artem Titovd00ce742021-07-28 20:00:17 +020044 // Copies `length` values from `position` in this vector to `copy_to`.
minyue-webrtc79553cb2016-05-10 19:55:56 +020045 virtual void CopyTo(size_t length, size_t position, int16_t* copy_to) const;
46
Artem Titovd00ce742021-07-28 20:00:17 +020047 // Prepends the contents of AudioVector `prepend_this` to this object. The
48 // length of this object is increased with the length of `prepend_this`.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000049 virtual void PushFront(const AudioVector& prepend_this);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
Artem Titovd00ce742021-07-28 20:00:17 +020051 // Same as above, but with an array `prepend_this` with `length` elements as
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052 // source.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000053 virtual void PushFront(const int16_t* prepend_this, size_t length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054
55 // Same as PushFront but will append to the end of this object.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000056 virtual void PushBack(const AudioVector& append_this);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057
Artem Titovd00ce742021-07-28 20:00:17 +020058 // Appends a segment of `append_this` to the end of this object. The segment
59 // starts from `position` and has `length` samples.
minyue-webrtc79553cb2016-05-10 19:55:56 +020060 virtual void PushBack(const AudioVector& append_this,
61 size_t length,
62 size_t position);
63
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064 // Same as PushFront but will append to the end of this object.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000065 virtual void PushBack(const int16_t* append_this, size_t length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066
Artem Titovd00ce742021-07-28 20:00:17 +020067 // Removes `length` elements from the beginning of this object.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 virtual void PopFront(size_t length);
69
Artem Titovd00ce742021-07-28 20:00:17 +020070 // Removes `length` elements from the end of this object.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071 virtual void PopBack(size_t length);
72
Artem Titovd00ce742021-07-28 20:00:17 +020073 // Extends this object with `extra_length` elements at the end. The new
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074 // elements are initialized to zero.
75 virtual void Extend(size_t extra_length);
76
Artem Titovd00ce742021-07-28 20:00:17 +020077 // Inserts `length` elements taken from the array `insert_this` and insert
78 // them at `position`. The length of the AudioVector is increased by `length`.
79 // `position` = 0 means that the new values are prepended to the vector.
80 // `position` = Size() means that the new values are appended to the vector.
Yves Gerey665174f2018-06-19 15:03:05 +020081 virtual void InsertAt(const int16_t* insert_this,
82 size_t length,
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +000083 size_t position);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084
Artem Titovd00ce742021-07-28 20:00:17 +020085 // Like InsertAt, but inserts `length` zero elements at `position`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086 virtual void InsertZerosAt(size_t length, size_t position);
87
Artem Titovd00ce742021-07-28 20:00:17 +020088 // Overwrites `length` elements of this AudioVector starting from `position`
89 // with first values in `AudioVector`. The definition of `position`
90 // is the same as for InsertAt(). If `length` and `position` are selected
minyue-webrtc79553cb2016-05-10 19:55:56 +020091 // such that the new data extends beyond the end of the current AudioVector,
92 // the vector is extended to accommodate the new data.
93 virtual void OverwriteAt(const AudioVector& insert_this,
94 size_t length,
95 size_t position);
96
Artem Titovd00ce742021-07-28 20:00:17 +020097 // Overwrites `length` elements of this AudioVector with values taken from the
98 // array `insert_this`, starting at `position`. The definition of `position`
99 // is the same as for InsertAt(). If `length` and `position` are selected
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100 // such that the new data extends beyond the end of the current AudioVector,
101 // the vector is extended to accommodate the new data.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +0000102 virtual void OverwriteAt(const int16_t* insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000103 size_t length,
104 size_t position);
105
Artem Titovd00ce742021-07-28 20:00:17 +0200106 // Appends `append_this` to the end of the current vector. Lets the two
107 // vectors overlap by `fade_length` samples, and cross-fade linearly in this
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 // region.
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +0000109 virtual void CrossFade(const AudioVector& append_this, size_t fade_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110
111 // Returns the number of elements in this AudioVector.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200112 virtual size_t Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113
114 // Returns true if this AudioVector is empty.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200115 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116
117 // Accesses and modifies an element of AudioVector.
henrik.lundin280eb222017-02-15 02:53:05 -0800118 inline const int16_t& operator[](size_t index) const {
119 return array_[WrapIndex(index, begin_index_, capacity_)];
120 }
121
122 inline int16_t& operator[](size_t index) {
123 return array_[WrapIndex(index, begin_index_, capacity_)];
124 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125
126 private:
henrik.lundin@webrtc.orge8433eb2013-11-12 13:15:02 +0000127 static const size_t kDefaultInitialSize = 10;
128
henrik.lundin280eb222017-02-15 02:53:05 -0800129 // This method is used by the [] operators to calculate an index within the
130 // capacity of the array, but without using the modulo operation (%).
131 static inline size_t WrapIndex(size_t index,
132 size_t begin_index,
133 size_t capacity) {
henrik.lundin5650a7d2017-02-22 03:45:40 -0800134 RTC_DCHECK_LT(index, capacity);
135 RTC_DCHECK_LT(begin_index, capacity);
136 size_t ix = begin_index + index;
137 RTC_DCHECK_GE(ix, index); // Check for overflow.
138 if (ix >= capacity) {
139 ix -= capacity;
140 }
henrik.lundin280eb222017-02-15 02:53:05 -0800141 RTC_DCHECK_LT(ix, capacity);
142 return ix;
143 }
144
henrik.lundin@webrtc.orge8433eb2013-11-12 13:15:02 +0000145 void Reserve(size_t n);
146
Yves Gerey665174f2018-06-19 15:03:05 +0200147 void InsertByPushBack(const int16_t* insert_this,
148 size_t length,
minyue-webrtc79553cb2016-05-10 19:55:56 +0200149 size_t position);
150
Yves Gerey665174f2018-06-19 15:03:05 +0200151 void InsertByPushFront(const int16_t* insert_this,
152 size_t length,
minyue-webrtc79553cb2016-05-10 19:55:56 +0200153 size_t position);
154
155 void InsertZerosByPushBack(size_t length, size_t position);
156
157 void InsertZerosByPushFront(size_t length, size_t position);
158
kwiberg2d0c3322016-02-14 09:28:33 -0800159 std::unique_ptr<int16_t[]> array_;
minyue-webrtc79553cb2016-05-10 19:55:56 +0200160
henrik.lundin@webrtc.orge8433eb2013-11-12 13:15:02 +0000161 size_t capacity_; // Allocated number of samples in the array.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162
Artem Titovd00ce742021-07-28 20:00:17 +0200163 // The index of the first sample in `array_`, except when
minyue-webrtc79553cb2016-05-10 19:55:56 +0200164 // |begin_index_ == end_index_|, which indicates an empty buffer.
165 size_t begin_index_;
166
Artem Titovd00ce742021-07-28 20:00:17 +0200167 // The index of the sample after the last sample in `array_`.
minyue-webrtc79553cb2016-05-10 19:55:56 +0200168 size_t end_index_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169};
170
171} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200172#endif // MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_