Skip to content

Commit 7c31d67

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 7c31d67

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-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: 15 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,20 @@ 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+
// Word time tags can't be used if there are no or more than one line time tags
21+
if (startTimes.Length is 0 or > 1)
22+
{
23+
return new LrcLyric
24+
{
25+
Text = rawLyric,
26+
StartTimes = startTimes,
27+
TimeTags = new SortedDictionary<TextIndex, int>()
28+
};
29+
}
30+
31+
var (lyric, timeTags) = LrcTimedTextUtils.TimedTextToObject(rawLyric);
1932

2033
return new LrcLyric
2134
{

0 commit comments

Comments
 (0)