Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions LrcParser.Tests/Parser/Lrc/Lines/LrcLyricParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ public void TestDecode(string lyric, LrcLyric expected)
TimeTags = [],
},
],
// Don't parse word time tags if multiple line time tags are found, as this is unsupported by LRC.
// Instead, return the unparsed line without the line time and no word time tags.
[
"[00:17.00][00:18.00] <00:00.00>帰<00:01.00>り<00:02.00>道<00:03.00>は",
new LrcLyric
{
Text = "<00:00.00>帰<00:01.00>り<00:02.00>道<00:03.00>は",
StartTimes = [17000, 18000],
TimeTags = [],
},
],
};

[TestCaseSource(nameof(testEncodeSource))]
Expand Down
25 changes: 23 additions & 2 deletions LrcParser/Parser/Lrc/Lines/LrcLyricParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) karaoke.dev <contact@karaoke.dev>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using LrcParser.Model;
using LrcParser.Parser.Lines;
using LrcParser.Parser.Lrc.Metadata;
using LrcParser.Parser.Lrc.Utils;
Expand All @@ -14,8 +15,28 @@ public override bool CanDecode(string text)

public override LrcLyric Decode(string text)
{
var (startTimes, lyricText) = LrcStartTimeUtils.SplitLyricAndTimeTag(text);
var (lyric, timeTags) = LrcTimedTextUtils.TimedTextToObject(lyricText);
var (startTimes, rawLyric) = LrcStartTimeUtils.SplitLyricAndTimeTag(text);

// If there are multiple start times, it is possible that the given word time tags are incompatible with one or more start times.
// For example, if a line starts at both [01:00.00] and [02:00.00],
// word time tags with the values <01:10.00> and <01:20.00> would be incompatible with the second start time.
// As there isn't an official LRC spec, this isn't clearly defined.
// While the format is technically valid, we chose a reasonable behavior of ignoring the word time tags in this case
// and returning the line as-is without parsing the word time tags.
// The same applies to lines that have no start times:
// As there might be lines like `Every <00:07.56> night`, the first word would not have a start time,
// so we chose the same approach of ignoring the word time tags in this case.
if (startTimes.Length is 0 or > 1)
{
return new LrcLyric
{
Text = rawLyric,
StartTimes = startTimes,
TimeTags = new SortedDictionary<TextIndex, int>()
};
}

var (lyric, timeTags) = LrcTimedTextUtils.TimedTextToObject(rawLyric);

return new LrcLyric
{
Expand Down