audio: rewrote audio cache to write to contiguous segments rather than one long file

Fixes issues with PCM file placement, optimizes some audio-based timeline
and cache operations, just generally a better approach.
This commit is contained in:
itsmattkc
2020-10-11 23:27:40 +11:00
parent 920a15d2ea
commit f7ab0bb640
17 changed files with 575 additions and 226 deletions
+10 -3
View File
@@ -82,16 +82,23 @@ void AudioManager::PushToOutput(const QByteArray &samples)
emit OutputPushed(samples); emit OutputPushed(samples);
} }
void AudioManager::StartOutput(const QString &filename, qint64 offset, int playback_speed) void AudioManager::StartOutput(AudioPlaybackCache *cache, qint64 offset, int playback_speed)
{ {
// Create device
QIODevice* device = cache->CreatePlaybackDevice();
// Move to output manager's thread
device->moveToThread(output_manager_.thread());
// Queue to output manger in other thread
QMetaObject::invokeMethod(&output_manager_, QMetaObject::invokeMethod(&output_manager_,
"PullFromDevice", "PullFromDevice",
Qt::QueuedConnection, Qt::QueuedConnection,
Q_ARG(const QString&, filename), Q_ARG(QIODevice*, device),
Q_ARG(qint64, offset), Q_ARG(qint64, offset),
Q_ARG(int, playback_speed)); Q_ARG(int, playback_speed));
emit OutputDeviceStarted(filename, offset, playback_speed); emit OutputDeviceStarted(cache, offset, playback_speed);
} }
void AudioManager::StopOutput() void AudioManager::StopOutput()
+4 -5
View File
@@ -30,6 +30,7 @@
#include "common/define.h" #include "common/define.h"
#include "outputmanager.h" #include "outputmanager.h"
#include "render/audioparams.h" #include "render/audioparams.h"
#include "render/audioplaybackcache.h"
OLIVE_NAMESPACE_ENTER OLIVE_NAMESPACE_ENTER
@@ -57,11 +58,9 @@ public:
void PushToOutput(const QByteArray& samples); void PushToOutput(const QByteArray& samples);
/** /**
* @brief Start playing audio from QIODevice * @brief Start playing audio from AudioPlaybackCache
*
* This takes ownership of the QIODevice and will delete it when StopOutput() is called
*/ */
void StartOutput(const QString& filename, qint64 offset, int playback_speed); void StartOutput(AudioPlaybackCache* cache, qint64 offset, int playback_speed);
/** /**
* @brief Stop audio output immediately * @brief Stop audio output immediately
@@ -86,7 +85,7 @@ signals:
void OutputNotified(); void OutputNotified();
void OutputDeviceStarted(const QString& filename, qint64 offset, int playback_speed); void OutputDeviceStarted(AudioPlaybackCache* cache, qint64 offset, int playback_speed);
void AudioParamsChanged(const AudioParams& params); void AudioParamsChanged(const AudioParams& params);
+21 -18
View File
@@ -24,11 +24,10 @@
OLIVE_NAMESPACE_ENTER OLIVE_NAMESPACE_ENTER
AudioOutputDeviceProxy::~AudioOutputDeviceProxy() AudioOutputDeviceProxy::AudioOutputDeviceProxy(QObject *parent) :
QIODevice(parent),
device_(nullptr)
{ {
if (file_.isOpen()) {
file_.close();
}
} }
void AudioOutputDeviceProxy::SetParameters(const AudioParams &params) void AudioOutputDeviceProxy::SetParameters(const AudioParams &params)
@@ -36,20 +35,23 @@ void AudioOutputDeviceProxy::SetParameters(const AudioParams &params)
params_ = params; params_ = params;
} }
void AudioOutputDeviceProxy::SetDevice(const QString &filename, qint64 offset, int playback_speed) void AudioOutputDeviceProxy::SetDevice(QIODevice* device, qint64 offset, int playback_speed)
{ {
if (file_.isOpen()) { if (device_) {
file_.close(); delete device_;
} }
file_.setFileName(filename); device_ = device;
device_->setParent(this);
if (!file_.open(QFile::ReadOnly)) { if (!device_->open(QFile::ReadOnly)) {
qCritical() << "Failed to open" << filename << "for audio playback"; qCritical() << "Failed to open IO device for audio playback";
delete device_;
device_ = nullptr;
return; return;
} }
file_.seek(offset); device_->seek(offset);
playback_speed_ = playback_speed; playback_speed_ = playback_speed;
@@ -62,7 +64,8 @@ void AudioOutputDeviceProxy::close()
{ {
QIODevice::close(); QIODevice::close();
file_.close(); delete device_;
device_ = nullptr;
if (tempo_processor_.IsOpen()) { if (tempo_processor_.IsOpen()) {
tempo_processor_.Close(); tempo_processor_.Close();
@@ -71,7 +74,7 @@ void AudioOutputDeviceProxy::close()
qint64 AudioOutputDeviceProxy::readData(char *data, qint64 maxlen) qint64 AudioOutputDeviceProxy::readData(char *data, qint64 maxlen)
{ {
if (!file_.isOpen()) { if (!device_) {
return 0; return 0;
} }
@@ -111,21 +114,21 @@ qint64 AudioOutputDeviceProxy::ReverseAwareRead(char *data, qint64 maxlen)
if (playback_speed_ < 0) { if (playback_speed_ < 0) {
// If we're reversing, we'll seek back by maxlen bytes before we read // If we're reversing, we'll seek back by maxlen bytes before we read
new_pos = file_.pos() - maxlen; new_pos = device_->pos() - maxlen;
if (new_pos < 0) { if (new_pos < 0) {
maxlen = file_.pos(); maxlen = device_->pos();
new_pos = 0; new_pos = 0;
} }
file_.seek(new_pos); device_->seek(new_pos);
} }
qint64 read_count = file_.read(data, maxlen); qint64 read_count = device_->read(data, maxlen);
if (playback_speed_ < 0) { if (playback_speed_ < 0) {
file_.seek(new_pos); device_->seek(new_pos);
// Reverse the samples here // Reverse the samples here
AudioManager::ReverseBuffer(data, static_cast<int>(read_count), params_.samples_to_bytes(1)); AudioManager::ReverseBuffer(data, static_cast<int>(read_count), params_.samples_to_bytes(1));
+3 -5
View File
@@ -35,13 +35,11 @@ class AudioOutputDeviceProxy : public QIODevice
{ {
Q_OBJECT Q_OBJECT
public: public:
AudioOutputDeviceProxy() = default; AudioOutputDeviceProxy(QObject* parent = nullptr);
virtual ~AudioOutputDeviceProxy() override;
void SetParameters(const AudioParams& params); void SetParameters(const AudioParams& params);
void SetDevice(const QString &filename, qint64 offset, int playback_speed); void SetDevice(QIODevice *device, qint64 offset, int playback_speed);
virtual void close() override; virtual void close() override;
@@ -53,7 +51,7 @@ protected:
private: private:
qint64 ReverseAwareRead(char* data, qint64 maxlen); qint64 ReverseAwareRead(char* data, qint64 maxlen);
QFile file_; QIODevice* device_;
TempoProcessor tempo_processor_; TempoProcessor tempo_processor_;
+3 -2
View File
@@ -30,6 +30,7 @@ OLIVE_NAMESPACE_ENTER
AudioOutputManager::AudioOutputManager(QObject *parent) : AudioOutputManager::AudioOutputManager(QObject *parent) :
QObject(parent), QObject(parent),
output_(nullptr), output_(nullptr),
device_proxy_(this),
push_device_(nullptr) push_device_(nullptr)
{ {
} }
@@ -87,7 +88,7 @@ void AudioOutputManager::Close()
} }
} }
void AudioOutputManager::PullFromDevice(const QString &filename, qint64 offset, int playback_speed) void AudioOutputManager::PullFromDevice(QIODevice *device, qint64 offset, int playback_speed)
{ {
if (!output_) { if (!output_) {
return; return;
@@ -99,7 +100,7 @@ void AudioOutputManager::PullFromDevice(const QString &filename, qint64 offset,
push_samples_.clear(); push_samples_.clear();
// Pull from the device // Pull from the device
device_proxy_.SetDevice(filename, offset, playback_speed); device_proxy_.SetDevice(device, offset, playback_speed);
device_proxy_.open(QIODevice::ReadOnly); device_proxy_.open(QIODevice::ReadOnly);
output_->start(&device_proxy_); output_->start(&device_proxy_);
} }
+1 -1
View File
@@ -53,7 +53,7 @@ public slots:
* This will clear any pushed samples or QIODevices currently being read and will start reading from this next time * This will clear any pushed samples or QIODevices currently being read and will start reading from this next time
* the audio output requests data. * the audio output requests data.
*/ */
void PullFromDevice(const QString &filename, qint64 offset, int playback_speed); void PullFromDevice(QIODevice* device, qint64 offset, int playback_speed);
// Queued // Queued
void ResetToPushMode(); void ResetToPushMode();
+8
View File
@@ -20,6 +20,8 @@
#include "encoder.h" #include "encoder.h"
#include <QFile>
#include "ffmpeg/ffmpegencoder.h" #include "ffmpeg/ffmpegencoder.h"
OLIVE_NAMESPACE_ENTER OLIVE_NAMESPACE_ENTER
@@ -34,6 +36,12 @@ const EncodingParams &Encoder::params() const
return params_; return params_;
} }
void Encoder::WriteAudio(AudioParams pcm_info, const QString &pcm_filename)
{
QFile f(pcm_filename);
WriteAudio(pcm_info, &f);
}
EncodingParams::EncodingParams() : EncodingParams::EncodingParams() :
video_enabled_(false), video_enabled_(false),
video_bit_rate_(0), video_bit_rate_(0),
+3 -1
View File
@@ -116,7 +116,9 @@ public:
virtual bool WriteFrame(OLIVE_NAMESPACE::FramePtr frame, OLIVE_NAMESPACE::rational time) = 0; virtual bool WriteFrame(OLIVE_NAMESPACE::FramePtr frame, OLIVE_NAMESPACE::rational time) = 0;
virtual void WriteAudio(OLIVE_NAMESPACE::AudioParams pcm_info, virtual void WriteAudio(OLIVE_NAMESPACE::AudioParams pcm_info,
const QString& pcm_filename) = 0; QIODevice *file) = 0;
void WriteAudio(OLIVE_NAMESPACE::AudioParams pcm_info,
const QString& pcm_filename);
virtual void Close() = 0; virtual void Close() = 0;
+7 -6
View File
@@ -185,10 +185,9 @@ fail:
return success; return success;
} }
void FFmpegEncoder::WriteAudio(AudioParams pcm_info, const QString &pcm_filename) void FFmpegEncoder::WriteAudio(AudioParams pcm_info, QIODevice* file)
{ {
QFile pcm(pcm_filename); if (file->open(QFile::ReadOnly)) {
if (pcm.open(QFile::ReadOnly)) {
// Divide PCM stream into AVFrames // Divide PCM stream into AVFrames
// See if the codec defines a number of samples per frame // See if the codec defines a number of samples per frame
@@ -239,7 +238,7 @@ void FFmpegEncoder::WriteAudio(AudioParams pcm_info, const QString &pcm_filename
int max_read = pcm_info.samples_to_bytes(samples_needed); int max_read = pcm_info.samples_to_bytes(samples_needed);
// Read bytes from PCM // Read bytes from PCM
QByteArray input_data = pcm.read(max_read); QByteArray input_data = file->read(max_read);
// Use swresample to convert the data into the correct format // Use swresample to convert the data into the correct format
const char* input_data_array = input_data.constData(); const char* input_data_array = input_data.constData();
@@ -273,7 +272,7 @@ void FFmpegEncoder::WriteAudio(AudioParams pcm_info, const QString &pcm_filename
} }
// Break if we've reached the end point // Break if we've reached the end point
if (pcm.atEnd()) { if (file->atEnd()) {
break; break;
} }
} }
@@ -282,7 +281,9 @@ void FFmpegEncoder::WriteAudio(AudioParams pcm_info, const QString &pcm_filename
swr_free(&swr_ctx); swr_free(&swr_ctx);
pcm.close(); file->close();
} else {
qWarning() << "Failed to open audio IO device for encoding";
} }
} }
+1 -1
View File
@@ -43,7 +43,7 @@ public:
virtual bool WriteFrame(OLIVE_NAMESPACE::FramePtr frame, OLIVE_NAMESPACE::rational time) override; virtual bool WriteFrame(OLIVE_NAMESPACE::FramePtr frame, OLIVE_NAMESPACE::rational time) override;
virtual void WriteAudio(OLIVE_NAMESPACE::AudioParams pcm_info, virtual void WriteAudio(OLIVE_NAMESPACE::AudioParams pcm_info,
const QString& pcm_filename) override; QIODevice *file) override;
virtual void Close() override; virtual void Close() override;
+378 -130
View File
@@ -28,11 +28,20 @@
OLIVE_NAMESPACE_ENTER OLIVE_NAMESPACE_ENTER
const rational AudioPlaybackCache::kDefaultSegmentSize = 5;
AudioPlaybackCache::AudioPlaybackCache(QObject* parent) : AudioPlaybackCache::AudioPlaybackCache(QObject* parent) :
PlaybackCache(parent) PlaybackCache(parent)
{ {
quint32 r = std::rand(); }
UpdateFilename(QString::number(r));
AudioPlaybackCache::~AudioPlaybackCache()
{
// Segments are volatile, so delete them here
foreach (const Segment& s, segments_) {
QFile::remove(s.filename());
}
segments_.clear();
} }
void AudioPlaybackCache::SetParameters(const AudioParams &params) void AudioPlaybackCache::SetParameters(const AudioParams &params)
@@ -44,16 +53,10 @@ void AudioPlaybackCache::SetParameters(const AudioParams &params)
params_ = params; params_ = params;
// Restart empty file so there's always "something" to play // Restart empty file so there's always "something" to play
QFile f(filename_); segments_.clear();
if (f.open(QFile::WriteOnly)) {
f.close();
}
// Our current audio cache is unusable, so we truncate it automatically // Our current audio cache is unusable, so we truncate it automatically
TimeRange invalidate_range(0, GetLength()); InvalidateAll();
if (invalidate_range.in() != invalidate_range.out()) {
Invalidate(invalidate_range);
}
emit ParametersChanged(); emit ParametersChanged();
} }
@@ -65,148 +68,213 @@ void AudioPlaybackCache::WritePCM(const TimeRange &range, SampleBufferPtr sample
return; return;
} }
QFile f(filename_); // Determine if we have enough segments to pull this off
if (f.open(QFile::ReadWrite)) { while (segment_length_ < range.out()) {
QByteArray a = samples->toPackedData(); rational seg_sz = qMin(kDefaultSegmentSize, range.out() - segment_length_);
segments_.push_back(CreateSegment(seg_sz));
segment_length_ += seg_sz;
}
foreach (const TimeRange& r, valid_ranges) { // Convert to packed data, which is what we store on disk
// Calculate destination offsets QByteArray a = samples->toPackedData();
qint64 start_offset = params_.time_to_bytes(r.in());
qint64 max_len = params_.time_to_bytes(r.out());
if (f.size() < max_len) { // Keep track of validated ranges so we can signal them all at once at the end
f.resize(max_len); TimeRangeList ranges_we_validated;
// Write each valid range to the segments
foreach (const TimeRange& r, valid_ranges) {
rational this_segment_in = 0;
for (auto it=segments_.begin(); it!=segments_.end(); it++) {
rational this_segment_out = this_segment_in + (*it).length();
if (r.in() < this_segment_out) {
// We'll write at least something to this segment
QFile seg_file((*it).filename());
if (seg_file.open(QFile::ReadWrite)) {
// Calculate how much to write
rational this_write_in_point = qMax(r.in(), this_segment_in);
rational this_write_out_point = qMin(r.out(), this_segment_out);
// Calculate what the byte offsets are going to be in this segment file
rational in_point_relative = this_write_in_point - this_segment_in;
qint64 dst_offset = params_.time_to_bytes(in_point_relative);
// Calculate where to retrieve data from in the source buffer
qint64 src_offset = params_.time_to_bytes(this_write_in_point - range.in());
// Determine how many bytes need to be written
qint64 total_write_length = params_.time_to_bytes(this_write_out_point - this_write_in_point);
// Determine how many bytes we actually have in the source buffer
qint64 possible_write_length = qMin(qMax(qint64(0), a.size() - src_offset), total_write_length);
// Seek to our start offset
seg_file.seek(dst_offset);
// If we have source bytes to write, write them here
if (possible_write_length > 0) {
seg_file.write(a.data() + src_offset, possible_write_length);
}
if (possible_write_length < total_write_length) {
// Fill remaining space with silence
QByteArray s(total_write_length - possible_write_length, 0x00);
seg_file.write(s);
}
seg_file.close();
ranges_we_validated.InsertTimeRange(TimeRange(this_write_in_point, this_write_out_point));
} else {
qWarning() << "Failed to write PCM data to" << seg_file.fileName();
}
} }
// Calculate source offsets if (r.out() <= this_segment_out) {
qint64 sample_start = params_.time_to_bytes(r.in() - range.in()); // We've reached the end of this range, we can break out of the loop here
qint64 sample_len = params_.time_to_bytes(r.length()); break;
qint64 actual_write = qMin(sample_len, a.size() - sample_start);
f.seek(start_offset);
f.write(a.data() + sample_start, actual_write);
if (actual_write < sample_len) {
// Fill remaining space with silence
QByteArray s(sample_len - actual_write, 0x00);
f.write(s);
} }
// Each segment is contiguous, so this out will be the next segment's in
this_segment_in = this_segment_out;
} }
}
f.close(); foreach (const TimeRange& v, ranges_we_validated) {
Validate(v);
Validate(range);
} else {
qWarning() << "Failed to write PCM data to" << filename_;
} }
} }
void AudioPlaybackCache::WriteSilence(const TimeRange &range) void AudioPlaybackCache::WriteSilence(const TimeRange &range, qint64 job_time)
{ {
QFile f(filename_); // WritePCM will automatically fill non-existent bytes with silence, so we just have to send
if (f.open(QFile::ReadWrite)) { // it an empty sample buffer
qint64 start_offset = params_.time_to_bytes(range.in()); WritePCM(range, std::make_shared<SampleBuffer>(), job_time);
qint64 max_len = params_.time_to_bytes(range.out());
qint64 write_len = max_len - start_offset;
if (f.size() < max_len) {
f.resize(max_len);
}
f.seek(start_offset);
QByteArray a(write_len, 0x00);
f.write(a);
f.close();
Validate(range);
} else {
qWarning() << "Failed to write PCM data to" << filename_;
}
} }
/*
void AudioPlaybackCache::SetUuid(const QUuid &id)
{
UpdateFilename(id.toByteArray());
}
*/
void AudioPlaybackCache::ShiftEvent(const rational &from, const rational &to) void AudioPlaybackCache::ShiftEvent(const rational &from, const rational &to)
{ {
qint64 from_offset = params_.time_to_bytes(from); if (from == to) {
qint64 to_offset = params_.time_to_bytes(to);
if (from_offset == to_offset) {
return; return;
} }
QFile f(filename_); int to_index = -1;
if (f.open(QFile::ReadWrite)) { int from_index = -1;
if (!f.size()) { rational to_start, from_start;
return;
}
qint64 chunk = qAbs(to_offset - from_offset); {
rational seg_start;
QByteArray buf(chunk, Qt::Uninitialized); // Find which segments intersect with this shift event
for (int i=0; i<segments_.size(); i++) {
const Segment& seg = segments_.at(i);
if (to > from) { rational seg_end = seg_start + seg.length();
// Shifting forwards, we must insert a new region and shift all the bytes there
// For shifting forwards, we copy bytes starting at the back so that bytes we need don't
// get overwritten.
qint64 read_offset = f.size();
f.resize(f.size() + chunk);
qint64 write_offset = f.size();
while (read_offset != from_offset) {
// Calculate how much will be read this time
qint64 chunk_sz = qMin(chunk, read_offset - from_offset);
read_offset -= chunk_sz;
// Read that chunk
f.seek(read_offset);
f.read(buf.data(), chunk_sz);
// Write it at the destination
write_offset -= chunk_sz;
f.seek(write_offset);
f.write(buf.data(), chunk_sz);
if (to_index == -1 && seg_end > to) {
to_index = i;
to_start = seg_start;
} }
// Replace remainder with silence if (from_index == -1 && seg_end >= from) {
f.seek(from_offset); from_index = i;
buf.fill(0); from_start = seg_start;
f.write(buf); }
if (to_index != -1 && from_index != -1) {
break;
}
seg_start = seg_end;
}
}
Segment& from_segment = segments_[from_index];
const rational& from_length = from_segment.length();
rational from_end = from_start + from_length;
if (from < to) {
// Shifting forwards, we must insert a new region and split a segment in half if necessary
int insert_index;
// Determine at what part of the array we'll be insert into
if (from == from_start) {
insert_index = from_index;
} else { } else {
// Shifting backwards, we will shift bytes and truncate insert_index = from_index + 1;
while (from_offset != f.size()) {
// Read region to be shifted
f.seek(from_offset);
qint64 read_sz = f.read(buf.data(), buf.size());
from_offset += read_sz;
// Write it at the destination
f.seek(to_offset);
to_offset += f.write(buf, read_sz);
}
// Truncate
f.resize(f.size() - chunk);
} }
f.close(); if (from < from_end) {
// Split from segment into two
Segment second = CloneSegment(from_segment);
TrimSegmentOut(&from_segment, from - from_start);
TrimSegmentIn(&second, from_end - from);
segments_.insert(insert_index, second);
}
// Insert silent segments
rational time_to_insert = to - from;
rational inserted_time;
while (inserted_time < time_to_insert) {
rational new_seg_sz = qMin(kDefaultSegmentSize, time_to_insert - inserted_time);
segments_.insert(insert_index, CreateSegment(new_seg_sz));
inserted_time += new_seg_sz;
}
segment_length_ += time_to_insert;
} else { } else {
qWarning() << "Failed to write PCM data to" << filename_; // Shifting backwards, we'll be removing segments and truncating them if necessary
Segment& to_segment = segments_[to_index];
const rational& to_length = to_segment.length();
rational to_end = to_start + to_length;
if (from_index == to_index) {
// Shift occurs in the same segment
if (to > to_start && from < to_end) {
// Split into two and process as normal
Segment second = CloneSegment(to_segment);
from_index++;
segments_.insert(from_index, second);
} else if (to == to_start && from == to_end) {
RemoveSegmentFromArray(to_index);
} else if (to == to_start) {
TrimSegmentIn(&to_segment, to_end - from);
} else {
TrimSegmentOut(&from_segment, to - to_start);
}
} else {
// Remove all central segments (if there are any)
while (from_index > to_index + 1) {
RemoveSegmentFromArray(to_index + 1);
from_index--;
}
}
if (from_index != to_index) {
// Remove or trim "to" segment
if (to == to_start) {
RemoveSegmentFromArray(to_index);
} else if (to < to_end) {
TrimSegmentOut(&to_segment, to - to_start);
}
// Remove or trim "from" segment
if (from == from_end) {
RemoveSegmentFromArray(from_index);
} else if (from > from_start) {
TrimSegmentIn(&from_segment, from_end - from);
}
}
segment_length_ -= (from - to);
} }
} }
@@ -216,11 +284,96 @@ void AudioPlaybackCache::LengthChangedEvent(const rational& old, const rational&
return; return;
} }
if (newlen < old) { while (newlen < segment_length_) {
QFile(filename_).resize(params_.time_to_bytes(newlen)); Segment& last_seg = segments_.back();
if (segment_length_ - last_seg.length() < newlen) {
// Truncate this segment rather than removing it
rational diff = segment_length_ - newlen;
TrimSegmentOut(&last_seg, last_seg.length() - diff);
segment_length_ -= diff;
} else {
// Remove last segment
segment_length_ -= last_seg.length();
RemoveSegmentFromArray(segments_.size() - 1);
}
} }
} }
AudioPlaybackCache::Segment AudioPlaybackCache::CloneSegment(const AudioPlaybackCache::Segment &s) const
{
Segment new_seg = s;
// Copy data to a new file
QString new_filename = GenerateSegmentFilename();
QFile::copy(s.filename(), new_filename);
new_seg.set_filename(new_filename);
return new_seg;
}
AudioPlaybackCache::Segment AudioPlaybackCache::CreateSegment(const rational &length) const
{
Segment s(length, GenerateSegmentFilename());
// Create empty file
QFile f(s.filename());
if (f.open(QFile::WriteOnly)) {
f.close();
}
return s;
}
QString AudioPlaybackCache::GenerateSegmentFilename() const
{
QString new_seg_filename;
do {
uint32_t r = std::rand();
new_seg_filename = QDir(GetCacheDirectory()).filePath(QStringLiteral("%1.pcm").arg(r));
} while (QFileInfo::exists(new_seg_filename));
return new_seg_filename;
}
void AudioPlaybackCache::TrimSegmentIn(AudioPlaybackCache::Segment *s, const rational &new_length)
{
// Read filename
QFile f(s->filename());
if (f.open(QFile::ReadWrite)) {
// Read whole segment into memory
QByteArray data = f.readAll();
// Trim to new length
data = data.right(params_.time_to_bytes(new_length));
// Clear existing file
f.resize(0);
// Write trimmed data
f.write(data);
f.close();
}
s->set_length(new_length);
}
void AudioPlaybackCache::TrimSegmentOut(AudioPlaybackCache::Segment *s, const rational &new_length)
{
QFile(s->filename()).resize(params_.time_to_bytes(new_length));
s->set_length(new_length);
}
void AudioPlaybackCache::RemoveSegmentFromArray(int index)
{
QFile::remove(segments_.at(index).filename());
segments_.removeAt(index);
}
QList<TimeRange> AudioPlaybackCache::GetValidRanges(const TimeRange& range, const qint64& job_time) QList<TimeRange> AudioPlaybackCache::GetValidRanges(const TimeRange& range, const qint64& job_time)
{ {
QList<TimeRange> valid_ranges; QList<TimeRange> valid_ranges;
@@ -236,15 +389,110 @@ QList<TimeRange> AudioPlaybackCache::GetValidRanges(const TimeRange& range, cons
return valid_ranges; return valid_ranges;
} }
void AudioPlaybackCache::UpdateFilename(const QString &s) AudioPlaybackCache::PlaybackDevice *AudioPlaybackCache::CreatePlaybackDevice(QObject* parent) const
{ {
filename_ = QDir(GetCacheDirectory()).filePath(s); return new PlaybackDevice(segments_, parent);
filename_.append(QStringLiteral(".pcm"));
} }
const QString &AudioPlaybackCache::GetPCMFilename() const AudioPlaybackCache::Segment::Segment(const rational &length, const QString &s)
{ {
return filename_; length_ = length;
filename_ = s;
}
AudioPlaybackCache::PlaybackDevice::PlaybackDevice(const AudioPlaybackCache::Playlist &playlist, QObject *parent) :
QIODevice(parent),
playlist_(playlist),
current_segment_(0),
segment_read_index_(0)
{
}
AudioPlaybackCache::PlaybackDevice::~PlaybackDevice()
{
close();
}
bool AudioPlaybackCache::PlaybackDevice::seek(qint64 pos)
{
// Default behavior
QIODevice::seek(pos);
// Find which segment we're in
// FIXME: Inefficient
qint64 seg_start_bytes = 0;
for (int i=0; i<playlist_.size(); i++) {
const Segment& s = playlist_.at(i);
qint64 this_seg_sz = QFile(s.filename()).size();
qint64 seg_end_bytes = seg_start_bytes + this_seg_sz;
if (pos < seg_end_bytes) {
// This must be the segment we're looking for
current_segment_ = i;
segment_read_index_ = pos - seg_start_bytes;
// Succeeded at seeking to this position
return true;
}
seg_start_bytes = seg_end_bytes;
}
// Couldn't find this position in the file
return false;
}
qint64 AudioPlaybackCache::PlaybackDevice::size() const
{
qint64 p = 0;
// FIXME: Inefficient
for (int i=0; i<playlist_.size(); i++) {
p += QFile(playlist_.at(i).filename()).size();
}
return p;
}
qint64 AudioPlaybackCache::PlaybackDevice::readData(char *data, qint64 maxSize)
{
qint64 read_size = 0;
while (read_size < maxSize && current_segment_ < playlist_.size()) {
QFile segment_file(playlist_.at(current_segment_).filename());
if (segment_file.open(QFile::ReadOnly)) {
// Seek to our stored index of this segment
segment_file.seek(segment_read_index_);
// Determine how many bytes to read
qint64 this_read_length = qMin(segment_file.size() - segment_read_index_,
maxSize - read_size);
// Read those bytes
segment_file.read(data + read_size, this_read_length);
// Close the file
segment_file.close();
// Add to the read index
segment_read_index_ += this_read_length;
// Add to the read size
read_size += this_read_length;
// If we've reached the end of this segment, tick the counter over to the next segment
if (segment_read_index_ == segment_file.size()) {
// Jump to the next file
segment_read_index_ = 0;
current_segment_++;
}
} else {
qWarning() << "Failed to read data from segment";
}
}
return read_size;
} }
OLIVE_NAMESPACE_EXIT OLIVE_NAMESPACE_EXIT
+91 -7
View File
@@ -33,6 +33,8 @@ class AudioPlaybackCache : public PlaybackCache
public: public:
AudioPlaybackCache(QObject* parent = nullptr); AudioPlaybackCache(QObject* parent = nullptr);
virtual ~AudioPlaybackCache() override;
AudioParams GetParameters() AudioParams GetParameters()
{ {
return params_; return params_;
@@ -42,14 +44,82 @@ public:
void WritePCM(const TimeRange &range, SampleBufferPtr samples, const qint64& job_time); void WritePCM(const TimeRange &range, SampleBufferPtr samples, const qint64& job_time);
void WriteSilence(const TimeRange &range); void WriteSilence(const TimeRange &range, qint64 job_time);
//void SetUuid(const QUuid& id);
const QString& GetPCMFilename() const;
QList<TimeRange> GetValidRanges(const TimeRange &range, const qint64 &job_time); QList<TimeRange> GetValidRanges(const TimeRange &range, const qint64 &job_time);
class Segment
{
public:
Segment() = default;
Segment(const rational& length, const QString& s);
const rational& length() const
{
return length_;
}
void set_length(const rational& length)
{
length_ = length;
}
const QString& filename() const
{
return filename_;
}
void set_filename(const QString& filename)
{
filename_ = filename;
}
private:
QString filename_;
rational length_;
};
using Playlist = QVector<Segment>;
class PlaybackDevice : public QIODevice
{
public:
PlaybackDevice(const Playlist& playlist, QObject* parent = nullptr);
virtual ~PlaybackDevice() override;
virtual bool isSequential() const override
{
return false;
}
virtual bool seek(qint64 pos) override;
virtual qint64 size() const override;
virtual qint64 readData(char *data, qint64 maxSize) override;
virtual qint64 writeData(const char *data, qint64 maxSize) override
{
Q_UNUSED(data)
Q_UNUSED(maxSize)
return -1;
}
private:
Playlist playlist_;
int current_segment_;
qint64 segment_read_index_;
};
PlaybackDevice* CreatePlaybackDevice(QObject *parent = nullptr) const;
signals: signals:
void ParametersChanged(); void ParametersChanged();
@@ -59,9 +129,23 @@ protected:
virtual void LengthChangedEvent(const rational& old, const rational& newlen) override; virtual void LengthChangedEvent(const rational& old, const rational& newlen) override;
private: private:
void UpdateFilename(const QString& s); static const rational kDefaultSegmentSize;
QString filename_; Segment CloneSegment(const Segment& s) const;
Segment CreateSegment(const rational& length) const;
QString GenerateSegmentFilename() const;
void TrimSegmentIn(Segment* s, const rational& new_length);
void TrimSegmentOut(Segment* s, const rational& new_length);
void RemoveSegmentFromArray(int index);
Playlist segments_;
rational segment_length_;
AudioParams params_; AudioParams params_;
+1 -1
View File
@@ -108,7 +108,7 @@ bool ExportTask::Run()
if (params_.audio_enabled()) { if (params_.audio_enabled()) {
// Write audio data now // Write audio data now
encoder_->WriteAudio(audio_params(), audio_data_.GetPCMFilename()); encoder_->WriteAudio(audio_params(), audio_data_.CreatePlaybackDevice(encoder_));
} }
encoder_->Close(); encoder_->Close();
+15 -19
View File
@@ -35,6 +35,7 @@ const int kMaximumSmoothness = 8;
AudioMonitor::AudioMonitor(QWidget *parent) : AudioMonitor::AudioMonitor(QWidget *parent) :
QOpenGLWidget(parent), QOpenGLWidget(parent),
file_(nullptr),
cached_channels_(0) cached_channels_(0)
{ {
values_.resize(kMaximumSmoothness); values_.resize(kMaximumSmoothness);
@@ -45,11 +46,6 @@ AudioMonitor::AudioMonitor(QWidget *parent) :
connect(AudioManager::instance(), &AudioManager::Stopped, this, &AudioMonitor::Stop); connect(AudioManager::instance(), &AudioManager::Stopped, this, &AudioMonitor::Stop);
} }
AudioMonitor::~AudioMonitor()
{
Stop();
}
void AudioMonitor::SetParams(const AudioParams &params) void AudioMonitor::SetParams(const AudioParams &params)
{ {
params_ = params; params_ = params;
@@ -63,18 +59,19 @@ void AudioMonitor::SetParams(const AudioParams &params)
peaked_.fill(false); peaked_.fill(false);
} }
void AudioMonitor::OutputDeviceSet(const QString &filename, qint64 offset, int playback_speed) void AudioMonitor::OutputDeviceSet(AudioPlaybackCache *cache, qint64 offset, int playback_speed)
{ {
Stop(); Stop();
file_.setFileName(filename); file_ = cache->CreatePlaybackDevice(this);
if (!file_.open(QFile::ReadOnly)) { if (!file_->open(QFile::ReadOnly)) {
qWarning() << "Failed to open" << filename; qWarning() << "Failed to open IO device for AudioMonitor display";
Stop();
return; return;
} }
file_.seek(offset); file_->seek(offset);
playback_speed_ = playback_speed; playback_speed_ = playback_speed;
@@ -85,9 +82,8 @@ void AudioMonitor::OutputDeviceSet(const QString &filename, qint64 offset, int p
void AudioMonitor::Stop() void AudioMonitor::Stop()
{ {
if (file_.isOpen()) { delete file_;
file_.close(); file_ = nullptr;
}
} }
void AudioMonitor::OutputPushed(const QByteArray &d) void AudioMonitor::OutputPushed(const QByteArray &d)
@@ -215,7 +211,7 @@ void AudioMonitor::paintGL()
QVector<double> v(params_.channel_count(), 0); QVector<double> v(params_.channel_count(), 0);
if (file_.isOpen()) { if (file_) {
UpdateValuesFromFile(v); UpdateValuesFromFile(v);
} }
@@ -258,7 +254,7 @@ void AudioMonitor::paintGL()
} }
} }
if (all_zeroes && !file_.isOpen()) { if (all_zeroes && !file_) {
// Optimize by disabling the update loop // Optimize by disabling the update loop
SetUpdateLoop(false); SetUpdateLoop(false);
} }
@@ -287,18 +283,18 @@ void AudioMonitor::UpdateValuesFromFile(QVector<double>& v)
if (playback_speed_ < 0) { if (playback_speed_ < 0) {
// If reversing, jump back by the amount of bytes we're going to read // If reversing, jump back by the amount of bytes we're going to read
bytes_to_read = qMin(bytes_to_read, file_.pos()); bytes_to_read = qMin(bytes_to_read, file_->pos());
file_.seek(file_.pos() - bytes_to_read); file_->seek(file_->pos() - bytes_to_read);
} }
// Read bytes in from file // Read bytes in from file
QByteArray b = file_.read(bytes_to_read); QByteArray b = file_->read(bytes_to_read);
if (playback_speed_ < 0) { if (playback_speed_ < 0) {
// If reversing, head back to where we were before the read so that the next read starts // If reversing, head back to where we were before the read so that the next read starts
// from where we left off // from where we left off
file_.seek(file_.pos() - bytes_to_read); file_->seek(file_->pos() - bytes_to_read);
} }
// If speed is not 1, transform it here // If speed is not 1, transform it here
+3 -4
View File
@@ -27,6 +27,7 @@
#include "common/define.h" #include "common/define.h"
#include "render/audioparams.h" #include "render/audioparams.h"
#include "render/audioplaybackcache.h"
OLIVE_NAMESPACE_ENTER OLIVE_NAMESPACE_ENTER
@@ -36,12 +37,10 @@ class AudioMonitor : public QOpenGLWidget
public: public:
AudioMonitor(QWidget* parent = nullptr); AudioMonitor(QWidget* parent = nullptr);
virtual ~AudioMonitor() override;
public slots: public slots:
void SetParams(const AudioParams& params); void SetParams(const AudioParams& params);
void OutputDeviceSet(const QString& filename, qint64 offset, int playback_speed); void OutputDeviceSet(AudioPlaybackCache* cache, qint64 offset, int playback_speed);
void Stop(); void Stop();
@@ -66,7 +65,7 @@ private:
AudioParams params_; AudioParams params_;
QFile file_; QIODevice* file_;
qint64 last_time_; qint64 last_time_;
int playback_speed_; int playback_speed_;
+13 -9
View File
@@ -63,11 +63,13 @@ void AudioWaveformView::paintEvent(QPaintEvent *event)
{ {
QWidget::paintEvent(event); QWidget::paintEvent(event);
if (!playback_) {
return;
}
const AudioParams& params = playback_->GetParameters(); const AudioParams& params = playback_->GetParameters();
if (!playback_ if (!params.is_valid()) {
|| playback_->GetPCMFilename().isEmpty()
|| !params.is_valid()) {
return; return;
} }
@@ -78,9 +80,9 @@ void AudioWaveformView::paintEvent(QPaintEvent *event)
cached_waveform_ = QPixmap(size()); cached_waveform_ = QPixmap(size());
cached_waveform_.fill(Qt::transparent); cached_waveform_.fill(Qt::transparent);
QFile fs(playback_->GetPCMFilename()); QIODevice* fs = playback_->CreatePlaybackDevice();
if (fs.open(QFile::ReadOnly)) { if (fs->open(QFile::ReadOnly)) {
QPainter wave_painter(&cached_waveform_); QPainter wave_painter(&cached_waveform_);
@@ -89,13 +91,13 @@ void AudioWaveformView::paintEvent(QPaintEvent *event)
int drew = 0; int drew = 0;
fs.seek(params.samples_to_bytes(ScreenToUnitRounded(0))); fs->seek(params.samples_to_bytes(ScreenToUnitRounded(0)));
for (int x=0; x<width() && !fs.atEnd(); x++) { for (int x=0; x<width() && !fs->atEnd(); x++) {
int samples_len = ScreenToUnitRounded(x+1) - ScreenToUnitRounded(x); int samples_len = ScreenToUnitRounded(x+1) - ScreenToUnitRounded(x);
int max_read_size = params.samples_to_bytes(samples_len); int max_read_size = params.samples_to_bytes(samples_len);
QByteArray read_buffer = fs.read(max_read_size); QByteArray read_buffer = fs->read(max_read_size);
// Detect whether we've reached EOF and recalculate sample count if so // Detect whether we've reached EOF and recalculate sample count if so
if (read_buffer.size() < max_read_size) { if (read_buffer.size() < max_read_size) {
@@ -117,9 +119,11 @@ void AudioWaveformView::paintEvent(QPaintEvent *event)
cached_scale_ = GetScale(); cached_scale_ = GetScale();
cached_scroll_ = GetScroll(); cached_scroll_ = GetScroll();
fs.close(); fs->close();
} }
delete fs;
} }
QPainter p(this); QPainter p(this);
+13 -14
View File
@@ -204,8 +204,8 @@ void ViewerWidget::ConnectNodeInternal(ViewerOutput *n)
UpdateStack(); UpdateStack();
waveform_view_->SetViewer(GetConnectedNode()->audio_playback_cache());
if (GetConnectedTimelinePoints()) { if (GetConnectedTimelinePoints()) {
waveform_view_->SetViewer(GetConnectedNode()->audio_playback_cache());
waveform_view_->ConnectTimelinePoints(GetConnectedTimelinePoints()); waveform_view_->ConnectTimelinePoints(GetConnectedTimelinePoints());
} }
@@ -512,23 +512,24 @@ void ViewerWidget::PushScrubbedAudio()
{ {
if (!IsPlaying() && Config::Current()["AudioScrubbing"].toBool()) { if (!IsPlaying() && Config::Current()["AudioScrubbing"].toBool()) {
// Get audio src device from renderer // Get audio src device from renderer
QString audio_fn = GetConnectedNode()->audio_playback_cache()->GetPCMFilename(); AudioPlaybackCache::PlaybackDevice* audio_src = GetConnectedNode()->audio_playback_cache()->CreatePlaybackDevice();
QFile audio_src(audio_fn);
if (audio_src.open(QFile::ReadOnly)) { if (audio_src->open(QIODevice::ReadOnly)) {
const AudioParams& params = GetConnectedNode()->audio_playback_cache()->GetParameters(); const AudioParams& params = GetConnectedNode()->audio_playback_cache()->GetParameters();
// FIXME: Hardcoded scrubbing interval (20ms) // FIXME: Hardcoded scrubbing interval (20ms)
int size_of_sample = params.time_to_bytes(rational(20, 1000)); int size_of_sample = params.time_to_bytes(rational(20, 1000));
// Push audio // Push audio
audio_src.seek(params.time_to_bytes(GetTime())); audio_src->seek(params.time_to_bytes(GetTime()));
QByteArray frame_audio = audio_src.read(size_of_sample); QByteArray frame_audio = audio_src->read(size_of_sample);
AudioManager::instance()->SetOutputParams(params); AudioManager::instance()->SetOutputParams(params);
AudioManager::instance()->PushToOutput(frame_audio); AudioManager::instance()->PushToOutput(frame_audio);
audio_src.close(); audio_src->close();
} }
delete audio_src;
} }
} }
@@ -628,13 +629,11 @@ void ViewerWidget::FinishPlayPreprocess()
{ {
int64_t playback_start_time = ruler()->GetTime(); int64_t playback_start_time = ruler()->GetTime();
QString audio_fn = GetConnectedNode()->audio_playback_cache()->GetPCMFilename(); AudioPlaybackCache* audio_cache = GetConnectedNode()->audio_playback_cache();
if (!audio_fn.isEmpty()) { AudioManager::instance()->SetOutputParams(audio_cache->GetParameters());
AudioManager::instance()->SetOutputParams(GetConnectedNode()->audio_playback_cache()->GetParameters()); AudioManager::instance()->StartOutput(audio_cache,
AudioManager::instance()->StartOutput(audio_fn, audio_cache->GetParameters().time_to_bytes(GetTime()),
GetConnectedNode()->audio_playback_cache()->GetParameters().time_to_bytes(GetTime()), playback_speed_);
playback_speed_);
}
playback_timer_.Start(playback_start_time, playback_speed_, timebase_dbl()); playback_timer_.Start(playback_start_time, playback_speed_, timebase_dbl());