/***
Olive - Non-Linear Video Editor
Copyright (C) 2021 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
***/
#include "samplebuffer.h"
#include "common/cpuoptimize.h"
namespace olive {
SampleBuffer::SampleBuffer() :
sample_count_per_channel_(0)
{
}
SampleBuffer::SampleBuffer(const AudioParams &audio_params, const rational &length) :
audio_params_(audio_params)
{
sample_count_per_channel_ = audio_params_.time_to_samples(length);
allocate();
}
SampleBuffer::SampleBuffer(const AudioParams &audio_params, int samples_per_channel) :
audio_params_(audio_params),
sample_count_per_channel_(samples_per_channel)
{
allocate();
}
const AudioParams &SampleBuffer::audio_params() const
{
return audio_params_;
}
void SampleBuffer::set_audio_params(const AudioParams ¶ms)
{
if (is_allocated()) {
qWarning() << "Tried to set parameters on allocated sample buffer";
return;
}
audio_params_ = params;
}
const int &SampleBuffer::sample_count() const
{
return sample_count_per_channel_;
}
void SampleBuffer::set_sample_count(const int &sample_count)
{
if (is_allocated()) {
qWarning() << "Tried to set sample count on allocated sample buffer";
return;
}
sample_count_per_channel_ = sample_count;
}
bool SampleBuffer::is_allocated() const
{
return !data_.isEmpty();
}
void SampleBuffer::allocate()
{
if (!audio_params_.is_valid()) {
qWarning() << "Tried to allocate sample buffer with invalid audio parameters";
return;
}
if (!sample_count_per_channel_) {
qWarning() << "Tried to allocate sample buffer with zero sample count";
return;
}
if (is_allocated()) {
qWarning() << "Tried to allocate already allocated sample buffer";
return;
}
data_.resize(audio_params_.channel_count());
for (int i=0; i(sample_count_per_channel_) / speed);
QVector< QVector > output_data;
output_data.resize(audio_params_.channel_count());
for (int i=0; i(i) * speed);
for (int j=0;j(data_[i].data()) + start_byte, 0, end_byte - start_byte);
}
}
void SampleBuffer::set(int channel, const float *data, int sample_offset, int sample_length)
{
if (!is_allocated()) {
qWarning() << "Tried to fill an unallocated sample buffer";
return;
}
memcpy(&data_[channel].data()[sample_offset], data, sizeof(float) * sample_length);
}
}