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.

295 satır
9.6KB

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