diff --git a/app/widget/audiomonitor/audiomonitor.cpp b/app/widget/audiomonitor/audiomonitor.cpp index 6a196e095..10777ba00 100644 --- a/app/widget/audiomonitor/audiomonitor.cpp +++ b/app/widget/audiomonitor/audiomonitor.cpp @@ -37,7 +37,6 @@ QVector AudioMonitor::instances_; AudioMonitor::AudioMonitor(QWidget *parent) : QOpenGLWidget(parent), - file_(nullptr), waveform_(nullptr), cached_channels_(0) { @@ -70,15 +69,13 @@ void AudioMonitor::SetParams(const AudioParams ¶ms) void AudioMonitor::Stop() { - delete file_; - file_ = nullptr; waveform_ = nullptr; // We don't stop the update loop here so that the monitor can show a smooth fade out. The update // loop will stop itself since file_ and waveform_ are null. } -void AudioMonitor::PushBytes(const QByteArray &d) +void AudioMonitor::PushSampleBuffer(SampleBufferPtr d) { if (!params_.channel_count()) { return; @@ -86,9 +83,12 @@ void AudioMonitor::PushBytes(const QByteArray &d) QVector v(params_.channel_count(), 0); - BytesToSampleSummary(d, v); + AudioVisualWaveform::Sample summed = AudioVisualWaveform::SumSamples(d, 0, d->sample_count()); - PushValue(v); + AudioVisualWaveformSampleToInternalValues(summed, v); + + // Fill values because they get averaged out for smoothing + values_.fill(v); SetUpdateLoop(true); } @@ -236,13 +236,7 @@ void AudioMonitor::paintGL() delta_time *= abs_speed; } - if (file_) { - UpdateValuesFromFile(v, delta_time); - - if (file_->atEnd()) { - Stop(); - } - } else if (waveform_) { + if (waveform_) { UpdateValuesFromWaveform(v, delta_time); if (waveform_time_ >= waveform_->length()) { @@ -304,30 +298,6 @@ void AudioMonitor::mousePressEvent(QMouseEvent *) update(); } -void AudioMonitor::UpdateValuesFromFile(QVector& v, qint64 delta_time) -{ - // Convert ms to float seconds and determine how many bytes that is - qint64 bytes_to_read = params_.time_to_bytes(static_cast(delta_time) * 0.001); - - if (playback_speed_ < 0) { - // If reversing, jump back by the amount of bytes we're going to read - bytes_to_read = qMin(bytes_to_read, file_->pos()); - - file_->seek(file_->pos() - bytes_to_read); - } - - // Read bytes in from file - QByteArray b = file_->read(bytes_to_read); - - if (playback_speed_ < 0) { - // If reversing, head back to where we were before the read so that the next read starts - // from where we left off - file_->seek(file_->pos() - bytes_to_read); - } - - BytesToSampleSummary(b, v); -} - void AudioMonitor::UpdateValuesFromWaveform(QVector &v, qint64 delta_time) { // Delta time is provided in milliseconds, so we convert to seconds in rational @@ -335,18 +305,23 @@ void AudioMonitor::UpdateValuesFromWaveform(QVector &v, qint64 delta_tim AudioVisualWaveform::Sample sum = waveform_->GetSummaryFromTime(waveform_time_, length); - for (int i=0; i v.at(output_index)) { - v[output_index] = max; - } - } + AudioVisualWaveformSampleToInternalValues(sum, v); waveform_time_ += length; } +void AudioMonitor::AudioVisualWaveformSampleToInternalValues(const AudioVisualWaveform::Sample &in, QVector &out) +{ + for (int i=0; i out.at(output_index)) { + out[output_index] = max; + } + } +} + void AudioMonitor::PushValue(const QVector &v) { int lim = values_.size()-1; diff --git a/app/widget/audiomonitor/audiomonitor.h b/app/widget/audiomonitor/audiomonitor.h index e9bdf05b3..f532aa206 100644 --- a/app/widget/audiomonitor/audiomonitor.h +++ b/app/widget/audiomonitor/audiomonitor.h @@ -42,7 +42,7 @@ public: bool IsPlaying() const { - return file_ || waveform_; + return waveform_; } static void StartWaveformOnAll(const AudioVisualWaveform *waveform, const rational& start, int playback_speed) @@ -59,10 +59,10 @@ public: } } - static void PushBytesOnAll(const QByteArray &d) + static void PushSampleBufferOnAll(SampleBufferPtr d) { foreach (AudioMonitor *m, instances_) { - m->PushBytes(d); + m->PushSampleBuffer(d); } } @@ -71,7 +71,7 @@ public slots: void Stop(); - void PushBytes(const QByteArray& d); + void PushSampleBuffer(SampleBufferPtr samples); void StartWaveform(const AudioVisualWaveform *waveform, const rational& start, int playback_speed); @@ -83,10 +83,10 @@ protected: private: void SetUpdateLoop(bool e); - void UpdateValuesFromFile(QVector &v, qint64 delta_time); - void UpdateValuesFromWaveform(QVector &v, qint64 delta_time); + void AudioVisualWaveformSampleToInternalValues(const AudioVisualWaveform::Sample &in, QVector &out); + void PushValue(const QVector& v); void BytesToSampleSummary(const QByteArray& bytes, QVector& v); @@ -95,7 +95,6 @@ private: AudioParams params_; - QIODevice* file_; qint64 last_time_; const AudioVisualWaveform* waveform_; diff --git a/app/widget/viewer/viewer.cpp b/app/widget/viewer/viewer.cpp index 4fbb94846..b5720f927 100644 --- a/app/widget/viewer/viewer.cpp +++ b/app/widget/viewer/viewer.cpp @@ -571,7 +571,7 @@ void ViewerWidget::ReceivedAudioBufferForScrubbing() if (!AudioManager::instance()->PushToOutput(audio_processor_.to(), packed, &error)) { Core::instance()->ShowStatusBarMessage(tr("Audio scrubbing failed: %1").arg(error)); } - AudioMonitor::PushBytesOnAll(packed); + AudioMonitor::PushSampleBufferOnAll(samples); } } else { qCritical() << "Failed to process audio for scrubbing:" << r;