Keyboard firmwares for Atmel AVR and Cortex-M
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.

MIDIMessage.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef MIDIMESSAGE_H
  19. #define MIDIMESSAGE_H
  20. #include "mbed.h"
  21. #define MAX_MIDI_MESSAGE_SIZE 256 // Max message size. SysEx can be up to 65536 but 256 should be fine for most usage
  22. // MIDI Message Format
  23. //
  24. // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]
  25. //
  26. // MIDI Data Messages (Channel Specific)
  27. //
  28. // Message msg n m
  29. // ---------------------------------------------
  30. // Note Off 0x8 Key Velocity
  31. // Note On 0x9 Key Velocity
  32. // Polyphonic Aftertouch 0xA Key Pressure
  33. // Control Change 0xB Controller Value
  34. // Program Change 0xC Program -
  35. // Channel Aftertouch 0xD Pressure -
  36. // Pitch Wheel 0xE LSB MSB
  37. #define CABLE_NUM (0<<4)
  38. /** A MIDI message container */
  39. class MIDIMessage {
  40. public:
  41. MIDIMessage() : length(4) {}
  42. MIDIMessage(uint8_t *buf) : length(4) {
  43. for (int i = 0; i < 4; i++)
  44. data[i] = buf[i];
  45. }
  46. // New constructor, buf is a true MIDI message (not USBMidi message) and buf_len true message length.
  47. MIDIMessage(uint8_t *buf, int buf_len) {
  48. length=buf_len+1;
  49. // first byte keeped for retro-compatibility
  50. data[0]=0;
  51. for (int i = 0; i < buf_len; i++)
  52. data[i+1] = buf[i];
  53. }
  54. // create messages
  55. /** Create a NoteOff message
  56. * @param key Key ID
  57. * @param velocity Key velocity (0-127, default = 127)
  58. * @param channel Key channel (0-15, default 0)
  59. * @returns A MIDIMessage
  60. */
  61. static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {
  62. MIDIMessage msg;
  63. msg.data[0] = CABLE_NUM | 0x08;
  64. msg.data[1] = 0x80 | (channel & 0x0F);
  65. msg.data[2] = key & 0x7F;
  66. msg.data[3] = velocity & 0x7F;
  67. return msg;
  68. }
  69. /** Create a NoteOn message
  70. * @param key Key ID
  71. * @param velocity Key velocity (0-127, default = 127)
  72. * @param channel Key channel (0-15, default 0)
  73. * @returns A MIDIMessage
  74. */
  75. static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {
  76. MIDIMessage msg;
  77. msg.data[0] = CABLE_NUM | 0x09;
  78. msg.data[1] = 0x90 | (channel & 0x0F);
  79. msg.data[2] = key & 0x7F;
  80. msg.data[3] = velocity & 0x7F;
  81. return msg;
  82. }
  83. /** Create a PolyPhonic Aftertouch message
  84. * @param key Key ID
  85. * @param pressure Aftertouch pressure (0-127)
  86. * @param channel Key channel (0-15, default 0)
  87. * @returns A MIDIMessage
  88. */
  89. static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {
  90. MIDIMessage msg;
  91. msg.data[0] = CABLE_NUM | 0x0A;
  92. msg.data[1] = 0xA0 | (channel & 0x0F);
  93. msg.data[2] = key & 0x7F;
  94. msg.data[3] = pressure & 0x7F;
  95. return msg;
  96. }
  97. /** Create a Control Change message
  98. * @param control Controller ID
  99. * @param value Controller value (0-127)
  100. * @param channel Controller channel (0-15, default 0)
  101. * @returns A MIDIMessage
  102. */
  103. static MIDIMessage ControlChange(int control, int value, int channel = 0) {
  104. MIDIMessage msg;
  105. msg.data[0] = CABLE_NUM | 0x0B;
  106. msg.data[1] = 0xB0 | (channel & 0x0F);
  107. msg.data[2] = control & 0x7F;
  108. msg.data[3] = value & 0x7F;
  109. return msg;
  110. }
  111. /** Create a Program Change message
  112. * @param program Program ID
  113. * @param channel Channel (0-15, default 0)
  114. * @returns A MIDIMessage
  115. */
  116. static MIDIMessage ProgramChange(int program, int channel = 0) {
  117. MIDIMessage msg;
  118. msg.data[0] = CABLE_NUM | 0x0C;
  119. msg.data[1] = 0xC0 | (channel & 0x0F);
  120. msg.data[2] = program & 0x7F;
  121. msg.data[3] = 0x00;
  122. return msg;
  123. }
  124. /** Create a Channel Aftertouch message
  125. * @param pressure Pressure
  126. * @param channel Key channel (0-15, default 0)
  127. * @returns A MIDIMessage
  128. */
  129. static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {
  130. MIDIMessage msg;
  131. msg.data[0] = CABLE_NUM | 0x0D;
  132. msg.data[1] = 0xD0 | (channel & 0x0F);
  133. msg.data[2] = pressure & 0x7F;
  134. msg.data[3] = 0x00;
  135. return msg;
  136. }
  137. /** Create a Pitch Wheel message
  138. * @param pitch Pitch (-8192 - 8191, default = 0)
  139. * @param channel Channel (0-15, default 0)
  140. * @returns A MIDIMessage
  141. */
  142. static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {
  143. MIDIMessage msg;
  144. int p = pitch + 8192; // 0 - 16383, 8192 is center
  145. msg.data[0] = CABLE_NUM | 0x0E;
  146. msg.data[1] = 0xE0 | (channel & 0x0F);
  147. msg.data[2] = p & 0x7F;
  148. msg.data[3] = (p >> 7) & 0x7F;
  149. return msg;
  150. }
  151. /** Create an All Notes Off message
  152. * @param channel Channel (0-15, default 0)
  153. * @returns A MIDIMessage
  154. */
  155. static MIDIMessage AllNotesOff(int channel = 0) {
  156. return ControlChange(123, 0, channel);
  157. }
  158. /** Create a SysEx message
  159. * @param data SysEx data (including 0xF0 .. 0xF7)
  160. * @param len SysEx data length
  161. * @returns A MIDIMessage
  162. */
  163. static MIDIMessage SysEx(uint8_t *data, int len) {
  164. MIDIMessage msg=MIDIMessage(data,len);
  165. return msg;
  166. }
  167. // decode messages
  168. /** MIDI Message Types */
  169. enum MIDIMessageType {
  170. ErrorType,
  171. NoteOffType,
  172. NoteOnType,
  173. PolyphonicAftertouchType,
  174. ControlChangeType,
  175. ProgramChangeType,
  176. ChannelAftertouchType,
  177. PitchWheelType,
  178. AllNotesOffType,
  179. SysExType
  180. };
  181. /** Read the message type
  182. * @returns MIDIMessageType
  183. */
  184. MIDIMessageType type() {
  185. switch((data[1] >> 4) & 0xF) {
  186. case 0x8: return NoteOffType;
  187. case 0x9: return NoteOnType;
  188. case 0xA: return PolyphonicAftertouchType;
  189. case 0xB:
  190. if(controller() < 120) { // standard controllers
  191. return ControlChangeType;
  192. } else if(controller() == 123) {
  193. return AllNotesOffType;
  194. } else {
  195. return ErrorType; // unsupported atm
  196. }
  197. case 0xC: return ProgramChangeType;
  198. case 0xD: return ChannelAftertouchType;
  199. case 0xE: return PitchWheelType;
  200. case 0xF: return SysExType;
  201. default: return ErrorType;
  202. }
  203. }
  204. /** Read the channel number */
  205. int channel() {
  206. return (data[1] & 0x0F);
  207. }
  208. /** Read the key ID */
  209. int key() {
  210. return (data[2] & 0x7F);
  211. }
  212. /** Read the velocity */
  213. int velocity() {
  214. return (data[3] & 0x7F);
  215. }
  216. /** Read the controller value */
  217. int value() {
  218. return (data[3] & 0x7F);
  219. }
  220. /** Read the aftertouch pressure */
  221. int pressure() {
  222. if(type() == PolyphonicAftertouchType) {
  223. return (data[3] & 0x7F);
  224. } else {
  225. return (data[2] & 0x7F);
  226. }
  227. }
  228. /** Read the controller number */
  229. int controller() {
  230. return (data[2] & 0x7F);
  231. }
  232. /** Read the program number */
  233. int program() {
  234. return (data[2] & 0x7F);
  235. }
  236. /** Read the pitch value */
  237. int pitch() {
  238. int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
  239. return p - 8192; // 0 - 16383, 8192 is center
  240. }
  241. uint8_t data[MAX_MIDI_MESSAGE_SIZE+1];
  242. uint8_t length;
  243. };
  244. #endif