mirror of
https://github.com/jayfunc/BetterLyrics.git
synced 2026-01-12 19:24:55 +08:00
148 lines
4.8 KiB
C#
148 lines
4.8 KiB
C#
using BetterLyrics.WinUI3.Helper;
|
|
using BetterLyrics.WinUI3.Services.ResourceService;
|
|
using CommunityToolkit.Mvvm.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace BetterLyrics.WinUI3.Models
|
|
{
|
|
public class LyricsData
|
|
{
|
|
private static readonly IResourceService _resourceService = Ioc.Default.GetRequiredService<IResourceService>();
|
|
|
|
public List<LyricsLine> LyricsLines { get; set; }
|
|
public string? LanguageCode
|
|
{
|
|
get => field ?? LanguageHelper.DetectLanguageCode(WrappedOriginalText);
|
|
set => field = value;
|
|
}
|
|
public bool AutoGenerated { get; set; } = false;
|
|
public string WrappedOriginalText => string.Join(StringHelper.NewLine, LyricsLines.Select(line => line.OriginalText));
|
|
|
|
public LyricsData()
|
|
{
|
|
LyricsLines = [];
|
|
}
|
|
|
|
public LyricsData(List<LyricsLine> lyricsLines)
|
|
{
|
|
LyricsLines = lyricsLines;
|
|
}
|
|
|
|
public void SetTranslatedText(LyricsData translationData, int toleranceMs = 50)
|
|
{
|
|
foreach (var line in LyricsLines)
|
|
{
|
|
// 在翻译歌词中查找与当前行开始时间最接近且在容忍范围内的行
|
|
var transLine = translationData.LyricsLines
|
|
.FirstOrDefault(t => Math.Abs(t.StartMs - line.StartMs) <= toleranceMs);
|
|
|
|
if (transLine != null)
|
|
{
|
|
// 此处 transLine.OriginalText 指翻译中的“原文”属性
|
|
line.TranslatedText = transLine.OriginalText;
|
|
}
|
|
else
|
|
{
|
|
// 没有匹配的翻译
|
|
line.TranslatedText = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetPhoneticText(LyricsData phoneticData, int toleranceMs = 50)
|
|
{
|
|
foreach (var line in LyricsLines)
|
|
{
|
|
// 在音译歌词中查找与当前行开始时间最接近且在容忍范围内的行
|
|
var transLine = phoneticData.LyricsLines
|
|
.FirstOrDefault(t => Math.Abs(t.StartMs - line.StartMs) <= toleranceMs);
|
|
|
|
if (transLine != null)
|
|
{
|
|
// 此处 transLine.OriginalText 指音译中的“原文”属性
|
|
line.PhoneticText = transLine.OriginalText;
|
|
}
|
|
else
|
|
{
|
|
// 没有匹配的音译
|
|
line.PhoneticText = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetTranslation(string translation)
|
|
{
|
|
List<string> translationArr = translation.Split(StringHelper.NewLine).ToList();
|
|
int i = 0;
|
|
foreach (var line in LyricsLines)
|
|
{
|
|
if (i >= translationArr.Count)
|
|
{
|
|
line.TranslatedText = ""; // No translation available, keep empty
|
|
}
|
|
else
|
|
{
|
|
line.TranslatedText = translationArr[i];
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
public void SetTransliteration(string transliteration)
|
|
{
|
|
List<string> transliterationArr = transliteration.Split(StringHelper.NewLine).ToList();
|
|
int i = 0;
|
|
foreach (var line in LyricsLines)
|
|
{
|
|
if (i >= transliterationArr.Count)
|
|
{
|
|
line.PhoneticText = ""; // No transliteration available, keep empty
|
|
}
|
|
else
|
|
{
|
|
line.PhoneticText = transliterationArr[i];
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
public static LyricsData GetNotfoundPlaceholder()
|
|
{
|
|
return new LyricsData([new LyricsLine
|
|
{
|
|
StartMs = 0,
|
|
EndMs = (int)TimeSpan.FromMinutes(99).TotalMilliseconds,
|
|
OriginalText = _resourceService.GetLocalizedString("LyricsNotFound"),
|
|
}]);
|
|
}
|
|
|
|
public static LyricsData GetLoadingPlaceholder()
|
|
{
|
|
return new LyricsData([
|
|
new LyricsLine
|
|
{
|
|
StartMs = 0,
|
|
EndMs = (int)TimeSpan.FromMinutes(99).TotalMilliseconds,
|
|
OriginalText = "● ● ●",
|
|
},
|
|
]);
|
|
}
|
|
|
|
public LyricsLine? GetLyricsLine(double sec)
|
|
{
|
|
for (int i = 0; i < LyricsLines.Count; i++)
|
|
{
|
|
var line = LyricsLines[i];
|
|
if (line.StartMs > sec * 1000)
|
|
{
|
|
return LyricsLines.ElementAtOrDefault(i - 1);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|