From 5f5d6e82abd8c7b39837677314d63eea55d85ce1 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Sat, 24 Oct 2020 14:18:59 +1100 Subject: [PATCH] audioplaybackcache: fixed bug in one of the shift operations --- app/render/audioplaybackcache.cpp | 30 +++++++++++++++--------------- app/render/audioplaybackcache.h | 9 +++++++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/app/render/audioplaybackcache.cpp b/app/render/audioplaybackcache.cpp index 8f114f040..eda8ae905 100644 --- a/app/render/audioplaybackcache.cpp +++ b/app/render/audioplaybackcache.cpp @@ -66,10 +66,11 @@ void AudioPlaybackCache::WritePCM(const TimeRange &range, SampleBufferPtr sample } // Determine if we have enough segments to pull this off - qint64 range_out_in_bytes = params_.time_to_bytes(range.out()); - while (playlist_.GetLength() < range_out_in_bytes) { - qint64 seg_sz = qMin(kDefaultSegmentSize, range_out_in_bytes - playlist_.GetLength()); + qint64 length_diff = params_.time_to_bytes(range.out()) - playlist_.GetLength(); + while (length_diff > 0) { + qint64 seg_sz = qMin(kDefaultSegmentSize, length_diff); playlist_.push_back(CreateSegment(seg_sz, playlist_.GetLength())); + length_diff -= seg_sz; } QByteArray a; @@ -179,29 +180,28 @@ void AudioPlaybackCache::ShiftEvent(const rational &from_in_time, const rational insert_index = from_index; } else { insert_index = from_index + 1; - } - if (from < from_end) { - // Split from segment into two - Segment second = CloneSegment(playlist_.at(from_index)); + if (from < from_end) { + // Split from segment into two + Segment second = CloneSegment(playlist_.at(from_index)); - TrimSegmentOut(&playlist_[from_index], from - from_start); - TrimSegmentIn(&second, from_end - from); + TrimSegmentOut(&playlist_[from_index], from - from_start); + TrimSegmentIn(&second, from_end - from); - playlist_.insert(insert_index, second); + playlist_.insert(insert_index, second); + } } // Insert silent segments - qint64 time_to_insert = params_.time_to_bytes(to - from); - qint64 inserted_time = 0; + qint64 time_to_insert = to - from; - while (inserted_time < time_to_insert) { - qint64 new_seg_sz = qMin(kDefaultSegmentSize, time_to_insert - inserted_time); + while (time_to_insert) { + qint64 new_seg_sz = qMin(kDefaultSegmentSize, time_to_insert); // Set offset to 0 for now and fill it in later playlist_.insert(insert_index, CreateSegment(new_seg_sz, 0)); - inserted_time += new_seg_sz; + time_to_insert -= new_seg_sz; } UpdateOffsetsFrom(insert_index); diff --git a/app/render/audioplaybackcache.h b/app/render/audioplaybackcache.h index 793c84ac8..8b4419b9d 100644 --- a/app/render/audioplaybackcache.h +++ b/app/render/audioplaybackcache.h @@ -170,6 +170,15 @@ public: }; + /** + * @brief Create a QIODevice that can play whatever's in the cache currently + * + * This device will act very much like a QFile, transparently linking together various segments + * into what will appear to be a single contiguous file. + * + * The caller becomes responsible for ownership of the device, though the parent can be set + * automatically as an optional parameter to this function. + */ PlaybackDevice* CreatePlaybackDevice(QObject *parent = nullptr) const; signals: