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