Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/src/audio/playback.c
7857 views
1
#include <ultra64.h>
2
3
#include "heap.h"
4
#include "data.h"
5
#include "load.h"
6
#include "seqplayer.h"
7
#include "playback.h"
8
#include "synthesis.h"
9
#include "effects.h"
10
#include "external.h"
11
12
void note_set_resampling_rate(struct Note *note, f32 resamplingRateInput);
13
14
#if defined(VERSION_EU) || defined(VERSION_SH)
15
#ifdef VERSION_SH
16
void note_set_vel_pan_reverb(struct Note *note, struct ReverbInfo *reverbInfo)
17
#else
18
void note_set_vel_pan_reverb(struct Note *note, f32 velocity, u8 pan, u8 reverbVol)
19
#endif
20
{
21
struct NoteSubEu *sub = &note->noteSubEu;
22
f32 volRight, volLeft;
23
u8 strongRight;
24
u8 strongLeft;
25
s32 smallPanIndex;
26
#ifdef VERSION_EU
27
u16 unkMask = ~0x80;
28
#else
29
UNUSED u32 pad;
30
UNUSED u32 pad1;
31
f32 velocity;
32
u8 pan;
33
u8 reverbVol;
34
struct ReverbBitsData reverbBits;
35
#endif
36
37
#ifdef VERSION_SH
38
note_set_resampling_rate(note, reverbInfo->freqScale);
39
velocity = reverbInfo->velocity;
40
pan = reverbInfo->pan;
41
reverbVol = reverbInfo->reverbVol;
42
reverbBits = reverbInfo->reverbBits.s;
43
pan &= 0x7f;
44
#else
45
pan &= unkMask;
46
#endif
47
48
if (get_mirror())
49
pan *= -1;
50
51
if (note->noteSubEu.stereoHeadsetEffects && gSoundMode == SOUND_MODE_HEADSET) {
52
#ifdef VERSION_SH
53
smallPanIndex = pan >> 1;
54
#else
55
smallPanIndex = pan >> 3;
56
#endif
57
if (smallPanIndex >= ARRAY_COUNT(gHeadsetPanQuantization)) {
58
smallPanIndex = ARRAY_COUNT(gHeadsetPanQuantization) - 1;
59
}
60
61
sub->headsetPanLeft = gHeadsetPanQuantization[smallPanIndex];
62
sub->headsetPanRight = gHeadsetPanQuantization[ARRAY_COUNT(gHeadsetPanQuantization) - 1 - smallPanIndex];
63
sub->stereoStrongRight = FALSE;
64
sub->stereoStrongLeft = FALSE;
65
sub->usesHeadsetPanEffects = TRUE;
66
67
volLeft = gHeadsetPanVolume[pan];
68
volRight = gHeadsetPanVolume[127 - pan];
69
} else if (sub->stereoHeadsetEffects && gSoundMode == SOUND_MODE_STEREO) {
70
#ifdef VERSION_SH
71
strongRight = FALSE;
72
strongLeft = FALSE;
73
sub->headsetPanRight = 0;
74
sub->headsetPanLeft = 0;
75
#else
76
strongLeft = FALSE;
77
strongRight = FALSE;
78
sub->headsetPanLeft = 0;
79
sub->headsetPanRight = 0;
80
#endif
81
82
sub->usesHeadsetPanEffects = FALSE;
83
84
volLeft = gStereoPanVolume[pan];
85
volRight = gStereoPanVolume[127 - pan];
86
if (pan < 0x20) {
87
strongLeft = TRUE;
88
} else if (pan > 0x60) {
89
strongRight = TRUE;
90
}
91
92
sub->stereoStrongRight = strongRight;
93
sub->stereoStrongLeft = strongLeft;
94
95
#ifdef VERSION_SH
96
switch (reverbBits.stereoHeadsetEffects) {
97
case 0:
98
sub->stereoStrongRight = reverbBits.strongRight;
99
sub->stereoStrongLeft = reverbBits.strongLeft;
100
break;
101
102
case 1:
103
break;
104
105
case 2:
106
sub->stereoStrongRight = reverbBits.strongRight | strongRight;
107
sub->stereoStrongLeft = reverbBits.strongLeft | strongLeft;
108
break;
109
110
case 3:
111
sub->stereoStrongRight = reverbBits.strongRight ^ strongRight;
112
sub->stereoStrongLeft = reverbBits.strongLeft ^ strongLeft;
113
break;
114
}
115
#endif
116
} else if (gSoundMode == SOUND_MODE_MONO) {
117
volLeft = 0.707f;
118
volRight = 0.707f;
119
} else {
120
volLeft = gDefaultPanVolume[pan];
121
volRight = gDefaultPanVolume[127 - pan];
122
}
123
124
#ifdef VERSION_SH
125
if (velocity < 0.0f) {
126
velocity = 0.0f;
127
}
128
if (velocity > 1.0f) {
129
velocity = 1.0f;
130
}
131
132
sub->targetVolLeft = ((s32) (velocity * volLeft * 4095.999f));
133
sub->targetVolRight = ((s32) (velocity * volRight * 4095.999f));
134
sub->synthesisVolume = reverbInfo->synthesisVolume;
135
sub->filter = reverbInfo->filter;
136
#else
137
if (velocity < 0.0f) {
138
stubbed_printf("Audio: setvol: volume minus %f\n", velocity);
139
velocity = 0.0f;
140
}
141
if (velocity > 32767.f) {
142
stubbed_printf("Audio: setvol: volume overflow %f\n", velocity);
143
velocity = 32767.f;
144
}
145
146
sub->targetVolLeft = ((s32) (velocity * volLeft) & 0xffff) >> 5;
147
sub->targetVolRight = ((s32) (velocity * volRight) & 0xffff) >> 5;
148
#endif
149
150
//! @bug for the change to UQ0.7, the if statement should also have been changed accordingly
151
if (sub->reverbVol != reverbVol) {
152
#ifdef VERSION_SH
153
sub->reverbVol = reverbVol >> 1;
154
#else
155
sub->reverbVol = reverbVol;
156
#endif
157
sub->envMixerNeedsInit = TRUE;
158
return;
159
}
160
161
if (sub->needsInit) {
162
sub->envMixerNeedsInit = TRUE;
163
} else {
164
sub->envMixerNeedsInit = FALSE;
165
}
166
}
167
168
#ifdef VERSION_SH
169
#define MIN_RESAMPLING_RATE 1.99998f
170
#else
171
#define MIN_RESAMPLING_RATE 1.99996f
172
#endif
173
174
void note_set_resampling_rate(struct Note *note, f32 resamplingRateInput) {
175
f32 resamplingRate = 0.0f;
176
struct NoteSubEu *tempSub = &note->noteSubEu;
177
178
#ifdef VERSION_EU
179
if (resamplingRateInput < 0.0f) {
180
stubbed_printf("Audio: setpitch: pitch minus %f\n", resamplingRateInput);
181
resamplingRateInput = 0.0f;
182
}
183
#endif
184
if (resamplingRateInput < 2.0f) {
185
tempSub->hasTwoAdpcmParts = 0;
186
187
if (MIN_RESAMPLING_RATE < resamplingRateInput) {
188
resamplingRate = MIN_RESAMPLING_RATE;
189
} else {
190
resamplingRate = resamplingRateInput;
191
}
192
193
} else {
194
tempSub->hasTwoAdpcmParts = 1;
195
if (2 * MIN_RESAMPLING_RATE < resamplingRateInput) {
196
resamplingRate = MIN_RESAMPLING_RATE;
197
} else {
198
resamplingRate = resamplingRateInput * 0.5f;
199
}
200
}
201
note->noteSubEu.resamplingRateFixedPoint = (s32) (resamplingRate * 32768.0f);
202
}
203
204
#ifdef VERSION_EU
205
struct AudioBankSound *instrument_get_audio_bank_sound(struct Instrument *instrument, s32 semitone) {
206
struct AudioBankSound *sound;
207
if (semitone < instrument->normalRangeLo) {
208
sound = &instrument->lowNotesSound;
209
} else if (semitone <= instrument->normalRangeHi) {
210
sound = &instrument->normalNotesSound;
211
} else {
212
sound = &instrument->highNotesSound;
213
}
214
return sound;
215
}
216
217
struct Instrument *get_instrument_inner(s32 bankId, s32 instId) {
218
struct Instrument *inst;
219
220
if (IS_BANK_LOAD_COMPLETE(bankId) == FALSE) {
221
stubbed_printf("Audio: voiceman: No bank error %d\n", bankId);
222
gAudioErrorFlags = bankId + 0x10000000;
223
return NULL;
224
}
225
226
if (instId >= gCtlEntries[bankId].numInstruments) {
227
stubbed_printf("Audio: voiceman: progNo. overflow %d,%d\n",
228
instId, gCtlEntries[bankId].numInstruments);
229
gAudioErrorFlags = ((bankId << 8) + instId) + 0x3000000;
230
return NULL;
231
}
232
233
inst = gCtlEntries[bankId].instruments[instId];
234
if (inst == NULL) {
235
stubbed_printf("Audio: voiceman: progNo. undefined %d,%d\n", bankId, instId);
236
gAudioErrorFlags = ((bankId << 8) + instId) + 0x1000000;
237
return inst;
238
}
239
240
#ifdef VERSION_EU
241
if (((uintptr_t) gBankLoadedPool.persistent.pool.start <= (uintptr_t) inst
242
&& (uintptr_t) inst <= (uintptr_t)(gBankLoadedPool.persistent.pool.start
243
+ gBankLoadedPool.persistent.pool.size))
244
|| ((uintptr_t) gBankLoadedPool.temporary.pool.start <= (uintptr_t) inst
245
&& (uintptr_t) inst <= (uintptr_t)(gBankLoadedPool.temporary.pool.start
246
+ gBankLoadedPool.temporary.pool.size))) {
247
return inst;
248
}
249
250
stubbed_printf("Audio: voiceman: BAD Voicepointer %x,%d,%d\n", inst, bankId, instId);
251
gAudioErrorFlags = ((bankId << 8) + instId) + 0x2000000;
252
return NULL;
253
#else
254
return inst;
255
#endif
256
}
257
258
struct Drum *get_drum(s32 bankId, s32 drumId) {
259
struct Drum *drum;
260
261
#ifdef VERSION_SH
262
if (IS_BANK_LOAD_COMPLETE(bankId) == FALSE) {
263
stubbed_printf("Audio: voiceman: No bank error %d\n", bankId);
264
gAudioErrorFlags = bankId + 0x10000000;
265
return NULL;
266
}
267
#endif
268
269
if (drumId >= gCtlEntries[bankId].numDrums) {
270
stubbed_printf("Audio: voiceman: Percussion Overflow %d,%d\n",
271
drumId, gCtlEntries[bankId].numDrums);
272
gAudioErrorFlags = ((bankId << 8) + drumId) + 0x4000000;
273
return NULL;
274
}
275
276
#ifndef NO_SEGMENTED_MEMORY
277
if ((uintptr_t) gCtlEntries[bankId].drums < 0x80000000U) {
278
stubbed_printf("Percussion Pointer Error\n");
279
return NULL;
280
}
281
#endif
282
283
drum = gCtlEntries[bankId].drums[drumId];
284
if (drum == NULL) {
285
stubbed_printf("Audio: voiceman: Percpointer NULL %d,%d\n", bankId, drumId);
286
gAudioErrorFlags = ((bankId << 8) + drumId) + 0x5000000;
287
}
288
return drum;
289
}
290
#endif
291
#endif // VERSION_EU
292
293
#if defined(VERSION_EU) || defined(VERSION_SH)
294
void note_init_for_layer(struct Note *note, struct SequenceChannelLayer *seqLayer);
295
#else
296
s32 note_init_for_layer(struct Note *note, struct SequenceChannelLayer *seqLayer);
297
#endif
298
299
void note_init(struct Note *note) {
300
if (note->parentLayer->adsr.releaseRate == 0) {
301
adsr_init(&note->adsr, note->parentLayer->seqChannel->adsr.envelope, &note->adsrVolScale);
302
} else {
303
adsr_init(&note->adsr, note->parentLayer->adsr.envelope, &note->adsrVolScale);
304
}
305
#ifdef VERSION_SH
306
note->unkSH34 = 0;
307
#endif
308
note->adsr.state = ADSR_STATE_INITIAL;
309
#if defined(VERSION_EU) || defined(VERSION_SH)
310
note->noteSubEu = gDefaultNoteSub;
311
#else
312
note_init_volume(note);
313
note_enable(note);
314
#endif
315
}
316
317
#if defined(VERSION_EU) || defined(VERSION_SH)
318
#define note_disable2 note_disable
319
void note_disable(struct Note *note) {
320
if (note->noteSubEu.needsInit == TRUE) {
321
note->noteSubEu.needsInit = FALSE;
322
}
323
#ifdef VERSION_EU
324
else {
325
note_set_vel_pan_reverb(note, 0, 0x40, 0);
326
}
327
#endif
328
note->priority = NOTE_PRIORITY_DISABLED;
329
#ifdef VERSION_SH
330
note->unkSH34 = 0;
331
#endif
332
note->parentLayer = NO_LAYER;
333
note->prevParentLayer = NO_LAYER;
334
note->noteSubEu.enabled = FALSE;
335
note->noteSubEu.finished = FALSE;
336
#ifdef VERSION_SH
337
note->adsr.state = ADSR_STATE_DISABLED;
338
note->adsr.current = 0;
339
#endif
340
}
341
#else
342
void note_disable2(struct Note *note) {
343
note_disable(note);
344
}
345
#endif // VERSION_EU || VERSION_SH
346
347
void process_notes(void) {
348
f32 scale;
349
#ifndef VERSION_SH
350
f32 frequency;
351
#if defined(VERSION_JP) || defined(VERSION_US)
352
u8 reverbVol;
353
#endif
354
f32 velocity;
355
#if defined(VERSION_JP) || defined(VERSION_US)
356
f32 pan;
357
f32 cap;
358
#endif
359
#endif
360
struct Note *note;
361
#if defined(VERSION_EU) || defined(VERSION_SH)
362
struct NotePlaybackState *playbackState;
363
struct NoteSubEu *noteSubEu;
364
#ifndef VERSION_SH
365
UNUSED u8 pad[12];
366
u8 reverbVol;
367
UNUSED u8 pad3;
368
u8 pan;
369
#else
370
UNUSED u8 pad[8];
371
struct ReverbInfo reverbInfo;
372
#endif
373
u8 bookOffset;
374
#endif
375
struct NoteAttributes *attributes;
376
#if defined(VERSION_JP) || defined(VERSION_US)
377
struct AudioListItem *it;
378
#endif
379
s32 i;
380
381
// Macro versions of audio_list_push_front and audio_list_remove.
382
// Should ideally be changed to use copt.
383
#define PREPEND(item, head_arg) \
384
((it = (item), it->prev != NULL) \
385
? it \
386
: (it->prev = (head_arg), it->next = (head_arg)->next, (head_arg)->next->prev = it, \
387
(head_arg)->next = it, (head_arg)->u.count++, it->pool = (head_arg)->pool, it))
388
#define POP(item) \
389
((it = (item), it->prev == NULL) \
390
? it \
391
: (it->prev->next = it->next, it->next->prev = it->prev, it->prev = NULL, it))
392
393
for (i = 0; i < gMaxSimultaneousNotes; i++) {
394
note = &gNotes[i];
395
#if defined(VERSION_EU) || defined(VERSION_SH)
396
playbackState = (struct NotePlaybackState *) &note->priority;
397
if (note->parentLayer != NO_LAYER) {
398
#ifndef NO_SEGMENTED_MEMORY
399
if ((uintptr_t) playbackState->parentLayer < 0x7fffffffU) {
400
continue;
401
}
402
#endif
403
#ifdef VERSION_SH
404
if (note != playbackState->parentLayer->note && playbackState->unkSH34 == 0) {
405
playbackState->adsr.action |= ADSR_ACTION_RELEASE;
406
playbackState->adsr.fadeOutVel = gAudioBufferParameters.updatesPerFrameInv;
407
playbackState->priority = 1;
408
playbackState->unkSH34 = 2;
409
goto d;
410
} else if (!playbackState->parentLayer->enabled && playbackState->unkSH34 == 0 &&
411
playbackState->priority >= 1) {
412
// do nothing
413
} else if (playbackState->parentLayer->seqChannel->seqPlayer == NULL) {
414
sequence_channel_disable(playbackState->parentLayer->seqChannel);
415
playbackState->priority = 1;
416
playbackState->unkSH34 = 1;
417
continue;
418
} else if (playbackState->parentLayer->seqChannel->seqPlayer->muted &&
419
(playbackState->parentLayer->seqChannel->muteBehavior
420
& (MUTE_BEHAVIOR_STOP_NOTES))) {
421
// do nothing
422
} else {
423
goto d;
424
}
425
426
seq_channel_layer_note_release(playbackState->parentLayer);
427
audio_list_remove(&note->listItem);
428
audio_list_push_front(&note->listItem.pool->decaying, &note->listItem);
429
playbackState->priority = 1;
430
playbackState->unkSH34 = 2;
431
} else if (playbackState->unkSH34 == 0 && playbackState->priority >= 1) {
432
continue;
433
}
434
#else
435
if (!playbackState->parentLayer->enabled && playbackState->priority >= NOTE_PRIORITY_MIN) {
436
goto c;
437
} else if (playbackState->parentLayer->seqChannel->seqPlayer == NULL) {
438
eu_stubbed_printf_0("CAUTION:SUB IS SEPARATED FROM GROUP");
439
sequence_channel_disable(playbackState->parentLayer->seqChannel);
440
playbackState->priority = NOTE_PRIORITY_STOPPING;
441
continue;
442
} else if (playbackState->parentLayer->seqChannel->seqPlayer->muted) {
443
if ((playbackState->parentLayer->seqChannel->muteBehavior
444
& (MUTE_BEHAVIOR_STOP_SCRIPT | MUTE_BEHAVIOR_STOP_NOTES))) {
445
goto c;
446
}
447
}
448
goto d;
449
if (1) {
450
c:
451
seq_channel_layer_note_release(playbackState->parentLayer);
452
audio_list_remove(&note->listItem);
453
audio_list_push_front(&note->listItem.pool->decaying, &note->listItem);
454
playbackState->priority = NOTE_PRIORITY_STOPPING;
455
}
456
} else if (playbackState->priority >= NOTE_PRIORITY_MIN) {
457
continue;
458
}
459
#endif
460
d:
461
if (playbackState->priority != NOTE_PRIORITY_DISABLED) {
462
#ifdef VERSION_SH
463
if (1) {}
464
#endif
465
noteSubEu = &note->noteSubEu;
466
#ifdef VERSION_SH
467
if (playbackState->unkSH34 >= 1 || noteSubEu->finished) {
468
#else
469
if (playbackState->priority == NOTE_PRIORITY_STOPPING || noteSubEu->finished) {
470
#endif
471
if (playbackState->adsr.state == ADSR_STATE_DISABLED || noteSubEu->finished) {
472
if (playbackState->wantedParentLayer != NO_LAYER) {
473
note_disable(note);
474
if (playbackState->wantedParentLayer->seqChannel != NULL) {
475
note_init_for_layer(note, playbackState->wantedParentLayer);
476
note_vibrato_init(note);
477
audio_list_remove(&note->listItem);
478
audio_list_push_back(&note->listItem.pool->active, &note->listItem);
479
playbackState->wantedParentLayer = NO_LAYER;
480
// don't skip
481
} else {
482
eu_stubbed_printf_0("Error:Wait Track disappear\n");
483
note_disable(note);
484
audio_list_remove(&note->listItem);
485
audio_list_push_back(&note->listItem.pool->disabled, &note->listItem);
486
playbackState->wantedParentLayer = NO_LAYER;
487
goto skip;
488
}
489
} else {
490
note_disable(note);
491
audio_list_remove(&note->listItem);
492
audio_list_push_back(&note->listItem.pool->disabled, &note->listItem);
493
goto skip;
494
}
495
}
496
#ifndef VERSION_SH
497
if (1) {
498
}
499
#endif
500
} else if (playbackState->adsr.state == ADSR_STATE_DISABLED) {
501
note_disable(note);
502
audio_list_remove(&note->listItem);
503
audio_list_push_back(&note->listItem.pool->disabled, &note->listItem);
504
goto skip;
505
}
506
507
scale = adsr_update(&playbackState->adsr);
508
note_vibrato_update(note);
509
attributes = &playbackState->attributes;
510
#ifdef VERSION_SH
511
if (playbackState->unkSH34 == 1 || playbackState->unkSH34 == 2) {
512
reverbInfo.freqScale = attributes->freqScale;
513
reverbInfo.velocity = attributes->velocity;
514
reverbInfo.pan = attributes->pan;
515
reverbInfo.reverbVol = attributes->reverbVol;
516
reverbInfo.reverbBits = attributes->reverbBits;
517
reverbInfo.synthesisVolume = attributes->synthesisVolume;
518
reverbInfo.filter = attributes->filter;
519
bookOffset = noteSubEu->bookOffset;
520
} else {
521
reverbInfo.freqScale = playbackState->parentLayer->noteFreqScale;
522
reverbInfo.velocity = playbackState->parentLayer->noteVelocity;
523
reverbInfo.pan = playbackState->parentLayer->notePan;
524
reverbInfo.reverbBits = playbackState->parentLayer->reverbBits;
525
reverbInfo.reverbVol = playbackState->parentLayer->seqChannel->reverbVol;
526
reverbInfo.synthesisVolume = playbackState->parentLayer->seqChannel->synthesisVolume;
527
reverbInfo.filter = playbackState->parentLayer->seqChannel->filter;
528
bookOffset = playbackState->parentLayer->seqChannel->bookOffset & 0x7;
529
if (playbackState->parentLayer->seqChannel->seqPlayer->muted
530
&& (playbackState->parentLayer->seqChannel->muteBehavior & 8)) {
531
reverbInfo.freqScale = 0.0f;
532
reverbInfo.velocity = 0.0f;
533
}
534
}
535
536
reverbInfo.freqScale *= playbackState->vibratoFreqScale * playbackState->portamentoFreqScale;
537
reverbInfo.freqScale *= gAudioBufferParameters.resampleRate;
538
reverbInfo.velocity *= scale;
539
note_set_vel_pan_reverb(note, &reverbInfo);
540
#else
541
if (playbackState->priority == NOTE_PRIORITY_STOPPING) {
542
frequency = attributes->freqScale;
543
velocity = attributes->velocity;
544
pan = attributes->pan;
545
reverbVol = attributes->reverbVol;
546
if (1) {
547
}
548
bookOffset = noteSubEu->bookOffset;
549
} else {
550
frequency = playbackState->parentLayer->noteFreqScale;
551
velocity = playbackState->parentLayer->noteVelocity;
552
pan = playbackState->parentLayer->notePan;
553
reverbVol = playbackState->parentLayer->seqChannel->reverbVol;
554
bookOffset = playbackState->parentLayer->seqChannel->bookOffset & 0x7;
555
}
556
557
frequency *= playbackState->vibratoFreqScale * playbackState->portamentoFreqScale;
558
frequency *= gAudioBufferParameters.resampleRate;
559
velocity = velocity * scale * scale;
560
note_set_resampling_rate(note, frequency);
561
note_set_vel_pan_reverb(note, velocity, pan, reverbVol);
562
#endif
563
noteSubEu->bookOffset = bookOffset;
564
skip:;
565
}
566
#else
567
if (note->priority != NOTE_PRIORITY_DISABLED) {
568
if (note->priority == NOTE_PRIORITY_STOPPING || note->finished) {
569
if (note->adsrVolScale == 0 || note->finished) {
570
if (note->wantedParentLayer != NO_LAYER) {
571
note_disable2(note);
572
if (note->wantedParentLayer->seqChannel != NULL) {
573
if (note_init_for_layer(note, note->wantedParentLayer) == TRUE) {
574
note_disable2(note);
575
POP(&note->listItem);
576
PREPEND(&note->listItem, &gNoteFreeLists.disabled);
577
} else {
578
note_vibrato_init(note);
579
audio_list_push_back(&note->listItem.pool->active,
580
POP(&note->listItem));
581
note->wantedParentLayer = NO_LAYER;
582
}
583
} else {
584
note_disable2(note);
585
audio_list_push_back(&note->listItem.pool->disabled, POP(&note->listItem));
586
note->wantedParentLayer = NO_LAYER;
587
continue;
588
}
589
} else {
590
note_disable2(note);
591
audio_list_push_back(&note->listItem.pool->disabled, POP(&note->listItem));
592
continue;
593
}
594
}
595
} else {
596
if (note->adsr.state == ADSR_STATE_DISABLED) {
597
note_disable2(note);
598
audio_list_push_back(&note->listItem.pool->disabled, POP(&note->listItem));
599
continue;
600
}
601
}
602
603
adsr_update(&note->adsr);
604
note_vibrato_update(note);
605
attributes = &note->attributes;
606
if (note->priority == NOTE_PRIORITY_STOPPING) {
607
frequency = attributes->freqScale;
608
velocity = attributes->velocity;
609
pan = attributes->pan;
610
reverbVol = attributes->reverbVol;
611
} else {
612
frequency = note->parentLayer->noteFreqScale;
613
velocity = note->parentLayer->noteVelocity;
614
pan = note->parentLayer->notePan;
615
reverbVol = note->parentLayer->seqChannel->reverbVol;
616
}
617
618
scale = note->adsrVolScale;
619
frequency *= note->vibratoFreqScale * note->portamentoFreqScale;
620
cap = 3.99992f;
621
if (gAiFrequency != 32006) {
622
frequency *= US_FLOAT(32000.0) / (f32) gAiFrequency;
623
}
624
frequency = (frequency < cap ? frequency : cap);
625
scale *= 4.3498e-5f; // ~1 / 23000
626
velocity = velocity * scale * scale;
627
note_set_frequency(note, frequency);
628
note_set_vel_pan_reverb(note, velocity, pan, reverbVol);
629
continue;
630
}
631
#endif
632
}
633
#undef PREPEND
634
#undef POP
635
}
636
637
#if defined(VERSION_SH)
638
// These three are matching but have been moved from above in shindou:
639
struct AudioBankSound *instrument_get_audio_bank_sound(struct Instrument *instrument, s32 semitone) {
640
struct AudioBankSound *sound;
641
if (semitone < instrument->normalRangeLo) {
642
sound = &instrument->lowNotesSound;
643
} else if (semitone <= instrument->normalRangeHi) {
644
sound = &instrument->normalNotesSound;
645
} else {
646
sound = &instrument->highNotesSound;
647
}
648
return sound;
649
}
650
struct Instrument *get_instrument_inner(s32 bankId, s32 instId) {
651
struct Instrument *inst;
652
653
if (IS_BANK_LOAD_COMPLETE(bankId) == FALSE) {
654
gAudioErrorFlags = bankId + 0x10000000;
655
return NULL;
656
}
657
658
if (instId >= gCtlEntries[bankId].numInstruments) {
659
gAudioErrorFlags = ((bankId << 8) + instId) + 0x3000000;
660
return NULL;
661
}
662
663
inst = gCtlEntries[bankId].instruments[instId];
664
if (inst == NULL) {
665
gAudioErrorFlags = ((bankId << 8) + instId) + 0x1000000;
666
return inst;
667
}
668
669
return inst;
670
}
671
672
struct Drum *get_drum(s32 bankId, s32 drumId) {
673
struct Drum *drum;
674
675
if (IS_BANK_LOAD_COMPLETE(bankId) == FALSE) {
676
gAudioErrorFlags = bankId + 0x10000000;
677
return NULL;
678
}
679
680
if (drumId >= gCtlEntries[bankId].numDrums) {
681
gAudioErrorFlags = ((bankId << 8) + drumId) + 0x4000000;
682
return NULL;
683
}
684
685
#ifndef NO_SEGMENTED_MEMORY
686
if ((uintptr_t) gCtlEntries[bankId].drums < 0x80000000U) {
687
return NULL;
688
}
689
#endif
690
691
drum = gCtlEntries[bankId].drums[drumId];
692
if (drum == NULL) {
693
gAudioErrorFlags = ((bankId << 8) + drumId) + 0x5000000;
694
}
695
return drum;
696
}
697
#endif
698
699
void seq_channel_layer_decay_release_internal(struct SequenceChannelLayer *seqLayer, s32 target) {
700
struct Note *note;
701
struct NoteAttributes *attributes;
702
703
if (seqLayer == NO_LAYER) {
704
return;
705
}
706
707
#ifdef VERSION_SH
708
seqLayer->status = SOUND_LOAD_STATUS_NOT_LOADED;
709
#endif
710
711
if (seqLayer->note == NULL) {
712
return;
713
}
714
715
note = seqLayer->note;
716
attributes = &note->attributes;
717
718
#if defined(VERSION_JP) || defined(VERSION_US)
719
if (seqLayer->seqChannel != NULL && seqLayer->seqChannel->noteAllocPolicy == 0) {
720
seqLayer->note = NULL;
721
}
722
#endif
723
724
if (note->wantedParentLayer == seqLayer) {
725
note->wantedParentLayer = NO_LAYER;
726
}
727
728
if (note->parentLayer != seqLayer) {
729
730
#if defined(VERSION_EU) || defined(VERSION_SH)
731
if (note->parentLayer == NO_LAYER && note->wantedParentLayer == NO_LAYER &&
732
note->prevParentLayer == seqLayer && target != ADSR_STATE_DECAY) {
733
// Just guessing that this printf goes here... it's hard to parse.
734
eu_stubbed_printf_0("Slow Release Batting\n");
735
note->adsr.fadeOutVel = gAudioBufferParameters.updatesPerFrameInv;
736
note->adsr.action |= ADSR_ACTION_RELEASE;
737
}
738
#endif
739
return;
740
}
741
742
#ifndef VERSION_SH
743
seqLayer->status = SOUND_LOAD_STATUS_NOT_LOADED;
744
#endif
745
if (note->adsr.state != ADSR_STATE_DECAY) {
746
attributes->freqScale = seqLayer->noteFreqScale;
747
attributes->velocity = seqLayer->noteVelocity;
748
attributes->pan = seqLayer->notePan;
749
#ifdef VERSION_SH
750
attributes->reverbBits = seqLayer->reverbBits;
751
#endif
752
if (seqLayer->seqChannel != NULL) {
753
attributes->reverbVol = seqLayer->seqChannel->reverbVol;
754
#ifdef VERSION_SH
755
attributes->synthesisVolume = seqLayer->seqChannel->synthesisVolume;
756
attributes->filter = seqLayer->seqChannel->filter;
757
if (seqLayer->seqChannel->seqPlayer->muted && (seqLayer->seqChannel->muteBehavior & 8) != 0) {
758
note->noteSubEu.finished = TRUE;
759
}
760
note->priority = seqLayer->seqChannel->unkSH06;
761
#endif
762
}
763
#ifdef VERSION_SH
764
else {
765
#endif
766
note->priority = NOTE_PRIORITY_STOPPING;
767
#ifdef VERSION_SH
768
}
769
#endif
770
note->prevParentLayer = note->parentLayer;
771
note->parentLayer = NO_LAYER;
772
if (target == ADSR_STATE_RELEASE) {
773
#if defined(VERSION_EU) || defined(VERSION_SH)
774
note->adsr.fadeOutVel = gAudioBufferParameters.updatesPerFrameInv;
775
#else
776
note->adsr.fadeOutVel = 0x8000 / gAudioUpdatesPerFrame;
777
#endif
778
note->adsr.action |= ADSR_ACTION_RELEASE;
779
#ifdef VERSION_SH
780
note->unkSH34 = 2;
781
#endif
782
} else {
783
#ifdef VERSION_SH
784
note->unkSH34 = 1;
785
#endif
786
note->adsr.action |= ADSR_ACTION_DECAY;
787
#if defined(VERSION_EU) || defined(VERSION_SH)
788
if (seqLayer->adsr.releaseRate == 0) {
789
note->adsr.fadeOutVel = seqLayer->seqChannel->adsr.releaseRate * gAudioBufferParameters.unkUpdatesPerFrameScaled;
790
} else {
791
note->adsr.fadeOutVel = seqLayer->adsr.releaseRate * gAudioBufferParameters.unkUpdatesPerFrameScaled;
792
}
793
note->adsr.sustain = (FLOAT_CAST(seqLayer->seqChannel->adsr.sustain) * note->adsr.current) / 256.0f;
794
#else
795
if (seqLayer->adsr.releaseRate == 0) {
796
note->adsr.fadeOutVel = seqLayer->seqChannel->adsr.releaseRate * 24;
797
} else {
798
note->adsr.fadeOutVel = seqLayer->adsr.releaseRate * 24;
799
}
800
note->adsr.sustain = (note->adsr.current * seqLayer->seqChannel->adsr.sustain) / 0x10000;
801
#endif
802
}
803
}
804
805
if (target == ADSR_STATE_DECAY) {
806
audio_list_remove(&note->listItem);
807
audio_list_push_front(&note->listItem.pool->decaying, &note->listItem);
808
}
809
}
810
811
void seq_channel_layer_note_decay(struct SequenceChannelLayer *seqLayer) {
812
seq_channel_layer_decay_release_internal(seqLayer, ADSR_STATE_DECAY);
813
}
814
815
void seq_channel_layer_note_release(struct SequenceChannelLayer *seqLayer) {
816
seq_channel_layer_decay_release_internal(seqLayer, ADSR_STATE_RELEASE);
817
}
818
819
#if defined(VERSION_EU) || defined(VERSION_SH)
820
s32 build_synthetic_wave(struct Note *note, struct SequenceChannelLayer *seqLayer, s32 waveId) {
821
f32 freqScale;
822
f32 ratio;
823
u8 sampleCountIndex;
824
825
if (waveId < 128) {
826
#ifdef VERSION_EU
827
stubbed_printf("Audio:Wavemem: Bad voiceno (%d)\n", waveId);
828
#endif
829
waveId = 128;
830
}
831
832
freqScale = seqLayer->freqScale;
833
if (seqLayer->portamento.mode != 0 && 0.0f < seqLayer->portamento.extent) {
834
freqScale *= (seqLayer->portamento.extent + 1.0f);
835
}
836
if (freqScale < 1.0f) {
837
sampleCountIndex = 0;
838
ratio = 1.0465f;
839
} else if (freqScale < 2.0f) {
840
sampleCountIndex = 1;
841
ratio = 0.52325f;
842
} else if (freqScale < 4.0f) {
843
sampleCountIndex = 2;
844
ratio = 0.26263f;
845
} else {
846
sampleCountIndex = 3;
847
ratio = 0.13081f;
848
}
849
seqLayer->freqScale *= ratio;
850
note->waveId = waveId;
851
note->sampleCountIndex = sampleCountIndex;
852
853
note->noteSubEu.sound.samples = &gWaveSamples[waveId - 128][sampleCountIndex * 64];
854
855
return sampleCountIndex;
856
}
857
858
#else
859
void build_synthetic_wave(struct Note *note, struct SequenceChannelLayer *seqLayer) {
860
s32 i;
861
s32 j;
862
s32 pos;
863
s32 stepSize;
864
s32 offset;
865
u8 lim;
866
u8 origSampleCount = note->sampleCount;
867
868
if (seqLayer->freqScale < US_FLOAT(1.0)) {
869
note->sampleCount = 64;
870
seqLayer->freqScale *= US_FLOAT(1.0465);
871
stepSize = 1;
872
} else if (seqLayer->freqScale < US_FLOAT(2.0)) {
873
note->sampleCount = 32;
874
seqLayer->freqScale *= US_FLOAT(0.52325);
875
stepSize = 2;
876
} else if (seqLayer->freqScale < US_FLOAT(4.0)) {
877
note->sampleCount = 16;
878
seqLayer->freqScale *= US_FLOAT(0.26263);
879
stepSize = 4;
880
} else {
881
note->sampleCount = 8;
882
seqLayer->freqScale *= US_FLOAT(0.13081);
883
stepSize = 8;
884
}
885
886
if (note->sampleCount == origSampleCount && seqLayer->seqChannel->instOrWave == note->instOrWave) {
887
return;
888
}
889
890
// Load wave sample
891
note->instOrWave = (u8) seqLayer->seqChannel->instOrWave;
892
for (i = -1, pos = 0; pos < 0x40; pos += stepSize) {
893
i++;
894
note->synthesisBuffers->samples[i] = gWaveSamples[seqLayer->seqChannel->instOrWave - 0x80][pos];
895
}
896
897
// Repeat sample
898
for (offset = note->sampleCount; offset < 0x40; offset += note->sampleCount) {
899
lim = note->sampleCount;
900
if (offset < 0 || offset > 0) {
901
for (j = 0; j < lim; j++) {
902
note->synthesisBuffers->samples[offset + j] = note->synthesisBuffers->samples[j];
903
}
904
} else {
905
for (j = 0; j < lim; j++) {
906
note->synthesisBuffers->samples[offset + j] = note->synthesisBuffers->samples[j];
907
}
908
}
909
}
910
911
osWritebackDCache(note->synthesisBuffers->samples, sizeof(note->synthesisBuffers->samples));
912
}
913
#endif
914
915
void init_synthetic_wave(struct Note *note, struct SequenceChannelLayer *seqLayer) {
916
#if defined(VERSION_EU) || defined(VERSION_SH)
917
s32 sampleCountIndex;
918
s32 waveSampleCountIndex;
919
s32 waveId = seqLayer->instOrWave;
920
if (waveId == 0xff) {
921
waveId = seqLayer->seqChannel->instOrWave;
922
}
923
sampleCountIndex = note->sampleCountIndex;
924
waveSampleCountIndex = build_synthetic_wave(note, seqLayer, waveId);
925
#if defined(VERSION_EU) || defined(VERSION_SH)
926
note->synthesisState.samplePosInt = note->synthesisState.samplePosInt * euUnknownData_8030194c[waveSampleCountIndex] / euUnknownData_8030194c[sampleCountIndex];
927
#else // Not a real change. Just temporary so I can remove this variable.
928
note->synthesisState.samplePosInt = note->synthesisState.samplePosInt * gDefaultShortNoteVelocityTable[waveSampleCountIndex] / gDefaultShortNoteVelocityTable[sampleCountIndex];
929
#endif
930
#else
931
s32 sampleCount = note->sampleCount;
932
build_synthetic_wave(note, seqLayer);
933
if (sampleCount != 0) {
934
note->samplePosInt *= note->sampleCount / sampleCount;
935
} else {
936
note->samplePosInt = 0;
937
}
938
#endif
939
}
940
941
void init_note_list(struct AudioListItem *list) {
942
list->prev = list;
943
list->next = list;
944
list->u.count = 0;
945
}
946
947
void init_note_lists(struct NotePool *pool) {
948
init_note_list(&pool->disabled);
949
init_note_list(&pool->decaying);
950
init_note_list(&pool->releasing);
951
init_note_list(&pool->active);
952
pool->disabled.pool = pool;
953
pool->decaying.pool = pool;
954
pool->releasing.pool = pool;
955
pool->active.pool = pool;
956
}
957
958
void init_note_free_list(void) {
959
s32 i;
960
961
init_note_lists(&gNoteFreeLists);
962
for (i = 0; i < gMaxSimultaneousNotes; i++) {
963
gNotes[i].listItem.u.value = &gNotes[i];
964
gNotes[i].listItem.prev = NULL;
965
audio_list_push_back(&gNoteFreeLists.disabled, &gNotes[i].listItem);
966
}
967
}
968
969
void note_pool_clear(struct NotePool *pool) {
970
s32 i;
971
struct AudioListItem *source;
972
struct AudioListItem *cur;
973
struct AudioListItem *dest;
974
UNUSED s32 j; // unused in EU
975
976
for (i = 0; i < 4; i++) {
977
switch (i) {
978
case 0:
979
source = &pool->disabled;
980
dest = &gNoteFreeLists.disabled;
981
break;
982
983
case 1:
984
source = &pool->decaying;
985
dest = &gNoteFreeLists.decaying;
986
break;
987
988
case 2:
989
source = &pool->releasing;
990
dest = &gNoteFreeLists.releasing;
991
break;
992
993
case 3:
994
source = &pool->active;
995
dest = &gNoteFreeLists.active;
996
break;
997
}
998
999
#if defined(VERSION_EU) || defined(VERSION_SH)
1000
for (;;) {
1001
cur = source->next;
1002
if (cur == source) {
1003
break;
1004
}
1005
if (cur == NULL) {
1006
eu_stubbed_printf_0("Audio: C-Alloc : Dealloc voice is NULL\n");
1007
break;
1008
}
1009
audio_list_remove(cur);
1010
audio_list_push_back(dest, cur);
1011
}
1012
#else
1013
j = 0;
1014
do {
1015
cur = source->next;
1016
if (cur == source) {
1017
break;
1018
}
1019
audio_list_remove(cur);
1020
audio_list_push_back(dest, cur);
1021
j++;
1022
} while (j <= gMaxSimultaneousNotes);
1023
#endif
1024
}
1025
}
1026
1027
void note_pool_fill(struct NotePool *pool, s32 count) {
1028
s32 i;
1029
s32 j;
1030
struct Note *note;
1031
struct AudioListItem *source;
1032
struct AudioListItem *dest;
1033
1034
note_pool_clear(pool);
1035
1036
for (i = 0, j = 0; j < count; i++) {
1037
if (i == 4) {
1038
eu_stubbed_printf_1("Alloc Error:Dim voice-Alloc %d", count);
1039
return;
1040
}
1041
1042
switch (i) {
1043
case 0:
1044
source = &gNoteFreeLists.disabled;
1045
dest = &pool->disabled;
1046
break;
1047
1048
case 1:
1049
source = &gNoteFreeLists.decaying;
1050
dest = &pool->decaying;
1051
break;
1052
1053
case 2:
1054
source = &gNoteFreeLists.releasing;
1055
dest = &pool->releasing;
1056
break;
1057
1058
case 3:
1059
source = &gNoteFreeLists.active;
1060
dest = &pool->active;
1061
break;
1062
}
1063
1064
while (j < count) {
1065
note = audio_list_pop_back(source);
1066
if (note == NULL) {
1067
break;
1068
}
1069
audio_list_push_back(dest, &note->listItem);
1070
j++;
1071
}
1072
}
1073
}
1074
1075
void audio_list_push_front(struct AudioListItem *list, struct AudioListItem *item) {
1076
// add 'item' to the front of the list given by 'list', if it's not in any list
1077
if (item->prev != NULL) {
1078
eu_stubbed_printf_0("Error:Same List Add\n");
1079
} else {
1080
item->prev = list;
1081
item->next = list->next;
1082
list->next->prev = item;
1083
list->next = item;
1084
list->u.count++;
1085
item->pool = list->pool;
1086
}
1087
}
1088
1089
void audio_list_remove(struct AudioListItem *item) {
1090
// remove 'item' from the list it's in, if any
1091
if (item->prev == NULL) {
1092
eu_stubbed_printf_0("Already Cut\n");
1093
} else {
1094
item->prev->next = item->next;
1095
item->next->prev = item->prev;
1096
item->prev = NULL;
1097
}
1098
}
1099
1100
struct Note *pop_node_with_lower_prio(struct AudioListItem *list, s32 limit) {
1101
struct AudioListItem *cur = list->next;
1102
struct AudioListItem *best;
1103
1104
if (cur == list) {
1105
return NULL;
1106
}
1107
1108
for (best = cur; cur != list; cur = cur->next) {
1109
if (((struct Note *) best->u.value)->priority >= ((struct Note *) cur->u.value)->priority) {
1110
best = cur;
1111
}
1112
}
1113
1114
#if defined(VERSION_EU) || defined(VERSION_SH)
1115
if (best == NULL) {
1116
return NULL;
1117
}
1118
1119
if (limit <= ((struct Note *) best->u.value)->priority) {
1120
return NULL;
1121
}
1122
#else
1123
if (limit < ((struct Note *) best->u.value)->priority) {
1124
return NULL;
1125
}
1126
#endif
1127
1128
#ifndef VERSION_SH
1129
audio_list_remove(best);
1130
#endif
1131
return best->u.value;
1132
}
1133
1134
#if defined(VERSION_EU) || defined(VERSION_SH)
1135
void note_init_for_layer(struct Note *note, struct SequenceChannelLayer *seqLayer) {
1136
UNUSED s32 pad[4];
1137
s16 instId;
1138
struct NoteSubEu *sub = &note->noteSubEu;
1139
1140
note->prevParentLayer = NO_LAYER;
1141
note->parentLayer = seqLayer;
1142
note->priority = seqLayer->seqChannel->notePriority;
1143
seqLayer->notePropertiesNeedInit = TRUE;
1144
seqLayer->status = SOUND_LOAD_STATUS_DISCARDABLE; // "loaded"
1145
seqLayer->note = note;
1146
seqLayer->seqChannel->noteUnused = note;
1147
seqLayer->seqChannel->layerUnused = seqLayer;
1148
seqLayer->noteVelocity = 0.0f;
1149
note_init(note);
1150
instId = seqLayer->instOrWave;
1151
if (instId == 0xff) {
1152
instId = seqLayer->seqChannel->instOrWave;
1153
}
1154
sub->sound.audioBankSound = seqLayer->sound;
1155
1156
if (instId >= 0x80) {
1157
sub->isSyntheticWave = TRUE;
1158
} else {
1159
sub->isSyntheticWave = FALSE;
1160
}
1161
1162
if (sub->isSyntheticWave) {
1163
build_synthetic_wave(note, seqLayer, instId);
1164
}
1165
#ifdef VERSION_SH
1166
note->bankId = seqLayer->seqChannel->bankId;
1167
#else
1168
sub->bankId = seqLayer->seqChannel->bankId;
1169
#endif
1170
sub->stereoHeadsetEffects = seqLayer->seqChannel->stereoHeadsetEffects;
1171
sub->reverbIndex = seqLayer->seqChannel->reverbIndex & 3;
1172
}
1173
#else
1174
s32 note_init_for_layer(struct Note *note, struct SequenceChannelLayer *seqLayer) {
1175
note->prevParentLayer = NO_LAYER;
1176
note->parentLayer = seqLayer;
1177
note->priority = seqLayer->seqChannel->notePriority;
1178
if (IS_BANK_LOAD_COMPLETE(seqLayer->seqChannel->bankId) == FALSE) {
1179
return TRUE;
1180
}
1181
1182
note->bankId = seqLayer->seqChannel->bankId;
1183
note->stereoHeadsetEffects = seqLayer->seqChannel->stereoHeadsetEffects;
1184
note->sound = seqLayer->sound;
1185
seqLayer->status = SOUND_LOAD_STATUS_DISCARDABLE; // "loaded"
1186
seqLayer->note = note;
1187
seqLayer->seqChannel->noteUnused = note;
1188
seqLayer->seqChannel->layerUnused = seqLayer;
1189
if (note->sound == NULL) {
1190
build_synthetic_wave(note, seqLayer);
1191
}
1192
note_init(note);
1193
return FALSE;
1194
}
1195
#endif
1196
1197
void func_80319728(struct Note *note, struct SequenceChannelLayer *seqLayer) {
1198
seq_channel_layer_note_release(note->parentLayer);
1199
note->wantedParentLayer = seqLayer;
1200
}
1201
1202
void note_release_and_take_ownership(struct Note *note, struct SequenceChannelLayer *seqLayer) {
1203
note->wantedParentLayer = seqLayer;
1204
#ifdef VERSION_SH
1205
note->priority = seqLayer->seqChannel->notePriority;
1206
#else
1207
note->priority = NOTE_PRIORITY_STOPPING;
1208
#endif
1209
1210
#if defined(VERSION_EU) || defined(VERSION_SH)
1211
note->adsr.fadeOutVel = gAudioBufferParameters.updatesPerFrameInv;
1212
#else
1213
note->adsr.fadeOutVel = 0x8000 / gAudioUpdatesPerFrame;
1214
#endif
1215
note->adsr.action |= ADSR_ACTION_RELEASE;
1216
}
1217
1218
struct Note *alloc_note_from_disabled(struct NotePool *pool, struct SequenceChannelLayer *seqLayer) {
1219
struct Note *note = audio_list_pop_back(&pool->disabled);
1220
if (note != NULL) {
1221
#if defined(VERSION_EU) || defined(VERSION_SH)
1222
note_init_for_layer(note, seqLayer);
1223
#else
1224
if (note_init_for_layer(note, seqLayer) == TRUE) {
1225
audio_list_push_front(&gNoteFreeLists.disabled, &note->listItem);
1226
return NULL;
1227
}
1228
#endif
1229
audio_list_push_front(&pool->active, &note->listItem);
1230
}
1231
return note;
1232
}
1233
1234
struct Note *alloc_note_from_decaying(struct NotePool *pool, struct SequenceChannelLayer *seqLayer) {
1235
struct Note *note = audio_list_pop_back(&pool->decaying);
1236
if (note != NULL) {
1237
note_release_and_take_ownership(note, seqLayer);
1238
audio_list_push_back(&pool->releasing, &note->listItem);
1239
}
1240
return note;
1241
}
1242
1243
struct Note *alloc_note_from_active(struct NotePool *pool, struct SequenceChannelLayer *seqLayer) {
1244
#ifdef VERSION_SH
1245
struct Note *rNote;
1246
#endif
1247
struct Note *aNote;
1248
#ifdef VERSION_SH
1249
s32 rPriority, aPriority;
1250
rPriority = aPriority = 0x10;
1251
1252
rNote = pop_node_with_lower_prio(&pool->releasing, seqLayer->seqChannel->notePriority);
1253
1254
if (rNote != NULL) {
1255
rPriority = rNote->priority;
1256
}
1257
#endif
1258
1259
aNote = pop_node_with_lower_prio(&pool->active, seqLayer->seqChannel->notePriority);
1260
1261
if (aNote == NULL) {
1262
eu_stubbed_printf_0("Audio: C-Alloc : lowerPrio is NULL\n");
1263
} else {
1264
#ifdef VERSION_SH
1265
aPriority = aNote->priority;
1266
#else
1267
func_80319728(aNote, seqLayer);
1268
audio_list_push_back(&pool->releasing, &aNote->listItem);
1269
#endif
1270
}
1271
1272
#ifdef VERSION_SH
1273
if (rNote == NULL && aNote == NULL) {
1274
return NULL;
1275
}
1276
1277
if (aPriority < rPriority) {
1278
audio_list_remove(&aNote->listItem);
1279
func_80319728(aNote, seqLayer);
1280
audio_list_push_back(&pool->releasing, &aNote->listItem);
1281
aNote->priority = seqLayer->seqChannel->notePriority;
1282
return aNote;
1283
}
1284
rNote->wantedParentLayer = seqLayer;
1285
rNote->priority = seqLayer->seqChannel->notePriority;
1286
return rNote;
1287
#else
1288
return aNote;
1289
#endif
1290
}
1291
1292
struct Note *alloc_note(struct SequenceChannelLayer *seqLayer) {
1293
struct Note *ret;
1294
u32 policy = seqLayer->seqChannel->noteAllocPolicy;
1295
1296
if (policy & NOTE_ALLOC_LAYER) {
1297
ret = seqLayer->note;
1298
if (ret != NULL && ret->prevParentLayer == seqLayer
1299
#if defined(VERSION_EU) || defined(VERSION_SH)
1300
&& ret->wantedParentLayer == NO_LAYER
1301
#endif
1302
) {
1303
note_release_and_take_ownership(ret, seqLayer);
1304
audio_list_remove(&ret->listItem);
1305
#if defined(VERSION_EU) || defined(VERSION_SH)
1306
audio_list_push_back(&ret->listItem.pool->releasing, &ret->listItem);
1307
#else
1308
audio_list_push_back(&gNoteFreeLists.releasing, &ret->listItem);
1309
#endif
1310
return ret;
1311
}
1312
}
1313
1314
if (policy & NOTE_ALLOC_CHANNEL) {
1315
if (!(ret = alloc_note_from_disabled(&seqLayer->seqChannel->notePool, seqLayer))
1316
&& !(ret = alloc_note_from_decaying(&seqLayer->seqChannel->notePool, seqLayer))
1317
&& !(ret = alloc_note_from_active(&seqLayer->seqChannel->notePool, seqLayer))) {
1318
#ifdef VERSION_SH
1319
goto null_return;
1320
#else
1321
eu_stubbed_printf_0("Sub Limited Warning: Drop Voice");
1322
seqLayer->status = SOUND_LOAD_STATUS_NOT_LOADED;
1323
return NULL;
1324
#endif
1325
}
1326
return ret;
1327
}
1328
1329
if (policy & NOTE_ALLOC_SEQ) {
1330
if (!(ret = alloc_note_from_disabled(&seqLayer->seqChannel->notePool, seqLayer))
1331
&& !(ret = alloc_note_from_disabled(&seqLayer->seqChannel->seqPlayer->notePool, seqLayer))
1332
&& !(ret = alloc_note_from_decaying(&seqLayer->seqChannel->notePool, seqLayer))
1333
&& !(ret = alloc_note_from_decaying(&seqLayer->seqChannel->seqPlayer->notePool, seqLayer))
1334
&& !(ret = alloc_note_from_active(&seqLayer->seqChannel->notePool, seqLayer))
1335
&& !(ret = alloc_note_from_active(&seqLayer->seqChannel->seqPlayer->notePool, seqLayer))) {
1336
#ifdef VERSION_SH
1337
goto null_return;
1338
#else
1339
eu_stubbed_printf_0("Warning: Drop Voice");
1340
seqLayer->status = SOUND_LOAD_STATUS_NOT_LOADED;
1341
return NULL;
1342
#endif
1343
}
1344
return ret;
1345
}
1346
1347
if (policy & NOTE_ALLOC_GLOBAL_FREELIST) {
1348
if (!(ret = alloc_note_from_disabled(&gNoteFreeLists, seqLayer))
1349
&& !(ret = alloc_note_from_decaying(&gNoteFreeLists, seqLayer))
1350
&& !(ret = alloc_note_from_active(&gNoteFreeLists, seqLayer))) {
1351
#ifdef VERSION_SH
1352
goto null_return;
1353
#else
1354
eu_stubbed_printf_0("Warning: Drop Voice");
1355
seqLayer->status = SOUND_LOAD_STATUS_NOT_LOADED;
1356
return NULL;
1357
#endif
1358
}
1359
return ret;
1360
}
1361
1362
if (!(ret = alloc_note_from_disabled(&seqLayer->seqChannel->notePool, seqLayer))
1363
&& !(ret = alloc_note_from_disabled(&seqLayer->seqChannel->seqPlayer->notePool, seqLayer))
1364
&& !(ret = alloc_note_from_disabled(&gNoteFreeLists, seqLayer))
1365
&& !(ret = alloc_note_from_decaying(&seqLayer->seqChannel->notePool, seqLayer))
1366
&& !(ret = alloc_note_from_decaying(&seqLayer->seqChannel->seqPlayer->notePool, seqLayer))
1367
&& !(ret = alloc_note_from_decaying(&gNoteFreeLists, seqLayer))
1368
&& !(ret = alloc_note_from_active(&seqLayer->seqChannel->notePool, seqLayer))
1369
&& !(ret = alloc_note_from_active(&seqLayer->seqChannel->seqPlayer->notePool, seqLayer))
1370
&& !(ret = alloc_note_from_active(&gNoteFreeLists, seqLayer))) {
1371
#ifdef VERSION_SH
1372
goto null_return;
1373
#else
1374
eu_stubbed_printf_0("Warning: Drop Voice");
1375
seqLayer->status = SOUND_LOAD_STATUS_NOT_LOADED;
1376
return NULL;
1377
#endif
1378
}
1379
return ret;
1380
1381
#ifdef VERSION_SH
1382
null_return:
1383
seqLayer->status = SOUND_LOAD_STATUS_NOT_LOADED;
1384
return NULL;
1385
#endif
1386
}
1387
1388
#if defined(VERSION_JP) || defined(VERSION_US)
1389
void reclaim_notes(void) {
1390
struct Note *note;
1391
s32 i;
1392
s32 cond;
1393
1394
for (i = 0; i < gMaxSimultaneousNotes; i++) {
1395
note = &gNotes[i];
1396
if (note->parentLayer != NO_LAYER) {
1397
cond = FALSE;
1398
if (!note->parentLayer->enabled && note->priority >= NOTE_PRIORITY_MIN) {
1399
cond = TRUE;
1400
} else if (note->parentLayer->seqChannel == NULL) {
1401
audio_list_push_back(&gLayerFreeList, &note->parentLayer->listItem);
1402
seq_channel_layer_disable(note->parentLayer);
1403
note->priority = NOTE_PRIORITY_STOPPING;
1404
} else if (note->parentLayer->seqChannel->seqPlayer == NULL) {
1405
sequence_channel_disable(note->parentLayer->seqChannel);
1406
note->priority = NOTE_PRIORITY_STOPPING;
1407
} else if (note->parentLayer->seqChannel->seqPlayer->muted) {
1408
if (note->parentLayer->seqChannel->muteBehavior
1409
& (MUTE_BEHAVIOR_STOP_SCRIPT | MUTE_BEHAVIOR_STOP_NOTES)) {
1410
cond = TRUE;
1411
}
1412
} else {
1413
cond = FALSE;
1414
}
1415
1416
if (cond) {
1417
seq_channel_layer_note_release(note->parentLayer);
1418
audio_list_remove(&note->listItem);
1419
audio_list_push_front(&note->listItem.pool->disabled, &note->listItem);
1420
note->priority = NOTE_PRIORITY_STOPPING;
1421
}
1422
}
1423
}
1424
}
1425
#endif
1426
1427
void note_init_all(void) {
1428
struct Note *note;
1429
s32 i;
1430
1431
for (i = 0; i < gMaxSimultaneousNotes; i++) {
1432
note = &gNotes[i];
1433
#if defined(VERSION_EU) || defined(VERSION_SH)
1434
note->noteSubEu = gZeroNoteSub;
1435
#else
1436
note->enabled = FALSE;
1437
note->stereoStrongRight = FALSE;
1438
note->stereoStrongLeft = FALSE;
1439
note->stereoHeadsetEffects = FALSE;
1440
#endif
1441
note->priority = NOTE_PRIORITY_DISABLED;
1442
#ifdef VERSION_SH
1443
note->unkSH34 = 0;
1444
#endif
1445
note->parentLayer = NO_LAYER;
1446
note->wantedParentLayer = NO_LAYER;
1447
note->prevParentLayer = NO_LAYER;
1448
#if defined(VERSION_EU) || defined(VERSION_SH)
1449
note->waveId = 0;
1450
#else
1451
note->reverbVol = 0;
1452
note->usesHeadsetPanEffects = FALSE;
1453
note->sampleCount = 0;
1454
note->instOrWave = 0;
1455
note->targetVolLeft = 0;
1456
note->targetVolRight = 0;
1457
note->frequency = 0.0f;
1458
note->unused1 = 0x3f;
1459
#endif
1460
note->attributes.velocity = 0.0f;
1461
note->adsrVolScale = 0;
1462
note->adsr.state = ADSR_STATE_DISABLED;
1463
note->adsr.action = 0;
1464
note->vibratoState.active = FALSE;
1465
note->portamento.cur = 0.0f;
1466
note->portamento.speed = 0.0f;
1467
#if defined(VERSION_SH)
1468
note->synthesisState.synthesisBuffers = sound_alloc_uninitialized(&gNotesAndBuffersPool, sizeof(struct NoteSynthesisBuffers));
1469
#elif defined(VERSION_EU)
1470
note->synthesisState.synthesisBuffers = soundAlloc(&gNotesAndBuffersPool, sizeof(struct NoteSynthesisBuffers));
1471
#else
1472
note->synthesisBuffers = soundAlloc(&gNotesAndBuffersPool, sizeof(struct NoteSynthesisBuffers));
1473
#endif
1474
}
1475
}
1476
1477