sm64

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

seqplayer.c (106639B)


      1 #include <PR/ultratypes.h>
      2 
      3 #include "data.h"
      4 #include "effects.h"
      5 #include "external.h"
      6 #include "heap.h"
      7 #include "load.h"
      8 #include "seqplayer.h"
      9 
     10 #define PORTAMENTO_IS_SPECIAL(x) ((x).mode & 0x80)
     11 #define PORTAMENTO_MODE(x) ((x).mode & ~0x80)
     12 #define PORTAMENTO_MODE_1 1
     13 #define PORTAMENTO_MODE_2 2
     14 #define PORTAMENTO_MODE_3 3
     15 #define PORTAMENTO_MODE_4 4
     16 #define PORTAMENTO_MODE_5 5
     17 
     18 #if defined(VERSION_SH) || defined(VERSION_CN)
     19 void seq_channel_layer_process_script_part1(struct SequenceChannelLayer *layer);
     20 s32 seq_channel_layer_process_script_part2(struct SequenceChannelLayer *layer);
     21 s32 seq_channel_layer_process_script_part3(struct SequenceChannelLayer *layer, s32 cmd);
     22 s32 seq_channel_layer_process_script_part4(struct SequenceChannelLayer *layer, s32 cmd);
     23 s32 seq_channel_layer_process_script_part5(struct SequenceChannelLayer *layer, s32 cmd);
     24 #endif
     25 void seq_channel_layer_process_script(struct SequenceChannelLayer *layer);
     26 void sequence_channel_process_script(struct SequenceChannel *seqChannel);
     27 u8 get_instrument(struct SequenceChannel *seqChannel, u8 instId, struct Instrument **instOut,
     28                   struct AdsrSettings *adsr);
     29 
     30 void sequence_channel_init(struct SequenceChannel *seqChannel) {
     31     s32 i;
     32 
     33     seqChannel->enabled = FALSE;
     34     seqChannel->finished = FALSE;
     35     seqChannel->stopScript = FALSE;
     36     seqChannel->stopSomething2 = FALSE;
     37     seqChannel->hasInstrument = FALSE;
     38     seqChannel->stereoHeadsetEffects = FALSE;
     39     seqChannel->transposition = 0;
     40     seqChannel->largeNotes = FALSE;
     41 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
     42     seqChannel->bookOffset = 0;
     43     seqChannel->changes.as_u8 = 0xff;
     44     seqChannel->scriptState.depth = 0;
     45     seqChannel->newPan = 0x40;
     46     seqChannel->panChannelWeight = 0x80;
     47     seqChannel->noteUnused = NULL;
     48     seqChannel->reverbIndex = 0;
     49 #else
     50     seqChannel->scriptState.depth = 0;
     51     seqChannel->volume = 1.0f;
     52     seqChannel->volumeScale = 1.0f;
     53     seqChannel->freqScale = 1.0f;
     54     seqChannel->pan = 0.5f;
     55     seqChannel->panChannelWeight = 1.0f;
     56     seqChannel->noteUnused = NULL;
     57 #endif
     58     seqChannel->reverbVol = 0;
     59 #if defined(VERSION_SH) || defined(VERSION_CN)
     60     seqChannel->synthesisVolume = 0;
     61 #endif
     62     seqChannel->notePriority = NOTE_PRIORITY_DEFAULT;
     63 #if defined(VERSION_SH) || defined(VERSION_CN)
     64     seqChannel->unkSH06 = 1;
     65 #endif
     66     seqChannel->delay = 0;
     67     seqChannel->adsr.envelope = gDefaultEnvelope;
     68     seqChannel->adsr.releaseRate = 0x20;
     69     seqChannel->adsr.sustain = 0;
     70 #if defined(VERSION_JP) || defined(VERSION_US)
     71     seqChannel->updatesPerFrameUnused = gAudioUpdatesPerFrame;
     72 #endif
     73     seqChannel->vibratoRateTarget = 0x800;
     74     seqChannel->vibratoRateStart = 0x800;
     75     seqChannel->vibratoExtentTarget = 0;
     76     seqChannel->vibratoExtentStart = 0;
     77     seqChannel->vibratoRateChangeDelay = 0;
     78     seqChannel->vibratoExtentChangeDelay = 0;
     79     seqChannel->vibratoDelay = 0;
     80 #if defined(VERSION_SH) || defined(VERSION_CN)
     81     seqChannel->filter = NULL;
     82 #endif
     83 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
     84     seqChannel->volume = 1.0f;
     85     seqChannel->volumeScale = 1.0f;
     86     seqChannel->freqScale = 1.0f;
     87 #endif
     88 
     89     for (i = 0; i < 8; i++) {
     90         seqChannel->soundScriptIO[i] = -1;
     91     }
     92 
     93     seqChannel->unused = FALSE;
     94     init_note_lists(&seqChannel->notePool);
     95 }
     96 
     97 s32 seq_channel_set_layer(struct SequenceChannel *seqChannel, s32 layerIndex) {
     98     struct SequenceChannelLayer *layer;
     99 
    100     if (seqChannel->layers[layerIndex] == NULL) {
    101 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    102         struct SequenceChannelLayer *layer;
    103 #endif
    104         layer = audio_list_pop_back(&gLayerFreeList);
    105         seqChannel->layers[layerIndex] = layer;
    106         if (layer == NULL) {
    107             seqChannel->layers[layerIndex] = NULL;
    108             return -1;
    109         }
    110     } else {
    111         seq_channel_layer_note_decay(seqChannel->layers[layerIndex]);
    112     }
    113 
    114     layer = seqChannel->layers[layerIndex];
    115     layer->seqChannel = seqChannel;
    116     layer->adsr = seqChannel->adsr;
    117     layer->adsr.releaseRate = 0;
    118     layer->enabled = TRUE;
    119     layer->stopSomething = FALSE;
    120     layer->continuousNotes = FALSE;
    121     layer->finished = FALSE;
    122 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    123     layer->ignoreDrumPan = FALSE;
    124 #endif
    125 #if defined(VERSION_SH) || defined(VERSION_CN)
    126     layer->reverbBits.asByte = 0x40;
    127 #endif
    128     layer->portamento.mode = 0;
    129     layer->scriptState.depth = 0;
    130     layer->status = SOUND_LOAD_STATUS_NOT_LOADED;
    131     layer->noteDuration = 0x80;
    132 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    133     layer->pan = 0x40;
    134 #endif
    135     layer->transposition = 0;
    136     layer->delay = 0;
    137     layer->duration = 0;
    138     layer->delayUnused = 0;
    139     layer->note = NULL;
    140     layer->instrument = NULL;
    141 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    142     layer->freqScale = 1.0f;
    143     layer->velocitySquare = 0.0f;
    144 #if defined(VERSION_SH) || defined(VERSION_CN)
    145     layer->freqScaleMultiplier = 1.0f;
    146 #endif
    147     layer->instOrWave = 0xff;
    148 #else
    149     layer->velocitySquare = 0.0f;
    150     layer->pan = 0.5f;
    151 #endif
    152     return 0;
    153 }
    154 
    155 void seq_channel_layer_disable(struct SequenceChannelLayer *layer) {
    156     if (layer != NULL) {
    157         seq_channel_layer_note_decay(layer);
    158         layer->enabled = FALSE;
    159         layer->finished = TRUE;
    160     }
    161 }
    162 
    163 void seq_channel_layer_free(struct SequenceChannel *seqChannel, s32 layerIndex) {
    164     struct SequenceChannelLayer *layer = seqChannel->layers[layerIndex];
    165 
    166     if (layer != NULL) {
    167 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    168         audio_list_push_back(&gLayerFreeList, &layer->listItem);
    169 #else
    170         struct AudioListItem *item = &layer->listItem;
    171         if (item->prev == NULL) {
    172             gLayerFreeList.prev->next = item;
    173             item->prev = gLayerFreeList.prev;
    174             item->next = &gLayerFreeList;
    175             gLayerFreeList.prev = item;
    176             gLayerFreeList.u.count++;
    177             item->pool = gLayerFreeList.pool;
    178         }
    179 #endif
    180         seq_channel_layer_disable(layer);
    181         seqChannel->layers[layerIndex] = NULL;
    182     }
    183 }
    184 
    185 void sequence_channel_disable(struct SequenceChannel *seqChannel) {
    186     s32 i;
    187     for (i = 0; i < LAYERS_MAX; i++) {
    188         seq_channel_layer_free(seqChannel, i);
    189     }
    190 
    191     note_pool_clear(&seqChannel->notePool);
    192     seqChannel->enabled = FALSE;
    193     seqChannel->finished = TRUE;
    194 }
    195 
    196 struct SequenceChannel *allocate_sequence_channel(void) {
    197     s32 i;
    198     for (i = 0; i < ARRAY_COUNT(gSequenceChannels); i++) {
    199         if (gSequenceChannels[i].seqPlayer == NULL) {
    200 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    201             return &gSequenceChannels[i];
    202 #else
    203             return gSequenceChannels + i;
    204 #endif
    205         }
    206     }
    207     return &gSequenceChannelNone;
    208 }
    209 
    210 void sequence_player_init_channels(struct SequencePlayer *seqPlayer, u16 channelBits) {
    211     struct SequenceChannel *seqChannel;
    212     s32 i;
    213 
    214     for (i = 0; i < CHANNELS_MAX; i++) {
    215         if (channelBits & 1) {
    216             seqChannel = seqPlayer->channels[i];
    217             if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == TRUE && seqChannel->seqPlayer == seqPlayer) {
    218                 sequence_channel_disable(seqChannel);
    219                 seqChannel->seqPlayer = NULL;
    220             }
    221             seqChannel = allocate_sequence_channel();
    222             if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == FALSE) {
    223                 eu_stubbed_printf_0("Audio:Track:Warning: No Free Notetrack\n");
    224                 gAudioErrorFlags = i + 0x10000;
    225                 seqPlayer->channels[i] = seqChannel;
    226             } else {
    227                 sequence_channel_init(seqChannel);
    228                 seqPlayer->channels[i] = seqChannel;
    229                 seqChannel->seqPlayer = seqPlayer;
    230                 seqChannel->bankId = seqPlayer->defaultBank[0];
    231                 seqChannel->muteBehavior = seqPlayer->muteBehavior;
    232                 seqChannel->noteAllocPolicy = seqPlayer->noteAllocPolicy;
    233             }
    234         }
    235 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    236         channelBits = channelBits >> 1;
    237 #else
    238         channelBits >>= 1;
    239 #endif
    240     }
    241 }
    242 
    243 void sequence_player_disable_channels(struct SequencePlayer *seqPlayer, u16 channelBits) {
    244     struct SequenceChannel *seqChannel;
    245     s32 i;
    246 
    247     eu_stubbed_printf_0("SUBTRACK DIM\n");
    248     for (i = 0; i < CHANNELS_MAX; i++) {
    249         if (channelBits & 1) {
    250             seqChannel = seqPlayer->channels[i];
    251             if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == TRUE) {
    252                 if (seqChannel->seqPlayer == seqPlayer) {
    253                     sequence_channel_disable(seqChannel);
    254                     seqChannel->seqPlayer = NULL;
    255                 }
    256 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    257                 else {
    258 #ifdef VERSION_EU
    259                     stubbed_printf("Audio:Track: Warning SUBTRACK PARENT CHANGED\n");
    260 #endif
    261                 }
    262 #endif
    263                 seqPlayer->channels[i] = &gSequenceChannelNone;
    264             }
    265         }
    266 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    267         channelBits = channelBits >> 1;
    268 #else
    269         channelBits >>= 1;
    270 #endif
    271     }
    272 }
    273 
    274 void sequence_channel_enable(struct SequencePlayer *seqPlayer, u8 channelIndex, void *script) {
    275     struct SequenceChannel *seqChannel = seqPlayer->channels[channelIndex];
    276     s32 i;
    277     if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == FALSE) {
    278 #ifdef VERSION_EU
    279         struct SequencePlayer *bgMusic = &gSequencePlayers[0];
    280         struct SequencePlayer *miscMusic = &gSequencePlayers[1];
    281 
    282         if (seqPlayer == bgMusic) {
    283             stubbed_printf("GROUP 0:");
    284         } else if (seqPlayer == miscMusic) {
    285             stubbed_printf("GROUP 1:");
    286         } else {
    287             stubbed_printf("SEQID %d,BANKID %d\n",
    288                     seqPlayer->seqId, seqPlayer->defaultBank[0]);
    289         }
    290         stubbed_printf("ERR:SUBTRACK %d NOT ALLOCATED\n", channelIndex);
    291 #endif
    292     } else {
    293         seqChannel->enabled = TRUE;
    294         seqChannel->finished = FALSE;
    295         seqChannel->scriptState.depth = 0;
    296         seqChannel->scriptState.pc = script;
    297         seqChannel->delay = 0;
    298         for (i = 0; i < LAYERS_MAX; i++) {
    299             if (seqChannel->layers[i] != NULL) {
    300                 seq_channel_layer_free(seqChannel, i);
    301             }
    302         }
    303     }
    304 }
    305 
    306 void sequence_player_disable(struct SequencePlayer *seqPlayer) {
    307     sequence_player_disable_channels(seqPlayer, 0xffff);
    308     note_pool_clear(&seqPlayer->notePool);
    309     seqPlayer->finished = TRUE;
    310     seqPlayer->enabled = FALSE;
    311 
    312     if (IS_SEQ_LOAD_COMPLETE(seqPlayer->seqId)
    313 #if defined(VERSION_SH) || defined(VERSION_CN)
    314         && gSeqLoadStatus[seqPlayer->seqId] != 5
    315 #endif
    316     ) {
    317         gSeqLoadStatus[seqPlayer->seqId] = SOUND_LOAD_STATUS_DISCARDABLE;
    318     }
    319 
    320     if (IS_BANK_LOAD_COMPLETE(seqPlayer->defaultBank[0])
    321 #if defined(VERSION_SH) || defined(VERSION_CN)
    322         && gBankLoadStatus[seqPlayer->defaultBank[0]] != 5
    323 #endif
    324     ) {
    325 #if defined(VERSION_SH) || defined(VERSION_CN)
    326         gBankLoadStatus[seqPlayer->defaultBank[0]] = 4;
    327 #else
    328         gBankLoadStatus[seqPlayer->defaultBank[0]] = SOUND_LOAD_STATUS_DISCARDABLE;
    329 #endif
    330     }
    331 
    332     // (Note that if this is called from alloc_bank_or_seq, the side will get swapped
    333     // later in that function. Thus, we signal that we want to load into the slot
    334     // of the bank that we no longer need.)
    335 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    336     if (seqPlayer->defaultBank[0] == gBankLoadedPool.temporary.entries[0].id) {
    337         gBankLoadedPool.temporary.nextSide = 1;
    338     } else if (seqPlayer->defaultBank[0] == gBankLoadedPool.temporary.entries[1].id) {
    339         gBankLoadedPool.temporary.nextSide = 0;
    340     }
    341 #else
    342     if (gBankLoadedPool.temporary.entries[0].id == seqPlayer->defaultBank[0]) {
    343         gBankLoadedPool.temporary.nextSide = 1;
    344     } else if (gBankLoadedPool.temporary.entries[1].id == seqPlayer->defaultBank[0]) {
    345         gBankLoadedPool.temporary.nextSide = 0;
    346     }
    347 #endif
    348 }
    349 
    350 /**
    351  * Add an item to the end of a list, if it's not already in any list.
    352  */
    353 void audio_list_push_back(struct AudioListItem *list, struct AudioListItem *item) {
    354     if (item->prev != NULL) {
    355         eu_stubbed_printf_0("Error:Same List Add\n");
    356     } else {
    357         list->prev->next = item;
    358         item->prev = list->prev;
    359         item->next = list;
    360         list->prev = item;
    361         list->u.count++;
    362         item->pool = list->pool;
    363     }
    364 }
    365 
    366 /**
    367  * Remove the last item from a list, and return it (or NULL if empty).
    368  */
    369 void *audio_list_pop_back(struct AudioListItem *list) {
    370     struct AudioListItem *item = list->prev;
    371     if (item == list) {
    372         return NULL;
    373     }
    374     item->prev->next = list;
    375     list->prev = item->prev;
    376     item->prev = NULL;
    377     list->u.count--;
    378     return item->u.value;
    379 }
    380 
    381 void init_layer_freelist(void) {
    382     s32 i;
    383 
    384     gLayerFreeList.prev = &gLayerFreeList;
    385     gLayerFreeList.next = &gLayerFreeList;
    386     gLayerFreeList.u.count = 0;
    387     gLayerFreeList.pool = NULL;
    388 
    389     for (i = 0; i < ARRAY_COUNT(gSequenceLayers); i++) {
    390 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    391         gSequenceLayers[i].listItem.u.value = &gSequenceLayers[i];
    392 #else
    393         gSequenceLayers[i].listItem.u.value = gSequenceLayers + i;
    394 #endif
    395         gSequenceLayers[i].listItem.prev = NULL;
    396         audio_list_push_back(&gLayerFreeList, &gSequenceLayers[i].listItem);
    397     }
    398 }
    399 
    400 u8 m64_read_u8(struct M64ScriptState *state) {
    401 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    402     return *(state->pc++);
    403 #else
    404     u8 *midiArg = state->pc++;
    405     return *midiArg;
    406 #endif
    407 }
    408 
    409 s16 m64_read_s16(struct M64ScriptState *state) {
    410     s16 ret = *(state->pc++) << 8;
    411     ret = *(state->pc++) | ret;
    412     return ret;
    413 }
    414 
    415 u16 m64_read_compressed_u16(struct M64ScriptState *state) {
    416     u16 ret = *(state->pc++);
    417     if (ret & 0x80) {
    418         ret = (ret << 8) & 0x7f00;
    419         ret = *(state->pc++) | ret;
    420     }
    421     return ret;
    422 }
    423 
    424 #if defined(VERSION_SH) || defined(VERSION_CN)
    425 void seq_channel_layer_process_script(struct SequenceChannelLayer *layer) {
    426     s32 cmd;
    427 
    428     if (layer->enabled == FALSE) {
    429         return;
    430     }
    431 
    432     if (layer->delay > 1) {
    433         layer->delay--;
    434         if (!layer->stopSomething && layer->delay <= layer->duration) {
    435             seq_channel_layer_note_decay(layer);
    436             layer->stopSomething = TRUE;
    437         }
    438         return;
    439     }
    440 
    441     seq_channel_layer_process_script_part1(layer);
    442     cmd = seq_channel_layer_process_script_part2(layer);
    443     if (cmd != -1) {
    444         cmd = seq_channel_layer_process_script_part3(layer, cmd);
    445         if (cmd != -1) {
    446             cmd = seq_channel_layer_process_script_part4(layer, cmd);
    447         }
    448         if (cmd != -1) {
    449             seq_channel_layer_process_script_part5(layer, cmd);
    450         }
    451 
    452         if (layer->stopSomething == TRUE) {
    453             if (layer->note != NULL || layer->continuousNotes) {
    454                 seq_channel_layer_note_decay(layer);
    455             }
    456         }
    457     }
    458 }
    459 #elif defined(VERSION_EU)
    460 void seq_channel_layer_process_script(struct SequenceChannelLayer *layer) {
    461     struct SequencePlayer *seqPlayer;
    462     struct SequenceChannel *seqChannel;
    463 #ifdef VERSION_EU
    464     UNUSED u32 pad0;
    465 #endif
    466     struct M64ScriptState *state;
    467     struct Portamento *portamento;
    468     struct AudioBankSound *sound;
    469     struct Instrument *instrument;
    470     struct Drum *drum;
    471     s32 temp_a0_5;
    472 #ifdef VERSION_EU
    473     u16 sp3A;
    474     s32 sameSound;
    475 #endif
    476     UNUSED u32 pad1;
    477 #ifndef VERSION_EU
    478     u8 sameSound;
    479 #endif
    480     u8 cmd;
    481     UNUSED u8 cmdSemitone;
    482 #ifndef VERSION_EU
    483     u16 sp3A;
    484 #endif
    485     f32 tuning;
    486     s32 vel;
    487     UNUSED s32 usedSemitone;
    488     f32 freqScale;
    489 #ifndef VERSION_EU
    490     UNUSED f32 sp24;
    491 #endif
    492     f32 temp_f12;
    493     f32 temp_f2;
    494 
    495     sameSound = TRUE;
    496     if (layer->enabled == FALSE) {
    497         return;
    498     }
    499 
    500     if (layer->delay > 1) {
    501         layer->delay--;
    502         if (!layer->stopSomething && layer->delay <= layer->duration) {
    503             seq_channel_layer_note_decay(layer);
    504             layer->stopSomething = TRUE;
    505         }
    506         return;
    507     }
    508 
    509     if (!layer->continuousNotes) {
    510         seq_channel_layer_note_decay(layer);
    511     }
    512 
    513     if (PORTAMENTO_MODE(layer->portamento) == PORTAMENTO_MODE_1 ||
    514         PORTAMENTO_MODE(layer->portamento) == PORTAMENTO_MODE_2) {
    515         layer->portamento.mode = 0;
    516     }
    517 
    518     seqChannel = layer->seqChannel;
    519     seqPlayer = seqChannel->seqPlayer;
    520 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    521     layer->notePropertiesNeedInit = TRUE;
    522 #endif
    523 
    524     for (;;) {
    525         state = &layer->scriptState;
    526         cmd = m64_read_u8(state);
    527 
    528         if (cmd <= 0xc0) {
    529             break;
    530         }
    531 
    532         switch (cmd) {
    533             case 0xff: // layer_end; function return or end of script
    534                 if (state->depth == 0) {
    535                     // N.B. this function call is *not* inlined even though it's
    536                     // within the same file, unlike in the rest of this function.
    537                     seq_channel_layer_disable(layer);
    538                     return;
    539                 }
    540                 state->pc = state->stack[--state->depth];
    541                 break;
    542 
    543             case 0xfc: // layer_call
    544                 if (0 && state->depth >= 4) {
    545                     eu_stubbed_printf_0("Macro Level Over Error!\n");
    546                 }
    547                 sp3A = m64_read_s16(state);
    548                 state->stack[state->depth++] = state->pc;
    549                 state->pc = seqPlayer->seqData + sp3A;
    550                 break;
    551 
    552             case 0xf8: // layer_loop; loop start, N iterations (or 256 if N = 0)
    553                 if (0 && state->depth >= 4) {
    554                     eu_stubbed_printf_0("Macro Level Over Error!\n");
    555                 }
    556                 state->remLoopIters[state->depth] = m64_read_u8(state);
    557                 state->stack[state->depth++] = state->pc;
    558                 break;
    559 
    560             case 0xf7: // layer_loopend
    561                 if (--state->remLoopIters[state->depth - 1] != 0) {
    562                     state->pc = state->stack[state->depth - 1];
    563                 } else {
    564                     state->depth--;
    565                 }
    566                 break;
    567 
    568             case 0xfb: // layer_jump
    569                 sp3A = m64_read_s16(state);
    570                 state->pc = seqPlayer->seqData + sp3A;
    571                 break;
    572 
    573 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    574             case 0xf4:
    575                 state->pc += (s8)m64_read_u8(state);
    576                 break;
    577 #endif
    578 
    579             case 0xc1: // layer_setshortnotevelocity
    580             case 0xca: // layer_setpan
    581                 temp_a0_5 = *(state->pc++);
    582                 if (cmd == 0xc1) {
    583                     layer->velocitySquare = (f32)(temp_a0_5 * temp_a0_5);
    584                 } else {
    585 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    586                     layer->pan = temp_a0_5;
    587 #else
    588                     layer->pan = (f32) temp_a0_5 / US_FLOAT(128.0);
    589 #endif
    590                 }
    591                 break;
    592 
    593             case 0xc2: // layer_transpose; set transposition in semitones
    594             case 0xc9: // layer_setshortnoteduration
    595                 temp_a0_5 = *(state->pc++);
    596                 if (cmd == 0xc9) {
    597                     layer->noteDuration = temp_a0_5;
    598                 } else {
    599                     layer->transposition = temp_a0_5;
    600                 }
    601                 break;
    602 
    603             case 0xc4: // layer_somethingon
    604             case 0xc5: // layer_somethingoff
    605                 if (cmd == 0xc4) {
    606                     layer->continuousNotes = TRUE;
    607                 } else {
    608                     layer->continuousNotes = FALSE;
    609                 }
    610                 seq_channel_layer_note_decay(layer);
    611                 break;
    612 
    613             case 0xc3: // layer_setshortnotedefaultplaypercentage
    614                 sp3A = m64_read_compressed_u16(state);
    615                 layer->shortNoteDefaultPlayPercentage = sp3A;
    616                 break;
    617 
    618             case 0xc6: // layer_setinstr
    619                 cmd = m64_read_u8(state);
    620 #if defined(VERSION_JP) || defined(VERSION_US)
    621                 if (cmd < 127) {
    622                     cmd = get_instrument(seqChannel, cmd, &layer->instrument, &layer->adsr);
    623                 }
    624 #else
    625                 if (cmd >= 0x7f) {
    626                     if (cmd == 0x7f) {
    627                         layer->instOrWave = 0;
    628                     } else {
    629                         layer->instOrWave = cmd;
    630                         layer->instrument = NULL;
    631                     }
    632 
    633                     if (1) {
    634                     }
    635 
    636                     if (cmd == 0xff) {
    637                         layer->adsr.releaseRate = 0;
    638                     }
    639                     break;
    640                 }
    641 
    642                 if ((layer->instOrWave = get_instrument(seqChannel, cmd, &layer->instrument, &layer->adsr)) == 0) {
    643                     eu_stubbed_printf_1("WARNING: NPRG: cannot change %d\n", cmd);
    644                     layer->instOrWave = 0xff;
    645                 }
    646 #endif
    647                 break;
    648 
    649             case 0xc7: // layer_portamento
    650                 layer->portamento.mode = m64_read_u8(state);
    651 
    652                 // cmd is reused for the portamento's semitone
    653                 cmd = m64_read_u8(state) + seqChannel->transposition +
    654                     layer->transposition + seqPlayer->transposition;
    655 
    656                 if (cmd >= 0x80) {
    657                     cmd = 0;
    658                 }
    659 
    660                 layer->portamentoTargetNote = cmd;
    661 
    662                 // If special, the next param is u8 instead of var
    663                 if (PORTAMENTO_IS_SPECIAL(layer->portamento)) {
    664                     layer->portamentoTime = *((state)->pc++);
    665                     break;
    666                 }
    667 
    668                 sp3A = m64_read_compressed_u16(state);
    669                 layer->portamentoTime = sp3A;
    670                 break;
    671 
    672             case 0xc8: // layer_disableportamento
    673                 layer->portamento.mode = 0;
    674                 break;
    675 
    676 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    677             case 0xcb:
    678                 sp3A = m64_read_s16(state);
    679                 layer->adsr.envelope = (struct AdsrEnvelope *) (seqPlayer->seqData + sp3A);
    680                 layer->adsr.releaseRate = m64_read_u8(state);
    681                 break;
    682 
    683             case 0xcc:
    684                 layer->ignoreDrumPan = TRUE;
    685                 break;
    686 #endif
    687 
    688             default:
    689                 switch (cmd & 0xf0) {
    690                     case 0xd0: // layer_setshortnotevelocityfromtable
    691                         sp3A = seqPlayer->shortNoteVelocityTable[cmd & 0xf];
    692                         layer->velocitySquare = (f32)(sp3A * sp3A);
    693                         break;
    694                     case 0xe0: // layer_setshortnotedurationfromtable
    695                         layer->noteDuration = seqPlayer->shortNoteDurationTable[cmd & 0xf];
    696                         break;
    697                     default:
    698                         eu_stubbed_printf_1("Audio:Track:NOTE:UNDEFINED NOTE COM. %x\n", cmd);
    699                         break;
    700                 }
    701         }
    702     }
    703 
    704     if (cmd == 0xc0) { // layer_delay
    705         layer->delay = m64_read_compressed_u16(state);
    706         layer->stopSomething = TRUE;
    707     } else {
    708         layer->stopSomething = FALSE;
    709 
    710         if (seqChannel->largeNotes == TRUE) {
    711             switch (cmd & 0xc0) {
    712                 case 0x00: // layer_note0 (play percentage, velocity, duration)
    713                     sp3A = m64_read_compressed_u16(state);
    714                     vel = *(state->pc++);
    715                     layer->noteDuration = *(state->pc++);
    716                     layer->playPercentage = sp3A;
    717                     break;
    718 
    719                 case 0x40: // layer_note1 (play percentage, velocity)
    720                     sp3A = m64_read_compressed_u16(state);
    721                     vel = *(state->pc++);
    722                     layer->noteDuration = 0;
    723                     layer->playPercentage = sp3A;
    724                     break;
    725 
    726                 case 0x80: // layer_note2 (velocity, duration; uses last play percentage)
    727                     sp3A = layer->playPercentage;
    728                     vel = *(state->pc++);
    729                     layer->noteDuration = *(state->pc++);
    730                     break;
    731             }
    732 
    733             // the remaining bits are used for the semitone
    734             cmd -= (cmd & 0xc0);
    735 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    736             layer->velocitySquare = (f32)(vel) * (f32)vel;
    737 #else
    738             layer->velocitySquare = vel * vel;
    739 #endif
    740         } else {
    741             switch (cmd & 0xc0) {
    742                 case 0x00: // play note, type 0 (play percentage)
    743                     sp3A = m64_read_compressed_u16(state);
    744                     layer->playPercentage = sp3A;
    745                     break;
    746 
    747                 case 0x40: // play note, type 1 (uses default play percentage)
    748                     sp3A = layer->shortNoteDefaultPlayPercentage;
    749                     break;
    750 
    751                 case 0x80: // play note, type 2 (uses last play percentage)
    752                     sp3A = layer->playPercentage;
    753                     break;
    754             }
    755 
    756             // the remaining bits are used for the semitone
    757             cmd -= cmd & 0xc0;
    758         }
    759 
    760         layer->delay = sp3A;
    761 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    762         layer->duration = layer->noteDuration * sp3A >> 8;
    763 #else
    764         layer->duration = layer->noteDuration * sp3A / 256;
    765 #endif
    766         if ((seqPlayer->muted && (seqChannel->muteBehavior & MUTE_BEHAVIOR_STOP_NOTES) != 0)
    767             || seqChannel->stopSomething2
    768 #if defined(VERSION_JP) || defined(VERSION_US)
    769             || !seqChannel->hasInstrument
    770 #endif
    771         ) {
    772             layer->stopSomething = TRUE;
    773         } else {
    774 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    775             s32 temp = layer->instOrWave;
    776             if (temp == 0xff) {
    777                 temp = seqChannel->instOrWave;
    778             }
    779             if (temp == 0)
    780 #else
    781             if (seqChannel->instOrWave == 0)
    782 #endif
    783             { // drum
    784                 // cmd is reused for the drum semitone
    785                 cmd += seqChannel->transposition + layer->transposition;
    786 
    787 #if defined(VERSION_EU)
    788                 drum = get_drum(seqChannel->bankId, cmd);
    789 #else
    790                 if (cmd >= gCtlEntries[seqChannel->bankId].numDrums) {
    791                     cmd = gCtlEntries[seqChannel->bankId].numDrums;
    792                     if (cmd == 0) {
    793                         // this goto looks a bit like a function return...
    794                         layer->stopSomething = TRUE;
    795                         goto skip;
    796                     }
    797 
    798                     cmd--;
    799                 }
    800 
    801                 drum = gCtlEntries[seqChannel->bankId].drums[cmd];
    802 #endif
    803                 if (drum == NULL) {
    804                     layer->stopSomething = TRUE;
    805                 } else {
    806                     layer->adsr.envelope = drum->envelope;
    807                     layer->adsr.releaseRate = drum->releaseRate;
    808 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    809                     if (!layer->ignoreDrumPan) {
    810                         layer->pan = drum->pan;
    811                     }
    812 #else
    813                     layer->pan = FLOAT_CAST(drum->pan) / US_FLOAT(128.0);
    814 #endif
    815                     layer->sound = &drum->sound;
    816                     layer->freqScale = layer->sound->tuning;
    817                 }
    818 #if defined(VERSION_JP) || defined(VERSION_US) || defined(VERSION_SH)
    819             skip:;
    820 #endif
    821             } else { // instrument
    822                 // cmd is reused for the instrument semitone
    823                 cmd += seqPlayer->transposition + seqChannel->transposition + layer->transposition;
    824 
    825                 if (cmd >= 0x80) {
    826                     layer->stopSomething = TRUE;
    827                 } else {
    828 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    829                     if (layer->instOrWave == 0xffu) {
    830                         instrument = seqChannel->instrument;
    831                     } else {
    832                         instrument = layer->instrument;
    833                     }
    834 #else
    835                     instrument = layer->instrument;
    836                     if (instrument == NULL) {
    837                         instrument = seqChannel->instrument;
    838                     }
    839 #endif
    840 
    841                     if (layer->portamento.mode != 0) {
    842                         if (layer->portamentoTargetNote < cmd) {
    843                             vel = cmd;
    844                         } else {
    845                             vel = layer->portamentoTargetNote;
    846                         }
    847 
    848                         if (instrument != NULL) {
    849 #if defined(VERSION_EU)
    850                             sound = instrument_get_audio_bank_sound(instrument, vel);
    851 #else
    852                             sound = (u8) vel <  instrument->normalRangeLo ? &instrument->lowNotesSound
    853                                   : (u8) vel <= instrument->normalRangeHi ?
    854                                         &instrument->normalNotesSound : &instrument->highNotesSound;
    855 #endif
    856                             sameSound = (sound == layer->sound);
    857                             layer->sound = sound;
    858                             tuning = sound->tuning;
    859                         } else {
    860                             layer->sound = NULL;
    861                             tuning = 1.0f;
    862                         }
    863 
    864                         temp_f2 = gNoteFrequencies[cmd] * tuning;
    865                         temp_f12 = gNoteFrequencies[layer->portamentoTargetNote] * tuning;
    866 
    867                         portamento = &layer->portamento;
    868                         switch (PORTAMENTO_MODE(layer->portamento)) {
    869                             case PORTAMENTO_MODE_1:
    870                             case PORTAMENTO_MODE_3:
    871                             case PORTAMENTO_MODE_5:
    872 #if defined(VERSION_JP) || defined(VERSION_US)
    873                                 sp24 = temp_f2;
    874 #endif
    875                                 freqScale = temp_f12;
    876                                 break;
    877 
    878                             case PORTAMENTO_MODE_2:
    879                             case PORTAMENTO_MODE_4:
    880 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    881                             default:
    882 #endif
    883                                 freqScale = temp_f2;
    884 #if defined(VERSION_JP) || defined(VERSION_US)
    885                                 sp24 = temp_f12;
    886 #endif
    887                                 break;
    888                         }
    889 
    890 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    891                         portamento->extent = temp_f2 / freqScale - 1.0f;
    892 #else
    893                         portamento->extent = sp24 / freqScale - US_FLOAT(1.0);
    894 #endif
    895 
    896                         if (PORTAMENTO_IS_SPECIAL(layer->portamento)) {
    897                             portamento->speed = US_FLOAT(32512.0) * FLOAT_CAST(seqPlayer->tempo)
    898                                                 / ((f32) layer->delay * (f32) gTempoInternalToExternal
    899                                                    * FLOAT_CAST(layer->portamentoTime));
    900                         } else {
    901                             portamento->speed = US_FLOAT(127.0) / FLOAT_CAST(layer->portamentoTime);
    902                         }
    903                         portamento->cur = 0.0f;
    904                         layer->freqScale = freqScale;
    905                         if (PORTAMENTO_MODE(layer->portamento) == PORTAMENTO_MODE_5) {
    906                             layer->portamentoTargetNote = cmd;
    907                         }
    908                     } else if (instrument != NULL) {
    909 #if defined(VERSION_EU)
    910                         sound = instrument_get_audio_bank_sound(instrument, cmd);
    911 #else
    912                         sound = cmd < instrument->normalRangeLo ?
    913                                          &instrument->lowNotesSound : cmd <= instrument->normalRangeHi ?
    914                                          &instrument->normalNotesSound : &instrument->highNotesSound;
    915 #endif
    916                         sameSound = (sound == layer->sound);
    917                         layer->sound = sound;
    918                         layer->freqScale = gNoteFrequencies[cmd] * sound->tuning;
    919                     } else {
    920                         layer->sound = NULL;
    921                         layer->freqScale = gNoteFrequencies[cmd];
    922                     }
    923                 }
    924             }
    925             layer->delayUnused = layer->delay;
    926         }
    927     }
    928 
    929     if (layer->stopSomething == TRUE) {
    930         if (layer->note != NULL || layer->continuousNotes) {
    931             seq_channel_layer_note_decay(layer);
    932         }
    933         return;
    934     }
    935 
    936     cmd = FALSE;
    937     if (!layer->continuousNotes) {
    938         cmd = TRUE;
    939     } else if (layer->note == NULL || layer->status == SOUND_LOAD_STATUS_NOT_LOADED) {
    940         cmd = TRUE;
    941     } else if (sameSound == FALSE) {
    942         seq_channel_layer_note_decay(layer);
    943         cmd = TRUE;
    944     }
    945 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    946     else if (layer != layer->note->parentLayer) {
    947         cmd = TRUE;
    948     }
    949 #endif
    950     else if (layer->sound == NULL) {
    951         init_synthetic_wave(layer->note, layer);
    952     }
    953 
    954     if (cmd != FALSE) {
    955         layer->note = alloc_note(layer);
    956     }
    957 
    958     if (layer->note != NULL && layer->note->parentLayer == layer) {
    959         note_vibrato_init(layer->note);
    960     }
    961 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
    962     if (seqChannel) {
    963     }
    964 #endif
    965 }
    966 
    967 #ifdef VERSION_EU
    968 u8 audioString106[] = "Audio: Note:Velocity Error %d\n";
    969 u8 audioString107[] = "Error: Your assignchannel is stolen.\n";
    970 #endif
    971 
    972 #else
    973 // US/JP version with macros to simulate inlining by copt. Edit if you dare.
    974 #include "copt/seq_channel_layer_process_script_copt.inc.c"
    975 #endif
    976 
    977 #if defined(VERSION_SH) || defined(VERSION_CN)
    978 void seq_channel_layer_process_script_part1(struct SequenceChannelLayer *layer) {
    979     if (!layer->continuousNotes) {
    980         seq_channel_layer_note_decay(layer);
    981     } else if (layer->note != NULL && layer->note->wantedParentLayer == layer) {
    982         seq_channel_layer_note_decay(layer);
    983     }
    984 
    985     if (PORTAMENTO_MODE(layer->portamento) == PORTAMENTO_MODE_1 ||
    986         PORTAMENTO_MODE(layer->portamento) == PORTAMENTO_MODE_2) {
    987         layer->portamento.mode = 0;
    988     }
    989 
    990     layer->notePropertiesNeedInit = TRUE;
    991 }
    992 
    993 s32 seq_channel_layer_process_script_part5(struct SequenceChannelLayer *layer, s32 cmd) {
    994     if (!layer->stopSomething && layer->sound != NULL && layer->sound->sample->codec == CODEC_SKIP &&
    995         layer->sound->sample->medium != 0) {
    996         layer->stopSomething = TRUE;
    997         return -1;
    998     }
    999 
   1000     if (layer->continuousNotes == 1 && layer->note != NULL && layer->status && cmd == 1 &&
   1001         layer->note->parentLayer == layer) {
   1002         if (layer->sound == NULL) {
   1003             init_synthetic_wave(layer->note, layer);
   1004         }
   1005     } else {
   1006         if (cmd == 0) {
   1007             seq_channel_layer_note_decay(layer);
   1008         }
   1009         layer->note = alloc_note(layer);
   1010     }
   1011     if (layer->note != NULL && layer->note->parentLayer == layer) {
   1012         note_vibrato_init(layer->note);
   1013     }
   1014     return 0;
   1015 }
   1016 
   1017 s32 seq_channel_layer_process_script_part2(struct SequenceChannelLayer *layer) {
   1018     struct SequenceChannel *seqChannel = layer->seqChannel;
   1019     struct SequencePlayer *seqPlayer = seqChannel->seqPlayer;
   1020     struct M64ScriptState *state;
   1021     s32 temp_a0_5;
   1022     u16 sp3A;
   1023     u8 cmd;
   1024 
   1025     for (;;) {
   1026         state = &layer->scriptState;
   1027         cmd = m64_read_u8(state);
   1028 
   1029         if (cmd <= 0xc0) {
   1030             return cmd;
   1031         }
   1032 
   1033         switch (cmd) {
   1034             case 0xff: // layer_end; function return or end of script
   1035                 if (state->depth == 0) {
   1036                     // N.B. this function call is *not* inlined even though it's
   1037                     // within the same file, unlike in the rest of this function.
   1038                     seq_channel_layer_disable(layer);
   1039                     return -1;
   1040                 }
   1041                 state->pc = state->stack[--state->depth];
   1042                 break;
   1043 
   1044             case 0xfc: // layer_call
   1045                 if (0 && state->depth >= 4) {
   1046                     eu_stubbed_printf_0("Macro Level Over Error!\n");
   1047                 }
   1048                 sp3A = m64_read_s16(state);
   1049                 state->stack[state->depth++] = state->pc;
   1050                 state->pc = seqPlayer->seqData + sp3A;
   1051                 break;
   1052 
   1053             case 0xf8: // layer_loop; loop start, N iterations (or 256 if N = 0)
   1054                 if (0 && state->depth >= 4) {
   1055                     eu_stubbed_printf_0("Macro Level Over Error!\n");
   1056                 }
   1057                 state->remLoopIters[state->depth] = m64_read_u8(state);
   1058                 state->stack[state->depth++] = state->pc;
   1059                 break;
   1060 
   1061             case 0xf7: // layer_loopend
   1062                 if (--state->remLoopIters[state->depth - 1] != 0) {
   1063                     state->pc = state->stack[state->depth - 1];
   1064                 } else {
   1065                     state->depth--;
   1066                 }
   1067                 break;
   1068 
   1069             case 0xfb: // layer_jump
   1070                 sp3A = m64_read_s16(state);
   1071                 state->pc = seqPlayer->seqData + sp3A;
   1072                 break;
   1073 
   1074             case 0xf4:
   1075                 state->pc += (s8)m64_read_u8(state);
   1076                 break;
   1077 
   1078             case 0xc1: // layer_setshortnotevelocity
   1079             case 0xca: // layer_setpan
   1080                 temp_a0_5 = *(state->pc++);
   1081                 if (cmd == 0xc1) {
   1082                     layer->velocitySquare = (f32) (temp_a0_5 * temp_a0_5) / (f32) (127 * 127);
   1083                 } else {
   1084                     layer->pan = temp_a0_5;
   1085                 }
   1086                 break;
   1087 
   1088             case 0xc2: // layer_transpose; set transposition in semitones
   1089             case 0xc9: // layer_setshortnoteduration
   1090                 temp_a0_5 = *(state->pc++);
   1091                 if (cmd == 0xc9) {
   1092                     layer->noteDuration = temp_a0_5;
   1093                 } else {
   1094                     layer->transposition = temp_a0_5;
   1095                 }
   1096                 break;
   1097 
   1098             case 0xc4: // layer_somethingon
   1099             case 0xc5: // layer_somethingoff
   1100                 if (cmd == 0xc4) {
   1101                     layer->continuousNotes = TRUE;
   1102                 } else {
   1103                     layer->continuousNotes = FALSE;
   1104                 }
   1105                 seq_channel_layer_note_decay(layer);
   1106                 break;
   1107 
   1108             case 0xc3: // layer_setshortnotedefaultplaypercentage
   1109                 sp3A = m64_read_compressed_u16(state);
   1110                 layer->shortNoteDefaultPlayPercentage = sp3A;
   1111                 break;
   1112 
   1113             case 0xc6: // layer_setinstr
   1114                 cmd = m64_read_u8(state);
   1115                 if (cmd >= 0x7f) {
   1116                     if (cmd == 0x7f) {
   1117                         layer->instOrWave = 0;
   1118                     } else {
   1119                         layer->instOrWave = cmd;
   1120                         layer->instrument = NULL;
   1121                     }
   1122 
   1123                     if (1) {
   1124                     }
   1125 
   1126                     if (cmd == 0xff) {
   1127                         layer->adsr.releaseRate = 0;
   1128                     }
   1129                     break;
   1130                 }
   1131 
   1132                 if ((layer->instOrWave = get_instrument(seqChannel, cmd, &layer->instrument, &layer->adsr)) == 0) {
   1133                     eu_stubbed_printf_1("WARNING: NPRG: cannot change %d\n", cmd);
   1134                     layer->instOrWave = 0xff;
   1135                 }
   1136                 break;
   1137 
   1138             case 0xc7: // layer_portamento
   1139                 layer->portamento.mode = m64_read_u8(state);
   1140 
   1141                 // cmd is reused for the portamento's semitone
   1142                 cmd = m64_read_u8(state) + seqChannel->transposition +
   1143                     layer->transposition + seqPlayer->transposition;
   1144 
   1145                 if (cmd >= 0x80) {
   1146                     cmd = 0;
   1147                 }
   1148 
   1149                 layer->portamentoTargetNote = cmd;
   1150 
   1151                 // If special, the next param is u8 instead of var
   1152                 if (PORTAMENTO_IS_SPECIAL(layer->portamento)) {
   1153                     layer->portamentoTime = *((state)->pc++);
   1154                     break;
   1155                 }
   1156 
   1157                 sp3A = m64_read_compressed_u16(state);
   1158                 layer->portamentoTime = sp3A;
   1159                 break;
   1160 
   1161             case 0xc8: // layer_disableportamento
   1162                 layer->portamento.mode = 0;
   1163                 break;
   1164 
   1165             case 0xcb:
   1166                 sp3A = m64_read_s16(state);
   1167                 layer->adsr.envelope = (struct AdsrEnvelope *) (seqPlayer->seqData + sp3A);
   1168                 layer->adsr.releaseRate = m64_read_u8(state);
   1169                 break;
   1170 
   1171             case 0xcc:
   1172                 layer->ignoreDrumPan = TRUE;
   1173                 break;
   1174 
   1175             case 0xcd:
   1176                 layer->reverbBits.asByte = m64_read_u8(state);
   1177                 break;
   1178 
   1179             case 0xce:
   1180                 cmd = m64_read_u8(state) + 0x80;
   1181                 layer->freqScaleMultiplier = unk_sh_data_1[cmd];
   1182                 // missing break :)
   1183 
   1184             default:
   1185                 switch (cmd & 0xf0) {
   1186                     case 0xd0: // layer_setshortnotevelocityfromtable
   1187                         sp3A = seqPlayer->shortNoteVelocityTable[cmd & 0xf];
   1188                         layer->velocitySquare = (f32) (sp3A * sp3A) / (f32) (127 * 127);
   1189                         break;
   1190                     case 0xe0: // layer_setshortnotedurationfromtable
   1191                         layer->noteDuration = seqPlayer->shortNoteDurationTable[cmd & 0xf];
   1192                         break;
   1193                     default:
   1194                         eu_stubbed_printf_1("Audio:Track:NOTE:UNDEFINED NOTE COM. %x\n", cmd);
   1195                         break;
   1196                 }
   1197         }
   1198     }
   1199     return cmd;
   1200 }
   1201 
   1202 s32 seq_channel_layer_process_script_part4(struct SequenceChannelLayer *layer, s32 cmd1) {
   1203     s32 sameSound = TRUE;
   1204     struct SequenceChannel *seqChannel = layer->seqChannel;
   1205     struct Portamento *portamento;
   1206     struct AudioBankSound *sound;
   1207     struct Instrument *instrument;
   1208     struct Drum *drum;
   1209     f32 tuning;
   1210     s32 vel;
   1211     f32 freqScale;
   1212     f32 sp24;
   1213     f32 temp_f12;
   1214     UNUSED s32 pad[2];
   1215     struct SequencePlayer *seqPlayer = seqChannel->seqPlayer;
   1216     u8 cmd = cmd1;
   1217     f32 temp_f2;
   1218 
   1219     s32 temp = layer->instOrWave;
   1220     if (temp == 0xff) {
   1221         if (!seqChannel->hasInstrument) {
   1222             return -1;
   1223         }
   1224         temp = seqChannel->instOrWave;
   1225     }
   1226     if (temp == 0) { // drum
   1227         // cmd is reused for the drum semitone
   1228         cmd += seqChannel->transposition + layer->transposition;
   1229 
   1230         drum = get_drum(seqChannel->bankId, cmd);
   1231         if (drum == NULL) {
   1232             layer->stopSomething = TRUE;
   1233             layer->delayUnused = layer->delay;
   1234             return -1;
   1235         } else {
   1236             layer->adsr.envelope = drum->envelope;
   1237             layer->adsr.releaseRate = drum->releaseRate;
   1238             if (!layer->ignoreDrumPan) {
   1239                 layer->pan = drum->pan;
   1240             }
   1241             layer->sound = &drum->sound;
   1242             layer->freqScale = layer->sound->tuning;
   1243         }
   1244     } else { // instrument
   1245         // cmd is reused for the instrument semitone
   1246         cmd += seqPlayer->transposition + seqChannel->transposition + layer->transposition;
   1247 
   1248         if (cmd >= 0x80) {
   1249             layer->stopSomething = TRUE;
   1250             return -1;
   1251         } else {
   1252             if (layer->instOrWave == 0xff) {
   1253                 instrument = seqChannel->instrument;
   1254             } else {
   1255                 instrument = layer->instrument;
   1256             }
   1257 
   1258             if (layer->portamento.mode != 0) {
   1259                 if (layer->portamentoTargetNote < cmd) {
   1260                     vel = cmd;
   1261                 } else {
   1262                     vel = layer->portamentoTargetNote;
   1263                 }
   1264 
   1265                 if (instrument != NULL) {
   1266                     sound = instrument_get_audio_bank_sound(instrument, vel);
   1267                     sameSound = (sound == layer->sound);
   1268                     layer->sound = sound;
   1269                     tuning = sound->tuning;
   1270                 } else {
   1271                     layer->sound = NULL;
   1272                     tuning = 1.0f;
   1273                 }
   1274 
   1275                 temp_f2 = gNoteFrequencies[cmd] * tuning;
   1276                 temp_f12 = gNoteFrequencies[layer->portamentoTargetNote] * tuning;
   1277 
   1278                 portamento = &layer->portamento;
   1279                 switch (PORTAMENTO_MODE(layer->portamento)) {
   1280                     case PORTAMENTO_MODE_1:
   1281                     case PORTAMENTO_MODE_3:
   1282                     case PORTAMENTO_MODE_5:
   1283                         sp24 = temp_f2;
   1284                         freqScale = temp_f12;
   1285                         break;
   1286 
   1287                     case PORTAMENTO_MODE_2:
   1288                     case PORTAMENTO_MODE_4:
   1289                         freqScale = temp_f2;
   1290                         sp24 = temp_f12;
   1291                         break;
   1292 
   1293                     default:
   1294                         freqScale = temp_f2;
   1295                         sp24 = temp_f2;
   1296                         break;
   1297                 }
   1298 
   1299                 portamento->extent = sp24 / freqScale - 1.0f;
   1300 
   1301                 if (PORTAMENTO_IS_SPECIAL(layer->portamento)) {
   1302                     portamento->speed = US_FLOAT(32512.0) * FLOAT_CAST(seqPlayer->tempo)
   1303                                         / ((f32) layer->delay * (f32) gTempoInternalToExternal
   1304                                             * FLOAT_CAST(layer->portamentoTime));
   1305                 } else {
   1306                     portamento->speed = US_FLOAT(127.0) / FLOAT_CAST(layer->portamentoTime);
   1307                 }
   1308                 portamento->cur = 0.0f;
   1309                 layer->freqScale = freqScale;
   1310                 if (PORTAMENTO_MODE(layer->portamento) == PORTAMENTO_MODE_5) {
   1311                     layer->portamentoTargetNote = cmd;
   1312                 }
   1313             } else if (instrument != NULL) {
   1314                 sound = instrument_get_audio_bank_sound(instrument, cmd);
   1315                 sameSound = (sound == layer->sound);
   1316                 layer->sound = sound;
   1317                 layer->freqScale = gNoteFrequencies[cmd] * sound->tuning;
   1318             } else {
   1319                 layer->sound = NULL;
   1320                 layer->freqScale = gNoteFrequencies[cmd];
   1321             }
   1322         }
   1323     }
   1324     layer->delayUnused = layer->delay;
   1325     layer->freqScale *= layer->freqScaleMultiplier;
   1326     return sameSound;
   1327 }
   1328 
   1329 s32 seq_channel_layer_process_script_part3(struct SequenceChannelLayer *layer, s32 cmd) {
   1330     struct M64ScriptState *state = &layer->scriptState;
   1331     u16 sp3A;
   1332     s32 vel;
   1333     struct SequenceChannel *seqChannel = layer->seqChannel;
   1334     struct SequencePlayer *seqPlayer = seqChannel->seqPlayer;
   1335 
   1336     if (cmd == 0xc0) { // layer_delay
   1337         layer->delay = m64_read_compressed_u16(state);
   1338         layer->stopSomething = TRUE;
   1339         return -1;
   1340     }
   1341 
   1342     layer->stopSomething = FALSE;
   1343 
   1344     if (seqChannel->largeNotes == TRUE) {
   1345         switch (cmd & 0xc0) {
   1346             case 0x00: // layer_note0 (play percentage, velocity, duration)
   1347                 sp3A = m64_read_compressed_u16(state);
   1348                 vel = *(state->pc++);
   1349                 layer->noteDuration = *(state->pc++);
   1350                 layer->playPercentage = sp3A;
   1351                 break;
   1352 
   1353             case 0x40: // layer_note1 (play percentage, velocity)
   1354                 sp3A = m64_read_compressed_u16(state);
   1355                 vel = *(state->pc++);
   1356                 layer->noteDuration = 0;
   1357                 layer->playPercentage = sp3A;
   1358                 break;
   1359 
   1360             case 0x80: // layer_note2 (velocity, duration; uses last play percentage)
   1361                 sp3A = layer->playPercentage;
   1362                 vel = *(state->pc++);
   1363                 layer->noteDuration = *(state->pc++);
   1364                 break;
   1365         }
   1366         if (vel >= 0x80 || vel < 0) {
   1367             vel = 0x7f;
   1368         }
   1369         layer->velocitySquare = ((f32) vel * (f32) vel) / (f32) (0x7f * 0x7f);
   1370         // the remaining bits are used for the semitone
   1371         cmd -= (cmd & 0xc0);
   1372     } else {
   1373         switch (cmd & 0xc0) {
   1374             case 0x00: // play note, type 0 (play percentage)
   1375                 sp3A = m64_read_compressed_u16(state);
   1376                 layer->playPercentage = sp3A;
   1377                 break;
   1378 
   1379             case 0x40: // play note, type 1 (uses default play percentage)
   1380                 sp3A = layer->shortNoteDefaultPlayPercentage;
   1381                 break;
   1382 
   1383             case 0x80: // play note, type 2 (uses last play percentage)
   1384                 sp3A = layer->playPercentage;
   1385                 break;
   1386         }
   1387 
   1388         // the remaining bits are used for the semitone
   1389         cmd -= cmd & 0xc0;
   1390     }
   1391 
   1392     layer->delay = sp3A;
   1393     layer->duration = layer->noteDuration * sp3A >> 8;
   1394     if ((seqPlayer->muted && (seqChannel->muteBehavior & 0x50) != 0)
   1395         || seqChannel->stopSomething2) {
   1396         layer->stopSomething = TRUE;
   1397         return -1;
   1398     }
   1399 
   1400     return cmd;
   1401 }
   1402 #endif
   1403 
   1404 u8 get_instrument(struct SequenceChannel *seqChannel, u8 instId, struct Instrument **instOut, struct AdsrSettings *adsr) {
   1405     struct Instrument *inst;
   1406 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1407     inst = get_instrument_inner(seqChannel->bankId, instId);
   1408     if (inst == NULL) {
   1409         *instOut = NULL;
   1410         return 0;
   1411     }
   1412     adsr->envelope = inst->envelope;
   1413     adsr->releaseRate = inst->releaseRate;
   1414     *instOut = inst;
   1415     instId++;
   1416     return instId;
   1417 #else
   1418     UNUSED u32 pad;
   1419 
   1420     if (instId >= gCtlEntries[seqChannel->bankId].numInstruments) {
   1421         instId = gCtlEntries[seqChannel->bankId].numInstruments;
   1422         if (instId == 0) {
   1423             return 0;
   1424         }
   1425         instId--;
   1426     }
   1427 
   1428     inst = gCtlEntries[seqChannel->bankId].instruments[instId];
   1429     if (inst == NULL) {
   1430         struct SequenceChannel seqChannelCpy = *seqChannel;
   1431 
   1432         while (instId != 0xff) {
   1433             inst = gCtlEntries[seqChannelCpy.bankId].instruments[instId];
   1434             if (inst != NULL) {
   1435                 break;
   1436             }
   1437             instId--;
   1438         }
   1439     }
   1440 
   1441     if (((uintptr_t) gBankLoadedPool.persistent.pool.start <= (uintptr_t) inst
   1442          && (uintptr_t) inst <= (uintptr_t)(gBankLoadedPool.persistent.pool.start
   1443                     + gBankLoadedPool.persistent.pool.size))
   1444         || ((uintptr_t) gBankLoadedPool.temporary.pool.start <= (uintptr_t) inst
   1445             && (uintptr_t) inst <= (uintptr_t)(gBankLoadedPool.temporary.pool.start
   1446                                    + gBankLoadedPool.temporary.pool.size))) {
   1447         adsr->envelope = inst->envelope;
   1448         adsr->releaseRate = inst->releaseRate;
   1449         *instOut = inst;
   1450         instId++;
   1451         return instId;
   1452     }
   1453 
   1454     gAudioErrorFlags = instId + 0x20000;
   1455     *instOut = NULL;
   1456     return 0;
   1457 #endif
   1458 }
   1459 
   1460 void set_instrument(struct SequenceChannel *seqChannel, u8 instId) {
   1461     if (instId >= 0x80) {
   1462         seqChannel->instOrWave = instId;
   1463         seqChannel->instrument = NULL;
   1464     } else if (instId == 0x7f) {
   1465         seqChannel->instOrWave = 0;
   1466         seqChannel->instrument = (struct Instrument *) 1;
   1467     } else {
   1468 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1469         if ((seqChannel->instOrWave =
   1470             get_instrument(seqChannel, instId, &seqChannel->instrument, &seqChannel->adsr)) == 0)
   1471 #else
   1472         seqChannel->instOrWave =
   1473             get_instrument(seqChannel, instId, &seqChannel->instrument, &seqChannel->adsr);
   1474         if (seqChannel->instOrWave == 0)
   1475 #endif
   1476         {
   1477             seqChannel->hasInstrument = FALSE;
   1478             return;
   1479         }
   1480     }
   1481     seqChannel->hasInstrument = TRUE;
   1482 }
   1483 
   1484 void sequence_channel_set_volume(struct SequenceChannel *seqChannel, u8 volume) {
   1485     seqChannel->volume = FLOAT_CAST(volume) / US_FLOAT(127.0);
   1486 }
   1487 
   1488 void sequence_channel_process_script(struct SequenceChannel *seqChannel) {
   1489     struct M64ScriptState *state;
   1490     struct SequencePlayer *seqPlayer;
   1491     u8 cmd;
   1492     s8 temp;
   1493     u8 loBits;
   1494     u16 sp5A;
   1495     s32 sp38;
   1496     s8 value;
   1497     s32 i;
   1498     u8 *seqData;
   1499 
   1500     if (!seqChannel->enabled) {
   1501         return;
   1502     }
   1503 
   1504     if (seqChannel->stopScript) {
   1505         for (i = 0; i < LAYERS_MAX; i++) {
   1506             if (seqChannel->layers[i] != NULL) {
   1507                 seq_channel_layer_process_script(seqChannel->layers[i]);
   1508             }
   1509         }
   1510         return;
   1511     }
   1512 
   1513     seqPlayer = seqChannel->seqPlayer;
   1514     if (seqPlayer->muted && (seqChannel->muteBehavior & MUTE_BEHAVIOR_STOP_SCRIPT) != 0) {
   1515         return;
   1516     }
   1517 
   1518     if (seqChannel->delay != 0) {
   1519         seqChannel->delay--;
   1520     }
   1521 
   1522     state = &seqChannel->scriptState;
   1523     if (seqChannel->delay == 0) {
   1524         for (;;) {
   1525             cmd = m64_read_u8(state);
   1526 #if defined(VERSION_JP) || defined(VERSION_US)
   1527             if (cmd == 0xff) { // chan_end
   1528                 if (state->depth == 0) {
   1529                     sequence_channel_disable(seqChannel);
   1530                     break;
   1531                 }
   1532                 state->depth--, state->pc = state->stack[state->depth];
   1533             }
   1534             if (cmd == 0xfe) { // chan_delay1
   1535                 break;
   1536             }
   1537             if (cmd == 0xfd) { // chan_delay
   1538                 seqChannel->delay = m64_read_compressed_u16(state);
   1539                 break;
   1540             }
   1541             if (cmd == 0xf3) { // chan_hang
   1542                 seqChannel->stopScript = TRUE;
   1543                 break;
   1544             }
   1545 #endif
   1546 
   1547 #if defined(VERSION_SH) || defined(VERSION_CN)
   1548             if (cmd >= 0xb0)
   1549 #else
   1550             if (cmd > 0xc0)
   1551 #endif
   1552             {
   1553                 switch (cmd) {
   1554                     case 0xff: // chan_end
   1555 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1556                         if (state->depth == 0) {
   1557                             sequence_channel_disable(seqChannel);
   1558                             goto out;
   1559                         } else {
   1560                             state->pc = state->stack[--state->depth];
   1561                         }
   1562 #endif
   1563                         break;
   1564 
   1565 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1566                     case 0xfe: // chan_delay1
   1567                         goto out;
   1568 
   1569                     case 0xfd: // chan_delay
   1570                         seqChannel->delay = m64_read_compressed_u16(state);
   1571                         goto out;
   1572 
   1573                     case 0xea:
   1574                         seqChannel->stopScript = TRUE;
   1575                         goto out;
   1576 #endif
   1577                     case 0xfc: // chan_call
   1578                         if (0 && state->depth >= 4) {
   1579                             eu_stubbed_printf_0("Audio:Track :Call Macro Level Over Error!\n");
   1580                         }
   1581                         sp5A = m64_read_s16(state);
   1582 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1583                         state->stack[state->depth++] = state->pc;
   1584 #else
   1585                         state->depth++, state->stack[state->depth - 1] = state->pc;
   1586 #endif
   1587                         state->pc = seqPlayer->seqData + sp5A;
   1588                         break;
   1589 
   1590                     case 0xf8: // chan_loop; loop start, N iterations (or 256 if N = 0)
   1591                         if (0 && state->depth >= 4) {
   1592                             eu_stubbed_printf_0("Audio:Track :Loops Macro Level Over Error!\n");
   1593                         }
   1594                         state->remLoopIters[state->depth] = m64_read_u8(state);
   1595 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1596                         state->stack[state->depth++] = state->pc;
   1597 #else
   1598                         state->depth++, state->stack[state->depth - 1] = state->pc;
   1599 #endif
   1600                         break;
   1601 
   1602                     case 0xf7: // chan_loopend
   1603                         state->remLoopIters[state->depth - 1]--;
   1604                         if (state->remLoopIters[state->depth - 1] != 0) {
   1605                             state->pc = state->stack[state->depth - 1];
   1606                         } else {
   1607                             state->depth--;
   1608                         }
   1609                         break;
   1610 
   1611                     case 0xf6: // chan_break; break loop, if combined with jump
   1612                         state->depth--;
   1613                         break;
   1614 
   1615                     case 0xfb: // chan_jump
   1616                     case 0xfa: // chan_beqz
   1617                     case 0xf9: // chan_bltz
   1618                     case 0xf5: // chan_bgez
   1619                         sp5A = m64_read_s16(state);
   1620                         if (cmd == 0xfa && value != 0) {
   1621                             break;
   1622                         }
   1623                         if (cmd == 0xf9 && value >= 0) {
   1624                             break;
   1625                         }
   1626                         if (cmd == 0xf5 && value < 0) {
   1627                             break;
   1628                         }
   1629                         state->pc = seqPlayer->seqData + sp5A;
   1630                         break;
   1631 
   1632 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1633                     case 0xf4: // chan_jump_rel
   1634                     case 0xf3: // chan_beqz_rel
   1635                     case 0xf2: // chan_bltz_rel
   1636                         temp = m64_read_u8(state);
   1637                         if (cmd == 0xf3 && value != 0) {
   1638                             break;
   1639                         }
   1640                         if (cmd == 0xf2 && value >= 0) {
   1641                             break;
   1642                         }
   1643                         state->pc += temp;
   1644                         break;
   1645 #endif
   1646 
   1647 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1648                     case 0xf1: // chan_reservenotes
   1649 #else
   1650                     case 0xf2: // chan_reservenotes
   1651 #endif
   1652                         note_pool_clear(&seqChannel->notePool);
   1653                         note_pool_fill(&seqChannel->notePool, m64_read_u8(state));
   1654                         break;
   1655 
   1656 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1657                     case 0xf0: // chan_unreservenotes
   1658 #else
   1659                     case 0xf1: // chan_unreservenotes
   1660 #endif
   1661                         note_pool_clear(&seqChannel->notePool);
   1662                         break;
   1663 
   1664                     case 0xc2: // chan_setdyntable
   1665                         sp5A = m64_read_s16(state);
   1666                         seqChannel->dynTable = (void *) (seqPlayer->seqData + sp5A);
   1667                         break;
   1668 
   1669                     case 0xc5: // chan_dynsetdyntable
   1670                         if (value != -1) {
   1671 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1672                             seqData = (*seqChannel->dynTable)[value];
   1673                             sp38 = (u16)((seqData[0] << 8) + seqData[1]);
   1674                             seqChannel->dynTable = (void *) (seqPlayer->seqData + sp38);
   1675 #else
   1676                             sp5A = (u16)((((*seqChannel->dynTable)[value])[0] << 8) + (((*seqChannel->dynTable)[value])[1]));
   1677                             seqChannel->dynTable = (void *) (seqPlayer->seqData + sp5A);
   1678 #endif
   1679                         }
   1680                         break;
   1681 
   1682 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1683                     case 0xeb: // chan_setbankandinstr
   1684                         cmd = m64_read_u8(state);
   1685                         // Switch to the cmd's (0-indexed) bank in this sequence's
   1686                         // bank set. Note that in the binary format (not in the JSON!)
   1687                         // the banks are listed backwards, so we counts from the back.
   1688                         // (gAlBankSets[offset] is number of banks)
   1689                         sp38 = ((u16 *) gAlBankSets)[seqPlayer->seqId];
   1690                         loBits = *(sp38 + gAlBankSets);
   1691                         cmd = gAlBankSets[(s32)sp38 + loBits - cmd];
   1692 
   1693 #if defined(VERSION_SH) || defined(VERSION_CN)
   1694                         if (get_bank_or_seq(1, 2, cmd) != NULL)
   1695 #else
   1696                         if (get_bank_or_seq(&gBankLoadedPool, 2, cmd) != NULL)
   1697 #endif
   1698                         {
   1699                             seqChannel->bankId = cmd;
   1700                         } else {
   1701                             eu_stubbed_printf_1("SUB:ERR:BANK %d NOT CACHED.\n", cmd);
   1702                         }
   1703                         // fallthrough
   1704 #endif
   1705 
   1706                     case 0xc1: // chan_setinstr ("set program"?)
   1707                         set_instrument(seqChannel, m64_read_u8(state));
   1708                         break;
   1709 
   1710                     case 0xc3: // chan_largenotesoff
   1711                         seqChannel->largeNotes = FALSE;
   1712                         break;
   1713 
   1714                     case 0xc4: // chan_largenoteson
   1715                         seqChannel->largeNotes = TRUE;
   1716                         break;
   1717 
   1718                     case 0xdf: // chan_setvol
   1719                         sequence_channel_set_volume(seqChannel, m64_read_u8(state));
   1720 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1721                         seqChannel->changes.as_bitfields.volume = TRUE;
   1722 #endif
   1723                         break;
   1724 
   1725                     case 0xe0: // chan_setvolscale
   1726                         seqChannel->volumeScale = FLOAT_CAST(m64_read_u8(state)) / US_FLOAT(128.0);
   1727 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1728                         seqChannel->changes.as_bitfields.volume = TRUE;
   1729 #endif
   1730                         break;
   1731 
   1732                     case 0xde: // chan_freqscale; pitch bend using raw frequency multiplier N/2^15 (N is u16)
   1733                         sp5A = m64_read_s16(state);
   1734                         seqChannel->freqScale = FLOAT_CAST(sp5A) / US_FLOAT(32768.0);
   1735 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1736                         seqChannel->changes.as_bitfields.freqScale = TRUE;
   1737 #endif
   1738                         break;
   1739 
   1740                     case 0xd3: // chan_pitchbend; pitch bend by <= 1 octave in either direction (-127..127)
   1741                         // (m64_read_u8(state) is really s8 here)
   1742 #if defined(VERSION_SH) || defined(VERSION_CN)
   1743                         cmd = m64_read_u8(state) + 128;
   1744 #else
   1745                         cmd = m64_read_u8(state) + 127;
   1746 #endif
   1747                         seqChannel->freqScale = gPitchBendFrequencyScale[cmd];
   1748 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1749                         seqChannel->changes.as_bitfields.freqScale = TRUE;
   1750 #endif
   1751                         break;
   1752 
   1753 #if defined(VERSION_SH) || defined(VERSION_CN)
   1754                     case 0xee:
   1755                         cmd = m64_read_u8(state) + 0x80;
   1756                         seqChannel->freqScale = unk_sh_data_1[cmd];
   1757                         seqChannel->changes.as_bitfields.freqScale = TRUE;
   1758                         break;
   1759 #endif
   1760 
   1761                     case 0xdd: // chan_setpan
   1762 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1763                         seqChannel->newPan = m64_read_u8(state);
   1764                         seqChannel->changes.as_bitfields.pan = TRUE;
   1765 #else
   1766                         seqChannel->pan = FLOAT_CAST(m64_read_u8(state)) / US_FLOAT(128.0);
   1767 #endif
   1768                         break;
   1769 
   1770                     case 0xdc: // chan_setpanmix; set proportion of pan to come from channel (0..128)
   1771 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1772                         seqChannel->panChannelWeight = m64_read_u8(state);
   1773                         seqChannel->changes.as_bitfields.pan = TRUE;
   1774 #else
   1775                         seqChannel->panChannelWeight = FLOAT_CAST(m64_read_u8(state)) / US_FLOAT(128.0);
   1776 #endif
   1777                         break;
   1778 
   1779                     case 0xdb: // chan_transpose; set transposition in semitones
   1780                         temp = *state->pc++;
   1781                         seqChannel->transposition = temp;
   1782                         break;
   1783 
   1784                     case 0xda: // chan_setenvelope
   1785                         sp5A = m64_read_s16(state);
   1786                         seqChannel->adsr.envelope = (struct AdsrEnvelope *) (seqPlayer->seqData + sp5A);
   1787                         break;
   1788 
   1789                     case 0xd9: // chan_setdecayrelease
   1790                         seqChannel->adsr.releaseRate = m64_read_u8(state);
   1791                         break;
   1792 
   1793                     case 0xd8: // chan_setvibratoextent
   1794                         seqChannel->vibratoExtentTarget = m64_read_u8(state) * 8;
   1795                         seqChannel->vibratoExtentStart = 0;
   1796                         seqChannel->vibratoExtentChangeDelay = 0;
   1797                         break;
   1798 
   1799                     case 0xd7: // chan_setvibratorate
   1800                         seqChannel->vibratoRateStart = seqChannel->vibratoRateTarget =
   1801                             m64_read_u8(state) * 32;
   1802                         seqChannel->vibratoRateChangeDelay = 0;
   1803                         break;
   1804 
   1805                     case 0xe2: // chan_setvibratoextentlinear
   1806                         seqChannel->vibratoExtentStart = m64_read_u8(state) * 8;
   1807                         seqChannel->vibratoExtentTarget = m64_read_u8(state) * 8;
   1808                         seqChannel->vibratoExtentChangeDelay = m64_read_u8(state) * 16;
   1809                         break;
   1810 
   1811                     case 0xe1: // chan_setvibratoratelinear
   1812                         seqChannel->vibratoRateStart = m64_read_u8(state) * 32;
   1813                         seqChannel->vibratoRateTarget = m64_read_u8(state) * 32;
   1814                         seqChannel->vibratoRateChangeDelay = m64_read_u8(state) * 16;
   1815                         break;
   1816 
   1817                     case 0xe3: // chan_setvibratodelay
   1818                         seqChannel->vibratoDelay = m64_read_u8(state) * 16;
   1819                         break;
   1820 
   1821 #if defined(VERSION_JP) || defined(VERSION_US)
   1822                     case 0xd6: // chan_setupdatesperframe_unimplemented
   1823                         cmd = m64_read_u8(state);
   1824                         if (cmd == 0) {
   1825                             cmd = gAudioUpdatesPerFrame;
   1826                         }
   1827                         seqChannel->updatesPerFrameUnused = cmd;
   1828                         break;
   1829 #endif
   1830 
   1831                     case 0xd4: // chan_setreverb
   1832                         seqChannel->reverbVol = m64_read_u8(state);
   1833                         break;
   1834 
   1835                     case 0xc6: // chan_setbank; switch bank within set
   1836                         cmd = m64_read_u8(state);
   1837                         // Switch to the temp's (0-indexed) bank in this sequence's
   1838                         // bank set. Note that in the binary format (not in the JSON!)
   1839                         // the banks are listed backwards, so we counts from the back.
   1840                         // (gAlBankSets[offset] is number of banks)
   1841 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1842                         sp38 = ((u16 *) gAlBankSets)[seqPlayer->seqId];
   1843                         loBits = *(sp38 + gAlBankSets);
   1844                         cmd = gAlBankSets[(s32)sp38 + loBits - cmd];
   1845 #else
   1846                         sp5A = ((u16 *) gAlBankSets)[seqPlayer->seqId];
   1847                         loBits = *(sp5A + gAlBankSets);
   1848                         cmd = gAlBankSets[sp5A + loBits - cmd];
   1849 #endif
   1850 #if defined(VERSION_SH) || defined(VERSION_CN)
   1851                         if (get_bank_or_seq(1, 2, cmd) != NULL)
   1852 #else
   1853                         if (get_bank_or_seq(&gBankLoadedPool, 2, cmd) != NULL)
   1854 #endif
   1855                         {
   1856                             seqChannel->bankId = cmd;
   1857                         } else {
   1858                             eu_stubbed_printf_1("SUB:ERR:BANK %d NOT CACHED.\n", cmd);
   1859                         }
   1860                         break;
   1861 
   1862                     case 0xc7: // chan_writeseq; write to sequence data (!)
   1863                         {
   1864 #if defined(VERSION_JP) || defined(VERSION_US)
   1865                             u8 *seqData;
   1866 #endif
   1867                             cmd = m64_read_u8(state);
   1868                             sp5A = m64_read_s16(state);
   1869                             seqData = seqPlayer->seqData + sp5A;
   1870                             *seqData = (u8)value + cmd;
   1871                         }
   1872                         break;
   1873 
   1874                     case 0xc8: // chan_subtract
   1875                     case 0xc9: // chan_bitand
   1876                     case 0xcc: // chan_setval
   1877                         temp = m64_read_u8(state);
   1878                         if (cmd == 0xc8) {
   1879                             value -= temp;
   1880                         } else if (cmd == 0xcc) {
   1881                             value = temp;
   1882                         } else {
   1883                             value &= temp;
   1884                         }
   1885                         break;
   1886 
   1887 #if defined(VERSION_SH) || defined(VERSION_CN)
   1888                     case 0xcd:
   1889                         sequence_channel_disable(seqPlayer->channels[m64_read_u8(state)]);
   1890                         break;
   1891 #endif
   1892 
   1893                     case 0xca: // chan_setmutebhv
   1894                         seqChannel->muteBehavior = m64_read_u8(state);
   1895 #if defined(VERSION_SH) || defined(VERSION_CN)
   1896                         seqChannel->changes.as_bitfields.volume = TRUE;
   1897 #endif
   1898                         break;
   1899 
   1900                     case 0xcb: // chan_readseq
   1901                         sp38 = (u16)m64_read_s16(state) + value;
   1902                         value = seqPlayer->seqData[sp38];
   1903                         break;
   1904 
   1905 #if defined(VERSION_SH) || defined(VERSION_CN)
   1906                     case 0xce:
   1907                         seqChannel->unkC8 = m64_read_s16(state);
   1908                         break;
   1909 
   1910                     case 0xcf:
   1911                         sp5A = m64_read_s16(state);
   1912                         seqData = seqPlayer->seqData + sp5A;
   1913                         seqData[0] = (seqChannel->unkC8 >> 8) & 0xffff;
   1914                         seqData[1] = (seqChannel->unkC8) & 0xffff;
   1915                         break;
   1916 #endif
   1917 
   1918                     case 0xd0: // chan_stereoheadseteffects
   1919                         seqChannel->stereoHeadsetEffects = m64_read_u8(state);
   1920                         break;
   1921 
   1922                     case 0xd1: // chan_setnoteallocationpolicy
   1923                         seqChannel->noteAllocPolicy = m64_read_u8(state);
   1924                         break;
   1925 
   1926                     case 0xd2: // chan_setsustain
   1927 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1928                         seqChannel->adsr.sustain = m64_read_u8(state);
   1929 #else
   1930                         seqChannel->adsr.sustain = m64_read_u8(state) << 8;
   1931 #endif
   1932                         break;
   1933 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1934                     case 0xe5:
   1935                         seqChannel->reverbIndex = m64_read_u8(state);
   1936                         break;
   1937 #endif
   1938                     case 0xe4: // chan_dyncall
   1939                         if (value != -1) {
   1940 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1941                             if (state->depth >= 4) {
   1942                                 eu_stubbed_printf_0("Audio:Track: CTBLCALL Macro Level Over Error!\n");
   1943                             }
   1944 #endif
   1945                             seqData = (*seqChannel->dynTable)[value];
   1946 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1947                             state->stack[state->depth++] = state->pc;
   1948                             sp38 = (u16)((seqData[0] << 8) + seqData[1]);
   1949                             state->pc = seqPlayer->seqData + sp38;
   1950 
   1951                             if (0 && sp38 >= gSeqFileHeader->seqArray[seqPlayer->seqId].len) {
   1952                                 eu_stubbed_printf_3("Err :Sub %x ,address %x:Undefined SubTrack Function %x", seqChannel, state->pc, sp38);
   1953                             }
   1954 #else
   1955                             state->depth++, state->stack[state->depth - 1] = state->pc;
   1956                             sp5A = ((seqData[0] << 8) + seqData[1]);
   1957                             state->pc = seqPlayer->seqData + sp5A;
   1958 #endif
   1959                         }
   1960                         break;
   1961 
   1962 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   1963                     case 0xe6:
   1964                         seqChannel->bookOffset = m64_read_u8(state);
   1965                         break;
   1966 
   1967                     case 0xe7:
   1968                         sp5A = m64_read_s16(state);
   1969                         seqData = seqPlayer->seqData + sp5A;
   1970                         seqChannel->muteBehavior = *seqData++;
   1971                         seqChannel->noteAllocPolicy = *seqData++;
   1972                         seqChannel->notePriority = *seqData++;
   1973                         seqChannel->transposition = (s8) *seqData++;
   1974                         seqChannel->newPan = *seqData++;
   1975                         seqChannel->panChannelWeight = *seqData++;
   1976                         seqChannel->reverbVol = *seqData++;
   1977                         seqChannel->reverbIndex = *seqData++;
   1978                         seqChannel->changes.as_bitfields.pan = TRUE;
   1979                         break;
   1980 
   1981                     case 0xe8:
   1982                         seqChannel->muteBehavior = m64_read_u8(state);
   1983                         seqChannel->noteAllocPolicy = m64_read_u8(state);
   1984                         seqChannel->notePriority = m64_read_u8(state);
   1985                         seqChannel->transposition = (s8) m64_read_u8(state);
   1986                         seqChannel->newPan = m64_read_u8(state);
   1987                         seqChannel->panChannelWeight = m64_read_u8(state);
   1988                         seqChannel->reverbVol = m64_read_u8(state);
   1989                         seqChannel->reverbIndex = m64_read_u8(state);
   1990                         seqChannel->changes.as_bitfields.pan = TRUE;
   1991                         break;
   1992 
   1993                     case 0xec:
   1994                         seqChannel->vibratoExtentTarget = 0;
   1995                         seqChannel->vibratoExtentStart = 0;
   1996                         seqChannel->vibratoExtentChangeDelay = 0;
   1997                         seqChannel->vibratoRateTarget = 0;
   1998                         seqChannel->vibratoRateStart = 0;
   1999                         seqChannel->vibratoRateChangeDelay = 0;
   2000                         seqChannel->freqScale = 1.0f;
   2001                         break;
   2002 
   2003                     case 0xe9: // chan_setnotepriority
   2004 #if defined(VERSION_SH) || defined(VERSION_CN)
   2005                         cmd = m64_read_u8(state);
   2006                         if ((cmd & 0xf) != 0) {
   2007                             seqChannel->notePriority = cmd & 0xf;
   2008                         }
   2009                         cmd = cmd >> 4;
   2010                         if (cmd != 0) {
   2011                             seqChannel->unkSH06 = cmd;
   2012                         }
   2013 #else
   2014                         seqChannel->notePriority = m64_read_u8(state);
   2015 #endif
   2016                         break;
   2017 #endif
   2018 #if defined(VERSION_SH) || defined(VERSION_CN)
   2019                     case 0xed:
   2020                         seqChannel->synthesisVolume = m64_read_u8(state);
   2021                         break;
   2022 
   2023                     case 0xef:
   2024                         m64_read_s16(state);
   2025                         m64_read_u8(state);
   2026                         break;
   2027 
   2028                     case 0xb0:
   2029                         sp5A = m64_read_s16(state);
   2030                         seqData = seqPlayer->seqData + sp5A;
   2031                         seqChannel->filter = (s16 *) (seqData);
   2032                         break;
   2033 
   2034                     case 0xb1:
   2035                         seqChannel->filter = NULL;
   2036                         break;
   2037 
   2038                     case 0xb3:
   2039                         if (seqChannel->filter != NULL) {
   2040                             cmd = m64_read_u8(state);
   2041                             if (cmd == 0) {
   2042                                 seqChannel->filter = NULL;
   2043                             } else {
   2044                                 loBits = (cmd >> 4) & 0xf;
   2045                                 cmd &= 0xf;
   2046                                 fill_filter(seqChannel->filter, loBits, cmd);
   2047                             }
   2048                         }
   2049                         break;
   2050 
   2051                     case 0xb2:
   2052                         i = (value * 2);
   2053                         sp5A = m64_read_s16(state);
   2054                         sp38 = sp5A + i;
   2055                         seqChannel->unkC8 = *(u16 *) (seqPlayer->seqData + sp38);
   2056                         break;
   2057 
   2058                     case 0xb4:
   2059                         seqChannel->dynTable = (void *) (seqPlayer->seqData + seqChannel->unkC8);
   2060                         break;
   2061 
   2062                     case 0xb5:
   2063                         seqChannel->unkC8 = *(u16 *) (*seqChannel->dynTable)[value];
   2064                         break;
   2065 
   2066                     case 0xb6:
   2067                         value = (*seqChannel->dynTable)[0][value];
   2068                         break;
   2069 #endif
   2070                 }
   2071             } else {
   2072 #if defined(VERSION_SH) || defined(VERSION_CN)
   2073                 if (cmd >= 0x80) {
   2074                     loBits = cmd & 7;
   2075                     switch (cmd & 0xf8) {
   2076                         case 0x80:
   2077                             if (seqChannel->layers[loBits] != NULL) {
   2078                                 value = seqChannel->layers[loBits]->finished;
   2079                             } else {
   2080                                 value = -1;
   2081                             }
   2082                             break;
   2083 
   2084                         case 0x88:
   2085                             sp5A = m64_read_s16(state);
   2086                             if (seq_channel_set_layer(seqChannel, loBits) == 0) {
   2087                                 if (1) {}
   2088                                 seqChannel->layers[loBits]->scriptState.pc = seqPlayer->seqData + sp5A;
   2089                             }
   2090                             break;
   2091 
   2092                         case 0x90:
   2093                             seq_channel_layer_free(seqChannel, loBits);
   2094                             break;
   2095 
   2096                         case 0x98:
   2097                             if (value != -1 && seq_channel_set_layer(seqChannel, loBits) != -1) {
   2098                                 seqData = (*seqChannel->dynTable)[value];
   2099                                 sp5A = ((seqData[0] << 8) + seqData[1]);
   2100                                 seqChannel->layers[loBits]->scriptState.pc = seqPlayer->seqData + sp5A;
   2101                             }
   2102                             break;
   2103                     }
   2104                 } else {
   2105 #endif
   2106                 loBits = cmd & 0xf;
   2107 
   2108                 switch (cmd & 0xf0) {
   2109 #if defined(VERSION_SH) || defined(VERSION_CN)
   2110                     case 0x00:
   2111                         seqChannel->delay = loBits;
   2112                         goto out;
   2113 
   2114                     case 0x10:
   2115                         seqChannel->soundScriptIO[loBits] = -1;
   2116                         if (func_sh_802f47c8(seqChannel->bankId, (u8)value, &seqChannel->soundScriptIO[loBits]) == -1) {
   2117                         }
   2118                         break;
   2119 #else
   2120                     case 0x00: // chan_testlayerfinished
   2121                         if (seqChannel->layers[loBits] != NULL) {
   2122                             value = seqChannel->layers[loBits]->finished;
   2123                         }
   2124 #ifdef VERSION_EU
   2125                         else {
   2126                             value = -1;
   2127                         }
   2128 #endif
   2129                         break;
   2130 #endif
   2131 
   2132                     // sh: 0x70
   2133                     case 0x70: // chan_iowriteval; write data back to audio lib
   2134                         seqChannel->soundScriptIO[loBits] = value;
   2135                         break;
   2136 
   2137 #if defined(VERSION_SH) || defined(VERSION_CN)
   2138                     case 0x60:
   2139 #else
   2140                     case 0x80: // chan_ioreadval; read data from audio lib
   2141 #endif
   2142                         value = seqChannel->soundScriptIO[loBits];
   2143                         if (loBits < 4) {
   2144                             seqChannel->soundScriptIO[loBits] = -1;
   2145                         }
   2146                         break;
   2147 
   2148                     // sh: 0x50
   2149                     case 0x50: // chan_ioreadvalsub; subtract with read data from audio lib
   2150                         value -= seqChannel->soundScriptIO[loBits];
   2151                         break;
   2152 
   2153 #if !defined(VERSION_SH) && !defined(VERSION_CN)
   2154 #ifdef VERSION_EU
   2155                     // sh: 0x00
   2156                     case 0x60: // chan_delayshort
   2157                         seqChannel->delay = loBits;
   2158                         goto out;
   2159 #endif
   2160 
   2161                     case 0x90: // chan_setlayer
   2162                         sp5A = m64_read_s16(state);
   2163                         if (seq_channel_set_layer(seqChannel, loBits) == 0) {
   2164 #ifdef VERSION_EU
   2165                             if (1) {}
   2166 #endif
   2167                             seqChannel->layers[loBits]->scriptState.pc = seqPlayer->seqData + sp5A;
   2168                         }
   2169                         break;
   2170 
   2171                     case 0xa0: // chan_freelayer
   2172                         seq_channel_layer_free(seqChannel, loBits);
   2173                         break;
   2174 
   2175                     case 0xb0: // chan_dynsetlayer
   2176                         if (value != -1 && seq_channel_set_layer(seqChannel, loBits) != -1) {
   2177                             seqData = (*seqChannel->dynTable)[value];
   2178                             sp5A = ((seqData[0] << 8) + seqData[1]);
   2179                             seqChannel->layers[loBits]->scriptState.pc = seqPlayer->seqData + sp5A;
   2180                         }
   2181                         break;
   2182 
   2183 #ifndef VERSION_EU
   2184                     case 0x60: // chan_setnotepriority (arg must be >= 2)
   2185                         seqChannel->notePriority = loBits;
   2186                         break;
   2187 #endif
   2188 #endif
   2189 
   2190 #if defined(VERSION_SH) || defined(VERSION_CN)
   2191                     case 0x20:
   2192 #else
   2193                     case 0x10: // chan_startchannel
   2194 #endif
   2195                         sp5A = m64_read_s16(state);
   2196                         sequence_channel_enable(seqPlayer, loBits, seqPlayer->seqData + sp5A);
   2197                         break;
   2198 
   2199 #if !defined(VERSION_SH) && !defined(VERSION_CN)
   2200                     case 0x20: // chan_disablechannel
   2201                         sequence_channel_disable(seqPlayer->channels[loBits]);
   2202                         break;
   2203 #endif
   2204 
   2205                     case 0x30: // chan_iowriteval2; write data back to audio lib for another channel
   2206                         cmd = m64_read_u8(state);
   2207                         seqPlayer->channels[loBits]->soundScriptIO[cmd] = value;
   2208                         break;
   2209 
   2210                     case 0x40: // chan_ioreadval2; read data from audio lib from another channel
   2211                         cmd = m64_read_u8(state);
   2212                         value = seqPlayer->channels[loBits]->soundScriptIO[cmd];
   2213                         break;
   2214                 }
   2215 #if defined(VERSION_SH) || defined(VERSION_CN)
   2216                 }
   2217 #endif
   2218             }
   2219         }
   2220     }
   2221 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2222     out:
   2223 #endif
   2224 
   2225     for (i = 0; i < LAYERS_MAX; i++) {
   2226         if (seqChannel->layers[i] != 0) {
   2227             seq_channel_layer_process_script(seqChannel->layers[i]);
   2228         }
   2229     }
   2230 }
   2231 
   2232 void sequence_player_process_sequence(struct SequencePlayer *seqPlayer) {
   2233     u8 cmd;
   2234 #if defined(VERSION_SH) || defined(VERSION_CN)
   2235     UNUSED u32 pad;
   2236 #endif
   2237     u8 loBits;
   2238     u8 temp;
   2239     s32 value;
   2240     s32 i;
   2241     u16 u16v;
   2242     u8 *seqData;
   2243     struct M64ScriptState *state;
   2244 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2245     s32 temp32;
   2246 #endif
   2247 
   2248     if (seqPlayer->enabled == FALSE) {
   2249         return;
   2250     }
   2251 
   2252 #if !defined(VERSION_SH) && !defined(VERSION_CN)
   2253     if (seqPlayer->bankDmaInProgress == TRUE) {
   2254 #ifdef VERSION_EU
   2255         if (osRecvMesg(&seqPlayer->bankDmaMesgQueue, NULL, 0) == -1) {
   2256             return;
   2257         }
   2258         if (seqPlayer->bankDmaRemaining == 0) {
   2259             seqPlayer->bankDmaInProgress = FALSE;
   2260             patch_audio_bank(
   2261                 (struct AudioBank *) (gCtlEntries[seqPlayer->loadingBankId].instruments - 1),
   2262                 gAlTbl->seqArray[seqPlayer->loadingBankId].offset,
   2263                 gCtlEntries[seqPlayer->loadingBankId].numInstruments,
   2264                 gCtlEntries[seqPlayer->loadingBankId].numDrums);
   2265             gCtlEntries[seqPlayer->loadingBankId].drums =
   2266                 ((struct AudioBank *) (gCtlEntries[seqPlayer->loadingBankId].instruments - 1))->drums;
   2267             gBankLoadStatus[seqPlayer->loadingBankId] = SOUND_LOAD_STATUS_COMPLETE;
   2268         } else {
   2269             audio_dma_partial_copy_async(&seqPlayer->bankDmaCurrDevAddr, &seqPlayer->bankDmaCurrMemAddr,
   2270                                          &seqPlayer->bankDmaRemaining, &seqPlayer->bankDmaMesgQueue,
   2271                                          &seqPlayer->bankDmaIoMesg);
   2272         }
   2273 #else
   2274         if (seqPlayer->bankDmaMesg == NULL) {
   2275             return;
   2276         }
   2277         if (seqPlayer->bankDmaRemaining == 0) {
   2278             seqPlayer->bankDmaInProgress = FALSE;
   2279             patch_audio_bank(seqPlayer->loadingBank, gAlTbl->seqArray[seqPlayer->loadingBankId].offset,
   2280                              seqPlayer->loadingBankNumInstruments, seqPlayer->loadingBankNumDrums);
   2281             gCtlEntries[seqPlayer->loadingBankId].numInstruments = seqPlayer->loadingBankNumInstruments;
   2282             gCtlEntries[seqPlayer->loadingBankId].numDrums = seqPlayer->loadingBankNumDrums;
   2283             gCtlEntries[seqPlayer->loadingBankId].instruments = seqPlayer->loadingBank->instruments;
   2284             gCtlEntries[seqPlayer->loadingBankId].drums = seqPlayer->loadingBank->drums;
   2285             gBankLoadStatus[seqPlayer->loadingBankId] = SOUND_LOAD_STATUS_COMPLETE;
   2286         } else {
   2287             osCreateMesgQueue(&seqPlayer->bankDmaMesgQueue, &seqPlayer->bankDmaMesg, 1);
   2288             seqPlayer->bankDmaMesg = NULL;
   2289             audio_dma_partial_copy_async(&seqPlayer->bankDmaCurrDevAddr, &seqPlayer->bankDmaCurrMemAddr,
   2290                                          &seqPlayer->bankDmaRemaining, &seqPlayer->bankDmaMesgQueue,
   2291                                          &seqPlayer->bankDmaIoMesg);
   2292         }
   2293 #endif
   2294         return;
   2295     }
   2296 
   2297     if (seqPlayer->seqDmaInProgress == TRUE) {
   2298 #ifdef VERSION_EU
   2299         if (osRecvMesg(&seqPlayer->seqDmaMesgQueue, NULL, 0) == -1) {
   2300             return;
   2301         }
   2302 #ifndef AVOID_UB
   2303         if (temp) {
   2304         }
   2305 #endif
   2306 #else
   2307         if (seqPlayer->seqDmaMesg == NULL) {
   2308             return;
   2309         }
   2310 #endif
   2311         seqPlayer->seqDmaInProgress = FALSE;
   2312         gSeqLoadStatus[seqPlayer->seqId] = SOUND_LOAD_STATUS_COMPLETE;
   2313     }
   2314 #endif
   2315 
   2316     // If discarded, bail out.
   2317     if (IS_SEQ_LOAD_COMPLETE(seqPlayer->seqId) == FALSE
   2318         || (
   2319 #if defined(VERSION_SH) || defined(VERSION_CN)
   2320         seqPlayer->defaultBank[0] != 0xff &&
   2321 #endif
   2322         IS_BANK_LOAD_COMPLETE(seqPlayer->defaultBank[0]) == FALSE)) {
   2323         eu_stubbed_printf_1("Disappear Sequence or Bank %d\n", seqPlayer->seqId);
   2324         sequence_player_disable(seqPlayer);
   2325         return;
   2326     }
   2327 
   2328     // Remove possible SOUND_LOAD_STATUS_DISCARDABLE marks.
   2329 #if defined(VERSION_SH) || defined(VERSION_CN)
   2330     if (gSeqLoadStatus[seqPlayer->seqId] != 5)
   2331 #endif
   2332         gSeqLoadStatus[seqPlayer->seqId] = SOUND_LOAD_STATUS_COMPLETE;
   2333 
   2334 #if defined(VERSION_SH) || defined(VERSION_CN)
   2335     if (gBankLoadStatus[seqPlayer->defaultBank[0]] != 5)
   2336 #endif
   2337         gBankLoadStatus[seqPlayer->defaultBank[0]] = SOUND_LOAD_STATUS_COMPLETE;
   2338 
   2339     if (seqPlayer->muted && (seqPlayer->muteBehavior & MUTE_BEHAVIOR_STOP_SCRIPT) != 0) {
   2340         return;
   2341     }
   2342 
   2343     // Check if we surpass the number of ticks needed for a tatum, else stop.
   2344     seqPlayer->tempoAcc += seqPlayer->tempo;
   2345 #if defined(VERSION_SH) || defined(VERSION_CN)
   2346     seqPlayer->tempoAcc += seqPlayer->tempoAdd;
   2347 #endif
   2348     if (seqPlayer->tempoAcc < gTempoInternalToExternal) {
   2349         return;
   2350     }
   2351     seqPlayer->tempoAcc -= (u16) gTempoInternalToExternal;
   2352 
   2353     state = &seqPlayer->scriptState;
   2354     if (seqPlayer->delay > 1) {
   2355 #ifndef AVOID_UB
   2356         if (temp) {
   2357         }
   2358 #endif
   2359         seqPlayer->delay--;
   2360     } else {
   2361 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2362         seqPlayer->recalculateVolume = 1;
   2363 #endif
   2364         for (;;) {
   2365             cmd = m64_read_u8(state);
   2366             if (cmd == 0xff) { // seq_end
   2367                 if (state->depth == 0) {
   2368                     sequence_player_disable(seqPlayer);
   2369                     break;
   2370                 }
   2371 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2372                 state->pc = state->stack[--state->depth];
   2373 #else
   2374                 state->depth--, state->pc = state->stack[state->depth];
   2375 #endif
   2376             }
   2377 
   2378             if (cmd == 0xfd) { // seq_delay
   2379                 seqPlayer->delay = m64_read_compressed_u16(state);
   2380                 break;
   2381             }
   2382 
   2383             if (cmd == 0xfe) { // seq_delay1
   2384                 seqPlayer->delay = 1;
   2385                 break;
   2386             }
   2387 
   2388             if (cmd >= 0xc0) {
   2389                 switch (cmd) {
   2390                     case 0xff: // seq_end
   2391                         break;
   2392 
   2393                     case 0xfc: // seq_call
   2394                         u16v = m64_read_s16(state);
   2395                         if (0 && state->depth >= 4) {
   2396                             eu_stubbed_printf_0("Macro Level Over Error!\n");
   2397                         }
   2398 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2399                         state->stack[state->depth++] = state->pc;
   2400 #else
   2401                         state->depth++, state->stack[state->depth - 1] = state->pc;
   2402 #endif
   2403                         state->pc = seqPlayer->seqData + u16v;
   2404                         break;
   2405 
   2406                     case 0xf8: // seq_loop; loop start, N iterations (or 256 if N = 0)
   2407                         if (0 && state->depth >= 4) {
   2408                             eu_stubbed_printf_0("Macro Level Over Error!\n");
   2409                         }
   2410                         state->remLoopIters[state->depth] = m64_read_u8(state);
   2411 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2412                         state->stack[state->depth++] = state->pc;
   2413 #else
   2414                         state->depth++, state->stack[state->depth - 1] = state->pc;
   2415 #endif
   2416                         break;
   2417 
   2418                     case 0xf7: // seq_loopend
   2419                         state->remLoopIters[state->depth - 1]--;
   2420                         if (state->remLoopIters[state->depth - 1] != 0) {
   2421                             state->pc = state->stack[state->depth - 1];
   2422                         } else {
   2423                             state->depth--;
   2424                         }
   2425                         break;
   2426 
   2427                     case 0xfb: // seq_jump
   2428                     case 0xfa: // seq_beqz; jump if == 0
   2429                     case 0xf9: // seq_bltz; jump if < 0
   2430                     case 0xf5: // seq_bgez; jump if >= 0
   2431                         u16v = m64_read_s16(state);
   2432                         if (cmd == 0xfa && value != 0) {
   2433                             break;
   2434                         }
   2435                         if (cmd == 0xf9 && value >= 0) {
   2436                             break;
   2437                         }
   2438                         if (cmd == 0xf5 && value < 0) {
   2439                             break;
   2440                         }
   2441                         state->pc = seqPlayer->seqData + u16v;
   2442                         break;
   2443 
   2444 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2445                     case 0xf4:
   2446                     case 0xf3:
   2447                     case 0xf2:
   2448                         temp = m64_read_u8(state);
   2449                         if (cmd == 0xf3 && value != 0) {
   2450                             break;
   2451                         }
   2452                         if (cmd == 0xf2 && value >= 0) {
   2453                             break;
   2454                         }
   2455                         state->pc += (s8) temp;
   2456                         break;
   2457 #endif
   2458 
   2459 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2460                     case 0xf1: // seq_reservenotes
   2461 #else
   2462                     case 0xf2: // seq_reservenotes
   2463 #endif
   2464                         note_pool_clear(&seqPlayer->notePool);
   2465                         note_pool_fill(&seqPlayer->notePool, m64_read_u8(state));
   2466                         break;
   2467 
   2468 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2469                     case 0xf0: // seq_unreservenotes
   2470 #else
   2471                     case 0xf1: // seq_unreservenotes
   2472 #endif
   2473                         note_pool_clear(&seqPlayer->notePool);
   2474                         break;
   2475 
   2476                     case 0xdf: // seq_transpose; set transposition in semitones
   2477                         seqPlayer->transposition = 0;
   2478                         // fallthrough
   2479 
   2480                     case 0xde: // seq_transposerel; add transposition
   2481                         seqPlayer->transposition += (s8) m64_read_u8(state);
   2482                         break;
   2483 
   2484                     case 0xdd: // seq_settempo (bpm)
   2485 #if !defined(VERSION_SH) && !defined(VERSION_CN)
   2486                     case 0xdc: // seq_addtempo (bpm)
   2487 #endif
   2488 #if defined(VERSION_SH) || defined(VERSION_CN)
   2489                         seqPlayer->tempo = m64_read_u8(state) * TEMPO_SCALE;
   2490 #else
   2491                         temp = m64_read_u8(state);
   2492                         if (cmd == 0xdd) {
   2493                             seqPlayer->tempo = temp * TEMPO_SCALE;
   2494                         } else {
   2495                             seqPlayer->tempo += (s8) temp * TEMPO_SCALE;
   2496                         }
   2497 #endif
   2498 
   2499                         if (seqPlayer->tempo > gTempoInternalToExternal) {
   2500                             seqPlayer->tempo = gTempoInternalToExternal;
   2501                         }
   2502 
   2503                         //if (cmd) {}
   2504 
   2505                         if ((s16) seqPlayer->tempo <= 0) {
   2506                             seqPlayer->tempo = 1;
   2507                         }
   2508                         break;
   2509 
   2510 #if defined(VERSION_SH) || defined(VERSION_CN)
   2511                     case 0xdc: // seq_addtempo (bpm)
   2512                         seqPlayer->tempoAdd = (s8) m64_read_u8(state) * TEMPO_SCALE;
   2513                         break;
   2514 #endif
   2515 
   2516 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2517                     case 0xda:
   2518                         cmd = m64_read_u8(state);
   2519                         u16v = m64_read_s16(state);
   2520                         switch (cmd) {
   2521                             case SEQUENCE_PLAYER_STATE_0:
   2522                             case SEQUENCE_PLAYER_STATE_FADE_OUT:
   2523                                 if (seqPlayer->state != SEQUENCE_PLAYER_STATE_2) {
   2524                                     seqPlayer->fadeTimerUnkEu = u16v;
   2525                                     seqPlayer->state = cmd;
   2526                                 }
   2527                                 break;
   2528                             case SEQUENCE_PLAYER_STATE_2:
   2529                                 seqPlayer->fadeRemainingFrames = u16v;
   2530                                 seqPlayer->state = cmd;
   2531                                 seqPlayer->fadeVelocity =
   2532                                     (0.0f - seqPlayer->fadeVolume) / (s32)(u16v & 0xFFFFu);
   2533                                 break;
   2534                         }
   2535                         break;
   2536 
   2537                     case 0xdb:
   2538                         temp32 = m64_read_u8(state);
   2539                         switch (seqPlayer->state) {
   2540                             case SEQUENCE_PLAYER_STATE_2:
   2541                                 break;
   2542                             case SEQUENCE_PLAYER_STATE_FADE_OUT:
   2543                                 seqPlayer->state = SEQUENCE_PLAYER_STATE_0;
   2544                                 seqPlayer->fadeVolume = 0.0f;
   2545                                 // fallthrough
   2546                             case SEQUENCE_PLAYER_STATE_0:
   2547                                 seqPlayer->fadeRemainingFrames = seqPlayer->fadeTimerUnkEu;
   2548                                 if (seqPlayer->fadeTimerUnkEu != 0) {
   2549                                     seqPlayer->fadeVelocity = (temp32 / 127.0f - seqPlayer->fadeVolume) / FLOAT_CAST(seqPlayer->fadeRemainingFrames);
   2550                                 } else {
   2551                                     seqPlayer->fadeVolume = temp32 / 127.0f;
   2552                                 }
   2553                         }
   2554                         break;
   2555 #else
   2556                     case 0xdb: // seq_setvol
   2557                         cmd = m64_read_u8(state);
   2558                         switch (seqPlayer->state) {
   2559                             case SEQUENCE_PLAYER_STATE_2:
   2560                                 if (seqPlayer->fadeRemainingFrames != 0) {
   2561                                     f32 targetVolume = FLOAT_CAST(cmd) / US_FLOAT(127.0);
   2562                                     seqPlayer->fadeVelocity = (targetVolume - seqPlayer->fadeVolume)
   2563                                                               / FLOAT_CAST(seqPlayer->fadeRemainingFrames);
   2564                                     break;
   2565                                 }
   2566                                 // fallthrough
   2567                             case SEQUENCE_PLAYER_STATE_0:
   2568                                 seqPlayer->fadeVolume = FLOAT_CAST(cmd) / US_FLOAT(127.0);
   2569                                 break;
   2570                             case SEQUENCE_PLAYER_STATE_FADE_OUT:
   2571                             case SEQUENCE_PLAYER_STATE_4:
   2572                                 seqPlayer->volume = FLOAT_CAST(cmd) / US_FLOAT(127.0);
   2573                                 break;
   2574                         }
   2575                         break;
   2576 
   2577                     case 0xda: // seq_changevol
   2578                         temp = m64_read_u8(state);
   2579                         seqPlayer->fadeVolume =
   2580                             seqPlayer->fadeVolume + (f32)(s8) temp / US_FLOAT(127.0);
   2581                         break;
   2582 #endif
   2583 
   2584 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2585                     case 0xd9:
   2586                         temp = m64_read_u8(state);
   2587                         seqPlayer->fadeVolumeScale = (s8) temp / 127.0f;
   2588                         break;
   2589 #endif
   2590 
   2591                     case 0xd7: // seq_initchannels
   2592                         u16v = m64_read_s16(state);
   2593                         sequence_player_init_channels(seqPlayer, u16v);
   2594                         break;
   2595 
   2596                     case 0xd6: // seq_disablechannels
   2597                         u16v = m64_read_s16(state);
   2598                         sequence_player_disable_channels(seqPlayer, u16v);
   2599                         break;
   2600 
   2601                     case 0xd5: // seq_setmutescale
   2602                         temp = m64_read_u8(state);
   2603                         seqPlayer->muteVolumeScale = (f32)(s8) temp / US_FLOAT(127.0);
   2604                         break;
   2605 
   2606                     case 0xd4: // seq_mute
   2607                         seqPlayer->muted = TRUE;
   2608                         break;
   2609 
   2610                     case 0xd3: // seq_setmutebhv
   2611                         seqPlayer->muteBehavior = m64_read_u8(state);
   2612                         break;
   2613 
   2614                     case 0xd2: // seq_setshortnotevelocitytable
   2615                     case 0xd1: // seq_setshortnotedurationtable
   2616                         u16v = m64_read_s16(state);
   2617                         seqData = seqPlayer->seqData + u16v;
   2618                         if (cmd == 0xd2) {
   2619                             seqPlayer->shortNoteVelocityTable = seqData;
   2620                         } else {
   2621                             seqPlayer->shortNoteDurationTable = seqData;
   2622                         }
   2623                         break;
   2624 
   2625                     case 0xd0: // seq_setnoteallocationpolicy
   2626                         seqPlayer->noteAllocPolicy = m64_read_u8(state);
   2627                         break;
   2628 
   2629                     case 0xcc: // seq_setval
   2630                         value = m64_read_u8(state);
   2631                         break;
   2632 
   2633                     case 0xc9: // seq_bitand
   2634 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2635                         value &= m64_read_u8(state);
   2636 #else
   2637                         value = m64_read_u8(state) & value;
   2638 #endif
   2639                         break;
   2640 
   2641                     case 0xc8: // seq_subtract
   2642                         value = value - m64_read_u8(state);
   2643                         break;
   2644 
   2645 #if defined(VERSION_SH) || defined(VERSION_CN)
   2646                     case 0xc7:
   2647                         cmd = m64_read_u8(state);
   2648                         u16v = m64_read_s16(state);
   2649                         seqData = seqPlayer->seqData + u16v;
   2650                         *seqData = (u8)value + cmd;
   2651                         break;
   2652 
   2653                     case 0xc6:
   2654                         seqPlayer->unkSh = TRUE;
   2655                         return;
   2656 #endif
   2657 
   2658                     default:
   2659                         eu_stubbed_printf_1("Group:Undefine upper C0h command (%x)\n", cmd);
   2660                         break;
   2661                 }
   2662             } else {
   2663                 loBits = cmd & 0xf;
   2664                 switch (cmd & 0xf0) {
   2665                     case 0x00: // seq_testchdisabled
   2666 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2667                         value = seqPlayer->channels[loBits]->finished;
   2668 #else
   2669                         if (IS_SEQUENCE_CHANNEL_VALID(seqPlayer->channels[loBits]) == TRUE) {
   2670                             value = seqPlayer->channels[loBits]->finished;
   2671                         }
   2672 #endif
   2673                         break;
   2674                     case 0x10:
   2675                         break;
   2676                     case 0x20:
   2677                         break;
   2678                     case 0x40:
   2679                         break;
   2680                     case 0x50: // seq_subvariation
   2681 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2682                         value -= seqPlayer->seqVariationEu[0];
   2683 #else
   2684                         value -= seqPlayer->seqVariation;
   2685 #endif
   2686                         break;
   2687                     case 0x60:
   2688                         break;
   2689                     case 0x70: // seq_setvariation
   2690 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2691                         seqPlayer->seqVariationEu[0] = value;
   2692 #else
   2693                         seqPlayer->seqVariation = value;
   2694 #endif
   2695                         break;
   2696                     case 0x80: // seq_getvariation
   2697 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2698                         value = seqPlayer->seqVariationEu[0];
   2699 #else
   2700                         value = seqPlayer->seqVariation;
   2701 #endif
   2702                         break;
   2703                     case 0x90: // seq_startchannel
   2704                         u16v = m64_read_s16(state);
   2705                         sequence_channel_enable(seqPlayer, loBits, seqPlayer->seqData + u16v);
   2706                         break;
   2707                     case 0xa0:
   2708                         break;
   2709 #if defined(VERSION_JP) || defined(VERSION_US)
   2710                     case 0xd8: // (this makes no sense)
   2711                         break;
   2712                     case 0xd9:
   2713                         break;
   2714 #endif
   2715 
   2716                     default:
   2717                         eu_stubbed_printf_0("Group:Undefined Command\n");
   2718                         break;
   2719                 }
   2720             }
   2721         }
   2722     }
   2723 
   2724     for (i = 0; i < CHANNELS_MAX; i++) {
   2725 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2726         if (IS_SEQUENCE_CHANNEL_VALID(seqPlayer->channels[i]) == TRUE) {
   2727             sequence_channel_process_script(seqPlayer->channels[i]);
   2728         }
   2729 #else
   2730         if (seqPlayer->channels[i] != &gSequenceChannelNone) {
   2731             sequence_channel_process_script(seqPlayer->channels[i]);
   2732         }
   2733 #endif
   2734     }
   2735 }
   2736 
   2737 // This runs 240 times per second.
   2738 void process_sequences(UNUSED s32 iterationsRemaining) {
   2739     s32 i;
   2740     for (i = 0; i < SEQUENCE_PLAYERS; i++) {
   2741         if (gSequencePlayers[i].enabled == TRUE) {
   2742 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2743             sequence_player_process_sequence(&gSequencePlayers[i]);
   2744             sequence_player_process_sound(&gSequencePlayers[i]);
   2745 #else
   2746             sequence_player_process_sequence(gSequencePlayers + i);
   2747             sequence_player_process_sound(gSequencePlayers + i);
   2748 #endif
   2749         }
   2750     }
   2751 #if defined(VERSION_JP) || defined(VERSION_US)
   2752     reclaim_notes();
   2753 #endif
   2754     process_notes();
   2755 }
   2756 
   2757 void init_sequence_player(u32 player) {
   2758     struct SequencePlayer *seqPlayer = &gSequencePlayers[player];
   2759 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2760     sequence_player_disable(seqPlayer);
   2761 #endif
   2762 #if defined(VERSION_SH) || defined(VERSION_CN)
   2763     seqPlayer->unkSh = FALSE;
   2764 #else
   2765     seqPlayer->muted = FALSE;
   2766 #endif
   2767     seqPlayer->delay = 0;
   2768 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2769     seqPlayer->state = 1;
   2770 #else
   2771     seqPlayer->state = SEQUENCE_PLAYER_STATE_0;
   2772 #endif
   2773     seqPlayer->fadeRemainingFrames = 0;
   2774 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2775     seqPlayer->fadeTimerUnkEu = 0;
   2776 #endif
   2777     seqPlayer->tempoAcc = 0;
   2778     seqPlayer->tempo = 120 * TEMPO_SCALE; // 120 BPM
   2779 #if defined(VERSION_SH) || defined(VERSION_CN)
   2780     seqPlayer->tempoAdd = 0;
   2781 #endif
   2782     seqPlayer->transposition = 0;
   2783 #if !defined(VERSION_SH) && !defined(VERSION_CN)
   2784     seqPlayer->muteBehavior = MUTE_BEHAVIOR_STOP_SCRIPT | MUTE_BEHAVIOR_STOP_NOTES | MUTE_BEHAVIOR_SOFTEN;
   2785 #endif
   2786     seqPlayer->noteAllocPolicy = 0;
   2787     seqPlayer->shortNoteVelocityTable = gDefaultShortNoteVelocityTable;
   2788     seqPlayer->shortNoteDurationTable = gDefaultShortNoteDurationTable;
   2789     seqPlayer->fadeVolume = 1.0f;
   2790 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2791     seqPlayer->fadeVolumeScale = 1.0f;
   2792 #endif
   2793     seqPlayer->fadeVelocity = 0.0f;
   2794     seqPlayer->volume = 0.0f;
   2795     seqPlayer->muteVolumeScale = 0.5f;
   2796 }
   2797 
   2798 void init_sequence_players(void) {
   2799     // Initialization function, called from audio_init
   2800     s32 i, j;
   2801 
   2802     for (i = 0; i < ARRAY_COUNT(gSequenceChannels); i++) {
   2803         gSequenceChannels[i].seqPlayer = NULL;
   2804         gSequenceChannels[i].enabled = FALSE;
   2805 #if defined(VERSION_JP) || defined(VERSION_US)
   2806     }
   2807 
   2808     for (i = 0; i < ARRAY_COUNT(gSequenceChannels); i++) {
   2809 #endif
   2810         // @bug Size of wrong array. Zeroes out second half of gSequenceChannels[0],
   2811         // all of gSequenceChannels[1..31], and part of gSequenceLayers[0].
   2812         // However, this is only called at startup, so it's harmless.
   2813 #ifdef AVOID_UB
   2814 #define LAYERS_SIZE LAYERS_MAX
   2815 #else
   2816 #define LAYERS_SIZE ARRAY_COUNT(gSequenceLayers)
   2817 #endif
   2818         for (j = 0; j < LAYERS_SIZE; j++) {
   2819             gSequenceChannels[i].layers[j] = NULL;
   2820         }
   2821     }
   2822 
   2823     init_layer_freelist();
   2824 
   2825     for (i = 0; i < ARRAY_COUNT(gSequenceLayers); i++) {
   2826         gSequenceLayers[i].seqChannel = NULL;
   2827         gSequenceLayers[i].enabled = FALSE;
   2828     }
   2829 
   2830     for (i = 0; i < SEQUENCE_PLAYERS; i++) {
   2831         for (j = 0; j < CHANNELS_MAX; j++) {
   2832             gSequencePlayers[i].channels[j] = &gSequenceChannelNone;
   2833         }
   2834 
   2835 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
   2836         gSequencePlayers[i].seqVariationEu[0] = -1;
   2837 #else
   2838         gSequencePlayers[i].seqVariation = -1;
   2839 #endif
   2840 #if defined(VERSION_SH) || defined(VERSION_CN)
   2841         gSequencePlayers[i].muteBehavior = MUTE_BEHAVIOR_STOP_SCRIPT | MUTE_BEHAVIOR_STOP_NOTES | MUTE_BEHAVIOR_SOFTEN;
   2842         gSequencePlayers[i].enabled = FALSE;
   2843         gSequencePlayers[i].muted = FALSE;
   2844 #endif
   2845         gSequencePlayers[i].bankDmaInProgress = FALSE;
   2846         gSequencePlayers[i].seqDmaInProgress = FALSE;
   2847         init_note_lists(&gSequencePlayers[i].notePool);
   2848         init_sequence_player(i);
   2849     }
   2850 }
   2851