midi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // midi for embedded chips,
  2. // Copyright 2010 Alex Norman
  3. //
  4. // This file is part of avr-midi.
  5. //
  6. // avr-midi is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. //(at your option) any later version.
  10. //
  11. // avr-midi is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
  18. /**
  19. * @file
  20. * @brief The main midi functions
  21. *
  22. * This file includes all of the functions you need to set up and process a
  23. * midi device, send midi, and register midi callbacks.
  24. *
  25. */
  26. #pragma once
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include "midi_device.h"
  31. #include "midi_function_types.h"
  32. /**
  33. * @defgroup midi_device_setup_process Device initialization and processing
  34. * @brief These are method that you must use to initialize and run a device
  35. *
  36. * @{
  37. */
  38. /**
  39. * @brief Initialize a device
  40. *
  41. * You must call this before using the device in question.
  42. *
  43. * @param device the device to initialize
  44. */
  45. void midi_device_init(MidiDevice* device); // [implementation in midi_device.c]
  46. /**
  47. * @brief Process input data
  48. *
  49. * This method drives the input processing, you must call this method frequently
  50. * if you expect to have your input callbacks called.
  51. *
  52. * @param device the device to process
  53. */
  54. void midi_device_process(MidiDevice* device); // [implementation in midi_device.c]
  55. /**@}*/
  56. /**
  57. * @defgroup send_functions Midi send functions
  58. * @brief These are the functions you use to send midi data through a device.
  59. * @{
  60. */
  61. /**
  62. * @brief Send a control change message (cc) via the given device.
  63. *
  64. * @param device the device to use for sending
  65. * @param chan the channel to send on, 0-15
  66. * @param num the cc num
  67. * @param val the value of that cc num
  68. */
  69. void midi_send_cc(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val);
  70. /**
  71. * @brief Send a note on message via the given device.
  72. *
  73. * @param device the device to use for sending
  74. * @param chan the channel to send on, 0-15
  75. * @param num the note number
  76. * @param vel the note velocity
  77. */
  78. void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel);
  79. /**
  80. * @brief Send a note off message via the given device.
  81. *
  82. * @param device the device to use for sending
  83. * @param chan the channel to send on, 0-15
  84. * @param num the note number
  85. * @param vel the note velocity
  86. */
  87. void midi_send_noteoff(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel);
  88. /**
  89. * @brief Send an after touch message via the given device.
  90. *
  91. * @param device the device to use for sending
  92. * @param chan the channel to send on, 0-15
  93. * @param note_num the note number
  94. * @param amt the after touch amount
  95. */
  96. void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt);
  97. /**
  98. * @brief Send a pitch bend message via the given device.
  99. *
  100. * @param device the device to use for sending
  101. * @param chan the channel to send on, 0-15
  102. * @param amt the bend amount range: -8192..8191, 0 means no bend
  103. */
  104. void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt); // range -8192, 8191
  105. /**
  106. * @brief Send a program change message via the given device.
  107. *
  108. * @param device the device to use for sending
  109. * @param chan the channel to send on, 0-15
  110. * @param num the program to change to
  111. */
  112. void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num);
  113. /**
  114. * @brief Send a channel pressure message via the given device.
  115. *
  116. * @param device the device to use for sending
  117. * @param chan the channel to send on, 0-15
  118. * @param amt the amount of channel pressure
  119. */
  120. void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt);
  121. /**
  122. * @brief Send a clock message via the given device.
  123. *
  124. * @param device the device to use for sending
  125. */
  126. void midi_send_clock(MidiDevice* device);
  127. /**
  128. * @brief Send a tick message via the given device.
  129. *
  130. * @param device the device to use for sending
  131. */
  132. void midi_send_tick(MidiDevice* device);
  133. /**
  134. * @brief Send a start message via the given device.
  135. *
  136. * @param device the device to use for sending
  137. */
  138. void midi_send_start(MidiDevice* device);
  139. /**
  140. * @brief Send a continue message via the given device.
  141. *
  142. * @param device the device to use for sending
  143. */
  144. void midi_send_continue(MidiDevice* device);
  145. /**
  146. * @brief Send a stop message via the given device.
  147. *
  148. * @param device the device to use for sending
  149. */
  150. void midi_send_stop(MidiDevice* device);
  151. /**
  152. * @brief Send an active sense message via the given device.
  153. *
  154. * @param device the device to use for sending
  155. */
  156. void midi_send_activesense(MidiDevice* device);
  157. /**
  158. * @brief Send a reset message via the given device.
  159. *
  160. * @param device the device to use for sending
  161. */
  162. void midi_send_reset(MidiDevice* device);
  163. /**
  164. * @brief Send a tc quarter frame message via the given device.
  165. *
  166. * @param device the device to use for sending
  167. * @param time the time of this quarter frame, range 0..16383
  168. */
  169. void midi_send_tcquarterframe(MidiDevice* device, uint8_t time);
  170. /**
  171. * @brief Send a song position message via the given device.
  172. *
  173. * @param device the device to use for sending
  174. * @param pos the song position
  175. */
  176. void midi_send_songposition(MidiDevice* device, uint16_t pos);
  177. /**
  178. * @brief Send a song select message via the given device.
  179. *
  180. * @param device the device to use for sending
  181. * @param song the song to select
  182. */
  183. void midi_send_songselect(MidiDevice* device, uint8_t song);
  184. /**
  185. * @brief Send a tune request message via the given device.
  186. *
  187. * @param device the device to use for sending
  188. */
  189. void midi_send_tunerequest(MidiDevice* device);
  190. /**
  191. * @brief Send a byte via the given device.
  192. *
  193. * This is a generic method for sending data via the given midi device.
  194. * This would be useful for sending sysex data or messages that are not
  195. * implemented in this API, if there are any. Please contact the author
  196. * if you find some so we can add them.
  197. *
  198. * @param device the device to use for sending
  199. * @param b the byte to send
  200. */
  201. void midi_send_byte(MidiDevice* device, uint8_t b);
  202. /**
  203. * @brief Send up to 3 bytes of data
  204. *
  205. * % 4 is applied to count so that you can use this to pass sysex through
  206. *
  207. * @param device the device to use for sending
  208. * @param count the count of bytes to send, %4 is applied
  209. * @param byte0 the first byte
  210. * @param byte1 the second byte, ignored if cnt % 4 != 2
  211. * @param byte2 the third byte, ignored if cnt % 4 != 3
  212. */
  213. void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2);
  214. /**
  215. * @brief Send an array of formatted midi data.
  216. *
  217. * Can be used for sysex.
  218. *
  219. * @param device the device to use for sending
  220. * @param count the count of bytes to send
  221. * @param array the array of bytes
  222. */
  223. void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array);
  224. /**@}*/
  225. /**
  226. * @defgroup input_callback_reg Input callback registration functions
  227. *
  228. * @brief These are the functions you use to register your input callbacks.
  229. *
  230. * The functions are called when the appropriate midi message is matched on the
  231. * associated device's input.
  232. *
  233. * @{
  234. */
  235. // three byte funcs
  236. /**
  237. * @brief Register a control change message (cc) callback.
  238. *
  239. * @param device the device associate with
  240. * @param func the callback function to register
  241. */
  242. void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func);
  243. /**
  244. * @brief Register a note on callback.
  245. *
  246. * @param device the device associate with
  247. * @param func the callback function to register
  248. */
  249. void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func);
  250. /**
  251. * @brief Register a note off callback.
  252. *
  253. * @param device the device associate with
  254. * @param func the callback function to register
  255. */
  256. void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func);
  257. /**
  258. * @brief Register an after touch callback.
  259. *
  260. * @param device the device associate with
  261. * @param func the callback function to register
  262. */
  263. void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func);
  264. /**
  265. * @brief Register a pitch bend callback.
  266. *
  267. * @param device the device associate with
  268. * @param func the callback function to register
  269. */
  270. void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func);
  271. /**
  272. * @brief Register a song position callback.
  273. *
  274. * @param device the device associate with
  275. * @param func the callback function to register
  276. */
  277. void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func);
  278. // two byte funcs
  279. /**
  280. * @brief Register a program change callback.
  281. *
  282. * @param device the device associate with
  283. * @param func the callback function to register
  284. */
  285. void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func);
  286. /**
  287. * @brief Register a channel pressure callback.
  288. *
  289. * @param device the device associate with
  290. * @param func the callback function to register
  291. */
  292. void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func);
  293. /**
  294. * @brief Register a song select callback.
  295. *
  296. * @param device the device associate with
  297. * @param func the callback function to register
  298. */
  299. void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func);
  300. /**
  301. * @brief Register a tc quarter frame callback.
  302. *
  303. * @param device the device associate with
  304. * @param func the callback function to register
  305. */
  306. void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func);
  307. // one byte funcs
  308. /**
  309. * @brief Register a realtime callback.
  310. *
  311. * The callback will be called for all of the real time message types.
  312. *
  313. * @param device the device associate with
  314. * @param func the callback function to register
  315. */
  316. void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func);
  317. /**
  318. * @brief Register a tune request callback.
  319. *
  320. * @param device the device associate with
  321. * @param func the callback function to register
  322. */
  323. void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func);
  324. /**
  325. * @brief Register a sysex callback.
  326. *
  327. * @param device the device associate with
  328. * @param func the callback function to register
  329. */
  330. void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func);
  331. /**
  332. * @brief Register fall through callback.
  333. *
  334. * This is only called if a more specific callback is not matched and called.
  335. * For instance, if you don't register a note on callback but you get a note on message
  336. * the fall through callback will be called, if it is registered.
  337. *
  338. * @param device the device associate with
  339. * @param func the callback function to register
  340. */
  341. void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func);
  342. /**
  343. * @brief Register a catch all callback.
  344. *
  345. * If registered, the catch all callback is called for every message that is
  346. * matched, even if a more specific or the fallthrough callback is registered.
  347. *
  348. * @param device the device associate with
  349. * @param func the callback function to register
  350. */
  351. void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func);
  352. /**@}*/
  353. /**
  354. * @defgroup midi_util Device independent utility functions.
  355. * @{
  356. */
  357. /**
  358. * \enum midi_packet_length_t
  359. *
  360. * An enumeration of the possible packet length values.
  361. */
  362. typedef enum { UNDEFINED = 0, ONE = 1, TWO = 2, THREE = 3 } midi_packet_length_t;
  363. /**
  364. * @brief Test to see if the byte given is a status byte
  365. * @param theByte the byte to test
  366. * @return true if the byte given is a midi status byte
  367. */
  368. bool midi_is_statusbyte(uint8_t theByte);
  369. /**
  370. * @brief Test to see if the byte given is a realtime message
  371. * @param theByte the byte to test
  372. * @return true if it is a realtime message, false otherwise
  373. */
  374. bool midi_is_realtime(uint8_t theByte);
  375. /**
  376. * @brief Find the length of the packet associated with the status byte given
  377. * @param status the status byte
  378. * @return the length of the packet, will return UNDEFINED if the byte is not
  379. * a status byte or if it is a sysex status byte
  380. */
  381. midi_packet_length_t midi_packet_length(uint8_t status);
  382. /**@}*/
  383. /**
  384. * @defgroup defines Midi status and miscellaneous utility #defines
  385. *
  386. * @{
  387. */
  388. #define SYSEX_BEGIN 0xF0
  389. #define SYSEX_END 0xF7
  390. // if you and this with a byte and you get anything non-zero
  391. // it is a status message
  392. #define MIDI_STATUSMASK 0x80
  393. // if you and this with a status message that contains channel info,
  394. // you'll get the channel
  395. #define MIDI_CHANMASK 0x0F
  396. #define MIDI_CC 0xB0
  397. #define MIDI_NOTEON 0x90
  398. #define MIDI_NOTEOFF 0x80
  399. #define MIDI_AFTERTOUCH 0xA0
  400. #define MIDI_PITCHBEND 0xE0
  401. #define MIDI_PROGCHANGE 0xC0
  402. #define MIDI_CHANPRESSURE 0xD0
  403. // midi realtime
  404. #define MIDI_CLOCK 0xF8
  405. #define MIDI_TICK 0xF9
  406. #define MIDI_START 0xFA
  407. #define MIDI_CONTINUE 0xFB
  408. #define MIDI_STOP 0xFC
  409. #define MIDI_ACTIVESENSE 0xFE
  410. #define MIDI_RESET 0xFF
  411. #define MIDI_TC_QUARTERFRAME 0xF1
  412. #define MIDI_SONGPOSITION 0xF2
  413. #define MIDI_SONGSELECT 0xF3
  414. #define MIDI_TUNEREQUEST 0xF6
  415. // This ID is for educational or development use only
  416. #define SYSEX_EDUMANUFID 0x7D
  417. /**@}*/
  418. #ifdef __cplusplus
  419. }
  420. #endif