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