From 356c79932f00f40c3f83c0bb84446244229346b4 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Fri, 20 Nov 2026 00:13:07 -0500 Subject: [PATCH] feat: blur inactive lyrics and fill words as they are sung Co-Authored-By: Claude Opus 5.5 --- apps/mobile/components/lyrics-view.tsx | 215 ++++++++++++++++++++----- apps/web/components/lyrics-panel.tsx | 148 +++++++++++++---- 2 files changed, 284 insertions(+), 79 deletions(-) diff --git a/apps/mobile/components/lyrics-view.tsx b/apps/mobile/components/lyrics-view.tsx index 68176dc..b55dd25 100644 --- a/apps/mobile/components/lyrics-view.tsx +++ b/apps/mobile/components/lyrics-view.tsx @@ -7,6 +7,14 @@ import { View, type ScrollViewInstance, } from "react-native" +import Animated, { + cancelAnimation, + Easing, + useAnimatedStyle, + useSharedValue, + withTiming, + type SharedValue, +} from "react-native-reanimated" import { usePlaybackActions, @@ -18,6 +26,7 @@ import type { NowPlayingColors } from "@/lib/now-playing-colors" import { activeLineIndex, type LyricLine, + type LyricWord, type Lyrics, } from "@otunes/convex/client/lyrics" @@ -25,6 +34,8 @@ import { const MANUAL_SCROLL_HOLD_MS = 3000 /** Where the playing line sits, as a share of the view's height from the top. */ const ACTIVE_LINE_POSITION = 0.12 +/** Blur radius by distance from the playing line; farther lines share the last. */ +const LINE_BLUR = [0, 1, 2, 3] /** * Lyrics in the player's own colors. Synced lines follow playback and seek @@ -70,19 +81,22 @@ function LyricsLines({ const scrollRef = useRef(null) const lineOffsets = useRef([]) const [height, setHeight] = useState(0) - const manualScrollAt = useRef(0) + // While the listener scrolls, lines stay sharp and the view stays put. + const [browsing, setBrowsing] = useState(false) + const browseTimer = useRef>(undefined) + + useEffect(() => () => clearTimeout(browseTimer.current), []) const scrollToLine = useCallback( (index: number) => { const y = lineOffsets.current[Math.max(index, 0)] - if (y === undefined || !synced || height === 0) return - if (Date.now() - manualScrollAt.current < MANUAL_SCROLL_HOLD_MS) return + if (y === undefined || !synced || height === 0 || browsing) return scrollRef.current?.scrollTo({ y: Math.max(0, y - height * ACTIVE_LINE_POSITION), animated: true, }) }, - [height, synced] + [height, synced, browsing] ) useEffect(() => { @@ -106,11 +120,20 @@ function LyricsLines({ (seconds: number) => { seekTo(seconds) setIsPlaying(true) - manualScrollAt.current = 0 + clearTimeout(browseTimer.current) + setBrowsing(false) }, [seekTo, setIsPlaying] ) + const releaseScroll = () => { + clearTimeout(browseTimer.current) + browseTimer.current = setTimeout( + () => setBrowsing(false), + MANUAL_SCROLL_HOLD_MS + ) + } + const lineStyle = useMemo( () => ({ fontSize, lineHeight: Math.round(fontSize * 1.18) }), [fontSize] @@ -127,12 +150,12 @@ function LyricsLines({ showsVerticalScrollIndicator={false} onLayout={(event) => setHeight(event.nativeEvent.layout.height)} onScrollBeginDrag={() => { - manualScrollAt.current = Date.now() - }} - onMomentumScrollEnd={() => { - // Hold from when the scroll settles, not when the finger lifted. - if (manualScrollAt.current > 0) manualScrollAt.current = Date.now() + clearTimeout(browseTimer.current) + setBrowsing(true) }} + // Hold from when the scroll settles, not when the finger lifted. + onScrollEndDrag={releaseScroll} + onMomentumScrollEnd={releaseScroll} > {lyrics.lines.map((line, index) => synced ? ( @@ -140,10 +163,14 @@ function LyricsLines({ key={index} index={index} line={line} - state={ - index === active ? "active" : index < active ? "past" : "next" + distance={ + browsing + ? 0 + : Math.min(Math.abs(index - active), LINE_BLUR.length - 1) } + active={index === active} time={index === active ? time : null} + isPlaying={isPlaying} colors={colors} textStyle={lineStyle} onSelect={playFrom} @@ -171,14 +198,17 @@ function LyricsLines({ } /** - * One timed line. Only the playing line gets the time, so the others skip - * re-rendering as the song plays. + * One timed line, blurred by its `distance` from the playing line. Only the + * playing line gets the time, so the others skip re-rendering as the song + * plays. */ const SyncedLine = memo(function SyncedLine({ index, line, - state, + distance, + active, time, + isPlaying, colors, textStyle, onSelect, @@ -186,58 +216,155 @@ const SyncedLine = memo(function SyncedLine({ }: { index: number line: LyricLine - state: "past" | "active" | "next" + distance: number + active: boolean time: number | null + isPlaying: boolean colors: NowPlayingColors textStyle: { fontSize: number; lineHeight: number } onSelect: (seconds: number) => void onLayoutY: (index: number, y: number) => void }) { - const active = state === "active" + const blur = LINE_BLUR[distance] return ( onSelect(line.start ?? 0)} onLayout={(event) => onLayoutY(index, event.nativeEvent.layout.y)} accessibilityRole="button" accessibilityHint="Plays from this line" - style={styles.line} + style={[styles.line, blur > 0 && { filter: [{ blur }] }]} > - - {active && line.words - ? line.words.map((word, wordIndex) => ( - = word.start - ? colors.foreground - : colors.lyricsUnsung, - }} - > - {word.text} - - )) - : line.text} - + {active && line.words && time !== null ? ( + + ) : ( + + {line.text} + + )} ) }) +/** + * The playing line's words, each wiping from dim to bright as it is sung. + * Reanimated runs the wipe; playback updates only re-anchor it. + */ +function FilledWords({ + words, + time, + isPlaying, + colors, + textStyle, +}: { + words: LyricWord[] + time: number + isPlaying: boolean + colors: NowPlayingColors + textStyle: { fontSize: number; lineHeight: number } +}) { + const sung = useSharedValue(time) + const end = words.at(-1)?.end ?? time + + useEffect(() => { + cancelAnimation(sung) + sung.value = time + if (isPlaying && end > time) { + sung.value = withTiming(end, { + duration: (end - time) * 1000, + easing: Easing.linear, + }) + } + }, [sung, time, isPlaying, end]) + + return ( + + {words.map((word, index) => ( + + ))} + + ) +} + +function FilledWord({ + word, + sung, + colors, + textStyle, +}: { + word: LyricWord + sung: SharedValue + colors: NowPlayingColors + textStyle: { fontSize: number; lineHeight: number } +}) { + const [width, setWidth] = useState(0) + const text = word.text.trim() + const wipe = useAnimatedStyle(() => { + const progress = + (sung.value - word.start) / Math.max(word.end - word.start, 0.05) + return { width: width * Math.min(Math.max(progress, 0), 1) } + }) + + return ( + + setWidth(event.nativeEvent.layout.width)} + > + {text} + + + + {text} + + + + ) +} + const styles = StyleSheet.create({ fill: { flex: 1 }, empty: { flex: 1, alignItems: "center", justifyContent: "center" }, emptyText: { fontSize: 17, fontWeight: "600" }, line: { paddingVertical: 9 }, lineText: { fontWeight: "800" }, + words: { flexDirection: "row", flexWrap: "wrap" }, + wipe: { + position: "absolute", + top: 0, + bottom: 0, + left: 0, + overflow: "hidden", + }, plainLine: { fontWeight: "700", paddingVertical: 2 }, stanzaGap: { height: 18 }, }) diff --git a/apps/web/components/lyrics-panel.tsx b/apps/web/components/lyrics-panel.tsx index a3b3520..7d63333 100644 --- a/apps/web/components/lyrics-panel.tsx +++ b/apps/web/components/lyrics-panel.tsx @@ -12,11 +12,26 @@ import { cn } from "@/lib/utils" import { activeLineIndex, type LyricLine, + type LyricWord, type Lyrics, } from "@otunes/convex/client/lyrics" /** How long a manual scroll keeps the panel from following the song. */ const MANUAL_SCROLL_HOLD_MS = 3000 +/** Blur by distance from the playing line; farther lines share the last. */ +const LINE_BLUR = ["blur-none", "blur-[1px]", "blur-[2px]", "blur-[3px]"] +/** Unsung words wipe from dim to bright, soft edge first, as they are sung. */ +const WORD_FILL = { + backgroundImage: + "linear-gradient(90deg, var(--foreground) 40%, color-mix(in oklab, var(--foreground) 45%, transparent) 60%)", + backgroundSize: "250% 100%", +} +const WORD_FILL_FRAMES = [ + { backgroundPosition: "100% 0" }, + { backgroundPosition: "0% 0" }, +] +/** How far a word's fill may drift from playback before it is reset. */ +const FILL_DRIFT_MS = 80 /** Lyrics beside the page, in the queue's place. Synced lines follow playback. */ export function LyricsPanel() { @@ -47,38 +62,42 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) { const time = useLyricsTime(lyrics, currentTime, isPlaying) const active = activeLineIndex(lyrics, time) const scrollRef = React.useRef(null) - const manualScrollAt = React.useRef(0) const synced = lyrics.synced !== "none" + // While the listener scrolls, lines stay sharp and the panel stays put. + const [browsing, setBrowsing] = React.useState(false) + const browseTimer = React.useRef>(undefined) + + React.useEffect(() => () => clearTimeout(browseTimer.current), []) React.useEffect(() => { const container = scrollRef.current const line = container?.querySelector( `[data-line="${Math.max(active, 0)}"]` ) - if ( - !container || - !line || - !synced || - Date.now() - manualScrollAt.current < MANUAL_SCROLL_HOLD_MS - ) { - return - } + if (!container || !line || !synced || browsing) return container.scrollTo({ top: line.offsetTop - container.clientHeight * 0.25, behavior: "smooth", }) - }, [active, synced]) + }, [active, synced, browsing]) const playFrom = React.useCallback( (seconds: number) => { seekTo(seconds) setIsPlaying(true) + clearTimeout(browseTimer.current) + setBrowsing(false) }, [seekTo, setIsPlaying] ) const holdScroll = () => { - manualScrollAt.current = Date.now() + setBrowsing(true) + clearTimeout(browseTimer.current) + browseTimer.current = setTimeout( + () => setBrowsing(false), + MANUAL_SCROLL_HOLD_MS + ) } return ( @@ -86,7 +105,7 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) { ref={scrollRef} onWheel={holdScroll} onTouchMove={holdScroll} - className="relative min-h-0 flex-1 overflow-y-auto px-6 pt-[25svh] pb-[60svh]" + className="group relative min-h-0 flex-1 overflow-y-auto px-6 pt-[25svh] pb-[60svh]" > {lyrics.lines.map((line, index) => synced ? ( @@ -94,10 +113,14 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) { key={index} index={index} line={line} - state={ - index === active ? "active" : index < active ? "past" : "next" + distance={ + browsing + ? 0 + : Math.min(Math.abs(index - active), LINE_BLUR.length - 1) } + active={index === active} time={index === active ? time : null} + isPlaying={isPlaying} onSelect={playFrom} /> ) : ( @@ -117,48 +140,103 @@ function LyricsLines({ lyrics }: { lyrics: Lyrics }) { } /** - * One timed line. Only the active line gets the playback time, so memoized - * lines elsewhere skip re-rendering as the song plays. + * One timed line, blurred by its `distance` from the playing line. Only the + * active line gets the playback time, so memoized lines elsewhere skip + * re-rendering as the song plays. */ const SyncedLine = React.memo(function SyncedLine({ index, line, - state, + distance, + active, time, + isPlaying, onSelect, }: { index: number line: LyricLine - state: "past" | "active" | "next" + distance: number + active: boolean time: number | null + isPlaying: boolean onSelect: (seconds: number) => void }) { - const active = state === "active" return ( ) }) + +/** + * The playing line's words, each filled as far as it has been sung. The + * browser runs the fills; playback updates only correct their drift. + */ +function FilledWords({ + words, + time, + isPlaying, +}: { + words: LyricWord[] + time: number + isPlaying: boolean +}) { + const spans = React.useRef<(HTMLSpanElement | null)[]>([]) + const fills = React.useRef<(Animation | undefined)[]>([]) + + React.useEffect(() => { + fills.current = words.map((word, index) => + spans.current[index]?.animate(WORD_FILL_FRAMES, { + duration: Math.max(word.end - word.start, 0.05) * 1000, + easing: "linear", + fill: "both", + }) + ) + return () => fills.current.forEach((fill) => fill?.cancel()) + }, [words]) + + React.useEffect(() => { + fills.current.forEach((fill, index) => { + if (!fill) return + const at = (time - words[index].start) * 1000 + if (Math.abs(Number(fill.currentTime ?? 0) - at) > FILL_DRIFT_MS) { + fill.currentTime = at + } + // `play` on a finished fill would rewind it. + const duration = Number(fill.effect?.getTiming().duration ?? 0) + if (isPlaying && at < duration) fill.play() + else fill.pause() + }) + }, [words, time, isPlaying]) + + return words.map((word, index) => { + const text = word.text.trimEnd() + return ( + + { + spans.current[index] = span + }} + className="bg-clip-text text-transparent" + style={WORD_FILL} + > + {text} + + {word.text.slice(text.length)} + + ) + }) +}