DE
games/alis / src / Alis / fn / sys_audio.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of the audio part of alis/src/sys/sys.c and sys/sys_sdl2.c (MIT, Olivier Huguenot, Vadim Kindl):// PSG / OPL host chips, the YM sound effects (io_canal) and the SDL audio callback (the mixer).//// HOST API (what the JumPHP host calls):// - audio_output_init(rate, channels): the SDL_OpenAudioDevice part of sys_init (sys_sdl2.c): output rate,//   channel count, PSG creation, sound effect tick rate. Call it once the game's platform is known (after//   pl_guess / sys_init, before the game runs), and again if the output changes.// - audio_mix(frames): the next `frames` frames of signed 16-bit little-endian PCM (interleaved when stereo),//   exactly what sys_audio_callback_S16MSB() would put in an SDL buffer of that many frames. Never blocks.// PORT: the Atari-only parts (DSP bridge, real YM2149, S8 callback), the OPL register capture (--opl-capture)// and the WAV export (debugging) are not ported.const AUDIO_S16LSB = 0x8010;const kRDiffLen = 4096;/** Opens the "audio device": the audio part of sys_init() (sys_sdl2.c). */function audio_output_init(int $rate, int $channels): void{    $rate = $rate > 0 ? $rate : 22050;    if (SysAudioVars::$audio_opl !== null && SysAudioVars::$freq != $rate) {        sys_deinit_opl(); // PORT: re-created at the new rate on the next use    }    SysAudioVars::$freq = $rate;    SysAudioVars::$channels = $channels >= 1 ? $channels : 2;    SysAudioVars::$opened = true;    Aud::$host_freq = $rate;    Aud::$host_format = AUDIO_S16LSB;    sys_init_psg();    // DOS games ticked their audio engine on a 60 Hz PIT; Atari/Amiga games on a 50 Hz PAL VBL.    $le = isset(Alis::$platform) ? Alis::$platform->is_little_endian : 0;    SysAudioVars::$sys_sfx_tick_hz = $le ? 60 : 50;    SysAudioVars::$isr_step = intdiv(SysAudioVars::$sys_sfx_tick_hz * 65536, $rate);    SysAudioVars::$isr_counter = 0x10000;}/** The next $frames frames of s16 little-endian PCM (the SDL audio callback). */function audio_mix(int $frames): string{    if ($frames <= 0) {        return '';    }    if (!SysAudioVars::$opened) {        audio_output_init(SysAudioVars::$freq, SysAudioVars::$channels);    }    return sys_audio_callback_S16MSB($frames);}/** PORT: the C music routines are called through audio.soundrout: the function name, '' = NULL. */function audio_soundrout_is(string $name): bool{    return Aud::$soundrout === __NAMESPACE__ . '\\' . $name;}// ============================================================================// Audio// ============================================================================function sys_init_psg(): void{    // ST YM2149: 2 MHz, no internal divider. Quality=0 keeps tiny tone    // periods audible (cexplode/cnoise rely on noise leaking through).    $psg = PSG_new(2000000, SysAudioVars::$freq);    PSG_setClockDivider($psg, 0);    PSG_setVolumeMode($psg, 1);    PSG_setQuality($psg, 0);    PSG_reset($psg);    SysAudioVars::$audio_psg = $psg;}function sys_deinit_psg(): void{    // Zero the channel volumes (regs 8-10)    sys_write_psg(8, 0);    sys_write_psg(9, 0);    sys_write_psg(10, 0);    SysAudioVars::$audio_psg = null;}function sys_write_psg(int $reg, int $val): void{    // PORT: the C PSG exists from sys_init on; here a write before audio_output_init() creates it    if (SysAudioVars::$audio_psg === null) {        sys_init_psg();    }    PSG_writeReg(SysAudioVars::$audio_psg, $reg, $val);}function sys_read_psg(int $reg): int{    if (SysAudioVars::$audio_psg === null) {        sys_init_psg();    }    return PSG_readReg(SysAudioVars::$audio_psg, $reg);}function sys_calc_psg_music(): void{    if (SysAudioVars::$audio_psg === null) {        sys_init_psg();    }    $n = Aud::$mutaloop;    $blk = PSG_calc_block(SysAudioVars::$audio_psg, $n);    $mu = &Aud::$muadresse;    for ($index = 0; $index < $n; $index++) {        $v = $blk[$index] * 4;        $mu[$index] = (($v + 0x8000) & 0xffff) - 0x8000; // stored into an s16    }}function sys_init_opl(): void{    if (SysAudioVars::$audio_opl !== null) {        return; // already initialized    }    $opl = OPL_new(3579545, SysAudioVars::$freq);    OPL_setChipType($opl, 2); // YM3812    OPL_reset($opl);    SysAudioVars::$audio_opl = $opl;}function sys_deinit_opl(): void{    if (SysAudioVars::$audio_opl !== null) {        OPL_delete(SysAudioVars::$audio_opl);        SysAudioVars::$audio_opl = null;    }}// OPL2 writes go to emu8950 (the --opl-capture register dump of the C is not ported).function sys_write_opl(int $reg, int $val): void{    if (SysAudioVars::$audio_opl !== null) {        OPL_writeReg(SysAudioVars::$audio_opl, $reg, $val);    }}// Called once per music frame (end of mv2_opl2rout), after that frame's writes and render.function sys_opl_frame_tick(): void{    SysAudioVars::$sys_opl_q_count = 0;}function sys_calc_opl_music(): void{    $n = Aud::$mutaloop;    $mu = &Aud::$muadresse;    if (SysAudioVars::$audio_opl === null) {        // C: OPL_calc(NULL) would crash; cannot happen (mv2_gomusic creates the chip before)        for ($index = 0; $index < $n; $index++) {            $mu[$index] = 0;        }        return;    }    $blk = OPL_calc_block(SysAudioVars::$audio_opl, $n);    for ($index = 0; $index < $n; $index++) {        $mu[$index] = $blk[$index];    }}function interpolate_cubic(float $x0, float $x1, float $x2, float $x3, float $t): float{    $a0 = $x3 - $x2 - $x0 + $x1;    $a1 = $x0 - $x1 - $a0;    $a2 = $x2 - $x0;    $a3 = $x1;    return ($a0 * ($t * $t * $t)) + ($a1 * ($t * $t)) + ($a2 * $t) + ($a3);}function interpolate_hermite_4pt_3ox(float $x0, float $x1, float $x2, float $x3, float $t): float{    $c0 = $x1;    $c1 = .5 * ($x2 - $x0);    $c2 = $x0 - (2.5 * $x1) + (2 * $x2) - (.5 * $x3);    $c3 = (.5 * ($x3 - $x0)) + (1.5 * ($x1 - $x2));    return (((((($c3 * $t) + $c2) * $t) + $c1) * $t) + $c0);}function interpolate_hermite(float $x0, float $x1, float $x2, float $x3, float $t): float{    $diff = $x1 - $x2;    $c1 = $x2 - $x0;    $c3 = $x3 - $x0 + 3 * $diff;    $c2 = -(2 * $diff + $c1 + $c3);    return 0.5 * (($c3 * $t + $c2) * $t + $c1) * $t + $x1;}function priorblanc(sChannel $channel): bool{    $result = 1;    if ($channel->curson < Aud::$channels[0]->curson) {        $result = pblanc10(Aud::$channels[0], $result);    }    if ($channel->curson < Aud::$channels[1]->curson) {        $result = pblanc10(Aud::$channels[1], $result);    }    if ($channel->curson < Aud::$channels[2]->curson) {        $result = pblanc10(Aud::$channels[2], $result);    }    return $result == 0;}function pblanc10(sChannel $channel, int $d1b): int{    return (($channel->type & 2) != 0) ? 0 : $d1b;}function io_canal(sChannel $channel, int $index): void{    // Mirrors Falcon RRQ asm io_canal (rrq-falcon.asm:0xD3EE).    // Both early-returns are required: index > 2 would clobber reg 6    // (noise period); sample channels (type & 0x80) must not touch PSG.    if ($index > 2) {        return;    }    if ($channel->type & 0x80) {        return;    }    giaccess(s8($index + 0x88), ($channel->volume >> 11) & 0xf, $index);    giaccess(s8($index * 2 + 0x80), ($channel->freq >> 3) & 0xff, $index);    giaccess(s8($index * 2 + 0x81), (($channel->freq >> 3) >> 8) & 0xf, $index);    $mixer = giaccess(7, 0, $index);    if ($index < 3) {        $mixer = ($mixer | 1 << ($index & 0x1f) | 1 << (($index + 3) & 0x1f)) & 0xff;        if (($channel->type & 1) != 0) {            $mixer &= ~(1 << ($index & 0x1f)) & 0xff;        }        if (($channel->type & 2) != 0) {            $mixer &= ~(1 << (($index + 3) & 0x1f)) & 0xff;            if (!priorblanc($channel)) {                giaccess(s8(0x86), (($channel->freq & 0xffff) >> 10) & 0xff, $index);            }        }    }    giaccess(s8(0x87), $mixer, $index);}/** One sound effect tick of a YM effect channel (the do { } while (false) block of sys_psg_tick / the callback). */function sys_sfx_tick(sChannel $ch, int $i, bool $opl): void{    if (ult($ch->played, $ch->length)) {        $ch->played = s32($ch->played + 1);        $vol = $ch->delta_volume + $ch->volume;        if (-1 < $vol) {            if (0x7fff < $vol) {                $vol = 0x7fff;            }            $ch->volume = s16($vol);            $freq = $ch->delta_freq + $ch->freq;            if (($freq & 0x8000) == 0) { // -1 < (s32)(freq << 0x10)                $ch->freq = s16($freq);                if ($opl) {                    io_canal_opl($ch, $i);                } else {                    io_canal($ch, $i);                }                return;            }        }    }    $ch->type = C::eChannelTypeNone;    $ch->volume = 0;    $ch->freq = 0;    $ch->curson = s8(0x80);    $ch->state = 0;    $ch->played = 0;    if ($opl) {        io_canal_opl($ch, $i);    } else {        io_canal($ch, $i);    }}// One 50/60 Hz tick of the YM effect channels (DingZap/Noise/Explode), same as// the audio callback's isr_counter step.function sys_psg_tick(): void{    for ($i = 0; $i < 3; $i++) {        $ch = Aud::$channels[$i];        if ($ch->type != C::eChannelTypeDingZap            && $ch->type != C::eChannelTypeNoise            && $ch->type != C::eChannelTypeExplode) {            continue;        }        sys_sfx_tick($ch, $i, false);    }}// OPL2 sound effect support: use OPL2 channels 4-5 for effects (0-3 reserved for music)function io_canal_opl_init(int $opl_ch, int $chan_type): void{    static $mod_off = [0x09, 0x0A];    static $car_off = [0x0C, 0x0D];    $opl = SysAudioVars::$audio_opl;    $idx = $opl_ch - 4;    $mod = $mod_off[$idx];    $car = $car_off[$idx];    if ($chan_type == C::eChannelTypeNoise || $chan_type == C::eChannelTypeExplode) {        // Noise/explosion: FM mode with high feedback + high multipliers        OPL_writeReg($opl, 0x20 + $mod, 0x20 | 0x07); // Sustain=1, Mul=7        OPL_writeReg($opl, 0x40 + $mod, 0x00);        OPL_writeReg($opl, 0x60 + $mod, 0xF0);         // AR=max, DR=0        OPL_writeReg($opl, 0x80 + $mod, 0xF0);         // SL=max, RR=0        OPL_writeReg($opl, 0xE0 + $mod, 0x00);         // Sine        OPL_writeReg($opl, 0x20 + $car, 0x20 | 0x0E);  // Sustain=1, Mul=14        OPL_writeReg($opl, 0x40 + $car, 0x00);        OPL_writeReg($opl, 0x60 + $car, 0xF0);        OPL_writeReg($opl, 0x80 + $car, 0xF0);        OPL_writeReg($opl, 0xE0 + $car, 0x00);        // FM mode (connection=0) with max feedback (7) -> noisy output        OPL_writeReg($opl, 0xC0 + $opl_ch, 0x0E);      // FB=7, CNT=0    } else {        // Tone (czap, cding): additive sine, from Ishar 2 DOS        OPL_writeReg($opl, 0x20 + $mod, 0x21);  // Sustain=1, Mul=1        OPL_writeReg($opl, 0x40 + $mod, 0x00);        OPL_writeReg($opl, 0x60 + $mod, 0xF0);        OPL_writeReg($opl, 0x80 + $mod, 0xF0);        OPL_writeReg($opl, 0xE0 + $mod, 0x00);        OPL_writeReg($opl, 0x20 + $car, 0x21);        OPL_writeReg($opl, 0x40 + $car, 0x00);        OPL_writeReg($opl, 0x60 + $car, 0xF0);        OPL_writeReg($opl, 0x80 + $car, 0xF0);        OPL_writeReg($opl, 0xE0 + $car, 0x00);        OPL_writeReg($opl, 0xC0 + $opl_ch, 0x01); // FB=0, CNT=1 (additive)    }    SysAudioVars::$opl_sfx_initialized[$idx] = 1;    SysAudioVars::$opl_sfx_type[$idx] = $chan_type & 0xff;}function io_canal_opl(sChannel $channel, int $index): void{    if (SysAudioVars::$audio_opl === null) {        sys_init_opl();    }    $opl = SysAudioVars::$audio_opl;    $opl_ch = 4 + ($index % 2);    $idx = $opl_ch - 4;    // Re-init if channel type changed    if (!SysAudioVars::$opl_sfx_initialized[$idx] || SysAudioVars::$opl_sfx_type[$idx] != ($channel->type & 0xff)) {        io_canal_opl_init($opl_ch, $channel->type);    }    // Volume: channel volume 0-0x7FFF -> OPL2 TL 0x3F(silent)-0x00(max)    $vol_4bit = ($channel->volume >> 11) & 0xf;    $tl = (0x3f - intdiv($vol_4bit * 0x3f, 0xf)) & 0xff;    static $mod_off_v = [0x09, 0x0A];    static $car_off_v = [0x0C, 0x0D];    if (SysAudioVars::$opl_sfx_type[$idx] == C::eChannelTypeNoise || SysAudioVars::$opl_sfx_type[$idx] == C::eChannelTypeExplode) {        // FM mode: only carrier TL controls output volume        OPL_writeReg($opl, 0x40 + $car_off_v[$idx], $tl);    } else {        // Additive mode: both operators output, update both        OPL_writeReg($opl, 0x40 + $mod_off_v[$idx], $tl);        OPL_writeReg($opl, 0x40 + $car_off_v[$idx], $tl);    }    if ($channel->type == C::eChannelTypeNone || $channel->volume == 0) {        OPL_writeReg($opl, 0xB0 + $opl_ch, 0x00);        SysAudioVars::$opl_sfx_initialized[$idx] = 0;        return;    }    // Frequency: from Ishar 2 DOS FUN_1000_0af5    $freq_idx = ($channel->freq >> 3) + 0x140;    if ($freq_idx < 1) {        $freq_idx = 1;    }    $fnum = intdiv(0xD6D8, $freq_idx) & 0xffff;    if ($fnum > 0x3FF) {        $fnum = 0x3FF;    }    OPL_writeReg($opl, 0xA0 + $opl_ch, $fnum & 0xFF);    OPL_writeReg($opl, 0xB0 + $opl_ch, 0x20 | 0x1C | (($fnum >> 8) & 0x03));}function giaccess(int $cmd, int $data, int $ch): int{    $regval = $cmd & 0xf;    if (($regval < 0xe) && ($cmd < 0)) {        sys_write_psg($regval, $data & 0xff);    }    return sys_read_psg($regval);}// (s8)sample * volratio, low 16 bits (all the mixers keep): per channel, rebuilt on volume change.function vol_table(int $ch, int $volratio): array{    if (SysAudioVars::$vol_tab_v[$ch] != $volratio) {        $t = [];        for ($k = 0; $k < 256; $k++) {            $t[$k] = s16(s8($k) * $volratio);        }        SysAudioVars::$vol_tab[$ch] = $t;        SysAudioVars::$vol_tab_v[$ch] = $volratio;    }    return SysAudioVars::$vol_tab[$ch];}/** C: (u32)((s32)(freq << 16) / audio_spec->freq): 16.16 step of a sample channel. */function sys_sample_ratio(int $freq, int $host): int{    return s32(intdiv(s16($freq) << 16, $host));}/** Mixes the next $n frames of one chip (OPL when $opl, else PSG) into $m[$from..]. */function sys_mix_chip(array &$m, int $from, int $n, bool $opl): void{    if ($n <= 0) {        return;    }    $blk = $opl ? OPL_calc_block(SysAudioVars::$audio_opl, $n) : PSG_calc_block(SysAudioVars::$audio_psg, $n);    for ($j = 0; $j < $n; $j++) {        $m[$from + $j] += $blk[$j];    }}/** * C sys_audio_callback_S16MSB(): 16-bit signed stereo/mono output (the desktop SDL path). * PORT: returns the buffer of $frames frames as s16 little-endian PCM (native order of the C desktop build). * The C mixes the first channel of each frame with modular 16-bit arithmetic (u16 buffer) and duplicates it at * the end; here that channel is the int array $m (values kept unwrapped, the final pack() keeps 16 bits). */function sys_audio_callback_S16MSB(int $frames): string{    $hostFreq = SysAudioVars::$freq;    $adv = SysAudioVars::$channels;    $buffer_length = $frames * $adv;    $m = array_fill(0, $frames, 0);    $smpidx = 0;    // MV2 stereo: the sfx mixers copy left into right, so right gets (right - left) music afterwards    AudioVars::$mustereo = ($adv >= 2 && !SysAudioVars::$opt_mono) ? 1 : 0;    $mst = AudioVars::$mustereo && audio_soundrout_is('mv2_soundrout');    $mrdiff = [];    // handle music    if (Aud::$muflag != 0 && Aud::$soundrout !== '' && Aud::$mutaloop > 0) {        $smprem = $frames;        $buflen = Aud::$mutaloop;        $len = intdiv($buffer_length + $buflen - 1, $buflen);        if (Aud::$smpidx) {            $smpcopy = min(Aud::$smpidx, $smprem);            $music_base = $buflen - Aud::$smpidx;            $mu = Aud::$muadresse;            for ($music_index = 0; $music_index < $smpcopy; $music_index++) {                $m[$music_index] = $mu[$music_base + $music_index] ?? 0;            }            if ($mst) {                $mr = AudioVars::$muright;                for ($k = 0; $k < $smpcopy && $k < kRDiffLen; $k++) {                    $mrdiff[$k] = ($mr[$music_base + $k] ?? 0) - ($mu[$music_base + $k] ?? 0);                }            }            $smpidx += $smpcopy;            $smprem -= $smpcopy;            Aud::$smpidx = s32(Aud::$smpidx - $smpcopy);        }        for ($i = 0; $i < $len && $smprem; $i++) {            (Aud::$soundrout)();            $smpcopy = min($buflen, $smprem);            $mu = Aud::$muadresse;            for ($music_index = 0; $music_index < $smpcopy; $music_index++) {                $m[$smpidx + $music_index] = $mu[$music_index] ?? 0;            }            if ($mst) {                $mr = AudioVars::$muright;                for ($k = 0; $k < $smpcopy && $smpidx + $k < kRDiffLen; $k++) {                    $mrdiff[$smpidx + $k] = ($mr[$k] ?? 0) - ($mu[$k] ?? 0);                }            }            $smpidx += $smpcopy;            $smprem -= $smpcopy;            Aud::$smpidx = $buflen - $smpcopy;        }    }    if (!Aud::$fmusic) {        // musicoff: the original stops mixing the music voices (0x10afd); the sequencer above keeps running        $m = array_fill(0, $frames, 0);        $mrdiff = [];    }    $mframes = $mst ? min($smpidx, kRDiffLen) : 0;    if (Aud::$muflag == 0) {        Aud::$working = 0;    }    // handle sounds    if (Aud::$fsound) {        $prev_counter = SysAudioVars::$isr_counter;        $isr_step = SysAudioVars::$isr_step;        $isPC = Alis::$platform->kind == C::EPlatformPC;        for ($i = 0; $i < 4; $i++) {            SysAudioVars::$isr_counter = $prev_counter;            $ch = Aud::$channels[$i];            switch ($ch->type) {                case C::eChannelTypeSample:                    $vt = vol_table($i, $ch->volume >> 1);                    // addr/length change when the next FLI chunk is spliced in.                    $addr = $ch->address;                    $length = $ch->length;                    $loop = $ch->loop;                    $forever = $ch->forever;                    $played = $ch->played;                    $ratio = sys_sample_ratio($ch->freq, $hostFreq);                    $start = $played * $ratio; // u64 (a float past 2^63 / 2^31)                    if (is_int($start) && $start >= 0) {                        $smpidx = s32($start >> 16);                        $acc = $start & 0xffff;                    } else {                        $q = floor($start / 65536.0);                        $smpidx = s32($q);                        $acc = (int) ($start - $q * 65536.0);                    }                    $mem = Alis::$mem;                    $s0 = $vt[ord($mem[$addr + $smpidx] ?? "\0")];                    $end = $length + 1;                    for ($f = 0; $f < $frames; $f++) {                        $add = $acc >> 16;                        if ($add) {                            $smpidx += $add;                            $s0 = $vt[ord($mem[$addr + $smpidx] ?? "\0")];                            if ($smpidx >= $end) {                                if ($forever || $loop > 1) {                                    if (!$forever) {                                        $loop--;                                    }                                    $played = $smpidx = $acc = 0;                                } elseif ($i == 3 && FilmVars::$fli_audio_q_head != FilmVars::$fli_audio_q_tail) {                                    // Splice in the next queued FLI chunk.                                    FilmVars::$fli_chunks_played = s32(FilmVars::$fli_chunks_played + 1);                                    $t = FilmVars::$fli_audio_q_tail;                                    $addr = FilmVars::$fli_audio_queue[$t]['addr'];                                    $length = FilmVars::$fli_audio_queue[$t]['length'];                                    $ch->address = $addr;                                    $ch->length = $length;                                    $ch->freq = FilmVars::$fli_audio_queue[$t]['freq'];                                    FilmVars::$fli_audio_q_tail = ($t + 1) & (C::FLI_AUDIO_QUEUE_SIZE - 1);                                    $ratio = sys_sample_ratio($ch->freq, $hostFreq);                                    $end = $length + 1;                                    $played = $smpidx = $acc = 0;                                } else {                                    if ($i == 3) {                                        FilmVars::$fli_chunks_played = s32(FilmVars::$fli_chunks_played + 1);                                    }                                    $ch->type = C::eChannelTypeNone;                                    $ch->curson = s8(0x80);                                    $ch->forever = false;                                    break;                                }                            }                        }                        $m[$f] += $s0;                        $acc = ($acc & 0xffff) + $ratio;                        $played++;                    }                    // Main thread may have replaced the chunk directly; then `played` is stale.                    if ($ch->address == $addr) {                        $ch->played = s32($played);                        $ch->loop = $loop;                    }                    break;                case C::eChannelTypeDingZap:                case C::eChannelTypeNoise:                case C::eChannelTypeExplode:                    if ($i < 3) {                        // Count frames, not samples: tick rate must not depend on channel count.                        // PORT: the chip samples between two ticks are rendered by blocks (same order).                        $ctr = SysAudioVars::$isr_counter;                        $from = 0;                        for ($p = 0; $p < $frames; $p++) {                            $ctr += $isr_step;                            if ($ctr >= 0x10000) {                                sys_mix_sfx_chip($m, $from, $p - $from, $isPC);                                $from = $p;                                $ctr -= 0x10000;                                sys_sfx_tick($ch, $i, $isPC);                            }                        }                        sys_mix_sfx_chip($m, $from, $frames - $from, $isPC);                        SysAudioVars::$isr_counter = $ctr;                    }                    break;                default:                    break;            }        }    }    // The OPL/PSG paths above write only the first channel; duplicate it (+ MV2 stereo right channel).    if ($adv == 1) {        $out = $m;    } else {        $out = [];        $f = 0;        for (; $f < $mframes; $f++) {            $l = $m[$f];            $v = ((($l + 0x8000) & 0xffff) - 0x8000) + ($mrdiff[$f] ?? 0);            $out[] = $l;            $out[] = $v < -32768 ? -32768 : ($v > 32767 ? 32767 : $v);            for ($o = 2; $o < $adv; $o++) {                $out[] = $l;            }        }        if ($adv == 2) {            for (; $f < $frames; $f++) {                $l = $m[$f];                $out[] = $l;                $out[] = $l;            }        } else {            for (; $f < $frames; $f++) {                for ($o = 0; $o < $adv; $o++) {                    $out[] = $m[$f];                }            }        }    }    if (Alis::$platform->version >= 60) {        foreach ($out as $k => $v) {            $out[$k] = (($v + 0x8000) & 0xffff) - 0x8000;        }        v6snd_render($out, $frames, $adv, $hostFreq);    }    return pack('v*', ...$out);}/** The chip part of the YM effect loop: frames [$from, $from + $n) of the first channel. */function sys_mix_sfx_chip(array &$m, int $from, int $n, bool $isPC): void{    if ($n <= 0) {        return;    }    if ($isPC && SysAudioVars::$audio_opl !== null) {        sys_mix_chip($m, $from, $n, true);    } elseif (!(Aud::$muflag != 0 && audio_soundrout_is('mv0_soundrout'))) {        if (SysAudioVars::$audio_psg === null) {            sys_init_psg();        }        sys_mix_chip($m, $from, $n, false);    }}