The mobile fill restarted on every progress update and word boundary, so it hitched; it had a hard edge, re-wrapped the playing line, and line changes snapped. A UI-thread clock now drives fills and resets only on drift, words fill through a gradient mask like web, lines always lay out word by word, and opacity and blur fade between lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
4.5 KiB
TypeScript
159 lines
4.5 KiB
TypeScript
import { File } from "expo-file-system"
|
|
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 type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import {
|
|
followLyrics,
|
|
parseLyrics,
|
|
type Lyrics,
|
|
} from "@otunes/convex/client/lyrics"
|
|
import { readLyricsTag } from "@otunes/convex/client/lyrics-tags"
|
|
import { useDownloads } from "@/contexts/downloads"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
|
import { downloadedAudioUri } from "@/lib/downloads"
|
|
|
|
// Lyrics read from downloaded files, by file. Files do not change once saved.
|
|
const localLyrics = new Map<string, Promise<string | null>>()
|
|
|
|
async function readFileLyrics(uri: string) {
|
|
const handle = new File(uri).open()
|
|
try {
|
|
const text = await readLyricsTag({
|
|
size: handle.size ?? 0,
|
|
read: (offset, length) => {
|
|
handle.offset = offset
|
|
return handle.readBytes(length)
|
|
},
|
|
})
|
|
return text?.trim() || null
|
|
} finally {
|
|
handle.close()
|
|
}
|
|
}
|
|
|
|
function readLocalLyrics(uri: string) {
|
|
let lyrics = localLyrics.get(uri)
|
|
if (!lyrics) {
|
|
lyrics = readFileLyrics(uri).catch(() => null)
|
|
localLyrics.set(uri, lyrics)
|
|
}
|
|
return lyrics
|
|
}
|
|
|
|
const noopSubscribe = () => () => {}
|
|
|
|
/**
|
|
* A song's lyrics: from its downloaded file when there is one, otherwise from
|
|
* the server. `undefined` while loading, `null` when the song has none.
|
|
*/
|
|
export function useLyrics(songId: Id<"songs"> | null) {
|
|
const { config } = useServerConfig()
|
|
const { queue } = useDownloads()
|
|
const fileUri = useSyncExternalStore(queue?.subscribe ?? noopSubscribe, () =>
|
|
songId && config && queue?.getDownloaded(songId)
|
|
? downloadedAudioUri(config, songId)
|
|
: null
|
|
)
|
|
const [local, setLocal] = useState<{ uri: string; text: string | null }>()
|
|
|
|
useEffect(() => {
|
|
if (!fileUri) return
|
|
let current = true
|
|
void readLocalLyrics(fileUri).then((text) => {
|
|
if (current) setLocal({ uri: fileUri, text })
|
|
})
|
|
return () => {
|
|
current = false
|
|
}
|
|
}, [fileUri])
|
|
|
|
const localText = fileUri && local?.uri === fileUri ? local.text : undefined
|
|
// Songs without a downloaded copy, or whose copy has no lyrics tag.
|
|
const remote = useCachedQuery(
|
|
api.lyrics.forSong,
|
|
songId && (!fileUri || localText === null) ? { id: songId } : "skip"
|
|
)
|
|
const text = localText || remote
|
|
|
|
return useMemo<Lyrics | null | undefined>(() => {
|
|
if (!songId) return null
|
|
if (text === undefined) return undefined
|
|
return text ? parseLyrics(text) : null
|
|
}, [songId, text])
|
|
}
|
|
|
|
/**
|
|
* The playback time for highlighting lyrics: the player's `currentTime`,
|
|
* advanced by `followLyrics` between its progress updates.
|
|
*/
|
|
export function useLyricsTime(
|
|
lyrics: Lyrics | null | undefined,
|
|
currentTime: number,
|
|
isPlaying: boolean
|
|
) {
|
|
// Time reached since the player last reported `from`.
|
|
const [advanced, setAdvanced] = useState<{ from: number; time: number }>()
|
|
|
|
useEffect(() => {
|
|
if (!lyrics || !isPlaying) return
|
|
return followLyrics(lyrics, currentTime, (time) =>
|
|
setAdvanced({ from: currentTime, time })
|
|
)
|
|
}, [lyrics, currentTime, isPlaying])
|
|
|
|
return isPlaying && advanced?.from === currentTime
|
|
? advanced.time
|
|
: 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
|
|
}
|