all repos — mgba @ 74c4cbe33e5bc67ef2b0eb1cb6bd518270735e63

mGBA Game Boy Advance Emulator

src/platform/qt/AudioProcessorQt.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 "AudioProcessorQt.h"
 7
 8#include "AudioDevice.h"
 9#include "LogController.h"
10
11#include <QAudioOutput>
12
13extern "C" {
14#include "gba/supervisor/thread.h"
15}
16
17using namespace QGBA;
18
19AudioProcessorQt::AudioProcessorQt(QObject* parent)
20	: AudioProcessor(parent)
21	, m_audioOutput(nullptr)
22	, m_device(nullptr)
23{
24}
25
26void AudioProcessorQt::setInput(GBAThread* input) {
27	AudioProcessor::setInput(input);
28	if (m_device) {
29		m_device->setInput(input);
30		if (m_audioOutput) {
31			m_device->setFormat(m_audioOutput->format());
32		}
33	}
34}
35
36void AudioProcessorQt::start() {
37	if (!input()) {
38		LOG(WARN) << tr("Can't start an audio processor without input");
39		return;
40	}
41
42	if (!m_device) {
43		m_device = new AudioDevice(this);
44	}
45
46	if (!m_audioOutput) {
47		QAudioFormat format;
48		format.setSampleRate(44100);
49		format.setChannelCount(2);
50		format.setSampleSize(16);
51		format.setCodec("audio/pcm");
52		format.setByteOrder(QAudioFormat::LittleEndian);
53		format.setSampleType(QAudioFormat::SignedInt);
54
55		m_audioOutput = new QAudioOutput(format, this);
56		m_audioOutput->setCategory("game");
57	}
58
59	m_device->setInput(input());
60	m_device->setFormat(m_audioOutput->format());
61	m_audioOutput->setBufferSize(input()->audioBuffers * 4);
62
63	m_audioOutput->start(m_device);
64}
65
66void AudioProcessorQt::pause() {
67	if (m_audioOutput) {
68		m_audioOutput->stop();
69	}
70}
71
72void AudioProcessorQt::setBufferSamples(int samples) {
73	AudioProcessor::setBufferSamples(samples);
74	if (m_audioOutput) {
75		m_audioOutput->stop();
76		m_audioOutput->setBufferSize(samples * 4);
77		m_audioOutput->start(m_device);
78	}
79}
80
81void AudioProcessorQt::inputParametersChanged() {
82	if (m_device) {
83		m_device->setFormat(m_audioOutput->format());
84	}
85}
86
87unsigned AudioProcessorQt::sampleRate() const {
88	if (!m_audioOutput) {
89		return 0;
90	}
91	return m_audioOutput->format().sampleRate();
92}