seq_midi_event.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * MIDI byte <-> sequencer event coder
  3. *
  4. * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>,
  5. * Jaroslav Kysela <perex@suse.cz>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/slab.h>
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <sound/core.h>
  26. #include <sound/seq_kernel.h>
  27. #include <sound/seq_midi_event.h>
  28. #include <sound/asoundef.h>
  29. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
  30. MODULE_DESCRIPTION("MIDI byte <-> sequencer event coder");
  31. MODULE_LICENSE("GPL");
  32. /* queue type */
  33. /* from 0 to 7 are normal commands (note off, on, etc.) */
  34. #define ST_NOTEOFF 0
  35. #define ST_NOTEON 1
  36. #define ST_SPECIAL 8
  37. #define ST_SYSEX ST_SPECIAL
  38. /* from 8 to 15 are events for 0xf0-0xf7 */
  39. /* status event types */
  40. typedef void (*event_encode_t)(snd_midi_event_t *dev, snd_seq_event_t *ev);
  41. typedef void (*event_decode_t)(snd_seq_event_t *ev, unsigned char *buf);
  42. /*
  43. * prototypes
  44. */
  45. static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
  46. static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
  47. static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
  48. static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
  49. static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
  50. static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
  51. static void note_decode(snd_seq_event_t *ev, unsigned char *buf);
  52. static void one_param_decode(snd_seq_event_t *ev, unsigned char *buf);
  53. static void pitchbend_decode(snd_seq_event_t *ev, unsigned char *buf);
  54. static void two_param_decode(snd_seq_event_t *ev, unsigned char *buf);
  55. static void songpos_decode(snd_seq_event_t *ev, unsigned char *buf);
  56. /*
  57. * event list
  58. */
  59. static struct status_event_list_t {
  60. int event;
  61. int qlen;
  62. event_encode_t encode;
  63. event_decode_t decode;
  64. } status_event[] = {
  65. /* 0x80 - 0xf0 */
  66. {SNDRV_SEQ_EVENT_NOTEOFF, 2, note_event, note_decode},
  67. {SNDRV_SEQ_EVENT_NOTEON, 2, note_event, note_decode},
  68. {SNDRV_SEQ_EVENT_KEYPRESS, 2, note_event, note_decode},
  69. {SNDRV_SEQ_EVENT_CONTROLLER, 2, two_param_ctrl_event, two_param_decode},
  70. {SNDRV_SEQ_EVENT_PGMCHANGE, 1, one_param_ctrl_event, one_param_decode},
  71. {SNDRV_SEQ_EVENT_CHANPRESS, 1, one_param_ctrl_event, one_param_decode},
  72. {SNDRV_SEQ_EVENT_PITCHBEND, 2, pitchbend_ctrl_event, pitchbend_decode},
  73. {SNDRV_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf0 */
  74. /* 0xf0 - 0xff */
  75. {SNDRV_SEQ_EVENT_SYSEX, 1, NULL, NULL}, /* sysex: 0xf0 */
  76. {SNDRV_SEQ_EVENT_QFRAME, 1, one_param_event, one_param_decode}, /* 0xf1 */
  77. {SNDRV_SEQ_EVENT_SONGPOS, 2, songpos_event, songpos_decode}, /* 0xf2 */
  78. {SNDRV_SEQ_EVENT_SONGSEL, 1, one_param_event, one_param_decode}, /* 0xf3 */
  79. {SNDRV_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf4 */
  80. {SNDRV_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf5 */
  81. {SNDRV_SEQ_EVENT_TUNE_REQUEST, 0, NULL, NULL}, /* 0xf6 */
  82. {SNDRV_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf7 */
  83. {SNDRV_SEQ_EVENT_CLOCK, 0, NULL, NULL}, /* 0xf8 */
  84. {SNDRV_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf9 */
  85. {SNDRV_SEQ_EVENT_START, 0, NULL, NULL}, /* 0xfa */
  86. {SNDRV_SEQ_EVENT_CONTINUE, 0, NULL, NULL}, /* 0xfb */
  87. {SNDRV_SEQ_EVENT_STOP, 0, NULL, NULL}, /* 0xfc */
  88. {SNDRV_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xfd */
  89. {SNDRV_SEQ_EVENT_SENSING, 0, NULL, NULL}, /* 0xfe */
  90. {SNDRV_SEQ_EVENT_RESET, 0, NULL, NULL}, /* 0xff */
  91. };
  92. static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);
  93. static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, snd_seq_event_t *ev);
  94. static struct extra_event_list_t {
  95. int event;
  96. int (*decode)(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);
  97. } extra_event[] = {
  98. {SNDRV_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
  99. {SNDRV_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn},
  100. {SNDRV_SEQ_EVENT_REGPARAM, extra_decode_xrpn},
  101. };
  102. /*
  103. * new/delete record
  104. */
  105. int snd_midi_event_new(int bufsize, snd_midi_event_t **rdev)
  106. {
  107. snd_midi_event_t *dev;
  108. *rdev = NULL;
  109. dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
  110. if (dev == NULL)
  111. return -ENOMEM;
  112. if (bufsize > 0) {
  113. dev->buf = kmalloc(bufsize, GFP_KERNEL);
  114. if (dev->buf == NULL) {
  115. kfree(dev);
  116. return -ENOMEM;
  117. }
  118. }
  119. dev->bufsize = bufsize;
  120. dev->lastcmd = 0xff;
  121. spin_lock_init(&dev->lock);
  122. *rdev = dev;
  123. return 0;
  124. }
  125. void snd_midi_event_free(snd_midi_event_t *dev)
  126. {
  127. if (dev != NULL) {
  128. kfree(dev->buf);
  129. kfree(dev);
  130. }
  131. }
  132. /*
  133. * initialize record
  134. */
  135. inline static void reset_encode(snd_midi_event_t *dev)
  136. {
  137. dev->read = 0;
  138. dev->qlen = 0;
  139. dev->type = 0;
  140. }
  141. void snd_midi_event_reset_encode(snd_midi_event_t *dev)
  142. {
  143. unsigned long flags;
  144. spin_lock_irqsave(&dev->lock, flags);
  145. reset_encode(dev);
  146. spin_unlock_irqrestore(&dev->lock, flags);
  147. }
  148. void snd_midi_event_reset_decode(snd_midi_event_t *dev)
  149. {
  150. unsigned long flags;
  151. spin_lock_irqsave(&dev->lock, flags);
  152. dev->lastcmd = 0xff;
  153. spin_unlock_irqrestore(&dev->lock, flags);
  154. }
  155. void snd_midi_event_init(snd_midi_event_t *dev)
  156. {
  157. snd_midi_event_reset_encode(dev);
  158. snd_midi_event_reset_decode(dev);
  159. }
  160. void snd_midi_event_no_status(snd_midi_event_t *dev, int on)
  161. {
  162. dev->nostat = on ? 1 : 0;
  163. }
  164. /*
  165. * resize buffer
  166. */
  167. int snd_midi_event_resize_buffer(snd_midi_event_t *dev, int bufsize)
  168. {
  169. unsigned char *new_buf, *old_buf;
  170. unsigned long flags;
  171. if (bufsize == dev->bufsize)
  172. return 0;
  173. new_buf = kmalloc(bufsize, GFP_KERNEL);
  174. if (new_buf == NULL)
  175. return -ENOMEM;
  176. spin_lock_irqsave(&dev->lock, flags);
  177. old_buf = dev->buf;
  178. dev->buf = new_buf;
  179. dev->bufsize = bufsize;
  180. reset_encode(dev);
  181. spin_unlock_irqrestore(&dev->lock, flags);
  182. kfree(old_buf);
  183. return 0;
  184. }
  185. /*
  186. * read bytes and encode to sequencer event if finished
  187. * return the size of encoded bytes
  188. */
  189. long snd_midi_event_encode(snd_midi_event_t *dev, unsigned char *buf, long count, snd_seq_event_t *ev)
  190. {
  191. long result = 0;
  192. int rc;
  193. ev->type = SNDRV_SEQ_EVENT_NONE;
  194. while (count-- > 0) {
  195. rc = snd_midi_event_encode_byte(dev, *buf++, ev);
  196. result++;
  197. if (rc < 0)
  198. return rc;
  199. else if (rc > 0)
  200. return result;
  201. }
  202. return result;
  203. }
  204. /*
  205. * read one byte and encode to sequencer event:
  206. * return 1 if MIDI bytes are encoded to an event
  207. * 0 data is not finished
  208. * negative for error
  209. */
  210. int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev)
  211. {
  212. int rc = 0;
  213. unsigned long flags;
  214. c &= 0xff;
  215. if (c >= MIDI_CMD_COMMON_CLOCK) {
  216. /* real-time event */
  217. ev->type = status_event[ST_SPECIAL + c - 0xf0].event;
  218. ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
  219. ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
  220. return 1;
  221. }
  222. spin_lock_irqsave(&dev->lock, flags);
  223. if (dev->qlen > 0) {
  224. /* rest of command */
  225. dev->buf[dev->read++] = c;
  226. if (dev->type != ST_SYSEX)
  227. dev->qlen--;
  228. } else {
  229. /* new command */
  230. dev->read = 1;
  231. if (c & 0x80) {
  232. dev->buf[0] = c;
  233. if ((c & 0xf0) == 0xf0) /* special events */
  234. dev->type = (c & 0x0f) + ST_SPECIAL;
  235. else
  236. dev->type = (c >> 4) & 0x07;
  237. dev->qlen = status_event[dev->type].qlen;
  238. } else {
  239. /* process this byte as argument */
  240. dev->buf[dev->read++] = c;
  241. dev->qlen = status_event[dev->type].qlen - 1;
  242. }
  243. }
  244. if (dev->qlen == 0) {
  245. ev->type = status_event[dev->type].event;
  246. ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
  247. ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
  248. if (status_event[dev->type].encode) /* set data values */
  249. status_event[dev->type].encode(dev, ev);
  250. rc = 1;
  251. } else if (dev->type == ST_SYSEX) {
  252. if (c == MIDI_CMD_COMMON_SYSEX_END ||
  253. dev->read >= dev->bufsize) {
  254. ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
  255. ev->flags |= SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
  256. ev->type = SNDRV_SEQ_EVENT_SYSEX;
  257. ev->data.ext.len = dev->read;
  258. ev->data.ext.ptr = dev->buf;
  259. if (c != MIDI_CMD_COMMON_SYSEX_END)
  260. dev->read = 0; /* continue to parse */
  261. else
  262. reset_encode(dev); /* all parsed */
  263. rc = 1;
  264. }
  265. }
  266. spin_unlock_irqrestore(&dev->lock, flags);
  267. return rc;
  268. }
  269. /* encode note event */
  270. static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
  271. {
  272. ev->data.note.channel = dev->buf[0] & 0x0f;
  273. ev->data.note.note = dev->buf[1];
  274. ev->data.note.velocity = dev->buf[2];
  275. }
  276. /* encode one parameter controls */
  277. static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
  278. {
  279. ev->data.control.channel = dev->buf[0] & 0x0f;
  280. ev->data.control.value = dev->buf[1];
  281. }
  282. /* encode pitch wheel change */
  283. static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
  284. {
  285. ev->data.control.channel = dev->buf[0] & 0x0f;
  286. ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192;
  287. }
  288. /* encode midi control change */
  289. static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
  290. {
  291. ev->data.control.channel = dev->buf[0] & 0x0f;
  292. ev->data.control.param = dev->buf[1];
  293. ev->data.control.value = dev->buf[2];
  294. }
  295. /* encode one parameter value*/
  296. static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
  297. {
  298. ev->data.control.value = dev->buf[1];
  299. }
  300. /* encode song position */
  301. static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
  302. {
  303. ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1];
  304. }
  305. /*
  306. * decode from a sequencer event to midi bytes
  307. * return the size of decoded midi events
  308. */
  309. long snd_midi_event_decode(snd_midi_event_t *dev, unsigned char *buf, long count, snd_seq_event_t *ev)
  310. {
  311. unsigned int cmd, type;
  312. if (ev->type == SNDRV_SEQ_EVENT_NONE)
  313. return -ENOENT;
  314. for (type = 0; type < ARRAY_SIZE(status_event); type++) {
  315. if (ev->type == status_event[type].event)
  316. goto __found;
  317. }
  318. for (type = 0; type < ARRAY_SIZE(extra_event); type++) {
  319. if (ev->type == extra_event[type].event)
  320. return extra_event[type].decode(dev, buf, count, ev);
  321. }
  322. return -ENOENT;
  323. __found:
  324. if (type >= ST_SPECIAL)
  325. cmd = 0xf0 + (type - ST_SPECIAL);
  326. else
  327. /* data.note.channel and data.control.channel is identical */
  328. cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f);
  329. if (cmd == MIDI_CMD_COMMON_SYSEX) {
  330. snd_midi_event_reset_decode(dev);
  331. return snd_seq_expand_var_event(ev, count, buf, 1, 0);
  332. } else {
  333. int qlen;
  334. unsigned char xbuf[4];
  335. unsigned long flags;
  336. spin_lock_irqsave(&dev->lock, flags);
  337. if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) {
  338. dev->lastcmd = cmd;
  339. spin_unlock_irqrestore(&dev->lock, flags);
  340. xbuf[0] = cmd;
  341. if (status_event[type].decode)
  342. status_event[type].decode(ev, xbuf + 1);
  343. qlen = status_event[type].qlen + 1;
  344. } else {
  345. spin_unlock_irqrestore(&dev->lock, flags);
  346. if (status_event[type].decode)
  347. status_event[type].decode(ev, xbuf + 0);
  348. qlen = status_event[type].qlen;
  349. }
  350. if (count < qlen)
  351. return -ENOMEM;
  352. memcpy(buf, xbuf, qlen);
  353. return qlen;
  354. }
  355. }
  356. /* decode note event */
  357. static void note_decode(snd_seq_event_t *ev, unsigned char *buf)
  358. {
  359. buf[0] = ev->data.note.note & 0x7f;
  360. buf[1] = ev->data.note.velocity & 0x7f;
  361. }
  362. /* decode one parameter controls */
  363. static void one_param_decode(snd_seq_event_t *ev, unsigned char *buf)
  364. {
  365. buf[0] = ev->data.control.value & 0x7f;
  366. }
  367. /* decode pitch wheel change */
  368. static void pitchbend_decode(snd_seq_event_t *ev, unsigned char *buf)
  369. {
  370. int value = ev->data.control.value + 8192;
  371. buf[0] = value & 0x7f;
  372. buf[1] = (value >> 7) & 0x7f;
  373. }
  374. /* decode midi control change */
  375. static void two_param_decode(snd_seq_event_t *ev, unsigned char *buf)
  376. {
  377. buf[0] = ev->data.control.param & 0x7f;
  378. buf[1] = ev->data.control.value & 0x7f;
  379. }
  380. /* decode song position */
  381. static void songpos_decode(snd_seq_event_t *ev, unsigned char *buf)
  382. {
  383. buf[0] = ev->data.control.value & 0x7f;
  384. buf[1] = (ev->data.control.value >> 7) & 0x7f;
  385. }
  386. /* decode 14bit control */
  387. static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int count, snd_seq_event_t *ev)
  388. {
  389. unsigned char cmd;
  390. int idx = 0;
  391. cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
  392. if (ev->data.control.param < 0x20) {
  393. if (count < 4)
  394. return -ENOMEM;
  395. if (dev->nostat && count < 6)
  396. return -ENOMEM;
  397. if (cmd != dev->lastcmd || dev->nostat) {
  398. if (count < 5)
  399. return -ENOMEM;
  400. buf[idx++] = dev->lastcmd = cmd;
  401. }
  402. buf[idx++] = ev->data.control.param;
  403. buf[idx++] = (ev->data.control.value >> 7) & 0x7f;
  404. if (dev->nostat)
  405. buf[idx++] = cmd;
  406. buf[idx++] = ev->data.control.param + 0x20;
  407. buf[idx++] = ev->data.control.value & 0x7f;
  408. } else {
  409. if (count < 2)
  410. return -ENOMEM;
  411. if (cmd != dev->lastcmd || dev->nostat) {
  412. if (count < 3)
  413. return -ENOMEM;
  414. buf[idx++] = dev->lastcmd = cmd;
  415. }
  416. buf[idx++] = ev->data.control.param & 0x7f;
  417. buf[idx++] = ev->data.control.value & 0x7f;
  418. }
  419. return idx;
  420. }
  421. /* decode reg/nonreg param */
  422. static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, snd_seq_event_t *ev)
  423. {
  424. unsigned char cmd;
  425. char *cbytes;
  426. static char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB,
  427. MIDI_CTL_NONREG_PARM_NUM_LSB,
  428. MIDI_CTL_MSB_DATA_ENTRY,
  429. MIDI_CTL_LSB_DATA_ENTRY };
  430. static char cbytes_rpn[4] = { MIDI_CTL_REGIST_PARM_NUM_MSB,
  431. MIDI_CTL_REGIST_PARM_NUM_LSB,
  432. MIDI_CTL_MSB_DATA_ENTRY,
  433. MIDI_CTL_LSB_DATA_ENTRY };
  434. unsigned char bytes[4];
  435. int idx = 0, i;
  436. if (count < 8)
  437. return -ENOMEM;
  438. if (dev->nostat && count < 12)
  439. return -ENOMEM;
  440. cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
  441. bytes[0] = ev->data.control.param & 0x007f;
  442. bytes[1] = (ev->data.control.param & 0x3f80) >> 7;
  443. bytes[2] = ev->data.control.value & 0x007f;
  444. bytes[3] = (ev->data.control.value & 0x3f80) >> 7;
  445. if (cmd != dev->lastcmd && !dev->nostat) {
  446. if (count < 9)
  447. return -ENOMEM;
  448. buf[idx++] = dev->lastcmd = cmd;
  449. }
  450. cbytes = ev->type == SNDRV_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn;
  451. for (i = 0; i < 4; i++) {
  452. if (dev->nostat)
  453. buf[idx++] = dev->lastcmd = cmd;
  454. buf[idx++] = cbytes[i];
  455. buf[idx++] = bytes[i];
  456. }
  457. return idx;
  458. }
  459. /*
  460. * exports
  461. */
  462. EXPORT_SYMBOL(snd_midi_event_new);
  463. EXPORT_SYMBOL(snd_midi_event_free);
  464. EXPORT_SYMBOL(snd_midi_event_resize_buffer);
  465. EXPORT_SYMBOL(snd_midi_event_init);
  466. EXPORT_SYMBOL(snd_midi_event_reset_encode);
  467. EXPORT_SYMBOL(snd_midi_event_reset_decode);
  468. EXPORT_SYMBOL(snd_midi_event_no_status);
  469. EXPORT_SYMBOL(snd_midi_event_encode);
  470. EXPORT_SYMBOL(snd_midi_event_encode_byte);
  471. EXPORT_SYMBOL(snd_midi_event_decode);
  472. static int __init alsa_seq_midi_event_init(void)
  473. {
  474. return 0;
  475. }
  476. static void __exit alsa_seq_midi_event_exit(void)
  477. {
  478. }
  479. module_init(alsa_seq_midi_event_init)
  480. module_exit(alsa_seq_midi_event_exit)