JA
games/alis / src / Alis / fn / music_v1.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/audio/music_v1.c (MIT, Olivier Huguenot, Vadim Kindl): older sample music// (Atari ST / Amiga Ishar and older).// Loop point of an instrument: the offset stored at `field`, relative to the sample start; 0 = none.// Some instruments hold no loop offset there; the original then read stray memory, which outside// the VM arena would fault here, so such a loop is dropped (the note plays once).function mv1_loop_addr(int $instr, int $field): int{    $rel = xread32be($field);    if ($rel == 0) {        return 0;    }    $loop = s32($instr + $rel);    return ult($loop, Alis::$platform->ram_sz) ? $loop : 0;}function mv1_gomusic(): void{    $mv1a = MusicV1Vars::$mv1a;    if (Aud::$host_freq <= 0) {        audio_output_init(SysAudioVars::$freq, SysAudioVars::$channels); // PORT: the C opens the device first    }    Aud::$soundrout = __NAMESPACE__ . '\\mv1_soundrout';    $mv1a->flag1 = 0;    $mv1a->flag2 = 0;    $mv1a->muspeed = 0;    $ratio_fp = intdiv(9600 << 16, Aud::$host_freq);  // 16.16 fixed-point    for ($i = 0; $i < 128; $i++) {        // (u64)tabfrq_st * ratio_fp >> 16: < 2^20 * 2^17, exact in a float on 32-bit PHP        $v = MusicV1Vars::mv1_tabfrq_st[$i] * $ratio_fp;        $mv1a->tabfrq[$i] = is_int($v) ? s32($v >> 16) : s32(floor($v / 65536.0));    }    $mv1a->mumax = intdiv(Aud::$host_freq, 200) & 0xffff;    set_mutaloop($mv1a->mumax * 4);    $mutempo = ((Aud::$mutempo & 0x7f) << 3) & 0xffff;    $muvolume = (((0x7f - (Aud::$muvolume & 0x7f)) & 0xffff) >> 4) & 7;    for ($c = 0; $c < C::kNumMV1Channels; $c++) {        $mv1a->channels[$c]->endsam = 0;    }    $mv1a->basevol = Aud::$muvolume;    $mv1a->basevol = ($mv1a->basevol << 8) & 0xffff;    $mv1a->tempo = $mutempo;    $mv1a->noteptr = Aud::$mupnote;    $mv1a->prevvol = $muvolume;    if ($mv1a->flag2 == 0) {        if ($mv1a->flag1 == 0) {            mv1_initchannels();            $mv1a->effect = 0x6016;            $mv1a->flag2 = 0xff;            mv1_setvolmusic($mv1a->prevvol);        } else {            mv1_initchannels();            $mv1a->prevvol = ($mv1a->prevvol + 2) & 0xffff;            if ($mv1a->prevvol == 2) {                $mv1a->prevvol += 3;            }            mv1_calcvolmusic($mv1a->prevvol);            $mv1a->effect = 0x2a78;            $mv1a->flag2 = 0xff;        }    } else {        $mv1a->prevvol = $mv1a->basevol;        if ($mv1a->flag1 != 0 && ($mv1a->prevvol = ($mv1a->basevol + 2) & 0xffff) == 2) {            $mv1a->prevvol = ($mv1a->basevol + 3) & 0xffff;        }        mv1_setvolmusic($mv1a->prevvol);    }    Aud::$smpidx = 0;    Aud::$muflag = 1;}function mv1_initchannels(): void{    for ($c = 0; $c < C::kNumMV1Channels; $c++) {        MusicV1Vars::$mv1a->channels[$c]->active = 0x6010;        MusicV1Vars::$mv1a->channels[$c]->actflag = 0;    }}function mv1_muroutine(): void{    $mv1a = MusicV1Vars::$mv1a;    if (Aud::$muattac == 0) {        if (Aud::$muduree == 0) {            if (Aud::$muchute != 0 && -1 < s16(Aud::$muchute)) {                $vol = s16($mv1a->basevol - Aud::$dchute);                if ((-1 < $vol) && ($vol != 0)) {                    Aud::$muchute = (Aud::$muchute - 1) & 0xffff;                    $mv1a->basevol = $vol & 0xffff;                    mv1_calcvolmusic($mv1a->basevol >> 8);                    mv1_tempmusic(Aud::$mutempo);                    return;                }            }            mv1_stopmusic();            Aud::$mustate = 0;            return;        }        Aud::$muduree = s16(Aud::$muduree - 1);    } else {        $mv1a->basevol = ($mv1a->basevol + Aud::$dattac) & 0xffff;        if (Aud::$maxvolume < Aud::$dattac) {            Aud::$muattac = 1;            $mv1a->basevol = Aud::$maxvolume;        }        if (0x7fff < $mv1a->basevol) {            $mv1a->basevol = 0x7fff;        }        Aud::$muattac = (Aud::$muattac - 1) & 0xffff;    }    mv1_calcvolmusic($mv1a->basevol >> 8);    mv1_tempmusic(Aud::$mutempo);}function mv1_offmusic(int $much): void{    Aud::$muchute = ($much + 0x10) & 0xffff;    Aud::$muattac = 0;    Aud::$muduree = 1;    if (Aud::$muchute != 0) {        Aud::$dchute = intdiv(MusicV1Vars::$mv1a->basevol, Aud::$muchute) & 0xffff;   // live volume (music() zeroes muvolume)    }}function mv1_stopmusic(): void{    Aud::$muflag = 0;}function mv1_soundrout(): void{    MusicV1Vars::$mv1a->mucnt = 0;   // 4 x mv1_checkcom() render all mutaloop samples: no clear needed    mv1_muroutine();    for ($m = 0; $m < 4; $m++) {        mv1_checkcom();    }}function mv1_checkcom(): void{    $mv1a = MusicV1Vars::$mv1a;    $mv1a->muspeed = ($mv1a->muspeed - $mv1a->tempo) & 0xffff;    if ((s16($mv1a->muspeed) < 0) || ($mv1a->muspeed == 0)) {        $mv1a->muspeed = 0;        $noteptr = $mv1a->noteptr;        $guard = 0;        while (true) {            if (++$guard > 0x10000) {                // PORT: data that never yields a speed byte spins forever in the C (its audio thread hangs);                // the port runs on the main thread: stop the music instead.                mv1_stopmusic();                return;            }            if ($noteptr == 0) {                $noteptr = $mv1a->noteptr;            }            $mv1a->noteptr = s32($noteptr + 1);            $nextnoteptr = $noteptr;            $speed = xread8($noteptr);            if (s8($speed) < 0) {                if ($speed != 0xff) {                    $mv1a->muspeed = (($speed - 0x80) << 8) & 0xffff;                    break;                }                $mv1a->notedata = xread8($mv1a->noteptr);                $nextnoteptr = s32($noteptr + 2);            }            $notedata = $mv1a->notedata;            if ($mv1a->notedata == 0xff) {                $notedata = 0;            }            $type = $notedata & 0x3f; // (u8 << 2) >> 2            $chidx = $notedata >> 6;            $channel = $mv1a->channels[$chidx] ?? null;            switch ($type) {                case 0:                    $noteptr = Aud::$mupnote;                    break;                case 1:                    if (xread8($nextnoteptr + 1) == 0) {                        $noteptr = s32($nextnoteptr + 2);                        if ((xread8($nextnoteptr) == $channel->unknown) && ($channel->actflag != 0)) {                            $channel->actflag = 0;                            $channel->active = 0x6010;                        }                    } else {                        $channel->clearendsam = 0x6002;                        $instr = $channel->instr;                        $data = $channel->data;                        $channel->startsam->address = $instr;                        $channel->endsam = mv1_loop_addr($instr, $instr - 8);                        $unkn = xread8($nextnoteptr);                        $channel->unknown = $unkn;                        $noteptr = s32($nextnoteptr + 2);                        $channel->speedsam = $mv1a->tabfrq[($data + $unkn) & 0xffff] ?? 0;                        $flag = $channel->actflag;                        $channel->actflag |= 0x80;                        if ($flag == 0) {                            $channel->active = 0x2a78;                        }                    }                    break;                case 2:                    $noteptr = s32($nextnoteptr + 1);                    $instidx = xread8($nextnoteptr) & 0x7f;                    $sample = Aud::$tabinst[$instidx]->address;                    $tval = Aud::$tabinst[$instidx]->unknown;                    $channel->data = $tval & 0xffff;                    $channel->instr = $sample;                    break;                case 3:                    $channel->endsam = mv1_loop_addr($channel->instr, $channel->instr - 4);                    $channel->clearendsam = 0x42b8;                    $noteptr = $nextnoteptr;                    break;                default:                    // Not music data (the original would jump through garbage): stop instead of spinning.                    mv1_stopmusic();                    for ($c = 0; $c < C::kNumMV1Channels; $c++) {                        $mv1a->channels[$c]->active = 0x6010;                        $mv1a->channels[$c]->actflag = 0;                    }                    return;            }        }    }    // Channel state must advance even on the DSP path: MV1 clears active/actflag    // at sample end, and note-on only re-activates a channel when actflag==0.    for ($l = 0; $l < $mv1a->mumax; $l++) {        mv1_playnote();    }}/** C mv1_advance(): (address:fraction) 32.32 += addvance << 16. */function mv1_advance(int &$address, int &$fraction, int $addvance): void{    // fraction low 16 bits are unchanged; its high 16 bits get the low 16 bits of addvance (carry into address)    $hi = (($fraction >> 16) & 0xffff) + ($addvance & 0xffff);    $fraction = shl32($hi & 0xffff, 16) | ($fraction & 0xffff);    $address = s32($address + (($addvance >> 16) & 0xffff) + ($hi >> 16));}function mv1_playnote(): void{    $mv1a = MusicV1Vars::$mv1a;    $mix = 0;    $addr = [$mv1a->channels[0]->endsam, $mv1a->channels[1]->endsam, $mv1a->channels[2]->endsam];    $mem = Alis::$mem;    for ($c = 0; $c < C::kNumMV1Channels; $c++) {        $channel = $mv1a->channels[$c];        if ($channel->active == 0x2a78) {            $smp = s8(ord($mem[$channel->startsam->address] ?? "\0"));            if ($smp != 0) {                $mix += $smp;                mv1_advance($channel->startsam->address, $channel->startsam->fraction, $channel->speedsam);            } else {                if ($channel->endsam != 0) {                    if ($channel->clearendsam == 0x42b8) {                        $channel->endsam = 0;                    }                    $smp = s8(ord($mem[$addr[$c]] ?? "\0") + 1);                    $mix += $smp;                    $channel->startsam->address = $addr[$c];                    mv1_advance($channel->startsam->address, $channel->startsam->fraction, $channel->speedsam);                } else {                    $channel->active = 0x6010;                    $channel->actflag = 0;                }            }        }    }    $mix = s16($mix * $mv1a->volume);    Aud::$muadresse[$mv1a->mucnt] = $mix;    $mv1a->mucnt = ($mv1a->mucnt + 1) & 0xffff;}function mv1_tempmusic(int $temp): void{    MusicV1Vars::$mv1a->tempo = (($temp & 0x7f) << 3) & 0xffff;}function mv1_calcvolmusic(int $vol): void{    $mv1a = MusicV1Vars::$mv1a;    $vol = (((0x7f - ($vol & 0x7f)) & 0xffff) >> 4) & 7;    // C: if (mv1a.flag1 < 0 && (vol += 2) == 2) vol += 3; -- flag1 is a u8: never true    $mv1a->prevvol = $vol;    $mv1a->volume = ((8 >> $vol) << 2) & 0xffff;}function mv1_setvolmusic(int $vol): void{    MusicV1Vars::$mv1a->volume = ((8 >> s16($vol)) << 2) & 0xffff;}