mirror of
https://github.com/hedge-dev/UnleashedRecomp.git
synced 2025-12-21 07:22:21 +00:00
commit
d51ba31f27
6 changed files with 128 additions and 11 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
project("UnleashedRecomp")
|
project("UnleashedRecomp")
|
||||||
set(TARGET_NAME "SWA")
|
set(TARGET_NAME "SWA")
|
||||||
|
|
||||||
|
option(SWA_XAUDIO2 "Use XAudio2 for audio playback" OFF)
|
||||||
|
|
||||||
add_compile_options(
|
add_compile_options(
|
||||||
/fp:strict
|
/fp:strict
|
||||||
-march=sandybridge
|
-march=sandybridge
|
||||||
|
|
@ -57,9 +59,14 @@ set(SWA_GPU_CXX_SOURCES
|
||||||
|
|
||||||
set(SWA_APU_CXX_SOURCES
|
set(SWA_APU_CXX_SOURCES
|
||||||
"apu/audio.cpp"
|
"apu/audio.cpp"
|
||||||
"apu/driver/xaudio_driver.cpp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if(SWA_XAUDIO2)
|
||||||
|
list(APPEND SWA_APU_CXX_SOURCES "apu/driver/xaudio_driver.cpp")
|
||||||
|
else()
|
||||||
|
list(APPEND SWA_APU_CXX_SOURCES "apu/driver/sdl2_driver.cpp")
|
||||||
|
endif()
|
||||||
|
|
||||||
set(SWA_HID_CXX_SOURCES
|
set(SWA_HID_CXX_SOURCES
|
||||||
"hid/hid.cpp"
|
"hid/hid.cpp"
|
||||||
"hid/driver/sdl_hid.cpp"
|
"hid/driver/sdl_hid.cpp"
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#define XAUDIO_SAMPLES_HZ 48000
|
||||||
|
#define XAUDIO_NUM_CHANNELS 6
|
||||||
|
#define XAUDIO_SAMPLE_BITS 32
|
||||||
|
|
||||||
|
// Number of samples in a frame
|
||||||
|
#define XAUDIO_NUM_SAMPLES 256
|
||||||
|
|
||||||
#ifdef SWA_IMPL
|
#ifdef SWA_IMPL
|
||||||
void XAudioInitializeSystem();
|
void XAudioInitializeSystem();
|
||||||
void XAudioRegisterClient(PPCFunc* callback, uint32_t param);
|
void XAudioRegisterClient(PPCFunc* callback, uint32_t param);
|
||||||
|
|
|
||||||
101
UnleashedRecomp/apu/driver/sdl2_driver.cpp
Normal file
101
UnleashedRecomp/apu/driver/sdl2_driver.cpp
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
#include "sdl2_driver.h"
|
||||||
|
#include <cpu/code_cache.h>
|
||||||
|
#include <cpu/guest_thread.h>
|
||||||
|
#include <cpu/guest_code.h>
|
||||||
|
#include <kernel/heap.h>
|
||||||
|
|
||||||
|
#define SDLAUDIO_DRIVER_KEY (uint32_t)('SDLA')
|
||||||
|
constexpr uint32_t g_semaphoreCount = 16;
|
||||||
|
constexpr uint32_t g_audioFrameSize = XAUDIO_NUM_SAMPLES * XAUDIO_NUM_CHANNELS;
|
||||||
|
|
||||||
|
PPCFunc* volatile g_clientCallback{};
|
||||||
|
DWORD g_clientCallbackParam{}; // pointer in guest memory
|
||||||
|
|
||||||
|
SDL_AudioDeviceID g_audioDevice{};
|
||||||
|
SDL_sem* g_audioSemaphore{ SDL_CreateSemaphore(g_semaphoreCount) };
|
||||||
|
uint32_t g_audioFrames[g_audioFrameSize * g_semaphoreCount];
|
||||||
|
uint32_t g_audioFrameIndex = 0;
|
||||||
|
uint32_t g_renderFrameIndex = 0; // Index of frame that's being rendered
|
||||||
|
uint32_t g_numRenderFrames = 0; // Number of frames to render
|
||||||
|
Mutex g_framesMutex{};
|
||||||
|
|
||||||
|
static void SDLAudioCallback(void*, uint8_t* frames, int len)
|
||||||
|
{
|
||||||
|
std::lock_guard guard{ g_framesMutex };
|
||||||
|
if (g_numRenderFrames == 0)
|
||||||
|
{
|
||||||
|
memset(frames, 0, len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
g_renderFrameIndex = (g_renderFrameIndex + 1) % g_semaphoreCount;
|
||||||
|
auto* srcFrames = &g_audioFrames[g_renderFrameIndex * g_audioFrameSize];
|
||||||
|
memcpy(frames, srcFrames, g_audioFrameSize * sizeof(float));
|
||||||
|
--g_numRenderFrames;
|
||||||
|
}
|
||||||
|
SDL_SemPost(g_audioSemaphore);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PPC_FUNC(DriverLoop)
|
||||||
|
{
|
||||||
|
GuestThread::SetThreadName(GetCurrentThreadId(), "Audio Driver");
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (!g_clientCallback)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SemWait(g_audioSemaphore);
|
||||||
|
ctx.r3.u64 = g_clientCallbackParam;
|
||||||
|
GuestCode::Run((void*)g_clientCallback, &ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void XAudioInitializeSystem()
|
||||||
|
{
|
||||||
|
assert(g_audioSemaphore);
|
||||||
|
|
||||||
|
SDL_SetHint(SDL_HINT_AUDIO_CATEGORY, "playback");
|
||||||
|
SDL_SetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME, "SWA");
|
||||||
|
|
||||||
|
auto err = SDL_InitSubSystem(SDL_INIT_AUDIO);
|
||||||
|
SDL_AudioSpec spec{};
|
||||||
|
spec.freq = XAUDIO_SAMPLES_HZ;
|
||||||
|
spec.format = AUDIO_F32SYS;
|
||||||
|
spec.channels = XAUDIO_NUM_CHANNELS;
|
||||||
|
spec.samples = XAUDIO_NUM_SAMPLES;
|
||||||
|
spec.callback = SDLAudioCallback;
|
||||||
|
g_audioDevice = SDL_OpenAudioDevice(nullptr, false, &spec, &spec, 0);
|
||||||
|
assert(g_audioDevice);
|
||||||
|
|
||||||
|
SDL_PauseAudioDevice(g_audioDevice, 0);
|
||||||
|
KeInsertHostFunction(SDLAUDIO_DRIVER_KEY, DriverLoop);
|
||||||
|
GuestThread::Start(SDLAUDIO_DRIVER_KEY, 0, 0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void XAudioRegisterClient(PPCFunc* callback, uint32_t param)
|
||||||
|
{
|
||||||
|
auto* pClientParam = static_cast<uint32_t*>(g_userHeap.Alloc(sizeof(param)));
|
||||||
|
ByteSwap(param);
|
||||||
|
*pClientParam = param;
|
||||||
|
g_clientCallbackParam = g_memory.MapVirtual(pClientParam);
|
||||||
|
|
||||||
|
g_clientCallback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void XAudioSubmitFrame(void* samples)
|
||||||
|
{
|
||||||
|
std::lock_guard guard{ g_framesMutex };
|
||||||
|
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 < XAUDIO_NUM_CHANNELS; j++)
|
||||||
|
audioFrame[i * XAUDIO_NUM_CHANNELS + j] = std::byteswap(((uint32_t*)samples)[j * XAUDIO_NUM_SAMPLES + i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
++g_numRenderFrames;
|
||||||
|
}
|
||||||
3
UnleashedRecomp/apu/driver/sdl2_driver.h
Normal file
3
UnleashedRecomp/apu/driver/sdl2_driver.h
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <apu/audio.h>
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
#define XAUDIO_DRIVER_KEY (uint32_t)('XAUD')
|
#define XAUDIO_DRIVER_KEY (uint32_t)('XAUD')
|
||||||
|
|
||||||
PPCFunc* g_clientCallback{};
|
PPCFunc* volatile g_clientCallback{};
|
||||||
DWORD g_clientCallbackParam{}; // pointer in guest memory
|
DWORD g_clientCallbackParam{}; // pointer in guest memory
|
||||||
DWORD g_driverThread{};
|
DWORD g_driverThread{};
|
||||||
|
|
||||||
|
|
@ -49,8 +49,6 @@ PPC_FUNC(DriverLoop)
|
||||||
{
|
{
|
||||||
if (!g_clientCallback)
|
if (!g_clientCallback)
|
||||||
{
|
{
|
||||||
// NOTE: This if statement doesn't get compiled in without this barrier. What?
|
|
||||||
_ReadBarrier();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,9 +75,9 @@ void XAudioInitializeSystem()
|
||||||
WAVEFORMATIEEEFLOATEX format{};
|
WAVEFORMATIEEEFLOATEX format{};
|
||||||
format.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
format.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
||||||
format.Format.cbSize = sizeof(format) - sizeof(format.Format);
|
format.Format.cbSize = sizeof(format) - sizeof(format.Format);
|
||||||
format.Format.nChannels = 6;
|
format.Format.nChannels = XAUDIO_NUM_CHANNELS;
|
||||||
format.Format.nSamplesPerSec = 48000;
|
format.Format.nSamplesPerSec = XAUDIO_SAMPLES_HZ;
|
||||||
format.Format.wBitsPerSample = 32;
|
format.Format.wBitsPerSample = XAUDIO_SAMPLE_BITS;
|
||||||
format.Format.nBlockAlign = (format.Format.nChannels * format.Format.wBitsPerSample) / 8;
|
format.Format.nBlockAlign = (format.Format.nChannels * format.Format.wBitsPerSample) / 8;
|
||||||
format.Format.nAvgBytesPerSec = format.Format.nSamplesPerSec * format.Format.nBlockAlign;
|
format.Format.nAvgBytesPerSec = format.Format.nSamplesPerSec * format.Format.nBlockAlign;
|
||||||
|
|
||||||
|
|
@ -110,16 +108,16 @@ void XAudioSubmitFrame(void* samples)
|
||||||
uint32_t* audioFrame = &g_audioFrames[g_audioFrameSize * g_audioFrameIndex];
|
uint32_t* audioFrame = &g_audioFrames[g_audioFrameSize * g_audioFrameIndex];
|
||||||
g_audioFrameIndex = (g_audioFrameIndex + 1) % g_semaphoreCount;
|
g_audioFrameIndex = (g_audioFrameIndex + 1) % g_semaphoreCount;
|
||||||
|
|
||||||
for (size_t i = 0; i < 256; i++)
|
for (size_t i = 0; i < XAUDIO_NUM_SAMPLES; i++)
|
||||||
{
|
{
|
||||||
for (size_t j = 0; j < 6; j++)
|
for (size_t j = 0; j < 6; j++)
|
||||||
audioFrame[i * 6 + j] = std::byteswap(((uint32_t*)samples)[j * 256 + i]);
|
audioFrame[i * XAUDIO_NUM_CHANNELS + j] = std::byteswap(((uint32_t*)samples)[j * XAUDIO_NUM_SAMPLES + i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
XAUDIO2_BUFFER buffer{};
|
XAUDIO2_BUFFER buffer{};
|
||||||
buffer.pAudioData = (BYTE*)audioFrame;
|
buffer.pAudioData = (BYTE*)audioFrame;
|
||||||
buffer.AudioBytes = 256 * 6 * sizeof(float);
|
buffer.AudioBytes = XAUDIO_NUM_SAMPLES * XAUDIO_NUM_CHANNELS * sizeof(float);
|
||||||
buffer.PlayLength = 256;
|
buffer.PlayLength = XAUDIO_NUM_SAMPLES;
|
||||||
|
|
||||||
g_sourceVoice->SubmitSourceBuffer(&buffer);
|
g_sourceVoice->SubmitSourceBuffer(&buffer);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <apu/audio.h>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue