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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef cmMidi_h
  2. #define cmMidi_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //( { file_desc:"MIDI utility constants and functions." kw:[midi]}
  7. enum
  8. {
  9. kMidiChCnt = 16,
  10. kInvalidMidiByte = 128,
  11. kMidiNoteCnt = kInvalidMidiByte,
  12. kMidiCtlCnt = kInvalidMidiByte,
  13. kMidiPgmCnt = kInvalidMidiByte,
  14. kInvalidMidiPitch = kInvalidMidiByte,
  15. kInvalidMidiVelocity = kInvalidMidiByte,
  16. kInvalidMidiCtl = kInvalidMidiByte,
  17. kInvalidMidiPgm = kInvalidMidiByte,
  18. kMidiSciPitchCharCnt = 5 // A#-1
  19. };
  20. // MIDI status bytes
  21. enum
  22. {
  23. kInvalidStatusMdId = 0x00,
  24. kNoteOffMdId = 0x80,
  25. kNoteOnMdId = 0x90,
  26. kPolyPresMdId = 0xa0,
  27. kCtlMdId = 0xb0,
  28. kPgmMdId = 0xc0,
  29. kChPresMdId = 0xd0,
  30. kPbendMdId = 0xe0,
  31. kSysExMdId = 0xf0,
  32. kSysComMtcMdId = 0xf1,
  33. kSysComSppMdId = 0xf2,
  34. kSysComSelMdId = 0xf3,
  35. kSysComUndef0MdId = 0xf4,
  36. kSysComUndef1MdId = 0xf5,
  37. kSysComTuneMdId = 0xf6,
  38. kSysComEoxMdId = 0xf7,
  39. kSysRtClockMdId = 0xf8,
  40. kSysRtUndef0MdId = 0xf9,
  41. kSysRtStartMdId = 0xfa,
  42. kSysRtContMdId = 0xfb,
  43. kSysRtStopMdId = 0xfc,
  44. kSysRtUndef1MdId = 0xfd,
  45. kSysRtSenseMdId = 0xfe,
  46. kSysRtResetMdId = 0xff,
  47. kMetaStId = 0xff,
  48. kSeqNumbMdId = 0x00,
  49. kTextMdId = 0x01,
  50. kCopyMdId = 0x02,
  51. kTrkNameMdId = 0x03,
  52. kInstrNameMdId = 0x04,
  53. kLyricsMdId = 0x05,
  54. kMarkerMdId = 0x06,
  55. kCuePointMdId = 0x07,
  56. kMidiChMdId = 0x20,
  57. kEndOfTrkMdId = 0x2f,
  58. kTempoMdId = 0x51,
  59. kSmpteMdId = 0x54,
  60. kTimeSigMdId = 0x58,
  61. kKeySigMdId = 0x59,
  62. kSeqSpecMdId = 0x7f,
  63. kInvalidMetaMdId = 0x80,
  64. kSustainCtlMdId = 0x40,
  65. kPortamentoCtlMdId = 0x41,
  66. kSostenutoCtlMdId = 0x42,
  67. kSoftPedalCtlMdId = 0x43,
  68. kLegatoCtlMdId = 0x44
  69. };
  70. typedef unsigned char cmMidiByte_t;
  71. //===============================================================================================
  72. // Utility Functions
  73. //
  74. #define cmMidiIsStatus( s ) (kNoteOffMdId <= (s) /*&& ((unsigned)(s)) <= kSysRtResetMdId*/ )
  75. #define cmMidiIsChStatus( s ) (kNoteOffMdId <= (s) && (s) < kSysExMdId)
  76. #define cmMidiIsNoteOn( s ) ( kNoteOnMdId <= (s) && (s) <= (kNoteOnMdId + kMidiChCnt) )
  77. #define cmMidiIsNoteOff( s, d1 ) ( (cmMidiIsNoteOn(s) && (d1)==0) || (kNoteOffMdId <= (s) && (s) <= (kNoteOffMdId + kMidiChCnt)) )
  78. #define cmMidiIsCtl( s ) ( kCtlMdId <= (s) && (s) <= (kCtlMdId + kMidiChCnt) )
  79. #define cmMidiIsSustainPedal( s, d0 ) ( kCtlMdId <= (s) && (s) <= (kCtlMdId + kMidiChCnt) && (d0)== kSustainCtlMdId )
  80. #define cmMidiIsSustainPedalDown( s, d0, d1) ( cmMidiIsSustainPedal(s,d0) && (d1)>=64 )
  81. #define cmMidiIsSustainPedalUp( s, d0, d1) ( cmMidiIsSustainPedal(s,d0) && (d1)<64 )
  82. #define cmMidiIsSostenutoPedal( s, d0 ) ( kCtlMdId <= (s) && (s) <= (kCtlMdId + kMidiChCnt) && (d0)== kSostenutoCtlMdId )
  83. #define cmMidiIsSostenutoPedalDown( s, d0, d1) ( cmMidiIsSostenutoPedal(s,d0) && (d1)>=64 )
  84. #define cmMidiIsSostenutoPedalUp( s, d0, d1) ( cmMidiIsSostenutoPedal(s,d0) && (d1)<64 )
  85. #define cmMidiIsPedal( s, d0 ) ( kCtlMdId <= (s) && (s) <= (kCtlMdId + kMidiChCnt) && (d0)>=kSustainCtlMdId && (d0)<=kLegatoCtlMdId )
  86. #define cmMidiIsPedalDown( s, d0, d1 ) ( cmMidiIsPedal(s,d0) && (d1)>=64 )
  87. #define cmMidiIsPedalUp( s, d0, d1 ) ( cmMidiIsPedal(s,d0) && (d1)<64 )
  88. const char* cmMidiStatusToLabel( cmMidiByte_t status );
  89. const char* cmMidiMetaStatusToLabel( cmMidiByte_t metaStatus );
  90. const char* cmMidiPedalLabel( cmMidiByte_t d0 );
  91. // Returns kInvalidMidiByte if status is not a valid status byte
  92. cmMidiByte_t cmMidiStatusToByteCount( cmMidiByte_t status );
  93. unsigned cmMidiTo14Bits( cmMidiByte_t d0, cmMidiByte_t d1 );
  94. void cmMidiSplit14Bits( unsigned v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref );
  95. int cmMidiToPbend( cmMidiByte_t d0, cmMidiByte_t d1 );
  96. void cmMidiSplitPbend( int v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref );
  97. //===============================================================================================
  98. // MIDI Communication data types
  99. //
  100. typedef struct
  101. {
  102. //unsigned deltaUs; // time since last MIDI msg in microseconds
  103. cmTimeSpec_t timeStamp;
  104. cmMidiByte_t status; // midi status byte
  105. cmMidiByte_t d0; // midi data byte 0
  106. cmMidiByte_t d1; // midi data byte 1
  107. cmMidiByte_t pad;
  108. } cmMidiMsg;
  109. typedef struct
  110. {
  111. void* cbDataPtr; // application supplied reference value from mdParserCreate()
  112. unsigned devIdx; // the device the msg originated from
  113. unsigned portIdx; // the port index on the source device
  114. cmMidiMsg* msgArray; // pointer to an array of 'msgCnt' mdMsg records or NULL if sysExMsg is non-NULL
  115. cmMidiByte_t* sysExMsg; // pointer to a sys-ex msg or NULL if msgArray is non-NULL (see note below)
  116. unsigned msgCnt; // count of mdMsg records or sys-ex bytes
  117. } cmMidiPacket_t;
  118. // Notes: If the sys-ex message can be contained in a single msg then
  119. // then the first msg byte is kSysExMdId and the last is kSysComEoxMdId.
  120. // If the sys-ex message is broken into multiple pieces then only the
  121. // first will begin with kSysExMdId and the last will end with kSysComEoxMdId.
  122. // If label is NULL or labelCharCnt==0 then a pointer to an internal static
  123. // buffer is returned. If label[] is given the it
  124. // should have at least 5 (kMidiPitchCharCnt) char's (including the terminating zero).
  125. // If 'pitch' is outside of the range 0-127 then a blank string is returned.
  126. const char* cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labelCharCnt );
  127. // Convert a scientific pitch to MIDI pitch. acc == 1 == sharp, acc == -1 == flat.
  128. // The pitch character must be in the range 'A' to 'G'. Upper or lower case is valid.
  129. // Return kInvalidMidiPitch if the arguments are not valid.
  130. cmMidiByte_t cmSciPitchToMidiPitch( cmChar_t pitch, int acc, int octave );
  131. // Scientific pitch string: [A-Ga-g][#b][#] where # may be -1 to 9.
  132. // Return kInvalidMidiPitch if sciPtichStr does not contain a valid
  133. // scientific pitch string. This function will convert C-1 to G9 to
  134. // valid MIDI pitch values 0 to 127. Scientific pitch strings outside
  135. // of this range will be returned as kInvalidMidiPitch.
  136. cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr );
  137. //)
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif