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 -1
View File
@@ -37,9 +37,10 @@ void AudioMonitor::resizeEvent(QResizeEvent *e) {
void AudioMonitor::paintEvent(QPaintEvent *) {
if (sequence != NULL) {
QPainter p(this);
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) {
peaks.resize(channel_count);
peaks.fill(false);
+3 -2
View File
@@ -16,23 +16,24 @@ QColor ColorButton::get_color() {
void ColorButton::set_color(QColor c) {
color = c;
set_button_color();
}
void ColorButton::set_button_color() {
QPalette pal = palette();
pal.setColor(QPalette::Button, color);
setPalette(pal);
emit color_changed();
}
void ColorButton::open_dialog() {
QColor old_color = color;
QColor new_color = QColorDialog::getColor(color, NULL);
if (old_color != new_color) {
if (new_color.isValid() && old_color != new_color) {
ColorCommand* command = new ColorCommand(this, old_color, new_color);
undo_stack.push(command);
set_button_color();
emit color_changed();
}
}
+1 -16
View File
@@ -3,20 +3,5 @@
#include <QFontDatabase>
FontCombobox::FontCombobox(QWidget* parent) : ComboBoxEx(parent) {
addItem(QFont().family());
}
void FontCombobox::showPopup() {
QString current = currentText();
clear();
QStringList fonts = QFontDatabase().families();
bool found = false;
for (int i=0;i<fonts.size();i++) {
addItem(fonts.at(i));
if (!found && fonts.at(i) == current) {
setCurrentIndex(i);
found = true;
}
}
QComboBox::showPopup();
addItems(QFontDatabase().families());
}
-2
View File
@@ -6,8 +6,6 @@
class FontCombobox : public ComboBoxEx {
public:
FontCombobox(QWidget* parent = 0);
protected:
void showPopup();
};
#endif // FONTCOMBOBOX_H
+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;
}
+1 -15
View File
@@ -13,24 +13,11 @@
struct Clip;
struct Sequence;
class AudioSenderThread : public QThread {
public:
AudioSenderThread();
void run();
QWaitCondition cond;
bool close;
QMutex lock;
private:
QVector<qint16> samples;
int send_audio_to_output(int offset, int max);
};
class ViewerWidget : public QOpenGLWidget, public QOpenGLFunctions
{
Q_OBJECT
public:
ViewerWidget(QWidget *parent = 0);
~ViewerWidget();
ViewerWidget(QWidget *parent = 0);
bool multithreaded;
bool force_audio;
@@ -43,7 +30,6 @@ protected:
// void resizeGL(int w, int h);
private:
QTimer retry_timer;
AudioSenderThread audio_sender_thread;
private slots:
void retry();
void deleteFunction();