From c5df54ae273aeab31c3c163028795d288ec9affb Mon Sep 17 00:00:00 2001 From: Nicholas Estelami Date: Mon, 13 Jun 2022 21:54:25 -0400 Subject: [PATCH] Fixed last remaining audio bugs --- OTRExporter/OTRExporter/AudioExporter.cpp | 29 ++++++++--------- ZAPDTR/ZAPD/ZAudio.cpp | 32 ++++++++++++++++--- ZAPDTR/ZAPD/ZAudio.h | 8 ++++- libultraship/libultraship/Audio.cpp | 10 +++--- .../libultraship/Lib/Fast3D/gfx_pc.cpp | 2 +- soh/assets/xml/GC_NMQ_D/audio/Audio.xml | 7 ++-- soh/include/z64audio.h | 1 - soh/src/code/audio_init_params.c | 1 - soh/src/code/audio_synthesis.c | 4 +-- 9 files changed, 61 insertions(+), 33 deletions(-) diff --git a/OTRExporter/OTRExporter/AudioExporter.cpp b/OTRExporter/OTRExporter/AudioExporter.cpp index f1a9bb2fd..a4d847771 100644 --- a/OTRExporter/OTRExporter/AudioExporter.cpp +++ b/OTRExporter/OTRExporter/AudioExporter.cpp @@ -10,22 +10,16 @@ void OTRExporter_Audio::WriteSampleEntryReference(ZAudio* audio, SampleEntry* en { writer->Write((uint8_t)(entry != nullptr ? 1 : 0)); - uint32_t addr = 0; - - for (auto pair : samples) - { - if (pair.second == entry) - { - addr = pair.first; - break; - } - } - if (entry != nullptr) { - if (audio->sampleOffsets[entry->bankId].find(entry->sampleDataOffset) != audio->sampleOffsets[entry->bankId].end()) + if (audio->sampleOffsets[entry->bankId].find(entry->sampleLoopOffset) != audio->sampleOffsets[entry->bankId].end()) { - writer->Write(StringHelper::Sprintf("audio/samples/%s", audio->sampleOffsets[entry->bankId][entry->sampleDataOffset].c_str())); + if (audio->sampleOffsets[entry->bankId][entry->sampleLoopOffset].find(entry->sampleDataOffset) != audio->sampleOffsets[entry->bankId][entry->sampleLoopOffset].end()) + { + writer->Write(StringHelper::Sprintf("audio/samples/%s", audio->sampleOffsets[entry->bankId][entry->sampleLoopOffset][entry->sampleDataOffset].c_str())); + } + else + writer->Write(entry->fileName); } else writer->Write(entry->fileName); @@ -101,8 +95,13 @@ void OTRExporter_Audio::Save(ZResource* res, const fs::path& outPath, BinaryWrit std::string basePath = ""; - if (audio->sampleOffsets[pair.second->bankId].find(pair.second->sampleDataOffset) != audio->sampleOffsets[pair.second->bankId].end()) - basePath = StringHelper::Sprintf("samples/%s", audio->sampleOffsets[pair.second->bankId][pair.second->sampleDataOffset].c_str()); + if (audio->sampleOffsets[pair.second->bankId].find(pair.second->sampleLoopOffset) != audio->sampleOffsets[pair.second->bankId].end()) + { + if (audio->sampleOffsets[pair.second->bankId][pair.second->sampleLoopOffset].find(pair.second->sampleDataOffset) != audio->sampleOffsets[pair.second->bankId][pair.second->sampleLoopOffset].end()) + basePath = StringHelper::Sprintf("samples/%s", audio->sampleOffsets[pair.second->bankId][pair.second->sampleLoopOffset][pair.second->sampleDataOffset].c_str()); + else + basePath = StringHelper::Sprintf("samples/sample_%08X", pair.first); + } else basePath = StringHelper::Sprintf("samples/sample_%08X", pair.first); diff --git a/ZAPDTR/ZAPD/ZAudio.cpp b/ZAPDTR/ZAPD/ZAudio.cpp index 8dd985701..976133513 100644 --- a/ZAPDTR/ZAPD/ZAudio.cpp +++ b/ZAPDTR/ZAPD/ZAudio.cpp @@ -26,7 +26,6 @@ void ZAudio::ParseXML(tinyxml2::XMLElement* reader) while (child != nullptr) { - int bp = 0; if (std::string(child->Value()) == "Sequences") { auto seqChild = child->FirstChildElement(); @@ -52,7 +51,17 @@ void ZAudio::ParseXML(tinyxml2::XMLElement* reader) if (std::string(sampChild->Value()) == "Sample") { auto atStr = sampChild->FirstChildElement()->Attribute("At"); - sampleOffsets[bankId][StringHelper::StrToL(atStr, 16)] = sampChild->Attribute("Name"); + auto loopStr = sampChild->FirstChildElement()->Attribute("LoopOffset"); + uint32_t loopOffset = 0xFFFFFFFF; + uint32_t atOffset = StringHelper::StrToL(atStr, 16); + + if (loopStr != NULL) + { + loopOffset = StringHelper::StrToL(loopStr, 16); + specialLoopSamples[loopOffset] = atOffset; + } + + sampleOffsets[bankId][loopOffset][atOffset] = sampChild->Attribute("Name"); } sampChild = sampChild->NextSiblingElement(); @@ -140,11 +149,20 @@ SampleEntry* ZAudio::ParseSampleEntry(std::vector audioBank, sample->loop.end = BitConverter::ToInt32BE(audioBank, loopOffset + 4); sample->loop.count = BitConverter::ToInt32BE(audioBank, loopOffset + 8); - if (sample->loop.count != 0xFFFFFFFF) + if (sample->loop.start == 0x3ADB) { - for (int i = 0; i < sample->loop.count; i++) + int bp = 0; + } + + if (/* sample->loop.count != 0xFFFFFFFF && */ sample->loop.count != 0) + { + //for (int i = 0; i < sample->loop.count; i++) + for (int i = 0; i < 16; i++) { - int16_t state = BitConverter::ToInt16BE(sample->data, loopOffset + 16 + (i * 2)); + //if ((loopOffset + 16 + (i * 2)) >= audioBank.size()) + //break; + + int16_t state = BitConverter::ToInt16BE(audioBank, loopOffset + 16 + (i * 2)); sample->loop.states.push_back(state); } } @@ -159,6 +177,10 @@ SampleEntry* ZAudio::ParseSampleEntry(std::vector audioBank, } sample->sampleDataOffset = sampleDataOffset; + + if (specialLoopSamples.find(loopOffset) != specialLoopSamples.end()) + sample->sampleLoopOffset = loopOffset; + sample->fileName = StringHelper::Sprintf("audio/samples/sample_%08X", sampleOffset); samples[sampleOffset] = sample; diff --git a/ZAPDTR/ZAPD/ZAudio.h b/ZAPDTR/ZAPD/ZAudio.h index 5d18c2e71..511ba5ed6 100644 --- a/ZAPDTR/ZAPD/ZAudio.h +++ b/ZAPDTR/ZAPD/ZAudio.h @@ -29,6 +29,7 @@ struct SampleEntry std::string fileName; uint8_t bankId; uint32_t sampleDataOffset; + uint32_t sampleLoopOffset = 0xFFFFFFFF; uint8_t codec; uint8_t medium; uint8_t unk_bit26; @@ -94,7 +95,12 @@ public: std::map samples; std::vector> fontIndices; std::vector seqNames; - std::map> sampleOffsets; + + // First Key = Bank ID, Sec Key = LoopDataOffset, Third Key = Sample Data Offset + std::map>> sampleOffsets; + + // Key = Loop Offset, Value = Sample Offset + std::map specialLoopSamples; ZAudio(ZFile* nParent); diff --git a/libultraship/libultraship/Audio.cpp b/libultraship/libultraship/Audio.cpp index 16168a01c..4f4668e88 100644 --- a/libultraship/libultraship/Audio.cpp +++ b/libultraship/libultraship/Audio.cpp @@ -73,11 +73,11 @@ namespace Ship { InstrumentEntry entry; - entry.isValidEntry = reader->ReadByte(); - entry.loaded = reader->ReadByte(); - entry.normalRangeLo = reader->ReadByte(); - entry.normalRangeHi = reader->ReadByte(); - entry.releaseRate = reader->ReadByte(); + entry.isValidEntry = reader->ReadUByte(); + entry.loaded = reader->ReadUByte(); + entry.normalRangeLo = reader->ReadUByte(); + entry.normalRangeHi = reader->ReadUByte(); + entry.releaseRate = reader->ReadUByte(); entry.env = ReadEnvelopeData(reader); diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp index 452b511d8..8d7933c0d 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp @@ -2085,7 +2085,7 @@ static inline void* seg_addr(uintptr_t w1) uint32_t segNum = (w1 >> 24); uint32_t offset = w1 & 0x00FFFFFE; - //offset = 0; // Cursed Malon bug + offset = 0; // Cursed Malon bug if (segmentPointers[segNum] != 0) return (void*)(segmentPointers[segNum] + offset); diff --git a/soh/assets/xml/GC_NMQ_D/audio/Audio.xml b/soh/assets/xml/GC_NMQ_D/audio/Audio.xml index 801228b34..2d35b24c2 100644 --- a/soh/assets/xml/GC_NMQ_D/audio/Audio.xml +++ b/soh/assets/xml/GC_NMQ_D/audio/Audio.xml @@ -461,8 +461,11 @@ - - + + + + + diff --git a/soh/include/z64audio.h b/soh/include/z64audio.h index b1b3bb06e..b804eb193 100644 --- a/soh/include/z64audio.h +++ b/soh/include/z64audio.h @@ -687,7 +687,6 @@ typedef struct { union{ u32 opArgs; struct { - // OTRTODO: struct members swapped for quick audio u8 arg2; u8 arg1; u8 arg0; diff --git a/soh/src/code/audio_init_params.c b/soh/src/code/audio_init_params.c index cb479020d..ebe5e8130 100644 --- a/soh/src/code/audio_init_params.c +++ b/soh/src/code/audio_init_params.c @@ -66,7 +66,6 @@ ReverbSettings D_80133420[][3] = { }, }; -// OTRTODO AudioSpec gAudioSpecs[18] = { { 44100, 1, 24, 4, 0, 0, 2, D_80133420[0], 0x300, 0x200, 0x7FFF, 0x7F0, 0xE00, 0, 0x4000, 0x2880, 0, 0, 0 }, { 44100, 1, 24, 4, 0, 0, 2, D_80133420[1], 0x300, 0x200, 0x7FFF, 0x7F0, 0xE00, 0, 0x3800, 0x2880, 0, 0, 0 }, diff --git a/soh/src/code/audio_synthesis.c b/soh/src/code/audio_synthesis.c index 0f402b111..2c5d03102 100644 --- a/soh/src/code/audio_synthesis.c +++ b/soh/src/code/audio_synthesis.c @@ -561,8 +561,8 @@ Acmd* AudioSynth_DoOneAudioUpdate(s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updat NoteSubEu* noteSubEu2; s32 unk14; - if (aiBufLen == 0) - return; + //if (aiBufLen == 0) + //return; t = gAudioContext.numNotes * updateIndex; count = 0;