vastly improved audio

This commit is contained in:
itsmattkc
2018-08-14 20:25:56 +10:00
parent d549586761
commit aa1096d86a
19 changed files with 237 additions and 146 deletions
+2 -76
View File
@@ -14,6 +14,7 @@
#include <QDebug>
#include <QPainter>
#include <QAudioOutput>
#include <QtMath>
extern "C" {
@@ -31,20 +32,11 @@ ViewerWidget::ViewerWidget(QWidget *parent) :
format.setDepthBufferSize(24);
setFormat(format);
// start audio sending thread
audio_sender_thread.start();
// error handler - retries after 250ms if we couldn't get the entire image
retry_timer.setInterval(250);
connect(&retry_timer, SIGNAL(timeout()), this, SLOT(retry()));
}
ViewerWidget::~ViewerWidget() {
audio_sender_thread.close = true;
audio_sender_thread.cond.wakeAll();
audio_sender_thread.wait();
}
void ViewerWidget::deleteFunction() {
// destroy all textures as well
for (int i=0;i<sequence->clip_count();i++) {
@@ -229,7 +221,7 @@ void ViewerWidget::compose_sequence(QVector<Clip*>& nests, bool render_audio) {
void ViewerWidget::paintGL() {
bool loop = true;
while (loop) {
while (loop) {
loop = false;
texture_failed = false;
@@ -241,11 +233,6 @@ void ViewerWidget::paintGL() {
QVector<Clip*> nests;
compose_sequence(nests, (panel_timeline->playing || force_audio));
// send audio to IO device
if (panel_timeline->playing) {
audio_sender_thread.cond.wakeAll();
}
if (texture_failed) {
if (multithreaded) {
retry_timer.start();
@@ -256,64 +243,3 @@ void ViewerWidget::paintGL() {
}
}
}
AudioSenderThread::AudioSenderThread() : close(false) {
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}
void AudioSenderThread::run() {
lock.lock();
while (true) {
cond.wait(&lock);
if (close) {
break;
} else {
int written_bytes = 0;
int adjusted_read_index = audio_ibuffer_read%audio_ibuffer_size;
int max_write = audio_ibuffer_size - adjusted_read_index;
int actual_write = send_audio_to_output(adjusted_read_index, max_write);
written_bytes += actual_write;
if (actual_write == max_write) {
// got all the bytes, write again
written_bytes += send_audio_to_output(0, audio_ibuffer_size);
}
qDebug() << "WB:" << written_bytes;
}
}
lock.unlock();
}
int AudioSenderThread::send_audio_to_output(int offset, int max) {
// send audio to device
int actual_write = audio_io_device->write((const char*) audio_ibuffer+offset, max);
audio_ibuffer_read += actual_write;
// send samples to audio monitor cache
if (panel_timeline->ui->audio_monitor->sample_cache_offset == -1) {
panel_timeline->ui->audio_monitor->sample_cache_offset = panel_timeline->playhead;
}
long sample_cache_playhead = panel_timeline->ui->audio_monitor->sample_cache_offset + panel_timeline->ui->audio_monitor->sample_cache.size();
int next_buffer_offset, buffer_offset_adjusted, i;
int buffer_offset = get_buffer_offset_from_frame(sample_cache_playhead);
samples.resize(av_get_channel_layout_nb_channels(sequence->audio_layout));
samples.fill(0);
while (buffer_offset < audio_ibuffer_read) {
sample_cache_playhead++;
next_buffer_offset = qMin(get_buffer_offset_from_frame(sample_cache_playhead), audio_ibuffer_read);
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->ui->audio_monitor->sample_cache.append(samples);
buffer_offset = next_buffer_offset;
}
memset(audio_ibuffer+offset, 0, actual_write);
return actual_write;
}