summaryrefslogtreecommitdiff
path: root/src/Z2AudioLib/Z2SoundHandles.cpp
blob: 68804a6d017c0598f3be55b89af0ca9c0e436b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Generated By: dol2asm
// Translation Unit: Z2SoundHandles
//

#include "Z2AudioLib/Z2SoundHandles.h"
#include "JSystem/JAudio2/JAISoundInfo.h"

Z2SoundHandles::Z2SoundHandles() {
    mNumHandles = 0;
}

Z2SoundHandles::~Z2SoundHandles() {
    deleteHandlesPool();
}

void Z2SoundHandles::initHandlesPool(u8 pNumHandles) {
    mNumHandles = pNumHandles;
}

/* 802AB120-802AB200 2A5A60 00E0+00 1/1 1/1 0/0 .text deleteHandlesPool__14Z2SoundHandlesFv */
void Z2SoundHandles::deleteHandlesPool() {
    JSULink<Z2SoundHandlePool>* link;
    while (link = getFirst(), link != NULL) {
        Z2SoundHandlePool* handle = link->getObject();
        remove(handle);
        delete handle;
    }
    mNumHandles = 0;
}

Z2SoundHandlePool* Z2SoundHandles::getHandleSoundID(JAISoundID pSoundId) {
    JSULink<Z2SoundHandlePool>* link;
    for (link = getFirst(); link != NULL; link = link->getNext()) {
        Z2SoundHandlePool* handle = link->getObject();
        if (handle->isSoundAttached()) {
            if ((*handle)->getID() == pSoundId) {
                return handle;
            }
        }
    }

    return NULL;
}

Z2SoundHandlePool* Z2SoundHandles::getHandleUserData(u32 pUserData) {
    JSULink<Z2SoundHandlePool>* link;
    for (link = getFirst(); link != NULL; link = link->getNext()) {
        Z2SoundHandlePool* handle = link->getObject();
        if (handle->isSoundAttached()) {
            if ((*handle)->getUserData() == pUserData) {
                return handle;
            }
        }
    }

    return NULL;
}

/* 802AB2D8-802AB3D0 2A5C18 00F8+00 0/0 3/3 0/0 .text            getFreeHandle__14Z2SoundHandlesFv
 */
Z2SoundHandlePool* Z2SoundHandles::getFreeHandle() {
    JSULink<Z2SoundHandlePool>* link;
    for (link = getFirst(); link != NULL; link = link->getNext()) {
        Z2SoundHandlePool* handle = link->getObject();
        if (!handle->isSoundAttached()) {
            return handle;
        }
    }

    if (getNumHandles() < mNumHandles)
    {
        Z2SoundHandlePool* handle = new Z2SoundHandlePool();
        if (handle != NULL) {
            append(handle);
            return handle;
        }
    }

    return NULL;
}

/* 802AB3D0-802AB4A0 2A5D10 00D0+00 0/0 2/2 0/0 .text
 * getLowPrioSound__14Z2SoundHandlesF10JAISoundID               */
Z2SoundHandlePool* Z2SoundHandles::getLowPrioSound(JAISoundID pSoundId) {
    JAISoundInfo* sound_info = JASGlobalInstance<JAISoundInfo>::getInstance();
    Z2SoundHandlePool* handle;
    u32 low_prio = 0xffff;
    Z2SoundHandlePool* low_prio_handle = NULL;
    JSULink<Z2SoundHandlePool>* link;
    for (link = getFirst(); link != NULL; link = link->getNext()) {
        handle = link->getObject();
        if (!handle->isSoundAttached()) {
            return handle;
        }
        u32 prio = sound_info->getPriority(handle->getSound()->getID());
        if (prio < low_prio) {
            low_prio = prio;
            low_prio_handle = handle;
        }
    }
    if (sound_info->getPriority(pSoundId) >= low_prio) {
        return low_prio_handle;
    }
    return NULL;
}

void Z2SoundHandles::stopAllSounds(u32 fadeout) {
    JSULink<Z2SoundHandlePool>* link;
    for (link = getFirst(); link != NULL; link = link->getNext()) {
        JAISoundHandle* handle = link->getObject();
        //! @meme: explicit operator bool call required to match and be similar
        //!        to CHN_debug; could more concisely write handle->isSoundAttached
        //!        (for some reason cast-to-bool doesn't work?)
        if (handle && handle->operator bool()) {
            (*handle)->stop(fadeout);
        }
    }
}

bool Z2SoundHandles::isActive() const {
    JSULink<Z2SoundHandlePool>* link;
    for (link = getFirst(); link != NULL; link = link->getNext()) {
        if (link->getObject()->isSoundAttached()) {
            return true;
        }
    }

    return false;
}

/* 802AB538-802AB64C 2A5E78 0114+00 0/0 1/1 0/0 .text
 * setPos__14Z2SoundHandlesFRCQ29JGeometry8TVec3<f>             */
void Z2SoundHandles::setPos(JGeometry::TVec3<f32> const& param_0) {
    JSULink<Z2SoundHandlePool>* link;
    for (link = getFirst(); link != NULL; link = link->getNext()) {
        Z2SoundHandlePool* handle = link->getObject();
        if (handle->isSoundAttached()) {
            handle->getSound()->setPos(param_0);
        }
        else {
            remove(handle);
            delete handle;
        }
    }
}