Qt: Port AudioProcessorQt to Qt6

This commit is contained in:
Vicki Pfau 2024-03-24 15:55:39 -07:00
parent cf0d8fdc28
commit 3f21de2b7c
3 changed files with 39 additions and 10 deletions

View File

@ -9,6 +9,9 @@
#include "LogController.h"
#include <QAudioOutput>
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
#include <QMediaDevices>
#endif
#include <mgba/core/core.h>
#include <mgba/core/thread.h>
@ -31,10 +34,13 @@ void AudioProcessorQt::setInput(std::shared_ptr<CoreController> controller) {
}
void AudioProcessorQt::stop() {
if (m_audioOutput) {
m_audioOutput->stop();
m_audioOutput.reset();
}
if (m_device) {
m_device.reset();
}
pause();
AudioProcessor::stop();
}
@ -52,25 +58,36 @@ bool AudioProcessorQt::start() {
QAudioFormat format;
format.setSampleRate(m_sampleRate);
format.setChannelCount(2);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::Endian(QSysInfo::ByteOrder));
format.setSampleType(QAudioFormat::SignedInt);
m_audioOutput = new QAudioOutput(format, this);
m_audioOutput = std::make_unique<QAudioOutput>(format);
m_audioOutput->setCategory("game");
#else
format.setSampleFormat(QAudioFormat::Int16);
QAudioDevice device(QMediaDevices::defaultAudioOutput());
m_audioOutput = std::make_unique<QAudioSink>(device, format);
LOG(QT, INFO) << "Audio outputting to " << device.description();
#endif
}
m_device->setInput(input());
m_device->setFormat(m_audioOutput->format());
m_audioOutput->start(m_device.get());
return m_audioOutput->state() == QAudio::ActiveState;
if (m_audioOutput->state() == QAudio::SuspendedState) {
m_audioOutput->resume();
} else {
m_device->setInput(input());
m_device->setFormat(m_audioOutput->format());
m_audioOutput->start(m_device.get());
}
return m_audioOutput->state() == QAudio::ActiveState && m_audioOutput->error() == QAudio::NoError;
}
void AudioProcessorQt::pause() {
if (m_audioOutput) {
m_audioOutput->stop();
m_audioOutput->suspend();
}
}

View File

@ -7,6 +7,12 @@
#include "AudioProcessor.h"
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QAudioOutput>
#else
#include <QAudioSink>
#endif
class QAudioOutput;
namespace QGBA {
@ -33,7 +39,11 @@ public slots:
virtual void requestSampleRate(unsigned) override;
private:
QAudioOutput* m_audioOutput = nullptr;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::unique_ptr<QAudioSink> m_audioOutput;
#else
std::unique_ptr<QAudioOutput> m_audioOutput;
#endif
std::unique_ptr<AudioDevice> m_device;
unsigned m_sampleRate = 44100;
};

View File

@ -1093,7 +1093,9 @@ void Window::reloadAudioDriver() {
m_audioProcessor = std::unique_ptr<AudioProcessor>(AudioProcessor::create());
m_audioProcessor->setInput(m_controller);
m_audioProcessor->configure(m_config);
m_audioProcessor->start();
if (!m_audioProcessor->start()) {
LOG(QT, WARN) << "Failed to start audio processor";
}
}
void Window::changeRenderer() {