all repos — mgba @ e27c10ab78c8109f2c2056854a8f871edf69dc3c

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}
 7
 8using namespace QGBA;
 9
10AudioDevice::AudioDevice(GBAAudio* audio, QObject* parent)
11	: QIODevice(parent)
12	, m_audio(audio)
13{
14	setOpenMode(ReadOnly);
15}
16
17void AudioDevice::setFormat(const QAudioFormat& format) {
18	// TODO: merge where the fudge rate exists
19	float fudgeRate = 16853760.0f / GBA_ARM7TDMI_FREQUENCY;
20	m_ratio = format.sampleRate() / (float) (m_audio->sampleRate * fudgeRate);
21}
22
23qint64 AudioDevice::readData(char* data, qint64 maxSize) {
24	if (maxSize > 0xFFFFFFFF) {
25		maxSize = 0xFFFFFFFF;
26	}
27
28	return GBAAudioResampleNN(m_audio, m_ratio, &m_drift, reinterpret_cast<GBAStereoSample*>(data), maxSize / sizeof(GBAStereoSample)) * sizeof(GBAStereoSample);
29}
30
31qint64 AudioDevice::writeData(const char*, qint64) {
32	return 0;
33}
34
35AudioThread::AudioThread(QObject* parent)
36	: QThread(parent)
37{
38	// Nothing to do
39}
40
41void AudioThread::setInput(GBAAudio* input) {
42	m_input = input;
43}
44
45void AudioThread::shutdown() {
46	m_audioOutput->stop();
47}
48
49void AudioThread::run() {
50	QAudioFormat format;
51	format.setSampleRate(44100);
52	format.setChannelCount(2);
53	format.setSampleSize(16);
54	format.setCodec("audio/pcm");
55	format.setByteOrder(QAudioFormat::LittleEndian);
56	format.setSampleType(QAudioFormat::SignedInt);
57
58	AudioDevice device(m_input);
59	m_audioOutput = new QAudioOutput(format);
60	m_audioOutput->setBufferSize(1024);
61	device.setFormat(m_audioOutput->format());
62	m_audioOutput->start(&device);
63
64	exec();
65}