Blame src/seq/seq_midi_event.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file seq/seq_midi_event.c
Packit 4a16fb
 * \brief MIDI byte <-> sequencer event coder
Packit 4a16fb
 * \author Takashi Iwai <tiwai@suse.de>
Packit 4a16fb
 * \author Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 * \date 2000-2001
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  MIDI byte <-> sequencer event coder
Packit 4a16fb
 *
Packit 4a16fb
 *  Copyright (C) 1998,99,2000 Takashi Iwai <tiwai@suse.de>,
Packit 4a16fb
 *			       Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <malloc.h>
Packit 4a16fb
#include "local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
Packit 4a16fb
/* midi status */
Packit 4a16fb
struct snd_midi_event {
Packit 4a16fb
	ssize_t qlen;	/* queue length */
Packit 4a16fb
	size_t read;	/* chars read */
Packit 4a16fb
	int type;	/* current event type */
Packit 4a16fb
	unsigned char lastcmd;
Packit 4a16fb
	unsigned char nostat;
Packit 4a16fb
	size_t bufsize;
Packit 4a16fb
	unsigned char *buf;	/* input buffer */
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
/* event type, index into status_event[] */
Packit 4a16fb
/* from 0 to 6 are normal commands (note off, on, etc.) for 0x8?-0xe? */
Packit 4a16fb
#define ST_INVALID	7
Packit 4a16fb
#define ST_SPECIAL	8
Packit 4a16fb
#define ST_SYSEX	ST_SPECIAL
Packit 4a16fb
/* from 8 to 15 are events for 0xf0-0xf7 */
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
/* status event types */
Packit 4a16fb
typedef void (*event_encode_t)(snd_midi_event_t *dev, snd_seq_event_t *ev);
Packit 4a16fb
typedef void (*event_decode_t)(const snd_seq_event_t *ev, unsigned char *buf);
Packit 4a16fb
Packit 4a16fb
#endif /* DOC_HIDDEN */
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * prototypes
Packit 4a16fb
 */
Packit 4a16fb
static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
Packit 4a16fb
static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
Packit 4a16fb
static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
Packit 4a16fb
static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
Packit 4a16fb
static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
Packit 4a16fb
static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
Packit 4a16fb
static void note_decode(const snd_seq_event_t *ev, unsigned char *buf);
Packit 4a16fb
static void one_param_decode(const snd_seq_event_t *ev, unsigned char *buf);
Packit 4a16fb
static void pitchbend_decode(const snd_seq_event_t *ev, unsigned char *buf);
Packit 4a16fb
static void two_param_decode(const snd_seq_event_t *ev, unsigned char *buf);
Packit 4a16fb
static void songpos_decode(const snd_seq_event_t *ev, unsigned char *buf);
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * event list
Packit 4a16fb
 */
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
static const struct status_event_list_t {
Packit 4a16fb
	int event;
Packit 4a16fb
	int qlen;
Packit 4a16fb
	event_encode_t encode;
Packit 4a16fb
	event_decode_t decode;
Packit 4a16fb
} status_event[] = {
Packit 4a16fb
	/* 0x80 - 0xef */
Packit 4a16fb
	{SND_SEQ_EVENT_NOTEOFF,		 2, note_event, note_decode},
Packit 4a16fb
	{SND_SEQ_EVENT_NOTEON,		 2, note_event, note_decode},
Packit 4a16fb
	{SND_SEQ_EVENT_KEYPRESS,	 2, note_event, note_decode},
Packit 4a16fb
	{SND_SEQ_EVENT_CONTROLLER,	 2, two_param_ctrl_event, two_param_decode},
Packit 4a16fb
	{SND_SEQ_EVENT_PGMCHANGE,	 1, one_param_ctrl_event, one_param_decode},
Packit 4a16fb
	{SND_SEQ_EVENT_CHANPRESS,	 1, one_param_ctrl_event, one_param_decode},
Packit 4a16fb
	{SND_SEQ_EVENT_PITCHBEND,	 2, pitchbend_ctrl_event, pitchbend_decode},
Packit 4a16fb
	/* invalid */
Packit 4a16fb
	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL},
Packit 4a16fb
	/* 0xf0 - 0xff */
Packit 4a16fb
	{SND_SEQ_EVENT_SYSEX,		 1, NULL, NULL}, /* sysex: 0xf0 */
Packit 4a16fb
	{SND_SEQ_EVENT_QFRAME,		 1, one_param_event, one_param_decode}, /* 0xf1 */
Packit 4a16fb
	{SND_SEQ_EVENT_SONGPOS,		 2, songpos_event, songpos_decode}, /* 0xf2 */
Packit 4a16fb
	{SND_SEQ_EVENT_SONGSEL,		 1, one_param_event, one_param_decode}, /* 0xf3 */
Packit 4a16fb
	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf4 */
Packit 4a16fb
	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf5 */
Packit 4a16fb
	{SND_SEQ_EVENT_TUNE_REQUEST,	 0, NULL, NULL}, /* 0xf6 */
Packit 4a16fb
	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf7 */
Packit 4a16fb
	{SND_SEQ_EVENT_CLOCK,		 0, NULL, NULL}, /* 0xf8 */
Packit 4a16fb
	{SND_SEQ_EVENT_NONE,		-1, NULL, NULL}, /* 0xf9 */
Packit 4a16fb
	{SND_SEQ_EVENT_START,		 0, NULL, NULL}, /* 0xfa */
Packit 4a16fb
	{SND_SEQ_EVENT_CONTINUE,	 0, NULL, NULL}, /* 0xfb */
Packit 4a16fb
	{SND_SEQ_EVENT_STOP, 		 0, NULL, NULL}, /* 0xfc */
Packit 4a16fb
	{SND_SEQ_EVENT_NONE, 		-1, NULL, NULL}, /* 0xfd */
Packit 4a16fb
	{SND_SEQ_EVENT_SENSING, 	 0, NULL, NULL}, /* 0xfe */
Packit 4a16fb
	{SND_SEQ_EVENT_RESET, 		 0, NULL, NULL}, /* 0xff */
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int len, const snd_seq_event_t *ev);
Packit 4a16fb
static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev);
Packit 4a16fb
Packit 4a16fb
static const struct extra_event_list_t {
Packit 4a16fb
	int event;
Packit 4a16fb
	int (*decode)(snd_midi_event_t *dev, unsigned char *buf, int len, const snd_seq_event_t *ev);
Packit 4a16fb
} extra_event[] = {
Packit 4a16fb
	{SND_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
Packit 4a16fb
	{SND_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn},
Packit 4a16fb
	{SND_SEQ_EVENT_REGPARAM, extra_decode_xrpn},
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
#define numberof(ary)	(sizeof(ary)/sizeof(ary[0]))
Packit 4a16fb
#endif /* DOC_HIDDEN */
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a MIDI event parser.
Packit 4a16fb
 * \param[in] bufsize Size of the buffer used for encoding; this should be
Packit 4a16fb
 *                    large enough to hold the largest MIDI message to be
Packit 4a16fb
 *                    encoded.
Packit 4a16fb
 * \param[out] rdev The new MIDI event parser.
Packit 4a16fb
 * \return Zero on success, otherwise a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function creates and initializes a MIDI parser object that can be used
Packit 4a16fb
 * to convert a MIDI byte stream to sequencer events (encoding) and/or to
Packit 4a16fb
 * convert sequencer events to a MIDI byte stream (decoding).
Packit 4a16fb
 *
Packit 4a16fb
 * \par Errors:
Packit 4a16fb
 * 
Packit 4a16fb
 * 
-ENOMEM
Out of memory.
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 */
Packit 4a16fb
int snd_midi_event_new(size_t bufsize, snd_midi_event_t **rdev)
Packit 4a16fb
{
Packit 4a16fb
	snd_midi_event_t *dev;
Packit 4a16fb
Packit 4a16fb
	*rdev = NULL;
Packit 4a16fb
	dev = (snd_midi_event_t *)calloc(1, sizeof(snd_midi_event_t));
Packit 4a16fb
	if (dev == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	if (bufsize > 0) {
Packit 4a16fb
		dev->buf = malloc(bufsize);
Packit 4a16fb
		if (dev->buf == NULL) {
Packit 4a16fb
			free(dev);
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	dev->bufsize = bufsize;
Packit 4a16fb
	dev->lastcmd = 0xff;
Packit 4a16fb
	dev->type = ST_INVALID;
Packit 4a16fb
	*rdev = dev;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Frees a MIDI event parser.
Packit 4a16fb
 * \param dev MIDI event parser.
Packit 4a16fb
 *
Packit 4a16fb
 * Frees a MIDI event parser.
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 */
Packit 4a16fb
void snd_midi_event_free(snd_midi_event_t *dev)
Packit 4a16fb
{
Packit 4a16fb
	if (dev != NULL) {
Packit 4a16fb
		free(dev->buf);
Packit 4a16fb
		free(dev);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Enables/disables MIDI command merging.
Packit 4a16fb
 * \param dev MIDI event parser.
Packit 4a16fb
 * \param on 0 to enable MIDI command merging,
Packit 4a16fb
 *           1 to always write the command byte.
Packit 4a16fb
 *
Packit 4a16fb
 * This function enables or disables MIDI command merging (running status).
Packit 4a16fb
 *
Packit 4a16fb
 * When MIDI command merging is not disabled, #snd_midi_event_decode is allowed
Packit 4a16fb
 * to omit any status byte that is identical to the previous status byte.
Packit 4a16fb
 */
Packit 4a16fb
void snd_midi_event_no_status(snd_midi_event_t *dev, int on)
Packit 4a16fb
{
Packit 4a16fb
	dev->nostat = on ? 1 : 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * initialize record
Packit 4a16fb
 */
Packit 4a16fb
inline static void reset_encode(snd_midi_event_t *dev)
Packit 4a16fb
{
Packit 4a16fb
	dev->read = 0;
Packit 4a16fb
	dev->qlen = 0;
Packit 4a16fb
	dev->type = ST_INVALID;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Resets MIDI encode parser.
Packit 4a16fb
 * \param dev MIDI event parser.
Packit 4a16fb
 *
Packit 4a16fb
 * This function resets the MIDI encoder of the parser \a dev.
Packit 4a16fb
 * Any partially encoded MIDI message is dropped,
Packit 4a16fb
 * and running status state is cleared.
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 */
Packit 4a16fb
void snd_midi_event_reset_encode(snd_midi_event_t *dev)
Packit 4a16fb
{
Packit 4a16fb
	reset_encode(dev);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Resets MIDI decode parser.
Packit 4a16fb
 * \param dev MIDI event parser.
Packit 4a16fb
 *
Packit 4a16fb
 * This function resets the MIDI decoder of the parser \a dev.
Packit 4a16fb
 * The next decoded message does not use running status from before the call to
Packit 4a16fb
 * \a snd_midi_event_reset_decode.
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 */
Packit 4a16fb
void snd_midi_event_reset_decode(snd_midi_event_t *dev)
Packit 4a16fb
{
Packit 4a16fb
	dev->lastcmd = 0xff;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Resets MIDI encode/decode parsers.
Packit 4a16fb
 * \param dev MIDI event parser.
Packit 4a16fb
 *
Packit 4a16fb
 * This function resets both encoder and decoder of the MIDI event parser.
Packit 4a16fb
 * \sa snd_midi_event_reset_encode, snd_midi_event_reset_decode
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 */
Packit 4a16fb
void snd_midi_event_init(snd_midi_event_t *dev)
Packit 4a16fb
{
Packit 4a16fb
	snd_midi_event_reset_encode(dev);
Packit 4a16fb
	snd_midi_event_reset_decode(dev);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Resizes the MIDI message encoding buffer.
Packit 4a16fb
 * \param dev MIDI event parser.
Packit 4a16fb
 * \param bufsize The new buffer size.
Packit 4a16fb
 * \return Zero on success, otherwise a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function resizes the buffer that is used to hold partially encoded MIDI
Packit 4a16fb
 * messages.
Packit 4a16fb
 *
Packit 4a16fb
 * If there is a partially encoded message in the buffer, it is dropped.
Packit 4a16fb
 *
Packit 4a16fb
 * \par Errors:
Packit 4a16fb
 * 
Packit 4a16fb
 * 
-ENOMEM
Out of memory.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_midi_event_encode, snd_midi_event_reset_encode
Packit 4a16fb
 */
Packit 4a16fb
int snd_midi_event_resize_buffer(snd_midi_event_t *dev, size_t bufsize)
Packit 4a16fb
{
Packit 4a16fb
	unsigned char *new_buf, *old_buf;
Packit 4a16fb
Packit 4a16fb
	if (bufsize == dev->bufsize)
Packit 4a16fb
		return 0;
Packit 4a16fb
	new_buf = malloc(bufsize);
Packit 4a16fb
	if (new_buf == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	old_buf = dev->buf;
Packit 4a16fb
	dev->buf = new_buf;
Packit 4a16fb
	dev->bufsize = bufsize;
Packit 4a16fb
	reset_encode(dev);
Packit 4a16fb
	free(old_buf);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Encodes bytes to sequencer event.
Packit 4a16fb
 * \param[in] dev MIDI event parser.
Packit 4a16fb
 * \param[in] buf Buffer containing bytes of a raw MIDI stream.
Packit 4a16fb
 * \param[in] count Number of bytes in \a buf.
Packit 4a16fb
 * \param[out] ev Sequencer event.
Packit 4a16fb
 * \return The number of bytes consumed, or a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function tries to use up to \a count bytes from the beginning of the
Packit 4a16fb
 * buffer to encode a sequencer event.  If a complete MIDI message has been
Packit 4a16fb
 * encoded, the sequencer event is written to \a ev; otherwise, \a ev->type is
Packit 4a16fb
 * set to #SND_SEQ_EVENT_NONE, and further bytes are required to complete
Packit 4a16fb
 * a message.
Packit 4a16fb
 *
Packit 4a16fb
 * The buffer in \a dev is used to hold any bytes of a not-yet-complete MIDI
Packit 4a16fb
 * message.  If a System Exclusive message is larger than the buffer, the
Packit 4a16fb
 * message is split into multiple parts, and a sequencer event is returned at
Packit 4a16fb
 * the end of each part.
Packit 4a16fb
 *
Packit 4a16fb
 * Any bytes that are not part of a valid MIDI message are silently ignored,
Packit 4a16fb
 * i.e., they are consumed without signaling an error.
Packit 4a16fb
 *
Packit 4a16fb
 * When this function returns a system exclusive sequencer event (\a ev->type
Packit 4a16fb
 * is #SND_SEQ_EVENT_SYSEX), the data pointer (\a ev->data.ext.ptr) points into
Packit 4a16fb
 * the MIDI event parser's buffer.  Therefore, the sequencer event can only be
Packit 4a16fb
 * used as long as that buffer remains valid, i.e., until the next call to
Packit 4a16fb
 * #snd_midi_event_encode, #snd_midi_event_encode_byte,
Packit 4a16fb
 * #snd_midi_event_resize_buffer, #snd_midi_event_init,
Packit 4a16fb
 * #snd_midi_event_reset_encode, or #snd_midi_event_free for that MIDI event
Packit 4a16fb
 * parser.
Packit 4a16fb
 *
Packit 4a16fb
 * This function can generate any sequencer event that corresponds to a MIDI
Packit 4a16fb
 * message, i.e.:
Packit 4a16fb
 * - #SND_SEQ_EVENT_NOTEOFF
Packit 4a16fb
 * - #SND_SEQ_EVENT_NOTEON
Packit 4a16fb
 * - #SND_SEQ_EVENT_KEYPRESS
Packit 4a16fb
 * - #SND_SEQ_EVENT_CONTROLLER
Packit 4a16fb
 * - #SND_SEQ_EVENT_PGMCHANGE
Packit 4a16fb
 * - #SND_SEQ_EVENT_CHANPRESS
Packit 4a16fb
 * - #SND_SEQ_EVENT_PITCHBEND
Packit 4a16fb
 * - #SND_SEQ_EVENT_SYSEX
Packit 4a16fb
 * - #SND_SEQ_EVENT_QFRAME
Packit 4a16fb
 * - #SND_SEQ_EVENT_SONGPOS
Packit 4a16fb
 * - #SND_SEQ_EVENT_SONGSEL
Packit 4a16fb
 * - #SND_SEQ_EVENT_TUNE_REQUEST
Packit 4a16fb
 * - #SND_SEQ_EVENT_CLOCK
Packit 4a16fb
 * - #SND_SEQ_EVENT_START
Packit 4a16fb
 * - #SND_SEQ_EVENT_CONTINUE
Packit 4a16fb
 * - #SND_SEQ_EVENT_STOP
Packit 4a16fb
 * - #SND_SEQ_EVENT_SENSING
Packit 4a16fb
 * - #SND_SEQ_EVENT_RESET
Packit 4a16fb
 * .
Packit 4a16fb
 * Some implementations may also be able to generate the following events
Packit 4a16fb
 * for a sequence of controller change messages:
Packit 4a16fb
 * - #SND_SEQ_EVENT_CONTROL14
Packit 4a16fb
 * - #SND_SEQ_EVENT_NONREGPARAM
Packit 4a16fb
 * - #SND_SEQ_EVENT_REGPARAM
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_midi_event_new, snd_midi_event_reset_encode, snd_midi_event_encode_byte
Packit 4a16fb
 */
Packit 4a16fb
long snd_midi_event_encode(snd_midi_event_t *dev, const unsigned char *buf, long count, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	long result = 0;
Packit 4a16fb
	int rc;
Packit 4a16fb
Packit 4a16fb
	ev->type = SND_SEQ_EVENT_NONE;
Packit 4a16fb
Packit 4a16fb
	while (count-- > 0) {
Packit 4a16fb
		rc = snd_midi_event_encode_byte(dev, *buf++, ev);
Packit 4a16fb
		result++;
Packit 4a16fb
		if (rc < 0)
Packit 4a16fb
			return rc;
Packit 4a16fb
		else if (rc > 0)
Packit 4a16fb
			return result;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return result;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Encodes byte to sequencer event.
Packit 4a16fb
 * \param[in] dev MIDI event parser.
Packit 4a16fb
 * \param[in] c A byte of a raw MIDI stream.
Packit 4a16fb
 * \param[out] ev Sequencer event.
Packit 4a16fb
 * \return 1 if a sequenver event has been completed, 0 if more bytes are
Packit 4a16fb
 *         required to complete an event, or a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function tries to use the byte \a c to encode a sequencer event.  If
Packit 4a16fb
 * a complete MIDI message has been encoded, the sequencer event is written to
Packit 4a16fb
 * \a ev; otherwise, further bytes are required to complete a message.
Packit 4a16fb
 *
Packit 4a16fb
 * See also the description of #snd_midi_event_encode.
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_midi_event_new, snd_midi_event_reset_encode, snd_midi_event_encode
Packit 4a16fb
 */
Packit 4a16fb
int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	int rc = 0;
Packit 4a16fb
Packit 4a16fb
	c &= 0xff;
Packit 4a16fb
Packit 4a16fb
	if (c >= MIDI_CMD_COMMON_CLOCK) {
Packit 4a16fb
		/* real-time event */
Packit 4a16fb
		ev->type = status_event[ST_SPECIAL + c - 0xf0].event;
Packit 4a16fb
		ev->flags &= ~SND_SEQ_EVENT_LENGTH_MASK;
Packit 4a16fb
		ev->flags |= SND_SEQ_EVENT_LENGTH_FIXED;
Packit 4a16fb
		return ev->type != SND_SEQ_EVENT_NONE;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if ((c & 0x80) &&
Packit 4a16fb
	    (c != MIDI_CMD_COMMON_SYSEX_END || dev->type != ST_SYSEX)) {
Packit 4a16fb
		/* new command */
Packit 4a16fb
		dev->buf[0] = c;
Packit 4a16fb
		if ((c & 0xf0) == 0xf0) /* system message */
Packit 4a16fb
			dev->type = (c & 0x0f) + ST_SPECIAL;
Packit 4a16fb
		else
Packit 4a16fb
			dev->type = (c >> 4) & 0x07;
Packit 4a16fb
		dev->read = 1;
Packit 4a16fb
		dev->qlen = status_event[dev->type].qlen;
Packit 4a16fb
	} else {
Packit 4a16fb
		if (dev->qlen > 0) {
Packit 4a16fb
			/* rest of command */
Packit 4a16fb
			dev->buf[dev->read++] = c;
Packit 4a16fb
			if (dev->type != ST_SYSEX)
Packit 4a16fb
				dev->qlen--;
Packit 4a16fb
		} else {
Packit 4a16fb
			/* running status */
Packit 4a16fb
			dev->buf[1] = c;
Packit 4a16fb
			dev->qlen = status_event[dev->type].qlen - 1;
Packit 4a16fb
			dev->read = 2;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (dev->qlen == 0) {
Packit 4a16fb
		ev->type = status_event[dev->type].event;
Packit 4a16fb
		ev->flags &= ~SND_SEQ_EVENT_LENGTH_MASK;
Packit 4a16fb
		ev->flags |= SND_SEQ_EVENT_LENGTH_FIXED;
Packit 4a16fb
		if (status_event[dev->type].encode) /* set data values */
Packit 4a16fb
			status_event[dev->type].encode(dev, ev);
Packit 4a16fb
		if (dev->type >= ST_SPECIAL)
Packit 4a16fb
			dev->type = ST_INVALID;
Packit 4a16fb
		rc = 1;
Packit 4a16fb
	} else 	if (dev->type == ST_SYSEX) {
Packit 4a16fb
		if (c == MIDI_CMD_COMMON_SYSEX_END ||
Packit 4a16fb
		    dev->read >= dev->bufsize) {
Packit 4a16fb
			ev->flags &= ~SND_SEQ_EVENT_LENGTH_MASK;
Packit 4a16fb
			ev->flags |= SND_SEQ_EVENT_LENGTH_VARIABLE;
Packit 4a16fb
			ev->type = SND_SEQ_EVENT_SYSEX;
Packit 4a16fb
			ev->data.ext.len = dev->read;
Packit 4a16fb
			ev->data.ext.ptr = dev->buf;
Packit 4a16fb
			if (c != MIDI_CMD_COMMON_SYSEX_END)
Packit 4a16fb
				dev->read = 0; /* continue to parse */
Packit 4a16fb
			else
Packit 4a16fb
				reset_encode(dev); /* all parsed */
Packit 4a16fb
			rc = 1;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return rc;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* encode note event */
Packit 4a16fb
static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	ev->data.note.channel = dev->buf[0] & 0x0f;
Packit 4a16fb
	ev->data.note.note = dev->buf[1];
Packit 4a16fb
	ev->data.note.velocity = dev->buf[2];
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* encode one parameter controls */
Packit 4a16fb
static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	ev->data.control.channel = dev->buf[0] & 0x0f;
Packit 4a16fb
	ev->data.control.value = dev->buf[1];
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* encode pitch wheel change */
Packit 4a16fb
static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	ev->data.control.channel = dev->buf[0] & 0x0f;
Packit 4a16fb
	ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* encode midi control change */
Packit 4a16fb
static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	ev->data.control.channel = dev->buf[0] & 0x0f;
Packit 4a16fb
	ev->data.control.param = dev->buf[1];
Packit 4a16fb
	ev->data.control.value = dev->buf[2];
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* encode one parameter value*/
Packit 4a16fb
static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	ev->data.control.value = dev->buf[1];
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* encode song position */
Packit 4a16fb
static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1];
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Decodes sequencer event to MIDI byte stream.
Packit 4a16fb
 * \param[in] dev MIDI event parser.
Packit 4a16fb
 * \param[out] buf Buffer for the resulting MIDI byte stream.
Packit 4a16fb
 * \param[in] count Number of bytes in \a buf.
Packit 4a16fb
 * \param[in] ev The sequencer event to decode.
Packit 4a16fb
 * \return The number of bytes written to \a buf, or a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function tries to decode the sequencer event into one or more MIDI
Packit 4a16fb
 * messages, and writes the raw MIDI byte(s) into \a buf.
Packit 4a16fb
 *
Packit 4a16fb
 * The generated MIDI messages may use running status, unless disabled with
Packit 4a16fb
 * #snd_midi_event_no_status.
Packit 4a16fb
 *
Packit 4a16fb
 * The required buffer size for a sequencer event it as most 12 bytes, except
Packit 4a16fb
 * for System Exclusive events (\a ev->type == #SND_SEQ_EVENT_SYSEX) which can
Packit 4a16fb
 * have any length (as specified by \a ev->data.ext.len).
Packit 4a16fb
 *
Packit 4a16fb
 * The following sequencer events correspond to MIDI messages:
Packit 4a16fb
 * - #SND_SEQ_EVENT_NOTEOFF
Packit 4a16fb
 * - #SND_SEQ_EVENT_NOTEON
Packit 4a16fb
 * - #SND_SEQ_EVENT_KEYPRESS
Packit 4a16fb
 * - #SND_SEQ_EVENT_CONTROLLER
Packit 4a16fb
 * - #SND_SEQ_EVENT_PGMCHANGE
Packit 4a16fb
 * - #SND_SEQ_EVENT_CHANPRESS
Packit 4a16fb
 * - #SND_SEQ_EVENT_PITCHBEND
Packit 4a16fb
 * - #SND_SEQ_EVENT_SYSEX
Packit 4a16fb
 * - #SND_SEQ_EVENT_QFRAME
Packit 4a16fb
 * - #SND_SEQ_EVENT_SONGPOS
Packit 4a16fb
 * - #SND_SEQ_EVENT_SONGSEL
Packit 4a16fb
 * - #SND_SEQ_EVENT_TUNE_REQUEST
Packit 4a16fb
 * - #SND_SEQ_EVENT_CLOCK
Packit 4a16fb
 * - #SND_SEQ_EVENT_START
Packit 4a16fb
 * - #SND_SEQ_EVENT_CONTINUE
Packit 4a16fb
 * - #SND_SEQ_EVENT_STOP
Packit 4a16fb
 * - #SND_SEQ_EVENT_SENSING
Packit 4a16fb
 * - #SND_SEQ_EVENT_RESET
Packit 4a16fb
 * - #SND_SEQ_EVENT_CONTROL14
Packit 4a16fb
 * - #SND_SEQ_EVENT_NONREGPARAM
Packit 4a16fb
 * - #SND_SEQ_EVENT_REGPARAM
Packit 4a16fb
 *
Packit 4a16fb
 * \par Errors:
Packit 4a16fb
 * 
Packit 4a16fb
 * 
-EINVAL
\a ev is not a valid sequencer event.
Packit 4a16fb
 * 
-ENOENT
The sequencer event does not correspond to one or more MIDI messages.
Packit 4a16fb
 * 
-ENOMEM
The MIDI message(s) would not fit into \a count bytes.
Packit 4a16fb
 *
Packit 4a16fb
 * \par Conforming to:
Packit 4a16fb
 * LSB 3.2
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_midi_event_reset_decode, snd_midi_event_no_status
Packit 4a16fb
 */
Packit 4a16fb
long snd_midi_event_decode(snd_midi_event_t *dev, unsigned char *buf, long count, const snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	int cmd;
Packit 4a16fb
	long qlen;
Packit 4a16fb
	unsigned int type;
Packit 4a16fb
Packit 4a16fb
	if (ev->type == SND_SEQ_EVENT_NONE)
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
Packit 4a16fb
	for (type = 0; type < numberof(status_event); type++) {
Packit 4a16fb
		if (ev->type == status_event[type].event)
Packit 4a16fb
			goto __found;
Packit 4a16fb
	}
Packit 4a16fb
	for (type = 0; type < numberof(extra_event); type++) {
Packit 4a16fb
		if (ev->type == extra_event[type].event)
Packit 4a16fb
			return extra_event[type].decode(dev, buf, count, ev);
Packit 4a16fb
	}
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
Packit 4a16fb
      __found:
Packit 4a16fb
	if (type >= ST_SPECIAL)
Packit 4a16fb
		cmd = 0xf0 + (type - ST_SPECIAL);
Packit 4a16fb
	else
Packit 4a16fb
		/* data.note.channel and data.control.channel is identical */
Packit 4a16fb
		cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f);
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
	if (cmd == MIDI_CMD_COMMON_SYSEX) {
Packit 4a16fb
		snd_midi_event_reset_decode(dev);
Packit 4a16fb
		qlen = ev->data.ext.len;
Packit 4a16fb
		if (count < qlen)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		switch (ev->flags & SND_SEQ_EVENT_LENGTH_MASK) {
Packit 4a16fb
		case SND_SEQ_EVENT_LENGTH_FIXED:
Packit 4a16fb
			return -EINVAL;	/* invalid event */
Packit 4a16fb
		}
Packit 4a16fb
		memcpy(buf, ev->data.ext.ptr, qlen);
Packit 4a16fb
		return qlen;
Packit 4a16fb
	} else {
Packit 4a16fb
		unsigned char xbuf[4];
Packit 4a16fb
Packit 4a16fb
		if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) {
Packit 4a16fb
			dev->lastcmd = cmd;
Packit 4a16fb
			xbuf[0] = cmd;
Packit 4a16fb
			if (status_event[type].decode)
Packit 4a16fb
				status_event[type].decode(ev, xbuf + 1);
Packit 4a16fb
			qlen = status_event[type].qlen + 1;
Packit 4a16fb
		} else {
Packit 4a16fb
			if (status_event[type].decode)
Packit 4a16fb
				status_event[type].decode(ev, xbuf + 0);
Packit 4a16fb
			qlen = status_event[type].qlen;
Packit 4a16fb
		}
Packit 4a16fb
		if (qlen <= 0)
Packit 4a16fb
			return 0;
Packit 4a16fb
		if (count < qlen)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		memcpy(buf, xbuf, qlen);
Packit 4a16fb
		return qlen;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
/* decode note event */
Packit 4a16fb
static void note_decode(const snd_seq_event_t *ev, unsigned char *buf)
Packit 4a16fb
{
Packit 4a16fb
	buf[0] = ev->data.note.note & 0x7f;
Packit 4a16fb
	buf[1] = ev->data.note.velocity & 0x7f;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* decode one parameter controls */
Packit 4a16fb
static void one_param_decode(const snd_seq_event_t *ev, unsigned char *buf)
Packit 4a16fb
{
Packit 4a16fb
	buf[0] = ev->data.control.value & 0x7f;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* decode pitch wheel change */
Packit 4a16fb
static void pitchbend_decode(const snd_seq_event_t *ev, unsigned char *buf)
Packit 4a16fb
{
Packit 4a16fb
	int value = ev->data.control.value + 8192;
Packit 4a16fb
	buf[0] = value & 0x7f;
Packit 4a16fb
	buf[1] = (value >> 7) & 0x7f;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* decode midi control change */
Packit 4a16fb
static void two_param_decode(const snd_seq_event_t *ev, unsigned char *buf)
Packit 4a16fb
{
Packit 4a16fb
	buf[0] = ev->data.control.param & 0x7f;
Packit 4a16fb
	buf[1] = ev->data.control.value & 0x7f;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* decode song position */
Packit 4a16fb
static void songpos_decode(const snd_seq_event_t *ev, unsigned char *buf)
Packit 4a16fb
{
Packit 4a16fb
	buf[0] = ev->data.control.value & 0x7f;
Packit 4a16fb
	buf[1] = (ev->data.control.value >> 7) & 0x7f;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* decode 14bit control */
Packit 4a16fb
static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	unsigned char cmd;
Packit 4a16fb
	int idx = 0;
Packit 4a16fb
Packit 4a16fb
	cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
Packit 4a16fb
	if (ev->data.control.param < 32) {
Packit 4a16fb
		if (count < 4)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		if (dev->nostat && count < 6)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		if (cmd != dev->lastcmd || dev->nostat) {
Packit 4a16fb
			if (count < 5)
Packit 4a16fb
				return -ENOMEM;
Packit 4a16fb
			buf[idx++] = dev->lastcmd = cmd;
Packit 4a16fb
		}
Packit 4a16fb
		buf[idx++] = ev->data.control.param;
Packit 4a16fb
		buf[idx++] = (ev->data.control.value >> 7) & 0x7f;
Packit 4a16fb
		if (dev->nostat)
Packit 4a16fb
			buf[idx++] = cmd;
Packit 4a16fb
		buf[idx++] = ev->data.control.param + 32;
Packit 4a16fb
		buf[idx++] = ev->data.control.value & 0x7f;
Packit 4a16fb
	} else {
Packit 4a16fb
		if (count < 2)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		if (cmd != dev->lastcmd || dev->nostat) {
Packit 4a16fb
			if (count < 3)
Packit 4a16fb
				return -ENOMEM;
Packit 4a16fb
			buf[idx++] = dev->lastcmd = cmd;
Packit 4a16fb
		}
Packit 4a16fb
		buf[idx++] = ev->data.control.param & 0x7f;
Packit 4a16fb
		buf[idx++] = ev->data.control.value & 0x7f;
Packit 4a16fb
	}
Packit 4a16fb
	return idx;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* decode reg/nonreg param */
Packit 4a16fb
static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, const snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	unsigned char cmd;
Packit 4a16fb
	const char *cbytes;
Packit 4a16fb
	static const char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB,
Packit 4a16fb
				       MIDI_CTL_NONREG_PARM_NUM_LSB,
Packit 4a16fb
				       MIDI_CTL_MSB_DATA_ENTRY,
Packit 4a16fb
				       MIDI_CTL_LSB_DATA_ENTRY };
Packit 4a16fb
	static const char cbytes_rpn[4] =  { MIDI_CTL_REGIST_PARM_NUM_MSB,
Packit 4a16fb
				       MIDI_CTL_REGIST_PARM_NUM_LSB,
Packit 4a16fb
				       MIDI_CTL_MSB_DATA_ENTRY,
Packit 4a16fb
				       MIDI_CTL_LSB_DATA_ENTRY };
Packit 4a16fb
	unsigned char bytes[4];
Packit 4a16fb
	int idx = 0, i;
Packit 4a16fb
Packit 4a16fb
	if (count < 8)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	if (dev->nostat && count < 12)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
Packit 4a16fb
	bytes[0] = (ev->data.control.param & 0x3f80) >> 7;
Packit 4a16fb
	bytes[1] = ev->data.control.param & 0x007f;
Packit 4a16fb
	bytes[2] = (ev->data.control.value & 0x3f80) >> 7;
Packit 4a16fb
	bytes[3] = ev->data.control.value & 0x007f;
Packit 4a16fb
	if (cmd != dev->lastcmd && !dev->nostat) {
Packit 4a16fb
		if (count < 9)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		buf[idx++] = dev->lastcmd = cmd;
Packit 4a16fb
	}
Packit 4a16fb
	cbytes = ev->type == SND_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn;
Packit 4a16fb
	for (i = 0; i < 4; i++) {
Packit 4a16fb
		if (dev->nostat)
Packit 4a16fb
			buf[idx++] = dev->lastcmd = cmd;
Packit 4a16fb
		buf[idx++] = cbytes[i];
Packit 4a16fb
		buf[idx++] = bytes[i];
Packit 4a16fb
	}
Packit 4a16fb
	return idx;
Packit 4a16fb
}