Remove ambiguous API implementation
[ardour.git] / libs / ardour / raw_midi_parser.cc
1 /*
2  * Copyright (C) 2014,2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2010 Devin Anderson
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "ardour/raw_midi_parser.h"
21
22 using namespace ARDOUR;
23
24 RawMidiParser::RawMidiParser ()
25 {
26         reset ();
27 }
28
29 /* based on AlsaRawMidiIn, some code-dup */
30 bool
31 RawMidiParser::process_byte (const uint8_t byte)
32 {
33         if (byte >= 0xf8) {
34                 // Realtime
35                 if (byte == 0xfd) {
36                         return false;
37                 }
38                 prepare_byte_event (byte);
39                 return true;
40         }
41         if (byte == 0xf7) {
42                 // Sysex end
43                 if (_status_byte == 0xf0) {
44                         record_byte (byte);
45                         return prepare_buffered_event ();
46                 }
47                 _total_bytes = 0;
48                 _unbuffered_bytes = 0;
49                 _expected_bytes = 0;
50                 _status_byte = 0;
51                 return false;
52         }
53         if (byte >= 0x80) {
54                 // Non-realtime status byte
55                 if (_total_bytes) {
56                         _total_bytes = 0;
57                         _unbuffered_bytes = 0;
58                 }
59                 _status_byte = byte;
60                 switch (byte & 0xf0) {
61                         case 0x80:
62                         case 0x90:
63                         case 0xa0:
64                         case 0xb0:
65                         case 0xe0:
66                                 // Note On, Note Off, Aftertouch, Control Change, Pitch Wheel
67                                 _expected_bytes = 3;
68                                 break;
69                         case 0xc0:
70                         case 0xd0:
71                                 // Program Change, Channel Pressure
72                                 _expected_bytes = 2;
73                                 break;
74                         case 0xf0:
75                                 switch (byte) {
76                                         case 0xf0:
77                                                 // Sysex
78                                                 _expected_bytes = 0;
79                                                 break;
80                                         case 0xf1:
81                                         case 0xf3:
82                                                 // MTC Quarter Frame, Song Select
83                                                 _expected_bytes = 2;
84                                                 break;
85                                         case 0xf2:
86                                                 // Song Position
87                                                 _expected_bytes = 3;
88                                                 break;
89                                         case 0xf4:
90                                         case 0xf5:
91                                                 // Undefined
92                                                 _expected_bytes = 0;
93                                                 _status_byte = 0;
94                                                 return false;
95                                         case 0xf6:
96                                                 // Tune Request
97                                                 prepare_byte_event (byte);
98                                                 _expected_bytes = 0;
99                                                 _status_byte = 0;
100                                                 return true;
101                                 }
102                 }
103                 record_byte (byte);
104                 return false;
105         }
106         // Data byte
107         if (!_status_byte) {
108                 // Data bytes without a status will be discarded.
109                 _total_bytes++;
110                 _unbuffered_bytes++;
111                 return false;
112         }
113         if (!_total_bytes) {
114                 record_byte (_status_byte);
115         }
116         record_byte (byte);
117         return (_total_bytes == _expected_bytes) ? prepare_buffered_event () : false;
118 }