Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

384 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. // maximum sysex length we can receive
  36. #if defined(__MKL26Z64__) || defined(__MK20DX128__)
  37. #define USB_MIDI_SYSEX_MAX 60
  38. #else
  39. #define USB_MIDI_SYSEX_MAX 290
  40. #endif
  41. // C language implementation
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. void usb_midi_write_packed(uint32_t n);
  46. void usb_midi_send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable);
  47. void usb_midi_send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable);
  48. void usb_midi_flush_output(void);
  49. int usb_midi_read(uint32_t channel);
  50. uint32_t usb_midi_available(void);
  51. uint32_t usb_midi_read_message(void);
  52. extern uint8_t usb_midi_msg_cable;
  53. extern uint8_t usb_midi_msg_channel;
  54. extern uint8_t usb_midi_msg_type;
  55. extern uint8_t usb_midi_msg_data1;
  56. extern uint8_t usb_midi_msg_data2;
  57. extern uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  58. extern uint16_t usb_midi_msg_sysex_len;
  59. extern void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  60. extern void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  61. extern void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  62. extern void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value);
  63. extern void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program);
  64. extern void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure);
  65. extern void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch);
  66. extern void (*usb_midi_handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete);
  67. extern void (*usb_midi_handleSysExComplete)(uint8_t *data, unsigned int size);
  68. extern void (*usb_midi_handleTimeCodeQuarterFrame)(uint8_t data);
  69. extern void (*usb_midi_handleSongPosition)(uint16_t beats);
  70. extern void (*usb_midi_handleSongSelect)(uint8_t songnumber);
  71. extern void (*usb_midi_handleTuneRequest)(void);
  72. extern void (*usb_midi_handleClock)(void);
  73. extern void (*usb_midi_handleStart)(void);
  74. extern void (*usb_midi_handleContinue)(void);
  75. extern void (*usb_midi_handleStop)(void);
  76. extern void (*usb_midi_handleActiveSensing)(void);
  77. extern void (*usb_midi_handleSystemReset)(void);
  78. extern void (*usb_midi_handleRealTimeSystem)(uint8_t rtb);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. // To test receiving on Linux, run "aseqdump -l" to list sequencer devices.
  83. //
  84. // Port Client name Port name
  85. // 0:0 System Timer
  86. // 0:1 System Announce
  87. // 14:0 Midi Through Midi Through Port-0
  88. // 24:0 Teensy MIDI Teensy MIDI MIDI 1
  89. // 28:0 AKM320 AKM320 MIDI 1
  90. //
  91. // Then run "aseqdump -p 24:0" to view the MIDI messages.
  92. //
  93. // Waiting for data. Press Ctrl+C to end.
  94. // Source Event Ch Data
  95. // 24:0 Note on 0, note 61, velocity 99
  96. // 24:0 Note off 0, note 61, velocity 0
  97. // 24:0 Note on 0, note 62, velocity 99
  98. // 24:0 Note off 0, note 62, velocity 0
  99. // 24:0 Note on 0, note 64, velocity 99
  100. // 24:0 Note off 0, note 64, velocity 0
  101. //
  102. // Quick-dirty way to transmit MIDI sysex:
  103. // echo -n -e '\xF0abcd\xF7' > /dev/midi2
  104. // C++ interface
  105. #ifdef __cplusplus
  106. class usb_midi_class
  107. {
  108. public:
  109. // Message type names for compatibility with Arduino MIDI library 4.3.1
  110. enum MidiType {
  111. InvalidType = 0x00, // For notifying errors
  112. NoteOff = 0x80, // Note Off
  113. NoteOn = 0x90, // Note On
  114. AfterTouchPoly = 0xA0, // Polyphonic AfterTouch
  115. ControlChange = 0xB0, // Control Change / Channel Mode
  116. ProgramChange = 0xC0, // Program Change
  117. AfterTouchChannel = 0xD0, // Channel (monophonic) AfterTouch
  118. PitchBend = 0xE0, // Pitch Bend
  119. SystemExclusive = 0xF0, // System Exclusive
  120. TimeCodeQuarterFrame = 0xF1, // System Common - MIDI Time Code Quarter Frame
  121. SongPosition = 0xF2, // System Common - Song Position Pointer
  122. SongSelect = 0xF3, // System Common - Song Select
  123. TuneRequest = 0xF6, // System Common - Tune Request
  124. Clock = 0xF8, // System Real Time - Timing Clock
  125. Start = 0xFA, // System Real Time - Start
  126. Continue = 0xFB, // System Real Time - Continue
  127. Stop = 0xFC, // System Real Time - Stop
  128. ActiveSensing = 0xFE, // System Real Time - Active Sensing
  129. SystemReset = 0xFF, // System Real Time - System Reset
  130. };
  131. void begin(void) { }
  132. void end(void) { }
  133. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  134. send(0x80, note, velocity, channel, cable);
  135. }
  136. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  137. send(0x90, note, velocity, channel, cable);
  138. }
  139. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  140. send(0xA0, note, pressure, channel, cable);
  141. }
  142. void sendAfterTouchPoly(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  143. send(0xA0, note, pressure, channel, cable);
  144. }
  145. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  146. send(0xB0, control, value, channel, cable);
  147. }
  148. void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  149. send(0xC0, program, 0, channel, cable);
  150. }
  151. void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  152. send(0xD0, pressure, 0, channel, cable);
  153. }
  154. void sendPitchBend(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  155. // MIDI 4.3 takes -8192 to +8191. We take 0 to 16383
  156. // MIDI 4.3 also has version that takes float -1.0 to +1.0
  157. send(0xE0, value, value >> 7, channel, cable);
  158. }
  159. void sendSysEx(uint32_t length, const uint8_t *data, bool hasTerm=false, uint8_t cable=0) __attribute__((always_inline)) {
  160. if (cable >= MIDI_NUM_CABLES) return;
  161. if (hasTerm) {
  162. usb_midi_send_sysex_buffer_has_term(data, length, cable);
  163. } else {
  164. usb_midi_send_sysex_add_term_bytes(data, length, cable);
  165. }
  166. }
  167. void sendRealTime(uint8_t type, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  168. switch (type) {
  169. case 0xF8: // Clock
  170. case 0xFA: // Start
  171. case 0xFB: // Continue
  172. case 0xFC: // Stop
  173. case 0xFE: // ActiveSensing
  174. case 0xFF: // SystemReset
  175. send(type, 0, 0, 0, cable);
  176. break;
  177. default: // Invalid Real Time marker
  178. break;
  179. }
  180. }
  181. void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  182. send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
  183. }
  184. void sendSongPosition(uint16_t beats, uint8_t cable=0) __attribute__((always_inline)) {
  185. send(0xF2, beats, beats >> 7, 0, cable);
  186. }
  187. void sendSongSelect(uint8_t song, uint8_t cable=0) __attribute__((always_inline)) {
  188. send(0xF3, song, 0, 0, cable);
  189. }
  190. void sendTuneRequest(uint8_t cable=0) __attribute__((always_inline)) {
  191. send(0xF6, 0, 0, 0, cable);
  192. }
  193. void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  194. sendControlChange(101, number >> 7, channel, cable);
  195. sendControlChange(100, number, channel, cable);
  196. }
  197. void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  198. sendControlChange(6, value >> 7, channel, cable);
  199. sendControlChange(38, value, channel, cable);
  200. }
  201. void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  202. sendControlChange(96, amount, channel, cable);
  203. }
  204. void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  205. sendControlChange(97, amount, channel, cable);
  206. }
  207. void endRpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  208. sendControlChange(101, 0x7F, channel, cable);
  209. sendControlChange(100, 0x7F, channel, cable);
  210. }
  211. void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  212. sendControlChange(99, number >> 7, channel, cable);
  213. sendControlChange(98, number, channel, cable);
  214. }
  215. void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  216. sendControlChange(6, value >> 7, channel, cable);
  217. sendControlChange(38, value, channel, cable);
  218. }
  219. void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  220. sendControlChange(96, amount, channel, cable);
  221. }
  222. void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  223. sendControlChange(97, amount, channel, cable);
  224. }
  225. void endNrpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  226. sendControlChange(99, 0x7F, channel, cable);
  227. sendControlChange(98, 0x7F, channel, cable);
  228. }
  229. void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable) __attribute__((always_inline)) {
  230. if (cable >= MIDI_NUM_CABLES) return;
  231. if (type < 0xF0) {
  232. if (type < 0x80) return;
  233. type &= 0xF0;
  234. usb_midi_write_packed((type << 8) | (type >> 4) | ((cable & 0x0F) << 4)
  235. | (((channel - 1) & 0x0F) << 8) | ((data1 & 0x7F) << 16)
  236. | ((data2 & 0x7F) << 24));
  237. } else if (type >= 0xF8 || type == 0xF6) {
  238. usb_midi_write_packed((type << 8) | 0x0F | ((cable & 0x0F) << 4));
  239. } else if (type == 0xF1 || type == 0xF3) {
  240. usb_midi_write_packed((type << 8) | 0x02 | ((cable & 0x0F) << 4)
  241. | ((data1 & 0x7F) << 16));
  242. } else if (type == 0xF2) {
  243. usb_midi_write_packed((type << 8) | 0x03 | ((cable & 0x0F) << 4)
  244. | ((data1 & 0x7F) << 16) | ((data2 & 0x7F) << 24));
  245. }
  246. }
  247. void send_now(void) __attribute__((always_inline)) {
  248. usb_midi_flush_output();
  249. }
  250. uint8_t analog2velocity(uint16_t val, uint8_t range);
  251. bool read(uint8_t channel=0) __attribute__((always_inline)) {
  252. return usb_midi_read(channel);
  253. }
  254. uint8_t getType(void) __attribute__((always_inline)) {
  255. return usb_midi_msg_type;
  256. }
  257. uint8_t getCable(void) __attribute__((always_inline)) {
  258. return usb_midi_msg_cable;
  259. }
  260. uint8_t getChannel(void) __attribute__((always_inline)) {
  261. return usb_midi_msg_channel;
  262. }
  263. uint8_t getData1(void) __attribute__((always_inline)) {
  264. return usb_midi_msg_data1;
  265. }
  266. uint8_t getData2(void) __attribute__((always_inline)) {
  267. return usb_midi_msg_data2;
  268. }
  269. uint8_t * getSysExArray(void) __attribute__((always_inline)) {
  270. return usb_midi_msg_sysex;
  271. }
  272. void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  273. // type: 0x80 NoteOff
  274. usb_midi_handleNoteOff = fptr;
  275. }
  276. void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  277. // type: 0x90 NoteOn
  278. usb_midi_handleNoteOn = fptr;
  279. }
  280. void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  281. // type: 0xA0 AfterTouchPoly
  282. usb_midi_handleVelocityChange = fptr;
  283. }
  284. void setHandleAfterTouchPoly(void (*fptr)(uint8_t channel, uint8_t note, uint8_t pressure)) {
  285. // type: 0xA0 AfterTouchPoly
  286. usb_midi_handleVelocityChange = fptr;
  287. }
  288. void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  289. // type: 0xB0 ControlChange
  290. usb_midi_handleControlChange = fptr;
  291. }
  292. void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  293. // type: 0xC0 ProgramChange
  294. usb_midi_handleProgramChange = fptr;
  295. }
  296. void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  297. // type: 0xD0 AfterTouchChannel
  298. usb_midi_handleAfterTouch = fptr;
  299. }
  300. void setHandleAfterTouchChannel(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  301. // type: 0xD0 AfterTouchChannel
  302. usb_midi_handleAfterTouch = fptr;
  303. }
  304. void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  305. // type: 0xE0 PitchBend
  306. usb_midi_handlePitchChange = fptr;
  307. }
  308. void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  309. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  310. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  311. }
  312. void setHandleSystemExclusive(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  313. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  314. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  315. }
  316. void setHandleSystemExclusive(void (*fptr)(uint8_t *data, unsigned int size)) {
  317. // type: 0xF0 SystemExclusive - single call, message larger than buffer is truncated
  318. usb_midi_handleSysExComplete = fptr;
  319. }
  320. void setHandleTimeCodeQuarterFrame(void (*fptr)(uint8_t data)) {
  321. // type: 0xF1 TimeCodeQuarterFrame
  322. usb_midi_handleTimeCodeQuarterFrame = fptr;
  323. }
  324. void setHandleSongPosition(void (*fptr)(uint16_t beats)) {
  325. // type: 0xF2 SongPosition
  326. usb_midi_handleSongPosition = fptr;
  327. }
  328. void setHandleSongSelect(void (*fptr)(uint8_t songnumber)) {
  329. // type: 0xF3 SongSelect
  330. usb_midi_handleSongSelect = fptr;
  331. }
  332. void setHandleTuneRequest(void (*fptr)(void)) {
  333. // type: 0xF6 TuneRequest
  334. usb_midi_handleTuneRequest = fptr;
  335. }
  336. void setHandleClock(void (*fptr)(void)) {
  337. // type: 0xF8 Clock
  338. usb_midi_handleClock = fptr;
  339. }
  340. void setHandleStart(void (*fptr)(void)) {
  341. // type: 0xFA Start
  342. usb_midi_handleStart = fptr;
  343. }
  344. void setHandleContinue(void (*fptr)(void)) {
  345. // type: 0xFB Continue
  346. usb_midi_handleContinue = fptr;
  347. }
  348. void setHandleStop(void (*fptr)(void)) {
  349. // type: 0xFC Stop
  350. usb_midi_handleStop = fptr;
  351. }
  352. void setHandleActiveSensing(void (*fptr)(void)) {
  353. // type: 0xFE ActiveSensing
  354. usb_midi_handleActiveSensing = fptr;
  355. }
  356. void setHandleSystemReset(void (*fptr)(void)) {
  357. // type: 0xFF SystemReset
  358. usb_midi_handleSystemReset = fptr;
  359. }
  360. void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  361. // type: 0xF8-0xFF - if more specific handler not configured
  362. usb_midi_handleRealTimeSystem = fptr;
  363. };
  364. };
  365. extern usb_midi_class usbMIDI;
  366. #endif // __cplusplus
  367. #endif // MIDI_INTERFACE
  368. #endif // USBmidi_h_