2020-10-03 15:22:44 +00:00
|
|
|
#include "ultra64.h"
|
|
|
|
#include "global.h"
|
2020-10-26 11:58:56 +00:00
|
|
|
#include "vt.h"
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2020-10-26 11:58:56 +00:00
|
|
|
typedef struct {
|
|
|
|
/* 0x00 */ u16 sfxId;
|
|
|
|
/* 0x04 */ Vec3f* pos;
|
2021-11-07 16:58:50 +00:00
|
|
|
/* 0x08 */ u8 token;
|
2021-08-30 00:08:41 +00:00
|
|
|
/* 0x0C */ f32* freqScale;
|
2021-11-07 16:58:50 +00:00
|
|
|
/* 0x10 */ f32* vol;
|
2021-08-30 00:08:41 +00:00
|
|
|
/* 0x14 */ s8* reverbAdd;
|
|
|
|
} SoundRequest; // size = 0x18
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2020-10-26 11:58:56 +00:00
|
|
|
typedef struct {
|
2021-11-07 16:58:50 +00:00
|
|
|
/* 0x00 */ f32 value;
|
|
|
|
/* 0x04 */ f32 target;
|
|
|
|
/* 0x08 */ f32 step;
|
|
|
|
/* 0x0C */ u16 remainingFrames;
|
|
|
|
} UnusedBankLerp; // size = 0x10
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
// rodata for Audio_ProcessSoundRequest (this file)
|
|
|
|
// (probably moved to .data due to -use_readwrite_const)
|
2021-08-12 19:37:37 +00:00
|
|
|
char D_80133340[] = "SE";
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
// rodata for Audio_ChooseActiveSounds (this file)
|
2020-10-26 11:58:56 +00:00
|
|
|
char D_80133344[] = VT_COL(RED, WHITE) "<INAGAKI CHECK> dist over! flag:%04X ptr:%08X pos:%f-%f-%f" VT_RST "\n";
|
|
|
|
|
2021-08-12 19:37:37 +00:00
|
|
|
// file padding
|
2020-10-26 11:58:56 +00:00
|
|
|
s32 D_8013338C = 0;
|
|
|
|
|
2021-08-12 19:37:37 +00:00
|
|
|
// rodata for Audio_ProcessSeqCmd (code_800F9280.c)
|
|
|
|
char D_80133390[] = "SEQ H";
|
|
|
|
char D_80133398[] = " L";
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2022-01-12 01:58:26 +00:00
|
|
|
SoundBankEntry D_8016BAD0[9];
|
|
|
|
SoundBankEntry D_8016BC80[12];
|
|
|
|
SoundBankEntry D_8016BEC0[22];
|
|
|
|
SoundBankEntry D_8016C2E0[20];
|
|
|
|
SoundBankEntry D_8016C6A0[8];
|
|
|
|
SoundBankEntry D_8016C820[3];
|
|
|
|
SoundBankEntry D_8016C8B0[5];
|
|
|
|
SoundRequest sSoundRequests[0x100];
|
|
|
|
u8 sSoundBankListEnd[7];
|
|
|
|
u8 sSoundBankFreeListStart[7];
|
|
|
|
u8 sSoundBankUnused[7];
|
|
|
|
ActiveSound gActiveSounds[7][3];
|
|
|
|
u8 sCurSfxPlayerChannelIdx;
|
|
|
|
u8 gSoundBankMuted[7];
|
|
|
|
UnusedBankLerp sUnusedBankLerp[7];
|
|
|
|
u16 gAudioSfxSwapSource[10];
|
|
|
|
u16 gAudioSfxSwapTarget[10];
|
|
|
|
u8 gAudioSfxSwapMode[10];
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
// sSoundRequests ring buffer endpoints. read index <= write index, wrapping around mod 256.
|
|
|
|
u8 sSoundRequestWriteIndex = 0;
|
|
|
|
u8 sSoundRequestReadIndex = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of pointers to arrays of SoundBankEntry of sizes: 9, 12, 22, 20, 8, 3, 5
|
|
|
|
*
|
|
|
|
* 0 : Player Bank size 9
|
|
|
|
* 1 : Item Bank size 12
|
|
|
|
* 2 : Environment Bank size 22
|
|
|
|
* 3 : Enemy Bank size 20
|
|
|
|
* 4 : System Bank size 8
|
|
|
|
* 5 : Ocarina Bank size 3
|
|
|
|
* 6 : Voice Bank size 5
|
|
|
|
*/
|
|
|
|
SoundBankEntry* gSoundBanks[7] = {
|
|
|
|
D_8016BAD0, D_8016BC80, D_8016BEC0, D_8016C2E0, D_8016C6A0, D_8016C820, D_8016C8B0,
|
|
|
|
};
|
|
|
|
|
|
|
|
u8 sBankSizes[ARRAY_COUNT(gSoundBanks)] = {
|
|
|
|
ARRAY_COUNT(D_8016BAD0), ARRAY_COUNT(D_8016BC80), ARRAY_COUNT(D_8016BEC0), ARRAY_COUNT(D_8016C2E0),
|
|
|
|
ARRAY_COUNT(D_8016C6A0), ARRAY_COUNT(D_8016C820), ARRAY_COUNT(D_8016C8B0),
|
|
|
|
};
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 gSfxChannelLayout = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
|
|
|
u16 D_801333D0 = 0;
|
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
Vec3f D_801333D4 = { 0.0f, 0.0f, 0.0f }; // default pos
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
f32 D_801333E0 = 1.0f; // default freqScale
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
s32 D_801333E4 = 0; // unused
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
s8 D_801333E8 = 0; // default reverbAdd
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
s32 D_801333EC = 0; // unused
|
2020-10-26 11:58:56 +00:00
|
|
|
|
|
|
|
u8 D_801333F0 = 0;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 gAudioSfxSwapOff = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
|
|
|
u8 D_801333F8 = 0;
|
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
void Audio_SetSoundBanksMute(u16 muteMask) {
|
2020-10-26 11:58:56 +00:00
|
|
|
u8 bankId;
|
|
|
|
|
|
|
|
for (bankId = 0; bankId < ARRAY_COUNT(gSoundBanks); bankId++) {
|
2021-08-30 00:08:41 +00:00
|
|
|
if (muteMask & 1) {
|
|
|
|
gSoundBankMuted[bankId] = true;
|
2020-10-26 11:58:56 +00:00
|
|
|
} else {
|
2021-08-30 00:08:41 +00:00
|
|
|
gSoundBankMuted[bankId] = false;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
muteMask = muteMask >> 1;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_QueueSeqCmdMute(u8 channelIdx) {
|
|
|
|
D_801333D0 |= (1 << channelIdx);
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_SetVolScale(SEQ_PLAYER_BGM_MAIN, 2, 0x40, 0xF);
|
|
|
|
Audio_SetVolScale(SEQ_PLAYER_BGM_SUB, 2, 0x40, 0xF);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_ClearBGMMute(u8 channelIdx) {
|
|
|
|
D_801333D0 &= ((1 << channelIdx) ^ 0xFFFF);
|
2020-10-26 11:58:56 +00:00
|
|
|
if (D_801333D0 == 0) {
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_SetVolScale(SEQ_PLAYER_BGM_MAIN, 2, 0x7F, 0xF);
|
|
|
|
Audio_SetVolScale(SEQ_PLAYER_BGM_SUB, 2, 0x7F, 0xF);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_PlaySoundGeneral(u16 sfxId, Vec3f* pos, u8 token, f32* freqScale, f32* vol, s8* reverbAdd) {
|
2020-10-26 11:58:56 +00:00
|
|
|
u8 i;
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundRequest* req;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
if (!gSoundBankMuted[SFX_BANK_SHIFT(sfxId)]) {
|
|
|
|
req = &sSoundRequests[sSoundRequestWriteIndex];
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!gAudioSfxSwapOff) {
|
2020-10-26 11:58:56 +00:00
|
|
|
for (i = 0; i < 10; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (sfxId == gAudioSfxSwapSource[i]) {
|
|
|
|
if (gAudioSfxSwapMode[i] == 0) { // "SWAP"
|
|
|
|
sfxId = gAudioSfxSwapTarget[i];
|
2021-08-30 00:08:41 +00:00
|
|
|
} else { // "ADD"
|
2021-11-07 16:58:50 +00:00
|
|
|
req->sfxId = gAudioSfxSwapTarget[i];
|
2021-08-30 00:08:41 +00:00
|
|
|
req->pos = pos;
|
2021-11-07 16:58:50 +00:00
|
|
|
req->token = token;
|
2021-08-30 00:08:41 +00:00
|
|
|
req->freqScale = freqScale;
|
2021-11-07 16:58:50 +00:00
|
|
|
req->vol = vol;
|
2021-08-30 00:08:41 +00:00
|
|
|
req->reverbAdd = reverbAdd;
|
|
|
|
sSoundRequestWriteIndex++;
|
|
|
|
req = &sSoundRequests[sSoundRequestWriteIndex];
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
i = 10; // "break;"
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
req->sfxId = sfxId;
|
|
|
|
req->pos = pos;
|
2021-11-07 16:58:50 +00:00
|
|
|
req->token = token;
|
2021-08-30 00:08:41 +00:00
|
|
|
req->freqScale = freqScale;
|
2021-11-07 16:58:50 +00:00
|
|
|
req->vol = vol;
|
2021-08-30 00:08:41 +00:00
|
|
|
req->reverbAdd = reverbAdd;
|
|
|
|
sSoundRequestWriteIndex++;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
void Audio_RemoveMatchingSoundRequests(u8 aspect, SoundBankEntry* cmp) {
|
|
|
|
SoundRequest* req;
|
|
|
|
s32 remove;
|
|
|
|
u8 i = sSoundRequestReadIndex;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
for (; i != sSoundRequestWriteIndex; i++) {
|
|
|
|
remove = false;
|
|
|
|
req = &sSoundRequests[i];
|
|
|
|
switch (aspect) {
|
2020-10-26 11:58:56 +00:00
|
|
|
case 0:
|
2021-08-31 13:22:56 +00:00
|
|
|
if (SFX_BANK_MASK(req->sfxId) == SFX_BANK_MASK(cmp->sfxId)) {
|
2021-08-30 00:08:41 +00:00
|
|
|
remove = true;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2021-08-31 13:22:56 +00:00
|
|
|
if (SFX_BANK_MASK(req->sfxId) == SFX_BANK_MASK(cmp->sfxId) && (&req->pos->x == cmp->posX)) {
|
2021-08-30 00:08:41 +00:00
|
|
|
remove = true;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2021-08-30 00:08:41 +00:00
|
|
|
if (&req->pos->x == cmp->posX) {
|
|
|
|
remove = true;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
2021-08-31 13:22:56 +00:00
|
|
|
if (&req->pos->x == cmp->posX && req->sfxId == cmp->sfxId) {
|
2021-08-30 00:08:41 +00:00
|
|
|
remove = true;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
2021-11-07 16:58:50 +00:00
|
|
|
if (req->token == cmp->token && req->sfxId == cmp->sfxId) {
|
2021-08-30 00:08:41 +00:00
|
|
|
remove = true;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
2021-08-31 13:22:56 +00:00
|
|
|
if (req->sfxId == cmp->sfxId) {
|
2021-08-30 00:08:41 +00:00
|
|
|
remove = true;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
if (remove) {
|
|
|
|
req->sfxId = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
void Audio_ProcessSoundRequest(void) {
|
2021-08-31 13:22:56 +00:00
|
|
|
u16 sfxId;
|
|
|
|
u8 count;
|
|
|
|
u8 index;
|
|
|
|
SoundRequest* req;
|
|
|
|
SoundBankEntry* entry;
|
2021-09-01 22:33:37 +00:00
|
|
|
SoundParams* soundParams;
|
2021-08-31 13:22:56 +00:00
|
|
|
s32 bankId;
|
2021-09-01 22:33:37 +00:00
|
|
|
u8 evictImportance;
|
|
|
|
u8 evictIndex;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
req = &sSoundRequests[sSoundRequestReadIndex];
|
2021-09-01 22:33:37 +00:00
|
|
|
evictIndex = 0x80;
|
2021-08-30 00:08:41 +00:00
|
|
|
if (req->sfxId == 0) {
|
2020-10-26 11:58:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
bankId = SFX_BANK(req->sfxId);
|
|
|
|
if ((1 << bankId) & D_801333F0) {
|
2021-08-30 00:08:41 +00:00
|
|
|
AudioDebug_ScrPrt((const s8*)D_80133340, req->sfxId);
|
2021-08-31 13:22:56 +00:00
|
|
|
bankId = SFX_BANK(req->sfxId);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
count = 0;
|
|
|
|
index = gSoundBanks[bankId][0].next;
|
|
|
|
while (index != 0xFF && index != 0) {
|
|
|
|
if (gSoundBanks[bankId][index].posX == &req->pos->x) {
|
2021-09-01 22:33:37 +00:00
|
|
|
if ((gSoundParams[SFX_BANK_SHIFT(req->sfxId)][SFX_INDEX(req->sfxId)].params & 0x20) &&
|
|
|
|
gSoundParams[SFX_BANK_SHIFT(req->sfxId)][SFX_INDEX(req->sfxId)].importance ==
|
|
|
|
gSoundBanks[bankId][index].sfxImportance) {
|
2021-03-27 20:01:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
if (gSoundBanks[bankId][index].sfxId == req->sfxId) {
|
2021-11-07 16:58:50 +00:00
|
|
|
count = gUsedChannelsPerBank[gSfxChannelLayout][bankId];
|
2021-03-27 20:01:50 +00:00
|
|
|
} else {
|
2021-08-31 13:22:56 +00:00
|
|
|
if (count == 0) {
|
2021-09-01 22:33:37 +00:00
|
|
|
evictIndex = index;
|
2021-08-31 13:22:56 +00:00
|
|
|
sfxId = gSoundBanks[bankId][index].sfxId & 0xFFFF;
|
2021-09-01 22:33:37 +00:00
|
|
|
evictImportance = gSoundParams[SFX_BANK_SHIFT(sfxId)][SFX_INDEX(sfxId)].importance;
|
|
|
|
} else if (gSoundBanks[bankId][index].sfxImportance < evictImportance) {
|
|
|
|
evictIndex = index;
|
2021-08-31 13:22:56 +00:00
|
|
|
sfxId = gSoundBanks[bankId][index].sfxId & 0xFFFF;
|
2021-09-01 22:33:37 +00:00
|
|
|
evictImportance = gSoundParams[SFX_BANK_SHIFT(sfxId)][SFX_INDEX(sfxId)].importance;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
count++;
|
2021-11-07 16:58:50 +00:00
|
|
|
if (count == gUsedChannelsPerBank[gSfxChannelLayout][bankId]) {
|
2021-09-01 22:33:37 +00:00
|
|
|
if (gSoundParams[SFX_BANK_SHIFT(req->sfxId)][SFX_INDEX(req->sfxId)].importance >= evictImportance) {
|
|
|
|
index = evictIndex;
|
|
|
|
} else {
|
|
|
|
index = 0;
|
|
|
|
}
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (count == gUsedChannelsPerBank[gSfxChannelLayout][bankId]) {
|
2021-09-01 22:33:37 +00:00
|
|
|
soundParams = &gSoundParams[SFX_BANK_SHIFT(req->sfxId)][SFX_INDEX(req->sfxId)];
|
|
|
|
if ((req->sfxId & 0xC00) || (soundParams->params & 4) || (index == evictIndex)) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if ((gSoundBanks[bankId][index].sfxParams & 8) &&
|
|
|
|
gSoundBanks[bankId][index].state != SFX_STATE_QUEUED) {
|
|
|
|
Audio_ClearBGMMute(gSoundBanks[bankId][index].channelIdx);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
gSoundBanks[bankId][index].token = req->token;
|
2021-08-31 13:22:56 +00:00
|
|
|
gSoundBanks[bankId][index].sfxId = req->sfxId;
|
2021-11-07 16:58:50 +00:00
|
|
|
gSoundBanks[bankId][index].state = SFX_STATE_QUEUED;
|
|
|
|
gSoundBanks[bankId][index].freshness = 2;
|
2021-08-31 13:22:56 +00:00
|
|
|
gSoundBanks[bankId][index].freqScale = req->freqScale;
|
2021-11-07 16:58:50 +00:00
|
|
|
gSoundBanks[bankId][index].vol = req->vol;
|
2021-08-31 13:22:56 +00:00
|
|
|
gSoundBanks[bankId][index].reverbAdd = req->reverbAdd;
|
2021-09-01 22:33:37 +00:00
|
|
|
gSoundBanks[bankId][index].sfxParams = soundParams->params;
|
|
|
|
gSoundBanks[bankId][index].sfxImportance = soundParams->importance;
|
2021-11-07 16:58:50 +00:00
|
|
|
} else if (gSoundBanks[bankId][index].state == SFX_STATE_PLAYING_2) {
|
|
|
|
gSoundBanks[bankId][index].state = SFX_STATE_PLAYING_1;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
index = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
if (index != 0) {
|
|
|
|
index = gSoundBanks[bankId][index].next;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (gSoundBanks[bankId][sSoundBankFreeListStart[bankId]].next != 0xFF && index != 0) {
|
|
|
|
index = sSoundBankFreeListStart[bankId];
|
2021-08-31 13:22:56 +00:00
|
|
|
entry = &gSoundBanks[bankId][index];
|
|
|
|
entry->posX = &req->pos->x;
|
|
|
|
entry->posY = &req->pos->y;
|
|
|
|
entry->posZ = &req->pos->z;
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->token = req->token;
|
2021-08-31 13:22:56 +00:00
|
|
|
entry->freqScale = req->freqScale;
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->vol = req->vol;
|
2021-08-31 13:22:56 +00:00
|
|
|
entry->reverbAdd = req->reverbAdd;
|
2021-09-01 22:33:37 +00:00
|
|
|
soundParams = &gSoundParams[SFX_BANK_SHIFT(req->sfxId)][SFX_INDEX(req->sfxId)];
|
|
|
|
entry->sfxParams = soundParams->params;
|
|
|
|
entry->sfxImportance = soundParams->importance;
|
2021-08-31 13:22:56 +00:00
|
|
|
entry->sfxId = req->sfxId;
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->state = SFX_STATE_QUEUED;
|
|
|
|
entry->freshness = 2;
|
|
|
|
entry->prev = sSoundBankListEnd[bankId];
|
|
|
|
gSoundBanks[bankId][sSoundBankListEnd[bankId]].next = sSoundBankFreeListStart[bankId];
|
|
|
|
sSoundBankListEnd[bankId] = sSoundBankFreeListStart[bankId];
|
|
|
|
sSoundBankFreeListStart[bankId] = gSoundBanks[bankId][sSoundBankFreeListStart[bankId]].next;
|
|
|
|
gSoundBanks[bankId][sSoundBankFreeListStart[bankId]].prev = 0xFF;
|
2021-08-31 13:22:56 +00:00
|
|
|
entry->next = 0xFF;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_RemoveSoundBankEntry(u8 bankId, u8 entryIndex) {
|
|
|
|
SoundBankEntry* entry = &gSoundBanks[bankId][entryIndex];
|
2020-10-26 11:58:56 +00:00
|
|
|
u8 i;
|
|
|
|
|
2021-09-01 22:33:37 +00:00
|
|
|
if (entry->sfxParams & 8) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_ClearBGMMute(entry->channelIdx);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entryIndex == sSoundBankListEnd[bankId]) {
|
|
|
|
sSoundBankListEnd[bankId] = entry->prev;
|
2020-10-26 11:58:56 +00:00
|
|
|
} else {
|
|
|
|
gSoundBanks[bankId][entry->next].prev = entry->prev;
|
|
|
|
}
|
|
|
|
gSoundBanks[bankId][entry->prev].next = entry->next;
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->next = sSoundBankFreeListStart[bankId];
|
2020-10-26 11:58:56 +00:00
|
|
|
entry->prev = 0xFF;
|
2021-11-07 16:58:50 +00:00
|
|
|
gSoundBanks[bankId][sSoundBankFreeListStart[bankId]].prev = entryIndex;
|
|
|
|
sSoundBankFreeListStart[bankId] = entryIndex;
|
|
|
|
entry->state = SFX_STATE_EMPTY;
|
|
|
|
|
|
|
|
for (i = 0; i < gChannelsPerBank[gSfxChannelLayout][bankId]; i++) {
|
|
|
|
if (gActiveSounds[bankId][i].entryIndex == entryIndex) {
|
|
|
|
gActiveSounds[bankId][i].entryIndex = 0xFF;
|
|
|
|
i = gChannelsPerBank[gSfxChannelLayout][bankId];
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_ChooseActiveSounds(u8 bankId) {
|
|
|
|
u8 numChosenSounds;
|
|
|
|
u8 numChannels;
|
|
|
|
u8 entryIndex;
|
2021-08-30 00:08:41 +00:00
|
|
|
u8 i;
|
|
|
|
u8 j;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 k;
|
2021-09-01 22:33:37 +00:00
|
|
|
u8 sfxImportance;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 needNewSound;
|
|
|
|
u8 chosenEntryIndex;
|
2021-03-27 20:01:50 +00:00
|
|
|
u16 temp3;
|
|
|
|
f32 tempf1;
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundBankEntry* entry;
|
2021-11-07 16:58:50 +00:00
|
|
|
ActiveSound chosenSounds[MAX_CHANNELS_PER_BANK];
|
|
|
|
ActiveSound* activeSound;
|
2021-03-27 20:01:50 +00:00
|
|
|
s32 pad;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
numChosenSounds = 0;
|
|
|
|
for (i = 0; i < MAX_CHANNELS_PER_BANK; i++) {
|
|
|
|
chosenSounds[i].priority = 0x7FFFFFFF;
|
|
|
|
chosenSounds[i].entryIndex = 0xFF;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
entryIndex = gSoundBanks[bankId][0].next;
|
|
|
|
k = 0;
|
|
|
|
while (entryIndex != 0xFF) {
|
|
|
|
if ((gSoundBanks[bankId][entryIndex].state == SFX_STATE_QUEUED) &&
|
|
|
|
(gSoundBanks[bankId][entryIndex].sfxId & 0xC00)) {
|
|
|
|
gSoundBanks[bankId][entryIndex].freshness--;
|
|
|
|
} else if (!(gSoundBanks[bankId][entryIndex].sfxId & 0xC00) &&
|
|
|
|
(gSoundBanks[bankId][entryIndex].state == SFX_STATE_PLAYING_2)) {
|
|
|
|
Audio_QueueCmdS8((gSoundBanks[bankId][entryIndex].channelIdx << 8) | 0x6020000, 0);
|
|
|
|
Audio_RemoveSoundBankEntry(bankId, entryIndex);
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (gSoundBanks[bankId][entryIndex].freshness == 0) {
|
|
|
|
Audio_RemoveSoundBankEntry(bankId, entryIndex);
|
|
|
|
} else if (gSoundBanks[bankId][entryIndex].state != SFX_STATE_EMPTY) {
|
|
|
|
entry = &gSoundBanks[bankId][entryIndex];
|
2021-03-27 20:01:50 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
if (&D_801333D4.x == entry[0].posX) {
|
|
|
|
entry->dist = 0.0f;
|
2021-03-27 20:01:50 +00:00
|
|
|
} else {
|
2021-08-30 00:08:41 +00:00
|
|
|
tempf1 = *entry->posY * 1;
|
|
|
|
entry->dist = (SQ(*entry->posX) + SQ(tempf1) + SQ(*entry->posZ)) * 1;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-09-01 22:33:37 +00:00
|
|
|
sfxImportance = entry->sfxImportance;
|
|
|
|
if (entry->sfxParams & 0x10) {
|
|
|
|
entry->priority = SQ(0xFF - sfxImportance) * SQ(76);
|
2021-03-27 20:01:50 +00:00
|
|
|
} else {
|
2021-08-30 00:08:41 +00:00
|
|
|
if (entry->dist > 0x7FFFFFD0) {
|
|
|
|
entry->dist = 0x70000008;
|
2021-08-31 13:22:56 +00:00
|
|
|
osSyncPrintf(D_80133344, entry->sfxId, entry->posX, entry->posZ, *entry->posX, *entry->posY,
|
|
|
|
*entry->posZ);
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
temp3 = entry->sfxId; // fake
|
2021-09-01 22:33:37 +00:00
|
|
|
entry->priority = (u32)entry->dist + (SQ(0xFF - sfxImportance) * SQ(76)) + temp3 - temp3;
|
2021-08-30 00:08:41 +00:00
|
|
|
if (*entry->posZ < 0.0f) {
|
2021-09-01 22:33:37 +00:00
|
|
|
entry->priority += (s32)(-*entry->posZ * 6.0f);
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
if (entry->dist > SQ(1e5f)) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state == SFX_STATE_PLAYING_1) {
|
|
|
|
Audio_QueueCmdS8((entry->channelIdx << 8) | 0x6020000, 0);
|
2021-08-31 13:22:56 +00:00
|
|
|
if (entry->sfxId & 0xC00) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_RemoveSoundBankEntry(bankId, entryIndex);
|
|
|
|
entryIndex = k;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
numChannels = gChannelsPerBank[gSfxChannelLayout][bankId];
|
|
|
|
for (i = 0; i < numChannels; i++) {
|
|
|
|
if (chosenSounds[i].priority >= entry->priority) {
|
|
|
|
if (numChosenSounds < gChannelsPerBank[gSfxChannelLayout][bankId]) {
|
|
|
|
numChosenSounds++;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
for (j = numChannels - 1; j > i; j--) {
|
|
|
|
chosenSounds[j].priority = chosenSounds[j - 1].priority;
|
|
|
|
chosenSounds[j].entryIndex = chosenSounds[j - 1].entryIndex;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
chosenSounds[i].priority = entry->priority;
|
|
|
|
chosenSounds[i].entryIndex = entryIndex;
|
|
|
|
i = numChannels; // "break;"
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
k = entryIndex;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
entryIndex = gSoundBanks[bankId][k].next;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < numChosenSounds; i++) {
|
|
|
|
entry = &gSoundBanks[bankId][chosenSounds[i].entryIndex];
|
|
|
|
if (entry->state == SFX_STATE_QUEUED) {
|
|
|
|
entry->state = SFX_STATE_READY;
|
|
|
|
} else if (entry->state == SFX_STATE_PLAYING_1) {
|
|
|
|
entry->state = SFX_STATE_PLAYING_REFRESH;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
// Pick something to play for all channels.
|
|
|
|
numChannels = gChannelsPerBank[gSfxChannelLayout][bankId];
|
|
|
|
for (i = 0; i < numChannels; i++) {
|
|
|
|
needNewSound = false;
|
|
|
|
activeSound = &gActiveSounds[bankId][i];
|
2021-03-27 20:01:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (activeSound->entryIndex == 0xFF) {
|
|
|
|
needNewSound = true;
|
2021-03-27 20:01:50 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
entry = &gSoundBanks[bankId][activeSound[0].entryIndex];
|
|
|
|
if (entry->state == SFX_STATE_PLAYING_1) {
|
2021-08-31 13:22:56 +00:00
|
|
|
if (entry->sfxId & 0xC00) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_RemoveSoundBankEntry(bankId, activeSound->entryIndex);
|
2021-03-27 20:01:50 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->state = SFX_STATE_QUEUED;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
needNewSound = true;
|
|
|
|
} else if (entry->state == SFX_STATE_EMPTY) {
|
|
|
|
activeSound->entryIndex = 0xFF;
|
|
|
|
needNewSound = true;
|
2021-03-27 20:01:50 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
// Sound is already playing as it should, nothing to do.
|
|
|
|
for (j = 0; j < numChannels; j++) {
|
|
|
|
if (activeSound->entryIndex == chosenSounds[j].entryIndex) {
|
|
|
|
chosenSounds[j].entryIndex = 0xFF;
|
|
|
|
j = numChannels;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
numChosenSounds--;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (needNewSound == true) {
|
|
|
|
for (j = 0; j < numChannels; j++) {
|
|
|
|
chosenEntryIndex = chosenSounds[j].entryIndex;
|
|
|
|
if ((chosenEntryIndex != 0xFF) &&
|
|
|
|
(gSoundBanks[bankId][chosenEntryIndex].state != SFX_STATE_PLAYING_REFRESH)) {
|
|
|
|
for (k = 0; k < numChannels; k++) {
|
|
|
|
if (chosenEntryIndex == gActiveSounds[bankId][k].entryIndex) {
|
|
|
|
needNewSound = false;
|
|
|
|
k = numChannels; // "break;"
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (needNewSound == true) {
|
|
|
|
activeSound->entryIndex = chosenEntryIndex;
|
|
|
|
chosenSounds[j].entryIndex = 0xFF;
|
|
|
|
j = numChannels + 1;
|
|
|
|
numChosenSounds--;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (j == numChannels) {
|
|
|
|
// nothing found
|
|
|
|
activeSound->entryIndex = 0xFF;
|
2021-03-27 20:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_PlayActiveSounds(u8 bankId) {
|
|
|
|
u8 entryIndex;
|
|
|
|
SequenceChannel* channel;
|
2020-10-26 11:58:56 +00:00
|
|
|
SoundBankEntry* entry;
|
|
|
|
u8 i;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gChannelsPerBank[gSfxChannelLayout][bankId]; i++) {
|
|
|
|
entryIndex = gActiveSounds[bankId][i].entryIndex;
|
|
|
|
if (entryIndex != 0xFF) {
|
|
|
|
entry = &gSoundBanks[bankId][entryIndex];
|
2021-11-30 18:47:09 +00:00
|
|
|
channel = gAudioContext.seqPlayers[SEQ_PLAYER_SFX].channels[sCurSfxPlayerChannelIdx];
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state == SFX_STATE_READY) {
|
|
|
|
entry->channelIdx = sCurSfxPlayerChannelIdx;
|
2021-09-01 22:33:37 +00:00
|
|
|
if (entry->sfxParams & 8) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_QueueSeqCmdMute(sCurSfxPlayerChannelIdx);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-09-01 22:33:37 +00:00
|
|
|
if (entry->sfxParams & 0xC0) {
|
|
|
|
switch (entry->sfxParams & 0xC0) {
|
2020-10-26 11:58:56 +00:00
|
|
|
case 0x40:
|
|
|
|
entry->unk_2F = Audio_NextRandom() & 0xF;
|
|
|
|
break;
|
|
|
|
case 0x80:
|
|
|
|
entry->unk_2F = Audio_NextRandom() & 0x1F;
|
|
|
|
break;
|
|
|
|
case 0xC0:
|
|
|
|
entry->unk_2F = Audio_NextRandom() & 0x3F;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
entry->unk_2F = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_SetSoundProperties(bankId, entryIndex, sCurSfxPlayerChannelIdx);
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((sCurSfxPlayerChannelIdx & 0xFF) << 8), 1);
|
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((sCurSfxPlayerChannelIdx & 0xFF) << 8) | 4,
|
|
|
|
entry->sfxId & 0xFF);
|
2021-11-07 16:58:50 +00:00
|
|
|
if (gIsLargeSoundBank[bankId]) {
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((sCurSfxPlayerChannelIdx & 0xFF) << 8) | 5,
|
2021-11-07 16:58:50 +00:00
|
|
|
(entry->sfxId & 0x100) >> 8);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
if (entry->sfxId & 0xC00) {
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->state = SFX_STATE_PLAYING_1;
|
2020-10-26 11:58:56 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->state = SFX_STATE_PLAYING_2;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
} else if ((u8)channel->soundScriptIO[1] == 0xFF) {
|
|
|
|
Audio_RemoveSoundBankEntry(bankId, entryIndex);
|
|
|
|
} else if (entry->state == SFX_STATE_PLAYING_REFRESH) {
|
|
|
|
Audio_SetSoundProperties(bankId, entryIndex, sCurSfxPlayerChannelIdx);
|
2021-08-31 13:22:56 +00:00
|
|
|
if (entry->sfxId & 0xC00) {
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->state = SFX_STATE_PLAYING_1;
|
2020-10-26 11:58:56 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->state = SFX_STATE_PLAYING_2;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
sCurSfxPlayerChannelIdx++;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_StopSfxByBank(u8 bankId) {
|
2020-10-26 11:58:56 +00:00
|
|
|
SoundBankEntry* entry;
|
|
|
|
s32 pad;
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundBankEntry cmp;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 entryIndex = gSoundBanks[bankId][0].next;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
while (entryIndex != 0xFF) {
|
|
|
|
entry = &gSoundBanks[bankId][entryIndex];
|
|
|
|
if (entry->state >= SFX_STATE_PLAYING_REFRESH) {
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((entry->channelIdx & 0xFF) << 8), 0);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state != SFX_STATE_EMPTY) {
|
|
|
|
Audio_RemoveSoundBankEntry(bankId, entryIndex);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
entryIndex = gSoundBanks[bankId][0].next;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
cmp.sfxId = bankId << 12;
|
2021-08-30 00:08:41 +00:00
|
|
|
Audio_RemoveMatchingSoundRequests(0, &cmp);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void func_800F8884(u8 bankId, Vec3f* pos) {
|
|
|
|
SoundBankEntry* entry;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 entryIndex = gSoundBanks[bankId][0].next;
|
|
|
|
u8 prevEntryIndex = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
while (entryIndex != 0xFF) {
|
|
|
|
entry = &gSoundBanks[bankId][entryIndex];
|
2020-10-26 11:58:56 +00:00
|
|
|
if (entry->posX == &pos->x) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state >= SFX_STATE_PLAYING_REFRESH) {
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((entry->channelIdx & 0xFF) << 8), 0);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state != SFX_STATE_EMPTY) {
|
|
|
|
Audio_RemoveSoundBankEntry(bankId, entryIndex);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
prevEntryIndex = entryIndex;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
entryIndex = gSoundBanks[bankId][prevEntryIndex].next;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_StopSfxByPosAndBank(u8 bankId, Vec3f* pos) {
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundBankEntry cmp;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
|
|
|
func_800F8884(bankId, pos);
|
2021-08-31 13:22:56 +00:00
|
|
|
cmp.sfxId = bankId << 12;
|
2021-08-30 00:08:41 +00:00
|
|
|
cmp.posX = &pos->x;
|
|
|
|
Audio_RemoveMatchingSoundRequests(1, &cmp);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_StopSfxByPos(Vec3f* pos) {
|
2020-10-26 11:58:56 +00:00
|
|
|
u8 i;
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundBankEntry cmp;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_COUNT(gSoundBanks); i++) {
|
|
|
|
func_800F8884(i, pos);
|
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
cmp.posX = &pos->x;
|
|
|
|
Audio_RemoveMatchingSoundRequests(2, &cmp);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_StopSfxByPosAndId(Vec3f* pos, u16 sfxId) {
|
2020-10-26 11:58:56 +00:00
|
|
|
SoundBankEntry* entry;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 entryIndex = gSoundBanks[SFX_BANK(sfxId)][0].next;
|
|
|
|
u8 prevEntryIndex = 0;
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundBankEntry cmp;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
while (entryIndex != 0xFF) {
|
|
|
|
entry = &gSoundBanks[SFX_BANK(sfxId)][entryIndex];
|
2021-08-31 13:22:56 +00:00
|
|
|
if (entry->posX == &pos->x && entry->sfxId == sfxId) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state >= SFX_STATE_PLAYING_REFRESH) {
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((entry->channelIdx & 0xFF) << 8), 0);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state != SFX_STATE_EMPTY) {
|
|
|
|
Audio_RemoveSoundBankEntry(SFX_BANK(sfxId), entryIndex);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
entryIndex = 0xFF;
|
2020-10-26 11:58:56 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
prevEntryIndex = entryIndex;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entryIndex != 0xFF) {
|
|
|
|
entryIndex = gSoundBanks[SFX_BANK(sfxId)][prevEntryIndex].next;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
cmp.posX = &pos->x;
|
2021-08-31 13:22:56 +00:00
|
|
|
cmp.sfxId = sfxId;
|
2021-08-30 00:08:41 +00:00
|
|
|
Audio_RemoveMatchingSoundRequests(3, &cmp);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_StopSfxByTokenAndId(u8 token, u16 sfxId) {
|
2020-10-26 11:58:56 +00:00
|
|
|
SoundBankEntry* entry;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 entryIndex = gSoundBanks[SFX_BANK(sfxId)][0].next;
|
|
|
|
u8 prevEntryIndex = 0;
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundBankEntry cmp;
|
2020-10-26 11:58:56 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
while (entryIndex != 0xFF) {
|
|
|
|
entry = &gSoundBanks[SFX_BANK(sfxId)][entryIndex];
|
|
|
|
if (entry->token == token && entry->sfxId == sfxId) {
|
|
|
|
if (entry->state >= SFX_STATE_PLAYING_REFRESH) {
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((entry->channelIdx & 0xFF) << 8), 0);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state != SFX_STATE_EMPTY) {
|
|
|
|
Audio_RemoveSoundBankEntry(SFX_BANK(sfxId), entryIndex);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
prevEntryIndex = entryIndex;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entryIndex != 0xFF) {
|
|
|
|
entryIndex = gSoundBanks[SFX_BANK(sfxId)][prevEntryIndex].next;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
cmp.token = token;
|
2021-08-31 13:22:56 +00:00
|
|
|
cmp.sfxId = sfxId;
|
2021-08-30 00:08:41 +00:00
|
|
|
Audio_RemoveMatchingSoundRequests(4, &cmp);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_StopSfxById(u32 sfxId) {
|
2020-10-26 11:58:56 +00:00
|
|
|
SoundBankEntry* entry;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 entryIndex = gSoundBanks[SFX_BANK(sfxId)][0].next;
|
|
|
|
u8 prevEntryIndex = 0;
|
2021-08-30 00:08:41 +00:00
|
|
|
SoundBankEntry cmp;
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
while (entryIndex != 0xFF) {
|
|
|
|
entry = &gSoundBanks[SFX_BANK(sfxId)][entryIndex];
|
2021-08-31 13:22:56 +00:00
|
|
|
if (entry->sfxId == sfxId) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state >= SFX_STATE_PLAYING_REFRESH) {
|
2021-11-30 18:47:09 +00:00
|
|
|
Audio_QueueCmdS8(0x6 << 24 | SEQ_PLAYER_SFX << 16 | ((entry->channelIdx & 0xFF) << 8), 0);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (entry->state != SFX_STATE_EMPTY) {
|
|
|
|
Audio_RemoveSoundBankEntry(SFX_BANK(sfxId), entryIndex);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
prevEntryIndex = entryIndex;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
entryIndex = gSoundBanks[SFX_BANK(sfxId)][prevEntryIndex].next;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-08-31 13:22:56 +00:00
|
|
|
cmp.sfxId = sfxId;
|
2021-08-30 00:08:41 +00:00
|
|
|
Audio_RemoveMatchingSoundRequests(5, &cmp);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
void Audio_ProcessSoundRequests(void) {
|
|
|
|
while (sSoundRequestWriteIndex != sSoundRequestReadIndex) {
|
|
|
|
Audio_ProcessSoundRequest();
|
|
|
|
sSoundRequestReadIndex++;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_SetUnusedBankLerp(u8 bankId, u8 target, u16 delay) {
|
|
|
|
if (delay == 0) {
|
|
|
|
delay++;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
sUnusedBankLerp[bankId].target = target / 127.0f;
|
|
|
|
sUnusedBankLerp[bankId].remainingFrames = delay;
|
|
|
|
sUnusedBankLerp[bankId].step = ((sUnusedBankLerp[bankId].value - sUnusedBankLerp[bankId].target) / delay);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_StepUnusedBankLerp(u8 bankId) {
|
|
|
|
if (sUnusedBankLerp[bankId].remainingFrames != 0) {
|
|
|
|
sUnusedBankLerp[bankId].remainingFrames--;
|
|
|
|
if (sUnusedBankLerp[bankId].remainingFrames != 0) {
|
|
|
|
sUnusedBankLerp[bankId].value -= sUnusedBankLerp[bankId].step;
|
2020-10-26 11:58:56 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
sUnusedBankLerp[bankId].value = sUnusedBankLerp[bankId].target;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2020-10-26 11:58:56 +00:00
|
|
|
void func_800F8F88(void) {
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 bankId;
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-30 18:47:09 +00:00
|
|
|
if (IS_SEQUENCE_CHANNEL_VALID(gAudioContext.seqPlayers[SEQ_PLAYER_SFX].channels[0])) {
|
2021-11-07 16:58:50 +00:00
|
|
|
sCurSfxPlayerChannelIdx = 0;
|
|
|
|
for (bankId = 0; bankId < ARRAY_COUNT(gSoundBanks); bankId++) {
|
|
|
|
Audio_ChooseActiveSounds(bankId);
|
|
|
|
Audio_PlayActiveSounds(bankId);
|
|
|
|
Audio_StepUnusedBankLerp(bankId);
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
u8 Audio_IsSfxPlaying(u32 sfxId) {
|
2020-10-26 11:58:56 +00:00
|
|
|
SoundBankEntry* entry;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 entryIndex = gSoundBanks[SFX_BANK(sfxId)][0].next;
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
while (entryIndex != 0xFF) {
|
|
|
|
entry = &gSoundBanks[SFX_BANK(sfxId)][entryIndex];
|
2021-08-31 13:22:56 +00:00
|
|
|
if (entry->sfxId == sfxId) {
|
2020-10-26 11:58:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
entryIndex = entry->next;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_ResetSounds(void) {
|
2020-10-26 11:58:56 +00:00
|
|
|
u8 bankId;
|
|
|
|
u8 i;
|
2021-11-07 16:58:50 +00:00
|
|
|
u8 entryIndex;
|
2020-07-20 01:08:50 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
sSoundRequestWriteIndex = 0;
|
|
|
|
sSoundRequestReadIndex = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
D_801333D0 = 0;
|
|
|
|
for (bankId = 0; bankId < ARRAY_COUNT(gSoundBanks); bankId++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
sSoundBankListEnd[bankId] = 0;
|
|
|
|
sSoundBankFreeListStart[bankId] = 1;
|
|
|
|
sSoundBankUnused[bankId] = 0;
|
2021-08-30 00:08:41 +00:00
|
|
|
gSoundBankMuted[bankId] = false;
|
2021-11-07 16:58:50 +00:00
|
|
|
sUnusedBankLerp[bankId].value = 1.0f;
|
|
|
|
sUnusedBankLerp[bankId].remainingFrames = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
for (bankId = 0; bankId < ARRAY_COUNT(gSoundBanks); bankId++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < MAX_CHANNELS_PER_BANK; i++) {
|
|
|
|
gActiveSounds[bankId][i].entryIndex = 0xFF;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (bankId = 0; bankId < ARRAY_COUNT(gSoundBanks); bankId++) {
|
|
|
|
gSoundBanks[bankId][0].prev = 0xFF;
|
|
|
|
gSoundBanks[bankId][0].next = 0xFF;
|
|
|
|
for (i = 1; i < sBankSizes[bankId] - 1; i++) {
|
|
|
|
gSoundBanks[bankId][i].prev = i - 1;
|
|
|
|
gSoundBanks[bankId][i].next = i + 1;
|
|
|
|
}
|
|
|
|
gSoundBanks[bankId][i].prev = i - 1;
|
|
|
|
gSoundBanks[bankId][i].next = 0xFF;
|
|
|
|
}
|
|
|
|
if (D_801333F8 == 0) {
|
|
|
|
for (bankId = 0; bankId < 10; bankId++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioSfxSwapSource[bankId] = 0;
|
|
|
|
gAudioSfxSwapTarget[bankId] = 0;
|
|
|
|
gAudioSfxSwapMode[bankId] = 0;
|
2020-10-26 11:58:56 +00:00
|
|
|
}
|
|
|
|
D_801333F8++;
|
|
|
|
}
|
|
|
|
}
|