usb_midi.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USB_MIDI_H
  7. #define USB_MIDI_H
  8. /* bDescriptorSubType */
  9. #define MIDI_VC_HEADER_DESCRIPTOR_SUBTYPE 0x01U
  10. #define MIDI_MS_HEADER_DESCRIPTOR_SUBTYPE 0x01U
  11. #define MIDI_MS_GENERAL_DESCRIPTOR_SUBTYPE 0x01U
  12. #define MIDI_MIDI_IN_JACK_DESCRIPTOR_SUBTYPE 0x02U
  13. #define MIDI_MIDI_OUT_JACK_DESCRIPTOR_SUBTYPE 0x03U
  14. /* bJackType */
  15. #define MIDI_JACK_TYPE_EMBEDDED 0x01
  16. #define MIDI_JACK_TYPE_EXTERNAL 0x02
  17. #define MIDI_CHANNEL_OMNI 0
  18. #define MIDI_CHANNEL_OFF 17
  19. #define MIDI_PITCHBEND_MIN -8192
  20. #define MIDI_PITCHBEND_MAX 8191
  21. /*! Enumeration of MIDI code index number */
  22. enum MidiCodeIndexNumber {
  23. MIDI_CIN_MISC = 0,
  24. MIDI_CIN_CABLE_EVENT = 1,
  25. MIDI_CIN_SYSCOM_2BYTE = 2, ///< 2 byte system common message e.g MTC, SongSelect
  26. MIDI_CIN_SYSCOM_3BYTE = 3, ///< 3 byte system common message e.g SPP
  27. MIDI_CIN_SYSEX_START = 4, ///< SysEx starts or continue
  28. MIDI_CIN_SYSEX_END_1BYTE = 5, ///< SysEx ends with 1 data, or 1 byte system common message
  29. MIDI_CIN_SYSEX_END_2BYTE = 6, ///< SysEx ends with 2 data
  30. MIDI_CIN_SYSEX_END_3BYTE = 7, ///< SysEx ends with 3 data
  31. MIDI_CIN_NOTE_OFF = 8,
  32. MIDI_CIN_NOTE_ON = 9,
  33. MIDI_CIN_POLY_KEYPRESS = 10,
  34. MIDI_CIN_CONTROL_CHANGE = 11,
  35. MIDI_CIN_PROGRAM_CHANGE = 12,
  36. MIDI_CIN_CHANNEL_PRESSURE = 13,
  37. MIDI_CIN_PITCH_BEND_CHANGE = 14,
  38. MIDI_CIN_1BYTE_DATA = 15
  39. };
  40. /*! Enumeration of MIDI types */
  41. enum MidiType {
  42. InvalidType = 0x00, ///< For notifying errors
  43. NoteOff = 0x80, ///< Note Off
  44. NoteOn = 0x90, ///< Note On
  45. AfterTouchPoly = 0xA0, ///< Polyphonic AfterTouch
  46. ControlChange = 0xB0, ///< Control Change / Channel Mode
  47. ProgramChange = 0xC0, ///< Program Change
  48. AfterTouchChannel = 0xD0, ///< Channel (monophonic) AfterTouch
  49. PitchBend = 0xE0, ///< Pitch Bend
  50. SystemExclusive = 0xF0, ///< System Exclusive
  51. TimeCodeQuarterFrame = 0xF1, ///< System Common - MIDI Time Code Quarter Frame
  52. SongPosition = 0xF2, ///< System Common - Song Position Pointer
  53. SongSelect = 0xF3, ///< System Common - Song Select
  54. TuneRequest = 0xF6, ///< System Common - Tune Request
  55. Clock = 0xF8, ///< System Real Time - Timing Clock
  56. Start = 0xFA, ///< System Real Time - Start
  57. Continue = 0xFB, ///< System Real Time - Continue
  58. Stop = 0xFC, ///< System Real Time - Stop
  59. ActiveSensing = 0xFE, ///< System Real Time - Active Sensing
  60. SystemReset = 0xFF, ///< System Real Time - System Reset
  61. };
  62. /*! Enumeration of Thru filter modes */
  63. enum MidiFilterMode {
  64. Off = 0, ///< Thru disabled (nothing passes through).
  65. Full = 1, ///< Fully enabled Thru (every incoming message is sent back).
  66. SameChannel = 2, ///< Only the messages on the Input Channel will be sent back.
  67. DifferentChannel = 3, ///< All the messages but the ones on the Input Channel will be sent back.
  68. };
  69. /*! \brief Enumeration of Control Change command numbers.
  70. See the detailed controllers numbers & description here:
  71. http://www.somascape.org/midi/tech/spec.html#ctrlnums
  72. */
  73. enum MidiControlChangeNumber {
  74. // High resolution Continuous Controllers MSB (+32 for LSB) ----------------
  75. BankSelect = 0,
  76. ModulationWheel = 1,
  77. BreathController = 2,
  78. // CC3 undefined
  79. FootController = 4,
  80. PortamentoTime = 5,
  81. DataEntry = 6,
  82. ChannelVolume = 7,
  83. Balance = 8,
  84. // CC9 undefined
  85. Pan = 10,
  86. ExpressionController = 11,
  87. EffectControl1 = 12,
  88. EffectControl2 = 13,
  89. // CC14 undefined
  90. // CC15 undefined
  91. GeneralPurposeController1 = 16,
  92. GeneralPurposeController2 = 17,
  93. GeneralPurposeController3 = 18,
  94. GeneralPurposeController4 = 19,
  95. // Switches ----------------------------------------------------------------
  96. Sustain = 64,
  97. Portamento = 65,
  98. Sostenuto = 66,
  99. SoftPedal = 67,
  100. Legato = 68,
  101. Hold = 69,
  102. // Low resolution continuous controllers -----------------------------------
  103. SoundController1 = 70, ///< Synth: Sound Variation FX: Exciter On/Off
  104. SoundController2 = 71, ///< Synth: Harmonic Content FX: Compressor On/Off
  105. SoundController3 = 72, ///< Synth: Release Time FX: Distortion On/Off
  106. SoundController4 = 73, ///< Synth: Attack Time FX: EQ On/Off
  107. SoundController5 = 74, ///< Synth: Brightness FX: Expander On/Off
  108. SoundController6 = 75, ///< Synth: Decay Time FX: Reverb On/Off
  109. SoundController7 = 76, ///< Synth: Vibrato Rate FX: Delay On/Off
  110. SoundController8 = 77, ///< Synth: Vibrato Depth FX: Pitch Transpose On/Off
  111. SoundController9 = 78, ///< Synth: Vibrato Delay FX: Flange/Chorus On/Off
  112. SoundController10 = 79, ///< Synth: Undefined FX: Special Effects On/Off
  113. GeneralPurposeController5 = 80,
  114. GeneralPurposeController6 = 81,
  115. GeneralPurposeController7 = 82,
  116. GeneralPurposeController8 = 83,
  117. PortamentoControl = 84,
  118. // CC85 to CC90 undefined
  119. Effects1 = 91, ///< Reverb send level
  120. Effects2 = 92, ///< Tremolo depth
  121. Effects3 = 93, ///< Chorus send level
  122. Effects4 = 94, ///< Celeste depth
  123. Effects5 = 95, ///< Phaser depth
  124. // Channel Mode messages ---------------------------------------------------
  125. AllSoundOff = 120,
  126. ResetAllControllers = 121,
  127. LocalControl = 122,
  128. AllNotesOff = 123,
  129. OmniModeOff = 124,
  130. OmniModeOn = 125,
  131. MonoModeOn = 126,
  132. PolyModeOn = 127
  133. };
  134. struct midi_cs_if_ac_header_descriptor {
  135. uint8_t bLength;
  136. uint8_t bDescriptorType;
  137. uint8_t bDescriptorSubType;
  138. uint16_t bcdADC;
  139. uint16_t wTotalLength;
  140. uint8_t bInCollection;
  141. uint8_t baInterfaceNr[];
  142. } __PACKED;
  143. #define MIDI_SIZEOF_AC_HEADER_DESC(n) (8 + n)
  144. struct midi_cs_if_ms_header_descriptor {
  145. uint8_t bLength;
  146. uint8_t bDescriptorType;
  147. uint8_t bDescriptorSubType;
  148. uint16_t bcdMSC;
  149. uint16_t wTotalLength;
  150. } __PACKED;
  151. #define MIDI_SIZEOF_MS_HEADER_DESC (7)
  152. struct midi_cs_if_in_jack_descriptor {
  153. uint8_t bLength;
  154. uint8_t bDescriptorType;
  155. uint8_t bDescriptorSubType;
  156. uint8_t bJackType;
  157. uint8_t bJackId;
  158. uint8_t iJack;
  159. } __PACKED;
  160. #define MIDI_SIZEOF_IN_JACK_DESC (6)
  161. struct midi_cs_if_out_jack_descriptor {
  162. uint8_t bLength;
  163. uint8_t bDescriptorType;
  164. uint8_t bDescriptorSubType;
  165. uint8_t bJackType;
  166. uint8_t bJackId;
  167. uint8_t bNrInputPins;
  168. uint8_t baSourceId;
  169. uint8_t baSourcePin;
  170. uint8_t iJack;
  171. } __PACKED;
  172. #define MIDI_SIZEOF_OUT_JACK_DESC (9)
  173. struct midi_cs_ep_ms_general_descriptor {
  174. uint8_t bLength;
  175. uint8_t bDescriptorType;
  176. uint8_t bDescriptorSubType;
  177. uint8_t bNumEmbMIDIJack;
  178. uint8_t baAssocJackID[];
  179. } __PACKED;
  180. #define MIDI_SIZEOF_MS_GENERAL_DESC(n) (4 + n)
  181. // clang-format off
  182. #define MIDI_CS_HEADER_DESCRIPTOR_INIT(wTotalLength) \
  183. 0x07, /* bLength */ \
  184. USB_CS_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
  185. MIDI_MS_HEADER_DESCRIPTOR_SUBTYPE, /* bDescriptorSubtype */ \
  186. WBVAL(0x0100), /* bcdMSC */ \
  187. WBVAL(wTotalLength) /* wTotalLength */
  188. #define MIDI_IN_JACK_DESCRIPTOR_INIT(bJackType, bJackID) \
  189. 0x06, \
  190. 0x24, \
  191. MIDI_MIDI_IN_JACK_DESCRIPTOR_SUBTYPE, \
  192. bJackType, \
  193. bJackID, \
  194. 0x00
  195. #define MIDI_OUT_JACK_DESCRIPTOR_INIT(bJackType, bJackID, baSourceID) \
  196. 0x09, \
  197. 0x24, \
  198. MIDI_MIDI_OUT_JACK_DESCRIPTOR_SUBTYPE, \
  199. bJackType, \
  200. bJackID, \
  201. 0x01, \
  202. baSourceID, \
  203. 0x01, \
  204. 0x00
  205. #define MIDI_JACK_DESCRIPTOR_INIT(bJackFirstID) \
  206. MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, bJackFirstID), \
  207. MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, (bJackFirstID + 1)), \
  208. MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, (bJackFirstID + 2), (bJackFirstID + 1)), \
  209. MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, (bJackFirstID + 3), (bJackFirstID))
  210. #define MIDI_SIZEOF_JACK_DESC (6 + 6 + 9 + 9)
  211. // clang-format on
  212. #endif /* USB_MIDI_H */