<?phpnamespace App\Mobile;use App\Audio\Player;use App\Media\Lrclib;use App\Model\Karaoke;use App\Model\Lyrics;use App\Model\Text;use JumPHP\Native\Clipboard;/** * The lyrics on the phone: the sheet "Paroles" (where to get them: LRCLIB, the marketplace, the old site, a .lrc / .lyr * file, Whisper on a computer; their timing shifted), the karaoke on the whole screen (the syllables light up as they are * sung, the ball of the computer's lyrics bar over them), and the lyrics editor made for a finger: the lines typed or * pasted, then the song plays and a tap on « Ligne suivante » gives each line its moment (LyricsEditorForm, simplified). */trait LyricsPages{ /** the line of the karaoke at the middle of the screen (it glides to the line sung) */ public float $karaScroll = 0.0; /** the finger scrolled the lyrics: they stay where it left them, until « Revenir à la ligne chantée » or a while untouched */ public bool $karaFree = false; /** seconds the lyrics scrolled by hand have stayed still */ private float $karaIdle = 0.0; /** their speed after a flick (dp / s, > 0: towards the end) */ private float $karaSpeed = 0.0; /** @var array<string, float> the height of each line (dp), sung or not, for the lyrics and the width shown */ private array $karaHeights = []; private string $karaHeightsOf = ''; /** @var list<string> the lines of the editor */ public array $edLines = []; /** @var list<float|null> their moments (null: not given yet) */ public array $edTimes = []; /** the next line a tap gives a moment to (-1: the lines are being typed) */ public int $edNext = -1; /** the song the editor is for */ private string $edSong = ''; /** The lyrics of the song playing, if any. */ private function songLyrics(): ?Lyrics { $l = $this->wz?->lyrics; return $l !== null && !$l->isEmpty() && $this->wz->playlist->current() !== null ? $l : null; } private function lyricsSheet(): ?array { $wz = $this->wz; if ($this->sheet === 'lyPreview' && $wz !== null) { return $this->lyricsPreviewSheet(); } if ($this->sheet !== 'lyrics' || $wz === null) { return null; } $file = $wz->playlist->current(); if ($file === null) { return [['title', 'Paroles', 'Aucun morceau'], ['text', 'Lancez d\'abord un morceau : ses paroles se cherchent pour lui.'], ['button', 'Fermer', 'sheetClose', null, true]]; } $lyrics = $this->songLyrics(); $words = $lyrics === null ? 0 : count(array_filter($lyrics->texts(), fn (string $t) => trim($t) !== '')); $rows = [['title', 'Paroles', $lyrics !== null ? $words . ' lignes · ' . ($lyrics->synced ? 'synchronisées' : 'non synchronisées') : 'Pas encore de paroles pour ce morceau']]; $rows[] = ['row', 'search', 'Chercher des paroles sur Internet', 'lyFind', null, 'text', 'chevron']; if ($lyrics !== null) { $rows[] = ['row', 'fullscreen', 'Karaoké plein écran', 'goto', 'karaoke', 'text', 'chevron']; if ($lyrics->synced) { $rows[] = ['stepper', 'Décaler les paroles', 'En retard ou en avance', '-0,5 s', '+0,5 s', 'lyShift']; } else { $rows[] = ['row', 'clock', 'Les synchroniser (éditeur)', 'goto', 'lyricsEdit', 'text', 'chevron']; } $chip = $this->translationChip(); if ($chip !== null && $chip[1] === 'trNow') { $rows[] = ['row', 'translate', 'Traduire en ' . mb_strtolower(\App\Model\Language::name($wz->translationLang())), 'trNow', null, 'text', '']; } elseif ($chip !== null) { $rows[] = ['toggle', 'Afficher la traduction', 'Sous chaque ligne, en bleu', 'trToggle', null, $this->showTranslation()]; } } $rows[] = ['row', 'mic', 'Paroles synchronisées (LRCLIB)', 'lyLrclib', null, 'text', '']; $rows[] = ['row', 'store', 'Marketplace des paroles', 'lyMarket', null, 'text', 'chevron']; $rows[] = ['row', 'search', 'Chercher sur l\'ancien site WinZik', 'lyOldSite', null, 'text', '']; $rows[] = ['row', 'folder', 'Importer un fichier (.lrc, .lyr)', 'lyImport', null, 'text', 'chevron']; $rows[] = ['row', 'edit', 'Éditeur de paroles (taper en rythme)', 'goto', 'lyricsEdit', 'text', 'chevron']; if ($wz->transcriber !== null) { $rows[] = ['row', 'note', 'Créer les paroles (Whisper)', 'lyWhisper', null, 'text', '']; } return $rows; } /** A .lrc / .lyr chosen (Ajouter in the lyrics mode), or a .fls (a play list): MediaPages::pickFile() for the play lists. */ public function importLyrics(string $path): bool { $wz = $this->wz; $song = $wz?->playlist->current(); if ($wz === null || $song === null) { $wz?->notice('Lancez d\'abord le morceau de ces paroles', 4); return false; } $content = @file_get_contents($path); $lyrics = $content === false ? null : (Text::extension($path) === 'lrc' ? Lrclib::fromLrc($content) : Lyrics::parse($content)); if ($lyrics === null || $lyrics->isEmpty()) { $wz->notice('Ce fichier n\'a pas de paroles synchronisées : choisissez un .lrc ou un .lyr', 5); return false; } // the song's own copy (var/lyr/<name>-<crc>.lyr): looked at first, never given to another song of that name $target = $wz->lyricsCachePath($song); @mkdir(dirname($target), 0777, true); file_put_contents($target, Text::extension($path) === 'lrc' ? $lyrics->toLyr() : (string)$content); $wz->refreshLyrics(); $wz->loadLyrics($song); $wz->notice('Paroles importées : ' . Text::baseName($path), 4); return true; } // the karaoke ----------------------------------------------------------------------------------------------------- private function enterKaraoke(): void { $this->karaScroll = (float)max(0, $this->songLyrics() !== null ? $this->wz->estimatedLine() : 0); $this->karaFree = false; $this->karaSpeed = 0.0; } /** Room above the line sung when the line before shows its translation (the ball over the syllables stays off it). */ private function karaPad(Lyrics $lyrics, int $i, bool $big): float { return $big && $lyrics->synced && $i > 0 && $this->lineTranslation($lyrics, $i - 1) !== '' ? 16.0 : 0.0; } /** The line sung big (its syllables lit): -1 for lyrics without times (all their lines the same size). */ private function karaNow(Lyrics $lyrics): int { return $lyrics->synced ? $lyrics->indexAt($this->wz->player->position) : -1; } private function updateKaraoke(float $dt): void { $this->updateLyricsCard($dt); if ($this->screen !== 'karaoke' || ($lyrics = $this->songLyrics()) === null) { return; } if ($this->karaFree) { if ($this->press !== null && $this->press['scroll'] === 'kara') { return; // the finger holds them } if ($this->karaSpeed != 0) { // a flick goes on, slower and slower $stopped = $this->karaShift($this->karaSpeed * $dt); $this->karaSpeed *= exp(-3.2 * $dt); if ($stopped || abs($this->karaSpeed) < 8) { $this->karaSpeed = 0.0; } return; } $this->karaIdle += $dt; if ($this->karaIdle < self::KARAOKE_FOLLOW) { return; } $this->karaFree = false; // left alone a while: back to the line sung } if (!$lyrics->synced && !$this->lyricsAutoScroll()) { return; // lines without times, « Défiler seul » off: only the finger moves them } // synchronised: the line sung; without times: the estimated line, followed slowly $sung = (float)max(0, $lyrics->synced ? $lyrics->indexAt($this->wz->player->position) : $this->wz->estimatedLine()); $gap = $sung - $this->karaScroll; $this->karaScroll = abs($gap) > 12 ? $sung : $this->karaScroll + $gap * min(1.0, $dt * ($lyrics->synced ? 7 : 1.6)); } /** The karaoke in landscape (the phone turned; the app itself stays upright): remembered. */ public function karaokeLandscape(): bool { return (bool)($this->wz?->ui['mobile']['karaokeLandscape'] ?? false); } /** The height of a line of the karaoke (dp): its rows at the width of the screen, bigger for the line sung. */ private function karaHeight(int $i, bool $big): float { $lyrics = $this->songLyrics(); $raw = $lyrics?->lines[$i][1] ?? null; if ($lyrics === null || $raw === null) { return 40.0; } $of = spl_object_id($lyrics) . '|' . count($lyrics->lines) . '|' . $this->W . '|' . ($this->t === null ? 0 : 1) . '|' . ($this->showTranslation() ? $lyrics->translationLang . count($lyrics->translations) : '-'); if ($this->karaHeightsOf !== $of) { $this->karaHeights = []; $this->karaHeightsOf = $of; } $key = $i . ($big ? 'b' : 's'); if (!isset($this->karaHeights[$key])) { if (!$lyrics->synced) { // lines without times: plain text; a blank between verses is a small gap $plain = Karaoke::plain($raw); $rows = trim($plain) === '' ? 0 : count($this->wrap($plain, 18.0, true, $this->W - 48)); } else { $start = (float)$lyrics->lines[$i][0]; [$syl, $times] = Karaoke::timing($raw, $start, $start + 6); if ($syl === []) { $syl = [Karaoke::plain($raw)]; $times = [$start]; } $rows = count($this->wrapSyllables($syl, $times, $big ? 26.0 : 18.0, $this->W - 48)); } $h = $rows === 0 && !$lyrics->synced ? 22.0 : max(1, $rows) * ($big ? 36.0 : 27.0) + ($big ? 18 : 12) + $this->karaPad($lyrics, $i, $big); $tr = $this->lineTranslation($lyrics, $i); if ($tr !== '') { // its translation under it $h += count($this->wrap($tr, $big ? 15.0 : 13.5, false, $this->W - 48)) * ($big ? 21.0 : 19.0) + 2; } $this->karaHeights[$key] = $h; } return $this->karaHeights[$key]; } /** * The lyrics moved by $dp (> 0: up, towards the end), line by line at their heights. * @return bool they came to the first or the last line */ private function karaShift(float $dp): bool { $lyrics = $this->songLyrics(); if ($lyrics === null) { return true; } $last = count($lyrics->lines) - 1; $now = $this->karaNow($lyrics); $at = max(0.0, min((float)$last, $this->karaScroll)); for ($guard = 0; abs($dp) > 0.001 && $guard < 2000; $guard++) { $i = (int)floor($at); if ($dp > 0) { if ($at >= $last) { $this->karaScroll = (float)$last; return true; } $h = $this->karaHeight($i, $i === $now); $room = ($i + 1 - $at) * $h; if ($dp < $room) { $at += $dp / $h; $dp = 0.0; } else { $at = (float)($i + 1); $dp -= $room; } } else { if ($at <= 0) { $this->karaScroll = 0.0; return true; } if ($at - $i < 1e-9) { $i--; // at the top of a line: the one above } $h = $this->karaHeight($i, $i === $now); $room = ($at - $i) * $h; if (-$dp < $room) { $at += $dp / $h; $dp = 0.0; } else { $at = (float)$i; $dp += $room; } } } $this->karaScroll = max(0.0, min((float)$last, $at)); return false; } /** A finger on the lyrics: it stops them if they were flicked (that touch taps nothing). */ private function karaokeDown(): void { if ($this->press !== null) { $this->press['stopper'] = $this->karaSpeed != 0; $this->press['from'] = $this->karaScroll; } $this->karaSpeed = 0.0; } /** The finger drags the lyrics: the line sung is no longer followed. @param array $press */ private function karaokeDrag(array &$press, float $y): void { $this->karaFree = true; $this->karaIdle = 0.0; $this->karaScroll = (float)$press['from']; $this->karaShift($press['y'] - $y); $press['track'][] = [$this->clock, $y]; $press['track'] = array_slice($press['track'], -6); } /** The finger let go of the lyrics: the speed of its last moves goes on. @param array $press */ private function karaokeFlick(array $press, float $y): void { $track = $press['track']; $track[] = [$this->clock, $y]; [$t0, $y0] = $track[0]; $span = $this->clock - $t0; $this->karaSpeed = $span > 0.001 ? max(-4000.0, min(4000.0, ($y0 - $y) / $span)) : 0.0; $this->karaIdle = 0.0; } private function karaokeWheel(float $notches): void { if ($this->songLyrics() === null) { return; } $this->karaFree = true; $this->karaIdle = 0.0; $this->karaSpeed = 0.0; $this->karaShift(-$notches * 60); } /** the karaoke animation chosen in the Options (the lyrics bar of the computer has the same) */ public function karaokeMode(): string { $mode = (string)($this->wz?->options['karaoke'] ?? 'ball'); return isset(\App\Gui\LyricsBar::KARAOKE[$mode]) ? $mode : 'ball'; } /** * A line cut into lines of the screen at its words, each syllable with its moment. * @param list<string> $syllables @param list<float> $times * @return list<list<array{0: string, 1: float}>> */ private function wrapSyllables(array $syllables, array $times, float $size, float $max): array { $words = []; $word = []; foreach ($syllables as $k => $syl) { $word[] = [$syl, $times[$k] ?? INF]; if (preg_match('/\s$/u', $syl)) { $words[] = $word; $word = []; } } if ($word !== []) { $words[] = $word; } $lines = []; $line = []; $width = 0.0; foreach ($words as $w) { $ww = $this->textWidth(implode('', array_column($w, 0)), $size, true); if ($line !== [] && $width + $ww > $max) { $lines[] = $line; $line = []; $width = 0.0; } array_push($line, ...$w); $width += $ww; } if ($line !== []) { $lines[] = $line; } return $lines; } /** * The karaoke on the whole screen, upright or in landscape (MobileView::renderTurned(): then $W > $H). The finger * scrolls the lyrics (the line sung is no longer followed until « Revenir à la ligne chantée » or a few seconds * untouched), a tap on a line takes the song there. Lyrics without times: a text the finger scrolls (followed slowly * by the estimated line when « Défiler seul » is on), « Non synchronisées », a tap on a line does not seek. The * translations under each line, in blue (« Traduction » shows / hides them). */ private function renderKaraoke(): void { $wz = $this->wz; $W = $this->W; $H = $this->H; // a stage: the colours of the theme, deep $top = self::mix($this->col('art2'), '#000000', 0.3); $bottom = self::mix($this->col('art2'), '#000000', 0.82); $stage = fn (float $y0, float $y1) => $this->gradientBox(0, $y0, $W, $y1 - $y0, self::mix($top, $bottom, $y0 / $H), self::mix($top, $bottom, $y1 / $H), 0); $stage(0, $H); $white = '#ffffff'; $soft = [255, 255, 255, 120]; $gold = '#ffd84a'; $blue = '#8cc4ff'; $lyrics = $this->songLyrics(); $synced = $lyrics === null || $lyrics->synced; $wide = $W > $H; $controlsY = $H - ($wide ? 52 : 64); // upright, the chips (Non synchronisées, Traduction) have a row of their own under the top $chipRow = !$wide && $lyrics !== null; $zoneTop = $chipRow ? 108.0 : 70.0; $zoneBottom = $controlsY - ($wide ? 62 : 70); if ($lyrics === null) { $cy = $wide ? $H * 0.2 : $H * 0.32; $this->icon('mic', $W / 2 - 28, $cy, 56, $soft); $this->text('Pas de paroles pour ce morceau', $W / 2, $cy + 88, 18, $white, true, 'center'); $this->pill($W / 2 - 130, $cy + 120, 260, 'search', 'Chercher des paroles', 'lyFind', null); } else { $pos = $wz->player->position; $now = $this->karaNow($lyrics); $est = $synced ? $now : $wz->estimatedLine(); $lines = $lyrics->lines; $max = $W - 48; $mid = $zoneTop + ($zoneBottom - $zoneTop) * 0.42; // the lyrics: the finger scrolls them (a drag), a tap on a line takes the song there $this->hit(0, $zoneTop, $W, $zoneBottom - $zoneTop, 'karaArea', null, 'kara'); // the line sung, big, its syllables lit; the others above and below, smaller, scrolling smoothly $anchor = max(0, min(count($lines) - 1, (int)floor($this->karaScroll))); $y = $mid - ($this->karaScroll - $anchor) * $this->karaHeight($anchor, $anchor === $now); $from = $anchor; while ($from > 0 && $y > $zoneTop - 60) { $from--; $y -= $this->karaHeight($from, $from === $now); } for ($i = $from; $i < count($lines) && $y < $zoneBottom; $i++) { $big = $i === $now; $h = $this->karaHeight($i, $big); if ($y + $h > $zoneTop - 30) { $size = $big ? 26.0 : 18.0; $lh = $big ? 36.0 : 27.0; [$start, $raw] = $lines[$i]; if ($this->pressed('karaLine', $i)) { $this->box(12, $y + 2, $W - 24, $h - 4, [255, 255, 255, 28], 14); } elseif (!$synced && $i === $est && trim(Karaoke::plain($raw)) !== '' && $y >= $zoneTop - 2 && $y + $h <= $zoneBottom + 2) { $this->box(12, $y + 2, $W - 24, $h - 4, [255, 255, 255, 14], 14); // about here in the song } $rows = 0; // the line sung under a translation: room above it for the ball over its syllables $pad = $this->karaPad($lyrics, $i, $big); if ($synced) { $next = $lines[$i + 1][0] ?? $start + 6; [$syl, $times] = Karaoke::timing($raw, $start, $next); if ($syl === []) { $syl = [Karaoke::plain($raw)]; $times = [$start]; } foreach ($this->wrapSyllables($syl, $times, $size, $max) as $k => $row) { $width = $this->textWidth(implode('', array_column($row, 0)), $size, true); $x = ($W - $width) / 2; $cy = $y + $pad + $lh / 2 + $k * $lh; foreach ($row as [$s, $at]) { $sw = $this->textWidth($s, $size, true); $lit = $big && $pos >= $at; $this->text($s, $x, $cy, $size, $big ? ($lit ? $gold : $white) : ($i < $now ? [255, 255, 255, 90] : $soft), true); if ($big && $this->karaokeMode() !== 'off') { $this->karaokeMark($row, $s, $at, $x, $sw, $cy, $size, $pos, $gold); } $x += $sw; } $rows++; } } else { // no times: plain lines, the estimated one brighter foreach ($this->wrap(Karaoke::plain($raw), $size, true, $max) as $k => $row) { $this->text($row, $W / 2, $y + $lh / 2 + $k * $lh, $size, $i === $est ? $white : [255, 255, 255, 150], true, 'center'); $rows++; } } // its translation under it, in blue $tr = $this->lineTranslation($lyrics, $i); if ($tr !== '') { $ts = $big ? 15.0 : 13.5; $tl = $big ? 21.0 : 19.0; $ty = $y + $pad + max(1, $rows) * $lh + $tl / 2 - ($big ? 2 : 3); $ink = $big ? $blue : [140, 196, 255, $synced && $i < $now ? 120 : 200]; foreach ($this->wrap($tr, $ts, false, $max) as $k => $row) { $this->text($row, $W / 2, $ty + $k * $tl, $ts, $ink, false, 'center'); } } // a tap on a line (only its part inside the zone of the lyrics) $y0 = max($zoneTop, $y); $y1 = min($zoneBottom, $y + $h); if ($y1 - $y0 > 6) { $this->hit(0, $y0, $W, $y1 - $y0, 'karaLine', $i, 'kara'); } } $y += $h; } // what scrolled out of the zone is hidden by the stage $stage(0, $zoneTop); $stage($zoneBottom, $H); if ($this->karaFree && ($synced || $this->lyricsAutoScroll())) { // scrolled by hand: the way back to the line sung $label = $synced ? 'Revenir à la ligne chantée' : 'Suivre la chanson'; $cw = $this->textWidth($label, 14, true) + 58; $cy = $zoneBottom - 26; $this->box(($W - $cw) / 2, $cy - 20, $cw, 40, $this->pressed('karaFollow') ? [255, 200, 40, 255] : [255, 216, 74, 240], 20); $this->icon(max(0, $est) < $this->karaScroll ? 'up' : 'down', ($W - $cw) / 2 + 14, $cy - 10, 20, '#1d2330'); $this->text($label, ($W - $cw) / 2 + 40, $cy, 14, '#1d2330', true); $this->hit(($W - $cw) / 2 - 6, $cy - 24, $cw + 12, 48, 'karaFollow'); } if (!$synced) { // said once, quietly: the finger scrolls them, a line touched does not move the song $this->text($wide ? 'Défilez du doigt · toucher une ligne ne déplace pas le morceau' : 'Défilez du doigt · une ligne touchée ne saute pas', $W / 2, $zoneBottom + 12, 12, [255, 255, 255, 110], false, 'center', $W - 32); } } // the top: back, the song, the chips, Portrait / Paysage, the lyrics' sheet $this->roundButton(8, 8, 48, 'back', 'addBack', null, $white); $this->roundButton($W - 56, 8, 48, 'more', 'lyricsMenu', null, $white); $turn = $wide ? 'Portrait' : 'Paysage'; $cw = $this->textWidth($turn, 13, true) + 46; $cx = $W - 60 - $cw; $this->box($cx, 14, $cw, 36, self::mix($top, '#ffffff', $this->pressed('karaTurn') ? 0.34 : 0.18), 18); $this->icon('rotate', $cx + 10, 22, 20, $white); $this->text($turn, $cx + 36, 32, 13, $white, true); $this->hit($cx - 4, 8, $cw + 8, 48, 'karaTurn'); $titleRight = $cx - 8; if ($lyrics !== null) { $chip = $this->translationChip(); $badge = !$synced ? 'Non synchronisées' : ''; $bw = $badge !== '' ? $this->textWidth($badge, 13, true) + 44 : 0.0; $tw = $chip !== null ? $this->textWidth($chip[0], 13, true) + 44 : 0.0; $total = $bw + $tw + ($bw > 0 && $tw > 0 ? 8 : 0); // upright: centred on a row of their own; in landscape: in the top bar, left of Portrait [$x, $y] = $wide ? [$cx - 8 - $total, 14.0] : [($W - $total) / 2, 62.0]; if ($wide) { $titleRight = $x - 8; } if ($badge !== '') { $this->box($x, $y, $bw, 36, [255, 255, 255, 40], 18); $this->box($x + 1.5, $y + 1.5, $bw - 3, 33, self::mix($top, '#000000', 0.3), 16.5); $this->icon('list', $x + 12, $y + 8, 20, [255, 255, 255, 200]); $this->text($badge, $x + 36, $y + 18, 13, [255, 255, 255, 210], true); $x += $bw + 8; } if ($chip !== null) { $this->stageChip($x, $y, 'translate', $chip[0], $chip[1], $chip[2], $chip[3]); } } [$title, $artist] = $this->titleOf($wz->playlist->active); $this->text($title, 64, 25, 18, $white, true, 'left', $titleRight - 72); $this->text($artist !== '' ? $artist : 'Karaoké', 64, 48, 13, $soft, false, 'left', $titleRight - 72); // the bottom: the position, then -0,5 s, play / pause, +0,5 s (no times: Défiler seul, Synchroniser) $d = $wz->player->duration(); $ratio = $d > 0 ? max(0.0, min(1.0, $wz->player->position / $d)) : 0.0; $barY = $controlsY - ($wide ? 38 : 44); $this->box(28, $barY, $W - 56, 4, self::mix($bottom, '#ffffff', 0.22), 2); $this->box(28, $barY, max(4, ($W - 56) * $ratio), 4, $gold, 2); $this->text(self::clock($wz->player->position), 28, $barY + 18, 12, $soft); $this->text($d > 0 ? self::clock($d) : '--:--', $W - 28, $barY + 18, 12, $soft, false, 'right'); $this->hit(14, $barY - 20, $W - 28, 44, 'seek'); $playing = $wz->player->state === Player::PLAYING; $this->circle($W / 2, $controlsY + 8, 30, $white); $this->icon($playing ? 'pause' : 'play', $W / 2 - 15, $controlsY - 7, 30, self::mix($this->col('art2'), '#000000', 0.5)); $this->hit($W / 2 - 32, $controlsY - 24, 64, 64, 'play'); if ($lyrics !== null && $synced) { foreach ([['-0,5 s', -1, 24], ['+0,5 s', 1, $W - 24 - 88]] as [$label, $sign, $bx]) { $this->box($bx, $controlsY - 12, 88, 40, self::mix($bottom, '#ffffff', $this->pressed('lyShift', $sign) ? 0.32 : 0.16), 20); $this->text($label, $bx + 44, $controlsY + 8, 14, $white, true, 'center'); $this->hit($bx - 4, $controlsY - 18, 96, 52, 'lyShift', $sign); } } elseif ($lyrics !== null) { $auto = $this->lyricsAutoScroll(); foreach ([['Défiler seul', 'lyAuto', 24.0, $auto], ['Synchroniser', 'karaSync', $W - 24 - 124, false]] as [$label, $id, $bx, $on]) { $fill = $on ? [255, 216, 74, 235] : self::mix($bottom, '#ffffff', $this->pressed($id) ? 0.32 : 0.16); $this->box($bx, $controlsY - 12, 124, 40, $fill, 20); $this->text($label, $bx + 62, $controlsY + 8, 13.5, $on ? '#1d2330' : $white, true, 'center'); $this->hit($bx - 4, $controlsY - 18, 132, 52, $id); } } } /** The ball (or star, note, bar) over the syllable sung now. @param list<array{0: string, 1: float}> $row */ private function karaokeMark(array $row, string $s, float $at, float $x, float $sw, float $cy, float $size, float $pos, string $gold): void { $next = INF; foreach ($row as [$other, $t]) { if ($t > $at) { $next = $t; break; } } if ($pos < $at || $pos >= min($next, $at + 1.2)) { return; } $f = $next === INF ? 0.5 : max(0.0, min(1.0, ($pos - $at) / max(0.05, $next - $at))); $mode = $this->karaokeMode(); if ($mode === 'bar') { $this->box($x, $cy + $size * 0.62, $sw * $f, 3, $gold, 1.5); return; } $bx = $x + $sw * (0.2 + 0.6 * $f); $by = $cy - $size * 0.95 - 7 - abs(sin($f * M_PI)) * 10; match ($mode) { 'star' => $this->icon('star', $bx - 8, $by - 8, 16, $gold), 'note' => $this->icon('note', $bx - 8, $by - 8, 16, $gold), default => $this->circle($bx, $by, 5.5, $gold), }; } // the editor -------------------------------------------------------------------------------------------------- private function enterEditor(): void { $wz = $this->wz; $song = (string)$wz?->playlist->current(); if ($song !== $this->edSong || !isset($this->fields['edText'])) { // the lyrics of the song (to time them again; without times: the lines alone), or nothing to type in $this->edSong = $song; $lyrics = $this->songLyrics(); $lyrics !== null ? $this->fillEditor($lyrics) : [$this->edLines, $this->edTimes, $this->fields['edText']] = [[], [], '']; } $this->edNext = -1; } /** * Lyrics put in the editor: their text (the blanks between verses kept), the lines to time, their times when they * have some (lyrics without times: none yet, « Caler sur la musique » gives them). */ private function fillEditor(Lyrics $lyrics): void { $texts = $lyrics->texts(); $this->edSong = (string)$this->wz?->playlist->current(); $this->fields['edText'] = trim(implode("\n", $texts), "\n"); $this->edLines = []; $this->edTimes = []; foreach ($texts as $i => $t) { if (trim($t) !== '') { $this->edLines[] = trim($t); $this->edTimes[] = $lyrics->synced ? (float)$lyrics->lines[$i][0] : null; } } $this->edNext = -1; } /** « Enregistrer sans synchro »: the lines typed become lyrics without times (they scroll during the song). */ public function edSaveText(): bool { $wz = $this->wz; $file = $wz?->playlist->current(); $text = trim(str_replace("\r", '', $this->fields['edText'] ?? '')); $lines = []; foreach (explode("\n", $text) as $row) { if (trim($row) === '' && ($lines === [] || trim($lines[count($lines) - 1][1]) === '')) { continue; // one blank between two verses } $lines[] = [0.0, trim($row)]; } if ($wz === null || $file === null || $lines === []) { $wz?->notice('Tapez, collez ou cherchez d\'abord les paroles', 4); return false; } if ($wz->saveLyrics($file, new Lyrics($lines, false)) === null) { return false; } $wz->notice('Paroles enregistrées sans synchro : elles défilent pendant la lecture', 4); return true; } /** The lines typed: the song from its start, each tap gives the next line its moment. */ public function edStart(): void { $wz = $this->wz; if ($wz === null || $wz->playlist->current() === null) { return; } $lines = array_values(array_filter(array_map('trim', explode("\n", $this->fields['edText'] ?? '')), fn (string $l) => $l !== '')); if ($lines === []) { $wz->notice('Tapez ou collez d\'abord les paroles, une ligne par phrase', 4); return; } $same = $lines === $this->edLines; $this->edLines = $lines; $this->edTimes = $same ? $this->edTimes : array_fill(0, count($lines), null); $this->edNext = 0; $this->field = ''; $this->scroll['page:lyricsEdit'] = 0.0; $wz->seek(0); if ($wz->player->state !== Player::PLAYING) { $wz->play(); } } /** « Ligne suivante »: the line takes the moment of the song now. */ public function edStamp(): void { if ($this->edNext < 0 || !isset($this->edLines[$this->edNext]) || $this->wz === null) { return; } $this->edTimes[$this->edNext] = round($this->wz->player->position, 2); $this->edNext++; // the next line stays in sight $this->scroll['page:lyricsEdit'] = max(0.0, ($this->edNext - 3) * 54.0); if ($this->edNext >= count($this->edLines)) { $this->wz->notice('Toutes les lignes ont leur moment : Enregistrer', 4); } } /** The lines with their moments saved through WinZik::saveLyrics() (the translations of the lines kept, the cloud told). */ public function edSave(): bool { $wz = $this->wz; $file = $wz?->playlist->current(); $lines = []; foreach ($this->edLines as $k => $l) { if (($this->edTimes[$k] ?? null) !== null) { $lines[] = [(float)$this->edTimes[$k], $l]; } } if ($wz === null || $file === null || $lines === []) { $wz?->notice('Rien à enregistrer : aucune ligne n\'a de moment', 4); return false; } usort($lines, fn (array $a, array $b) => $a[0] <=> $b[0]); if ($wz->saveLyrics($file, new Lyrics($lines, true)) === null) { return false; } $wz->notice('Paroles enregistrées (' . count($lines) . ' lignes)', 4); return true; } private function renderEditor(): void { $wz = $this->wz; [$title] = $this->titleOf($wz->playlist->active); if ($wz->playlist->current() === null) { $this->renderPage('Éditeur de paroles', '', [['note', 'Lancez d\'abord un morceau : les paroles se calent sur lui.']]); return; } if ($this->edNext < 0) { $this->renderPage('Éditeur de paroles', $title, [ ['note', 'Tapez, collez ou cherchez les paroles, une ligne par phrase. Puis « Caler sur la musique » : le morceau repart du début, touchez « Ligne suivante » quand chaque ligne commence. Ou enregistrez-les sans synchro : elles défileront.'], ['input', 'Les paroles', 'edText', 'Une ligne par phrase…'], ['pills', [['Coller', 'edPaste', null, false, 'paste'], ['Effacer', 'edClear', null, false]]], ['pills', [['Chercher sur Internet', 'edSearch', null, false, 'search']]], ['pills', [['Caler sur la musique', 'edStart', null, true, 'mic']]], ['pills', [['Enregistrer sans synchro', 'edSaveText', null, false, 'check']]], ]); return; } $rows = [['note', 'Touchez « Ligne suivante » au moment où chaque ligne commence. Une ligne touchée : le morceau y retourne, on la recale.']]; foreach ($this->edLines as $k => $l) { $t = $this->edTimes[$k] ?? null; $rows[] = ['line', $t === null ? '--:--' : sprintf('%d:%05.2f', intdiv((int)$t, 60), fmod($t, 60)), $l, $k, $k === $this->edNext ? 'next' : ($t === null ? 'todo' : 'done')]; } $done = count(array_filter($this->edTimes, fn ($t) => $t !== null)); $this->renderPage('Éditeur de paroles', $done . ' / ' . count($this->edLines) . ' lignes calées', $rows, [['check', 'edSave']], 150.0); // the buttons stay at the bottom: a big one for the finger, the others smaller $W = $this->W; $y = $this->H - 150; $this->rect(0, $y, $W, 150, $this->col('surface')); $this->rect(0, $y, $W, 1, $this->col('line')); $playing = $wz->player->state === Player::PLAYING; $this->pill(16, $y + 12, $W - 32, 'down', $this->edNext < count($this->edLines) ? 'Ligne suivante' : 'Terminé : enregistrer', $this->edNext < count($this->edLines) ? 'edStamp' : 'edSave', null); $this->hits[count($this->hits) - 1][3] += 12; // a bigger target: the finger taps it in rhythm $bw = ($W - 32 - 20) / 3; foreach ([['Annuler', 'edUndo', 'back'], [$playing ? 'Pause' : 'Lecture', 'play', $playing ? 'pause' : 'play'], ['Textes', 'edText', 'edit']] as $k => [$label, $id, $icon]) { $this->pill(16 + $k * ($bw + 10), $y + 80, $bw, $icon, $label, $id, null, false); } } /** What a tap does on the lyrics pages. */ private function tapLyrics(string $id, mixed $data): bool { $wz = $this->wz; if ($wz === null) { return false; } if ($this->tapLyricsWeb($id, $data)) { return true; } switch ($id) { case 'lyShift': $wz->shiftLyrics((int)$data < 0 ? -0.5 : 0.5); return true; case 'lyLrclib': $this->closeSheet(); $wz->fetchSyncedLyrics(true); return true; case 'lyMarket': $this->closeSheet(); $this->marketKind = 'lyrics'; $this->go('market'); return true; case 'lyOldSite': $this->closeSheet(); $d = $wz->startDownload(); $wz->notice($d !== null ? 'Recherche sur l\'ancien site WinZik...' : 'Lancez d\'abord un morceau', 4); return true; case 'lyImport': $this->closeSheet(); $this->pickMode = 'lyrics'; $this->folder = ''; $this->go('add'); return true; case 'lyWhisper': $this->closeSheet(); $wz->transcribe(); return true; case 'karaLine': // the song goes to that line and plays; the lyrics follow it again $line = $wz->lyrics?->lines[(int)$data] ?? null; if ($line !== null && !$wz->lyricsSynced()) { // no times: the song stays where it is (said quietly) $wz->notice('Paroles non synchronisées : pas de saut dans le morceau', 2); } elseif ($line !== null) { $wz->seek(max(0.0, (float)$line[0] - 0.3)); if ($wz->player->state !== Player::PLAYING) { $wz->play(); } $this->karaFree = false; $this->karaSpeed = 0.0; } return true; case 'karaFollow': $this->karaFree = false; $this->karaSpeed = 0.0; return true; case 'karaTurn': $wz->ui['mobile']['karaokeLandscape'] = !$this->karaokeLandscape(); $wz->touch(); $wz->notice($this->karaokeLandscape() ? 'Karaoké en paysage : tournez le téléphone' : 'Karaoké en portrait', 2); return true; case 'karaArea': return true; case 'karaSync': // « Synchroniser »: the editor, the lines in it (read again), ready to be timed $this->edSong = ''; $this->go('lyricsEdit'); return true; case 'edSaveText': if ($this->edSaveText()) { $this->back(); } return true; case 'edPaste': Clipboard::paste()->then(function (mixed $text): void { $text = str_replace("\r", '', trim((string)$text)); if ($text !== '') { $old = trim($this->fields['edText'] ?? ''); $this->fields['edText'] = $old === '' ? $text : $old . "\n" . $text; } }, fn () => null); return true; case 'edClear': $this->fields['edText'] = ''; $this->edLines = $this->edTimes = []; return true; case 'edStart': $this->edStart(); return true; case 'edStamp': $this->edStamp(); return true; case 'edUndo': if ($this->edNext > 0) { $this->edNext--; $this->edTimes[$this->edNext] = null; $wz->seek(max(0.0, (float)($this->edTimes[$this->edNext - 1] ?? 0.0))); } return true; case 'edLine': $k = (int)$data; $this->edNext = $k; $wz->seek(max(0.0, (float)($this->edTimes[$k - 1] ?? 0.0))); return true; case 'edText': $this->edNext = -1; return true; case 'edSave': if ($this->edSave()) { $this->back(); } return true; } return false; } private function submitLyrics(string $id): bool { if ($id === 'lfArtist') { $this->field = 'lfTitle'; // Entrée: the title next return true; } if ($id === 'lfTitle') { $this->lfStart(); return true; } return false; }}