com.softsynth.jmsl.util
Class MIDIFileToMusicShape

java.lang.Object
  extended by com.softsynth.jmsl.util.MIDIFileToMusicShape
All Implemented Interfaces:
MIDIFileListener

public class MIDIFileToMusicShape
extends java.lang.Object
implements MIDIFileListener

MIDIFileToMusicShape Parse a MidiFile and build a ParallelCollection of MusicShape
If the MidiFile is Type 1 (which includes Track chunks), one MusicShape will be created for each track.
If the Midifile is Type 0 (one single mixed track), one MusicShape will be created for each Midi Channel detected.

Usage:

 MIDIFileToMusicShape mf2ms = new MIDIFileToMusicShape();
 String filename = "myMidiFile.MID";
 mf2ms.convertMIDIFile(filename);
 ParallelCollection p = mf2ms.getParallelCollection();
 // subtract the startTime of the first event from all event times (optional)
 mf2ms.startAtZero();
 // durations of MusicShapes in ParallelCollection are in absolute time. 
 // To convert to delta (relative) time, do this, which operates on the 
 // MusicShapes in p, and converts their time dimensions:
 mf2ms.differentiate();
 // if you are going to play these with amplitudes ranging 0..1 do this:
 mf2ms.normalizeAmplitudes();
 

Author:
Nick Didkovsky, Feb 2002, (c) 2002 Nick Didkovsky and Phil Burk, all rights reserved

Constructor Summary
MIDIFileToMusicShape()
           
 
Method Summary
 void convertMIDIFile(java.io.InputStream stream)
          Read a midi file from an input stream and convert it to a a ParallelCollection of MusicShape
 void convertMIDIFile(java.lang.String fileName)
          Read a midi file from disk and convert it to a ParallelCollection of MusicShape
 void differentiate()
          Midi tracks are read into MusicShapes with absolute time in dimension 0.
 ParallelCollection getParallelCollection()
           
 java.util.Vector getTempi()
           
 java.util.Vector getTimeSignatures()
           
 void handleBeginTrack(int index)
          Called at the beginning of each track
 void handleEndTrack(int index)
          Called at the end of each track
 void handleEvent(int ticks, int command, int data1, int data2)
          Called when a MIDI Message event is encountered.
 void handleHeader(int format, int numTracks, short division)
          Called when the header is parsed.
 void handleMetaEvent(int ticks, int type, byte[] b)
          Called when a MetaEvent is encountered.
 void handleSysExF0(int ticks, byte[] b)
          Called when an F0 SysEx event is encountered.
 void handleSysExF7(int ticks, byte[] b)
          Called when an F7 SysEx event is encountered.
static void main(java.lang.String[] args)
           
 void normalizeAmplitudes()
          Scale midi velocities 0..127 to amplitudes 0..1
 void setDebug(boolean b)
          Show or hide printed messages
 void startAtZero()
          Look for start time of first event of each MusicShape, and find the earliest.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MIDIFileToMusicShape

public MIDIFileToMusicShape()
Method Detail

getParallelCollection

public ParallelCollection getParallelCollection()

getTimeSignatures

public java.util.Vector getTimeSignatures()

setDebug

public void setDebug(boolean b)
Show or hide printed messages


handleHeader

public void handleHeader(int format,
                         int numTracks,
                         short division)
Called when the header is parsed.

Specified by:
handleHeader in interface MIDIFileListener
Parameters:
format - Format of file. 0, 1 or 2.
numTracks -
division -

handleBeginTrack

public void handleBeginTrack(int index)
Called at the beginning of each track

Specified by:
handleBeginTrack in interface MIDIFileListener
Parameters:
index - Track index starting at zero

handleEndTrack

public void handleEndTrack(int index)
Called at the end of each track

Specified by:
handleEndTrack in interface MIDIFileListener
Parameters:
index - Track index starting at zero

handleSysExF0

public void handleSysExF0(int ticks,
                          byte[] b)
Called when an F0 SysEx event is encountered.

Specified by:
handleSysExF0 in interface MIDIFileListener
Parameters:
ticks - MIDIFile time-stamp
b - bytes read from file for SysEx. May be null.

handleSysExF7

public void handleSysExF7(int ticks,
                          byte[] b)
Called when an F7 SysEx event is encountered.

Specified by:
handleSysExF7 in interface MIDIFileListener
Parameters:
ticks - MIDIFile time-stamp
b - bytes read from file for SysEx. May be null.

handleMetaEvent

public void handleMetaEvent(int ticks,
                            int type,
                            byte[] b)
Called when a MetaEvent is encountered.

Specified by:
handleMetaEvent in interface MIDIFileListener
Parameters:
ticks - MIDIFile time-stamp
type - MetaEvent type, from 0 to 127.
b - bytes read from file for SysEx. May be null.

handleEvent

public void handleEvent(int ticks,
                        int command,
                        int data1,
                        int data2)
Called when a MIDI Message event is encountered.

Specified by:
handleEvent in interface MIDIFileListener
Parameters:
ticks - MIDIFile time-stamp
command - MIDI Command, eg. NOTE_ON.
data1 - First data byte of message.
data2 - Second optional data byte of message.

convertMIDIFile

public void convertMIDIFile(java.lang.String fileName)
                     throws java.io.IOException
Read a midi file from disk and convert it to a ParallelCollection of MusicShape

Throws:
java.io.IOException

convertMIDIFile

public void convertMIDIFile(java.io.InputStream stream)
                     throws java.io.IOException
Read a midi file from an input stream and convert it to a a ParallelCollection of MusicShape

Throws:
java.io.IOException

differentiate

public void differentiate()
Midi tracks are read into MusicShapes with absolute time in dimension 0. Call this method to convert each MusicShape to relative time. Nothing here you couldn't do yourself: enumerates through the resulting ParallelCollection and calls musicShape.differentiate(onTimeOfLastElement + holdTimeOfLastElement, 0 ) on each child


normalizeAmplitudes

public void normalizeAmplitudes()
Scale midi velocities 0..127 to amplitudes 0..1


startAtZero

public void startAtZero()
Look for start time of first event of each MusicShape, and find the earliest. Subtract that amount from all start times of all MusicShapes


getTempi

public java.util.Vector getTempi()
Returns:
Vector of MidiTempoHolder

main

public static void main(java.lang.String[] args)