Tuesday, January 19, 2010

Storing Midi Information









Storing Midi Information


A MidiInfo object holds a single MIDI sequence and a reference to the sequencer created in MidisLoader. This allows it to play, stop, pause, and resume a clip, and make it loop.


The constructor is passed the sequence's name, filename, and the sequencer reference, and then it loads the sequence using MidiSystem.getSequence( ). A sequence is played by loading it into the sequencer and starting the sequence:



public void play(boolean toLoop)
{
if ((sequencer != null) && (seq != null)) {
try {
sequencer.setSequence(seq); // load sequence into sequencer
sequencer.setTickPosition(0); // reset to the start
isLooping = toLoop;
sequencer.start( ); // play it
}
catch (InvalidMidiDataException e) {
System.out.println("Invalid midi file: " + filename);
}
}
}



The Sequencer class has several loop( ) methods, but they aren't used here. A similar coding technique is employed as in ClipInfo: A global isLooping Boolean is set to true and employed later by tryLooping( ). This permits us to trigger a callback in a watcher at the end of each iteration.



Stopping Sequences


Stopping a sequence with Sequencer.stop( ) causes it to stop at its current position. More importantly, no metaevent is generated unless the stopping coincides with the end of the track. In order to generate an event, my stop( ) method "winds" the sequence to its end:



public void stop( )
{
if ((sequencer != null) && (seq != null)) {
isLooping = false;
if (!sequencer.isRunning( )) // the sequence may be paused
sequencer.start( );
sequencer.setTickPosition( sequencer.getTickLength( ) );
// move to end of sequence to trigger end-of-track event
}
}



This behavior means that meta( ) in MidisLoader is called in two situations: when the sequence reaches its end and when the sequence is stopped. This corresponds to the ways that a LineListener STOP event can be generated for clips.


MidisLoader's meta( ) method calls TRylooping( ) in MidiInfo to determine if the sequence is looping. TRyLooping( ) is responsible for restarting the sequence if its isLooping Boolean is TRue:



public boolean tryLooping( )
{
if ((sequencer != null) && (seq != null)) {
if (sequencer.isRunning( ))
sequencer.stop( );
sequencer.setTickPosition(0);
if (isLooping) { // play it again
sequencer.start( );
return true;
}
}
return false;
}



Admittedly, this is rather convoluted coding: stop( ) TRiggers meta( ), which calls tryLooping( ), and then TRyLooping( ) restarts a looping sequence.


Part of the problem is that looping isn't implemented with Sequencer.loop( ). Instead, a sequence comes to its end and is started again by tryLooping( ) calling start( ). This allows additional processing in meta( ) (e.g., watcher communication) between the end of the sequence and its restart.


Another aspect is that the sequence control code is located in MidiInfo (stop( ) and tryLooping( )), but the metaevent processing is inside meta( ) in MidisLoader.




Pausing and Resuming Sequences


MidiInfo's pause( ) and resume( ) methods are implemented using the Sequencer class's start( ) and stop( ) methods. These Sequencer methods don't adjust the sequence's playing position:



public void pause( )
{ if ((sequencer != null) && (seq != null)) {
if (sequencer.isRunning( ))
sequencer.stop( );
}
}


public void resume( )
{ if ((sequencer != null) && (seq != null))
sequencer.start( );
}











    No comments: