Merge branch 'master' into windows
[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++/types.h"
29
30 namespace MIDI {
31
32 class Port;
33 class Parser;
34
35 typedef PBD::Signal1<void,Parser&>                   ZeroByteSignal;
36 typedef PBD::Signal2<void,Parser&,framecnt_t>        TimestampedSignal;
37 typedef PBD::Signal2<void,Parser&, byte>             OneByteSignal;
38 typedef PBD::Signal2<void,Parser &, EventTwoBytes *> TwoByteSignal;
39 typedef PBD::Signal2<void,Parser &, pitchbend_t>     PitchBendSignal;
40 typedef PBD::Signal3<void,Parser &, byte *, size_t>  Signal;
41
42 class Parser {
43  public:
44         Parser ();
45         ~Parser ();
46
47         /* sets the time that will be reported for any MTC or MIDI Clock
48            message the next time ::scanner() parses such a message. It should
49            therefore be set before every byte passed into ::scanner().
50         */
51         
52         framecnt_t get_timestamp() const { return _timestamp; }
53         void set_timestamp (const framecnt_t timestamp) { _timestamp = timestamp; } 
54
55         /* signals that anyone can connect to */
56         
57         OneByteSignal         bank_change;
58         TwoByteSignal         note_on;
59         TwoByteSignal         note_off;
60         TwoByteSignal         poly_pressure;
61         OneByteSignal         pressure;
62         OneByteSignal         program_change;
63         PitchBendSignal       pitchbend;
64         TwoByteSignal         controller;
65
66         OneByteSignal         channel_bank_change[16];
67         TwoByteSignal         channel_note_on[16];
68         TwoByteSignal         channel_note_off[16];
69         TwoByteSignal         channel_poly_pressure[16];
70         OneByteSignal         channel_pressure[16];
71         OneByteSignal         channel_program_change[16];
72         PitchBendSignal       channel_pitchbend[16];
73         TwoByteSignal         channel_controller[16];
74         ZeroByteSignal        channel_active_preparse[16];
75         ZeroByteSignal        channel_active_postparse[16];
76
77         OneByteSignal         mtc_quarter_frame; /* see below for more useful signals */
78         Signal                mtc;
79         Signal                raw_preparse;
80         Signal                raw_postparse;
81         Signal                any;
82         Signal                sysex;
83         Signal                mmc;
84         Signal                position;
85         Signal                song;
86
87         ZeroByteSignal        all_notes_off;
88         ZeroByteSignal        tune;
89         ZeroByteSignal        active_sense;
90         ZeroByteSignal        reset;
91         ZeroByteSignal        eox;
92
93         TimestampedSignal     timing;
94         TimestampedSignal     start;
95         TimestampedSignal     stop;
96         TimestampedSignal     contineu;  /* note spelling */
97
98         /* This should really be protected, but then derivatives of Port
99            can't access it.
100         */
101
102         void scanner (byte c);
103
104         size_t *message_counts() { return message_counter; }
105         const char *midi_event_type_name (MIDI::eventType);
106         void trace (bool onoff, std::ostream *o, const std::string &prefix = "");
107         bool tracing() { return trace_stream != 0; }
108
109         void set_offline (bool);
110         bool offline() const { return _offline; }
111         PBD::Signal0<void> OfflineStatusChanged;
112
113         PBD::Signal2<int,byte *, size_t> edit;
114
115         void set_mmc_forwarding (bool yn) {
116                 _mmc_forward = yn;
117         }
118
119         /* MTC */
120
121         MTC_FPS mtc_fps() const { return _mtc_fps; }
122         MTC_Status  mtc_running() const { return _mtc_running; }
123         const byte *mtc_current() const { return _mtc_time; }
124         bool        mtc_locked() const  { return _mtc_locked; }
125         
126         PBD::Signal3<void, Parser &, int, framecnt_t>      mtc_qtr;
127         PBD::Signal3<void, const byte *, bool, framecnt_t> mtc_time;
128         PBD::Signal1<void, MTC_Status>                     mtc_status;
129         PBD::Signal0<bool>                                 mtc_skipped;
130
131         void set_mtc_forwarding (bool yn) {
132                 _mtc_forward = yn;
133         }
134
135         void reset_mtc_state ();
136         
137   private:
138         /* tracing */
139         
140         std::ostream *trace_stream;
141         std::string trace_prefix;
142         void trace_event (Parser &p, byte *msg, size_t len);
143         PBD::ScopedConnection trace_connection;
144
145         size_t message_counter[256];
146
147         enum ParseState { 
148                 NEEDSTATUS,
149                 NEEDONEBYTE,
150                 NEEDTWOBYTES,
151                 VARIABLELENGTH
152         };
153         ParseState state;
154         unsigned char *msgbuf;
155         int msglen;
156         int msgindex;
157         MIDI::eventType msgtype;
158         channel_t channel;
159         bool _offline;
160         bool runnable;
161         bool was_runnable;
162         bool _mmc_forward;
163         bool _mtc_forward;
164         int   expected_mtc_quarter_frame_code;
165         byte _mtc_time[5];
166         byte _qtr_mtc_time[5];
167         unsigned long consecutive_qtr_frame_cnt;
168         MTC_FPS _mtc_fps;
169         MTC_Status _mtc_running;
170         bool       _mtc_locked;
171         byte last_qtr_frame;
172         
173         framecnt_t _timestamp;
174
175         ParseState pre_variable_state;
176         MIDI::eventType pre_variable_msgtype;
177         byte last_status_byte;
178
179         void channel_msg (byte);
180         void realtime_msg (byte);
181         void system_msg (byte);
182         void signal (byte *msg, size_t msglen);
183         bool possible_mmc (byte *msg, size_t msglen);
184         bool possible_mtc (byte *msg, size_t msglen);
185         void process_mtc_quarter_frame (byte *msg);
186 };
187
188 } // namespace MIDI
189
190 #endif   // __midi_parse_h__
191