all repos — mgba @ 463ce997397850e6ea5ca27fb5c806e2edca4e89

mGBA Game Boy Advance Emulator

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/core.h"
 14#include "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	m_audioOutput->setBufferSize(input()->core->getAudioBufferSize(input()->core) * 4);
 62
 63	m_audioOutput->start(m_device);
 64	return m_audioOutput->state() == QAudio::ActiveState;
 65}
 66
 67void AudioProcessorQt::pause() {
 68	if (m_audioOutput) {
 69		m_audioOutput->stop();
 70	}
 71}
 72
 73void AudioProcessorQt::setBufferSamples(int samples) {
 74	AudioProcessor::setBufferSamples(samples);
 75	if (m_audioOutput) {
 76		m_audioOutput->stop();
 77		m_audioOutput->setBufferSize(samples * 4);
 78		m_audioOutput->start(m_device);
 79	}
 80}
 81
 82void AudioProcessorQt::inputParametersChanged() {
 83	if (m_device) {
 84		m_device->setFormat(m_audioOutput->format());
 85	}
 86}
 87
 88void AudioProcessorQt::requestSampleRate(unsigned rate) {
 89	m_sampleRate = rate;
 90	if (m_device) {
 91		QAudioFormat format(m_audioOutput->format());
 92		format.setSampleRate(rate);
 93		m_device->setFormat(format);
 94	}
 95}
 96
 97unsigned AudioProcessorQt::sampleRate() const {
 98	if (!m_audioOutput) {
 99		return 0;
100	}
101	return m_audioOutput->format().sampleRate();
102}