diff MidiPlayer.java @ 22:5c54f5213f3d

This is a fix for the problem where some MIDI files cannot be read because they encode OFF signals as ON signals with a velocity of 0.
author JShulver
date Wed, 31 Oct 2012 15:16:26 +0000
parents 15f3b6d84c81
children 9fc8683b8fed
line wrap: on
line diff
--- a/MidiPlayer.java	Tue Mar 27 16:54:17 2012 +0100
+++ b/MidiPlayer.java	Wed Oct 31 15:16:26 2012 +0000
@@ -34,7 +34,7 @@
 import javax.sound.midi.MetaMessage;
 import javax.sound.midi.SysexMessage;
 import javax.sound.midi.MetaMessage;
-
+import java.util.Arrays;
 public class MidiPlayer { 
 
     /* variables */ 
@@ -166,7 +166,10 @@
 //                               System.out.println("onset in ticks = " + event.getTick()+ 
 //                                                  "; onset in microseconds = " + 
 //                                                  ticksToMicroseconds(event.getTick()));
-                              ons.add(ticksToMicroseconds(event.getTick()));
+                              //if the event does not have a velocity of zero (i.e. switching a note off)
+                              if(message.getMessage()[2] != 0) {
+                                 ons.add(ticksToMicroseconds(event.getTick())); //THEN we can add the note
+                              }
                 }
             }
         } 
@@ -191,6 +194,13 @@
                     //                   (float)event.getTick() * microsecondsPerTick);
                     offs.add(ticksToMicroseconds(event.getTick()));
                 }
+                //if we have not found a note off,
+                else if (message instanceof ShortMessage 
+                        && ((ShortMessage)message).getCommand() == ShortMessage.NOTE_ON
+                        && message.getMessage()[2] == 0) { //but it is a note on with a velocity of zero
+                        offs.add(ticksToMicroseconds(event.getTick())); //add it as an off signal
+                }
+                
             }
         }
         return offs;