@@ -27,6 +27,29 @@
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
/**
|
||||
* @brief A fully integrated system of storing and playing cached audio
|
||||
*
|
||||
* All audio in Olive is processed and rendered in advance. This makes playback extremely smooth
|
||||
* and reliable, but provides some challenges as far as storing and manipulating this audio while
|
||||
* minimizing the amount of re-renders necessary.
|
||||
*
|
||||
* Olive's PlaybackCaches support "shifting"; moving cached data to a different spot on the
|
||||
* timeline without requiring a costly re-render. While video is naturally stored on disk as
|
||||
* separate frames that are easy to swap out, audio works a little differently. It would be
|
||||
* extremely inefficient to store each sample as a separate file on the disk, but storing in
|
||||
* one single contiguous file would be detrimental to shifting, particularly for longer timelines
|
||||
* since the data will actually have to be shifted on disk.
|
||||
*
|
||||
* As such, AudioPlaybackCache compromises by storing audio in several "segments". This makes
|
||||
* operations like shifting much easier since segments can simply be removed from the playlist
|
||||
* rather than having to shift or re-render potentially hours of audio in every operation.
|
||||
*
|
||||
* Naturally, storing in segments means you can't simply play the PCM data like a file, so
|
||||
* AudioPlaybackCache also provides a playback device (accessible from CreatePlaybackDevice()) that
|
||||
* acts identically to a file-based IO device, transparently joining segments together and acting
|
||||
* like one contiguous file.
|
||||
*/
|
||||
class AudioPlaybackCache : public PlaybackCache
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -52,16 +75,26 @@ public:
|
||||
{
|
||||
public:
|
||||
Segment() = default;
|
||||
Segment(const rational& length, const QString& s);
|
||||
Segment(qint64 size, const QString& filename);
|
||||
|
||||
const rational& length() const
|
||||
qint64 size() const
|
||||
{
|
||||
return length_;
|
||||
return size_;
|
||||
}
|
||||
|
||||
void set_length(const rational& length)
|
||||
void set_size(qint64 sz)
|
||||
{
|
||||
length_ = length;
|
||||
size_ = sz;
|
||||
}
|
||||
|
||||
qint64 offset() const
|
||||
{
|
||||
return offset_;
|
||||
}
|
||||
|
||||
void set_offset(qint64 o)
|
||||
{
|
||||
offset_ = o;
|
||||
}
|
||||
|
||||
const QString& filename() const
|
||||
@@ -74,14 +107,30 @@ public:
|
||||
filename_ = filename;
|
||||
}
|
||||
|
||||
qint64 end() const
|
||||
{
|
||||
return offset_ + size_;
|
||||
}
|
||||
|
||||
private:
|
||||
QString filename_;
|
||||
|
||||
rational length_;
|
||||
qint64 size_;
|
||||
|
||||
qint64 offset_;
|
||||
|
||||
};
|
||||
|
||||
using Playlist = QVector<Segment>;
|
||||
class Playlist : public QVector<Segment>
|
||||
{
|
||||
public:
|
||||
Playlist() = default;
|
||||
|
||||
int GetIndexOfPosition(qint64 pos);
|
||||
|
||||
qint64 GetLength() const;
|
||||
|
||||
};
|
||||
|
||||
class PlaybackDevice : public QIODevice
|
||||
{
|
||||
@@ -97,7 +146,10 @@ public:
|
||||
|
||||
virtual bool seek(qint64 pos) override;
|
||||
|
||||
virtual qint64 size() const override;
|
||||
virtual qint64 size() const override
|
||||
{
|
||||
return playlist_.GetLength();
|
||||
}
|
||||
|
||||
virtual qint64 readData(char *data, qint64 maxSize) override;
|
||||
|
||||
@@ -129,23 +181,25 @@ protected:
|
||||
virtual void LengthChangedEvent(const rational& old, const rational& newlen) override;
|
||||
|
||||
private:
|
||||
static const rational kDefaultSegmentSize;
|
||||
static const qint64 kDefaultSegmentSize;
|
||||
|
||||
Segment CloneSegment(const Segment& s) const;
|
||||
|
||||
Segment CreateSegment(const rational& length) const;
|
||||
Segment CreateSegment(const qint64 &size, const qint64 &offset) const;
|
||||
|
||||
QString GenerateSegmentFilename() const;
|
||||
|
||||
void TrimSegmentIn(Segment* s, const rational& new_length);
|
||||
void TrimSegmentIn(Segment* s, qint64 new_length);
|
||||
|
||||
void TrimSegmentOut(Segment* s, const rational& new_length);
|
||||
void TrimSegmentOut(Segment* s, qint64 new_length);
|
||||
|
||||
void RemoveSegmentFromArray(int index);
|
||||
|
||||
Playlist segments_;
|
||||
void ClearPlaylist();
|
||||
|
||||
rational segment_length_;
|
||||
void UpdateOffsetsFrom(int index);
|
||||
|
||||
Playlist playlist_;
|
||||
|
||||
AudioParams params_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user