waveformview: implemented basic waveformview display

This commit is contained in:
itsmattkc
2020-03-14 01:22:03 +11:00
parent f0e6f9a5f9
commit 73d693f2d5
6 changed files with 176 additions and 51 deletions
@@ -29,11 +29,10 @@
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include "audio/sumsamples.h"
#include "common/clamp.h"
#include "common/qtutils.h"
#include "config/config.h"
#include "node/block/transition/transition.h"
#include "widget/viewer/waveformview.h"
TimelineViewBlockItem::TimelineViewBlockItem(Block *block, QGraphicsItem* parent) :
TimelineViewRect(parent),
@@ -94,47 +93,12 @@ void TimelineViewBlockItem::paint(QPainter *painter, const QStyleOptionGraphicsI
QByteArray w = wave_file.readAll();
// FIXME: Hardcoded channel count
int channels = 2;
const SampleSummer::Sum* samples = reinterpret_cast<const SampleSummer::Sum*>(w.constData());
int nb_samples = w.size() / sizeof(SampleSummer::Sum);
int sample_index, next_sample_index = 0;
QVector<SampleSummer::Sum> summary;
int summary_index = -1;
int channel_height = rect().height() / channels;
int channel_half_height = channel_height / 2;
for (int i=0;i<rect().width();i++) {
sample_index = next_sample_index;
if (sample_index == nb_samples) {
break;
}
next_sample_index = qMin(nb_samples,
qFloor(static_cast<double>(SampleSummer::kSumSampleRate) * static_cast<double>(i+1) / this->GetScale()) * channels);
if (summary_index != sample_index) {
summary = SampleSummer::ReSumSamples(&samples[sample_index],
qMax(channels, next_sample_index - sample_index),
channels);
summary_index = sample_index;
}
int line_x = i + rect().x();
for (int j=0;j<summary.size();j++) {
int channel_mid = rect().y() + channel_height * j + channel_half_height;
painter->drawLine(line_x,
channel_mid + clamp(qRound(summary.at(j).min * channel_half_height), -channel_half_height, channel_half_height),
line_x,
channel_mid + clamp(qRound(summary.at(j).max * channel_half_height), -channel_half_height, channel_half_height));
}
}
WaveformView::DrawWaveform(painter,
rect().toRect(),
this->GetScale(),
reinterpret_cast<const SampleSummer::Sum*>(w.constData()),
w.size() / sizeof(SampleSummer::Sum),
2);
wave_file.close();
}