alrc 0.1.0

Advanced Lyrics File Format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#![warn(missing_docs)]
//! # Advanced Lyrics File
//!
//! What does this add to the `lrc` base format ?
//!
//! - Named Markers
//! - Vocals
//! - Instrumental Line Check
//! - Custom Tags
//!
//! Those new additions don’t by any mean break previous specification of the
//! format. The compatibility with `A2 extension` is preserved and is even
//! supported in the vocals. Chained timestamps are also supported.
//!
//! ## Named Markers
//!
//! Those aim to provide a context for the lyric line.
//! An use case can be for the singer that sing the line
//!
//! **Named Marker Example**
//!
//! ```lrc
//! {@singer:The Name Of The Singer}
//! ```
//!
//! The marker will be represented with the key `singer` and
//! value `The Name Of The Singer`
//!
//! Each time a new Markers is encounter by the parser the current marker
//! value is changed.
//!
//! **Named Marker Change Example**
//!
//! ```lrc
//! {@singer:Childish Gambino}
//!
//! [00:15.84] Cody LaRae
//! [00:19.30] He had a break
//! [00:22.75] He's findin' out
//! [00:25.92] That nobody gives a fuck
//! [00:29.68] I did my job
//! [00:32.91] I paid my dues
//! [00:36.13] Love is for fools
//! [00:39.33] 'Cause nobody gives a fuck
//!
//! {@singer:VOCALS}
//!
//! [00:45.54] (No one, no one)
//!
//! ...
//! ```
//!
//! ## Vocals
//!
//! Vocals are background voices other than the main artist voice.
//! Most of the times, they are represented between parenthesis to signify
//! that they are not important.
//!
//! **Example of vocals**
//!
//! ```lrc
//! ...
//!
//! [00:52.71] {#vocal:No one} nobody gives a fuck
//! ...
//! ```
//!
//! A single single line can have multiples vocals
//!
//! ## Instrumental Line Check
//!
//! Any line that contains only `#INSTRUMENTAL` is consider as a line where
//! there is a long instrumental pause.
//!
//! ```lrc
//! [00:50.07] #INSTRUMENTAL
//! ```
//!
//! ## Custom Tags
//!
//! Now with this parser, tags does not matter to it. Therefore, you can create any
//! tag that suite your needs.

#![doc(issue_tracker_base_url = "https://github.com/luxluth/alrc/issues/")]

use lazy_static::lazy_static;
use nom::{
    character::complete::{char, digit1},
    combinator::{map_res, opt},
    sequence::{preceded, tuple},
    IResult,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, time::Duration, usize};

lazy_static! {
    static ref VOCAL_RE: regex::Regex = regex::Regex::new(r"\{#vocal:(.*?)\}").unwrap();
}

/// HashMap that store key value of and attribute
pub type Metadata = HashMap<String, String>;

/// Marker are used to denote a line
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub enum Marker {
    /// Named marker
    Named(String, String),
    /// Empty marker
    Empty,
}

impl std::fmt::Display for Marker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Marker::Named(key, value) => {
                write!(f, "[MARKER:Named({})] {}", key, value)
            }
            Marker::Empty => {
                write!(f, "[MARKER:Empty]")
            }
        }
    }
}

/// A chunk of the base text that is timed
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Syllable {
    /// Syllable text
    pub text: String,
    /// The time at the syllable is pronouunced
    pub start: RawTimestamp,
}

fn make_time(arr: &mut impl Iterator<Item = char>) -> RawTimestamp {
    let mut text = String::new();
    for c in arr {
        if c == '>' {
            text.push('>');
            break;
        } else {
            text.push(c);
        }
    }

    RawTimestamp::parse(&text, '<', '>').unwrap().1
}

impl Syllable {
    fn parse_many(input: &str, start_time: RawTimestamp) -> Vec<Self> {
        let mut text_iter = input.chars().peekable();
        let mut current_text = String::new();
        let mut syllables: Vec<Syllable> = vec![];
        let mut current_time = start_time;

        while let Some(character) = text_iter.peek() {
            if *character == '<' {
                if !current_text.is_empty() {
                    syllables.push(Syllable {
                        text: current_text,
                        start: current_time,
                    });
                    current_text = String::new();
                }
                current_time = make_time(&mut text_iter);
            } else {
                current_text.push(text_iter.next().unwrap());
            }
        }

        if !current_text.is_empty() {
            syllables.push(Syllable {
                text: current_text,
                start: current_time,
            });
        }

        syllables
    }
}

trait ToString {
    fn to_string(&self) -> String;
}

impl ToString for Vec<Syllable> {
    fn to_string(&self) -> String {
        let mut out = String::new();
        for s in self {
            out.push_str(&s.text);
        }
        out
    }
}

impl Vocal {
    fn parse(input: &str, start_time: RawTimestamp) -> Self {
        let syllables = Syllable::parse_many(input, start_time);
        Self {
            text: syllables.to_string(),
            time: start_time,
            syllables,
        }
    }
}

/// Vocal Container
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Vocal {
    /// Global vocal text
    pub text: String,
    /// Time at which start the vocals
    pub time: RawTimestamp,
    /// syllables from text
    pub syllables: Vec<Syllable>,
}

/// Vocal Container
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Line {
    /// [Marker]
    pub marker: Marker,
    /// The [Syllable] array
    pub syllables: Vec<Syllable>,
    /// The text representing the line
    pub text: String,
    /// Lyric line time start
    pub time: RawTimestamp,
    /// The [Vocal] array
    pub vocals: Vec<Vocal>,
    /// Line location
    pub ln: usize,
    /// Determine if a line is instrumental
    pub is_instrumental: bool,
}

/// Parsed lyrics conatiner
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct AdvancedLrc {
    /// Lyrics [Metadata]
    pub metadata: Metadata,
    /// Parsed [Line] array
    pub lines: Vec<Line>,
}

impl AdvancedLrc {
    /// Parse lrc text
    pub fn parse(input: &str) -> Result<Self, String> {
        let lines: Vec<&str> = input.lines().collect();
        let mut meta = Metadata::new();
        let mut parsed_lines: Vec<Line> = vec![];
        let mut current_marker = Marker::Empty;
        let mut ln: usize = 1;

        for line in lines {
            let line = line.trim();
            if line.is_empty() {
                ln += 1;
                continue;
            }
            if line.starts_with('[') && line.ends_with(']') {
                let (key, value) = line
                    .trim_start_matches('[')
                    .trim_end_matches(']')
                    .split_once(':')
                    .unwrap();
                if key.chars().next().unwrap().is_ascii_digit() {
                    parsed_lines.extend(AdvancedLrc::parse_line(line, current_marker.clone(), ln)?);
                } else {
                    meta.insert(key.to_string(), value.to_string());
                }
            } else if line.starts_with('{') && line.ends_with('}') {
                let (marker, value) = line
                    .trim_start_matches('{')
                    .trim_end_matches('}')
                    .split_once(':')
                    .unwrap();
                if marker.starts_with('@') {
                    current_marker =
                        Marker::Named(marker.replace('@', "").to_string(), value.to_string());
                }
            } else if line.starts_with('#') {
                ln += 1;
                continue;
            } else {
                parsed_lines.extend(AdvancedLrc::parse_line(line, current_marker.clone(), ln)?);
            }

            ln += 1;
        }

        Ok(Self {
            metadata: meta,
            lines: parsed_lines,
        })
    }

    fn parse_line(text: &str, current_marker: Marker, ln: usize) -> Result<Vec<Line>, String> {
        let (times, remaining) = RawTimestamp::parse_many(text, '[', ']');

        let mut lines: Vec<Line> = Vec::with_capacity(times.len());

        for time in times {
            let mut text = remaining.trim().to_string();
            let mut vocals: Vec<Vocal> = vec![];
            let text_clone = text.clone();
            for cap in VOCAL_RE.captures_iter(&text_clone) {
                let vocal_text = cap.get(1).unwrap().as_str();
                text = text
                    .replace(&format!("{{#vocal:{vocal_text}}}"), "")
                    .trim()
                    .to_string();
                vocals.push(Vocal::parse(vocal_text, time));
            }

            let syllables = Syllable::parse_many(&text, time);
            let mut is_instrumental = false;
            if text == String::from("#INSTRUMENTAL") {
                is_instrumental = true;
            };

            lines.push(Line {
                marker: current_marker.clone(),
                syllables,
                text,
                time,
                vocals,
                ln,
                is_instrumental,
            });
        }

        Ok(lines)
    }
}

/// Simple Timestamp
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct RawTimestamp {
    /// Timestamp minutes
    pub minutes: u8,
    /// Timestamp seconds
    pub seconds: u8,
    /// Timestamp millis
    pub millis: Option<u8>,
}

impl RawTimestamp {
    fn parse(input: &str, start: char, end: char) -> IResult<&str, RawTimestamp> {
        let (input, (_, minutes, _, seconds, millis, _)) = tuple((
            char(start),
            parse_minutes,
            char(':'),
            parse_seconds,
            parse_millis,
            char(end),
        ))(input)?;

        Ok((
            input,
            RawTimestamp {
                minutes,
                seconds,
                millis,
            },
        ))
    }

    /// Convert timestamp to standard duration
    pub fn to_duration(&self) -> Duration {
        Duration::from_millis(
            (((self.minutes as u64) * 60 + self.seconds as u64) * 1000)
                + self.millis.unwrap_or(0) as u64,
        )
    }

    fn parse_many(input: &str, start: char, end: char) -> (Vec<RawTimestamp>, &str) {
        let mut remaining = input;
        let mut timestamps = Vec::new();

        while let Ok((new_remaining, timestamp)) = RawTimestamp::parse(remaining, start, end) {
            timestamps.push(timestamp);
            remaining = new_remaining;
        }

        (timestamps, remaining)
    }
}

impl std::fmt::Display for RawTimestamp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}ms", self.to_duration().as_millis())
    }
}

fn parse_u8(input: &str) -> IResult<&str, u8> {
    map_res(digit1, str::parse)(input)
}
fn parse_minutes(input: &str) -> IResult<&str, u8> {
    parse_u8(input)
}
fn parse_seconds(input: &str) -> IResult<&str, u8> {
    parse_u8(input)
}
fn parse_millis(input: &str) -> IResult<&str, Option<u8>> {
    opt(preceded(char('.'), parse_u8))(input)
}