Files
UnleashedRecomp-hedge-dev/UnleashedRecomp/apu/driver/sdl2_driver.cpp

122 lines
4.0 KiB
C++

#include "sdl2_driver.h"
#include <cpu/guest_thread.h>
#include <kernel/heap.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()
{
SDL_SetHint(SDL_HINT_AUDIO_CATEGORY, "playback");
SDL_SetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME, "Unleashed Recompiled");
SDL_InitSubSystem(SDL_INIT_AUDIO);
SDL_AudioSpec desired{}, obtained{};
desired.freq = XAUDIO_SAMPLES_HZ;
desired.format = AUDIO_F32SYS;
desired.channels = XAUDIO_NUM_CHANNELS;
desired.samples = XAUDIO_NUM_SAMPLES;
g_audioDevice = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, SDL_AUDIO_ALLOW_CHANNELS_CHANGE);
if (obtained.channels != 2 && obtained.channels != XAUDIO_NUM_CHANNELS)
{
SDL_CloseAudioDevice(g_audioDevice);
g_audioDevice = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, 0);
}
g_downMixToStereo = (obtained.channels == 2);
}
static std::unique_ptr<std::thread> g_audioThread;
static void AudioThread()
{
using namespace std::chrono_literals;
GuestThreadContext ctx(0);
size_t channels = g_downMixToStereo ? 2 : XAUDIO_NUM_CHANNELS;
while (true)
{
uint32_t queuedAudioSize = SDL_GetQueuedAudioSize(g_audioDevice);
constexpr size_t MAX_LATENCY = 10;
const size_t callbackAudioSize = channels * XAUDIO_NUM_SAMPLES * sizeof(float);
if ((queuedAudioSize / callbackAudioSize) <= MAX_LATENCY)
{
ctx.ppcContext.r3.u32 = g_clientCallbackParam;
g_clientCallback(ctx.ppcContext, g_memory.base);
}
auto now = std::chrono::steady_clock::now();
constexpr auto INTERVAL = 1000000000ns * XAUDIO_NUM_SAMPLES / XAUDIO_SAMPLES_HZ;
auto next = now + (INTERVAL - now.time_since_epoch() % INTERVAL);
std::this_thread::sleep_for(std::chrono::floor<std::chrono::milliseconds>(next - now));
while (std::chrono::steady_clock::now() < next)
std::this_thread::yield();
}
}
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;
SDL_PauseAudioDevice(g_audioDevice, 0);
g_audioThread = std::make_unique<std::thread>(AudioThread);
}
void XAudioSubmitFrame(void* samples)
{
if (g_downMixToStereo)
{
// 0: left 1.0f, right 0.0f
// 1: left 0.0f, right 1.0f
// 2: left 0.75f, right 0.75f
// 3: left 0.0f, right 0.0f
// 4: left 1.0f, right 0.0f
// 5: left 0.0f, right 1.0f
auto floatSamples = reinterpret_cast<be<float>*>(samples);
std::array<float, 2 * XAUDIO_NUM_SAMPLES> audioFrames;
for (size_t i = 0; i < XAUDIO_NUM_SAMPLES; i++)
{
float ch0 = floatSamples[0 * XAUDIO_NUM_SAMPLES + i];
float ch1 = floatSamples[1 * XAUDIO_NUM_SAMPLES + i];
float ch2 = floatSamples[2 * XAUDIO_NUM_SAMPLES + i];
float ch3 = floatSamples[3 * XAUDIO_NUM_SAMPLES + i];
float ch4 = floatSamples[4 * XAUDIO_NUM_SAMPLES + i];
float ch5 = floatSamples[5 * XAUDIO_NUM_SAMPLES + i];
audioFrames[i * 2 + 0] = ch0 + ch2 * 0.75f + ch4;
audioFrames[i * 2 + 1] = ch1 + ch2 * 0.75f + ch5;
}
SDL_QueueAudio(g_audioDevice, &audioFrames, sizeof(audioFrames));
}
else
{
auto rawSamples = reinterpret_cast<be<uint32_t>*>(samples);
std::array<uint32_t, XAUDIO_NUM_CHANNELS * XAUDIO_NUM_SAMPLES> audioFrames;
for (size_t i = 0; i < XAUDIO_NUM_SAMPLES; i++)
{
for (size_t j = 0; j < XAUDIO_NUM_CHANNELS; j++)
audioFrames[i * XAUDIO_NUM_CHANNELS + j] = rawSamples[j * XAUDIO_NUM_SAMPLES + i];
}
SDL_QueueAudio(g_audioDevice, &audioFrames, sizeof(audioFrames));
}
}