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