Skip to content
Merged
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
19 changes: 6 additions & 13 deletions RhythmGameUtilities.Tests/UtilitiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,18 @@ public void TestCalculateBeatBars()

var beatBars = Utilities.CalculateBeatBars(bpmChanges, resolution, timeSignature, true);

Assert.That(beatBars.Count, Is.EqualTo(440));
Assert.That(beatBars.Length, Is.EqualTo(440));
}

[Test]
public void TestFindPositionNearGivenTick()
{
var notes = new List<Note>
var notes = new Note[]
{
new() { Position = 768 },
new() { Position = 960 },
new() { Position = 1152 },
new() { Position = 1536 },
new() { Position = 1728 },
new() { Position = 1920 },
new() { Position = 2304 },
new() { Position = 2496 },
new() { Position = 2688 },
new() { Position = 3072 },
new() { Position = 3264 },
new() { Position = 768 }, new() { Position = 960 }, new() { Position = 1152 },
new() { Position = 1536 }, new() { Position = 1728 }, new() { Position = 1920 },
new() { Position = 2304 }, new() { Position = 2496 }, new() { Position = 2688 },
new() { Position = 3072 }, new() { Position = 3264 },
};

Assert.That(Utilities.FindPositionNearGivenTick(notes, 100), Is.Null);
Expand Down
11 changes: 6 additions & 5 deletions RhythmGameUtilities/Scripts/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,35 @@ public static int RoundUpToTheNearestMultiplier(int value, int multiplier)
return UtilitiesInternal.RoundUpToTheNearestMultiplier(value, multiplier);
}

public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpmChanges, int resolution = 192, int ts = 4,
public static BeatBar[] CalculateBeatBars(Dictionary<int, int> bpmChanges, int resolution = 192, int ts = 4,
bool includeHalfNotes = true)
{
var beatBars = new List<BeatBar>();

var ptrArray = UtilitiesInternal.CalculateBeatBarsInternal(bpmChanges.Keys.ToArray(),
bpmChanges.Values.ToArray(), bpmChanges.Count, resolution, ts, includeHalfNotes,
out var size);

var beatBarSize = Marshal.SizeOf(typeof(BeatBar));

var beatBars = new BeatBar[size];

for (var i = 0; i < size; i += 1)
{
var beatBarSizePtr = new IntPtr(ptrArray.ToInt64() + beatBarSize * i);
var beatBar = Marshal.PtrToStructure<BeatBar>(beatBarSizePtr);

beatBars.Add(beatBar);
beatBars[i] = beatBar;
}

Marshal.FreeHGlobal(ptrArray);

return beatBars;
}

public static Note? FindPositionNearGivenTick(List<Note> notes, int tick, int delta = 50)
public static Note? FindPositionNearGivenTick(Note[] notes, int tick, int delta = 50)
{
var left = 0;
var right = notes.Count - 1;
var right = notes.Length - 1;

while (left <= right)
{
Expand Down
19 changes: 6 additions & 13 deletions UnityPackage/Editor/Tests/UtilitiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,18 @@ public void TestCalculateBeatBars()

var beatBars = Utilities.CalculateBeatBars(bpmChanges, resolution, timeSignature, true);

Assert.That(beatBars.Count, Is.EqualTo(440));
Assert.That(beatBars.Length, Is.EqualTo(440));
}

[Test]
public void TestFindPositionNearGivenTick()
{
var notes = new List<Note>
var notes = new Note[]
{
new() { Position = 768 },
new() { Position = 960 },
new() { Position = 1152 },
new() { Position = 1536 },
new() { Position = 1728 },
new() { Position = 1920 },
new() { Position = 2304 },
new() { Position = 2496 },
new() { Position = 2688 },
new() { Position = 3072 },
new() { Position = 3264 },
new() { Position = 768 }, new() { Position = 960 }, new() { Position = 1152 },
new() { Position = 1536 }, new() { Position = 1728 }, new() { Position = 1920 },
new() { Position = 2304 }, new() { Position = 2496 }, new() { Position = 2688 },
new() { Position = 3072 }, new() { Position = 3264 },
};

Assert.That(Utilities.FindPositionNearGivenTick(notes, 100), Is.Null);
Expand Down
11 changes: 6 additions & 5 deletions UnityPackage/Scripts/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,35 @@ public static int RoundUpToTheNearestMultiplier(int value, int multiplier)
return UtilitiesInternal.RoundUpToTheNearestMultiplier(value, multiplier);
}

public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpmChanges, int resolution = 192, int ts = 4,
public static BeatBar[] CalculateBeatBars(Dictionary<int, int> bpmChanges, int resolution = 192, int ts = 4,
bool includeHalfNotes = true)
{
var beatBars = new List<BeatBar>();

var ptrArray = UtilitiesInternal.CalculateBeatBarsInternal(bpmChanges.Keys.ToArray(),
bpmChanges.Values.ToArray(), bpmChanges.Count, resolution, ts, includeHalfNotes,
out var size);

var beatBarSize = Marshal.SizeOf(typeof(BeatBar));

var beatBars = new BeatBar[size];

for (var i = 0; i < size; i += 1)
{
var beatBarSizePtr = new IntPtr(ptrArray.ToInt64() + beatBarSize * i);
var beatBar = Marshal.PtrToStructure<BeatBar>(beatBarSizePtr);

beatBars.Add(beatBar);
beatBars[i] = beatBar;
}

Marshal.FreeHGlobal(ptrArray);

return beatBars;
}

public static Note? FindPositionNearGivenTick(List<Note> notes, int tick, int delta = 50)
public static Note? FindPositionNearGivenTick(Note[] notes, int tick, int delta = 50)
{
var left = 0;
var right = notes.Count - 1;
var right = notes.Length - 1;

while (left <= right)
{
Expand Down
Loading