xref: /openbmc/linux/sound/core/seq/seq_midi_event.c (revision 31527da5)
1 /*
2  *  MIDI byte <-> sequencer event coder
3  *
4  *  Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>,
5  *                        Jaroslav Kysela <perex@perex.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 <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>
30 
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");
34 
35 /* event type, index into status_event[] */
36 /* from 0 to 6 are normal commands (note off, on, etc.) for 0x9?-0xe? */
37 #define ST_INVALID	7
38 #define ST_SPECIAL	8
39 #define ST_SYSEX	ST_SPECIAL
40 /* from 8 to 15 are events for 0xf0-0xf7 */
41 
42 
43 /*
44  * prototypes
45  */
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);
57 
58 /*
59  * event list
60  */
61 static struct status_event_list {
62 	int event;
63 	int qlen;
64 	void (*encode)(struct snd_midi_event *dev, struct snd_seq_event *ev);
65 	void (*decode)(struct snd_seq_event *ev, unsigned char *buf);
66 } status_event[] = {
67 	/* 0x80 - 0xef */
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},
75 	/* invalid */
76 	{SNDRV_SEQ_EVENT_NONE,		-1, NULL, NULL},
77 	/* 0xf0 - 0xff */
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 */
94 };
95 
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);
100 
101 static struct extra_event_list {
102 	int event;
103 	int (*decode)(struct snd_midi_event *dev, unsigned char *buf, int len,
104 		      struct snd_seq_event *ev);
105 } extra_event[] = {
106 	{SNDRV_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
107 	{SNDRV_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn},
108 	{SNDRV_SEQ_EVENT_REGPARAM, extra_decode_xrpn},
109 };
110 
111 /*
112  *  new/delete record
113  */
114 
115 int snd_midi_event_new(int bufsize, struct snd_midi_event **rdev)
116 {
117 	struct snd_midi_event *dev;
118 
119 	*rdev = NULL;
120 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
121 	if (dev == NULL)
122 		return -ENOMEM;
123 	if (bufsize > 0) {
124 		dev->buf = kmalloc(bufsize, GFP_KERNEL);
125 		if (dev->buf == NULL) {
126 			kfree(dev);
127 			return -ENOMEM;
128 		}
129 	}
130 	dev->bufsize = bufsize;
131 	dev->lastcmd = 0xff;
132 	dev->type = ST_INVALID;
133 	spin_lock_init(&dev->lock);
134 	*rdev = dev;
135 	return 0;
136 }
137 EXPORT_SYMBOL(snd_midi_event_new);
138 
139 void snd_midi_event_free(struct snd_midi_event *dev)
140 {
141 	if (dev != NULL) {
142 		kfree(dev->buf);
143 		kfree(dev);
144 	}
145 }
146 EXPORT_SYMBOL(snd_midi_event_free);
147 
148 /*
149  * initialize record
150  */
151 static inline void reset_encode(struct snd_midi_event *dev)
152 {
153 	dev->read = 0;
154 	dev->qlen = 0;
155 	dev->type = ST_INVALID;
156 }
157 
158 void snd_midi_event_reset_encode(struct snd_midi_event *dev)
159 {
160 	unsigned long flags;
161 
162 	spin_lock_irqsave(&dev->lock, flags);
163 	reset_encode(dev);
164 	spin_unlock_irqrestore(&dev->lock, flags);
165 }
166 EXPORT_SYMBOL(snd_midi_event_reset_encode);
167 
168 void snd_midi_event_reset_decode(struct snd_midi_event *dev)
169 {
170 	unsigned long flags;
171 
172 	spin_lock_irqsave(&dev->lock, flags);
173 	dev->lastcmd = 0xff;
174 	spin_unlock_irqrestore(&dev->lock, flags);
175 }
176 EXPORT_SYMBOL(snd_midi_event_reset_decode);
177 
178 void snd_midi_event_no_status(struct snd_midi_event *dev, int on)
179 {
180 	dev->nostat = on ? 1 : 0;
181 }
182 EXPORT_SYMBOL(snd_midi_event_no_status);
183 
184 /*
185  *  read one byte and encode to sequencer event:
186  *  return true if MIDI bytes are encoded to an event
187  *         false data is not finished
188  */
189 bool snd_midi_event_encode_byte(struct snd_midi_event *dev, unsigned char c,
190 				struct snd_seq_event *ev)
191 {
192 	bool rc = false;
193 	unsigned long flags;
194 
195 	if (c >= MIDI_CMD_COMMON_CLOCK) {
196 		/* real-time event */
197 		ev->type = status_event[ST_SPECIAL + c - 0xf0].event;
198 		ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
199 		ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
200 		return ev->type != SNDRV_SEQ_EVENT_NONE;
201 	}
202 
203 	spin_lock_irqsave(&dev->lock, flags);
204 	if ((c & 0x80) &&
205 	    (c != MIDI_CMD_COMMON_SYSEX_END || dev->type != ST_SYSEX)) {
206 		/* new command */
207 		dev->buf[0] = c;
208 		if ((c & 0xf0) == 0xf0) /* system messages */
209 			dev->type = (c & 0x0f) + ST_SPECIAL;
210 		else
211 			dev->type = (c >> 4) & 0x07;
212 		dev->read = 1;
213 		dev->qlen = status_event[dev->type].qlen;
214 	} else {
215 		if (dev->qlen > 0) {
216 			/* rest of command */
217 			dev->buf[dev->read++] = c;
218 			if (dev->type != ST_SYSEX)
219 				dev->qlen--;
220 		} else {
221 			/* running status */
222 			dev->buf[1] = c;
223 			dev->qlen = status_event[dev->type].qlen - 1;
224 			dev->read = 2;
225 		}
226 	}
227 	if (dev->qlen == 0) {
228 		ev->type = status_event[dev->type].event;
229 		ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
230 		ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
231 		if (status_event[dev->type].encode) /* set data values */
232 			status_event[dev->type].encode(dev, ev);
233 		if (dev->type >= ST_SPECIAL)
234 			dev->type = ST_INVALID;
235 		rc = true;
236 	} else 	if (dev->type == ST_SYSEX) {
237 		if (c == MIDI_CMD_COMMON_SYSEX_END ||
238 		    dev->read >= dev->bufsize) {
239 			ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
240 			ev->flags |= SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
241 			ev->type = SNDRV_SEQ_EVENT_SYSEX;
242 			ev->data.ext.len = dev->read;
243 			ev->data.ext.ptr = dev->buf;
244 			if (c != MIDI_CMD_COMMON_SYSEX_END)
245 				dev->read = 0; /* continue to parse */
246 			else
247 				reset_encode(dev); /* all parsed */
248 			rc = true;
249 		}
250 	}
251 
252 	spin_unlock_irqrestore(&dev->lock, flags);
253 	return rc;
254 }
255 EXPORT_SYMBOL(snd_midi_event_encode_byte);
256 
257 /* encode note event */
258 static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
259 {
260 	ev->data.note.channel = dev->buf[0] & 0x0f;
261 	ev->data.note.note = dev->buf[1];
262 	ev->data.note.velocity = dev->buf[2];
263 }
264 
265 /* encode one parameter controls */
266 static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
267 {
268 	ev->data.control.channel = dev->buf[0] & 0x0f;
269 	ev->data.control.value = dev->buf[1];
270 }
271 
272 /* encode pitch wheel change */
273 static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
274 {
275 	ev->data.control.channel = dev->buf[0] & 0x0f;
276 	ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192;
277 }
278 
279 /* encode midi control change */
280 static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
281 {
282 	ev->data.control.channel = dev->buf[0] & 0x0f;
283 	ev->data.control.param = dev->buf[1];
284 	ev->data.control.value = dev->buf[2];
285 }
286 
287 /* encode one parameter value*/
288 static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
289 {
290 	ev->data.control.value = dev->buf[1];
291 }
292 
293 /* encode song position */
294 static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
295 {
296 	ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1];
297 }
298 
299 /*
300  * decode from a sequencer event to midi bytes
301  * return the size of decoded midi events
302  */
303 long snd_midi_event_decode(struct snd_midi_event *dev, unsigned char *buf, long count,
304 			   struct snd_seq_event *ev)
305 {
306 	unsigned int cmd, type;
307 
308 	if (ev->type == SNDRV_SEQ_EVENT_NONE)
309 		return -ENOENT;
310 
311 	for (type = 0; type < ARRAY_SIZE(status_event); type++) {
312 		if (ev->type == status_event[type].event)
313 			goto __found;
314 	}
315 	for (type = 0; type < ARRAY_SIZE(extra_event); type++) {
316 		if (ev->type == extra_event[type].event)
317 			return extra_event[type].decode(dev, buf, count, ev);
318 	}
319 	return -ENOENT;
320 
321       __found:
322 	if (type >= ST_SPECIAL)
323 		cmd = 0xf0 + (type - ST_SPECIAL);
324 	else
325 		/* data.note.channel and data.control.channel is identical */
326 		cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f);
327 
328 
329 	if (cmd == MIDI_CMD_COMMON_SYSEX) {
330 		snd_midi_event_reset_decode(dev);
331 		return snd_seq_expand_var_event(ev, count, buf, 1, 0);
332 	} else {
333 		int qlen;
334 		unsigned char xbuf[4];
335 		unsigned long flags;
336 
337 		spin_lock_irqsave(&dev->lock, flags);
338 		if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) {
339 			dev->lastcmd = cmd;
340 			spin_unlock_irqrestore(&dev->lock, flags);
341 			xbuf[0] = cmd;
342 			if (status_event[type].decode)
343 				status_event[type].decode(ev, xbuf + 1);
344 			qlen = status_event[type].qlen + 1;
345 		} else {
346 			spin_unlock_irqrestore(&dev->lock, flags);
347 			if (status_event[type].decode)
348 				status_event[type].decode(ev, xbuf + 0);
349 			qlen = status_event[type].qlen;
350 		}
351 		if (count < qlen)
352 			return -ENOMEM;
353 		memcpy(buf, xbuf, qlen);
354 		return qlen;
355 	}
356 }
357 EXPORT_SYMBOL(snd_midi_event_decode);
358 
359 
360 /* decode note event */
361 static void note_decode(struct snd_seq_event *ev, unsigned char *buf)
362 {
363 	buf[0] = ev->data.note.note & 0x7f;
364 	buf[1] = ev->data.note.velocity & 0x7f;
365 }
366 
367 /* decode one parameter controls */
368 static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf)
369 {
370 	buf[0] = ev->data.control.value & 0x7f;
371 }
372 
373 /* decode pitch wheel change */
374 static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf)
375 {
376 	int value = ev->data.control.value + 8192;
377 	buf[0] = value & 0x7f;
378 	buf[1] = (value >> 7) & 0x7f;
379 }
380 
381 /* decode midi control change */
382 static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf)
383 {
384 	buf[0] = ev->data.control.param & 0x7f;
385 	buf[1] = ev->data.control.value & 0x7f;
386 }
387 
388 /* decode song position */
389 static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf)
390 {
391 	buf[0] = ev->data.control.value & 0x7f;
392 	buf[1] = (ev->data.control.value >> 7) & 0x7f;
393 }
394 
395 /* decode 14bit control */
396 static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf,
397 			       int count, struct snd_seq_event *ev)
398 {
399 	unsigned char cmd;
400 	int idx = 0;
401 
402 	cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
403 	if (ev->data.control.param < 0x20) {
404 		if (count < 4)
405 			return -ENOMEM;
406 		if (dev->nostat && count < 6)
407 			return -ENOMEM;
408 		if (cmd != dev->lastcmd || dev->nostat) {
409 			if (count < 5)
410 				return -ENOMEM;
411 			buf[idx++] = dev->lastcmd = cmd;
412 		}
413 		buf[idx++] = ev->data.control.param;
414 		buf[idx++] = (ev->data.control.value >> 7) & 0x7f;
415 		if (dev->nostat)
416 			buf[idx++] = cmd;
417 		buf[idx++] = ev->data.control.param + 0x20;
418 		buf[idx++] = ev->data.control.value & 0x7f;
419 	} else {
420 		if (count < 2)
421 			return -ENOMEM;
422 		if (cmd != dev->lastcmd || dev->nostat) {
423 			if (count < 3)
424 				return -ENOMEM;
425 			buf[idx++] = dev->lastcmd = cmd;
426 		}
427 		buf[idx++] = ev->data.control.param & 0x7f;
428 		buf[idx++] = ev->data.control.value & 0x7f;
429 	}
430 	return idx;
431 }
432 
433 /* decode reg/nonreg param */
434 static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf,
435 			     int count, struct snd_seq_event *ev)
436 {
437 	unsigned char cmd;
438 	char *cbytes;
439 	static char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB,
440 				       MIDI_CTL_NONREG_PARM_NUM_LSB,
441 				       MIDI_CTL_MSB_DATA_ENTRY,
442 				       MIDI_CTL_LSB_DATA_ENTRY };
443 	static char cbytes_rpn[4] =  { MIDI_CTL_REGIST_PARM_NUM_MSB,
444 				       MIDI_CTL_REGIST_PARM_NUM_LSB,
445 				       MIDI_CTL_MSB_DATA_ENTRY,
446 				       MIDI_CTL_LSB_DATA_ENTRY };
447 	unsigned char bytes[4];
448 	int idx = 0, i;
449 
450 	if (count < 8)
451 		return -ENOMEM;
452 	if (dev->nostat && count < 12)
453 		return -ENOMEM;
454 	cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
455 	bytes[0] = (ev->data.control.param & 0x3f80) >> 7;
456 	bytes[1] = ev->data.control.param & 0x007f;
457 	bytes[2] = (ev->data.control.value & 0x3f80) >> 7;
458 	bytes[3] = ev->data.control.value & 0x007f;
459 	if (cmd != dev->lastcmd && !dev->nostat) {
460 		if (count < 9)
461 			return -ENOMEM;
462 		buf[idx++] = dev->lastcmd = cmd;
463 	}
464 	cbytes = ev->type == SNDRV_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn;
465 	for (i = 0; i < 4; i++) {
466 		if (dev->nostat)
467 			buf[idx++] = dev->lastcmd = cmd;
468 		buf[idx++] = cbytes[i];
469 		buf[idx++] = bytes[i];
470 	}
471 	return idx;
472 }
473