From 4ef8cf8b3d9ad5235aa858a68f70a8aaf2b356da Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Mon, 3 Jul 2023 21:40:42 +0100 Subject: Sound queue This is to queue sounds and play them when we update the screen, so the sounds are synced with the action. It is required because the MikMod_Update function updates on the clock int, and is not synced with the VGA's vsync. --- src/sound.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/sound.c') diff --git a/src/sound.c b/src/sound.c index f57c133..4a608cf 100644 --- a/src/sound.c +++ b/src/sound.c @@ -29,6 +29,11 @@ static uint32_t voice = 0; static MODULE *music = NULL; +#define MAX_QUEUE 4 + +static uint8_t queue[MAX_QUEUE]; +static uint8_t queue_top; + uint8_t sound_init() { MikMod_RegisterAllDrivers(); @@ -61,6 +66,8 @@ uint8_t sound_init() Player_Start(music); MikMod_EnableOutput(); + queue_top = 0; + return 1; } @@ -73,7 +80,9 @@ void sound_update() if (++divider == 10) { divider = 0; + MikMod_Lock(); MikMod_Update(); + MikMod_Unlock(); } } @@ -114,3 +123,21 @@ void sound_play_efx(uint8_t efxno) cur = efxno; voice = Sample_Play(efx[cur].s, 0, 0); } + +void sound_queue_efx(uint8_t efxno) +{ + if (queue_top >= MAX_QUEUE) + return; + + queue[queue_top++] = efxno; +} + +void sound_play_queue() +{ + while (queue_top > 0) + sound_play_efx(queue[--queue_top]); + + MikMod_Lock(); + MikMod_Update(); + MikMod_Unlock(); +} -- cgit v1.2.3