Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187import { createEffect, createMemo, type Component, For, Show } from "solid-js";
import type { LibrarySong } from "../lib/harvest/types";import { lineIndexAt } from "../lib/lyrics/lrc";import { lyrics } from "../lib/lyrics";import { player } from "../lib/player";import { formatClock } from "../lib/player/time";
/** * The synced lyric, sung along the bar. * * It sits against the transport and reads to its right edge — a column of words ending on one line, * beside the controls you are watching, rather than hanging off the three names like a fourth * caption. The three names are a caption and this is a timeline: the line being heard is the line the * listener is looking for, and the rest of the list is the shape around it, which is why the column * is a window with the active row on its reading line rather than a block of text that happens to * be scrolled. Three lines are shown (see `.pb-lyrics` in App.css, which owns the number); enough * to see what is coming, few enough that the art column stays the tallest thing on the row — which * is what keeps the bar's height constant while a lyric arrives mid-track, while you are looking at * it. * * Three decisions worth their comments: * * - **one clock.** The highlight is read from `props.at`, which is the bar's own `shown()` — the * same number the elapsed clock, the waveform's fill and the thumb are drawn from. A second * reading of the playhead here (a `peekLivePosition()` of its own, say) would be a second answer * to "where in the track is this", and the two agreeing most of the time is worse than their * disagreeing: the words would drift out of step with the line above them, which is the one thing * a column like this cannot do. It is also why the shifter's latency is not compensated for the * way the beat pulse is — the playhead does not wear that correction, so a highlight that did * would be a word ahead of the position the bar is printing. * - **the words are looked up by the track, not remembered about one.** The column asks the store * what it knows about the song the bar is naming, so a lyric found for the previous track is not * a thing this component has to notice and refuse — it never enters the derivation. That is the * same guarantee `pb-art-with-track` gives the cover by other means, and it is here because an * async third-party look-up is exactly what produces the stale answer in the first place. * - **the active row is painted, not re-rendered.** Solid would give every row its own attribute * binding and re-run fifty of them whenever the index moved; instead one effect carries the mark * from the row that had it to the row that has it, which is the whole of the work. The `active()` * memo is what makes that cheap — it recomputes at the position sample's 4 Hz and *notifies* only * when the line actually changes, which is every few seconds. */interface LyricsProps { /** The track the bar is naming. The words are shown only while they belong to it. */ song: LibrarySong | null; /** Where the listener is, in track seconds — the bar's own reading, passed and not re-derived. */ at: number;}
/** A row that refuses to keep focus, the way every control in this bar does (see `PlayerBar.tsx`). */const noFocus = (e: MouseEvent) => e.preventDefault();
export const LyricsColumn: Component<LyricsProps> = (props) => { const store = lyrics();
/** * The lines, if this track has any. * * Asked of the store by track rather than read off a "current lyrics" signal, and that is the * whole of the staleness story: the words are looked up by what is on the bar, so an answer that * belongs to another track is not a thing this has to notice and refuse — it is not in the * derivation to begin with. The first version compared a stored state's id against the song's and * cost every reader a second run per track change to undo the frame in which they disagreed * (`EFFECT_RELAY_TEAR`); asking by track makes the disagreement unrepresentable. * * Every status but `found` is nothing to draw, and is *not* said in the bar: a column that * reserved its three lines for "no lyrics" would spend them on silence for the tracks that have * nothing, and the absence of the column already says it. */ const found = createMemo( () => { // The setting is part of the derivation and not a class on the container, so that switching // lyrics off takes the words away on the same frame the press lands — and switching them back on // brings them from what is already known, without asking the host a second time for a track it // has already answered for. if (!store.enabled()) return null; const answer = store.for(props.song); if (!answer || answer.status !== "found" || !answer.lines.length) return null; return answer; }, { name: "pb-lyrics-found" }, );
const lines = createMemo(() => found()?.lines ?? [], { name: "pb-lyric-lines" });
/** * Which row is being heard. * * `props.at` is the bar's displayed position, so scrubbing drags the highlight with it — the same * rule the waveform's fill runs by, and for the same reason: the finger on the bar is where the * listener says they are. * * The index is the only reactive thing the rows depend on, which is what keeps this O(lines) per * line change rather than O(rows): the rows are `<For>`-keyed on the line and are not re-created * when the mark moves, and the position is sampled four times a second, so the search runs 4× a * second over a lyric of any length and the DOM is touched only when the number that comes back is * a different one. */ const active = createMemo(() => lineIndexAt(lines(), props.at), { name: "pb-lyric-active" });
let scroller: HTMLSpanElement | undefined;
/** * Move the mark, and bring the marked row to the reading line. * * Centred rather than pinned to the top, because what the listener reads is the current line and * what they read next is the one after it: with the active row in the middle both are on screen, * and the list moves once per line instead of jumping a whole window every three lines. * * The effect is armed on the answer's row count as well as on the index — not because the count * is read here, but because both are numbers about rows rather than the rows themselves. The rows * are the `Show`'s children, so they are destroyed and rebuilt whenever the column goes away and * comes back, and an effect waiting for the index to move would sit out that rebuild — leaving the * lyric back on screen with nobody marked as singing. The other case is the same fact wearing a * hat: a new track whose opening line lands on the index the previous track's happened to be on, * where an unchanged number is not an unchanged set of rows to put it on. */ createEffect( () => ({ rows: found()?.lines.length ?? 0, at: active() }), ({ at }) => { const el = scroller; if (!el) return; const children = el.children; for (let i = 0; i < children.length; i++) { const row = children[i] as HTMLElement; const on = i === at; if (row.hasAttribute("data-active") === on) continue; if (on) { row.setAttribute("data-active", "true"); // The a11y tree is told the same thing the ink is: a listener following the lyric by ear // gets `aria-current` on the row being heard, and nothing on the others. row.setAttribute("aria-current", "true"); } else { row.removeAttribute("data-active"); row.removeAttribute("aria-current"); } } // Before the first line lands there is nothing to be on, and the window sits at the top of the // list — where the words that have not been sung yet start. const marked = at >= 0 ? (children[at] as HTMLElement | undefined) : undefined; if (!marked || children.length === 0) { el.scrollTop = 0; return; } el.scrollTop = Math.max(0, marked.offsetTop - (el.clientHeight - marked.clientHeight) / 2); }, { name: "pb-lyric-paint" }, );
/** Whose words these are, in the tooltip: the bar never claims a lyric it did not find. */ const credit = createMemo( () => { const record = found()?.record; if (!record) return ""; const who = [record.artistName, record.trackName].filter(Boolean).join(" \u2014 "); return `lyrics \u00b7 lrclib${who ? ` \u00b7 ${who}` : ""} \u00b7 click a line to sing from there`; }, { name: "pb-lyrics-credit" }, );
return ( <Show when={found()}> <span class="pb-lyrics" role="group" aria-label="Lyrics" title={credit()}> <span class="pb-lyric-scroller" ref={scroller}> <For each={lines()}> {(line) => ( <button type="button" class="pb-lyric" tabindex={-1} onMouseDown={noFocus} // The time on every row, because the row is a seek: what a listener clicking a line // is asking for is "start the song where that line is", and the number is what makes // the click predictable before it happens. title={`${formatClock(line.at)} \u00b7 sing from here`} onClick={() => player().seek(line.at)} > {line.text} </button> )} </For> </span> </span> </Show> );};