audio_multi_vector.h (6163B)
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 11 #ifndef MODULES_AUDIO_CODING_NETEQ_AUDIO_MULTI_VECTOR_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_AUDIO_MULTI_VECTOR_H_ 13 14 #include <stdint.h> 15 #include <string.h> 16 17 #include <memory> 18 #include <vector> 19 20 #include "api/array_view.h" 21 #include "api/audio/audio_view.h" 22 #include "modules/audio_coding/neteq/audio_vector.h" 23 24 namespace webrtc { 25 26 // TODO: b/335805780 - Update to use InterleavedView. 27 class AudioMultiVector { 28 public: 29 // Creates an empty AudioMultiVector with `N` audio channels. `N` must be 30 // larger than 0. 31 explicit AudioMultiVector(size_t N); 32 33 // Creates an AudioMultiVector with `N` audio channels, each channel having 34 // an initial size. `N` must be larger than 0. 35 AudioMultiVector(size_t N, size_t initial_size); 36 37 virtual ~AudioMultiVector(); 38 39 AudioMultiVector(const AudioMultiVector&) = delete; 40 AudioMultiVector& operator=(const AudioMultiVector&) = delete; 41 42 // Deletes all values and make the vector empty. 43 void Clear(); 44 45 // Clears the vector and inserts `length` zeros into each channel. 46 void Zeros(size_t length); 47 48 // Copies all values from this vector to `copy_to`. Any contents in `copy_to` 49 // are deleted. After the operation is done, `copy_to` will be an exact 50 // replica of this object. The source and the destination must have the same 51 // number of channels. 52 void CopyTo(AudioMultiVector* copy_to) const; 53 54 // Appends the contents of `append_this` to the end of this object. The array 55 // is assumed to be channel-interleaved. The length must be an even multiple 56 // of this object's number of channels. The length of this object is increased 57 // with the length of the array divided by the number of channels. 58 void PushBackInterleaved(ArrayView<const int16_t> append_this); 59 60 // Appends the contents of AudioMultiVector `append_this` to this object. The 61 // length of this object is increased with the length of `append_this`. 62 virtual void PushBack(const AudioMultiVector& append_this); 63 64 // Appends the contents of AudioMultiVector `append_this` to this object, 65 // taken from `index` up until the end of `append_this`. The length of this 66 // object is increased. 67 void PushBackFromIndex(const AudioMultiVector& append_this, size_t index); 68 69 // Removes `length` elements from the beginning of this object, from each 70 // channel. 71 void PopFront(size_t length); 72 73 // Removes `length` elements from the end of this object, from each 74 // channel. 75 void PopBack(size_t length); 76 77 // Reads `length` samples from each channel and writes them interleaved to 78 // `destination`. The total number of elements written to `destination` is 79 // returned, i.e., `length` * number of channels. If the AudioMultiVector 80 // contains less than `length` samples per channel, this is reflected in the 81 // return value. 82 size_t ReadInterleaved(size_t length, int16_t* destination) const; 83 84 // Like ReadInterleaved() above, but reads from `start_index` instead of from 85 // the beginning. 86 size_t ReadInterleavedFromIndex(size_t start_index, 87 size_t length, 88 int16_t* destination) const; 89 90 // Reads `dst.samples_per_channel()` from each channel into `dst`, a total of 91 // `dst.size()` samples, starting from the position provided by `start_index`. 92 // 93 // If not enough samples are available to read, then *none* will be read and 94 // the function returns false. If enough samples could be read, the return 95 // value will be true. 96 // 97 // This behavior is currently different from the pointer based 98 // `ReadInterleaved*` methods, but intentionally so in order to simplify the 99 // logic at the caller site. 100 bool ReadInterleavedFromIndex(const size_t start_index, 101 InterleavedView<int16_t> dst) const; 102 103 // Like ReadInterleaved() above, but reads from the end instead of from 104 // the beginning. 105 size_t ReadInterleavedFromEnd(size_t length, int16_t* destination) const; 106 107 // Overwrites each channel in this AudioMultiVector with values taken from 108 // `insert_this`. The values are taken from the beginning of `insert_this` and 109 // are inserted starting at `position`. `length` values are written into each 110 // channel. If `length` and `position` are selected such that the new data 111 // extends beyond the end of the current AudioVector, the vector is extended 112 // to accommodate the new data. `length` is limited to the length of 113 // `insert_this`. 114 void OverwriteAt(const AudioMultiVector& insert_this, 115 size_t length, 116 size_t position); 117 118 // Appends `append_this` to the end of the current vector. Lets the two 119 // vectors overlap by `fade_length` samples (per channel), and cross-fade 120 // linearly in this region. 121 void CrossFade(const AudioMultiVector& append_this, size_t fade_length); 122 123 // Returns the number of channels. 124 size_t Channels() const; 125 126 // Returns the number of elements per channel in this AudioMultiVector. 127 size_t Size() const; 128 129 // Verify that each channel can hold at least `required_size` elements. If 130 // not, extend accordingly. 131 void AssertSize(size_t required_size); 132 133 bool Empty() const; 134 135 // Copies the data between two channels in the AudioMultiVector. The method 136 // does not add any new channel. Thus, `from_channel` and `to_channel` must 137 // both be valid channel numbers. 138 void CopyChannel(size_t from_channel, size_t to_channel); 139 140 // Accesses and modifies a channel (i.e., an AudioVector object) of this 141 // AudioMultiVector. 142 const AudioVector& operator[](size_t index) const; 143 AudioVector& operator[](size_t index); 144 145 protected: 146 const std::vector<std::unique_ptr<AudioVector>> channels_; 147 }; 148 149 } // namespace webrtc 150 #endif // MODULES_AUDIO_CODING_NETEQ_AUDIO_MULTI_VECTOR_H_