Fix deadlock and potential race condition when editing MIDI.
[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 <assert.h>
25 #include "evoral/midi_events.h"
26
27 namespace Evoral {
28
29
30 /** Return the size of the given event including the status byte,
31  * or -1 if unknown (e.g. sysex)
32  */
33 static inline int
34 midi_event_size(uint8_t status)
35 {
36         // if we have a channel event
37         if (status >= 0x80 && status < 0xF0) {
38                 status &= 0xF0; // mask off the channel
39         }
40
41         switch (status) {
42         case MIDI_CMD_NOTE_OFF:
43         case MIDI_CMD_NOTE_ON:
44         case MIDI_CMD_NOTE_PRESSURE:
45         case MIDI_CMD_CONTROL:
46         case MIDI_CMD_BENDER:
47         case MIDI_CMD_COMMON_SONG_POS:
48                 return 3;
49
50         case MIDI_CMD_PGM_CHANGE:
51         case MIDI_CMD_CHANNEL_PRESSURE:
52         case MIDI_CMD_COMMON_MTC_QUARTER:
53         case MIDI_CMD_COMMON_SONG_SELECT:
54                 return 2;
55
56         case MIDI_CMD_COMMON_TUNE_REQUEST:
57         case MIDI_CMD_COMMON_SYSEX_END:
58         case MIDI_CMD_COMMON_CLOCK:
59         case MIDI_CMD_COMMON_START:
60         case MIDI_CMD_COMMON_CONTINUE:
61         case MIDI_CMD_COMMON_STOP:
62         case MIDI_CMD_COMMON_SENSING:
63         case MIDI_CMD_COMMON_RESET:
64                 return 1;
65         
66         case MIDI_CMD_COMMON_SYSEX:
67                 return -1;
68         }
69
70         return -1;
71 }
72
73 /** Return the size of the given event including the status byte,
74  * or -1 if event is illegal.
75  */
76 static inline int
77 midi_event_size(const uint8_t* buffer)
78 {
79         uint8_t status = buffer[0];
80         
81         // Mask off channel if applicable
82         if (status >= 0x80 && status < 0xF0) {
83                 status &= 0xF0;
84         }
85         
86         // FIXME: This is not correct, read the size and verify
87         // A sysex can contain the byte MIDI_CMD_COMMON_SYSEX_END, so this
88         // is likely to result in corrupt buffers and catastrophic failure
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] == MIDI_CMD_COMMON_SYSEX_END);
93                 return end + 1;
94         } else {
95                 return midi_event_size(status);
96         }
97 }
98
99 /** Return true iff the given buffer is a valid MIDI event.
100  * \a len must be exactly correct for the contents of \a buffer
101  */
102 static inline bool
103 midi_event_is_valid(const uint8_t* buffer, size_t len)
104 {
105         uint8_t status = buffer[0];
106         if (status < 0x80) {
107                 return false;
108         }
109         const int size = midi_event_size(buffer);
110         if (size < 0 || (size_t)size != len) {
111                 return false;
112         }
113         return true;
114 }
115
116
117 } // namespace Evoral
118
119 #endif // EVORAL_MIDI_UTIL_H
120