port: experimental multithreaded audio

This commit is contained in:
fgsfds committed 2024-05-18 18:53:12 +02:00
1 parent e8e513fecb
commit 4c552a42f2
17 files changed
+186 -56

No files matched your search

+2 -8
View File
@@ -9840,20 +9840,14 @@ bool aiIfMusicEventQueueIsEmpty(void)
{
f32 value = (u64)osGetCount() * 64 / 3000;
#ifdef PLATFORM_N64 // will hang forever until the audio thread wakes it up
if (g_MusicEventQueueLength) {
#else // HACK: will wait 1 frame and get on with it
static bool waited = false;
if (g_MusicEventQueueLength && !waited) {
waited = true;
#ifndef PLATFORM_N64
musicTickEvents();
#endif
g_Vars.aioffset += 4;
} else {
u8 *cmd = g_Vars.ailist + g_Vars.aioffset;
g_Vars.aioffset = chraiGoToLabel(g_Vars.ailist, g_Vars.aioffset, cmd[3]);
#ifndef PLATFORM_N64
waited = false;
#endif
}
return false;
+1 -1
View File
@@ -47,7 +47,7 @@ void frametimeCalculate(void)
#ifndef PLATFORM_N64
if (g_TickExtraSleep) {
sysSleep(EXTRA_SLEEP_TIME);
sysFrameLimiterSleep(EXTRA_SLEEP_TIME);
}
#endif
} while (g_Vars.mininc60 && diffframe60 < g_Vars.mininc60);
+6 -1
View File
@@ -17,7 +17,12 @@ OSMesgQueue *amgrGetFrameMesgQueue(void);
void amgrStopThread(void);
#ifndef PLATFORM_N64
void amgrFrame(void);
void amgrLock(void);
void amgrUnlock(void);
void amgrUpdateFrameCounter(u32 framecount);
#else
#define amgrLock() do {} while(0)
#define amgrUnlock() do {} while(0)
#endif
#endif
+3
View File
@@ -34,6 +34,9 @@
#error "64-bit platforms are currently not supported."
#endif
// threading backend
#define PLATFORM_USE_PTHREAD 1
// detect endianness
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+74
View File
@@ -10,6 +10,13 @@
#include "lib/libc/ll.h"
#include "data.h"
#include "types.h"
#ifndef PLATFORM_N64
#include "lib/audiomgr.h"
#include "thread.h"
#include "audio.h"
#include "system.h"
#define AMGR_THREAD_SLEEP_US 2000
#endif
u32 var80091560;
u32 var80091564;
@@ -43,6 +50,14 @@ void *g_AudioSp;
u32 var8005cf90 = 0x00000000;
u8 var8005cf94 = 1;
#ifndef PLATFORM_N64
systhread g_AmgrThread;
sysmutex g_AmgrMutex = SYSMUTEX_STATIC_INIT;
u32 g_AmgrFrame;
u32 g_AmgrFrameTarget;
void *amgrThread(void *arg);
#endif
void amgrHandleDoneMsg(AudioInfo *info);
void amgrHandleFrameMsg(AudioInfo *info, AudioInfo *previnfo);
void amgrMain(void *arg);
@@ -155,6 +170,9 @@ void amgrStartThread(void)
{
osStartThread(&g_AudioManager.thread);
g_AudioIsThreadRunning = true;
#ifndef PLATFORM_N64
sysThreadCreate(&g_AmgrThread, &amgrThread, NULL);
#endif
}
OSMesgQueue *amgrGetFrameMesgQueue(void)
@@ -171,6 +189,12 @@ void amgrStopThread(void)
{
if (g_AudioIsThreadRunning) {
osStopThread(&g_AudioManager.thread);
#ifndef PLATFORM_N64
amgrLock();
g_AudioIsThreadRunning = false;
amgrUnlock();
sysThreadJoin(&g_AmgrThread, NULL);
#endif
}
}
@@ -321,11 +345,14 @@ void amgrHandleDoneMsg(AudioInfo *info)
}
#ifndef PLATFORM_N64
void amgrFrame(void)
{
static AudioInfo *previnfo = NULL;
static s32 count = 0;
amgrLock();
var80091588 = osGetTime();
AudioInfo *info = g_AudioManager.audioInfo[g_AdmaCurFrame % 3];
@@ -363,6 +390,8 @@ void amgrFrame(void)
count++;
amgrUnlock();
var80091590 = osGetTime();
var80091570 = var80091590 - var80091588;
@@ -377,4 +406,49 @@ void amgrFrame(void)
var80091568 = var80091590 - var80091588;
}
}
void amgrLock(void)
{
sysMutexLock(&g_AmgrMutex);
}
void amgrUnlock(void)
{
sysMutexUnlock(&g_AmgrMutex);
}
void amgrUpdateFrameCounter(u32 newframes)
{
amgrLock();
g_AmgrFrameTarget = newframes;
amgrUnlock();
}
void *amgrThread(void *arg)
{
const f64 frametime = 1000000.0 / (PAL ? 50.0 : 60.0);
bool running = g_AudioIsThreadRunning;
f64 nexttick = sysGetMicroseconds();
f64 tick;
while (running)
{
tick = sysGetMicroseconds() + 0.01;
while (nexttick < tick) {
amgrFrame();
g_AmgrFrame++;
nexttick += frametime;
}
sysSleep(AMGR_THREAD_SLEEP_US);
amgrLock();
running = g_AudioIsThreadRunning;
amgrUnlock();
}
return NULL;
}
#endif