fix merge conflicts from master
[ardour.git] / libs / midi++2 / mmc.cc
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     $Id$
19 */
20
21 #include <fcntl.h>
22 #include <map>
23
24 #include "timecode/time.h"
25 #include "pbd/error.h"
26 #include "midi++/mmc.h"
27 #include "midi++/port.h"
28 #include "midi++/jack_midi_port.h"
29 #include "midi++/parser.h"
30 #include "midi++/manager.h"
31
32 #ifndef __INT_MAX__   // 'ssize_t' won't be defined yet
33 typedef long ssize_t;
34 #endif
35
36 using namespace std;
37 using namespace MIDI;
38 using namespace PBD;
39
40 static std::map<int,string> mmc_cmd_map;
41 static void build_mmc_cmd_map ()
42 {
43         pair<int,string> newpair;
44
45         newpair.first = 0x1;
46         newpair.second = "Stop";
47         mmc_cmd_map.insert (newpair);
48
49         newpair.first = 0x2;
50         newpair.second = "Play";
51         mmc_cmd_map.insert (newpair);
52
53         newpair.first = 0x3;
54         newpair.second = "DeferredPlay";
55         mmc_cmd_map.insert (newpair);
56
57         newpair.first = 0x4;
58         newpair.second = "FastForward";
59         mmc_cmd_map.insert (newpair);
60
61         newpair.first = 0x5;
62         newpair.second = "Rewind";
63         mmc_cmd_map.insert (newpair);
64
65         newpair.first = 0x6;
66         newpair.second = "RecordStrobe";
67         mmc_cmd_map.insert (newpair);
68
69         newpair.first = 0x7;
70         newpair.second = "RecordExit";
71         mmc_cmd_map.insert (newpair);
72
73         newpair.first = 0x8;
74         newpair.second = "RecordPause";
75         mmc_cmd_map.insert (newpair);
76
77         newpair.first = 0x9;
78         newpair.second = "Pause";
79         mmc_cmd_map.insert (newpair);
80
81         newpair.first = 0xA;
82         newpair.second = "Eject";
83         mmc_cmd_map.insert (newpair);
84
85         newpair.first = 0xB;
86         newpair.second = "Chase";
87         mmc_cmd_map.insert (newpair);
88
89         newpair.first = 0xC;
90         newpair.second = "CommandErrorReset";
91         mmc_cmd_map.insert (newpair);
92
93         newpair.first = 0xD;
94         newpair.second = "MmcReset";
95         mmc_cmd_map.insert (newpair);
96
97         newpair.first = 0x20;
98         newpair.second = "Illegal Mackie Jog Start";
99         mmc_cmd_map.insert (newpair);
100
101         newpair.first = 0x21;
102         newpair.second = "Illegal Mackie Jog Stop";
103         mmc_cmd_map.insert (newpair);
104
105         newpair.first = 0x40;
106         newpair.second = "Write";
107         mmc_cmd_map.insert (newpair);
108
109         newpair.first = 0x41;
110         newpair.second = "MaskedWrite";
111         mmc_cmd_map.insert (newpair);
112
113         newpair.first = 0x42;
114         newpair.second = "Read";
115         mmc_cmd_map.insert (newpair);
116
117         newpair.first = 0x43;
118         newpair.second = "Update";
119         mmc_cmd_map.insert (newpair);
120
121         newpair.first = 0x44;
122         newpair.second = "Locate";
123         mmc_cmd_map.insert (newpair);
124
125         newpair.first = 0x45;
126         newpair.second = "VariablePlay";
127         mmc_cmd_map.insert (newpair);
128
129         newpair.first = 0x46;
130         newpair.second = "Search";
131         mmc_cmd_map.insert (newpair);
132
133         newpair.first = 0x47;
134         newpair.second = "Shuttle";
135         mmc_cmd_map.insert (newpair);
136
137         newpair.first = 0x48;
138         newpair.second = "Step";
139         mmc_cmd_map.insert (newpair);
140
141         newpair.first = 0x49;
142         newpair.second = "AssignSystemMaster";
143         mmc_cmd_map.insert (newpair);
144
145         newpair.first = 0x4A;
146         newpair.second = "GeneratorCommand";
147         mmc_cmd_map.insert (newpair);
148
149         newpair.first = 0x4B;
150         newpair.second = "MtcCommand";
151         mmc_cmd_map.insert (newpair);
152
153         newpair.first = 0x4C;
154         newpair.second = "Move";
155         mmc_cmd_map.insert (newpair);
156
157         newpair.first = 0x4D;
158         newpair.second = "Add";
159         mmc_cmd_map.insert (newpair);
160
161         newpair.first = 0x4E;
162         newpair.second = "Subtract";
163         mmc_cmd_map.insert (newpair);
164
165         newpair.first = 0x4F;
166         newpair.second = "DropFrameAdjust";
167         mmc_cmd_map.insert (newpair);
168
169         newpair.first = 0x50;
170         newpair.second = "Procedure";
171         mmc_cmd_map.insert (newpair);
172
173         newpair.first = 0x51;
174         newpair.second = "Event";
175         mmc_cmd_map.insert (newpair);
176
177         newpair.first = 0x52;
178         newpair.second = "Group";
179         mmc_cmd_map.insert (newpair);
180
181         newpair.first = 0x53;
182         newpair.second = "CommandSegment";
183         mmc_cmd_map.insert (newpair);
184
185         newpair.first = 0x54;
186         newpair.second = "DeferredVariablePlay";
187         mmc_cmd_map.insert (newpair);
188
189         newpair.first = 0x55;
190         newpair.second = "RecordStrobeVariable";
191         mmc_cmd_map.insert (newpair);
192
193         newpair.first = 0x7C;
194         newpair.second = "Wait";
195         mmc_cmd_map.insert (newpair);
196
197         newpair.first = 0x7F;
198         newpair.second = "Resume";
199         mmc_cmd_map.insert (newpair);
200 }
201
202
203 MachineControl::MachineControl (Manager* m, jack_client_t* jack)
204 {
205         build_mmc_cmd_map ();
206
207         _receive_device_id = 0x7f;
208         _send_device_id = 0x7f;
209
210         _input_port = m->add_port (new JackMIDIPort ("MMC in", Port::IsInput, jack));
211         _output_port = m->add_port (new JackMIDIPort ("MMC out", Port::IsOutput, jack));
212
213         _input_port->parser()->mmc.connect_same_thread (port_connections, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
214         _input_port->parser()->start.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_start, this));
215         _input_port->parser()->contineu.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_continue, this));
216         _input_port->parser()->stop.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_stop, this));
217 }
218
219 void
220 MachineControl::set_receive_device_id (MIDI::byte id)
221 {
222         _receive_device_id = id & 0x7f;
223 }
224
225 void
226 MachineControl::set_send_device_id (MIDI::byte id)
227 {
228         _send_device_id = id & 0x7f;
229 }
230
231 bool
232 MachineControl::is_mmc (MIDI::byte *sysex_buf, size_t len)
233 {
234         if (len < 4 || len > 48) {
235                 return false;
236         }
237
238         if (sysex_buf[1] != 0x7f) {
239                 return false;
240         }
241
242         if (sysex_buf[3] != 0x6 && /* MMC Command */
243             sysex_buf[3] != 0x7) { /* MMC Response */
244                 return false;
245         }
246         
247         return true;
248 }
249
250 void
251 MachineControl::process_mmc_message (Parser &, MIDI::byte *msg, size_t len)
252 {
253         size_t skiplen;
254         byte *mmc_msg;
255         bool single_byte;
256
257         /* Reject if its not for us. 0x7f is the "all-call" device ID */
258
259         /* msg[0] = 0x7f (MMC sysex ID(
260            msg[1] = device ID
261            msg[2] = 0x6 (MMC command) or 0x7 (MMC response)
262            msg[3] = MMC command code
263            msg[4] = (typically) byte count for following part of command
264         */
265
266 #if 0
267         cerr << "*** me = " << (int) _receive_device_id << " MMC message: len = " << len << "\n\t";
268         for (size_t i = 0; i < len; i++) {
269                 cerr << hex << (int) msg[i] << dec << ' ';
270         }
271         cerr << endl;
272 #endif
273
274         if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
275                 return;
276         }
277
278         mmc_msg = &msg[3];
279         len -= 3;
280
281         do {
282
283                 single_byte = false;
284
285                 /* this works for all non-single-byte "counted"
286                    commands. we set it to 1 for the exceptions.
287                 */
288
289                 std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
290                 string cmdname = "unknown";
291
292                 if (x != mmc_cmd_map.end()) {
293                         cmdname = (*x).second;
294                 }
295
296 #if 0
297                 cerr << "+++ MMC type " 
298                      << hex
299                      << ((int) *mmc_msg)
300                      << dec
301                      << " \"" << cmdname << "\" "
302                      << " len = " << len
303                      << endl;
304 #endif
305
306                 switch (*mmc_msg) {
307
308                 /* SINGLE-BYTE, UNCOUNTED COMMANDS */
309
310                 case cmdStop:
311                         Stop (*this);
312                         single_byte = true;
313                         break;
314
315                 case cmdPlay:
316                         Play (*this);
317                         single_byte = true;
318                         break;
319
320                 case cmdDeferredPlay:
321                         DeferredPlay (*this);
322                         single_byte = true;
323                         break;
324
325                 case cmdFastForward:
326                         FastForward (*this);
327                         single_byte = true;
328                         break;
329
330                 case cmdRewind:
331                         Rewind (*this);
332                         single_byte = true;
333                         break;
334
335                 case cmdRecordStrobe:
336                         RecordStrobe (*this);
337                         single_byte = true;
338                         break;
339
340                 case cmdRecordExit:
341                         RecordExit (*this);
342                         single_byte = true;
343                         break;
344
345                 case cmdRecordPause:
346                         RecordPause (*this);
347                         single_byte = true;
348                         break;
349
350                 case cmdPause:
351                         Pause (*this);
352                         single_byte = true;
353                         break;
354
355                 case cmdEject:
356                         Eject (*this);
357                         single_byte = true;
358                         break;
359
360                 case cmdChase:
361                         Chase (*this);
362                         single_byte = true;
363                         break;
364
365                 case cmdCommandErrorReset:
366                         CommandErrorReset (*this);
367                         single_byte = true;
368                         break;
369
370                 case cmdMmcReset:
371                         MmcReset (*this);
372                         single_byte = true;
373                         break;
374
375                 case cmdIllegalMackieJogStart:
376                         JogStart (*this);
377                         single_byte = true;
378                         break;
379
380                 case cmdIllegalMackieJogStop:
381                         JogStop (*this);
382                         single_byte = true;
383                         break;
384
385                 /* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
386
387                 case cmdMaskedWrite:
388                         do_masked_write (mmc_msg, len);
389                         break;
390
391                 case cmdLocate:
392                         do_locate (mmc_msg, len);
393                         break;
394
395                 case cmdShuttle:
396                         do_shuttle (mmc_msg, len);
397                         break;
398
399                 case cmdStep:
400                         do_step (mmc_msg, len);
401                         break;
402
403                 case cmdWrite:
404                 case cmdRead:
405                 case cmdUpdate:
406                 case cmdVariablePlay:
407                 case cmdSearch:
408                 case cmdAssignSystemMaster:
409                 case cmdGeneratorCommand:
410                 case cmdMtcCommand:
411                 case cmdMove:
412                 case cmdAdd:
413                 case cmdSubtract:
414                 case cmdDropFrameAdjust:
415                 case cmdProcedure:
416                 case cmdEvent:
417                 case cmdGroup:
418                 case cmdCommandSegment:
419                 case cmdDeferredVariablePlay:
420                 case cmdRecordStrobeVariable:
421                 case cmdWait:
422                 case cmdResume:
423                         error << "MIDI::MachineControl: unimplemented MMC command "
424                               << hex << (int) *mmc_msg << dec
425                               << endmsg;
426
427                         break;
428
429                 default:
430                         error << "MIDI::MachineControl: unknown MMC command "
431                               << hex << (int) *mmc_msg << dec
432                               << endmsg;
433
434                         break;
435                 }
436
437                 /* increase skiplen to cover the command byte and 
438                    count byte (if it existed).
439                 */
440
441                 if (!single_byte) {
442                         skiplen = mmc_msg[1] + 2;
443                 } else {
444                         skiplen = 1;
445                 }
446
447                 if (len <= skiplen) {
448                         break;
449                 }
450
451                 mmc_msg += skiplen;
452                 len -= skiplen;
453
454         } while (len > 1); /* skip terminating EOX byte */
455 }               
456
457 int
458 MachineControl::do_masked_write (MIDI::byte *msg, size_t len)
459 {
460         /* return the number of bytes "consumed" */
461
462         int retval = msg[1] + 2; /* bytes following + 2 */
463         
464         switch (msg[2]) {
465         case 0x4f:  /* Track Record Ready Status */
466                 write_track_status (&msg[3], len - 3, msg[2]);
467                 break;
468
469         case 0x62: /* track mute */
470                 write_track_status (&msg[3], len - 3, msg[2]);
471                 break;
472
473         default:
474                 warning << "MIDI::MachineControl: masked write to "
475                         << hex << (int) msg[2] << dec
476                         << " not implemented"
477                         << endmsg;
478         }
479
480         return retval;
481 }
482
483 void
484 MachineControl::write_track_status (MIDI::byte *msg, size_t /*len*/, MIDI::byte reg)
485 {
486         size_t n;
487         ssize_t base_track;
488
489         /* Bits 0-4 of the first byte are for special tracks:
490
491            bit 0: video
492            bit 1: reserved
493            bit 2: time code
494            bit 3: aux track a
495            bit 4: aux track b
496
497            the format of the message (its an MMC Masked Write) is:
498            
499            0x41      Command Code
500            <count>   byte count of following data
501            <name>    byte value of the field being written
502            <byte #>  byte number of target byte in the 
503            bitmap being written to
504            <mask>    ones in the mask indicate which bits will be changed
505            <data>    new data for the byte being written
506            
507            by the time this code is executing, msg[0] is the
508            byte number of the target byte. if its zero, we
509            are writing to a special byte in the standard
510            track bitmap, in which the first 5 bits are
511            special. hence the bits for tracks 1 + 2 are bits
512            5 and 6 of the first byte of the track
513            bitmap. so:
514            
515            change track 1:  msg[0] = 0;       << first byte of track bitmap 
516                             msg[1] = 0100000; << binary: bit 5 set
517         
518            change track 2:  msg[0] = 0;       << first byte of track bitmap
519                             msg[1] = 1000000; << binary: bit 6 set
520         
521            change track 3:  msg[0] = 1;       << second byte of track bitmap
522                             msg[1] = 0000001; << binary: bit 0 set
523         
524            the (msg[0] * 8) - 6 computation is an attempt to
525            extract the value of the first track: ie. the one
526            that would be indicated by bit 0 being set.
527                 
528            so, if msg[0] = 0, msg[1] = 0100000 (binary),
529            what happens is that base_track = -5, but by the
530            time we check the correct bit, n = 5, and so the
531            computed track for the status change is 0 (first
532            track).
533
534            if msg[0] = 1, then the base track for any change is 2 (the third track), and so on.
535         */
536
537         /* XXX check needed to make sure we don't go outside the
538            supported number of tracks.
539         */
540
541         if (msg[0] == 0) {
542                 base_track = -5;
543         } else {
544                 base_track = (msg[0] * 8) - 6;
545         }
546
547         for (n = 0; n < 7; n++) {
548                 if (msg[1] & (1<<n)) {
549
550                         /* Only touch tracks that have the "mask"
551                            bit set.
552                         */
553
554                         bool val = (msg[2] & (1<<n));
555                         
556                         switch (reg) {
557                         case 0x4f:
558                                 trackRecordStatus[base_track+n] = val;
559                                 TrackRecordStatusChange (*this, base_track+n, val);
560                                 break;
561                                 
562                         case 0x62:
563                                 trackMute[base_track+n] = val;
564                                 TrackMuteChange (*this, base_track+n, val);
565                                 break;
566                         }
567                 } 
568
569         }
570 }
571
572 int
573 MachineControl::do_locate (MIDI::byte *msg, size_t /*msglen*/)
574 {
575         if (msg[2] == 0) {
576                 warning << "MIDI::MMC: locate [I/F] command not supported"
577                         << endmsg;
578                 return 0;
579         }
580
581         /* regular "target" locate command */
582
583         Locate (*this, &msg[3]);
584         return 0;
585 }
586
587 int
588 MachineControl::do_step (MIDI::byte *msg, size_t /*msglen*/)
589 {
590         int steps = msg[2] & 0x3f;
591
592         if (msg[2] & 0x40) {
593                 steps = -steps;
594         }
595
596         Step (*this, steps);
597         return 0;
598 }
599
600 int
601 MachineControl::do_shuttle (MIDI::byte *msg, size_t /*msglen*/)
602 {
603         size_t forward;
604         byte sh = msg[2];
605         byte sm = msg[3];
606         byte sl = msg[4];
607         size_t left_shift;
608         size_t integral;
609         size_t fractional;
610         float shuttle_speed;
611
612         if (sh & (1<<6)) {
613                 forward = false;
614         } else {
615                 forward = true;
616         }
617         
618         left_shift = (sh & 0x38);
619
620         integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
621         fractional = ((sm << left_shift) << 7) | sl;
622
623         shuttle_speed = integral + 
624                 ((float)fractional / (1 << (14 - left_shift)));
625
626         Shuttle (*this, shuttle_speed, forward);
627
628         return 0;
629 }
630
631 void
632 MachineControl::enable_send (bool yn)
633 {
634         _enable_send = yn;
635 }
636
637 /** Send a MMC command to a the MMC port.
638  *  @param c command.
639  */
640 void
641 MachineControl::send (MachineControlCommand const & c)
642 {
643         if (_output_port == 0 || !_enable_send) {
644                 // cerr << "Not delivering MMC " << _mmc->port() << " - " << session_send_mmc << endl;
645                 return;
646         }
647
648         MIDI::byte buffer[32];
649         MIDI::byte* b = c.fill_buffer (this, buffer);
650
651         if (_output_port->midimsg (buffer, b - buffer, 0)) {
652                 error << "MMC: cannot send command" << endmsg;
653         }
654 }
655
656 void
657 MachineControl::spp_start ()
658 {
659         SPPStart (); /* EMIT SIGNAL */
660 }
661
662 void
663 MachineControl::spp_continue ()
664 {
665         SPPContinue (); /* EMIT SIGNAL */
666 }
667
668 void
669 MachineControl::spp_stop ()
670 {
671         SPPStop (); /* EMIT SIGNAL */
672 }
673
674 MachineControlCommand::MachineControlCommand (MachineControl::Command c)
675         : _command (c)
676 {
677
678 }
679
680 MachineControlCommand::MachineControlCommand (Timecode::Time t)
681         : _command (MachineControl::cmdLocate)
682         , _time (t)
683 {
684
685 }
686
687 MIDI::byte * 
688 MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
689 {
690         *b++ = 0xf0; // SysEx
691         *b++ = 0x7f; // Real-time SysEx ID for MMC
692         *b++ = mmc->send_device_id();
693         *b++ = 0x6; // MMC command
694
695         *b++ = _command;
696
697         if (_command == MachineControl::cmdLocate) {
698                 *b++ = 0x6; // byte count
699                 *b++ = 0x1; // "TARGET" subcommand
700                 *b++ = _time.hours;
701                 *b++ = _time.minutes;
702                 *b++ = _time.seconds;
703                 *b++ = _time.frames;
704                 *b++ = _time.subframes;
705         }
706
707         *b++ = 0xf7;
708
709         return b;
710 }
711