comparison 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
comparison
equal deleted inserted replaced
21:9e45ae01f81e 22:5c54f5213f3d
32 import javax.sound.midi.MidiMessage; 32 import javax.sound.midi.MidiMessage;
33 import javax.sound.midi.ShortMessage; 33 import javax.sound.midi.ShortMessage;
34 import javax.sound.midi.MetaMessage; 34 import javax.sound.midi.MetaMessage;
35 import javax.sound.midi.SysexMessage; 35 import javax.sound.midi.SysexMessage;
36 import javax.sound.midi.MetaMessage; 36 import javax.sound.midi.MetaMessage;
37 37 import java.util.Arrays;
38 public class MidiPlayer { 38 public class MidiPlayer {
39 39
40 /* variables */ 40 /* variables */
41 private Sequencer sequencer = null; 41 private Sequencer sequencer = null;
42 private Synthesizer synthesizer = null; 42 private Synthesizer synthesizer = null;
164 if (message instanceof ShortMessage && 164 if (message instanceof ShortMessage &&
165 ((ShortMessage)message).getCommand() == ShortMessage.NOTE_ON) { 165 ((ShortMessage)message).getCommand() == ShortMessage.NOTE_ON) {
166 // System.out.println("onset in ticks = " + event.getTick()+ 166 // System.out.println("onset in ticks = " + event.getTick()+
167 // "; onset in microseconds = " + 167 // "; onset in microseconds = " +
168 // ticksToMicroseconds(event.getTick())); 168 // ticksToMicroseconds(event.getTick()));
169 ons.add(ticksToMicroseconds(event.getTick())); 169 //if the event does not have a velocity of zero (i.e. switching a note off)
170 if(message.getMessage()[2] != 0) {
171 ons.add(ticksToMicroseconds(event.getTick())); //THEN we can add the note
172 }
170 } 173 }
171 } 174 }
172 } 175 }
173 return ons; 176 return ons;
174 } 177 }
189 // "; microsecondsPerTick = " + microsecondsPerTick + 192 // "; microsecondsPerTick = " + microsecondsPerTick +
190 // "; offset in microseconds = " + 193 // "; offset in microseconds = " +
191 // (float)event.getTick() * microsecondsPerTick); 194 // (float)event.getTick() * microsecondsPerTick);
192 offs.add(ticksToMicroseconds(event.getTick())); 195 offs.add(ticksToMicroseconds(event.getTick()));
193 } 196 }
197 //if we have not found a note off,
198 else if (message instanceof ShortMessage
199 && ((ShortMessage)message).getCommand() == ShortMessage.NOTE_ON
200 && message.getMessage()[2] == 0) { //but it is a note on with a velocity of zero
201 offs.add(ticksToMicroseconds(event.getTick())); //add it as an off signal
202 }
203
194 } 204 }
195 } 205 }
196 return offs; 206 return offs;
197 } 207 }
198 208