Move MIDI control port ownership into the MIDI Manager, since control port state...
[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 "control_protocol/timecode.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* output_port() { return _output_port; }
95         
96         void set_receive_device_id (byte id);
97         void set_send_device_id (byte id);
98         byte receive_device_id () const { return _receive_device_id; }
99         byte send_device_id () const { return _send_device_id; }
100         void enable_send (bool);
101         void send (MachineControlCommand const &);
102
103         static bool is_mmc (byte *sysex_buf, size_t len);
104
105         /* Signals to connect to if you want to run "callbacks"
106            when certain MMC commands are received.
107         */
108                         
109         MMCSignal Stop;
110         MMCSignal Play;
111         MMCSignal DeferredPlay;
112         MMCSignal FastForward;
113         MMCSignal Rewind;
114         MMCSignal RecordStrobe;
115         MMCSignal RecordExit;
116         MMCSignal RecordPause;
117         MMCSignal Pause;
118         MMCSignal Eject;
119         MMCSignal Chase;
120         MMCSignal CommandErrorReset;
121         MMCSignal MmcReset;
122         MMCSignal JogStart;
123         MMCSignal JogStop;
124         MMCSignal Write;
125         MMCSignal MaskedWrite;
126         MMCSignal Read;
127         MMCSignal Update;
128         MMCSignal VariablePlay;
129         MMCSignal Search;
130         MMCSignal AssignSystemMaster;
131         MMCSignal GeneratorCommand;
132         MMCSignal MidiTimeCodeCommand;
133         MMCSignal Move;
134         MMCSignal Add;
135         MMCSignal Subtract;
136         MMCSignal DropFrameAdjust;
137         MMCSignal Procedure;
138         MMCSignal Event;
139         MMCSignal Group;
140         MMCSignal CommandSegment;
141         MMCSignal DeferredVariablePlay;
142         MMCSignal RecordStrobeVariable;
143         MMCSignal Wait;
144         MMCSignal Resume;
145
146         TimestampedSignal SPPStart;
147         TimestampedSignal SPPContinue;
148         TimestampedSignal SPPStop;
149
150         /* The second argument is the shuttle speed, the third is
151            true if the direction is "forwards", false for "reverse"
152         */
153         
154         PBD::Signal3<void,MachineControl&,float,bool> Shuttle;
155
156         /* The second argument specifies the desired track record enabled
157            status.
158         */
159
160         PBD::Signal3<void,MachineControl &,size_t,bool> 
161                                              TrackRecordStatusChange;
162         
163         /* The second argument specifies the desired track record enabled
164            status.
165         */
166
167         PBD::Signal3<void,MachineControl &,size_t,bool> 
168                                              TrackMuteChange;
169         
170         /* The second argument points to a byte array containing
171            the locate target value in MMC Standard Time Code
172            format (5 bytes, roughly: hrs/mins/secs/frames/subframes)
173         */
174
175         PBD::Signal2<void,MachineControl &, const byte *> Locate;
176
177         /* The second argument is the number of steps to jump */
178         
179         PBD::Signal2<void,MachineControl &, int> Step;
180
181 #define MMC_NTRACKS 48
182
183         /* note: these are not currently in use */
184         
185         byte updateRate;
186         byte responseError;
187         byte commandError;
188         byte commandErrorLevel;
189
190         byte motionControlTally;
191         byte velocityTally;
192         byte stopMode;
193         byte fastMode;
194         byte recordMode;
195         byte recordStatus;
196         bool trackRecordStatus[MMC_NTRACKS];
197         bool trackRecordReady[MMC_NTRACKS];
198         byte globalMonitor;
199         byte recordMonitor;
200         byte trackSyncMonitor;
201         byte trackInputMonitor;
202         byte stepLength;
203         byte playSpeedReference;
204         byte fixedSpeed;
205         byte lifterDefeat;
206         byte controlDisable;
207         byte trackMute[MMC_NTRACKS];
208         byte failure;
209         byte selectedTimeCode;
210         byte shortSelectedTimeCode;
211         byte timeStandard;
212         byte selectedTimeCodeSource;
213         byte selectedTimeCodeUserbits;
214         byte selectedMasterCode;
215         byte requestedOffset;
216         byte actualOffset;
217         byte lockDeviation;
218         byte shortSelectedMasterCode;
219         byte shortRequestedOffset;
220         byte shortActualOffset;
221         byte shortLockDeviation;
222         byte resolvedPlayMode;
223         byte chaseMode;
224         byte generatorTimeCode;
225         byte shortGeneratorTimeCode;
226         byte generatorCommandTally;
227         byte generatorSetUp;
228         byte generatorUserbits;
229         byte vitcInsertEnable;
230         byte midiTimeCodeInput;
231         byte shortMidiTimeCodeInput;
232         byte midiTimeCodeCommandTally;
233         byte midiTimeCodeSetUp;
234         byte gp0;
235         byte gp1;
236         byte gp2;
237         byte gp3;
238         byte gp4;
239         byte gp5;
240         byte gp6;
241         byte gp7;
242         byte shortGp0;
243         byte shortGp1;
244         byte shortGp2;
245         byte shortGp3;
246         byte shortGp4;
247         byte shortGp5;
248         byte shortGp6;
249         byte shortGp7;
250         byte procedureResponse;
251         byte eventResponse;
252         byte responseSegment;
253         byte wait;
254         byte resume;
255         
256   private:
257         byte _receive_device_id;
258         byte _send_device_id;
259         Port* _input_port;
260         Port* _output_port;
261         bool _enable_send; ///< true if MMC sending is enabled
262
263         void process_mmc_message (Parser &p, byte *, size_t len);
264         PBD::ScopedConnectionList port_connections; ///< connections to our parser for incoming data
265
266         int  do_masked_write (byte *, size_t len);
267         int  do_locate (byte *, size_t len);
268         int  do_step (byte *, size_t len);
269         int  do_shuttle (byte *, size_t len);
270         
271         void write_track_status (byte *, size_t len, byte reg);
272         void spp_start (Parser&, nframes_t);
273         void spp_continue (Parser&, nframes_t);
274         void spp_stop (Parser&, nframes_t);
275 };
276
277 /** Class to describe a MIDI machine control command to be sent.
278  *  In an ideal world we might use a class hierarchy for this, but objects of this type
279  *  have to be allocated off the stack for RT safety.
280  */
281 class MachineControlCommand
282 {
283 public:
284         MachineControlCommand () : _command (MachineControl::Command (0)) {}
285         MachineControlCommand (MachineControl::Command);
286         MachineControlCommand (Timecode::Time);
287         
288         MIDI::byte* fill_buffer (MachineControl *mmc, MIDI::byte *) const;
289
290 private:
291         MachineControl::Command _command;
292         Timecode::Time _time;
293 };
294
295 } // namespace MIDI
296
297 #endif /* __midipp_mmc_h_h__ */