<?phpnamespace App\Mobile;use App\Media\LyricsSearch;use App\Model\Karaoke;use App\Model\Language;use App\Model\Lyrics;use App\Model\Text;/** * The lyrics found on the Internet and what comes with them, on the phone: the page « Chercher des paroles » (artist and * title of the song, the candidates of LRCLIB, the marketplace, the old site, Claude as they come, a preview sheet: * Importer / Importer dans l'éditeur), the lyrics without times (a text the finger scrolls, followed slowly by the * estimated line), the translations under each line (a darker blue; « Traduction » shows / hides them, remembered), the * marks of the play list (⏱ synchronised, ≡ to scroll, translated) and its chip « Seulement avec paroles ». The core * (WinZik::searchLyrics(), importLyrics(), saveLyrics(), translate(), setLyricsOnly()...) does the work. */trait LyricsWebPages{ /** the song the search page looks for ('' : the song playing) */ public string $lfFile = ''; /** what a candidate is for: 'song' (its lyrics) or 'editor' (the text of the editor) */ public string $lfFor = 'song'; /** the search shown by the page (null: not started) */ public ?LyricsSearch $lfSearch = null; /** the candidate of the preview sheet */ public int $lfPick = -1; /** the song whose artist / title fill the fields */ private string $lfFieldsOf = "\0"; /** @var array<string, array{0: float, 1: list<string>}> the marks of the rows (file => [when looked, icons]) */ private array $lyricsMarkCache = []; /** the lyrics card of the Lecteur scrolled by the finger: it follows the estimated line again after this clock */ private float $cardFreeUntil = -100.0; /** one step of the Lecteur's card (dp of a finger's drag per line) */ private static function cardStep(): float { return 26.0; } // the search page ------------------------------------------------------------------------------------------------ /** The page « Chercher des paroles » for a song ($file null: the song playing), its fields filled from its tag / name. */ public function openLyricsFind(?string $file, string $for = 'song'): void { $wz = $this->wz; if ($wz === null) { return; } $file ??= (string)$wz->playlist->current(); $this->closeSheet(); $this->lfFor = $for; if ($file !== $this->lfFile) { $this->lfSearch = null; } $this->lfFile = $file; $this->go('lyricsFind'); } private function enterLyricsFind(): void { $wz = $this->wz; if ($this->lfFile === '' || !in_array($this->lfFile, $wz->playlist->items, true)) { $this->lfFile = (string)$wz->playlist->current(); } if ($this->lfFieldsOf !== $this->lfFile) { [$artist, $title] = $this->lfFile !== '' ? $wz->songQuery($this->lfFile) : ['', '']; $this->fields['lfArtist'] = $artist; $this->fields['lfTitle'] = $title; $this->lfFieldsOf = $this->lfFile; $this->lfSearch = null; } $this->lfPick = -1; } /** « Chercher »: the sources asked at once, their candidates shown as they come. */ public function lfStart(): void { $wz = $this->wz; $artist = trim($this->fields['lfArtist'] ?? ''); $title = trim($this->fields['lfTitle'] ?? ''); $this->field = ''; if ($artist === '' && $title === '') { $wz->notice('Donnez au moins le titre du morceau', 3); return; } $this->lfSearch = $wz->searchLyrics($artist, $title, $this->lfFile !== '' ? $this->lfFile : null); if ($this->lfSearch === null) { $wz->notice('Pas de réseau ici : collez les paroles dans l\'éditeur', 4); } } /** @return array<string, mixed>|null a candidate of the search */ private function lfCandidate(int $index): ?array { return $this->lfSearch?->candidates[$index] ?? null; } /** « Importer »: the candidate becomes the song's lyrics (saved, sent to the account, shown), back where it came from. */ public function lfImport(int $index): bool { $wz = $this->wz; $c = $this->lfCandidate($index); if ($c === null || $this->lfFile === '') { return false; } $saved = $wz->importLyrics($c, $this->lfFile); if ($saved === null) { $wz->notice('Ces paroles n\'ont pas pu être enregistrées', 4); return false; } if ($this->lfFile !== $wz->playlist->current()) { $wz->notice('Paroles enregistrées pour « ' . (string)($c['title'] ?? '') . ' »', 4); } unset($this->lyricsMarkCache[$this->lfFile]); $this->sheet = ''; $this->sheetOpen = 0.0; if ($this->screen === 'lyricsFind') { $this->back(); } return true; } /** « Importer dans l'éditeur » (or « Mettre dans l'éditeur »): the lines of the candidate typed into the editor. */ public function lfToEditor(int $index): bool { $wz = $this->wz; $c = $this->lfCandidate($index); $lyrics = $c['lyrics'] ?? null; if (!$lyrics instanceof Lyrics) { return false; } // the editor times the song playing: the song chosen plays if ($this->lfFile !== '' && $this->lfFile !== $wz->playlist->current()) { $line = array_search($this->lfFile, $wz->playlist->items, true); if (is_int($line)) { $wz->playIndex($line); } } $this->fillEditor($lyrics); $this->sheet = ''; $this->sheetOpen = 0.0; $toEditor = $this->lfFor === 'editor'; if ($this->screen === 'lyricsFind') { $this->back(); } if (!$toEditor) { $this->go('lyricsEdit'); } $wz->notice(count($this->edLines) . ' lignes dans l\'éditeur' . ($lyrics->synced ? ' (avec leurs temps)' : ' : calez-les sur la musique, ou enregistrez-les telles quelles'), 4); return true; } private function renderLyricsFind(): void { $wz = $this->wz; $file = $this->lfFile; $line = $file === '' ? false : array_search($file, $wz->playlist->items, true); [$song, $artist] = is_int($line) ? $this->titleOf($line) : ['Aucun morceau', '']; $has = $file !== '' && $wz->hasLyrics($file); $rows = [ ['note', $this->lfFor === 'editor' ? 'Les paroles trouvées remplissent l\'éditeur : vous les calez ensuite sur la musique.' : 'Sur LRCLIB, la marketplace WinZik, l\'ancien site (et Claude sur un ordinateur). Les paroles sans temps se lisent en les faisant défiler du doigt.'], ['row2', 'note', $song, ($artist !== '' ? $artist . ' · ' : '') . ($has ? 'a déjà des paroles' : 'pas encore de paroles'), 'page', null, $has ? 'check' : ''], ['input', 'Artiste', 'lfArtist', 'Le chanteur, le groupe'], ['input', 'Titre', 'lfTitle', 'Le titre du morceau'], ['pills', [['Chercher', 'lfStart', null, true, 'search']]], ]; $search = $this->lfSearch; if ($search !== null) { $n = count($search->candidates); $rows[] = ['section', 'Résultats', $n > 0 ? ($n > 1 ? "$n trouvées" : '1 trouvée') : '']; $rows[] = ['lySources', $search->sources]; if (!$search->done()) { $rows[] = ['busy', $search->describe()]; } foreach ($search->candidates as $k => $c) { $rows[] = ['lyCand', $c, $k]; } if ($search->done() && $n === 0) { $rows[] = ['note', 'Rien trouvé. Vérifiez l\'orthographe, essayez le titre seul, ou écrivez les paroles vous-même dans l\'éditeur.']; } } if ($this->lfFor !== 'editor') { $rows[] = ['space', 8]; $rows[] = ['pills', [['Écrire ou coller les paroles', 'lfEditor', null, false, 'edit']]]; } $this->renderPage('Chercher des paroles', $this->lfFor === 'editor' ? 'Pour l\'éditeur' : 'Sur Internet', $rows); } /** The sheet of a candidate: its first lines, Importer / Importer dans l'éditeur. */ private function lyricsPreviewSheet(): array { $c = $this->lfCandidate($this->lfPick); if ($c === null) { return [['title', 'Paroles', ''], ['button', 'Fermer', 'sheetClose', null, true]]; } /** @var Lyrics $lyrics */ $lyrics = $c['lyrics']; $texts = $lyrics->texts(); $words = count(array_filter($texts, fn (string $t) => trim($t) !== '')); $shown = array_slice($texts, 0, 9); if (count($texts) > 9) { $shown[] = '…'; } $sub = (string)$c['sourceName'] . ' · ' . ($c['synced'] ? 'synchronisées' : 'non synchronisées') . ' · ' . $words . ' lignes'; $rows = [ ['title', trim((string)$c['artist'] . ' – ' . (string)$c['title'], ' –'), $sub], ['lyText', $shown], ]; if (!$c['synced'] && $this->lfFor !== 'editor') { $rows[] = ['note', 'Sans temps : elles défileront pendant la lecture. Vous pourrez les synchroniser dans l\'éditeur.']; } if ($this->lfFor === 'editor') { $rows[] = ['button', 'Mettre dans l\'éditeur', 'lfToEditor', $this->lfPick, true]; } else { $rows[] = ['button', 'Importer', 'lfImport', $this->lfPick, true]; $rows[] = ['button', 'Importer dans l\'éditeur', 'lfToEditor', $this->lfPick, false]; } return $rows; } private function lyricsRowHeight(array $row): ?float { return match ($row[0]) { 'lyCand' => 92.0, 'lySources' => 46.0, 'lyText' => 22.0 * count($row[1]) + 18, default => null, }; } /** The rows of the lyrics' pages and sheets. @return bool drawn */ private function drawLyricsRow(array $row, float $y, float $h): bool { $W = $this->W; switch ($row[0]) { case 'lyCand': [, $c, $k] = $row; $ix = $this->cards ? 12.0 : 0.0; if ($this->pressed('lfPick', $k)) { $this->rect($ix, $y, $W - 2 * $ix, $h, self::alpha($this->col('accent'), 25)); } // ⏱ synchronised or ≡ text only: a chip at the right of the title $synced = (bool)$c['synced']; $chip = $synced ? 'Synchro' : 'Texte'; $cw = $this->textWidth($chip, 12, true) + 34; $cx = $W - 24 - $cw; $this->box($cx, $y + 12, $cw, 26, $synced ? $this->col('soft') : $this->col('surface2'), 13); $this->icon($synced ? 'clock' : 'list', $cx + 8, $y + 17, 16, $synced ? $this->col('accent') : $this->col('muted')); $this->text($chip, $cx + 28, $y + 25, 12, $synced ? $this->col('accent') : $this->col('muted'), true); $this->text((string)$c['title'], 24, $y + 25, 15.5, $this->col('text'), true, 'left', $cx - 32); $d = (float)($c['duration'] ?? 0); $sub = (string)$c['sourceName'] . ' · ' . ((string)$c['artist'] !== '' ? (string)$c['artist'] : 'Artiste inconnu') . ($d > 0 ? ' · ' . self::clock($d) : ''); $this->text($sub, 24, $y + 48, 13, $this->col('muted'), false, 'left', $W - 48); $preview = trim((string)preg_replace('/\s+/u', ' ', (string)($c['preview'] ?? ''))); $this->text('« ' . $preview . ' »', 24, $y + 72, 13, $this->col('text'), false, 'left', $W - 48); $this->hit($ix, $y, $W - 2 * $ix, $h, 'lfPick', $k); return true; case 'lySources': // each source: searching (…), found (✓), nothing, off $x = 24.0; foreach (['lrclib' => 'LRCLIB', 'market' => 'Marketplace', 'archive' => 'Ancien site', 'claude' => 'Claude'] as $key => $label) { $state = (string)($row[1][$key] ?? 'off'); if ($state === 'off') { continue; } $w = $this->textWidth($label, 12, true) + 30; if ($x + $w > $W - 16) { break; } $found = $state === 'found'; $this->box($x, $y + 8, $w, 28, $found ? $this->col('soft') : self::alpha($this->col('surface'), 200), 14); if ($state === 'searching') { $this->spinner($x + 13, $y + 22, 5, $this->col('accent')); } else { $this->icon($found ? 'check' : 'close', $x + 6, $y + 15, 14, $found ? $this->col('accent') : $this->col('muted')); } $this->text($label, $x + 24, $y + 22, 12, $found ? $this->col('accent') : $this->col('muted'), true); $x += $w + 6; } return true; case 'lyText': $ty = $y + 18; foreach ($row[1] as $l) { $this->text($l === '' ? ' ' : (string)$l, 24, $ty, 15, $this->col('text'), false, 'left', $W - 48); $ty += 22; } return true; } return false; } /** What a tap does on the search, the translations, the filter. */ private function tapLyricsWeb(string $id, mixed $data): bool { $wz = $this->wz; switch ($id) { case 'lyFind': // the row « Paroles… » of a song (its sheet), the lyrics sheet, the karaoke $file = is_int($data) ? ($wz->playlist->items[$data] ?? null) : null; $this->openLyricsFind($file === null ? null : (string)$file); return true; case 'lfStart': $this->lfStart(); return true; case 'lfPick': $this->lfPick = (int)$data; $this->openSheet('lyPreview'); return true; case 'lfImport': $this->lfImport((int)$data); return true; case 'lfToEditor': $this->lfToEditor((int)$data); return true; case 'lfEditor': $this->back(); if ($this->lfFile !== '' && $this->lfFile !== $wz->playlist->current()) { $line = array_search($this->lfFile, $wz->playlist->items, true); is_int($line) && $wz->playIndex($line); } $this->go('lyricsEdit'); return true; case 'edSearch': $this->openLyricsFind(null, 'editor'); return true; case 'trToggle': $wz->ui['mobile']['translation'] = !$this->showTranslation(); $wz->touch(); $this->karaHeightsOf = ''; $wz->notice($this->showTranslation() ? 'Traduction affichée sous chaque ligne' : 'Traduction masquée', 2); return true; case 'trNow': $this->translateNow(); return true; case 'lyAuto': $wz->ui['mobile']['lyricsAutoScroll'] = !$this->lyricsAutoScroll(); $wz->touch(); $this->karaFree = false; $wz->notice($this->lyricsAutoScroll() ? 'Les paroles défilent seules, à peu près au rythme du morceau' : 'Les paroles défilent sous votre doigt seulement', 3); return true; case 'lyAutoTr': $wz->options['autoTranslate'] = !($wz->options['autoTranslate'] ?? true); $wz->touch(); if ($wz->options['autoTranslate']) { $wz->autoTranslate(); } return true; case 'lyLang': $wz->options['translateLang'] = (string)$data; $wz->touch(); $this->karaHeightsOf = ''; $this->lyricsMarkCache = []; if (!empty($wz->options['autoTranslate'] ?? true)) { $wz->autoTranslate(); } return true; case 'lyOnly': $wz->setLyricsOnly(!$wz->playsLyricsOnly()); $this->scroll['list'] = 0.0; return true; } return false; } /** « Traduire maintenant »: the song playing, in the language of the options (progress: translationState()). */ public function translateNow(): void { $wz = $this->wz; if ($this->songLyrics() === null) { $wz->notice('Pas de paroles à traduire : lancez un morceau qui en a', 3); return; } $lang = $wz->translationLang(); $wz->notice('Traduction en ' . mb_strtolower(Language::name($lang)) . '…', 2); $wz->translate(null, $lang)->then(fn () => null, function (\Throwable $e) use ($wz): void { $wz->notice('Traduction impossible : ' . $e->getMessage(), 5); }); } // translations ------------------------------------------------------------------------------------------------------ /** « Traduction »: the translations shown under the lines (remembered; on by default). */ public function showTranslation(): bool { return (bool)($this->wz?->ui['mobile']['translation'] ?? true); } /** Lines without times scroll by themselves at about the pace of the song (remembered; on by default). */ public function lyricsAutoScroll(): bool { return (bool)($this->wz?->ui['mobile']['lyricsAutoScroll'] ?? true); } /** The translation of a line of the song playing, '' when none or hidden. */ private function lineTranslation(Lyrics $lyrics, int $i): string { if (!$this->showTranslation() || $lyrics->translationLang === '') { return ''; } return trim($lyrics->translation($i)); } /** The blue of the translations: darker than the lyrics on a light screen, light on a dark one (the karaoke, Nuit). */ private function translationInk(bool $onDark): string { $s = $this->col('surface'); $dark = is_string($s) && strlen($s) === 7 && hexdec(substr($s, 1, 2)) + hexdec(substr($s, 3, 2)) + hexdec(substr($s, 5, 2)) < 300; return $onDark || $dark ? '#8cc4ff' : '#1c56a6'; } /** * The chip of the translations: a toggle when the lyrics have some, « Traduction… 40 % » while they are made, * « Traduire » when they could be (lyrics in another language, or one not known). null: nothing to show. * @return array{0: string, 1: string, 2: bool, 3: float}|null label, id, on, progress (-1: none) */ private function translationChip(): ?array { $wz = $this->wz; $lyrics = $this->songLyrics(); if ($lyrics === null) { return null; } $state = $wz->translationState(); if ($state['state'] === 'translating' && $state['file'] === $wz->playlist->current()) { return ['Traduction ' . (int)round(100 * $state['progress']) . ' %', 'trToggle', false, (float)$state['progress']]; } if ($lyrics->translations !== []) { return ['Traduction', 'trToggle', $this->showTranslation(), -1.0]; } $lang = $lyrics->language ?: Language::detect($lyrics->plainText()); if ($lang === $wz->translationLang()) { return null; } return ['Traduire', 'trNow', false, -1.0]; } /** A chip of the karaoke (on its dark stage): its width. */ private function stageChip(float $x, float $y, string $icon, string $label, string $id, bool $on, float $progress = -1.0): float { $w = $this->textWidth($label, 13, true) + 44; $fill = $on ? [255, 216, 74, 235] : [255, 255, 255, $this->pressed($id) ? 70 : 38]; $ink = $on ? '#1d2330' : '#ffffff'; $this->box($x, $y, $w, 36, $fill, 18); if ($progress >= 0) { $this->box($x + 14, $y + 30, ($w - 28) * max(0.04, min(1.0, $progress)), 3, '#8cc4ff', 1.5); } $this->icon($icon, $x + 12, $y + 8, 20, $ink); $this->text($label, $x + 36, $y + 18, 13, $ink, true); $this->hit($x - 4, $y - 6, $w + 8, 48, $id); return $w; } // the play list ----------------------------------------------------------------------------------------------------- /** The row « Paroles… » of a song's sheet. */ private function lyricsRowLabel(int $line): string { $marks = $this->lyricsMarks($line); return match (true) { $marks === [] => 'Paroles… (en chercher)', in_array('clock', $marks, true) => 'Paroles… (synchronisées)', default => 'Paroles… (non synchronisées)', }; } /** * The marks of a song's lyrics on its row: 'clock' (synchronised), 'list' (lines to scroll), 'translate' (translated * into the language of the options). Looked at again every second or two (a file read once per song). * @return list<string> */ private function lyricsMarks(int $line): array { $wz = $this->wz; $file = $wz->playlist->items[$line] ?? null; if ($file === null || !$wz->hasLyrics((string)$file)) { return []; } $file = (string)$file; if ($file === $wz->playlist->current() && $wz->lyrics !== null) { $l = $wz->lyrics; return [$l->synced ? 'clock' : 'list', ...($l->hasTranslation($wz->translationLang()) ? ['translate'] : [])]; } $cached = $this->lyricsMarkCache[$file] ?? null; if ($cached !== null && $this->clock - $cached[0] < 1.5) { return $cached[1]; } $marks = []; $found = \App\Cloud\Cloud::isPseudo($file) ? null : $wz->findLyricsFile($file); $l = $found !== null ? Lyrics::load($found) : null; if ($l !== null && !$l->isEmpty()) { $marks = [$l->synced ? 'clock' : 'list', ...($l->hasTranslation($wz->translationLang()) ? ['translate'] : [])]; } elseif (\App\Cloud\Cloud::isPseudo($file)) { $marks = ['list']; } $this->lyricsMarkCache[$file] = [$this->clock, $marks]; return $marks; } /** the height of the row « Seulement avec paroles » under the order chips */ private static function lyricsFilterHeight(): float { return 46.0; } /** « Seulement avec paroles » (the play mode: the songs without lyrics are skipped and hidden) and how many have some. */ private function renderLyricsFilter(float $y): void { $wz = $this->wz; $on = $wz->playsLyricsOnly(); $label = 'Seulement avec paroles'; $w = $this->textWidth($label, 14, $on) + 52; $this->box(16, $y, $w, 36, $on ? $this->col('soft') : self::alpha($this->col('surface'), 160), 18); if ($this->pressed('lyOnly')) { $this->box(16, $y, $w, 36, self::alpha($this->col('accent'), 30), 18); } $this->icon($on ? 'check' : 'lyrics', 28, $y + 9, 18, $on ? $this->col('accent') : $this->col('muted')); $this->text($label, 52, $y + 18, 14, $on ? $this->col('accent') : $this->col('muted'), $on); $this->hit(16, $y - 6, $w, 48, 'lyOnly'); $n = $wz->songsWithLyrics(); $this->text($n . ' / ' . $wz->playlist->count() . ' avec paroles', $this->W - 20, $y + 18, 12.5, $this->col('muted'), false, 'right', $this->W - $w - 48); } /** « Seulement avec paroles » and none has any: what to do. */ private function renderLyricsOnlyEmpty(float $top, float $bottom): void { $W = $this->W; $cy = $top + min(150.0, ($bottom - $top) * 0.3); $this->circle($W / 2, $cy, 40, $this->col('soft')); $this->icon('lyrics', $W / 2 - 20, $cy - 20, 40, $this->col('accent')); $this->text('Aucun morceau n\'a de paroles', $W / 2, $cy + 66, 19, $this->col('text'), true, 'center'); $y = $cy + 96; foreach ($this->wrap('Rien ne sera lu en mode « Seulement avec paroles ». Cherchez les paroles d\'un morceau (son menu > « Paroles… »), ou revenez à tous les morceaux.', 14.5, false, $W - 64) as $l) { $this->text($l, $W / 2, $y, 14.5, $this->col('muted'), false, 'center'); $y += 21; } $first = $this->wz->playlist->active >= 0 ? $this->wz->playlist->active : 0; $this->pill(32, $y + 12, $W - 64, 'search', 'Chercher des paroles', 'lyFind', $first); $this->pill(32, $y + 74, $W - 64, 'list', 'Tous les morceaux', 'lyOnly', null, false); } // the Lecteur's lyrics card ------------------------------------------------------------------------------------------- /** @return list<int> the lines of the lyrics that have words (the card skips the blanks between verses) */ private static function wordLines(Lyrics $lyrics): array { $out = []; foreach ($lyrics->texts() as $i => $t) { if (trim($t) !== '') { $out[] = $i; } } return $out; } /** * The lyrics of the moment on the Lecteur: synchronised, their syllables light up (the next line, or the translation * under it); without times, the estimated line and the next (or its translation), the finger scrolls them, « non * synchronisées » on the card. A tap: the karaoke on the whole screen (no lyrics: the search). */ private function renderLyrics(float $top, float $bottom): void { $wz = $this->wz; $lyrics = $wz->lyrics; $W = $this->W; if ($lyrics === null || $lyrics->isEmpty() || $wz->playlist->current() === null) { $this->hit(16, $top, $W - 32, $bottom - $top, 'lyricsBox'); $label = $wz->playlist->current() === null ? 'Pas de paroles' : 'Pas de paroles · touchez pour en trouver'; $tw = $this->textWidth($label, 14) + 24; $this->icon('lyrics', ($W - $tw) / 2, ($top + $bottom) / 2 - 8, 16, self::alpha($this->col('muted'), 150)); $this->text($label, ($W - $tw) / 2 + 24, ($top + $bottom) / 2, 14, self::alpha($this->col('muted'), 180)); return; } $this->box(16, $top, $W - 32, $bottom - $top, self::alpha($this->col('surface'), 150), 18); $bs = 40.0; $max = $W - 2 * (24 + $bs + 8); $ink = $this->translationInk(false); $cy = $top + 22; if (!$lyrics->synced) { // a text to scroll: the finger drags it line by line, the estimated line follows the song $this->scroll['lyCard'] ??= 0.0; $this->speed['lyCard'] ??= 0.0; $shown = self::wordLines($lyrics); $this->scrollMax['lyCard'] = max(0.0, (count($shown) - 1) * self::cardStep()); $this->scroll['lyCard'] = min($this->scroll['lyCard'], $this->scrollMax['lyCard']); $this->hit(16, $top, $W - 32, $bottom - $top, 'lyricsBox', null, 'lyCard'); $k = max(0, min(count($shown) - 1, (int)round($this->scroll['lyCard'] / self::cardStep()))); $i = $shown[$k] ?? 0; $this->text(Karaoke::plain((string)$lyrics->lines[$i][1]), $W / 2, $cy, 16, $this->col('text'), true, 'center', $max); $tr = $this->lineTranslation($lyrics, $i); if ($tr !== '') { $this->text($tr, $W / 2, $cy + 23, 13.5, $ink, false, 'center', $max); } elseif (isset($shown[$k + 1])) { $this->text(Karaoke::plain((string)$lyrics->lines[$shown[$k + 1]][1]), $W / 2, $cy + 24, 14, $this->col('muted'), false, 'center', $max); } // « non synchronisées »: a small label on the card's lower edge $label = 'non synchronisées'; $lw = $this->textWidth($label, 10.5, true) + 30; $this->box(28, $bottom - 9, $lw, 18, $this->col('surface'), 9); $this->box(28, $bottom - 9, $lw, 18, self::alpha($this->col('accent'), 30), 9); $this->icon('list', 34, $bottom - 6, 12, $this->col('accent')); $this->text($label, 50, $bottom, 10.5, $this->col('accent'), true); } else { $this->hit(16, $top, $W - 32, $bottom - $top, 'lyricsBox'); $pos = $wz->player->position; $i = $lyrics->indexAt($pos); $lines = $lyrics->lines; if ($i < 0) { $this->text(Karaoke::plain($lines[0][1]), $W / 2, ($top + $bottom) / 2, 15, $this->col('muted'), false, 'center', $max); } else { [$start, $raw] = $lines[$i]; $next = $lines[$i + 1][0] ?? $start + 6; [$syllables, $times] = Karaoke::timing($raw, $start, $next); $size = 17.0; $plain = Karaoke::plain($raw); if ($syllables === [] || $this->textWidth($plain, $size, true) > $max) { $this->text($plain, $W / 2, $cy, $size, $this->col('accent'), true, 'center', $max); } else { $x = $W / 2 - $this->textWidth(implode('', $syllables), $size, true) / 2; foreach ($syllables as $k => $syl) { $sung = $pos >= ($times[$k] ?? INF); $this->text($syl, $x, $cy, $size, $sung ? $this->col('accent') : $this->col('text'), true); $x += $this->textWidth($syl, $size, true); } } $tr = $this->lineTranslation($lyrics, $i); if ($tr !== '') { $this->text($tr, $W / 2, $cy + 24, 13.5, $ink, false, 'center', $max); } elseif (isset($lines[$i + 1])) { $this->text(Karaoke::plain($lines[$i + 1][1]), $W / 2, $cy + 26, 14, $this->col('muted'), false, 'center', $max); } } } // the karaoke on the whole screen: a button that shows it $by = ($top + $bottom) / 2 - $bs / 2; $this->circle($W - 24 - $bs / 2, $by + $bs / 2, $bs / 2, $this->pressed('karaokeOpen') ? self::mix($this->col('accent'), '#000000', 0.15) : $this->col('accent')); $this->icon('fullscreen', $W - 24 - $bs / 2 - 10, $by + $bs / 2 - 10, 20, $this->col('onAccent')); $this->hit($W - 24 - $bs - 6, $top, $bs + 14, $bottom - $top, 'karaokeOpen'); } /** Each frame: the card of lyrics without times follows the estimated line, unless the finger scrolled it a moment ago. */ private function updateLyricsCard(float $dt): void { $lyrics = $this->songLyrics(); if ($lyrics === null || $lyrics->synced || !isset($this->scroll['lyCard'])) { return; } $held = $this->press !== null && $this->press['scroll'] === 'lyCard' && $this->press['moved']; if ($held || ($this->speed['lyCard'] ?? 0.0) != 0.0) { $this->cardFreeUntil = $this->clock + self::KARAOKE_FOLLOW; return; } if ($this->clock < $this->cardFreeUntil || !$this->lyricsAutoScroll()) { return; } $est = $this->wz->estimatedLine(); $k = 0; foreach (self::wordLines($lyrics) as $n => $i) { if ($i <= $est) { $k = $n; } } $target = $k * self::cardStep(); $now = $this->scroll['lyCard']; $this->scroll['lyCard'] = abs($target - $now) < 0.5 ? $target : $now + ($target - $now) * min(1.0, $dt * 4); } // the options ------------------------------------------------------------------------------------------------------- /** @return list<array> the rows of the Options about translations and the lyrics without times */ private function lyricsOptionRows(): array { $wz = $this->wz; $lang = $wz->translationLang(); $chips = []; foreach (Language::NAMES as $code => $name) { $chips[] = [$name, 'lyLang', $code, $code === $lang]; } $rows = [ ['section', 'Traduction des paroles'], ['toggle', 'Traduire automatiquement', 'Les paroles d\'une autre langue', 'lyAutoTr', null, (bool)($wz->options['autoTranslate'] ?? true)], ['toggle', 'Afficher la traduction', 'Sous chaque ligne, en bleu', 'trToggle', null, $this->showTranslation()], ['note', 'Traduire en :'], ['chipset', $chips], ]; $state = $wz->translationState(); if ($state['state'] === 'translating') { $rows[] = ['progress', $state['text'] !== '' ? $state['text'] : 'Traduction…', (float)$state['progress']]; } elseif ($state['state'] === 'error' && $state['text'] !== '') { $rows[] = ['note', $state['text'], $this->col('danger')]; } elseif ($this->songLyrics() !== null && $wz->hasTranslation(null, $lang)) { $rows[] = ['note', 'Le morceau en cours est traduit en ' . mb_strtolower(Language::name($lang)) . '.']; } $rows[] = ['pills', [['Traduire maintenant', 'trNow', null, false, 'translate']]]; $rows[] = ['section', 'Paroles non synchronisées']; $rows[] = ['toggle', 'Défilement automatique', 'Elles suivent à peu près le morceau', 'lyAuto', null, $this->lyricsAutoScroll()]; $rows[] = ['toggle', 'Seulement les morceaux avec paroles', $wz->songsWithLyrics() . ' morceau(x) de la playlist en ont', 'lyOnly', null, $wz->playsLyricsOnly()]; return $rows; }}