Run audio on separate thread
Jeffrey Pfau jeffrey@endrift.com
Wed, 29 Jan 2014 01:39:40 -0800
6 files changed,
41 insertions(+),
15 deletions(-)
M
src/platform/qt/AudioDevice.cpp
→
src/platform/qt/AudioDevice.cpp
@@ -31,3 +31,19 @@
qint64 AudioDevice::writeData(const char*, qint64) { return 0; } + +AudioDevice::Thread::Thread(AudioDevice* device, QObject* parent) + : QThread(parent) + , m_device(device) +{ + // Nothing to do +} + +void AudioDevice::Thread::setOutput(QAudioOutput* output) { + m_audio = output; +} + +void AudioDevice::Thread::run() { + m_audio->start(m_device); + exec(); +}
M
src/platform/qt/AudioDevice.h
→
src/platform/qt/AudioDevice.h
@@ -2,7 +2,9 @@ #ifndef QGBA_AUDIO_DEVICE
#define QGBA_AUDIO_DEVICE #include <QAudioFormat> +#include <QAudioOutput> #include <QIODevice> +#include <QThread> struct GBAAudio;@@ -15,6 +17,20 @@ public:
AudioDevice(GBAAudio* audio, QObject* parent = 0); void setFormat(const QAudioFormat& format); + + class Thread : public QThread { + public: + Thread(AudioDevice* device, QObject* parent = 0); + + void setOutput(QAudioOutput* output); + + protected: + void run(); + + private: + QAudioOutput* m_audio; + AudioDevice* m_device; + }; protected: virtual qint64 readData(char* data, qint64 maxSize);
M
src/platform/qt/GameController.cpp
→
src/platform/qt/GameController.cpp
@@ -24,7 +24,7 @@ m_threadContext.sync.videoFrameWait = 0;
m_threadContext.sync.audioWait = 1; m_threadContext.startCallback = [] (GBAThread* context) { GameController* controller = static_cast<GameController*>(context->userData); - controller->setupAudio(&context->gba->audio); + controller->audioDeviceAvailable(&context->gba->audio); }; m_threadContext.cleanCallback = 0; m_threadContext.frameCallback = [] (GBAThread* context) {@@ -53,12 +53,3 @@ m_threadContext.fname = path.toLocal8Bit().constData();
GBAThreadStart(&m_threadContext); return true; } - -void GameController::setupAudio(GBAAudio* audio) { - if (m_audioContext) { - delete m_audioContext; - } - m_audioContext = new AudioDevice(audio); - - emit audioDeviceAvailable(m_audioContext); -}
M
src/platform/qt/GameController.h
→
src/platform/qt/GameController.h
@@ -26,7 +26,7 @@ ~GameController();
signals: void frameAvailable(const QImage&); - void audioDeviceAvailable(AudioDevice*); + void audioDeviceAvailable(GBAAudio*); public slots: bool loadGame(const QString& path);
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -11,7 +11,7 @@ m_controller = new GameController(this);
m_display = new Display(this); setCentralWidget(m_display); connect(m_controller, SIGNAL(frameAvailable(const QImage&)), m_display, SLOT(draw(const QImage&))); - connect(m_controller, SIGNAL(audioDeviceAvailable(AudioDevice*)), this, SLOT(setupAudio(AudioDevice*))); + connect(m_controller, SIGNAL(audioDeviceAvailable(GBAAudio*)), this, SLOT(setupAudio(GBAAudio*))); connect(actionOpen, SIGNAL(triggered()), this, SLOT(selectROM())); }@@ -23,7 +23,9 @@ m_controller->loadGame(filename);
} } -void Window::setupAudio(AudioDevice* device) { +void Window::setupAudio(GBAAudio* audio) { + AudioDevice* device = new AudioDevice(audio, this); + AudioDevice::Thread* thread = new AudioDevice::Thread(device, this); if (!m_audio) { QAudioFormat format; format.setSampleRate(44100);@@ -37,5 +39,6 @@ m_audio = new QAudioOutput(format, this);
m_audio->setBufferSize(1024); } device->setFormat(m_audio->format()); - m_audio->start(device); + thread->setOutput(m_audio); + thread->start(); }
M
src/platform/qt/Window.h
→
src/platform/qt/Window.h
@@ -21,7 +21,7 @@ public slots:
void selectROM(); private slots: - void setupAudio(AudioDevice*); + void setupAudio(GBAAudio*); private: QAudioOutput* m_audio;