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 , m_audioOutput(nullptr)
21 , m_device(nullptr)
22 , m_sampleRate(44100)
23{
24}
25
26void AudioProcessorQt::setInput(mCoreThread* 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
36bool AudioProcessorQt::start() {
37 if (!input()) {
38 LOG(QT, WARN) << tr("Can't start an audio processor without input");
39 return false;
40 }
41
42 if (!m_device) {
43 m_device = new AudioDevice(this);
44 }
45
46 if (!m_audioOutput) {
47 QAudioFormat format;
48 format.setSampleRate(m_sampleRate);
49 format.setChannelCount(2);
50 format.setSampleSize(16);
51 format.setCodec("audio/pcm");
52 format.setByteOrder(QAudioFormat::Endian(QSysInfo::ByteOrder));
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
62 m_audioOutput->start(m_device);
63 return m_audioOutput->state() == QAudio::ActiveState;
64}
65
66void AudioProcessorQt::pause() {
67 if (m_audioOutput) {
68 m_audioOutput->stop();
69 }
70}
71
72void AudioProcessorQt::setBufferSamples(int samples) {
73}
74
75void AudioProcessorQt::inputParametersChanged() {
76 if (m_device) {
77 m_device->setFormat(m_audioOutput->format());
78 }
79}
80
81void AudioProcessorQt::requestSampleRate(unsigned rate) {
82 m_sampleRate = rate;
83 if (m_device) {
84 QAudioFormat format(m_audioOutput->format());
85 format.setSampleRate(rate);
86 m_device->setFormat(format);
87 }
88}
89
90unsigned AudioProcessorQt::sampleRate() const {
91 if (!m_audioOutput) {
92 return 0;
93 }
94 return m_audioOutput->format().sampleRate();
95}