/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 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"
OLIVE_NAMESPACE_ENTER
SampleBuffer::SampleBuffer() :
sample_count_per_channel_(0),
data_(nullptr)
{
}
SampleBuffer::~SampleBuffer()
{
destroy();
}
SampleBufferPtr SampleBuffer::Create()
{
return std::make_shared();
}
SampleBufferPtr SampleBuffer::CreateAllocated(const AudioParams &audio_params, int samples_per_channel)
{
SampleBufferPtr buffer = Create();
buffer->set_audio_params(audio_params);
buffer->set_sample_count(samples_per_channel);
buffer->allocate();
return buffer;
}
SampleBufferPtr SampleBuffer::CreateFromPackedData(const AudioParams &audio_params, const QByteArray &bytes)
{
if (!audio_params.is_valid()) {
qWarning() << "Tried to create from packed data with invalid parameters";
return nullptr;
}
int samples_per_channel = audio_params.bytes_to_samples(bytes.size());
SampleBufferPtr buffer = CreateAllocated(audio_params, samples_per_channel);
int total_samples = samples_per_channel * audio_params.channel_count();
const float* packed_data = reinterpret_cast(bytes.constData());
for (int i=0;idata_[channel][index] = packed_data[i];
}
return buffer;
}
const AudioParams &SampleBuffer::audio_params() const
{
return audio_params_;
}
void SampleBuffer::set_audio_params(const AudioParams ¶ms)
{
if (data_) {
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 (data_) {
qWarning() << "Tried to set sample count on allocated sample buffer";
return;
}
sample_count_per_channel_ = sample_count;
}
float **SampleBuffer::data()
{
return data_;
}
const float **SampleBuffer::const_data() const
{
return const_cast(data_);
}
float *SampleBuffer::channel_data(int channel)
{
return data_[channel];
}
bool SampleBuffer::is_allocated() const
{
return data_;
}
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 (data_) {
qWarning() << "Tried to allocate already allocated sample buffer";
return;
}
allocate_sample_buffer(&data_, audio_params_.channel_count(), sample_count_per_channel_);
}
void SampleBuffer::destroy()
{
destroy_sample_buffer(&data_, audio_params_.channel_count());
}
void SampleBuffer::reverse()
{
if (!is_allocated()) {
qWarning() << "Tried to reverse an unallocated sample buffer";
return;
}
int half_nb_sample = sample_count_per_channel_ / 2;
for (int i=0;i(sample_count_per_channel_) / speed);
float** input_data = data_;
float** output_data;
allocate_sample_buffer(&output_data, audio_params_.channel_count(), sample_count_per_channel_);
for (int i=0;i(i) * speed);
for (int j=0;j(packed_data.data());
int output_index = 0;
for (int j=0;j 0);
*data = new float* [nb_channels];
for (int i=0;i