<?phpnamespace Tests;use App\Dm\Music;use App\Dm\Wad;use PHPUnit\Framework\TestCase;/** * The music's formats: the MIDI files of Freedoom and the MUS songs of many other WADs play the same way. */final class MusicTest extends TestCase{ /** A MUS song: an instrument on channel 0, a note of volume 100 for half a second, a drum, then the end. */ private static function mus(): string { $score = "\x40\x00\x13" // controller 0 (instrument) of channel 0: 19, a church organ . "\x90\xBC\x64\x46" // play note 60 (high bit: a volume follows) at volume 100, last: a delay of 70 ticks . "\x00\x3C" // release note 60 . "\x9F\x24\x46" // play note 36 on channel 15 (the percussion) at its channel's last volume, a delay of 70 ticks . "\x60"; // the end of the score return "MUS\x1A" . pack('vvvvvv', strlen($score), 18, 1, 0, 1, 0) . pack('v', 19) . $score; } private static function music(): Music { return new Music(Wad::fromParts(__DIR__ . '/../assets/wads/freedoom1.wad')); } /** @return list<array{0:float,1:int,2:int,3:int,4:int}> */ private static function events(Music $m): array { return (new \ReflectionProperty(Music::class, 'events'))->getValue($m); } public function testAMusSongIsReadAsItsNotesInTime(): void { $m = self::music(); $m->play('D_TEST', self::mus()); self::assertSame([ [0.0, 0xC0, 0, 19, 0], [0.0, 0x90, 0, 60, 100], [0.5, 0x80, 0, 60, 0], [0.5, 0x90, 9, 36, 127], // channel 15 of MUS is MIDI's percussion channel; its last volume: 127 at first ], self::events($m)); self::assertEqualsWithDelta(1.0, (new \ReflectionProperty(Music::class, 'length'))->getValue($m), 1e-9); } public function testAMusSongIsHeard(): void { $m = self::music(); $m->play('D_TEST', self::mus()); self::assertNotSame(str_repeat("\0", 4096), $m->render(2048), 'the synthesizer plays the MUS notes'); } public function testTheMusPitchWheelIsCentredAt128(): void { $m = self::music(); $score = "\x20\x80" . "\x20\xFF" . "\x60"; // pitch wheel 128 (centre), then 255 (up), then the end $m->play('D_BEND', "MUS\x1A" . pack('vvvvvv', strlen($score), 16, 1, 0, 0, 0) . $score); self::assertSame([[0.0, 0xE0, 0, 0, 64], [0.0, 0xE0, 0, 64, 127]], self::events($m)); }}