@@ -120,6 +120,8 @@ static int decode_midi_inside_memory_limits( ) {
120
120
// It returns always 0.
121
121
static int decode_midi_payload_set_tempo ( MidiMessagePayload * _payload ) {
122
122
123
+ // printf( "iBPM = %d\n", _payload->MsgData.MetaEvent.Data.Tempo.iBPM );
124
+
123
125
tempo = _payload -> MsgData .MetaEvent .Data .Tempo .iBPM ;
124
126
125
127
return 0 ;
@@ -454,7 +456,7 @@ Variable * music_load_to_variable( Environment * _environment, char * _filename,
454
456
if ( mf ) {
455
457
456
458
imfBuffer = malloc ( 16 * MAX_TEMPORARY_STORAGE );
457
- int ppq = 120 ;
459
+ int ppq = midiFileGetPPQN ( mf ) ;
458
460
int midiConversionDone = 0 ;
459
461
int track = 0 ;
460
462
@@ -626,10 +628,21 @@ Variable * music_load_to_variable( Environment * _environment, char * _filename,
626
628
// If the minimum position is valid...
627
629
if ( midiMinPosition >= midiPosition && midiMinPosition < (midiPosition + 0xffffff ) ) {
628
630
631
+ // just divide 60,000 by your tempo and you will get the number of milliseconds per beat.
632
+ // Thereafter you can divide by two for eighth notes or four for sixteenth notes.
633
+ // microseconds per tick = microseconds per quarter note / ticks per quarter note
634
+
635
+ float kMillisecondsPerBPM = (float )( 60 * 1000 ) / (float )tempo ;
636
+ float kMillisecondsPerTick = kMillisecondsPerBPM / ppq ;
637
+ float deltaTimeInMilliseconds = ( midiMinPosition - midiPosition ) * kMillisecondsPerTick ;
638
+
629
639
// We must calculate the effective delay (in jiffies)
630
640
// to introduce into the IMF stream, and it is based
631
641
// on the real MIDI tempo.
632
- int jiffies = ( ( midiMinPosition - midiPosition ) * 600 ) / ( tempo * ppq );
642
+ int jiffies = deltaTimeInMilliseconds / 20 ;
643
+
644
+ // printf( " ( midiMinPosition - midiPosition ): %d\n", ( midiMinPosition - midiPosition ) );
645
+ // printf( "deltaTimeInMilliseconds: %f, jiffies: %d\n", deltaTimeInMilliseconds, jiffies );
633
646
634
647
// printf(" DELAY: %d jiffies\n", jiffies );
635
648
0 commit comments