Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

283 lines
9.1KB

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