Qt: Implement audio output

This commit is contained in:
Connor McLaughlin 2020-01-07 14:17:41 +10:00
parent 207c75e6eb
commit 1b7be01507
11 changed files with 177 additions and 16 deletions

View file

@ -123,6 +123,18 @@ void AudioStream::EndWrite(u32 num_samples)
m_buffer_mutex.unlock();
}
u32 AudioStream::GetSamplesAvailable() const
{
// TODO: Use atomic loads
u32 available_buffers;
{
std::unique_lock<std::mutex> lock(m_buffer_mutex);
available_buffers = m_num_available_buffers;
}
return available_buffers * m_buffer_size;
}
u32 AudioStream::ReadSamples(SampleType* samples, u32 num_samples)
{
u32 remaining_samples = num_samples;

View file

@ -49,6 +49,7 @@ protected:
bool IsDeviceOpen() const { return (m_output_sample_rate > 0); }
u32 GetSamplesAvailable() const;
u32 ReadSamples(SampleType* samples, u32 num_samples);
void DropBuffer();
@ -69,7 +70,7 @@ private:
void EnsureBuffer();
std::vector<Buffer> m_buffers;
std::mutex m_buffer_mutex;
mutable std::mutex m_buffer_mutex;
// For input.
u32 m_first_free_buffer = 0;

View file

@ -53,6 +53,14 @@ public:
protected:
using ThrottleClock = std::chrono::steady_clock;
enum : u32
{
AUDIO_SAMPLE_RATE = 44100,
AUDIO_CHANNELS = 2,
AUDIO_BUFFER_SIZE = 2048,
AUDIO_BUFFERS = 2
};
struct OSDMessage
{
std::string text;

View file

@ -24,6 +24,8 @@ add_executable(duckstation-qt
opengldisplaywindow.h
portsettingswidget.cpp
portsettingswidget.h
qtaudiostream.cpp
qtaudiostream.h
qtdisplaywindow.cpp
qtdisplaywindow.h
qthostinterface.cpp

View file

@ -39,6 +39,7 @@
<ClCompile Include="gpusettingswidget.cpp" />
<ClCompile Include="hotkeysettingswidget.cpp" />
<ClCompile Include="inputbindingwidgets.cpp" />
<ClCompile Include="qtaudiostream.cpp" />
<ClCompile Include="qtdisplaywindow.cpp" />
<ClCompile Include="gamelistsettingswidget.cpp" />
<ClCompile Include="gamelistwidget.cpp" />
@ -57,6 +58,7 @@
<QtMoc Include="gpusettingswidget.h" />
<QtMoc Include="hotkeysettingswidget.h" />
<QtMoc Include="inputbindingwidgets.h" />
<ClInclude Include="qtaudiostream.h" />
<ClInclude Include="settingwidgetbinder.h" />
<QtMoc Include="consolesettingswidget.h" />
<QtMoc Include="gamelistsettingswidget.h" />
@ -281,7 +283,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32-debug;$(SolutionDir)dep\msvc\qt5-x86\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;Qt5Multimediad.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -302,7 +304,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64-debug;$(SolutionDir)dep\msvc\qt5-x64\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;Qt5Multimediad.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|Win32'">
@ -325,7 +327,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32-debug;$(SolutionDir)dep\msvc\qt5-x86\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;Qt5Multimediad.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|x64'">
@ -348,7 +350,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64-debug;$(SolutionDir)dep\msvc\qt5-x64\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;Qt5Multimediad.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -370,7 +372,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32;$(SolutionDir)dep\msvc\qt5-x86\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;Qt5Multimedia.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|Win32'">
@ -393,7 +395,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32;$(SolutionDir)dep\msvc\qt5-x86\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;Qt5Multimedia.lib;%(AdditionalDependencies)</AdditionalDependencies>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
@ -416,7 +418,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64;$(SolutionDir)dep\msvc\qt5-x64\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;Qt5Multimedia.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|x64'">
@ -439,7 +441,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64;$(SolutionDir)dep\msvc\qt5-x64\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;Qt5Multimedia.lib;%(AdditionalDependencies)</AdditionalDependencies>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>

View file

@ -27,13 +27,15 @@
<ClCompile Include="$(IntDir)moc_gpusettingswidget.cpp" />
<ClCompile Include="inputbindingwidgets.cpp" />
<ClCompile Include="hotkeysettingswidget.cpp" />
<ClCompile Include="$(IntDir)moc_hotkeysettingswidget.cpp" />
<ClCompile Include="$(IntDir)moc_inputbindingwidgets.cpp" />
<ClCompile Include="qtaudiostream.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="qtsettingsinterface.h" />
<ClInclude Include="qtutils.h" />
<ClInclude Include="settingwidgetbinder.h" />
<ClInclude Include="inputbindingwidgets.h" />
<ClInclude Include="hotkeysettingswidget.h" />
<ClInclude Include="qtaudiostream.h" />
</ItemGroup>
<ItemGroup>
<Filter Include="resources">
@ -54,6 +56,8 @@
<QtMoc Include="portsettingswidget.h" />
<QtMoc Include="qtdisplaywindow.h" />
<QtMoc Include="gpusettingswidget.h" />
<QtMoc Include="hotkeysettingswidget.h" />
<QtMoc Include="inputbindingwidgets.h" />
</ItemGroup>
<ItemGroup>
<QtUi Include="consolesettingswidget.ui" />

View file

@ -0,0 +1,84 @@
#include "qtaudiostream.h"
#include <QtCore/QDebug>
#include <QtMultimedia/QAudioOutput>
QtAudioStream::QtAudioStream()
{
QIODevice::open(QIODevice::ReadOnly);
}
QtAudioStream::~QtAudioStream() = default;
std::unique_ptr<AudioStream> QtAudioStream::Create()
{
return std::make_unique<QtAudioStream>();
}
bool QtAudioStream::OpenDevice()
{
QAudioFormat format;
format.setSampleRate(m_output_sample_rate);
format.setChannelCount(m_channels);
format.setSampleSize(sizeof(SampleType) * 8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo adi = QAudioDeviceInfo::defaultOutputDevice();
if (!adi.isFormatSupported(format))
{
qWarning() << "Audio format not supported by device";
return false;
}
m_output = std::make_unique<QAudioOutput>(format);
m_output->setBufferSize(sizeof(SampleType) * m_channels * m_buffer_size);
return true;
}
void QtAudioStream::PauseDevice(bool paused)
{
if (paused)
{
m_output->stop();
return;
}
m_output->start(this);
}
void QtAudioStream::CloseDevice()
{
m_output.reset();
}
void QtAudioStream::BufferAvailable() {}
bool QtAudioStream::isSequential() const
{
return true;
}
qint64 QtAudioStream::bytesAvailable() const
{
return GetSamplesAvailable() * m_channels * sizeof(SampleType);
}
qint64 QtAudioStream::readData(char* data, qint64 maxlen)
{
const u32 num_samples = static_cast<u32>(maxlen) / sizeof(SampleType) / m_channels;
const u32 read_samples = ReadSamples(reinterpret_cast<SampleType*>(data), num_samples);
const u32 silence_samples = num_samples - read_samples;
if (silence_samples > 0)
{
std::memset(reinterpret_cast<SampleType*>(data) + (read_samples * m_channels), 0,
silence_samples * m_channels * sizeof(SampleType));
}
return num_samples * m_channels * sizeof(SampleType);
}
qint64 QtAudioStream::writeData(const char* data, qint64 len)
{
return 0;
}

View file

@ -0,0 +1,29 @@
#pragma once
#include "common/audio_stream.h"
#include <QtCore/QIODevice>
#include <memory>
class QAudioOutput;
class QtAudioStream final : public AudioStream, private QIODevice
{
public:
QtAudioStream();
~QtAudioStream();
static std::unique_ptr<AudioStream> Create();
protected:
bool OpenDevice() override;
void PauseDevice(bool paused) override;
void CloseDevice() override;
void BufferAvailable() override;
private:
bool isSequential() const override;
qint64 bytesAvailable() const override;
qint64 readData(char* data, qint64 maxlen) override;
qint64 writeData(const char* data, qint64 len) override;
std::unique_ptr<QAudioOutput> m_output;
};

View file

@ -6,6 +6,7 @@
#include "core/game_list.h"
#include "core/gpu.h"
#include "core/system.h"
#include "qtaudiostream.h"
#include "qtsettingsinterface.h"
#include "qtutils.h"
#include <QtCore/QCoreApplication>
@ -20,6 +21,7 @@ QtHostInterface::QtHostInterface(QObject* parent)
checkSettings();
createGameList();
doUpdateInputMap();
createAudioStream();
createThread();
}
@ -328,6 +330,8 @@ void QtHostInterface::powerOffSystem()
}
m_system.reset();
m_audio_stream->PauseOutput(true);
m_audio_stream->EmptyBuffers();
m_display_window->destroyDeviceContext();
emit emulationStopped();
@ -359,6 +363,7 @@ void QtHostInterface::pauseSystem(bool paused)
}
m_paused = paused;
m_audio_stream->PauseOutput(paused);
emit emulationPaused(paused);
}
@ -372,9 +377,6 @@ void QtHostInterface::doBootSystem(QString initial_filename, QString initial_sav
return;
}
m_audio_stream = NullAudioStream::Create();
m_audio_stream->Reconfigure();
std::string initial_filename_str = initial_filename.toStdString();
std::string initial_save_state_filename_str = initial_save_state_filename.toStdString();
if (!CreateSystem() ||
@ -386,9 +388,25 @@ void QtHostInterface::doBootSystem(QString initial_filename, QString initial_sav
return;
}
m_audio_stream->PauseOutput(false);
emit emulationStarted();
}
void QtHostInterface::createAudioStream()
{
// Qt at least on Windows seems to want a buffer size of at least 8KB.
m_audio_stream = QtAudioStream::Create();
if (!m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, AUDIO_BUFFER_SIZE, 4))
{
qWarning() << "Failed to configure audio stream, falling back to null output";
// fall back to null output
m_audio_stream.reset();
m_audio_stream = NullAudioStream::Create();
m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, AUDIO_BUFFER_SIZE, 4);
}
}
void QtHostInterface::createThread()
{
m_original_thread = QThread::currentThread();

View file

@ -106,6 +106,7 @@ private:
void updateControllerInputMap();
void updateHotkeyInputMap();
void addButtonToInputMap(const QString& binding, InputButtonHandler handler);
void createAudioStream();
void createThread();
void stopThread();
void threadEntryPoint();

View file

@ -135,12 +135,12 @@ void SDLHostInterface::CreateAudioStream()
break;
}
if (!m_audio_stream->Reconfigure(44100, 2))
if (!m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS))
{
ReportError("Failed to recreate audio stream, falling back to null");
m_audio_stream.reset();
m_audio_stream = NullAudioStream::Create();
if (!m_audio_stream->Reconfigure(44100, 2))
if (!m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS))
Panic("Failed to reconfigure null audio stream");
}
}