Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the recording caret algorithm might calculate wrong position if time-tag is not order in the lyric. #1166

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ protected Lyric[] GetLyricsByMethodName(string methodName)
throw new MissingMethodException("Test method is not exist.");

return (Lyric[])theMethod.GetValue(this)!;

/*
var lyrics = theMethod.GetValue(this) as Lyric[] ?? Array.Empty<Lyric>();

foreach (var lyric in lyrics)
{
// because time-tag will not always sort by order, so we need to shuffle the time-tag in the list for testing.
lyric.TimeTags = TestCaseListHelper.Shuffle(lyric.TimeTags);
}

return lyrics;
*/
}
}
}
26 changes: 26 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseListHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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;

namespace osu.Game.Rulesets.Karaoke.Tests.Helper
{
public static class TestCaseListHelper
{
private static readonly Random rng = new();

/// <summary>
/// Random the order of the list.
/// </summary>
/// <param name="list"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IList<T> Shuffle<T>(IList<T> list)
{
// see: https://stackoverflow.com/a/4262134
return list.OrderBy(_ => rng.Next()).ToList();
}
}
}
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Karaoke.Tests/Utils/TimeTagsUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void TestSort(string[] timeTagTexts, string[] expectedTimeTags)
var timeTags = TestCaseTagHelper.ParseTimeTags(timeTagTexts);

var expected = TestCaseTagHelper.ParseTimeTags(expectedTimeTags);
var actual = TimeTagsUtils.Sort(timeTags);
var actual = TimeTagsUtils.Sort(timeTags).ToArray();
TimeTagAssert.ArePropertyEqual(expected, actual);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Extensions;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.CaretPosition.Algorithms
{
Expand Down Expand Up @@ -41,7 +43,7 @@ public override bool PositionMovable(TimeTagCaretPosition position)
if (previousLyric == null)
return null;

var timeTags = previousLyric.TimeTags.Where(timeTagMovable).ToArray();
var timeTags = sortTimeTag(previousLyric.TimeTags.Where(timeTagMovable)).ToArray();
var upTimeTag = timeTags.FirstOrDefault(x => x.Index >= currentTimeTag.Index)
?? timeTags.LastOrDefault();

Expand All @@ -65,7 +67,7 @@ public override bool PositionMovable(TimeTagCaretPosition position)
if (nextLyric == null)
return null;

var timeTags = nextLyric.TimeTags.Where(timeTagMovable).ToArray();
var timeTags = sortTimeTag(nextLyric.TimeTags.Where(timeTagMovable)).ToArray();
var downTimeTag = timeTags.FirstOrDefault(x => x.Index >= currentTimeTag.Index)
?? timeTags.LastOrDefault();

Expand All @@ -77,7 +79,7 @@ public override bool PositionMovable(TimeTagCaretPosition position)

public override TimeTagCaretPosition? MoveLeft(TimeTagCaretPosition currentPosition)
{
var timeTags = Lyrics.SelectMany(x => x.TimeTags).ToArray();
var timeTags = getAllSortedTimeTag();
var previousTimeTag = timeTags.GetPreviousMatch(currentPosition.TimeTag, timeTagMovable);
if (previousTimeTag == null)
return null;
Expand All @@ -87,7 +89,7 @@ public override bool PositionMovable(TimeTagCaretPosition position)

public override TimeTagCaretPosition? MoveRight(TimeTagCaretPosition currentPosition)
{
var timeTags = Lyrics.SelectMany(x => x.TimeTags).ToArray();
var timeTags = getAllSortedTimeTag();
var nextTimeTag = timeTags.GetNextMatch(currentPosition.TimeTag, timeTagMovable);
if (nextTimeTag == null)
return null;
Expand All @@ -97,7 +99,7 @@ public override bool PositionMovable(TimeTagCaretPosition position)

public override TimeTagCaretPosition? MoveToFirst()
{
var timeTags = Lyrics.SelectMany(x => x.TimeTags).ToArray();
var timeTags = getAllSortedTimeTag();
var firstTimeTag = timeTags.FirstOrDefault(timeTagMovable);
if (firstTimeTag == null)
return null;
Expand All @@ -107,7 +109,7 @@ public override bool PositionMovable(TimeTagCaretPosition position)

public override TimeTagCaretPosition? MoveToLast()
{
var timeTags = Lyrics.SelectMany(x => x.TimeTags).ToArray();
var timeTags = getAllSortedTimeTag();
var lastTimeTag = timeTags.LastOrDefault(timeTagMovable);
if (lastTimeTag == null)
return null;
Expand All @@ -123,6 +125,12 @@ public override bool PositionMovable(TimeTagCaretPosition position)
return targetTimeTag == null ? null : new TimeTagCaretPosition(lyric, targetTimeTag);
}

private IEnumerable<TimeTag> sortTimeTag(IEnumerable<TimeTag> timeTags)
=> TimeTagsUtils.Sort(timeTags);

private IEnumerable<TimeTag> getAllSortedTimeTag()
=> sortTimeTag(Lyrics.SelectMany(x => x.TimeTags));

private TimeTagCaretPosition? timeTagToPosition(TimeTag timeTag)
{
var lyric = timeTagInLyric(timeTag);
Expand Down
10 changes: 5 additions & 5 deletions osu.Game.Rulesets.Karaoke/Utils/TimeTagsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public static TimeTag GenerateCenterTimeTag(TimeTag startTimeTag, TimeTag endTim
/// </summary>
/// <param name="timeTags">Time tags</param>
/// <returns>Sorted time tags</returns>
public static IList<TimeTag> Sort(IEnumerable<TimeTag> timeTags)
public static IEnumerable<TimeTag> Sort(IEnumerable<TimeTag> timeTags)
{
return timeTags.OrderBy(x => x.Index)
.ThenBy(x => x.Time).ToArray();
.ThenBy(x => x.Time);
}

/// <summary>
Expand Down Expand Up @@ -177,7 +177,7 @@ IEnumerable<TimeTag> findSelfOverlapping()
}
}

return Sort(overlappingTimeTagList.Distinct());
return Sort(overlappingTimeTagList.Distinct()).ToArray();
}

/// <summary>
Expand All @@ -192,15 +192,15 @@ public static IList<TimeTag> FixOverlapping(IList<TimeTag> timeTags, GroupCheck
if (!timeTags.Any())
return timeTags;

var sortedTimeTags = Sort(timeTags);
var sortedTimeTags = Sort(timeTags).ToArray();
var groupedTimeTags = sortedTimeTags.GroupBy(x => x.Index.Index).ToArray();

var overlappingTimeTags = FindOverlapping(timeTags, other, self);
var validTimeTags = sortedTimeTags.Except(overlappingTimeTags);

foreach (var overlappingTimeTag in overlappingTimeTags)
{
int listIndex = sortedTimeTags.IndexOf(overlappingTimeTag);
int listIndex = Array.IndexOf(sortedTimeTags, overlappingTimeTag);
var timeTag = overlappingTimeTag.Index;

// fix self-overlapping
Expand Down