* fix comment
[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 <assert.h>
23
24 #include "evoral/midi_events.h"
25
26 namespace Evoral {
27
28 /** Return the size of the given event including the status byte,
29  * or -1 if unknown (eg sysex)
30  */
31 static inline int
32 midi_event_size(unsigned char status)
33 {
34         // if we have a channel event
35         if (status >= 0x80 && status < 0xF0) {
36                 status &= 0xF0; // mask off the channel
37         }
38
39         switch (status) {
40         case MIDI_CMD_NOTE_OFF:
41         case MIDI_CMD_NOTE_ON:
42         case MIDI_CMD_NOTE_PRESSURE:
43         case MIDI_CMD_CONTROL:
44         case MIDI_CMD_BENDER:
45         case MIDI_CMD_COMMON_SONG_POS:
46                 return 3;
47
48         case MIDI_CMD_PGM_CHANGE:
49         case MIDI_CMD_CHANNEL_PRESSURE:
50         case MIDI_CMD_COMMON_MTC_QUARTER:
51         case MIDI_CMD_COMMON_SONG_SELECT:
52                 return 2;
53
54         case MIDI_CMD_COMMON_TUNE_REQUEST:
55         case MIDI_CMD_COMMON_SYSEX_END:
56         case MIDI_CMD_COMMON_CLOCK:
57         case MIDI_CMD_COMMON_START:
58         case MIDI_CMD_COMMON_CONTINUE:
59         case MIDI_CMD_COMMON_STOP:
60         case MIDI_CMD_COMMON_SENSING:
61         case MIDI_CMD_COMMON_RESET:
62                 return 1;
63         
64         case MIDI_CMD_COMMON_SYSEX:
65                 return -1;
66         }
67
68         return -1;
69 }
70
71 /** Return the size of the given event including the status byte
72  * (which must be the first byte in \a buffer),
73  * or -1 if unknown (eg sysex)
74  */
75 static inline int
76 midi_event_size(uint8_t* buffer)
77 {
78         uint8_t status = buffer[0];
79         
80         // if we have a channel event
81         if (status >= 0x80 && status < 0xF0) {
82                 status &= 0xF0; // mask off the channel
83         }
84         
85         if (status == MIDI_CMD_COMMON_SYSEX) {
86                 int end;
87                 for (end = 1; buffer[end] != MIDI_CMD_COMMON_SYSEX_END; end++);
88                 assert(buffer[end] == MIDI_CMD_COMMON_SYSEX_END);
89                 return end + 1;
90         } else {
91                 return midi_event_size(status);
92         }
93 }
94
95 } // namespace Evoral
96
97 #endif // EVORAL_MIDI_UTIL_H
98