fix(mobile): fill lyrics as smoothly as web (#148)

This commit was merged in pull request #148.
This commit is contained in:
joshuafhiggins committed 2026-11-20 16:26:09 -05:00
1 parent d8c1364057
commit 75f8c63911
5 files changed
+139 -113

No files matched your search

+85 -109
View File
@@ -1,4 +1,13 @@
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react" import {
memo,
type ComponentType,
type ReactElement,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react"
import { import {
Pressable, Pressable,
ScrollView, ScrollView,
@@ -6,14 +15,11 @@ import {
Text, Text,
View, View,
type ScrollViewInstance, type ScrollViewInstance,
type ViewProps,
} from "react-native" } from "react-native"
import MaskedViewClass from "@react-native-masked-view/masked-view"
import Animated, { import Animated, {
cancelAnimation,
Easing,
useAnimatedStyle, useAnimatedStyle,
useSharedValue,
withSequence,
withTiming,
type SharedValue, type SharedValue,
} from "react-native-reanimated" } from "react-native-reanimated"
@@ -22,7 +28,7 @@ import {
usePlaybackProgress, usePlaybackProgress,
usePlaybackQueue, usePlaybackQueue,
} from "@/contexts/playback" } from "@/contexts/playback"
import { useLyricsTime } from "@/hooks/use-lyrics" import { useLyricsTime, usePlaybackClock } from "@/hooks/use-lyrics"
import type { NowPlayingColors } from "@/lib/now-playing-colors" import type { NowPlayingColors } from "@/lib/now-playing-colors"
import { import {
activeLineIndex, activeLineIndex,
@@ -31,12 +37,21 @@ import {
type Lyrics, type Lyrics,
} from "@otunes/convex/client/lyrics" } from "@otunes/convex/client/lyrics"
// The package's class typings predate React 19's; this is its documented API.
const MaskedView = MaskedViewClass as unknown as ComponentType<
ViewProps & { maskElement: ReactElement }
>
/** How long a manual scroll keeps the view from following the song. */ /** How long a manual scroll keeps the view from following the song. */
const MANUAL_SCROLL_HOLD_MS = 3000 const MANUAL_SCROLL_HOLD_MS = 3000
/** Where the playing line sits, as a share of the view's height from the top. */ /** Where the playing line sits, as a share of the view's height from the top. */
const ACTIVE_LINE_POSITION = 0.12 const ACTIVE_LINE_POSITION = 0.12
/** Blur radius by distance from the playing line; farther lines share the last. */ /** Blur radius by distance from the playing line; farther lines share the last. */
const LINE_BLUR = [0, 1, 2, 3] const LINE_BLUR = [0, 1, 2, 3]
/** Opacity of lines other than the one playing. */
const INACTIVE_LINE_OPACITY = 0.35
/** How long a line takes to brighten, dim, or change blur. */
const LINE_FADE_MS = 300
/** /**
* Lyrics in the player's own colors. Synced lines follow playback and seek * Lyrics in the player's own colors. Synced lines follow playback and seek
@@ -77,6 +92,7 @@ function LyricsLines({
const { isPlaying } = usePlaybackQueue() const { isPlaying } = usePlaybackQueue()
const { seekTo, setIsPlaying } = usePlaybackActions() const { seekTo, setIsPlaying } = usePlaybackActions()
const time = useLyricsTime(lyrics, currentTime, isPlaying) const time = useLyricsTime(lyrics, currentTime, isPlaying)
const clock = usePlaybackClock(currentTime, isPlaying)
const active = activeLineIndex(lyrics, time) const active = activeLineIndex(lyrics, time)
const synced = lyrics.synced !== "none" const synced = lyrics.synced !== "none"
const scrollRef = useRef<ScrollViewInstance>(null) const scrollRef = useRef<ScrollViewInstance>(null)
@@ -170,8 +186,7 @@ function LyricsLines({
: Math.min(Math.abs(index - active), LINE_BLUR.length - 1) : Math.min(Math.abs(index - active), LINE_BLUR.length - 1)
} }
active={index === active} active={index === active}
time={index === active ? time : null} clock={clock}
isPlaying={isPlaying}
colors={colors} colors={colors}
textStyle={lineStyle} textStyle={lineStyle}
onSelect={playFrom} onSelect={playFrom}
@@ -199,17 +214,16 @@ function LyricsLines({
} }
/** /**
* One timed line, blurred by its `distance` from the playing line. Only the * One timed line, laid out word by word whether or not it is playing, so it
* playing line gets the time, so the others skip re-rendering as the song * never re-wraps. It fades and blurs by its `distance` from the playing line,
* plays. * and only the playing line's words fill.
*/ */
const SyncedLine = memo(function SyncedLine({ const SyncedLine = memo(function SyncedLine({
index, index,
line, line,
distance, distance,
active, active,
time, clock,
isPlaying,
colors, colors,
textStyle, textStyle,
onSelect, onSelect,
@@ -219,140 +233,100 @@ const SyncedLine = memo(function SyncedLine({
line: LyricLine line: LyricLine
distance: number distance: number
active: boolean active: boolean
time: number | null clock: SharedValue<number>
isPlaying: boolean
colors: NowPlayingColors colors: NowPlayingColors
textStyle: { fontSize: number; lineHeight: number } textStyle: { fontSize: number; lineHeight: number }
onSelect: (seconds: number) => void onSelect: (seconds: number) => void
onLayoutY: (index: number, y: number) => void onLayoutY: (index: number, y: number) => void
}) { }) {
const blur = LINE_BLUR[distance] const wordStyle = [styles.lineText, textStyle, { color: colors.foreground }]
return ( return (
<Pressable <Pressable
onPress={() => onSelect(line.start ?? 0)} onPress={() => onSelect(line.start ?? 0)}
onLayout={(event) => onLayoutY(index, event.nativeEvent.layout.y)} onLayout={(event) => onLayoutY(index, event.nativeEvent.layout.y)}
accessibilityRole="button" accessibilityRole="button"
accessibilityHint="Plays from this line" accessibilityHint="Plays from this line"
style={[styles.line, blur > 0 && { filter: [{ blur }] }]} style={styles.line}
> >
{active && line.words && time !== null ? ( <Animated.View
<FilledWords style={{
words={line.words} opacity: active ? 1 : INACTIVE_LINE_OPACITY,
time={time} filter: [{ blur: LINE_BLUR[distance] }],
isPlaying={isPlaying} transitionProperty: ["opacity", "filter"],
transitionDuration: LINE_FADE_MS,
}}
>
{line.words ? (
<View
style={[styles.words, { columnGap: textStyle.fontSize * 0.25 }]}
>
{line.words.map((word, wordIndex) =>
active ? (
<FilledWord
key={wordIndex}
word={word}
clock={clock}
colors={colors} colors={colors}
textStyle={textStyle} textStyle={textStyle}
/> />
) : ( ) : (
<Text <Text key={wordIndex} style={wordStyle}>
style={[ {word.text.trim()}
styles.lineText,
textStyle,
{
color:
active && !line.words
? colors.foreground
: colors.lyricsInactive,
},
]}
>
{line.text}
</Text> </Text>
)
)} )}
</View>
) : (
<Text style={wordStyle}>{line.text}</Text>
)}
</Animated.View>
</Pressable> </Pressable>
) )
}) })
/** /**
* The playing line's words, each wiping from dim to bright as it is sung. * A word of the playing line, filled from dim to bright with a soft edge as
* Reanimated runs the wipe; playback updates only re-anchor it. * it is sung: a gradient 2.5 times its width slides under the word's
* shape, like the web panel's.
*/ */
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)
// Jump to the reported time first, so the wipe never runs from a stale one.
sung.value =
isPlaying && end > time
? withSequence(
withTiming(time, { duration: 0 }),
withTiming(end, {
duration: (end - time) * 1000,
easing: Easing.linear,
})
)
: time
}, [sung, time, isPlaying, end])
return (
<View style={[styles.words, { columnGap: textStyle.fontSize * 0.25 }]}>
{words.map((word, index) => (
<FilledWord
key={index}
word={word}
sung={sung}
colors={colors}
textStyle={textStyle}
/>
))}
</View>
)
}
function FilledWord({ function FilledWord({
word, word,
sung, clock,
colors, colors,
textStyle, textStyle,
}: { }: {
word: LyricWord word: LyricWord
sung: SharedValue<number> clock: SharedValue<number>
colors: NowPlayingColors colors: NowPlayingColors
textStyle: { fontSize: number; lineHeight: number } textStyle: { fontSize: number; lineHeight: number }
}) { }) {
const [width, setWidth] = useState(0)
const text = word.text.trim() const text = word.text.trim()
const wipe = useAnimatedStyle(() => { const slide = useAnimatedStyle(() => {
const progress = const progress =
(sung.value - word.start) / Math.max(word.end - word.start, 0.05) (clock.value - word.start) / Math.max(word.end - word.start, 0.05)
return { width: width * Math.min(Math.max(progress, 0), 1) } const sung = Math.min(Math.max(progress, 0), 1)
// -60% of the gradient's width shows its dim end; 0% its bright one.
return { transform: [{ translateX: `${-60 * (1 - sung)}%` }] }
}) })
return ( return (
<View> <MaskedView
<Text maskElement={
style={[styles.lineText, textStyle, { color: colors.lyricsUnsung }]} <Text style={[styles.lineText, textStyle, styles.mask]}>{text}</Text>
onLayout={(event) => setWidth(event.nativeEvent.layout.width)} }
> >
{text} {/* Sizes the word; the mask draws it. */}
</Text> <Text style={[styles.lineText, textStyle, styles.hidden]}>{text}</Text>
<Animated.View style={[styles.wipe, wipe]}> <Animated.View
<Text
numberOfLines={1}
style={[ style={[
styles.lineText, styles.fillGradient,
textStyle, {
{ color: colors.foreground, width }, backgroundImage: `linear-gradient(90deg, ${colors.foreground} 40%, ${colors.lyricsUnsung} 60%)`,
},
slide,
]} ]}
> />
{text} </MaskedView>
</Text>
</Animated.View>
</View>
) )
} }
@@ -363,12 +337,14 @@ const styles = StyleSheet.create({
line: { paddingVertical: 9 }, line: { paddingVertical: 9 },
lineText: { fontWeight: "800" }, lineText: { fontWeight: "800" },
words: { flexDirection: "row", flexWrap: "wrap" }, words: { flexDirection: "row", flexWrap: "wrap" },
wipe: { mask: { color: "black" },
hidden: { opacity: 0 },
fillGradient: {
position: "absolute", position: "absolute",
top: 0, top: 0,
bottom: 0, bottom: 0,
left: 0, left: 0,
overflow: "hidden", width: "250%",
}, },
plainLine: { fontWeight: "700", paddingVertical: 2 }, plainLine: { fontWeight: "700", paddingVertical: 2 },
stanzaGap: { height: 18 }, stanzaGap: { height: 18 },
+50 -1
View File
@@ -1,5 +1,18 @@
import { File } from "expo-file-system" import { File } from "expo-file-system"
import { useEffect, useMemo, useState, useSyncExternalStore } from "react" import {
useEffect,
useMemo,
useRef,
useState,
useSyncExternalStore,
} from "react"
import {
cancelAnimation,
Easing,
useSharedValue,
withSequence,
withTiming,
} from "react-native-reanimated"
import { api } from "@otunes/convex/_generated/api" import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel" import type { Id } from "@otunes/convex/_generated/dataModel"
@@ -107,3 +120,39 @@ export function useLyricsTime(
? advanced.time ? advanced.time
: currentTime : currentTime
} }
/** How far the clock may drift from the player before it is reset. */
const CLOCK_DRIFT_SECONDS = 0.15
/** How far ahead the clock runs on its own; progress updates come long before. */
const CLOCK_RUN_SECONDS = 3600
/**
* Playback time in seconds on the UI thread, running smoothly between the
* player's progress updates. It is reset only when it drifts, so animations
* driven by it never restart mid-word.
*/
export function usePlaybackClock(currentTime: number, isPlaying: boolean) {
const clock = useSharedValue(currentTime)
const running = useRef(false)
useEffect(() => {
if (!isPlaying) {
running.current = false
cancelAnimation(clock)
clock.value = currentTime
return
}
const drift = Math.abs(clock.value - currentTime)
if (running.current && drift < CLOCK_DRIFT_SECONDS) return
running.current = true
clock.value = withSequence(
withTiming(currentTime, { duration: 0 }),
withTiming(currentTime + CLOCK_RUN_SECONDS, {
duration: CLOCK_RUN_SECONDS * 1000,
easing: Easing.linear,
})
)
}, [clock, currentTime, isPlaying])
return clock
}
-3
View File
@@ -12,8 +12,6 @@ export type NowPlayingColors = {
selected: string selected: string
selectedForeground: string selectedForeground: string
highlight: string highlight: string
/** Lyrics lines other than the one playing. */
lyricsInactive: string
/** Words of the playing line not yet sung. */ /** Words of the playing line not yet sung. */
lyricsUnsung: string lyricsUnsung: string
isDark: boolean isDark: boolean
@@ -32,7 +30,6 @@ export function nowPlayingColors(palette: CoverPalette): NowPlayingColors {
selected: withAlpha(foreground, 0.8), selected: withAlpha(foreground, 0.8),
selectedForeground: palette.background, selectedForeground: palette.background,
highlight: withAlpha(foreground, 0.1), highlight: withAlpha(foreground, 0.1),
lyricsInactive: withAlpha(foreground, 0.35),
lyricsUnsung: withAlpha(foreground, 0.55), lyricsUnsung: withAlpha(foreground, 0.55),
isDark: palette.isDark, isDark: palette.isDark,
} }
+1
View File
@@ -20,6 +20,7 @@
"@otunes/convex": "workspace:*", "@otunes/convex": "workspace:*",
"@react-native-async-storage/async-storage": "2.2.0", "@react-native-async-storage/async-storage": "2.2.0",
"@react-native-community/slider": "5.2.1", "@react-native-community/slider": "5.2.1",
"@react-native-masked-view/masked-view": "0.3.2",
"@react-native/metro-config": "0.88.0", "@react-native/metro-config": "0.88.0",
"convex": "^1.40.0", "convex": "^1.40.0",
"expo": "~58.0.17", "expo": "~58.0.17",
+3
View File
@@ -279,6 +279,9 @@ importers:
'@react-native-community/slider': '@react-native-community/slider':
specifier: 5.2.1 specifier: 5.2.1
version: 5.2.1(patch_hash=73deacaea9aae000a42050d343517da9e16e736e92a4fff3f61f81dba093a1c4) version: 5.2.1(patch_hash=73deacaea9aae000a42050d343517da9e16e736e92a4fff3f61f81dba093a1c4)
'@react-native-masked-view/masked-view':
specifier: 0.3.2
version: 0.3.2(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.3.0)(react@19.3.0)(supports-color@8.1.1))(react@19.3.0)
'@react-native/metro-config': '@react-native/metro-config':
specifier: 0.88.0 specifier: 0.88.0
version: 0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1) version: 0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1)