libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmMidiFile.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef cmMidiFile_h
  2. #define cmMidiFile_h
  3. // MIDI file timing:
  4. // Messages in the MIDI file are time tagged with a delta offset in 'ticks'
  5. // from the previous message in the same track.
  6. //
  7. // A 'tick' can be converted to microsends as follows:
  8. //
  9. // microsecond per tick = micros per quarter note / ticks per quarter note
  10. //
  11. // MpT = MpQN / TpQN
  12. //
  13. // TpQN is given as a constant in the MIDI file header.
  14. // MpQN is given as the value of the MIDI file tempo message.
  15. //
  16. // See cmMidiFileSeekUSecs() for an example of converting ticks to milliseconds.
  17. typedef cmHandle_t cmMidiFileH_t;
  18. typedef unsigned cmMfRC_t;
  19. typedef struct
  20. {
  21. cmMidiByte_t hr;
  22. cmMidiByte_t min;
  23. cmMidiByte_t sec;
  24. cmMidiByte_t frm;
  25. cmMidiByte_t sfr;
  26. } cmMidiSmpte_t;
  27. typedef struct
  28. {
  29. cmMidiByte_t num;
  30. cmMidiByte_t den;
  31. cmMidiByte_t metro;
  32. cmMidiByte_t th2s;
  33. } cmMidiTimeSig_t;
  34. typedef struct
  35. {
  36. cmMidiByte_t key;
  37. cmMidiByte_t scale;
  38. } cmMidiKeySig_t;
  39. typedef struct
  40. {
  41. cmMidiByte_t ch;
  42. cmMidiByte_t d0;
  43. cmMidiByte_t d1;
  44. unsigned durTicks; // note duration calc'd by
  45. } cmMidiChMsg_t;
  46. typedef struct cmMidiTrackMsg_str
  47. {
  48. unsigned dtick; // delta ticks
  49. cmMidiByte_t status; // ch msg's have the channel value removed (it is stored in u.chMsgPtr->ch)
  50. cmMidiByte_t metaId; //
  51. unsigned short trkIdx; //
  52. unsigned byteCnt; // length of data pointed to by u.voidPtr (or any other pointer in the union)
  53. struct cmMidiTrackMsg_str* link; // link to next record in this track
  54. union
  55. {
  56. cmMidiByte_t bVal;
  57. unsigned iVal;
  58. unsigned short sVal;
  59. const char* text;
  60. const void* voidPtr;
  61. const cmMidiSmpte_t* smptePtr;
  62. const cmMidiTimeSig_t* timeSigPtr;
  63. const cmMidiKeySig_t* keySigPtr;
  64. const cmMidiChMsg_t* chMsgPtr;
  65. const cmMidiByte_t* sysExPtr;
  66. } u;
  67. } cmMidiTrackMsg_t;
  68. enum
  69. {
  70. kOkMfRC = cmOkRC, // 0
  71. kSysFopenFailMfRC, // 1
  72. kSysFreadFailMfRC, // 2
  73. kSysFseekFailMfRC, // 3
  74. kSysFtellFailMfRC, // 4
  75. kSysFcloseFailMfRC, // 5
  76. kNotAMidiFileMfRC, // 6
  77. kMemAllocFailMfRC, // 7
  78. kFileCorruptMfRC, // 8
  79. kMissingEoxMfRC, // 9
  80. kUnknownMetaIdMfRC, // 10
  81. kInvalidHandleMfRC, // 11
  82. kMissingNoteOffMfRC, // 12
  83. kInvalidStatusMfRC // 13
  84. };
  85. extern cmMidiFileH_t cmMidiFileNullHandle;
  86. cmMfRC_t cmMidiFileOpen( const char* fn, cmMidiFileH_t* hPtr, cmCtx_t* ctx );
  87. cmMfRC_t cmMidiFileClose( cmMidiFileH_t* hp );
  88. // Returns track count or kInvalidCnt if 'h' is invalid.
  89. unsigned cmMidiFileTrackCount( cmMidiFileH_t h );
  90. // Return midi file format id (0,1,2) or kInvalidId if 'h' is invalid.
  91. unsigned cmMidiFileType( cmMidiFileH_t h );
  92. // Returns ticks per quarter note or kInvalidMidiByte if 'h' is invalid or 0 if file uses SMPTE ticks per frame time base.
  93. unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h );
  94. // The file name used in an earlier call to midiFileOpen() or NULL if this
  95. // midi file did not originate from an actual file.
  96. const char* cmMidiFileName( cmMidiFileH_t h );
  97. // Returns SMPTE ticks per frame or kInvalidMidiByte if 'h' is invalid or 0 if file uses ticks per quarter note time base.
  98. cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h );
  99. // Returns SMPTE format or kInvalidMidiByte if 'h' is invalid or 0 if file uses ticks per quarter note time base.
  100. cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h );
  101. // Returns count of records in track 'trackIdx' or kInvalidCnt if 'h' is invalid.
  102. unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx );
  103. // Returns base of record chain from track 'trackIdx' or NULL if 'h' is invalid.
  104. const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx );
  105. // Returns the total count of records in the midi file and the number in the array returned by cmMidiFileMsgArray().
  106. // Return kInvalidCnt if 'h' is invalid.
  107. unsigned cmMidiFileMsgCount( cmMidiFileH_t h );
  108. // Returns a pointer to the base of an array of pointers to each record in the file sorted in ascending time order.
  109. // Returns NULL if 'h' is invalid.
  110. const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h );
  111. // Return a pointer to the first msg at or after 'usecsOffs' or kInvalidIdx if no
  112. // msg exists after 'usecsOffs'. Note that 'usecOffs' is an offset from the beginning
  113. // of the file.
  114. // On return *'msgUsecsPtr' is set to the actual time of the msg.
  115. // (which will be equal to or greater than 'usecsOffs').
  116. unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned usecsOffs, unsigned* msgUsecsPtr, unsigned* newMicrosPerTickPtr );
  117. double cmMidiFileDurSecs( cmMidiFileH_t h );
  118. // Convert the track message 'dtick' field to delta-microseconds.
  119. void cmMidiFileTickToMicros( cmMidiFileH_t h );
  120. // Calculate Note Duration
  121. void cmMidiFileCalcNoteDurations( cmMidiFileH_t h );
  122. // Set the delay prior to the first non-zero msg.
  123. void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks );
  124. // This function packs a track msg into a single consecutive
  125. // block of memory buf[ bufByteCnt ]. Call cmMidiFilePackTracMsgBufByteCount()
  126. // to get the required buffer length for any given cmMidiTrackMsg_t instance.
  127. cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt );
  128. unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m );
  129. void cmMidiFilePrint( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt );
  130. bool cmMidiFileIsNull( cmMidiFileH_t h );
  131. void cmMidiFileTest( const char* fn, cmCtx_t* ctx );
  132. #endif