Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

usb_midi.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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. #include "usb_dev.h"
  31. #include "usb_midi.h"
  32. #include "core_pins.h" // for yield()
  33. #include "HardwareSerial.h"
  34. #ifdef MIDI_INTERFACE // defined by usb_dev.h -> usb_desc.h
  35. #if F_CPU >= 20000000
  36. uint8_t usb_midi_msg_channel;
  37. uint8_t usb_midi_msg_type;
  38. uint8_t usb_midi_msg_data1;
  39. uint8_t usb_midi_msg_data2;
  40. uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  41. uint8_t usb_midi_msg_sysex_len;
  42. void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  43. void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  44. void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  45. void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value) = NULL;
  46. void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program) = NULL;
  47. void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure) = NULL;
  48. void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch) = NULL;
  49. void (*usb_midi_handleSysEx)(const uint8_t *data, uint16_t length, uint8_t complete) = NULL;
  50. void (*usb_midi_handleRealTimeSystem)(uint8_t rtb) = NULL;
  51. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  52. #define TX_PACKET_LIMIT 6
  53. static usb_packet_t *rx_packet=NULL;
  54. static usb_packet_t *tx_packet=NULL;
  55. static uint8_t transmit_previous_timeout=0;
  56. static uint8_t tx_noautoflush=0;
  57. // When the PC isn't listening, how long do we wait before discarding data?
  58. #define TX_TIMEOUT_MSEC 40
  59. #if F_CPU == 168000000
  60. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  61. #elif F_CPU == 144000000
  62. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  63. #elif F_CPU == 120000000
  64. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  65. #elif F_CPU == 96000000
  66. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  67. #elif F_CPU == 72000000
  68. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  69. #elif F_CPU == 48000000
  70. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  71. #elif F_CPU == 24000000
  72. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  73. #endif
  74. void usb_midi_write_packed(uint32_t n)
  75. {
  76. uint32_t index, wait_count=0;
  77. tx_noautoflush = 1;
  78. if (!tx_packet) {
  79. while (1) {
  80. if (!usb_configuration) {
  81. //serial_print("error1\n");
  82. return;
  83. }
  84. if (usb_tx_packet_count(MIDI_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  85. tx_packet = usb_malloc();
  86. if (tx_packet) break;
  87. }
  88. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  89. transmit_previous_timeout = 1;
  90. //serial_print("error2\n");
  91. return;
  92. }
  93. yield();
  94. }
  95. }
  96. transmit_previous_timeout = 0;
  97. index = tx_packet->index;
  98. //*((uint32_t *)(tx_packet->buf) + index++) = n;
  99. ((uint32_t *)(tx_packet->buf))[index++] = n;
  100. if (index < MIDI_TX_SIZE/4) {
  101. tx_packet->index = index;
  102. } else {
  103. tx_packet->len = MIDI_TX_SIZE;
  104. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  105. tx_packet = usb_malloc();
  106. }
  107. tx_noautoflush = 0;
  108. }
  109. void usb_midi_send_sysex(const uint8_t *data, uint32_t length)
  110. {
  111. // TODO: MIDI 2.5 lib automatically adds start and stop bytes
  112. while (length > 3) {
  113. usb_midi_write_packed(0x04 | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  114. data += 3;
  115. length -= 3;
  116. }
  117. if (length == 3) {
  118. usb_midi_write_packed(0x07 | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  119. } else if (length == 2) {
  120. usb_midi_write_packed(0x06 | (data[0] << 8) | (data[1] << 16));
  121. } else if (length == 1) {
  122. usb_midi_write_packed(0x05 | (data[0] << 8));
  123. }
  124. }
  125. void usb_midi_flush_output(void)
  126. {
  127. if (tx_noautoflush == 0 && tx_packet && tx_packet->index > 0) {
  128. tx_packet->len = tx_packet->index * 4;
  129. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  130. tx_packet = usb_malloc();
  131. }
  132. }
  133. void static sysex_byte(uint8_t b)
  134. {
  135. // when buffer is full, send another chunk to handler.
  136. if (usb_midi_msg_sysex_len == USB_MIDI_SYSEX_MAX) {
  137. if (usb_midi_handleSysEx) {
  138. (*usb_midi_handleSysEx)(usb_midi_msg_sysex, usb_midi_msg_sysex_len, 0);
  139. usb_midi_msg_sysex_len = 0;
  140. }
  141. }
  142. if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  143. usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
  144. }
  145. }
  146. int usb_midi_read(uint32_t channel)
  147. {
  148. uint32_t n, index, ch, type1, type2;
  149. if (!rx_packet) {
  150. if (!usb_configuration) return 0;
  151. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  152. if (!rx_packet) return 0;
  153. if (rx_packet->len == 0) {
  154. usb_free(rx_packet);
  155. rx_packet = NULL;
  156. return 0;
  157. }
  158. }
  159. index = rx_packet->index;
  160. //n = *(uint32_t *)(rx_packet->buf + index);
  161. n = ((uint32_t *)rx_packet->buf)[index/4];
  162. //serial_print("midi rx, n=");
  163. //serial_phex32(n);
  164. //serial_print("\n");
  165. index += 4;
  166. if (index < rx_packet->len) {
  167. rx_packet->index = index;
  168. } else {
  169. usb_free(rx_packet);
  170. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  171. }
  172. type1 = n & 15;
  173. type2 = (n >> 12) & 15;
  174. ch = ((n >> 8) & 15) + 1;
  175. if (type1 >= 0x08 && type1 <= 0x0E) {
  176. if (channel && channel != ch) {
  177. // ignore other channels when user wants single channel read
  178. return 0;
  179. }
  180. if (type1 == 0x08 && type2 == 0x08) {
  181. usb_midi_msg_type = 0; // 0 = Note off
  182. if (usb_midi_handleNoteOff)
  183. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  184. } else
  185. if (type1 == 0x09 && type2 == 0x09) {
  186. if ((n >> 24) > 0) {
  187. usb_midi_msg_type = 1; // 1 = Note on
  188. if (usb_midi_handleNoteOn)
  189. (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
  190. } else {
  191. usb_midi_msg_type = 0; // 0 = Note off
  192. if (usb_midi_handleNoteOff)
  193. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  194. }
  195. } else
  196. if (type1 == 0x0A && type2 == 0x0A) {
  197. usb_midi_msg_type = 2; // 2 = Poly Pressure
  198. if (usb_midi_handleVelocityChange)
  199. (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
  200. } else
  201. if (type1 == 0x0B && type2 == 0x0B) {
  202. usb_midi_msg_type = 3; // 3 = Control Change
  203. if (usb_midi_handleControlChange)
  204. (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
  205. } else
  206. if (type1 == 0x0C && type2 == 0x0C) {
  207. usb_midi_msg_type = 4; // 4 = Program Change
  208. if (usb_midi_handleProgramChange)
  209. (*usb_midi_handleProgramChange)(ch, (n >> 16));
  210. } else
  211. if (type1 == 0x0D && type2 == 0x0D) {
  212. usb_midi_msg_type = 5; // 5 = After Touch
  213. if (usb_midi_handleAfterTouch)
  214. (*usb_midi_handleAfterTouch)(ch, (n >> 16));
  215. } else
  216. if (type1 == 0x0E && type2 == 0x0E) {
  217. usb_midi_msg_type = 6; // 6 = Pitch Bend
  218. if (usb_midi_handlePitchChange)
  219. (*usb_midi_handlePitchChange)(ch,
  220. ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  221. } else {
  222. return 0;
  223. }
  224. return_message:
  225. usb_midi_msg_channel = ch;
  226. usb_midi_msg_data1 = (n >> 16);
  227. usb_midi_msg_data2 = (n >> 24);
  228. return 1;
  229. }
  230. if (type1 == 0x04) {
  231. sysex_byte(n >> 8);
  232. sysex_byte(n >> 16);
  233. sysex_byte(n >> 24);
  234. return 0;
  235. }
  236. if (type1 >= 0x05 && type1 <= 0x07) {
  237. sysex_byte(n >> 8);
  238. if (type1 >= 0x06) sysex_byte(n >> 16);
  239. if (type1 == 0x07) sysex_byte(n >> 24);
  240. usb_midi_msg_data1 = usb_midi_msg_sysex_len;
  241. usb_midi_msg_sysex_len = 0;
  242. usb_midi_msg_type = 7; // 7 = Sys Ex
  243. if (usb_midi_handleSysEx)
  244. (*usb_midi_handleSysEx)(usb_midi_msg_sysex, usb_midi_msg_data1, 1);
  245. return 1;
  246. }
  247. if (type1 == 0x0F) {
  248. // TODO: does this need to be a full MIDI parser?
  249. // What software actually uses this message type in practice?
  250. if (usb_midi_msg_sysex_len > 0) {
  251. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  252. // OSX sometimes uses Single Byte Unparsed to
  253. // send bytes in the middle of a SYSEX message.
  254. sysex_byte(n >> 8);
  255. } else {
  256. // From Sebastian Tomczak, seb.tomczak at gmail.com
  257. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  258. usb_midi_msg_type = 8;
  259. if (usb_midi_handleRealTimeSystem)
  260. (*usb_midi_handleRealTimeSystem)(n >> 8);
  261. goto return_message;
  262. }
  263. }
  264. return 0;
  265. }
  266. #endif // F_CPU
  267. #endif // MIDI_INTERFACE