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

include/jdkmidi/filewrite.h

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 #ifndef _JDKMIDI_FILEWRITE_H
00035 #define _JDKMIDI_FILEWRITE_H
00036 
00037 #include "jdkmidi/midi.h"
00038 #include "jdkmidi/msg.h"
00039 #include "jdkmidi/sysex.h"
00040 #include "jdkmidi/file.h"
00041 
00042 namespace jdkmidi
00043 {
00044   
00045   class MIDIFileWriteStream;
00046   class MIDIFileWriteStreamFile;
00047   class MIDIFileWrite;
00048   
00049   class MIDIFileWriteStream 
00050     { 
00051     public:
00052       MIDIFileWriteStream();
00053       virtual ~MIDIFileWriteStream();
00054       
00055       virtual long Seek( long pos, int whence=SEEK_SET ) = 0;
00056       virtual int WriteChar( int c ) = 0; 
00057     }; 
00058   
00059   class MIDIFileWriteStreamFile : public MIDIFileWriteStream 
00060     {
00061     public:
00062       MIDIFileWriteStreamFile( FILE *f_ );
00063       virtual ~MIDIFileWriteStreamFile();
00064       
00065       long Seek( long pos, int whence=SEEK_SET );
00066       int WriteChar( int c );
00067     protected:
00068       FILE *f;
00069     }; 
00070   
00071   class MIDIFileWriteStreamFileName : public MIDIFileWriteStreamFile
00072     {
00073     public:
00074       MIDIFileWriteStreamFileName( const char *fname ) : MIDIFileWriteStreamFile( fopen( fname, "wb" ) )
00075         {
00076         }
00077 
00078       bool IsValid() { return f!=0; }
00079       
00080       virtual ~MIDIFileWriteStreamFileName()
00081         {
00082           if( f )
00083           {
00084             fclose(f);
00085           }
00086         }
00087 
00088     };
00089   
00090   class MIDIFileWrite : protected MIDIFile  
00091     {
00092     public:
00093       MIDIFileWrite( MIDIFileWriteStream *out_stream_ );
00094       virtual     ~MIDIFileWrite();
00095       
00096       
00097       bool ErrorOccurred()            { return error;         }
00098       unsigned long   GetFileLength()         { return file_length;   }
00099       unsigned long   GetTrackLength()        { return track_length;  }
00100       void    ResetTrackLength()      { track_length=0;       }
00101       void    ResetTrackTime()        { track_time=0;         }
00102       
00103       void    WriteFileHeader(
00104         int format,
00105         int ntrks,
00106         int division
00107         );
00108       
00109       void    WriteTrackHeader( unsigned long length );
00110       
00111       void    WriteEvent( const MIDITimedMessage &m );
00112       void    WriteEvent( unsigned long time, const MIDISystemExclusive *e );
00113       void    WriteEvent( unsigned long time, unsigned short text_type, const char *text );
00114       void  WriteEvent( const MIDITimedBigMessage &m );
00115       
00116       void    WriteMetaEvent( unsigned long time, unsigned char type, const unsigned char *data, long length );
00117       void    WriteTempo( unsigned long time, long tempo );
00118       void    WriteKeySignature( unsigned long time, char sharp_flat, char minor );
00119       void    WriteTimeSignature(
00120         unsigned long time,
00121         char numerator=4,
00122         char denominator_power=2,
00123         char midi_clocks_per_metronome=24,
00124         char num_32nd_per_midi_quarter_note=8
00125         );
00126       
00127       void    WriteEndOfTrack(unsigned long time );
00128       
00129       virtual void    RewriteTrackLength();
00130       
00131     protected:
00132       virtual void    Error(char *s);
00133       
00134       void    WriteCharacter( uchar c )
00135         {
00136           if( out_stream->WriteChar( c )<0 )
00137             error=true;
00138         }
00139       
00140       void    Seek( long pos )
00141         {
00142           if( out_stream->Seek( pos )<0 )
00143             error=true;
00144         }
00145       
00146       void    IncrementCounters( int c )   { track_length+=c; file_length+=c; }
00147       
00148       void    WriteShort( unsigned short c );
00149       void    Write3Char( long c );
00150       void    WriteLong( unsigned long c );
00151       
00152       int     WriteVariableNum( unsigned long n );
00153       
00154       void    WriteDeltaTime( unsigned long time );
00155       
00156     private:
00157       bool error;
00158       bool within_track;
00159       unsigned long   file_length;
00160       unsigned long   track_length;
00161       unsigned long   track_time;
00162       unsigned long   track_position;
00163       uchar   running_status;
00164       
00165       MIDIFileWriteStream *out_stream;
00166       
00167     }; 
00168 }
00169 
00170 #endif
00171 
00172