use vectors for sample buffers rather than raw ptr arrays
This commit is contained in:
@@ -200,7 +200,7 @@ QVector<AudioVisualWaveform::SamplePerChannel> AudioVisualWaveform::SumSamples(S
|
||||
|
||||
for (int i=start_index; i<end_index; i++) {
|
||||
for (int channel=0; channel<samples->audio_params().channel_count(); channel++) {
|
||||
ExpandMinMax<float>(summed_samples[channel], samples->data()[channel][i]);
|
||||
ExpandMinMax<float>(summed_samples[channel], samples->data(channel)[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+18
-68
@@ -23,16 +23,10 @@
|
||||
namespace olive {
|
||||
|
||||
SampleBuffer::SampleBuffer() :
|
||||
sample_count_per_channel_(0),
|
||||
data_(nullptr)
|
||||
sample_count_per_channel_(0)
|
||||
{
|
||||
}
|
||||
|
||||
SampleBuffer::~SampleBuffer()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
SampleBufferPtr SampleBuffer::Create()
|
||||
{
|
||||
return std::make_shared<SampleBuffer>();
|
||||
@@ -85,7 +79,7 @@ const AudioParams &SampleBuffer::audio_params() const
|
||||
|
||||
void SampleBuffer::set_audio_params(const AudioParams ¶ms)
|
||||
{
|
||||
if (data_) {
|
||||
if (is_allocated()) {
|
||||
qWarning() << "Tried to set parameters on allocated sample buffer";
|
||||
return;
|
||||
}
|
||||
@@ -100,7 +94,7 @@ const int &SampleBuffer::sample_count() const
|
||||
|
||||
void SampleBuffer::set_sample_count(const int &sample_count)
|
||||
{
|
||||
if (data_) {
|
||||
if (is_allocated()) {
|
||||
qWarning() << "Tried to set sample count on allocated sample buffer";
|
||||
return;
|
||||
}
|
||||
@@ -108,24 +102,9 @@ void SampleBuffer::set_sample_count(const int &sample_count)
|
||||
sample_count_per_channel_ = sample_count;
|
||||
}
|
||||
|
||||
float **SampleBuffer::data()
|
||||
{
|
||||
return data_;
|
||||
}
|
||||
|
||||
const float **SampleBuffer::const_data() const
|
||||
{
|
||||
return const_cast<const float**>(data_);
|
||||
}
|
||||
|
||||
float *SampleBuffer::channel_data(int channel)
|
||||
{
|
||||
return data_[channel];
|
||||
}
|
||||
|
||||
bool SampleBuffer::is_allocated() const
|
||||
{
|
||||
return data_;
|
||||
return !data_.isEmpty();
|
||||
}
|
||||
|
||||
void SampleBuffer::allocate()
|
||||
@@ -140,17 +119,20 @@ void SampleBuffer::allocate()
|
||||
return;
|
||||
}
|
||||
|
||||
if (data_) {
|
||||
if (is_allocated()) {
|
||||
qWarning() << "Tried to allocate already allocated sample buffer";
|
||||
return;
|
||||
}
|
||||
|
||||
allocate_sample_buffer(&data_, audio_params_.channel_count(), sample_count_per_channel_);
|
||||
data_.resize(audio_params_.channel_count());
|
||||
for (int i=0; i<audio_params_.channel_count(); i++) {
|
||||
data_[i].resize(sample_count_per_channel_);
|
||||
}
|
||||
}
|
||||
|
||||
void SampleBuffer::destroy()
|
||||
{
|
||||
destroy_sample_buffer(&data_, audio_params_.channel_count());
|
||||
data_.clear();
|
||||
}
|
||||
|
||||
void SampleBuffer::reverse()
|
||||
@@ -180,21 +162,21 @@ void SampleBuffer::speed(double speed)
|
||||
|
||||
sample_count_per_channel_ = qRound(static_cast<double>(sample_count_per_channel_) / speed);
|
||||
|
||||
float** input_data = data_;
|
||||
float** output_data;
|
||||
QVector< QVector<float> > output_data;
|
||||
|
||||
allocate_sample_buffer(&output_data, audio_params_.channel_count(), sample_count_per_channel_);
|
||||
output_data.resize(audio_params_.channel_count());
|
||||
for (int i=0; i<audio_params_.channel_count(); i++) {
|
||||
output_data[i].resize(sample_count_per_channel_);
|
||||
}
|
||||
|
||||
for (int i=0;i<sample_count_per_channel_;i++) {
|
||||
int input_index = qFloor(static_cast<double>(i) * speed);
|
||||
|
||||
for (int j=0;j<audio_params_.channel_count();j++) {
|
||||
output_data[j][i] = input_data[j][input_index];
|
||||
output_data[j][i] = data_[j][input_index];
|
||||
}
|
||||
}
|
||||
|
||||
destroy_sample_buffer(&input_data, audio_params_.channel_count());
|
||||
|
||||
data_ = output_data;
|
||||
}
|
||||
|
||||
@@ -245,23 +227,14 @@ void SampleBuffer::fill(const float &f, int start_sample, int end_sample)
|
||||
}
|
||||
}
|
||||
|
||||
void SampleBuffer::set(const float **data, int sample_offset, int sample_length)
|
||||
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;
|
||||
}
|
||||
|
||||
for (int i=0;i<audio_params().channel_count();i++) {
|
||||
for (int j=0;j<sample_length;j++) {
|
||||
data_[i][j + sample_offset] = data[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SampleBuffer::set(const float **data, int sample_length)
|
||||
{
|
||||
set(data, 0, sample_length);
|
||||
memcpy(&data_[channel].data()[sample_offset], data, sizeof(float) * sample_length);
|
||||
}
|
||||
|
||||
QByteArray SampleBuffer::toPackedData() const
|
||||
@@ -287,27 +260,4 @@ QByteArray SampleBuffer::toPackedData() const
|
||||
return packed_data;
|
||||
}
|
||||
|
||||
void SampleBuffer::allocate_sample_buffer(float ***data, int nb_channels, int nb_samples)
|
||||
{
|
||||
Q_ASSERT(nb_samples > 0);
|
||||
|
||||
*data = new float* [nb_channels];
|
||||
|
||||
for (int i=0;i<nb_channels;i++) {
|
||||
(*data)[i] = new float[nb_samples];
|
||||
}
|
||||
}
|
||||
|
||||
void SampleBuffer::destroy_sample_buffer(float ***data, int nb_channels)
|
||||
{
|
||||
if (*data) {
|
||||
for (int i=0;i<nb_channels;i++) {
|
||||
delete [] (*data)[i];
|
||||
}
|
||||
|
||||
delete [] *data;
|
||||
*data = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+15
-12
@@ -43,8 +43,6 @@ class SampleBuffer
|
||||
public:
|
||||
SampleBuffer();
|
||||
|
||||
virtual ~SampleBuffer();
|
||||
|
||||
static SampleBufferPtr Create();
|
||||
static SampleBufferPtr CreateAllocated(const AudioParams& audio_params, const rational& length);
|
||||
static SampleBufferPtr CreateAllocated(const AudioParams& audio_params, int samples_per_channel);
|
||||
@@ -58,9 +56,15 @@ public:
|
||||
const int &sample_count() const;
|
||||
void set_sample_count(const int &sample_count);
|
||||
|
||||
float** data();
|
||||
const float** const_data() const;
|
||||
float* channel_data(int channel);
|
||||
float* data(int channel)
|
||||
{
|
||||
return data_[channel].data();
|
||||
}
|
||||
|
||||
const float* data(int channel) const
|
||||
{
|
||||
return data_.at(channel).constData();
|
||||
}
|
||||
|
||||
bool is_allocated() const;
|
||||
void allocate();
|
||||
@@ -76,21 +80,20 @@ public:
|
||||
void fill(const float& f);
|
||||
void fill(const float& f, int start_sample, int end_sample);
|
||||
|
||||
void set(const float** data, int sample_offset, int sample_length);
|
||||
void set(const float** data, int sample_length);
|
||||
void set(int channel, const float* data, int sample_offset, int sample_length);
|
||||
void set(int channel, const float* data, int sample_length)
|
||||
{
|
||||
set(channel, data, 0, sample_length);
|
||||
}
|
||||
|
||||
QByteArray toPackedData() const;
|
||||
|
||||
private:
|
||||
static void allocate_sample_buffer(float*** data, int nb_channels, int nb_samples);
|
||||
|
||||
static void destroy_sample_buffer(float*** data, int nb_channels);
|
||||
|
||||
AudioParams audio_params_;
|
||||
|
||||
int sample_count_per_channel_;
|
||||
|
||||
float** data_;
|
||||
QVector< QVector<float> > data_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -103,13 +103,13 @@ void PanNode::ProcessSamples(NodeValueDatabase &values, const SampleBufferPtr in
|
||||
float pan_val = values[kPanningInput].Get(NodeValue::kFloat).toFloat();
|
||||
|
||||
for (int i=0;i<input->audio_params().channel_count();i++) {
|
||||
output->data()[i][index] = input->data()[i][index];
|
||||
output->data(i)[index] = input->data(i)[index];
|
||||
}
|
||||
|
||||
if (pan_val > 0) {
|
||||
output->data()[0][index] *= (1.0F - pan_val);
|
||||
output->data(0)[index] *= (1.0F - pan_val);
|
||||
} else if (pan_val < 0) {
|
||||
output->data()[1][index] *= (1.0F - qAbs(pan_val));
|
||||
output->data(1)[index] *= (1.0F - qAbs(pan_val));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,11 +73,11 @@ void CrossDissolveTransition::SampleJobEvent(SampleBufferPtr from_samples, Sampl
|
||||
double progress = GetTotalProgress(this_sample_time);
|
||||
|
||||
for (int j=0; j<out_samples->audio_params().channel_count(); j++) {
|
||||
out_samples->data()[j][i] = 0;
|
||||
out_samples->data(j)[i] = 0;
|
||||
|
||||
if (from_samples) {
|
||||
if (i < from_samples->sample_count()) {
|
||||
out_samples->data()[j][i] += from_samples->data()[j][i] * TransformCurve(1.0 - progress);
|
||||
out_samples->data(j)[i] += from_samples->data(j)[i] * TransformCurve(1.0 - progress);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ void CrossDissolveTransition::SampleJobEvent(SampleBufferPtr from_samples, Sampl
|
||||
int in_index = i - (out_samples->sample_count() - to_samples->sample_count());
|
||||
|
||||
if (in_index >= 0) {
|
||||
out_samples->data()[j][i] += to_samples->data()[j][in_index] * TransformCurve(progress);
|
||||
out_samples->data(j)[i] += to_samples->data(j)[in_index] * TransformCurve(progress);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,7 +260,7 @@ NodeValueTable MathNodeBase::ValueInternal(NodeValueDatabase &value, Operation o
|
||||
for (int i=0;i<mixed_samples->audio_params().channel_count();i++) {
|
||||
// Mix samples that are in both buffers
|
||||
for (int j=0;j<min_samples;j++) {
|
||||
mixed_samples->data()[i][j] = PerformAll<float, float>(operation, samples_a->data()[i][j], samples_b->data()[i][j]);
|
||||
mixed_samples->data(i)[j] = PerformAll<float, float>(operation, samples_a->data(i)[j], samples_b->data(i)[j]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,8 +271,8 @@ NodeValueTable MathNodeBase::ValueInternal(NodeValueDatabase &value, Operation o
|
||||
SampleBufferPtr larger_buffer = (max_samples == samples_a->sample_count()) ? samples_a : samples_b;
|
||||
|
||||
for (int i=0;i<mixed_samples->audio_params().channel_count();i++) {
|
||||
memcpy(&mixed_samples->data()[i][min_samples],
|
||||
&larger_buffer->data()[i][min_samples],
|
||||
memcpy(&mixed_samples->data(i)[min_samples],
|
||||
&larger_buffer->data(i)[min_samples],
|
||||
remainder * sizeof(float));
|
||||
}
|
||||
}
|
||||
@@ -354,7 +354,7 @@ NodeValueTable MathNodeBase::ValueInternal(NodeValueDatabase &value, Operation o
|
||||
if (!NumberIsNoOp(operation, number)) {
|
||||
for (int i=0;i<job.samples()->audio_params().channel_count();i++) {
|
||||
for (int j=0;j<job.samples()->sample_count();j++) {
|
||||
job.samples()->data()[i][j] = PerformAll(operation, job.samples()->data()[i][j], number);
|
||||
job.samples()->data(i)[j] = PerformAll(operation, job.samples()->data(i)[j], number);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -391,7 +391,7 @@ void MathNodeBase::ProcessSamplesInternal(NodeValueDatabase &values, MathNodeBas
|
||||
float number_flt = RetrieveNumber(number_val);
|
||||
|
||||
for (int i=0;i<output->audio_params().channel_count();i++) {
|
||||
output->data()[i][index] = PerformAll<float, float>(operation, input->data()[i][index], number_flt);
|
||||
output->data(i)[index] = PerformAll<float, float>(operation, input->data(i)[index], number_flt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -236,7 +236,9 @@ NodeValueTable RenderProcessor::GenerateBlockTable(const Track *track, const Tim
|
||||
int copy_length = qMin(max_dest_sz, samples_from_this_block->sample_count());
|
||||
|
||||
// Copy samples into destination buffer
|
||||
block_range_buffer->set(samples_from_this_block->const_data(), destination_offset, copy_length);
|
||||
for (int i=0; i<samples_from_this_block->audio_params().channel_count(); i++) {
|
||||
block_range_buffer->set(i, samples_from_this_block->data(i), destination_offset, copy_length);
|
||||
}
|
||||
|
||||
NodeValueTable::Merge({merged_table, table});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user