1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_coding/neteq/audio_multi_vector.h"
#include <algorithm>
#include "rtc_base/checks.h"
namespace webrtc {
AudioMultiVector::AudioMultiVector(size_t N) {
RTC_DCHECK_GT(N, 0);
if (N < 1)
N = 1;
for (size_t n = 0; n < N; ++n) {
channels_.push_back(new AudioVector);
}
num_channels_ = N;
}
AudioMultiVector::AudioMultiVector(size_t N, size_t initial_size) {
RTC_DCHECK_GT(N, 0);
if (N < 1)
N = 1;
for (size_t n = 0; n < N; ++n) {
channels_.push_back(new AudioVector(initial_size));
}
num_channels_ = N;
}
AudioMultiVector::~AudioMultiVector() {
std::vector<AudioVector*>::iterator it = channels_.begin();
while (it != channels_.end()) {
delete (*it);
++it;
}
}
void AudioMultiVector::Clear() {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->Clear();
}
}
void AudioMultiVector::Zeros(size_t length) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->Clear();
channels_[i]->Extend(length);
}
}
void AudioMultiVector::CopyTo(AudioMultiVector* copy_to) const {
if (copy_to) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->CopyTo(&(*copy_to)[i]);
}
}
}
void AudioMultiVector::PushBackInterleaved(
rtc::ArrayView<const int16_t> append_this) {
RTC_DCHECK_EQ(append_this.size() % num_channels_, 0);
if (append_this.empty()) {
return;
}
if (num_channels_ == 1) {
// Special case to avoid extra allocation and data shuffling.
channels_[0]->PushBack(append_this.data(), append_this.size());
return;
}
size_t length_per_channel = append_this.size() / num_channels_;
int16_t* temp_array = new int16_t[length_per_channel]; // Temporary storage.
for (size_t channel = 0; channel < num_channels_; ++channel) {
// Copy elements to `temp_array`.
for (size_t i = 0; i < length_per_channel; ++i) {
temp_array[i] = append_this[channel + i * num_channels_];
}
channels_[channel]->PushBack(temp_array, length_per_channel);
}
delete[] temp_array;
}
void AudioMultiVector::PushBack(const AudioMultiVector& append_this) {
RTC_DCHECK_EQ(num_channels_, append_this.num_channels_);
if (num_channels_ == append_this.num_channels_) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->PushBack(append_this[i]);
}
}
}
void AudioMultiVector::PushBackFromIndex(const AudioMultiVector& append_this,
size_t index) {
RTC_DCHECK_LT(index, append_this.Size());
index = std::min(index, append_this.Size() - 1);
size_t length = append_this.Size() - index;
RTC_DCHECK_EQ(num_channels_, append_this.num_channels_);
if (num_channels_ == append_this.num_channels_) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->PushBack(append_this[i], length, index);
}
}
}
void AudioMultiVector::PopFront(size_t length) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->PopFront(length);
}
}
void AudioMultiVector::PopBack(size_t length) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->PopBack(length);
}
}
size_t AudioMultiVector::ReadInterleaved(size_t length,
int16_t* destination) const {
return ReadInterleavedFromIndex(0, length, destination);
}
size_t AudioMultiVector::ReadInterleavedFromIndex(size_t start_index,
size_t length,
int16_t* destination) const {
RTC_DCHECK(destination);
size_t index = 0; // Number of elements written to `destination` so far.
RTC_DCHECK_LE(start_index, Size());
start_index = std::min(start_index, Size());
if (length + start_index > Size()) {
length = Size() - start_index;
}
if (num_channels_ == 1) {
// Special case to avoid the nested for loop below.
(*this)[0].CopyTo(length, start_index, destination);
return length;
}
for (size_t i = 0; i < length; ++i) {
for (size_t channel = 0; channel < num_channels_; ++channel) {
destination[index] = (*this)[channel][i + start_index];
++index;
}
}
return index;
}
size_t AudioMultiVector::ReadInterleavedFromEnd(size_t length,
int16_t* destination) const {
length = std::min(length, Size()); // Cannot read more than Size() elements.
return ReadInterleavedFromIndex(Size() - length, length, destination);
}
void AudioMultiVector::OverwriteAt(const AudioMultiVector& insert_this,
size_t length,
size_t position) {
RTC_DCHECK_EQ(num_channels_, insert_this.num_channels_);
// Cap `length` at the length of `insert_this`.
RTC_DCHECK_LE(length, insert_this.Size());
length = std::min(length, insert_this.Size());
if (num_channels_ == insert_this.num_channels_) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->OverwriteAt(insert_this[i], length, position);
}
}
}
void AudioMultiVector::CrossFade(const AudioMultiVector& append_this,
size_t fade_length) {
RTC_DCHECK_EQ(num_channels_, append_this.num_channels_);
if (num_channels_ == append_this.num_channels_) {
for (size_t i = 0; i < num_channels_; ++i) {
channels_[i]->CrossFade(append_this[i], fade_length);
}
}
}
size_t AudioMultiVector::Channels() const {
return num_channels_;
}
size_t AudioMultiVector::Size() const {
RTC_DCHECK(channels_[0]);
return channels_[0]->Size();
}
void AudioMultiVector::AssertSize(size_t required_size) {
if (Size() < required_size) {
size_t extend_length = required_size - Size();
for (size_t channel = 0; channel < num_channels_; ++channel) {
channels_[channel]->Extend(extend_length);
}
}
}
bool AudioMultiVector::Empty() const {
RTC_DCHECK(channels_[0]);
return channels_[0]->Empty();
}
void AudioMultiVector::CopyChannel(size_t from_channel, size_t to_channel) {
RTC_DCHECK_LT(from_channel, num_channels_);
RTC_DCHECK_LT(to_channel, num_channels_);
channels_[from_channel]->CopyTo(channels_[to_channel]);
}
const AudioVector& AudioMultiVector::operator[](size_t index) const {
return *(channels_[index]);
}
AudioVector& AudioMultiVector::operator[](size_t index) {
return *(channels_[index]);
}
} // namespace webrtc
|