From e1a66b8063f5db382903744b595243d444202646 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 28 Jul 2024 16:28:29 +0800 Subject: [PATCH] Fix the lrc parser. --- LrcParser.Tests/Parser/Lrc/LrcParserTest.cs | 26 +++++++++++---------- LrcParser/Parser/Lrc/LrcParser.cs | 25 ++++++++++++++------ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/LrcParser.Tests/Parser/Lrc/LrcParserTest.cs b/LrcParser.Tests/Parser/Lrc/LrcParserTest.cs index d0c5f22..d1ed788 100644 --- a/LrcParser.Tests/Parser/Lrc/LrcParserTest.cs +++ b/LrcParser.Tests/Parser/Lrc/LrcParserTest.cs @@ -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 @@ -24,13 +24,14 @@ public void TestDecode() new Lyric { Text = "帰り道は", + StartTime = 17000, TimeTags = new SortedDictionary { - { 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 }, }, }, ], @@ -49,13 +50,14 @@ public void TestEncode() new Lyric { Text = "帰り道は", + StartTime = 17000, TimeTags = new SortedDictionary { - { 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 }, }, }, ], @@ -63,7 +65,7 @@ public void TestEncode() 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); diff --git a/LrcParser/Parser/Lrc/LrcParser.cs b/LrcParser/Parser/Lrc/LrcParser.cs index 7378511..c4aa108 100644 --- a/LrcParser/Parser/Lrc/LrcParser.cs +++ b/LrcParser/Parser/Lrc/LrcParser.cs @@ -26,14 +26,23 @@ protected override Song PostProcess(List 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 getTimeTags(SortedDictionary timeTags, int offsetTime = 0) + static IEnumerable 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 getTimeTags(SortedDictionary timeTags, int offsetTime) => new(timeTags.ToDictionary(k => k.Key, v => v.Value + offsetTime as int?)); } @@ -42,12 +51,14 @@ protected override IEnumerable 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), }; }