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