fully implement and deploy explicit x-thread signal connection syntax (testing comes...
[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 <map>
22
23 #include "pbd/error.h"
24 #include "midi++/mmc.h"
25 #include "midi++/port.h"
26 #include "midi++/parser.h"
27
28 using namespace std;
29 using namespace MIDI;
30 using namespace PBD;
31
32 static std::map<int,string> mmc_cmd_map;
33 static void build_mmc_cmd_map ()
34 {
35         pair<int,string> newpair;
36
37         newpair.first = 0x1;
38         newpair.second = "Stop";
39         mmc_cmd_map.insert (newpair);
40
41         newpair.first = 0x2;
42         newpair.second = "Play";
43         mmc_cmd_map.insert (newpair);
44
45         newpair.first = 0x3;
46         newpair.second = "DeferredPlay";
47         mmc_cmd_map.insert (newpair);
48
49         newpair.first = 0x4;
50         newpair.second = "FastForward";
51         mmc_cmd_map.insert (newpair);
52
53         newpair.first = 0x5;
54         newpair.second = "Rewind";
55         mmc_cmd_map.insert (newpair);
56
57         newpair.first = 0x6;
58         newpair.second = "RecordStrobe";
59         mmc_cmd_map.insert (newpair);
60
61         newpair.first = 0x7;
62         newpair.second = "RecordExit";
63         mmc_cmd_map.insert (newpair);
64
65         newpair.first = 0x8;
66         newpair.second = "RecordPause";
67         mmc_cmd_map.insert (newpair);
68
69         newpair.first = 0x9;
70         newpair.second = "Pause";
71         mmc_cmd_map.insert (newpair);
72
73         newpair.first = 0xA;
74         newpair.second = "Eject";
75         mmc_cmd_map.insert (newpair);
76
77         newpair.first = 0xB;
78         newpair.second = "Chase";
79         mmc_cmd_map.insert (newpair);
80
81         newpair.first = 0xC;
82         newpair.second = "CommandErrorReset";
83         mmc_cmd_map.insert (newpair);
84
85         newpair.first = 0xD;
86         newpair.second = "MmcReset";
87         mmc_cmd_map.insert (newpair);
88
89         newpair.first = 0x20;
90         newpair.second = "Illegal Mackie Jog Start";
91         mmc_cmd_map.insert (newpair);
92
93         newpair.first = 0x21;
94         newpair.second = "Illegal Mackie Jog Stop";
95         mmc_cmd_map.insert (newpair);
96
97         newpair.first = 0x40;
98         newpair.second = "Write";
99         mmc_cmd_map.insert (newpair);
100
101         newpair.first = 0x41;
102         newpair.second = "MaskedWrite";
103         mmc_cmd_map.insert (newpair);
104
105         newpair.first = 0x42;
106         newpair.second = "Read";
107         mmc_cmd_map.insert (newpair);
108
109         newpair.first = 0x43;
110         newpair.second = "Update";
111         mmc_cmd_map.insert (newpair);
112
113         newpair.first = 0x44;
114         newpair.second = "Locate";
115         mmc_cmd_map.insert (newpair);
116
117         newpair.first = 0x45;
118         newpair.second = "VariablePlay";
119         mmc_cmd_map.insert (newpair);
120
121         newpair.first = 0x46;
122         newpair.second = "Search";
123         mmc_cmd_map.insert (newpair);
124
125         newpair.first = 0x47;
126         newpair.second = "Shuttle";
127         mmc_cmd_map.insert (newpair);
128
129         newpair.first = 0x48;
130         newpair.second = "Step";
131         mmc_cmd_map.insert (newpair);
132
133         newpair.first = 0x49;
134         newpair.second = "AssignSystemMaster";
135         mmc_cmd_map.insert (newpair);
136
137         newpair.first = 0x4A;
138         newpair.second = "GeneratorCommand";
139         mmc_cmd_map.insert (newpair);
140
141         newpair.first = 0x4B;
142         newpair.second = "MtcCommand";
143         mmc_cmd_map.insert (newpair);
144
145         newpair.first = 0x4C;
146         newpair.second = "Move";
147         mmc_cmd_map.insert (newpair);
148
149         newpair.first = 0x4D;
150         newpair.second = "Add";
151         mmc_cmd_map.insert (newpair);
152
153         newpair.first = 0x4E;
154         newpair.second = "Subtract";
155         mmc_cmd_map.insert (newpair);
156
157         newpair.first = 0x4F;
158         newpair.second = "DropFrameAdjust";
159         mmc_cmd_map.insert (newpair);
160
161         newpair.first = 0x50;
162         newpair.second = "Procedure";
163         mmc_cmd_map.insert (newpair);
164
165         newpair.first = 0x51;
166         newpair.second = "Event";
167         mmc_cmd_map.insert (newpair);
168
169         newpair.first = 0x52;
170         newpair.second = "Group";
171         mmc_cmd_map.insert (newpair);
172
173         newpair.first = 0x53;
174         newpair.second = "CommandSegment";
175         mmc_cmd_map.insert (newpair);
176
177         newpair.first = 0x54;
178         newpair.second = "DeferredVariablePlay";
179         mmc_cmd_map.insert (newpair);
180
181         newpair.first = 0x55;
182         newpair.second = "RecordStrobeVariable";
183         mmc_cmd_map.insert (newpair);
184
185         newpair.first = 0x7C;
186         newpair.second = "Wait";
187         mmc_cmd_map.insert (newpair);
188
189         newpair.first = 0x7F;
190         newpair.second = "Resume";
191         mmc_cmd_map.insert (newpair);
192 }
193
194
195 MachineControl::MachineControl (Port &p, float /*version*/,
196                                 CommandSignature & /*csig*/,
197                                 ResponseSignature & /*rsig*/)
198
199         : _port (p)
200 {
201         Parser *parser;
202         
203         build_mmc_cmd_map ();
204
205         _receive_device_id = 0;
206         _send_device_id = 0x7f;
207         
208         if ((parser = _port.input()) != 0) {
209                 parser->mmc.connect_same_thread (mmc_connection, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
210         } else {
211                 warning << "MMC connected to a non-input port: useless!"
212                         << endmsg;
213         }
214 }
215
216 void
217 MachineControl::set_receive_device_id (byte id)
218 {
219         _receive_device_id = id & 0x7f;
220 }
221
222 void
223 MachineControl::set_send_device_id (byte id)
224 {
225         _send_device_id = id & 0x7f;
226 }
227
228 bool
229 MachineControl::is_mmc (byte *sysex_buf, size_t len)
230
231 {
232         if (len < 4 || len > 48) {
233                 return false;
234         }
235
236         if (sysex_buf[1] != 0x7f) {
237                 return false;
238         }
239
240         if (sysex_buf[3] != 0x6 && /* MMC Command */
241             sysex_buf[3] != 0x7) { /* MMC Response */
242                 return false;
243         }
244         
245         return true;
246 }
247
248 void
249 MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
250
251 {
252         size_t skiplen;
253         byte *mmc_msg;
254         bool single_byte;
255
256         /* Reject if its not for us. 0x7f is the "all-call" device ID */
257
258         /* msg[0] = 0x7f (MMC sysex ID(
259            msg[1] = device ID
260            msg[2] = 0x6 (MMC command) or 0x7 (MMC response)
261            msg[3] = MMC command code
262            msg[4] = (typically) byte count for following part of command
263         */
264
265 #if 0
266         cerr << "*** me = " << (int) _receive_device_id << " MMC message: len = " << len << "\n\t";
267         for (size_t i = 0; i < len; i++) {
268                 cerr << hex << (int) msg[i] << dec << ' ';
269         }
270         cerr << endl;
271 #endif
272
273         if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
274                 return;
275         }
276
277         mmc_msg = &msg[3];
278         len -= 3;
279
280         do {
281
282                 single_byte = false;
283
284                 /* this works for all non-single-byte "counted"
285                    commands. we set it to 1 for the exceptions.
286                 */
287
288                 std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
289                 string cmdname = "unknown";
290
291                 if (x != mmc_cmd_map.end()) {
292                         cmdname = (*x).second;
293                 }
294
295 #if 0
296                 cerr << "+++ MMC type " 
297                      << hex
298                      << ((int) *mmc_msg)
299                      << dec
300                      << " \"" << cmdname << "\" "
301                      << " len = " << len
302                      << endl;
303 #endif
304
305                 switch (*mmc_msg) {
306
307                 /* SINGLE-BYTE, UNCOUNTED COMMANDS */
308
309                 case cmdStop:
310                         Stop (*this);
311                         single_byte = true;
312                         break;
313
314                 case cmdPlay:
315                         Play (*this);
316                         single_byte = true;
317                         break;
318
319                 case cmdDeferredPlay:
320                         DeferredPlay (*this);
321                         single_byte = true;
322                         break;
323
324                 case cmdFastForward:
325                         FastForward (*this);
326                         single_byte = true;
327                         break;
328
329                 case cmdRewind:
330                         Rewind (*this);
331                         single_byte = true;
332                         break;
333
334                 case cmdRecordStrobe:
335                         RecordStrobe (*this);
336                         single_byte = true;
337                         break;
338
339                 case cmdRecordExit:
340                         RecordExit (*this);
341                         single_byte = true;
342                         break;
343
344                 case cmdRecordPause:
345                         RecordPause (*this);
346                         single_byte = true;
347                         break;
348
349                 case cmdPause:
350                         Pause (*this);
351                         single_byte = true;
352                         break;
353
354                 case cmdEject:
355                         Eject (*this);
356                         single_byte = true;
357                         break;
358
359                 case cmdChase:
360                         Chase (*this);
361                         single_byte = true;
362                         break;
363
364                 case cmdCommandErrorReset:
365                         CommandErrorReset (*this);
366                         single_byte = true;
367                         break;
368
369                 case cmdMmcReset:
370                         MmcReset (*this);
371                         single_byte = true;
372                         break;
373
374                 case cmdIllegalMackieJogStart:
375                         JogStart (*this);
376                         single_byte = true;
377                         break;
378
379                 case cmdIllegalMackieJogStop:
380                         JogStop (*this);
381                         single_byte = true;
382                         break;
383
384                 /* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
385
386                 case cmdMaskedWrite:
387                         do_masked_write (mmc_msg, len);
388                         break;
389
390                 case cmdLocate:
391                         do_locate (mmc_msg, len);
392                         break;
393
394                 case cmdShuttle:
395                         do_shuttle (mmc_msg, len);
396                         break;
397
398                 case cmdStep:
399                         do_step (mmc_msg, len);
400                         break;
401
402                 case cmdWrite:
403                 case cmdRead:
404                 case cmdUpdate:
405                 case cmdVariablePlay:
406                 case cmdSearch:
407                 case cmdAssignSystemMaster:
408                 case cmdGeneratorCommand:
409                 case cmdMtcCommand:
410                 case cmdMove:
411                 case cmdAdd:
412                 case cmdSubtract:
413                 case cmdDropFrameAdjust:
414                 case cmdProcedure:
415                 case cmdEvent:
416                 case cmdGroup:
417                 case cmdCommandSegment:
418                 case cmdDeferredVariablePlay:
419                 case cmdRecordStrobeVariable:
420                 case cmdWait:
421                 case cmdResume:
422                         error << "MIDI::MachineControl: unimplemented MMC command "
423                               << hex << (int) *mmc_msg << dec
424                               << endmsg;
425
426                         break;
427
428                 default:
429                         error << "MIDI::MachineControl: unknown MMC command "
430                               << hex << (int) *mmc_msg << dec
431                               << endmsg;
432
433                         break;
434                 }
435
436                 /* increase skiplen to cover the command byte and 
437                    count byte (if it existed).
438                 */
439
440                 if (!single_byte) {
441                         skiplen = mmc_msg[1] + 2;
442                 } else {
443                         skiplen = 1;
444                 }
445
446                 if (len <= skiplen) {
447                         break;
448                 }
449
450                 mmc_msg += skiplen;
451                 len -= skiplen;
452
453         } while (len > 1); /* skip terminating EOX byte */
454 }               
455
456 int
457 MachineControl::do_masked_write (byte *msg, size_t len)
458
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 (byte *msg, size_t /*len*/, 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 (byte *msg, size_t /*msglen*/)
574
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 {
605         size_t forward;
606         byte sh = msg[2];
607         byte sm = msg[3];
608         byte sl = msg[4];
609         size_t left_shift;
610         size_t integral;
611         size_t fractional;
612         float shuttle_speed;
613
614         if (sh & (1<<6)) {
615                 forward = false;
616         } else {
617                 forward = true;
618         }
619         
620         left_shift = (sh & 0x38);
621
622         integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
623         fractional = ((sm << left_shift) << 7) | sl;
624
625         shuttle_speed = integral + 
626                 ((float)fractional / (1 << (14 - left_shift)));
627
628         Shuttle (*this, shuttle_speed, forward);
629
630         return 0;
631 }
632