9 #ifndef SAL_AUDIOBUFFER_H
10 #define SAL_AUDIOBUFFER_H
14 #include "pointwiseop.h"
16 #include "digitalfilter.h"
30 num_channels_(num_channels), num_samples_(num_samples),
31 owns_data_(true), temporary_vector_(std::vector<
Sample>(num_samples, 0.0)) {
32 ASSERT(num_channels >= 0 && num_samples >= 0);
50 temporary_vector_(std::vector<Sample>(
num_samples, 0.0)) {
51 data_ = data_referenced;
61 ASSERT(sample_id >= 0 && sample_id <
num_samples());
63 return data_[channel_id][sample_id];
69 const Sample sample) noexcept {
71 ASSERT(sample_id >= 0 && sample_id <
num_samples());
73 data_[channel_id][sample_id] = sample;
87 ASSERT(from_sample_id >= 0);
91 for (
Int sample_id=from_sample_id;
94 data_[channel_id][sample_id] = samples[sample_id-from_sample_id];
99 ASSERT(num_samples_ == other.num_samples_);
100 ASSERT(num_channels_ == other.num_channels_);
101 for (
int chan_id=0; chan_id<num_channels_; ++chan_id) {
102 for (
int sample_id=0; sample_id<num_samples_; ++sample_id) {
103 data_[chan_id][sample_id] = other.data_[chan_id][sample_id];
116 const Int from_sample_id,
118 const Sample* samples) noexcept {
120 ASSERT(from_sample_id >= 0);
122 ASSERT((from_sample_id+
num_samples) <= num_samples_);
126 &(data_[channel_id][from_sample_id]));
132 const Int from_sample_id,
135 const Sample constant) noexcept {
137 ASSERT(from_sample_id >= 0);
139 ASSERT((from_sample_id+
num_samples) <= num_samples_);
140 mcl::MultiplyAdd(samples, constant, &(data_[channel_id][from_sample_id]),
141 num_samples, &(data_[channel_id][from_sample_id]));
145 const Int from_sample_id,
148 mcl::DigitalFilter& filter) noexcept {
150 ASSERT(from_sample_id >= 0);
152 ASSERT((from_sample_id+
num_samples) <= num_samples_);
153 filter.Filter(samples,
num_samples, temporary_vector_.data());
154 mcl::Add(temporary_vector_.data(),
156 &(data_[channel_id][from_sample_id]));
162 return data_[channel_id];
167 return data_[channel_id];
182 buffer.GetReadPointer(chan_id),
190 const Int frame_length,
193 mcl::Int index = frame_id*frame_length + n;
194 if (index < (
mcl::Int) signal.size()) {
204 for (
int chan_id=0; chan_id<num_channels_; ++chan_id) {
205 for (
int sample_id=0; sample_id<num_samples_; ++sample_id) {
206 std::cout<<data_[chan_id][sample_id]<<
" ";
208 std::cout<<std::endl;
216 data_[chan_id][sample_id] = 0.0;
229 num_channels_(other.num_channels_), num_samples_(other.num_samples_),
230 owns_data_(other.owns_data_),
246 if (
this != &other) {
247 if (owns_data_ && other.data_ == data_) {
return *
this; }
249 if (owns_data_) { DeallocateMemory(); }
251 num_channels_ = other.num_channels_;
252 num_samples_ = other.num_samples_;
253 owns_data_ = other.owns_data_;
254 temporary_vector_ = std::vector<Sample>(other.
num_samples(), 0.0);
267 if (owns_data_) { DeallocateMemory(); }
277 std::vector<Sample> temporary_vector_;
279 void AllocateMemory() {
280 data_ =
new Sample*[num_channels_];
281 for (
Int chan_id=0; chan_id<num_channels_; ++chan_id) {
282 data_[chan_id] =
new Sample[num_samples_]();
286 void DeallocateMemory() {
287 for (
int chan_id=0; chan_id<num_channels_; ++chan_id) {
288 delete[] data_[chan_id];
303 data_referenced_(data_referenced) {}
312 Buffer(&(referenced_buffer.GetWritePointers()[channel_id]), 1,
313 referenced_buffer.num_samples()) {}
320 const Sample constant) noexcept {
326 const Sample sample_value) noexcept {
333 const Sample* samples) noexcept {
339 const Int frame_length,
366 const Sample* samples) noexcept {
385 const Sample sample_value) noexcept {
390 const Sample sample_value) noexcept {
419 const Int from_sample_id,
420 const Int num_samples_to_add) noexcept {
422 num_samples_to_add, samples);
428 mcl::DigitalFilter& filter) noexcept {
436 mcl::DigitalFilter& filter) noexcept {
442 const Int from_sample_id,
443 const Int num_samples_to_add) noexcept {
445 num_samples_to_add, samples);
465 ordering_(ordering) {}
468 const Sample& sample_value) noexcept {
470 sample_id, sample_value);
476 const Int from_sample_id,
480 from_sample_id, num_samples, samples);
486 const Int from_sample_id,
489 const Sample constant) noexcept {
496 const Int sample_id)
const noexcept {
502 ASSERT(degree <= std::abs(order));
505 case HoaOrdering::Acn: {
506 return order*order + order + degree;
512 }
else if (order == 1) {
515 }
else if (degree == -1) {
517 }
else if (degree == 0) {
521 const Int center_index = order*order;
522 Int channel_id = center_index;
524 while (this_degree != degree) {
525 this_degree = (this_degree > 0) ? -this_degree : abs(this_degree)+1;
543 ASSERT(max_order > 0);
544 return (max_order+1)*(max_order+1);