Skip to content

Commit 1c2e841

Browse files
authored
Merge pull request #34 from neogeek/hotfix/cleaned-up-calculate-beat-bars
[hotfix] Cleaned up CalculateBeatBars.
2 parents 87edffc + c45b41b commit 1c2e841

File tree

4 files changed

+72
-66
lines changed

4 files changed

+72
-66
lines changed

RhythmGameUtilities/Scripts/Utilities.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Runtime.InteropServices;
@@ -101,17 +102,48 @@ public static float InverseLerp(float a, float b, float v)
101102
return UtilitiesInternal.InverseLerp(a, b, v);
102103
}
103104

104-
public static List<int[]> GenerateAdjacentKeyPairs<T>(Dictionary<int, T> dictionary)
105+
public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpmChanges, int resolution = 192, int ts = 4,
106+
bool includeHalfNotes = true)
107+
{
108+
var beatBars = new List<BeatBar>();
109+
110+
var keyValuePairs = GenerateAdjacentKeyPairs(bpmChanges);
111+
112+
foreach (var (startTick, endTick) in keyValuePairs)
113+
{
114+
for (var tick = startTick; tick <= endTick; tick += resolution)
115+
{
116+
beatBars.Add(new BeatBar
117+
{
118+
Position = tick, BPM = bpmChanges[startTick], TimeSignature = new[] { ts }
119+
});
120+
121+
if (includeHalfNotes && tick != endTick)
122+
{
123+
beatBars.Add(new BeatBar
124+
{
125+
Position = tick + resolution / 2,
126+
BPM = bpmChanges[startTick],
127+
TimeSignature = new[] { ts }
128+
});
129+
}
130+
}
131+
}
132+
133+
return beatBars;
134+
}
135+
136+
public static List<Tuple<T, T>> GenerateAdjacentKeyPairs<T>(Dictionary<T, T> dictionary)
105137
{
106138
var keys = dictionary.Keys.ToList();
107139

108140
keys.Sort();
109141

110-
var adjacentKeyPairs = new List<int[]>();
142+
var adjacentKeyPairs = new List<Tuple<T, T>>();
111143

112144
for (var i = 0; i < keys.Count - 1; i += 1)
113145
{
114-
adjacentKeyPairs.Add(new[] { keys[i], keys[i + 1] });
146+
adjacentKeyPairs.Add(new Tuple<T, T>(keys[i], keys[i + 1]));
115147
}
116148

117149
return adjacentKeyPairs;

RhythmGameUtilities/Structs/Song.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public List<BeatBar> BeatBars
122122
{
123123
if (_beatBars.Count == 0)
124124
{
125-
_beatBars = CalculateBeatBars(BPM);
125+
_beatBars = Utilities.CalculateBeatBars(BPM);
126126
}
127127

128128
return _beatBars;
@@ -198,35 +198,6 @@ public int[] GetCurrentTimeSignature(Note note)
198198
return TimeSignatures.Last(item => item.Key <= note.Position).Value;
199199
}
200200

201-
public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpm, int resolution = 192, int ts = 4,
202-
bool includeHalfNotes = true)
203-
{
204-
var newBpm = new List<BeatBar>();
205-
206-
var keyValuePairs = Utilities.GenerateAdjacentKeyPairs(bpm);
207-
208-
foreach (var keys in keyValuePairs)
209-
{
210-
var startTick = keys[0];
211-
var endTick = keys[1];
212-
213-
for (var tick = startTick; tick <= endTick; tick += resolution)
214-
{
215-
newBpm.Add(new BeatBar { Position = tick, BPM = bpm[startTick], TimeSignature = new[] { ts } });
216-
217-
if (includeHalfNotes && tick != endTick)
218-
{
219-
newBpm.Add(new BeatBar
220-
{
221-
Position = tick + resolution / 2, BPM = bpm[keys[0]], TimeSignature = new[] { 4 }
222-
});
223-
}
224-
}
225-
}
226-
227-
return newBpm;
228-
}
229-
230201
}
231202

232203
}

UnityPackage/Scripts/Utilities.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Runtime.InteropServices;
@@ -101,17 +102,48 @@ public static float InverseLerp(float a, float b, float v)
101102
return UtilitiesInternal.InverseLerp(a, b, v);
102103
}
103104

104-
public static List<int[]> GenerateAdjacentKeyPairs<T>(Dictionary<int, T> dictionary)
105+
public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpmChanges, int resolution = 192, int ts = 4,
106+
bool includeHalfNotes = true)
107+
{
108+
var beatBars = new List<BeatBar>();
109+
110+
var keyValuePairs = GenerateAdjacentKeyPairs(bpmChanges);
111+
112+
foreach (var (startTick, endTick) in keyValuePairs)
113+
{
114+
for (var tick = startTick; tick <= endTick; tick += resolution)
115+
{
116+
beatBars.Add(new BeatBar
117+
{
118+
Position = tick, BPM = bpmChanges[startTick], TimeSignature = new[] { ts }
119+
});
120+
121+
if (includeHalfNotes && tick != endTick)
122+
{
123+
beatBars.Add(new BeatBar
124+
{
125+
Position = tick + resolution / 2,
126+
BPM = bpmChanges[startTick],
127+
TimeSignature = new[] { ts }
128+
});
129+
}
130+
}
131+
}
132+
133+
return beatBars;
134+
}
135+
136+
public static List<Tuple<T, T>> GenerateAdjacentKeyPairs<T>(Dictionary<T, T> dictionary)
105137
{
106138
var keys = dictionary.Keys.ToList();
107139

108140
keys.Sort();
109141

110-
var adjacentKeyPairs = new List<int[]>();
142+
var adjacentKeyPairs = new List<Tuple<T, T>>();
111143

112144
for (var i = 0; i < keys.Count - 1; i += 1)
113145
{
114-
adjacentKeyPairs.Add(new[] { keys[i], keys[i + 1] });
146+
adjacentKeyPairs.Add(new Tuple<T, T>(keys[i], keys[i + 1]));
115147
}
116148

117149
return adjacentKeyPairs;

UnityPackage/Structs/Song.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public List<BeatBar> BeatBars
122122
{
123123
if (_beatBars.Count == 0)
124124
{
125-
_beatBars = CalculateBeatBars(BPM);
125+
_beatBars = Utilities.CalculateBeatBars(BPM);
126126
}
127127

128128
return _beatBars;
@@ -198,35 +198,6 @@ public int[] GetCurrentTimeSignature(Note note)
198198
return TimeSignatures.Last(item => item.Key <= note.Position).Value;
199199
}
200200

201-
public static List<BeatBar> CalculateBeatBars(Dictionary<int, int> bpm, int resolution = 192, int ts = 4,
202-
bool includeHalfNotes = true)
203-
{
204-
var newBpm = new List<BeatBar>();
205-
206-
var keyValuePairs = Utilities.GenerateAdjacentKeyPairs(bpm);
207-
208-
foreach (var keys in keyValuePairs)
209-
{
210-
var startTick = keys[0];
211-
var endTick = keys[1];
212-
213-
for (var tick = startTick; tick <= endTick; tick += resolution)
214-
{
215-
newBpm.Add(new BeatBar { Position = tick, BPM = bpm[startTick], TimeSignature = new[] { ts } });
216-
217-
if (includeHalfNotes && tick != endTick)
218-
{
219-
newBpm.Add(new BeatBar
220-
{
221-
Position = tick + resolution / 2, BPM = bpm[keys[0]], TimeSignature = new[] { 4 }
222-
});
223-
}
224-
}
225-
}
226-
227-
return newBpm;
228-
}
229-
230201
}
231202

232203
}

0 commit comments

Comments
 (0)