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.

386 lines
16KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef USBmidi_h_
  31. #define USBmidi_h_
  32. #include "usb_desc.h"
  33. #if defined(MIDI_INTERFACE)
  34. #include <inttypes.h>
  35. /*
  36. These were originally meant to allow programs written for
  37. Francois Best's MIDI library to be easily used with
  38. Teensy's usbMIDI which implements the same API. However,
  39. the MIDI library definitions have changed, so these names
  40. now conflict. They've never been documented (the PJRC web
  41. page documents usbMIDI.getType() in numbers) so they are
  42. now commented out so usbMIDI and the MIDI library can be
  43. used together without conflict.
  44. #define NoteOff 0
  45. #define NoteOn 1
  46. #define AfterTouchPoly 2
  47. #define ControlChange 3
  48. #define ProgramChange 4
  49. #define AfterTouchChannel 5
  50. #define PitchBend 6
  51. #define SystemExclusive 7
  52. */
  53. // maximum sysex length we can receive
  54. #if defined(__MKL26Z64__) || defined(__MK20DX128__)
  55. #define USB_MIDI_SYSEX_MAX 60
  56. #else
  57. #define USB_MIDI_SYSEX_MAX 290
  58. #endif
  59. // C language implementation
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. void usb_midi_write_packed(uint32_t n);
  64. void usb_midi_send_sysex(const uint8_t *data, uint32_t length, uint8_t cable);
  65. void usb_midi_flush_output(void);
  66. int usb_midi_read(uint32_t channel);
  67. uint32_t usb_midi_available(void);
  68. uint32_t usb_midi_read_message(void);
  69. extern uint8_t usb_midi_msg_cable;
  70. extern uint8_t usb_midi_msg_channel;
  71. extern uint8_t usb_midi_msg_type;
  72. extern uint8_t usb_midi_msg_data1;
  73. extern uint8_t usb_midi_msg_data2;
  74. extern uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  75. extern uint16_t usb_midi_msg_sysex_len;
  76. extern void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  77. extern void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  78. extern void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  79. extern void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value);
  80. extern void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program);
  81. extern void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure);
  82. extern void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch);
  83. extern void (*usb_midi_handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete);
  84. extern void (*usb_midi_handleSysExComplete)(uint8_t *data, unsigned int size);
  85. extern void (*usb_midi_handleTimeCodeQuarterFrame)(uint8_t data);
  86. extern void (*usb_midi_handleSongPosition)(uint16_t beats);
  87. extern void (*usb_midi_handleSongSelect)(uint8_t songnumber);
  88. extern void (*usb_midi_handleTuneRequest)(void);
  89. extern void (*usb_midi_handleClock)(void);
  90. extern void (*usb_midi_handleStart)(void);
  91. extern void (*usb_midi_handleContinue)(void);
  92. extern void (*usb_midi_handleStop)(void);
  93. extern void (*usb_midi_handleActiveSensing)(void);
  94. extern void (*usb_midi_handleSystemReset)(void);
  95. extern void (*usb_midi_handleRealTimeSystem)(uint8_t rtb);
  96. #ifdef __cplusplus
  97. }
  98. #endif
  99. // To test receiving on Linux, run "aseqdump -l" to list sequencer devices.
  100. //
  101. // Port Client name Port name
  102. // 0:0 System Timer
  103. // 0:1 System Announce
  104. // 14:0 Midi Through Midi Through Port-0
  105. // 24:0 Teensy MIDI Teensy MIDI MIDI 1
  106. // 28:0 AKM320 AKM320 MIDI 1
  107. //
  108. // Then run "aseqdump -p 24:0" to view the MIDI messages.
  109. //
  110. // Waiting for data. Press Ctrl+C to end.
  111. // Source Event Ch Data
  112. // 24:0 Note on 0, note 61, velocity 99
  113. // 24:0 Note off 0, note 61, velocity 0
  114. // 24:0 Note on 0, note 62, velocity 99
  115. // 24:0 Note off 0, note 62, velocity 0
  116. // 24:0 Note on 0, note 64, velocity 99
  117. // 24:0 Note off 0, note 64, velocity 0
  118. // C++ interface
  119. #ifdef __cplusplus
  120. class usb_midi_class
  121. {
  122. public:
  123. void begin(void) { }
  124. void end(void) { }
  125. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  126. send(0x80, note, velocity, channel, cable);
  127. };
  128. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  129. send(0x90, note, velocity, channel, cable);
  130. };
  131. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  132. send(0xA0, note, pressure, channel, cable);
  133. };
  134. void sendAfterTouch(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  135. send(0xA0, note, pressure, channel, cable);
  136. };
  137. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  138. send(0xB0, control, value, channel, cable);
  139. };
  140. void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  141. send(0xC0, program, 0, channel, cable);
  142. };
  143. void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  144. send(0xD0, pressure, 0, channel, cable);
  145. };
  146. void sendPitchBend(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  147. // MIDI 4.3 takes -8192 to +8191. We take 0 to 16383
  148. // MIDI 4.3 also has version that takes float -1.0 to +1.0
  149. send(0xE0, value, value >> 7, channel, cable);
  150. };
  151. void sendSysEx(uint32_t length, const uint8_t *data, bool hasTerm=false, uint8_t cable=0) __attribute__((always_inline)) {
  152. if (cable >= MIDI_NUM_CABLES) return;
  153. if (hasTerm) {
  154. if (length > 2) {
  155. usb_midi_send_sysex(data + 1, length - 2, cable);
  156. }
  157. } else {
  158. usb_midi_send_sysex(data, length, cable);
  159. }
  160. };
  161. void sendRealTime(uint8_t type, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  162. switch (type) {
  163. case 0xF8: // Clock
  164. case 0xFA: // Start
  165. case 0xFB: // Continue
  166. case 0xFC: // Stop
  167. case 0xFE: // ActiveSensing
  168. case 0xFF: // SystemReset
  169. send(type, 0, 0, 0, cable);
  170. break;
  171. default: // Invalid Real Time marker
  172. break;
  173. }
  174. };
  175. void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  176. send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
  177. };
  178. //void sendTimeCodeQuarterFrame(uint8_t data, uint8_t cable=0) __attribute__((always_inline)) {
  179. // MIDI 4.3 has this, but we can't implement with cable param
  180. //send(0xF1, data, 0, 0, cable);
  181. //};
  182. void sendSongPosition(uint16_t beats, uint8_t cable=0) __attribute__((always_inline)) {
  183. send(0xF2, beats, beats >> 7, 0, cable);
  184. };
  185. void sendSongSelect(uint8_t song, uint8_t cable=0) __attribute__((always_inline)) {
  186. send(0xF4, song, 0, 0, cable);
  187. };
  188. void sendTuneRequest(uint8_t cable=0) __attribute__((always_inline)) {
  189. send(0xF6, 0, 0, 0, cable);
  190. };
  191. void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  192. sendControlChange(101, number >> 7, channel, cable);
  193. sendControlChange(100, number, channel, cable);
  194. };
  195. void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  196. sendControlChange(6, value >> 7, channel, cable);
  197. sendControlChange(38, value, channel, cable);
  198. };
  199. void sendRpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  200. sendControlChange(6, msb, channel, cable);
  201. sendControlChange(38, lsb, channel, cable);
  202. };
  203. void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  204. sendControlChange(96, amount, channel, cable);
  205. };
  206. void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  207. sendControlChange(97, amount, channel, cable);
  208. };
  209. void endRpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  210. sendControlChange(101, 0x7F, channel, cable);
  211. sendControlChange(100, 0x7F, channel, cable);
  212. };
  213. void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  214. sendControlChange(99, number >> 7, channel, cable);
  215. sendControlChange(98, number, channel, cable);
  216. }
  217. void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  218. sendControlChange(6, value >> 7, channel, cable);
  219. sendControlChange(38, value, channel, cable);
  220. }
  221. void sendNrpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  222. sendControlChange(6, msb, channel, cable);
  223. sendControlChange(38, lsb, channel, cable);
  224. }
  225. void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  226. sendControlChange(96, amount, channel, cable);
  227. }
  228. void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  229. sendControlChange(97, amount, channel, cable);
  230. }
  231. void endNrpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  232. sendControlChange(99, 0x7F, channel, cable);
  233. sendControlChange(98, 0x7F, channel, cable);
  234. }
  235. void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable) __attribute__((always_inline)) {
  236. if (cable >= MIDI_NUM_CABLES) return;
  237. if (type < 0xF0) {
  238. if (type < 0x80) return;
  239. type &= 0xF0;
  240. usb_midi_write_packed((type << 8) | (type >> 4) | ((cable & 0x0F) << 4)
  241. | (((channel - 1) & 0x0F) << 8) | ((data1 & 0x7F) << 16)
  242. | ((data2 & 0x7F) << 24));
  243. } else {
  244. usb_midi_write_packed((type << 8) | 0x0F | ((cable & 0x0F) << 4)
  245. | ((data1 & 0x7F) << 16) | ((data2 & 0x7F) << 24));
  246. }
  247. };
  248. void send_now(void) __attribute__((always_inline)) {
  249. usb_midi_flush_output();
  250. };
  251. uint8_t analog2velocity(uint16_t val, uint8_t range);
  252. bool read(uint8_t channel=0) __attribute__((always_inline)) {
  253. return usb_midi_read(channel);
  254. };
  255. uint8_t getType(void) __attribute__((always_inline)) {
  256. return usb_midi_msg_type;
  257. };
  258. uint8_t getCable(void) __attribute__((always_inline)) {
  259. return usb_midi_msg_cable;
  260. };
  261. uint8_t getChannel(void) __attribute__((always_inline)) {
  262. return usb_midi_msg_channel;
  263. };
  264. uint8_t getData1(void) __attribute__((always_inline)) {
  265. return usb_midi_msg_data1;
  266. };
  267. uint8_t getData2(void) __attribute__((always_inline)) {
  268. return usb_midi_msg_data2;
  269. };
  270. uint8_t * getSysExArray(void) __attribute__((always_inline)) {
  271. return usb_midi_msg_sysex;
  272. };
  273. void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  274. // type: 0x80 NoteOff
  275. usb_midi_handleNoteOff = fptr;
  276. };
  277. void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  278. // type: 0x90 NoteOn
  279. usb_midi_handleNoteOn = fptr;
  280. };
  281. void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  282. // type: 0xA0 AfterTouchPoly
  283. usb_midi_handleVelocityChange = fptr;
  284. };
  285. void setHandleAfterTouchPoly(void (*fptr)(uint8_t channel, uint8_t note, uint8_t pressure)) {
  286. // type: 0xA0 AfterTouchPoly
  287. usb_midi_handleVelocityChange = fptr;
  288. };
  289. void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  290. // type: 0xB0 ControlChange
  291. usb_midi_handleControlChange = fptr;
  292. };
  293. void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  294. // type: 0xC0 ProgramChange
  295. usb_midi_handleProgramChange = fptr;
  296. };
  297. void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  298. // type: 0xD0 AfterTouchChannel
  299. usb_midi_handleAfterTouch = fptr;
  300. };
  301. void setHandleAfterTouchChannel(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  302. // type: 0xD0 AfterTouchChannel
  303. usb_midi_handleAfterTouch = fptr;
  304. };
  305. void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  306. // type: 0xE0 PitchBend
  307. usb_midi_handlePitchChange = fptr;
  308. };
  309. void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  310. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  311. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  312. }
  313. void setHandleSystemExclusive(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  314. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  315. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  316. }
  317. void setHandleSystemExclusive(void (*fptr)(uint8_t *data, unsigned int size)) {
  318. // type: 0xF0 SystemExclusive - single call, message larger than buffer is truncated
  319. usb_midi_handleSysExComplete = fptr;
  320. }
  321. void setHandleTimeCodeQuarterFrame(void (*fptr)(uint8_t data)) {
  322. // type: 0xF1 TimeCodeQuarterFrame
  323. usb_midi_handleTimeCodeQuarterFrame = fptr;
  324. };
  325. void setHandleSongPosition(void (*fptr)(uint16_t beats)) {
  326. // type: 0xF2 SongPosition
  327. usb_midi_handleSongPosition = fptr;
  328. }
  329. void setHandleSongSelect(void (*fptr)(uint8_t songnumber)) {
  330. // type: 0xF3 SongSelect
  331. usb_midi_handleSongSelect = fptr;
  332. }
  333. void setHandleTuneRequest(void (*fptr)(void)) {
  334. // type: 0xF6 TuneRequest
  335. usb_midi_handleTuneRequest = fptr;
  336. }
  337. void setHandleClock(void (*fptr)(void)) {
  338. // type: 0xF8 Clock
  339. usb_midi_handleClock = fptr;
  340. }
  341. void setHandleStart(void (*fptr)(void)) {
  342. // type: 0xFA Start
  343. usb_midi_handleStart = fptr;
  344. }
  345. void setHandleContinue(void (*fptr)(void)) {
  346. // type: 0xFB Continue
  347. usb_midi_handleContinue = fptr;
  348. }
  349. void setHandleStop(void (*fptr)(void)) {
  350. // type: 0xFC Stop
  351. usb_midi_handleStop = fptr;
  352. }
  353. void setHandleActiveSensing(void (*fptr)(void)) {
  354. // type: 0xFE ActiveSensing
  355. usb_midi_handleActiveSensing = fptr;
  356. }
  357. void setHandleSystemReset(void (*fptr)(void)) {
  358. // type: 0xFF SystemReset
  359. usb_midi_handleSystemReset = fptr;
  360. }
  361. void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  362. // type: 0xF8-0xFF - if more specific handler not configured
  363. usb_midi_handleRealTimeSystem = fptr;
  364. };
  365. };
  366. extern usb_midi_class usbMIDI;
  367. #endif // __cplusplus
  368. #endif // MIDI_INTERFACE
  369. #endif // USBmidi_h_