audiomonitor: use samplebuffer directly
Pushing bytes was broken and pretty much pointless anyway, this should be faster and easier to use too
This commit is contained in:
@@ -37,7 +37,6 @@ QVector<AudioMonitor*> 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<double> 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<double>& 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<double>(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<double> &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<double> &v, qint64 delta_tim
|
||||
|
||||
AudioVisualWaveform::Sample sum = waveform_->GetSummaryFromTime(waveform_time_, length);
|
||||
|
||||
for (int i=0; i<sum.size(); i++) {
|
||||
float max = qMax(qAbs(sum.at(i).min), qAbs(sum.at(i).max));
|
||||
|
||||
int output_index = i%v.size();
|
||||
if (max > v.at(output_index)) {
|
||||
v[output_index] = max;
|
||||
}
|
||||
}
|
||||
AudioVisualWaveformSampleToInternalValues(sum, v);
|
||||
|
||||
waveform_time_ += length;
|
||||
}
|
||||
|
||||
void AudioMonitor::AudioVisualWaveformSampleToInternalValues(const AudioVisualWaveform::Sample &in, QVector<double> &out)
|
||||
{
|
||||
for (int i=0; i<in.size(); i++) {
|
||||
float max = qMax(qAbs(in.at(i).min), qAbs(in.at(i).max));
|
||||
|
||||
int output_index = i%out.size();
|
||||
if (max > out.at(output_index)) {
|
||||
out[output_index] = max;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AudioMonitor::PushValue(const QVector<double> &v)
|
||||
{
|
||||
int lim = values_.size()-1;
|
||||
|
||||
@@ -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<double> &v, qint64 delta_time);
|
||||
|
||||
void UpdateValuesFromWaveform(QVector<double> &v, qint64 delta_time);
|
||||
|
||||
void AudioVisualWaveformSampleToInternalValues(const AudioVisualWaveform::Sample &in, QVector<double> &out);
|
||||
|
||||
void PushValue(const QVector<double>& v);
|
||||
|
||||
void BytesToSampleSummary(const QByteArray& bytes, QVector<double>& v);
|
||||
@@ -95,7 +95,6 @@ private:
|
||||
|
||||
AudioParams params_;
|
||||
|
||||
QIODevice* file_;
|
||||
qint64 last_time_;
|
||||
|
||||
const AudioVisualWaveform* waveform_;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user