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