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