libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmMidiFile.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #ifndef cmMidiFile_h
  2. #define cmMidiFile_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //( { file_desc:"MIDI file reader and writer." kw:[midi file]}
  7. // MIDI file timing:
  8. // Messages in the MIDI file are time tagged with a delta offset in 'ticks'
  9. // from the previous message in the same track.
  10. //
  11. // A 'tick' can be converted to microsends as follows:
  12. //
  13. // microsecond per tick = micros per quarter note / ticks per quarter note
  14. //
  15. // MpT = MpQN / TpQN
  16. //
  17. // TpQN is given as a constant in the MIDI file header.
  18. // MpQN is given as the value of the MIDI file tempo message.
  19. //
  20. // See cmMidiFileSeekUSecs() for an example of converting ticks to milliseconds.
  21. //
  22. // As part of the file reading process, the status byte of note-on messages
  23. // with velocity=0 are is changed to a note-off message. See _cmMidiFileReadChannelMsg().
  24. //)
  25. //(
  26. typedef cmHandle_t cmMidiFileH_t;
  27. typedef unsigned cmMfRC_t;
  28. typedef struct
  29. {
  30. cmMidiByte_t hr;
  31. cmMidiByte_t min;
  32. cmMidiByte_t sec;
  33. cmMidiByte_t frm;
  34. cmMidiByte_t sfr;
  35. } cmMidiSmpte_t;
  36. typedef struct
  37. {
  38. cmMidiByte_t num;
  39. cmMidiByte_t den;
  40. cmMidiByte_t metro;
  41. cmMidiByte_t th2s;
  42. } cmMidiTimeSig_t;
  43. typedef struct
  44. {
  45. cmMidiByte_t key;
  46. cmMidiByte_t scale;
  47. } cmMidiKeySig_t;
  48. struct cmMidiTrackMsg_str;
  49. typedef struct
  50. {
  51. cmMidiByte_t ch;
  52. cmMidiByte_t d0;
  53. cmMidiByte_t d1;
  54. unsigned durMicros; // note duration in microseconds (corrected for tempo changes)
  55. struct cmMidiTrackMsg_str* end; // note-off or pedal-up message
  56. } cmMidiChMsg_t;
  57. enum
  58. {
  59. kDropTrkMsgFl = 0x01
  60. };
  61. typedef struct cmMidiTrackMsg_str
  62. {
  63. unsigned flags; // see k???TrkMsgFl
  64. unsigned uid; // uid's are unique among all msg's in the file
  65. unsigned dtick; // delta ticks between events on this track (ticks between this event and the previous event on this track)
  66. unsigned long long atick; // global (all tracks interleaved) accumulated ticks
  67. unsigned long long amicro; // global (all tracks interleaved) accumulated microseconds adjusted for tempo changes
  68. cmMidiByte_t status; // ch msg's have the channel value removed (it is stored in u.chMsgPtr->ch)
  69. cmMidiByte_t metaId; //
  70. unsigned short trkIdx; //
  71. unsigned byteCnt; // length of data pointed to by u.voidPtr (or any other pointer in the union)
  72. struct cmMidiTrackMsg_str* link; // link to next record in this track
  73. union
  74. {
  75. cmMidiByte_t bVal;
  76. unsigned iVal;
  77. unsigned short sVal;
  78. const char* text;
  79. const void* voidPtr;
  80. const cmMidiSmpte_t* smptePtr;
  81. const cmMidiTimeSig_t* timeSigPtr;
  82. const cmMidiKeySig_t* keySigPtr;
  83. const cmMidiChMsg_t* chMsgPtr;
  84. const cmMidiByte_t* sysExPtr;
  85. } u;
  86. } cmMidiTrackMsg_t;
  87. #define cmMidiFileIsNoteOn(m) (cmMidiIsNoteOn((m)->status) && ((m)->u.chMsgPtr->d1>0))
  88. #define cmMidiFileIsNoteOff(m) (cmMidiIsNoteOff((m)->status,(m)->u.chMsgPtr->d1))
  89. #define cmMidiFileIsPedalUp(m) (cmMidiIsPedalUp( (m)->status, (m)->u.chMsgPtr->d0, (m)->u.chMsgPtr->d1) )
  90. #define cmMidiFileIsPedalDown(m) (cmMidiIsPedalDown( (m)->status, (m)->u.chMsgPtr->d0, (m)->u.chMsgPtr->d1) )
  91. #define cmMidiFileIsSustainPedalUp(m) (cmMidiIsSustainPedalUp( (m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  92. #define cmMidiFileIsSustainPedalDown(m) (cmMidiIsSustainPedalDown( (m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  93. #define cmMidiFileIsSostenutoPedalUp(m) (cmMidiIsSostenutoPedalUp( (m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  94. #define cmMidiFileIsSostenutoPedalDown(m) (cmMidiIsSostenutoPedalDown((m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  95. enum
  96. {
  97. kOkMfRC = cmOkRC, // 0
  98. kFileFailMfRC, // 1
  99. kNotAMidiFileMfRC, // 2
  100. kMemAllocFailMfRC, // 3
  101. kFileCorruptMfRC, // 4
  102. kMissingEoxMfRC, // 5
  103. kUnknownMetaIdMfRC, // 6
  104. kInvalidHandleMfRC, // 7
  105. kMissingNoteOffMfRC, // 8
  106. kInvalidStatusMfRC, // 9
  107. kSustainPedalMfRC, // 10
  108. kSostenutoPedalMfRC, // 11
  109. kLargeDeltaTickMfRC, // 12 (a large delta tick value was filtered)
  110. kUidNotFoundMfRC, // 13
  111. kUidNotANoteMsgMfRC, // 14
  112. kInvalidTrkIndexMfRC,// 15
  113. kSvgFailMfRC, // 16
  114. kMsgNotFoundMfRC, // 17
  115. kEventTerminationMfRC // 18
  116. };
  117. extern cmMidiFileH_t cmMidiFileNullHandle;
  118. cmMfRC_t cmMidiFileOpen( cmCtx_t* ctx, cmMidiFileH_t* h, const char* fn );
  119. cmMfRC_t cmMidiFileCreate( cmCtx_t* ctx, cmMidiFileH_t* hp, unsigned trkN, unsigned ticksPerQN );
  120. cmMfRC_t cmMidiFileClose( cmMidiFileH_t* hp );
  121. cmMfRC_t cmMidiFileWrite( cmMidiFileH_t h, const char* fn );
  122. bool cmMidiFileIsValid( cmMidiFileH_t h );
  123. // Returns track count or kInvalidCnt if 'h' is invalid.
  124. unsigned cmMidiFileTrackCount( cmMidiFileH_t h );
  125. // Return midi file format id (0,1,2) or kInvalidId if 'h' is invalid.
  126. unsigned cmMidiFileType( cmMidiFileH_t h );
  127. // Returns ticks per quarter note or kInvalidMidiByte if 'h' is
  128. // invalid or 0 if file uses SMPTE ticks per frame time base.
  129. unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h );
  130. // The file name used in an earlier call to midiFileOpen() or NULL if this
  131. // midi file did not originate from an actual file.
  132. const char* cmMidiFileName( cmMidiFileH_t h );
  133. // Returns SMPTE ticks per frame or kInvalidMidiByte if 'h' is
  134. // invalid or 0 if file uses ticks per quarter note time base.
  135. cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h );
  136. // Returns SMPTE format or kInvalidMidiByte if 'h' is invalid or 0
  137. // if file uses ticks per quarter note time base.
  138. cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h );
  139. // Returns count of records in track 'trackIdx' or kInvalidCnt if 'h' is invalid.
  140. unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx );
  141. // Returns base of record chain from track 'trackIdx' or NULL if 'h' is invalid.
  142. const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx );
  143. // Returns the total count of records in the midi file and the
  144. // number in the array returned by cmMidiFileMsgArray().
  145. // Return kInvalidCnt if 'h' is invalid.
  146. unsigned cmMidiFileMsgCount( cmMidiFileH_t h );
  147. // Returns a pointer to the base of an array of pointers to all records
  148. // in the file sorted in ascending time order.
  149. // Returns NULL if 'h' is invalid.
  150. const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h );
  151. // Set the velocity of a note-on/off msg identified by 'uid'.
  152. cmMfRC_t cmMidiFileSetVelocity( cmMidiFileH_t h, unsigned uid, cmMidiByte_t vel );
  153. // Insert a MIDI message relative to the reference msg identified by 'uid'.
  154. // If dtick is positive/negative then the new msg is inserted after/before the reference msg.
  155. cmMfRC_t cmMidiFileInsertMsg( cmMidiFileH_t h, unsigned uid, int dtick, cmMidiByte_t ch, cmMidiByte_t status, cmMidiByte_t d0, cmMidiByte_t d1 );
  156. //
  157. // Insert a new cmMidiTrackMsg_t into the MIDI file on the specified track.
  158. //
  159. // Only the following fields need be set in 'msg'.
  160. // atick - used to position the msg in the track
  161. // status - this field is always set (Note that channel information must stripped from the status byte and included in the channel msg data)
  162. // metaId - this field is optional depending on the msg type
  163. // byteCnt - used to allocate storage for the data element in 'cmMidiTrackMsg_t.u'
  164. // u - the message data
  165. //
  166. cmMfRC_t cmMidiFileInsertTrackMsg( cmMidiFileH_t h, unsigned trkIdx, const cmMidiTrackMsg_t* msg );
  167. cmMfRC_t cmMidiFileInsertTrackChMsg( cmMidiFileH_t h, unsigned trkIdx, unsigned atick, cmMidiByte_t status, cmMidiByte_t d0, cmMidiByte_t d1 );
  168. cmMfRC_t cmMidFileInsertTrackTempoMsg( cmMidiFileH_t h, unsigned trkIdx, unsigned atick, unsigned bpm );
  169. // Return a pointer to the first msg at or after 'usecsOffs' or kInvalidIdx if no
  170. // msg exists after 'usecsOffs'. Note that 'usecOffs' is an offset from the beginning
  171. // of the file.
  172. // On return *'msgUsecsPtr' is set to the actual time of the msg.
  173. // (which will be equal to or greater than 'usecsOffs').
  174. unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned long long usecsOffs, unsigned* msgUsecsPtr, unsigned* newMicrosPerTickPtr );
  175. double cmMidiFileDurSecs( cmMidiFileH_t h );
  176. // Calculate Note Duration
  177. enum { kWarningsMfFl=0x01, kDropReattacksMfFl=0x02 };
  178. void cmMidiFileCalcNoteDurations( cmMidiFileH_t h, unsigned flags );
  179. // Set the delay prior to the first non-zero msg.
  180. void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks );
  181. // This function packs a track msg into a single consecutive
  182. // block of memory buf[ bufByteCnt ]. Call cmMidiFilePackTracMsgBufByteCount()
  183. // to get the required buffer length for any given cmMidiTrackMsg_t instance.
  184. cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt );
  185. unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m );
  186. void cmMidiFilePrintMsgs( cmMidiFileH_t h, cmRpt_t* rpt );
  187. void cmMidiFilePrintTrack( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt );
  188. typedef struct
  189. {
  190. unsigned uid;
  191. unsigned long long amicro;
  192. unsigned density;
  193. } cmMidiFileDensity_t;
  194. // Generate the note onset density measure for each note in the MIDI file.
  195. // Delete the returned memory with a call to cmMemFree().
  196. cmMidiFileDensity_t* cmMidiFileNoteDensity( cmMidiFileH_t h, unsigned* cntRef );
  197. // Generate a piano-roll plot description file which can be displayed with cmXScore.m
  198. cmMfRC_t cmMidiFileGenPlotFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outFn );
  199. cmMfRC_t cmMidiFileGenSvgFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outSvgFn, const cmChar_t* cssFn, bool standAloneFl, bool panZoomFl );
  200. // Generate a text file reportusing cmMIdiFilePrintMsgs()
  201. cmMfRC_t cmMidiFileReport( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outTextFn );
  202. void cmMidiFileTest( const char* fn, cmCtx_t* ctx );
  203. //)
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif