GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / core / seq / seq_midi_event.c
blob03ae37917f3a5d04a55496bbd2746e25f31b55ba
1 /*
2 * MIDI byte <-> sequencer event coder
4 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>,
5 * Jaroslav Kysela <perex@perex.cz>
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.
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.
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
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>
30 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
31 MODULE_DESCRIPTION("MIDI byte <-> sequencer event coder");
32 MODULE_LICENSE("GPL");
34 /* event type, index into status_event[] */
35 /* from 0 to 6 are normal commands (note off, on, etc.) for 0x9?-0xe? */
36 #define ST_INVALID 7
37 #define ST_SPECIAL 8
38 #define ST_SYSEX ST_SPECIAL
39 /* from 8 to 15 are events for 0xf0-0xf7 */
43 * prototypes
45 static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
46 static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
47 static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
48 static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
49 static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
50 static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
51 static void note_decode(struct snd_seq_event *ev, unsigned char *buf);
52 static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf);
53 static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf);
54 static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf);
55 static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf);
58 * event list
60 static struct status_event_list {
61 int event;
62 int qlen;
63 void (*encode)(struct snd_midi_event *dev, struct snd_seq_event *ev);
64 void (*decode)(struct snd_seq_event *ev, unsigned char *buf);
65 } status_event[] = {
66 /* 0x80 - 0xef */
67 {SNDRV_SEQ_EVENT_NOTEOFF, 2, note_event, note_decode},
68 {SNDRV_SEQ_EVENT_NOTEON, 2, note_event, note_decode},
69 {SNDRV_SEQ_EVENT_KEYPRESS, 2, note_event, note_decode},
70 {SNDRV_SEQ_EVENT_CONTROLLER, 2, two_param_ctrl_event, two_param_decode},
71 {SNDRV_SEQ_EVENT_PGMCHANGE, 1, one_param_ctrl_event, one_param_decode},
72 {SNDRV_SEQ_EVENT_CHANPRESS, 1, one_param_ctrl_event, one_param_decode},
73 {SNDRV_SEQ_EVENT_PITCHBEND, 2, pitchbend_ctrl_event, pitchbend_decode},
74 /* invalid */
75 {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL},
76 /* 0xf0 - 0xff */
77 {SNDRV_SEQ_EVENT_SYSEX, 1, NULL, NULL}, /* sysex: 0xf0 */
78 {SNDRV_SEQ_EVENT_QFRAME, 1, one_param_event, one_param_decode}, /* 0xf1 */
79 {SNDRV_SEQ_EVENT_SONGPOS, 2, songpos_event, songpos_decode}, /* 0xf2 */
80 {SNDRV_SEQ_EVENT_SONGSEL, 1, one_param_event, one_param_decode}, /* 0xf3 */
81 {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf4 */
82 {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf5 */
83 {SNDRV_SEQ_EVENT_TUNE_REQUEST, 0, NULL, NULL}, /* 0xf6 */
84 {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf7 */
85 {SNDRV_SEQ_EVENT_CLOCK, 0, NULL, NULL}, /* 0xf8 */
86 {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf9 */
87 {SNDRV_SEQ_EVENT_START, 0, NULL, NULL}, /* 0xfa */
88 {SNDRV_SEQ_EVENT_CONTINUE, 0, NULL, NULL}, /* 0xfb */
89 {SNDRV_SEQ_EVENT_STOP, 0, NULL, NULL}, /* 0xfc */
90 {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xfd */
91 {SNDRV_SEQ_EVENT_SENSING, 0, NULL, NULL}, /* 0xfe */
92 {SNDRV_SEQ_EVENT_RESET, 0, NULL, NULL}, /* 0xff */
95 static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf, int len,
96 struct snd_seq_event *ev);
97 static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf, int count,
98 struct snd_seq_event *ev);
100 static struct extra_event_list {
101 int event;
102 int (*decode)(struct snd_midi_event *dev, unsigned char *buf, int len,
103 struct snd_seq_event *ev);
104 } extra_event[] = {
105 {SNDRV_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
106 {SNDRV_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn},
107 {SNDRV_SEQ_EVENT_REGPARAM, extra_decode_xrpn},
111 * new/delete record
114 int snd_midi_event_new(int bufsize, struct snd_midi_event **rdev)
116 struct snd_midi_event *dev;
118 *rdev = NULL;
119 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
120 if (dev == NULL)
121 return -ENOMEM;
122 if (bufsize > 0) {
123 dev->buf = kmalloc(bufsize, GFP_KERNEL);
124 if (dev->buf == NULL) {
125 kfree(dev);
126 return -ENOMEM;
129 dev->bufsize = bufsize;
130 dev->lastcmd = 0xff;
131 dev->type = ST_INVALID;
132 spin_lock_init(&dev->lock);
133 *rdev = dev;
134 return 0;
137 void snd_midi_event_free(struct snd_midi_event *dev)
139 if (dev != NULL) {
140 kfree(dev->buf);
141 kfree(dev);
146 * initialize record
148 static inline void reset_encode(struct snd_midi_event *dev)
150 dev->read = 0;
151 dev->qlen = 0;
152 dev->type = ST_INVALID;
155 void snd_midi_event_reset_encode(struct snd_midi_event *dev)
157 unsigned long flags;
159 spin_lock_irqsave(&dev->lock, flags);
160 reset_encode(dev);
161 spin_unlock_irqrestore(&dev->lock, flags);
164 void snd_midi_event_reset_decode(struct snd_midi_event *dev)
166 unsigned long flags;
168 spin_lock_irqsave(&dev->lock, flags);
169 dev->lastcmd = 0xff;
170 spin_unlock_irqrestore(&dev->lock, flags);
174 void snd_midi_event_no_status(struct snd_midi_event *dev, int on)
176 dev->nostat = on ? 1 : 0;
180 * resize buffer
184 * read bytes and encode to sequencer event if finished
185 * return the size of encoded bytes
187 long snd_midi_event_encode(struct snd_midi_event *dev, unsigned char *buf, long count,
188 struct snd_seq_event *ev)
190 long result = 0;
191 int rc;
193 ev->type = SNDRV_SEQ_EVENT_NONE;
195 while (count-- > 0) {
196 rc = snd_midi_event_encode_byte(dev, *buf++, ev);
197 result++;
198 if (rc < 0)
199 return rc;
200 else if (rc > 0)
201 return result;
204 return result;
208 * read one byte and encode to sequencer event:
209 * return 1 if MIDI bytes are encoded to an event
210 * 0 data is not finished
211 * negative for error
213 int snd_midi_event_encode_byte(struct snd_midi_event *dev, int c,
214 struct snd_seq_event *ev)
216 int rc = 0;
217 unsigned long flags;
219 c &= 0xff;
221 if (c >= MIDI_CMD_COMMON_CLOCK) {
222 /* real-time event */
223 ev->type = status_event[ST_SPECIAL + c - 0xf0].event;
224 ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
225 ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
226 return ev->type != SNDRV_SEQ_EVENT_NONE;
229 spin_lock_irqsave(&dev->lock, flags);
230 if ((c & 0x80) &&
231 (c != MIDI_CMD_COMMON_SYSEX_END || dev->type != ST_SYSEX)) {
232 /* new command */
233 dev->buf[0] = c;
234 if ((c & 0xf0) == 0xf0) /* system messages */
235 dev->type = (c & 0x0f) + ST_SPECIAL;
236 else
237 dev->type = (c >> 4) & 0x07;
238 dev->read = 1;
239 dev->qlen = status_event[dev->type].qlen;
240 } else {
241 if (dev->qlen > 0) {
242 /* rest of command */
243 dev->buf[dev->read++] = c;
244 if (dev->type != ST_SYSEX)
245 dev->qlen--;
246 } else {
247 /* running status */
248 dev->buf[1] = c;
249 dev->qlen = status_event[dev->type].qlen - 1;
250 dev->read = 2;
253 if (dev->qlen == 0) {
254 ev->type = status_event[dev->type].event;
255 ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
256 ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
257 if (status_event[dev->type].encode) /* set data values */
258 status_event[dev->type].encode(dev, ev);
259 if (dev->type >= ST_SPECIAL)
260 dev->type = ST_INVALID;
261 rc = 1;
262 } else if (dev->type == ST_SYSEX) {
263 if (c == MIDI_CMD_COMMON_SYSEX_END ||
264 dev->read >= dev->bufsize) {
265 ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
266 ev->flags |= SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
267 ev->type = SNDRV_SEQ_EVENT_SYSEX;
268 ev->data.ext.len = dev->read;
269 ev->data.ext.ptr = dev->buf;
270 if (c != MIDI_CMD_COMMON_SYSEX_END)
271 dev->read = 0; /* continue to parse */
272 else
273 reset_encode(dev); /* all parsed */
274 rc = 1;
278 spin_unlock_irqrestore(&dev->lock, flags);
279 return rc;
282 /* encode note event */
283 static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
285 ev->data.note.channel = dev->buf[0] & 0x0f;
286 ev->data.note.note = dev->buf[1];
287 ev->data.note.velocity = dev->buf[2];
290 /* encode one parameter controls */
291 static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
293 ev->data.control.channel = dev->buf[0] & 0x0f;
294 ev->data.control.value = dev->buf[1];
297 /* encode pitch wheel change */
298 static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
300 ev->data.control.channel = dev->buf[0] & 0x0f;
301 ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192;
304 /* encode midi control change */
305 static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
307 ev->data.control.channel = dev->buf[0] & 0x0f;
308 ev->data.control.param = dev->buf[1];
309 ev->data.control.value = dev->buf[2];
312 /* encode one parameter value*/
313 static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
315 ev->data.control.value = dev->buf[1];
318 /* encode song position */
319 static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
321 ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1];
325 * decode from a sequencer event to midi bytes
326 * return the size of decoded midi events
328 long snd_midi_event_decode(struct snd_midi_event *dev, unsigned char *buf, long count,
329 struct snd_seq_event *ev)
331 unsigned int cmd, type;
333 if (ev->type == SNDRV_SEQ_EVENT_NONE)
334 return -ENOENT;
336 for (type = 0; type < ARRAY_SIZE(status_event); type++) {
337 if (ev->type == status_event[type].event)
338 goto __found;
340 for (type = 0; type < ARRAY_SIZE(extra_event); type++) {
341 if (ev->type == extra_event[type].event)
342 return extra_event[type].decode(dev, buf, count, ev);
344 return -ENOENT;
346 __found:
347 if (type >= ST_SPECIAL)
348 cmd = 0xf0 + (type - ST_SPECIAL);
349 else
350 /* data.note.channel and data.control.channel is identical */
351 cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f);
354 if (cmd == MIDI_CMD_COMMON_SYSEX) {
355 snd_midi_event_reset_decode(dev);
356 return snd_seq_expand_var_event(ev, count, buf, 1, 0);
357 } else {
358 int qlen;
359 unsigned char xbuf[4];
360 unsigned long flags;
362 spin_lock_irqsave(&dev->lock, flags);
363 if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) {
364 dev->lastcmd = cmd;
365 spin_unlock_irqrestore(&dev->lock, flags);
366 xbuf[0] = cmd;
367 if (status_event[type].decode)
368 status_event[type].decode(ev, xbuf + 1);
369 qlen = status_event[type].qlen + 1;
370 } else {
371 spin_unlock_irqrestore(&dev->lock, flags);
372 if (status_event[type].decode)
373 status_event[type].decode(ev, xbuf + 0);
374 qlen = status_event[type].qlen;
376 if (count < qlen)
377 return -ENOMEM;
378 memcpy(buf, xbuf, qlen);
379 return qlen;
384 /* decode note event */
385 static void note_decode(struct snd_seq_event *ev, unsigned char *buf)
387 buf[0] = ev->data.note.note & 0x7f;
388 buf[1] = ev->data.note.velocity & 0x7f;
391 /* decode one parameter controls */
392 static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf)
394 buf[0] = ev->data.control.value & 0x7f;
397 /* decode pitch wheel change */
398 static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf)
400 int value = ev->data.control.value + 8192;
401 buf[0] = value & 0x7f;
402 buf[1] = (value >> 7) & 0x7f;
405 /* decode midi control change */
406 static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf)
408 buf[0] = ev->data.control.param & 0x7f;
409 buf[1] = ev->data.control.value & 0x7f;
412 /* decode song position */
413 static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf)
415 buf[0] = ev->data.control.value & 0x7f;
416 buf[1] = (ev->data.control.value >> 7) & 0x7f;
419 /* decode 14bit control */
420 static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf,
421 int count, struct snd_seq_event *ev)
423 unsigned char cmd;
424 int idx = 0;
426 cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
427 if (ev->data.control.param < 0x20) {
428 if (count < 4)
429 return -ENOMEM;
430 if (dev->nostat && count < 6)
431 return -ENOMEM;
432 if (cmd != dev->lastcmd || dev->nostat) {
433 if (count < 5)
434 return -ENOMEM;
435 buf[idx++] = dev->lastcmd = cmd;
437 buf[idx++] = ev->data.control.param;
438 buf[idx++] = (ev->data.control.value >> 7) & 0x7f;
439 if (dev->nostat)
440 buf[idx++] = cmd;
441 buf[idx++] = ev->data.control.param + 0x20;
442 buf[idx++] = ev->data.control.value & 0x7f;
443 } else {
444 if (count < 2)
445 return -ENOMEM;
446 if (cmd != dev->lastcmd || dev->nostat) {
447 if (count < 3)
448 return -ENOMEM;
449 buf[idx++] = dev->lastcmd = cmd;
451 buf[idx++] = ev->data.control.param & 0x7f;
452 buf[idx++] = ev->data.control.value & 0x7f;
454 return idx;
457 /* decode reg/nonreg param */
458 static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf,
459 int count, struct snd_seq_event *ev)
461 unsigned char cmd;
462 char *cbytes;
463 static char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB,
464 MIDI_CTL_NONREG_PARM_NUM_LSB,
465 MIDI_CTL_MSB_DATA_ENTRY,
466 MIDI_CTL_LSB_DATA_ENTRY };
467 static char cbytes_rpn[4] = { MIDI_CTL_REGIST_PARM_NUM_MSB,
468 MIDI_CTL_REGIST_PARM_NUM_LSB,
469 MIDI_CTL_MSB_DATA_ENTRY,
470 MIDI_CTL_LSB_DATA_ENTRY };
471 unsigned char bytes[4];
472 int idx = 0, i;
474 if (count < 8)
475 return -ENOMEM;
476 if (dev->nostat && count < 12)
477 return -ENOMEM;
478 cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
479 bytes[0] = (ev->data.control.param & 0x3f80) >> 7;
480 bytes[1] = ev->data.control.param & 0x007f;
481 bytes[2] = (ev->data.control.value & 0x3f80) >> 7;
482 bytes[3] = ev->data.control.value & 0x007f;
483 if (cmd != dev->lastcmd && !dev->nostat) {
484 if (count < 9)
485 return -ENOMEM;
486 buf[idx++] = dev->lastcmd = cmd;
488 cbytes = ev->type == SNDRV_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn;
489 for (i = 0; i < 4; i++) {
490 if (dev->nostat)
491 buf[idx++] = dev->lastcmd = cmd;
492 buf[idx++] = cbytes[i];
493 buf[idx++] = bytes[i];
495 return idx;
499 * exports
502 EXPORT_SYMBOL(snd_midi_event_new);
503 EXPORT_SYMBOL(snd_midi_event_free);
504 EXPORT_SYMBOL(snd_midi_event_reset_encode);
505 EXPORT_SYMBOL(snd_midi_event_reset_decode);
506 EXPORT_SYMBOL(snd_midi_event_no_status);
507 EXPORT_SYMBOL(snd_midi_event_encode);
508 EXPORT_SYMBOL(snd_midi_event_encode_byte);
509 EXPORT_SYMBOL(snd_midi_event_decode);
511 static int __init alsa_seq_midi_event_init(void)
513 return 0;
516 static void __exit alsa_seq_midi_event_exit(void)
520 module_init(alsa_seq_midi_event_init)
521 module_exit(alsa_seq_midi_event_exit)