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

src/jdkmidi_filereadmultitrack.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 **  Copyright 1986 to 1998 By J.D. Koftinoff Software, Ltd.
00026 **
00027 **  All rights reserved.
00028 **
00029 **  No one may duplicate this source code in any form for any reason
00030 **  without the written permission given by J.D. Koftinoff Software, Ltd.
00031 **
00032 */
00033 
00034 
00035 #include "jdkmidi/world.h"
00036 
00037 #include "jdkmidi/filereadmultitrack.h"
00038 
00039 namespace jdkmidi
00040 {
00041   
00042   
00043   MIDIFileReadMultiTrack::MIDIFileReadMultiTrack( MIDIMultiTrack *mlttrk )
00044     : multitrack( mlttrk ), cur_track(-1)
00045   {
00046     
00047   }
00048   
00049   MIDIFileReadMultiTrack::~MIDIFileReadMultiTrack()
00050   {
00051     
00052   }
00053   
00054   void    MIDIFileReadMultiTrack::mf_error( char * )  
00055   {
00056   } 
00057   
00058   
00059   
00060   void    MIDIFileReadMultiTrack::mf_starttrack( int trk )  
00061   {
00062     cur_track = trk;
00063   } 
00064   
00065   
00066   void    MIDIFileReadMultiTrack::mf_endtrack( int trk )  
00067   {
00068     cur_track = -1;
00069   } 
00070   
00071   void  MIDIFileReadMultiTrack::AddEventToMultiTrack( 
00072     const MIDITimedMessage &msg,
00073     MIDISystemExclusive *sysex,
00074     int dest_track
00075     ) 
00076   {
00077     if( dest_track!=-1 && dest_track<multitrack->GetNumTracks() )
00078     {
00079       MIDITrack *t = multitrack->GetTrack( dest_track );
00080       
00081       if( t )
00082       {
00083         t->PutEvent( msg, sysex );
00084       }   
00085     } 
00086   } 
00087   
00088   
00089   void    MIDIFileReadMultiTrack::mf_header( 
00090     int the_format_, 
00091     int ntrks_, 
00092     int division_ 
00093     ) 
00094   {
00095     the_format = the_format_;
00096     num_tracks = ntrks_;
00097     division = division_;
00098     
00099     multitrack->SetClksPerBeat( division );
00100     
00101   } 
00102   
00103   
00104   void    MIDIFileReadMultiTrack::ChanMessage( const MIDITimedMessage &msg )  
00105   {
00106     if( the_format == 0 || cur_track==0 )
00107     {
00108       // split format 0 files into separate tracks, one for each channel,
00109       // keep track 0 for tempo and meta-events
00110       
00111       AddEventToMultiTrack( msg, 0, msg.GetChannel()+1 );
00112     }
00113     else
00114     {
00115       AddEventToMultiTrack( msg, 0, cur_track );
00116     } 
00117   } 
00118   
00119   
00120   
00121   
00122   void    MIDIFileReadMultiTrack::mf_sysex( MIDIClockTime time, const MIDISystemExclusive &ex ) 
00123   {
00124     MIDITimedMessage msg;
00125     
00126     msg.SetSysEx();
00127     msg.SetTime(time);
00128     
00129     MIDISystemExclusive *sysex = new MIDISystemExclusive( ex );
00130     
00131     AddEventToMultiTrack( msg, sysex, cur_track );
00132   } 
00133   
00134   
00135   
00136   void    MIDIFileReadMultiTrack::mf_arbitrary( MIDIClockTime time, int len, unsigned char *data )  
00137   {
00138     // ignore arbitrary byte strings
00139   } 
00140   
00141   
00142   void    MIDIFileReadMultiTrack::mf_metamisc( MIDIClockTime time, int, int, unsigned char *  ) 
00143   {
00144     // ignore miscellaneous meta events
00145   } 
00146   
00147   
00148   void    MIDIFileReadMultiTrack::mf_seqnum( MIDIClockTime time, int )  
00149   {
00150     // ignore sequence number events
00151   } 
00152   
00153   
00154   void    MIDIFileReadMultiTrack::mf_smpte( MIDIClockTime time, int, int, int, int, int ) 
00155   {
00156     // ignore smpte events
00157   } 
00158   
00159   
00160   void    MIDIFileReadMultiTrack::mf_timesig( 
00161     MIDIClockTime time, 
00162     int num,
00163     int denom_power, 
00164     int clks_per_metro,
00165     int notated_32nd_per_quarter
00166     ) 
00167   {
00168     MIDITimedMessage msg;
00169     
00170     int denom= 1<<denom_power;
00171     
00172     msg.SetTimeSig( (unsigned char)num, (unsigned char)denom );
00173     msg.SetTime( time );
00174     
00175     MIDISystemExclusive *sysex = new MIDISystemExclusive( 4 );
00176     
00177     sysex->PutByte( (unsigned char)num );
00178     sysex->PutByte( (unsigned char)denom_power );
00179     sysex->PutByte( (unsigned char)clks_per_metro );
00180     sysex->PutByte( (unsigned char)notated_32nd_per_quarter );
00181     
00182     AddEventToMultiTrack( msg, sysex, cur_track );
00183   } 
00184   
00185   
00186   void    MIDIFileReadMultiTrack::mf_tempo( MIDIClockTime time, unsigned long tempo ) 
00187   {
00188     unsigned long tempo_bpm_times_32;
00189     
00190     if( tempo==0 )
00191       tempo=1;
00192     
00193     // tempo is in microseconds per beat
00194     
00195     // calculate beats per second by
00196     
00197     float beats_per_second = 1e6   // 1 million microseconds per second
00198       / (double)tempo;
00199     
00200     float beats_per_minute = beats_per_second * 60;
00201     
00202     tempo_bpm_times_32 = (unsigned long) (beats_per_minute * 32.0);
00203     
00204     MIDITimedMessage msg;
00205     
00206     msg.SetTempo32( tempo_bpm_times_32 );
00207     msg.SetTime( time );
00208     
00209     AddEventToMultiTrack( msg, 0, cur_track );
00210   } 
00211   
00212   
00213   void    MIDIFileReadMultiTrack::mf_keysig(MIDIClockTime time, int c, int v )  
00214   {
00215     MIDITimedMessage msg;
00216     
00217     msg.SetKeySig( (unsigned char)c, (unsigned char)v );
00218     msg.SetTime( time );
00219     
00220     AddEventToMultiTrack( msg, 0, cur_track );    
00221   } 
00222   
00223   
00224   void    MIDIFileReadMultiTrack::mf_sqspecific( MIDIClockTime time, int, unsigned char * ) 
00225   {
00226     // ignore any sequencer specific messages
00227   } 
00228   
00229   
00230   void    MIDIFileReadMultiTrack::mf_text( MIDIClockTime time, int type, int len, unsigned char *s )  
00231   {
00232     MIDITimedMessage msg;
00233     
00234     msg.SetStatus( META_EVENT );
00235     msg.SetMetaType( (uchar)type ); // remember - MF_*_TEXT* id codes match META_*_TEXT codes
00236     msg.SetTime( time );
00237     
00238     MIDISystemExclusive *sysex = new MIDISystemExclusive(len);
00239     
00240     for( int i=0; i<len; ++i )
00241     {
00242       sysex->PutSysByte( s[i] );
00243     }
00244     
00245     AddEventToMultiTrack( msg, sysex, cur_track );    
00246   } 
00247   
00248   
00249   void    MIDIFileReadMultiTrack::mf_eot( MIDIClockTime time )  
00250   {
00251     MIDITimedMessage msg;
00252     
00253     msg.SetStatus( META_EVENT );
00254     msg.SetMetaType( META_END_OF_TRACK );
00255     msg.SetTime( time );
00256     
00257     AddEventToMultiTrack( msg, 0, cur_track );  
00258   } 
00259   
00260   
00261   
00262 }