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