mirror of
https://github.com/hedge-dev/UnleashedRecomp
synced 2026-07-28 23:28:21 -04:00
Implement config option to toggle surround sound. (#144)
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
void XAudioInitializeSystem();
|
||||
void XAudioRegisterClient(PPCFunc* callback, uint32_t param);
|
||||
void XAudioSubmitFrame(void* samples);
|
||||
void XAudioConfigValueChangedCallback(class IConfigDef* configDef);
|
||||
|
||||
uint32_t XAudioRegisterRenderDriverClient(be<uint32_t>* callback, be<uint32_t>* driver);
|
||||
uint32_t XAudioUnregisterRenderDriverClient(uint32_t driver);
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
#include "sdl2_driver.h"
|
||||
#include <apu/audio.h>
|
||||
#include <cpu/guest_thread.h>
|
||||
#include <kernel/heap.h>
|
||||
#include <user/config.h>
|
||||
|
||||
static PPCFunc* g_clientCallback{};
|
||||
static uint32_t g_clientCallbackParam{}; // pointer in guest memory
|
||||
static SDL_AudioDeviceID g_audioDevice{};
|
||||
static bool g_downMixToStereo;
|
||||
|
||||
void XAudioInitializeSystem()
|
||||
static void CreateAudioDevice()
|
||||
{
|
||||
SDL_SetHint(SDL_HINT_AUDIO_CATEGORY, "playback");
|
||||
SDL_SetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME, "Unleashed Recompiled");
|
||||
SDL_InitSubSystem(SDL_INIT_AUDIO);
|
||||
if (g_audioDevice != NULL)
|
||||
SDL_CloseAudioDevice(g_audioDevice);
|
||||
|
||||
bool surround = Config::ChannelConfiguration == EChannelConfiguration::Surround;
|
||||
int allowedChanges = surround ? SDL_AUDIO_ALLOW_CHANNELS_CHANGE : 0;
|
||||
|
||||
SDL_AudioSpec desired{}, obtained{};
|
||||
desired.freq = XAUDIO_SAMPLES_HZ;
|
||||
desired.format = AUDIO_F32SYS;
|
||||
desired.channels = XAUDIO_NUM_CHANNELS;
|
||||
desired.channels = surround ? XAUDIO_NUM_CHANNELS : 2;
|
||||
desired.samples = XAUDIO_NUM_SAMPLES;
|
||||
g_audioDevice = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, SDL_AUDIO_ALLOW_CHANNELS_CHANGE);
|
||||
g_audioDevice = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, allowedChanges);
|
||||
|
||||
if (obtained.channels != 2 && obtained.channels != XAUDIO_NUM_CHANNELS)
|
||||
if (obtained.channels != 2 && obtained.channels != XAUDIO_NUM_CHANNELS) // This check may fail only when surround sound is enabled.
|
||||
{
|
||||
SDL_CloseAudioDevice(g_audioDevice);
|
||||
g_audioDevice = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, 0);
|
||||
@@ -29,7 +32,16 @@ void XAudioInitializeSystem()
|
||||
g_downMixToStereo = (obtained.channels == 2);
|
||||
}
|
||||
|
||||
void XAudioInitializeSystem()
|
||||
{
|
||||
SDL_SetHint(SDL_HINT_AUDIO_CATEGORY, "playback");
|
||||
SDL_SetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME, "Unleashed Recompiled");
|
||||
SDL_InitSubSystem(SDL_INIT_AUDIO);
|
||||
CreateAudioDevice();
|
||||
}
|
||||
|
||||
static std::unique_ptr<std::thread> g_audioThread;
|
||||
static volatile bool g_audioThreadShouldExit;
|
||||
|
||||
static void AudioThread()
|
||||
{
|
||||
@@ -39,7 +51,7 @@ static void AudioThread()
|
||||
|
||||
size_t channels = g_downMixToStereo ? 2 : XAUDIO_NUM_CHANNELS;
|
||||
|
||||
while (true)
|
||||
while (!g_audioThreadShouldExit)
|
||||
{
|
||||
uint32_t queuedAudioSize = SDL_GetQueuedAudioSize(g_audioDevice);
|
||||
constexpr size_t MAX_LATENCY = 10;
|
||||
@@ -62,6 +74,13 @@ static void AudioThread()
|
||||
}
|
||||
}
|
||||
|
||||
static void CreateAudioThread()
|
||||
{
|
||||
SDL_PauseAudioDevice(g_audioDevice, 0);
|
||||
g_audioThreadShouldExit = false;
|
||||
g_audioThread = std::make_unique<std::thread>(AudioThread);
|
||||
}
|
||||
|
||||
void XAudioRegisterClient(PPCFunc* callback, uint32_t param)
|
||||
{
|
||||
auto* pClientParam = static_cast<uint32_t*>(g_userHeap.Alloc(sizeof(param)));
|
||||
@@ -70,8 +89,7 @@ void XAudioRegisterClient(PPCFunc* callback, uint32_t param)
|
||||
g_clientCallbackParam = g_memory.MapVirtual(pClientParam);
|
||||
g_clientCallback = callback;
|
||||
|
||||
SDL_PauseAudioDevice(g_audioDevice, 0);
|
||||
g_audioThread = std::make_unique<std::thread>(AudioThread);
|
||||
CreateAudioThread();
|
||||
}
|
||||
|
||||
void XAudioSubmitFrame(void* samples)
|
||||
@@ -119,3 +137,18 @@ void XAudioSubmitFrame(void* samples)
|
||||
SDL_QueueAudio(g_audioDevice, &audioFrames, sizeof(audioFrames));
|
||||
}
|
||||
}
|
||||
|
||||
void XAudioConfigValueChangedCallback(IConfigDef* configDef)
|
||||
{
|
||||
if (configDef == &Config::ChannelConfiguration)
|
||||
{
|
||||
if (g_audioThread->joinable())
|
||||
{
|
||||
g_audioThreadShouldExit = true;
|
||||
g_audioThread->join();
|
||||
}
|
||||
|
||||
CreateAudioDevice();
|
||||
CreateAudioThread();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <apu/audio.h>
|
||||
@@ -1,121 +0,0 @@
|
||||
#include <stdafx.h>
|
||||
#include "xaudio_driver.h"
|
||||
#include <xaudio2.h>
|
||||
#include <cpu/guest_thread.h>
|
||||
#include <cpu/ppc_context.h>
|
||||
#include <kernel/heap.h>
|
||||
|
||||
#define XAUDIO_DRIVER_KEY (uint32_t)('XAUD')
|
||||
|
||||
PPCFunc* volatile g_clientCallback{};
|
||||
DWORD g_clientCallbackParam{}; // pointer in guest memory
|
||||
DWORD g_driverThread{};
|
||||
|
||||
// TODO: Should use a counted ptr
|
||||
IXAudio2* g_audio{};
|
||||
IXAudio2MasteringVoice* g_masteringVoice{};
|
||||
IXAudio2SourceVoice* g_sourceVoice{};
|
||||
|
||||
constexpr uint32_t g_semaphoreCount = 16;
|
||||
constexpr uint32_t g_audioFrameSize = 256 * 6;
|
||||
HANDLE g_audioSemaphore{ CreateSemaphoreA(nullptr, g_semaphoreCount, g_semaphoreCount, nullptr) };
|
||||
uint32_t g_audioFrames[g_audioFrameSize * g_semaphoreCount];
|
||||
uint32_t g_audioFrameIndex = 0;
|
||||
|
||||
class VoiceCallback : public IXAudio2VoiceCallback
|
||||
{
|
||||
STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32 BytesRequired) override {}
|
||||
STDMETHOD_(void, OnVoiceProcessingPassEnd)() override {}
|
||||
|
||||
STDMETHOD_(void, OnBufferStart)(void* pBufferContext) override {}
|
||||
STDMETHOD_(void, OnBufferEnd)(void* pBufferContext) override
|
||||
{
|
||||
ReleaseSemaphore(g_audioSemaphore, 1, nullptr);
|
||||
}
|
||||
|
||||
STDMETHOD_(void, OnStreamEnd)() override {}
|
||||
|
||||
STDMETHOD_(void, OnLoopEnd)(void* pBufferContext) override {}
|
||||
STDMETHOD_(void, OnVoiceError)(void* pBufferContext, HRESULT Error) override {}
|
||||
} gVoiceCallback;
|
||||
|
||||
PPC_FUNC(DriverLoop)
|
||||
{
|
||||
GuestThread::SetThreadName(GetCurrentThreadId(), "Audio Driver");
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (!g_clientCallback)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
WaitForSingleObject(g_audioSemaphore, INFINITE);
|
||||
|
||||
ctx.r3.u64 = g_clientCallbackParam;
|
||||
g_clientCallback(ctx, g_memory.base);
|
||||
}
|
||||
}
|
||||
|
||||
void XAudioInitializeSystem()
|
||||
{
|
||||
if (g_audio)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//reinterpret_cast<decltype(&XAudio2Create)>(
|
||||
// GetProcAddress(LoadLibraryA("XAudio2_8.dll"), "XAudio2Create"))(&gAudio, 0, 1);
|
||||
|
||||
XAudio2Create(&g_audio);
|
||||
g_audio->CreateMasteringVoice(&g_masteringVoice);
|
||||
|
||||
WAVEFORMATIEEEFLOATEX format{};
|
||||
format.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
||||
format.Format.cbSize = sizeof(format) - sizeof(format.Format);
|
||||
format.Format.nChannels = XAUDIO_NUM_CHANNELS;
|
||||
format.Format.nSamplesPerSec = XAUDIO_SAMPLES_HZ;
|
||||
format.Format.wBitsPerSample = XAUDIO_SAMPLE_BITS;
|
||||
format.Format.nBlockAlign = (format.Format.nChannels * format.Format.wBitsPerSample) / 8;
|
||||
format.Format.nAvgBytesPerSec = format.Format.nSamplesPerSec * format.Format.nBlockAlign;
|
||||
|
||||
format.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
|
||||
format.Samples.wValidBitsPerSample = format.Format.wBitsPerSample;
|
||||
format.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT |
|
||||
SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
|
||||
|
||||
g_audio->CreateSourceVoice(&g_sourceVoice, &format.Format, 0, 1024, &gVoiceCallback);
|
||||
g_sourceVoice->Start();
|
||||
|
||||
KeInsertHostFunction(XAUDIO_DRIVER_KEY, DriverLoop);
|
||||
GuestThread::Start({ XAUDIO_DRIVER_KEY, 0, 0 }, nullptr);
|
||||
}
|
||||
|
||||
void XAudioRegisterClient(PPCFunc* callback, uint32_t param)
|
||||
{
|
||||
auto* pClientParam = static_cast<uint32_t*>(g_userHeap.Alloc(sizeof(param)));
|
||||
ByteSwapInplace(param);
|
||||
*pClientParam = param;
|
||||
g_clientCallbackParam = g_memory.MapVirtual(pClientParam);
|
||||
|
||||
g_clientCallback = callback;
|
||||
}
|
||||
|
||||
void XAudioSubmitFrame(void* samples)
|
||||
{
|
||||
uint32_t* audioFrame = &g_audioFrames[g_audioFrameSize * g_audioFrameIndex];
|
||||
g_audioFrameIndex = (g_audioFrameIndex + 1) % g_semaphoreCount;
|
||||
|
||||
for (size_t i = 0; i < XAUDIO_NUM_SAMPLES; i++)
|
||||
{
|
||||
for (size_t j = 0; j < 6; j++)
|
||||
audioFrame[i * XAUDIO_NUM_CHANNELS + j] = ByteSwap(((uint32_t*)samples)[j * XAUDIO_NUM_SAMPLES + i]);
|
||||
}
|
||||
|
||||
XAUDIO2_BUFFER buffer{};
|
||||
buffer.pAudioData = (BYTE*)audioFrame;
|
||||
buffer.AudioBytes = XAUDIO_NUM_SAMPLES * XAUDIO_NUM_CHANNELS * sizeof(float);
|
||||
buffer.PlayLength = XAUDIO_NUM_SAMPLES;
|
||||
|
||||
g_sourceVoice->SubmitSourceBuffer(&buffer);
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
#pragma once
|
||||
#include <apu/audio.h>
|
||||
Reference in New Issue
Block a user