fixed audio monitor regression

This commit is contained in:
itsmattkc
2019-01-22 12:57:00 +11:00
parent efc868d217
commit e7f05ff64b
5 changed files with 77 additions and 113 deletions
+28 -34
View File
@@ -164,47 +164,36 @@ void AudioSenderThread::run() {
lock.unlock(); lock.unlock();
} }
int AudioSenderThread::send_audio_to_output(int offset, int max) { int AudioSenderThread::send_audio_to_output(unsigned long offset, int max) {
// send audio to device // send audio to device
unsigned long actual_write = audio_io_device->write((const char*) audio_ibuffer+offset, max); unsigned long actual_write = audio_io_device->write((const char*) audio_ibuffer+offset, max);
unsigned long audio_ibuffer_limit = audio_ibuffer_read + actual_write; unsigned long audio_ibuffer_limit = audio_ibuffer_read + actual_write;
// send samples to audio monitor cache if (actual_write > 0) {
// TODO make this work for the footage viewer - currently, enabling it causes crash due to an ASSERT // average values and send to audio monitor
Sequence* s = nullptr; int channels = audio_output->format().channelCount();
/*if (panel_footage_viewer->playing) { unsigned long lim = offset + actual_write;
s = panel_footage_viewer->seq; QVector<double> averages;
}*/ averages.resize(channels);
if (panel_sequence_viewer->playing) { averages.fill(0);
s = panel_sequence_viewer->seq;
}
if (s != nullptr) {
if (panel_timeline->audio_monitor->sample_cache_offset == -1) {
panel_timeline->audio_monitor->sample_cache_offset = s->playhead;
}
int channel_count = av_get_channel_layout_nb_channels(s->audio_layout);
long sample_cache_playhead = panel_timeline->audio_monitor->sample_cache_offset + (panel_timeline->audio_monitor->sample_cache.size()/channel_count);
unsigned long next_buffer_offset, buffer_offset_adjusted;
int i;
unsigned long buffer_offset = get_buffer_offset_from_frame(s->frame_rate, sample_cache_playhead);
if (samples.size() != channel_count) samples.resize(channel_count);
samples.fill(0);
// TODO: I don't like this, but i'm not sure if there's a smarter way to do it for (int i=0;i<channels;i++) {
while (static_cast<unsigned long>(buffer_offset) < audio_ibuffer_limit) { averages[i] = 0;
sample_cache_playhead++;
next_buffer_offset = qMin(get_buffer_offset_from_frame(s->frame_rate, sample_cache_playhead), static_cast<unsigned long>(audio_ibuffer_limit));
while (buffer_offset < next_buffer_offset) {
for (i=0;i<samples.size();i++) {
buffer_offset_adjusted = buffer_offset%audio_ibuffer_size;
samples[i] = qMax(qAbs(qint16(((audio_ibuffer[buffer_offset_adjusted+1] & 0xFF) << 8) | (audio_ibuffer[buffer_offset_adjusted] & 0xFF))), samples[i]);
buffer_offset += 2;
}
}
panel_timeline->audio_monitor->sample_cache.append(samples);
buffer_offset = next_buffer_offset;
} }
int counter = 0;
qint16 sample;
for (unsigned long i=offset;i<lim;i+=2) {
sample = qint16(((audio_ibuffer[i+1] & 0xFF) << 8) | (audio_ibuffer[i] & 0xFF));
averages[counter%channels] += qAbs(double(sample)/32768.0);
counter++;
}
double divider = counter / channels;
for (int i=0;i<channels;i++) {
averages[i] = log_volume(1.0-(averages[i]/divider));
}
panel_timeline->audio_monitor->set_value(averages);
} }
memset(audio_ibuffer+offset, 0, actual_write); memset(audio_ibuffer+offset, 0, actual_write);
@@ -214,6 +203,11 @@ int AudioSenderThread::send_audio_to_output(int offset, int max) {
return actual_write; return actual_write;
} }
double log_volume(double linear) {
// expects a value between 0 and 1 (or more if amplifying)
return (qExp(linear)-1)/(M_E-1);
}
void int32_to_char_array(qint32 i, char* array) { void int32_to_char_array(qint32 i, char* array) {
memcpy(array, &i, 4); memcpy(array, &i, 4);
} }
+3 -1
View File
@@ -27,9 +27,11 @@ public slots:
void notifyReceiver(); void notifyReceiver();
private: private:
QVector<qint16> samples; QVector<qint16> samples;
int send_audio_to_output(int offset, int max); int send_audio_to_output(unsigned long offset, int max);
}; };
double log_volume(double linear);
extern QAudioOutput* audio_output; extern QAudioOutput* audio_output;
extern QIODevice* audio_io_device; extern QIODevice* audio_io_device;
extern AudioSenderThread* audio_thread; extern AudioSenderThread* audio_thread;
+1 -6
View File
@@ -693,7 +693,7 @@ void Effect::startEffect() {
void Effect::endEffect() { void Effect::endEffect() {
if (bound) glslProgram->release(); if (bound) glslProgram->release();
bound = false; bound = false;
} }
void Effect::process_image(double, uint8_t *, uint8_t *, int){} void Effect::process_image(double, uint8_t *, uint8_t *, int){}
@@ -912,8 +912,3 @@ qint16 mix_audio_sample(qint16 a, qint16 b) {
mixed_sample = qMax(qMin(mixed_sample, static_cast<qint32>(INT16_MAX)), static_cast<qint32>(INT16_MIN)); mixed_sample = qMax(qMin(mixed_sample, static_cast<qint32>(INT16_MAX)), static_cast<qint32>(INT16_MIN));
return static_cast<qint16>(mixed_sample); return static_cast<qint16>(mixed_sample);
} }
double log_volume(double linear) {
// expects a value between 0 and 1 (or more if amplifying)
return (qExp(linear)-1)/(M_E-1);
}
+38 -63
View File
@@ -9,6 +9,8 @@
#include <QLinearGradient> #include <QLinearGradient>
#include <QtMath> #include <QtMath>
#include <QDebug>
#define AUDIO_MONITOR_PEAK_HEIGHT 15 #define AUDIO_MONITOR_PEAK_HEIGHT 15
#define AUDIO_MONITOR_GAP 3 #define AUDIO_MONITOR_GAP 3
@@ -16,79 +18,52 @@ extern "C" {
#include "libavformat/avformat.h" #include "libavformat/avformat.h"
} }
AudioMonitor::AudioMonitor(QWidget *parent) : QWidget(parent) { AudioMonitor::AudioMonitor(QWidget *parent) :
reset(); QWidget(parent)
{
values.resize(2);
values.fill(1.0);
} }
void AudioMonitor::reset() { void AudioMonitor::set_value(const QVector<double> &ivalues) {
sample_cache_offset = -1; values = ivalues;
sample_cache.clear(); update();
update();
} }
void AudioMonitor::resizeEvent(QResizeEvent *e) { void AudioMonitor::resizeEvent(QResizeEvent *e) {
gradient = QLinearGradient(QPoint(0, rect().top()), QPoint(0, rect().bottom())); gradient = QLinearGradient(QPoint(0, rect().top()), QPoint(0, rect().bottom()));
gradient.setColorAt(0, Qt::red); gradient.setColorAt(0, Qt::red);
gradient.setColorAt(0.25, Qt::yellow); gradient.setColorAt(0.25, Qt::yellow);
gradient.setColorAt(1, Qt::green); gradient.setColorAt(1, Qt::green);
QWidget::resizeEvent(e); QWidget::resizeEvent(e);
} }
void AudioMonitor::paintEvent(QPaintEvent *) { void AudioMonitor::paintEvent(QPaintEvent *) {
if (sequence != nullptr) { if (sequence != nullptr) {
QPainter p(this); QPainter p(this);
int channel_x = AUDIO_MONITOR_GAP; int channel_x = AUDIO_MONITOR_GAP;
int channel_count = av_get_channel_layout_nb_channels(sequence->audio_layout); // int channel_count = av_get_channel_layout_nb_channels(sequence->audio_layout);
/*if (peaks.size() != channel_count) { int channel_count = values.size();
peaks.resize(channel_count); int channel_width = (width()/channel_count) - AUDIO_MONITOR_GAP;
peaks.fill(false); int i;
}*/ for (i=0;i<channel_count;i++) {
int channel_width = (width()/channel_count) - AUDIO_MONITOR_GAP; QRect r(channel_x, AUDIO_MONITOR_PEAK_HEIGHT + AUDIO_MONITOR_GAP, channel_width, height());
long playhead_offset = -1; p.fillRect(r, gradient);
int i;
for (i=0;i<channel_count;i++) {
QRect r(channel_x, AUDIO_MONITOR_PEAK_HEIGHT + AUDIO_MONITOR_GAP, channel_width, height());
p.fillRect(r, gradient);
if (sample_cache_offset != -1 && sample_cache.size() > 0) { bool peak = false;
playhead_offset = (sequence->playhead - sample_cache_offset) * channel_count;
if (playhead_offset >= 0 && playhead_offset < sample_cache.size()) {
double multiplier = 1 - qAbs((double) sample_cache.at(playhead_offset+i) / 32768.0); // 16-bit int divided to float
/*if (multiplier == (double) 0) {
peaks[i] = true;
}*/
r.setHeight(r.height()*multiplier);
} else {
reset();
}
}
/*QRect peak_rect(channel_x, 0, channel_width, AUDIO_MONITOR_PEAK_HEIGHT); r.setHeight(qRound(r.height()*(values.at(i))));
if (peaks.at(i)) {
p.fillRect(peak_rect, QColor(255, 0, 0));
} else {
p.fillRect(peak_rect, QColor(64, 0, 0));
}*/
p.fillRect(r, QColor(0, 0, 0, 160)); QRect peak_rect(channel_x, 0, channel_width, AUDIO_MONITOR_PEAK_HEIGHT);
if (peak) {
p.fillRect(peak_rect, QColor(255, 0, 0));
} else {
p.fillRect(peak_rect, QColor(64, 0, 0));
}
channel_x += channel_width + AUDIO_MONITOR_GAP; p.fillRect(r, QColor(0, 0, 0, 160));
}
if (playhead_offset > -1) { channel_x += channel_width + AUDIO_MONITOR_GAP;
// clean up used samples }
bool error = false; }
while (sample_cache_offset < sequence->playhead && !error) {
sample_cache_offset++;
for (i=0;i<channel_count;i++) {
if (sample_cache.isEmpty()) {
reset();
error = true;
break;
} else {
sample_cache.removeFirst();
}
}
}
}
}
} }
+7 -9
View File
@@ -5,24 +5,22 @@
class AudioMonitor : public QWidget class AudioMonitor : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit AudioMonitor(QWidget *parent = 0); explicit AudioMonitor(QWidget *parent = 0);
QVector<qint16> sample_cache; void set_value(const QVector<double>& values);
long sample_cache_offset;
void reset();
protected: protected:
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *); void resizeEvent(QResizeEvent *);
signals: signals:
public slots: public slots:
private: private:
QLinearGradient gradient; QLinearGradient gradient;
// QVector<bool> peaks; QVector<double> values;
}; };
#endif // AUDIOMONITOR_H #endif // AUDIOMONITOR_H