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 "core/thread.h"
14
15using namespace QGBA;
16
17AudioProcessorQt::AudioProcessorQt(QObject* parent)
18 : AudioProcessor(parent)
19 , m_audioOutput(nullptr)
20 , m_device(nullptr)
21 , m_sampleRate(44100)
22{
23}
24
25void AudioProcessorQt::setInput(mCoreThread* 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
35bool AudioProcessorQt::start() {
36 if (!input()) {
37 LOG(QT, WARN) << tr("Can't start an audio processor without input");
38 return false;
39 }
40
41 if (!m_device) {
42 m_device = new AudioDevice(this);
43 }
44
45 if (!m_audioOutput) {
46 QAudioFormat format;
47 format.setSampleRate(m_sampleRate);
48 format.setChannelCount(2);
49 format.setSampleSize(16);
50 format.setCodec("audio/pcm");
51 format.setByteOrder(QAudioFormat::Endian(QSysInfo::ByteOrder));
52 format.setSampleType(QAudioFormat::SignedInt);
53
54 m_audioOutput = new QAudioOutput(format, this);
55 m_audioOutput->setCategory("game");
56 }
57
58 m_device->setInput(input());
59 m_device->setFormat(m_audioOutput->format());
60 m_audioOutput->setBufferSize(input()->core->getAudioBufferSize(input()->core) * 4);
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 AudioProcessor::setBufferSamples(samples);
74 if (m_audioOutput) {
75 m_audioOutput->stop();
76 m_audioOutput->setBufferSize(samples * 4);
77 m_audioOutput->start(m_device);
78 }
79}
80
81void AudioProcessorQt::inputParametersChanged() {
82 if (m_device) {
83 m_device->setFormat(m_audioOutput->format());
84 }
85}
86
87void AudioProcessorQt::requestSampleRate(unsigned rate) {
88 m_sampleRate = rate;
89 if (m_device) {
90 QAudioFormat format(m_audioOutput->format());
91 format.setSampleRate(rate);
92 m_device->setFormat(format);
93 }
94}
95
96unsigned AudioProcessorQt::sampleRate() const {
97 if (!m_audioOutput) {
98 return 0;
99 }
100 return m_audioOutput->format().sampleRate();
101}