|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.ComponentModel; |
4 | 4 | using System.Linq; |
| 5 | +using System.Text; |
5 | 6 | using System.Web; |
6 | 7 | using MusicLyricApp.Exception; |
7 | 8 | using MusicLyricApp.Utils; |
@@ -196,76 +197,194 @@ public bool IsEmpty() |
196 | 197 | return string.IsNullOrEmpty(Lyric) && string.IsNullOrEmpty(TranslateLyric); |
197 | 198 | } |
198 | 199 | } |
| 200 | + |
| 201 | + public class LyricTimestamp : IComparable |
| 202 | + { |
| 203 | + public long Minute { get; private set; } |
| 204 | + |
| 205 | + public string MinuteS { get; private set; } |
| 206 | + |
| 207 | + public long Second { get; private set; } |
| 208 | + |
| 209 | + public string SecondS { get; private set; } |
| 210 | + |
| 211 | + public long Millisecond { get; private set; } |
| 212 | + |
| 213 | + public string MillisecondS { get; private set; } |
| 214 | + |
| 215 | + public long TimeOffset { get;} |
| 216 | + |
| 217 | + public LyricTimestamp(long millisecond) |
| 218 | + { |
| 219 | + TimeOffset = millisecond; |
| 220 | + |
| 221 | + Millisecond = millisecond % 1000; |
| 222 | + |
| 223 | + millisecond /= 1000; |
| 224 | + |
| 225 | + Second = millisecond % 60; |
| 226 | + |
| 227 | + Minute = millisecond / 60; |
| 228 | + |
| 229 | + UpdateMinute(Minute); |
| 230 | + UpdateSecond(Second); |
| 231 | + UpdateMillisecond(Millisecond); |
| 232 | + } |
| 233 | + |
| 234 | + public LyricTimestamp(string timestamp) |
| 235 | + { |
| 236 | + if (string.IsNullOrWhiteSpace(timestamp) || timestamp[0] != '[' || timestamp[timestamp.Length - 1] != ']') |
| 237 | + { |
| 238 | + // 不支持的格式 |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + timestamp = timestamp.Substring(1, timestamp.Length - 2); |
| 243 | + |
| 244 | + var split = timestamp.Split(':'); |
| 245 | + |
| 246 | + Minute = GlobalUtils.toInt(split[0], 0); |
| 247 | + |
| 248 | + split = split[1].Split('.'); |
| 249 | + |
| 250 | + Second = GlobalUtils.toInt(split[0], 0); |
| 251 | + |
| 252 | + if (split.Length > 1) |
| 253 | + { |
| 254 | + Millisecond = GlobalUtils.toInt(split[1], 0); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + UpdateMinute(Minute); |
| 259 | + UpdateSecond(Second); |
| 260 | + UpdateMillisecond(Millisecond); |
| 261 | + |
| 262 | + TimeOffset = (Minute * 60 + Second) * 1000 + Millisecond; |
| 263 | + } |
| 264 | + |
| 265 | + public string ToString(OutputFormatEnum outputFormat) |
| 266 | + { |
| 267 | + if (outputFormat == OutputFormatEnum.LRC) |
| 268 | + { |
| 269 | + return "[" + MinuteS + ":" + SecondS + "." + MillisecondS + "]"; |
| 270 | + } |
| 271 | + else |
| 272 | + { |
| 273 | + return "00:" + MinuteS + ":" + SecondS + "," + MillisecondS; |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + public int CompareTo(object input) |
| 278 | + { |
| 279 | + if (!(input is LyricTimestamp obj)) |
| 280 | + { |
| 281 | + throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR); |
| 282 | + } |
| 283 | + |
| 284 | + if (TimeOffset == obj.TimeOffset) |
| 285 | + { |
| 286 | + return 0; |
| 287 | + } |
| 288 | + |
| 289 | + if (TimeOffset == -1) |
| 290 | + { |
| 291 | + return -1; |
| 292 | + } |
| 293 | + |
| 294 | + if (obj.TimeOffset == -1) |
| 295 | + { |
| 296 | + return 1; |
| 297 | + } |
| 298 | + |
| 299 | + if (TimeOffset > obj.TimeOffset) |
| 300 | + { |
| 301 | + return 1; |
| 302 | + } |
| 303 | + else |
| 304 | + { |
| 305 | + return -1; |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + private void UpdateMinute(long value) |
| 310 | + { |
| 311 | + Minute = value; |
| 312 | + MinuteS = value.ToString("00"); |
| 313 | + } |
| 314 | + |
| 315 | + private void UpdateSecond(long value) |
| 316 | + { |
| 317 | + Second = value; |
| 318 | + SecondS = value.ToString("00"); |
| 319 | + } |
| 320 | + |
| 321 | + public void UpdateMillisecond(long value, int scale = 3) |
| 322 | + { |
| 323 | + var format = new StringBuilder().Insert(0, "0", scale).ToString(); |
| 324 | + |
| 325 | + Millisecond = value; |
| 326 | + MillisecondS = Millisecond.ToString(format); |
| 327 | + } |
| 328 | + } |
199 | 329 |
|
200 | 330 | /// <summary> |
201 | 331 | /// 当行歌词信息 |
202 | 332 | /// </summary> |
203 | 333 | public class LyricLineVo : IComparable |
204 | 334 | { |
205 | | - /// <summary> |
206 | | - /// 时间戳字符串 |
207 | | - /// </summary> |
208 | | - public string Timestamp { get; set; } |
209 | | - |
210 | | - /** |
211 | | - * 时间偏移量 |
212 | | - */ |
213 | | - public long TimeOffset { get; set; } |
| 335 | + public LyricTimestamp Timestamp { get; set; } |
214 | 336 |
|
215 | 337 | /// <summary> |
216 | 338 | /// 歌词正文 |
217 | 339 | /// </summary> |
218 | 340 | public string Content { get; set; } |
219 | 341 |
|
220 | | - public LyricLineVo(string lyricLine) |
| 342 | + public LyricLineVo(string lyricLine = "") |
221 | 343 | { |
222 | 344 | var index = lyricLine.IndexOf("]"); |
223 | 345 | if (index == -1) |
224 | 346 | { |
225 | | - Timestamp = ""; |
226 | | - TimeOffset = -1; |
| 347 | + Timestamp = new LyricTimestamp(""); |
227 | 348 | Content = lyricLine; |
228 | 349 | } |
229 | 350 | else |
230 | 351 | { |
231 | | - Timestamp = lyricLine.Substring(0, index + 1); |
| 352 | + Timestamp = new LyricTimestamp(lyricLine.Substring(0, index + 1)); |
232 | 353 | Content = lyricLine.Substring(index + 1); |
233 | | - TimeOffset = GlobalUtils.TimestampStrToLong(Timestamp); |
234 | 354 | } |
235 | 355 | } |
236 | 356 |
|
237 | | - public LyricLineVo() |
238 | | - { |
239 | | - } |
240 | | - |
241 | 357 | public int CompareTo(object input) |
242 | 358 | { |
243 | 359 | if (!(input is LyricLineVo obj)) |
244 | 360 | { |
245 | 361 | throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR); |
246 | 362 | } |
247 | | - |
248 | | - if (TimeOffset == -1 && obj.TimeOffset == -1) |
249 | | - { |
250 | | - return 0; |
251 | | - } |
252 | | - |
253 | | - if (TimeOffset == -1) |
| 363 | + |
| 364 | + return Timestamp.CompareTo(obj.Timestamp); |
| 365 | + } |
| 366 | + |
| 367 | + /// <summary> |
| 368 | + /// 是否是无效的内容 |
| 369 | + /// </summary> |
| 370 | + public bool IsIllegalContent() |
| 371 | + { |
| 372 | + if (string.IsNullOrWhiteSpace(Content)) |
254 | 373 | { |
255 | | - return -1; |
| 374 | + return true; |
256 | 375 | } |
257 | 376 |
|
258 | | - if (obj.TimeOffset == -1) |
| 377 | + if ("//".Equals(Content)) |
259 | 378 | { |
260 | | - return 1; |
| 379 | + return true; |
261 | 380 | } |
262 | 381 |
|
263 | | - return (int) (TimeOffset - obj.TimeOffset); |
| 382 | + return false; |
264 | 383 | } |
265 | | - |
| 384 | + |
266 | 385 | public override string ToString() |
267 | 386 | { |
268 | | - return Timestamp + Content; |
| 387 | + return Timestamp.ToString(OutputFormatEnum.LRC) + Content; |
269 | 388 | } |
270 | 389 | } |
271 | 390 |
|
|
0 commit comments