Skip to content

Commit 4b16a60

Browse files
committed
Don't parse word time tags if multiple line times are present
Word time tags are absolute timestamps in the lyrics, so having multiple line times would generally conflict with them. Thus, they should be just ignored instead, since the format is wrong. The same applies if there is no line time tag, in that case, it wouldn't be known when the first word starts. Both are inconsidered incorrect LRC and thus only parsed on a best-effort basis.
1 parent 6202b64 commit 4b16a60

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

LrcParser.Tests/Parser/Lrc/Lines/LrcLyricParserTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ public void TestDecode(string lyric, LrcLyric expected)
105105
TimeTags = [],
106106
},
107107
],
108+
// Don't parse word time tags if multiple line time tags are found, as this is unsupported by LRC.
109+
// Instead, return the unparsed line without the line time and no word time tags.
110+
[
111+
"[00:17.00][00:18.00] <00:00.00>帰<00:01.00>り<00:02.00>道<00:03.00>は",
112+
new LrcLyric
113+
{
114+
Text = "<00:00.00>帰<00:01.00>り<00:02.00>道<00:03.00>は",
115+
StartTimes = [17000, 18000],
116+
TimeTags = [],
117+
},
118+
],
108119
};
109120

110121
[TestCaseSource(nameof(testEncodeSource))]

LrcParser/Parser/Lrc/Lines/LrcLyricParser.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) karaoke.dev <contact@karaoke.dev>. Licensed under the MIT Licence.
22
// See the LICENCE file in the repository root for full licence text.
33

4+
using LrcParser.Model;
45
using LrcParser.Parser.Lines;
56
using LrcParser.Parser.Lrc.Metadata;
67
using LrcParser.Parser.Lrc.Utils;
@@ -14,8 +15,28 @@ public override bool CanDecode(string text)
1415

1516
public override LrcLyric Decode(string text)
1617
{
17-
var (startTimes, lyricText) = LrcStartTimeUtils.SplitLyricAndTimeTag(text);
18-
var (lyric, timeTags) = LrcTimedTextUtils.TimedTextToObject(lyricText);
18+
var (startTimes, rawLyric) = LrcStartTimeUtils.SplitLyricAndTimeTag(text);
19+
20+
// If there are multiple start times, it is possible that the given word time tags are incompatible with one or more start times.
21+
// For example, if a line starts at both [01:00.00] and [02:00.00],
22+
// word time tags with the values <01:10.00> and <01:20.00> would be incompatible with the second start time.
23+
// As there isn't an official LRC spec, this isn't clearly defined.
24+
// While the format is technically valid, we chose a reasonable behavior of ignoring the word time tags in this case
25+
// and returning the line as-is without parsing the word time tags.
26+
// The same applies to lines that have no start times:
27+
// As there might be lines like `Every <00:07.56> night`, the first word would not have a start time,
28+
// so we chose the same approach of ignoring the word time tags in this case.
29+
if (startTimes.Length is 0 or > 1)
30+
{
31+
return new LrcLyric
32+
{
33+
Text = rawLyric,
34+
StartTimes = startTimes,
35+
TimeTags = new SortedDictionary<TextIndex, int>()
36+
};
37+
}
38+
39+
var (lyric, timeTags) = LrcTimedTextUtils.TimedTextToObject(rawLyric);
1940

2041
return new LrcLyric
2142
{

0 commit comments

Comments
 (0)