From ac3f20a78ce518c4076f00d13bf586d9fc27c5d0 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Wed, 27 Nov 2024 21:25:49 -0600 Subject: [PATCH] Test audio xmls --- src/Companion.cpp | 5 +- src/factories/naudio/v1/AudioContext.cpp | 3 +- src/factories/naudio/v1/InstrumentFactory.cpp | 64 +++++++++++++++++++ src/factories/naudio/v1/InstrumentFactory.h | 5 ++ src/factories/naudio/v1/SampleFactory.cpp | 4 +- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/Companion.cpp b/src/Companion.cpp index 6b813b5..07f0845 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -1230,9 +1230,7 @@ std::optional> Companion::RegisterAsset(cons auto entry = std::make_tuple(output, node); this->gAddrMap[this->gCurrentFile][node["offset"].as()] = entry; - - - + return entry; } @@ -1418,7 +1416,6 @@ std::optional Companion::AddAsset(YAML::Node asset) { const auto symbol = GetSafeNode(asset, "symbol", ""); const auto decl = this->GetNodeByAddr(offset); - if(decl.has_value()) { return std::get<1>(decl.value()); } diff --git a/src/factories/naudio/v1/AudioContext.cpp b/src/factories/naudio/v1/AudioContext.cpp index ce434ac..b4f72e2 100644 --- a/src/factories/naudio/v1/AudioContext.cpp +++ b/src/factories/naudio/v1/AudioContext.cpp @@ -62,8 +62,9 @@ TunedSample AudioContext::LoadTunedSample(LUS::BinaryReader& reader, uint32_t pa node["parent"] = parent; node["offset"] = parent + sampleAddr; node["tuning"] = tuning; + SPDLOG_INFO("Tuning {}", tuning); node["sampleBankId"] = sampleBankId; - node["symbol"] = StringHelper::Sprintf("%d_Sample_P_%X_B_%d", parent + sampleAddr, parent, sampleBankId); + // node["symbol"] = StringHelper::Sprintf("%d_Sample_P_%X_B_%d", parent + sampleAddr, parent, sampleBankId); Companion::Instance->AddAsset(node); return { parent + sampleAddr, sampleBankId, tuning }; diff --git a/src/factories/naudio/v1/InstrumentFactory.cpp b/src/factories/naudio/v1/InstrumentFactory.cpp index acfada1..5315a78 100644 --- a/src/factories/naudio/v1/InstrumentFactory.cpp +++ b/src/factories/naudio/v1/InstrumentFactory.cpp @@ -1,6 +1,9 @@ #include "InstrumentFactory.h" #include "utils/Decompressor.h" #include "Companion.h" +#include "EnvelopeFactory.h" +#include "SampleFactory.h" +#include ExportResult InstrumentHeaderExporter::Export(std::ostream &write, std::shared_ptr raw, std::string& entryName, YAML::Node &node, std::string* replacement) { const auto symbol = GetSafeNode(node, "symbol", entryName); @@ -40,6 +43,67 @@ ExportResult InstrumentBinaryExporter::Export(std::ostream &write, std::shared_p return std::nullopt; } +ExportResult InstrumentXMLExporter::Export(std::ostream &write, std::shared_ptr raw, std::string& entryName, YAML::Node &node, std::string* replacement ) { + auto writer = LUS::BinaryWriter(); + auto instrument = std::static_pointer_cast(raw); + + auto envelopeData = std::static_pointer_cast(Companion::Instance->GetParseDataByAddr(instrument->envelope)->data.value()); + + auto path = fs::path(*replacement); + + *replacement += ".meta"; + + tinyxml2::XMLDocument inst; + tinyxml2::XMLElement* root = inst.NewElement("Instrument"); + root->SetAttribute("NormalRangeLo", instrument->normalRangeLo); + root->SetAttribute("NormalRangeHi", instrument->normalRangeLo); + root->SetAttribute("ReleaseRate", instrument->adsrDecayIndex); + + tinyxml2::XMLElement* envelopes = inst.NewElement("Envelopes"); + for(auto& point : envelopeData->points){ + tinyxml2::XMLElement* pointEntry = envelopes->InsertNewChildElement("Point"); + 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 = inst.NewElement("LowPitchSample"); + low->SetAttribute("Tuning", lowSample.tuning); + low->SetAttribute("Path", std::get(Companion::Instance->GetNodeByAddr(lowSample.sample).value()).c_str()); + + root->InsertEndChild(low); + } + + if(normSample.sample != 0 && normSample.tuning != 0.0f) { + tinyxml2::XMLElement* normal = inst.NewElement("NormalPitchSample"); + normal->SetAttribute("Tuning", normSample.tuning); + normal->SetAttribute("Path", std::get(Companion::Instance->GetNodeByAddr(normSample.sample).value()).c_str()); + + root->InsertEndChild(normal); + } + + if(highSample.sample != 0 && highSample.tuning != 0.0f) { + tinyxml2::XMLElement* high = inst.NewElement("HighPitchSample"); + high->SetAttribute("Tuning", highSample.tuning); + high->SetAttribute("Path", std::get(Companion::Instance->GetNodeByAddr(highSample.sample).value()).c_str()); + + root->InsertEndChild(high); + } + + inst.InsertEndChild(root); + + tinyxml2::XMLPrinter printer; + inst.Accept(&printer); + write.write(printer.CStr(), printer.CStrSize() - 1); + + return std::nullopt; +} + std::optional> InstrumentFactory::parse(std::vector& buffer, YAML::Node& node) { auto offset = GetSafeNode(node, "offset"); auto parent = GetSafeNode(node, "parent"); diff --git a/src/factories/naudio/v1/InstrumentFactory.h b/src/factories/naudio/v1/InstrumentFactory.h index 9af9295..ab0f623 100644 --- a/src/factories/naudio/v1/InstrumentFactory.h +++ b/src/factories/naudio/v1/InstrumentFactory.h @@ -27,6 +27,10 @@ class InstrumentCodeExporter : public BaseExporter { ExportResult Export(std::ostream& write, std::shared_ptr data, std::string& entryName, YAML::Node& node, std::string* replacement) override; }; +class InstrumentXMLExporter : public BaseExporter { + ExportResult Export(std::ostream& write, std::shared_ptr data, std::string& entryName, YAML::Node& node, std::string* replacement) override; +}; + class InstrumentFactory : public BaseFactory { public: std::optional> parse(std::vector& buffer, YAML::Node& data) override; @@ -35,6 +39,7 @@ 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 58e9afb..ec8dbe2 100644 --- a/src/factories/naudio/v1/SampleFactory.cpp +++ b/src/factories/naudio/v1/SampleFactory.cpp @@ -76,7 +76,7 @@ ExportResult NSampleXMLExporter::Export(std::ostream &write, std::shared_ptrSetAttribute("Codec", AudioContext::GetCodecStr(entry->codec)); root->SetAttribute("Medium", AudioContext::GetMediumStr(entry->medium)); root->SetAttribute("Unk", entry->unk); - // root->SetAttribute("Relocated", entry->isRelocated); + root->SetAttribute("Tuning", entry->tuning); tinyxml2::XMLElement* adpcmLoop = sample.NewElement("ADPCMLoop"); adpcmLoop->SetAttribute("Start", loop->start); @@ -119,7 +119,7 @@ ExportResult NSampleXMLExporter::Export(std::ostream &write, std::shared_ptr> NSampleFactory::parse(std::vector& buffer, YAML::Node& node) { auto offset = GetSafeNode(node, "offset"); auto parent = GetSafeNode(node, "parent"); - auto tuning = GetSafeNode(node, "tuning", 1.0f); + auto tuning = GetSafeNode(node, "tuning", 0.0f); auto sampleRate = GetSafeNode(node, "sampleRate", 0); auto sampleBankId = GetSafeNode(node, "sampleBankId");