feat: blur inactive lyrics and fill words as they are sung
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
96feb89b9d
commit
356c79932f
2 files changed
+284
-79
No files matched your search
@@ -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<ScrollViewInstance>(null)
|
||||
const lineOffsets = useRef<number[]>([])
|
||||
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<ReturnType<typeof setTimeout>>(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 (
|
||||
<Pressable
|
||||
onPress={() => 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 }] }]}
|
||||
>
|
||||
<Text
|
||||
style={[
|
||||
styles.lineText,
|
||||
textStyle,
|
||||
{
|
||||
color:
|
||||
active && !line.words ? colors.foreground : colors.lyricsInactive,
|
||||
},
|
||||
]}
|
||||
>
|
||||
{active && line.words
|
||||
? line.words.map((word, wordIndex) => (
|
||||
<Text
|
||||
key={wordIndex}
|
||||
style={{
|
||||
color:
|
||||
time !== null && time >= word.start
|
||||
? colors.foreground
|
||||
: colors.lyricsUnsung,
|
||||
}}
|
||||
>
|
||||
{word.text}
|
||||
</Text>
|
||||
))
|
||||
: line.text}
|
||||
</Text>
|
||||
{active && line.words && time !== null ? (
|
||||
<FilledWords
|
||||
words={line.words}
|
||||
time={time}
|
||||
isPlaying={isPlaying}
|
||||
colors={colors}
|
||||
textStyle={textStyle}
|
||||
/>
|
||||
) : (
|
||||
<Text
|
||||
style={[
|
||||
styles.lineText,
|
||||
textStyle,
|
||||
{
|
||||
color:
|
||||
active && !line.words
|
||||
? colors.foreground
|
||||
: colors.lyricsInactive,
|
||||
},
|
||||
]}
|
||||
>
|
||||
{line.text}
|
||||
</Text>
|
||||
)}
|
||||
</Pressable>
|
||||
)
|
||||
})
|
||||
|
||||
/**
|
||||
* 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 (
|
||||
<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({
|
||||
word,
|
||||
sung,
|
||||
colors,
|
||||
textStyle,
|
||||
}: {
|
||||
word: LyricWord
|
||||
sung: SharedValue<number>
|
||||
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 (
|
||||
<View>
|
||||
<Text
|
||||
style={[styles.lineText, textStyle, { color: colors.lyricsUnsung }]}
|
||||
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
|
||||
>
|
||||
{text}
|
||||
</Text>
|
||||
<Animated.View style={[styles.wipe, wipe]}>
|
||||
<Text
|
||||
numberOfLines={1}
|
||||
style={[
|
||||
styles.lineText,
|
||||
textStyle,
|
||||
{ color: colors.foreground, width },
|
||||
]}
|
||||
>
|
||||
{text}
|
||||
</Text>
|
||||
</Animated.View>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
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 },
|
||||
})
|
||||
@@ -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<HTMLDivElement>(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<ReturnType<typeof setTimeout>>(undefined)
|
||||
|
||||
React.useEffect(() => () => clearTimeout(browseTimer.current), [])
|
||||
|
||||
React.useEffect(() => {
|
||||
const container = scrollRef.current
|
||||
const line = container?.querySelector<HTMLElement>(
|
||||
`[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 (
|
||||
<button
|
||||
type="button"
|
||||
data-line={index}
|
||||
onClick={() => onSelect(line.start ?? 0)}
|
||||
className={cn(
|
||||
"-mx-2 my-1 block w-[calc(100%+1rem)] rounded-md px-2 py-1.5 text-left text-[22px] leading-tight font-bold transition-colors duration-300 hover:bg-foreground/5",
|
||||
"-mx-2 my-1 block w-[calc(100%+1rem)] rounded-md px-2 py-1.5 text-left text-[22px] leading-tight font-bold transition-[color,filter] duration-300 group-hover:blur-none hover:bg-foreground/5",
|
||||
LINE_BLUR[distance],
|
||||
active && !line.words ? "text-foreground" : "text-foreground/30"
|
||||
)}
|
||||
>
|
||||
{active && line.words
|
||||
? line.words.map((word, wordIndex) => (
|
||||
<span
|
||||
key={wordIndex}
|
||||
className={cn(
|
||||
"transition-colors duration-200",
|
||||
time !== null && time >= word.start
|
||||
? "text-foreground"
|
||||
: "text-foreground/45"
|
||||
)}
|
||||
>
|
||||
{word.text}
|
||||
</span>
|
||||
))
|
||||
: line.text}
|
||||
{active && line.words && time !== null ? (
|
||||
<FilledWords words={line.words} time={time} isPlaying={isPlaying} />
|
||||
) : (
|
||||
line.text
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
})
|
||||
|
||||
/**
|
||||
* 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 (
|
||||
<React.Fragment key={index}>
|
||||
<span
|
||||
ref={(span) => {
|
||||
spans.current[index] = span
|
||||
}}
|
||||
className="bg-clip-text text-transparent"
|
||||
style={WORD_FILL}
|
||||
>
|
||||
{text}
|
||||
</span>
|
||||
{word.text.slice(text.length)}
|
||||
</React.Fragment>
|
||||
)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user