| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef COMMON_AUDIO_CHANNEL_BUFFER_H_ |
| 12 |
#define COMMON_AUDIO_CHANNEL_BUFFER_H_ |
| 13 |
|
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <memory> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include "api/array_view.h" |
| 20 |
#include "common_audio/include/audio_util.h" |
| 21 |
#include "rtc_base/checks.h" |
| 22 |
#include "rtc_base/gtest_prod_util.h" |
| 23 |
|
| 24 |
namespace webrtc { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
template <typename T> |
| 43 |
class ChannelBuffer { |
| 44 |
public: |
| 45 |
ChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1) |
| 46 |
: data_(new T[num_frames * num_channels]()), |
| 47 |
channels_(new T*[num_channels * num_bands]), |
| 48 |
bands_(new T*[num_channels * num_bands]), |
| 49 |
num_frames_(num_frames), |
| 50 |
num_frames_per_band_(num_frames / num_bands), |
| 51 |
num_allocated_channels_(num_channels), |
| 52 |
num_channels_(num_channels), |
| 53 |
num_bands_(num_bands), |
| 54 |
bands_view_(num_allocated_channels_, |
| 55 |
std::vector<rtc::ArrayView<T>>(num_bands_)), |
| 56 |
channels_view_( |
| 57 |
num_bands_, |
| 58 |
std::vector<rtc::ArrayView<T>>(num_allocated_channels_)) { |
| 59 |
|
| 60 |
auto* bands_view = |
| 61 |
const_cast<std::vector<std::vector<rtc::ArrayView<T>>>*>(&bands_view_); |
| 62 |
auto* channels_view = |
| 63 |
const_cast<std::vector<std::vector<rtc::ArrayView<T>>>*>( |
| 64 |
&channels_view_); |
| 65 |
|
| 66 |
for (size_t ch = 0; ch < num_allocated_channels_; ++ch) { |
| 67 |
for (size_t band = 0; band < num_bands_; ++band) { |
| 68 |
(*channels_view)[band][ch] = rtc::ArrayView<T>( |
| 69 |
&data_[ch * num_frames_ + band * num_frames_per_band_], |
| 70 |
num_frames_per_band_); |
| 71 |
(*bands_view)[ch][band] = channels_view_[band][ch]; |
| 72 |
channels_[band * num_allocated_channels_ + ch] = |
| 73 |
channels_view_[band][ch].data(); |
| 74 |
bands_[ch * num_bands_ + band] = |
| 75 |
channels_[band * num_allocated_channels_ + ch]; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
const T* const* channels(size_t band = 0) const { |
| 94 |
RTC_DCHECK_LT(band, num_bands_); |
| 95 |
return &channels_[band * num_allocated_channels_]; |
| 96 |
} |
| 97 |
T* const* channels(size_t band = 0) { |
| 98 |
const ChannelBuffer<T>* t = this; |
| 99 |
return const_cast<T* const*>(t->channels(band)); |
| 100 |
} |
| 101 |
rtc::ArrayView<const rtc::ArrayView<T>> channels_view(size_t band = 0) { |
| 102 |
return channels_view_[band]; |
| 103 |
} |
| 104 |
rtc::ArrayView<const rtc::ArrayView<T>> channels_view(size_t band = 0) const { |
| 105 |
return channels_view_[band]; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
const T* const* bands(size_t channel) const { |
| 116 |
RTC_DCHECK_LT(channel, num_channels_); |
| 117 |
RTC_DCHECK_GE(channel, 0); |
| 118 |
return &bands_[channel * num_bands_]; |
| 119 |
} |
| 120 |
T* const* bands(size_t channel) { |
| 121 |
const ChannelBuffer<T>* t = this; |
| 122 |
return const_cast<T* const*>(t->bands(channel)); |
| 123 |
} |
| 124 |
|
| 125 |
rtc::ArrayView<const rtc::ArrayView<T>> bands_view(size_t channel) { |
| 126 |
return bands_view_[channel]; |
| 127 |
} |
| 128 |
rtc::ArrayView<const rtc::ArrayView<T>> bands_view(size_t channel) const { |
| 129 |
return bands_view_[channel]; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
const T* const* Slice(T** slice, size_t start_frame) const { |
| 135 |
RTC_DCHECK_LT(start_frame, num_frames_); |
| 136 |
for (size_t i = 0; i < num_channels_; ++i) |
| 137 |
slice[i] = &channels_[i][start_frame]; |
| 138 |
return slice; |
| 139 |
} |
| 140 |
T** Slice(T** slice, size_t start_frame) { |
| 141 |
const ChannelBuffer<T>* t = this; |
| 142 |
return const_cast<T**>(t->Slice(slice, start_frame)); |
| 143 |
} |
| 144 |
|
| 145 |
size_t num_frames() const { return num_frames_; } |
| 146 |
size_t num_frames_per_band() const { return num_frames_per_band_; } |
| 147 |
size_t num_channels() const { return num_channels_; } |
| 148 |
size_t num_bands() const { return num_bands_; } |
| 149 |
size_t size() const { return num_frames_ * num_allocated_channels_; } |
| 150 |
|
| 151 |
void set_num_channels(size_t num_channels) { |
| 152 |
RTC_DCHECK_LE(num_channels, num_allocated_channels_); |
| 153 |
num_channels_ = num_channels; |
| 154 |
} |
| 155 |
|
| 156 |
void SetDataForTesting(const T* data, size_t size) { |
| 157 |
RTC_CHECK_EQ(size, this->size()); |
| 158 |
memcpy(data_.get(), data, size * sizeof(*data)); |
| 159 |
} |
| 160 |
|
| 161 |
private: |
| 162 |
std::unique_ptr<T[]> data_; |
| 163 |
std::unique_ptr<T*[]> channels_; |
| 164 |
std::unique_ptr<T*[]> bands_; |
| 165 |
const size_t num_frames_; |
| 166 |
const size_t num_frames_per_band_; |
| 167 |
|
| 168 |
const size_t num_allocated_channels_; |
| 169 |
|
| 170 |
size_t num_channels_; |
| 171 |
const size_t num_bands_; |
| 172 |
const std::vector<std::vector<rtc::ArrayView<T>>> bands_view_; |
| 173 |
const std::vector<std::vector<rtc::ArrayView<T>>> channels_view_; |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
class IFChannelBuffer { |
| 183 |
public: |
| 184 |
IFChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1); |
| 185 |
~IFChannelBuffer(); |
| 186 |
|
| 187 |
ChannelBuffer<int16_t>* ibuf(); |
| 188 |
ChannelBuffer<float>* fbuf(); |
| 189 |
const ChannelBuffer<int16_t>* ibuf_const() const; |
| 190 |
const ChannelBuffer<float>* fbuf_const() const; |
| 191 |
|
| 192 |
size_t num_frames() const { return ibuf_.num_frames(); } |
| 193 |
size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); } |
| 194 |
size_t num_channels() const { |
| 195 |
return ivalid_ ? ibuf_.num_channels() : fbuf_.num_channels(); |
| 196 |
} |
| 197 |
void set_num_channels(size_t num_channels) { |
| 198 |
ibuf_.set_num_channels(num_channels); |
| 199 |
fbuf_.set_num_channels(num_channels); |
| 200 |
} |
| 201 |
size_t num_bands() const { return ibuf_.num_bands(); } |
| 202 |
|
| 203 |
private: |
| 204 |
void RefreshF() const; |
| 205 |
void RefreshI() const; |
| 206 |
|
| 207 |
mutable bool ivalid_; |
| 208 |
mutable ChannelBuffer<int16_t> ibuf_; |
| 209 |
mutable bool fvalid_; |
| 210 |
mutable ChannelBuffer<float> fbuf_; |
| 211 |
}; |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
#endif |