all repos — mgba @ ddab7a7e44b16f75855f071fffacb73b701c80ff

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
 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(GBAThread* 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
 37void AudioProcessorQt::start() {
 38	if (!input()) {
 39		LOG(WARN) << tr("Can't start an audio processor without input");
 40		return;
 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(input()->audioBuffers * 4);
 63
 64	m_audioOutput->start(m_device);
 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}