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