<commit_before>/**
 * \file seq/seq_midi_event.c
 * \brief MIDI byte <-> sequencer event coder
 * \author <NAME> <<EMAIL>>
 * \author <NAME> <<EMAIL>>
 * \date 2000-2001
 */

/*
 *  MIDI byte <-> sequencer event coder
 *
 *  Copyright (C) 1998,99,2000 <NAME> <<EMAIL>>,
 *			       <NAME> <<EMAIL>>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <malloc.h>
#include "local.h"

#ifndef DOC_HIDDEN

/* midi status */
struct snd_midi_event {
	size_t qlen;	/* queue length */
	size_t read;	/* chars read */
	int type;	/* current event type */
	unsigned char lastcmd;
	size_t bufsize;
	unsigned char *buf;	/* input buffer */
};


/* queue type */
/* from 0 to 7 are normal commands (note off, on, etc.) */
#define ST_NOTEOFF	0
#define ST_NOTEON	1
#define ST_SPECIAL	8
#define ST_SYSEX	ST_SPECIAL
/* from 8 to 15 are events for 0xf0-0xf7 */


/* status event types */
typedef void (*event_encode_t)(snd_midi_event_t *dev, snd_seq_event_t *ev);
typedef void (*event_decode_t)(snd_seq_event_t *ev, unsigned char *buf);

#endif /* DOC_HIDDEN */

/*
 * prototypes
 */
static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void note_decode(snd_seq_event_t *ev, unsigned char *buf);
static void one_param_decode(snd_seq_event_t *ev, unsigned char *buf);
static void pitchbend_decode(snd_seq_event_t *ev, unsigned char *buf);
static void two_param_decode(snd_seq_event_t *ev, unsigned char *buf);
static void songpos_decode(snd_seq_event_t *ev, unsigned char *buf);

/*
 * event list
 */
static struct status_event_list_t {
	int event;
	int qlen;
	event_encode_t encode;
	event_decode_t decode;
} status_event[] = {
	/* 0x80 - 0xf0 */
	{SND_SEQ_EVENT_NOTEOFF,		2, note_event, note_decode},
	{SND_SEQ_EVENT_NOTEON,		2, note_event, note_decode},
	{SND_SEQ_EVENT_KEYPRESS,	2, note_event, note_decode},
	{SND_SEQ_EVENT_CONTROLLER,	2, two_param_ctrl_event, two_param_decode},
	{SND_SEQ_EVENT_PGMCHANGE,	1, one_param_ctrl_event, one_param_decode},
	{SND_SEQ_EVENT_CHANPRESS,	1, one_param_ctrl_event, one_param_decode},
	{SND_SEQ_EVENT_PITCHBEND,	2, pitchbend_ctrl_event, pitchbend_decode},
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf0 */
	/* 0xf0 - 0xff */
	{SND_SEQ_EVENT_SYSEX,		1, NULL, NULL}, /* sysex: 0xf0 */
	{SND_SEQ_EVENT_QFRAME,		1, one_param_event, one_param_decode}, /* 0xf1 */
	{SND_SEQ_EVENT_SONGPOS,		2, songpos_event, songpos_decode}, /* 0xf2 */
	{SND_SEQ_EVENT_SONGSEL,		1, one_param_event, one_param_decode}, /* 0xf3 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf4 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf5 */
	{SND_SEQ_EVENT_TUNE_REQUEST,	0, NULL, NULL},	/* 0xf6 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf7 */
	{SND_SEQ_EVENT_CLOCK,		0, NULL, NULL}, /* 0xf8 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf9 */
	{SND_SEQ_EVENT_START,		0, NULL, NULL}, /* 0xfa */
	{SND_SEQ_EVENT_CONTINUE,	0, NULL, NULL}, /* 0xfb */
	{SND_SEQ_EVENT_STOP, 		0, NULL, NULL}, /* 0xfc */
	{SND_SEQ_EVENT_NONE, 		0, NULL, NULL}, /* 0xfd */
	{SND_SEQ_EVENT_SENSING, 	0, NULL, NULL}, /* 0xfe */
	{SND_SEQ_EVENT_RESET, 		0, NULL, NULL}, /* 0xff */
};

static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);

static struct extra_event_list_t {
	int event;
	int (*decode)(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);
} extra_event[] = {
	{SND_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
	/*{SND_SEQ_EVENT_NONREGPARAM, extra_decode_nrpn},*/
	/*{SND_SEQ_EVENT_REGPARAM, extra_decode_rpn},*/
};

#ifndef DOC_HIDDEN
#define numberof(ary)	(sizeof(ary)/sizeof(ary[0]))
#endif

/**
 * \brief Initialize MIDI event parser
 * \param bufsize buffer size for MIDI message
 * \param rdev allocated MIDI event parser
 * \return 0 on success otherwise a negative error code
 *
 * Allocates and initializes MIDI event parser.
 */
int snd_midi_event_new(size_t bufsize, snd_midi_event_t **rdev)
{
	snd_midi_event_t *dev;

	*rdev = NULL;
	dev = (snd_midi_event_t *)malloc(sizeof(snd_midi_event_t));
	if (dev == NULL)
		return -ENOMEM;
	if (bufsize > 0) {
		dev->buf = malloc(bufsize);
		if (dev->buf == NULL) {
			free(dev);
			return -ENOMEM;
		}
	}
	dev->bufsize = bufsize;
	*rdev = dev;
	return 0;
}

/**
 * \brief Free MIDI event parser
 * \param rdev MIDI event parser
 * \return 0 on success otherwise a negative error code
 *
 * Frees MIDI event parser.
 */
void snd_midi_event_free(snd_midi_event_t *dev)
{
	if (dev != NULL) {
		if (dev->buf)
			free(dev->buf);
		free(dev);
	}
}

/*
 * initialize record
 */
inline static void reset_encode(snd_midi_event_t *dev)
{
	dev->read = 0;
	dev->qlen = 0;<commit_msg>Fixed initialization of snd_midi_event_t structure seq_midi_event_t<commit_after>/**
 * \file seq/seq_midi_event.c
 * \brief MIDI byte <-> sequencer event coder
 * \author <NAME> <<EMAIL>>
 * \author <NAME> <<EMAIL>>
 * \date 2000-2001
 */

/*
 *  MIDI byte <-> sequencer event coder
 *
 *  Copyright (C) 1998,99,2000 <NAME> <<EMAIL>>,
 *			       <NAME> <<EMAIL>>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <malloc.h>
#include "local.h"

#ifndef DOC_HIDDEN

/* midi status */
struct snd_midi_event {
	size_t qlen;	/* queue length */
	size_t read;	/* chars read */
	int type;	/* current event type */
	unsigned char lastcmd;
	size_t bufsize;
	unsigned char *buf;	/* input buffer */
};


/* queue type */
/* from 0 to 7 are normal commands (note off, on, etc.) */
#define ST_NOTEOFF	0
#define ST_NOTEON	1
#define ST_SPECIAL	8
#define ST_SYSEX	ST_SPECIAL
/* from 8 to 15 are events for 0xf0-0xf7 */


/* status event types */
typedef void (*event_encode_t)(snd_midi_event_t *dev, snd_seq_event_t *ev);
typedef void (*event_decode_t)(snd_seq_event_t *ev, unsigned char *buf);

#endif /* DOC_HIDDEN */

/*
 * prototypes
 */
static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
static void note_decode(snd_seq_event_t *ev, unsigned char *buf);
static void one_param_decode(snd_seq_event_t *ev, unsigned char *buf);
static void pitchbend_decode(snd_seq_event_t *ev, unsigned char *buf);
static void two_param_decode(snd_seq_event_t *ev, unsigned char *buf);
static void songpos_decode(snd_seq_event_t *ev, unsigned char *buf);

/*
 * event list
 */
static struct status_event_list_t {
	int event;
	int qlen;
	event_encode_t encode;
	event_decode_t decode;
} status_event[] = {
	/* 0x80 - 0xf0 */
	{SND_SEQ_EVENT_NOTEOFF,		2, note_event, note_decode},
	{SND_SEQ_EVENT_NOTEON,		2, note_event, note_decode},
	{SND_SEQ_EVENT_KEYPRESS,	2, note_event, note_decode},
	{SND_SEQ_EVENT_CONTROLLER,	2, two_param_ctrl_event, two_param_decode},
	{SND_SEQ_EVENT_PGMCHANGE,	1, one_param_ctrl_event, one_param_decode},
	{SND_SEQ_EVENT_CHANPRESS,	1, one_param_ctrl_event, one_param_decode},
	{SND_SEQ_EVENT_PITCHBEND,	2, pitchbend_ctrl_event, pitchbend_decode},
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf0 */
	/* 0xf0 - 0xff */
	{SND_SEQ_EVENT_SYSEX,		1, NULL, NULL}, /* sysex: 0xf0 */
	{SND_SEQ_EVENT_QFRAME,		1, one_param_event, one_param_decode}, /* 0xf1 */
	{SND_SEQ_EVENT_SONGPOS,		2, songpos_event, songpos_decode}, /* 0xf2 */
	{SND_SEQ_EVENT_SONGSEL,		1, one_param_event, one_param_decode}, /* 0xf3 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf4 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf5 */
	{SND_SEQ_EVENT_TUNE_REQUEST,	0, NULL, NULL},	/* 0xf6 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf7 */
	{SND_SEQ_EVENT_CLOCK,		0, NULL, NULL}, /* 0xf8 */
	{SND_SEQ_EVENT_NONE,		0, NULL, NULL}, /* 0xf9 */
	{SND_SEQ_EVENT_START,		0, NULL, NULL}, /* 0xfa */
	{SND_SEQ_EVENT_CONTINUE,	0, NULL, NULL}, /* 0xfb */
	{SND_SEQ_EVENT_STOP, 		0, NULL, NULL}, /* 0xfc */
	{SND_SEQ_EVENT_NONE, 		0, NULL, NULL}, /* 0xfd */
	{SND_SEQ_EVENT_SENSING, 	0, NULL, NULL}, /* 0xfe */
	{SND_SEQ_EVENT_RESET, 		0, NULL, NULL}, /* 0xff */
};

static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);

static struct extra_event_list_t {
	int event;
	int (*decode)(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);
} extra_event[] = {
	{SND_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
	/*{SND_SEQ_EVENT_NONREGPARAM, extra_decode_nrpn},*/
	/*{SND_SEQ_EVENT_REGPARAM, extra_decode_rpn},*/
};

#ifndef DOC_HIDDEN
#define numberof(ary)	(sizeof(ary)/sizeof(ary[0]))
#endif

/**
 * \brief Initialize MIDI event parser
 * \param bufsize buffer size for MIDI message
 * \param rdev allocated MIDI event parser
 * \return 0 on success otherwise a negative error code
 *
 * Allocates and initializes MIDI event parser.
 */
int snd_midi_event_new(size_t bufsize, snd_midi_event_t **rdev)
{
	snd_midi_event_t *dev;

	*rdev = NULL;
	dev = (snd_midi_event_t *)calloc(1, sizeof(snd_midi_event_t));
	if (dev == NULL)
		return -ENOMEM;
	if (bufsize > 0) {
		dev->buf = malloc(bufsize);
		if (dev->buf == NULL) {
			free(dev);
			return -ENOMEM;
		}
	}
	dev->bufsize = bufsize;
	dev->lastcmd = 0xff;
	*rdev = dev;
	return 0;
}

/**
 * \brief Free MIDI event parser
 * \param rdev MIDI event parser
 * \return 0 on success otherwise a negative error code
 *
 * Frees MIDI event parser.
 */
void snd_midi_event_free(snd_midi_event_t *dev)
{
	if (dev != NULL) {
		if (dev->buf)
			free(dev->buf);
		free(dev);
	}
}

/*
 * initialize record
 */
inline static void reset_encode(snd_midi_event_t *dev)
{
	dev->read = 0;
	dev->qlen = 0;
{'fraction_non_alphanumeric': 0.12106685261047677, 'fraction_numerical': 0.020918678636799443, 'mean_word_length': 4.484703632887189, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7453606', 'n_tokens_mistral': 4704, 'n_tokens_neox': 4445, 'n_words': 1173}