all repos — mgba @ 7f592f78e87d6bbbae650d51c0d08f6919d91ed3

mGBA Game Boy Advance Emulator

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

 1/* Copyright (c) 2013-2015 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "AudioDevice.h"
 7
 8extern "C" {
 9#include "gba/gba.h"
10#include "gba/audio.h"
11#include "gba/supervisor/thread.h"
12}
13
14using namespace QGBA;
15
16AudioDevice::AudioDevice(QObject* parent)
17	: QIODevice(parent)
18	, m_context(nullptr)
19	, m_drift(0)
20{
21	setOpenMode(ReadOnly);
22}
23
24void AudioDevice::setFormat(const QAudioFormat& format) {
25	if (!GBAThreadHasStarted(m_context)) {
26		return;
27	}
28#if RESAMPLE_LIBRARY == RESAMPLE_NN
29	GBAThreadInterrupt(m_context);
30	m_ratio = GBAAudioCalculateRatio(m_context->gba->audio.sampleRate, m_context->fpsTarget, format.sampleRate());
31	GBAThreadContinue(m_context);
32#elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
33	double fauxClock = GBAAudioCalculateRatio(1, m_context->fpsTarget, 1);
34	GBASyncLockAudio(&m_context->sync);
35	blip_set_rates(m_context->gba->audio.left, GBA_ARM7TDMI_FREQUENCY, format.sampleRate() * fauxClock);
36	blip_set_rates(m_context->gba->audio.right, GBA_ARM7TDMI_FREQUENCY, format.sampleRate() * fauxClock);
37#endif
38}
39
40void AudioDevice::setInput(GBAThread* input) {
41	m_context = input;
42}
43
44qint64 AudioDevice::readData(char* data, qint64 maxSize) {
45	if (maxSize > 0xFFFFFFFF) {
46		maxSize = 0xFFFFFFFF;
47	}
48
49	if (!m_context->gba) {
50		return 0;
51	}
52
53#if RESAMPLE_LIBRARY == RESAMPLE_NN
54	return GBAAudioResampleNN(&m_context->gba->audio, m_ratio, &m_drift, reinterpret_cast<GBAStereoSample*>(data), maxSize / sizeof(GBAStereoSample)) * sizeof(GBAStereoSample);
55#elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
56	int available = blip_samples_avail(m_context->gba->audio.left);
57	if (available > maxSize / sizeof(GBAStereoSample)) {
58		available = maxSize / sizeof(GBAStereoSample);
59	}
60	blip_read_samples(m_context->gba->audio.left, &reinterpret_cast<GBAStereoSample*>(data)->left, available, true);
61	blip_read_samples(m_context->gba->audio.right, &reinterpret_cast<GBAStereoSample*>(data)->right, available, true);
62	GBASyncConsumeAudio(&m_context->sync);
63	return available * sizeof(GBAStereoSample);
64#endif
65}
66
67qint64 AudioDevice::writeData(const char*, qint64) {
68	return 0;
69}