UK
games/alis / tools / audio_bench.php · commit 9c722cf
<?php// Audio checks / benchmark of the ALIS port (sys_audio.php)://   bin/jumphp php games/alis tools/audio_bench.php                 synthetic benchmark (no game data)//   bin/jumphp php games/alis tools/audio_bench.php game <dir> <frames> [clicks]   runs a game with audio,//        prints what plays each second and the mixing time, writes var/run/audio.wav// Output: 22050 Hz stereo, 441 frames per call (one 20 ms VM frame).namespace App\Alis;require __DIR__ . '/../vendor/autoload.php';function bench(string $name, callable $setup, int $seconds = 2): void{    $setup();    $calls = $seconds * 50;    audio_mix(441); // warm-up    $t = microtime(true);    $pcm = '';    for ($i = 0; $i < $calls; $i++) {        $pcm .= audio_mix(441);    }    $dt = microtime(true) - $t;    $v = unpack('s*', $pcm);    printf("%-28s %6.2f us/frame  %6.1f ms per s of audio   peak %d\n", $name, $dt * 1e6 / ($calls * 441), $dt * 1000 / $seconds, \max(\max($v), -\min($v)));}function fake_platform(int $kind, int $le): void{    Alis::init();    Aud::init();    $pl = new Platform();    $pl->kind = $kind;    $pl->is_little_endian = $le;    $pl->version = 31;    Alis::$platform = $pl;    Alis::$mem = str_repeat("\0", 0x100000);    Alis::$finmem = 0x100000;    Aud::$fsound = 1;    Aud::$fmusic = 1;    Aud::$muflag = 0;    Aud::$soundrout = '';    SysAudioVars::$audio_opl = null;    audio_output_init(22050, 2);}if (($argv[1] ?? '') !== 'game') {    echo 'PHP ' . PHP_VERSION . ' ' . (PHP_INT_SIZE * 8) . "-bit, JIT " . (function_exists('opcache_get_status') && (opcache_get_status()['jit']['on'] ?? false) ? 'on' : 'off') . "\n";    // (a) idle    bench('(a) idle', fn () => fake_platform(C::EPlatformPC, 1));    // (b) 4 sample channels (looping 8-bit samples, 10..20 kHz)    bench('(b) 4 sample channels', function () {        fake_platform(C::EPlatformAtari, 0);        $mem = '';        for ($i = 0; $i < 0x10000; $i++) {            $mem .= chr((int) (sin($i / 7.0) * 100) & 0xff);        }        mem_write(0x1000, $mem);        for ($c = 0; $c < 4; $c++) {            playsample(C::eChannelTypeSample, 0x1000 + $c * 0x100, 10 + $c * 3, 0xff, 0xe000, 200, 10 + $c);        }    });    // (c) PSG music: a YM effect channel with the 3 PSG voices playing (Atari: emu2149 per frame)    bench('(c) PSG (3 voices + noise)', function () {        fake_platform(C::EPlatformAtari, 0);        foreach ([0 => 0x80, 1 => 0x01, 2 => 0xc0, 3 => 0x00, 4 => 0x50, 5 => 0x01, 6 => 0x0c, 7 => 0x30, 8 => 15, 9 => 13, 10 => 12] as $r => $v) {            sys_write_psg($r, $v);        }        Aud::$channels[0]->type = C::eChannelTypeDingZap;        Aud::$channels[0]->volume = 0x7000;        Aud::$channels[0]->freq = 0x400;        Aud::$channels[0]->length = 0x7fffffff;    });    // (d) OPL with the 9 melodic channels busy (sustained notes) + one OPL effect channel driving the chip    bench('(d) OPL 9 channels busy', function () {        fake_platform(C::EPlatformPC, 1);        sys_init_opl();        $o = SysAudioVars::$audio_opl;        $ops = [[0, 3], [1, 4], [2, 5], [8, 11], [9, 12], [10, 13], [16, 19], [17, 20], [18, 21]];        foreach ($ops as $c => [$m, $k]) {            foreach ([$m, $k] as $op) {                OPL_writeReg($o, 0x20 + $op, 0x21);                OPL_writeReg($o, 0x40 + $op, 0x10);                OPL_writeReg($o, 0x60 + $op, 0xF4);                OPL_writeReg($o, 0x80 + $op, 0x55);            }            OPL_writeReg($o, 0xC0 + $c, 0x06);            OPL_writeReg($o, 0xA0 + $c, 0x80 + $c * 20);            OPL_writeReg($o, 0xB0 + $c, 0x31);        }        // the OPL music path: sys_calc_opl_music() through a soundrout rendering mutaloop samples per tick        set_mutaloop(intdiv(0x3E7 * 22050, 50000) + 1);        Aud::$soundrout = __NAMESPACE__ . '\\sys_calc_opl_music';        Aud::$muflag = 1;    });    exit(0);}// ---- a game run ----$dir = $argv[2] ?? '../../.alis-data/robinson';$frames = (int) ($argv[3] ?? 3000);$clicks = [];foreach (array_filter(explode(',', $argv[4] ?? '')) as $k) {    $p = explode(':', $k);    $clicks[(int) $p[0]] = $p;}$m = new Machine();$gameId = basename($dir);SysVars::$lazy = (GameLibrary::catalog(__DIR__ . '/../catalog', $gameId))['lazy'] ?? [];SysVars::$lazyDev = is_dir(__DIR__ . "/../../../.alis-data/$gameId") ? realpath(__DIR__ . "/../../../.alis-data/$gameId") : '';if (!$m->boot(__DIR__ . '/../' . $dir)) {    fwrite(STDERR, "boot failed: {$m->error}\n");    exit(1);}audio_output_init(22050, 2);$pcm = '';$mix = 0.0;$sec = [];for ($i = 1; $i <= $frames && $m->running; $i++) {    if (isset($clicks[$i])) {        $p = $clicks[$i];        if ($p[1] === 'm') {            SysVars::$mouse->x = (int) $p[2];            SysVars::$mouse->y = (int) $p[3];            SysVars::$mouse->lb = 1;            SysVars::$mouse->lb_clicked = 1;        } else {            SysVars::$held[(int) $p[1]] = true;            SysVars::$button = (int) $p[1];        }    }    if (isset($clicks[$i - 4])) {        SysVars::$mouse->lb = 0;        SysVars::$held = [];        SysVars::$button = 0;    }    $m->advance(0.02, $i * 20);    $t = microtime(true);    $pcm .= audio_mix(441);    $dt = microtime(true) - $t;    $mix += $dt;    $s = intdiv($i - 1, 50);    $msec[$s] = ($msec[$s] ?? 0) + $dt;    $types = implode('', array_map(fn ($c) => match ($c->type) { 0 => '.', 128 => 'S', default => (string) $c->type }, Aud::$channels));    $rout = Aud::$muflag ? substr(Aud::$soundrout, strlen(__NAMESPACE__) + 1) : '-';    $sec[$s][$rout . ' ' . $types . sprintf(' mus=%x', Aud::$musicId)] = true;}if ($m->error !== null) {    echo "VM ERROR: {$m->error}\n";}$v = unpack('s*', $pcm);foreach ($sec as $s => $what) {    $seg = array_slice($v, $s * 44100, 44100);    printf("%3d s  peak %5d  mix %5.1f ms  %s\n", $s, \max(\max($seg), -\min($seg)), $msec[$s] * 1000, implode(' | ', array_keys($what)));}$n = strlen($pcm);@mkdir(__DIR__ . '/../var/run', 0777, true);file_put_contents(__DIR__ . '/../var/run/audio.wav', 'RIFF' . pack('V', 36 + $n) . 'WAVEfmt ' . pack('VvvVVvv', 16, 1, 2, 22050, 22050 * 4, 4, 16) . 'data' . pack('V', $n) . $pcm);printf("%.1f s of audio mixed in %.3f s: %.2f us per frame (var/run/audio.wav)\n", $n / 4 / 22050, $mix, $mix * 1e6 / max(1, $n / 4));