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