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 <linux/module.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@perex.cz>");
32 MODULE_DESCRIPTION("MIDI byte <-> sequencer event coder");
33 MODULE_LICENSE("GPL");
35 /* event type, index into status_event[] */
36 /* from 0 to 6 are normal commands (note off, on, etc.) for 0x9?-0xe? */
39 #define ST_SYSEX ST_SPECIAL
40 /* from 8 to 15 are events for 0xf0-0xf7 */
46 static void note_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
);
47 static void one_param_ctrl_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
);
48 static void pitchbend_ctrl_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
);
49 static void two_param_ctrl_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
);
50 static void one_param_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
);
51 static void songpos_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
);
52 static void note_decode(struct snd_seq_event
*ev
, unsigned char *buf
);
53 static void one_param_decode(struct snd_seq_event
*ev
, unsigned char *buf
);
54 static void pitchbend_decode(struct snd_seq_event
*ev
, unsigned char *buf
);
55 static void two_param_decode(struct snd_seq_event
*ev
, unsigned char *buf
);
56 static void songpos_decode(struct snd_seq_event
*ev
, unsigned char *buf
);
61 static struct status_event_list
{
64 void (*encode
)(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
);
65 void (*decode
)(struct snd_seq_event
*ev
, unsigned char *buf
);
68 {SNDRV_SEQ_EVENT_NOTEOFF
, 2, note_event
, note_decode
},
69 {SNDRV_SEQ_EVENT_NOTEON
, 2, note_event
, note_decode
},
70 {SNDRV_SEQ_EVENT_KEYPRESS
, 2, note_event
, note_decode
},
71 {SNDRV_SEQ_EVENT_CONTROLLER
, 2, two_param_ctrl_event
, two_param_decode
},
72 {SNDRV_SEQ_EVENT_PGMCHANGE
, 1, one_param_ctrl_event
, one_param_decode
},
73 {SNDRV_SEQ_EVENT_CHANPRESS
, 1, one_param_ctrl_event
, one_param_decode
},
74 {SNDRV_SEQ_EVENT_PITCHBEND
, 2, pitchbend_ctrl_event
, pitchbend_decode
},
76 {SNDRV_SEQ_EVENT_NONE
, -1, NULL
, NULL
},
78 {SNDRV_SEQ_EVENT_SYSEX
, 1, NULL
, NULL
}, /* sysex: 0xf0 */
79 {SNDRV_SEQ_EVENT_QFRAME
, 1, one_param_event
, one_param_decode
}, /* 0xf1 */
80 {SNDRV_SEQ_EVENT_SONGPOS
, 2, songpos_event
, songpos_decode
}, /* 0xf2 */
81 {SNDRV_SEQ_EVENT_SONGSEL
, 1, one_param_event
, one_param_decode
}, /* 0xf3 */
82 {SNDRV_SEQ_EVENT_NONE
, -1, NULL
, NULL
}, /* 0xf4 */
83 {SNDRV_SEQ_EVENT_NONE
, -1, NULL
, NULL
}, /* 0xf5 */
84 {SNDRV_SEQ_EVENT_TUNE_REQUEST
, 0, NULL
, NULL
}, /* 0xf6 */
85 {SNDRV_SEQ_EVENT_NONE
, -1, NULL
, NULL
}, /* 0xf7 */
86 {SNDRV_SEQ_EVENT_CLOCK
, 0, NULL
, NULL
}, /* 0xf8 */
87 {SNDRV_SEQ_EVENT_NONE
, -1, NULL
, NULL
}, /* 0xf9 */
88 {SNDRV_SEQ_EVENT_START
, 0, NULL
, NULL
}, /* 0xfa */
89 {SNDRV_SEQ_EVENT_CONTINUE
, 0, NULL
, NULL
}, /* 0xfb */
90 {SNDRV_SEQ_EVENT_STOP
, 0, NULL
, NULL
}, /* 0xfc */
91 {SNDRV_SEQ_EVENT_NONE
, -1, NULL
, NULL
}, /* 0xfd */
92 {SNDRV_SEQ_EVENT_SENSING
, 0, NULL
, NULL
}, /* 0xfe */
93 {SNDRV_SEQ_EVENT_RESET
, 0, NULL
, NULL
}, /* 0xff */
96 static int extra_decode_ctrl14(struct snd_midi_event
*dev
, unsigned char *buf
, int len
,
97 struct snd_seq_event
*ev
);
98 static int extra_decode_xrpn(struct snd_midi_event
*dev
, unsigned char *buf
, int count
,
99 struct snd_seq_event
*ev
);
101 static struct extra_event_list
{
103 int (*decode
)(struct snd_midi_event
*dev
, unsigned char *buf
, int len
,
104 struct snd_seq_event
*ev
);
106 {SNDRV_SEQ_EVENT_CONTROL14
, extra_decode_ctrl14
},
107 {SNDRV_SEQ_EVENT_NONREGPARAM
, extra_decode_xrpn
},
108 {SNDRV_SEQ_EVENT_REGPARAM
, extra_decode_xrpn
},
115 int snd_midi_event_new(int bufsize
, struct snd_midi_event
**rdev
)
117 struct snd_midi_event
*dev
;
120 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
124 dev
->buf
= kmalloc(bufsize
, GFP_KERNEL
);
125 if (dev
->buf
== NULL
) {
130 dev
->bufsize
= bufsize
;
132 dev
->type
= ST_INVALID
;
133 spin_lock_init(&dev
->lock
);
137 EXPORT_SYMBOL(snd_midi_event_new
);
139 void snd_midi_event_free(struct snd_midi_event
*dev
)
146 EXPORT_SYMBOL(snd_midi_event_free
);
151 static inline void reset_encode(struct snd_midi_event
*dev
)
155 dev
->type
= ST_INVALID
;
158 void snd_midi_event_reset_encode(struct snd_midi_event
*dev
)
162 spin_lock_irqsave(&dev
->lock
, flags
);
164 spin_unlock_irqrestore(&dev
->lock
, flags
);
166 EXPORT_SYMBOL(snd_midi_event_reset_encode
);
168 void snd_midi_event_reset_decode(struct snd_midi_event
*dev
)
172 spin_lock_irqsave(&dev
->lock
, flags
);
174 spin_unlock_irqrestore(&dev
->lock
, flags
);
176 EXPORT_SYMBOL(snd_midi_event_reset_decode
);
179 void snd_midi_event_init(struct snd_midi_event
*dev
)
181 snd_midi_event_reset_encode(dev
);
182 snd_midi_event_reset_decode(dev
);
186 void snd_midi_event_no_status(struct snd_midi_event
*dev
, int on
)
188 dev
->nostat
= on
? 1 : 0;
190 EXPORT_SYMBOL(snd_midi_event_no_status
);
196 int snd_midi_event_resize_buffer(struct snd_midi_event
*dev
, int bufsize
)
198 unsigned char *new_buf
, *old_buf
;
201 if (bufsize
== dev
->bufsize
)
203 new_buf
= kmalloc(bufsize
, GFP_KERNEL
);
206 spin_lock_irqsave(&dev
->lock
, flags
);
209 dev
->bufsize
= bufsize
;
211 spin_unlock_irqrestore(&dev
->lock
, flags
);
218 * read bytes and encode to sequencer event if finished
219 * return the size of encoded bytes
221 long snd_midi_event_encode(struct snd_midi_event
*dev
, unsigned char *buf
, long count
,
222 struct snd_seq_event
*ev
)
227 ev
->type
= SNDRV_SEQ_EVENT_NONE
;
229 while (count
-- > 0) {
230 rc
= snd_midi_event_encode_byte(dev
, *buf
++, ev
);
240 EXPORT_SYMBOL(snd_midi_event_encode
);
243 * read one byte and encode to sequencer event:
244 * return 1 if MIDI bytes are encoded to an event
245 * 0 data is not finished
248 int snd_midi_event_encode_byte(struct snd_midi_event
*dev
, int c
,
249 struct snd_seq_event
*ev
)
256 if (c
>= MIDI_CMD_COMMON_CLOCK
) {
257 /* real-time event */
258 ev
->type
= status_event
[ST_SPECIAL
+ c
- 0xf0].event
;
259 ev
->flags
&= ~SNDRV_SEQ_EVENT_LENGTH_MASK
;
260 ev
->flags
|= SNDRV_SEQ_EVENT_LENGTH_FIXED
;
261 return ev
->type
!= SNDRV_SEQ_EVENT_NONE
;
264 spin_lock_irqsave(&dev
->lock
, flags
);
266 (c
!= MIDI_CMD_COMMON_SYSEX_END
|| dev
->type
!= ST_SYSEX
)) {
269 if ((c
& 0xf0) == 0xf0) /* system messages */
270 dev
->type
= (c
& 0x0f) + ST_SPECIAL
;
272 dev
->type
= (c
>> 4) & 0x07;
274 dev
->qlen
= status_event
[dev
->type
].qlen
;
277 /* rest of command */
278 dev
->buf
[dev
->read
++] = c
;
279 if (dev
->type
!= ST_SYSEX
)
284 dev
->qlen
= status_event
[dev
->type
].qlen
- 1;
288 if (dev
->qlen
== 0) {
289 ev
->type
= status_event
[dev
->type
].event
;
290 ev
->flags
&= ~SNDRV_SEQ_EVENT_LENGTH_MASK
;
291 ev
->flags
|= SNDRV_SEQ_EVENT_LENGTH_FIXED
;
292 if (status_event
[dev
->type
].encode
) /* set data values */
293 status_event
[dev
->type
].encode(dev
, ev
);
294 if (dev
->type
>= ST_SPECIAL
)
295 dev
->type
= ST_INVALID
;
297 } else if (dev
->type
== ST_SYSEX
) {
298 if (c
== MIDI_CMD_COMMON_SYSEX_END
||
299 dev
->read
>= dev
->bufsize
) {
300 ev
->flags
&= ~SNDRV_SEQ_EVENT_LENGTH_MASK
;
301 ev
->flags
|= SNDRV_SEQ_EVENT_LENGTH_VARIABLE
;
302 ev
->type
= SNDRV_SEQ_EVENT_SYSEX
;
303 ev
->data
.ext
.len
= dev
->read
;
304 ev
->data
.ext
.ptr
= dev
->buf
;
305 if (c
!= MIDI_CMD_COMMON_SYSEX_END
)
306 dev
->read
= 0; /* continue to parse */
308 reset_encode(dev
); /* all parsed */
313 spin_unlock_irqrestore(&dev
->lock
, flags
);
316 EXPORT_SYMBOL(snd_midi_event_encode_byte
);
318 /* encode note event */
319 static void note_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
)
321 ev
->data
.note
.channel
= dev
->buf
[0] & 0x0f;
322 ev
->data
.note
.note
= dev
->buf
[1];
323 ev
->data
.note
.velocity
= dev
->buf
[2];
326 /* encode one parameter controls */
327 static void one_param_ctrl_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
)
329 ev
->data
.control
.channel
= dev
->buf
[0] & 0x0f;
330 ev
->data
.control
.value
= dev
->buf
[1];
333 /* encode pitch wheel change */
334 static void pitchbend_ctrl_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
)
336 ev
->data
.control
.channel
= dev
->buf
[0] & 0x0f;
337 ev
->data
.control
.value
= (int)dev
->buf
[2] * 128 + (int)dev
->buf
[1] - 8192;
340 /* encode midi control change */
341 static void two_param_ctrl_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
)
343 ev
->data
.control
.channel
= dev
->buf
[0] & 0x0f;
344 ev
->data
.control
.param
= dev
->buf
[1];
345 ev
->data
.control
.value
= dev
->buf
[2];
348 /* encode one parameter value*/
349 static void one_param_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
)
351 ev
->data
.control
.value
= dev
->buf
[1];
354 /* encode song position */
355 static void songpos_event(struct snd_midi_event
*dev
, struct snd_seq_event
*ev
)
357 ev
->data
.control
.value
= (int)dev
->buf
[2] * 128 + (int)dev
->buf
[1];
361 * decode from a sequencer event to midi bytes
362 * return the size of decoded midi events
364 long snd_midi_event_decode(struct snd_midi_event
*dev
, unsigned char *buf
, long count
,
365 struct snd_seq_event
*ev
)
367 unsigned int cmd
, type
;
369 if (ev
->type
== SNDRV_SEQ_EVENT_NONE
)
372 for (type
= 0; type
< ARRAY_SIZE(status_event
); type
++) {
373 if (ev
->type
== status_event
[type
].event
)
376 for (type
= 0; type
< ARRAY_SIZE(extra_event
); type
++) {
377 if (ev
->type
== extra_event
[type
].event
)
378 return extra_event
[type
].decode(dev
, buf
, count
, ev
);
383 if (type
>= ST_SPECIAL
)
384 cmd
= 0xf0 + (type
- ST_SPECIAL
);
386 /* data.note.channel and data.control.channel is identical */
387 cmd
= 0x80 | (type
<< 4) | (ev
->data
.note
.channel
& 0x0f);
390 if (cmd
== MIDI_CMD_COMMON_SYSEX
) {
391 snd_midi_event_reset_decode(dev
);
392 return snd_seq_expand_var_event(ev
, count
, buf
, 1, 0);
395 unsigned char xbuf
[4];
398 spin_lock_irqsave(&dev
->lock
, flags
);
399 if ((cmd
& 0xf0) == 0xf0 || dev
->lastcmd
!= cmd
|| dev
->nostat
) {
401 spin_unlock_irqrestore(&dev
->lock
, flags
);
403 if (status_event
[type
].decode
)
404 status_event
[type
].decode(ev
, xbuf
+ 1);
405 qlen
= status_event
[type
].qlen
+ 1;
407 spin_unlock_irqrestore(&dev
->lock
, flags
);
408 if (status_event
[type
].decode
)
409 status_event
[type
].decode(ev
, xbuf
+ 0);
410 qlen
= status_event
[type
].qlen
;
414 memcpy(buf
, xbuf
, qlen
);
418 EXPORT_SYMBOL(snd_midi_event_decode
);
421 /* decode note event */
422 static void note_decode(struct snd_seq_event
*ev
, unsigned char *buf
)
424 buf
[0] = ev
->data
.note
.note
& 0x7f;
425 buf
[1] = ev
->data
.note
.velocity
& 0x7f;
428 /* decode one parameter controls */
429 static void one_param_decode(struct snd_seq_event
*ev
, unsigned char *buf
)
431 buf
[0] = ev
->data
.control
.value
& 0x7f;
434 /* decode pitch wheel change */
435 static void pitchbend_decode(struct snd_seq_event
*ev
, unsigned char *buf
)
437 int value
= ev
->data
.control
.value
+ 8192;
438 buf
[0] = value
& 0x7f;
439 buf
[1] = (value
>> 7) & 0x7f;
442 /* decode midi control change */
443 static void two_param_decode(struct snd_seq_event
*ev
, unsigned char *buf
)
445 buf
[0] = ev
->data
.control
.param
& 0x7f;
446 buf
[1] = ev
->data
.control
.value
& 0x7f;
449 /* decode song position */
450 static void songpos_decode(struct snd_seq_event
*ev
, unsigned char *buf
)
452 buf
[0] = ev
->data
.control
.value
& 0x7f;
453 buf
[1] = (ev
->data
.control
.value
>> 7) & 0x7f;
456 /* decode 14bit control */
457 static int extra_decode_ctrl14(struct snd_midi_event
*dev
, unsigned char *buf
,
458 int count
, struct snd_seq_event
*ev
)
463 cmd
= MIDI_CMD_CONTROL
|(ev
->data
.control
.channel
& 0x0f);
464 if (ev
->data
.control
.param
< 0x20) {
467 if (dev
->nostat
&& count
< 6)
469 if (cmd
!= dev
->lastcmd
|| dev
->nostat
) {
472 buf
[idx
++] = dev
->lastcmd
= cmd
;
474 buf
[idx
++] = ev
->data
.control
.param
;
475 buf
[idx
++] = (ev
->data
.control
.value
>> 7) & 0x7f;
478 buf
[idx
++] = ev
->data
.control
.param
+ 0x20;
479 buf
[idx
++] = ev
->data
.control
.value
& 0x7f;
483 if (cmd
!= dev
->lastcmd
|| dev
->nostat
) {
486 buf
[idx
++] = dev
->lastcmd
= cmd
;
488 buf
[idx
++] = ev
->data
.control
.param
& 0x7f;
489 buf
[idx
++] = ev
->data
.control
.value
& 0x7f;
494 /* decode reg/nonreg param */
495 static int extra_decode_xrpn(struct snd_midi_event
*dev
, unsigned char *buf
,
496 int count
, struct snd_seq_event
*ev
)
500 static char cbytes_nrpn
[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB
,
501 MIDI_CTL_NONREG_PARM_NUM_LSB
,
502 MIDI_CTL_MSB_DATA_ENTRY
,
503 MIDI_CTL_LSB_DATA_ENTRY
};
504 static char cbytes_rpn
[4] = { MIDI_CTL_REGIST_PARM_NUM_MSB
,
505 MIDI_CTL_REGIST_PARM_NUM_LSB
,
506 MIDI_CTL_MSB_DATA_ENTRY
,
507 MIDI_CTL_LSB_DATA_ENTRY
};
508 unsigned char bytes
[4];
513 if (dev
->nostat
&& count
< 12)
515 cmd
= MIDI_CMD_CONTROL
|(ev
->data
.control
.channel
& 0x0f);
516 bytes
[0] = (ev
->data
.control
.param
& 0x3f80) >> 7;
517 bytes
[1] = ev
->data
.control
.param
& 0x007f;
518 bytes
[2] = (ev
->data
.control
.value
& 0x3f80) >> 7;
519 bytes
[3] = ev
->data
.control
.value
& 0x007f;
520 if (cmd
!= dev
->lastcmd
&& !dev
->nostat
) {
523 buf
[idx
++] = dev
->lastcmd
= cmd
;
525 cbytes
= ev
->type
== SNDRV_SEQ_EVENT_NONREGPARAM
? cbytes_nrpn
: cbytes_rpn
;
526 for (i
= 0; i
< 4; i
++) {
528 buf
[idx
++] = dev
->lastcmd
= cmd
;
529 buf
[idx
++] = cbytes
[i
];
530 buf
[idx
++] = bytes
[i
];
535 static int __init
alsa_seq_midi_event_init(void)
540 static void __exit
alsa_seq_midi_event_exit(void)
544 module_init(alsa_seq_midi_event_init
)
545 module_exit(alsa_seq_midi_event_exit
)