Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
louist103 committed Sep 24, 2024
1 parent a554307 commit fcc0ac6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 58 deletions.
80 changes: 26 additions & 54 deletions ZAPD/ZAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand All @@ -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");
}
Expand All @@ -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);
Expand Down Expand Up @@ -138,24 +124,16 @@ SampleEntry* ZAudio::ParseSampleEntry(const std::vector<uint8_t>& 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<uint8_t>(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;
Expand All @@ -171,6 +149,7 @@ SampleEntry* ZAudio::ParseSampleEntry(const std::vector<uint8_t>& 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);
}
Expand All @@ -179,42 +158,28 @@ SampleEntry* ZAudio::ParseSampleEntry(const std::vector<uint8_t>& 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)));
}

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];
}
}

Expand All @@ -226,7 +191,7 @@ std::vector<AudioTableEntry> ZAudio::ParseAudioTable(const std::vector<uint8_t>&
int romAddr = BitConverter::ToInt16BE(codeData, baseOffset + 4);

int currentOffset = baseOffset + 16;

entries.reserve(numEntries);
for (int i = 0; i < numEntries; i++)
{
AudioTableEntry entry;
Expand Down Expand Up @@ -259,6 +224,10 @@ void ZAudio::ParseSoundFont(const std::vector<uint8_t>& 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++)
{
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions ZAPD/ZAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,14 @@ class ZAudio : public ZResource
std::vector<AudioTableEntry> sequenceTable;
std::vector<AudioTableEntry> sampleBankTable;
std::vector<std::vector<char>> sequences;
std::map<uint64_t, SampleEntry*> samples;
std::map<uint32_t, SampleEntry*> samples;
std::vector<std::vector<uint32_t>> fontIndices;
std::vector<std::string> seqNames;
std::map<uint32_t, std::string> soundFontNames;

// First Key = Bank ID, Sec Key = Sample Data Offset, Third Key = LoopDataOffset,
std::map<uint32_t, std::map<uint32_t, std::string>> sampleOffsets;

// Key = Loop Offset, Value = Sample Offset
std::map<uint64_t, uint32_t> loopOverrideSamples;

ZAudio(ZFile* nParent);

void ParseXML(tinyxml2::XMLElement* reader) override;
Expand Down

0 comments on commit fcc0ac6

Please sign in to comment.