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();
}
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
unsigned long actual_write = audio_io_device->write((const char*) audio_ibuffer+offset, max);
unsigned long audio_ibuffer_limit = audio_ibuffer_read + actual_write;
// send samples to audio monitor cache
// TODO make this work for the footage viewer - currently, enabling it causes crash due to an ASSERT
Sequence* s = nullptr;
/*if (panel_footage_viewer->playing) {
s = panel_footage_viewer->seq;
}*/
if (panel_sequence_viewer->playing) {
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);
if (actual_write > 0) {
// average values and send to audio monitor
int channels = audio_output->format().channelCount();
unsigned long lim = offset + actual_write;
QVector<double> averages;
averages.resize(channels);
averages.fill(0);
// TODO: I don't like this, but i'm not sure if there's a smarter way to do it
while (static_cast<unsigned long>(buffer_offset) < audio_ibuffer_limit) {
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;
for (int i=0;i<channels;i++) {
averages[i] = 0;
}
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);
@@ -214,6 +203,11 @@ int AudioSenderThread::send_audio_to_output(int offset, int max) {
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) {
memcpy(array, &i, 4);
}
+3 -1
View File
@@ -27,9 +27,11 @@ public slots:
void notifyReceiver();
private:
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 QIODevice* audio_io_device;
extern AudioSenderThread* audio_thread;
+1 -6
View File
@@ -693,7 +693,7 @@ void Effect::startEffect() {
void Effect::endEffect() {
if (bound) glslProgram->release();
bound = false;
bound = false;
}
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));
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 <QtMath>
#include <QDebug>
#define AUDIO_MONITOR_PEAK_HEIGHT 15
#define AUDIO_MONITOR_GAP 3
@@ -16,79 +18,52 @@ extern "C" {
#include "libavformat/avformat.h"
}
AudioMonitor::AudioMonitor(QWidget *parent) : QWidget(parent) {
reset();
AudioMonitor::AudioMonitor(QWidget *parent) :
QWidget(parent)
{
values.resize(2);
values.fill(1.0);
}
void AudioMonitor::reset() {
sample_cache_offset = -1;
sample_cache.clear();
update();
void AudioMonitor::set_value(const QVector<double> &ivalues) {
values = ivalues;
update();
}
void AudioMonitor::resizeEvent(QResizeEvent *e) {
gradient = QLinearGradient(QPoint(0, rect().top()), QPoint(0, rect().bottom()));
gradient.setColorAt(0, Qt::red);
gradient.setColorAt(0.25, Qt::yellow);
gradient.setColorAt(1, Qt::green);
QWidget::resizeEvent(e);
gradient = QLinearGradient(QPoint(0, rect().top()), QPoint(0, rect().bottom()));
gradient.setColorAt(0, Qt::red);
gradient.setColorAt(0.25, Qt::yellow);
gradient.setColorAt(1, Qt::green);
QWidget::resizeEvent(e);
}
void AudioMonitor::paintEvent(QPaintEvent *) {
if (sequence != nullptr) {
QPainter p(this);
int channel_x = AUDIO_MONITOR_GAP;
int channel_count = av_get_channel_layout_nb_channels(sequence->audio_layout);
/*if (peaks.size() != channel_count) {
peaks.resize(channel_count);
peaks.fill(false);
}*/
int channel_width = (width()/channel_count) - AUDIO_MONITOR_GAP;
long playhead_offset = -1;
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 (sequence != nullptr) {
QPainter p(this);
int channel_x = AUDIO_MONITOR_GAP;
// int channel_count = av_get_channel_layout_nb_channels(sequence->audio_layout);
int channel_count = values.size();
int channel_width = (width()/channel_count) - AUDIO_MONITOR_GAP;
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) {
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();
}
}
bool peak = false;
/*QRect peak_rect(channel_x, 0, channel_width, AUDIO_MONITOR_PEAK_HEIGHT);
if (peaks.at(i)) {
p.fillRect(peak_rect, QColor(255, 0, 0));
} else {
p.fillRect(peak_rect, QColor(64, 0, 0));
}*/
r.setHeight(qRound(r.height()*(values.at(i))));
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;
}
if (playhead_offset > -1) {
// 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();
}
}
}
}
}
p.fillRect(r, QColor(0, 0, 0, 160));
channel_x += channel_width + AUDIO_MONITOR_GAP;
}
}
}
+7 -9
View File
@@ -5,24 +5,22 @@
class AudioMonitor : public QWidget
{
Q_OBJECT
Q_OBJECT
public:
explicit AudioMonitor(QWidget *parent = 0);
QVector<qint16> sample_cache;
long sample_cache_offset;
void reset();
explicit AudioMonitor(QWidget *parent = 0);
void set_value(const QVector<double>& values);
protected:
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
signals:
public slots:
private:
QLinearGradient gradient;
// QVector<bool> peaks;
QLinearGradient gradient;
QVector<double> values;
};
#endif // AUDIOMONITOR_H