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