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
2 changes: 1 addition & 1 deletion RhythmGameUtilities.Tests/Mocks/song.json

Large diffs are not rendered by default.

46 changes: 35 additions & 11 deletions RhythmGameUtilities.Tests/UtilitiesTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

Expand All @@ -22,18 +23,19 @@ public void TestConvertSecondsToTicks()
const int seconds = 5;
const int resolution = 192;

var bpmChanges = new Dictionary<int, int>
{
{ 0, 88000 },
{ 3840, 112000 },
{ 9984, 89600 },
{ 22272, 112000 },
{ 33792, 111500 },
{ 34560, 112000 },
{ 42240, 111980 }
};

Assert.That(
Utilities.ConvertSecondsToTicks(seconds, resolution,
new Dictionary<int, int>
{
{ 0, 88000 },
{ 3840, 112000 },
{ 9984, 89600 },
{ 22272, 112000 },
{ 33792, 111500 },
{ 34560, 112000 },
{ 42240, 111980 }
}), Is.EqualTo(1408));
Utilities.ConvertSecondsToTicks(seconds, resolution, bpmChanges), Is.EqualTo(1408));
}

[Test]
Expand Down Expand Up @@ -66,6 +68,28 @@ public void TestInverseLerp()
Assert.That(Utilities.InverseLerp(0, 10, 10), Is.EqualTo(1));
}

[Test]
public void TestCalculateBeatBars()
{
const int resolution = 192;
const int timeSignature = 4;

var bpmChanges = new Dictionary<int, int>
{
{ 0, 88000 },
{ 3840, 112000 },
{ 9984, 89600 },
{ 22272, 112000 },
{ 33792, 111500 },
{ 34560, 112000 },
{ 42240, 111980 }
};

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

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

}

}
56 changes: 21 additions & 35 deletions RhythmGameUtilities/Scripts/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public static extern int ConvertSecondsToTicksInternal(float seconds, int resolu
#endif
public static extern float InverseLerp(float a, float b, float v);

#if WINDOWS_BUILD || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
[DllImport("libRhythmGameUtilities.dll", CallingConvention = CallingConvention.Cdecl)]
#elif MACOS_BUILD || UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
[DllImport("libRhythmGameUtilities.dylib", CallingConvention = CallingConvention.Cdecl)]
#elif LINUX_BUILD || UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
[DllImport("libRhythmGameUtilities.so", CallingConvention = CallingConvention.Cdecl)]
#endif
public static extern IntPtr CalculateBeatBarsInternal(int[] bpmChangesKeys,
int[] bpmChangesValues, int bpmChangesSize, int resolution, int ts,
bool includeHalfNotes, out int size);

}

public static class Utilities
Expand Down Expand Up @@ -107,46 +118,21 @@ public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpmChanges, i
{
var beatBars = new List<BeatBar>();

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

foreach (var (startTick, endTick) in keyValuePairs)
{
for (var tick = startTick; tick <= endTick; tick += resolution)
{
beatBars.Add(new BeatBar
{
Position = tick, BPM = bpmChanges[startTick], TimeSignature = new[] { ts }
});

if (includeHalfNotes && tick != endTick)
{
beatBars.Add(new BeatBar
{
Position = tick + resolution / 2,
BPM = bpmChanges[startTick],
TimeSignature = new[] { ts }
});
}
}
}
var beatBarSize = Marshal.SizeOf(typeof(BeatBar));

return beatBars;
}

public static List<Tuple<T, T>> GenerateAdjacentKeyPairs<T>(Dictionary<T, T> dictionary)
{
var keys = dictionary.Keys.ToList();

keys.Sort();

var adjacentKeyPairs = new List<Tuple<T, T>>();

for (var i = 0; i < keys.Count - 1; i += 1)
for (var i = 0; i < size; i += 1)
{
adjacentKeyPairs.Add(new Tuple<T, T>(keys[i], keys[i + 1]));
var beatBarSizePtr = new IntPtr(ptrArray.ToInt64() + beatBarSize * i);
var beatBar = Marshal.PtrToStructure<BeatBar>(beatBarSizePtr);

beatBars.Add(beatBar);
}

return adjacentKeyPairs;
return beatBars;
}

}
Expand Down
2 changes: 0 additions & 2 deletions RhythmGameUtilities/Structs/BeatBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public struct BeatBar

public int BPM;

public int[] TimeSignature;

}

}
46 changes: 35 additions & 11 deletions UnityPackage/Editor/Tests/UtilitiesTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

Expand All @@ -22,18 +23,19 @@ public void TestConvertSecondsToTicks()
const int seconds = 5;
const int resolution = 192;

var bpmChanges = new Dictionary<int, int>
{
{ 0, 88000 },
{ 3840, 112000 },
{ 9984, 89600 },
{ 22272, 112000 },
{ 33792, 111500 },
{ 34560, 112000 },
{ 42240, 111980 }
};

Assert.That(
Utilities.ConvertSecondsToTicks(seconds, resolution,
new Dictionary<int, int>
{
{ 0, 88000 },
{ 3840, 112000 },
{ 9984, 89600 },
{ 22272, 112000 },
{ 33792, 111500 },
{ 34560, 112000 },
{ 42240, 111980 }
}), Is.EqualTo(1408));
Utilities.ConvertSecondsToTicks(seconds, resolution, bpmChanges), Is.EqualTo(1408));
}

[Test]
Expand Down Expand Up @@ -66,6 +68,28 @@ public void TestInverseLerp()
Assert.That(Utilities.InverseLerp(0, 10, 10), Is.EqualTo(1));
}

[Test]
public void TestCalculateBeatBars()
{
const int resolution = 192;
const int timeSignature = 4;

var bpmChanges = new Dictionary<int, int>
{
{ 0, 88000 },
{ 3840, 112000 },
{ 9984, 89600 },
{ 22272, 112000 },
{ 33792, 111500 },
{ 34560, 112000 },
{ 42240, 111980 }
};

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

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

}

}
56 changes: 21 additions & 35 deletions UnityPackage/Scripts/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public static extern int ConvertSecondsToTicksInternal(float seconds, int resolu
#endif
public static extern float InverseLerp(float a, float b, float v);

#if WINDOWS_BUILD || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
[DllImport("libRhythmGameUtilities.dll", CallingConvention = CallingConvention.Cdecl)]
#elif MACOS_BUILD || UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
[DllImport("libRhythmGameUtilities.dylib", CallingConvention = CallingConvention.Cdecl)]
#elif LINUX_BUILD || UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
[DllImport("libRhythmGameUtilities.so", CallingConvention = CallingConvention.Cdecl)]
#endif
public static extern IntPtr CalculateBeatBarsInternal(int[] bpmChangesKeys,
int[] bpmChangesValues, int bpmChangesSize, int resolution, int ts,
bool includeHalfNotes, out int size);

}

public static class Utilities
Expand Down Expand Up @@ -107,46 +118,21 @@ public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpmChanges, i
{
var beatBars = new List<BeatBar>();

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

foreach (var (startTick, endTick) in keyValuePairs)
{
for (var tick = startTick; tick <= endTick; tick += resolution)
{
beatBars.Add(new BeatBar
{
Position = tick, BPM = bpmChanges[startTick], TimeSignature = new[] { ts }
});

if (includeHalfNotes && tick != endTick)
{
beatBars.Add(new BeatBar
{
Position = tick + resolution / 2,
BPM = bpmChanges[startTick],
TimeSignature = new[] { ts }
});
}
}
}
var beatBarSize = Marshal.SizeOf(typeof(BeatBar));

return beatBars;
}

public static List<Tuple<T, T>> GenerateAdjacentKeyPairs<T>(Dictionary<T, T> dictionary)
{
var keys = dictionary.Keys.ToList();

keys.Sort();

var adjacentKeyPairs = new List<Tuple<T, T>>();

for (var i = 0; i < keys.Count - 1; i += 1)
for (var i = 0; i < size; i += 1)
{
adjacentKeyPairs.Add(new Tuple<T, T>(keys[i], keys[i + 1]));
var beatBarSizePtr = new IntPtr(ptrArray.ToInt64() + beatBarSize * i);
var beatBar = Marshal.PtrToStructure<BeatBar>(beatBarSizePtr);

beatBars.Add(beatBar);
}

return adjacentKeyPairs;
return beatBars;
}

}
Expand Down
2 changes: 0 additions & 2 deletions UnityPackage/Structs/BeatBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public struct BeatBar

public int BPM;

public int[] TimeSignature;

}

}
9 changes: 9 additions & 0 deletions includes/RhythmGameUtilities/Structs/BeatBar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

struct BeatBar
{

int Position;

int BPM;
};
57 changes: 57 additions & 0 deletions includes/RhythmGameUtilities/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "Utilities.h"

#include "Structs/BeatBar.h"
#include "Structs/Note.h"

const float SECONDS_PER_MINUTE = 60.0f;
Expand Down Expand Up @@ -111,6 +112,62 @@ std::vector<std::string> Split(const char *contents, const char delimiter)
return parts;
}

std::vector<BeatBar> CalculateBeatBars(std::map<int, int> bpmChanges,
int resolution, int ts,
bool includeHalfNotes)
{
std::vector<BeatBar> beatBars;

auto keyValuePairs = GenerateAdjacentKeyPairs(bpmChanges);

for (const auto &keyValuePair : keyValuePairs)
{
auto startTick = std::get<0>(keyValuePair);
auto endTick = std::get<1>(keyValuePair);

for (auto tick = startTick; tick <= endTick; tick += resolution)
{
beatBars.push_back(
{.Position = tick, .BPM = bpmChanges[startTick]});

if (includeHalfNotes && tick != endTick)
{
beatBars.push_back({.Position = tick + resolution / 2,
.BPM = bpmChanges[startTick]});
}
}
}

return beatBars;
}

BeatBar *CalculateBeatBarsInternal(int *bpmChangesKeys, int *bpmChangesValues,
int bpmChangesSize, int resolution, int ts,
bool includeHalfNotes, int *outSize)
{
auto bpmChanges = std::map<int, int>();

for (auto i = 0; i < bpmChangesSize; i += 1)
{
bpmChanges[bpmChangesKeys[i]] = bpmChangesValues[i];
}

auto internalBeatBars =
CalculateBeatBars(bpmChanges, resolution, ts, includeHalfNotes);

*outSize = internalBeatBars.size();

auto beatBars =
(BeatBar *)malloc(internalBeatBars.size() * sizeof(BeatBar));

for (auto i = 0; i < internalBeatBars.size(); i += 1)
{
beatBars[i] = internalBeatBars[i];
}

return beatBars;
}

std::vector<std::tuple<int, int>>
GenerateAdjacentKeyPairs(std::map<int, int> keyValuePairs)
{
Expand Down
Loading