src/platform/qt/AudioProcessorQt.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 "AudioProcessorQt.h"
7
8#include "AudioDevice.h"
9
10#include <QAudioOutput>
11
12extern "C" {
13#include "gba-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 }
51
52 m_device->setInput(input());
53 m_device->setFormat(m_audioOutput->format());
54 m_audioOutput->setBufferSize(input()->audioBuffers * 4);
55
56 m_audioOutput->start(m_device);
57}
58
59void AudioProcessorQt::pause() {
60 if (m_audioOutput) {
61 m_audioOutput->stop();
62 }
63}
64
65void AudioProcessorQt::setBufferSamples(int samples) {
66 AudioProcessor::setBufferSamples(samples);
67 if (m_audioOutput) {
68 m_audioOutput->stop();
69 m_audioOutput->setBufferSize(samples * 4);
70 m_audioOutput->start(m_device);
71 }
72}
73
74void AudioProcessorQt::inputParametersChanged() {
75 if (m_device) {
76 m_device->setFormat(m_audioOutput->format());
77 }
78}