#include "waveformview.h" #include #include #include #include "common/clamp.h" WaveformView::WaveformView(QWidget *parent) : TimelineScaledWidget(parent), backend_(nullptr) { setAutoFillBackground(true); setBackgroundRole(QPalette::Base); } void WaveformView::SetBackend(AudioRenderBackend *backend) { if (backend_) { disconnect(backend_, &AudioRenderBackend::QueueComplete, this, static_cast(&WaveformView::update)); } backend_ = backend; if (backend_) { connect(backend_, &AudioRenderBackend::QueueComplete, this, static_cast(&WaveformView::update)); } update(); } void WaveformView::DrawWaveform(QPainter *painter, const QRect& rect, const double& scale, const SampleSummer::Sum* samples, int nb_samples, int channels) { int sample_index, next_sample_index = 0; QVector summary; int summary_index = -1; int channel_height = rect.height() / channels; int channel_half_height = channel_height / 2; for (int i=0;i(SampleSummer::kSumSampleRate) * static_cast(i+1) / scale) * 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;jdrawLine(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)); } } } void WaveformView::SetScroll(int scroll) { scroll_ = scroll; qDebug() << "Got scroll" << scroll_; update(); } void WaveformView::paintEvent(QPaintEvent *event) { QWidget::paintEvent(event); if (!backend_ || backend_->CachePathName().isEmpty() || !backend_->params().is_valid()) { return; } const AudioRenderingParams& params = backend_->params(); QFile fs(backend_->CachePathName()); if (fs.open(QFile::ReadOnly)) { QPainter p(this); p.setPen(Qt::green); int channel_height = height() / params.channel_count(); int channel_half_height = channel_height / 2; int drew = 0; fs.seek(params.samples_to_bytes(GetSampleIndexFromPixel(0))); for (int x=0; x samples = SampleSummer::SumSamples(reinterpret_cast(read_buffer.constData()), samples_len, params.channel_count()); for (int i=0;i(channel_half_height), x, channel_mid + samples.at(i).max * static_cast(channel_half_height)); drew++; } } fs.close(); } } void WaveformView::ScaleChangedEvent(const double &) { update(); } int WaveformView::GetSampleIndexFromPixel(int x) const { qDebug() << "X was" << x << "scroll was" << scroll_ << "=" << (x + scroll_); return qRound(static_cast(x + scroll_) * static_cast(backend_->params().sample_rate()) / GetScale()); }