From c2ec01704b3bdbeeba5f0afa439ac60ee1124552 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Fri, 22 Nov 2024 00:12:40 -0600 Subject: [PATCH] Added drum and instrument custom tuning until we have xml factories --- src/port/Engine.cpp | 2 ++ .../resource/importers/audio/DrumFactory.cpp | 5 ++-- .../importers/audio/InstrumentFactory.cpp | 20 +++++++++++----- .../importers/audio/SampleFactory.cpp | 24 +++++++++++++++++++ .../resource/importers/audio/SampleFactory.h | 5 ++++ src/port/resource/type/audio/Sample.h | 1 + 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/port/Engine.cpp b/src/port/Engine.cpp index 7f737ce3..e514a5f3 100644 --- a/src/port/Engine.cpp +++ b/src/port/Engine.cpp @@ -159,6 +159,8 @@ GameEngine::GameEngine() { loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "Sample", static_cast(SF64::ResourceType::Sample), 1); + loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, + "Sample", static_cast(SF64::ResourceType::Sample), 2); loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "SoundFont", static_cast(SF64::ResourceType::SoundFont), 0); diff --git a/src/port/resource/importers/audio/DrumFactory.cpp b/src/port/resource/importers/audio/DrumFactory.cpp index ca53d468..c6047983 100644 --- a/src/port/resource/importers/audio/DrumFactory.cpp +++ b/src/port/resource/importers/audio/DrumFactory.cpp @@ -14,8 +14,9 @@ std::shared_ptr ResourceFactoryBinaryDrumV0::ReadResource(std:: drum->mDrum.adsrDecayIndex = reader->ReadUByte(); drum->mDrum.pan = reader->ReadUByte(); drum->mDrum.isRelocated = reader->ReadUByte(); - drum->mDrum.tunedSample.sample = LoadChild(reader->ReadUInt64()); - drum->mDrum.tunedSample.tuning = reader->ReadFloat(); + auto sample = LoadChild(reader->ReadUInt64()); + drum->mDrum.tunedSample.sample = sample; + drum->mDrum.tunedSample.tuning = sample->tuning != 0.0f ? sample->tuning : reader->ReadFloat(); drum->mDrum.envelope = LoadChild(reader->ReadUInt64()); drum->mDrum.isRelocated = 1; diff --git a/src/port/resource/importers/audio/InstrumentFactory.cpp b/src/port/resource/importers/audio/InstrumentFactory.cpp index 357ef0c6..0a79e305 100644 --- a/src/port/resource/importers/audio/InstrumentFactory.cpp +++ b/src/port/resource/importers/audio/InstrumentFactory.cpp @@ -16,12 +16,20 @@ std::shared_ptr ResourceFactoryBinaryInstrumentV0::ReadResource instrument->mInstrument.normalRangeHi = reader->ReadUByte(); instrument->mInstrument.adsrDecayIndex = reader->ReadUByte(); instrument->mInstrument.envelope = LoadChild(reader->ReadUInt64()); - instrument->mInstrument.lowPitchTunedSample.sample = LoadChild(reader->ReadUInt64()); - instrument->mInstrument.lowPitchTunedSample.tuning = reader->ReadFloat(); - instrument->mInstrument.normalPitchTunedSample.sample = LoadChild(reader->ReadUInt64()); - instrument->mInstrument.normalPitchTunedSample.tuning = reader->ReadFloat(); - instrument->mInstrument.highPitchTunedSample.sample = LoadChild(reader->ReadUInt64()); - instrument->mInstrument.highPitchTunedSample.tuning = reader->ReadFloat(); + auto lowSample = LoadChild(reader->ReadUInt64()); + instrument->mInstrument.lowPitchTunedSample.sample = lowSample; + instrument->mInstrument.lowPitchTunedSample.tuning = + lowSample != nullptr && lowSample->tuning != 0.0f ? lowSample->tuning : reader->ReadFloat(); + + auto normalSample = LoadChild(reader->ReadUInt64()); + instrument->mInstrument.normalPitchTunedSample.sample = normalSample; + instrument->mInstrument.normalPitchTunedSample.tuning = + normalSample != nullptr && normalSample->tuning != 0.0f ? normalSample->tuning : reader->ReadFloat(); + + auto highSample = LoadChild(reader->ReadUInt64()); + instrument->mInstrument.highPitchTunedSample.sample = highSample; + instrument->mInstrument.highPitchTunedSample.tuning = + highSample != nullptr && highSample->tuning != 0.0f ? highSample->tuning : reader->ReadFloat(); instrument->mInstrument.isRelocated = 1; return instrument; diff --git a/src/port/resource/importers/audio/SampleFactory.cpp b/src/port/resource/importers/audio/SampleFactory.cpp index ff89b227..25197aae 100644 --- a/src/port/resource/importers/audio/SampleFactory.cpp +++ b/src/port/resource/importers/audio/SampleFactory.cpp @@ -25,4 +25,28 @@ std::shared_ptr ResourceFactoryBinarySampleV1::ReadResource(std return sample; } + +std::shared_ptr ResourceFactoryBinarySampleV2::ReadResource(std::shared_ptr file) { + if (!FileHasValidFormatAndReader(file)) { + return nullptr; + } + + auto sample = std::make_shared(file->InitData); + auto reader = std::get>(file->Reader); + + sample->mSample.codec = reader->ReadUInt32(); + sample->mSample.medium = reader->ReadUInt32(); + sample->mSample.unk = reader->ReadUInt32(); + sample->mSample.size = reader->ReadUInt32(); + sample->mSample.tuning = reader->ReadFloat(); + sample->mSample.loop = LoadChild(reader->ReadUInt64()); + sample->mSample.book = LoadChild(reader->ReadUInt64()); + sample->mSample.sampleAddr = new uint8_t[sample->mSample.size]; + reader->Read((char*) sample->mSample.sampleAddr, sample->mSample.size); + + sample->mSample.medium = 0; + sample->mSample.isRelocated = 1; + + return sample; +} } // namespace LUS diff --git a/src/port/resource/importers/audio/SampleFactory.h b/src/port/resource/importers/audio/SampleFactory.h index 7a92fd1f..4b2017eb 100644 --- a/src/port/resource/importers/audio/SampleFactory.h +++ b/src/port/resource/importers/audio/SampleFactory.h @@ -8,4 +8,9 @@ class ResourceFactoryBinarySampleV1 : public Ship::ResourceFactoryBinary { public: std::shared_ptr ReadResource(std::shared_ptr file) override; }; + +class ResourceFactoryBinarySampleV2 : public Ship::ResourceFactoryBinary { + public: + std::shared_ptr ReadResource(std::shared_ptr file) override; +}; }; // namespace LUS diff --git a/src/port/resource/type/audio/Sample.h b/src/port/resource/type/audio/Sample.h index 80c50cd7..c4585459 100644 --- a/src/port/resource/type/audio/Sample.h +++ b/src/port/resource/type/audio/Sample.h @@ -16,6 +16,7 @@ struct SampleData { uint8_t* sampleAddr; AdpcmLoopData* loop; AdpcmBookData* book; + float tuning = 0.0f; }; class Sample : public Ship::Resource {