Skip to content

Commit

Permalink
Make the LRC parser even more lenient
Browse files Browse the repository at this point in the history
  • Loading branch information
cqjjjzr committed Aug 7, 2022
1 parent bd56e63 commit 9337813
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions LyricParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LyricParser
{
private static readonly Regex LyricWordRegex = new Regex(@".*\](.*)", RegexOptions.Compiled);
private static readonly Regex LyricTimeRegex = new Regex(@"\[([0-9.:]*)\]+(.*)", RegexOptions.Compiled);
private static readonly Regex LyricTimeSingleRegex = new Regex(@"(\d+):(\d+)\.(\d+)", RegexOptions.Compiled);
private static readonly Regex LyricTimeSingleRegex = new Regex(@"(\d+):(\d+)(\.(\d+))?", RegexOptions.Compiled);

public static bool PreserveSlash { get; set; } = false;

Expand Down Expand Up @@ -55,14 +55,18 @@ public static Lyrics ParseLyric(string lyric)
if (!int.TryParse(singleMatch.Groups[1].Value, NumberStyles.Any, null, out var mins))
continue;
if (!int.TryParse(singleMatch.Groups[2].Value, NumberStyles.Any, null, out var secs))
continue;

var fsecString = singleMatch.Groups[3].Value;
if (!int.TryParse(fsecString, NumberStyles.Any, null, out var fsrc))
continue;

var msec = 0;
if (singleMatch.Groups.Count > 4)
{
var fsecString = singleMatch.Groups[3].Value;
if (int.TryParse(fsecString, NumberStyles.Any, null, out msec))
msec *= fsecString.Length == 3 ? 1 : 10;
}

// Netease uses .MMM that provides milliseconds instead of 1/100s second
var time = mins * 60 * 1000 + secs * 1000 + fsrc * (fsecString.Length == 3 ? 1 : 10);
var time = mins * 60 * 1000 + secs * 1000 + msec;
rawLyrics.Add(new RawLyricEntry(time, word));
}
}
Expand Down

0 comments on commit 9337813

Please sign in to comment.