libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmMidiFile.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. typedef struct cmMidiTrackMsg_str
  58. {
  59. unsigned uid; // uid's are unique among all msg's in the file
  60. unsigned dtick; // delta ticks between events on this track
  61. unsigned long long atick; // global (all tracks interleaved) accumulated ticks
  62. unsigned long long amicro; // global (all tracks interleaved) accumulated microseconds adjusted for tempo changes
  63. cmMidiByte_t status; // ch msg's have the channel value removed (it is stored in u.chMsgPtr->ch)
  64. cmMidiByte_t metaId; //
  65. unsigned short trkIdx; //
  66. unsigned byteCnt; // length of data pointed to by u.voidPtr (or any other pointer in the union)
  67. struct cmMidiTrackMsg_str* link; // link to next record in this track
  68. union
  69. {
  70. cmMidiByte_t bVal;
  71. unsigned iVal;
  72. unsigned short sVal;
  73. const char* text;
  74. const void* voidPtr;
  75. const cmMidiSmpte_t* smptePtr;
  76. const cmMidiTimeSig_t* timeSigPtr;
  77. const cmMidiKeySig_t* keySigPtr;
  78. const cmMidiChMsg_t* chMsgPtr;
  79. const cmMidiByte_t* sysExPtr;
  80. } u;
  81. } cmMidiTrackMsg_t;
  82. #define cmMidiFileIsNoteOn(m) (cmMidiIsNoteOn((m)->status) && (m)->u.chMsgPtr->d1>0)
  83. #define cmMidiFileIsNoteOff(m) (cmMidiIsNoteOff((m)->status,(m)->u.chMsgPtr->d1))
  84. #define cmMidiFileIsSustainPedalUp(m) (cmMidiIsSustainPedalUp( (m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  85. #define cmMidiFileIsSustainPedalDown(m) (cmMidiIsSustainPedalDown( (m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  86. #define cmMidiFileIsSostenutoPedalUp(m) (cmMidiIsSostenutoPedalUp( (m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  87. #define cmMidiFileIsSostenutoPedalDown(m) (cmMidiIsSostenutoPedalDown((m)->status,(m)->u.chMsgPtr->d0,(m)->u.chMsgPtr->d1))
  88. enum
  89. {
  90. kOkMfRC = cmOkRC, // 0
  91. kFileFailMfRC, // 1
  92. kNotAMidiFileMfRC, // 2
  93. kMemAllocFailMfRC, // 3
  94. kFileCorruptMfRC, // 4
  95. kMissingEoxMfRC, // 5
  96. kUnknownMetaIdMfRC, // 6
  97. kInvalidHandleMfRC, // 7
  98. kMissingNoteOffMfRC, // 8
  99. kInvalidStatusMfRC, // 9
  100. kSustainPedalMfRC, // 10
  101. kSostenutoPedalMfRC, // 11
  102. kLargeDeltaTickMfRC, // 12 (a large delta tick value was filtered)
  103. kUidNotFoundMfRC, // 13
  104. kUidNotANoteMsgMfRC, // 14
  105. kInvalidTrkIndexMfRC // 15
  106. };
  107. extern cmMidiFileH_t cmMidiFileNullHandle;
  108. cmMfRC_t cmMidiFileOpen( cmCtx_t* ctx, cmMidiFileH_t* h, const char* fn );
  109. cmMfRC_t cmMidiFileCreate( cmCtx_t* ctx, cmMidiFileH_t* hp, unsigned trkN, unsigned ticksPerQN );
  110. cmMfRC_t cmMidiFileClose( cmMidiFileH_t* hp );
  111. cmMfRC_t cmMidiFileWrite( cmMidiFileH_t h, const char* fn );
  112. bool cmMidiFileIsValid( cmMidiFileH_t h );
  113. // Returns track count or kInvalidCnt if 'h' is invalid.
  114. unsigned cmMidiFileTrackCount( cmMidiFileH_t h );
  115. // Return midi file format id (0,1,2) or kInvalidId if 'h' is invalid.
  116. unsigned cmMidiFileType( cmMidiFileH_t h );
  117. // Returns ticks per quarter note or kInvalidMidiByte if 'h' is invalid or 0 if file uses SMPTE ticks per frame time base.
  118. unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h );
  119. // The file name used in an earlier call to midiFileOpen() or NULL if this
  120. // midi file did not originate from an actual file.
  121. const char* cmMidiFileName( cmMidiFileH_t h );
  122. // Returns SMPTE ticks per frame or kInvalidMidiByte if 'h' is invalid or 0 if file uses ticks per quarter note time base.
  123. cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h );
  124. // Returns SMPTE format or kInvalidMidiByte if 'h' is invalid or 0 if file uses ticks per quarter note time base.
  125. cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h );
  126. // Returns count of records in track 'trackIdx' or kInvalidCnt if 'h' is invalid.
  127. unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx );
  128. // Returns base of record chain from track 'trackIdx' or NULL if 'h' is invalid.
  129. const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx );
  130. // Returns the total count of records in the midi file and the number in the array returned by cmMidiFileMsgArray().
  131. // Return kInvalidCnt if 'h' is invalid.
  132. unsigned cmMidiFileMsgCount( cmMidiFileH_t h );
  133. // Returns a pointer to the base of an array of pointers to each record in the file sorted in ascending time order.
  134. // Returns NULL if 'h' is invalid.
  135. const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h );
  136. // Set the velocity of a note-on/off msg identified by 'uid'.
  137. cmMfRC_t cmMidiFileSetVelocity( cmMidiFileH_t h, unsigned uid, cmMidiByte_t vel );
  138. // Insert a MIDI message relative to the reference msg identified by 'uid'.
  139. // If dtick is positive/negative then the new msg is inserted after/before the reference msg.
  140. cmMfRC_t cmMidiFileInsertMsg( cmMidiFileH_t h, unsigned uid, int dtick, cmMidiByte_t ch, cmMidiByte_t status, cmMidiByte_t d0, cmMidiByte_t d1 );
  141. cmMfRC_t cmMidiFileInsertTrackMsg( cmMidiFileH_t h, unsigned trkIdx, const cmMidiTrackMsg_t* msg );
  142. cmMfRC_t cmMidiFileInsertTrackChMsg( cmMidiFileH_t h, unsigned trkIdx, unsigned atick, cmMidiByte_t status, cmMidiByte_t d0, cmMidiByte_t d1 );
  143. cmMfRC_t cmMidFileInsertTrackTempoMsg( cmMidiFileH_t h, unsigned trkIdx, unsigned atick, unsigned bpm );
  144. // Return a pointer to the first msg at or after 'usecsOffs' or kInvalidIdx if no
  145. // msg exists after 'usecsOffs'. Note that 'usecOffs' is an offset from the beginning
  146. // of the file.
  147. // On return *'msgUsecsPtr' is set to the actual time of the msg.
  148. // (which will be equal to or greater than 'usecsOffs').
  149. unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned long long usecsOffs, unsigned* msgUsecsPtr, unsigned* newMicrosPerTickPtr );
  150. double cmMidiFileDurSecs( cmMidiFileH_t h );
  151. // Calculate Note Duration
  152. void cmMidiFileCalcNoteDurations( cmMidiFileH_t h );
  153. // Set the delay prior to the first non-zero msg.
  154. void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks );
  155. // This function packs a track msg into a single consecutive
  156. // block of memory buf[ bufByteCnt ]. Call cmMidiFilePackTracMsgBufByteCount()
  157. // to get the required buffer length for any given cmMidiTrackMsg_t instance.
  158. cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt );
  159. unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m );
  160. void cmMidiFilePrintMsgs( cmMidiFileH_t h, cmRpt_t* rpt );
  161. void cmMidiFilePrintTrack( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt );
  162. void cmMidiFileTest( const char* fn, cmCtx_t* ctx );
  163. // Generate a piano-roll plot description file which can be displayed with cmXScore.m
  164. cmMfRC_t cmMidiFileGenPlotFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outFn );
  165. //)
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169. #endif