Fix some capitalisation in the preferences dialog.
[ardour.git] / libs / midi++2 / midi++ / parser.h
1 /*
2     Copyright (C) 1998 Paul Barton-Davis
3     
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef  __midi_parse_h__
21 #define  __midi_parse_h__
22
23 #include <string>
24 #include <iostream>
25
26 #include "pbd/signals.h"
27
28 #include "midi++/libmidi_visibility.h"
29 #include "midi++/types.h"
30
31 namespace MIDI {
32
33 class Port;
34 class Parser;
35
36 typedef PBD::Signal1<void,Parser&>                   ZeroByteSignal;
37 typedef PBD::Signal2<void,Parser&,framecnt_t>        TimestampedSignal;
38 typedef PBD::Signal2<void,Parser&, byte>             OneByteSignal;
39 typedef PBD::Signal2<void,Parser &, EventTwoBytes *> TwoByteSignal;
40 typedef PBD::Signal2<void,Parser &, pitchbend_t>     PitchBendSignal;
41 typedef PBD::Signal3<void,Parser &, byte *, size_t>  Signal;
42
43 class LIBMIDIPP_API Parser {
44  public:
45         Parser ();
46         ~Parser ();
47
48         /* sets the time that will be reported for any MTC or MIDI Clock
49            message the next time ::scanner() parses such a message. It should
50            therefore be set before every byte passed into ::scanner().
51         */
52         
53         framecnt_t get_timestamp() const { return _timestamp; }
54         void set_timestamp (const framecnt_t timestamp) { _timestamp = timestamp; } 
55
56         /* signals that anyone can connect to */
57         
58         OneByteSignal         bank_change;
59         TwoByteSignal         note_on;
60         TwoByteSignal         note_off;
61         TwoByteSignal         poly_pressure;
62         OneByteSignal         pressure;
63         OneByteSignal         program_change;
64         PitchBendSignal       pitchbend;
65         TwoByteSignal         controller;
66
67         OneByteSignal         channel_bank_change[16];
68         TwoByteSignal         channel_note_on[16];
69         TwoByteSignal         channel_note_off[16];
70         TwoByteSignal         channel_poly_pressure[16];
71         OneByteSignal         channel_pressure[16];
72         OneByteSignal         channel_program_change[16];
73         PitchBendSignal       channel_pitchbend[16];
74         TwoByteSignal         channel_controller[16];
75         ZeroByteSignal        channel_active_preparse[16];
76         ZeroByteSignal        channel_active_postparse[16];
77
78         OneByteSignal         mtc_quarter_frame; /* see below for more useful signals */
79         Signal                mtc;
80         Signal                raw_preparse;
81         Signal                raw_postparse;
82         Signal                any;
83         Signal                sysex;
84         Signal                mmc;
85         Signal                position;
86         Signal                song;
87
88         ZeroByteSignal        all_notes_off;
89         ZeroByteSignal        tune;
90         ZeroByteSignal        active_sense;
91         ZeroByteSignal        reset;
92         ZeroByteSignal        eox;
93
94         TimestampedSignal     timing;
95         TimestampedSignal     start;
96         TimestampedSignal     stop;
97         TimestampedSignal     contineu;  /* note spelling */
98
99         /* This should really be protected, but then derivatives of Port
100            can't access it.
101         */
102
103         void scanner (byte c);
104
105         size_t *message_counts() { return message_counter; }
106         const char *midi_event_type_name (MIDI::eventType);
107         void trace (bool onoff, std::ostream *o, const std::string &prefix = "");
108         bool tracing() { return trace_stream != 0; }
109
110         void set_offline (bool);
111         bool offline() const { return _offline; }
112         PBD::Signal0<void> OfflineStatusChanged;
113
114         PBD::Signal2<int,byte *, size_t> edit;
115
116         void set_mmc_forwarding (bool yn) {
117                 _mmc_forward = yn;
118         }
119
120         /* MTC */
121
122         MTC_FPS mtc_fps() const { return _mtc_fps; }
123         MTC_Status  mtc_running() const { return _mtc_running; }
124         const byte *mtc_current() const { return _mtc_time; }
125         bool        mtc_locked() const  { return _mtc_locked; }
126         
127         PBD::Signal3<void, Parser &, int, framecnt_t>      mtc_qtr;
128         PBD::Signal3<void, const byte *, bool, framecnt_t> mtc_time;
129         PBD::Signal1<void, MTC_Status>                     mtc_status;
130         PBD::Signal0<bool>                                 mtc_skipped;
131
132         void set_mtc_forwarding (bool yn) {
133                 _mtc_forward = yn;
134         }
135
136         void reset_mtc_state ();
137         
138   private:
139         /* tracing */
140         
141         std::ostream *trace_stream;
142         std::string trace_prefix;
143         void trace_event (Parser &p, byte *msg, size_t len);
144         PBD::ScopedConnection trace_connection;
145
146         size_t message_counter[256];
147
148         enum ParseState { 
149                 NEEDSTATUS,
150                 NEEDONEBYTE,
151                 NEEDTWOBYTES,
152                 VARIABLELENGTH
153         };
154         ParseState state;
155         unsigned char *msgbuf;
156         int msglen;
157         int msgindex;
158         MIDI::eventType msgtype;
159         channel_t channel;
160         bool _offline;
161         bool runnable;
162         bool was_runnable;
163         bool _mmc_forward;
164         bool _mtc_forward;
165         int   expected_mtc_quarter_frame_code;
166         byte _mtc_time[5];
167         byte _qtr_mtc_time[5];
168         unsigned long consecutive_qtr_frame_cnt;
169         MTC_FPS _mtc_fps;
170         MTC_Status _mtc_running;
171         bool       _mtc_locked;
172         byte last_qtr_frame;
173         
174         framecnt_t _timestamp;
175
176         ParseState pre_variable_state;
177         MIDI::eventType pre_variable_msgtype;
178         byte last_status_byte;
179
180         void channel_msg (byte);
181         void realtime_msg (byte);
182         void system_msg (byte);
183         void signal (byte *msg, size_t msglen);
184         bool possible_mmc (byte *msg, size_t msglen);
185         bool possible_mtc (byte *msg, size_t msglen);
186         void process_mtc_quarter_frame (byte *msg);
187 };
188
189 } // namespace MIDI
190
191 #endif   // __midi_parse_h__
192