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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
|
/**
* Original Filename: sub_sys.c
*/
#include "array_count.h"
#include "audiothread_cmd.h"
#include "ultra64.h"
#include "versions.h"
#include "audio.h"
#define SAMPLES_TO_OVERPRODUCE 0x10
#define EXTRA_BUFFERED_AI_SAMPLES_TARGET 0x80
AudioTask* AudioThread_UpdateImpl(void);
void AudioThread_SetFadeInTimer(s32 seqPlayerIndex, s32 fadeTimer);
void AudioThread_SetFadeOutTimer(s32 seqPlayerIndex, s32 fadeTimer);
void AudioThread_ProcessCmds(u32);
void AudioThread_ProcessSeqPlayerCmd(SequencePlayer* seqPlayer, AudioCmd* cmd);
void AudioThread_ProcessChannelCmd(SequenceChannel* channel, AudioCmd* cmd);
s32 func_800E66C0(s32 flags);
// AudioMgr_Retrace
AudioTask* AudioThread_Update(void) {
return AudioThread_UpdateImpl();
}
#if !(OOT_VERSION < PAL_1_0 || !PLATFORM_N64)
static s32 sMaxAbiCmdCnt = 0x80;
static AudioTask* sWaitingAudioTask = NULL;
#endif
/**
* This is Audio_Update for the audio thread
*
* original name: CreateAudioTask (note: function is heavily modified in Animal Crossing)
*/
AudioTask* AudioThread_UpdateImpl(void) {
#if OOT_VERSION < PAL_1_0 || !PLATFORM_N64
static s32 sMaxAbiCmdCnt = 0x80;
static AudioTask* sWaitingAudioTask = NULL;
#endif
u32 samplesRemainingInAi;
s32 abiCmdCnt;
s32 pad;
s32 j;
s32 sp5C;
s16* curAiBuffer;
OSTask_t* task;
s32 index;
u32 sp4C;
s32 sp48;
s32 i;
gAudioCtx.totalTaskCount++;
if (gAudioCtx.totalTaskCount % (gAudioCtx.audioBufferParameters.specUnk4) != 0) {
if (gAudioCustomUpdateFunction != NULL) {
gAudioCustomUpdateFunction();
}
if ((gAudioCtx.totalTaskCount % gAudioCtx.audioBufferParameters.specUnk4) + 1 ==
gAudioCtx.audioBufferParameters.specUnk4) {
return sWaitingAudioTask;
} else {
return NULL;
}
}
osSendMesg(gAudioCtx.taskStartQueueP, (OSMesg)gAudioCtx.totalTaskCount, OS_MESG_NOBLOCK);
gAudioCtx.rspTaskIndex ^= 1;
gAudioCtx.curAiBufIndex++;
gAudioCtx.curAiBufIndex %= 3;
index = (gAudioCtx.curAiBufIndex - 2 + 3) % 3;
samplesRemainingInAi = osAiGetLength() / 4;
if (gAudioCtx.resetTimer < 16) {
if (gAudioCtx.aiBufLengths[index] != 0) {
osAiSetNextBuffer(gAudioCtx.aiBuffers[index], gAudioCtx.aiBufLengths[index] * 4);
if (gAudioCtx.aiBuffers[index]) {}
if (gAudioCtx.aiBufLengths[index]) {}
}
}
if (gAudioCustomUpdateFunction != NULL) {
gAudioCustomUpdateFunction();
}
sp5C = gAudioCtx.curAudioFrameDmaCount;
for (i = 0; i < gAudioCtx.curAudioFrameDmaCount; i++) {
if (osRecvMesg(&gAudioCtx.curAudioFrameDmaQueue, NULL, OS_MESG_NOBLOCK) == 0) {
sp5C--;
}
}
if (sp5C != 0) {
for (i = 0; i < sp5C; i++) {
osRecvMesg(&gAudioCtx.curAudioFrameDmaQueue, NULL, OS_MESG_BLOCK);
}
}
sp48 = MQ_GET_COUNT(&gAudioCtx.curAudioFrameDmaQueue);
if (sp48 != 0) {
for (i = 0; i < sp48; i++) {
osRecvMesg(&gAudioCtx.curAudioFrameDmaQueue, NULL, OS_MESG_NOBLOCK);
}
}
gAudioCtx.curAudioFrameDmaCount = 0;
AudioLoad_DecreaseSampleDmaTtls();
AudioLoad_ProcessLoads(gAudioCtx.resetStatus);
AudioLoad_ProcessScriptLoads();
if (gAudioCtx.resetStatus != 0) {
if (AudioHeap_ResetStep() == 0) {
if (gAudioCtx.resetStatus == 0) {
osSendMesg(gAudioCtx.audioResetQueueP, (OSMesg)(u32)gAudioCtx.specId, OS_MESG_NOBLOCK);
}
sWaitingAudioTask = NULL;
return NULL;
}
}
if (gAudioCtx.resetTimer > 16) {
return NULL;
}
if (gAudioCtx.resetTimer != 0) {
gAudioCtx.resetTimer++;
}
gAudioCtx.curTask = &gAudioCtx.rspTask[gAudioCtx.rspTaskIndex];
gAudioCtx.curAbiCmdBuf = gAudioCtx.abiCmdBufs[gAudioCtx.rspTaskIndex];
index = gAudioCtx.curAiBufIndex;
curAiBuffer = gAudioCtx.aiBuffers[index];
gAudioCtx.aiBufLengths[index] =
(s16)((((gAudioCtx.audioBufferParameters.samplesPerFrameTarget - samplesRemainingInAi) +
EXTRA_BUFFERED_AI_SAMPLES_TARGET) &
~0xF) +
SAMPLES_TO_OVERPRODUCE);
if (gAudioCtx.aiBufLengths[index] < gAudioCtx.audioBufferParameters.minAiBufferLength) {
gAudioCtx.aiBufLengths[index] = gAudioCtx.audioBufferParameters.minAiBufferLength;
}
if (gAudioCtx.aiBufLengths[index] > gAudioCtx.audioBufferParameters.maxAiBufferLength) {
gAudioCtx.aiBufLengths[index] = gAudioCtx.audioBufferParameters.maxAiBufferLength;
}
j = 0;
if (gAudioCtx.resetStatus == 0) {
// msg = 0000RREE R = read pos, E = End Pos
while (osRecvMesg(gAudioCtx.threadCmdProcQueueP, (OSMesg*)&sp4C, OS_MESG_NOBLOCK) != -1) {
if (1) {}
AudioThread_ProcessCmds(sp4C);
j++;
}
if ((j == 0) && (gAudioCtx.threadCmdQueueFinished)) {
AudioThread_ScheduleProcessCmds();
}
}
gAudioCtx.curAbiCmdBuf =
AudioSynth_Update(gAudioCtx.curAbiCmdBuf, &abiCmdCnt, curAiBuffer, gAudioCtx.aiBufLengths[index]);
// Update audioRandom to the next random number
gAudioCtx.audioRandom = (gAudioCtx.audioRandom + gAudioCtx.totalTaskCount) * osGetCount();
gAudioCtx.audioRandom = gAudioCtx.audioRandom + gAudioCtx.aiBuffers[index][gAudioCtx.totalTaskCount & 0xFF];
// gWaveSamples[8] interprets compiled assembly code as s16 samples as a way to generate sound with noise.
// Start with the address of AudioThread_Update, and offset it by a random number between 0 - 0xFFF0
// Use the resulting address as the starting address to interpret an array of samples i.e. `s16 samples[]`
gWaveSamples[8] = (s16*)((u8*)AudioThread_Update + (gAudioCtx.audioRandom & 0xFFF0));
index = gAudioCtx.rspTaskIndex;
gAudioCtx.curTask->msgQueue = NULL;
gAudioCtx.curTask->unk_44 = NULL;
task = &gAudioCtx.curTask->task.t;
task->type = M_AUDTASK;
task->flags = 0;
task->ucode_boot = aspMainTextStart;
task->ucode_boot_size = SP_UCODE_SIZE;
task->ucode_data_size = (size_t)(aspMainDataEnd - aspMainDataStart) * sizeof(u64) - 1;
task->ucode = aspMainTextStart;
task->ucode_data = aspMainDataStart;
task->ucode_size = SP_UCODE_SIZE;
task->dram_stack = NULL;
task->dram_stack_size = 0;
task->output_buff = NULL;
task->output_buff_size = NULL;
if (1) {}
task->data_ptr = (u64*)gAudioCtx.abiCmdBufs[index];
task->data_size = abiCmdCnt * sizeof(Acmd);
task->yield_data_ptr = NULL;
task->yield_data_size = 0;
if (sMaxAbiCmdCnt < abiCmdCnt) {
sMaxAbiCmdCnt = abiCmdCnt;
}
if (gAudioCtx.audioBufferParameters.specUnk4 == 1) {
return gAudioCtx.curTask;
} else {
sWaitingAudioTask = gAudioCtx.curTask;
return NULL;
}
}
/**
* original name: Nap_AudioSysProcess
*/
void AudioThread_ProcessGlobalCmd(AudioCmd* cmd) {
s32 i;
s32 pad[3];
u32 flags;
switch (cmd->op) {
case AUDIOCMD_OP_GLOBAL_SYNC_LOAD_SEQ_PARTS:
AudioLoad_SyncLoadSeqParts(cmd->arg1, cmd->arg2);
break;
case AUDIOCMD_OP_GLOBAL_INIT_SEQPLAYER:
AudioLoad_SyncInitSeqPlayer(cmd->arg0, cmd->arg1, cmd->arg2);
AudioThread_SetFadeInTimer(cmd->arg0, cmd->asInt);
break;
case AUDIOCMD_OP_GLOBAL_INIT_SEQPLAYER_SKIP_TICKS:
AudioLoad_SyncInitSeqPlayerSkipTicks(cmd->arg0, cmd->arg1, cmd->asInt);
break;
case AUDIOCMD_OP_GLOBAL_DISABLE_SEQPLAYER:
if (gAudioCtx.seqPlayers[cmd->arg0].enabled) {
if (cmd->asInt == 0) {
AudioSeq_SequencePlayerDisableAsFinished(&gAudioCtx.seqPlayers[cmd->arg0]);
} else {
AudioThread_SetFadeOutTimer(cmd->arg0, cmd->asInt);
}
}
break;
case AUDIOCMD_OP_GLOBAL_SET_SOUND_OUTPUT_MODE:
gAudioCtx.soundOutputMode = cmd->asUInt;
break;
case AUDIOCMD_OP_GLOBAL_MUTE:
for (i = 0; i < gAudioCtx.audioBufferParameters.numSequencePlayers; i++) {
SequencePlayer* seqPlayer = &gAudioCtx.seqPlayers[i];
seqPlayer->muted = true;
seqPlayer->recalculateVolume = true;
}
break;
case AUDIOCMD_OP_GLOBAL_UNMUTE:
if (cmd->asUInt == 1) {
for (i = 0; i < gAudioCtx.numNotes; i++) {
Note* note = &gAudioCtx.notes[i];
NoteSubEu* subEu = ¬e->noteSubEu;
if (subEu->bitField0.enabled && (note->playbackState.unk_04 == 0) &&
(note->playbackState.parentLayer->channel->muteBehavior & MUTE_BEHAVIOR_3)) {
subEu->bitField0.finished = true;
}
}
}
for (i = 0; i < gAudioCtx.audioBufferParameters.numSequencePlayers; i++) {
SequencePlayer* seqPlayer = &gAudioCtx.seqPlayers[i];
seqPlayer->muted = false;
seqPlayer->recalculateVolume = true;
}
break;
case AUDIOCMD_OP_GLOBAL_SYNC_LOAD_INSTRUMENT:
AudioLoad_SyncLoadInstrument(cmd->arg0, cmd->arg1, cmd->arg2);
break;
case AUDIOCMD_OP_GLOBAL_ASYNC_LOAD_SAMPLE_BANK:
AudioLoad_AsyncLoadSampleBank(cmd->arg0, cmd->arg1, cmd->arg2, &gAudioCtx.externalLoadQueue);
break;
case AUDIOCMD_OP_GLOBAL_ASYNC_LOAD_FONT:
AudioLoad_AsyncLoadFont(cmd->arg0, cmd->arg1, cmd->arg2, &gAudioCtx.externalLoadQueue);
break;
case AUDIOCMD_OP_GLOBAL_ASYNC_LOAD_SEQ:
AudioLoad_AsyncLoadSeq(cmd->arg0, cmd->arg1, cmd->arg2, &gAudioCtx.externalLoadQueue);
break;
case AUDIOCMD_OP_GLOBAL_DISCARD_SEQ_FONTS:
AudioLoad_DiscardSeqFonts(cmd->arg1);
break;
case AUDIOCMD_OP_GLOBAL_SET_CHANNEL_MASK:
gAudioCtx.threadCmdChannelMask[cmd->arg0] = cmd->asUShort;
break;
case AUDIOCMD_OP_GLOBAL_RESET_AUDIO_HEAP:
gAudioCtx.resetStatus = 5;
gAudioCtx.specId = cmd->asUInt;
break;
case AUDIOCMD_OP_GLOBAL_SET_CUSTOM_UPDATE_FUNCTION:
gAudioCustomUpdateFunction = (AudioCustomUpdateFunction)cmd->asUInt;
break;
case AUDIOCMD_OP_GLOBAL_SET_DRUM_FONT:
case AUDIOCMD_OP_GLOBAL_SET_SFX_FONT:
case AUDIOCMD_OP_GLOBAL_SET_INSTRUMENT_FONT:
Audio_SetFontInstrument(cmd->op - AUDIOCMD_OP_GLOBAL_SET_DRUM_FONT, cmd->arg0, cmd->arg1, cmd->data);
break;
case AUDIOCMD_OP_GLOBAL_DISABLE_ALL_SEQPLAYERS:
flags = cmd->asUInt;
if (flags == 1) {
for (i = 0; i < gAudioCtx.audioBufferParameters.numSequencePlayers; i++) {
SequencePlayer* seqPlayer = &gAudioCtx.seqPlayers[i];
if (seqPlayer->enabled) {
AudioSeq_SequencePlayerDisableAsFinished(seqPlayer);
}
}
}
func_800E66C0(flags);
break;
case AUDIOCMD_OP_GLOBAL_POP_PERSISTENT_CACHE:
AudioHeap_PopPersistentCache(cmd->asInt);
break;
default:
break;
}
}
/**
* original name: __Nas_GroupFadeOut
*/
void AudioThread_SetFadeOutTimer(s32 seqPlayerIndex, s32 fadeTimer) {
SequencePlayer* seqPlayer = &gAudioCtx.seqPlayers[seqPlayerIndex];
if (fadeTimer == 0) {
fadeTimer = 1;
}
seqPlayer->fadeVelocity = -(seqPlayer->fadeVolume / fadeTimer);
seqPlayer->state = 2;
seqPlayer->fadeTimer = fadeTimer;
}
/**
* original name: __Nas_GroupFadeIn
*/
void AudioThread_SetFadeInTimer(s32 seqPlayerIndex, s32 fadeTimer) {
SequencePlayer* seqPlayer;
if (fadeTimer != 0) {
seqPlayer = &gAudioCtx.seqPlayers[seqPlayerIndex];
seqPlayer->state = 1;
seqPlayer->fadeTimerUnkEu = fadeTimer;
seqPlayer->fadeTimer = fadeTimer;
seqPlayer->fadeVolume = 0.0f;
seqPlayer->fadeVelocity = 0.0f;
}
}
/**
* original name: Nap_AudioPortInit
*/
void AudioThread_InitMesgQueuesImpl(void) {
gAudioCtx.threadCmdWritePos = 0;
gAudioCtx.threadCmdReadPos = 0;
gAudioCtx.threadCmdQueueFinished = false;
gAudioCtx.taskStartQueueP = &gAudioCtx.taskStartQueue;
gAudioCtx.threadCmdProcQueueP = &gAudioCtx.threadCmdProcQueue;
gAudioCtx.audioResetQueueP = &gAudioCtx.audioResetQueue;
osCreateMesgQueue(gAudioCtx.taskStartQueueP, gAudioCtx.taskStartMsgBuf, ARRAY_COUNT(gAudioCtx.taskStartMsgBuf));
osCreateMesgQueue(gAudioCtx.threadCmdProcQueueP, gAudioCtx.threadCmdProcMsgBuf,
ARRAY_COUNT(gAudioCtx.threadCmdProcMsgBuf));
osCreateMesgQueue(gAudioCtx.audioResetQueueP, gAudioCtx.audioResetMsgBuf, ARRAY_COUNT(gAudioCtx.audioResetMsgBuf));
}
/**
* original name: Nap_PortSet
*/
void AudioThread_QueueCmd(u32 opArgs, void** data) {
AudioCmd* cmd = &gAudioCtx.threadCmdBuf[gAudioCtx.threadCmdWritePos & 0xFF];
cmd->opArgs = opArgs;
cmd->data = *data;
gAudioCtx.threadCmdWritePos++;
if (gAudioCtx.threadCmdWritePos == gAudioCtx.threadCmdReadPos) {
gAudioCtx.threadCmdWritePos--;
}
}
/**
* original name: Nap_SetF32
*/
void AudioThread_QueueCmdF32(u32 opArgs, f32 data) {
AudioThread_QueueCmd(opArgs, (void**)&data);
}
/**
* original name: Nap_SetS32
*/
void AudioThread_QueueCmdS32(u32 opArgs, s32 data) {
AudioThread_QueueCmd(opArgs, (void**)&data);
}
/**
* original name: Nap_SetS8
*/
void AudioThread_QueueCmdS8(u32 opArgs, s8 data) {
u32 uData = data << 0x18;
AudioThread_QueueCmd(opArgs, (void**)&uData);
}
/**
* original name: Nap_SetU16
*/
void AudioThread_QueueCmdU16(u32 opArgs, u16 data) {
u32 uData = data << 0x10;
AudioThread_QueueCmd(opArgs, (void**)&uData);
}
/**
* original name: Nap_SendStart
*/
s32 AudioThread_ScheduleProcessCmds(void) {
static s32 D_801304E8 = 0;
s32 ret;
if (D_801304E8 < (u8)((gAudioCtx.threadCmdWritePos - gAudioCtx.threadCmdReadPos) + 0x100)) {
D_801304E8 = (u8)((gAudioCtx.threadCmdWritePos - gAudioCtx.threadCmdReadPos) + 0x100);
}
ret = osSendMesg(gAudioCtx.threadCmdProcQueueP,
(OSMesg)(((gAudioCtx.threadCmdReadPos & 0xFF) << 8) | (gAudioCtx.threadCmdWritePos & 0xFF)),
OS_MESG_NOBLOCK);
if (ret != -1) {
gAudioCtx.threadCmdReadPos = gAudioCtx.threadCmdWritePos;
ret = 0;
} else {
return -1;
}
return ret;
}
/**
* original name: Nap_FlushPort
*/
void AudioThread_ResetCmdQueue(void) {
gAudioCtx.threadCmdQueueFinished = false;
gAudioCtx.threadCmdReadPos = gAudioCtx.threadCmdWritePos;
}
/**
* original name: Nap_Process1Command
*/
void AudioThread_ProcessCmd(AudioCmd* cmd) {
SequencePlayer* seqPlayer;
u16 threadCmdChannelMask;
s32 channelIndex;
if ((cmd->op & 0xF0) == 0xF0) {
AudioThread_ProcessGlobalCmd(cmd);
return;
}
if (cmd->arg0 < gAudioCtx.audioBufferParameters.numSequencePlayers) {
seqPlayer = &gAudioCtx.seqPlayers[cmd->arg0];
if (cmd->op & 0x80) {
AudioThread_ProcessGlobalCmd(cmd);
return;
}
if (cmd->op & 0x40) {
AudioThread_ProcessSeqPlayerCmd(seqPlayer, cmd);
return;
}
if (cmd->arg1 < SEQ_NUM_CHANNELS) {
AudioThread_ProcessChannelCmd(seqPlayer->channels[cmd->arg1], cmd);
return;
}
if (cmd->arg1 == AUDIOCMD_ALL_CHANNELS) {
threadCmdChannelMask = gAudioCtx.threadCmdChannelMask[cmd->arg0];
for (channelIndex = 0; channelIndex < SEQ_NUM_CHANNELS; channelIndex++) {
if (threadCmdChannelMask & 1) {
AudioThread_ProcessChannelCmd(seqPlayer->channels[channelIndex], cmd);
}
threadCmdChannelMask = threadCmdChannelMask >> 1;
}
}
}
}
/**
* original name: Nap_AudioPortProcess
*/
void AudioThread_ProcessCmds(u32 msg) {
static u8 sCurCmdRdPos = 0;
AudioCmd* cmd;
u8 endPos;
if (!gAudioCtx.threadCmdQueueFinished) {
sCurCmdRdPos = msg >> 8;
}
while (true) {
endPos = msg & 0xFF;
if (sCurCmdRdPos == endPos) {
gAudioCtx.threadCmdQueueFinished = false;
return;
}
cmd = &gAudioCtx.threadCmdBuf[sCurCmdRdPos++ & 0xFF];
if (cmd->op == AUDIOCMD_OP_GLOBAL_STOP_AUDIOCMDS) {
gAudioCtx.threadCmdQueueFinished = true;
return;
}
AudioThread_ProcessCmd(cmd);
cmd->op = AUDIOCMD_OP_NOOP;
}
}
u32 func_800E5E20(u32* out) {
u32 sp1C;
if (osRecvMesg(&gAudioCtx.externalLoadQueue, (OSMesg*)&sp1C, OS_MESG_NOBLOCK) == -1) {
*out = 0;
return 0;
}
*out = sp1C & 0xFFFFFF;
return sp1C >> 0x18;
}
u8* AudioThread_GetFontsForSequence(s32 seqId, u32* outNumFonts) {
return AudioLoad_GetFontsForSequence(seqId, outNumFonts);
}
void Audio_GetSampleBankIdsOfFont(s32 fontId, u32* sampleBankId1, u32* sampleBankId2) {
*sampleBankId1 = gAudioCtx.soundFontList[fontId].sampleBankId1;
*sampleBankId2 = gAudioCtx.soundFontList[fontId].sampleBankId2;
}
/**
* original name: Nap_CheckSpecChange
*/
s32 func_800E5EDC(void) {
s32 pad;
s32 specId;
if (osRecvMesg(gAudioCtx.audioResetQueueP, (OSMesg*)&specId, OS_MESG_NOBLOCK) == -1) {
return 0;
} else if (gAudioCtx.specId != specId) {
return -1;
} else {
return 1;
}
}
/**
* original name: __ClearSpecChangeQ
*/
void func_800E5F34(void) {
// macro?
// clang-format off
s32 chk = -1; OSMesg msg; do {} while (osRecvMesg(gAudioCtx.audioResetQueueP, &msg, OS_MESG_NOBLOCK) != chk);
// clang-format on
}
/**
* original name: Nap_StartSpecChange
*/
s32 AudioThread_ResetAudioHeap(s32 specId) {
s32 resetStatus;
OSMesg msg;
s32 pad;
func_800E5F34();
resetStatus = gAudioCtx.resetStatus;
if (resetStatus != 0) {
AudioThread_ResetCmdQueue();
if (gAudioCtx.specId == specId) {
return -2;
} else if (resetStatus > 2) {
gAudioCtx.specId = specId;
return -3;
} else {
osRecvMesg(gAudioCtx.audioResetQueueP, &msg, OS_MESG_BLOCK);
}
}
func_800E5F34();
AUDIOCMD_GLOBAL_RESET_AUDIO_HEAP(specId);
return AudioThread_ScheduleProcessCmds();
}
/**
* original name: Nap_StartReset
*/
void AudioThread_PreNMIInternal(void) {
gAudioCtx.resetTimer = 1;
if (gAudioContextInitialized) {
AudioThread_ResetAudioHeap(0);
gAudioCtx.resetStatus = 0;
}
}
/**
* original name: Nap_ReadSubPort
*/
s8 AudioThread_GetChannelIO(s32 seqPlayerIndex, s32 channelIndex, s32 ioPort) {
SequencePlayer* seqPlayer = &gAudioCtx.seqPlayers[seqPlayerIndex];
SequenceChannel* channel;
if (seqPlayer->enabled) {
channel = seqPlayer->channels[channelIndex];
return channel->seqScriptIO[ioPort];
} else {
return SEQ_IO_VAL_NONE;
}
}
/**
* original name: Nap_ReadGrpPort
*/
s8 AudioThread_GetSeqPlayerIO(s32 seqPlayerIndex, s32 ioPort) {
return gAudioCtx.seqPlayers[seqPlayerIndex].seqScriptIO[ioPort];
}
void AudioThread_InitExternalPool(void* ramAddr, u32 size) {
AudioHeap_InitPool(&gAudioCtx.externalPool, ramAddr, size);
}
void AudioThread_ResetExternalPool(void) {
gAudioCtx.externalPool.startRamAddr = NULL;
}
/**
* original name: __SetGrpParam
*/
void AudioThread_ProcessSeqPlayerCmd(SequencePlayer* seqPlayer, AudioCmd* cmd) {
f32 fadeVolume;
switch (cmd->op) {
case AUDIOCMD_OP_SEQPLAYER_FADE_VOLUME_SCALE:
if (seqPlayer->fadeVolumeScale != cmd->asFloat) {
seqPlayer->fadeVolumeScale = cmd->asFloat;
seqPlayer->recalculateVolume = true;
}
break;
case AUDIOCMD_OP_SEQPLAYER_SET_TEMPO:
seqPlayer->tempo = cmd->asInt * SEQTICKS_PER_BEAT;
break;
case AUDIOCMD_OP_SEQPLAYER_CHANGE_TEMPO:
seqPlayer->tempoChange = cmd->asInt * SEQTICKS_PER_BEAT;
break;
case AUDIOCMD_OP_SEQPLAYER_CHANGE_TEMPO_SEQTICKS:
seqPlayer->tempoChange = cmd->asInt;
break;
case AUDIOCMD_OP_SEQPLAYER_SET_TRANSPOSITION:
seqPlayer->transposition = cmd->asSbyte;
break;
case AUDIOCMD_OP_SEQPLAYER_SET_IO:
seqPlayer->seqScriptIO[cmd->arg2] = cmd->asSbyte;
break;
case AUDIOCMD_OP_SEQPLAYER_FADE_TO_SET_VOLUME:
fadeVolume = (s32)cmd->arg1 / 127.0f;
goto apply_fade;
case AUDIOCMD_OP_SEQPLAYER_FADE_TO_SCALED_VOLUME:
fadeVolume = ((s32)cmd->arg1 / 100.0f) * seqPlayer->fadeVolume;
apply_fade:
if (seqPlayer->state != 2) {
seqPlayer->volume = seqPlayer->fadeVolume;
if (cmd->asInt == 0) {
seqPlayer->fadeVolume = fadeVolume;
} else {
s32 fadeTimer = cmd->asInt;
seqPlayer->state = 0;
seqPlayer->fadeTimer = fadeTimer;
seqPlayer->fadeVelocity = (fadeVolume - seqPlayer->fadeVolume) / fadeTimer;
}
}
break;
case AUDIOCMD_OP_SEQPLAYER_RESET_VOLUME:
if (seqPlayer->state != 2) {
if (cmd->asInt == 0) {
seqPlayer->fadeVolume = seqPlayer->volume;
} else {
s32 fadeTimer = cmd->asInt;
seqPlayer->state = 0;
seqPlayer->fadeTimer = fadeTimer;
seqPlayer->fadeVelocity = (seqPlayer->volume - seqPlayer->fadeVolume) / fadeTimer;
}
}
break;
case AUDIOCMD_OP_SEQPLAYER_SET_BEND:
seqPlayer->bend = cmd->asFloat;
if (seqPlayer->bend == 1.0f) {
seqPlayer->applyBend = false;
} else {
seqPlayer->applyBend = true;
}
break;
default:
break;
}
}
/**
* original name: __SetSubParam
*/
void AudioThread_ProcessChannelCmd(SequenceChannel* channel, AudioCmd* cmd) {
switch (cmd->op) {
case AUDIOCMD_OP_CHANNEL_SET_VOL_SCALE:
if (channel->volumeScale != cmd->asFloat) {
channel->volumeScale = cmd->asFloat;
channel->changes.s.volume = true;
}
break;
case AUDIOCMD_OP_CHANNEL_SET_VOL:
if (channel->volume != cmd->asFloat) {
channel->volume = cmd->asFloat;
channel->changes.s.volume = true;
}
break;
case AUDIOCMD_OP_CHANNEL_SET_PAN:
if (channel->newPan != cmd->asSbyte) {
channel->newPan = cmd->asSbyte;
channel->changes.s.pan = true;
}
break;
case AUDIOCMD_OP_CHANNEL_SET_PAN_WEIGHT:
//! @bug: Should compare `asSbyte` to `panChannelWeight`
if (channel->newPan != cmd->asSbyte) {
channel->panChannelWeight = cmd->asSbyte;
channel->changes.s.pan = true;
}
break;
case AUDIOCMD_OP_CHANNEL_SET_FREQ_SCALE:
if (channel->freqScale != cmd->asFloat) {
channel->freqScale = cmd->asFloat;
channel->changes.s.freqScale = true;
}
break;
case AUDIOCMD_OP_CHANNEL_SET_REVERB_VOLUME:
if (channel->targetReverbVol != cmd->asSbyte) {
channel->targetReverbVol = cmd->asSbyte;
}
break;
case AUDIOCMD_OP_CHANNEL_SET_IO:
if (cmd->arg2 < ARRAY_COUNT(channel->seqScriptIO)) {
channel->seqScriptIO[cmd->arg2] = cmd->asSbyte;
}
break;
case AUDIOCMD_OP_CHANNEL_SET_MUTE:
channel->muted = cmd->asSbyte;
break;
case AUDIOCMD_OP_CHANNEL_SET_MUTE_BEHAVIOR:
channel->muteBehavior = cmd->asSbyte;
break;
case AUDIOCMD_OP_CHANNEL_SET_VIBRATO_DEPTH:
channel->vibratoDepthTarget = cmd->asUbyte * 8;
channel->vibratoDepthChangeDelay = 1;
break;
case AUDIOCMD_OP_CHANNEL_SET_VIBRATO_RATE:
channel->vibratoRateTarget = cmd->asUbyte * 32;
channel->vibratoRateChangeDelay = 1;
break;
case AUDIOCMD_OP_CHANNEL_SET_COMB_FILTER_SIZE:
channel->combFilterSize = cmd->asUbyte;
break;
case AUDIOCMD_OP_CHANNEL_SET_COMB_FILTER_GAIN:
channel->combFilterGain = cmd->asUShort;
break;
case AUDIOCMD_OP_CHANNEL_SET_STEREO:
channel->stereo.asByte = cmd->asUbyte;
break;
default:
break;
}
}
/**
* Call an audio-thread command that has no code to process it. Unused.
*/
void AudioThread_Noop1Cmd(s32 arg0, s32 arg1, s32 arg2) {
AUDIOCMD_GLOBAL_NOOP_1(arg0, arg1, arg2, 1);
}
/**
* Call an audio-thread command that has no code to process it. Unused.
*/
void AudioThread_Noop1CmdZeroed(void) {
AUDIOCMD_GLOBAL_NOOP_1(0, 0, 0, 0);
}
/**
* Call an audio-thread command that has no code to process it. Unused.
*/
void AudioThread_Noop2Cmd(u32 arg0, s32 arg1) {
AUDIOCMD_GLOBAL_NOOP_2(0, 0, arg1, arg0);
}
/**
* original name: Nap_WaitVsync
*/
void AudioThread_WaitForAudioTask(void) {
osRecvMesg(gAudioCtx.taskStartQueueP, NULL, OS_MESG_NOBLOCK);
osRecvMesg(gAudioCtx.taskStartQueueP, NULL, OS_MESG_BLOCK);
}
s32 func_800E6590(s32 seqPlayerIndex, s32 channelIndex, s32 layerIndex) {
SequencePlayer* seqPlayer;
SequenceLayer* layer;
Note* note;
TunedSample* tunedSample;
s32 loopEnd;
s32 samplePos;
seqPlayer = &gAudioCtx.seqPlayers[seqPlayerIndex];
if (seqPlayer->enabled && seqPlayer->channels[channelIndex]->enabled) {
layer = seqPlayer->channels[channelIndex]->layers[layerIndex];
if (layer == NULL) {
return 0;
}
if (layer->enabled) {
if (layer->note == NULL) {
return 0;
}
if (!layer->bit3) {
return 0;
}
note = layer->note;
if (layer == note->playbackState.parentLayer) {
tunedSample = note->noteSubEu.tunedSample;
if (tunedSample == NULL) {
return 0;
}
loopEnd = tunedSample->sample->loop->header.end;
samplePos = note->synthesisState.samplePosInt;
return loopEnd - samplePos;
}
return 0;
}
}
return 0;
}
/**
* original name possibly "Nap_SilenceCheck"
*/
s32 func_800E6680(void) {
return func_800E66C0(0);
}
void func_800E66A0(void) {
func_800E66C0(2);
}
/**
* original name: Nap_SilenceCheck_Inner
*/
s32 func_800E66C0(s32 flags) {
s32 phi_v1;
NotePlaybackState* playbackState;
NoteSubEu* noteSubEu;
s32 i;
Note* note;
TunedSample* tunedSample;
phi_v1 = 0;
for (i = 0; i < gAudioCtx.numNotes; i++) {
note = &gAudioCtx.notes[i];
playbackState = ¬e->playbackState;
if (note->noteSubEu.bitField0.enabled) {
noteSubEu = ¬e->noteSubEu;
if (playbackState->adsr.action.s.state != 0) {
if (flags >= 2) {
tunedSample = noteSubEu->tunedSample;
if (tunedSample == NULL || noteSubEu->bitField1.isSyntheticWave) {
continue;
}
if (tunedSample->sample->medium == MEDIUM_RAM) {
continue;
}
}
phi_v1++;
if ((flags & 1) == 1) {
playbackState->adsr.fadeOutVel = gAudioCtx.audioBufferParameters.ticksPerUpdateInv;
playbackState->adsr.action.s.release = 1;
}
}
}
}
return phi_v1;
}
/**
* original name: Nap_GetRandom
*/
u32 AudioThread_NextRandom(void) {
static u32 sAudioRandom = 0x12345678;
sAudioRandom = ((osGetCount() + 0x1234567) * (sAudioRandom + gAudioCtx.totalTaskCount));
sAudioRandom += gAudioCtx.audioRandom;
return sAudioRandom;
}
/**
* original name: Nas_InitGAudio
*/
void AudioThread_InitMesgQueues(void) {
AudioThread_InitMesgQueuesImpl();
}
|