Blame profiles/midi/libmidi.h

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2015,2016 Felipe F. Tonello <eu@felipetonello.com>
Packit 34410b
 *  Copyright (C) 2016 ROLI Ltd.
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifndef LIBMIDI_H
Packit 34410b
#define LIBMIDI_H
Packit 34410b
Packit 34410b
#include <stdint.h>
Packit 34410b
#include <stdbool.h>
Packit 34410b
#include <alsa/asoundlib.h>
Packit 34410b
Packit 34410b
#define MIDI_UUID "03B80E5A-EDE8-4B33-A751-6CE34EC4C700"
Packit 34410b
#define MIDI_IO_UUID "7772E5DB-3868-4112-A1A9-F2669D106BF3"
Packit 34410b
Packit 34410b
#define MIDI_MAX_TIMESTAMP 8191
Packit 34410b
#define MIDI_MSG_MAX_SIZE 12
Packit 34410b
#define MIDI_SYSEX_MAX_SIZE (4 * 1024)
Packit 34410b
Packit 34410b
struct midi_buffer {
Packit 34410b
	uint8_t *data;
Packit 34410b
	size_t len;
Packit 34410b
};
Packit 34410b
Packit 34410b
/* MIDI I/O Write parser */
Packit 34410b
Packit 34410b
struct midi_write_parser {
Packit 34410b
	int64_t rtime;                  /* last writer's real time */
Packit 34410b
	snd_seq_event_type_t rstatus;   /* running status event type */
Packit 34410b
	struct midi_buffer midi_stream; /* MIDI I/O byte stream */
Packit 34410b
	size_t stream_size;             /* what is the maximum size of the midi_stream array */
Packit 34410b
	snd_midi_event_t *midi_ev;      /* midi<->seq event */
Packit 34410b
};
Packit 34410b
Packit 34410b
int midi_write_init(struct midi_write_parser *parser, size_t buffer_size);
Packit 34410b
Packit 34410b
static inline void midi_write_free(struct midi_write_parser *parser)
Packit 34410b
{
Packit 34410b
	free(parser->midi_stream.data);
Packit 34410b
	snd_midi_event_free(parser->midi_ev);
Packit 34410b
}
Packit 34410b
Packit 34410b
static inline void midi_write_reset(struct midi_write_parser *parser)
Packit 34410b
{
Packit 34410b
	parser->rstatus = SND_SEQ_EVENT_NONE;
Packit 34410b
	parser->midi_stream.len = 0;
Packit 34410b
}
Packit 34410b
Packit 34410b
static inline bool midi_write_has_data(const struct midi_write_parser *parser)
Packit 34410b
{
Packit 34410b
	return parser->midi_stream.len > 0;
Packit 34410b
}
Packit 34410b
Packit 34410b
static inline const uint8_t * midi_write_data(const struct midi_write_parser *parser)
Packit 34410b
{
Packit 34410b
	return parser->midi_stream.data;
Packit 34410b
}
Packit 34410b
Packit 34410b
static inline size_t midi_write_data_size(const struct midi_write_parser *parser)
Packit 34410b
{
Packit 34410b
	return parser->midi_stream.len;
Packit 34410b
}
Packit 34410b
Packit 34410b
typedef void (*midi_read_ev_cb)(const struct midi_write_parser *parser, void *);
Packit 34410b
Packit 34410b
/* It creates BLE-MIDI raw packets from the a sequencer event. If the packet
Packit 34410b
   is full, then it calls write_cb and resets its internal state as many times
Packit 34410b
   as necessary.
Packit 34410b
 */
Packit 34410b
void midi_read_ev(struct midi_write_parser *parser, const snd_seq_event_t *ev,
Packit 34410b
                  midi_read_ev_cb write_cb, void *user_data);
Packit 34410b
Packit 34410b
/* MIDI I/O Read parser */
Packit 34410b
Packit 34410b
struct midi_read_parser {
Packit 34410b
	uint8_t rstatus;                 /* running status byte */
Packit 34410b
	int64_t rtime;                   /* last reader's real time */
Packit 34410b
	int16_t timestamp;               /* last MIDI-BLE timestamp */
Packit 34410b
	int8_t timestamp_low;            /* MIDI-BLE timestampLow from the current packet */
Packit 34410b
	int8_t timestamp_high;           /* MIDI-BLE timestampHigh from the current packet */
Packit 34410b
	struct midi_buffer sysex_stream; /* SysEx stream */
Packit 34410b
	snd_midi_event_t *midi_ev;       /* midi<->seq event */
Packit 34410b
};
Packit 34410b
Packit 34410b
int midi_read_init(struct midi_read_parser *parser);
Packit 34410b
Packit 34410b
static inline void midi_read_free(struct midi_read_parser *parser)
Packit 34410b
{
Packit 34410b
	free(parser->sysex_stream.data);
Packit 34410b
	snd_midi_event_free(parser->midi_ev);
Packit 34410b
}
Packit 34410b
Packit 34410b
static inline void midi_read_reset(struct midi_read_parser *parser)
Packit 34410b
{
Packit 34410b
	parser->rstatus = 0;
Packit 34410b
	parser->timestamp_low = 0;
Packit 34410b
	parser->timestamp_high = 0;
Packit 34410b
}
Packit 34410b
Packit 34410b
/* Parses raw BLE-MIDI messages and populates a sequencer event representing the
Packit 34410b
   current MIDI message. It returns how much raw data was processed.
Packit 34410b
 */
Packit 34410b
size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data,
Packit 34410b
                     size_t size, snd_seq_event_t *ev /* OUT */);
Packit 34410b
Packit 34410b
#endif /* LIBMIDI_H */