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.

cmMidi.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 cmMidiIsPedal( s, d0 ) ( kCtlMdId <= (s) && (s) <= (kCtlMdId + kMidiChCnt) && (d0)>=kSustainCtlMdId && (d0)<=kLegatoCtlMdId )
  83. #define cmMidiIsPedalDown( s, d0, d1 ) ( cmMidiIsPedal(s,d0) && (d1)>=64 )
  84. #define cmMidiIsPedalUp( s, d0, d1 ) ( cmMidiIsPedal(s,d0) && (d1)<64 )
  85. const char* cmMidiStatusToLabel( cmMidiByte_t status );
  86. const char* cmMidiMetaStatusToLabel( cmMidiByte_t metaStatus );
  87. const char* cmMidiPedalLabel( cmMidiByte_t d0 );
  88. // Returns kInvalidMidiByte if status is not a valid status byte
  89. cmMidiByte_t cmMidiStatusToByteCount( cmMidiByte_t status );
  90. unsigned cmMidiTo14Bits( cmMidiByte_t d0, cmMidiByte_t d1 );
  91. void cmMidiSplit14Bits( unsigned v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref );
  92. int cmMidiToPbend( cmMidiByte_t d0, cmMidiByte_t d1 );
  93. void cmMidiSplitPbend( int v, cmMidiByte_t* d0Ref, cmMidiByte_t* d1Ref );
  94. //===============================================================================================
  95. // MIDI Communication data types
  96. //
  97. typedef struct
  98. {
  99. //unsigned deltaUs; // time since last MIDI msg in microseconds
  100. cmTimeSpec_t timeStamp;
  101. cmMidiByte_t status; // midi status byte
  102. cmMidiByte_t d0; // midi data byte 0
  103. cmMidiByte_t d1; // midi data byte 1
  104. cmMidiByte_t pad;
  105. } cmMidiMsg;
  106. typedef struct
  107. {
  108. void* cbDataPtr; // application supplied reference value from mdParserCreate()
  109. unsigned devIdx; // the device the msg originated from
  110. unsigned portIdx; // the port index on the source device
  111. cmMidiMsg* msgArray; // pointer to an array of 'msgCnt' mdMsg records or NULL if sysExMsg is non-NULL
  112. cmMidiByte_t* sysExMsg; // pointer to a sys-ex msg or NULL if msgArray is non-NULL (see note below)
  113. unsigned msgCnt; // count of mdMsg records or sys-ex bytes
  114. } cmMidiPacket_t;
  115. // Notes: If the sys-ex message can be contained in a single msg then
  116. // then the first msg byte is kSysExMdId and the last is kSysComEoxMdId.
  117. // If the sys-ex message is broken into multiple pieces then only the
  118. // first will begin with kSysExMdId and the last will end with kSysComEoxMdId.
  119. // If label is NULL or labelCharCnt==0 then a pointer to an internal static
  120. // buffer is returned. If label[] is given the it
  121. // should have at least 5 (kMidiPitchCharCnt) char's (including the terminating zero).
  122. // If 'pitch' is outside of the range 0-127 then a blank string is returned.
  123. const char* cmMidiToSciPitch( cmMidiByte_t pitch, char* label, unsigned labelCharCnt );
  124. // Scientific pitch string: [A-Ga-g][#b][#] where # may be -1 to 9.
  125. // Return kInvalidMidiPitch if sciPtichStr does not contain a valid
  126. // scientific pitch string. This function will convert C-1 to G9 to
  127. // valid MIDI pitch values 0 to 127. Scientific pitch strings outside
  128. // of this range will be returned as kInvalidMidiPitch.
  129. cmMidiByte_t cmSciPitchToMidi( const char* sciPitchStr );
  130. //)
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif