From 396f732bc58080430562ea60fd345bf2e51cfc0b Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Sun, 17 Sep 2023 23:55:59 -0600 Subject: Refactored to use a better file format --- src/factories/BankFactory.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/factories/BankFactory.cpp (limited to 'src/factories/BankFactory.cpp') diff --git a/src/factories/BankFactory.cpp b/src/factories/BankFactory.cpp new file mode 100644 index 0000000..252fd48 --- /dev/null +++ b/src/factories/BankFactory.cpp @@ -0,0 +1,94 @@ +#include "BankFactory.h" + +#include +#include "audio/AudioManager.h" +#include "audio/samples_table.h" +#include "binarytools/BinaryReader.h" + +bool BankFactory::process(LUS::BinaryWriter* writer, YAML::Node& data, std::vector& buffer) { + auto banks = AudioManager::Instance->get_banks(); + auto bankId = data["id"].as(); + auto bank = banks[bankId]; + + WRITE_HEADER(LUS::ResourceType::Bank, 0); + + WRITE_U32(bank.insts.size()); + for(auto &instrument : bank.insts){ + WRITE_U8(instrument.valid); + if(!instrument.valid){ + continue; + } + WRITE_U8(instrument.releaseRate); + WRITE_U8(instrument.normalRangeLo); + WRITE_U8(instrument.normalRangeHi); + + auto envelope = bank.envelopes[instrument.envelope]; + if(!envelope.entries.empty()){ + WRITE_U32(envelope.entries.size()); + for(auto &entry : envelope.entries){ + writer->Write(entry.delay); + writer->Write(entry.arg); + } + } else { + WRITE_U32(0); + } + + uint32_t soundFlags = 0; + bool hasLo = instrument.soundLo.has_value(); + bool hasMed = instrument.soundMed.has_value(); + bool hasHi = instrument.soundHi.has_value(); + if(hasLo){ + soundFlags |= (1 << 0); + } + if(hasMed){ + soundFlags |= (1 << 1); + } + if(hasHi){ + soundFlags |= (1 << 2); + } + + WRITE_U32(soundFlags); + if(hasLo){ + auto value = instrument.soundLo.value(); + uint32_t idx = AudioManager::Instance->get_index(bank.samples[value.offset]); + writer->Write(std::string(gSampleTable[idx])); + writer->Write(value.tuning); + } + if(hasMed){ + auto value = instrument.soundMed.value(); + uint32_t idx = AudioManager::Instance->get_index(bank.samples[value.offset]); + writer->Write(std::string(gSampleTable[idx])); + writer->Write(value.tuning); + } + if(hasHi){ + auto value = instrument.soundHi.value(); + uint32_t idx = AudioManager::Instance->get_index(bank.samples[value.offset]); + writer->Write(std::string(gSampleTable[idx])); + writer->Write(value.tuning); + } + + } + + WRITE_U32(bank.drums.size()); + for(auto &drum : bank.drums){ + WRITE_U8(drum.releaseRate); + WRITE_U8(drum.pan); + auto envelope = bank.envelopes[drum.envelope]; + if(!envelope.entries.empty()){ + WRITE_U32(envelope.entries.size()); + for(auto &entry : envelope.entries){ + writer->Write(entry.delay); + writer->Write(entry.arg); + } + } else { + WRITE_U32(0); + } + + uint32_t idx = AudioManager::Instance->get_index(bank.samples[drum.sound.offset]); + std::string out = std::string(gSampleTable[idx]); + writer->Write(out); + writer->Write(drum.sound.tuning); + } + + return true; +} \ No newline at end of file -- cgit v1.2.3