all repos — mgba @ e9a2b2a57c4f478fb3600fe9d87e1b97804d73de

mGBA Game Boy Advance Emulator

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

 1/* Copyright (c) 2013-2014 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.h"
10#include "gba-audio.h"
11#include "gba-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	GBAThreadInterrupt(m_context);
29	m_ratio = GBAAudioCalculateRatio(m_context->gba->audio.sampleRate, m_context->fpsTarget, format.sampleRate());
30	GBAThreadContinue(m_context);
31}
32
33void AudioDevice::setInput(GBAThread* input) {
34	m_context = input;
35}
36
37qint64 AudioDevice::readData(char* data, qint64 maxSize) {
38	if (maxSize > 0xFFFFFFFF) {
39		maxSize = 0xFFFFFFFF;
40	}
41
42	if (!m_context->gba) {
43		return 0;
44	}
45
46	return GBAAudioResampleNN(&m_context->gba->audio, m_ratio, &m_drift, reinterpret_cast<GBAStereoSample*>(data), maxSize / sizeof(GBAStereoSample)) * sizeof(GBAStereoSample);
47}
48
49qint64 AudioDevice::writeData(const char*, qint64) {
50	return 0;
51}