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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. typedef struct
  49. {
  50. cmMidiByte_t ch;
  51. cmMidiByte_t d0;
  52. cmMidiByte_t d1;
  53. unsigned durTicks; // note duration calc'd by cmMidiFileCalcNoteDurations();
  54. } cmMidiChMsg_t;
  55. typedef struct cmMidiTrackMsg_str
  56. {
  57. unsigned uid; // uid's are unique among all msg's in the file
  58. unsigned dtick; // delta ticks
  59. unsigned atick; // accumulated ticks
  60. cmMidiByte_t status; // ch msg's have the channel value removed (it is stored in u.chMsgPtr->ch)
  61. cmMidiByte_t metaId; //
  62. unsigned short trkIdx; //
  63. unsigned byteCnt; // length of data pointed to by u.voidPtr (or any other pointer in the union)
  64. struct cmMidiTrackMsg_str* link; // link to next record in this track
  65. union
  66. {
  67. cmMidiByte_t bVal;
  68. unsigned iVal;
  69. unsigned short sVal;
  70. const char* text;
  71. const void* voidPtr;
  72. const cmMidiSmpte_t* smptePtr;
  73. const cmMidiTimeSig_t* timeSigPtr;
  74. const cmMidiKeySig_t* keySigPtr;
  75. const cmMidiChMsg_t* chMsgPtr;
  76. const cmMidiByte_t* sysExPtr;
  77. } u;
  78. } cmMidiTrackMsg_t;
  79. enum
  80. {
  81. kOkMfRC = cmOkRC, // 0
  82. kFileFailMfRC, // 1
  83. kNotAMidiFileMfRC, // 2
  84. kMemAllocFailMfRC, // 3
  85. kFileCorruptMfRC, // 4
  86. kMissingEoxMfRC, // 5
  87. kUnknownMetaIdMfRC, // 6
  88. kInvalidHandleMfRC, // 7
  89. kMissingNoteOffMfRC, // 8
  90. kInvalidStatusMfRC, // 9
  91. kSustainPedalMfRC // 10
  92. };
  93. extern cmMidiFileH_t cmMidiFileNullHandle;
  94. cmMfRC_t cmMidiFileOpen( const char* fn, cmMidiFileH_t* hPtr, cmCtx_t* ctx );
  95. cmMfRC_t cmMidiFileClose( cmMidiFileH_t* hp );
  96. cmMfRC_t cmMidiFileWrite( cmMidiFileH_t h, const char* fn );
  97. bool cmMidiFileIsValid( cmMidiFileH_t h );
  98. // Returns track count or kInvalidCnt if 'h' is invalid.
  99. unsigned cmMidiFileTrackCount( cmMidiFileH_t h );
  100. // Return midi file format id (0,1,2) or kInvalidId if 'h' is invalid.
  101. unsigned cmMidiFileType( cmMidiFileH_t h );
  102. // Returns ticks per quarter note or kInvalidMidiByte if 'h' is invalid or 0 if file uses SMPTE ticks per frame time base.
  103. unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h );
  104. // The file name used in an earlier call to midiFileOpen() or NULL if this
  105. // midi file did not originate from an actual file.
  106. const char* cmMidiFileName( cmMidiFileH_t h );
  107. // Returns SMPTE ticks per frame or kInvalidMidiByte if 'h' is invalid or 0 if file uses ticks per quarter note time base.
  108. cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h );
  109. // Returns SMPTE format or kInvalidMidiByte if 'h' is invalid or 0 if file uses ticks per quarter note time base.
  110. cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h );
  111. // Returns count of records in track 'trackIdx' or kInvalidCnt if 'h' is invalid.
  112. unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx );
  113. // Returns base of record chain from track 'trackIdx' or NULL if 'h' is invalid.
  114. const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx );
  115. // Returns the total count of records in the midi file and the number in the array returned by cmMidiFileMsgArray().
  116. // Return kInvalidCnt if 'h' is invalid.
  117. unsigned cmMidiFileMsgCount( cmMidiFileH_t h );
  118. // Returns a pointer to the base of an array of pointers to each record in the file sorted in ascending time order.
  119. // Returns NULL if 'h' is invalid.
  120. const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h );
  121. // Return a pointer to the first msg at or after 'usecsOffs' or kInvalidIdx if no
  122. // msg exists after 'usecsOffs'. Note that 'usecOffs' is an offset from the beginning
  123. // of the file.
  124. // On return *'msgUsecsPtr' is set to the actual time of the msg.
  125. // (which will be equal to or greater than 'usecsOffs').
  126. unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned usecsOffs, unsigned* msgUsecsPtr, unsigned* newMicrosPerTickPtr );
  127. double cmMidiFileDurSecs( cmMidiFileH_t h );
  128. // Convert the track message 'dtick' field to delta-microseconds.
  129. void cmMidiFileTickToMicros( cmMidiFileH_t h );
  130. // Convert the track message 'dtick' field to samples.
  131. // If the absFl is set then the delta times are converted to absolute time.
  132. void cmMidiFileTickToSamples( cmMidiFileH_t h, double srate, bool absFl );
  133. // Calculate Note Duration
  134. void cmMidiFileCalcNoteDurations( cmMidiFileH_t h );
  135. // Set the delay prior to the first non-zero msg.
  136. void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks );
  137. // This function packs a track msg into a single consecutive
  138. // block of memory buf[ bufByteCnt ]. Call cmMidiFilePackTracMsgBufByteCount()
  139. // to get the required buffer length for any given cmMidiTrackMsg_t instance.
  140. cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt );
  141. unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m );
  142. void cmMidiFilePrintMsgs( cmMidiFileH_t h, cmRpt_t* rpt );
  143. void cmMidiFilePrintTrack( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt );
  144. bool cmMidiFileIsNull( cmMidiFileH_t h );
  145. void cmMidiFileTest( const char* fn, cmCtx_t* ctx );
  146. //)
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif