Implement miniaudio. (#15)

This commit is contained in:
Skyth (Asilkan) 2024-12-03 18:10:06 +03:00 committed by GitHub
parent 913f5a388b
commit 6c65e0914d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 96 additions and 105 deletions

View file

@ -64,7 +64,7 @@ set(SWA_APU_CXX_SOURCES
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")
list(APPEND SWA_APU_CXX_SOURCES "apu/driver/miniaudio_driver.cpp")
endif()
set(SWA_HID_CXX_SOURCES
@ -160,7 +160,7 @@ find_package(unofficial-concurrentqueue REQUIRED)
find_package(imgui CONFIG REQUIRED)
find_package(magic_enum CONFIG REQUIRED)
find_package(unofficial-tiny-aes-c CONFIG REQUIRED)
find_path(READERWRITERQUEUE_INCLUDE_DIRS "readerwriterqueue/atomicops.h")
find_path(MINIAUDIO_INCLUDE_DIRS "miniaudio.h")
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/D3D12)
add_custom_command(TARGET UnleashedRecomp POST_BUILD
@ -208,7 +208,7 @@ target_include_directories(UnleashedRecomp PRIVATE
${LIBMSPACK_PATH}
${Stb_INCLUDE_DIR}
${SMOLV_SOURCE_DIR}
${READERWRITERQUEUE_INCLUDE_DIRS}
${MINIAUDIO_INCLUDE_DIRS}
)
target_precompile_headers(UnleashedRecomp PUBLIC ${SWA_PRECOMPILED_HEADERS})

View file

@ -0,0 +1,56 @@
#include "miniaudio_driver.h"
#include <cpu/code_cache.h>
#include <cpu/guest_thread.h>
#include <cpu/guest_code.h>
#include <kernel/heap.h>
#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio.h>
static PPCFunc* g_clientCallback{};
static DWORD g_clientCallbackParam{}; // pointer in guest memory
static ma_device g_audioDevice{};
static std::unique_ptr<GuestThreadContext> g_audioCtx;
static uint32_t* g_audioOutput;
static void AudioCallback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
if (g_audioCtx == nullptr)
g_audioCtx = std::make_unique<GuestThreadContext>(0);
g_audioCtx->ppcContext.r3.u64 = g_clientCallbackParam;
g_audioOutput = reinterpret_cast<uint32_t*>(pOutput);
(*g_clientCallback)(g_audioCtx->ppcContext, reinterpret_cast<uint8_t*>(g_memory.base));
}
void XAudioInitializeSystem()
{
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.sampleRate = XAUDIO_SAMPLES_HZ;
deviceConfig.periodSizeInFrames = XAUDIO_NUM_SAMPLES;
deviceConfig.noPreSilencedOutputBuffer = true;
deviceConfig.dataCallback = AudioCallback;
deviceConfig.playback.format = ma_format_f32;
deviceConfig.playback.channels = XAUDIO_NUM_CHANNELS;
ma_device_init(nullptr, &deviceConfig, &g_audioDevice);
}
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;
ma_device_start(&g_audioDevice);
}
void XAudioSubmitFrame(void* samples)
{
for (size_t i = 0; i < XAUDIO_NUM_SAMPLES; i++)
{
for (size_t j = 0; j < XAUDIO_NUM_CHANNELS; j++)
g_audioOutput[i * XAUDIO_NUM_CHANNELS + j] = std::byteswap(((uint32_t*)samples)[j * XAUDIO_NUM_SAMPLES + i]);
}
}

View file

@ -1,3 +1,2 @@
#pragma once
#include <SDL.h>
#include <apu/audio.h>

View file

@ -1,81 +0,0 @@
#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')
static constexpr uint32_t AUDIO_FRAME_SIZE = XAUDIO_NUM_SAMPLES * XAUDIO_NUM_CHANNELS;
static std::atomic<PPCFunc*> g_clientCallback{};
static DWORD g_clientCallbackParam{}; // pointer in guest memory
static SDL_AudioDeviceID g_audioDevice{};
static moodycamel::BlockingReaderWriterCircularBuffer<std::array<uint32_t, AUDIO_FRAME_SIZE>> g_audioQueue(16);
static void SDLAudioCallback(void*, uint8_t* frames, int len)
{
std::array<uint32_t, AUDIO_FRAME_SIZE> audioFrame;
if (g_audioQueue.try_dequeue(audioFrame))
memcpy(frames, &audioFrame, sizeof(audioFrame));
else
memset(frames, 0, len);
}
static PPC_FUNC(DriverLoop)
{
GuestThread::SetThreadName(GetCurrentThreadId(), "Audio Driver");
while (true)
{
if (!g_clientCallback)
continue;
ctx.r3.u64 = g_clientCallbackParam;
GuestCode::Run((void*)g_clientCallback, &ctx);
}
}
void XAudioInitializeSystem()
{
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::array<uint32_t, AUDIO_FRAME_SIZE> audioFrame;
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_audioQueue.wait_enqueue(audioFrame);
}

View file

@ -4,7 +4,7 @@
struct GuestCode
{
inline static void Run(void* hostAddress, PPCContext* ctx, void* baseAddress, void* callStack)
inline static void Run(void* hostAddress, PPCContext* ctx, void* baseAddress)
{
ctx->fpscr.loadFromHost();
reinterpret_cast<PPCFunc*>(hostAddress)(*ctx, reinterpret_cast<uint8_t*>(baseAddress));

View file

@ -11,24 +11,15 @@ constexpr size_t PCR_SIZE = 0xAB0;
constexpr size_t TLS_SIZE = 0x100;
constexpr size_t TEB_SIZE = 0x2E0;
constexpr size_t STACK_SIZE = 0x40000;
constexpr size_t CALL_STACK_SIZE = 0x8000;
constexpr size_t TOTAL_SIZE = PCR_SIZE + TLS_SIZE + TEB_SIZE + STACK_SIZE + CALL_STACK_SIZE;
constexpr size_t TOTAL_SIZE = PCR_SIZE + TLS_SIZE + TEB_SIZE + STACK_SIZE;
constexpr size_t TEB_OFFSET = PCR_SIZE + TLS_SIZE;
DWORD GuestThread::Start(uint32_t function)
GuestThreadContext::GuestThreadContext(uint32_t cpuNumber)
{
const GuestThreadParameter parameter{ function };
return Start(parameter);
}
DWORD GuestThread::Start(const GuestThreadParameter& parameter)
{
auto* thread = (uint8_t*)g_userHeap.Alloc(TOTAL_SIZE);
const auto procMask = (uint8_t)(parameter.flags >> 24);
const auto cpuNumber = procMask == 0 ? 0 : 7 - std::countl_zero(procMask);
assert(thread == nullptr);
thread = (uint8_t*)g_userHeap.Alloc(TOTAL_SIZE);
memset(thread, 0, TOTAL_SIZE);
*(uint32_t*)thread = std::byteswap(g_memory.MapVirtual(thread + PCR_SIZE)); // tls pointer
@ -38,18 +29,36 @@ DWORD GuestThread::Start(const GuestThreadParameter& parameter)
*(uint32_t*)(thread + PCR_SIZE + 0x10) = 0xFFFFFFFF; // that one TLS entry that felt quirky
*(uint32_t*)(thread + PCR_SIZE + TLS_SIZE + 0x14C) = std::byteswap(GetCurrentThreadId()); // thread id
PPCContext ppcContext{};
ppcContext.fn = (uint8_t*)g_codeCache.bucket;
ppcContext.r1.u64 = g_memory.MapVirtual(thread + PCR_SIZE + TLS_SIZE + TEB_SIZE + STACK_SIZE); // stack pointer
ppcContext.r3.u64 = parameter.value;
ppcContext.r13.u64 = g_memory.MapVirtual(thread);
assert(GetPPCContext() == nullptr);
SetPPCContext(ppcContext);
}
GuestCode::Run(g_codeCache.Find(parameter.function), &ppcContext, g_memory.Translate(0), g_memory.Translate(ppcContext.r1.u32));
GuestThreadContext::~GuestThreadContext()
{
g_userHeap.Free(thread);
}
return (DWORD)ppcContext.r3.u64;
DWORD GuestThread::Start(uint32_t function)
{
const GuestThreadParameter parameter{ function };
return Start(parameter);
}
DWORD GuestThread::Start(const GuestThreadParameter& parameter)
{
const auto procMask = (uint8_t)(parameter.flags >> 24);
const auto cpuNumber = procMask == 0 ? 0 : 7 - std::countl_zero(procMask);
GuestThreadContext ctx(cpuNumber);
ctx.ppcContext.r3.u64 = parameter.value;
GuestCode::Run(g_codeCache.Find(parameter.function), &ctx.ppcContext, g_memory.Translate(0));
return (DWORD)ctx.ppcContext.r3.u64;
}
DWORD HostThreadStart(void* pParameter)

View file

@ -8,6 +8,15 @@ struct GuestThreadParameter
uint32_t flags;
};
struct GuestThreadContext
{
PPCContext ppcContext{};
uint8_t* thread = nullptr;
GuestThreadContext(uint32_t cpuNumber);
~GuestThreadContext();
};
struct GuestThread
{
static DWORD Start(uint32_t function);

View file

@ -32,7 +32,6 @@
#include <wrl/client.h>
#include <smolv.h>
#include <print>
#include <readerwriterqueue/readerwritercircularbuffer.h>
using Microsoft::WRL::ComPtr;

View file

@ -22,6 +22,6 @@
"features": [ "sdl2-binding" ]
},
"magic-enum",
"readerwriterqueue"
"miniaudio"
]
}