Chris@0: /* DSSI ALSA compatibility library Chris@0: * Chris@0: * This library provides for non-ALSA systems the ALSA snd_seq_event_t handling Chris@0: * necessary to compile and run DSSI. It was extracted from alsa-lib 1.0.8. Chris@0: * Chris@0: * See ./alsa/README for more information. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include "alsa/asoundef.h" Chris@0: #include "alsa/sound/asequencer.h" Chris@0: #include "alsa/seq.h" Chris@0: #include "alsa/seq_event.h" Chris@0: #include "alsa/seq_midi_event.h" Chris@0: Chris@0: /** Chris@0: * \file seq/seq_event.c Chris@0: * \brief Sequencer Event Types Chris@0: * \author Takashi Iwai Chris@0: * \date 2001 Chris@0: */ Chris@0: Chris@0: #define FIXED_EV(x) (_SND_SEQ_TYPE(SND_SEQ_EVFLG_FIXED) | _SND_SEQ_TYPE(x)) Chris@0: Chris@0: /** Event types conversion array */ Chris@608: /* Chris@0: const unsigned int snd_seq_event_types[256] = { Chris@0: [SND_SEQ_EVENT_SYSTEM ... SND_SEQ_EVENT_RESULT] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_RESULT), Chris@0: [SND_SEQ_EVENT_NOTE] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_NOTE) | _SND_SEQ_TYPE_OPT(SND_SEQ_EVFLG_NOTE_TWOARG), Chris@0: [SND_SEQ_EVENT_NOTEON ... SND_SEQ_EVENT_KEYPRESS] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_NOTE), Chris@0: [SND_SEQ_EVENT_CONTROLLER ... SND_SEQ_EVENT_REGPARAM] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_CONTROL), Chris@0: [SND_SEQ_EVENT_START ... SND_SEQ_EVENT_STOP] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_QUEUE), Chris@0: [SND_SEQ_EVENT_SETPOS_TICK] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_QUEUE) | _SND_SEQ_TYPE_OPT(SND_SEQ_EVFLG_QUEUE_TICK), Chris@0: [SND_SEQ_EVENT_SETPOS_TIME] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_QUEUE) | _SND_SEQ_TYPE_OPT(SND_SEQ_EVFLG_QUEUE_TIME), Chris@0: [SND_SEQ_EVENT_TEMPO ... SND_SEQ_EVENT_SYNC_POS] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_QUEUE) | _SND_SEQ_TYPE_OPT(SND_SEQ_EVFLG_QUEUE_VALUE), Chris@0: [SND_SEQ_EVENT_TUNE_REQUEST ... SND_SEQ_EVENT_SENSING] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_NONE), Chris@0: [SND_SEQ_EVENT_ECHO ... SND_SEQ_EVENT_OSS] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_RAW) | FIXED_EV(SND_SEQ_EVFLG_SYSTEM), Chris@0: [SND_SEQ_EVENT_CLIENT_START ... SND_SEQ_EVENT_PORT_CHANGE] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_MESSAGE), Chris@0: [SND_SEQ_EVENT_PORT_SUBSCRIBED ... SND_SEQ_EVENT_PORT_UNSUBSCRIBED] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_CONNECTION), Chris@0: [SND_SEQ_EVENT_SAMPLE ... SND_SEQ_EVENT_SAMPLE_PRIVATE1] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_SAMPLE), Chris@0: [SND_SEQ_EVENT_USR0 ... SND_SEQ_EVENT_USR9] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_RAW) | FIXED_EV(SND_SEQ_EVFLG_USERS), Chris@0: [SND_SEQ_EVENT_INSTR_BEGIN ... SND_SEQ_EVENT_INSTR_CHANGE] Chris@0: = _SND_SEQ_TYPE(SND_SEQ_EVFLG_INSTR) | _SND_SEQ_TYPE(SND_SEQ_EVFLG_VARUSR), Chris@0: [SND_SEQ_EVENT_SYSEX ... SND_SEQ_EVENT_BOUNCE] Chris@0: = _SND_SEQ_TYPE(SND_SEQ_EVFLG_VARIABLE), Chris@0: [SND_SEQ_EVENT_USR_VAR0 ... SND_SEQ_EVENT_USR_VAR4] Chris@0: = _SND_SEQ_TYPE(SND_SEQ_EVFLG_VARIABLE) | _SND_SEQ_TYPE(SND_SEQ_EVFLG_USERS), Chris@0: [SND_SEQ_EVENT_NONE] Chris@0: = FIXED_EV(SND_SEQ_EVFLG_NONE), Chris@0: }; Chris@608: */ Chris@0: /** Chris@0: * \file seq/seq_midi_event.c Chris@0: * \brief MIDI byte <-> sequencer event coder Chris@0: * \author Takashi Iwai Chris@0: * \author Jaroslav Kysela Chris@0: * \date 2000-2001 Chris@0: */ Chris@0: Chris@0: /* Chris@0: * MIDI byte <-> sequencer event coder Chris@0: * Chris@0: * Copyright (C) 1998,99,2000 Takashi Iwai , Chris@0: * Jaroslav Kysela Chris@0: * Chris@0: * Chris@0: * This library is free software; you can redistribute it and/or modify Chris@0: * it under the terms of the GNU Lesser General Public License as Chris@0: * published by the Free Software Foundation; either version 2.1 of Chris@0: * the License, or (at your option) any later version. Chris@0: * Chris@0: * This program is distributed in the hope that it will be useful, Chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: * GNU Lesser General Public License for more details. Chris@0: * Chris@0: * You should have received a copy of the GNU Lesser General Public Chris@0: * License along with this library; if not, write to the Free Software Chris@0: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: */ Chris@0: Chris@0: /* midi status */ Chris@0: struct snd_midi_event { Chris@0: size_t qlen; /* queue length */ Chris@0: size_t read; /* chars read */ Chris@0: int type; /* current event type */ Chris@0: unsigned char lastcmd; Chris@0: unsigned char nostat; Chris@0: size_t bufsize; Chris@0: unsigned char *buf; /* input buffer */ Chris@0: }; Chris@0: Chris@0: Chris@0: /* queue type */ Chris@0: /* from 0 to 7 are normal commands (note off, on, etc.) */ Chris@0: #define ST_NOTEOFF 0 Chris@0: #define ST_NOTEON 1 Chris@0: #define ST_SPECIAL 8 Chris@0: #define ST_SYSEX ST_SPECIAL Chris@0: /* from 8 to 15 are events for 0xf0-0xf7 */ Chris@0: Chris@0: Chris@0: /* status event types */ Chris@0: typedef void (*event_encode_t)(snd_midi_event_t *dev, snd_seq_event_t *ev); Chris@0: typedef void (*event_decode_t)(const snd_seq_event_t *ev, unsigned char *buf); Chris@0: Chris@0: /* Chris@0: * prototypes Chris@0: */ Chris@0: static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev); Chris@0: static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev); Chris@0: static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev); Chris@0: static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev); Chris@0: static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev); Chris@0: static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev); Chris@0: static void note_decode(const snd_seq_event_t *ev, unsigned char *buf); Chris@0: static void one_param_decode(const snd_seq_event_t *ev, unsigned char *buf); Chris@0: static void pitchbend_decode(const snd_seq_event_t *ev, unsigned char *buf); Chris@0: static void two_param_decode(const snd_seq_event_t *ev, unsigned char *buf); Chris@0: static void songpos_decode(const snd_seq_event_t *ev, unsigned char *buf); Chris@0: Chris@0: /* Chris@0: * event list Chris@0: */ Chris@0: static struct status_event_list_t { Chris@0: int event; Chris@0: int qlen; Chris@0: event_encode_t encode; Chris@0: event_decode_t decode; Chris@0: } status_event[] = { Chris@0: /* 0x80 - 0xf0 */ Chris@0: {SND_SEQ_EVENT_NOTEOFF, 2, note_event, note_decode}, Chris@0: {SND_SEQ_EVENT_NOTEON, 2, note_event, note_decode}, Chris@0: {SND_SEQ_EVENT_KEYPRESS, 2, note_event, note_decode}, Chris@0: {SND_SEQ_EVENT_CONTROLLER, 2, two_param_ctrl_event, two_param_decode}, Chris@0: {SND_SEQ_EVENT_PGMCHANGE, 1, one_param_ctrl_event, one_param_decode}, Chris@0: {SND_SEQ_EVENT_CHANPRESS, 1, one_param_ctrl_event, one_param_decode}, Chris@0: {SND_SEQ_EVENT_PITCHBEND, 2, pitchbend_ctrl_event, pitchbend_decode}, Chris@0: {SND_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf0 */ Chris@0: /* 0xf0 - 0xff */ Chris@0: {SND_SEQ_EVENT_SYSEX, 1, NULL, NULL}, /* sysex: 0xf0 */ Chris@0: {SND_SEQ_EVENT_QFRAME, 1, one_param_event, one_param_decode}, /* 0xf1 */ Chris@0: {SND_SEQ_EVENT_SONGPOS, 2, songpos_event, songpos_decode}, /* 0xf2 */ Chris@0: {SND_SEQ_EVENT_SONGSEL, 1, one_param_event, one_param_decode}, /* 0xf3 */ Chris@0: {SND_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf4 */ Chris@0: {SND_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf5 */ Chris@0: {SND_SEQ_EVENT_TUNE_REQUEST, 0, NULL, NULL}, /* 0xf6 */ Chris@0: {SND_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf7 */ Chris@0: {SND_SEQ_EVENT_CLOCK, 0, NULL, NULL}, /* 0xf8 */ Chris@0: {SND_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xf9 */ Chris@0: {SND_SEQ_EVENT_START, 0, NULL, NULL}, /* 0xfa */ Chris@0: {SND_SEQ_EVENT_CONTINUE, 0, NULL, NULL}, /* 0xfb */ Chris@0: {SND_SEQ_EVENT_STOP, 0, NULL, NULL}, /* 0xfc */ Chris@0: {SND_SEQ_EVENT_NONE, 0, NULL, NULL}, /* 0xfd */ Chris@0: {SND_SEQ_EVENT_SENSING, 0, NULL, NULL}, /* 0xfe */ Chris@0: {SND_SEQ_EVENT_RESET, 0, NULL, NULL}, /* 0xff */ Chris@0: }; Chris@0: Chris@0: #ifdef UNNEEDED_BY_DSSI Chris@0: static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int len, const snd_seq_event_t *ev); Chris@0: static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev); Chris@0: Chris@0: static struct extra_event_list_t { Chris@0: int event; Chris@0: int (*decode)(snd_midi_event_t *dev, unsigned char *buf, int len, const snd_seq_event_t *ev); Chris@0: } extra_event[] = { Chris@0: {SND_SEQ_EVENT_CONTROL14, extra_decode_ctrl14}, Chris@0: {SND_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn}, Chris@0: {SND_SEQ_EVENT_REGPARAM, extra_decode_xrpn}, Chris@0: }; Chris@0: Chris@0: #define numberof(ary) (sizeof(ary)/sizeof(ary[0])) Chris@0: #endif /* UNNEEDED_BY_DSSI */ Chris@0: Chris@0: /** Chris@0: * \brief Initialize MIDI event parser Chris@0: * \param bufsize buffer size for MIDI message Chris@0: * \param rdev allocated MIDI event parser Chris@0: * \return 0 on success otherwise a negative error code Chris@0: * Chris@0: * Allocates and initializes MIDI event parser. Chris@0: */ Chris@0: int snd_midi_event_new(size_t bufsize, snd_midi_event_t **rdev) Chris@0: { Chris@0: snd_midi_event_t *dev; Chris@0: Chris@0: *rdev = NULL; Chris@0: dev = (snd_midi_event_t *)calloc(1, sizeof(snd_midi_event_t)); Chris@0: if (dev == NULL) Chris@0: return -ENOMEM; Chris@0: if (bufsize > 0) { Chris@0: dev->buf = malloc(bufsize); Chris@0: if (dev->buf == NULL) { Chris@0: free(dev); Chris@0: return -ENOMEM; Chris@0: } Chris@0: } Chris@0: dev->bufsize = bufsize; Chris@0: dev->lastcmd = 0xff; Chris@0: *rdev = dev; Chris@0: return 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * \brief Free MIDI event parser Chris@0: * \param rdev MIDI event parser Chris@0: * \return 0 on success otherwise a negative error code Chris@0: * Chris@0: * Frees MIDI event parser. Chris@0: */ Chris@0: void snd_midi_event_free(snd_midi_event_t *dev) Chris@0: { Chris@0: if (dev != NULL) { Chris@0: if (dev->buf) Chris@0: free(dev->buf); Chris@0: free(dev); Chris@0: } Chris@0: } Chris@0: Chris@0: #ifdef UNNEEDED_BY_DSSI Chris@0: /** Chris@0: * \brief Enable/disable MIDI command merging Chris@0: * \param dev MIDI event parser Chris@0: * \param on 0 - enable MIDI command merging, 1 - always pass the command Chris@0: * Chris@0: * Enable/disable MIDI command merging Chris@0: */ Chris@0: void snd_midi_event_no_status(snd_midi_event_t *dev, int on) Chris@0: { Chris@0: dev->nostat = on ? 1 : 0; Chris@0: } Chris@0: #endif /* UNNEEDED_BY_DSSI */ Chris@0: Chris@0: /* Chris@0: * initialize record Chris@0: */ Chris@0: inline static void reset_encode(snd_midi_event_t *dev) Chris@0: { Chris@0: dev->read = 0; Chris@0: dev->qlen = 0; Chris@0: dev->type = 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * \brief Reset MIDI encode parser Chris@0: * \param dev MIDI event parser Chris@0: * \return 0 on success otherwise a negative error code Chris@0: * Chris@0: * Resets MIDI encode parser Chris@0: */ Chris@0: void snd_midi_event_reset_encode(snd_midi_event_t *dev) Chris@0: { Chris@0: reset_encode(dev); Chris@0: } Chris@0: Chris@0: #ifdef UNNEEDED_BY_DSSI Chris@0: /** Chris@0: * \brief Reset MIDI decode parser Chris@0: * \param dev MIDI event parser Chris@0: * \return 0 on success otherwise a negative error code Chris@0: * Chris@0: * Resets MIDI decode parser Chris@0: */ Chris@0: void snd_midi_event_reset_decode(snd_midi_event_t *dev) Chris@0: { Chris@0: dev->lastcmd = 0xff; Chris@0: } Chris@0: Chris@0: /** Chris@0: * \brief Initializes MIDI parsers Chris@0: * \param dev MIDI event parser Chris@0: * \return 0 on success otherwise a negative error code Chris@0: * Chris@0: * Initializes MIDI parsers (both encode and decode) Chris@0: */ Chris@0: void snd_midi_event_init(snd_midi_event_t *dev) Chris@0: { Chris@0: snd_midi_event_reset_encode(dev); Chris@0: snd_midi_event_reset_decode(dev); Chris@0: } Chris@0: Chris@0: /** Chris@0: * \brief Resize MIDI message (event) buffer Chris@0: * \param dev MIDI event parser Chris@0: * \param bufsize new requested buffer size Chris@0: * \return 0 on success otherwise a negative error code Chris@0: * Chris@0: * Resizes MIDI message (event) buffer. Chris@0: */ Chris@0: int snd_midi_event_resize_buffer(snd_midi_event_t *dev, size_t bufsize) Chris@0: { Chris@0: unsigned char *new_buf, *old_buf; Chris@0: Chris@0: if (bufsize == dev->bufsize) Chris@0: return 0; Chris@0: new_buf = malloc(bufsize); Chris@0: if (new_buf == NULL) Chris@0: return -ENOMEM; Chris@0: old_buf = dev->buf; Chris@0: dev->buf = new_buf; Chris@0: dev->bufsize = bufsize; Chris@0: reset_encode(dev); Chris@0: if (old_buf) Chris@0: free(old_buf); Chris@0: return 0; Chris@0: } Chris@0: #endif /* UNNEEDED_BY_DSSI */ Chris@0: Chris@0: /** Chris@0: * \brief Read bytes and encode to sequencer event if finished Chris@0: * \param dev MIDI event parser Chris@0: * \param buf MIDI byte stream Chris@0: * \param count count of bytes of MIDI byte stream to encode Chris@0: * \param ev Result - sequencer event Chris@0: * \return count of encoded bytes otherwise a negative error code Chris@0: * Chris@0: * Read bytes and encode to sequencer event if finished. Chris@0: * If complete sequencer event is available, ev->type is not Chris@0: * equal to #SND_SEQ_EVENT_NONE. Chris@0: */ Chris@0: long snd_midi_event_encode(snd_midi_event_t *dev, const unsigned char *buf, long count, snd_seq_event_t *ev) Chris@0: { Chris@0: long result = 0; Chris@0: int rc; Chris@0: Chris@0: ev->type = SND_SEQ_EVENT_NONE; Chris@0: Chris@0: while (count-- > 0) { Chris@0: rc = snd_midi_event_encode_byte(dev, *buf++, ev); Chris@0: result++; Chris@0: if (rc < 0) Chris@0: return rc; Chris@0: else if (rc > 0) Chris@0: return result; Chris@0: } Chris@0: Chris@0: return result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * \brief Read one byte and encode to sequencer event if finished Chris@0: * \param dev MIDI event parser Chris@0: * \param c a byte of MIDI stream Chris@0: * \param ev Result - sequencer event Chris@0: * \return 1 - sequencer event is completed, 0 - next byte is required for completion, otherwise a negative error code Chris@0: * Chris@0: * Read byte and encode to sequencer event if finished. Chris@0: */ Chris@0: int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev) Chris@0: { Chris@0: int rc = 0; Chris@0: Chris@0: c &= 0xff; Chris@0: Chris@0: if (c >= MIDI_CMD_COMMON_CLOCK) { Chris@0: /* real-time event */ Chris@0: ev->type = status_event[ST_SPECIAL + c - 0xf0].event; Chris@0: ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; Chris@0: ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED; Chris@0: return 1; Chris@0: } Chris@0: Chris@0: if (dev->qlen > 0) { Chris@0: /* rest of command */ Chris@0: dev->buf[dev->read++] = c; Chris@0: if (dev->type != ST_SYSEX) Chris@0: dev->qlen--; Chris@0: } else { Chris@0: /* new command */ Chris@0: dev->read = 1; Chris@0: if (c & 0x80) { Chris@0: dev->buf[0] = c; Chris@0: if ((c & 0xf0) == 0xf0) /* special events */ Chris@0: dev->type = (c & 0x0f) + ST_SPECIAL; Chris@0: else Chris@0: dev->type = (c >> 4) & 0x07; Chris@0: dev->qlen = status_event[dev->type].qlen; Chris@0: } else { Chris@0: /* process this byte as argument */ Chris@0: dev->buf[dev->read++] = c; Chris@0: dev->qlen = status_event[dev->type].qlen - 1; Chris@0: } Chris@0: } Chris@0: if (dev->qlen == 0) { Chris@0: ev->type = status_event[dev->type].event; Chris@0: ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; Chris@0: ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED; Chris@0: if (status_event[dev->type].encode) /* set data values */ Chris@0: status_event[dev->type].encode(dev, ev); Chris@0: rc = 1; Chris@0: } else if (dev->type == ST_SYSEX) { Chris@0: if (c == MIDI_CMD_COMMON_SYSEX_END || Chris@0: dev->read >= dev->bufsize) { Chris@0: ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; Chris@0: ev->flags |= SNDRV_SEQ_EVENT_LENGTH_VARIABLE; Chris@0: ev->type = SNDRV_SEQ_EVENT_SYSEX; Chris@0: ev->data.ext.len = dev->read; Chris@0: ev->data.ext.ptr = dev->buf; Chris@0: if (c != MIDI_CMD_COMMON_SYSEX_END) Chris@0: dev->read = 0; /* continue to parse */ Chris@0: else Chris@0: reset_encode(dev); /* all parsed */ Chris@0: rc = 1; Chris@0: } Chris@0: } Chris@0: Chris@0: return rc; Chris@0: } Chris@0: Chris@0: /* encode note event */ Chris@0: static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev) Chris@0: { Chris@0: ev->data.note.channel = dev->buf[0] & 0x0f; Chris@0: ev->data.note.note = dev->buf[1]; Chris@0: ev->data.note.velocity = dev->buf[2]; Chris@0: } Chris@0: Chris@0: /* encode one parameter controls */ Chris@0: static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev) Chris@0: { Chris@0: ev->data.control.channel = dev->buf[0] & 0x0f; Chris@0: ev->data.control.value = dev->buf[1]; Chris@0: } Chris@0: Chris@0: /* encode pitch wheel change */ Chris@0: static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev) Chris@0: { Chris@0: ev->data.control.channel = dev->buf[0] & 0x0f; Chris@0: ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192; Chris@0: } Chris@0: Chris@0: /* encode midi control change */ Chris@0: static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev) Chris@0: { Chris@0: ev->data.control.channel = dev->buf[0] & 0x0f; Chris@0: ev->data.control.param = dev->buf[1]; Chris@0: ev->data.control.value = dev->buf[2]; Chris@0: } Chris@0: Chris@0: /* encode one parameter value*/ Chris@0: static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev) Chris@0: { Chris@0: ev->data.control.value = dev->buf[1]; Chris@0: } Chris@0: Chris@0: /* encode song position */ Chris@0: static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev) Chris@0: { Chris@0: ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1]; Chris@0: } Chris@0: Chris@0: #ifdef UNNEEDED_BY_DSSI Chris@0: /** Chris@0: * \brief Decode sequencer event to MIDI byte stream Chris@0: * \param dev MIDI event parser Chris@0: * \param buf Result - MIDI byte stream Chris@0: * \param count Available bytes in MIDI byte stream Chris@0: * \param ev Event to decode Chris@0: * \return count of decoded bytes otherwise a negative error code Chris@0: * Chris@0: * Decode sequencer event to MIDI byte stream. Chris@0: */ Chris@0: long snd_midi_event_decode(snd_midi_event_t *dev, unsigned char *buf, long count, const snd_seq_event_t *ev) Chris@0: { Chris@0: int cmd; Chris@0: long qlen; Chris@0: unsigned int type; Chris@0: Chris@0: if (ev->type == SNDRV_SEQ_EVENT_NONE) Chris@0: return -ENOENT; Chris@0: Chris@0: for (type = 0; type < numberof(status_event); type++) { Chris@0: if (ev->type == status_event[type].event) Chris@0: goto __found; Chris@0: } Chris@0: for (type = 0; type < numberof(extra_event); type++) { Chris@0: if (ev->type == extra_event[type].event) Chris@0: return extra_event[type].decode(dev, buf, count, ev); Chris@0: } Chris@0: return -ENOENT; Chris@0: Chris@0: __found: Chris@0: if (type >= ST_SPECIAL) Chris@0: cmd = 0xf0 + (type - ST_SPECIAL); Chris@0: else Chris@0: /* data.note.channel and data.control.channel is identical */ Chris@0: cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f); Chris@0: Chris@0: Chris@0: if (cmd == MIDI_CMD_COMMON_SYSEX) { Chris@0: qlen = ev->data.ext.len; Chris@0: if (count < qlen) Chris@0: return -ENOMEM; Chris@0: switch (ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) { Chris@0: case SNDRV_SEQ_EVENT_LENGTH_FIXED: Chris@0: return -EINVAL; /* invalid event */ Chris@0: } Chris@0: memcpy(buf, ev->data.ext.ptr, qlen); Chris@0: return qlen; Chris@0: } else { Chris@0: unsigned char xbuf[4]; Chris@0: Chris@0: if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) { Chris@0: dev->lastcmd = cmd; Chris@0: xbuf[0] = cmd; Chris@0: if (status_event[type].decode) Chris@0: status_event[type].decode(ev, xbuf + 1); Chris@0: qlen = status_event[type].qlen + 1; Chris@0: } else { Chris@0: if (status_event[type].decode) Chris@0: status_event[type].decode(ev, xbuf + 0); Chris@0: qlen = status_event[type].qlen; Chris@0: } Chris@0: if (count < qlen) Chris@0: return -ENOMEM; Chris@0: memcpy(buf, xbuf, qlen); Chris@0: return qlen; Chris@0: } Chris@0: } Chris@0: #endif /* UNNEEDED_BY_DSSI */ Chris@0: Chris@0: /* decode note event */ Chris@0: static void note_decode(const snd_seq_event_t *ev, unsigned char *buf) Chris@0: { Chris@0: buf[0] = ev->data.note.note & 0x7f; Chris@0: buf[1] = ev->data.note.velocity & 0x7f; Chris@0: } Chris@0: Chris@0: /* decode one parameter controls */ Chris@0: static void one_param_decode(const snd_seq_event_t *ev, unsigned char *buf) Chris@0: { Chris@0: buf[0] = ev->data.control.value & 0x7f; Chris@0: } Chris@0: Chris@0: /* decode pitch wheel change */ Chris@0: static void pitchbend_decode(const snd_seq_event_t *ev, unsigned char *buf) Chris@0: { Chris@0: int value = ev->data.control.value + 8192; Chris@0: buf[0] = value & 0x7f; Chris@0: buf[1] = (value >> 7) & 0x7f; Chris@0: } Chris@0: Chris@0: /* decode midi control change */ Chris@0: static void two_param_decode(const snd_seq_event_t *ev, unsigned char *buf) Chris@0: { Chris@0: buf[0] = ev->data.control.param & 0x7f; Chris@0: buf[1] = ev->data.control.value & 0x7f; Chris@0: } Chris@0: Chris@0: /* decode song position */ Chris@0: static void songpos_decode(const snd_seq_event_t *ev, unsigned char *buf) Chris@0: { Chris@0: buf[0] = ev->data.control.value & 0x7f; Chris@0: buf[1] = (ev->data.control.value >> 7) & 0x7f; Chris@0: } Chris@0: Chris@0: #ifdef UNNEEDED_BY_DSSI Chris@0: /* decode 14bit control */ Chris@0: static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev) Chris@0: { Chris@0: unsigned char cmd; Chris@0: int idx = 0; Chris@0: Chris@0: cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f); Chris@0: if (ev->data.control.param < 32) { Chris@0: if (count < 4) Chris@0: return -ENOMEM; Chris@0: if (dev->nostat && count < 6) Chris@0: return -ENOMEM; Chris@0: if (cmd != dev->lastcmd || dev->nostat) { Chris@0: if (count < 5) Chris@0: return -ENOMEM; Chris@0: buf[idx++] = dev->lastcmd = cmd; Chris@0: } Chris@0: buf[idx++] = ev->data.control.param; Chris@0: buf[idx++] = (ev->data.control.value >> 7) & 0x7f; Chris@0: if (dev->nostat) Chris@0: buf[idx++] = cmd; Chris@0: buf[idx++] = ev->data.control.param + 32; Chris@0: buf[idx++] = ev->data.control.value & 0x7f; Chris@0: } else { Chris@0: if (count < 2) Chris@0: return -ENOMEM; Chris@0: if (cmd != dev->lastcmd || dev->nostat) { Chris@0: if (count < 3) Chris@0: return -ENOMEM; Chris@0: buf[idx++] = dev->lastcmd = cmd; Chris@0: } Chris@0: buf[idx++] = ev->data.control.param & 0x7f; Chris@0: buf[idx++] = ev->data.control.value & 0x7f; Chris@0: } Chris@0: return idx; Chris@0: } Chris@0: Chris@0: /* decode reg/nonreg param */ Chris@0: static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev) Chris@0: { Chris@0: unsigned char cmd; Chris@0: char *cbytes; Chris@0: static char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB, Chris@0: MIDI_CTL_NONREG_PARM_NUM_LSB, Chris@0: MIDI_CTL_MSB_DATA_ENTRY, Chris@0: MIDI_CTL_LSB_DATA_ENTRY }; Chris@0: static char cbytes_rpn[4] = { MIDI_CTL_REGIST_PARM_NUM_MSB, Chris@0: MIDI_CTL_REGIST_PARM_NUM_LSB, Chris@0: MIDI_CTL_MSB_DATA_ENTRY, Chris@0: MIDI_CTL_LSB_DATA_ENTRY }; Chris@0: unsigned char bytes[4]; Chris@0: int idx = 0, i; Chris@0: Chris@0: if (count < 8) Chris@0: return -ENOMEM; Chris@0: if (dev->nostat && count < 12) Chris@0: return -ENOMEM; Chris@0: cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f); Chris@0: bytes[0] = ev->data.control.param & 0x007f; Chris@0: bytes[1] = (ev->data.control.param & 0x3f80) >> 7; Chris@0: bytes[2] = ev->data.control.value & 0x007f; Chris@0: bytes[3] = (ev->data.control.value & 0x3f80) >> 7; Chris@0: if (cmd != dev->lastcmd && !dev->nostat) { Chris@0: if (count < 9) Chris@0: return -ENOMEM; Chris@0: buf[idx++] = dev->lastcmd = cmd; Chris@0: } Chris@0: cbytes = ev->type == SND_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn; Chris@0: for (i = 0; i < 4; i++) { Chris@0: if (dev->nostat) Chris@0: buf[idx++] = dev->lastcmd = cmd; Chris@0: buf[idx++] = cbytes[i]; Chris@0: buf[idx++] = bytes[i]; Chris@0: } Chris@0: return idx; Chris@0: } Chris@0: #endif /* UNNEEDED_BY_DSSI */