2 * MIDI byte <-> sequencer event coder
4 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>,
5 * Jaroslav Kysela <perex@suse.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 <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>
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");
36 /* from 0 to 7 are normal commands (note off, on, etc.) */
40 #define ST_SYSEX ST_SPECIAL
41 /* from 8 to 15 are events for 0xf0-0xf7 */
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
);
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
);
66 static struct status_event_list_t
{
69 event_encode_t encode
;
70 event_decode_t decode
;
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 */
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 */
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
);
103 static struct extra_event_list_t
{
105 int (*decode
)(snd_midi_event_t
*dev
, unsigned char *buf
, int len
, snd_seq_event_t
*ev
);
107 {SNDRV_SEQ_EVENT_CONTROL14
, extra_decode_ctrl14
},
108 {SNDRV_SEQ_EVENT_NONREGPARAM
, extra_decode_xrpn
},
109 {SNDRV_SEQ_EVENT_REGPARAM
, extra_decode_xrpn
},
116 int snd_midi_event_new(int bufsize
, snd_midi_event_t
**rdev
)
118 snd_midi_event_t
*dev
;
121 dev
= kcalloc(1, sizeof(*dev
), GFP_KERNEL
);
125 dev
->buf
= kmalloc(bufsize
, GFP_KERNEL
);
126 if (dev
->buf
== NULL
) {
131 dev
->bufsize
= bufsize
;
133 spin_lock_init(&dev
->lock
);
138 void snd_midi_event_free(snd_midi_event_t
*dev
)
149 inline static void reset_encode(snd_midi_event_t
*dev
)
156 void snd_midi_event_reset_encode(snd_midi_event_t
*dev
)
160 spin_lock_irqsave(&dev
->lock
, flags
);
162 spin_unlock_irqrestore(&dev
->lock
, flags
);
165 void snd_midi_event_reset_decode(snd_midi_event_t
*dev
)
169 spin_lock_irqsave(&dev
->lock
, flags
);
171 spin_unlock_irqrestore(&dev
->lock
, flags
);
174 void snd_midi_event_init(snd_midi_event_t
*dev
)
176 snd_midi_event_reset_encode(dev
);
177 snd_midi_event_reset_decode(dev
);
180 void snd_midi_event_no_status(snd_midi_event_t
*dev
, int on
)
182 dev
->nostat
= on
? 1 : 0;
188 int snd_midi_event_resize_buffer(snd_midi_event_t
*dev
, int bufsize
)
190 unsigned char *new_buf
, *old_buf
;
193 if (bufsize
== dev
->bufsize
)
195 new_buf
= kmalloc(bufsize
, GFP_KERNEL
);
198 spin_lock_irqsave(&dev
->lock
, flags
);
201 dev
->bufsize
= bufsize
;
203 spin_unlock_irqrestore(&dev
->lock
, flags
);
209 * read bytes and encode to sequencer event if finished
210 * return the size of encoded bytes
212 long snd_midi_event_encode(snd_midi_event_t
*dev
, unsigned char *buf
, long count
, snd_seq_event_t
*ev
)
217 ev
->type
= SNDRV_SEQ_EVENT_NONE
;
219 while (count
-- > 0) {
220 rc
= snd_midi_event_encode_byte(dev
, *buf
++, ev
);
232 * read one byte and encode to sequencer event:
233 * return 1 if MIDI bytes are encoded to an event
234 * 0 data is not finished
237 int snd_midi_event_encode_byte(snd_midi_event_t
*dev
, int c
, snd_seq_event_t
*ev
)
244 if (c
>= MIDI_CMD_COMMON_CLOCK
) {
245 /* real-time event */
246 ev
->type
= status_event
[ST_SPECIAL
+ c
- 0xf0].event
;
247 ev
->flags
&= ~SNDRV_SEQ_EVENT_LENGTH_MASK
;
248 ev
->flags
|= SNDRV_SEQ_EVENT_LENGTH_FIXED
;
252 spin_lock_irqsave(&dev
->lock
, flags
);
254 /* rest of command */
255 dev
->buf
[dev
->read
++] = c
;
256 if (dev
->type
!= ST_SYSEX
)
263 if ((c
& 0xf0) == 0xf0) /* special events */
264 dev
->type
= (c
& 0x0f) + ST_SPECIAL
;
266 dev
->type
= (c
>> 4) & 0x07;
267 dev
->qlen
= status_event
[dev
->type
].qlen
;
269 /* process this byte as argument */
270 dev
->buf
[dev
->read
++] = c
;
271 dev
->qlen
= status_event
[dev
->type
].qlen
- 1;
274 if (dev
->qlen
== 0) {
275 ev
->type
= status_event
[dev
->type
].event
;
276 ev
->flags
&= ~SNDRV_SEQ_EVENT_LENGTH_MASK
;
277 ev
->flags
|= SNDRV_SEQ_EVENT_LENGTH_FIXED
;
278 if (status_event
[dev
->type
].encode
) /* set data values */
279 status_event
[dev
->type
].encode(dev
, ev
);
281 } else if (dev
->type
== ST_SYSEX
) {
282 if (c
== MIDI_CMD_COMMON_SYSEX_END
||
283 dev
->read
>= dev
->bufsize
) {
284 ev
->flags
&= ~SNDRV_SEQ_EVENT_LENGTH_MASK
;
285 ev
->flags
|= SNDRV_SEQ_EVENT_LENGTH_VARIABLE
;
286 ev
->type
= SNDRV_SEQ_EVENT_SYSEX
;
287 ev
->data
.ext
.len
= dev
->read
;
288 ev
->data
.ext
.ptr
= dev
->buf
;
289 if (c
!= MIDI_CMD_COMMON_SYSEX_END
)
290 dev
->read
= 0; /* continue to parse */
292 reset_encode(dev
); /* all parsed */
297 spin_unlock_irqrestore(&dev
->lock
, flags
);
301 /* encode note event */
302 static void note_event(snd_midi_event_t
*dev
, snd_seq_event_t
*ev
)
304 ev
->data
.note
.channel
= dev
->buf
[0] & 0x0f;
305 ev
->data
.note
.note
= dev
->buf
[1];
306 ev
->data
.note
.velocity
= dev
->buf
[2];
309 /* encode one parameter controls */
310 static void one_param_ctrl_event(snd_midi_event_t
*dev
, snd_seq_event_t
*ev
)
312 ev
->data
.control
.channel
= dev
->buf
[0] & 0x0f;
313 ev
->data
.control
.value
= dev
->buf
[1];
316 /* encode pitch wheel change */
317 static void pitchbend_ctrl_event(snd_midi_event_t
*dev
, snd_seq_event_t
*ev
)
319 ev
->data
.control
.channel
= dev
->buf
[0] & 0x0f;
320 ev
->data
.control
.value
= (int)dev
->buf
[2] * 128 + (int)dev
->buf
[1] - 8192;
323 /* encode midi control change */
324 static void two_param_ctrl_event(snd_midi_event_t
*dev
, snd_seq_event_t
*ev
)
326 ev
->data
.control
.channel
= dev
->buf
[0] & 0x0f;
327 ev
->data
.control
.param
= dev
->buf
[1];
328 ev
->data
.control
.value
= dev
->buf
[2];
331 /* encode one parameter value*/
332 static void one_param_event(snd_midi_event_t
*dev
, snd_seq_event_t
*ev
)
334 ev
->data
.control
.value
= dev
->buf
[1];
337 /* encode song position */
338 static void songpos_event(snd_midi_event_t
*dev
, snd_seq_event_t
*ev
)
340 ev
->data
.control
.value
= (int)dev
->buf
[2] * 128 + (int)dev
->buf
[1];
344 * decode from a sequencer event to midi bytes
345 * return the size of decoded midi events
347 long snd_midi_event_decode(snd_midi_event_t
*dev
, unsigned char *buf
, long count
, snd_seq_event_t
*ev
)
349 unsigned int cmd
, type
;
351 if (ev
->type
== SNDRV_SEQ_EVENT_NONE
)
354 for (type
= 0; type
< ARRAY_SIZE(status_event
); type
++) {
355 if (ev
->type
== status_event
[type
].event
)
358 for (type
= 0; type
< ARRAY_SIZE(extra_event
); type
++) {
359 if (ev
->type
== extra_event
[type
].event
)
360 return extra_event
[type
].decode(dev
, buf
, count
, ev
);
365 if (type
>= ST_SPECIAL
)
366 cmd
= 0xf0 + (type
- ST_SPECIAL
);
368 /* data.note.channel and data.control.channel is identical */
369 cmd
= 0x80 | (type
<< 4) | (ev
->data
.note
.channel
& 0x0f);
372 if (cmd
== MIDI_CMD_COMMON_SYSEX
) {
373 snd_midi_event_reset_decode(dev
);
374 return snd_seq_expand_var_event(ev
, count
, buf
, 1, 0);
377 unsigned char xbuf
[4];
380 spin_lock_irqsave(&dev
->lock
, flags
);
381 if ((cmd
& 0xf0) == 0xf0 || dev
->lastcmd
!= cmd
|| dev
->nostat
) {
383 spin_unlock_irqrestore(&dev
->lock
, flags
);
385 if (status_event
[type
].decode
)
386 status_event
[type
].decode(ev
, xbuf
+ 1);
387 qlen
= status_event
[type
].qlen
+ 1;
389 spin_unlock_irqrestore(&dev
->lock
, flags
);
390 if (status_event
[type
].decode
)
391 status_event
[type
].decode(ev
, xbuf
+ 0);
392 qlen
= status_event
[type
].qlen
;
396 memcpy(buf
, xbuf
, qlen
);
402 /* decode note event */
403 static void note_decode(snd_seq_event_t
*ev
, unsigned char *buf
)
405 buf
[0] = ev
->data
.note
.note
& 0x7f;
406 buf
[1] = ev
->data
.note
.velocity
& 0x7f;
409 /* decode one parameter controls */
410 static void one_param_decode(snd_seq_event_t
*ev
, unsigned char *buf
)
412 buf
[0] = ev
->data
.control
.value
& 0x7f;
415 /* decode pitch wheel change */
416 static void pitchbend_decode(snd_seq_event_t
*ev
, unsigned char *buf
)
418 int value
= ev
->data
.control
.value
+ 8192;
419 buf
[0] = value
& 0x7f;
420 buf
[1] = (value
>> 7) & 0x7f;
423 /* decode midi control change */
424 static void two_param_decode(snd_seq_event_t
*ev
, unsigned char *buf
)
426 buf
[0] = ev
->data
.control
.param
& 0x7f;
427 buf
[1] = ev
->data
.control
.value
& 0x7f;
430 /* decode song position */
431 static void songpos_decode(snd_seq_event_t
*ev
, unsigned char *buf
)
433 buf
[0] = ev
->data
.control
.value
& 0x7f;
434 buf
[1] = (ev
->data
.control
.value
>> 7) & 0x7f;
437 /* decode 14bit control */
438 static int extra_decode_ctrl14(snd_midi_event_t
*dev
, unsigned char *buf
, int count
, snd_seq_event_t
*ev
)
443 cmd
= MIDI_CMD_CONTROL
|(ev
->data
.control
.channel
& 0x0f);
444 if (ev
->data
.control
.param
< 0x20) {
447 if (dev
->nostat
&& count
< 6)
449 if (cmd
!= dev
->lastcmd
|| dev
->nostat
) {
452 buf
[idx
++] = dev
->lastcmd
= cmd
;
454 buf
[idx
++] = ev
->data
.control
.param
;
455 buf
[idx
++] = (ev
->data
.control
.value
>> 7) & 0x7f;
458 buf
[idx
++] = ev
->data
.control
.param
+ 0x20;
459 buf
[idx
++] = ev
->data
.control
.value
& 0x7f;
463 if (cmd
!= dev
->lastcmd
|| dev
->nostat
) {
466 buf
[idx
++] = dev
->lastcmd
= cmd
;
468 buf
[idx
++] = ev
->data
.control
.param
& 0x7f;
469 buf
[idx
++] = ev
->data
.control
.value
& 0x7f;
474 /* decode reg/nonreg param */
475 static int extra_decode_xrpn(snd_midi_event_t
*dev
, unsigned char *buf
, int count
, snd_seq_event_t
*ev
)
479 static char cbytes_nrpn
[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB
,
480 MIDI_CTL_NONREG_PARM_NUM_LSB
,
481 MIDI_CTL_MSB_DATA_ENTRY
,
482 MIDI_CTL_LSB_DATA_ENTRY
};
483 static char cbytes_rpn
[4] = { MIDI_CTL_REGIST_PARM_NUM_MSB
,
484 MIDI_CTL_REGIST_PARM_NUM_LSB
,
485 MIDI_CTL_MSB_DATA_ENTRY
,
486 MIDI_CTL_LSB_DATA_ENTRY
};
487 unsigned char bytes
[4];
492 if (dev
->nostat
&& count
< 12)
494 cmd
= MIDI_CMD_CONTROL
|(ev
->data
.control
.channel
& 0x0f);
495 bytes
[0] = ev
->data
.control
.param
& 0x007f;
496 bytes
[1] = (ev
->data
.control
.param
& 0x3f80) >> 7;
497 bytes
[2] = ev
->data
.control
.value
& 0x007f;
498 bytes
[3] = (ev
->data
.control
.value
& 0x3f80) >> 7;
499 if (cmd
!= dev
->lastcmd
&& !dev
->nostat
) {
502 buf
[idx
++] = dev
->lastcmd
= cmd
;
504 cbytes
= ev
->type
== SNDRV_SEQ_EVENT_NONREGPARAM
? cbytes_nrpn
: cbytes_rpn
;
505 for (i
= 0; i
< 4; i
++) {
507 buf
[idx
++] = dev
->lastcmd
= cmd
;
508 buf
[idx
++] = cbytes
[i
];
509 buf
[idx
++] = bytes
[i
];
518 EXPORT_SYMBOL(snd_midi_event_new
);
519 EXPORT_SYMBOL(snd_midi_event_free
);
520 EXPORT_SYMBOL(snd_midi_event_resize_buffer
);
521 EXPORT_SYMBOL(snd_midi_event_init
);
522 EXPORT_SYMBOL(snd_midi_event_reset_encode
);
523 EXPORT_SYMBOL(snd_midi_event_reset_decode
);
524 EXPORT_SYMBOL(snd_midi_event_no_status
);
525 EXPORT_SYMBOL(snd_midi_event_encode
);
526 EXPORT_SYMBOL(snd_midi_event_encode_byte
);
527 EXPORT_SYMBOL(snd_midi_event_decode
);
529 static int __init
alsa_seq_midi_event_init(void)
534 static void __exit
alsa_seq_midi_event_exit(void)
538 module_init(alsa_seq_midi_event_init
)
539 module_exit(alsa_seq_midi_event_exit
)