jdkmidi class library documentation

Copyright © 2004 J.D. Koftinoff Software, Ltd.

Released under the GNU General Public License (GPL)




Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

tests/jdkmidi_test_sequencer.cpp

Go to the documentation of this file.
00001 /*
00002  *  libjdkmidi-2004 C++ Class Library for MIDI
00003  *
00004  *  Copyright (C) 2004  J.D. Koftinoff Software, Ltd.
00005  *  www.jdkoftinoff.com
00006  *  jeffk@jdkoftinoff.com
00007  *
00008  *  *** RELEASED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) April 27, 2004 ***
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 #ifdef WIN32
00026 #include <windows.h>
00027 #endif
00028 
00029 #include "jdkmidi/world.h"
00030 #include "jdkmidi/track.h"
00031 #include "jdkmidi/multitrack.h"
00032 #include "jdkmidi/filereadmultitrack.h"
00033 #include "jdkmidi/fileread.h"
00034 #include "jdkmidi/fileshow.h"
00035 #include "jdkmidi/sequencer.h"
00036 
00037 using namespace jdkmidi;
00038 
00039 
00040 void DumpMIDIBigMessage( MIDITimedBigMessage *msg )
00041 {
00042   if( msg )
00043   {
00044     char msgbuf[1024];
00045     
00046     fprintf( stdout, "%s\n", msg->MsgToText(msgbuf) );
00047     if( msg->IsSysEx() )
00048     {
00049       fprintf( stdout, "\tSYSEX length: %d\n", msg->GetSysEx()->GetLength() );
00050     }
00051     
00052   } 
00053   
00054 } 
00055 
00056 void DumpMIDITimedBigMessage( MIDITimedBigMessage *msg )
00057 {
00058   if( msg )
00059   {
00060     char msgbuf[1024];
00061     
00062     fprintf( stdout, "%8ld : %s\n", msg->GetTime(), msg->MsgToText(msgbuf) );
00063     if( msg->IsSysEx() )
00064     {
00065       fprintf( stdout, "\tSYSEX length: %d\n", msg->GetSysEx()->GetLength() );
00066     }
00067     
00068   } 
00069   
00070 } 
00071 
00072 void DumpMIDITrack( MIDITrack *t ) 
00073 {
00074   MIDITimedBigMessage *msg;
00075   
00076   for( int i=0; i<t->GetNumEvents(); ++i )
00077   {
00078     msg = t->GetEventAddress( i );
00079     DumpMIDITimedBigMessage( msg );   
00080     
00081   }
00082   
00083 } 
00084 
00085 void DumpAllTracks( MIDIMultiTrack *mlt ) 
00086 {
00087   
00088   fprintf( stdout , "Clocks per beat: %d\n\n", mlt->GetClksPerBeat() );
00089   
00090   for( int i=0; i<mlt->GetNumTracks(); ++i )
00091   {
00092     if( mlt->GetTrack(i)->GetNumEvents() > 0 )
00093     {
00094       fprintf( stdout, "DUMP OF TRACK #%2d:\n", i );
00095       DumpMIDITrack( mlt->GetTrack(i) );
00096       fprintf( stdout, "\n" );
00097     }
00098     
00099   }
00100   
00101 } 
00102 
00103 void DumpMIDIMultiTrack( MIDIMultiTrack *mlt ) 
00104 {
00105   MIDIMultiTrackIterator i( mlt );
00106   MIDITimedBigMessage *msg;
00107   
00108   fprintf( stdout , "Clocks per beat: %d\n\n", mlt->GetClksPerBeat() );
00109   
00110   i.GoToTime(0);
00111   
00112   do
00113   {
00114     int trk_num;
00115     
00116     if( i.GetCurEvent(&trk_num, &msg ) )
00117     {
00118       fprintf( stdout, "#%2d - ", trk_num );
00119       DumpMIDITimedBigMessage( msg );
00120     }       
00121   } while( i.GoToNextEvent() );
00122   
00123 } 
00124 
00125 void PlayDumpSequencer( MIDISequencer *seq ) 
00126 { 
00127   float pretend_clock_time = 0.0;
00128   float next_event_time = 0.0;
00129   MIDITimedBigMessage ev;
00130   int ev_track;
00131   
00132   seq->GoToTimeMs( pretend_clock_time );
00133   
00134   if( !seq->GetNextEventTimeMs( &next_event_time ) )
00135   {
00136     return;
00137   }
00138   
00139   // simulate a clock going forward with 10ms resolution for 1 minute
00140   
00141   for( ; pretend_clock_time<60.0*1000.0; pretend_clock_time+=10.0 )
00142   {
00143     
00144     // find all events that came before or a the current time
00145     
00146     while( next_event_time <= pretend_clock_time )
00147     {
00148       if( seq->GetNextEvent( &ev_track, &ev ) )
00149       {
00150         // found the event!
00151         // show it to stdout
00152         
00153         fprintf( stdout, "tm=%06.0f : evtm=%06.0f :trk%02d : ",
00154                  pretend_clock_time, next_event_time, ev_track );
00155         
00156         DumpMIDITimedBigMessage( &ev );
00157         
00158         // now find the next message
00159         
00160         if( !seq->GetNextEventTimeMs( &next_event_time ) )
00161         {
00162           // no events left so end
00163           
00164           fprintf( stdout, "End\n" );
00165           return;
00166         }
00167         
00168       }     
00169     }   
00170   }
00171   
00172   
00173 } 
00174 
00175 
00176 int main( int argc, char **argv )
00177 {
00178   if( argc>1 )
00179   {   
00180     MIDIFileReadStreamFile rs( argv[1] );
00181     MIDIMultiTrack tracks(64);
00182     MIDIFileReadMultiTrack track_loader( &tracks );
00183     MIDIFileRead reader( &rs, &track_loader );
00184     MIDISequencer seq( &tracks );   
00185     
00186     reader.Parse();
00187     
00188     //DumpMIDIMultiTrack( &tracks );
00189     
00190     
00191     PlayDumpSequencer( &seq );
00192   }
00193   
00194   return 0;   
00195 }