all repos — mgba @ d020bf4f0a4532f21c3573fcab75e4a7b3f658f1

mGBA Game Boy Advance Emulator

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
11using namespace QGBA;
12
13AudioProcessor::AudioProcessor(QObject* parent)
14	: QObject(parent)
15	, m_audioOutput(nullptr)
16	, m_device(nullptr)
17{
18}
19
20void AudioProcessor::setInput(GBAThread* input) {
21	m_context = input;
22	if (m_device) {
23		m_device->setInput(input);
24		if (m_audioOutput) {
25			m_device->setFormat(m_audioOutput->format());
26		}
27	}
28}
29
30void AudioProcessor::start() {
31	if (!m_device) {
32		m_device = new AudioDevice(this);
33	}
34
35	if (!m_audioOutput) {
36		QAudioFormat format;
37		format.setSampleRate(44100);
38		format.setChannelCount(2);
39		format.setSampleSize(16);
40		format.setCodec("audio/pcm");
41		format.setByteOrder(QAudioFormat::LittleEndian);
42		format.setSampleType(QAudioFormat::SignedInt);
43
44		m_audioOutput = new QAudioOutput(format, this);
45	}
46
47	m_device->setInput(m_context);
48	m_device->setFormat(m_audioOutput->format());
49	m_audioOutput->setBufferSize(m_context->audioBuffers * 4);
50
51	m_audioOutput->start(m_device);
52}
53
54void AudioProcessor::pause() {
55	if (m_audioOutput) {
56		m_audioOutput->stop();
57	}
58}
59
60void AudioProcessor::setBufferSamples(int samples) {
61	QAudioFormat format = m_audioOutput->format();
62	m_audioOutput->setBufferSize(samples * format.channelCount() * format.sampleSize() / 8);
63}
64
65void AudioProcessor::inputParametersChanged() {
66	m_device->setFormat(m_audioOutput->format());
67}