Rename libmusictime libtimecode (consistent with already used namespace "Timecode").
[ardour.git] / libs / midi++2 / midi++ / mmc.h
1 /*
2     Copyright (C) 2000 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 __midipp_mmc_h_h__
21 #define __midipp_mmc_h_h__
22
23 #include <jack/types.h>
24 #include "timecode/time.h"
25 #include "pbd/signals.h"
26 #include "pbd/ringbuffer.h"
27 #include "midi++/types.h"
28 #include "midi++/parser.h"
29
30 namespace MIDI {
31
32 class Port;
33 class Parser;
34 class MachineControlCommand;
35 class Manager;  
36
37 /** Class to handle incoming and outgoing MIDI machine control messages */
38 class MachineControl 
39 {
40   public:
41         typedef PBD::Signal1<void,MachineControl&> MMCSignal;
42
43         enum Command {
44                 cmdStop = 0x1,
45                 cmdPlay = 0x2,
46                 cmdDeferredPlay = 0x3,
47                 cmdFastForward = 0x4,
48                 cmdRewind = 0x5,
49                 cmdRecordStrobe = 0x6,
50
51                 cmdRecordExit = 0x7,
52                 cmdRecordPause = 0x8,
53                 cmdPause = 0x9,
54                 cmdEject = 0xA,
55                 cmdChase = 0xB,
56                 cmdCommandErrorReset = 0xC,
57                 cmdMmcReset = 0xD,
58                 
59                 cmdIllegalMackieJogStart = 0x20,
60                 cmdIllegalMackieJogStop = 0x21,
61                 
62                 cmdWrite = 0x40,
63                 cmdMaskedWrite = 0x41,
64                 cmdRead = 0x42,
65                 cmdUpdate = 0x43,
66                 cmdLocate = 0x44,
67                 cmdVariablePlay = 0x45,
68                 cmdSearch = 0x46,
69
70                 cmdShuttle = 0x47,
71                 cmdStep = 0x48,
72                 cmdAssignSystemMaster = 0x49,
73                 cmdGeneratorCommand = 0x4A,
74                 cmdMtcCommand = 0x4B,
75                 cmdMove = 0x4C,
76                 cmdAdd = 0x4D,
77
78                 cmdSubtract = 0x4E,
79                 cmdDropFrameAdjust = 0x4F,
80                 cmdProcedure = 0x50,
81                 cmdEvent = 0x51,
82                 cmdGroup = 0x52,
83                 cmdCommandSegment = 0x53,
84                 cmdDeferredVariablePlay = 0x54,
85
86                 cmdRecordStrobeVariable = 0x55,
87
88                 cmdWait = 0x7C,
89                 cmdResume = 0x7F
90         };
91         
92         MachineControl (Manager *, jack_client_t *);
93
94         Port* input_port() { return _input_port; }
95         Port* output_port() { return _output_port; }
96         
97         void set_receive_device_id (byte id);
98         void set_send_device_id (byte id);
99         byte receive_device_id () const { return _receive_device_id; }
100         byte send_device_id () const { return _send_device_id; }
101         void enable_send (bool);
102         void send (MachineControlCommand const &);
103
104         static bool is_mmc (byte *sysex_buf, size_t len);
105
106         /* Signals to connect to if you want to run "callbacks"
107            when certain MMC commands are received.
108         */
109                         
110         MMCSignal Stop;
111         MMCSignal Play;
112         MMCSignal DeferredPlay;
113         MMCSignal FastForward;
114         MMCSignal Rewind;
115         MMCSignal RecordStrobe;
116         MMCSignal RecordExit;
117         MMCSignal RecordPause;
118         MMCSignal Pause;
119         MMCSignal Eject;
120         MMCSignal Chase;
121         MMCSignal CommandErrorReset;
122         MMCSignal MmcReset;
123         MMCSignal JogStart;
124         MMCSignal JogStop;
125         MMCSignal Write;
126         MMCSignal MaskedWrite;
127         MMCSignal Read;
128         MMCSignal Update;
129         MMCSignal VariablePlay;
130         MMCSignal Search;
131         MMCSignal AssignSystemMaster;
132         MMCSignal GeneratorCommand;
133         MMCSignal MidiTimeCodeCommand;
134         MMCSignal Move;
135         MMCSignal Add;
136         MMCSignal Subtract;
137         MMCSignal DropFrameAdjust;
138         MMCSignal Procedure;
139         MMCSignal Event;
140         MMCSignal Group;
141         MMCSignal CommandSegment;
142         MMCSignal DeferredVariablePlay;
143         MMCSignal RecordStrobeVariable;
144         MMCSignal Wait;
145         MMCSignal Resume;
146
147         TimestampedSignal SPPStart;
148         TimestampedSignal SPPContinue;
149         TimestampedSignal SPPStop;
150
151         /* The second argument is the shuttle speed, the third is
152            true if the direction is "forwards", false for "reverse"
153         */
154         
155         PBD::Signal3<void,MachineControl&,float,bool> Shuttle;
156
157         /* The second argument specifies the desired track record enabled
158            status.
159         */
160
161         PBD::Signal3<void,MachineControl &,size_t,bool> 
162                                              TrackRecordStatusChange;
163         
164         /* The second argument specifies the desired track record enabled
165            status.
166         */
167
168         PBD::Signal3<void,MachineControl &,size_t,bool> 
169                                              TrackMuteChange;
170         
171         /* The second argument points to a byte array containing
172            the locate target value in MMC Standard Time Code
173            format (5 bytes, roughly: hrs/mins/secs/frames/subframes)
174         */
175
176         PBD::Signal2<void,MachineControl &, const byte *> Locate;
177
178         /* The second argument is the number of steps to jump */
179         
180         PBD::Signal2<void,MachineControl &, int> Step;
181
182 #define MMC_NTRACKS 48
183
184         /* note: these are not currently in use */
185         
186         byte updateRate;
187         byte responseError;
188         byte commandError;
189         byte commandErrorLevel;
190
191         byte motionControlTally;
192         byte velocityTally;
193         byte stopMode;
194         byte fastMode;
195         byte recordMode;
196         byte recordStatus;
197         bool trackRecordStatus[MMC_NTRACKS];
198         bool trackRecordReady[MMC_NTRACKS];
199         byte globalMonitor;
200         byte recordMonitor;
201         byte trackSyncMonitor;
202         byte trackInputMonitor;
203         byte stepLength;
204         byte playSpeedReference;
205         byte fixedSpeed;
206         byte lifterDefeat;
207         byte controlDisable;
208         byte trackMute[MMC_NTRACKS];
209         byte failure;
210         byte selectedTimeCode;
211         byte shortSelectedTimeCode;
212         byte timeStandard;
213         byte selectedTimeCodeSource;
214         byte selectedTimeCodeUserbits;
215         byte selectedMasterCode;
216         byte requestedOffset;
217         byte actualOffset;
218         byte lockDeviation;
219         byte shortSelectedMasterCode;
220         byte shortRequestedOffset;
221         byte shortActualOffset;
222         byte shortLockDeviation;
223         byte resolvedPlayMode;
224         byte chaseMode;
225         byte generatorTimeCode;
226         byte shortGeneratorTimeCode;
227         byte generatorCommandTally;
228         byte generatorSetUp;
229         byte generatorUserbits;
230         byte vitcInsertEnable;
231         byte midiTimeCodeInput;
232         byte shortMidiTimeCodeInput;
233         byte midiTimeCodeCommandTally;
234         byte midiTimeCodeSetUp;
235         byte gp0;
236         byte gp1;
237         byte gp2;
238         byte gp3;
239         byte gp4;
240         byte gp5;
241         byte gp6;
242         byte gp7;
243         byte shortGp0;
244         byte shortGp1;
245         byte shortGp2;
246         byte shortGp3;
247         byte shortGp4;
248         byte shortGp5;
249         byte shortGp6;
250         byte shortGp7;
251         byte procedureResponse;
252         byte eventResponse;
253         byte responseSegment;
254         byte wait;
255         byte resume;
256         
257   private:
258         byte _receive_device_id;
259         byte _send_device_id;
260         Port* _input_port;
261         Port* _output_port;
262         bool _enable_send; ///< true if MMC sending is enabled
263
264         void process_mmc_message (Parser &p, byte *, size_t len);
265         PBD::ScopedConnectionList port_connections; ///< connections to our parser for incoming data
266
267         int  do_masked_write (byte *, size_t len);
268         int  do_locate (byte *, size_t len);
269         int  do_step (byte *, size_t len);
270         int  do_shuttle (byte *, size_t len);
271         
272         void write_track_status (byte *, size_t len, byte reg);
273         void spp_start (Parser&, framecnt_t);
274         void spp_continue (Parser&, framecnt_t);
275         void spp_stop (Parser&, framecnt_t);
276 };
277
278 /** Class to describe a MIDI machine control command to be sent.
279  *  In an ideal world we might use a class hierarchy for this, but objects of this type
280  *  have to be allocated off the stack for RT safety.
281  */
282 class MachineControlCommand
283 {
284 public:
285         MachineControlCommand () : _command (MachineControl::Command (0)) {}
286         MachineControlCommand (MachineControl::Command);
287         MachineControlCommand (Timecode::Time);
288         
289         MIDI::byte* fill_buffer (MachineControl *mmc, MIDI::byte *) const;
290
291 private:
292         MachineControl::Command _command;
293         Timecode::Time _time;
294 };
295
296 } // namespace MIDI
297
298 #endif /* __midipp_mmc_h_h__ */