1 #ifndef _MIDIFILE_H
2 #define _MIDIFILE_H
3 
4 #include <stdint.h>
5 #include "midiinfo.h"		/* enumerations and constants for GM */
6 
7 /*
8  * midiFile.c -  Header file for Steevs MIDI Library
9  * Version 1.4
10  *
11  *  AUTHOR: Steven Goodwin (StevenGoodwin@gmail.com)
12  *			Copyright 1998-2010, Steven Goodwin.
13  *
14  *  This program is free software; you can redistribute it and/or
15  *  modify it under the terms of the GNU General Public License as
16  *  published by the Free Software Foundation; either version 2 of
17  *  the License,or (at your option) any later version.
18  *
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software
26  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28 
29 /*
30 ** All functions start with one of the following prefixes:
31 **		midiFile*		For non-GM features that relate to the file, and have
32 **						no use once the file has been created, i.e. CreateFile
33 **						or SetTrack (those data is embedded into the file, but
34 **						not explicitly stored)
35 **		midiSong*		For operations that work across the song, i.e. SetTempo
36 **		midiTrack*		For operations on a specific track, i.e. AddNoteOn
37 */
38 
39 /*
40 ** Types because we're dealing with files, and need to be careful
41 */
42 typedef	unsigned char		BYTE;
43 typedef	uint16_t 		WORD;
44 typedef	uint32_t 		_DWORD;
45 typedef int					BOOL;
46 #ifndef TRUE
47 #define TRUE	1
48 #endif
49 #ifndef FALSE
50 #define FALSE	0
51 #endif
52 
53 
54 /*
55 ** MIDI Constants
56 */
57 #define MIDI_PPQN_DEFAULT		384
58 #define MIDI_VERSION_DEFAULT	1
59 
60 /*
61 ** MIDI Limits
62 */
63 #define MAX_MIDI_TRACKS			256
64 #define MAX_TRACK_POLYPHONY		64
65 
66 /*
67 ** MIDI structures, accessibly externably
68 */
69 typedef	void 	MIDI_FILE;
70 typedef struct {
71 					tMIDI_MSG	iType;
72 
73 					_DWORD		dt;		/* delta time */
74 					_DWORD		dwAbsPos;
75 					_DWORD		iMsgSize;
76 
77 					BOOL		bImpliedMsg;
78 					tMIDI_MSG	iImpliedMsg;
79 
80 					/* Raw data chunk */
81 					BYTE *data;		/* dynamic data block */
82 					_DWORD data_sz;
83 
84 					union {
85 						struct {
86 								int			iNote;
87 								int			iChannel;
88 								int			iVolume;
89 								} NoteOn;
90 						struct {
91 								int			iNote;
92 								int			iChannel;
93 								} NoteOff;
94 						struct {
95 								int			iNote;
96 								int			iChannel;
97 								int			iPressure;
98 								} NoteKeyPressure;
99 						struct {
100 								int			iChannel;
101 								tMIDI_CC	iControl;
102 								int			iParam;
103 								} NoteParameter;
104 						struct {
105 								int			iChannel;
106 								int			iProgram;
107 								} ChangeProgram;
108 						struct {
109 								int			iChannel;
110 								int			iPressure;
111 								} ChangePressure;
112 						struct {
113 								int			iChannel;
114 								int			iPitch;
115 								} PitchWheel;
116 						struct {
117 								tMIDI_META	iType;
118 								union {
119 									int					iMIDIPort;
120 									int					iSequenceNumber;
121 									struct {
122 										BYTE			*pData;
123 										} Text;
124 									struct {
125 										int				iBPM;
126 										} Tempo;
127 									struct {
128 										int				iHours, iMins;
129 										int				iSecs, iFrames,iFF;
130 										} SMPTE;
131 									struct {
132 										tMIDI_KEYSIG	iKey;
133 										} KeySig;
134 									struct {
135 										int				iNom, iDenom;
136 										} TimeSig;
137 									struct {
138 										BYTE			*pData;
139 										int				iSize;
140 										} Sequencer;
141 									} Data;
142 								} MetaEvent;
143 						struct {
144 								BYTE		*pData;
145 								int			iSize;
146 								} SysEx;
147 						} MsgData;
148 
149 				/* State information - Please treat these as private*/
150 				tMIDI_MSG	iLastMsgType;
151 				BYTE		iLastMsgChnl;
152 
153 				} MIDI_MSG;
154 
155 /*
156 ** midiFile* Prototypes
157 */
158 MIDI_FILE  *midiFileCreate(const char *pFilename, BOOL bOverwriteIfExists);
159 int			midiFileSetTracksDefaultChannel(MIDI_FILE *pMF, int iTrack, int iChannel);
160 int			midiFileGetTracksDefaultChannel(const MIDI_FILE *pMF, int iTrack);
161 BOOL		midiFileFlushTrack(MIDI_FILE *pMF, int iTrack, BOOL bFlushToEnd, _DWORD dwEndTimePos);
162 BOOL		midiFileSyncTracks(MIDI_FILE *pMF, int iTrack1, int iTrack2);
163 int			midiFileSetPPQN(MIDI_FILE *pMF, int PPQN);
164 int			midiFileGetPPQN(const MIDI_FILE *pMF);
165 int			midiFileSetVersion(MIDI_FILE *pMF, int iVersion);
166 int			midiFileGetVersion(const MIDI_FILE *pMF);
167 MIDI_FILE  *midiFileOpen(const char *pFilename);
168 BOOL		midiFileClose(MIDI_FILE *pMF);
169 
170 /*
171 ** midiSong* Prototypes
172 */
173 BOOL		midiSongAddSMPTEOffset(MIDI_FILE *pMF, int iTrack, int iHours, int iMins, int iSecs, int iFrames, int iFFrames);
174 BOOL		midiSongAddSimpleTimeSig(MIDI_FILE *pMF, int iTrack, int iNom, int iDenom);
175 BOOL		midiSongAddTimeSig(MIDI_FILE *pMF, int iTrack, int iNom, int iDenom, int iClockInMetroTick, int iNotated32nds);
176 BOOL		midiSongAddKeySig(MIDI_FILE *pMF, int iTrack, tMIDI_KEYSIG iKey);
177 BOOL		midiSongAddTempo(MIDI_FILE *pMF, int iTrack, int iTempo);
178 BOOL		midiSongAddMIDIPort(MIDI_FILE *pMF, int iTrack, int iPort);
179 BOOL		midiSongAddEndSequence(MIDI_FILE *pMF, int iTrack);
180 
181 /*
182 ** midiTrack* Prototypes
183 */
184 BOOL		midiTrackAddRaw(MIDI_FILE *pMF, int iTrack, int iDataSize, const BYTE *pData, BOOL bMovePtr, int iDeltaTime);
185 BOOL		midiTrackIncTime(MIDI_FILE *pMF, int iTrack, int iDeltaTime, BOOL bOverridePPQN);
186 BOOL		midiTrackAddText(MIDI_FILE *pMF, int iTrack, tMIDI_TEXT iType, const char *pTxt);
187 BOOL		midiTrackAddMsg(MIDI_FILE *pMF, int iTrack, tMIDI_MSG iMsg, int iParam1, int iParam2);
188 BOOL		midiTrackSetKeyPressure(MIDI_FILE *pMF, int iTrack, int iNote, int iAftertouch);
189 BOOL		midiTrackAddControlChange(MIDI_FILE *pMF, int iTrack, tMIDI_CC iCCType, int iParam);
190 BOOL		midiTrackAddProgramChange(MIDI_FILE *pMF, int iTrack, int iInstrPatch);
191 BOOL		midiTrackChangeKeyPressure(MIDI_FILE *pMF, int iTrack, int iDeltaPressure);
192 BOOL		midiTrackSetPitchWheel(MIDI_FILE *pMF, int iTrack, int iWheelPos);
193 BOOL		midiTrackAddNote(MIDI_FILE *pMF, int iTrack, int iNote, int iLength, int iVol, BOOL bAutoInc, BOOL bOverrideLength);
194 BOOL		midiTrackAddRest(MIDI_FILE *pMF, int iTrack, int iLength, BOOL bOverridePPQN);
195 BOOL		midiTrackGetEndPos(MIDI_FILE *pMF, int iTrack);
196 
197 /*
198 ** midiRead* Prototypes
199 */
200 int			midiReadGetNumTracks(const MIDI_FILE *pMF);
201 BOOL		midiReadGetNextMessage(const MIDI_FILE *pMF, int iTrack, MIDI_MSG *pMsg);
202 void		midiReadInitMessage(MIDI_MSG *pMsg);
203 void		midiReadFreeMessage(MIDI_MSG *pMsg);
204 
205 
206 #endif /* _MIDIFILE_H */
207