Home | History | Annotate | Download | only in seq
      1 /**
      2  * \file seq/seq_midi_event.c
      3  * \brief MIDI byte <-> sequencer event coder
      4  * \author Takashi Iwai <tiwai (at) suse.de>
      5  * \author Jaroslav Kysela <perex (at) perex.cz>
      6  * \date 2000-2001
      7  */
      8 
      9 /*
     10  *  MIDI byte <-> sequencer event coder
     11  *
     12  *  Copyright (C) 1998,99,2000 Takashi Iwai <tiwai (at) suse.de>,
     13  *			       Jaroslav Kysela <perex (at) perex.cz>
     14  *
     15  *
     16  *   This library is free software; you can redistribute it and/or modify
     17  *   it under the terms of the GNU Lesser General Public License as
     18  *   published by the Free Software Foundation; either version 2.1 of
     19  *   the License, or (at your option) any later version.
     20  *
     21  *   This program is distributed in the hope that it will be useful,
     22  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     23  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     24  *   GNU Lesser General Public License for more details.
     25  *
     26  *   You should have received a copy of the GNU Lesser General Public
     27  *   License along with this library; if not, write to the Free Software
     28  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
     29  */
     30 
     31 #include <malloc.h>
     32 #include "local.h"
     33 
     34 #ifndef DOC_HIDDEN
     35 
     36 /* midi status */
     37 struct snd_midi_event {
     38 	size_t qlen;	/* queue length */
     39 	size_t read;	/* chars read */
     40 	int type;	/* current event type */
     41 	unsigned char lastcmd;
     42 	unsigned char nostat;
     43 	size_t bufsize;
     44 	unsigned char *buf;	/* input buffer */
     45 };
     46 
     47 
     48 /* event type, index into status_event[] */
     49 /* from 0 to 6 are normal commands (note off, on, etc.) for 0x8?-0xe? */
     50 #define ST_INVALID	7
     51 #define ST_SPECIAL	8
     52 #define ST_SYSEX	ST_SPECIAL
     53 /* from 8 to 15 are events for 0xf0-0xf7 */
     54 
     55 
     56 /* status event types */
     57 typedef void (*event_encode_t)(snd_midi_event_t *dev, snd_seq_event_t *ev);
     58 typedef void (*event_decode_t)(const snd_seq_event_t *ev, unsigned char *buf);
     59 
     60 #endif /* DOC_HIDDEN */
     61 
     62 /*
     63  * prototypes
     64  */
     65 static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
     66 static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
     67 static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
     68 static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
     69 static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
     70 static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
     71 static void note_decode(const snd_seq_event_t *ev, unsigned char *buf);
     72 static void one_param_decode(const snd_seq_event_t *ev, unsigned char *buf);
     73 static void pitchbend_decode(const snd_seq_event_t *ev, unsigned char *buf);
     74 static void two_param_decode(const snd_seq_event_t *ev, unsigned char *buf);
     75 static void songpos_decode(const snd_seq_event_t *ev, unsigned char *buf);
     76 
     77 /*
     78  * event list
     79  */
     80 #ifndef DOC_HIDDEN
     81 static const struct status_event_list_t {
     82 	int event;
     83 	int qlen;
     84 	event_encode_t encode;
     85 	event_decode_t decode;
     86 } status_event[] = {
     87 	/* 0x80 - 0xef */
     88 	{SND_SEQ_EVENT_NOTEOFF,		 2, note_event, note_decode},
     89 	{SND_SEQ_EVENT_NOTEON,		 2, note_event, note_decode},
     90 	{SND_SEQ_EVENT_KEYPRESS,	 2, note_event, note_decode},
     91 	{SND_SEQ_EVENT_CONTROLLER,	 2, two_param_ctrl_event, two_param_decode},
     92 	{SND_SEQ_EVENT_PGMCHANGE,	 1, one_param_ctrl_event, one_param_decode},
     93 	{SND_SEQ_EVENT_CHANPRESS,	 1, one_param_ctrl_event, one_param_decode},
     94 	{SND_SEQ_EVENT_PITCHBEND,	 2, pitchbend_ctrl_event, pitchbend_decode},
     95 	/* invalid */
     96 	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL},
     97 	/* 0xf0 - 0xff */
     98 	{SND_SEQ_EVENT_SYSEX,		 1, NULL, NULL}, /* sysex: 0xf0 */
     99 	{SND_SEQ_EVENT_QFRAME,		 1, one_param_event, one_param_decode}, /* 0xf1 */
    100 	{SND_SEQ_EVENT_SONGPOS,		 2, songpos_event, songpos_decode}, /* 0xf2 */
    101 	{SND_SEQ_EVENT_SONGSEL,		 1, one_param_event, one_param_decode}, /* 0xf3 */
    102 	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf4 */
    103 	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf5 */
    104 	{SND_SEQ_EVENT_TUNE_REQUEST,	 0, NULL, NULL}, /* 0xf6 */
    105 	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf7 */
    106 	{SND_SEQ_EVENT_CLOCK,		 0, NULL, NULL}, /* 0xf8 */
    107 	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf9 */
    108 	{SND_SEQ_EVENT_START,		 0, NULL, NULL}, /* 0xfa */
    109 	{SND_SEQ_EVENT_CONTINUE,	 0, NULL, NULL}, /* 0xfb */
    110 	{SND_SEQ_EVENT_STOP, 		 0, NULL, NULL}, /* 0xfc */
    111 	{SND_SEQ_EVENT_NONE, 		-1, NULL, NULL}, /* 0xfd */
    112 	{SND_SEQ_EVENT_SENSING, 	 0, NULL, NULL}, /* 0xfe */
    113 	{SND_SEQ_EVENT_RESET, 		 0, NULL, NULL}, /* 0xff */
    114 };
    115 
    116 static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int len, const snd_seq_event_t *ev);
    117 static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev);
    118 
    119 static const struct extra_event_list_t {
    120 	int event;
    121 	int (*decode)(snd_midi_event_t *dev, unsigned char *buf, int len, const snd_seq_event_t *ev);
    122 } extra_event[] = {
    123 	{SND_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
    124 	{SND_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn},
    125 	{SND_SEQ_EVENT_REGPARAM, extra_decode_xrpn},
    126 };
    127 
    128 #define numberof(ary)	(sizeof(ary)/sizeof(ary[0]))
    129 #endif /* DOC_HIDDEN */
    130 
    131 /**
    132  * \brief Initialize MIDI event parser
    133  * \param bufsize buffer size for MIDI message
    134  * \param rdev allocated MIDI event parser
    135  * \return 0 on success otherwise a negative error code
    136  *
    137  * Allocates and initializes MIDI event parser.
    138  */
    139 int snd_midi_event_new(size_t bufsize, snd_midi_event_t **rdev)
    140 {
    141 	snd_midi_event_t *dev;
    142 
    143 	*rdev = NULL;
    144 	dev = (snd_midi_event_t *)calloc(1, sizeof(snd_midi_event_t));
    145 	if (dev == NULL)
    146 		return -ENOMEM;
    147 	if (bufsize > 0) {
    148 		dev->buf = malloc(bufsize);
    149 		if (dev->buf == NULL) {
    150 			free(dev);
    151 			return -ENOMEM;
    152 		}
    153 	}
    154 	dev->bufsize = bufsize;
    155 	dev->lastcmd = 0xff;
    156 	dev->type = ST_INVALID;
    157 	*rdev = dev;
    158 	return 0;
    159 }
    160 
    161 /**
    162  * \brief Free MIDI event parser
    163  * \param dev MIDI event parser
    164  * \return 0 on success otherwise a negative error code
    165  *
    166  * Frees MIDI event parser.
    167  */
    168 void snd_midi_event_free(snd_midi_event_t *dev)
    169 {
    170 	if (dev != NULL) {
    171 		free(dev->buf);
    172 		free(dev);
    173 	}
    174 }
    175 
    176 /**
    177  * \brief Enable/disable MIDI command merging
    178  * \param dev MIDI event parser
    179  * \param on 0 - enable MIDI command merging, 1 - always pass the command
    180  *
    181  * Enable/disable MIDI command merging
    182  */
    183 void snd_midi_event_no_status(snd_midi_event_t *dev, int on)
    184 {
    185 	dev->nostat = on ? 1 : 0;
    186 }
    187 
    188 /*
    189  * initialize record
    190  */
    191 inline static void reset_encode(snd_midi_event_t *dev)
    192 {
    193 	dev->read = 0;
    194 	dev->qlen = 0;
    195 	dev->type = ST_INVALID;
    196 }
    197 
    198 /**
    199  * \brief Reset MIDI encode parser
    200  * \param dev MIDI event parser
    201  * \return 0 on success otherwise a negative error code
    202  *
    203  * Resets MIDI encode parser
    204  */
    205 void snd_midi_event_reset_encode(snd_midi_event_t *dev)
    206 {
    207 	reset_encode(dev);
    208 }
    209 
    210 /**
    211  * \brief Reset MIDI decode parser
    212  * \param dev MIDI event parser
    213  * \return 0 on success otherwise a negative error code
    214  *
    215  * Resets MIDI decode parser
    216  */
    217 void snd_midi_event_reset_decode(snd_midi_event_t *dev)
    218 {
    219 	dev->lastcmd = 0xff;
    220 }
    221 
    222 /**
    223  * \brief Initializes MIDI parsers
    224  * \param dev MIDI event parser
    225  * \return 0 on success otherwise a negative error code
    226  *
    227  * Initializes MIDI parsers (both encode and decode)
    228  */
    229 void snd_midi_event_init(snd_midi_event_t *dev)
    230 {
    231 	snd_midi_event_reset_encode(dev);
    232 	snd_midi_event_reset_decode(dev);
    233 }
    234 
    235 /**
    236  * \brief Resize MIDI message (event) buffer
    237  * \param dev MIDI event parser
    238  * \param bufsize new requested buffer size
    239  * \return 0 on success otherwise a negative error code
    240  *
    241  * Resizes MIDI message (event) buffer.
    242  */
    243 int snd_midi_event_resize_buffer(snd_midi_event_t *dev, size_t bufsize)
    244 {
    245 	unsigned char *new_buf, *old_buf;
    246 
    247 	if (bufsize == dev->bufsize)
    248 		return 0;
    249 	new_buf = malloc(bufsize);
    250 	if (new_buf == NULL)
    251 		return -ENOMEM;
    252 	old_buf = dev->buf;
    253 	dev->buf = new_buf;
    254 	dev->bufsize = bufsize;
    255 	reset_encode(dev);
    256 	free(old_buf);
    257 	return 0;
    258 }
    259 
    260 /**
    261  * \brief Read bytes and encode to sequencer event if finished
    262  * \param dev MIDI event parser
    263  * \param buf MIDI byte stream
    264  * \param count count of bytes of MIDI byte stream to encode
    265  * \param ev Result - sequencer event
    266  * \return count of encoded bytes otherwise a negative error code
    267  *
    268  * Read bytes and encode to sequencer event if finished.
    269  * If complete sequencer event is available, ev->type is not
    270  * equal to #SND_SEQ_EVENT_NONE.
    271  */
    272 long snd_midi_event_encode(snd_midi_event_t *dev, const unsigned char *buf, long count, snd_seq_event_t *ev)
    273 {
    274 	long result = 0;
    275 	int rc;
    276 
    277 	ev->type = SND_SEQ_EVENT_NONE;
    278 
    279 	while (count-- > 0) {
    280 		rc = snd_midi_event_encode_byte(dev, *buf++, ev);
    281 		result++;
    282 		if (rc < 0)
    283 			return rc;
    284 		else if (rc > 0)
    285 			return result;
    286 	}
    287 
    288 	return result;
    289 }
    290 
    291 /**
    292  * \brief Read one byte and encode to sequencer event if finished
    293  * \param dev MIDI event parser
    294  * \param c a byte of MIDI stream
    295  * \param ev Result - sequencer event
    296  * \return 1 - sequencer event is completed, 0 - next byte is required for completion, otherwise a negative error code
    297  *
    298  * Read byte and encode to sequencer event if finished.
    299  */
    300 int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev)
    301 {
    302 	int rc = 0;
    303 
    304 	c &= 0xff;
    305 
    306 	if (c >= MIDI_CMD_COMMON_CLOCK) {
    307 		/* real-time event */
    308 		ev->type = status_event[ST_SPECIAL + c - 0xf0].event;
    309 		ev->flags &= ~SND_SEQ_EVENT_LENGTH_MASK;
    310 		ev->flags |= SND_SEQ_EVENT_LENGTH_FIXED;
    311 		return ev->type != SND_SEQ_EVENT_NONE;
    312 	}
    313 
    314 	if ((c & 0x80) &&
    315 	    (c != MIDI_CMD_COMMON_SYSEX_END || dev->type != ST_SYSEX)) {
    316 		/* new command */
    317 		dev->buf[0] = c;
    318 		if ((c & 0xf0) == 0xf0) /* system message */
    319 			dev->type = (c & 0x0f) + ST_SPECIAL;
    320 		else
    321 			dev->type = (c >> 4) & 0x07;
    322 		dev->read = 1;
    323 		dev->qlen = status_event[dev->type].qlen;
    324 	} else {
    325 		if (dev->qlen > 0) {
    326 			/* rest of command */
    327 			dev->buf[dev->read++] = c;
    328 			if (dev->type != ST_SYSEX)
    329 				dev->qlen--;
    330 		} else {
    331 			/* running status */
    332 			dev->buf[1] = c;
    333 			dev->qlen = status_event[dev->type].qlen - 1;
    334 			dev->read = 2;
    335 		}
    336 	}
    337 	if (dev->qlen == 0) {
    338 		ev->type = status_event[dev->type].event;
    339 		ev->flags &= ~SND_SEQ_EVENT_LENGTH_MASK;
    340 		ev->flags |= SND_SEQ_EVENT_LENGTH_FIXED;
    341 		if (status_event[dev->type].encode) /* set data values */
    342 			status_event[dev->type].encode(dev, ev);
    343 		if (dev->type >= ST_SPECIAL)
    344 			dev->type = ST_INVALID;
    345 		rc = 1;
    346 	} else 	if (dev->type == ST_SYSEX) {
    347 		if (c == MIDI_CMD_COMMON_SYSEX_END ||
    348 		    dev->read >= dev->bufsize) {
    349 			ev->flags &= ~SND_SEQ_EVENT_LENGTH_MASK;
    350 			ev->flags |= SND_SEQ_EVENT_LENGTH_VARIABLE;
    351 			ev->type = SND_SEQ_EVENT_SYSEX;
    352 			ev->data.ext.len = dev->read;
    353 			ev->data.ext.ptr = dev->buf;
    354 			if (c != MIDI_CMD_COMMON_SYSEX_END)
    355 				dev->read = 0; /* continue to parse */
    356 			else
    357 				reset_encode(dev); /* all parsed */
    358 			rc = 1;
    359 		}
    360 	}
    361 
    362 	return rc;
    363 }
    364 
    365 /* encode note event */
    366 static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
    367 {
    368 	ev->data.note.channel = dev->buf[0] & 0x0f;
    369 	ev->data.note.note = dev->buf[1];
    370 	ev->data.note.velocity = dev->buf[2];
    371 }
    372 
    373 /* encode one parameter controls */
    374 static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
    375 {
    376 	ev->data.control.channel = dev->buf[0] & 0x0f;
    377 	ev->data.control.value = dev->buf[1];
    378 }
    379 
    380 /* encode pitch wheel change */
    381 static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
    382 {
    383 	ev->data.control.channel = dev->buf[0] & 0x0f;
    384 	ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192;
    385 }
    386 
    387 /* encode midi control change */
    388 static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
    389 {
    390 	ev->data.control.channel = dev->buf[0] & 0x0f;
    391 	ev->data.control.param = dev->buf[1];
    392 	ev->data.control.value = dev->buf[2];
    393 }
    394 
    395 /* encode one parameter value*/
    396 static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
    397 {
    398 	ev->data.control.value = dev->buf[1];
    399 }
    400 
    401 /* encode song position */
    402 static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
    403 {
    404 	ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1];
    405 }
    406 
    407 /**
    408  * \brief Decode sequencer event to MIDI byte stream
    409  * \param dev MIDI event parser
    410  * \param buf Result - MIDI byte stream
    411  * \param count Available bytes in MIDI byte stream
    412  * \param ev Event to decode
    413  * \return count of decoded bytes otherwise a negative error code
    414  *
    415  * Decode sequencer event to MIDI byte stream.
    416  */
    417 long snd_midi_event_decode(snd_midi_event_t *dev, unsigned char *buf, long count, const snd_seq_event_t *ev)
    418 {
    419 	int cmd;
    420 	long qlen;
    421 	unsigned int type;
    422 
    423 	if (ev->type == SND_SEQ_EVENT_NONE)
    424 		return -ENOENT;
    425 
    426 	for (type = 0; type < numberof(status_event); type++) {
    427 		if (ev->type == status_event[type].event)
    428 			goto __found;
    429 	}
    430 	for (type = 0; type < numberof(extra_event); type++) {
    431 		if (ev->type == extra_event[type].event)
    432 			return extra_event[type].decode(dev, buf, count, ev);
    433 	}
    434 	return -ENOENT;
    435 
    436       __found:
    437 	if (type >= ST_SPECIAL)
    438 		cmd = 0xf0 + (type - ST_SPECIAL);
    439 	else
    440 		/* data.note.channel and data.control.channel is identical */
    441 		cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f);
    442 
    443 
    444 	if (cmd == MIDI_CMD_COMMON_SYSEX) {
    445 		qlen = ev->data.ext.len;
    446 		if (count < qlen)
    447 			return -ENOMEM;
    448 		switch (ev->flags & SND_SEQ_EVENT_LENGTH_MASK) {
    449 		case SND_SEQ_EVENT_LENGTH_FIXED:
    450 			return -EINVAL;	/* invalid event */
    451 		}
    452 		memcpy(buf, ev->data.ext.ptr, qlen);
    453 		return qlen;
    454 	} else {
    455 		unsigned char xbuf[4];
    456 
    457 		if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) {
    458 			dev->lastcmd = cmd;
    459 			xbuf[0] = cmd;
    460 			if (status_event[type].decode)
    461 				status_event[type].decode(ev, xbuf + 1);
    462 			qlen = status_event[type].qlen + 1;
    463 		} else {
    464 			if (status_event[type].decode)
    465 				status_event[type].decode(ev, xbuf + 0);
    466 			qlen = status_event[type].qlen;
    467 		}
    468 		if (count < qlen)
    469 			return -ENOMEM;
    470 		memcpy(buf, xbuf, qlen);
    471 		return qlen;
    472 	}
    473 }
    474 
    475 
    476 /* decode note event */
    477 static void note_decode(const snd_seq_event_t *ev, unsigned char *buf)
    478 {
    479 	buf[0] = ev->data.note.note & 0x7f;
    480 	buf[1] = ev->data.note.velocity & 0x7f;
    481 }
    482 
    483 /* decode one parameter controls */
    484 static void one_param_decode(const snd_seq_event_t *ev, unsigned char *buf)
    485 {
    486 	buf[0] = ev->data.control.value & 0x7f;
    487 }
    488 
    489 /* decode pitch wheel change */
    490 static void pitchbend_decode(const snd_seq_event_t *ev, unsigned char *buf)
    491 {
    492 	int value = ev->data.control.value + 8192;
    493 	buf[0] = value & 0x7f;
    494 	buf[1] = (value >> 7) & 0x7f;
    495 }
    496 
    497 /* decode midi control change */
    498 static void two_param_decode(const snd_seq_event_t *ev, unsigned char *buf)
    499 {
    500 	buf[0] = ev->data.control.param & 0x7f;
    501 	buf[1] = ev->data.control.value & 0x7f;
    502 }
    503 
    504 /* decode song position */
    505 static void songpos_decode(const snd_seq_event_t *ev, unsigned char *buf)
    506 {
    507 	buf[0] = ev->data.control.value & 0x7f;
    508 	buf[1] = (ev->data.control.value >> 7) & 0x7f;
    509 }
    510 
    511 /* decode 14bit control */
    512 static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev)
    513 {
    514 	unsigned char cmd;
    515 	int idx = 0;
    516 
    517 	cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
    518 	if (ev->data.control.param < 32) {
    519 		if (count < 4)
    520 			return -ENOMEM;
    521 		if (dev->nostat && count < 6)
    522 			return -ENOMEM;
    523 		if (cmd != dev->lastcmd || dev->nostat) {
    524 			if (count < 5)
    525 				return -ENOMEM;
    526 			buf[idx++] = dev->lastcmd = cmd;
    527 		}
    528 		buf[idx++] = ev->data.control.param;
    529 		buf[idx++] = (ev->data.control.value >> 7) & 0x7f;
    530 		if (dev->nostat)
    531 			buf[idx++] = cmd;
    532 		buf[idx++] = ev->data.control.param + 32;
    533 		buf[idx++] = ev->data.control.value & 0x7f;
    534 	} else {
    535 		if (count < 2)
    536 			return -ENOMEM;
    537 		if (cmd != dev->lastcmd || dev->nostat) {
    538 			if (count < 3)
    539 				return -ENOMEM;
    540 			buf[idx++] = dev->lastcmd = cmd;
    541 		}
    542 		buf[idx++] = ev->data.control.param & 0x7f;
    543 		buf[idx++] = ev->data.control.value & 0x7f;
    544 	}
    545 	return idx;
    546 }
    547 
    548 /* decode reg/nonreg param */
    549 static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev)
    550 {
    551 	unsigned char cmd;
    552 	const char *cbytes;
    553 	static const char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB,
    554 				       MIDI_CTL_NONREG_PARM_NUM_LSB,
    555 				       MIDI_CTL_MSB_DATA_ENTRY,
    556 				       MIDI_CTL_LSB_DATA_ENTRY };
    557 	static const char cbytes_rpn[4] =  { MIDI_CTL_REGIST_PARM_NUM_MSB,
    558 				       MIDI_CTL_REGIST_PARM_NUM_LSB,
    559 				       MIDI_CTL_MSB_DATA_ENTRY,
    560 				       MIDI_CTL_LSB_DATA_ENTRY };
    561 	unsigned char bytes[4];
    562 	int idx = 0, i;
    563 
    564 	if (count < 8)
    565 		return -ENOMEM;
    566 	if (dev->nostat && count < 12)
    567 		return -ENOMEM;
    568 	cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
    569 	bytes[0] = ev->data.control.param & 0x007f;
    570 	bytes[1] = (ev->data.control.param & 0x3f80) >> 7;
    571 	bytes[2] = ev->data.control.value & 0x007f;
    572 	bytes[3] = (ev->data.control.value & 0x3f80) >> 7;
    573 	if (cmd != dev->lastcmd && !dev->nostat) {
    574 		if (count < 9)
    575 			return -ENOMEM;
    576 		buf[idx++] = dev->lastcmd = cmd;
    577 	}
    578 	cbytes = ev->type == SND_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn;
    579 	for (i = 0; i < 4; i++) {
    580 		if (dev->nostat)
    581 			buf[idx++] = dev->lastcmd = cmd;
    582 		buf[idx++] = cbytes[i];
    583 		buf[idx++] = bytes[i];
    584 	}
    585 	return idx;
    586 }
    587