nodes: revise means of limiting array elements

This commit is contained in:
itsmattkc
2022-09-19 08:55:37 -07:00
parent 4dbfbd63ec
commit 3abc9eb1d1
9 changed files with 239 additions and 230 deletions
+169 -53
View File
@@ -24,8 +24,10 @@
#include <QDebug>
#include <QFontMetrics>
#include "audio/audioprocessor.h"
#include "node/block/clip/clip.h"
#include "node/block/gap/gap.h"
#include "node/graph.h"
#include "node/block/transition/transition.h"
namespace olive {
@@ -95,6 +97,51 @@ QString Track::Description() const
"a Sequence.");
}
Node::ActiveElements Track::GetActiveElementsAtTime(const QString &input, const TimeRange &r) const
{
if (input == kBlockInput) {
if (IsMuted() || blocks_.empty() || r.in() >= track_length() || r.out() <= 0) {
return ActiveElements::kNoElements;
} else {
int start = GetBlockIndexAtTime(r.in());
int end = GetBlockIndexAtTime(r.out());
if (start == -1) {
start = 0;
}
if (end == -1) {
end = blocks_.size()-1;
}
ActiveElements a;
for (int i=start; i<=end; i++) {
Block *b = blocks_.at(i);
if (b->is_enabled()) {
a.add(GetArrayIndexFromCacheIndex(i));
}
}
return a;
}
} else {
return super::GetActiveElementsAtTime(input, r);
}
}
void Track::Value(const NodeValueRow &value, const NodeGlobals &globals, NodeValueTable *table) const
{
if (this->type() == Track::kVideo) {
// Just pass straight through
NodeValueArray a = value[kBlockInput].toArray();
if (!a.empty()) {
table->Push(a.begin()->second);
}
} else if (this->type() == Track::kAudio) {
// Audio
ProcessAudioTrack(table, globals.time());
}
}
TimeRange Track::InputTimeAdjustment(const QString& input, int element, const TimeRange& input_time) const
{
if (input == kBlockInput && element >= 0) {
@@ -352,58 +399,6 @@ Block *Track::NearestBlockAfter(const rational &time) const
return nullptr;
}
Block *Track::BlockAtTime(const rational &time) const
{
if (IsMuted() || time > track_length() || blocks_.isEmpty()) {
return nullptr;
}
// Use binary search to find block at time
Block* using_block = nullptr;
int low = 0;
int high = blocks_.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
Block* block = blocks_.at(mid);
if (block->in() <= time && block->out() > time) {
using_block = block;
break;
} else if (block->out() <= time) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (using_block && !using_block->is_enabled()) {
using_block = nullptr;
}
return using_block;
}
QVector<Block *> Track::BlocksAtTimeRange(const TimeRange &range) const
{
QVector<Block*> list;
if (IsMuted()) {
return list;
}
foreach (Block* block, blocks_) {
if (block
&& block->is_enabled()
&& block->out() > range.in()
&& block->in() < range.out()) {
list.append(block);
}
}
return list;
}
void Track::InvalidateCache(const TimeRange& range, const QString& from, int element, InvalidateCacheOptions options)
{
TimeRange limited;
@@ -579,6 +574,127 @@ int Track::GetCacheIndexFromArrayIndex(int index) const
return block_array_indexes_.indexOf(index);
}
int Track::GetBlockIndexAtTime(const rational &time) const
{
if (time < 0 || time >= track_length()) {
return -1;
}
// Use binary search to find block at time
int low = 0;
int high = blocks_.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
Block* block = blocks_.at(mid);
if (block->in() <= time && block->out() > time) {
return mid;
} else if (block->out() <= time) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
void Track::ProcessAudioTrack(NodeValueTable *table, const TimeRange &range) const
{
/*
// All these blocks will need to output to a buffer so we create one here
SampleBuffer block_range_buffer(audio_params, range.length());
block_range_buffer.silence();
NodeValueTable merged_table;
// Loop through active blocks retrieving their audio
foreach (Block* b, active_blocks) {
if (dynamic_cast<ClipBlock*>(b) || dynamic_cast<TransitionBlock*>(b)) {
TimeRange range_for_block(qMax(b->in(), range.in()),
qMin(b->out(), range.out()));
qint64 destination_offset = audio_params.time_to_samples(range_for_block.in() - range.in());
qint64 max_dest_sz = audio_params.time_to_samples(range_for_block.length());
// Destination buffer
NodeValueTable table = GenerateTable(b, Track::TransformRangeForBlock(b, range_for_block));
SampleBuffer samples_from_this_block = table.Take(NodeValue::kSamples).toSamples();
ClipBlock *clip_cast = dynamic_cast<ClipBlock*>(b);
if (samples_from_this_block.is_allocated()) {
// If this is a clip, we might have extra speed/reverse information
if (clip_cast) {
double speed_value = clip_cast->speed();
bool reversed = clip_cast->reverse();
if (qIsNull(speed_value)) {
// Just silence, don't think there's any other practical application of 0 speed audio
samples_from_this_block.silence();
} else if (!qFuzzyCompare(speed_value, 1.0)) {
if (clip_cast->maintain_audio_pitch()) {
AudioProcessor processor;
if (processor.Open(samples_from_this_block.audio_params(), samples_from_this_block.audio_params(), speed_value)) {
AudioProcessor::Buffer out;
// FIXME: This is not the best way to do this, the TempoProcessor works best
// when it's given a continuous stream of audio, which is challenging
// in our current "modular" audio system. This should still work reasonably
// well on export (assuming audio is all generated at once on export), but
// users may hear clicks and pops in the audio during preview due to this
// approach.
int r = processor.Convert(samples_from_this_block.to_raw_ptrs().data(), samples_from_this_block.sample_count(), nullptr);
if (r < 0) {
qCritical() << "Failed to change tempo of audio:" << r;
} else {
processor.Flush();
processor.Convert(nullptr, 0, &out);
if (!out.empty()) {
int nb_samples = out.front().size() * samples_from_this_block.audio_params().bytes_per_sample_per_channel();
if (nb_samples) {
SampleBuffer new_samples(samples_from_this_block.audio_params(), nb_samples);
for (int i=0; i<out.size(); i++) {
memcpy(new_samples.data(i), out[i].data(), out[i].size());
}
samples_from_this_block = new_samples;
}
}
}
}
} else {
// Multiply time
samples_from_this_block.speed(speed_value);
}
}
if (reversed) {
samples_from_this_block.reverse();
}
}
qint64 copy_length = qMin(max_dest_sz, qint64(samples_from_this_block.sample_count()));
// Copy samples into destination buffer
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});
}
}
}
table->Push(NodeValue::kSamples, QVariant::fromValue(block_range_buffer), this);
*/
}
void Track::BlockLengthChanged()
{
// Assumes sender is a Block