From 063df3243d36334dd485a6f9120a099e8332150e Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 6 Aug 2023 18:08:21 +0200 Subject: [PATCH] port: make audio tick at 30hz, add hack to prevent popping this is a shit solution, but it'll do for now --- port/include/audio.h | 1 + port/src/audio.c | 14 +++++++++----- port/src/libultra.c | 19 ++----------------- port/src/pdsched.c | 6 ++++-- src/lib/audiomgr.c | 4 ++++ 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/port/include/audio.h b/port/include/audio.h index f9060d376..32fc16cbe 100644 --- a/port/include/audio.h +++ b/port/include/audio.h @@ -5,6 +5,7 @@ s32 audioInit(void); s32 audioGetBytesBuffered(void); +s32 audioGetSamplesBuffered(void); void audioSetNextBuffer(const s16 *buf, u32 len); void audioEndFrame(void); diff --git a/port/src/audio.c b/port/src/audio.c index 28887c969..a363228ec 100644 --- a/port/src/audio.c +++ b/port/src/audio.c @@ -3,9 +3,6 @@ #include #include "audio.h" -#define SAMPLES_HIGH 736 -#define SAMPLES_LOW 720 - static SDL_AudioDeviceID dev; static const s16 *nextBuf; static u32 nextSize = 0; @@ -19,7 +16,7 @@ s32 audioInit(void) SDL_AudioSpec want, have; SDL_zero(want); - want.freq = 22020; + want.freq = 22020; // TODO: this might cause trouble for some platforms want.format = AUDIO_S16SYS; want.channels = 2; want.samples = 512; @@ -43,6 +40,11 @@ s32 audioGetBytesBuffered(void) return SDL_GetQueuedAudioSize(dev); } +s32 audioGetSamplesBuffered(void) +{ + return audioGetBytesBuffered() / 4; +} + void audioSetNextBuffer(const s16 *buf, u32 len) { nextBuf = buf; @@ -52,7 +54,9 @@ void audioSetNextBuffer(const s16 *buf, u32 len) void audioEndFrame(void) { if (nextBuf && nextSize) { - SDL_QueueAudio(dev, nextBuf, nextSize); + if (audioGetSamplesBuffered() < 8192) { + SDL_QueueAudio(dev, nextBuf, nextSize); + } nextBuf = NULL; nextSize = 0; } diff --git a/port/src/libultra.c b/port/src/libultra.c index 3c5c13ce3..60ae71eb6 100644 --- a/port/src/libultra.c +++ b/port/src/libultra.c @@ -145,23 +145,8 @@ void osViSetYScale(f32 value) s32 osAiSetFrequency(u32 frequency) { - register unsigned int dacRate; - register char bitRate; - register float f; - f = osViClock / (float)frequency + .5f; - dacRate = f; - - if (dacRate < AI_MIN_DAC_RATE) { - return -1; - } - - bitRate = dacRate / 66; - - if (bitRate > AI_MAX_BIT_RATE) { - bitRate = AI_MAX_BIT_RATE; - } - - return osViClock / (s32)dacRate; + // we can allow for any freq + return (s32)frequency; } u32 osAiGetLength(void) diff --git a/port/src/pdsched.c b/port/src/pdsched.c index 1bdec5e55..77ed28034 100644 --- a/port/src/pdsched.c +++ b/port/src/pdsched.c @@ -235,11 +235,13 @@ void schedStartFrame(OSSched *sc) void schedAudioFrame(OSSched *sc) { - if (!g_SndDisabled) - { + // audio ticks at 30hz + static s32 runFrame = 0; + if (!g_SndDisabled && runFrame) { amgrFrame(); audioEndFrame(); } + runFrame ^= 1; } /** diff --git a/src/lib/audiomgr.c b/src/lib/audiomgr.c index a4ef72a72..5533a0880 100644 --- a/src/lib/audiomgr.c +++ b/src/lib/audiomgr.c @@ -258,6 +258,10 @@ void amgrHandleFrameMsg(AudioInfo *info, AudioInfo *previnfo) somevalue = IO_READ(OS_PHYSICAL_TO_K1(AI_LEN_REG)) / 4; #else somevalue = osAiGetLength() / 4; + // HACK: only allow small frames if really needed + if (somevalue < 1100) { + somevalue = 248; + } #endif datastart = g_AudioManager.ACMDList[var8005cf90]; outbuffer = (s16 *) osVirtualToPhysical(info->data);