From 803bc041c7e228e2b7c88afd1121550e51db3f82 Mon Sep 17 00:00:00 2001 From: Max Roncace Date: Fri, 20 Feb 2026 05:53:27 -0500 Subject: Implement Z2AudioCS (#3103) --- src/Z2AudioCS/Z2AudioCS.cpp | 162 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 src/Z2AudioCS/Z2AudioCS.cpp (limited to 'src/Z2AudioCS/Z2AudioCS.cpp') diff --git a/src/Z2AudioCS/Z2AudioCS.cpp b/src/Z2AudioCS/Z2AudioCS.cpp new file mode 100644 index 0000000000..e619876537 --- /dev/null +++ b/src/Z2AudioCS/Z2AudioCS.cpp @@ -0,0 +1,162 @@ +#include "Z2AudioCS/Z2AudioCS.h" + +#include "Z2AudioCS/SpkSystem.h" +#include "JSystem/JKernel/JKRHeap.h" +#include +#include + +#define HANDLES_MAX 0x30 + +static SpkSoundHandle* sSpkHandles; + +static u8 l_spkVolume; + +void Z2AudioCS::newSpkSoundMemPool() { + SpkSystem::newSoundMemPool(HANDLES_MAX); +} + +int Z2AudioCS::init(JKRHeap* heap, JKRArchive* res, s32 param_2, s32 param_3) { + JUT_ASSERT(59, heap); + JUT_ASSERT(60, res); + SpkSystem* spkSys = new(heap, 0) SpkSystem(heap); + JUT_ASSERT(67, spkSys); + + sSpkHandles = new (heap, 0) SpkSoundHandle[HANDLES_MAX]; + JUT_ASSERT(71, sSpkHandles); + + spkSys->setResource(res, 2, 3); + spkSys->setMasterVolume(1.0f); + spkSys->setConfigVolume(15); +} + +void Z2AudioCS::update() { + if (JASGlobalInstance::getInstance() != NULL) { + JASGlobalInstance::getInstance()->framework(); + } +} + +void Z2AudioCS::connect(s32 chan) { + SpkSystem::connect(chan); + l_spkVolume = WPADGetSpeakerVolume(); +} + +void Z2AudioCS::disconnect(s32 chan) { + SpkSystem::disconnect(chan); +} + +void Z2AudioCS::extensionProcess(s32 chan, s32 param_1) { + SpkSystem::extensionProcess(chan, param_1); +} + +static SpkSoundHandle* getFreeSpkHandle(void) { + JUT_ASSERT(125, JASGlobalInstance::getInstance()); + JUT_ASSERT(126, sSpkHandles); + + SpkSoundHandle* highestPriorityHandle = NULL; + s32 highestPriority = 255; + for (s32 i = 0; i < HANDLES_MAX; i++) { + if (!sSpkHandles[i].isSoundAttached()) { + return &sSpkHandles[i]; + } + + if (sSpkHandles[i]->getPriority() < highestPriority) { + highestPriorityHandle = &sSpkHandles[i]; + highestPriority = sSpkHandles[i]->getPriority(); + } + } + + return highestPriorityHandle; +} + +SpkSoundHandle* Z2AudioCS::getHandleSoundID(s32 soundNum) { + JUT_ASSERT(145, JASGlobalInstance::getInstance()); + JUT_ASSERT(146, sSpkHandles); + + for (s32 i = 0; i < HANDLES_MAX; i++) { + if (!sSpkHandles[i].isSoundAttached()) { + continue; + } + + if (sSpkHandles[i]->getSoundNum() == soundNum) { + return &sSpkHandles[i]; + } + } + + return NULL; +} + +SpkSoundHandle* Z2AudioCS::start(s32 id, s32 chan) { + if (JASGlobalInstance::getInstance() == NULL) { + return NULL; + } + if (sSpkHandles == NULL) { + return NULL; + } + if (l_spkVolume == 0) { + return NULL; + } + + OS_REPORT("[Z2AudioCS::start] id:%d ch:%d\n", id, chan); + SpkSoundHandle* handle = getFreeSpkHandle(); + JUT_ASSERT(172, handle); + + JASGlobalInstance::getInstance()->startSound(chan, id, handle); + return handle; +} + +SpkSoundHandle* Z2AudioCS::startLevel(s32 id, s32 chan) { + if (JASGlobalInstance::getInstance() == NULL) { + return NULL; + } + if (sSpkHandles == NULL) { + return NULL; + } + if (l_spkVolume == 0) { + return NULL; + } + + OS_REPORT("[Z2AudioCS::startLevel] id:%d ch:%d\n", id, chan); + + SpkSoundHandle* handle = getHandleSoundID(id); + if (handle == NULL) { + handle = getFreeSpkHandle(); + } + JUT_ASSERT(191, handle); + + JASGlobalInstance::getInstance()->startLevelSound(chan, id, handle); + return handle; +} + +s32 Z2AudioCS::getName(s32 num) { + if (JASGlobalInstance::getInstance() == NULL) { + return 0; + } + if (JASGlobalInstance::getInstance()->getData() == NULL) { + return 0; + } + + return JASGlobalInstance::getInstance()->getData()->getTableMgr().getName(num); +} + +s32 Z2AudioCS::getNumOfSound(void) { + if (JASGlobalInstance::getInstance() == NULL) { + return 0; + } + if (JASGlobalInstance::getInstance()->getData() == NULL) { + return 0; + } + + return JASGlobalInstance::getInstance()->getData()->getTableMgr().getNumOfSound(); +} + +void Z2AudioCS::stopAll(s32 chan, s32 msec) { + if (JASGlobalInstance::getInstance() == NULL) { + return; + } + + JASGlobalInstance::getInstance()->stopAll(chan, msec); +} + +void Z2AudioCS::stop(s32 chan) { + stopAll(chan, 0); +} -- cgit v1.2.3