@@ -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)
7782public 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
0 commit comments