From 5f1a34d9c9b26b36e03836ae2d843954be78c391 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Sat, 2 Nov 2024 08:49:29 -0600 Subject: [PATCH] Trying to reimplement the original functions --- src/audio/audio_load.c | 232 +++++++++++----------- src/audio/audio_thread.c | 13 +- src/port/Engine.cpp | 6 +- src/port/resource/loaders/AudioLoader.cpp | 36 ++-- src/port/resource/loaders/AudioLoader.h | 2 +- 5 files changed, 151 insertions(+), 138 deletions(-) diff --git a/src/audio/audio_load.c b/src/audio/audio_load.c index e358afc8..65448fcb 100644 --- a/src/audio/audio_load.c +++ b/src/audio/audio_load.c @@ -37,7 +37,7 @@ void AudioLoad_ProcessAsyncLoads(s32 resetStatus); void AudioLoad_ProcessAsyncLoad(AudioAsyncLoad* asyncLoad, s32 resetStatus); void AudioLoad_AsyncDma(AudioAsyncLoad* asyncLoad, u32 size); void AudioLoad_AsyncDmaUnkMedium(uintptr_t devAddr, u8* ramAddr, u32 size, s32 unkMediumParam); -void AudioLoad_RelocateSample(TunedSample* sample, TunedSample32Bit* tSample, uintptr_t fontDataAddr, SampleBankRelocInfo* relocInfo); +void AudioLoad_RelocateSample(TunedSample* tSample, u32 fontDataAddr, SampleBankRelocInfo* relocInfo); s32 AudioLoad_RelocateFontAndPreloadSamples(s32 fontId, uintptr_t fontDataAddr, SampleBankRelocInfo* relocData, s32 isAsync); s32 AudioLoad_ProcessSamplePreloads(s32 resetStatus); @@ -394,11 +394,12 @@ void AudioLoad_SyncInitSeqPlayerInternal(s32 playerIdx, s32 seqId, s32 arg2) { func_800144E4(&gSeqPlayers[playerIdx]); index = BSWAP16(*((u16*) gSeqFontTable + seqId)); + numFonts = gSeqFontTable[index++]; fontId = 0xFF; - for (numFonts = gSeqFontTable[index++]; numFonts > 0; numFonts--) { + for (numFonts; numFonts > 0; numFonts--) { fontId = gSeqFontTable[index++]; - // AudioLoad_SyncLoadFont(fontId); + AudioLoad_SyncLoadFont(fontId); } seqData = AudioLoad_SyncLoadSeq(seqId); @@ -407,18 +408,19 @@ void AudioLoad_SyncInitSeqPlayerInternal(s32 playerIdx, s32 seqId, s32 arg2) { gSeqPlayers[playerIdx].seqId = seqId; gSeqPlayers[playerIdx].defaultFont = fontId; - gSeqPlayers[playerIdx].enabled = 1; + gSeqPlayers[playerIdx].enabled = true; gSeqPlayers[playerIdx].seqData = seqData; gSeqPlayers[playerIdx].scriptState.pc = seqData; gSeqPlayers[playerIdx].scriptState.depth = 0; gSeqPlayers[playerIdx].delay = 0; - gSeqPlayers[playerIdx].finished = 0; + gSeqPlayers[playerIdx].finished = false; } void* AudioLoad_SyncLoadSeq(s32 seqId) { AudioTable* table = AudioLoad_GetLoadTable(SEQUENCE_TABLE); - gSeqLoadStatus[seqId] = 2; - return Audio_LoadBlob(gAudioTable, table->entries[seqId].romAddr); + s32 seqIdx = AudioLoad_GetLoadTableIndex(SEQUENCE_TABLE, seqId); + + return Audio_LoadBlob(gAudioTable, table->entries[seqIdx].romAddr); } void* AudioLoad_SyncLoadSampleBank(u32 sampleBankId, s32* outMedium) { @@ -487,7 +489,7 @@ void* AudioLoad_SyncLoadFont(s32 fontId) { } if (didAllocate == 1) { - AudioLoad_RelocateFontAndPreloadSamples(fontId, fontData, &relocInfo, AUDIOLOAD_SYNC); + AudioLoad_RelocateFontAndPreloadSamples(fontId, fontData, &relocInfo, AUDIOLOAD_SYNC); } return fontData; @@ -513,46 +515,57 @@ void* AudioLoad_SyncLoad(u32 tableType, u32 id, s32* didAllocate) { size = ALIGN16(size); medium = table->entries[id].medium; cachePolicy = table->entries[id].cachePolicy; - romAddr = table->entries[id].romAddr; - switch (cachePolicy) { - case CACHEPOLICY_0: - ramAddr = AudioHeap_AllocPermanent(tableType, id, size); - if (ramAddr == NULL) { - return ramAddr; - } + switch (tableType) { + case SEQUENCE_TABLE: + romAddr = Audio_LoadBlob(gAudioTable, table->entries[id].romAddr); break; - - case CACHEPOLICY_1: - ramAddr = AudioHeap_AllocCached(tableType, size, CACHE_PERSISTENT, id); - if (ramAddr == NULL) { - return ramAddr; - } + case FONT_TABLE: + romAddr = Audio_LoadFont(table->entries[id]); break; - - case CACHEPOLICY_2: - ramAddr = AudioHeap_AllocCached(tableType, size, CACHE_TEMPORARY, id); - if (ramAddr == NULL) { - return ramAddr; - } - break; - - case CACHEPOLICY_3: - case CACHEPOLICY_4: - ramAddr = AudioHeap_AllocCached(tableType, size, CACHE_EITHER, id); - if (ramAddr == NULL) { - return ramAddr; - } + case SAMPLE_TABLE: + loadStatus = 0; break; } +// switch (cachePolicy) { +// case CACHEPOLICY_0: +// ramAddr = AudioHeap_AllocPermanent(tableType, id, size); +// if (ramAddr == NULL) { +// return ramAddr; +// } +// break; +// +// case CACHEPOLICY_1: +// ramAddr = AudioHeap_AllocCached(tableType, size, CACHE_PERSISTENT, id); +// if (ramAddr == NULL) { +// return ramAddr; +// } +// break; +// +// case CACHEPOLICY_2: +// ramAddr = AudioHeap_AllocCached(tableType, size, CACHE_TEMPORARY, id); +// if (ramAddr == NULL) { +// return ramAddr; +// } +// break; +// +// case CACHEPOLICY_3: +// case CACHEPOLICY_4: +// ramAddr = AudioHeap_AllocCached(tableType, size, CACHE_EITHER, id); +// if (ramAddr == NULL) { +// return ramAddr; +// } +// break; +// } + *didAllocate = true; - if (medium == MEDIUM_UNK) { - AudioLoad_SyncDmaUnkMedium(romAddr, ramAddr, size, table->base.unkMediumParam); - } else { - AudioLoad_SyncDma(romAddr, ramAddr, size, medium); - } +// if (medium == MEDIUM_UNK) { +// AudioLoad_SyncDmaUnkMedium(romAddr, ramAddr, size, table->base.unkMediumParam); +// } else { +// AudioLoad_SyncDma(romAddr, ramAddr, size, medium); +// } loadStatus = (cachePolicy == CACHEPOLICY_0) ? 5 : 2; } @@ -575,7 +588,7 @@ void* AudioLoad_SyncLoad(u32 tableType, u32 id, s32* didAllocate) { } break; } - return ramAddr; + return romAddr; } s32 AudioLoad_GetLoadTableIndex(s32 tableType, u32 entryId) { @@ -623,50 +636,45 @@ AudioTable* AudioLoad_GetLoadTable(s32 tableType) { } void AudioLoad_RelocateFont(s32 fontId, uintptr_t fontBaseAddr, SampleBankRelocInfo* relocData) { - uint32_t* fontDataPtrs = fontBaseAddr; - uint32_t** drumDataPtrs = fontBaseAddr; - s32 numDrums; - u32 offset; - s32 i; - s32 numInstruments; +// uint32_t* fontDataPtrs = fontBaseAddr; +// uint32_t** drumDataPtrs = fontBaseAddr; +// s32 numDrums; +// u32 offset; +// s32 i; +// s32 numInstruments; +// +// numDrums = gSoundFontList[fontId].numDrums; +// numInstruments = gSoundFontList[fontId].numInstruments; +// +// Drum** relocatedDrums = memalloc(numDrums * sizeof(Drum*)); +// Instrument** relocatedInstruments = memalloc((numInstruments + 1) * sizeof(Instrument*)); +// +// if ((fontDataPtrs[0] != 0) && (numDrums != 0)) { +// for (i = 0; i < numDrums; i++) { +// offset = *(*drumDataPtrs + i); +// if (offset != 0) { +// relocatedDrums[i] = Audio_LoadDrum(BSWAP32(offset), 0, relocData->sampleBankId1); +// } +// } +// } +// +// for (i = 1; i <= numInstruments; i++) { +// if (fontDataPtrs[i] != 0) { +// // TODO: fix this +// relocatedInstruments[i] = Audio_LoadInstrument(BSWAP32(fontDataPtrs[i]), relocData->sampleBankId1); +// } +// } + AudioTable* table = AudioLoad_GetLoadTable(FONT_TABLE); + SoundFont* font = Audio_LoadFont(table->entries[fontId]); - numDrums = gSoundFontList[fontId].numDrums; - numInstruments = gSoundFontList[fontId].numInstruments; - - Drum** relocatedDrums = memalloc(numDrums * sizeof(Drum*)); - Instrument** relocatedInstruments = memalloc((numInstruments + 1) * sizeof(Instrument*)); - - if ((fontDataPtrs[0] != 0) && (numDrums != 0)) { - for (i = 0; i < numDrums; i++) { - offset = *(*drumDataPtrs + i); - if (offset != 0) { - relocatedDrums[i] = Audio_LoadDrum(BSWAP32(offset) + fontBaseAddr, 0, 0); - } - } - } - - for (i = 1; i <= numInstruments; i++) { - if (fontDataPtrs[i] != 0) { - Instrument* instrument = Audio_LoadInstrument(BSWAP32(fontDataPtrs[i]) + fontBaseAddr, 0); - Instrument32Bit* base = fontBaseAddr + BSWAP32(fontDataPtrs[i]); - - if (instrument->normalRangeLo != 0) { - AudioLoad_RelocateSample(&instrument->lowPitchTunedSample, &base->lowPitchTunedSample, fontBaseAddr, relocData); - } - AudioLoad_RelocateSample(&instrument->normalPitchTunedSample, &base->normalPitchTunedSample, fontBaseAddr, relocData); - if (instrument->normalRangeHi != 0x7F) { - AudioLoad_RelocateSample(&instrument->highPitchTunedSample, &base->highPitchTunedSample, fontBaseAddr, relocData); - } - - relocatedInstruments[i] = instrument; - } - } - - gSoundFontList[fontId].drums = relocatedDrums; - gSoundFontList[fontId].instruments = relocatedInstruments; + gSoundFontList[fontId].drums = font->drums; + gSoundFontList[fontId].instruments = font->instruments; } void AudioLoad_SyncDma(uintptr_t devAddr, u8* ramAddr, u32 size, s32 medium) { + memcpy(ramAddr, devAddr, size); + + return; size = ALIGN16(size); osInvalDCache(ramAddr, size); @@ -937,8 +945,10 @@ void AudioLoad_Init(void) { gSoundFontList = AudioHeap_Alloc(&gInitPool, numFonts * sizeof(SoundFont)); for (i = 0; i < numFonts; i++) { - gSoundFontList[i] = Audio_LoadFont(gSoundFontTable->entries[i]); - gFontLoadStatus[i] = 2; + gSoundFontList[i].sampleBankId1 = (gSoundFontTable->entries[i].shortData1 >> 8) & 0xFF; + gSoundFontList[i].sampleBankId2 = gSoundFontTable->entries[i].shortData1 & 0xFF; + gSoundFontList[i].numInstruments = (gSoundFontTable->entries[i].shortData2 >> 8) & 0xFF; + gSoundFontList[i].numDrums = gSoundFontTable->entries[i].shortData2 & 0xFF; } ramAddr = AudioHeap_Alloc(&gInitPool, gPermanentPoolSize); @@ -1262,7 +1272,7 @@ void AudioLoad_AsyncDmaUnkMedium(uintptr_t devAddr, u8* ramAddr, u32 size, s32 u static const char devstr50[] = "Error: Already wavetable is touched %x.\n"; static const char devstr51[] = "Touch Warning: Length zero %x\n"; -void AudioLoad_RelocateSample(TunedSample* tunedSample, TunedSample32Bit* tSample, uintptr_t fontDataAddr, SampleBankRelocInfo* relocInfo) { +void AudioLoad_RelocateSample(TunedSample* tSample, u32 fontDataAddr, SampleBankRelocInfo* relocInfo) { Sample32Bit* baseSample = fontDataAddr + BSWAP32(tSample->sample); // "Touch Warning: Length zero %x\n"; @@ -1314,6 +1324,7 @@ s32 AudioLoad_RelocateFontAndPreloadSamples(s32 fontId, uintptr_t fontDataAddr, s32 pad; u32 nChunks; s32 inProgress; + isAsync = 0; inProgress = false; if (gPreloadSampleStackTop != 0) { @@ -1339,41 +1350,40 @@ s32 AudioLoad_RelocateFontAndPreloadSamples(s32 fontId, uintptr_t fontDataAddr, sampleRamAddr = NULL; #endif //! @bug Those are assignments, not equality checks. - switch (isAsync) { - case AUDIOLOAD_SYNC: - if (sample->medium = relocData->medium1) { - sampleRamAddr = AudioHeap_AllocPersistentSampleCache(sample->size, relocData->sampleBankId1, - sample->sampleAddr, sample->medium); - } else if (sample->medium = relocData->medium2) { - sampleRamAddr = AudioHeap_AllocPersistentSampleCache(sample->size, relocData->sampleBankId2, - sample->sampleAddr, sample->medium); - } - break; +// switch (isAsync) { +// case AUDIOLOAD_SYNC: +// if (sample->medium = relocData->medium1) { +// sampleRamAddr = AudioHeap_AllocPersistentSampleCache(sample->size, relocData->sampleBankId1, +// sample->sampleAddr, sample->medium); +// } else if (sample->medium = relocData->medium2) { +// sampleRamAddr = AudioHeap_AllocPersistentSampleCache(sample->size, relocData->sampleBankId2, +// sample->sampleAddr, sample->medium); +// } +// break; +// +// case AUDIOLOAD_ASYNC: +// if (sample->medium = relocData->medium1) { +// sampleRamAddr = AudioHeap_AllocTemporarySampleCache(sample->size, relocData->sampleBankId1, +// sample->sampleAddr, sample->medium); +// } else if (sample->medium = relocData->medium2) { +// sampleRamAddr = AudioHeap_AllocTemporarySampleCache(sample->size, relocData->sampleBankId2, +// sample->sampleAddr, sample->medium); +// } +// break; +// } - case AUDIOLOAD_ASYNC: - if (sample->medium = relocData->medium1) { - sampleRamAddr = AudioHeap_AllocTemporarySampleCache(sample->size, relocData->sampleBankId1, - sample->sampleAddr, sample->medium); - } else if (sample->medium = relocData->medium2) { - sampleRamAddr = AudioHeap_AllocTemporarySampleCache(sample->size, relocData->sampleBankId2, - sample->sampleAddr, sample->medium); - } - break; - } - - if (sampleRamAddr == NULL) { - continue; - } +// if (sampleRamAddr == NULL) { +// continue; +// } switch (isAsync) { case AUDIOLOAD_SYNC: if (sample->medium == MEDIUM_UNK) { - AudioLoad_SyncDmaUnkMedium(sample->sampleAddr, sampleRamAddr, sample->size, - gSampleBankTable->base.unkMediumParam); +// AudioLoad_SyncDmaUnkMedium(sample->sampleAddr, sampleRamAddr, sample->size, gSampleBankTable->base.unkMediumParam); sample->sampleAddr = sampleRamAddr; sample->medium = MEDIUM_RAM; } else { - AudioLoad_SyncDma(sample->sampleAddr, sampleRamAddr, sample->size, sample->medium); +// AudioLoad_SyncDma(sample->sampleAddr, sampleRamAddr, sample->size, sample->medium); sample->sampleAddr = sampleRamAddr; sample->medium = MEDIUM_RAM; } diff --git a/src/audio/audio_thread.c b/src/audio/audio_thread.c index 0b42ed33..1fec6f02 100644 --- a/src/audio/audio_thread.c +++ b/src/audio/audio_thread.c @@ -55,7 +55,7 @@ void AudioThread_CreateNextAudioBuffer(s16 *samples, u32 num_samples) { gCurAudioFrameDmaCount = 0; AudioLoad_DecreaseSampleDmaTtls(); - // AudioLoad_ProcessLoads(gAudioResetStep); + AudioLoad_ProcessLoads(gAudioResetStep); if (MQ_GET_MESG(gAudioSpecQueue, &specId)) { if (gAudioResetStep == 0) { @@ -362,16 +362,19 @@ void AudioThread_ProcessCmds(u32 msg) { u8 writePos; if (!gThreadCmdQueueFinished) { - gCurCmdReadPos = (msg >> 8) & 0xFF; + gCurCmdReadPos = msg >> 8; } - writePos = msg & 0xFF; + while (true) { + writePos = msg & 0xFF; + if (gCurCmdReadPos == writePos) { gThreadCmdQueueFinished = 0; break; } - cmd = &gThreadCmdBuffer[gCurCmdReadPos & 0xFF]; - gCurCmdReadPos++; + + cmd = &gThreadCmdBuffer[gCurCmdReadPos++ & 0xFF]; + if (cmd->op == AUDIOCMD_OP_GLOBAL_STOP_AUDIOCMDS) { gThreadCmdQueueFinished = true; break; diff --git a/src/port/Engine.cpp b/src/port/Engine.cpp index 53b10a5f..2b22e1d2 100644 --- a/src/port/Engine.cpp +++ b/src/port/Engine.cpp @@ -42,7 +42,7 @@ void AudioThread_CreateNextAudioBuffer(int16_t *samples, uint32_t num_samples); GameEngine* GameEngine::Instance; static GamePool MemoryPool = { - .chunk = 1024 * 20, + .chunk = 1024 * 512, .cursor = 0, .length = 0, .memory = nullptr @@ -437,7 +437,7 @@ extern "C" int32_t OTRConvertHUDXToScreenX(int32_t v) { extern "C" void* GameEngine_Malloc(size_t size) { // This is really wrong - // return malloc(size); + return malloc(size); // TODO: Kenix please take a look at this, i think it works but you are better at this const auto chunk = MemoryPool.chunk; @@ -449,7 +449,7 @@ extern "C" void* GameEngine_Malloc(size_t size) { if(MemoryPool.cursor + size < MemoryPool.length) { const auto res = static_cast(MemoryPool.memory) + MemoryPool.cursor; MemoryPool.cursor += size; - SPDLOG_INFO("Allocating {} into memory pool", size); + //SPDLOG_INFO("Allocating {} into memory pool", size); return res; } diff --git a/src/port/resource/loaders/AudioLoader.cpp b/src/port/resource/loaders/AudioLoader.cpp index 03175e06..6998102b 100644 --- a/src/port/resource/loaders/AudioLoader.cpp +++ b/src/port/resource/loaders/AudioLoader.cpp @@ -43,37 +43,36 @@ EnvelopePoint* Audio_LoadEnvelope(uint32_t addr) { return envelopes; } -extern "C" SoundFont Audio_LoadFont(AudioTableEntry entry) { +extern "C" SoundFont* Audio_LoadFont(AudioTableEntry entry) { auto reader = Audio_MakeReader(gAudioBank, entry.romAddr); - SoundFont font = { - .numInstruments = (uint32_t)((entry.shortData2 >> 8) & 0xFF), - .numDrums = (uint32_t)(entry.shortData2 & 0xFF), - .sampleBankId1 = (uint32_t)((entry.shortData1 >> 8) & 0xFF), - .sampleBankId2 = (uint32_t)(entry.shortData1 & 0xFF), - }; - - font.instruments = memallocn(Instrument*, font.numInstruments); - font.drums = memallocn(Drum*, font.numDrums); + SoundFont* font = memalloc(SoundFont); + + font->numInstruments = (entry.shortData2 >> 8) & 0xFFu; + font->numDrums = entry.shortData2 & 0xFFu; + font->sampleBankId1 = (entry.shortData1 >> 8) & 0xFFu; + font->sampleBankId2 = entry.shortData1 & 0xFFu; + font->instruments = memallocn(Instrument*, font->numInstruments); + font->drums = memallocn(Drum*, font->numDrums); uint32_t drumBaseAddr = entry.romAddr + reader.ReadUInt32(); uint32_t instBaseAddr = 4; - if(font.drums != nullptr && drumBaseAddr != 0){ + if(font->drums != nullptr && drumBaseAddr != 0){ reader.Seek(drumBaseAddr, Ship::SeekOffsetType::Start); - for(size_t i = 0; i < font.numDrums; i++){ - font.drums[i] = Audio_LoadDrum(entry.romAddr + reader.ReadUInt32(), entry.romAddr, font.sampleBankId1); + for(size_t i = 0; i < font->numDrums; i++){ + font->drums[i] = Audio_LoadDrum(entry.romAddr + reader.ReadUInt32(), entry.romAddr, font->sampleBankId1); } } - if(font.instruments != nullptr){ + if(font->instruments != nullptr){ reader.Seek(instBaseAddr, Ship::SeekOffsetType::Start); - for(size_t i = 1; i < font.numInstruments; i++){ - font.instruments[i] = Audio_LoadInstrument(reader.ReadUInt32(), font.sampleBankId1); + for(size_t i = 1; i < font->numInstruments; i++){ + font->instruments[i] = Audio_LoadInstrument(reader.ReadUInt32(), font->sampleBankId1); } } - gSampleFontLoadStatus[font.sampleBankId1] = 2; + gSampleFontLoadStatus[font->sampleBankId1] = 2; return font; } @@ -105,7 +104,7 @@ extern "C" AdpcmBook* Audio_LoadBook(uint32_t addr) { size_t length = 8 * book->order * book->numPredictors; book->book = memallocn(int16_t, length); - if(length > 16){ + if(length > 0x40){ return nullptr; } @@ -128,6 +127,7 @@ Sample* Audio_LoadSample(uint32_t sampleAddr, uint32_t baseAddr = 0, uint32_t sa sample->isRelocated = 1; sample->sampleAddr = (uint8_t*) Audio_LoadBlob(gAudioTable, gSeqTableInit.entries[sampleBankID].romAddr) + addr; + gUsedSamples[gNumUsedSamples++] = sample; return sample; } diff --git a/src/port/resource/loaders/AudioLoader.h b/src/port/resource/loaders/AudioLoader.h index 8f2f81da..f28dc7d4 100644 --- a/src/port/resource/loaders/AudioLoader.h +++ b/src/port/resource/loaders/AudioLoader.h @@ -13,7 +13,7 @@ AdpcmLoop* Audio_LoadLoop(uint32_t addr); Instrument* Audio_LoadInstrument(uint32_t addr, uint32_t sampleBankID); Drum* Audio_LoadDrum(uint32_t addr, uint32_t baseAddr, uint32_t sampleBankID); Sample* Audio_LoadSample(uint32_t addr, uint32_t sampleBankID); -SoundFont Audio_LoadFont(AudioTableEntry entry); +SoundFont* Audio_LoadFont(AudioTableEntry entry); #ifdef __cplusplus }