Skip to content

Commit

Permalink
Fix the lrc parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Jul 28, 2024
1 parent 2f8bcce commit e1a66b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
26 changes: 14 additions & 12 deletions LrcParser.Tests/Parser/Lrc/LrcParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void TestDecode()
{
var lrcText = new[]
{
"[00:17:97]帰[00:18:37]り[00:18:55]道[00:18:94]は[00:19:22]",
"[00:17.00] <00:00.00>帰<00:01.00>り<00:02.00>道<00:03.00>は<00:04.00>",
};

var song = new Song
Expand All @@ -24,13 +24,14 @@ public void TestDecode()
new Lyric
{
Text = "帰り道は",
StartTime = 17000,
TimeTags = new SortedDictionary<TextIndex, int?>
{
{ new TextIndex(0), 17970 },
{ new TextIndex(1), 18370 },
{ new TextIndex(2), 18550 },
{ new TextIndex(3), 18940 },
{ new TextIndex(3, IndexState.End), 19220 },
{ new TextIndex(0), 17000 },
{ new TextIndex(1), 18000 },
{ new TextIndex(2), 19000 },
{ new TextIndex(3), 20000 },
{ new TextIndex(3, IndexState.End), 21000 },
},
},
],
Expand All @@ -49,21 +50,22 @@ public void TestEncode()
new Lyric
{
Text = "帰り道は",
StartTime = 17000,
TimeTags = new SortedDictionary<TextIndex, int?>
{
{ new TextIndex(0), 17970 },
{ new TextIndex(1), 18370 },
{ new TextIndex(2), 18550 },
{ new TextIndex(3), 18940 },
{ new TextIndex(3, IndexState.End), 19220 },
{ new TextIndex(0), 17000 },
{ new TextIndex(1), 18000 },
{ new TextIndex(2), 19000 },
{ new TextIndex(3), 20000 },
{ new TextIndex(3, IndexState.End), 21000 },
},
},
],
};

var lrcText = new[]
{
"[00:17.97]帰[00:18.37]り[00:18.55]道[00:18.94]は[00:19.22]",
"[00:17.00] <00:00.00>帰<00:01.00>り<00:02.00>道<00:03.00>は<00:04.00>",
};

checkEncode(song, lrcText);
Expand Down
25 changes: 18 additions & 7 deletions LrcParser/Parser/Lrc/LrcParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ protected override Song PostProcess(List<object> values)

return new Song
{
Lyrics = lyrics.Select(l => new Lyric
{
Text = l.Text,
TimeTags = getTimeTags(l.TimeTags),
}).ToList(),
Lyrics = lyrics.SelectMany(convertLyric).ToList(),
};

static SortedDictionary<TextIndex, int?> getTimeTags(SortedDictionary<TextIndex, int> timeTags, int offsetTime = 0)
static IEnumerable<Lyric> convertLyric(LrcLyric lrcLyric)
{
foreach (var startTime in lrcLyric.StartTimes)
{
yield return new Lyric
{
Text = lrcLyric.Text,
StartTime = startTime,
TimeTags = getTimeTags(lrcLyric.TimeTags, startTime),
};
}
}

static SortedDictionary<TextIndex, int?> getTimeTags(SortedDictionary<TextIndex, int> timeTags, int offsetTime)
=> new(timeTags.ToDictionary(k => k.Key, v => v.Value + offsetTime as int?));
}

Expand All @@ -42,12 +51,14 @@ protected override IEnumerable<object> PreProcess(Song song)
var lyrics = song.Lyrics;

// first, should return the time-tag first.
// todo: implement the algorithm to combine the lyric with different start time but same time-tag.
foreach (var lyric in lyrics)
{
yield return new LrcLyric
{
Text = lyric.Text,
TimeTags = getTimeTags(lyric.TimeTags),
StartTimes = [lyric.StartTime],
TimeTags = getTimeTags(lyric.TimeTags, -lyric.StartTime),
};
}

Expand Down

0 comments on commit e1a66b8

Please sign in to comment.