2020-10-03 15:22:44 +00:00
|
|
|
#include "ultra64.h"
|
|
|
|
#include "global.h"
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_InitSampleCaches(u32 persistentSize, u32 temporarySize);
|
|
|
|
SampleCacheEntry* AudioHeap_AllocTemporarySampleCacheEntry(u32 size);
|
|
|
|
SampleCacheEntry* AudioHeap_AllocPersistentSampleCacheEntry(u32 size);
|
|
|
|
void AudioHeap_DiscardSampleCacheEntry(SampleCacheEntry* entry);
|
|
|
|
void AudioHeap_UnapplySampleCache(SampleCacheEntry* entry, SoundFontSample* sample);
|
|
|
|
void AudioHeap_DiscardSampleCaches(void);
|
|
|
|
void AudioHeap_DiscardSampleBank(s32 sampleBankId);
|
|
|
|
void AudioHeap_DiscardSampleBanks(void);
|
|
|
|
|
2022-05-29 18:31:43 +00:00
|
|
|
/**
|
|
|
|
* Effectively scales `updatesPerFrameInv` by the reciprocal of `scaleInv`
|
|
|
|
* `updatesPerFrameInvScaled` is just `updatesPerFrameInv` scaled down by a factor of 256.0f
|
|
|
|
* i.e. (256.0f * `updatesPerFrameInvScaled`) is just `updatesPerFrameInv`
|
|
|
|
*/
|
|
|
|
f32 AudioHeap_CalculateAdsrDecay(f32 scaleInv) {
|
|
|
|
return (256.0f * gAudioContext.audioBufferParameters.updatesPerFrameInvScaled) / scaleInv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the decay rate table used for decaying notes as part of adsr
|
|
|
|
*/
|
|
|
|
void AudioHeap_InitAdsrDecayTable(void) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-05-29 18:31:43 +00:00
|
|
|
gAudioContext.adsrDecayTable[255] = AudioHeap_CalculateAdsrDecay(0.25f);
|
|
|
|
gAudioContext.adsrDecayTable[254] = AudioHeap_CalculateAdsrDecay(0.33f);
|
|
|
|
gAudioContext.adsrDecayTable[253] = AudioHeap_CalculateAdsrDecay(0.5f);
|
|
|
|
gAudioContext.adsrDecayTable[252] = AudioHeap_CalculateAdsrDecay(0.66f);
|
|
|
|
gAudioContext.adsrDecayTable[251] = AudioHeap_CalculateAdsrDecay(0.75f);
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 128; i < 251; i++) {
|
2022-05-29 18:31:43 +00:00
|
|
|
gAudioContext.adsrDecayTable[i] = AudioHeap_CalculateAdsrDecay(251 - i);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 16; i < 128; i++) {
|
2022-05-29 18:31:43 +00:00
|
|
|
gAudioContext.adsrDecayTable[i] = AudioHeap_CalculateAdsrDecay(4 * (143 - i));
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 1; i < 16; i++) {
|
2022-05-29 18:31:43 +00:00
|
|
|
gAudioContext.adsrDecayTable[i] = AudioHeap_CalculateAdsrDecay(60 * (23 - i));
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-05-29 18:31:43 +00:00
|
|
|
gAudioContext.adsrDecayTable[0] = 0.0f;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ResetLoadStatus(void) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < 0x30; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.fontLoadStatus[i] != LOAD_STATUS_PERMANENTLY_LOADED) {
|
|
|
|
gAudioContext.fontLoadStatus[i] = LOAD_STATUS_NOT_LOADED;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < 0x30; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.sampleFontLoadStatus[i] != LOAD_STATUS_PERMANENTLY_LOADED) {
|
|
|
|
gAudioContext.sampleFontLoadStatus[i] = LOAD_STATUS_NOT_LOADED;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < 0x80; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.seqLoadStatus[i] != LOAD_STATUS_PERMANENTLY_LOADED) {
|
|
|
|
gAudioContext.seqLoadStatus[i] = LOAD_STATUS_NOT_LOADED;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_DiscardFont(s32 fontId) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
Note* note = &gAudioContext.notes[i];
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (note->playbackState.fontId == fontId) {
|
2020-09-20 17:22:09 +00:00
|
|
|
if (note->playbackState.unk_04 == 0 && note->playbackState.priority != 0) {
|
|
|
|
note->playbackState.parentLayer->enabled = false;
|
|
|
|
note->playbackState.parentLayer->finished = true;
|
|
|
|
}
|
|
|
|
Audio_NoteDisable(note);
|
|
|
|
Audio_AudioListRemove(¬e->listItem);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(&gAudioContext.noteFreeLists.disabled, ¬e->listItem);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ReleaseNotesForFont(s32 fontId) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
Note* note = &gAudioContext.notes[i];
|
2020-09-20 17:22:09 +00:00
|
|
|
NotePlaybackState* state = ¬e->playbackState;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (state->fontId == fontId) {
|
2020-09-20 17:22:09 +00:00
|
|
|
if (state->priority != 0 && state->adsr.action.s.state == ADSR_STATE_DECAY) {
|
|
|
|
state->priority = 1;
|
2021-07-27 23:44:58 +00:00
|
|
|
state->adsr.fadeOutVel = gAudioContext.audioBufferParameters.updatesPerFrameInv;
|
2020-09-20 17:22:09 +00:00
|
|
|
state->adsr.action.s.release = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_DiscardSequence(s32 seqId) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
for (i = 0; i < gAudioContext.audioBufferParameters.numSequencePlayers; i++) {
|
|
|
|
if (gAudioContext.seqPlayers[i].enabled && gAudioContext.seqPlayers[i].seqId == seqId) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_SequencePlayerDisable(&gAudioContext.seqPlayers[i]);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
/**
|
|
|
|
* Perform a writeback from the data cache to the ram.
|
|
|
|
*/
|
|
|
|
void AudioHeap_WritebackDCache(void* ramAddr, u32 size) {
|
|
|
|
Audio_WritebackDCache(ramAddr, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
/**
|
|
|
|
* Attempt to allocate space externally to the audio heap. If no external pool is available,
|
|
|
|
* then allocate space on the pool provided in the argument.
|
|
|
|
* The newly allocated space is zero'ed
|
|
|
|
*/
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocZeroedAttemptExternal(AudioAllocPool* pool, u32 size) {
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr = NULL;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.externalPool.startRamAddr != NULL) {
|
|
|
|
ramAddr = AudioHeap_AllocZeroed(&gAudioContext.externalPool, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
if (ramAddr == NULL) {
|
|
|
|
ramAddr = AudioHeap_AllocZeroed(pool, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
return ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocAttemptExternal(AudioAllocPool* pool, u32 size) {
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr = NULL;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.externalPool.startRamAddr != NULL) {
|
|
|
|
ramAddr = AudioHeap_Alloc(&gAudioContext.externalPool, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
if (ramAddr == NULL) {
|
|
|
|
ramAddr = AudioHeap_Alloc(pool, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
return ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocDmaMemory(AudioAllocPool* pool, u32 size) {
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr = AudioHeap_Alloc(pool, size);
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (ramAddr != NULL) {
|
|
|
|
AudioHeap_WritebackDCache(ramAddr, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
return ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocDmaMemoryZeroed(AudioAllocPool* pool, u32 size) {
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
ramAddr = AudioHeap_AllocZeroed(pool, size);
|
|
|
|
if (ramAddr != NULL) {
|
|
|
|
AudioHeap_WritebackDCache(ramAddr, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
return ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
/**
|
|
|
|
* Allocates space on a pool contained within the heap and sets all the allocated space to 0
|
|
|
|
*/
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocZeroed(AudioAllocPool* pool, u32 size) {
|
2022-06-03 19:59:02 +00:00
|
|
|
u8* ramAddr = AudioHeap_Alloc(pool, size);
|
2020-09-20 17:22:09 +00:00
|
|
|
u8* ptr;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (ramAddr != NULL) {
|
|
|
|
for (ptr = ramAddr; ptr < pool->curRamAddr; ptr++) {
|
2020-09-20 17:22:09 +00:00
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
return ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_Alloc(AudioAllocPool* pool, u32 size) {
|
2020-09-20 17:22:09 +00:00
|
|
|
u32 aligned = ALIGN16(size);
|
2022-06-03 19:59:02 +00:00
|
|
|
u8* ramAddr = pool->curRamAddr;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (pool->startRamAddr + pool->size >= pool->curRamAddr + aligned) {
|
|
|
|
pool->curRamAddr += aligned;
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
pool->numEntries++;
|
|
|
|
return ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
/**
|
|
|
|
* Initialize a pool to allocate memory from the specified address, up to the specified size.
|
|
|
|
* Store the metadata of this pool in AudioAllocPool* pool
|
|
|
|
*/
|
|
|
|
void AudioHeap_AllocPoolInit(AudioAllocPool* pool, void* ramAddr, u32 size) {
|
|
|
|
pool->curRamAddr = pool->startRamAddr = (u8*)ALIGN16((u32)ramAddr);
|
|
|
|
pool->size = size - ((u32)ramAddr & 0xF);
|
|
|
|
pool->numEntries = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_PersistentCacheClear(AudioPersistentCache* persistent) {
|
2022-06-03 19:59:02 +00:00
|
|
|
persistent->pool.numEntries = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
persistent->numEntries = 0;
|
2022-06-03 19:59:02 +00:00
|
|
|
persistent->pool.curRamAddr = persistent->pool.startRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_TemporaryCacheClear(AudioTemporaryCache* temporary) {
|
2022-06-03 19:59:02 +00:00
|
|
|
temporary->pool.numEntries = 0;
|
|
|
|
temporary->pool.curRamAddr = temporary->pool.startRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
temporary->nextSide = 0;
|
2022-06-03 19:59:02 +00:00
|
|
|
temporary->entries[0].ramAddr = temporary->pool.startRamAddr;
|
|
|
|
temporary->entries[1].ramAddr = temporary->pool.startRamAddr + temporary->pool.size;
|
2020-09-20 17:22:09 +00:00
|
|
|
temporary->entries[0].id = -1;
|
|
|
|
temporary->entries[1].id = -1;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ResetPool(AudioAllocPool* pool) {
|
2022-06-03 19:59:02 +00:00
|
|
|
pool->numEntries = 0;
|
|
|
|
pool->curRamAddr = pool->startRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_PopCache(s32 tableType) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioCache* loadedCache;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioAllocPool* persistentPool;
|
|
|
|
AudioPersistentCache* persistent;
|
2022-06-03 19:59:02 +00:00
|
|
|
void* entryRamAddr;
|
|
|
|
u8* loadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
switch (tableType) {
|
|
|
|
case SEQUENCE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.seqCache;
|
|
|
|
loadStatus = gAudioContext.seqLoadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case FONT_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.fontCache;
|
|
|
|
loadStatus = gAudioContext.fontLoadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case SAMPLE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.sampleBankCache;
|
|
|
|
loadStatus = gAudioContext.sampleFontLoadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
persistent = &loadedCache->persistent;
|
2020-09-20 17:22:09 +00:00
|
|
|
persistentPool = &persistent->pool;
|
|
|
|
|
|
|
|
if (persistent->numEntries == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
entryRamAddr = persistent->entries[persistent->numEntries - 1].ramAddr;
|
|
|
|
persistentPool->curRamAddr = entryRamAddr;
|
|
|
|
persistentPool->numEntries--;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == SAMPLE_TABLE) {
|
|
|
|
AudioHeap_DiscardSampleBank(persistent->entries[persistent->numEntries - 1].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == FONT_TABLE) {
|
|
|
|
AudioHeap_DiscardFont(persistent->entries[persistent->numEntries - 1].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
loadStatus[persistent->entries[persistent->numEntries - 1].id] = LOAD_STATUS_NOT_LOADED;
|
2020-09-20 17:22:09 +00:00
|
|
|
persistent->numEntries--;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_InitMainPools(s32 initPoolSize) {
|
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.audioInitPool, gAudioContext.audioHeap, initPoolSize);
|
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.audioSessionPool, gAudioContext.audioHeap + initPoolSize,
|
|
|
|
gAudioContext.audioHeapSize - initPoolSize);
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.externalPool.startRamAddr = NULL;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
void AudioHeap_SessionPoolsInit(AudioSessionPoolSplit* split) {
|
|
|
|
gAudioContext.audioSessionPool.curRamAddr = gAudioContext.audioSessionPool.startRamAddr;
|
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.miscPool,
|
|
|
|
AudioHeap_Alloc(&gAudioContext.audioSessionPool, split->miscPoolSize), split->miscPoolSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.cachePool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.audioSessionPool, split->cachePoolSize),
|
|
|
|
split->cachePoolSize);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
void AudioHeap_CachePoolInit(AudioCachePoolSplit* split) {
|
|
|
|
gAudioContext.cachePool.curRamAddr = gAudioContext.cachePool.startRamAddr;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.persistentCommonPool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.cachePool, split->persistentCommonPoolSize),
|
|
|
|
split->persistentCommonPoolSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.temporaryCommonPool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.cachePool, split->temporaryCommonPoolSize),
|
|
|
|
split->temporaryCommonPoolSize);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
void AudioHeap_PersistentCachesInit(AudioCommonPoolSplit* split) {
|
|
|
|
gAudioContext.persistentCommonPool.curRamAddr = gAudioContext.persistentCommonPool.startRamAddr;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.seqCache.persistent.pool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.persistentCommonPool, split->seqCacheSize),
|
|
|
|
split->seqCacheSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.fontCache.persistent.pool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.persistentCommonPool, split->fontCacheSize),
|
|
|
|
split->fontCacheSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.sampleBankCache.persistent.pool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.persistentCommonPool, split->sampleBankCacheSize),
|
|
|
|
split->sampleBankCacheSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_PersistentCacheClear(&gAudioContext.seqCache.persistent);
|
|
|
|
AudioHeap_PersistentCacheClear(&gAudioContext.fontCache.persistent);
|
|
|
|
AudioHeap_PersistentCacheClear(&gAudioContext.sampleBankCache.persistent);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
void AudioHeap_TemporaryCachesInit(AudioCommonPoolSplit* split) {
|
|
|
|
gAudioContext.temporaryCommonPool.curRamAddr = gAudioContext.temporaryCommonPool.startRamAddr;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.seqCache.temporary.pool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.temporaryCommonPool, split->seqCacheSize),
|
|
|
|
split->seqCacheSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.fontCache.temporary.pool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.temporaryCommonPool, split->fontCacheSize),
|
|
|
|
split->fontCacheSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.sampleBankCache.temporary.pool,
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_Alloc(&gAudioContext.temporaryCommonPool, split->sampleBankCacheSize),
|
|
|
|
split->sampleBankCacheSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_TemporaryCacheClear(&gAudioContext.seqCache.temporary);
|
|
|
|
AudioHeap_TemporaryCacheClear(&gAudioContext.fontCache.temporary);
|
|
|
|
AudioHeap_TemporaryCacheClear(&gAudioContext.sampleBankCache.temporary);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocCached(s32 tableType, s32 size, s32 cache, s32 id) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioCache* loadedCache;
|
|
|
|
AudioTemporaryCache* temporaryCache;
|
|
|
|
AudioAllocPool* temporaryPool;
|
|
|
|
void* persistentRamAddr;
|
|
|
|
void* temporaryRamAddr;
|
|
|
|
u8 loadStatusEntry0;
|
|
|
|
u8 loadStatusEntry1;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2022-06-03 19:59:02 +00:00
|
|
|
u8* loadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 side;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
switch (tableType) {
|
|
|
|
case SEQUENCE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.seqCache;
|
|
|
|
loadStatus = gAudioContext.seqLoadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case FONT_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.fontCache;
|
|
|
|
loadStatus = gAudioContext.fontLoadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case SAMPLE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.sampleBankCache;
|
|
|
|
loadStatus = gAudioContext.sampleFontLoadStatus;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (cache == CACHE_TEMPORARY) {
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache = &loadedCache->temporary;
|
|
|
|
temporaryPool = &temporaryCache->pool;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (temporaryPool->size < size) {
|
2020-09-20 17:22:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
loadStatusEntry0 =
|
|
|
|
(temporaryCache->entries[0].id == -1) ? LOAD_STATUS_NOT_LOADED : loadStatus[temporaryCache->entries[0].id];
|
|
|
|
loadStatusEntry1 =
|
|
|
|
(temporaryCache->entries[1].id == -1) ? LOAD_STATUS_NOT_LOADED : loadStatus[temporaryCache->entries[1].id];
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == FONT_TABLE) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry0 == LOAD_STATUS_MAYBE_DISCARDABLE) {
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.notes[i].playbackState.fontId == temporaryCache->entries[0].id &&
|
|
|
|
gAudioContext.notes[i].noteSubEu.bitField0.enabled) {
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (i == gAudioContext.numNotes) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioLoad_SetFontLoadStatus(temporaryCache->entries[0].id, LOAD_STATUS_DISCARDABLE);
|
|
|
|
loadStatusEntry0 = LOAD_STATUS_DISCARDABLE;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry1 == LOAD_STATUS_MAYBE_DISCARDABLE) {
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.notes[i].playbackState.fontId == temporaryCache->entries[1].id &&
|
|
|
|
gAudioContext.notes[i].noteSubEu.bitField0.enabled) {
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (i == gAudioContext.numNotes) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioLoad_SetFontLoadStatus(temporaryCache->entries[1].id, LOAD_STATUS_DISCARDABLE);
|
|
|
|
loadStatusEntry1 = LOAD_STATUS_DISCARDABLE;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry0 == LOAD_STATUS_NOT_LOADED) {
|
|
|
|
temporaryCache->nextSide = 0;
|
|
|
|
} else if (loadStatusEntry1 == LOAD_STATUS_NOT_LOADED) {
|
|
|
|
temporaryCache->nextSide = 1;
|
|
|
|
} else if (loadStatusEntry0 == LOAD_STATUS_DISCARDABLE && loadStatusEntry1 == LOAD_STATUS_DISCARDABLE) {
|
2020-09-20 17:22:09 +00:00
|
|
|
// Use the opposite side from last time.
|
2022-06-03 19:59:02 +00:00
|
|
|
} else if (loadStatusEntry0 == LOAD_STATUS_DISCARDABLE) {
|
|
|
|
temporaryCache->nextSide = 0;
|
|
|
|
} else if (loadStatusEntry1 == LOAD_STATUS_DISCARDABLE) {
|
|
|
|
temporaryCache->nextSide = 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
|
|
|
// Check if there is a side which isn't in active use, if so, evict that one.
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == SEQUENCE_TABLE) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry0 == LOAD_STATUS_COMPLETE) {
|
2021-07-27 23:44:58 +00:00
|
|
|
for (i = 0; i < gAudioContext.audioBufferParameters.numSequencePlayers; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.seqPlayers[i].enabled &&
|
|
|
|
gAudioContext.seqPlayers[i].seqId == temporaryCache->entries[0].id) {
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
if (i == gAudioContext.audioBufferParameters.numSequencePlayers) {
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->nextSide = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry1 == LOAD_STATUS_COMPLETE) {
|
2021-07-27 23:44:58 +00:00
|
|
|
for (i = 0; i < gAudioContext.audioBufferParameters.numSequencePlayers; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.seqPlayers[i].enabled &&
|
|
|
|
gAudioContext.seqPlayers[i].seqId == temporaryCache->entries[1].id) {
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
if (i == gAudioContext.audioBufferParameters.numSequencePlayers) {
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->nextSide = 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
} else if (tableType == FONT_TABLE) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry0 == LOAD_STATUS_COMPLETE) {
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.notes[i].playbackState.fontId == temporaryCache->entries[0].id &&
|
|
|
|
gAudioContext.notes[i].noteSubEu.bitField0.enabled) {
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (i == gAudioContext.numNotes) {
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->nextSide = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry1 == LOAD_STATUS_COMPLETE) {
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.notes[i].playbackState.fontId == temporaryCache->entries[1].id &&
|
|
|
|
gAudioContext.notes[i].noteSubEu.bitField0.enabled) {
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (i == gAudioContext.numNotes) {
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->nextSide = 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No such luck. Evict the side that wasn't chosen last time, except
|
|
|
|
// if it is being loaded into.
|
2022-06-03 19:59:02 +00:00
|
|
|
if (temporaryCache->nextSide == 0) {
|
|
|
|
if (loadStatusEntry0 == LOAD_STATUS_IN_PROGRESS) {
|
|
|
|
if (loadStatusEntry1 == LOAD_STATUS_IN_PROGRESS) {
|
2020-09-20 17:22:09 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->nextSide = 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-03 19:59:02 +00:00
|
|
|
if (loadStatusEntry1 == LOAD_STATUS_IN_PROGRESS) {
|
|
|
|
if (loadStatusEntry0 == LOAD_STATUS_IN_PROGRESS) {
|
2020-09-20 17:22:09 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->nextSide = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0) {
|
|
|
|
fail:
|
|
|
|
// Both sides are being loaded into.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
side = temporaryCache->nextSide;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (temporaryCache->entries[side].id != -1) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == SAMPLE_TABLE) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardSampleBank(temporaryCache->entries[side].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
loadStatus[temporaryCache->entries[side].id] = LOAD_STATUS_NOT_LOADED;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == FONT_TABLE) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardFont(temporaryCache->entries[side].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (side) {
|
|
|
|
case 0:
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->entries[0].ramAddr = temporaryPool->startRamAddr;
|
|
|
|
temporaryCache->entries[0].id = id;
|
|
|
|
temporaryCache->entries[0].size = size;
|
|
|
|
temporaryPool->curRamAddr = temporaryPool->startRamAddr + size;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (temporaryCache->entries[1].id != -1 &&
|
|
|
|
temporaryCache->entries[1].ramAddr < temporaryPool->curRamAddr) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == SAMPLE_TABLE) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardSampleBank(temporaryCache->entries[1].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
loadStatus[temporaryCache->entries[1].id] = LOAD_STATUS_NOT_LOADED;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
switch (tableType) {
|
|
|
|
case SEQUENCE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardSequence((s32)temporaryCache->entries[1].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case FONT_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardFont((s32)temporaryCache->entries[1].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->entries[1].id = -1;
|
|
|
|
temporaryCache->entries[1].ramAddr = temporaryPool->startRamAddr + temporaryPool->size;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryRamAddr = temporaryCache->entries[0].ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->entries[1].ramAddr =
|
|
|
|
(u8*)((u32)(temporaryPool->startRamAddr + temporaryPool->size - size) & ~0xF);
|
|
|
|
temporaryCache->entries[1].id = id;
|
|
|
|
temporaryCache->entries[1].size = size;
|
|
|
|
if (temporaryCache->entries[0].id != -1 &&
|
|
|
|
temporaryCache->entries[1].ramAddr < temporaryPool->curRamAddr) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (tableType == SAMPLE_TABLE) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardSampleBank(temporaryCache->entries[0].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
loadStatus[temporaryCache->entries[0].id] = LOAD_STATUS_NOT_LOADED;
|
2021-11-07 16:58:50 +00:00
|
|
|
switch (tableType) {
|
|
|
|
case SEQUENCE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardSequence(temporaryCache->entries[0].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case FONT_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardFont(temporaryCache->entries[0].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->entries[0].id = -1;
|
|
|
|
temporaryPool->curRamAddr = temporaryPool->startRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryRamAddr = temporaryCache->entries[1].ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
temporaryCache->nextSide ^= 1;
|
|
|
|
return temporaryRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
persistentRamAddr = AudioHeap_Alloc(&loadedCache->persistent.pool, size);
|
|
|
|
loadedCache->persistent.entries[loadedCache->persistent.numEntries].ramAddr = persistentRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (persistentRamAddr == NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
switch (cache) {
|
|
|
|
case CACHE_EITHER:
|
|
|
|
return AudioHeap_AllocCached(tableType, size, CACHE_TEMPORARY, id);
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case CACHE_TEMPORARY:
|
|
|
|
case CACHE_PERSISTENT:
|
2020-09-20 17:22:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache->persistent.entries[loadedCache->persistent.numEntries].id = id;
|
|
|
|
loadedCache->persistent.entries[loadedCache->persistent.numEntries].size = size;
|
|
|
|
|
|
|
|
return loadedCache->persistent.entries[loadedCache->persistent.numEntries++].ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_SearchCaches(s32 tableType, s32 cache, s32 id) {
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
// Always search the permanent cache in addition to the regular ones.
|
2022-06-03 19:59:02 +00:00
|
|
|
ramAddr = AudioHeap_SearchPermanentCache(tableType, id);
|
|
|
|
if (ramAddr != NULL) {
|
|
|
|
return ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (cache == CACHE_PERMANENT) {
|
2020-09-20 17:22:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
return AudioHeap_SearchRegularCaches(tableType, cache, id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_SearchRegularCaches(s32 tableType, s32 cache, s32 id) {
|
2020-09-20 17:22:09 +00:00
|
|
|
u32 i;
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioCache* loadedCache;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioTemporaryCache* temporary;
|
|
|
|
AudioPersistentCache* persistent;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
switch (tableType) {
|
|
|
|
case SEQUENCE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.seqCache;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case FONT_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.fontCache;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
case SAMPLE_TABLE:
|
2022-06-03 19:59:02 +00:00
|
|
|
loadedCache = &gAudioContext.sampleBankCache;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
temporary = &loadedCache->temporary;
|
2021-11-07 16:58:50 +00:00
|
|
|
if (cache == CACHE_TEMPORARY) {
|
|
|
|
if (temporary->entries[0].id == id) {
|
2020-09-20 17:22:09 +00:00
|
|
|
temporary->nextSide = 1;
|
2022-06-03 19:59:02 +00:00
|
|
|
return temporary->entries[0].ramAddr;
|
2021-11-07 16:58:50 +00:00
|
|
|
} else if (temporary->entries[1].id == id) {
|
2020-09-20 17:22:09 +00:00
|
|
|
temporary->nextSide = 0;
|
2022-06-03 19:59:02 +00:00
|
|
|
return temporary->entries[1].ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
persistent = &loadedCache->persistent;
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < persistent->numEntries; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (persistent->entries[i].id == id) {
|
2022-06-03 19:59:02 +00:00
|
|
|
return persistent->entries[i].ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (cache == CACHE_EITHER) {
|
|
|
|
return AudioHeap_SearchCaches(tableType, CACHE_TEMPORARY, id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void func_800DF1D8(f32 p, f32 q, u16* out) {
|
|
|
|
// With the bug below fixed, this mysterious unused function computes two recurrences
|
|
|
|
// out[0..7] = a_i, out[8..15] = b_i, where
|
|
|
|
// a_{-2} = b_{-1} = 262159 = 2^18 + 15
|
|
|
|
// a_{-1} = b_{-2} = 0
|
|
|
|
// a_i = q * a_{i-1} + p * a_{i-2}
|
|
|
|
// b_i = q * b_{i-1} + p * b_{i-2}
|
|
|
|
// These grow exponentially if p < -1 or p + |q| > 1.
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
|
|
|
f32 tmp[16];
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
tmp[0] = (f32)(q * 262159.0f);
|
|
|
|
tmp[8] = (f32)(p * 262159.0f);
|
|
|
|
tmp[1] = (f32)((q * p) * 262159.0f);
|
|
|
|
tmp[9] = (f32)(((p * p) + q) * 262159.0f);
|
2020-09-20 17:22:09 +00:00
|
|
|
|
|
|
|
for (i = 2; i < 8; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
//! @bug value should be stored to tmp[i] and tmp[8 + i], otherwise we read
|
|
|
|
//! garbage in later loop iterations.
|
|
|
|
out[i] = q * tmp[i - 2] + p * tmp[i - 1];
|
|
|
|
out[8 + i] = q * tmp[6 + i] + p * tmp[7 + i];
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
out[i] = tmp[i];
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ClearFilter(s16* filter) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
2021-08-31 22:53:35 +00:00
|
|
|
filter[i] = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_LoadLowPassFilter(s16* filter, s32 cutoff) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2022-06-03 19:59:02 +00:00
|
|
|
s16* ptr = &gLowPassFilterData[8 * cutoff];
|
2020-09-20 17:22:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
2021-08-31 22:53:35 +00:00
|
|
|
filter[i] = ptr[i];
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_LoadHighPassFilter(s16* filter, s32 cutoff) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
2022-06-03 19:59:02 +00:00
|
|
|
s16* ptr = &gHighPassFilterData[8 * (cutoff - 1)];
|
2020-09-20 17:22:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
2021-08-31 22:53:35 +00:00
|
|
|
filter[i] = ptr[i];
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_LoadFilter(s16* filter, s32 lowPassCutoff, s32 highPassCutoff) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (lowPassCutoff == 0 && highPassCutoff == 0) {
|
|
|
|
// Identity filter
|
|
|
|
AudioHeap_LoadLowPassFilter(filter, 0);
|
|
|
|
} else if (highPassCutoff == 0) {
|
|
|
|
AudioHeap_LoadLowPassFilter(filter, lowPassCutoff);
|
|
|
|
} else if (lowPassCutoff == 0) {
|
|
|
|
AudioHeap_LoadHighPassFilter(filter, highPassCutoff);
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2022-06-03 19:59:02 +00:00
|
|
|
s16* ptr1 = &gLowPassFilterData[8 * lowPassCutoff];
|
|
|
|
s16* ptr2 = &gHighPassFilterData[8 * (highPassCutoff - 1)];
|
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
2021-08-31 22:53:35 +00:00
|
|
|
filter[i] = (ptr1[i] + ptr2[i]) / 2;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_UpdateReverb(SynthesisReverb* reverb) {
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_UpdateReverbs(void) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 count;
|
|
|
|
s32 i;
|
|
|
|
s32 j;
|
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
if (gAudioContext.audioBufferParameters.specUnk4 == 2) {
|
2020-09-20 17:22:09 +00:00
|
|
|
count = 2;
|
|
|
|
} else {
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
for (i = 0; i < gAudioContext.numSynthesisReverbs; i++) {
|
2020-09-20 17:22:09 +00:00
|
|
|
for (j = 0; j < count; j++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UpdateReverb(&gAudioContext.synthesisReverbs[i]);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
/**
|
|
|
|
* Clear the current Audio Interface Buffer
|
|
|
|
*/
|
|
|
|
void AudioHeap_ClearCurrentAiBuffer(void) {
|
|
|
|
s32 curAiBufferIndex = gAudioContext.curAiBufIndex;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.aiBufLengths[curAiBufferIndex] = gAudioContext.audioBufferParameters.minAiBufferLength;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < AIBUF_LEN; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.aiBuffers[curAiBufferIndex][i] = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
s32 AudioHeap_ResetStep(void) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
|
|
|
s32 j;
|
|
|
|
s32 sp24;
|
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
if (gAudioContext.audioBufferParameters.specUnk4 == 2) {
|
2020-09-20 17:22:09 +00:00
|
|
|
sp24 = 2;
|
|
|
|
} else {
|
|
|
|
sp24 = 1;
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
switch (gAudioContext.resetStatus) {
|
2020-09-20 17:22:09 +00:00
|
|
|
case 5:
|
2021-07-27 23:44:58 +00:00
|
|
|
for (i = 0; i < gAudioContext.audioBufferParameters.numSequencePlayers; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_SequencePlayerDisableAsFinished(&gAudioContext.seqPlayers[i]);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.audioResetFadeOutFramesLeft = 2 / sp24;
|
|
|
|
gAudioContext.resetStatus--;
|
2020-09-20 17:22:09 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2021-07-27 23:44:58 +00:00
|
|
|
if (gAudioContext.audioResetFadeOutFramesLeft != 0) {
|
|
|
|
gAudioContext.audioResetFadeOutFramesLeft--;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UpdateReverbs();
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
|
|
|
if (gAudioContext.notes[i].noteSubEu.bitField0.enabled &&
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.notes[i].playbackState.adsr.action.s.state != ADSR_STATE_DISABLED) {
|
|
|
|
gAudioContext.notes[i].playbackState.adsr.fadeOutVel =
|
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrameInv;
|
|
|
|
gAudioContext.notes[i].playbackState.adsr.action.s.release = true;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.audioResetFadeOutFramesLeft = 8 / sp24;
|
|
|
|
gAudioContext.resetStatus--;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2021-07-27 23:44:58 +00:00
|
|
|
if (gAudioContext.audioResetFadeOutFramesLeft != 0) {
|
|
|
|
gAudioContext.audioResetFadeOutFramesLeft--;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UpdateReverbs();
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.audioResetFadeOutFramesLeft = 2 / sp24;
|
|
|
|
gAudioContext.resetStatus--;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_ClearCurrentAiBuffer();
|
2021-07-27 23:44:58 +00:00
|
|
|
if (gAudioContext.audioResetFadeOutFramesLeft != 0) {
|
|
|
|
gAudioContext.audioResetFadeOutFramesLeft--;
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.resetStatus--;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_DiscardSampleCaches();
|
|
|
|
AudioHeap_DiscardSampleBanks();
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_Init();
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.resetStatus = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.aiBufLengths[i] = gAudioContext.audioBufferParameters.maxAiBufferLength;
|
2021-11-07 16:58:50 +00:00
|
|
|
for (j = 0; j < AIBUF_LEN; j++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.aiBuffers[i][j] = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
if (gAudioContext.resetStatus < 3) {
|
2022-06-03 19:59:02 +00:00
|
|
|
return false;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
return true;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_Init(void) {
|
2021-01-18 02:31:47 +00:00
|
|
|
s32 pad1[4];
|
2022-06-03 19:59:02 +00:00
|
|
|
s16* ramAddr;
|
|
|
|
s32 persistentSize;
|
|
|
|
s32 temporarySize;
|
|
|
|
s32 cachePoolSize;
|
|
|
|
s32 miscPoolSize;
|
2021-12-01 00:08:57 +00:00
|
|
|
OSIntMask intMask;
|
2021-01-18 02:31:47 +00:00
|
|
|
s32 i;
|
|
|
|
s32 j;
|
|
|
|
s32 pad2;
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioSpec* spec = &gAudioSpecs[gAudioContext.audioResetSpecIdToLoad]; // Audio Specifications
|
2021-07-27 23:44:58 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.sampleDmaCount = 0;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
// audio buffer parameters
|
|
|
|
gAudioContext.audioBufferParameters.samplingFrequency = spec->samplingFrequency;
|
|
|
|
gAudioContext.audioBufferParameters.aiSamplingFrequency =
|
|
|
|
osAiSetFrequency(gAudioContext.audioBufferParameters.samplingFrequency);
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.audioBufferParameters.samplesPerFrameTarget =
|
2022-06-03 19:59:02 +00:00
|
|
|
ALIGN16(gAudioContext.audioBufferParameters.samplingFrequency / gAudioContext.refreshRate);
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.audioBufferParameters.minAiBufferLength =
|
|
|
|
gAudioContext.audioBufferParameters.samplesPerFrameTarget - 0x10;
|
|
|
|
gAudioContext.audioBufferParameters.maxAiBufferLength =
|
|
|
|
gAudioContext.audioBufferParameters.samplesPerFrameTarget + 0x10;
|
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrame =
|
|
|
|
((gAudioContext.audioBufferParameters.samplesPerFrameTarget + 0x10) / 0xD0) + 1;
|
2021-07-28 21:59:52 +00:00
|
|
|
gAudioContext.audioBufferParameters.samplesPerUpdate = (gAudioContext.audioBufferParameters.samplesPerFrameTarget /
|
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrame) &
|
|
|
|
~7;
|
|
|
|
gAudioContext.audioBufferParameters.samplesPerUpdateMax = gAudioContext.audioBufferParameters.samplesPerUpdate + 8;
|
|
|
|
gAudioContext.audioBufferParameters.samplesPerUpdateMin = gAudioContext.audioBufferParameters.samplesPerUpdate - 8;
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.audioBufferParameters.resampleRate =
|
|
|
|
32000.0f / (s32)gAudioContext.audioBufferParameters.samplingFrequency;
|
2022-05-29 18:31:43 +00:00
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrameInvScaled =
|
2021-07-27 23:44:58 +00:00
|
|
|
(1.0f / 256.0f) / gAudioContext.audioBufferParameters.updatesPerFrame;
|
2022-05-29 18:31:43 +00:00
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrameScaled =
|
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrame / 4.0f;
|
2021-07-28 21:59:52 +00:00
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrameInv = 1.0f / gAudioContext.audioBufferParameters.updatesPerFrame;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
// SampleDma buffer size
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.sampleDmaBufSize1 = spec->sampleDmaBufSize1;
|
|
|
|
gAudioContext.sampleDmaBufSize2 = spec->sampleDmaBufSize2;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.numNotes = spec->numNotes;
|
2021-08-30 00:08:41 +00:00
|
|
|
gAudioContext.audioBufferParameters.numSequencePlayers = spec->numSequencePlayers;
|
2021-07-27 23:44:58 +00:00
|
|
|
if (gAudioContext.audioBufferParameters.numSequencePlayers > 4) {
|
|
|
|
gAudioContext.audioBufferParameters.numSequencePlayers = 4;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2021-08-30 00:08:41 +00:00
|
|
|
gAudioContext.unk_2 = spec->unk_14;
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.tempoInternalToExternal = (u32)(gAudioContext.audioBufferParameters.updatesPerFrame * 2880000.0f /
|
2021-07-28 21:59:52 +00:00
|
|
|
gTatumsPerBeat / gAudioContext.unk_2960);
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.unk_2870 = gAudioContext.refreshRate;
|
|
|
|
gAudioContext.unk_2870 *= gAudioContext.audioBufferParameters.updatesPerFrame;
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.unk_2870 /= gAudioContext.audioBufferParameters.aiSamplingFrequency;
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.unk_2870 /= gAudioContext.tempoInternalToExternal;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
gAudioContext.audioBufferParameters.specUnk4 = spec->unk_04;
|
|
|
|
gAudioContext.audioBufferParameters.samplesPerFrameTarget *= gAudioContext.audioBufferParameters.specUnk4;
|
|
|
|
gAudioContext.audioBufferParameters.maxAiBufferLength *= gAudioContext.audioBufferParameters.specUnk4;
|
|
|
|
gAudioContext.audioBufferParameters.minAiBufferLength *= gAudioContext.audioBufferParameters.specUnk4;
|
|
|
|
gAudioContext.audioBufferParameters.updatesPerFrame *= gAudioContext.audioBufferParameters.specUnk4;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
if (gAudioContext.audioBufferParameters.specUnk4 >= 2) {
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.audioBufferParameters.maxAiBufferLength -= 0x10;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
// Determine the length of the buffer for storing the audio command list passed to the rsp audio microcode
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.maxAudioCmds = gAudioContext.numNotes * 0x10 * gAudioContext.audioBufferParameters.updatesPerFrame +
|
|
|
|
spec->numReverbs * 0x18 + 0x140;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
// Calculate sizes for various caches on the audio heap
|
|
|
|
persistentSize =
|
|
|
|
spec->persistentSeqCacheSize + spec->persistentFontCacheSize + spec->persistentSampleBankCacheSize + 0x10;
|
|
|
|
temporarySize =
|
|
|
|
spec->temporarySeqCacheSize + spec->temporaryFontCacheSize + spec->temporarySampleBankCacheSize + 0x10;
|
|
|
|
cachePoolSize = persistentSize + temporarySize;
|
|
|
|
miscPoolSize = gAudioContext.audioSessionPool.size - cachePoolSize - 0x100;
|
2021-07-27 23:44:58 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (gAudioContext.externalPool.startRamAddr != NULL) {
|
|
|
|
gAudioContext.externalPool.curRamAddr = gAudioContext.externalPool.startRamAddr;
|
2021-07-27 23:44:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
// Session Pool Split (split into Cache and Misc pools)
|
|
|
|
gAudioContext.sessionPoolSplit.miscPoolSize = miscPoolSize;
|
|
|
|
gAudioContext.sessionPoolSplit.cachePoolSize = cachePoolSize;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_SessionPoolsInit(&gAudioContext.sessionPoolSplit);
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
// Cache Pool Split (split into Persistent and Temporary pools)
|
|
|
|
gAudioContext.cachePoolSplit.persistentCommonPoolSize = persistentSize;
|
|
|
|
gAudioContext.cachePoolSplit.temporaryCommonPoolSize = temporarySize;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_CachePoolInit(&gAudioContext.cachePoolSplit);
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
// Persistent Pool Split (split into Sequences, SoundFonts, Samples pools)
|
|
|
|
gAudioContext.persistentCommonPoolSplit.seqCacheSize = spec->persistentSeqCacheSize;
|
|
|
|
gAudioContext.persistentCommonPoolSplit.fontCacheSize = spec->persistentFontCacheSize;
|
|
|
|
gAudioContext.persistentCommonPoolSplit.sampleBankCacheSize = spec->persistentSampleBankCacheSize;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_PersistentCachesInit(&gAudioContext.persistentCommonPoolSplit);
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
// Temporary Pool Split (split into Sequences, SoundFonts, Samples pools)
|
|
|
|
gAudioContext.temporaryCommonPoolSplit.seqCacheSize = spec->temporarySeqCacheSize;
|
|
|
|
gAudioContext.temporaryCommonPoolSplit.fontCacheSize = spec->temporaryFontCacheSize;
|
|
|
|
gAudioContext.temporaryCommonPoolSplit.sampleBankCacheSize = spec->temporarySampleBankCacheSize;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_TemporaryCachesInit(&gAudioContext.temporaryCommonPoolSplit);
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_ResetLoadStatus();
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
// Initialize notes
|
|
|
|
gAudioContext.notes = AudioHeap_AllocZeroed(&gAudioContext.miscPool, gAudioContext.numNotes * sizeof(Note));
|
2020-09-20 17:22:09 +00:00
|
|
|
Audio_NoteInitAll();
|
|
|
|
Audio_InitNoteFreeList();
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.noteSubsEu =
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_AllocZeroed(&gAudioContext.miscPool, gAudioContext.audioBufferParameters.updatesPerFrame *
|
|
|
|
gAudioContext.numNotes * sizeof(NoteSubEu));
|
|
|
|
// Initialize audio binary interface command list buffers
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i != 2; i++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.abiCmdBufs[i] =
|
|
|
|
AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, gAudioContext.maxAudioCmds * sizeof(u64));
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 18:31:43 +00:00
|
|
|
// Initialize the decay rate table for adsr
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.adsrDecayTable = AudioHeap_Alloc(&gAudioContext.miscPool, 256 * sizeof(f32));
|
2022-05-29 18:31:43 +00:00
|
|
|
AudioHeap_InitAdsrDecayTable();
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
// Initialize reverbs
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.synthesisReverbs[i].useReverb = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 00:08:41 +00:00
|
|
|
gAudioContext.numSynthesisReverbs = spec->numReverbs;
|
2021-07-27 23:44:58 +00:00
|
|
|
for (i = 0; i < gAudioContext.numSynthesisReverbs; i++) {
|
2021-08-30 00:08:41 +00:00
|
|
|
ReverbSettings* settings = &spec->reverbSettings[i];
|
2021-07-27 23:44:58 +00:00
|
|
|
SynthesisReverb* reverb = &gAudioContext.synthesisReverbs[i];
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
reverb->downsampleRate = settings->downsampleRate;
|
|
|
|
reverb->windowSize = settings->windowSize * 64;
|
|
|
|
reverb->windowSize /= reverb->downsampleRate;
|
2022-06-03 19:59:02 +00:00
|
|
|
reverb->decayRatio = settings->decayRatio;
|
|
|
|
reverb->volume = settings->volume;
|
2020-09-20 17:22:09 +00:00
|
|
|
reverb->unk_14 = settings->unk_6 * 64;
|
|
|
|
reverb->unk_16 = settings->unk_8;
|
|
|
|
reverb->unk_18 = 0;
|
2021-08-31 22:53:35 +00:00
|
|
|
reverb->leakRtl = settings->leakRtl;
|
|
|
|
reverb->leakLtr = settings->leakLtr;
|
2020-09-20 17:22:09 +00:00
|
|
|
reverb->unk_05 = settings->unk_10;
|
|
|
|
reverb->unk_08 = settings->unk_12;
|
|
|
|
reverb->useReverb = 8;
|
2021-11-07 16:58:50 +00:00
|
|
|
reverb->leftRingBuf =
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, reverb->windowSize * sizeof(s16));
|
2021-11-07 16:58:50 +00:00
|
|
|
reverb->rightRingBuf =
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, reverb->windowSize * sizeof(s16));
|
2021-07-27 23:44:58 +00:00
|
|
|
reverb->nextRingBufPos = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
reverb->unk_20 = 0;
|
2021-07-27 23:44:58 +00:00
|
|
|
reverb->curFrame = 0;
|
|
|
|
reverb->bufSizePerChan = reverb->windowSize;
|
|
|
|
reverb->framesToIgnore = 2;
|
|
|
|
reverb->resampleFlags = 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
reverb->sound.sample = &reverb->sample;
|
|
|
|
reverb->sample.loop = &reverb->loop;
|
2021-01-18 02:31:47 +00:00
|
|
|
reverb->sound.tuning = 1.0f;
|
2021-11-07 16:58:50 +00:00
|
|
|
reverb->sample.codec = CODEC_REVERB;
|
|
|
|
reverb->sample.medium = MEDIUM_RAM;
|
2021-07-27 23:44:58 +00:00
|
|
|
reverb->sample.size = reverb->windowSize * 2;
|
|
|
|
reverb->sample.sampleAddr = (u8*)reverb->leftRingBuf;
|
2020-09-20 17:22:09 +00:00
|
|
|
reverb->loop.start = 0;
|
|
|
|
reverb->loop.count = 1;
|
|
|
|
reverb->loop.end = reverb->windowSize;
|
|
|
|
|
|
|
|
if (reverb->downsampleRate != 1) {
|
|
|
|
reverb->unk_0E = 0x8000 / reverb->downsampleRate;
|
2022-06-03 19:59:02 +00:00
|
|
|
reverb->unk_30 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20);
|
|
|
|
reverb->unk_34 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20);
|
|
|
|
reverb->unk_38 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20);
|
|
|
|
reverb->unk_3C = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20);
|
2021-07-27 23:44:58 +00:00
|
|
|
for (j = 0; j < gAudioContext.audioBufferParameters.updatesPerFrame; j++) {
|
2022-06-03 19:59:02 +00:00
|
|
|
ramAddr = AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, 0x340);
|
|
|
|
reverb->items[0][j].toDownsampleLeft = ramAddr;
|
|
|
|
reverb->items[0][j].toDownsampleRight = ramAddr + 0x1A0 / sizeof(s16);
|
|
|
|
|
|
|
|
ramAddr = AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, 0x340);
|
|
|
|
reverb->items[1][j].toDownsampleLeft = ramAddr;
|
|
|
|
reverb->items[1][j].toDownsampleRight = ramAddr + 0x1A0 / sizeof(s16);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (settings->lowPassFilterCutoffLeft != 0) {
|
2022-06-03 19:59:02 +00:00
|
|
|
reverb->filterLeftState = AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, 0x40);
|
|
|
|
reverb->filterLeft = AudioHeap_AllocDmaMemory(&gAudioContext.miscPool, 8 * sizeof(s16));
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_LoadLowPassFilter(reverb->filterLeft, settings->lowPassFilterCutoffLeft);
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2021-08-31 22:53:35 +00:00
|
|
|
reverb->filterLeft = NULL;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (settings->lowPassFilterCutoffRight != 0) {
|
2022-06-03 19:59:02 +00:00
|
|
|
reverb->filterRightState = AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, 0x40);
|
|
|
|
reverb->filterRight = AudioHeap_AllocDmaMemory(&gAudioContext.miscPool, 8 * sizeof(s16));
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_LoadLowPassFilter(reverb->filterRight, settings->lowPassFilterCutoffRight);
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2021-08-31 22:53:35 +00:00
|
|
|
reverb->filterRight = NULL;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
// Initialize sequence players
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_InitSequencePlayers();
|
2021-07-27 23:44:58 +00:00
|
|
|
for (j = 0; j < gAudioContext.audioBufferParameters.numSequencePlayers; j++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_InitSequencePlayerChannels(j);
|
|
|
|
AudioSeq_ResetSequencePlayer(&gAudioContext.seqPlayers[j]);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
// Initialize two additional sample caches for individual samples
|
|
|
|
AudioHeap_InitSampleCaches(spec->persistentSampleCacheSize, spec->temporarySampleCacheSize);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioLoad_InitSampleDmaBuffers(gAudioContext.numNotes);
|
2022-06-03 19:59:02 +00:00
|
|
|
|
|
|
|
// Initalize Loads
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.preloadSampleStackTop = 0;
|
|
|
|
AudioLoad_InitSlowLoads();
|
|
|
|
AudioLoad_InitScriptLoads();
|
|
|
|
AudioLoad_InitAsyncLoads();
|
2020-09-20 17:22:09 +00:00
|
|
|
gAudioContext.unk_4 = 0x1000;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioLoad_LoadPermanentSamples();
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2022-02-02 21:43:34 +00:00
|
|
|
intMask = osSetIntMask(OS_IM_NONE);
|
2020-09-20 17:22:09 +00:00
|
|
|
osWritebackDCacheAll();
|
|
|
|
osSetIntMask(intMask);
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_SearchPermanentCache(s32 tableType, s32 id) {
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 i;
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
for (i = 0; i < gAudioContext.permanentPool.numEntries; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (gAudioContext.permanentCache[i].tableType == tableType && gAudioContext.permanentCache[i].id == id) {
|
2022-06-03 19:59:02 +00:00
|
|
|
return gAudioContext.permanentCache[i].ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocPermanent(s32 tableType, s32 id, u32 size) {
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr;
|
|
|
|
s32 index = gAudioContext.permanentPool.numEntries;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
ramAddr = AudioHeap_Alloc(&gAudioContext.permanentPool, size);
|
|
|
|
gAudioContext.permanentCache[index].ramAddr = ramAddr;
|
|
|
|
if (ramAddr == NULL) {
|
2020-09-20 17:22:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.permanentCache[index].tableType = tableType;
|
|
|
|
gAudioContext.permanentCache[index].id = id;
|
|
|
|
gAudioContext.permanentCache[index].size = size;
|
2022-02-19 21:50:56 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
//! @bug UB: missing return. "ramAddr" is in v0 at this point, but doing an
|
2022-05-01 22:06:35 +00:00
|
|
|
//! explicit return uses an additional register.
|
2022-02-19 21:50:56 +00:00
|
|
|
#ifdef AVOID_UB
|
2022-06-03 19:59:02 +00:00
|
|
|
return ramAddr;
|
2022-02-19 21:50:56 +00:00
|
|
|
#endif
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void* AudioHeap_AllocSampleCache(u32 size, s32 fontId, void* sampleAddr, s8 medium, s32 cache) {
|
|
|
|
SampleCacheEntry* entry;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (cache == CACHE_TEMPORARY) {
|
|
|
|
entry = AudioHeap_AllocTemporarySampleCacheEntry(size);
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
entry = AudioHeap_AllocPersistentSampleCacheEntry(size);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
if (entry != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
//! @bug Should use sampleBankId, not fontId
|
|
|
|
entry->sampleBankId = fontId;
|
|
|
|
entry->sampleAddr = sampleAddr;
|
|
|
|
entry->origMedium = medium;
|
|
|
|
return entry->allocatedAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
/**
|
|
|
|
* Initializes the persistent and temporary caches used for individual samples. Will attempt to use heap space available
|
|
|
|
* on the external pool. If no external pool is provided, then default to using space on the misc pool.
|
|
|
|
*/
|
|
|
|
void AudioHeap_InitSampleCaches(u32 persistentSampleCacheSize, u32 temporarySampleCacheSize) {
|
|
|
|
void* ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
ramAddr = AudioHeap_AllocAttemptExternal(&gAudioContext.miscPool, persistentSampleCacheSize);
|
|
|
|
if (ramAddr == NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.persistentSampleCache.pool.size = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.persistentSampleCache.pool, ramAddr, persistentSampleCacheSize);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
ramAddr = AudioHeap_AllocAttemptExternal(&gAudioContext.miscPool, temporarySampleCacheSize);
|
|
|
|
if (ramAddr == NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.temporarySampleCache.pool.size = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_AllocPoolInit(&gAudioContext.temporarySampleCache.pool, ramAddr, temporarySampleCacheSize);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
gAudioContext.persistentSampleCache.numEntries = 0;
|
|
|
|
gAudioContext.temporarySampleCache.numEntries = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
SampleCacheEntry* AudioHeap_AllocTemporarySampleCacheEntry(u32 size) {
|
2020-09-20 17:22:09 +00:00
|
|
|
u8* allocAfter;
|
|
|
|
u8* allocBefore;
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 index;
|
|
|
|
s32 i;
|
2022-06-03 19:59:02 +00:00
|
|
|
SampleCacheEntry* entry;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioPreloadReq* preload;
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioSampleCache* cache;
|
|
|
|
u8* startRamAddr;
|
|
|
|
u8* endRamAddr;
|
|
|
|
|
|
|
|
cache = &gAudioContext.temporarySampleCache;
|
|
|
|
allocBefore = cache->pool.curRamAddr;
|
|
|
|
ramAddr = AudioHeap_Alloc(&cache->pool, size);
|
|
|
|
if (ramAddr == NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
// Reset the pool and try again. We still keep pointers to within the
|
|
|
|
// pool, so we have to be careful to discard existing overlapping
|
|
|
|
// allocations further down.
|
2022-06-03 19:59:02 +00:00
|
|
|
u8* old = cache->pool.curRamAddr;
|
|
|
|
|
|
|
|
cache->pool.curRamAddr = cache->pool.startRamAddr;
|
|
|
|
ramAddr = AudioHeap_Alloc(&cache->pool, size);
|
|
|
|
if (ramAddr == NULL) {
|
|
|
|
cache->pool.curRamAddr = old;
|
2020-09-20 17:22:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
allocBefore = cache->pool.startRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
allocAfter = cache->pool.curRamAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
|
|
|
index = -1;
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.preloadSampleStackTop; i++) {
|
|
|
|
preload = &gAudioContext.preloadSampleStack[i];
|
|
|
|
if (preload->isFree == false) {
|
2022-06-03 19:59:02 +00:00
|
|
|
startRamAddr = preload->ramAddr;
|
|
|
|
endRamAddr = preload->ramAddr + preload->sample->size - 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (endRamAddr < allocBefore && startRamAddr < allocBefore) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
if (endRamAddr >= allocAfter && startRamAddr >= allocAfter) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
// Overlap, skip this preload.
|
|
|
|
preload->isFree = true;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
for (i = 0; i < cache->numEntries; i++) {
|
|
|
|
if (!cache->entries[i].inUse) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
startRamAddr = cache->entries[i].allocatedAddr;
|
|
|
|
endRamAddr = startRamAddr + cache->entries[i].size - 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (endRamAddr < allocBefore && startRamAddr < allocBefore) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
if (endRamAddr >= allocAfter && startRamAddr >= allocAfter) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
// Overlap, discard existing entry.
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioHeap_DiscardSampleCacheEntry(&cache->entries[i]);
|
2020-09-20 17:22:09 +00:00
|
|
|
if (index == -1) {
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == -1) {
|
2022-06-03 19:59:02 +00:00
|
|
|
index = cache->numEntries++;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
entry = &cache->entries[index];
|
|
|
|
entry->inUse = true;
|
|
|
|
entry->allocatedAddr = ramAddr;
|
|
|
|
entry->size = size;
|
|
|
|
|
|
|
|
return entry;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_UnapplySampleCacheForFont(SampleCacheEntry* entry, s32 fontId) {
|
2020-09-20 17:22:09 +00:00
|
|
|
Drum* drum;
|
|
|
|
Instrument* inst;
|
2021-11-07 16:58:50 +00:00
|
|
|
SoundFontSound* sfx;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 instId;
|
|
|
|
s32 drumId;
|
|
|
|
s32 sfxId;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (instId = 0; instId < gAudioContext.soundFonts[fontId].numInstruments; instId++) {
|
|
|
|
inst = Audio_GetInstrumentInner(fontId, instId);
|
2020-09-20 17:22:09 +00:00
|
|
|
if (inst != NULL) {
|
|
|
|
if (inst->normalRangeLo != 0) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UnapplySampleCache(entry, inst->lowNotesSound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
if (inst->normalRangeHi != 0x7F) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UnapplySampleCache(entry, inst->highNotesSound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UnapplySampleCache(entry, inst->normalNotesSound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (drumId = 0; drumId < gAudioContext.soundFonts[fontId].numDrums; drumId++) {
|
|
|
|
drum = Audio_GetDrum(fontId, drumId);
|
2020-09-20 17:22:09 +00:00
|
|
|
if (drum != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UnapplySampleCache(entry, drum->sound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (sfxId = 0; sfxId < gAudioContext.soundFonts[fontId].numSfx; sfxId++) {
|
|
|
|
sfx = Audio_GetSfx(fontId, sfxId);
|
2020-09-20 17:22:09 +00:00
|
|
|
if (sfx != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_UnapplySampleCache(entry, sfx->sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_DiscardSampleCacheEntry(SampleCacheEntry* entry) {
|
|
|
|
s32 numFonts;
|
|
|
|
s32 sampleBankId1;
|
|
|
|
s32 sampleBankId2;
|
|
|
|
s32 fontId;
|
|
|
|
|
|
|
|
numFonts = gAudioContext.soundFontTable->numEntries;
|
|
|
|
for (fontId = 0; fontId < numFonts; fontId++) {
|
|
|
|
sampleBankId1 = gAudioContext.soundFonts[fontId].sampleBankId1;
|
|
|
|
sampleBankId2 = gAudioContext.soundFonts[fontId].sampleBankId2;
|
|
|
|
if (((sampleBankId1 != 0xFF) && (entry->sampleBankId == sampleBankId1)) ||
|
|
|
|
((sampleBankId2 != 0xFF) && (entry->sampleBankId == sampleBankId2)) || entry->sampleBankId == 0) {
|
|
|
|
if (AudioHeap_SearchCaches(FONT_TABLE, CACHE_EITHER, fontId) != NULL) {
|
|
|
|
if (AudioLoad_IsFontLoadComplete(fontId) != 0) {
|
|
|
|
AudioHeap_UnapplySampleCacheForFont(entry, fontId);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_UnapplySampleCache(SampleCacheEntry* entry, SoundFontSample* sample) {
|
2020-09-20 17:22:09 +00:00
|
|
|
if (sample != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (sample->sampleAddr == entry->allocatedAddr) {
|
|
|
|
sample->sampleAddr = entry->sampleAddr;
|
|
|
|
sample->medium = entry->origMedium;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
SampleCacheEntry* AudioHeap_AllocPersistentSampleCacheEntry(u32 size) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioSampleCache* cache;
|
2021-11-07 16:58:50 +00:00
|
|
|
SampleCacheEntry* entry;
|
2022-06-03 19:59:02 +00:00
|
|
|
void* ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
cache = &gAudioContext.persistentSampleCache;
|
|
|
|
ramAddr = AudioHeap_Alloc(&cache->pool, size);
|
|
|
|
if (ramAddr == NULL) {
|
2020-09-20 17:22:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
entry = &cache->entries[cache->numEntries];
|
2021-11-07 16:58:50 +00:00
|
|
|
entry->inUse = true;
|
2022-06-03 19:59:02 +00:00
|
|
|
entry->allocatedAddr = ramAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
entry->size = size;
|
2022-06-03 19:59:02 +00:00
|
|
|
cache->numEntries++;
|
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_DiscardSampleCacheForFont(SampleCacheEntry* entry, s32 sampleBankId1, s32 sampleBankId2, s32 fontId) {
|
|
|
|
if ((entry->sampleBankId == sampleBankId1) || (entry->sampleBankId == sampleBankId2) ||
|
|
|
|
(entry->sampleBankId == 0)) {
|
|
|
|
AudioHeap_UnapplySampleCacheForFont(entry, fontId);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_DiscardSampleCaches(void) {
|
|
|
|
s32 numFonts;
|
|
|
|
s32 sampleBankId1;
|
|
|
|
s32 sampleBankId2;
|
|
|
|
s32 fontId;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 j;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
numFonts = gAudioContext.soundFontTable->numEntries;
|
|
|
|
for (fontId = 0; fontId < numFonts; fontId++) {
|
|
|
|
sampleBankId1 = gAudioContext.soundFonts[fontId].sampleBankId1;
|
|
|
|
sampleBankId2 = gAudioContext.soundFonts[fontId].sampleBankId2;
|
|
|
|
if ((sampleBankId1 == 0xFF) && (sampleBankId2 == 0xFF)) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (AudioHeap_SearchCaches(FONT_TABLE, CACHE_PERMANENT, fontId) == NULL ||
|
|
|
|
!AudioLoad_IsFontLoadComplete(fontId)) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
for (j = 0; j < gAudioContext.persistentSampleCache.numEntries; j++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_DiscardSampleCacheForFont(&gAudioContext.persistentSampleCache.entries[j], sampleBankId1,
|
|
|
|
sampleBankId2, fontId);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
for (j = 0; j < gAudioContext.temporarySampleCache.numEntries; j++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_DiscardSampleCacheForFont(&gAudioContext.temporarySampleCache.entries[j], sampleBankId1,
|
|
|
|
sampleBankId2, fontId);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
2021-11-07 16:58:50 +00:00
|
|
|
u32 oldAddr;
|
|
|
|
u32 newAddr;
|
|
|
|
u32 size;
|
|
|
|
u8 newMedium;
|
|
|
|
} StorageChange;
|
2020-09-20 17:22:09 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ChangeStorage(StorageChange* change, SoundFontSample* sample) {
|
2020-09-20 17:22:09 +00:00
|
|
|
if (sample != NULL) {
|
2022-06-03 19:59:02 +00:00
|
|
|
u32 startAddr = change->oldAddr;
|
|
|
|
u32 endAddr = change->oldAddr + change->size;
|
2021-11-30 23:40:42 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (startAddr <= (u32)sample->sampleAddr && (u32)sample->sampleAddr < endAddr) {
|
|
|
|
sample->sampleAddr = sample->sampleAddr - startAddr + change->newAddr;
|
2021-11-07 16:58:50 +00:00
|
|
|
sample->medium = change->newMedium;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ApplySampleBankCacheInternal(s32 apply, s32 id);
|
|
|
|
|
|
|
|
void AudioHeap_DiscardSampleBank(s32 sampleBankId) {
|
|
|
|
AudioHeap_ApplySampleBankCacheInternal(false, sampleBankId);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ApplySampleBankCache(s32 sampleBankId) {
|
|
|
|
AudioHeap_ApplySampleBankCacheInternal(true, sampleBankId);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_ApplySampleBankCacheInternal(s32 apply, s32 sampleBankId) {
|
|
|
|
AudioTable* sampleBankTable;
|
|
|
|
AudioTableEntry* entry;
|
|
|
|
s32 numFonts;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 instId;
|
|
|
|
s32 drumId;
|
|
|
|
s32 sfxId;
|
2021-11-07 16:58:50 +00:00
|
|
|
StorageChange change;
|
|
|
|
s32 sampleBankId1;
|
|
|
|
s32 sampleBankId2;
|
|
|
|
s32 fontId;
|
2020-09-20 17:22:09 +00:00
|
|
|
Drum* drum;
|
|
|
|
Instrument* inst;
|
2021-11-07 16:58:50 +00:00
|
|
|
SoundFontSound* sfx;
|
|
|
|
u32* fakematch;
|
2020-09-20 17:22:09 +00:00
|
|
|
s32 pad[4];
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
sampleBankTable = gAudioContext.sampleBankTable;
|
|
|
|
numFonts = gAudioContext.soundFontTable->numEntries;
|
|
|
|
change.oldAddr = AudioHeap_SearchCaches(SAMPLE_TABLE, CACHE_EITHER, sampleBankId);
|
|
|
|
if (change.oldAddr == 0) {
|
2020-09-20 17:22:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
entry = &sampleBankTable->entries[sampleBankId];
|
|
|
|
change.size = entry->size;
|
|
|
|
change.newMedium = entry->medium;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if ((change.newMedium == MEDIUM_CART) || (change.newMedium == MEDIUM_DISK_DRIVE)) {
|
|
|
|
change.newAddr = entry->romAddr;
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
change.newAddr = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
fakematch = &change.oldAddr;
|
|
|
|
if ((apply != false) && (apply == true)) {
|
|
|
|
u32 temp = change.newAddr;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
change.newAddr = *fakematch; // = change.oldAddr
|
|
|
|
change.oldAddr = temp;
|
|
|
|
change.newMedium = MEDIUM_RAM;
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (fontId = 0; fontId < numFonts; fontId++) {
|
|
|
|
sampleBankId1 = gAudioContext.soundFonts[fontId].sampleBankId1;
|
|
|
|
sampleBankId2 = gAudioContext.soundFonts[fontId].sampleBankId2;
|
|
|
|
if ((sampleBankId1 != 0xFF) || (sampleBankId2 != 0xFF)) {
|
|
|
|
if (!AudioLoad_IsFontLoadComplete(fontId) ||
|
|
|
|
AudioHeap_SearchCaches(FONT_TABLE, CACHE_EITHER, fontId) == NULL) {
|
2020-09-20 17:22:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (sampleBankId1 == sampleBankId) {
|
|
|
|
} else if (sampleBankId2 == sampleBankId) {
|
2020-09-20 17:22:09 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (instId = 0; instId < gAudioContext.soundFonts[fontId].numInstruments; instId++) {
|
|
|
|
inst = Audio_GetInstrumentInner(fontId, instId);
|
2020-09-20 17:22:09 +00:00
|
|
|
if (inst != NULL) {
|
|
|
|
if (inst->normalRangeLo != 0) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_ChangeStorage(&change, inst->lowNotesSound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
if (inst->normalRangeHi != 0x7F) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_ChangeStorage(&change, inst->highNotesSound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_ChangeStorage(&change, inst->normalNotesSound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (drumId = 0; drumId < gAudioContext.soundFonts[fontId].numDrums; drumId++) {
|
|
|
|
drum = Audio_GetDrum(fontId, drumId);
|
2020-09-20 17:22:09 +00:00
|
|
|
if (drum != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_ChangeStorage(&change, drum->sound.sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (sfxId = 0; sfxId < gAudioContext.soundFonts[fontId].numSfx; sfxId++) {
|
|
|
|
sfx = Audio_GetSfx(fontId, sfxId);
|
2020-09-20 17:22:09 +00:00
|
|
|
if (sfx != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_ChangeStorage(&change, sfx->sample);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void AudioHeap_DiscardSampleBanks(void) {
|
2022-06-03 19:59:02 +00:00
|
|
|
AudioCache* cache;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioPersistentCache* persistent;
|
|
|
|
AudioTemporaryCache* temporary;
|
2020-09-20 17:22:09 +00:00
|
|
|
u32 i;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
cache = &gAudioContext.sampleBankCache;
|
|
|
|
temporary = &cache->temporary;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
if (temporary->entries[0].id != -1) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_DiscardSampleBank(temporary->entries[0].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
if (temporary->entries[1].id != -1) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_DiscardSampleBank(temporary->entries[1].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
persistent = &cache->persistent;
|
2020-09-20 17:22:09 +00:00
|
|
|
for (i = 0; i < persistent->numEntries; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioHeap_DiscardSampleBank(persistent->entries[i].id);
|
2020-09-20 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|