Skip to content

Commit

Permalink
Fix sample exporting (#20)
Browse files Browse the repository at this point in the history
* fix how samples are exported

* cleanups

* forgot to re-add these
  • Loading branch information
louist103 authored Oct 5, 2024
1 parent b002f28 commit 215d3c2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 53 deletions.
81 changes: 40 additions & 41 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,34 +42,25 @@ 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->FirstChildElement()->Attribute("At");
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");
uint32_t atOffset = sampChild->UnsignedAttribute("Offset");

sampleOffsets[bankId][atOffset] = sampChild->Attribute("Name");
}

sampChild = sampChild->NextSiblingElement();
}
}

if (std::string(child->Value()) == "Soundfont")
if (strcmp(child->Value(), "Soundfont") == 0)
{
auto name = child->Attribute("Name");
auto index = child->IntAttribute("Index", 0);
Expand All @@ -80,7 +71,7 @@ void ZAudio::ParseXML(tinyxml2::XMLElement* reader)
}
}

std::vector<AdsrEnvelope*> ZAudio::ParseEnvelopeData(std::vector<uint8_t> audioBank, std::vector<uint8_t> audioTable, int envelopeOffset, int baseOffset)
std::vector<AdsrEnvelope*> ZAudio::ParseEnvelopeData(const std::vector<uint8_t>& audioBank, const std::vector<uint8_t>& audioTable, int envelopeOffset, int baseOffset)
{
std::vector<AdsrEnvelope*> result;

Expand All @@ -102,8 +93,8 @@ std::vector<AdsrEnvelope*> ZAudio::ParseEnvelopeData(std::vector<uint8_t> audioB
return result;
}

SoundFontEntry* ZAudio::ParseSoundFontEntry(std::vector<uint8_t> audioBank,
std::vector<uint8_t> audioTable,
SoundFontEntry* ZAudio::ParseSoundFontEntry(const std::vector<uint8_t>& audioBank,
const std::vector<uint8_t>& audioTable,
AudioTableEntry audioSampleBankEntry, int bankIndex,
int soundFontOffset,
int baseOffset)
Expand All @@ -123,28 +114,26 @@ SoundFontEntry* ZAudio::ParseSoundFontEntry(std::vector<uint8_t> audioBank,
return soundFont;
}

SampleEntry* ZAudio::ParseSampleEntry(std::vector<uint8_t> audioBank,
std::vector<uint8_t> audioTable,
SampleEntry* ZAudio::ParseSampleEntry(const std::vector<uint8_t>& audioBank,
const std::vector<uint8_t>& audioTable,
AudioTableEntry audioSampleBankEntry, int bankIndex,
int sampleOffset,
int baseOffset)
{
int sampleDataOffset = BitConverter::ToInt32BE(audioBank, sampleOffset + 4) + audioSampleBankEntry.ptr;
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 (samples.find(sampleOffset) == samples.end())
if (samples.find(sampleDataOffset) == samples.end())
{
SampleEntry* sample = new SampleEntry();

sample->bankId = bankIndex;

int sampleSize = BitConverter::ToInt32BE(audioBank, sampleOffset + 0) & 0x00FFFFFF;
int loopOffset = BitConverter::ToInt32BE(audioBank, sampleOffset + 8) + baseOffset;
int bookOffset = BitConverter::ToInt32BE(audioBank, sampleOffset + 12) + baseOffset;

char* sampleData = (char*)malloc(sampleSize);
memcpy(sampleData, audioTable.data() + sampleDataOffset, sampleSize);
sample->data = std::vector<uint8_t>(sampleSize);
memcpy(sample->data.data(), sampleData, sampleSize);
memcpy(sample->data.data(), audioTable.data() + sampleDataOffset, sampleSize);

uint32_t origField = (BitConverter::ToUInt32BE(audioBank, sampleOffset + 0));
sample->codec = (origField >> 28) & 0x0F;
Expand All @@ -160,6 +149,7 @@ SampleEntry* ZAudio::ParseSampleEntry(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 @@ -168,38 +158,40 @@ SampleEntry* ZAudio::ParseSampleEntry(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 (specialLoopSamples.find(loopOffset) != specialLoopSamples.end())
sample->sampleLoopOffset = loopOffset;
sample->sampleLoopOffset = 0;


sample->fileName = StringHelper::Sprintf("audio/samples/sample_%08X", sampleOffset);
sample->fileName = sampleOffsets[bankIndex][sampleDataOffset];

samples[sampleOffset] = sample;
samples[sampleDataOffset] = sample;

return sample;
}
else
{
return samples[sampleOffset];
return samples[sampleDataOffset];
}
}

std::vector<AudioTableEntry> ZAudio::ParseAudioTable(std::vector<uint8_t> codeData, int baseOffset)
std::vector<AudioTableEntry> ZAudio::ParseAudioTable(const std::vector<uint8_t>& codeData, int baseOffset)
{
std::vector<AudioTableEntry> entries;

int numEntries = BitConverter::ToInt16BE(codeData, baseOffset + 0);
int romAddr = BitConverter::ToInt16BE(codeData, baseOffset + 4);

int currentOffset = baseOffset + 16;

entries.reserve(numEntries);
for (int i = 0; i < numEntries; i++)
{
AudioTableEntry entry;
Expand All @@ -220,8 +212,8 @@ std::vector<AudioTableEntry> ZAudio::ParseAudioTable(std::vector<uint8_t> codeDa
return entries;
}

void ZAudio::ParseSoundFont(std::vector<uint8_t> codeData, std::vector<uint8_t> audioTable,
std::vector<AudioTableEntry> audioSampleBank,
void ZAudio::ParseSoundFont(const std::vector<uint8_t>& codeData, const std::vector<uint8_t>& audioTable,
const std::vector<AudioTableEntry>& audioSampleBank,
AudioTableEntry& entry)
{
int ptr = entry.ptr;
Expand All @@ -232,6 +224,10 @@ void ZAudio::ParseSoundFont(std::vector<uint8_t> codeData, std::vector<uint8_t>
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 @@ -362,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 @@ -380,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
21 changes: 9 additions & 12 deletions ZAPD/ZAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,29 @@ class ZAudio : public ZResource
std::vector<std::string> seqNames;
std::map<uint32_t, std::string> soundFontNames;

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

// Key = Loop Offset, Value = Sample Offset
std::map<uint32_t, uint32_t> specialLoopSamples;
// First Key = Bank ID, Sec Key = Sample Data Offset.
std::map<uint32_t, std::map<uint32_t, std::string>> sampleOffsets;

ZAudio(ZFile* nParent);

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

std::vector<AdsrEnvelope*> ParseEnvelopeData(std::vector<uint8_t> audioBank, std::vector<uint8_t> audioTable,
std::vector<AdsrEnvelope*> ParseEnvelopeData(const std::vector<uint8_t>& audioBank, const std::vector<uint8_t>& audioTable,
int envelopeOffset, int baseOffset);

SoundFontEntry* ParseSoundFontEntry(std::vector<uint8_t> audioBank,
std::vector<uint8_t> audioTable,
SoundFontEntry* ParseSoundFontEntry(const std::vector<uint8_t>& audioBank,
const std::vector<uint8_t>& audioTable,
AudioTableEntry audioSampleBankEntry, int bankIndex,
int soundFontOffset,
int baseOffset);

SampleEntry* ParseSampleEntry(std::vector<uint8_t> audioBank, std::vector<uint8_t> audioTable,
SampleEntry* ParseSampleEntry(const std::vector<uint8_t>& audioBank, const std::vector<uint8_t>& audioTable,
AudioTableEntry audioSampleBankEntry, int bankIndex,
int sampleOffset, int baseOffset);

std::vector<AudioTableEntry> ParseAudioTable(std::vector<uint8_t> codeData, int baseOffset);
void ParseSoundFont(std::vector<uint8_t> codeData, std::vector<uint8_t> audioTable,
std::vector<AudioTableEntry> audioSampleBank, AudioTableEntry& entry);
std::vector<AudioTableEntry> ParseAudioTable(const std::vector<uint8_t>& codeData, int baseOffset);
void ParseSoundFont(const std::vector<uint8_t>& codeData, const std::vector<uint8_t>& audioTable,
const std::vector<AudioTableEntry>& audioSampleBank, AudioTableEntry& entry);

void ParseRawData() override;

Expand Down

0 comments on commit 215d3c2

Please sign in to comment.