MusicKit  0.0.0
midifile.h
00001 /*
00002   $Id$
00003   Defined In: The MusicKit
00004 
00005   Description:
00006   Original Author: David Jaffe
00007 
00008   Copyright (c) 1988-1992, NeXT Computer, Inc.
00009   Portions Copyright (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT
00010   Portions Copyright (c) 1994 Stanford University
00011   Portions Copyright (c) 1999-2003 The MusicKit Project.
00012 */
00013 /*
00014 Modification history before CVS repository commital.
00015 
00016   02/25/90/daj - Changed to make instancable. Added sysexcl support.
00017   11/18/92/daj - Added evaluateTempo arg to beginWriting/reading
00018   04/02/99/lms - Made public header
00019 */
00020 #ifndef MK__midifile_H___
00021 #define MK__midifile_H___
00022 
00023 #import <Foundation/Foundation.h>
00024 
00025 /* The magic number appearing as the first 4 bytes of a MIDI file. */
00026 #define MK_MIDIMAGIC  ((int)1297377380)  // "MThd"
00027 
00028 /*
00029  * Only the two following metaevents are supported; data[0] contains one
00030  * of the following codes if the metaevent flag is set. The metaevents are
00031  * provided for reading. Separate functions exist to write metaevents.
00032  */
00033 
00034 #define MKMIDI_DEFAULTQUANTASIZE (1000)
00035 
00036 typedef enum MKMIDIMetaEvent {
00037     /* In all of the metaevents, data[0] is the metaevent itself. */
00038     MKMIDI_sequenceNumber = 0,
00039     /*
00040      * data[1] and data[2] contain high and low order bits of number. 
00041      */
00042     MKMIDI_text = 1,
00043     MKMIDI_copyright = 2,
00044     MKMIDI_sequenceOrTrackName = 3,
00045     MKMIDI_instrumentName = 4,
00046     MKMIDI_lyric = 5,
00047     MKMIDI_marker = 6,
00048     MKMIDI_cuePoint = 7,
00049     /* data[1]* specifies null-terminated text. 
00050      */
00051     /*
00052      * MKMIDI_channelprefix, should be implemented by midifile.m and 
00053      * should not be passed up to user. 
00054      */
00055     MKMIDI_trackChange,
00056     /*
00057      * Track change metaevent: data[1] and data[2] contain high/low order bits,
00058      * respectively, containing the track number. These events can only be 
00059      * encountered when reading a level-1 file.
00060      */
00061     MKMIDI_tempoChange,
00062     /*
00063      * Tempo change metaevent: data[1:3] contain 3 bytes of data.
00064      */
00065     MKMIDI_smpteOffset,
00066     /*
00067       data[1:5] are the 5 numbers hr mn sec fr ff
00068       */
00069     MKMIDI_timeSig,
00070     /* data is a single int, where 1-byte fields are nn dd cc bb */
00071     MKMIDI_keySig
00072     /*  data is a single short, where 1-byte fields are sf mi  */
00073 } MKMIDIMetaevent;
00074 
00075 typedef struct _midiFileInStruct {
00076     double tempo;       /* in quarter notes per minute */
00077     double timeScale;   /* timeScale * currentTime gives time in seconds */
00078     int currentTrack;   /* Current track number */
00079     int currentTime;    /* Current time in quanta. */
00080     int currentOffset;  /* Added by dirk */
00081     int division;       /* # of delta-time ticks per quarter. (See spec) */
00082     short format;       /* Level 0, 1 or 2 */
00083     int quantaSize;     /* In micro-seconds. */
00084     unsigned char runningStatus; /* Current MIDI running status */
00085     NSMutableData *midiStream;     /* Midifile stream */
00086     int curBufSize;     /* Size of data buffer */
00087     /* Info for current msg. These are passed up to caller */
00088     int quanta;         /* Time in quanta */
00089     BOOL metaeventFlag; /* YES if this is a meta-event */
00090     int nData;          /* Number of significant bytes in data */
00091     unsigned char *data;/* Data bytes */
00092     BOOL evaluateTempo;
00093     unsigned int streamPos; /*sb: used to keep track of position within stream, for reading and writing. */
00094 } MKMIDIFileIn;
00095 
00096 typedef struct _midiFileOutStruct {
00097     double tempo;     
00098     double timeScale; 
00099     int currentTrack;
00100     int division;
00101     int currentCount;
00102     int lastTime;
00103     NSMutableData *midiStream;
00104     int quantaSize;
00105     BOOL evaluateTempo;
00106 } MKMIDIFileOut;
00107 
00112 extern MKMIDIFileIn *MKMIDIFileBeginReading(NSMutableData *fileData, BOOL evaluateTempo);
00113 
00114 extern void MKMIDIFileEndReading(MKMIDIFileIn *p);
00115 
00122 extern int MKMIDIFileReadPreamble(MKMIDIFileIn *p, int *level, int *track_count);
00123 
00130 extern int MKMIDIFileReadEvent(MKMIDIFileIn *p);
00131 
00139 MKMIDIFileOut *MKMIDIFileBeginWriting(NSMutableData *midiStream, int level, NSString *sequenceName, BOOL evaluateTempo);
00140 
00144 extern int MKMIDIFileEndWriting(MKMIDIFileOut *p);
00145 
00150 extern int MKMIDIFileBeginWritingTrack(MKMIDIFileOut *p, NSString *trackName);
00151 
00156 extern int MKMIDIFileEndWritingTrack(MKMIDIFileOut *p, int quanta);
00157 
00158 extern int MKMIDIFileWriteTempo(MKMIDIFileOut *p, int quanta, double beatsPerMinute);
00159 
00160 extern int MKMIDIFileWriteEvent(MKMIDIFileOut *p, int quanta, int ndata, unsigned char *bytes);
00161 
00165 extern int MKMIDIFileWriteSysExcl(MKMIDIFileOut *p, int quanta, int ndata, unsigned char *bytes);
00166 
00170 extern int MKMIDIFileWriteSig(MKMIDIFileOut *p, int quanta, short metaevent, unsigned data);
00171 
00172 extern int MKMIDIFileWriteText(MKMIDIFileOut *p, int quanta, short metaevent, NSString *data);
00173 
00174 extern int MKMIDIFileWriteSMPTEoffset(MKMIDIFileOut *p,
00175                                       unsigned char hr,
00176                                       unsigned char min,
00177                                       unsigned char sec,
00178                                       unsigned char ff,
00179                                       unsigned char fr);
00180 
00181 int MKMIDIFileWriteSequenceNumber(MKMIDIFileOut *p, int data);
00182 
00183 #endif