2008-09-30 11:19:49 -04:00
|
|
|
/* This file is part of Evoral.
|
|
|
|
* Copyright(C) 2008 Dave Robillard <http://drobilla.net>
|
|
|
|
* Copyright(C) 2000-2008 Paul Davis
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2008-09-30 11:19:49 -04:00
|
|
|
* Evoral is free software; you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation; either version 2 of the License, or(at your option) any later
|
|
|
|
* version.
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2008-09-30 11:19:49 -04:00
|
|
|
* Evoral 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 General Public License for details.
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2008-09-30 11:19:49 -04:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EVORAL_MIDI_UTIL_H
|
|
|
|
#define EVORAL_MIDI_UTIL_H
|
|
|
|
|
2009-02-15 19:16:28 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2009-08-26 23:09:30 -04:00
|
|
|
#include <string>
|
|
|
|
#include <sys/types.h>
|
2009-02-03 03:46:24 -05:00
|
|
|
#include <assert.h>
|
2009-02-01 19:20:18 -05:00
|
|
|
#include "evoral/midi_events.h"
|
2008-09-30 11:19:49 -04:00
|
|
|
|
|
|
|
namespace Evoral {
|
|
|
|
|
2009-02-15 19:16:28 -05:00
|
|
|
|
2009-02-03 03:47:13 -05:00
|
|
|
/** Return the size of the given event including the status byte,
|
2009-02-15 15:31:05 -05:00
|
|
|
* or -1 if unknown (e.g. sysex)
|
2008-09-30 11:19:49 -04:00
|
|
|
*/
|
|
|
|
static inline int
|
2009-02-15 19:16:28 -05:00
|
|
|
midi_event_size(uint8_t status)
|
2008-09-30 11:19:49 -04:00
|
|
|
{
|
|
|
|
// if we have a channel event
|
|
|
|
if (status >= 0x80 && status < 0xF0) {
|
|
|
|
status &= 0xF0; // mask off the channel
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case MIDI_CMD_NOTE_OFF:
|
|
|
|
case MIDI_CMD_NOTE_ON:
|
|
|
|
case MIDI_CMD_NOTE_PRESSURE:
|
|
|
|
case MIDI_CMD_CONTROL:
|
|
|
|
case MIDI_CMD_BENDER:
|
|
|
|
case MIDI_CMD_COMMON_SONG_POS:
|
2009-02-03 03:46:24 -05:00
|
|
|
return 3;
|
2008-09-30 11:19:49 -04:00
|
|
|
|
|
|
|
case MIDI_CMD_PGM_CHANGE:
|
|
|
|
case MIDI_CMD_CHANNEL_PRESSURE:
|
|
|
|
case MIDI_CMD_COMMON_MTC_QUARTER:
|
|
|
|
case MIDI_CMD_COMMON_SONG_SELECT:
|
2009-02-03 03:46:24 -05:00
|
|
|
return 2;
|
2008-09-30 11:19:49 -04:00
|
|
|
|
|
|
|
case MIDI_CMD_COMMON_TUNE_REQUEST:
|
|
|
|
case MIDI_CMD_COMMON_SYSEX_END:
|
|
|
|
case MIDI_CMD_COMMON_CLOCK:
|
|
|
|
case MIDI_CMD_COMMON_START:
|
|
|
|
case MIDI_CMD_COMMON_CONTINUE:
|
|
|
|
case MIDI_CMD_COMMON_STOP:
|
|
|
|
case MIDI_CMD_COMMON_SENSING:
|
|
|
|
case MIDI_CMD_COMMON_RESET:
|
2009-02-03 03:46:24 -05:00
|
|
|
return 1;
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-30 11:19:49 -04:00
|
|
|
case MIDI_CMD_COMMON_SYSEX:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-15 19:36:11 -05:00
|
|
|
/** Return the size of the given event including the status byte,
|
|
|
|
* or -1 if event is illegal.
|
2009-02-03 03:46:24 -05:00
|
|
|
*/
|
|
|
|
static inline int
|
2009-02-15 19:36:11 -05:00
|
|
|
midi_event_size(const uint8_t* buffer)
|
2009-02-03 03:46:24 -05:00
|
|
|
{
|
|
|
|
uint8_t status = buffer[0];
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2009-02-15 19:36:11 -05:00
|
|
|
// Mask off channel if applicable
|
2009-02-03 03:46:24 -05:00
|
|
|
if (status >= 0x80 && status < 0xF0) {
|
2009-02-15 19:36:11 -05:00
|
|
|
status &= 0xF0;
|
2009-02-03 03:46:24 -05:00
|
|
|
}
|
2009-02-17 11:42:17 -05:00
|
|
|
|
|
|
|
// see http://www.midi.org/techspecs/midimessages.php
|
2009-02-03 03:46:24 -05:00
|
|
|
if (status == MIDI_CMD_COMMON_SYSEX) {
|
|
|
|
int end;
|
2009-02-17 11:42:17 -05:00
|
|
|
for (end = 1; buffer[end] != MIDI_CMD_COMMON_SYSEX_END; end++) {
|
|
|
|
assert((buffer[end] & 0x80) == 0);
|
|
|
|
}
|
2009-02-03 03:46:24 -05:00
|
|
|
assert(buffer[end] == MIDI_CMD_COMMON_SYSEX_END);
|
|
|
|
return end + 1;
|
|
|
|
} else {
|
|
|
|
return midi_event_size(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-15 19:36:11 -05:00
|
|
|
/** Return true iff the given buffer is a valid MIDI event.
|
|
|
|
* \a len must be exactly correct for the contents of \a buffer
|
|
|
|
*/
|
2009-02-15 19:16:28 -05:00
|
|
|
static inline bool
|
2009-02-15 19:36:11 -05:00
|
|
|
midi_event_is_valid(const uint8_t* buffer, size_t len)
|
2009-02-15 19:16:28 -05:00
|
|
|
{
|
|
|
|
uint8_t status = buffer[0];
|
|
|
|
if (status < 0x80) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-15 19:36:11 -05:00
|
|
|
const int size = midi_event_size(buffer);
|
|
|
|
if (size < 0 || (size_t)size != len) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-15 19:16:28 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-26 23:09:30 -04:00
|
|
|
std::string midi_note_name (uint8_t noteval);
|
2009-02-15 19:16:28 -05:00
|
|
|
|
2008-09-30 11:19:49 -04:00
|
|
|
} // namespace Evoral
|
|
|
|
|
|
|
|
#endif // EVORAL_MIDI_UTIL_H
|
|
|
|
|