| 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_FILEREAD_H |
|---|
| 35 | #define JDKMIDI_FILEREAD_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 | class MIDIFileReadStream; |
|---|
| 45 | class MIDIFileReadStreamFile; |
|---|
| 46 | class MIDIFileEvents; |
|---|
| 47 | class MIDIFileRead; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | class MIDIFileReadStream |
|---|
| 51 | { |
|---|
| 52 | public: |
|---|
| 53 | MIDIFileReadStream() |
|---|
| 54 | { |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | virtual ~MIDIFileReadStream() |
|---|
| 58 | { |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | virtual int ReadChar() = 0; |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | class MIDIFileReadStreamFile : public MIDIFileReadStream |
|---|
| 65 | { |
|---|
| 66 | public: |
|---|
| 67 | explicit MIDIFileReadStreamFile ( const char *fname ) |
|---|
| 68 | { |
|---|
| 69 | f=fopen ( fname, "rb" ); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | explicit MIDIFileReadStreamFile ( FILE *f_ ) : f ( f_ ) |
|---|
| 73 | { |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | virtual ~MIDIFileReadStreamFile() |
|---|
| 77 | { |
|---|
| 78 | if ( f ) |
|---|
| 79 | { |
|---|
| 80 | fclose ( f ); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | virtual int ReadChar() |
|---|
| 87 | { |
|---|
| 88 | int r=-1; |
|---|
| 89 | |
|---|
| 90 | if ( f && !feof ( f ) && !ferror ( f ) ) |
|---|
| 91 | { |
|---|
| 92 | r=fgetc ( f ); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | return r; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | private: |
|---|
| 100 | FILE *f; |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | class MIDIFileEvents : protected MIDIFile |
|---|
| 104 | { |
|---|
| 105 | public: |
|---|
| 106 | MIDIFileEvents() |
|---|
| 107 | { |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | virtual ~MIDIFileEvents() |
|---|
| 111 | { |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | // |
|---|
| 116 | // The possible events in a MIDI Files |
|---|
| 117 | // |
|---|
| 118 | |
|---|
| 119 | virtual void mf_system_mode ( const MIDITimedMessage &msg ); |
|---|
| 120 | virtual void mf_note_on ( const MIDITimedMessage &msg ); |
|---|
| 121 | virtual void mf_note_off ( const MIDITimedMessage &msg ); |
|---|
| 122 | virtual void mf_poly_after ( const MIDITimedMessage &msg ); |
|---|
| 123 | virtual void mf_bender ( const MIDITimedMessage &msg ); |
|---|
| 124 | virtual void mf_program ( const MIDITimedMessage &msg ); |
|---|
| 125 | virtual void mf_chan_after ( const MIDITimedMessage &msg ); |
|---|
| 126 | virtual void mf_control ( const MIDITimedMessage &msg ); |
|---|
| 127 | virtual void mf_sysex ( MIDIClockTime time, const MIDISystemExclusive &ex ); |
|---|
| 128 | |
|---|
| 129 | virtual void mf_arbitrary ( MIDIClockTime time, int len, unsigned char *data ); |
|---|
| 130 | virtual void mf_metamisc ( MIDIClockTime time, int, int, unsigned char * ); |
|---|
| 131 | virtual void mf_seqnum ( MIDIClockTime time, int ); |
|---|
| 132 | virtual void mf_smpte ( MIDIClockTime time, int, int, int, int, int ); |
|---|
| 133 | virtual void mf_timesig ( MIDIClockTime time, int, int, int, int ); |
|---|
| 134 | virtual void mf_tempo ( MIDIClockTime time, unsigned long tempo ); |
|---|
| 135 | virtual void mf_keysig ( MIDIClockTime time, int, int ); |
|---|
| 136 | virtual void mf_sqspecific ( MIDIClockTime time, int, unsigned char * ); |
|---|
| 137 | virtual void mf_text ( MIDIClockTime time, int, int, unsigned char * ); |
|---|
| 138 | virtual void mf_eot ( MIDIClockTime time ); |
|---|
| 139 | |
|---|
| 140 | // |
|---|
| 141 | // the following methods are to be overridden for your specific purpose |
|---|
| 142 | // |
|---|
| 143 | |
|---|
| 144 | virtual void mf_error ( const char * ); |
|---|
| 145 | |
|---|
| 146 | virtual void mf_starttrack ( int trk ); |
|---|
| 147 | virtual void mf_endtrack ( int trk ); |
|---|
| 148 | virtual void mf_header ( int, int, int ); |
|---|
| 149 | |
|---|
| 150 | // |
|---|
| 151 | // Higher level dispatch functions |
|---|
| 152 | // |
|---|
| 153 | virtual void UpdateTime ( MIDIClockTime delta_time ); |
|---|
| 154 | virtual void MetaEvent ( MIDIClockTime time, int type, int len, unsigned char *buf ); |
|---|
| 155 | virtual void ChanMessage ( const MIDITimedMessage &msg ); |
|---|
| 156 | |
|---|
| 157 | }; |
|---|
| 158 | |
|---|
| 159 | class MIDIFileRead : protected MIDIFile |
|---|
| 160 | { |
|---|
| 161 | public: |
|---|
| 162 | |
|---|
| 163 | MIDIFileRead ( |
|---|
| 164 | MIDIFileReadStream *input_stream_, |
|---|
| 165 | MIDIFileEvents *event_handler_, |
|---|
| 166 | unsigned long max_msg_len=8192 |
|---|
| 167 | ); |
|---|
| 168 | virtual ~MIDIFileRead(); |
|---|
| 169 | |
|---|
| 170 | virtual bool Parse(); |
|---|
| 171 | |
|---|
| 172 | int GetFormat() |
|---|
| 173 | { |
|---|
| 174 | return header_format; |
|---|
| 175 | } |
|---|
| 176 | int GetNumberTracks() |
|---|
| 177 | { |
|---|
| 178 | return header_ntrks; |
|---|
| 179 | } |
|---|
| 180 | int GetDivision() |
|---|
| 181 | { |
|---|
| 182 | return header_division; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | protected: |
|---|
| 186 | |
|---|
| 187 | virtual int ReadHeader(); |
|---|
| 188 | |
|---|
| 189 | virtual void mf_error ( const char * ); |
|---|
| 190 | |
|---|
| 191 | protected: |
|---|
| 192 | int no_merge; |
|---|
| 193 | MIDIClockTime cur_time; |
|---|
| 194 | int skip_init; |
|---|
| 195 | unsigned long to_be_read; |
|---|
| 196 | int cur_track; |
|---|
| 197 | int abort_parse; |
|---|
| 198 | |
|---|
| 199 | unsigned char *the_msg; |
|---|
| 200 | int max_msg_len; |
|---|
| 201 | int msg_index; |
|---|
| 202 | |
|---|
| 203 | private: |
|---|
| 204 | unsigned long ReadVariableNum(); |
|---|
| 205 | unsigned long Read32Bit(); |
|---|
| 206 | int Read16Bit(); |
|---|
| 207 | |
|---|
| 208 | void ReadTrack(); |
|---|
| 209 | |
|---|
| 210 | void MsgAdd ( int ); |
|---|
| 211 | void MsgInit(); |
|---|
| 212 | |
|---|
| 213 | int EGetC(); |
|---|
| 214 | |
|---|
| 215 | int ReadMT ( unsigned long, int ); |
|---|
| 216 | void BadByte ( int ); |
|---|
| 217 | |
|---|
| 218 | void FormChanMessage ( unsigned char st, unsigned char b1, unsigned char b2 ); |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | int header_format; |
|---|
| 222 | int header_ntrks; |
|---|
| 223 | int header_division; |
|---|
| 224 | |
|---|
| 225 | MIDIFileReadStream *input_stream; |
|---|
| 226 | MIDIFileEvents *event_handler; |
|---|
| 227 | }; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | #endif |
|---|