ES
games/alis / src / Alis / fn / music_v0.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/audio/music_v0.c (MIT, Olivier Huguenot, Vadim Kindl): v1.0 YM music (Manhattan Dealers).// Channel taken by a YM sound effect.function mv0_sfx(int $i): int{    $t = Aud::$channels[$i]->type & 0xff;    return (int) ($t == C::eChannelTypeDingZap || $t == C::eChannelTypeNoise || $t == C::eChannelTypeExplode);}function mv0_wmix(int $i): void{    $c = MusicV0Vars::$mv0a->ch[$i];    $m = (sys_read_psg(7) | (9 << $i)) & 0xff;    $f = $c->vflags | $c->pflags;    if ($f & 1) {        $m &= ~(1 << $i) & 0xff;    }    if ($f & 2) {        $m &= ~(8 << $i) & 0xff;        sys_write_psg(6, $c->period >> 7);    }    sys_write_psg(7, $m);}function mv0_canal(int $i): void{    $mv0a = MusicV0Vars::$mv0a;    $c = $mv0a->ch[$i];    if ($c->vflags && ($c->vcnt = ($c->vcnt - 1) & 0xff) == 0) {        $c->vol = ($c->vol + $c->vdelta) & 0xff;        if ($c->vol & 0x80) {            $c->vol = 0;        }        if (($c->vnum = ($c->vnum - 1) & 0xff) == 0) {            $r = $c->venv;            $c->vflags = MusicV0Vars::mv0_tabins[$r] ?? 0;            $c->vdelta = s8(MusicV0Vars::mv0_tabins[$r + 1] ?? 0);            $c->vrate = MusicV0Vars::mv0_tabins[$r + 2] ?? 0;            $c->vnum = MusicV0Vars::mv0_tabins[$r + 3] ?? 0;            $c->venv = ($c->venv + 4) & 0xffff;        }        $c->vcnt = $c->vrate;        sys_write_psg(8 + $i, (($c->vol * ($mv0a->curvol + 1)) >> 4) & 0xf);        mv0_wmix($i);    }    // No instrument has a pitch envelope: it only latches the period once.    if ($c->pflags) {        $c->pflags = 0;        sys_write_psg($i * 2, $c->period & 0xff);        sys_write_psg($i * 2 + 1, ($c->period >> 8) & 0xf);        mv0_wmix($i);    }}function mv0_end(): void{    $mv0a = MusicV0Vars::$mv0a;    $mv0a->playing = 0;    if ($mv0a->pending && !$mv0a->starting && Aud::$mupnote) {        mv0_start();    }}// Bit 6: tied note, its length sums the following bit 5 events of the same pitch.function mv0_tie(int $p, sMV0Channel $c): int{    $q = $p;    $dur = MusicV0Vars::mv0_tabdur[xread8($q++) & 31];    $note = xread8($q++);    for ($guard = 0; $guard < 0x4000; $guard++) {        $b = xread8($q++);        if ($b == 0) {            continue;        }        if ($b & 0x80) {            if ($b == 0x80 || $b == 0x84) {                $q += 2;            } elseif ($b == 0x81 || $b == 0x83 || $b == 0x85) {                $q++;            } elseif ($b != 0x82 && $b != 0x86) {                break;            }            continue;        }        $d = xread8($q++);        $n = xread8($q++);        if (!($b & 0x20) || $n != $note) {            continue;        }        $dur = ($dur + MusicV0Vars::mv0_tabdur[$d & 31]) & 0xff;        if ($b & 0x40) {            continue;        }        $c->dur = $dur;        $c->busy = 1;        break;    }    return $p + 1;}function mv0_note(int $p, int $cmd): int{    $mv0a = MusicV0Vars::$mv0a;    if ($cmd & 0x20) {        return $p + 2;    }    $mv0a->played = 1;    $i = 0;    while ($i < 3 && $mv0a->ch[$i]->busy) {        $i++;    }    if ($i == 3) {        $i = 0;        while ($i < 3 && $mv0a->ch[$i]->busy != 2) {            $i++;        }    }    if ($i == 3) {        $i = $mv0a->rr < 2 ? $mv0a->rr : 2;    }    $c = $mv0a->ch[$i];    if ($cmd & 0x40) {        $p = mv0_tie($p, $c);    } else {        $c->dur = MusicV0Vars::mv0_tabdur[xread8($p++) & 31];        $c->busy = 1;    }    $n = (xread8($p++) - $mv0a->transpose - 12) & 0xff;    if (!mv0_sfx($i)) {        $c->period = MusicV0Vars::mv0_tabfrq[$n < 96 ? $n : 95];        $c->vflags = 1;        $c->vnum = 1;        $c->vcnt = 1;        $c->vdelta = 0;        $c->vol = 0;        $c->pflags = 1;        $ins = ($cmd - 1) & 31;        $c->venv = MusicV0Vars::mv0_insoff[$ins > 15 ? 16 : $ins];        $c->on = 1;    }    $mv0a->rr = $i + 1 < 3 ? $i + 1 : 0;    return $p;}// Plays the events up to the next 0 that follows a note.function mv0_events(): void{    $mv0a = MusicV0Vars::$mv0a;    $mv0a->played = 0;    $p = $mv0a->songptr;    for ($guard = 0; $guard < 0x4000; $guard++) {        $b = xread8($p++);        if ($b == 0) {            if ($mv0a->played) {                break;            }            continue;        }        if ($b < 0x80) {            $p = mv0_note($p, $b);            continue;        }        switch ($b) {            case 0xff:                mv0_end();                return;            case 0x80:            case 0x84:                $p += 2;                break;            case 0x81:                $mv0a->divider = ((0xff - xread8($p++)) & 0xff) >> 5;                break;            case 0x83:                $mv0a->transpose = xread8($p++);                break;            case 0x85:                // The original never pops this stack: kept as is.                $sp = $mv0a->loopsp < C::kMV0Loops ? $mv0a->loopsp++ : C::kMV0Loops - 1;                $mv0a->loopcnt[$sp] = xread8($p++);                $mv0a->loopptr[$sp] = $p;                break;            case 0x86:                if ($mv0a->loopsp && ($mv0a->loopcnt[$mv0a->loopsp - 1] = ($mv0a->loopcnt[$mv0a->loopsp - 1] - 1) & 0xffff)) {                    $p = $mv0a->loopptr[$mv0a->loopsp - 1];                }                break;            default:                break;        }    }    $mv0a->songptr = $p;}function mv0_start(): void{    $mv0a = MusicV0Vars::$mv0a;    if (Aud::$host_freq <= 0) {        audio_output_init(SysAudioVars::$freq, SysAudioVars::$channels); // PORT: the C opens the device first    }    $mv0a->playing = 0;    $mv0a->starting = 1;    $mv0a->songptr = Aud::$mupnote;    $mv0a->divider = 1;    $mv0a->transpose = 0;    $mv0a->loopsp = 0;    $mv0a->rr = 0;    for ($i = 0; $i < 3; $i++) {        $mv0a->ch[$i]->busy = 0;    }    mv0_events();    mv0_events();    $mv0a->count = $mv0a->divider;    $mv0a->starting = 0;    $mv0a->tgtvol = $mv0a->maxvol;    $mv0a->curvol = 0;    $mv0a->fadein = $mv0a->pfadein;    $mv0a->step = $mv0a->rate = $mv0a->fadein >> 4;    $mv0a->sustain = $mv0a->psustain;    $mv0a->fadeout = $mv0a->pfadeout;    if ($mv0a->cadence) {        $mv0a->divider = $mv0a->cadence;    }    $mv0a->pending = ($mv0a->pending - 1) & 0xff;    $mv0a->playing = 1;    set_mutaloop(intdiv(Aud::$host_freq, 50));    Aud::$soundrout = __NAMESPACE__ . '\\mv0_soundrout';    Aud::$muflag = 1;}// One 100 Hz tick: volume fades, then the sequencer.function mv0_tick(): void{    $mv0a = MusicV0Vars::$mv0a;    if ($mv0a->fadein) {        if (($mv0a->step = ($mv0a->step - 1) & 0xffff) == 0) {            if (s8($mv0a->curvol) < s8($mv0a->tgtvol)) {                $mv0a->curvol = ($mv0a->curvol + 1) & 0xff;            }            $mv0a->step = $mv0a->rate;        }        $mv0a->fadein = ($mv0a->fadein - 1) & 0xffff;    } elseif ($mv0a->sustain) {        if (($mv0a->sustain = ($mv0a->sustain - 1) & 0xffff) == 0) {            $mv0a->step = $mv0a->rate = $mv0a->fadeout >> 4;        }    } elseif ($mv0a->fadeout) {        if (($mv0a->step = ($mv0a->step - 1) & 0xffff) == 0) {            if ($mv0a->curvol) {                $mv0a->curvol--;            }            $mv0a->step = $mv0a->rate;        }        if (($mv0a->fadeout = ($mv0a->fadeout - 1) & 0xffff) == 0) {            mv0_end();            return;        }    }    if (($mv0a->count = ($mv0a->count - 1) & 0xff) == 0) {        $ended = 0;        for ($i = 0; $i < 3; $i++) {            $c = $mv0a->ch[$i];            if ($c->busy) {                $c->busy = 2;                if (($c->dur = ($c->dur - 1) & 0xff) == 0) {                    $c->busy = 0;                    $ended = 1;                }            }        }        if ($ended) {            mv0_events();        }        $mv0a->count = $mv0a->divider;    }}// The original runs at 100 Hz, soundrout at 50 Hz.function mv0_soundrout(): void{    $mv0a = MusicV0Vars::$mv0a;    for ($t = 0; $t < 2; $t++) {        if ($mv0a->playing) {            mv0_tick();        }        for ($i = 0; $i < 3; $i++) {            if ($mv0a->ch[$i]->on && !mv0_sfx($i)) {                mv0_canal($i);            }        }    }    // As sys_calc_psg_music, saturated: 3 loud channels overflow at x4.    if (SysAudioVars::$audio_psg === null) {        sys_init_psg();    }    $n = Aud::$mutaloop;    $blk = PSG_calc_block(SysAudioVars::$audio_psg, $n);    $mu = &Aud::$muadresse;    for ($i = 0; $i < $n; $i++) {        $v = $blk[$i] * 4;        $mu[$i] = $v > 0x7fff ? 0x7fff : $v;    }}// cmusic: song at addr (data after the 3-byte header); queued while one plays.function mv0_gomusic(int $addr, int $vol, int $fadein, int $sustain, int $fadeout): void{    $mv0a = MusicV0Vars::$mv0a;    Aud::$mupnote = s32($addr + 3);    $mv0a->maxvol = $vol & 0xff;    $mv0a->pfadein = (s16($fadein) + 0x10) & 0xffff;    $mv0a->psustain = $sustain & 0xffff;    $mv0a->pfadeout = (s16($fadeout) + 0x10) & 0xffff;    $mv0a->pending = 1;    if (!$mv0a->playing) {        mv0_start();    }}// cdelmusic: fade out, no queued song.function mv0_offmusic(): void{    $mv0a = MusicV0Vars::$mv0a;    $mv0a->fadein = 0;    $mv0a->sustain = 0;    $mv0a->pending = 0;}function mv0_cadence(int $cadence): void{    MusicV0Vars::$mv0a->cadence = ($cadence + 1) & 0xff;}function mv0_volume(int $vol): void{    MusicV0Vars::$mv0a->tgtvol = $vol & 0xff;    MusicV0Vars::$mv0a->curvol = $vol & 0xff;}function mv0_reset(): void{    MusicV0Vars::$mv0a = new sMV0Audio();}function mv0_relocate(int $lo, int $hi, int $delta): void{    $mv0a = MusicV0Vars::$mv0a;    $mv0a->songptr = moved($mv0a->songptr, $lo, $hi, $delta);    for ($i = 0; $i < C::kMV0Loops; $i++) {        $mv0a->loopptr[$i] = moved($mv0a->loopptr[$i], $lo, $hi, $delta);    }    if ($mv0a->songptr == 0) {        $mv0a->playing = $mv0a->pending = 0;        for ($i = 0; $i < 3; $i++) {            if ($mv0a->ch[$i]->on && !mv0_sfx($i)) {                sys_write_psg(8 + $i, 0);            }            $mv0a->ch[$i]->on = 0;        }    }}