Skip to content

Commit 0e1c13a

Browse files
committed
Cleaned up CalculateBeatBars.
1 parent 87edffc commit 0e1c13a

File tree

4 files changed

+74
-66
lines changed

4 files changed

+74
-66
lines changed

RhythmGameUtilities/Scripts/Utilities.cs

Lines changed: 36 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,49 @@ 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+
115+
for (var tick = startTick; tick <= endTick; tick += resolution)
116+
{
117+
beatBars.Add(new BeatBar
118+
{
119+
Position = tick, BPM = bpmChanges[startTick], TimeSignature = new[] { ts }
120+
});
121+
122+
if (includeHalfNotes && tick != endTick)
123+
{
124+
beatBars.Add(new BeatBar
125+
{
126+
Position = tick + resolution / 2,
127+
BPM = bpmChanges[startTick],
128+
TimeSignature = new[] { ts }
129+
});
130+
}
131+
}
132+
}
133+
134+
return beatBars;
135+
}
136+
137+
public static List<Tuple<int, int>> GenerateAdjacentKeyPairs<T>(Dictionary<int, T> dictionary)
105138
{
106139
var keys = dictionary.Keys.ToList();
107140

108141
keys.Sort();
109142

110-
var adjacentKeyPairs = new List<int[]>();
143+
var adjacentKeyPairs = new List<Tuple<int, int>>();
111144

112145
for (var i = 0; i < keys.Count - 1; i += 1)
113146
{
114-
adjacentKeyPairs.Add(new[] { keys[i], keys[i + 1] });
147+
adjacentKeyPairs.Add(new Tuple<int, int>(keys[i], keys[i + 1]));
115148
}
116149

117150
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: 36 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,49 @@ 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+
115+
for (var tick = startTick; tick <= endTick; tick += resolution)
116+
{
117+
beatBars.Add(new BeatBar
118+
{
119+
Position = tick, BPM = bpmChanges[startTick], TimeSignature = new[] { ts }
120+
});
121+
122+
if (includeHalfNotes && tick != endTick)
123+
{
124+
beatBars.Add(new BeatBar
125+
{
126+
Position = tick + resolution / 2,
127+
BPM = bpmChanges[startTick],
128+
TimeSignature = new[] { ts }
129+
});
130+
}
131+
}
132+
}
133+
134+
return beatBars;
135+
}
136+
137+
public static List<Tuple<int, int>> GenerateAdjacentKeyPairs<T>(Dictionary<int, T> dictionary)
105138
{
106139
var keys = dictionary.Keys.ToList();
107140

108141
keys.Sort();
109142

110-
var adjacentKeyPairs = new List<int[]>();
143+
var adjacentKeyPairs = new List<Tuple<int, int>>();
111144

112145
for (var i = 0; i < keys.Count - 1; i += 1)
113146
{
114-
adjacentKeyPairs.Add(new[] { keys[i], keys[i + 1] });
147+
adjacentKeyPairs.Add(new Tuple<int, int>(keys[i], keys[i + 1]));
115148
}
116149

117150
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)