From fcc0ac62276d2f29fac1f34d92868492fa857d04 Mon Sep 17 00:00:00 2001 From: Louis <35883445+louist103@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:22:39 -0400 Subject: [PATCH] cleanups --- ZAPD/ZAudio.cpp | 80 ++++++++++++++++--------------------------------- ZAPD/ZAudio.h | 5 +--- 2 files changed, 27 insertions(+), 58 deletions(-) diff --git a/ZAPD/ZAudio.cpp b/ZAPD/ZAudio.cpp index 00acce1..40efcbb 100644 --- a/ZAPD/ZAudio.cpp +++ b/ZAPD/ZAudio.cpp @@ -27,13 +27,13 @@ void ZAudio::ParseXML(tinyxml2::XMLElement* reader) while (child != nullptr) { - if (std::string(child->Value()) == "Sequences") + if (strcmp(child->Value(), "Sequences") == 0) { auto seqChild = child->FirstChildElement(); while (seqChild != nullptr) { - if (std::string(seqChild->Value()) == "Sequence") + if (strcmp(seqChild->Value(), "Sequence") == 0) { seqNames.push_back(seqChild->Attribute("Name")); } @@ -42,30 +42,16 @@ void ZAudio::ParseXML(tinyxml2::XMLElement* reader) } } - if (std::string(child->Value()) == "Samples") + if (strcmp(child->Value(), "Samples") == 0) { int bankId = child->IntAttribute("Bank", 0); auto sampChild = child->FirstChildElement(); while (sampChild != nullptr) { - if (std::string(sampChild->Value()) == "Sample") + if (strcmp(sampChild->Value(), "Sample") == 0) { - auto atStr = sampChild->Attribute("Offset"); - auto loopStr = sampChild->Attribute("LoopOffset"); - uint32_t loopOffset = 0; - uint32_t atOffset = StringHelper::StrToL(atStr, 16); - if (atOffset == 0x000593f0) - { - int bp = 0; - } - - //if (loopStr != NULL) - //{ - // loopOffset = StringHelper::StrToL(loopStr, 16); - // loopOverrideSamples[((uint64_t)atOffset << 32) | loopOffset] = loopOffset; - //} - + uint32_t atOffset = sampChild->UnsignedAttribute("Offset"); sampleOffsets[bankId][atOffset] = sampChild->Attribute("Name"); } @@ -74,7 +60,7 @@ void ZAudio::ParseXML(tinyxml2::XMLElement* reader) } } - if (std::string(child->Value()) == "Soundfont") + if (strcmp(child->Value(), "Soundfont") == 0) { auto name = child->Attribute("Name"); auto index = child->IntAttribute("Index", 0); @@ -138,24 +124,16 @@ SampleEntry* ZAudio::ParseSampleEntry(const std::vector& audioBank, int sampleSize = BitConverter::ToInt32BE(audioBank, sampleOffset + 0) & 0x00FFFFFF; int loopOffset = BitConverter::ToInt32BE(audioBank, sampleOffset + 8) + baseOffset; int bookOffset = BitConverter::ToInt32BE(audioBank, sampleOffset + 12) + baseOffset; - - if (sampleDataOffset == 0x000593f0) - { - int bp = 0; - } - if (samples.find((uint64_t)sampleDataOffset << 32 | 0) == samples.end()) + + if (samples.find((uint64_t)sampleDataOffset) == samples.end()) { SampleEntry* sample = new SampleEntry(); sample->bankId = bankIndex; - - char* sampleData = (char*)malloc(sampleSize); - memcpy(sampleData, audioTable.data() + sampleDataOffset, sampleSize); sample->data = std::vector(sampleSize); - memcpy(sample->data.data(), sampleData, sampleSize); - free(sampleData); + memcpy(sample->data.data(), audioTable.data() + sampleDataOffset, sampleSize); uint32_t origField = (BitConverter::ToUInt32BE(audioBank, sampleOffset + 0)); sample->codec = (origField >> 28) & 0x0F; @@ -171,6 +149,7 @@ SampleEntry* ZAudio::ParseSampleEntry(const std::vector& audioBank, { for (int i = 0; i < 16; i++) { + // TODO can loop.states be an array of size 16? int16_t state = BitConverter::ToInt16BE(audioBank, loopOffset + 16 + (i * 2)); sample->loop.states.push_back(state); } @@ -179,7 +158,9 @@ SampleEntry* ZAudio::ParseSampleEntry(const std::vector& audioBank, sample->book.order = BitConverter::ToInt32BE(audioBank, bookOffset + 0); sample->book.npredictors = BitConverter::ToInt32BE(audioBank, bookOffset + 4); - for (int i = 0; i < sample->book.npredictors * sample->book.order * 8; i++) + int32_t numBooks = sample->book.npredictors * sample->book.order * 8; + sample->book.books.reserve(numBooks); + for (int i = 0; i < numBooks; i++) { sample->book.books.push_back( BitConverter::ToInt16BE(audioBank, bookOffset + 8 + (i * 2))); @@ -187,34 +168,18 @@ SampleEntry* ZAudio::ParseSampleEntry(const std::vector& audioBank, sample->sampleDataOffset = sampleDataOffset; - //if ((loopOverrideSamples.contains(((uint64_t)sampleDataOffset << 32) | loopOffset))) - //{ - // sample->sampleLoopOffset = - // loopOverrideSamples[(((uint64_t)sampleDataOffset << 32) | loopOffset)]; - //} - //else - //{ - // if (sampleOffsets[bankIndex][sampleDataOffset].contains(0xFFFFFFFF)) - // { - // std::string nameBackup = sampleOffsets[bankIndex][sampleDataOffset][0xFFFFFFFF]; - // sampleOffsets[bankIndex][sampleDataOffset].erase(0xFFFFFFFF); - // sampleOffsets[bankIndex][sampleDataOffset][loopOffset] = nameBackup; - // } - // sample->sampleLoopOffset = loopOffset; - //} sample->sampleLoopOffset = 0; - sample->fileName = sampleOffsets[bankIndex][sampleDataOffset]; // StringHelper::Sprintf("audio/samples/sample_%08X", - // sampleOffset); + sample->fileName = sampleOffsets[bankIndex][sampleDataOffset]; - samples[(uint64_t)sampleDataOffset << 32 | 0] = sample; + samples[sampleDataOffset] = sample; return sample; } else { - return samples[(uint64_t)sampleDataOffset << 32 | 0]; + return samples[sampleDataOffset]; } } @@ -226,7 +191,7 @@ std::vector ZAudio::ParseAudioTable(const std::vector& int romAddr = BitConverter::ToInt16BE(codeData, baseOffset + 4); int currentOffset = baseOffset + 16; - + entries.reserve(numEntries); for (int i = 0; i < numEntries; i++) { AudioTableEntry entry; @@ -259,6 +224,10 @@ void ZAudio::ParseSoundFont(const std::vector& codeData, const std::vec int numDrums = entry.data2 & 0xFF; int numSfx = entry.data3; + entry.drums.reserve(numDrums); + entry.soundEffects.reserve(numSfx); + entry.instruments.reserve(numInstruments); + int currentOffset = BitConverter::ToInt32BE(codeData, ptr) + ptr; for (int i = 0; i < numDrums; i++) { @@ -389,6 +358,9 @@ void ZAudio::ParseRawData() sequenceTable = ParseAudioTable(codeData, gSequenceTableOffset); sampleBankTable = ParseAudioTable(codeData, gSampleBankTableOffset); + fontIndices.reserve(sequenceTable.size()); + sequences.reserve(sequenceTable.size()); + // SEQEUNCE FONT TABLE PARSING for (int i = 0; i < sequenceTable.size(); i++) { @@ -407,9 +379,9 @@ void ZAudio::ParseRawData() // SAMPLE/FONT PARSING - for (int i = 0; i < soundFontTable.size(); i++) + for (auto& sft : soundFontTable) { - ParseSoundFont(audioBankData, audioTableData, sampleBankTable, soundFontTable[i]); + ParseSoundFont(audioBankData, audioTableData, sampleBankTable, sft); } // SEQUENCE PARSING diff --git a/ZAPD/ZAudio.h b/ZAPD/ZAudio.h index 7aec4d1..d9504a8 100644 --- a/ZAPD/ZAudio.h +++ b/ZAPD/ZAudio.h @@ -92,7 +92,7 @@ class ZAudio : public ZResource std::vector sequenceTable; std::vector sampleBankTable; std::vector> sequences; - std::map samples; + std::map samples; std::vector> fontIndices; std::vector seqNames; std::map soundFontNames; @@ -100,9 +100,6 @@ class ZAudio : public ZResource // First Key = Bank ID, Sec Key = Sample Data Offset, Third Key = LoopDataOffset, std::map> sampleOffsets; - // Key = Loop Offset, Value = Sample Offset - std::map loopOverrideSamples; - ZAudio(ZFile* nParent); void ParseXML(tinyxml2::XMLElement* reader) override;