seq_midi_event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * MIDI byte <-> sequencer event coder
  3. *
  4. * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>,
  5. * Jaroslav Kysela <perex@perex.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 <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/string.h>
  24. #include <linux/module.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@perex.cz>");
  30. MODULE_DESCRIPTION("MIDI byte <-> sequencer event coder");
  31. MODULE_LICENSE("GPL");
  32. /* event type, index into status_event[] */
  33. /* from 0 to 6 are normal commands (note off, on, etc.) for 0x9?-0xe? */
  34. #define ST_INVALID 7
  35. #define ST_SPECIAL 8
  36. #define ST_SYSEX ST_SPECIAL
  37. /* from 8 to 15 are events for 0xf0-0xf7 */
  38. /*
  39. * prototypes
  40. */
  41. static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
  42. static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
  43. static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
  44. static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
  45. static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
  46. static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
  47. static void note_decode(struct snd_seq_event *ev, unsigned char *buf);
  48. static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf);
  49. static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf);
  50. static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf);
  51. static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf);
  52. /*
  53. * event list
  54. */
  55. static struct status_event_list {
  56. int event;
  57. int qlen;
  58. void (*encode)(struct snd_midi_event *dev, struct snd_seq_event *ev);
  59. void (*decode)(struct snd_seq_event *ev, unsigned char *buf);
  60. } status_event[] = {
  61. /* 0x80 - 0xef */
  62. {SNDRV_SEQ_EVENT_NOTEOFF, 2, note_event, note_decode},
  63. {SNDRV_SEQ_EVENT_NOTEON, 2, note_event, note_decode},
  64. {SNDRV_SEQ_EVENT_KEYPRESS, 2, note_event, note_decode},
  65. {SNDRV_SEQ_EVENT_CONTROLLER, 2, two_param_ctrl_event, two_param_decode},
  66. {SNDRV_SEQ_EVENT_PGMCHANGE, 1, one_param_ctrl_event, one_param_decode},
  67. {SNDRV_SEQ_EVENT_CHANPRESS, 1, one_param_ctrl_event, one_param_decode},
  68. {SNDRV_SEQ_EVENT_PITCHBEND, 2, pitchbend_ctrl_event, pitchbend_decode},
  69. /* invalid */
  70. {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL},
  71. /* 0xf0 - 0xff */
  72. {SNDRV_SEQ_EVENT_SYSEX, 1, NULL, NULL}, /* sysex: 0xf0 */
  73. {SNDRV_SEQ_EVENT_QFRAME, 1, one_param_event, one_param_decode}, /* 0xf1 */
  74. {SNDRV_SEQ_EVENT_SONGPOS, 2, songpos_event, songpos_decode}, /* 0xf2 */
  75. {SNDRV_SEQ_EVENT_SONGSEL, 1, one_param_event, one_param_decode}, /* 0xf3 */
  76. {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf4 */
  77. {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf5 */
  78. {SNDRV_SEQ_EVENT_TUNE_REQUEST, 0, NULL, NULL}, /* 0xf6 */
  79. {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf7 */
  80. {SNDRV_SEQ_EVENT_CLOCK, 0, NULL, NULL}, /* 0xf8 */
  81. {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf9 */
  82. {SNDRV_SEQ_EVENT_START, 0, NULL, NULL}, /* 0xfa */
  83. {SNDRV_SEQ_EVENT_CONTINUE, 0, NULL, NULL}, /* 0xfb */
  84. {SNDRV_SEQ_EVENT_STOP, 0, NULL, NULL}, /* 0xfc */
  85. {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xfd */
  86. {SNDRV_SEQ_EVENT_SENSING, 0, NULL, NULL}, /* 0xfe */
  87. {SNDRV_SEQ_EVENT_RESET, 0, NULL, NULL}, /* 0xff */
  88. };
  89. static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf, int len,
  90. struct snd_seq_event *ev);
  91. static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf, int count,
  92. struct snd_seq_event *ev);
  93. static struct extra_event_list {
  94. int event;
  95. int (*decode)(struct snd_midi_event *dev, unsigned char *buf, int len,
  96. struct snd_seq_event *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, struct snd_midi_event **rdev)
  106. {
  107. struct snd_midi_event *dev;
  108. *rdev = NULL;
  109. dev = kzalloc(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. dev->type = ST_INVALID;
  122. spin_lock_init(&dev->lock);
  123. *rdev = dev;
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(snd_midi_event_new);
  127. void snd_midi_event_free(struct snd_midi_event *dev)
  128. {
  129. if (dev != NULL) {
  130. kfree(dev->buf);
  131. kfree(dev);
  132. }
  133. }
  134. EXPORT_SYMBOL(snd_midi_event_free);
  135. /*
  136. * initialize record
  137. */
  138. static inline void reset_encode(struct snd_midi_event *dev)
  139. {
  140. dev->read = 0;
  141. dev->qlen = 0;
  142. dev->type = ST_INVALID;
  143. }
  144. void snd_midi_event_reset_encode(struct snd_midi_event *dev)
  145. {
  146. unsigned long flags;
  147. spin_lock_irqsave(&dev->lock, flags);
  148. reset_encode(dev);
  149. spin_unlock_irqrestore(&dev->lock, flags);
  150. }
  151. EXPORT_SYMBOL(snd_midi_event_reset_encode);
  152. void snd_midi_event_reset_decode(struct snd_midi_event *dev)
  153. {
  154. unsigned long flags;
  155. spin_lock_irqsave(&dev->lock, flags);
  156. dev->lastcmd = 0xff;
  157. spin_unlock_irqrestore(&dev->lock, flags);
  158. }
  159. EXPORT_SYMBOL(snd_midi_event_reset_decode);
  160. void snd_midi_event_no_status(struct snd_midi_event *dev, int on)
  161. {
  162. dev->nostat = on ? 1 : 0;
  163. }
  164. EXPORT_SYMBOL(snd_midi_event_no_status);
  165. /*
  166. * read one byte and encode to sequencer event:
  167. * return true if MIDI bytes are encoded to an event
  168. * false data is not finished
  169. */
  170. bool snd_midi_event_encode_byte(struct snd_midi_event *dev, unsigned char c,
  171. struct snd_seq_event *ev)
  172. {
  173. bool rc = false;
  174. unsigned long flags;
  175. if (c >= MIDI_CMD_COMMON_CLOCK) {
  176. /* real-time event */
  177. ev->type = status_event[ST_SPECIAL + c - 0xf0].event;
  178. ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
  179. ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
  180. return ev->type != SNDRV_SEQ_EVENT_NONE;
  181. }
  182. spin_lock_irqsave(&dev->lock, flags);
  183. if ((c & 0x80) &&
  184. (c != MIDI_CMD_COMMON_SYSEX_END || dev->type != ST_SYSEX)) {
  185. /* new command */
  186. dev->buf[0] = c;
  187. if ((c & 0xf0) == 0xf0) /* system messages */
  188. dev->type = (c & 0x0f) + ST_SPECIAL;
  189. else
  190. dev->type = (c >> 4) & 0x07;
  191. dev->read = 1;
  192. dev->qlen = status_event[dev->type].qlen;
  193. } else {
  194. if (dev->qlen > 0) {
  195. /* rest of command */
  196. dev->buf[dev->read++] = c;
  197. if (dev->type != ST_SYSEX)
  198. dev->qlen--;
  199. } else {
  200. /* running status */
  201. dev->buf[1] = c;
  202. dev->qlen = status_event[dev->type].qlen - 1;
  203. dev->read = 2;
  204. }
  205. }
  206. if (dev->qlen == 0) {
  207. ev->type = status_event[dev->type].event;
  208. ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
  209. ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
  210. if (status_event[dev->type].encode) /* set data values */
  211. status_event[dev->type].encode(dev, ev);
  212. if (dev->type >= ST_SPECIAL)
  213. dev->type = ST_INVALID;
  214. rc = true;
  215. } else if (dev->type == ST_SYSEX) {
  216. if (c == MIDI_CMD_COMMON_SYSEX_END ||
  217. dev->read >= dev->bufsize) {
  218. ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
  219. ev->flags |= SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
  220. ev->type = SNDRV_SEQ_EVENT_SYSEX;
  221. ev->data.ext.len = dev->read;
  222. ev->data.ext.ptr = dev->buf;
  223. if (c != MIDI_CMD_COMMON_SYSEX_END)
  224. dev->read = 0; /* continue to parse */
  225. else
  226. reset_encode(dev); /* all parsed */
  227. rc = true;
  228. }
  229. }
  230. spin_unlock_irqrestore(&dev->lock, flags);
  231. return rc;
  232. }
  233. EXPORT_SYMBOL(snd_midi_event_encode_byte);
  234. /* encode note event */
  235. static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
  236. {
  237. ev->data.note.channel = dev->buf[0] & 0x0f;
  238. ev->data.note.note = dev->buf[1];
  239. ev->data.note.velocity = dev->buf[2];
  240. }
  241. /* encode one parameter controls */
  242. static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
  243. {
  244. ev->data.control.channel = dev->buf[0] & 0x0f;
  245. ev->data.control.value = dev->buf[1];
  246. }
  247. /* encode pitch wheel change */
  248. static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
  249. {
  250. ev->data.control.channel = dev->buf[0] & 0x0f;
  251. ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192;
  252. }
  253. /* encode midi control change */
  254. static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
  255. {
  256. ev->data.control.channel = dev->buf[0] & 0x0f;
  257. ev->data.control.param = dev->buf[1];
  258. ev->data.control.value = dev->buf[2];
  259. }
  260. /* encode one parameter value*/
  261. static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
  262. {
  263. ev->data.control.value = dev->buf[1];
  264. }
  265. /* encode song position */
  266. static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
  267. {
  268. ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1];
  269. }
  270. /*
  271. * decode from a sequencer event to midi bytes
  272. * return the size of decoded midi events
  273. */
  274. long snd_midi_event_decode(struct snd_midi_event *dev, unsigned char *buf, long count,
  275. struct snd_seq_event *ev)
  276. {
  277. unsigned int cmd, type;
  278. if (ev->type == SNDRV_SEQ_EVENT_NONE)
  279. return -ENOENT;
  280. for (type = 0; type < ARRAY_SIZE(status_event); type++) {
  281. if (ev->type == status_event[type].event)
  282. goto __found;
  283. }
  284. for (type = 0; type < ARRAY_SIZE(extra_event); type++) {
  285. if (ev->type == extra_event[type].event)
  286. return extra_event[type].decode(dev, buf, count, ev);
  287. }
  288. return -ENOENT;
  289. __found:
  290. if (type >= ST_SPECIAL)
  291. cmd = 0xf0 + (type - ST_SPECIAL);
  292. else
  293. /* data.note.channel and data.control.channel is identical */
  294. cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f);
  295. if (cmd == MIDI_CMD_COMMON_SYSEX) {
  296. snd_midi_event_reset_decode(dev);
  297. return snd_seq_expand_var_event(ev, count, buf, 1, 0);
  298. } else {
  299. int qlen;
  300. unsigned char xbuf[4];
  301. unsigned long flags;
  302. spin_lock_irqsave(&dev->lock, flags);
  303. if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) {
  304. dev->lastcmd = cmd;
  305. spin_unlock_irqrestore(&dev->lock, flags);
  306. xbuf[0] = cmd;
  307. if (status_event[type].decode)
  308. status_event[type].decode(ev, xbuf + 1);
  309. qlen = status_event[type].qlen + 1;
  310. } else {
  311. spin_unlock_irqrestore(&dev->lock, flags);
  312. if (status_event[type].decode)
  313. status_event[type].decode(ev, xbuf + 0);
  314. qlen = status_event[type].qlen;
  315. }
  316. if (count < qlen)
  317. return -ENOMEM;
  318. memcpy(buf, xbuf, qlen);
  319. return qlen;
  320. }
  321. }
  322. EXPORT_SYMBOL(snd_midi_event_decode);
  323. /* decode note event */
  324. static void note_decode(struct snd_seq_event *ev, unsigned char *buf)
  325. {
  326. buf[0] = ev->data.note.note & 0x7f;
  327. buf[1] = ev->data.note.velocity & 0x7f;
  328. }
  329. /* decode one parameter controls */
  330. static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf)
  331. {
  332. buf[0] = ev->data.control.value & 0x7f;
  333. }
  334. /* decode pitch wheel change */
  335. static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf)
  336. {
  337. int value = ev->data.control.value + 8192;
  338. buf[0] = value & 0x7f;
  339. buf[1] = (value >> 7) & 0x7f;
  340. }
  341. /* decode midi control change */
  342. static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf)
  343. {
  344. buf[0] = ev->data.control.param & 0x7f;
  345. buf[1] = ev->data.control.value & 0x7f;
  346. }
  347. /* decode song position */
  348. static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf)
  349. {
  350. buf[0] = ev->data.control.value & 0x7f;
  351. buf[1] = (ev->data.control.value >> 7) & 0x7f;
  352. }
  353. /* decode 14bit control */
  354. static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf,
  355. int count, struct snd_seq_event *ev)
  356. {
  357. unsigned char cmd;
  358. int idx = 0;
  359. cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
  360. if (ev->data.control.param < 0x20) {
  361. if (count < 4)
  362. return -ENOMEM;
  363. if (dev->nostat && count < 6)
  364. return -ENOMEM;
  365. if (cmd != dev->lastcmd || dev->nostat) {
  366. if (count < 5)
  367. return -ENOMEM;
  368. buf[idx++] = dev->lastcmd = cmd;
  369. }
  370. buf[idx++] = ev->data.control.param;
  371. buf[idx++] = (ev->data.control.value >> 7) & 0x7f;
  372. if (dev->nostat)
  373. buf[idx++] = cmd;
  374. buf[idx++] = ev->data.control.param + 0x20;
  375. buf[idx++] = ev->data.control.value & 0x7f;
  376. } else {
  377. if (count < 2)
  378. return -ENOMEM;
  379. if (cmd != dev->lastcmd || dev->nostat) {
  380. if (count < 3)
  381. return -ENOMEM;
  382. buf[idx++] = dev->lastcmd = cmd;
  383. }
  384. buf[idx++] = ev->data.control.param & 0x7f;
  385. buf[idx++] = ev->data.control.value & 0x7f;
  386. }
  387. return idx;
  388. }
  389. /* decode reg/nonreg param */
  390. static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf,
  391. int count, struct snd_seq_event *ev)
  392. {
  393. unsigned char cmd;
  394. char *cbytes;
  395. static char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB,
  396. MIDI_CTL_NONREG_PARM_NUM_LSB,
  397. MIDI_CTL_MSB_DATA_ENTRY,
  398. MIDI_CTL_LSB_DATA_ENTRY };
  399. static char cbytes_rpn[4] = { MIDI_CTL_REGIST_PARM_NUM_MSB,
  400. MIDI_CTL_REGIST_PARM_NUM_LSB,
  401. MIDI_CTL_MSB_DATA_ENTRY,
  402. MIDI_CTL_LSB_DATA_ENTRY };
  403. unsigned char bytes[4];
  404. int idx = 0, i;
  405. if (count < 8)
  406. return -ENOMEM;
  407. if (dev->nostat && count < 12)
  408. return -ENOMEM;
  409. cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
  410. bytes[0] = (ev->data.control.param & 0x3f80) >> 7;
  411. bytes[1] = ev->data.control.param & 0x007f;
  412. bytes[2] = (ev->data.control.value & 0x3f80) >> 7;
  413. bytes[3] = ev->data.control.value & 0x007f;
  414. if (cmd != dev->lastcmd && !dev->nostat) {
  415. if (count < 9)
  416. return -ENOMEM;
  417. buf[idx++] = dev->lastcmd = cmd;
  418. }
  419. cbytes = ev->type == SNDRV_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn;
  420. for (i = 0; i < 4; i++) {
  421. if (dev->nostat)
  422. buf[idx++] = dev->lastcmd = cmd;
  423. buf[idx++] = cbytes[i];
  424. buf[idx++] = bytes[i];
  425. }
  426. return idx;
  427. }