audiowaveformview: multithreaded waveform rendering

This commit is contained in:
itsmattkc
2020-11-30 08:18:11 +11:00
parent f48c8b552b
commit b39565cff4
4 changed files with 158 additions and 70 deletions
+5 -5
View File
@@ -129,27 +129,27 @@ void SeekableWidget::SetScroll(int s)
update();
}
double SeekableWidget::ScreenToUnitFloat(int screen)
double SeekableWidget::ScreenToUnitFloat(int screen) const
{
return (screen + scroll_) / GetScale() / timebase_dbl();
}
int64_t SeekableWidget::ScreenToUnit(int screen)
int64_t SeekableWidget::ScreenToUnit(int screen) const
{
return qFloor(ScreenToUnitFloat(screen));
}
int64_t SeekableWidget::ScreenToUnitRounded(int screen)
int64_t SeekableWidget::ScreenToUnitRounded(int screen) const
{
return qRound64(ScreenToUnitFloat(screen));
}
int SeekableWidget::UnitToScreen(int64_t unit)
int SeekableWidget::UnitToScreen(int64_t unit) const
{
return qFloor(static_cast<double>(unit) * GetScale() * timebase_dbl()) - scroll_;
}
int SeekableWidget::TimeToScreen(const rational &time)
int SeekableWidget::TimeToScreen(const rational &time) const
{
return qFloor(time.toDouble() * GetScale()) - scroll_;
}
+5 -5
View File
@@ -60,14 +60,14 @@ protected:
TimelinePoints* timeline_points() const;
double ScreenToUnitFloat(int screen);
double ScreenToUnitFloat(int screen) const;
int64_t ScreenToUnit(int screen);
int64_t ScreenToUnitRounded(int screen);
int64_t ScreenToUnit(int screen) const;
int64_t ScreenToUnitRounded(int screen) const;
int UnitToScreen(int64_t unit);
int UnitToScreen(int64_t unit) const;
int TimeToScreen(const rational& time);
int TimeToScreen(const rational& time) const;
void DrawPlayhead(QPainter* p, int x, int y);
+115 -56
View File
@@ -36,6 +36,8 @@ AudioWaveformView::AudioWaveformView(QWidget *parent) :
{
setAutoFillBackground(true);
setBackgroundRole(QPalette::Base);
cached_waveform_.resize(QThread::idealThreadCount());
}
void AudioWaveformView::SetViewer(AudioPlaybackCache *playback)
@@ -73,66 +75,44 @@ void AudioWaveformView::paintEvent(QPaintEvent *event)
return;
}
if (cached_size_ != size()
|| cached_scale_ != GetScale()
|| cached_scroll_ != GetScroll()) {
cached_waveform_ = QPixmap(size());
cached_waveform_.fill(Qt::transparent);
QIODevice* fs = playback_->CreatePlaybackDevice();
if (fs->open(QFile::ReadOnly)) {
QPainter wave_painter(&cached_waveform_);
// FIXME: Hardcoded color
wave_painter.setPen(QColor(64, 255, 160));
int drew = 0;
fs->seek(params.samples_to_bytes(ScreenToUnitRounded(0)));
for (int x=0; x<width() && !fs->atEnd(); x++) {
int samples_len = ScreenToUnitRounded(x+1) - ScreenToUnitRounded(x);
int max_read_size = params.samples_to_bytes(samples_len);
QByteArray read_buffer = fs->read(max_read_size);
// Detect whether we've reached EOF and recalculate sample count if so
if (read_buffer.size() < max_read_size) {
samples_len = params.bytes_to_samples(read_buffer.size());
}
QVector<AudioVisualWaveform::SamplePerChannel> samples = AudioVisualWaveform::SumSamples(reinterpret_cast<const float*>(read_buffer.constData()),
samples_len,
params.channel_count());
for (int i=0;i<params.channel_count();i++) {
AudioVisualWaveform::DrawSample(&wave_painter, samples, x, 0, height());
drew++;
}
}
cached_size_ = size();
cached_scale_ = GetScale();
cached_scroll_ = GetScroll();
fs->close();
}
delete fs;
}
QPainter p(this);
// Draw in/out points
DrawTimelinePoints(&p);
// Draw cached waveform pixmap
p.drawPixmap(0, 0, cached_waveform_);
CachedWaveformInfo wanted_info = {size(), GetScale(), GetScroll(), params};
for (int i=0; i<cached_waveform_.size(); i++) {
ActiveCache& cache = cached_waveform_[i];
int slice_start = width()/cached_waveform_.size() * i;
int slice_end = width()/cached_waveform_.size() * (i+1);
if (cache.info == wanted_info) {
// Draw pixmap
p.drawPixmap(slice_start, 0, cache.pixmap);
} else if (cache.caching_info != wanted_info) {
// Pixmap is obsolete, will need to draw again
// Delete any existing watcher so we don't receive the signal
delete cache.watcher;
// Queue a new background cache operation
cache.caching_info = wanted_info;
cache.watcher = new QFutureWatcher<QPixmap>();
connect(cache.watcher, &QFutureWatcher<QPixmap>::finished, this, &AudioWaveformView::BackgroundCacheFinished);
cache.watcher->setFuture(QtConcurrent::run(this,
&AudioWaveformView::DrawWaveform,
playback_->CreatePlaybackDevice(),
wanted_info,
slice_start,
slice_end));
}
}
// Draw playhead
p.setPen(PLAYHEAD_COLOR);
@@ -141,6 +121,53 @@ void AudioWaveformView::paintEvent(QPaintEvent *event)
p.drawLine(playhead_x, 0, playhead_x, height());
}
QPixmap AudioWaveformView::DrawWaveform(QIODevice* fs, CachedWaveformInfo info, int slice_start, int slice_end) const
{
QPixmap pixmap(slice_end - slice_start, info.size.height());
pixmap.fill(Qt::transparent);
if (fs->open(QFile::ReadOnly)) {
QPainter wave_painter(&pixmap);
// FIXME: Hardcoded color
wave_painter.setPen(QColor(64, 255, 160));
int drew = 0;
fs->seek(info.params.samples_to_bytes(ScreenToUnitRounded(slice_start)));
for (int x=slice_start; x<slice_end && !fs->atEnd(); x++) {
int samples_len = ScreenToUnitRounded(x+1) - ScreenToUnitRounded(x);
int max_read_size = info.params.samples_to_bytes(samples_len);
QByteArray read_buffer = fs->read(max_read_size);
// Detect whether we've reached EOF and recalculate sample count if so
if (read_buffer.size() < max_read_size) {
samples_len = info.params.bytes_to_samples(read_buffer.size());
}
QVector<AudioVisualWaveform::SamplePerChannel> samples = AudioVisualWaveform::SumSamples(reinterpret_cast<const float*>(read_buffer.constData()),
samples_len,
info.params.channel_count());
for (int i=0;i<info.params.channel_count();i++) {
AudioVisualWaveform::DrawSample(&wave_painter, samples, x - slice_start, 0, info.size.height());
drew++;
}
}
fs->close();
}
delete fs;
return pixmap;
}
void AudioWaveformView::BackendParamsChanged()
{
SetTimebase(playback_->GetParameters().time_base());
@@ -148,8 +175,40 @@ void AudioWaveformView::BackendParamsChanged()
void AudioWaveformView::ForceUpdate()
{
cached_size_ = QSize();
// Forces the cache to invalidate
for (int i=0; i<cached_waveform_.size(); i++) {
cached_waveform_[i].info.size = QSize();
}
update();
}
void AudioWaveformView::BackgroundCacheFinished()
{
// Retrieve sender
QFutureWatcher<QPixmap>* watcher = static_cast<QFutureWatcher<QPixmap>*>(sender());
// Determine index
int index = -1;
for (int i=0; i<cached_waveform_.size(); i++) {
if (cached_waveform_.at(i).watcher == watcher) {
index = i;
break;
}
}
if (index > -1) {
// Store generated pixmap
cached_waveform_[index].info = cached_waveform_[index].caching_info;
cached_waveform_[index].pixmap = watcher->result();
cached_waveform_[index].watcher = nullptr;
// Update with new pixmap
update();
}
// Clean up
delete watcher;
}
}
+33 -4
View File
@@ -21,6 +21,7 @@
#ifndef AUDIOWAVEFORMVIEW_H
#define AUDIOWAVEFORMVIEW_H
#include <QtConcurrent/QtConcurrent>
#include <QWidget>
#include "audio/audiovisualwaveform.h"
@@ -44,18 +45,46 @@ protected:
virtual void paintEvent(QPaintEvent* event) override;
private:
struct CachedWaveformInfo {
QSize size;
double scale;
int scroll;
AudioParams params;
bool operator==(const CachedWaveformInfo& rhs) const
{
return size == rhs.size
&& qFuzzyCompare(scale, rhs.scale)
&& scroll == rhs.scroll
&& params == rhs.params;
}
bool operator!=(const CachedWaveformInfo& rhs) const
{
return !(*this == rhs);
}
};
struct ActiveCache {
QPixmap pixmap;
CachedWaveformInfo info;
CachedWaveformInfo caching_info;
QFutureWatcher<QPixmap>* watcher = nullptr;
};
QPixmap DrawWaveform(QIODevice *fs, CachedWaveformInfo info, int slice_start, int slice_end) const;
AudioPlaybackCache *playback_;
QPixmap cached_waveform_;
QSize cached_size_;
double cached_scale_;
int cached_scroll_;
QVector<ActiveCache> cached_waveform_;
private slots:
void BackendParamsChanged();
void ForceUpdate();
void BackgroundCacheFinished();
};
}