| 1 | /* |
|---|
| 2 | * libjdkmidi-2004 C++ Class Library for MIDI |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004 J.D. Koftinoff Software, Ltd. |
|---|
| 5 | * www.jdkoftinoff.com |
|---|
| 6 | * jeffk@jdkoftinoff.com |
|---|
| 7 | * |
|---|
| 8 | * *** RELEASED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) April 27, 2004 *** |
|---|
| 9 | * |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU General Public License as published by |
|---|
| 12 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | * (at your option) any later version. |
|---|
| 14 | * |
|---|
| 15 | * This program is distributed in the hope that it will be useful, |
|---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | * GNU General Public License for more details. |
|---|
| 19 | * |
|---|
| 20 | * You should have received a copy of the GNU General Public License |
|---|
| 21 | * along with this program; if not, write to the Free Software |
|---|
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | */ |
|---|
| 24 | /* |
|---|
| 25 | ** Copyright 1986 to 1998 By J.D. Koftinoff Software, Ltd. |
|---|
| 26 | ** |
|---|
| 27 | ** All rights reserved. |
|---|
| 28 | ** |
|---|
| 29 | ** No one may duplicate this source code in any form for any reason |
|---|
| 30 | ** without the written permission given by J.D. Koftinoff Software, Ltd. |
|---|
| 31 | ** |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #ifndef JDKMIDI_FILEWRITE_H |
|---|
| 35 | #define JDKMIDI_FILEWRITE_H |
|---|
| 36 | |
|---|
| 37 | #include "jdkmidi/midi.h" |
|---|
| 38 | #include "jdkmidi/msg.h" |
|---|
| 39 | #include "jdkmidi/sysex.h" |
|---|
| 40 | #include "jdkmidi/file.h" |
|---|
| 41 | |
|---|
| 42 | namespace jdkmidi |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | class MIDIFileWriteStream; |
|---|
| 46 | class MIDIFileWriteStreamFile; |
|---|
| 47 | class MIDIFileWrite; |
|---|
| 48 | |
|---|
| 49 | class MIDIFileWriteStream |
|---|
| 50 | { |
|---|
| 51 | public: |
|---|
| 52 | MIDIFileWriteStream(); |
|---|
| 53 | virtual ~MIDIFileWriteStream(); |
|---|
| 54 | |
|---|
| 55 | virtual long Seek ( long pos, int whence=SEEK_SET ) = 0; |
|---|
| 56 | virtual int WriteChar ( int c ) = 0; |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | class MIDIFileWriteStreamFile : public MIDIFileWriteStream |
|---|
| 60 | { |
|---|
| 61 | public: |
|---|
| 62 | MIDIFileWriteStreamFile ( FILE *f_ ); |
|---|
| 63 | virtual ~MIDIFileWriteStreamFile(); |
|---|
| 64 | |
|---|
| 65 | long Seek ( long pos, int whence=SEEK_SET ); |
|---|
| 66 | int WriteChar ( int c ); |
|---|
| 67 | protected: |
|---|
| 68 | FILE *f; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | class MIDIFileWriteStreamFileName : public MIDIFileWriteStreamFile |
|---|
| 72 | { |
|---|
| 73 | public: |
|---|
| 74 | MIDIFileWriteStreamFileName ( const char *fname ) : MIDIFileWriteStreamFile ( fopen ( fname, "wb" ) ) |
|---|
| 75 | { |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | bool IsValid() |
|---|
| 79 | { |
|---|
| 80 | return f!=0; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | virtual ~MIDIFileWriteStreamFileName() |
|---|
| 84 | { |
|---|
| 85 | if ( f ) |
|---|
| 86 | { |
|---|
| 87 | fclose ( f ); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | class MIDIFileWrite : protected MIDIFile |
|---|
| 94 | { |
|---|
| 95 | public: |
|---|
| 96 | MIDIFileWrite ( MIDIFileWriteStream *out_stream_ ); |
|---|
| 97 | virtual ~MIDIFileWrite(); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | bool ErrorOccurred() |
|---|
| 101 | { |
|---|
| 102 | return error; |
|---|
| 103 | } |
|---|
| 104 | unsigned long GetFileLength() |
|---|
| 105 | { |
|---|
| 106 | return file_length; |
|---|
| 107 | } |
|---|
| 108 | unsigned long GetTrackLength() |
|---|
| 109 | { |
|---|
| 110 | return track_length; |
|---|
| 111 | } |
|---|
| 112 | void ResetTrackLength() |
|---|
| 113 | { |
|---|
| 114 | track_length=0; |
|---|
| 115 | } |
|---|
| 116 | void ResetTrackTime() |
|---|
| 117 | { |
|---|
| 118 | track_time=0; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void WriteFileHeader ( |
|---|
| 122 | int format, |
|---|
| 123 | int ntrks, |
|---|
| 124 | int division |
|---|
| 125 | ); |
|---|
| 126 | |
|---|
| 127 | void WriteTrackHeader ( unsigned long length ); |
|---|
| 128 | |
|---|
| 129 | void WriteEvent ( const MIDITimedMessage &m ); |
|---|
| 130 | void WriteEvent ( unsigned long time, const MIDISystemExclusive *e ); |
|---|
| 131 | void WriteEvent ( unsigned long time, unsigned short text_type, const char *text ); |
|---|
| 132 | void WriteEvent ( const MIDITimedBigMessage &m ); |
|---|
| 133 | |
|---|
| 134 | void WriteMetaEvent ( unsigned long time, unsigned char type, const unsigned char *data, long length ); |
|---|
| 135 | void WriteTempo ( unsigned long time, long tempo ); |
|---|
| 136 | void WriteKeySignature ( unsigned long time, char sharp_flat, char minor ); |
|---|
| 137 | void WriteTimeSignature ( |
|---|
| 138 | unsigned long time, |
|---|
| 139 | char numerator=4, |
|---|
| 140 | char denominator_power=2, |
|---|
| 141 | char midi_clocks_per_metronome=24, |
|---|
| 142 | char num_32nd_per_midi_quarter_note=8 |
|---|
| 143 | ); |
|---|
| 144 | |
|---|
| 145 | void WriteEndOfTrack ( unsigned long time ); |
|---|
| 146 | |
|---|
| 147 | virtual void RewriteTrackLength(); |
|---|
| 148 | |
|---|
| 149 | protected: |
|---|
| 150 | virtual void Error ( char *s ); |
|---|
| 151 | |
|---|
| 152 | void WriteCharacter ( uchar c ) |
|---|
| 153 | { |
|---|
| 154 | if ( out_stream->WriteChar ( c ) <0 ) |
|---|
| 155 | error=true; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void Seek ( long pos ) |
|---|
| 159 | { |
|---|
| 160 | if ( out_stream->Seek ( pos ) <0 ) |
|---|
| 161 | error=true; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | void IncrementCounters ( int c ) |
|---|
| 165 | { |
|---|
| 166 | track_length+=c; |
|---|
| 167 | file_length+=c; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void WriteShort ( unsigned short c ); |
|---|
| 171 | void Write3Char ( long c ); |
|---|
| 172 | void WriteLong ( unsigned long c ); |
|---|
| 173 | |
|---|
| 174 | int WriteVariableNum ( unsigned long n ); |
|---|
| 175 | |
|---|
| 176 | void WriteDeltaTime ( unsigned long time ); |
|---|
| 177 | |
|---|
| 178 | private: |
|---|
| 179 | bool error; |
|---|
| 180 | bool within_track; |
|---|
| 181 | unsigned long file_length; |
|---|
| 182 | unsigned long track_length; |
|---|
| 183 | unsigned long track_time; |
|---|
| 184 | unsigned long track_position; |
|---|
| 185 | uchar running_status; |
|---|
| 186 | |
|---|
| 187 | MIDIFileWriteStream *out_stream; |
|---|
| 188 | |
|---|
| 189 | }; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | #endif |
|---|
| 193 | |
|---|