src/platform/qt/AudioProcessor.cpp (view raw)
1#include "AudioProcessor.h"
2
3#include "AudioDevice.h"
4
5#include <QAudioOutput>
6
7extern "C" {
8#include "gba-thread.h"
9}
10
11#include <cassert>
12
13using namespace QGBA;
14
15AudioProcessor::AudioProcessor(QObject* parent)
16 : QObject(parent)
17 , m_audioOutput(nullptr)
18 , m_device(nullptr)
19{
20}
21
22void AudioProcessor::setInput(GBAThread* input) {
23 m_context = input;
24 if (m_device) {
25 m_device->setInput(input);
26 if (m_audioOutput) {
27 m_device->setFormat(m_audioOutput->format());
28 }
29 }
30}
31
32void AudioProcessor::start() {
33 if (!m_device) {
34 m_device = new AudioDevice(this);
35 }
36
37 if (!m_audioOutput) {
38 QAudioFormat format;
39 format.setSampleRate(44100);
40 format.setChannelCount(2);
41 format.setSampleSize(16);
42 format.setCodec("audio/pcm");
43 format.setByteOrder(QAudioFormat::LittleEndian);
44 format.setSampleType(QAudioFormat::SignedInt);
45
46 m_audioOutput = new QAudioOutput(format, this);
47 }
48
49 m_device->setInput(m_context);
50 m_device->setFormat(m_audioOutput->format());
51
52 assert(m_audioOutput->thread() == thread());
53 m_audioOutput->start(m_device);
54}
55
56void AudioProcessor::pause() {
57 m_audioOutput->stop();
58}