henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [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 04:47:31 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_AUDIO_MULTI_VECTOR_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_AUDIO_MULTI_VECTOR_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 19 | #include "api/array_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "modules/audio_coding/neteq/audio_vector.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 24 | class AudioMultiVector { |
| 25 | public: |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 26 | // Creates an empty AudioMultiVector with `N` audio channels. `N` must be |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 27 | // larger than 0. |
| 28 | explicit AudioMultiVector(size_t N); |
| 29 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 30 | // Creates an AudioMultiVector with `N` audio channels, each channel having |
| 31 | // an initial size. `N` must be larger than 0. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 32 | AudioMultiVector(size_t N, size_t initial_size); |
| 33 | |
| 34 | virtual ~AudioMultiVector(); |
| 35 | |
Byoungchan Lee | 604fd2f | 2022-01-21 00:49:39 | [diff] [blame] | 36 | AudioMultiVector(const AudioMultiVector&) = delete; |
| 37 | AudioMultiVector& operator=(const AudioMultiVector&) = delete; |
| 38 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 39 | // Deletes all values and make the vector empty. |
| 40 | virtual void Clear(); |
| 41 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 42 | // Clears the vector and inserts `length` zeros into each channel. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 43 | virtual void Zeros(size_t length); |
| 44 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 45 | // Copies all values from this vector to `copy_to`. Any contents in `copy_to` |
| 46 | // are deleted. After the operation is done, `copy_to` will be an exact |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 47 | // replica of this object. The source and the destination must have the same |
| 48 | // number of channels. |
henrik.lundin@webrtc.org | f6ab6f8 | 2014-09-04 10:58:43 | [diff] [blame] | 49 | virtual void CopyTo(AudioMultiVector* copy_to) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 50 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 51 | // Appends the contents of `append_this` to the end of this object. The array |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 52 | // is assumed to be channel-interleaved. The length must be an even multiple |
| 53 | // of this object's number of channels. The length of this object is increased |
| 54 | // with the length of the array divided by the number of channels. |
| 55 | void PushBackInterleaved(rtc::ArrayView<const int16_t> append_this); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 56 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 57 | // Appends the contents of AudioMultiVector `append_this` to this object. The |
| 58 | // length of this object is increased with the length of `append_this`. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 59 | virtual void PushBack(const AudioMultiVector& append_this); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 60 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 61 | // Appends the contents of AudioMultiVector `append_this` to this object, |
| 62 | // taken from `index` up until the end of `append_this`. The length of this |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 63 | // object is increased. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 64 | virtual void PushBackFromIndex(const AudioMultiVector& append_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 65 | size_t index); |
| 66 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 67 | // Removes `length` elements from the beginning of this object, from each |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 68 | // channel. |
| 69 | virtual void PopFront(size_t length); |
| 70 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 71 | // Removes `length` elements from the end of this object, from each |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 72 | // channel. |
| 73 | virtual void PopBack(size_t length); |
| 74 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 75 | // Reads `length` samples from each channel and writes them interleaved to |
| 76 | // `destination`. The total number of elements written to `destination` is |
| 77 | // returned, i.e., `length` * number of channels. If the AudioMultiVector |
| 78 | // contains less than `length` samples per channel, this is reflected in the |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 79 | // return value. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 80 | virtual size_t ReadInterleaved(size_t length, int16_t* destination) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 81 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 82 | // Like ReadInterleaved() above, but reads from `start_index` instead of from |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 83 | // the beginning. |
| 84 | virtual size_t ReadInterleavedFromIndex(size_t start_index, |
| 85 | size_t length, |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 86 | int16_t* destination) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 87 | |
| 88 | // Like ReadInterleaved() above, but reads from the end instead of from |
| 89 | // the beginning. |
| 90 | virtual size_t ReadInterleavedFromEnd(size_t length, |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 91 | int16_t* destination) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 92 | |
| 93 | // Overwrites each channel in this AudioMultiVector with values taken from |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 94 | // `insert_this`. The values are taken from the beginning of `insert_this` and |
| 95 | // are inserted starting at `position`. `length` values are written into each |
| 96 | // channel. If `length` and `position` are selected such that the new data |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 97 | // extends beyond the end of the current AudioVector, the vector is extended |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 98 | // to accommodate the new data. `length` is limited to the length of |
| 99 | // `insert_this`. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 100 | virtual void OverwriteAt(const AudioMultiVector& insert_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 101 | size_t length, |
| 102 | size_t position); |
| 103 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 104 | // Appends `append_this` to the end of the current vector. Lets the two |
| 105 | // vectors overlap by `fade_length` samples (per channel), and cross-fade |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 106 | // linearly in this region. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 107 | virtual void CrossFade(const AudioMultiVector& append_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 108 | size_t fade_length); |
| 109 | |
| 110 | // Returns the number of channels. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 13:44:22 | [diff] [blame] | 111 | virtual size_t Channels() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 112 | |
| 113 | // Returns the number of elements per channel in this AudioMultiVector. |
| 114 | virtual size_t Size() const; |
| 115 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 116 | // Verify that each channel can hold at least `required_size` elements. If |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 117 | // not, extend accordingly. |
| 118 | virtual void AssertSize(size_t required_size); |
| 119 | |
| 120 | virtual bool Empty() const; |
| 121 | |
henrik.lundin@webrtc.org | 7825b1a | 2014-09-04 07:39:21 | [diff] [blame] | 122 | // Copies the data between two channels in the AudioMultiVector. The method |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 123 | // does not add any new channel. Thus, `from_channel` and `to_channel` must |
henrik.lundin@webrtc.org | 7825b1a | 2014-09-04 07:39:21 | [diff] [blame] | 124 | // both be valid channel numbers. |
| 125 | virtual void CopyChannel(size_t from_channel, size_t to_channel); |
| 126 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 127 | // Accesses and modifies a channel (i.e., an AudioVector object) of this |
| 128 | // AudioMultiVector. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 | [diff] [blame] | 129 | const AudioVector& operator[](size_t index) const; |
| 130 | AudioVector& operator[](size_t index); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 131 | |
| 132 | protected: |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 | [diff] [blame] | 133 | std::vector<AudioVector*> channels_; |
henrik.lundin@webrtc.org | e8433eb | 2013-11-12 13:15:02 | [diff] [blame] | 134 | size_t num_channels_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 138 | #endif // MODULES_AUDIO_CODING_NETEQ_AUDIO_MULTI_VECTOR_H_ |