Skip to content

Commit

Permalink
Merge pull request #111 from jitwxs/dev
Browse files Browse the repository at this point in the history
Release v4.7
  • Loading branch information
jitwxs authored Jul 17, 2022
2 parents a860cc5 + cfea5a6 commit 734787d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 121 deletions.
2 changes: 1 addition & 1 deletion MusicLyricApp/Bean/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MusicLyricApp.Bean
{
public static class Constants
{
public const string Version = "v4.6";
public const string Version = "v4.7";

public static readonly string SettingPath = Environment.CurrentDirectory + "\\MusicLyricAppSetting.json";

Expand Down
117 changes: 54 additions & 63 deletions MusicLyricApp/Bean/MusicLyricsVO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,52 +207,40 @@ public bool IsEmpty()

public class LyricTimestamp : IComparable
{
public long Minute { get; }

public long Second { get; }

public long Millisecond { get; }

public long TimeOffset { get;}

public LyricTimestamp(long millisecond)
{
TimeOffset = millisecond;

Millisecond = millisecond % 1000;

millisecond /= 1000;

Second = millisecond % 60;

Minute = millisecond / 60;
}

public LyricTimestamp(string timestamp)
{
if (string.IsNullOrWhiteSpace(timestamp) || timestamp[0] != '[' || timestamp[timestamp.Length - 1] != ']')
{
// 不支持的格式
TimeOffset = 0;
}
else
{
timestamp = timestamp.Substring(1, timestamp.Length - 2);

var split = timestamp.Split(':');

Minute = GlobalUtils.toInt(split[0], 0);
var minute = GlobalUtils.toInt(split[0], 0);

split = split[1].Split('.');

Second = GlobalUtils.toInt(split[0], 0);
var second = GlobalUtils.toInt(split[0], 0);

TimeOffset = (minute * 60 + second) * 1000;

if (split.Length > 1)
{
Millisecond = GlobalUtils.toInt(split[1], 0);
// 三位毫秒,右填充 0
TimeOffset += GlobalUtils.toInt(split[1].PadRight(3, '0'), 0);
}
}

TimeOffset = (Minute * 60 + Second) * 1000 + Millisecond;
}

public int CompareTo(object input)
Expand Down Expand Up @@ -287,20 +275,58 @@ public int CompareTo(object input)
}
}

public string PrintTimestamp(string timestampFormat, DotTypeEnum dotTypeEnum)
public string PrintTimestamp(string timestampFormat, DotTypeEnum dotType)
{
var output = timestampFormat;

int msDigit;
if (output.Contains("SSS"))
{
msDigit = 3;
} else if (output.Contains("SS"))
{
msDigit = 2;
} else if (output.Contains("S"))
{
msDigit = 1;
}
else
{
msDigit = 3;
}

long offset;
if (msDigit == 3)
{
offset = TimeOffset;
}
else
{
if (dotType == DotTypeEnum.DOWN)
{
offset = TimeOffset / 10 * 10;
}
else
{
offset = (TimeOffset + 5) / 10 * 10;
}
}

var ms = offset % 1000;
offset /= 1000;
var minute = offset / 60;
var second = offset - minute * 60;

long actualMinute;
if (output.Contains("HH"))
{
var hour = Minute / 60;
actualMinute = Minute % 60;
var hour = minute / 60;
actualMinute = minute % 60;
output = output.Replace("HH", hour.ToString("00"));
}
else
{
actualMinute = Minute;
actualMinute = minute;
}

if (output.Contains("mm"))
Expand All @@ -310,61 +336,26 @@ public string PrintTimestamp(string timestampFormat, DotTypeEnum dotTypeEnum)

if (output.Contains("ss"))
{
output = output.Replace("ss", Second.ToString("00"));
output = output.Replace("ss", second.ToString("00"));
}

if (output.Contains("SSS"))
{
output = output.Replace("SSS", Millisecond.ToString("000"));
output = output.Replace("SSS", ms.ToString("000"));
}

if (output.Contains("SS"))
{
var actualMillisecond = AdjustMillisecondScale(2, dotTypeEnum);
output = output.Replace("SS", actualMillisecond.ToString("00"));
output = output.Replace("SS", (ms / 10).ToString("00"));
}

if (output.Contains("S"))
{
var actualMillisecond = AdjustMillisecondScale(1, dotTypeEnum);
output = output.Replace("S", actualMillisecond.ToString("0"));
output = output.Replace("S", (ms / 100).ToString("0"));
}

return output;
}

/// <summary>
/// 调整毫秒位数
/// </summary>
/// <param name="scale">位数,取值为 1 ~ 3</param>
/// <param name="dotTypeEnum">截位规则</param>
/// <returns></returns>
private long AdjustMillisecondScale(int scale, DotTypeEnum dotTypeEnum)
{
var limit = 1;
for (var i = 0; i < scale; i++)
{
limit *= 10;
}

var actualMillisecond = Millisecond;

while (actualMillisecond >= limit)
{
var round = 0;
if (dotTypeEnum == DotTypeEnum.HALF_UP)
{
if (actualMillisecond % 10 >= 5)
{
round = 1;
}
}

actualMillisecond = actualMillisecond / 10 + round;
}

return actualMillisecond;
}
}

/// <summary>
Expand Down
74 changes: 36 additions & 38 deletions MusicLyricApp/Utils/LyricUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ private static async Task<List<LyricLineVo>> FormatLyric(string originLrc, strin

var translateLyrics = SplitLrc(translateLrc, searchSource);

translateLyrics = DealTranslateLyricDefaultRule(originLyrics, translateLyrics, searchInfo.SettingBean.Config.TranslateLyricDefaultRule);

if (romajiConfig.Enable)
{
translateLyrics = await RomajiUtils.ToRomaji(originLyrics, translateLyrics, romajiConfig);
Expand Down Expand Up @@ -106,7 +108,7 @@ private static async Task<List<LyricLineVo>> FormatLyric(string originLrc, strin
throw new NotSupportedException("not support showLrcType: " + showLrcType);
}

return DealTranslateLyricDefaultRule(res, searchInfo.SettingBean.Config.TranslateLyricDefaultRule);
return res;
}

/**
Expand Down Expand Up @@ -212,49 +214,33 @@ private static List<LyricLineVo> MergeLrc(List<LyricLineVo> originList, List<Lyr
/**
* 译文缺省逻辑处理
*/
private static List<LyricLineVo> DealTranslateLyricDefaultRule(List<LyricLineVo> voList, TranslateLyricDefaultRuleEnum rule)
private static List<LyricLineVo> DealTranslateLyricDefaultRule(List<LyricLineVo> originList, List<LyricLineVo> translateList, TranslateLyricDefaultRuleEnum rule)
{
if (rule != TranslateLyricDefaultRuleEnum.IGNORE)
if (rule == TranslateLyricDefaultRuleEnum.IGNORE)
{
var timestampCounts = new Dictionary<long, int>();

foreach (var key in voList.Select(one => one.Timestamp.TimeOffset))
{
if (timestampCounts.ContainsKey(key))
{
timestampCounts[key] += 1;
}
else
{
timestampCounts.Add(key,1);
}
}
return translateList;
}

var dealTimestamps = (from pair in timestampCounts where pair.Value == 1 select pair.Key).ToList();
dealTimestamps.Sort((a, b) => a.CompareTo(b));
var originTimeOffsetMap = ConvertLyricLineVoListToMapByTimeOffset(originList);
var translateTimeOffsetMap = ConvertLyricLineVoListToMapByTimeOffset(translateList);

foreach (var pair in translateTimeOffsetMap.Where(pair => originTimeOffsetMap.ContainsKey(pair.Key)))
{
originTimeOffsetMap.Remove(pair.Key);
}

foreach (var pair in originTimeOffsetMap)
{
var content = rule == TranslateLyricDefaultRuleEnum.FILL_ORIGIN ? pair.Value.Content : "";

var dealTimestampIndex = 0;

for (var i = 0; i < voList.Count; i++)
{
var cur = voList[i];
var timestamp = dealTimestamps[dealTimestampIndex];

if (cur.Timestamp.TimeOffset == timestamp)
{
var content = rule == TranslateLyricDefaultRuleEnum.FILL_ORIGIN ? cur.Content : "";
voList.Insert(++i, new LyricLineVo(content, cur.Timestamp));
dealTimestampIndex++;

if (dealTimestampIndex >= dealTimestamps.Count)
{
break;
}
}
}
translateTimeOffsetMap[pair.Key] = new LyricLineVo(content, pair.Value.Timestamp);
}

var res = new List<LyricLineVo>(translateTimeOffsetMap.Values);

res.Sort();

return voList;
return res;
}

/**
Expand All @@ -271,5 +257,17 @@ private static int Compare(LyricLineVo originLrc, LyricLineVo translateLrc, bool

return compareTo;
}

private static Dictionary<long, LyricLineVo> ConvertLyricLineVoListToMapByTimeOffset(List<LyricLineVo> lyricLineVos)
{
var map = new Dictionary<long, LyricLineVo>();

foreach (var one in lyricLineVos)
{
map[one.Timestamp.TimeOffset] = one;
}

return map;
}
}
}
38 changes: 20 additions & 18 deletions MusicLyricAppTest/Bean/LyricTimestampTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public void TestIllegalScenario()
new LyricTimestamp("-1]")
})
{
AreEqual(0, scenario.Minute);
AreEqual(0, scenario.Second);
AreEqual(0, scenario.Millisecond);

AreEqual("[00:00.000]", scenario.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.DOWN));
}
}
Expand All @@ -33,9 +29,6 @@ public void TestCreateByTimestamp()
var bean = new LyricTimestamp(18962305L);

AreEqual(18962305L, bean.TimeOffset);
AreEqual(316, bean.Minute);
AreEqual(2, bean.Second);
AreEqual(305, bean.Millisecond);

AreEqual("[05:16:02+305]", bean.PrintTimestamp("[HH:mm:ss+SSS]", DotTypeEnum.HALF_UP));
AreEqual("[316:02+305]", bean.PrintTimestamp("[mm:ss+SSS]", DotTypeEnum.DOWN));
Expand All @@ -52,27 +45,36 @@ public void TestCreateByTimestampStr()
{
// scenario1 [1:2.3]
var scenario1 = new LyricTimestamp("[1:2.3]");
AreEqual(1, scenario1.Minute);
AreEqual(2, scenario1.Second);
AreEqual(3, scenario1.Millisecond);

AreEqual("[01:02.003]", scenario1.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.DOWN));
AreEqual("[01:02.300]", scenario1.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.DOWN));

// scenario2 [00:39.17]
var scenario2 = new LyricTimestamp("[[00:39.17]");
AreEqual(0, scenario2.Minute);
AreEqual(39, scenario2.Second);
AreEqual(17, scenario2.Millisecond);

AreEqual("[00:39.017]", scenario2.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.DOWN));
AreEqual("[00:39.170]", scenario2.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.DOWN));

// scenario3 [00:39.17]
var scenario3 = new LyricTimestamp("[00:2]");
AreEqual(0, scenario3.Minute);
AreEqual(2, scenario3.Second);
AreEqual(0, scenario3.Millisecond);

AreEqual("[00:02.000]", scenario3.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.DOWN));
}

[Test]
public void TestIssue109()
{
var scenario = new LyricTimestamp("[04:17.995]");

AreEqual("[04:18.00]", scenario.PrintTimestamp("[mm:ss.SS]", DotTypeEnum.HALF_UP));

scenario = new LyricTimestamp("[04:19.093]");

AreEqual("[04:19.09]", scenario.PrintTimestamp("[mm:ss.SS]", DotTypeEnum.HALF_UP));

scenario = new LyricTimestamp("[00:10.98]");

AreEqual("[00:10.980]", scenario.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.DOWN));

AreEqual("[00:10.980]", scenario.PrintTimestamp("[mm:ss.SSS]", DotTypeEnum.HALF_UP));
}
}
}
2 changes: 1 addition & 1 deletion MusicLyricAppTest/Bean/MusicLyricsVOTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void TestLyricLineVo()

var scenario6 = new LyricLineVo("[01:10.050] 作词 : Kirara Magic");

Assert.AreEqual("[01:10.50]", scenario6.Timestamp.PrintTimestamp("[mm:ss.SS]", DotTypeEnum.HALF_UP));
Assert.AreEqual("[01:10.05]", scenario6.Timestamp.PrintTimestamp("[mm:ss.SS]", DotTypeEnum.HALF_UP));
}
}
}

0 comments on commit 734787d

Please sign in to comment.