Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[ardour.git] / libs / evoral / evoral / midi_util.h
1 /* This file is part of Evoral.
2  * Copyright(C) 2008 Dave 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 <stdint.h>
23 #include <stdbool.h>
24 #include <string>
25 #include <sys/types.h>
26 #include <assert.h>
27 #include "evoral/midi_events.h"
28
29 namespace Evoral {
30
31
32 /** Return the size of the given event including the status byte,
33  * or -1 if unknown (e.g. sysex)
34  */
35 static inline int
36 midi_event_size(uint8_t status)
37 {
38         // if we have a channel event
39         if (status >= 0x80 && status < 0xF0) {
40                 status &= 0xF0; // mask off the channel
41         }
42
43         switch (status) {
44         case MIDI_CMD_NOTE_OFF:
45         case MIDI_CMD_NOTE_ON:
46         case MIDI_CMD_NOTE_PRESSURE:
47         case MIDI_CMD_CONTROL:
48         case MIDI_CMD_BENDER:
49         case MIDI_CMD_COMMON_SONG_POS:
50                 return 3;
51
52         case MIDI_CMD_PGM_CHANGE:
53         case MIDI_CMD_CHANNEL_PRESSURE:
54         case MIDI_CMD_COMMON_MTC_QUARTER:
55         case MIDI_CMD_COMMON_SONG_SELECT:
56                 return 2;
57
58         case MIDI_CMD_COMMON_TUNE_REQUEST:
59         case MIDI_CMD_COMMON_SYSEX_END:
60         case MIDI_CMD_COMMON_CLOCK:
61         case MIDI_CMD_COMMON_START:
62         case MIDI_CMD_COMMON_CONTINUE:
63         case MIDI_CMD_COMMON_STOP:
64         case MIDI_CMD_COMMON_SENSING:
65         case MIDI_CMD_COMMON_RESET:
66                 return 1;
67
68         case MIDI_CMD_COMMON_SYSEX:
69                 return -1;
70         }
71
72         return -1;
73 }
74
75 /** Return the size of the given event including the status byte,
76  * or -1 if event is illegal.
77  */
78 static inline int
79 midi_event_size(const uint8_t* buffer)
80 {
81         uint8_t status = buffer[0];
82
83         // Mask off channel if applicable
84         if (status >= 0x80 && status < 0xF0) {
85                 status &= 0xF0;
86         }
87
88         // see http://www.midi.org/techspecs/midimessages.php
89         if (status == MIDI_CMD_COMMON_SYSEX) {
90                 int end;
91                 for (end = 1; buffer[end] != MIDI_CMD_COMMON_SYSEX_END; end++) {
92                         assert((buffer[end] & 0x80) == 0);
93                 }
94                 assert(buffer[end] == MIDI_CMD_COMMON_SYSEX_END);
95                 return end + 1;
96         } else {
97                 return midi_event_size(status);
98         }
99 }
100
101 /** Return true iff the given buffer is a valid MIDI event.
102  * \a len must be exactly correct for the contents of \a buffer
103  */
104 static inline bool
105 midi_event_is_valid(const uint8_t* buffer, size_t len)
106 {
107         uint8_t status = buffer[0];
108         if (status < 0x80) {
109                 return false;
110         }
111         const int size = midi_event_size(buffer);
112         if (size < 0 || (size_t)size != len) {
113                 return false;
114         }
115         return true;
116 }
117
118 std::string midi_note_name (uint8_t noteval);
119
120 } // namespace Evoral
121
122 #endif // EVORAL_MIDI_UTIL_H
123