all repos — mgba @ a57f911eccd740d3dbc5581cd766cee65f43dee1

mGBA Game Boy Advance Emulator

src/platform/qt/AudioDevice.cpp (view raw)

 1#include "AudioDevice.h"
 2
 3extern "C" {
 4#include "gba.h"
 5#include "gba-audio.h"
 6#include "gba-thread.h"
 7}
 8
 9using namespace QGBA;
10
11AudioDevice::AudioDevice(GBAThread* threadContext, QObject* parent)
12	: QIODevice(parent)
13	, m_context(threadContext)
14{
15	setOpenMode(ReadOnly);
16}
17
18void AudioDevice::setFormat(const QAudioFormat& format) {
19	// TODO: merge where the fudge rate exists
20	float fudgeRate = 16853760.0f / GBA_ARM7TDMI_FREQUENCY;
21	m_ratio = format.sampleRate() / (float) (m_context->gba->audio.sampleRate * fudgeRate);
22}
23
24qint64 AudioDevice::readData(char* data, qint64 maxSize) {
25	if (maxSize > 0xFFFFFFFF) {
26		maxSize = 0xFFFFFFFF;
27	}
28
29	if (!m_context->gba) {
30		return 0;
31	}
32
33	return GBAAudioResampleNN(&m_context->gba->audio, m_ratio, &m_drift, reinterpret_cast<GBAStereoSample*>(data), maxSize / sizeof(GBAStereoSample)) * sizeof(GBAStereoSample);
34}
35
36qint64 AudioDevice::writeData(const char*, qint64) {
37	return 0;
38}
39
40AudioThread::AudioThread(QObject* parent)
41	: QThread(parent)
42{
43	// Nothing to do
44}
45
46void AudioThread::setInput(GBAThread* input) {
47	m_input = input;
48}
49
50void AudioThread::shutdown() {
51	disconnect();
52	m_audioOutput->stop();
53	quit();
54}
55
56void AudioThread::pause() {
57	m_audioOutput->stop();
58}
59
60void AudioThread::resume() {
61	m_audioOutput->start(m_device);
62}
63
64void AudioThread::run() {
65	QAudioFormat format;
66	format.setSampleRate(44100);
67	format.setChannelCount(2);
68	format.setSampleSize(16);
69	format.setCodec("audio/pcm");
70	format.setByteOrder(QAudioFormat::LittleEndian);
71	format.setSampleType(QAudioFormat::SignedInt);
72
73	m_device = new AudioDevice(m_input);
74	m_audioOutput = new QAudioOutput(format);
75	m_audioOutput->setBufferSize(1024);
76	m_device->setFormat(m_audioOutput->format());
77	m_audioOutput->start(m_device);
78
79	exec();
80}