all repos — mgba @ ddd8c8db29a1a7418626ade2ea35d432825579f3

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(QObject* parent)
12	: QIODevice(parent)
13	, m_context(nullptr)
14{
15	setOpenMode(ReadOnly);
16}
17
18void AudioDevice::setFormat(const QAudioFormat& format) {
19	if (!GBAThreadHasStarted(m_context)) {
20		return;
21	}
22	GBAThreadInterrupt(m_context);
23	m_ratio = GBAAudioCalculateRatio(&m_context->gba->audio, m_context->fpsTarget, format.sampleRate());
24	GBAThreadContinue(m_context);
25}
26
27void AudioDevice::setInput(GBAThread* input) {
28	m_context = input;
29}
30
31qint64 AudioDevice::readData(char* data, qint64 maxSize) {
32	if (maxSize > 0xFFFFFFFF) {
33		maxSize = 0xFFFFFFFF;
34	}
35
36	if (!m_context->gba) {
37		return 0;
38	}
39
40	return GBAAudioResampleNN(&m_context->gba->audio, m_ratio, &m_drift, reinterpret_cast<GBAStereoSample*>(data), maxSize / sizeof(GBAStereoSample)) * sizeof(GBAStereoSample);
41}
42
43qint64 AudioDevice::writeData(const char*, qint64) {
44	return 0;
45}