diff options
| author | KiritoDv <kiritodev01@gmail.com> | 2024-12-25 00:40:19 -0600 |
|---|---|---|
| committer | KiritoDv <kiritodev01@gmail.com> | 2024-12-25 00:40:19 -0600 |
| commit | 0efd51b717fe7ffef8a8c93a4c25efcb8ef3f919 (patch) | |
| tree | 17bc5ebae2be672de9f904c8ea554a7b29507bbc /src | |
| parent | ac3f20a78ce518c4076f00d13bf586d9fc27c5d0 (diff) | |
Modified XML format
Diffstat (limited to 'src')
| -rw-r--r-- | src/factories/naudio/v1/AudioTableFactory.cpp | 9 | ||||
| -rw-r--r-- | src/factories/naudio/v1/InstrumentFactory.cpp | 1 | ||||
| -rw-r--r-- | src/factories/naudio/v1/InstrumentFactory.h | 1 | ||||
| -rw-r--r-- | src/factories/naudio/v1/SampleFactory.cpp | 15 | ||||
| -rw-r--r-- | src/factories/naudio/v1/SoundFontFactory.cpp | 135 | ||||
| -rw-r--r-- | src/factories/naudio/v1/SoundFontFactory.h | 5 |
6 files changed, 157 insertions, 9 deletions
diff --git a/src/factories/naudio/v1/AudioTableFactory.cpp b/src/factories/naudio/v1/AudioTableFactory.cpp index 96d6954..00f0f5c 100644 --- a/src/factories/naudio/v1/AudioTableFactory.cpp +++ b/src/factories/naudio/v1/AudioTableFactory.cpp @@ -124,7 +124,6 @@ std::optional<std::shared_ptr<IParsedData>> AudioTableFactory::parse(std::vector SPDLOG_INFO("addr: {}", baddr); std::vector<AudioTableEntry> entries; - auto parent = AudioContext::tableOffsets[AudioTableType::SEQ_TABLE]; for(size_t i = 0; i < count; i++){ auto addr = reader.ReadUInt32(); @@ -141,12 +140,20 @@ std::optional<std::shared_ptr<IParsedData>> AudioTableFactory::parse(std::vector YAML::Node font; font["type"] = "NAUDIO:V1:SOUND_FONT"; font["offset"] = addr; + font["id"] = (uint32_t) i; + font["medium"] = (int32_t) medium; + font["policy"] = (int32_t) policy; + font["sd1"] = (int32_t) sd1; + font["sd2"] = (int32_t) sd2; + font["sd3"] = (int32_t) sd3; + Companion::Instance->AddAsset(font); std::string path = font["vpath"].as<std::string>(); crc = CRC64(path.c_str()); break; } case AudioTableType::SEQ_TABLE: { + auto parent = AudioContext::tableOffsets[AudioTableType::SEQ_TABLE]; if(size != 0){ YAML::Node seq; seq["type"] = "BLOB"; diff --git a/src/factories/naudio/v1/InstrumentFactory.cpp b/src/factories/naudio/v1/InstrumentFactory.cpp index 5315a78..10ba852 100644 --- a/src/factories/naudio/v1/InstrumentFactory.cpp +++ b/src/factories/naudio/v1/InstrumentFactory.cpp @@ -2,7 +2,6 @@ #include "utils/Decompressor.h" #include "Companion.h" #include "EnvelopeFactory.h" -#include "SampleFactory.h" #include <tinyxml2.h> ExportResult InstrumentHeaderExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) { diff --git a/src/factories/naudio/v1/InstrumentFactory.h b/src/factories/naudio/v1/InstrumentFactory.h index ab0f623..245b6d9 100644 --- a/src/factories/naudio/v1/InstrumentFactory.h +++ b/src/factories/naudio/v1/InstrumentFactory.h @@ -39,7 +39,6 @@ public: REGISTER(Header, InstrumentHeaderExporter) REGISTER(Binary, InstrumentBinaryExporter) REGISTER(Code, InstrumentCodeExporter) - REGISTER(XML, InstrumentXMLExporter) }; } diff --git a/src/factories/naudio/v1/SampleFactory.cpp b/src/factories/naudio/v1/SampleFactory.cpp index ec8dbe2..18d539a 100644 --- a/src/factories/naudio/v1/SampleFactory.cpp +++ b/src/factories/naudio/v1/SampleFactory.cpp @@ -62,7 +62,6 @@ ExportResult NSampleModdingExporter::Export(std::ostream &write, std::shared_ptr } ExportResult NSampleXMLExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) { - auto aiff = LUS::BinaryWriter(); auto entry = std::static_pointer_cast<NSampleData>(raw); auto loop = std::static_pointer_cast<ADPCMLoopData>(Companion::Instance->GetParseDataByAddr(entry->loop)->data.value()); auto book = std::static_pointer_cast<ADPCMBookData>(Companion::Instance->GetParseDataByAddr(entry->book)->data.value()); @@ -73,19 +72,23 @@ ExportResult NSampleXMLExporter::Export(std::ostream &write, std::shared_ptr<IPa tinyxml2::XMLDocument sample; tinyxml2::XMLElement* root = sample.NewElement("Sample"); + root->SetAttribute("Version", 0); root->SetAttribute("Codec", AudioContext::GetCodecStr(entry->codec)); root->SetAttribute("Medium", AudioContext::GetMediumStr(entry->medium)); - root->SetAttribute("Unk", entry->unk); + root->SetAttribute("bit26", entry->unk); root->SetAttribute("Tuning", entry->tuning); + root->SetAttribute("Size", entry->size); + root->SetAttribute("Relocated", 0); + root->SetAttribute("Path", path.c_str()); tinyxml2::XMLElement* adpcmLoop = sample.NewElement("ADPCMLoop"); adpcmLoop->SetAttribute("Start", loop->start); adpcmLoop->SetAttribute("End", loop->end); adpcmLoop->SetAttribute("Count", loop->count); if (loop->count != 0) { - for (size_t i = 0; i < 16; i++) { + for (auto& state : loop->predictorState) { tinyxml2::XMLElement* loopEntry = adpcmLoop->InsertNewChildElement("Predictor"); - loopEntry->SetAttribute("State", loop->predictorState[i]); + loopEntry->SetAttribute("State", state); adpcmLoop->InsertEndChild(loopEntry); } } @@ -95,9 +98,9 @@ ExportResult NSampleXMLExporter::Export(std::ostream &write, std::shared_ptr<IPa adpcmBook->SetAttribute("Order", book->order); adpcmBook->SetAttribute("Npredictors", book->numPredictors); - for (size_t i = 0; i < book->book.size(); i++) { + for (auto& page : book->book) { tinyxml2::XMLElement* bookEntry = adpcmBook->InsertNewChildElement("Book"); - bookEntry->SetAttribute("Page", book->book[i]); + bookEntry->SetAttribute("Page", page); adpcmBook->InsertEndChild(bookEntry); } root->InsertEndChild(adpcmBook); diff --git a/src/factories/naudio/v1/SoundFontFactory.cpp b/src/factories/naudio/v1/SoundFontFactory.cpp index ca84065..81419fa 100644 --- a/src/factories/naudio/v1/SoundFontFactory.cpp +++ b/src/factories/naudio/v1/SoundFontFactory.cpp @@ -3,6 +3,10 @@ #include "Companion.h" #include "spdlog/spdlog.h" #include "utils/Decompressor.h" +#include <tinyxml2.h> +#include "DrumFactory.h" +#include "EnvelopeFactory.h" +#include "InstrumentFactory.h" ExportResult SoundFontHeaderExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) { const auto symbol = GetSafeNode(node, "symbol", entryName); @@ -45,6 +49,137 @@ ExportResult SoundFontBinaryExporter::Export(std::ostream &write, std::shared_pt return std::nullopt; } +void WriteInstrument(tinyxml2::XMLElement* parent, uint32_t offset){ + auto instrument = std::static_pointer_cast<InstrumentData>(Companion::Instance->GetParseDataByAddr(offset)->data.value()); + auto envelopeData = std::static_pointer_cast<EnvelopeData>(Companion::Instance->GetParseDataByAddr(instrument->envelope)->data.value()); + + tinyxml2::XMLElement* root = parent->InsertNewChildElement("Instrument"); + root->SetAttribute("NormalRangeLo", instrument->normalRangeLo); + root->SetAttribute("NormalRangeHi", instrument->normalRangeLo); + root->SetAttribute("ReleaseRate", instrument->adsrDecayIndex); + + tinyxml2::XMLElement* envelopes = root->InsertNewChildElement("Envelopes"); + for(size_t i = 0; i < envelopeData->points.size(); i++){ + auto point = envelopeData->points[i]; + tinyxml2::XMLElement* pointEntry = envelopes->InsertNewChildElement("Envelope"); + pointEntry->SetAttribute("Delay", point.delay); + pointEntry->SetAttribute("Arg", point.arg); + envelopes->InsertEndChild(pointEntry); + } + + auto lowSample = instrument->lowPitchTunedSample; + auto normSample = instrument->normalPitchTunedSample; + auto highSample = instrument->highPitchTunedSample; + + if(lowSample.sample != 0 && lowSample.tuning != 0.0f){ + tinyxml2::XMLElement* low = root->InsertNewChildElement("LowPitchSample"); + low->SetAttribute("Tuning", lowSample.tuning); + low->SetAttribute("SampleRef", std::get<std::string>(Companion::Instance->GetNodeByAddr(lowSample.sample).value()).c_str()); + + root->InsertEndChild(low); + } + + if(normSample.sample != 0 && normSample.tuning != 0.0f) { + tinyxml2::XMLElement* normal = root->InsertNewChildElement("NormalPitchSample"); + normal->SetAttribute("Tuning", normSample.tuning); + normal->SetAttribute("SampleRef", std::get<std::string>(Companion::Instance->GetNodeByAddr(normSample.sample).value()).c_str()); + + root->InsertEndChild(normal); + } + + if(highSample.sample != 0 && highSample.tuning != 0.0f) { + tinyxml2::XMLElement* high = root->InsertNewChildElement("HighPitchSample"); + high->SetAttribute("Tuning", highSample.tuning); + high->SetAttribute("SampleRef", std::get<std::string>(Companion::Instance->GetNodeByAddr(highSample.sample).value()).c_str()); + + root->InsertEndChild(high); + } + + parent->InsertEndChild(root); +} + +void WriteDrum(tinyxml2::XMLElement* parent, uint32_t offset){ + auto drum = std::static_pointer_cast<DrumData>(Companion::Instance->GetParseDataByAddr(offset)->data.value()); + auto envelopeData = std::static_pointer_cast<EnvelopeData>(Companion::Instance->GetParseDataByAddr(drum->envelope)->data.value()); + auto sample = drum->tunedSample; + + tinyxml2::XMLElement* root = parent->InsertNewChildElement("Drum"); + root->SetAttribute("ReleaseRate", drum->adsrDecayIndex); + root->SetAttribute("Pan", drum->pan); + root->SetAttribute("Loaded", 0); + root->SetAttribute("SampleRef", std::get<std::string>(Companion::Instance->GetNodeByAddr(sample.sample).value()).c_str()); + root->SetAttribute("Tuning", sample.tuning); + + tinyxml2::XMLElement* envelopes = root->InsertNewChildElement("Envelopes"); + envelopes->SetAttribute("Count", (uint32_t) envelopeData->points.size()); + for(size_t i = 0; i < envelopeData->points.size(); i++){ + auto point = envelopeData->points[i]; + tinyxml2::XMLElement* pointEntry = envelopes->InsertNewChildElement("Envelope"); + pointEntry->SetAttribute("Delay", point.delay); + pointEntry->SetAttribute("Arg", point.arg); + envelopes->InsertEndChild(pointEntry); + } + + parent->InsertEndChild(root); +} + +ExportResult SoundFontXMLExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) { + auto font = std::static_pointer_cast<SoundFontData>(raw); + auto id = GetSafeNode<uint32_t>(node, "id"); + auto medium = (int8_t) GetSafeNode<int32_t>(node, "medium"); + auto policy = (int8_t) GetSafeNode<int32_t>(node, "policy"); + auto sd1 = (int16_t) GetSafeNode<int32_t>(node, "sd1"); + auto sd2 = (int16_t) GetSafeNode<int32_t>(node, "sd2"); + auto sd3 = (int16_t) GetSafeNode<int32_t>(node, "sd3"); + + *replacement += ".meta"; + + tinyxml2::XMLDocument doc; + tinyxml2::XMLElement* root = doc.NewElement("SoundFont"); + root->SetAttribute("Version", 0); + root->SetAttribute("Num", id); + root->SetAttribute("Medium", AudioContext::GetMediumStr(medium)); + root->SetAttribute("CachePolicy", AudioContext::GetCachePolicyStr(policy)); + root->SetAttribute("Data1", sd1); + root->SetAttribute("Data2", sd2); + root->SetAttribute("Data3", sd3); + + tinyxml2::XMLElement* drums = doc.NewElement("Drums"); + drums->SetAttribute("Count", font->numDrums); + + for(auto& drum : font->drums){ + if(drum == 0){ + continue; + } + WriteDrum(drums, drum); + } + root->InsertEndChild(drums); + + tinyxml2::XMLElement* insts = doc.NewElement("Instruments"); + insts->SetAttribute("Count", font->numInstruments); + + for(auto& inst : font->instruments){ + if(inst == 0){ + continue; + } + WriteInstrument(insts, inst); + } + root->InsertEndChild(insts); + + // This is ignored since it's an exclusive feature of OoT and MM + tinyxml2::XMLElement* sfx = doc.NewElement("SfxTable"); + sfx->SetAttribute("Count", 0); + root->InsertEndChild(sfx); + + doc.InsertEndChild(root); + + tinyxml2::XMLPrinter printer; + doc.Accept(&printer); + write.write(printer.CStr(), printer.CStrSize() - 1); + + return std::nullopt; +} + std::optional<std::shared_ptr<IParsedData>> SoundFontFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) { auto offset = GetSafeNode<uint32_t>(node, "offset"); auto entry = AudioContext::tables[AudioTableType::FONT_TABLE].at(offset); diff --git a/src/factories/naudio/v1/SoundFontFactory.h b/src/factories/naudio/v1/SoundFontFactory.h index 256f2ce..72e1190 100644 --- a/src/factories/naudio/v1/SoundFontFactory.h +++ b/src/factories/naudio/v1/SoundFontFactory.h @@ -24,6 +24,10 @@ class SoundFontCodeExporter : public BaseExporter { ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override; }; +class SoundFontXMLExporter : public BaseExporter { + ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override; +}; + class SoundFontFactory : public BaseFactory { public: std::optional<std::shared_ptr<IParsedData>> parse(std::vector<uint8_t>& buffer, YAML::Node& data) override; @@ -32,6 +36,7 @@ public: REGISTER(Header, SoundFontHeaderExporter) REGISTER(Binary, SoundFontBinaryExporter) REGISTER(Code, SoundFontCodeExporter) + REGISTER(XML, SoundFontXMLExporter) }; } |
