Skip to content

Commit

Permalink
Implement combine note.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Dec 13, 2020
1 parent 285eb0c commit 0a294ae
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
39 changes: 36 additions & 3 deletions osu.Game.Rulesets.Karaoke.Tests/Utils/NoteUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ public void TestSeparateNoteTime(double[] time, double percentage, double[] firs
}

[Test]
public void TestSeparateLyricOtherPtoperty()
public void TestSeparateNoteOtherProperty()
{
const double percentage = 0.3;
var lyric = new Lyric();

const double percentage = 0.3;
var note = new Note
{
StartTime = 1000,
Duration = 2000,
StartIndex = 1,
EndIndex = 2,
Text = "ka",
Singers = new int[] { 0 },
Singers = new[] { 0 },
Display = false,
Tone = new Tone(-1, true),
ParentLyric = lyric
Expand Down Expand Up @@ -112,5 +112,38 @@ static void testRemainProperty(Note expect, Note actual)
Assert.AreEqual(expect.ParentLyric, actual.ParentLyric);
}
}

[TestCase(new double[] { 1000, 1000 }, new double[] { 2000, 4000 }, new double[] { 1000, 5000 })]
[TestCase(new double[] { 1000, 2500 }, new double[] { 3500, 2500 }, new double[] { 1000, 5000 })]
[TestCase(new double[] { 1000, 0 }, new double[] { 1000, 0 }, new double[] { 1000, 0 })] // it's ok to combine if duration is 0.
public void TestCombineNoteTime(double[] firstTime, double[] secondTime, double[] actualTime)
{
const int start_index = 3;
const int end_index = 5;

var lyric = new Lyric();

var firstNote = new Note
{
StartIndex = start_index,
EndIndex = end_index,
ParentLyric = lyric,
StartTime = firstTime[0],
Duration = firstTime[1],
};

var secondNote = new Note
{
StartIndex = start_index,
EndIndex = end_index,
ParentLyric = lyric,
StartTime = secondTime[0],
Duration = secondTime[1],
};

var combineNote = NoteUtils.CombineNote(firstNote, secondNote);
Assert.AreEqual(combineNote.StartTime, actualTime[0]);
Assert.AreEqual(combineNote.Duration, actualTime[1]);
}
}
}
1 change: 0 additions & 1 deletion osu.Game.Rulesets.Karaoke/Objects/Note.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 Newtonsoft.Json;
using osu.Framework.Bindables;
using osu.Game.Rulesets.Judgements;
Expand Down
41 changes: 27 additions & 14 deletions osu.Game.Rulesets.Karaoke/Utils/NoteUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace osu.Game.Rulesets.Karaoke.Utils
{
public static class NoteUtils
{
public static Note SliceNote(Note note, double startPercentage, double durationPercentage)
public static Note SliceNote(Note note, double startPercentage, double durationPercentage)
{
if (startPercentage < 0 || startPercentage + durationPercentage > 1)
throw new ArgumentOutOfRangeException($"{nameof(Note)} cannot assign split range of start from {startPercentage} and duration {durationPercentage}");
Expand All @@ -18,6 +18,7 @@ public static Note SliceNote(Note note, double startPercentage, double duration

return copyByTime(note, startTime, duration);
}

public static Tuple<Note, Note> SplitNote(Note note, double percentage = 0.5)
{
if (percentage < 0 || percentage > 1)
Expand All @@ -38,25 +39,37 @@ public static Tuple<Note, Note> SplitNote(Note note, double percentage = 0.5)
return new Tuple<Note, Note>(firstNote, secondNote);
}

private static Note copyByTime(Note oritinNote, double startTime, double duration)
public static Note CombineNote(Note firstLyric, Note secondLyric)
{
if (firstLyric.ParentLyric != secondLyric.ParentLyric)
throw new InvalidOperationException($"{nameof(firstLyric.ParentLyric)} and {nameof(secondLyric.ParentLyric)} should be same.");

if (firstLyric.StartIndex != secondLyric.StartIndex)
throw new InvalidOperationException($"{nameof(firstLyric.StartIndex)} and {nameof(secondLyric.StartIndex)} should be same.");

if (firstLyric.EndIndex != secondLyric.EndIndex)
throw new InvalidOperationException($"{nameof(firstLyric.EndIndex)} and {nameof(secondLyric.EndIndex)} should be same.");

var startTime = Math.Min(firstLyric.StartTime, secondLyric.StartTime);
var endTime = Math.Max(firstLyric.EndTime, secondLyric.EndTime);

return copyByTime(firstLyric, startTime, endTime - startTime);
}

private static Note copyByTime(Note originNote, double startTime, double duration)
{
return new Note
{
StartTime = startTime,
Duration = duration,
StartIndex = oritinNote.StartIndex,
EndIndex = oritinNote.EndIndex,
Text = oritinNote.Text,
Singers = oritinNote.Singers?.Clone() as int[],
Display = oritinNote.Display,
Tone = oritinNote.Tone,
ParentLyric = oritinNote.ParentLyric
StartIndex = originNote.StartIndex,
EndIndex = originNote.EndIndex,
Text = originNote.Text,
Singers = originNote.Singers?.Clone() as int[],
Display = originNote.Display,
Tone = originNote.Tone,
ParentLyric = originNote.ParentLyric
};
}

public static Note CombineNote(Note firstLyric, Note secondLyric)
{
return null;
}
}
}

0 comments on commit 0a294ae

Please sign in to comment.