midi.h 13 KB

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