summaryrefslogtreecommitdiff
path: root/src/Z2AudioLib/Z2SoundMgr.cpp
blob: 5d1f8952df77f135cce4eae1f99eb0f56d89b151 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// Generated By: dol2asm
// Translation Unit: Z2SoundMgr
//

#include "Z2AudioLib/Z2SoundMgr.h"
#include "Z2AudioLib/Z2Param.h"
#include "Z2AudioLib/Z2SceneMgr.h"
#include "Z2AudioLib/Z2SpeechMgr2.h"
#include "Z2AudioLib/Z2SeqMgr.h"
#include "Z2AudioLib/Z2SeMgr.h"
#include "Z2AudioLib/Z2SoundInfo.h"
#include "JSystem/JAudio2/JASCalc.h"
#include "JSystem/JAudio2/JASDriverIF.h"
#include "JSystem/JAudio2/JAUSectionHeap.h"

void dComIfGs_staffroll_next_go();

/* 802A9BC8-802A9E80 2A4508 02B8+00 1/0 1/1 0/0 .text            seqCallback__FP8JASTrackUs */
u16 seqCallback(JASTrack* i_track, u16 param_1) {
    JSUList<JAISe>* list;
    switch (param_1) {
    case 0x1000:
        Z2GetSeqMgr()->setChildTrackVolume(Z2GetSeqMgr()->getSubBgmHandle(),
                                           3, 1.0f, 0, -1.0f, -1.0f);
        Z2GetSoundMgr()->getSeqMgr()->calc();
        Z2GetSoundMgr()->getSeqMgr()->mixOut();
        break;
    case 0x1001:
        Z2GetSeqMgr()->onFieldBgmJumpStart();
        break;
    case 0x1002:
        Z2GetSeqMgr()->onFieldBgmJumpEnd();
        break;
    case 0x1003:
        Z2GetSpeechMgr2()->speakOneWord(true);
        break;
    case 0x1004:
        Z2GetSeqMgr()->onVariantBgmJumpEnd(true);
        break;
    case 0x1005:
        Z2GetSeqMgr()->onVariantBgmJumpEnd(false);
        break;
    case 0x1006:
        list = Z2GetSoundMgr()->getSeMgr()->getCategory(5)->getSeList();
        for (JSULink<JAISe>* link = list->getFirst(); link != NULL; link = link->getNext()) {
            switch (link->getObject()->getID()) {
                case Z2SE_GRO_MV_SINGING:
                case Z2SE_GRR_MV_SINGING:
                case Z2SE_CLERK_MV_SONG_HO:
                case Z2SE_WAD_A_MV_DANCE:
                case Z2SE_MCN_A_V_DANCE:
                case Z2SE_WCN_A_V_DANCE:
                    link->getObject()->getTrack()->writePort(14, 1);
                    break;
            }
        }
        break;
    case 0x1015:
        list = Z2GetSoundMgr()->getSeMgr()->getCategory(5)->getSeList();
        for (JSULink<JAISe>* link = list->getFirst(); link != NULL; link = link->getNext()) {
            switch (link->getObject()->getID()) {
                case Z2SE_MCN_A_V_DANCE:
                case Z2SE_WCN_A_V_DANCE:
                    link->getObject()->getTrack()->writePort(14, 1);
                    break;
            }
        }
        break;
    case 0x1007:
        Z2GetSeqMgr()->changeBgmStatus(7);
        break;
    case 0x1008:
        Z2GetSeqMgr()->changeBgmStatus(8);
        break;
    case 0x1014:
        dComIfGs_staffroll_next_go();
        break;
    }
    if (param_1 >= 0x1009 && param_1 <= 0x1013) {
        JAISoundID sound_id = Z2SE_CSL_TOWN_BAND_VOCAL;
        if (param_1 <= 0x100a) {
            sound_id = Z2SE_CSL_TOWN_BAND_GT_1;
        } else if (param_1 <= 0x100c) {
            sound_id = Z2SE_CSL_TOWN_BAND_GT_2;
        } else if (param_1 <= 0x100f) {
            sound_id = Z2SE_CSL_TOWN_BAND_GT_3;
        } else if (param_1 <= 0x1011) {
            sound_id = Z2SE_CSL_TOWN_BAND_FLUTE;
        }
        list = Z2GetSoundMgr()->getSeMgr()->getCategory(6)->getSeList();
        for (JSULink<JAISe>* link = list->getFirst(); link != NULL; link = link->getNext()) {
            if (link->getObject()->getID() == sound_id) {
                link->getObject()->getTrack()->writePort(14, param_1);
                break;
            }
        }
    }
    return 0;
}

/* 802A9E80-802A9EE8 2A47C0 0068+00 0/0 1/1 0/0 .text            __ct__10Z2SoundMgrFv */
Z2SoundMgr::Z2SoundMgr() :
    JASGlobalInstance<Z2SoundMgr>(this),
    mSeMgr(true),
    mSeqMgr(true),
    mStreamMgr(true)
{
    mSoundID.setAnonymous();
}

/* 802A9EE8-802AA1B0 2A4828 02C8+00 1/0 0/0 0/0 .text
 * startSound__10Z2SoundMgrF10JAISoundIDP14JAISoundHandlePCQ29JGeometry8TVec3<f> */
bool Z2SoundMgr::startSound(JAISoundID i_soundID, JAISoundHandle* i_handle,
                           JGeometry::TVec3<f32> const* i_pos) {
    switch (Z2GetSoundInfo()->getSoundType(i_soundID)) {
    case 0:
        if (Z2GetSoundInfo()->getSwBit(i_soundID) & 8) {
            Z2GetSeqMgr()->bgmAllMute(3, 0.3f);
            mSoundID = i_soundID;
        }
        return mSeMgr.startSound(i_soundID, i_handle, i_pos);
    case 1:
        if (i_soundID == Z2BGM_LUTERA_DEMO) {
            Z2GetSeqMgr()->bgmStart(Z2BGM_LUTERA2, 0, 0);
            Z2GetSeqMgr()->unMuteSceneBgm(0);
            Z2GetSeqMgr()->changeBgmStatus(0);
            return mSeMgr.startSound(Z2SE_NO_SOUND, i_handle, i_pos);
        } else {
            bool loaded = false;
            JAUSectionHeap* heap = JASGlobalInstance<JAUSectionHeap>::getInstance();
            JAUSeqDataBlocks const& loaded_blocks =
                heap->getSectionHeapData().seqDataBlocks.getLoadedBlocks();
            JSULink<JAUSeqDataBlock>* link;
            for (link = loaded_blocks.getFirst(); link != NULL; link = link->getNext()) {
                if (link->getObject()->field_0x10 == i_soundID) {
                    loaded = true;
                    break;
                }
            }
            if (!loaded) {
                heap->releaseIdleDynamicSeqDataBlock();
                if (!heap->loadDynamicSeq(i_soundID, true)) {
                    JAUSeqDataBlocks const& free_blocks =
                        heap->getSectionHeapData().seqDataBlocks.getFreeBlocks();
                    for (link = free_blocks.getFirst(); link != NULL; link = link->getNext()) {
                    }
                }

            }
            return mSeqMgr.startSound(i_soundID, i_handle, i_pos);
        }
    case 2:
        if (i_soundID == 0x2000001 || i_soundID == 0x200004d) {
            mStreamMgr.stop(180);
            return mSeMgr.startSound(Z2SE_NO_SOUND, i_handle, i_pos);
        }
        if (i_soundID == 0x2000005) {
            mSeMgr.getCategory(9)->pause(false);
            mSeMgr.getCategory(9)->getParams()->moveVolume(1.0f, 45);
        }
        return mStreamMgr.startSound(i_soundID, i_handle, i_pos);
    default:
        char error[32];
        sprintf(error, "Unknown Sound-Type id :%08x\n", (u32)i_soundID);
        return false;
    }
}


/* 802AA1B0-802AA270 2A4AF0 00C0+00 2/2 0/0 0/0 .text            calc__10Z2SoundMgrFv */
// NONMATCHING extra load
void Z2SoundMgr::calc() {
    mSeMgr.calc();
    if (!mSoundID.isAnonymous() && !isPlayingSoundID(mSoundID)) {
        if (Z2GetSceneMgr()->isSceneExist()) {
            Z2GetSeqMgr()->bgmAllUnMute(3);
        }
        mSoundID.setAnonymous();
    }
    mSeqMgr.calc();
    mStreamMgr.calc();
}

/* 802AA270-802AA33C 2A4BB0 00CC+00 1/1 2/2 0/0 .text            setIIR__10Z2SoundMgrFP8JAISoundPCs
 */
void Z2SoundMgr::setIIR(JAISound* param_0, s16 const* param_1) {
    if (param_0 != NULL) {
        JASTrack* track = param_0->getTrack();
        for (u32 i = 0; i < 4; i++) {
            JASTrack* child = track->getChild(i);
            if (child != NULL && child->getStatus() == 1) {
                child->setIIR(param_1);
                for (u32 j = 0; j < 4; j++) {
                    JASTrack* child2 = child->getChild(j);
                    if (child2 != NULL && child2->getStatus() == 1) {
                        child2->setIIR(param_1);
                    }
                }
            }
        }
    }
}

/* 8039B9C0-8039B9D0 028020 0010+00 1/1 0/0 0/0 .rodata          cResetFilterTable */
static s16 const cResetFilterTable[8] = {
    0x7FFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
};

/* 802AA33C-802AA430 2A4C7C 00F4+00 1/1 0/0 0/0 .text setFilterOff__10Z2SoundMgrFP8JAISound */
void Z2SoundMgr::setFilterOff(JAISound* param_0) {
    if (param_0 != NULL) {
        JASTrack* track = param_0->getTrack();
        for (u32 i = 0; i < 4; i++) {
            JASTrack* child = track->getChild(i);
            if (child != NULL && child->getStatus() == 1) {
                child->setIIR(cResetFilterTable);
                child->setFIR(cResetFilterTable);
                for (u32 j = 0; j < 4; j++) {
                    JASTrack* child2 = child->getChild(j);
                    if (child2 != NULL && child2->getStatus() == 1) {
                        child2->setIIR(cResetFilterTable);
                        child2->setFIR(cResetFilterTable);
                    }
                }
            }
        }
    }
}

/* 802AA430-802AA528 2A4D70 00F8+00 0/0 1/1 0/0 .text            resetFilterAll__10Z2SoundMgrFv */
void Z2SoundMgr::resetFilterAll() {
    int i = 1;
    if (struct_80451342 != 0) {
        i = 0;
    }
    for (; i < 9; i++) {
        JAISeCategoryMgr* category = mSeMgr.getCategory(i);
        int se_count = category->getMaxActiveSe();
        if (se_count > 0) {
            for (JSULink<JAISe>* link = category->getSeList()->getFirst();
                link != NULL && se_count > 0 && !link->getObject()->isFarAway();
                link = link->getNext(), se_count--)
            {
                setFilterOff(link->getObject());
            }
        } else {
            for (JSULink<JAISe>* link = category->getSeList()->getFirst();
                link != NULL && !link->getObject()->isFarAway();
                link = link->getNext())
            {
                setFilterOff(link->getObject());
            }
        }
    }
}

/* 802AA528-802AA67C 2A4E68 0154+00 2/2 0/0 0/0 .text            mixOut__10Z2SoundMgrFv */
void Z2SoundMgr::mixOut() {
    if (struct_80451340 != 0 && Z2GetSceneMgr()->isInDarkness()) {
        int i = 1;
        if (struct_80451342 != 0) {
            i = 0;
        }
        for (; i < 9; i++) {
            JAISeCategoryMgr* category = mSeMgr.getCategory(i);
            int se_count = category->getMaxActiveSe();
            if (se_count > 0) {
                for (JSULink<JAISe>* link = category->getSeList()->getFirst();
                    link != NULL && se_count > 0 && !link->getObject()->isFarAway();
                    link = link->getNext(), se_count--)
                {
                    setIIR(link->getObject(), JASCalc::CUTOFF_TO_IIR_TABLE[struct_80451341]);
                }
            } else {
                for (JSULink<JAISe>* link = category->getSeList()->getFirst();
                    link != NULL && !link->getObject()->isFarAway();
                    link = link->getNext())
                {
                    setIIR(link->getObject(), JASCalc::CUTOFF_TO_IIR_TABLE[struct_80451341]);
                }
            }
        }
    }
    mSeMgr.mixOut();
    mSeqMgr.mixOut();
    mStreamMgr.mixOut();
}

/* 802AA67C-802AA6B0 2A4FBC 0034+00 0/0 1/1 0/0 .text            framework__10Z2SoundMgrFv */
void Z2SoundMgr::framework() {
    calc();
    mixOut();
}

/* 802AA6B0-802AA7DC 2A4FF0 012C+00 0/0 1/1 0/0 .text            pauseAllGameSound__10Z2SoundMgrFb
 */
void Z2SoundMgr::pauseAllGameSound(bool i_pause) {
    mSeMgr.getCategory(0)->pause(i_pause);
    mSeMgr.getCategory(1)->pause(i_pause);
    mSeMgr.getCategory(2)->pause(i_pause);
    mSeMgr.getCategory(3)->pause(i_pause);
    mSeMgr.getCategory(4)->pause(i_pause);
    mSeMgr.getCategory(5)->pause(i_pause);
    mSeMgr.getCategory(6)->pause(i_pause);
    mSeMgr.getCategory(7)->pause(i_pause);
    mSeMgr.getCategory(8)->pause(i_pause);
    mSeMgr.getCategory(9)->pause(i_pause);
    mSeqMgr.pause(i_pause);
    mStreamMgr.pause(i_pause);
    if (i_pause) {
        Z2GetSeMgr()->seMoveVolumeAll(0.0f, 10);
        mSeqMgr.getParams()->moveVolume(0.0f, 10);
        mStreamMgr.getParams()->moveVolume(0.0f, 10);
    }
    else {
        Z2GetSeMgr()->seMoveVolumeAll(1.0f, 10);
        mSeqMgr.getParams()->moveVolume(1.0f, 10);
        mStreamMgr.getParams()->moveVolume(1.0f, 10);
    }
}


/* 802AA7DC-802AA84C 2A511C 0070+00 0/0 1/1 0/0 .text stopSoundID__10Z2SoundMgrF10JAISoundID */
void Z2SoundMgr::stopSoundID(JAISoundID i_soundID) {
    mSeMgr.stopSoundID(i_soundID);
    mSeqMgr.stopSoundID(i_soundID);
    mStreamMgr.stopSoundID(i_soundID);
}

/* 802AA84C-802AA8C8 2A518C 007C+00 0/0 1/1 0/0 .text            stopSync__10Z2SoundMgrFv */
void Z2SoundMgr::stopSync() {
    stop();
    while (mSeMgr.getNumActiveSe() > 0 || mSeqMgr.getNumActiveSeqs() > 0) {
        calc();
        if (mSeMgr.getNumActiveSe() == 0 && mSeqMgr.getNumActiveSeqs() == 0) {
            break;
        }
        mixOut();
        JASDriver::waitSubFrame();
    }
}

/* 802AA8C8-802AA908 2A5208 0040+00 1/1 0/0 0/0 .text            stop__10Z2SoundMgrFv */
void Z2SoundMgr::stop() {
    mSeMgr.stop();
    mSeqMgr.stop();
    mStreamMgr.stop();
}

/* 802AA908-802AA9E8 2A5248 00E0+00 0/0 1/1 0/0 .text            initParams__10Z2SoundMgrFv */
void Z2SoundMgr::initParams() {
    mSeMgr.initParams();
    mSeqMgr.getParams()->init();
    mStreamMgr.getParams()->init();
}


/* 802AA9E8-802AAAC4 2A5328 00DC+00 0/0 1/1 0/0 .text
 * multiVolumeSoundID__10Z2SoundMgrF10JAISoundIDf               */
// NONMATCHING extra load
void Z2SoundMgr::multiVolumeSoundID(JAISoundID i_soundID, f32 i_multi) {
    if (!i_soundID.isAnonymous() && Z2GetSoundInfo()->getSoundType(i_soundID) == 0) {
        JSUList<JAISe>* list =
            mSeMgr.getCategory(Z2GetSoundInfo()->getCategory(i_soundID))->getSeList();
        for (JSULink<JAISe>* link = list->getFirst(); link != NULL; link = link->getNext()) {
            if (link->getObject()->getID() == i_soundID) {
                link->getObject()->getAuxiliary().mParams.mVolume *= i_multi;
            }
        }
    }
}

/* 802AAAC4-802AAB94 2A5404 00D0+00 1/1 0/0 0/0 .text isPlayingSoundID__10Z2SoundMgrF10JAISoundID
 */
// NONMATCHING extra load
bool Z2SoundMgr::isPlayingSoundID(JAISoundID i_soundID) {
    if (i_soundID.isAnonymous()) {
        return false;
    }
    if (Z2GetSoundInfo()->getSoundType(i_soundID) == 0) {
        JSUList<JAISe>* list =
            mSeMgr.getCategory(Z2GetSoundInfo()->getCategory(i_soundID))->getSeList();
        for (JSULink<JAISe>* link = list->getFirst(); link != NULL; link = link->getNext()) {
            if (link->getObject()->getID() == i_soundID) {
                return true;
            }
        }
    }
    return false;
}

/* 8039B9D0-8039B9D0 028030 0000+00 0/0 0/0 0/0 .rodata          @stringBase0 */