Update summaries, Use spans

pull/8381/head
1hitsong 3 years ago
parent b1771f07e9
commit 35399ce8fe

@ -12,17 +12,17 @@ public class LyricLine
/// <param name="start">The lyric start time in ticks.</param> /// <param name="start">The lyric start time in ticks.</param>
public LyricLine(string text, long? start = null) public LyricLine(string text, long? start = null)
{ {
Start = start;
Text = text; Text = text;
Start = start;
} }
/// <summary> /// <summary>
/// Gets the start time in ticks. /// Gets the text of this lyric line.
/// </summary> /// </summary>
public long? Start { get; } public string Text { get; }
/// <summary> /// <summary>
/// Gets the text. /// Gets the start time in ticks.
/// </summary> /// </summary>
public string Text { get; } public long? Start { get; }
} }

@ -8,47 +8,47 @@ namespace MediaBrowser.Controller.Lyrics;
public class LyricMetadata public class LyricMetadata
{ {
/// <summary> /// <summary>
/// Gets or sets Artist - The song artist. /// Gets or sets the song artist.
/// </summary> /// </summary>
public string? Artist { get; set; } public string? Artist { get; set; }
/// <summary> /// <summary>
/// Gets or sets Album - The album this song is on. /// Gets or sets the album this song is on.
/// </summary> /// </summary>
public string? Album { get; set; } public string? Album { get; set; }
/// <summary> /// <summary>
/// Gets or sets Title - The title of the song. /// Gets or sets the title of the song.
/// </summary> /// </summary>
public string? Title { get; set; } public string? Title { get; set; }
/// <summary> /// <summary>
/// Gets or sets Author - Creator of the lyric data. /// Gets or sets the author of the lyric data.
/// </summary> /// </summary>
public string? Author { get; set; } public string? Author { get; set; }
/// <summary> /// <summary>
/// Gets or sets Length - How long the song is. /// Gets or sets the length of the song in ticks.
/// </summary> /// </summary>
public long? Length { get; set; } public long? Length { get; set; }
/// <summary> /// <summary>
/// Gets or sets By - Creator of the LRC file. /// Gets or sets who the LRC file was created by.
/// </summary> /// </summary>
public string? By { get; set; } public string? By { get; set; }
/// <summary> /// <summary>
/// Gets or sets Offset - Offset:+/- Timestamp adjustment in milliseconds. /// Gets or sets the lyric offset compared to audio in ticks.
/// </summary> /// </summary>
public long? Offset { get; set; } public long? Offset { get; set; }
/// <summary> /// <summary>
/// Gets or sets Creator - The Software used to create the LRC file. /// Gets or sets the software used to create the LRC file.
/// </summary> /// </summary>
public string? Creator { get; set; } public string? Creator { get; set; }
/// <summary> /// <summary>
/// Gets or sets Version - The version of the Creator used. /// Gets or sets the version of the creator used.
/// </summary> /// </summary>
public string? Version { get; set; } public string? Version { get; set; }
} }

@ -9,12 +9,12 @@ namespace MediaBrowser.Controller.Lyrics;
public class LyricResponse public class LyricResponse
{ {
/// <summary> /// <summary>
/// Gets or sets Metadata. /// Gets or sets Metadata for the lyrics.
/// </summary> /// </summary>
public LyricMetadata Metadata { get; set; } = new(); public LyricMetadata Metadata { get; set; } = new();
/// <summary> /// <summary>
/// Gets or sets Lyrics. /// Gets or sets a collection of individual lyric lines.
/// </summary> /// </summary>
public IReadOnlyList<LyricLine> Lyrics { get; set; } = Array.Empty<LyricLine>(); public IReadOnlyList<LyricLine> Lyrics { get; set; } = Array.Empty<LyricLine>();
} }

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using LrcParser.Model; using LrcParser.Model;
using LrcParser.Parser; using LrcParser.Parser;
@ -59,7 +60,7 @@ public class LrcLyricProvider : ILyricProvider
} }
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath); string lrcFileContent = File.ReadAllText(lyricFilePath);
Song lyricData; Song lyricData;
@ -84,25 +85,24 @@ public class LrcLyricProvider : ILyricProvider
foreach (string metaDataRow in metaDataRows) foreach (string metaDataRow in metaDataRows)
{ {
if (!metaDataRow.Contains(':', StringComparison.OrdinalIgnoreCase)) var index = metaDataRow.IndexOf(':', StringComparison.OrdinalIgnoreCase);
if (index == -1)
{ {
continue; continue;
} }
string[] metaDataField = metaDataRow.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
// Remove square bracket before field name, and after field value // Remove square bracket before field name, and after field value
// Example 1: [au: 1hitsong] // Example 1: [au: 1hitsong]
// Example 2: [ar: Calabrese] // Example 2: [ar: Calabrese]
string metaDataFieldName = metaDataField[0][1..]; var metaDataFieldNameSpan = metaDataRow.AsSpan(1, index - 1).Trim();
string metaDataFieldValue = metaDataField[1][..^1]; var metaDataFieldValueSpan = metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim();
if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue)) if (metaDataFieldValueSpan.IsEmpty || metaDataFieldValueSpan.IsEmpty)
{ {
continue; continue;
} }
fileMetaData[metaDataFieldName] = metaDataFieldValue; fileMetaData[metaDataFieldNameSpan.ToString()] = metaDataFieldValueSpan.ToString();
} }
if (sortedLyricData.Count == 0) if (sortedLyricData.Count == 0)

@ -1,4 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics; using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers; using MediaBrowser.Controller.Resolvers;
@ -36,18 +38,18 @@ public class TxtLyricProvider : ILyricProvider
return null; return null;
} }
string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath); string[] lyricTextLines = File.ReadAllLines(lyricFilePath);
if (lyricTextLines.Length == 0) if (lyricTextLines.Length == 0)
{ {
return null; return null;
} }
List<LyricLine> lyricList = new(lyricTextLines.Length); LyricLine[] lyricList = new LyricLine[lyricTextLines.Length];
foreach (string lyricTextLine in lyricTextLines) for (int lyricLine = 0; lyricLine < lyricTextLines.Length; lyricLine++)
{ {
lyricList.Add(new LyricLine(lyricTextLine)); lyricList[lyricLine] = new LyricLine(lyricTextLines[lyricLine]);
} }
return new LyricResponse { Lyrics = lyricList }; return new LyricResponse { Lyrics = lyricList };

Loading…
Cancel
Save