Skip to content

Commit 13bfc18

Browse files
committed
Get note beat directly from midi
Requires midi file contains correct time signature and bpm
1 parent 3d3d328 commit 13bfc18

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

Audio/midi/Song2.mid

-1.68 KB
Binary file not shown.

Classes/MidiMaestro/MidiMaestro.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public partial class MidiMaestro : Resource
1010
{
1111
private MidiFile _midiFile;
1212

13+
public static TempoMap TempoMap;
14+
public static TimeSignature TimeSignature;
15+
1316
//The four note rows that we care about
1417
private midiNoteInfo[] _upNotes;
1518
private midiNoteInfo[] _downNotes;
@@ -28,18 +31,20 @@ public MidiMaestro(string filePath)
2831

2932
if (!FileAccess.FileExists(filePath))
3033
{
31-
GD.PrintErr("ERROR: Unable to load level Midi file: " + filePath);
34+
GD.PushError("ERROR: Unable to load level Midi file: " + filePath);
3235
}
3336

3437
_midiFile = MidiFile.Read(filePath);
38+
TempoMap = _midiFile.GetTempoMap();
39+
TimeSignature = TempoMap.GetTimeSignatureAtTime(new MidiTimeSpan());
3540

3641
//Strip out the notes from the midi file
3742
foreach (var track in _midiFile.GetTrackChunks())
3843
{
3944
string trackName = track.Events.OfType<SequenceTrackNameEvent>().FirstOrDefault()?.Text;
4045
midiNoteInfo[] noteEvents = track
4146
.GetNotes()
42-
.Select(note => new midiNoteInfo(note, _midiFile.GetTempoMap()))
47+
.Select(note => new midiNoteInfo(note))
4348
.ToArray();
4449

4550
switch (trackName)
@@ -77,19 +82,23 @@ public midiNoteInfo[] GetNotes(ArrowType arrowType)
7782
public class midiNoteInfo
7883
{
7984
private readonly Melanchall.DryWetMidi.Interaction.Note _note;
80-
private readonly TempoMap _tempoMap;
8185

82-
public midiNoteInfo(Melanchall.DryWetMidi.Interaction.Note note, TempoMap tempoMap)
86+
public midiNoteInfo(Melanchall.DryWetMidi.Interaction.Note note)
8387
{
8488
_note = note;
85-
_tempoMap = tempoMap;
89+
}
90+
91+
public long GetStartTimeBeat()
92+
{
93+
var beatsBar = _note.TimeAs<BarBeatTicksTimeSpan>(MidiMaestro.TempoMap);
94+
return beatsBar.Bars * MidiMaestro.TimeSignature.Numerator + beatsBar.Beats;
8695
}
8796

8897
public long GetStartTimeTicks() => _note.Time;
8998

9099
public float GetStartTimeSeconds() =>
91-
_note.TimeAs<MetricTimeSpan>(_tempoMap).Milliseconds / 1000f
92-
+ _note.TimeAs<MetricTimeSpan>(_tempoMap).Seconds;
100+
_note.TimeAs<MetricTimeSpan>(MidiMaestro.TempoMap).Milliseconds / 1000f
101+
+ _note.TimeAs<MetricTimeSpan>(MidiMaestro.TempoMap).Seconds;
93102

94103
public long GetEndTime() => _note.EndTime;
95104

scenes/BattleDirector/scripts/Conductor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ private void AddExampleNotes()
8787
{
8888
foreach (midiNoteInfo mNote in MM.GetNotes(type))
8989
{
90-
AddNoteToLane(
91-
type,
92-
(int)(mNote.GetStartTimeSeconds() / (60 / (double)TimeKeeper.Bpm)),
93-
Scribe.NoteDictionary[0]
94-
);
90+
AddNoteToLane(type, (int)mNote.GetStartTimeBeat(), Scribe.NoteDictionary[0]);
9591
}
9692
}
9793
}

0 commit comments

Comments
 (0)