Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sample exporting #20

Merged
merged 3 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 55 additions & 28 deletions ZAPD/ZAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,23 @@ void ZAudio::ParseXML(tinyxml2::XMLElement* reader)
{
if (std::string(sampChild->Value()) == "Sample")
{
auto atStr = sampChild->FirstChildElement()->Attribute("At");
auto loopStr = sampChild->FirstChildElement()->Attribute("LoopOffset");
uint32_t loopOffset = 0xFFFFFFFF;
auto atStr = sampChild->Attribute("Offset");
auto loopStr = sampChild->Attribute("LoopOffset");
uint32_t loopOffset = 0;
uint32_t atOffset = StringHelper::StrToL(atStr, 16);

if (loopStr != NULL)
if (atOffset == 0x000593f0)
{
loopOffset = StringHelper::StrToL(loopStr, 16);
specialLoopSamples[loopOffset] = atOffset;
int bp = 0;
}

sampleOffsets[bankId][loopOffset][atOffset] = sampChild->Attribute("Name");
//if (loopStr != NULL)
//{
// loopOffset = StringHelper::StrToL(loopStr, 16);
// loopOverrideSamples[((uint64_t)atOffset << 32) | loopOffset] = loopOffset;
//}


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

sampChild = sampChild->NextSiblingElement();
Expand All @@ -80,7 +85,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 +107,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 +128,34 @@ 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;

if (samples.find(sampleOffset) == samples.end())
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())
{
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);
free(sampleData);

uint32_t origField = (BitConverter::ToUInt32BE(audioBank, sampleOffset + 0));
sample->codec = (origField >> 28) & 0x0F;
Expand Down Expand Up @@ -176,22 +187,38 @@ SampleEntry* ZAudio::ParseSampleEntry(std::vector<uint8_t> 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;
//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);

samples[(uint64_t)sampleDataOffset << 32 | 0] = sample;

return sample;
}
else
{
return samples[sampleOffset];
return samples[(uint64_t)sampleDataOffset << 32 | 0];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then everywhere we index it, we are just shifting the value over without adding anything else, so I'm not sure the intent here.

How does this all impact 32 bit platforms (WiiU)?

}
}

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;

Expand Down Expand Up @@ -220,8 +247,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 Down
22 changes: 11 additions & 11 deletions ZAPD/ZAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,37 @@ class ZAudio : public ZResource
std::vector<AudioTableEntry> sequenceTable;
std::vector<AudioTableEntry> sampleBankTable;
std::vector<std::vector<char>> sequences;
std::map<uint32_t, SampleEntry*> samples;
std::map<uint64_t, SampleEntry*> samples;
louist103 marked this conversation as resolved.
Show resolved Hide resolved
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 = LoopDataOffset, Third Key = Sample Data Offset
std::map<uint32_t, std::map<uint32_t, std::map<uint32_t, std::string>>> sampleOffsets;
// First Key = Bank ID, Sec Key = Sample Data Offset, Third Key = LoopDataOffset,
std::map<uint32_t, std::map<uint32_t, std::string>> sampleOffsets;
louist103 marked this conversation as resolved.
Show resolved Hide resolved

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

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
Loading