summaryrefslogtreecommitdiff
path: root/src/Z2AudioCS/Z2AudioCS.cpp
diff options
context:
space:
mode:
authorMax Roncace <me@caseif.net>2026-02-20 05:53:27 -0500
committerGitHub <noreply@github.com>2026-02-20 02:53:27 -0800
commit803bc041c7e228e2b7c88afd1121550e51db3f82 (patch)
tree5462e8061bc8a8084db8ea2fc677a0fef5bb16d1 /src/Z2AudioCS/Z2AudioCS.cpp
parent0558bde1e61374f5a1b54453f07fe6cc15525bc1 (diff)
Implement Z2AudioCS (#3103)
Diffstat (limited to 'src/Z2AudioCS/Z2AudioCS.cpp')
-rw-r--r--src/Z2AudioCS/Z2AudioCS.cpp162
1 files changed, 162 insertions, 0 deletions
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 <revolution/types.h>
+#include <revolution/wpad.h>
+
+#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<SpkSystem>::getInstance() != NULL) {
+ JASGlobalInstance<SpkSystem>::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<SpkSystem>::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<SpkSystem>::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<SpkSystem>::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<SpkSystem>::getInstance()->startSound(chan, id, handle);
+ return handle;
+}
+
+SpkSoundHandle* Z2AudioCS::startLevel(s32 id, s32 chan) {
+ if (JASGlobalInstance<SpkSystem>::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<SpkSystem>::getInstance()->startLevelSound(chan, id, handle);
+ return handle;
+}
+
+s32 Z2AudioCS::getName(s32 num) {
+ if (JASGlobalInstance<SpkSystem>::getInstance() == NULL) {
+ return 0;
+ }
+ if (JASGlobalInstance<SpkSystem>::getInstance()->getData() == NULL) {
+ return 0;
+ }
+
+ return JASGlobalInstance<SpkSystem>::getInstance()->getData()->getTableMgr().getName(num);
+}
+
+s32 Z2AudioCS::getNumOfSound(void) {
+ if (JASGlobalInstance<SpkSystem>::getInstance() == NULL) {
+ return 0;
+ }
+ if (JASGlobalInstance<SpkSystem>::getInstance()->getData() == NULL) {
+ return 0;
+ }
+
+ return JASGlobalInstance<SpkSystem>::getInstance()->getData()->getTableMgr().getNumOfSound();
+}
+
+void Z2AudioCS::stopAll(s32 chan, s32 msec) {
+ if (JASGlobalInstance<SpkSystem>::getInstance() == NULL) {
+ return;
+ }
+
+ JASGlobalInstance<SpkSystem>::getInstance()->stopAll(chan, msec);
+}
+
+void Z2AudioCS::stop(s32 chan) {
+ stopAll(chan, 0);
+}