UK
apps/winzik / tests / BlanksTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Media\Blanks;use App\Media\YouTube;use App\Model\Id3Tag;/** The blanks and the intros left out of a song: found in its sound, given by YouTube's chapters, or set by hand (its ID3 tag). */final class BlanksTest extends PlayerTestCase{    /** A WAV: $silence s of silence, $sound s of a tone, $after s of silence. */    private function wav(float $silence, float $sound, float $after): string    {        $rate = 44100;        $pcm = '';        foreach ([[$silence, false], [$sound, true], [$after, false]] as [$s, $on]) {            for ($i = 0, $n = (int)($s * $rate); $i < $n; $i++) {                $v = $on ? (int)(12000 * sin(2 * M_PI * 440 * $i / $rate)) : 0;                $pcm .= pack('vv', $v & 0xFFFF, $v & 0xFFFF);            }        }        $file = sys_get_temp_dir() . '/wz-blanks-' . getmypid() . '.wav';        file_put_contents($file, 'RIFF' . pack('V', 36 + strlen($pcm)) . 'WAVEfmt ' . pack('VvvVVvv', 16, 1, 2, $rate, $rate * 4, 4, 16) . 'data' . pack('V', strlen($pcm)) . $pcm);        return $file;    }    public function testTheSilenceAtTheStartAndTheEndIsFoundInTheSound(): void    {        $file = $this->wav(1.0, 2.0, 1.5);        [$start, $end] = Blanks::find($file);        self::assertEqualsWithDelta(0.95, $start, 0.06, 'the music begins after 1 s');        self::assertEqualsWithDelta(3.1, $end, 0.11, 'and ends 1.5 s before the file');        $short = $this->wav(0.2, 2.0, 0.2);        self::assertSame([0.0, 0.0], Blanks::find($short), 'less than half a second: not worth a skip');        @unlink($file);    }    public function testTheMarksGoIntoTheId3TagAndTheSoundStaysTheSame(): void    {        $this->startPlayer();        $copy = sys_get_temp_dir() . '/wz-marks-' . getmypid() . '.mp3';        copy($this->wz->path('assets/music/winzik.mp3'), $copy);        $before = Id3Tag::read($copy);        $tail = substr((string)file_get_contents($copy), -4096);        self::assertTrue(Id3Tag::writeMarks($copy, 2.5, 41.25));        $after = Id3Tag::read($copy);        self::assertSame(2.5, $after->markStart);        self::assertSame(41.25, $after->markEnd);        self::assertSame($before->title, $after->title, 'its other frames kept');        self::assertSame($tail, substr((string)file_get_contents($copy), -4096), 'the sound not touched');        // written again: no second pair, removed with null        self::assertTrue(Id3Tag::writeMarks($copy, 3.0, null));        $again = Id3Tag::read($copy);        self::assertSame(3.0, $again->markStart);        self::assertNull($again->markEnd);        @unlink($copy);    }    public function testThePartsLeftOutAreSkippedWhilePlaying(): void    {        $this->startPlayer();        $index = (int)array_search('assets/music/chaine-hifi.wav', $this->wz->playlist->items, true);        $file = (string)$this->wz->playlist->items[$index];        $this->wz->setBounds($file, 2.0, 12.0);        $this->wz->playIndex($index);        $this->game->run(3);        self::assertGreaterThanOrEqual(2.0, $this->wz->player->position, 'its start left out');        $this->wz->seek(12.1);        $this->game->run(3);        self::assertNotSame($index, $this->wz->playlist->active, 'its end left out: the next song');        $this->wz->setBounds($file, 0, 0);    }    public function testAnIntroAndAnOutroComeFromTheChaptersOfTheVideo(): void    {        $chapters = json_encode([['title' => 'Intro', 'start_time' => 0, 'end_time' => 14.5], ['title' => 'Couplet', 'start_time' => 14.5, 'end_time' => 200],            ['title' => 'Outro', 'start_time' => 200, 'end_time' => 230]]);        self::assertSame([14.5, 200.0], YouTube::bounds((string)$chapters));        self::assertNull(YouTube::bounds('NA'), 'no chapters');        self::assertNull(YouTube::bounds((string)json_encode([['title' => 'Partie 1', 'start_time' => 0, 'end_time' => 9], ['title' => 'Partie 2', 'start_time' => 9, 'end_time' => 20]])));    }}