Skip to main content

koan_core/
lyrics.rs

1//! Lyrics for a track: the database cache, then LRCLIB, cached back to the
2//! database. Embedded tags and sidecar `.lrc` files are not read.
3use rusqlite::Connection;
4
5use crate::db::connection::DbError;
6use crate::db::queries::lyrics::{cache_lyrics, get_cached_lyrics, lyrics_fetched_at};
7use crate::remote::lrclib::{self, LrclibError};
8
9// ---------------------------------------------------------------------------
10// Public types
11// ---------------------------------------------------------------------------
12
13/// The resolved lyrics for a track.
14#[derive(Debug, Clone)]
15pub struct Lyrics {
16    /// Raw lyrics text. LRC format if `synced` is true, plain text otherwise.
17    pub content: String,
18    /// Whether `content` is in LRC (time-tagged) format.
19    pub synced: bool,
20    /// Which source provided the lyrics.
21    pub source: LyricsSource,
22}
23
24/// Which source provided the lyrics.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub enum LyricsSource {
27    /// Embedded in the audio file tags (e.g. USLT ID3, Vorbis LYRICS).
28    Embedded,
29    /// Sidecar `.lrc` file next to the audio file.
30    Sidecar,
31    /// LRCLIB community lyrics database.
32    Lrclib,
33    /// DB cache hit (originally from any of the above sources).
34    Cache,
35}
36
37impl LyricsSource {
38    fn as_str(&self) -> &'static str {
39        match self {
40            LyricsSource::Embedded => "embedded",
41            LyricsSource::Sidecar => "sidecar",
42            LyricsSource::Lrclib => "lrclib",
43            LyricsSource::Cache => "cache",
44        }
45    }
46}
47
48/// Errors that can occur during lyrics fetching.
49#[derive(Debug, thiserror::Error)]
50pub enum LyricsError {
51    #[error("database error: {0}")]
52    Db(#[from] DbError),
53    #[error("lrclib error: {0}")]
54    Lrclib(#[from] LrclibError),
55    #[error("lyrics not found")]
56    NotFound,
57}
58
59// ---------------------------------------------------------------------------
60// LRC parsing
61// ---------------------------------------------------------------------------
62
63/// A single time-tagged line from an LRC file.
64#[derive(Debug, Clone)]
65pub struct LrcLine {
66    /// Timestamp in seconds (e.g. 12.0 for `[00:12.00]`).
67    pub time_secs: f64,
68    /// The lyric text for this timestamp.
69    pub text: String,
70}
71
72/// Parse LRC-format text into a sorted list of [`LrcLine`]s.
73///
74/// Lines without a valid timestamp are silently skipped. The result is sorted
75/// by `time_secs` ascending so binary search works correctly.
76pub fn parse_lrc(content: &str) -> Vec<LrcLine> {
77    let mut lines: Vec<LrcLine> = content
78        .lines()
79        .filter_map(|line| {
80            // LRC timestamp: `[mm:ss.xx]` or `[mm:ss.xxx]`
81            let line = line.trim();
82            if !line.starts_with('[') {
83                return None;
84            }
85            let close = line.find(']')?;
86            let tag = &line[1..close];
87            let text = line[close + 1..].trim().to_string();
88
89            // Parse mm:ss.xx
90            let colon = tag.find(':')?;
91            let mins: f64 = tag[..colon].parse().ok()?;
92            let secs: f64 = tag[colon + 1..].parse().ok()?;
93            let time_secs = mins * 60.0 + secs;
94
95            Some(LrcLine { time_secs, text })
96        })
97        .collect();
98
99    lines.sort_by(|a, b| {
100        a.time_secs
101            .partial_cmp(&b.time_secs)
102            .unwrap_or(std::cmp::Ordering::Equal)
103    });
104    lines
105}
106
107/// Return the index of the current lyric line for the given playback position.
108///
109/// Returns `None` if the position is before the first timestamped line.
110/// Uses binary search for O(log n) lookup.
111pub fn current_line_index(lines: &[LrcLine], position_secs: f64) -> Option<usize> {
112    if lines.is_empty() {
113        return None;
114    }
115    // Find the last line whose timestamp <= position_secs.
116    match lines.binary_search_by(|l| {
117        l.time_secs
118            .partial_cmp(&position_secs)
119            .unwrap_or(std::cmp::Ordering::Less)
120    }) {
121        Ok(i) => Some(i),
122        Err(0) => None,        // before first line
123        Err(i) => Some(i - 1), // i is the insertion point; i-1 is the active line
124    }
125}
126
127// ---------------------------------------------------------------------------
128// Fetch pipeline
129// ---------------------------------------------------------------------------
130
131/// How long a plain (unsynced) cached copy is trusted before LRCLIB is asked
132/// again. Synced lyrics are often added upstream after plain ones, and a plain
133/// copy held forever would never highlight.
134const PLAIN_RECHECK_SECS: i64 = 30 * 24 * 60 * 60;
135
136/// Fetch lyrics for a track: from the DB cache (instant, no network), else from
137/// LRCLIB. A synced cached copy is final; a plain one is re-checked against
138/// LRCLIB once it is older than [`PLAIN_RECHECK_SECS`].
139///
140/// On a successful LRCLIB fetch the result is written to the DB cache so the
141/// next call is instant.
142pub fn fetch_lyrics(
143    conn: &Connection,
144    track_id: i64,
145    artist: &str,
146    title: &str,
147    album: &str,
148    duration_secs: u64,
149) -> Result<Lyrics, LyricsError> {
150    match look_up_cached(conn, track_id)? {
151        CacheLookup::Fresh(lyrics) => Ok(lyrics),
152        CacheLookup::Stale(cached) => {
153            let fetched = fetch_from_lrclib(artist, title, album, duration_secs);
154            settle(conn, track_id, fetched, cached)
155        }
156    }
157}
158
159/// What the cache answers for a track.
160///
161/// [`fetch_lyrics`] in the halves either side of the network, for a caller
162/// that holds its connection from a bounded pool and should give it back while
163/// LRCLIB answers: [`look_up_cached`], then [`fetch_from_lrclib`] with no
164/// connection, then [`settle`].
165pub enum CacheLookup {
166    /// Served as it is.
167    Fresh(Lyrics),
168    /// Ask LRCLIB, and fall back to this — content and whether it is synced —
169    /// if it has nothing better.
170    Stale(Option<(String, bool)>),
171}
172
173pub fn look_up_cached(conn: &Connection, track_id: i64) -> Result<CacheLookup, LyricsError> {
174    let cached = get_cached_lyrics(conn, track_id)?;
175    if let Some((content, synced)) = &cached {
176        let stale = !synced
177            && lyrics_fetched_at(conn, track_id)?
178                .is_none_or(|at| now_secs() - at >= PLAIN_RECHECK_SECS);
179        if !stale {
180            return Ok(CacheLookup::Fresh(Lyrics {
181                content: content.clone(),
182                synced: *synced,
183                source: LyricsSource::Cache,
184            }));
185        }
186    }
187    Ok(CacheLookup::Stale(cached))
188}
189
190/// Cache what LRCLIB said, or keep the stale copy when it said nothing.
191pub fn settle(
192    conn: &Connection,
193    track_id: i64,
194    fetched: Result<Lyrics, LyricsError>,
195    cached: Option<(String, bool)>,
196) -> Result<Lyrics, LyricsError> {
197    match (fetched, cached) {
198        (Ok(lyrics), _) => {
199            cache_lyrics(
200                conn,
201                track_id,
202                LyricsSource::Lrclib.as_str(),
203                lyrics.synced,
204                &lyrics.content,
205            )?;
206            Ok(lyrics)
207        }
208        // No synced copy upstream, or LRCLIB unreachable: keep the plain one,
209        // and restart its clock so the next play does not ask again.
210        (Err(_), Some((content, synced))) => {
211            cache_lyrics(
212                conn,
213                track_id,
214                LyricsSource::Lrclib.as_str(),
215                synced,
216                &content,
217            )?;
218            Ok(Lyrics {
219                content,
220                synced,
221                source: LyricsSource::Cache,
222            })
223        }
224        (Err(e), None) => Err(e),
225    }
226}
227
228fn now_secs() -> i64 {
229    std::time::SystemTime::now()
230        .duration_since(std::time::UNIX_EPOCH)
231        .map_or(0, |d| d.as_secs() as i64)
232}
233
234/// Ask LRCLIB. Synced lyrics are preferred, plain ones taken otherwise.
235pub fn fetch_from_lrclib(
236    artist: &str,
237    title: &str,
238    album: &str,
239    duration_secs: u64,
240) -> Result<Lyrics, LyricsError> {
241    let response =
242        lrclib::get_lyrics(artist, title, album, duration_secs).map_err(|e| match e {
243            LrclibError::NotFound => LyricsError::NotFound,
244            other => LyricsError::Lrclib(other),
245        })?;
246
247    let (content, synced) = if let Some(synced_lyrics) = response.synced_lyrics {
248        (synced_lyrics, true)
249    } else if let Some(plain_lyrics) = response.plain_lyrics {
250        (plain_lyrics, false)
251    } else {
252        return Err(LyricsError::NotFound);
253    };
254
255    Ok(Lyrics {
256        content,
257        synced,
258        source: LyricsSource::Lrclib,
259    })
260}
261
262// ---------------------------------------------------------------------------
263// Tests
264// ---------------------------------------------------------------------------
265
266#[cfg(test)]
267mod tests {
268    use super::*;
269
270    fn db_with_track() -> (rusqlite::Connection, i64) {
271        let conn = rusqlite::Connection::open_in_memory().unwrap();
272        crate::db::schema::create_tables(&conn).unwrap();
273        let meta = crate::db::queries::sample_meta("Windowlicker", "Aphex Twin", "Windowlicker EP");
274        let id = crate::db::queries::tracks::upsert_track(&conn, &meta).unwrap();
275        (conn, id)
276    }
277
278    /// A recent plain copy is answered from the cache, with no network: this
279    /// test would fail against an unreachable LRCLIB if it asked.
280    #[test]
281    fn a_fresh_plain_copy_is_served_from_cache() {
282        let (conn, id) = db_with_track();
283        cache_lyrics(&conn, id, "lrclib", false, "plain").unwrap();
284        let got = fetch_lyrics(
285            &conn,
286            id,
287            "Aphex Twin",
288            "Windowlicker",
289            "Windowlicker EP",
290            0,
291        )
292        .unwrap();
293        assert!(!got.synced);
294        assert_eq!(got.content, "plain");
295    }
296
297    #[test]
298    fn a_synced_copy_is_final_however_old() {
299        let (conn, id) = db_with_track();
300        cache_lyrics(&conn, id, "lrclib", true, "[00:01.00]line").unwrap();
301        conn.execute("UPDATE lyrics_cache SET fetched_at = 0", [])
302            .unwrap();
303        let got = fetch_lyrics(
304            &conn,
305            id,
306            "Aphex Twin",
307            "Windowlicker",
308            "Windowlicker EP",
309            0,
310        )
311        .unwrap();
312        assert!(got.synced);
313    }
314
315    #[test]
316    fn test_parse_lrc_basic() {
317        let lrc = "[00:12.00]Hello world\n[00:17.20]Second line\n[01:05.50]Third line";
318        let lines = parse_lrc(lrc);
319        assert_eq!(lines.len(), 3);
320        assert!((lines[0].time_secs - 12.0).abs() < 0.01);
321        assert_eq!(lines[0].text, "Hello world");
322        assert!((lines[1].time_secs - 17.2).abs() < 0.01);
323        assert!((lines[2].time_secs - 65.5).abs() < 0.01);
324    }
325
326    #[test]
327    fn test_parse_lrc_skips_non_timestamp_lines() {
328        let lrc = "[ti:Song Title]\n[ar:Artist]\n[00:05.00]First lyric\n[00:10.00]Second lyric";
329        let lines = parse_lrc(lrc);
330        // [ti:...] and [ar:...] tags won't parse as mm:ss.xx (colon at wrong position / no dots)
331        // They might or might not parse depending on content; just verify the lyric lines are there.
332        assert!(lines.iter().any(|l| l.text == "First lyric"));
333        assert!(lines.iter().any(|l| l.text == "Second lyric"));
334    }
335
336    #[test]
337    fn test_current_line_index_empty() {
338        assert_eq!(current_line_index(&[], 5.0), None);
339    }
340
341    #[test]
342    fn test_current_line_index_before_first() {
343        let lines = parse_lrc("[00:10.00]First");
344        assert_eq!(current_line_index(&lines, 5.0), None);
345    }
346
347    #[test]
348    fn test_current_line_index_exact_match() {
349        let lines = parse_lrc("[00:10.00]First\n[00:20.00]Second");
350        assert_eq!(current_line_index(&lines, 10.0), Some(0));
351        assert_eq!(current_line_index(&lines, 20.0), Some(1));
352    }
353
354    #[test]
355    fn test_current_line_index_between_lines() {
356        let lines = parse_lrc("[00:10.00]First\n[00:20.00]Second\n[00:30.00]Third");
357        assert_eq!(current_line_index(&lines, 15.0), Some(0));
358        assert_eq!(current_line_index(&lines, 25.0), Some(1));
359        assert_eq!(current_line_index(&lines, 35.0), Some(2));
360    }
361}