Optimize plugin-processing for non-automated params
[ardour.git] / libs / ardour / ardour / raw_midi_parser.h
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 #ifndef _ardour_raw_midi_parser_h_
21 #define _ardour_raw_midi_parser_h_
22
23 #include "ardour/libardour_visibility.h"
24 #include "ardour/types.h"
25
26 namespace ARDOUR {
27
28 class LIBARDOUR_API RawMidiParser
29 {
30 public:
31         RawMidiParser ();
32
33         void reset () {
34                 _event_size = 0;
35                 _unbuffered_bytes = 0;
36                 _total_bytes = 0;
37                 _expected_bytes = 0;
38                 _status_byte = 0;
39         }
40
41         uint8_t const * midi_buffer () const { return  _parser_buffer; }
42         size_t buffer_size () const { return _event_size; }
43
44         /** parse a MIDI byte
45          * @return true if message is complete, false if more data is needed
46          */
47         bool process_byte (const uint8_t byte);
48
49 private:
50
51         void record_byte (uint8_t byte) {
52                 if (_total_bytes < sizeof (_parser_buffer)) {
53                         _parser_buffer[_total_bytes] = byte;
54                 } else {
55                         ++_unbuffered_bytes;
56                 }
57                 ++_total_bytes;
58         }
59
60         void prepare_byte_event (const uint8_t byte) {
61                 _parser_buffer[0] = byte;
62                 _event_size = 1;
63         }
64
65         bool prepare_buffered_event () {
66                 const bool result = _unbuffered_bytes == 0;
67                 if (result) {
68                         _event_size = _total_bytes;
69                 }
70                 _total_bytes = 0;
71                 _unbuffered_bytes = 0;
72                 if (_status_byte >= 0xf0) {
73                         _expected_bytes = 0;
74                         _status_byte = 0;
75                 }
76                 return result;
77         }
78
79         size_t  _event_size;
80         size_t  _unbuffered_bytes;
81         size_t  _total_bytes;
82         size_t  _expected_bytes;
83         uint8_t _status_byte;
84         uint8_t _parser_buffer[1024];
85 };
86
87 } // namespace ARDOUR
88
89 #endif /* _ardour_raw_midi_parser_h_ */