ardour
midi_util.h
Go to the documentation of this file.
1 /* This file is part of Evoral.
2  * Copyright(C) 2008 David Robillard <http://drobilla.net>
3  * Copyright(C) 2000-2008 Paul Davis
4  *
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or(at your option) any later
8  * version.
9  *
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef EVORAL_MIDI_UTIL_H
20 #define EVORAL_MIDI_UTIL_H
21 
22 #include <iostream>
23 
24 #include <stdint.h>
25 #include <string>
26 #include <sys/types.h>
27 #include <assert.h>
28 
29 #include "evoral/visibility.h"
30 #include "evoral/midi_events.h"
31 
32 namespace Evoral {
33 
34 
38 static inline int
39 midi_event_size(uint8_t status)
40 {
41  // if we have a channel event
42  if (status >= 0x80 && status < 0xF0) {
43  status &= 0xF0; // mask off the channel
44  }
45 
46  switch (status) {
47  case MIDI_CMD_NOTE_OFF:
48  case MIDI_CMD_NOTE_ON:
50  case MIDI_CMD_CONTROL:
51  case MIDI_CMD_BENDER:
53  return 3;
54 
59  return 2;
60 
69  return 1;
70 
72  std::cerr << "event size called for sysex\n";
73  return -1;
74  }
75 
76  std::cerr << "event size called for unknown status byte " << std::hex << (int) status << "\n";
77  return -1;
78 }
79 
83 static inline int
84 midi_event_size(const uint8_t* buffer)
85 {
86  uint8_t status = buffer[0];
87 
88  // Mask off channel if applicable
89  if (status >= 0x80 && status < 0xF0) {
90  status &= 0xF0;
91  }
92 
93  // see http://www.midi.org/techspecs/midimessages.php
94  if (status == MIDI_CMD_COMMON_SYSEX) {
95  int end;
96 
97  for (end = 1; buffer[end] != MIDI_CMD_COMMON_SYSEX_END; end++) {
98  if ((buffer[end] & 0x80) != 0) {
99  return -1;
100  }
101  }
102  assert(buffer[end] == MIDI_CMD_COMMON_SYSEX_END);
103  return end + 1;
104  } else {
105  return midi_event_size(status);
106  }
107 }
108 
112 static inline bool
113 midi_event_is_valid(const uint8_t* buffer, size_t len)
114 {
115  uint8_t status = buffer[0];
116  if (status < 0x80) {
117  return false;
118  }
119  const int size = midi_event_size(buffer);
120  if (size < 0 || (size_t)size != len) {
121  return false;
122  }
123  if (status < 0xf0) {
124  /* Channel messages: all start with status byte followed by
125  * non status bytes.
126  */
127  for (size_t i = 1; i < len; ++i) {
128  if ((buffer[i] & 0x80) != 0) {
129  return false; // Non-status byte has MSb set
130  }
131  }
132  }
133  return true;
134 }
135 
136 LIBEVORAL_API std::string midi_note_name (uint8_t noteval);
137 
138 } // namespace Evoral
139 
140 #endif // EVORAL_MIDI_UTIL_H
141 
LIBEVORAL_API std::string midi_note_name(uint8_t noteval)
Definition: midi_util.cpp:25
static int midi_event_size(uint8_t status)
Definition: midi_util.h:39
#define MIDI_CMD_COMMON_CLOCK
Definition: midi_events.h:120
#define MIDI_CMD_NOTE_OFF
Definition: midi_events.h:107
#define MIDI_CMD_NOTE_ON
Definition: midi_events.h:108
#define MIDI_CMD_CHANNEL_PRESSURE
Definition: midi_events.h:112
static bool midi_event_is_valid(const uint8_t *buffer, size_t len)
Definition: midi_util.h:113
#define MIDI_CMD_PGM_CHANGE
Definition: midi_events.h:111
#define MIDI_CMD_COMMON_TUNE_REQUEST
Definition: midi_events.h:118
#define LIBEVORAL_API
Definition: visibility.h:45
#define MIDI_CMD_COMMON_SONG_POS
Definition: midi_events.h:116
#define MIDI_CMD_BENDER
Definition: midi_events.h:113
#define MIDI_CMD_COMMON_CONTINUE
Definition: midi_events.h:123
#define MIDI_CMD_CONTROL
Definition: midi_events.h:110
#define MIDI_CMD_COMMON_SENSING
Definition: midi_events.h:125
#define MIDI_CMD_COMMON_MTC_QUARTER
Definition: midi_events.h:115
#define MIDI_CMD_COMMON_RESET
Definition: midi_events.h:126
#define MIDI_CMD_COMMON_START
Definition: midi_events.h:122
#define MIDI_CMD_COMMON_SYSEX
Definition: midi_events.h:114
#define MIDI_CMD_COMMON_SONG_SELECT
Definition: midi_events.h:117
#define MIDI_CMD_NOTE_PRESSURE
Definition: midi_events.h:109
#define MIDI_CMD_COMMON_STOP
Definition: midi_events.h:124
#define MIDI_CMD_COMMON_SYSEX_END
Definition: midi_events.h:119