Skip to content

Commit

Permalink
fix initial LyricTimestamp bugs #112
Browse files Browse the repository at this point in the history
  • Loading branch information
jitwxs committed Jul 23, 2022
1 parent 5f0e9bc commit 3277ea0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
59 changes: 34 additions & 25 deletions MusicLyricApp/Bean/MusicLyricsVO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using MusicLyricApp.Exception;
using MusicLyricApp.Utils;
Expand All @@ -29,7 +28,7 @@ public enum OutputFilenameTypeEnum
[Description("歌手 - 歌曲名")] SINGER_NAME = 1,
[Description("歌曲名")] NAME = 2
}

// 搜索来源
public enum SearchSourceEnum
{
Expand Down Expand Up @@ -76,7 +75,7 @@ public enum RomajiModeEnum
[Description("送假名")] OKURIGANA = 2,
[Description("注音假名")] FURIGANA = 3,
}

// 罗马音字体系
public enum RomajiSystemEnum
{
Expand Down Expand Up @@ -207,13 +206,17 @@ public bool IsEmpty()

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

public LyricTimestamp(long millisecond)
{
TimeOffset = millisecond;
}


/// <summary>
/// 初始化 LyricTimestamp
/// </summary>
/// <param name="timestamp">[mm:ss.SSS] or [mm:ss]</param>
public LyricTimestamp(string timestamp)
{
if (string.IsNullOrWhiteSpace(timestamp) || timestamp[0] != '[' || timestamp[timestamp.Length - 1] != ']')
Expand All @@ -228,18 +231,22 @@ public LyricTimestamp(string timestamp)
var split = timestamp.Split(':');

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

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

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

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

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

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

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

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

Expand All @@ -249,12 +256,12 @@ public int CompareTo(object input)
{
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
}

if (TimeOffset == obj.TimeOffset)
{
return 0;
}

if (TimeOffset == -1)
{
return -1;
Expand All @@ -274,7 +281,7 @@ public int CompareTo(object input)
return -1;
}
}

public string PrintTimestamp(string timestampFormat, DotTypeEnum dotType)
{
var output = timestampFormat;
Expand All @@ -283,18 +290,20 @@ public string PrintTimestamp(string timestampFormat, DotTypeEnum dotType)
if (output.Contains("SSS"))
{
msDigit = 3;
} else if (output.Contains("SS"))
}
else if (output.Contains("SS"))
{
msDigit = 2;
} else if (output.Contains("S"))
}
else if (output.Contains("S"))
{
msDigit = 1;
}
else
{
msDigit = 3;
}

long offset;
if (msDigit == 3)
{
Expand All @@ -316,12 +325,12 @@ public string PrintTimestamp(string timestampFormat, DotTypeEnum dotType)
offset /= 1000;
var minute = offset / 60;
var second = offset - minute * 60;

long actualMinute;
if (output.Contains("HH"))
{
var hour = minute / 60;
actualMinute = minute % 60;
actualMinute = minute % 60;
output = output.Replace("HH", hour.ToString("00"));
}
else
Expand All @@ -343,12 +352,12 @@ public string PrintTimestamp(string timestampFormat, DotTypeEnum dotType)
{
output = output.Replace("SSS", ms.ToString("000"));
}

if (output.Contains("SS"))
{
output = output.Replace("SS", (ms / 10).ToString("00"));
}

if (output.Contains("S"))
{
output = output.Replace("S", (ms / 100).ToString("0"));
Expand All @@ -357,14 +366,14 @@ public string PrintTimestamp(string timestampFormat, DotTypeEnum dotType)
return output;
}
}

/// <summary>
/// 当行歌词信息
/// </summary>
public class LyricLineVo : IComparable
{
public LyricTimestamp Timestamp { get; set; }

/// <summary>
/// 歌词正文
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions MusicLyricAppTest/Bean/MusicLyricsVOTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ public void TestLyricLineVo()

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

[Test]
public void TestLyricTimestamp()
{
var timestamp = new LyricTimestamp("[End]");
Assert.AreEqual(0, timestamp.TimeOffset);
}
}
}

0 comments on commit 3277ea0

Please sign in to comment.