Dear all,

I am developing some software that can deal with MIDI files and do some fun stuff with them, but I am running into some strange trouble when analysing the MidiEvents.

I want to list all the MidiEvents occurring in a particular Track, with their tickposition, status, and precise byte-message. The tick position works fine, but I am having troubles with the status bytes.

Many of the MidiEvents are not recognised by Java and are neither NoteOn nor NoteOff events, although I think they should be. Is it me having implemented something wrong, or are these MIDI-files with a really strange format?


The following is an excerpt of the code:


Java Code:
	

public class MidiEventStorage 
{	
	static Sequencer midiSequencer;
	
	public static void main(String[] args) 
	{
		Sequence mySequence = loadMidiSequence();
		RedBlackTree<MyMidiEvent> midiEventTree = midiSequenceToTree(mySequence);
		midiSequencer.start();		
		midiEventTree.printTree();
		
/*----*/pauseProg();/*--------------------------------------*/
		
		midiSequencer.stop();
		midiSequencer.close();
	}// End main
	
	private static Sequence loadMidiSequence() 
	{
		Sequence midiSequence;
		File myFile = new File(System.getProperty("user.dir"));			
		JFileChooser fileChooser = new JFileChooser(myFile);
		
		if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
		{
			if ( fileChooser.getSelectedFile().exists())
			{				
				try 
				{
					midiSequencer = MidiSystem.getSequencer();
					midiSequencer.open();
					
					midiSequence = MidiSystem.getSequence(fileChooser.getSelectedFile());
					midiSequencer.setSequence(midiSequence);
					return midiSequence;
				} 
				catch (MidiUnavailableException e) {} 
				catch (InvalidMidiDataException e) {}
				catch (IOException e) {}				
			}			
		}
		return null;
	}
	
	public static RedBlackTree<MyMidiEvent> midiSequenceToTree(Sequence midiSequence)
	{
		RedBlackTree<MyMidiEvent> midiEventTree = new RedBlackTree<MyMidiEvent>();
		
		Track [] midiTracks = midiSequence.getTracks();
		Track currentTrack;
		int size;
				
		for (int ii = 0; ii < midiTracks.length; ii++)
		{
			currentTrack = midiTracks[ii];
			size = currentTrack.size();
			
			for (int jj = 0; jj < size; jj++)
			{
				midiEventTree.insert( new MyMidiEvent(currentTrack.get(jj), ii) );
			}
		}
		
		return midiEventTree;
	}
	
	public static void pauseProg()
	{
		System.out.print("Press [ENTER] to continue...");  
        try  
        {  
          System.in.read();  
          System.in.skip(System.in.available());  
        }  
        catch(Exception e){e.printStackTrace();}   
        
	}
}
Please assume that I have a class RedBlackTree that works fine, and which contains a method printTree() that executes myMidiEvent.toString() at all the nodes it contains.

The System output that I tend to get generally looks like this (this is only a tiny part of the thousands of events):

Java Code:
@ tick 576: 153-Unknown ( 10011001 00101010 01011010), track 3
@ tick 636: 153-Unknown ( 10011001 00101010 00000000), track 3
@ tick 664: 149-Unknown ( 10010101 00100110 00000000), track 1
@ tick 768: 144-NoteOn ( 10010000 01000011 01000000), track 0
@ tick 768: 153-Unknown ( 10011001 00111000 01000000), track 3
@ tick 812: 128-NoteOff ( 10000000 01000011 00000000), track 0
@ tick 816: 144-NoteOn ( 10010000 01000010 01001010), track 0
@ tick 828: 153-Unknown ( 10011001 00111000 00000000), track 3
@ tick 860: 128-NoteOff ( 10000000 01000010 00000000), track 0
@ tick 864: 144-NoteOn ( 10010000 01000001 01011110), track 0
@ tick 864: 149-Unknown ( 10010101 00101010 01000000), track 1
@ tick 908: 128-NoteOff ( 10000000 01000001 00000000), track 0
@ tick 912: 144-NoteOn ( 10010000 01000000 01110010), track 0
@ tick 956: 128-NoteOff ( 10000000 01000000 00000000), track 0
@ tick 956: 149-Unknown ( 10010101 00101010 00000000), track 1
@ tick 960: 153-Unknown ( 10011001 00111000 00010000), track 3
So some events are recognised, but many are not. Can someone tell me why not? What is going on in the brains of Java?

Best,
Jelle