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 (!input()) {
37 return;
38 }
39
40 if (!m_device) {
41 m_device = new AudioDevice(this);
42 }
43
44 if (!m_audioOutput) {
45 QAudioFormat format;
46 format.setSampleRate(44100);
47 format.setChannelCount(2);
48 format.setSampleSize(16);
49 format.setCodec("audio/pcm");
50 format.setByteOrder(QAudioFormat::LittleEndian);
51 format.setSampleType(QAudioFormat::SignedInt);
52
53 m_audioOutput = new QAudioOutput(format, this);
54 m_audioOutput->setCategory("game");
55 }
56
57 m_device->setInput(input());
58 m_device->setFormat(m_audioOutput->format());
59 m_audioOutput->setBufferSize(input()->audioBuffers * 4);
60
61 m_audioOutput->start(m_device);
62}
63
64void AudioProcessorQt::pause() {
65 if (m_audioOutput) {
66 m_audioOutput->stop();
67 }
68}
69
70void AudioProcessorQt::setBufferSamples(int samples) {
71 AudioProcessor::setBufferSamples(samples);
72 if (m_audioOutput) {
73 m_audioOutput->stop();
74 m_audioOutput->setBufferSize(samples * 4);
75 m_audioOutput->start(m_device);
76 }
77}
78
79void AudioProcessorQt::inputParametersChanged() {
80 if (m_device) {
81 m_device->setFormat(m_audioOutput->format());
82 }
83}