fix: lyrics parser

This commit is contained in:
Zhe Fang
2026-01-10 07:38:17 -05:00
parent 28757d9880
commit 047e53b830
8 changed files with 66 additions and 23 deletions

View File

@@ -83,7 +83,7 @@ namespace BetterLyrics.WinUI3.Logic
? 1.15
: lyricsEffect.LyricsScaleEffectAmount / 100.0;
var maxAnimationDurationMs = Math.Max(line.EndMs - currentPositionMs, 0);
var maxAnimationDurationMs = Math.Max(line.EndMs ?? 0 - currentPositionMs, 0);
bool isSecondaryLinePlaying = line.GetIsPlaying(currentPositionMs);
bool isSecondaryLinePlayingChanged = line.IsPlayingLastFrame != isSecondaryLinePlaying;

View File

@@ -215,7 +215,7 @@ namespace BetterLyrics.WinUI3.Logic
lanesEndMs.Add(0);
}
lanesEndMs[assignedLane] = end;
lanesEndMs[assignedLane] = end ?? 0;
line.LaneIndex = assignedLane;
}
}

View File

@@ -74,7 +74,7 @@ namespace BetterLyrics.WinUI3.Logic
if (line == null) return state;
double lineEndMs = line.EndMs;
double lineEndMs = line.EndMs ?? 0;
// 还没到
if (currentTimeMs < line.StartMs) return state;
@@ -129,8 +129,7 @@ namespace BetterLyrics.WinUI3.Logic
var timing = line.PrimaryRenderSyllables[i];
var nextTiming = (i + 1 < count) ? line.PrimaryRenderSyllables[i + 1] : null;
//double timingEndMs = timing.EndMs ?? nextTiming?.StartMs ?? lineEndMs;
double timingEndMs = timing.EndMs;
double timingEndMs = timing.EndMs ?? 0;
// 在当前字范围内
if (time >= timing.StartMs && time <= timingEndMs)

View File

@@ -7,8 +7,8 @@ namespace BetterLyrics.WinUI3.Models.Lyrics
public class BaseLyrics
{
public int StartMs { get; set; }
public int EndMs { get; set; }
public int DurationMs => EndMs - StartMs;
public int? EndMs { get; set; } = null;
public int DurationMs => Math.Max((EndMs ?? 0) - StartMs, 0);
public string Text { get; set; } = "";
public int Length => Text.Length;

View File

@@ -40,24 +40,11 @@ namespace BetterLyrics.WinUI3.Parsers.LyricsParser
startIndex += text.Length;
}
int lineEndMs = 0;
if (syllables.Count > 0)
{
var lastSyllable = syllables[syllables.Count - 1];
if (string.IsNullOrWhiteSpace(lastSyllable.Text))
{
lineEndMs = lastSyllable.StartMs;
syllables.RemoveAt(syllables.Count - 1);
}
}
if (syllables.Count > 1)
{
lrcLines.Add(new LyricsLine
{
StartMs = syllables[0].StartMs,
EndMs = lineEndMs,
PrimaryText = string.Concat(syllables.Select(s => s.Text)),
PrimarySyllables = syllables
});
@@ -81,7 +68,19 @@ namespace BetterLyrics.WinUI3.Parsers.LyricsParser
content = bracketRegex!.Replace(line, "").Trim();
if (content == "//") content = "";
lrcLines.Add(new LyricsLine { StartMs = lineStartMs, PrimaryText = content });
lrcLines.Add(new LyricsLine
{
StartMs = lineStartMs,
PrimarySyllables = [
new BaseLyrics
{
StartIndex = 0,
StartMs = lineStartMs,
Text = content
}
],
PrimaryText = content
});
}
}
}
@@ -125,5 +124,6 @@ namespace BetterLyrics.WinUI3.Parsers.LyricsParser
}
}
}
}
}

View File

@@ -21,7 +21,7 @@ namespace BetterLyrics.WinUI3.Parsers.LyricsParser
var lineWrite = new LyricsLine
{
StartMs = lineRead.StartTime ?? 0,
EndMs = lineRead.EndTime ?? (nextLineRead?.StartTime ?? 0),
EndMs = lineRead.EndTime,
PrimaryText = lineRead.Text,
PrimarySyllables = [],
};

View File

@@ -69,6 +69,8 @@ namespace BetterLyrics.WinUI3.Parsers.LyricsParser
LoadTransliteration(lyricsSearchResult);
GenerateTransliterationLyricsData();
EnsureEndMs(lyricsSearchResult?.Duration);
return _lyricsDataArr;
}
@@ -271,5 +273,45 @@ namespace BetterLyrics.WinUI3.Parsers.LyricsParser
}
}
private void EnsureEndMs(double? duration)
{
foreach (var lyricsData in _lyricsDataArr)
{
var lines = lyricsData.LyricsLines;
// 计算结束时间
for (int i = 0; i < lines.Count; i++)
{
// 计算行结束时间
if (lines[i].EndMs == null)
{
if (i + 1 < lines.Count)
{
lines[i].EndMs = lines[i + 1].StartMs;
}
else
{
lines[i].EndMs = (int)(duration ?? 0) * 1000;
}
}
// 计算音节结束时间
for (int j = 0; j < lines[i].PrimarySyllables.Count; j++)
{
var syllable = lines[i].PrimarySyllables[j];
if (syllable.EndMs == null)
{
if (j < lines[i].PrimarySyllables.Count - 1)
{
syllable.EndMs = lines[i].PrimarySyllables[j + 1].StartMs;
}
else
{
syllable.EndMs = lines[i].EndMs;
}
}
}
}
}
}
}
}