optimized waveform generation

This commit is contained in:
itsmattkc
2019-04-07 11:31:37 +10:00
parent 62cdd49080
commit a2d7025776
8 changed files with 120 additions and 82 deletions
+2 -59
View File
@@ -61,6 +61,7 @@
#include "timeline/track.h"
#include "global/math.h"
#include "project/projectfunctions.h"
#include "ui/waveform.h"
#define MAX_TEXT_WIDTH 20
#define TRANSITION_BETWEEN_RANGE 40
@@ -2784,64 +2785,6 @@ void TimelineView::leaveEvent(QEvent*) {
tooltip_timer.stop();
}
void draw_waveform(Clip* clip, const FootageStream* ms, long media_length, QPainter *p, const QRect& clip_rect, int waveform_start, int waveform_limit, double zoom) {
// audio channels multiplied by the number of bytes in a 16-bit audio sample
int divider = ms->audio_channels*2;
int channel_height = clip_rect.height()/ms->audio_channels;
int last_waveform_index = -1;
for (int i=waveform_start;i<waveform_limit;i++) {
int waveform_index = qFloor((((clip->clip_in() + (double(i)/zoom))/media_length) * ms->audio_preview.size())/divider)*divider;
if (clip->reversed()) {
waveform_index = ms->audio_preview.size() - waveform_index - (ms->audio_channels * 2);
}
if (last_waveform_index < 0) last_waveform_index = waveform_index;
for (int j=0;j<ms->audio_channels;j++) {
int mid = (olive::config.rectified_waveforms) ? clip_rect.top()+channel_height*(j+1) : clip_rect.top()+channel_height*j+(channel_height/2);
int offset_range_start = last_waveform_index+(j*2);
int offset_range_end = waveform_index+(j*2);
int offset_range_min = qMin(offset_range_start, offset_range_end);
int offset_range_max = qMax(offset_range_start, offset_range_end);
// Break if we're about to draw from an index that doesn't exist
if (offset_range_min+1 >= ms->audio_preview.size()) {
break;
}
qint8 min = qint8(qRound(double(ms->audio_preview.at(offset_range_min)) / 128.0 * (channel_height/2)));
qint8 max = qint8(qRound(double(ms->audio_preview.at(offset_range_min+1)) / 128.0 * (channel_height/2)));
if ((offset_range_max + 1) < ms->audio_preview.size()) {
// for waveform drawings, we get the maximum below 0 and maximum above 0 for this waveform range
for (int k=offset_range_min+2;k<=offset_range_max;k+=2) {
min = qMin(min, qint8(qRound(double(ms->audio_preview.at(k)) / 128.0 * (channel_height/2))));
max = qMax(max, qint8(qRound(double(ms->audio_preview.at(k+1)) / 128.0 * (channel_height/2))));
}
// draw waveforms
if (olive::config.rectified_waveforms) {
// rectified waveforms start from the bottom and draw upwards
p->drawLine(clip_rect.left()+i, mid, clip_rect.left()+i, mid - (max - min));
} else {
// non-rectified waveforms start from the center and draw outwards
p->drawLine(clip_rect.left()+i, mid+min, clip_rect.left()+i, mid+max);
}
}
}
last_waveform_index = waveform_index;
}
}
void TimelineView::draw_transition(QPainter& p, Clip* c, const QRect& clip_rect, QRect& text_rect, int transition_type) {
TransitionPtr t = (transition_type == kTransitionOpening) ? c->opening_transition : c->closing_transition;
if (t != nullptr) {
@@ -3033,7 +2976,7 @@ void TimelineView::paintEvent(QPaintEvent*) {
if (waveform_limit > 0) checkerboard_rect.setLeft(checkerboard_rect.left() + waveform_limit);
}
draw_waveform(clip, ms, media_length, &p, clip_rect, waveform_start, waveform_limit, ParentTimeline()->zoom);
olive::ui::DrawWaveform(clip, ms, media_length, &p, clip_rect, waveform_start, waveform_limit, ParentTimeline()->zoom);
}
}
if (draw_checkerboard) {
-2
View File
@@ -38,8 +38,6 @@
class Timeline;
void draw_waveform(Clip* clip, const FootageStream *ms, long media_length, QPainter* p, const QRect& clip_rect, int waveform_start, int waveform_limit, double zoom);
class TimelineView : public QWidget {
Q_OBJECT
public:
+2 -1
View File
@@ -61,6 +61,7 @@ extern "C" {
#include "rendering/shadergenerators.h"
#include "ui/viewerwindow.h"
#include "ui/menu.h"
#include "ui/waveform.h"
#include "mainwindow.h"
const int kTitleActionSafeVertexSize = 84;
@@ -436,7 +437,7 @@ void ViewerWidget::draw_waveform_func() {
wr.setX(wr.x() - waveform_scroll);
p.setPen(Qt::green);
draw_waveform(waveform_clip.get(), waveform_ms, waveform_clip->timeline_out(), &p, wr, waveform_scroll, width()+waveform_scroll, waveform_zoom);
olive::ui::DrawWaveform(waveform_clip.get(), waveform_ms, waveform_clip->timeline_out(), &p, wr, waveform_scroll, width()+waveform_scroll, waveform_zoom);
p.setPen(Qt::red);
int playhead_x = getScreenPointFromFrame(waveform_zoom, viewer->seq->playhead) - waveform_scroll;
p.drawLine(playhead_x, 0, playhead_x, height());
+70
View File
@@ -0,0 +1,70 @@
#include "waveform.h"
#include <QtMath>
#include "global/config.h"
int ConvertSampleToHeight(qint8 signed_sample, int channel_height) {
int half_channel_height = channel_height >> 1;
return (half_channel_height) + qRound(((double(signed_sample) / 128.0)) * half_channel_height);
}
void olive::ui::DrawWaveform(Clip* clip,
const FootageStream* ms,
long media_length,
QPainter *p,
const QRect& clip_rect,
int waveform_start,
int waveform_limit,
double zoom) {
int divider = ms->audio_channels;
int channel_height = clip_rect.height()/ms->audio_channels;
int last_waveform_index = -1;
for (int i=waveform_start;i<waveform_limit;i++) {
int waveform_index = qFloor((((clip->clip_in() + (double(i)/zoom))/media_length) * ms->audio_preview.size())/divider)*divider;
if (clip->reversed()) {
waveform_index = ms->audio_preview.size() - waveform_index - (ms->audio_channels * 2);
}
if (last_waveform_index < 0) last_waveform_index = waveform_index;
for (int j=0;j<ms->audio_channels;j++) {
int bottom = clip_rect.top()+channel_height*(j+1);
int offset_range_start = last_waveform_index+(j*2);
int offset_range_end = waveform_index+(j*2);
int offset_range_min = qMin(offset_range_start, offset_range_end);
int offset_range_max = qMax(offset_range_start, offset_range_end);
// Break if we're about to draw from an index that doesn't exist
if (offset_range_min+1 >= ms->audio_preview.size()) {
break;
}
int min = ConvertSampleToHeight(ms->audio_preview.at(offset_range_min), channel_height);
int max = ConvertSampleToHeight(ms->audio_preview.at(offset_range_min+1), channel_height);
if ((offset_range_max + 1) < ms->audio_preview.size()) {
// for waveform drawings, we get the maximum below 0 and maximum above 0 for this waveform range
for (int k=offset_range_min+2;k<=offset_range_max;k+=2) {
min = ConvertSampleToHeight(ms->audio_preview.at(k), channel_height);
max = ConvertSampleToHeight(ms->audio_preview.at(k+1), channel_height);
}
// draw waveforms
if (olive::config.rectified_waveforms) {
p->drawLine(clip_rect.left()+i, bottom, clip_rect.left()+i, bottom - (max - min));
} else {
p->drawLine(clip_rect.left()+i, bottom - min, clip_rect.left()+i, bottom - max);
}
}
}
last_waveform_index = waveform_index;
}
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef WAVEFORM_H
#define WAVEFORM_H
#include <QPainter>
#include <QRect>
#include "timeline/clip.h"
#include "project/footage.h"
namespace olive {
namespace ui {
void DrawWaveform(Clip* clip,
const FootageStream *ms,
long media_length,
QPainter* p,
const QRect& clip_rect,
int waveform_start,
int waveform_limit,
double zoom);
}
}
#endif // WAVEFORM_H