sm64

A Super Mario 64 decompilation
Log | Files | Refs | README | LICENSE

playback.c (52213B)


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