Skip to content

Commit a56471b

Browse files
authored
Merge pull request #80 from neogeek/feature/is-on-beat-update
[feat] Added delta param to IsOnTheBeat method.
2 parents 56d7d1f + 7516681 commit a56471b

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

Documentation/API/Utilities/IsOnTheBeat.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ using System;
99
using RhythmGameUtilities;
1010

1111
const int bpm = 120;
12-
const int currentTime = 10;
12+
const float currentTime = 10f;
13+
const float delta = 0.05f;
1314

14-
if (Utilities.IsOnTheBeat(bpm, currentTime))
15+
if (Utilities.IsOnTheBeat(bpm, currentTime, delta))
1516
{
1617
Console.WriteLine("Is on the beat!");
1718
}
@@ -29,9 +30,10 @@ using namespace RhythmGameUtilities;
2930
int main()
3031
{
3132
const int bpm = 120;
32-
const int currentTime = 10;
33+
const float currentTime = 10f;
34+
const float delta = 0.05f;
3335

34-
if (IsOnTheBeat(bpm, currentTime))
36+
if (IsOnTheBeat(bpm, currentTime, delta))
3537
{
3638
std::cout << "Is on the beat!" << std::endl;
3739
}

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,10 @@ using System;
773773
using RhythmGameUtilities;
774774

775775
const int bpm = 120;
776-
const int currentTime = 10;
776+
const float currentTime = 10f;
777+
const float delta = 0.05f;
777778

778-
if (Utilities.IsOnTheBeat(bpm, currentTime))
779+
if (Utilities.IsOnTheBeat(bpm, currentTime, delta))
779780
{
780781
Console.WriteLine("Is on the beat!");
781782
}
@@ -793,9 +794,10 @@ using namespace RhythmGameUtilities;
793794
int main()
794795
{
795796
const int bpm = 120;
796-
const int currentTime = 10;
797+
const float currentTime = 10f;
798+
const float delta = 0.05f;
797799

798-
if (IsOnTheBeat(bpm, currentTime))
800+
if (IsOnTheBeat(bpm, currentTime, delta))
799801
{
800802
std::cout << "Is on the beat!" << std::endl;
801803
}

RhythmGameUtilities/Scripts/Utilities.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static extern int ConvertSecondsToTicksInternal(float seconds, int resolu
3535
#elif LINUX_BUILD || UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
3636
[DllImport("libRhythmGameUtilities.so", CallingConvention = CallingConvention.Cdecl)]
3737
#endif
38-
public static extern bool IsOnTheBeat(int bpm, float currentTime);
38+
public static extern bool IsOnTheBeat(int bpm, float currentTime, float delta);
3939

4040
#if WINDOWS_BUILD || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
4141
[DllImport("libRhythmGameUtilities.dll", CallingConvention = CallingConvention.Cdecl)]
@@ -103,9 +103,10 @@ public static int ConvertSecondsToTicks(float seconds, int resolution, Dictionar
103103
///
104104
/// <param name="bpm">The base BPM for a song.</param>
105105
/// <param name="currentTime">A timestamp to compare to the BPM.</param>
106-
public static bool IsOnTheBeat(int bpm, float currentTime)
106+
/// <param name="delta">The plus/minus delta to test the current time against.</param>
107+
public static bool IsOnTheBeat(int bpm, float currentTime, float delta = 0.05f)
107108
{
108-
return UtilitiesInternal.IsOnTheBeat(bpm, currentTime);
109+
return UtilitiesInternal.IsOnTheBeat(bpm, currentTime, delta);
109110
}
110111

111112
/// <summary>

UnityPackage/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,10 @@ using System;
773773
using RhythmGameUtilities;
774774

775775
const int bpm = 120;
776-
const int currentTime = 10;
776+
const float currentTime = 10f;
777+
const float delta = 0.05f;
777778

778-
if (Utilities.IsOnTheBeat(bpm, currentTime))
779+
if (Utilities.IsOnTheBeat(bpm, currentTime, delta))
779780
{
780781
Console.WriteLine("Is on the beat!");
781782
}
@@ -793,9 +794,10 @@ using namespace RhythmGameUtilities;
793794
int main()
794795
{
795796
const int bpm = 120;
796-
const int currentTime = 10;
797+
const float currentTime = 10f;
798+
const float delta = 0.05f;
797799

798-
if (IsOnTheBeat(bpm, currentTime))
800+
if (IsOnTheBeat(bpm, currentTime, delta))
799801
{
800802
std::cout << "Is on the beat!" << std::endl;
801803
}

UnityPackage/Scripts/Utilities.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static extern int ConvertSecondsToTicksInternal(float seconds, int resolu
3535
#elif LINUX_BUILD || UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
3636
[DllImport("libRhythmGameUtilities.so", CallingConvention = CallingConvention.Cdecl)]
3737
#endif
38-
public static extern bool IsOnTheBeat(int bpm, float currentTime);
38+
public static extern bool IsOnTheBeat(int bpm, float currentTime, float delta);
3939

4040
#if WINDOWS_BUILD || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
4141
[DllImport("libRhythmGameUtilities.dll", CallingConvention = CallingConvention.Cdecl)]
@@ -103,9 +103,10 @@ public static int ConvertSecondsToTicks(float seconds, int resolution, Dictionar
103103
///
104104
/// <param name="bpm">The base BPM for a song.</param>
105105
/// <param name="currentTime">A timestamp to compare to the BPM.</param>
106-
public static bool IsOnTheBeat(int bpm, float currentTime)
106+
/// <param name="delta">The plus/minus delta to test the current time against.</param>
107+
public static bool IsOnTheBeat(int bpm, float currentTime, float delta = 0.05f)
107108
{
108-
return UtilitiesInternal.IsOnTheBeat(bpm, currentTime);
109+
return UtilitiesInternal.IsOnTheBeat(bpm, currentTime, delta);
109110
}
110111

111112
/// <summary>

include/RhythmGameUtilities/Utilities.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,20 @@ extern "C"
169169
*
170170
* @param bpm The base BPM for a song.
171171
* @param currentTime A timestamp to compare to the BPM.
172+
* @param delta The plus/minus delta to test the current time against.
172173
* @public
173174
*/
174175

175-
PACKAGE_API bool IsOnTheBeat(int bpm, float currentTime)
176+
PACKAGE_API bool IsOnTheBeat(int bpm, float currentTime,
177+
float delta = 0.05f)
176178
{
177179
auto beatInterval = SECONDS_PER_MINUTE / (float)bpm;
178180

179181
auto beatFraction = currentTime / beatInterval;
180182

181183
auto difference = std::abs(beatFraction - std::round(beatFraction));
182184

183-
auto result = difference < 0.05f;
185+
auto result = difference < delta;
184186

185187
return result;
186188
}

0 commit comments

Comments
 (0)