<?phpnamespace App\Alis;// Port of alis/src/audio/music_v2.c (MIT, Olivier Huguenot, Vadim Kindl): newer music variant (Ishar 1 Falcon/DOS// and later): sample voices (mv2_soundrout), STE chip music (mv2_chiprout), OPL2 (mv2_opl2rout).// The ALIS 2.x DOS driver (Bunny Bricks, Ishar 2 DOS): sequencer on the ~60 Hz game timer// (with --native-timing), effect F sets the speed to its raw parameter.function mv2_old_dos(): bool{ return Alis::$platform->kind == C::EPlatformPC && Alis::$platform->version < 30;}/** PORT: clears a sAudioVoice (C memset / field-by-field resets). */function mv2_voice_clear(sAudioVoice $v): void{ $v->freqsam = 0; $v->startsam1 = 0; $v->longsam1 = 0; $v->volsam = 0; $v->startsam2 = 0; $v->longsam2 = 0; $v->value = 0; $v->type = 0; $v->delta = 0;}/** * PORT: mv2a.trkval[i] as the C reads it. The table is never written (all 0) and mv2_soundins() can index past * its end, into the next fields of sMV2Audio: they are mapped here (s16 view of the struct), the rest reads 0. */function mv2_trkval_at(int $i): int{ if ($i >= 0 && $i < 36) { return s16(MusicV2Vars::$mv2a->trkval[$i]); } $a = MusicV2Vars::$mv2a; switch ($i - 36) { case 0: return s16($a->prevmufreq); case 1: return s16($a->prevmuvol); case 2: return s16($a->mutype); case 3: return s16($a->mufreq); case 4: return s16($a->muchip); case 5: return s16($a->muopl2); case 6: return s16($a->muptr); case 7: return s16($a->mumax); case 8: return s16($a->mucnt); case 9: return s16($a->mubufa); case 10: return s16($a->mubufc); case 11: return s16($a->muspeed); case 12: return s16($a->muvolgen); case 13: return s16($a->mubreak); case 14: return s16($a->mutadata); case 15: return s16($a->frqmod); case 16: return s16($a->frqmod >> 16); case 17: return s16($a->samples); case 18: return s16($a->chipmixer); } return 0;}/** PORT: audio.tabinst[i] for the index the C computes (out of the table: no instrument). */function mv2_inst(int $i): ?sAudioInstrument{ return ($i >= 0 && $i < 128) ? Aud::$tabinst[$i] : null;}function mv2_gomusic(): void{ $mv2a = MusicV2Vars::$mv2a; if (Aud::$host_freq <= 0) { audio_output_init(SysAudioVars::$freq, SysAudioVars::$channels); // PORT: the C opens the device first } Aud::$muflag = 0; $chipinst = 0; $opl2inst = 0; for ($i = 0; $i < 0x20; $i++) { $instrument = Aud::$tabinst[$i]; if ($instrument->address != 0) { $itype = xread8($instrument->address - 0x10); if ($itype == 5) { $chipinst++; } elseif ($itype == 6) { $opl2inst++; } } } $mv2a->muchip = (int) (2 < $chipinst); $mv2a->muopl2 = (int) (2 < $opl2inst); $mv2a->mutype = (int) (2 >= $chipinst); Aud::$muvol = ((Aud::$muvolume >> 1) + 1) & 0xff; Aud::$mutemp = ((Aud::$mutempo * 6) >> 5) & 0xff; // init if ($mv2a->muopl2) { sys_init_opl(); for ($i = 0; $i < 4; $i++) { mv2_voice_clear($mv2a->voices[$i]); } $mv2a->prevmuvol = 0; $mv2a->prevmufreq = 0; mv2_calculfrq(); mv2_calculvol(); // mutaloop is sized for a 50 Hz tick; rescale to the music rate (60 Hz DOS with --native-timing). set_mutaloop(intdiv(Aud::$mutaloop * 50, sys_music_hz())); Aud::$soundrout = __NAMESPACE__ . '\\mv2_opl2rout'; } elseif ($mv2a->mutype != 0) { for ($i = 0; $i < 4; $i++) { mv2_voice_clear($mv2a->voices[$i]); } $mv2a->prevmuvol = 0; $mv2a->prevmufreq = 0; mv2_calculfrq(); mv2_calculvol(); if (mv2_old_dos()) { set_mutaloop(intdiv(Aud::$mutaloop * 50, sys_music_hz())); } Aud::$soundrout = __NAMESPACE__ . '\\mv2_soundrout'; } else { for ($i = 0; $i < 0xc; $i++) { MusicV2Vars::$chipdata[$i * 4 + 2] = 0; MusicV2Vars::$chipdata[$i * 4 + 3] = 0; } for ($i = 0; $i < 3; $i++) { $mv2a->chipch[$i] = new sChipChannel(); } $mv2a->chipmixer = -1; $mv2a->prevmuvol = 0; $mv2a->prevmufreq = 0; mv2_calculfrq(); mv2_calculvol(); Aud::$soundrout = __NAMESPACE__ . '\\mv2_chiprout'; } for ($i = 0; $i < 0x20; $i++) { $mv2a->defvol[$i] = 0x40; } $mv2a->mumax = xread8(Aud::$mupnote); Aud::$mupnote = s32(Aud::$mupnote + 2); $mv2a->mubreak = 0; $mv2a->muptr = 0; $mv2a->mucnt = 0; $mv2a->mubufa = Aud::$muattac; $mv2a->mubufc = Aud::$muchute; $mv2a->muspeed = 1; Aud::$smpidx = 0; Aud::$muflag = 1;}function mv2_calculfrq(): void{ $mv2a = MusicV2Vars::$mv2a; // Fixed-point: ratio = 50000 / host_freq (16.16) // frqmod = ratio * 0x48D378 = 50000 * 0x48D378 / host_freq // samples = 0x3E7 / ratio = 0x3E7 * host_freq / 50000 // (u64 products: done in floats, exact below 2^53, floor of an exact quotient) $mv2a->frqmod = s32((int) floor(50000.0 * 0x48D378 / Aud::$host_freq)); $mv2a->samples = intdiv(0x3E7 * Aud::$host_freq, 50000) & 0xffff; $freq = 4; if ($freq != $mv2a->prevmufreq) { $freqdata = MusicV2Vars::mv2_trkfrq_ste[$freq - 1]; $mv2a->mutadata = $freqdata[0]; set_mutaloop($mv2a->samples + 1); $mv2a->prevmufreq = $freq; $index = 0; for ($i = 1; $i < 0x358; $i++, $index++) { $mv2a->tabfrq[$index] = intdiv($mv2a->frqmod, $i); } }}function mv2_calculvol(): void{ // The original also built a volume table here; nothing reads it (mixers scale inline). if (MusicV2Vars::$mv2a->prevmuvol == 0) { MusicV2Vars::$mv2a->prevmuvol = -1; }}// Stereo mix, default amiga paula layout (voices 0,3 left / 1,2 right)// falcon's own 0,2 / 1,3. 30% cross-feedfunction mv2_soundcal_stereo(): void{ $mv2a = MusicV2Vars::$mv2a; $l2 = Alis::$platform->kind == C::EPlatformFalcon ? 2 : 3; $n = Aud::$mutaloop; $r = AudioVars::$muright; for ($i = 0; $i < $n; $i++) { $r[$i] = 0; } mv2_soundcal($mv2a->voices[0], Aud::$muadresse, true); mv2_soundcal($mv2a->voices[1], $r, true); mv2_soundcal($mv2a->voices[$l2], Aud::$muadresse, false); mv2_soundcal($mv2a->voices[5 - $l2], $r, false); $l = &Aud::$muadresse; for ($i = 0; $i < $n; $i++) { $li = $l[$i]; $ri = $r[$i]; $a = $li + (($ri * 77) >> 8); $b = $ri + (($li * 77) >> 8); $l[$i] = $a < -32768 ? -32768 : ($a > 32767 ? 32767 : $a); $r[$i] = $b < -32768 ? -32768 : ($b > 32767 ? 32767 : $b); } AudioVars::$muright = $r;}/** The volume ramps shared by the three routines; returns false when the music stops (C: return). */function mv2_volgen(int $prevmuspeed): bool{ $mv2a = MusicV2Vars::$mv2a; $newvolgen = Aud::$muvol; if ($mv2a->mubreak == 0) { if (Aud::$muattac == 0) { if (Aud::$muduree == 0) { goto f_soundroutc; } if (Aud::$muduree > 0) { Aud::$muduree = s16(Aud::$muduree - 1); } } else { Aud::$muattac = (Aud::$muattac - 1) & 0xffff; $tmpvol = $mv2a->mubufa ? intdiv(Aud::$muvol * Aud::$muattac, $mv2a->mubufa) & 0xffff : 0; $newvolgen = (($tmpvol & 0xff00) | -(s8($tmpvol) - Aud::$muvol)) & 0xffff; } goto done; } f_soundroutc: if (Aud::$muchute != 0) { Aud::$muchute = (Aud::$muchute - 1) & 0xffff; $newvolgen = $mv2a->mubufc ? intdiv(Aud::$muvol * Aud::$muchute, $mv2a->mubufc) & 0xffff : 0; } else { mv2_stopmusic(); return false; } done: $mv2a->muvolgen = $newvolgen; // Speed 0 (effect F00 = end of song): the original stops the music. if ($mv2a->muspeed == 0) { mv2_stopmusic(); return false; } return true;}/** u32 noteat of the current row (C: mucnt * 0x10 + pattern * 0x400 + mupnote + 0x84). */function mv2_noteat(): int{ $mv2a = MusicV2Vars::$mv2a; return s32($mv2a->mucnt * 0x10 + xread8(Aud::$mupnote + $mv2a->muptr) * 0x400 + Aud::$mupnote + 0x84);}/** The row advance shared by the routines (C: mucnt / muptr wrap). */function mv2_nextrow(): void{ $mv2a = MusicV2Vars::$mv2a; $mv2a->muspeed = Aud::$mutemp; if (-1 < s16($mv2a->mucnt - 0x40)) { $mv2a->mucnt = 0; $mv2a->muptr = ($mv2a->muptr + 1) & 0xffff; } if (-1 < s16($mv2a->muptr - $mv2a->mumax)) { $mv2a->muptr = 0; }}function mv2_soundrout(): void{ $mv2a = MusicV2Vars::$mv2a; $prevmuspeed = $mv2a->muspeed; if (Aud::$muflag == 0) { return; } if (!mv2_volgen($prevmuspeed)) { return; } $mv2a->muspeed = ($mv2a->muspeed - 1) & 0xffff; if ($mv2a->muspeed == 0 || s16($prevmuspeed) < 1) { mv2_nextrow(); $noteat = mv2_noteat(); $noteat = mv2_soundvoix($noteat, $mv2a->voices[0]); $noteat = mv2_soundvoix($noteat, $mv2a->voices[1]); $noteat = mv2_soundvoix($noteat, $mv2a->voices[2]); $noteat = mv2_soundvoix($noteat, $mv2a->voices[3]); $mv2a->mucnt = ($mv2a->mucnt + 1) & 0xffff; } mv2_checkefft($mv2a->voices[0]); mv2_checkefft($mv2a->voices[1]); mv2_checkefft($mv2a->voices[2]); mv2_checkefft($mv2a->voices[3]); // --- Sample mixing: CPU (original mixing path) --- $n = Aud::$mutaloop; $mu = &Aud::$muadresse; for ($i = 0; $i < $n; $i++) { $mu[$i] = 0; } unset($mu); if (AudioVars::$mustereo) { mv2_soundcal_stereo(); } else { mv2_soundcal($mv2a->voices[0], Aud::$muadresse, true); mv2_soundcal($mv2a->voices[1], Aud::$muadresse, false); mv2_soundcal($mv2a->voices[2], Aud::$muadresse, false); mv2_soundcal($mv2a->voices[3], Aud::$muadresse, false); }}function mv2_soundvoix(int $noteat, sAudioVoice $voice): int{ $notedata = xread32be($noteat); if ($notedata != 0) { $voice->value = s16($notedata >> 0x10); $voice->type = ($notedata >> 8) & 0xff; $voice->delta = $notedata & 0xff; } $nextat = s32($noteat + 2); $newfreq = xread16be($noteat); if ($newfreq != 0) { $instidx = xread8($nextat); $instidx &= 0xf0; if (BIT_CHK($newfreq, 0xc)) { $newfreq &= 0xfff; $instidx |= 0x100; } $instidx >>= 1; $instidx = ($instidx - 8) & 0xffff; $instidx >>= 3; mv2_checkport($nextat, $voice); mv2_soundins($voice, $newfreq, $instidx); } $volsam = $voice->volsam; mv2_checkcom($nextat, $volsam); $voice->volsam = $volsam; return s32($nextat + 2);}function mv2_checkport(int $noteat, sAudioVoice $voice): void{ $d2b = xread8($noteat) & 0xf; if ($d2b == 3 && $voice->freqsam != 0 && -1 < s16($voice->freqsam - $voice->value)) { $d2b = (-xread8($noteat + 1)) & 0xff; $voice->delta = $d2b; }}function mv2_soundins(sAudioVoice $voice, int $newfreq, int $instidx): void{ // PORT: an index past tabinst[128] (instrument nibble 0) reads garbage in the C: no instrument here $inst = mv2_inst($instidx); $sample = $inst ? $inst->address : 0; $tval = $inst ? $inst->unknown : 0; if ($tval != 0) { $tval = s32($tval + $tval); $i = 0; for (; $i < 0x24; $i++) { if ($newfreq == mv2_trkval_at($i)) { break; } } $newfreq = mv2_trkval_at($i + $tval); } if (s16($newfreq - 0x71) < 0) { $newfreq = 0x71; } if (0x357 < $newfreq) { $newfreq = 0x357; } $voice->freqsam = $newfreq & 0xffff; $type = $sample == 0 ? -1 : xread8($sample - 0x10); if ($type == 2) { $voice->sample = s32($sample - 0x10); // the original reverses samples and reads start + longsam, we read forward as end - longsam $len = xread32($sample - 0xe); $lend = xread32($sample - 4); if (ult(s32($len - 0x20), $lend)) { $lend = s32($len - 0x20); } // loop is +8, raw +0xc as in the DOS versions if ($lend) { $lend = s32($lend + 0x10); } $voice->longsam2 = s32($lend - xread32($sample - 8)); $voice->startsam2 = s32($sample + $lend - 1); // attack runs up to the loop end, so the wrap is seamless $aend = ($voice->longsam2 > 0) ? $lend : s32($len - 0x20); $voice->startsam1 = s32($sample + $aend - 1); $voice->longsam1 = $aend; $voice->volsam = 0x40; $voice->frqto = 0; } elseif ($type == 5 || $type == 6) { // OPL2: NOP } else { mv2_voice_clear($voice); $voice->loopsam = 0; }}function mv2_checkcom(int $noteat, int &$volsam): void{ $mv2a = MusicV2Vars::$mv2a; $data = xread8($noteat + 1); $type = xread8($noteat) & 0xf; switch ($type) { case 0xb: $mv2a->muptr = ($data - 1) & 0xffff; $mv2a->mucnt = 0x40; break; case 0xc: if (0x40 < $data) { $data = 0; } $volsam = $data; break; case 0xd: $mv2a->mucnt = 0x40; break; case 0xf: // Bunny Bricks' driver skips F00 (its song then loops); later engines stop on speed 0. if ($data == 0 && Alis::$platform->kind == C::EPlatformPC && Alis::$platform->uid == C::EGameBunnyBricks) { break; } $newval = mv2_old_dos() ? $data : ((Aud::$mutempo * ($data & 0x1f)) >> 5); Aud::$mutemp = $newval & 0xff; $mv2a->muspeed = $newval & 0xffff; break; }}function mv2_checkefft(sAudioVoice $voice): void{ $newval = $voice->value & 0xffff; $delta = $voice->delta & 0xff; $type = $voice->type & 0xf; switch ($type) { case 0x1: $newval = ($voice->freqsam - $delta) & 0xffff; if (s16($newval - 0x71) < 0) { $newval = 0x71; $voice->value = 0; $voice->type = 0; $voice->delta = 0; } $voice->freqsam = $newval; break; case 0x2: $newval = ($voice->freqsam + $delta) & 0xffff; if (-1 < s16($newval - 0x357)) { $newval = 0x357; $voice->value = 0; $voice->type = 0; $voice->delta = 0; } $voice->freqsam = $newval; break; case 0x3: // ((s16)delta < 0 is never true: delta is a u8 widened to u16) $newfreq = ($voice->freqsam + $delta) & 0xffff; if (-1 < s16($newfreq - $newval)) { $newfreq = $newval; } $voice->freqsam = $newfreq; break; case 0x5: case 0x6: case 0xa: if ($delta >> 4 == 0) { $voice->volsam = ($voice->volsam - ($delta & 0xf)) & 0xffff; if (s16($voice->volsam) < 0) { $voice->volsam = 0; } } else { $voice->volsam = (($delta >> 4) + $voice->volsam) & 0xffff; if (0x40 < $voice->volsam) { $voice->volsam = 0x40; } } break; }}function mv2_offmusic(int $much): void{ Aud::$muchute = $much & 0xffff; if (Aud::$muchute == 0) { mv2_stopmusic(); } else { Aud::$muvol = MusicV2Vars::$mv2a->muvolgen & 0xff; MusicV2Vars::$mv2a->mubufc = Aud::$muchute; MusicV2Vars::$mv2a->mubreak = 1; }}function mv2_stopmusic(): void{ Aud::$muflag = 0;}function mv2_onmusic(): void{ MusicV2Vars::$mv2a->mubufa = Aud::$muattac; Aud::$muflag = 1;}function f_volume(int $volsam): int{ // ST/STE $volume = s16(((($volsam * MusicV2Vars::$mv2a->muvolgen) & 0xffff) >> 6) - 1); return $volume < 0 ? 0 : s16($volume << 8);}// a1 - 0x16function f_updatevoice(sAudioVoice $voice, int $smpstart, int $smplength): void{ if ($voice->loopsam == 0) { mv2_voice_clear($voice); $voice->loopsam = 0; } else { $voice->startsam1 = $smpstart; $voice->longsam1 = $smplength; }}/** * Pure-C mixer (the original decompiled body). PORT: same arithmetic with the 16.16 position split in 16-bit * halves (no 32-bit overflow on 32-bit PHP) and the sample bytes scaled through a 256-entry table. */function mv2_soundcal_c(sAudioVoice $voice, array &$dst, bool $first): void{ $mv2a = MusicV2Vars::$mv2a; $freqsam = s16($voice->freqsam); if ($freqsam < 0) { $freqsam = -$freqsam; $freqsam = $freqsam >> 2; $freq = ($mv2a->tabfrq[$freqsam] ?? 0) >> 2; } else { $freq = $mv2a->tabfrq[$freqsam] ?? 0; } $startsam1 = $voice->startsam1; $longsam1 = $voice->longsam1; $volsam = f_volume($voice->volsam) & 0xffff; $startsam2 = $voice->startsam2; $longsam2 = s32($voice->longsam2 - 1); $frqlo = $freq & 0xffff; $frqhi = ($freq >> 16) & 0xffff; if ($longsam2 < 1) { $longsam2 = 0; $freq = 0; } $vol_fixed = ($volsam >> 8) & 0xff; $frqto = $voice->frqto; $sample = $voice->sample; $lengthX = s32(xread32($sample + 2) - 0x11); $smpendX = s32($sample + 0x10 + $lengthX); if (!$sample || ult(Alis::$finmem, $smpendX)) { return; // no sample, or a stale header (script data moved) } if (ult($startsam1, s32($sample + 0x10)) || ult($smpendX, $startsam1)) { return; } $mem = Alis::$mem; $memlen = strlen($mem); $mutaloop = Aud::$mutaloop; $vt = []; for ($k = 0; $k < 256; $k++) { $vt[$k] = ((($k ^ 0x80) - 0x80) * $vol_fixed); } // position = longsam1 (32 bits) : frqto (16 bits); the step only borrows from the low 16 bits of longsam1 $L = $longsam1 & 0xffff; $H = ($longsam1 >> 16) & 0xffff; $base = $startsam1 - ($H << 16); $SH = $frqhi; $SL = $frqlo; $l2lo = $longsam2 & 0xffff; $l2hi = ($longsam2 >> 16) & 0xffff; $ok = $base >= 0 && $startsam1 < $memlen; $sw2ok = ($startsam2 - ($l2hi << 16)) >= 0 && $startsam2 < $memlen; for ($index = 0; $index < $mutaloop; $index++) { $lo = $frqto - $SL; if ($lo < 0) { $frqto = $lo + 0x10000; $mid = $L - $SH - 1; } else { $frqto = $lo; $mid = $L - $SH; } if ($mid < 0) { // phase switch: (step stays freq; longsam2 == 0 stops) $L = $l2lo; $H = $l2hi; $startsam1 = $startsam2; $base = $startsam1 - ($H << 16); $ok = $sw2ok; if ($longsam2 == 0) { break; } } else { $L = $mid; } if (!$ok) { break; // PORT: the C would read outside its memory here } $v = $vt[ord($mem[$base - $L])]; if ($first) { $dst[$index] = $v; } else { $total = $dst[$index] + $v; $dst[$index] = $total < -32768 ? -32768 : ($total > 32767 ? 32767 : $total); } } $voice->longsam1 = s32(($H << 16) | $L); $voice->startsam1 = $startsam1; $voice->frqto = $frqto & 0xffff;}function mv2_soundcal(sAudioVoice $voice, array &$dst, bool $first): void{ mv2_soundcal_c($voice, $dst, $first);}// Atari STE chipmusicfunction mv2_chiprout(): void{ $mv2a = MusicV2Vars::$mv2a; $prevmuspeed = $mv2a->muspeed; if (Aud::$muflag == 0) { return; } if ($mv2a->mutype != 0) { return; } // (chiprout decrements muduree without the "> 0" test of the two other routines) $newvolgen = Aud::$muvol; if ($mv2a->mubreak == 0) { if (Aud::$muattac == 0) { if (Aud::$muduree == 0) { goto f_chiprouttc; } Aud::$muduree = s16(Aud::$muduree - 1); } else { Aud::$muattac = (Aud::$muattac - 1) & 0xffff; $tmpvol = $mv2a->mubufa ? intdiv(Aud::$muvol * Aud::$muattac, $mv2a->mubufa) & 0xffff : 0; $newvolgen = (($tmpvol & 0xff00) | -(s8($tmpvol) - Aud::$muvol)) & 0xffff; } goto chipdone; } f_chiprouttc: if (Aud::$muchute != 0) { Aud::$muchute = (Aud::$muchute - 1) & 0xffff; $newvolgen = $mv2a->mubufc ? intdiv(Aud::$muvol * Aud::$muchute, $mv2a->mubufc) & 0xffff : 0; } else { mv2_stopmusic(); return; } chipdone: $mv2a->muvolgen = $newvolgen; // Speed 0 (effect F00 = end of song): the original stops the music. if ($mv2a->muspeed == 0) { mv2_stopmusic(); return; } $mv2a->muspeed = ($mv2a->muspeed - 1) & 0xffff; if ($mv2a->muspeed == 0 || $prevmuspeed < 1) { // C: (s16)(prevmuspeed < 1) mv2_nextrow(); $noteat = mv2_noteat(); if ($mv2a->muchip != 0) { $noteat += 4; } $noteat = mv2_chipvoix($noteat, $mv2a->chipch[0]); $noteat = mv2_chipvoix($noteat, $mv2a->chipch[1]); $noteat = mv2_chipvoix($noteat, $mv2a->chipch[2]); $mv2a->mucnt = ($mv2a->mucnt + 1) & 0xffff; } mv2_chipcanal($mv2a->chipch[0], 0); mv2_chipcanal($mv2a->chipch[1], 1); mv2_chipcanal($mv2a->chipch[2], 2); MusicV2Vars::$chipdata[0x1e] = $mv2a->chipmixer & 0xff; for ($i = 0; $i < 13; $i++) { sys_write_psg(MusicV2Vars::$chipdata[$i * 4], MusicV2Vars::$chipdata[4 * $i + 2]); } sys_calc_psg_music();}function mv2_chipinstr(sChipChannel $chanel, int $idx): int{ $idx = s16($idx); if (s16($idx + 0x18) < 0) { $idx = -0x18; } if (-1 < s16($idx - 0xa8)) { $idx = 0xa8; } $result = s16(MusicV2Vars::trkval[48 + intdiv($idx, 2)] ?? 0); $alt = s16(MusicV2Vars::trkval[48 + s8($chanel->unknown4)] ?? 0); $newuA = s16($chanel->unknownA); if ($chanel->notedata != 0) { // (chanel->notedata is a u16: the "< 0" branch of the C never runs) $newuA = s16($newuA + $chanel->notedata); $result = s16($result + $newuA); if (-1 < s16($result - $alt)) { $chanel->notedata = 0; $chanel->tvalue = $chanel->unknown4; $chanel->unknown4 = 0; $newuA = 0; $result = $alt; } $chanel->unknownA = $newuA & 0xffff; } return $result;}function rotl(int $rotate, int $value): int{ if (($rotate &= 31) == 0) { return s32($value); } return s32(shl32($value, $rotate) | shr32($value, 32 - $rotate));}function mv2_chipcanal(sChipChannel $chanel, int $idx): void{ $mv2a = MusicV2Vars::$mv2a; $cd = &MusicV2Vars::$chipdata; $idx1 = $idx * 8; $idx2 = 0x22 + $idx * 4; $tval = $chanel->tvalue; $address = $chanel->address1; if ($address != 0) { $guard = 0; while (($type = xread8($address)) != 0xff) { if (++$guard > 0x10000) { break; // PORT: a looping instrument list would hang the C audio thread } $address++; if ($type == 0xf5) { $mv2a->chipmixer |= 1 << $idx; $mv2a->chipmixer |= 1 << ($idx + 3); $type = xread8($address); $address++; } else { if ($type == 0xfd) { $mv2a->chipmixer |= 1 << $idx; $mv2a->chipmixer &= ~(1 << ($idx + 3)); $type = xread8($address); $address++; } if ($type == 0xfe) { $mv2a->chipmixer &= ~(1 << $idx); $mv2a->chipmixer |= 1 << ($idx + 3); $type = xread8($address); $address++; } if ($type == 0xfc) { $mv2a->chipmixer &= ~(1 << $idx); $mv2a->chipmixer &= ~(1 << ($idx + 3)); $type = xread8($address); $address++; } } $mv2a->chipmixer = s16($mv2a->chipmixer); if ($type == 0xfb) { $cd[0x1a] = xread8($address); $address++; $type = xread8($address); $address++; } if ($type != 0xfa) { if ($type == 0xf9) { $cd[$idx2] = 0x10; $cd[0x32] = 0; $cd[0x2e] = xread8($address); $address++; $cd[$idx1 + 6] = xread8($address); $address++; $cd[$idx1 + 2] = xread8($address); $address++; } elseif ($type == 0xf8) { $cd[$idx2] = 0x10; $cd[$idx1 + 6] = xread8($address); $address++; $cd[$idx1 + 2] = xread8($address); $address++; $result = mv2_chipinstr($chanel, s8(xread8($address) + s8($tval)) * 2); $address++; $cd[0x2e] = $result & 0xff; $cd[0x32] = ($result >> 8) & 0xff; } elseif ($type == 0xf7) { $cd[$idx2] = 0x10; $cd[0x32] = xread8($address); $address++; $cd[0x2e] = xread8($address); $address++; $result = mv2_chipinstr($chanel, s8(xread8($address) + s8($tval)) * 2); $address++; $cd[$idx1 + 2] = $result & 0xff; $cd[$idx1 + 6] = ($result >> 8) & 0xff; } elseif ($type == 0xf6) { $cd[$idx2] = 0x10; $result = mv2_chipinstr($chanel, s8(xread8($address) + s8($tval)) * 2); $address++; $cd[$idx1 + 2] = $result & 0xff; $cd[$idx1 + 6] = ($result >> 8) & 0xff; $rot = xread8($address); $address++; $rotres = rotl($rot & 0xff, s32($result)) & 0xffff; $cd[0x2e] = $rotres & 0xff; $cd[0x32] = ($rotres >> 8) & 0xff; } else { $add = xread8($address); $address++; $cd[$idx2] = intdiv($type * ((($mv2a->muvolgen * 10) & 0xffff) >> 6), 0xf) & 0xff; $result = mv2_chipinstr($chanel, s8(xread8($address) + s8($tval)) * 2); $address++; $result = s16($result + $add); $cd[$idx1 + 2] = $result & 0xff; $cd[$idx1 + 6] = ($result >> 8) & 0xff; } $chanel->address1 = s32($address); break; } $address = s32($chanel->address2 + xread8($address)); } }}function mv2_chipvoix(int $noteat, sChipChannel $chanel): int{ $address = 0; $guard = 0; do { $test = xread16be($noteat); $noteat = s32($noteat + 2); if ($test == 0) { goto chipvoixend; } $i = 0; for (; $i < 36; $i++) { if ($test == MusicV2Vars::trkval[$i]) { break; } } $instidx = (((xread8($noteat) & 0xf0) >> 1) - 8) >> 3; $inst = mv2_inst($instidx); // PORT: tabinst[-1] (nibble 0) reads garbage in the C: no instrument $address = $inst ? $inst->address : 0; $tval = s16(($inst ? $inst->unknown : 0) + $i); if ((xread8($noteat) & 0xf) == 3) { $chanel->unknown4 = $tval; $notedata = xread8($noteat + 1); if (s16($chanel->tvalue - $chanel->unknown4) < 0) { $notedata = (-$notedata) & 0xffff; } $chanel->notedata = $notedata; goto chipvoixend; } $chanel->unknown4 = 0; $chanel->notedata = 0; $chanel->unknownA = 0; $chanel->tvalue = $tval; } while ($address == 0 && ++$guard < 0x10000); if (xread8($address - 0x10) == 5) { $chanel->address1 = $address; $chanel->address2 = $address; chipvoixend: $volume = $chanel->volume; mv2_checkcom($noteat, $volume); $chanel->volume = $volume; } return s32($noteat + 2);}// OPL2 musicfunction mv2_opl2_write_op(int $op_offset, int $data, int $global_vol, int $scale_vol): void{ // 13-byte operator parameter block at $data (offset in Alis::$mem) $ksl = xread8($data + 0); $mul = xread8($data + 1); $ar = xread8($data + 3); $sl = xread8($data + 4); $eg = xread8($data + 5); $dr = xread8($data + 6); $rr = xread8($data + 7); $tl = xread8($data + 8); $am = xread8($data + 9); $vib = xread8($data + 10); $ksr = xread8($data + 11); // scale TL by global volume $adjusted_tl = $tl; if ($scale_vol) { $adjusted_tl = (0x3f - (((($global_vol & 0xffff) * ((0x3f - $tl) & 0xffff)) >> 6) & 0xff)) & 0xff; } // Register 0x40: KSL(2) + TL(6) $reg40 = ((($ksl & 0x03) << 6) | ($adjusted_tl & 0x3f)) & 0xff; // Register 0x20: AM(1) VIB(1) EG(1) KSR(1) MUL(4) $reg20 = (($mul & 0x0f) | (($eg & 1) << 5) | (($am & 1) << 7) | (($vib & 1) << 6) | (($ksr & 1) << 4)) & 0xff; // Register 0x60: AR(4) + DR(4) $reg60 = ((($ar & 0x0f) << 4) | ($dr & 0x0f)) & 0xff; // Register 0x80: SL(4) + RR(4) $reg80 = ((($sl & 0x0f) << 4) | ($rr & 0x0f)) & 0xff; sys_write_opl(0x40 + $op_offset, $reg40); sys_write_opl(0x20 + $op_offset, $reg20); sys_write_opl(0x60 + $op_offset, $reg60); sys_write_opl(0x80 + $op_offset, $reg80); // NOTE: Waveform (0xE0) is written separately in mv2_opl2_setinst}function mv2_opl2_setinst(int $sample, int $channel): void{ $channel &= 0xff; if ($channel >= 9) { return; } // Instrument: 0-1 header (channel override), 2-14 modulator, 15-27 carrier, // 28/29 modulator/carrier waveform (reg 0xE0). $mod_data = $sample + 2; $car_data = $sample + 15; $mod_offset = MusicV2Vars::opl2_op_offset[$channel][0]; $car_offset = MusicV2Vars::opl2_op_offset[$channel][1]; $global_vol = MusicV2Vars::$mv2a->muvolgen & 0xff; // Connection/algorithm from modulator block byte 12 $cnt = xread8($mod_data + 12); $connection = ($cnt ^ 1) & 0xff; // Key-off first if ($channel < 6) { sys_write_opl(0xB0 + $channel, 0); } // In FM mode (connection=0), only carrier is scaled; in additive mode (connection=1), both are scaled mv2_opl2_write_op($mod_offset, $mod_data, $global_vol, $connection); // Write carrier operator registers (always volume-scaled) if ($car_offset != 0xFF) { mv2_opl2_write_op($car_offset, $car_data, $global_vol, 1); } // Waveform select registers (0xE0) - from bytes 28-29 after both operator blocks sys_write_opl(0xE0 + $mod_offset, xread8($sample + 28) & 0x03); if ($car_offset != 0xFF) { sys_write_opl(0xE0 + $car_offset, xread8($sample + 29) & 0x03); } // Feedback/Connection register (0xC0) - only for melody channels (< 7) if ($channel < 7) { $feedback = xread8($mod_data + 2); // FB field from modulator block $regC0 = ($connection | (($feedback & 0x07) << 1)) & 0xff; sys_write_opl(0xC0 + $channel, $regC0); }}function mv2_opl2_noteon(int $channel, int $freqsam): void{ $channel &= 0xff; $freqsam = s16($freqsam); if ($channel >= 9 || $freqsam <= 0) { return; } // Convert freqsam (Atari ST YM2149 period) to OPL2 F-Number + Block: // F-Number * 2^Block = 125000 * 2^20 / (49716 * period) = 0x283986 / period $raw = intdiv(0x283986, $freqsam); // Decompose into F-Number (10 bits, 0-1023) and Block (3 bits, 0-7) $block = 0; while ($raw >= 1024 && $block < 7) { $raw >>= 1; $block++; } $fnum = $raw & 0x3FF; // Write F-Number low byte to register 0xA0+channel sys_write_opl(0xA0 + $channel, $fnum & 0xFF); // Write F-Number high 2 bits + Block + Key-On to register 0xB0+channel $regB0 = ((($fnum >> 8) & 0x03) | (($block & 0x07) << 2) | 0x20) & 0xff; sys_write_opl(0xB0 + $channel, $regB0);}function mv2_opl2_noteoff(int $channel): void{ $channel &= 0xff; if ($channel >= 9) { return; } // Key-off: clear the key-on bit in 0xB0 register sys_write_opl(0xB0 + $channel, 0);}function mv2_opl2voix(int $noteat, sAudioVoice $voice, int $voiceidx): int{ $notedata = xread32be($noteat); if ($notedata != 0) { $voice->value = s16($notedata >> 0x10); $voice->type = ($notedata >> 8) & 0xff; $voice->delta = $notedata & 0xff; } $nextat = s32($noteat + 2); $newfreq = xread16be($noteat); if ($newfreq != 0) { $instidx = xread8($nextat); $instidx &= 0xf0; if (BIT_CHK($newfreq, 0xc)) { $newfreq &= 0xfff; $instidx |= 0x100; } $instidx >>= 1; $instidx = ($instidx - 8) & 0xffff; $instidx >>= 3; $inst = mv2_inst($instidx); $sample = $inst ? $inst->address : 0; if ($sample != 0 && xread8($sample - 0x10) == 6) { // Determine OPL2 channel $channel = $voiceidx & 0xff; // default: voice index = channel if (xread8($sample) == 1) { $channel = xread8($sample + 1); // override channel } // Clamp frequency if (s16($newfreq - 0x71) < 0) { $newfreq = 0x71; } if (0x357 < $newfreq) { $newfreq = 0x357; } $voice->freqsam = $newfreq & 0xffff; // Program instrument on OPL2 channel mv2_opl2_setinst($sample, $channel); // Play note mv2_opl2_noteon($channel, $newfreq); $voice->volsam = 0x40; $voice->loopsam = $channel; // store OPL2 channel for later use } else { // Note with no valid instrument: note off if (($newfreq & 0xfff) == 0) { mv2_opl2_noteoff($voice->loopsam); } } } $volsam = $voice->volsam; mv2_checkcom($nextat, $volsam); $voice->volsam = $volsam; return s32($nextat + 2);}function mv2_opl2rout(): void{ $mv2a = MusicV2Vars::$mv2a; $prevmuspeed = $mv2a->muspeed; if (Aud::$muflag == 0) { return; } if (!mv2_volgen($prevmuspeed)) { return; } $mv2a->muspeed = ($mv2a->muspeed - 1) & 0xffff; if ($mv2a->muspeed == 0 || s16($prevmuspeed) < 1) { mv2_nextrow(); $noteat = mv2_noteat(); $noteat = mv2_opl2voix($noteat, $mv2a->voices[0], 0); $noteat = mv2_opl2voix($noteat, $mv2a->voices[1], 1); $noteat = mv2_opl2voix($noteat, $mv2a->voices[2], 2); $noteat = mv2_opl2voix($noteat, $mv2a->voices[3], 3); $mv2a->mucnt = ($mv2a->mucnt + 1) & 0xffff; } // Render OPL2 into the music buffer. sys_calc_opl_music(); // End of OPL frame. sys_opl_frame_tick();}