-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1339 from andy840119/switch-the-lrc-parser
Switch the lrc parser
- Loading branch information
Showing
5 changed files
with
83 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using LyricMaker.Extensions; | ||
using LyricMaker.Parser; | ||
using osu.Framework.Graphics.Sprites; | ||
using LrcParser.Model; | ||
using osu.Game.Beatmaps; | ||
using osu.Game.Beatmaps.Formats; | ||
using osu.Game.IO; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
using Lyric = osu.Game.Rulesets.Karaoke.Objects.Lyric; | ||
using RubyTag = osu.Game.Rulesets.Karaoke.Objects.RubyTag; | ||
using TextIndex = osu.Framework.Graphics.Sprites.TextIndex; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.Beatmaps.Formats | ||
{ | ||
|
@@ -28,67 +28,50 @@ protected override void ParseStreamInto(LineBufferedReader stream, Beatmap outpu | |
output.HitObjects.Clear(); | ||
|
||
string lyricText = stream.ReadToEnd(); | ||
var result = new LrcParser().Decode(lyricText); | ||
var result = new LrcParser.Parser.Lrc.LrcParser().Decode(lyricText); | ||
|
||
// Convert line | ||
for (int i = 0; i < result.Lines.Length; i++) | ||
foreach (var lrcLyric in result.Lyrics) | ||
{ | ||
// Empty line should not be imported | ||
var line = result.Lines[i]; | ||
if (string.IsNullOrEmpty(line.Text)) | ||
continue; | ||
var lrcTimeTags = lrcLyric.TimeTags.Select(convertTimeTag).ToList(); | ||
var lrcRubies = lrcLyric.RubyTags.Select(convertRubyTag).ToArray(); | ||
|
||
try | ||
double? startTime = lrcTimeTags.Select(x => x.Time).Min(); | ||
double? endTime = lrcTimeTags.Select(x => x.Time).Max(); | ||
double? duration = endTime - startTime; | ||
|
||
var lyric = new Lyric | ||
{ | ||
// todo : check list ls sorted by time. | ||
var lrcTimeTag = line.TimeTags; | ||
var timeTags = line.TimeTags.Where(x => x.Check).ToDictionary(k => | ||
{ | ||
int index = (Array.IndexOf(lrcTimeTag, k) - 1) / 2; | ||
var state = (Array.IndexOf(lrcTimeTag, k) - 1) % 2 == 0 ? TextIndex.IndexState.Start : TextIndex.IndexState.End; | ||
return new TextIndex(index, state); | ||
}, v => (double)v.Time); | ||
ID = output.HitObjects.Count, // id is star from zero. | ||
Order = output.HitObjects.Count + 1, // should create default order. | ||
Text = lrcLyric.Text, | ||
// Start time and end time should be re-assigned | ||
StartTime = startTime ?? 0, | ||
Duration = duration ?? 0, | ||
TimeTags = lrcTimeTags, | ||
RubyTags = lrcRubies | ||
}; | ||
lyric.InitialWorkingTime(); | ||
output.HitObjects.Add(lyric); | ||
} | ||
|
||
double startTime = timeTags.FirstOrDefault(x => x.Value > 0).Value; | ||
double duration = timeTags.LastOrDefault(x => x.Value > 0).Value - startTime; | ||
static TimeTag convertTimeTag(KeyValuePair<LrcParser.Model.TextIndex, int?> timeTag) | ||
=> new(convertTextIndex(timeTag.Key), timeTag.Value); | ||
|
||
var lyric = new Lyric | ||
{ | ||
ID = output.HitObjects.Count, // id is star from zero. | ||
Order = output.HitObjects.Count + 1, // should create default order. | ||
Text = line.Text, | ||
// Start time and end time should be re-assigned | ||
StartTime = startTime, | ||
Duration = duration, | ||
TimeTags = ToTimeTagList(timeTags), | ||
RubyTags = result.QueryRubies(line.Text).Select(ruby => new RubyTag | ||
{ | ||
Text = ruby.Ruby.Ruby, | ||
StartIndex = ruby.StartIndex, | ||
EndIndex = ruby.EndIndex | ||
}).ToArray() | ||
}; | ||
lyric.InitialWorkingTime(); | ||
output.HitObjects.Add(lyric); | ||
} | ||
catch (Exception ex) | ||
{ | ||
string message = $"Parsing lyric '{line.Text}' got error in line:{i}" + | ||
"Please check time tag should be ordered and not duplicated." + | ||
"Then re-import again."; | ||
throw new FormatException(message, ex); | ||
} | ||
static TextIndex convertTextIndex(LrcParser.Model.TextIndex textIndex) | ||
{ | ||
int index = textIndex.Index; | ||
var state = textIndex.State == IndexState.Start ? TextIndex.IndexState.Start : TextIndex.IndexState.End; | ||
|
||
return new TextIndex(index, state); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Convert dictionary to list of time tags. | ||
/// </summary> | ||
/// <param name="dictionary">Dictionary.</param> | ||
/// <returns>Time tags</returns> | ||
internal static TimeTag[] ToTimeTagList(IReadOnlyDictionary<TextIndex, double> dictionary) | ||
{ | ||
return dictionary.Select(d => new TimeTag(d.Key, d.Value)).ToArray(); | ||
static RubyTag convertRubyTag(LrcParser.Model.RubyTag rubyTag) | ||
=> new() | ||
{ | ||
Text = rubyTag.Text, | ||
StartIndex = rubyTag.StartIndex, | ||
EndIndex = rubyTag.EndIndex | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters