From 8413886c173d07344390c8dc1c1e3ed21451128c Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Tue, 31 Mar 2020 13:56:02 +1100 Subject: [PATCH] audiobackend: fixed bug that sometimes extended the While we implement an audio cache "truncate" function when the sequence length changes, the render functions also have the ability to extend it. This is fine, but if truncation happened while workers were caching, it could truncate and then extend beyond the length of the sequence. This commit changes the worker extend function to only extend as much as the length, therefore not undoing the truncation. --- app/render/backend/audio/audiobackend.cpp | 54 +++++++++++++---------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/app/render/backend/audio/audiobackend.cpp b/app/render/backend/audio/audiobackend.cpp index cab440175..3c9bd5003 100644 --- a/app/render/backend/audio/audiobackend.cpp +++ b/app/render/backend/audio/audiobackend.cpp @@ -66,34 +66,40 @@ void AudioBackend::ThreadCompletedCache(NodeDependency dep, NodeValueTable data, int offset = params().time_to_bytes(dep.in()); int length = params().time_to_bytes(dep.range().length()); - int out_point = offset + length; + int out_point = qMin(offset + length, params().time_to_bytes(GetSequenceLength())); - QFile f(CachePathName()); - if (f.open(QFile::ReadWrite)) { - - if (f.size() < out_point && !f.resize(out_point)) { - qCritical() << "Failed to resize file" << CachePathName(); + if (offset < out_point) { + if (offset + length > out_point) { + length = out_point - offset; } - if (!f.seek(offset)) { - qCritical() << "Failed to seek file" << CachePathName(); + QFile f(CachePathName()); + if (f.open(QFile::ReadWrite)) { + + if (f.size() < out_point && !f.resize(out_point)) { + qCritical() << "Failed to resize file" << CachePathName(); + } + + if (!f.seek(offset)) { + qCritical() << "Failed to seek file" << CachePathName(); + } + + // Replace data with this data + int copy_length = qMin(length, cached_samples.size()); + + f.write(cached_samples.data(), copy_length); + + if (copy_length < length) { + + // Fill in remainder with silence + QByteArray empty_space(length - copy_length, 0); + f.write(empty_space); + } + + f.close(); + } else { + qWarning() << "Failed to write to cached PCM file"; } - - // Replace data with this data - int copy_length = qMin(length, cached_samples.size()); - - f.write(cached_samples.data(), copy_length); - - if (copy_length < length) { - - // Fill in remainder with silence - QByteArray empty_space(length - copy_length, 0); - f.write(empty_space); - } - - f.close(); - } else { - qWarning() << "Failed to write to cached PCM file"; } }