diff --git a/src/include/lib/lib_317f0.h b/src/include/lib/lib_317f0.h index 3f0ba7d41..0cdd829f5 100644 --- a/src/include/lib/lib_317f0.h +++ b/src/include/lib/lib_317f0.h @@ -7,9 +7,9 @@ void func00033090(struct sndstate *handle); void func00033100(struct sndstate *state); u16 sndpCountStates(s16 *numfreeptr, s16 *numallocedptr); -void func00033378(void *fn); +void sndpSetAddRefCallback(void *fn); struct sndstate *func00033390(s32 arg0, ALSound *sound); -void func00033634(void *fn); +void sndpSetRemoveRefCallback(void *fn); s32 sndGetState(struct sndstate *handle); struct sndstate *func00033820(s32 arg0, s16 soundnum, u16 vol, ALPan pan, f32 pitch, u8 fxmix, u8 fxbus, struct sndstate **handleptr); void audioStop(struct sndstate *handle); diff --git a/src/include/types.h b/src/include/types.h index fc362e54c..97ec15640 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -7023,7 +7023,7 @@ struct mp3vars { struct sndcache { /*0x0000*/ u16 *indexes; // indexed by sfxnum, value is cache index (0-44) or 0xffff - /*0x0004*/ u8 permanent[45]; + /*0x0004*/ u8 refcounts[45]; /*0x0032*/ u16 ages[45]; /*0x008c*/ ALEnvelope envelopes[45]; /*0x035c*/ ALKeyMap keymaps[45]; diff --git a/src/lib/naudio/n_sndplayer.c b/src/lib/naudio/n_sndplayer.c index aed694c88..5367ca157 100644 --- a/src/lib/naudio/n_sndplayer.c +++ b/src/lib/naudio/n_sndplayer.c @@ -19,11 +19,11 @@ N_ALSndPlayer *g_SndPlayer = &var8009c2d0; s16 var8005f130 = 0; s32 var8005f134 = 0; s32 var8005f138 = 0; -void (*var8005f13c)(ALSound *) = NULL; -void (*var8005f140)(ALSound *) = NULL; +void (*g_SndpAddRefCallback)(ALSound *) = NULL; +void (*g_SndpRemoveRefCallback)(ALSound *) = NULL; -void func00033378(void *fn); -void func00033634(void *fn); +void sndpSetAddRefCallback(void *fn); +void sndpSetRemoveRefCallback(void *fn); void sndpFreeState(struct sndstate *state); void func00033bc0(struct sndstate *state); @@ -52,8 +52,8 @@ void n_alSndpNew(ALSndpConfig *config) alLink(&sndstate[i].node, &sndstate[i - 1].node); } - func00033378(NULL); - func00033634(NULL); + sndpSetAddRefCallback(NULL); + sndpSetRemoveRefCallback(NULL); var8009c334 = alHeapAlloc(config->heap, sizeof(s16), config->unk10); @@ -547,9 +547,9 @@ u16 sndpCountStates(s16 *numfreeptr, s16 *numallocedptr) return numalloced2; } -void func00033378(void *fn) +void sndpSetAddRefCallback(void *fn) { - var8005f13c = fn; + g_SndpAddRefCallback = fn; } struct sndstate *func00033390(s32 arg0, ALSound *sound) @@ -614,8 +614,8 @@ struct sndstate *func00033390(s32 arg0, ALSound *sound) state->pan = AL_PAN_CENTER; state->vol = 0x7fff; - if (var8005f13c != NULL) { - var8005f13c(state->sound); + if (g_SndpAddRefCallback != NULL) { + g_SndpAddRefCallback(state->sound); } } else { osSetIntMask(mask); @@ -624,9 +624,9 @@ struct sndstate *func00033390(s32 arg0, ALSound *sound) return state; } -void func00033634(void *fn) +void sndpSetRemoveRefCallback(void *fn) { - var8005f140 = fn; + g_SndpRemoveRefCallback = fn; } void sndpFreeState(struct sndstate *state) @@ -667,8 +667,8 @@ void sndpFreeState(struct sndstate *state) state->unk30 = NULL; } - if (var8005f140) { - var8005f140(state->sound); + if (g_SndpRemoveRefCallback != NULL) { + g_SndpRemoveRefCallback(state->sound); } } diff --git a/src/lib/snd.c b/src/lib/snd.c index c89228d06..2c46c137a 100644 --- a/src/lib/snd.c +++ b/src/lib/snd.c @@ -976,7 +976,7 @@ void sndLoadSfxCtl(void) for (i = 0; i < 45; i++) { g_SndCache.ages[i] = 1; - g_SndCache.permanent[i] = 0; + g_SndCache.refcounts[i] = 0; } } #else @@ -1119,7 +1119,7 @@ void sndIncrementAges(void) s32 i; for (i = 0; i < 45; i++) { - if (!g_SndCache.permanent[i] && g_SndCache.ages[i] < 32000) { + if (g_SndCache.refcounts[i] == 0 && g_SndCache.ages[i] < 32000) { g_SndCache.ages[i]++; } } @@ -1442,7 +1442,7 @@ ALSound *sndLoadSound(s16 soundnum) oldestage = 0; for (i = 0; i < 45; i++) { - if (!g_SndCache.permanent[i] && oldestage < g_SndCache.ages[i]) { + if (g_SndCache.refcounts[i] == 0 && oldestage < g_SndCache.ages[i]) { oldestage = g_SndCache.ages[i]; oldestindex = i; } @@ -1648,36 +1648,18 @@ void seqInit(struct seqinstance *seq) n_alCSPSetBank(seq->seqp, var80095204); } -void snd0000f76c(void); +void sndAddRef(ALSound *sound) +{ + if (sound >= &g_SndCache.sounds[0] && sound <= &g_SndCache.sounds[44]) { + s32 cacheindex = sound - g_SndCache.sounds; + g_SndCache.refcounts[cacheindex]++; + } +} + +void sndRemoveRef(void); GLOBAL_ASM( -glabel snd0000f76c -/* f76c: 3c0e800a */ lui $t6,%hi(g_SndCache+0x3e14) -/* f770: 25ce9024 */ addiu $t6,$t6,%lo(g_SndCache+0x3e14) -/* f774: 008e082b */ sltu $at,$a0,$t6 -/* f778: 1420000d */ bnez $at,.L0000f7b0 -/* f77c: 3c0f800a */ lui $t7,%hi(g_SndCache+0x40d4) -/* f780: 25ef92e4 */ addiu $t7,$t7,%lo(g_SndCache+0x40d4) -/* f784: 01e4082b */ sltu $at,$t7,$a0 -/* f788: 14200009 */ bnez $at,.L0000f7b0 -/* f78c: 3c038009 */ lui $v1,%hi(g_SndCache) -/* f790: 24635210 */ addiu $v1,$v1,%lo(g_SndCache) -/* f794: 0083c023 */ subu $t8,$a0,$v1 -/* f798: 2719c1ec */ addiu $t9,$t8,-15892 -/* f79c: 00194103 */ sra $t0,$t9,0x4 -/* f7a0: 00681021 */ addu $v0,$v1,$t0 -/* f7a4: 90490004 */ lbu $t1,0x4($v0) -/* f7a8: 252a0001 */ addiu $t2,$t1,0x1 -/* f7ac: a04a0004 */ sb $t2,0x4($v0) -.L0000f7b0: -/* f7b0: 03e00008 */ jr $ra -/* f7b4: 00000000 */ nop -); - -void snd0000f7b8(void); - -GLOBAL_ASM( -glabel snd0000f7b8 +glabel sndRemoveRef /* f7b8: 3c0e800a */ lui $t6,%hi(g_SndCache+0x3e14) /* f7bc: 25ce9024 */ addiu $t6,$t6,%lo(g_SndCache+0x3e14) /* f7c0: 008e082b */ sltu $at,$a0,$t6 @@ -1838,8 +1820,8 @@ void sndInit(void) osSyncPrintf("Set the sample callbacks\n"); - func00033378(snd0000f76c); - func00033634(snd0000f7b8); + sndpSetAddRefCallback(sndAddRef); + sndpSetRemoveRefCallback(sndRemoveRef); amgrStartThread();