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.

пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef USBmidi_h_
  31. #define USBmidi_h_
  32. #if defined(USB_MIDI)
  33. #include <inttypes.h>
  34. /*
  35. These were originally meant to allow programs written for
  36. Francois Best's MIDI library to be easily used with
  37. Teensy's usbMIDI which implements the same API. However,
  38. the MIDI library definitions have changed, so these names
  39. now conflict. They've never been documented (the PJRC web
  40. page documents usbMIDI.getType() in numbers) so they are
  41. now commented out so usbMIDI and the MIDI library can be
  42. used together without conflict.
  43. #define NoteOff 0
  44. #define NoteOn 1
  45. #define AfterTouchPoly 2
  46. #define ControlChange 3
  47. #define ProgramChange 4
  48. #define AfterTouchChannel 5
  49. #define PitchBend 6
  50. #define SystemExclusive 7
  51. */
  52. #define USB_MIDI_SYSEX_MAX 60 // maximum sysex length we can receive
  53. // C language implementation
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. void usb_midi_write_packed(uint32_t n);
  58. void usb_midi_send_sysex(const uint8_t *data, uint32_t length);
  59. void usb_midi_flush_output(void);
  60. int usb_midi_read(uint32_t channel);
  61. extern uint8_t usb_midi_msg_channel;
  62. extern uint8_t usb_midi_msg_type;
  63. extern uint8_t usb_midi_msg_data1;
  64. extern uint8_t usb_midi_msg_data2;
  65. extern uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  66. extern uint8_t usb_midi_msg_sysex_len;
  67. extern void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  68. extern void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  69. extern void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  70. extern void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value);
  71. extern void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program);
  72. extern void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure);
  73. extern void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch);
  74. extern void (*usb_midi_handleRealTimeSystem)(uint8_t rtb);
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. // C++ interface
  79. #ifdef __cplusplus
  80. class usb_midi_class
  81. {
  82. public:
  83. void begin(void) { }
  84. void end(void) { }
  85. void sendNoteOff(uint32_t note, uint32_t velocity, uint32_t channel) __attribute__((always_inline)) {
  86. usb_midi_write_packed(0x8008 | (((channel - 1) & 0x0F) << 8)
  87. | ((note & 0x7F) << 16) | ((velocity & 0x7F) << 24));
  88. }
  89. void sendNoteOn(uint32_t note, uint32_t velocity, uint32_t channel) __attribute__((always_inline)) {
  90. usb_midi_write_packed(0x9009 | (((channel - 1) & 0x0F) << 8)
  91. | ((note & 0x7F) << 16) | ((velocity & 0x7F) << 24));
  92. }
  93. void sendPolyPressure(uint32_t note, uint32_t pressure, uint32_t channel) __attribute__((always_inline)) {
  94. usb_midi_write_packed(0xA00A | (((channel - 1) & 0x0F) << 8)
  95. | ((note & 0x7F) << 16) | ((pressure & 0x7F) << 24));
  96. }
  97. void sendControlChange(uint32_t control, uint32_t value, uint32_t channel) __attribute__((always_inline)) {
  98. usb_midi_write_packed(0xB00B | (((channel - 1) & 0x0F) << 8)
  99. | ((control & 0x7F) << 16) | ((value & 0x7F) << 24));
  100. }
  101. void sendProgramChange(uint32_t program, uint32_t channel) __attribute__((always_inline)) {
  102. usb_midi_write_packed(0xC00C | (((channel - 1) & 0x0F) << 8)
  103. | ((program & 0x7F) << 16));
  104. }
  105. void sendAfterTouch(uint32_t pressure, uint32_t channel) __attribute__((always_inline)) {
  106. usb_midi_write_packed(0xD00D | (((channel - 1) & 0x0F) << 8)
  107. | ((pressure & 0x7F) << 16));
  108. }
  109. void sendPitchBend(uint32_t value, uint32_t channel) __attribute__((always_inline)) {
  110. usb_midi_write_packed(0xE00E | (((channel - 1) & 0x0F) << 8)
  111. | ((value & 0x7F) << 16) | ((value & 0x3F80) << 17));
  112. }
  113. void sendSysEx(uint32_t length, const uint8_t *data) {
  114. usb_midi_send_sysex(data, length);
  115. }
  116. void send_now(void) {
  117. usb_midi_flush_output();
  118. }
  119. uint8_t analog2velocity(uint16_t val, uint8_t range);
  120. bool read(uint8_t channel=0) {
  121. return usb_midi_read(channel);
  122. }
  123. inline uint8_t getType(void) {
  124. return usb_midi_msg_type;
  125. };
  126. inline uint8_t getChannel(void) {
  127. return usb_midi_msg_channel;
  128. };
  129. inline uint8_t getData1(void) {
  130. return usb_midi_msg_data1;
  131. };
  132. inline uint8_t getData2(void) {
  133. return usb_midi_msg_data2;
  134. };
  135. inline uint8_t * getSysExArray(void) {
  136. return usb_midi_msg_sysex;
  137. };
  138. inline void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  139. usb_midi_handleNoteOff = fptr;
  140. };
  141. inline void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  142. usb_midi_handleNoteOn = fptr;
  143. };
  144. inline void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  145. usb_midi_handleVelocityChange = fptr;
  146. };
  147. inline void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  148. usb_midi_handleControlChange = fptr;
  149. };
  150. inline void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  151. usb_midi_handleProgramChange = fptr;
  152. };
  153. inline void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  154. usb_midi_handleAfterTouch = fptr;
  155. };
  156. inline void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  157. usb_midi_handlePitchChange = fptr;
  158. };
  159. inline void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  160. usb_midi_handleRealTimeSystem = fptr;
  161. };
  162. private:
  163. };
  164. extern usb_midi_class usbMIDI;
  165. #endif // __cplusplus
  166. #endif // USB_MIDI
  167. #endif // USBmidi_h_