2 Copyright (C) 2000 Paul Barton-Davis
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.
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.
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.
24 #include "timecode/time.h"
26 #include "pbd/error.h"
28 #include "midi++/mmc.h"
29 #include "midi++/port.h"
30 #include "midi++/parser.h"
36 static std::map<int,string> mmc_cmd_map;
37 static void build_mmc_cmd_map ()
39 pair<int,string> newpair;
42 newpair.second = "Stop";
43 mmc_cmd_map.insert (newpair);
46 newpair.second = "Play";
47 mmc_cmd_map.insert (newpair);
50 newpair.second = "DeferredPlay";
51 mmc_cmd_map.insert (newpair);
54 newpair.second = "FastForward";
55 mmc_cmd_map.insert (newpair);
58 newpair.second = "Rewind";
59 mmc_cmd_map.insert (newpair);
62 newpair.second = "RecordStrobe";
63 mmc_cmd_map.insert (newpair);
66 newpair.second = "RecordExit";
67 mmc_cmd_map.insert (newpair);
70 newpair.second = "RecordPause";
71 mmc_cmd_map.insert (newpair);
74 newpair.second = "Pause";
75 mmc_cmd_map.insert (newpair);
78 newpair.second = "Eject";
79 mmc_cmd_map.insert (newpair);
82 newpair.second = "Chase";
83 mmc_cmd_map.insert (newpair);
86 newpair.second = "CommandErrorReset";
87 mmc_cmd_map.insert (newpair);
90 newpair.second = "MmcReset";
91 mmc_cmd_map.insert (newpair);
94 newpair.second = "Illegal Mackie Jog Start";
95 mmc_cmd_map.insert (newpair);
98 newpair.second = "Illegal Mackie Jog Stop";
99 mmc_cmd_map.insert (newpair);
101 newpair.first = 0x40;
102 newpair.second = "Write";
103 mmc_cmd_map.insert (newpair);
105 newpair.first = 0x41;
106 newpair.second = "MaskedWrite";
107 mmc_cmd_map.insert (newpair);
109 newpair.first = 0x42;
110 newpair.second = "Read";
111 mmc_cmd_map.insert (newpair);
113 newpair.first = 0x43;
114 newpair.second = "Update";
115 mmc_cmd_map.insert (newpair);
117 newpair.first = 0x44;
118 newpair.second = "Locate";
119 mmc_cmd_map.insert (newpair);
121 newpair.first = 0x45;
122 newpair.second = "VariablePlay";
123 mmc_cmd_map.insert (newpair);
125 newpair.first = 0x46;
126 newpair.second = "Search";
127 mmc_cmd_map.insert (newpair);
129 newpair.first = 0x47;
130 newpair.second = "Shuttle";
131 mmc_cmd_map.insert (newpair);
133 newpair.first = 0x48;
134 newpair.second = "Step";
135 mmc_cmd_map.insert (newpair);
137 newpair.first = 0x49;
138 newpair.second = "AssignSystemMaster";
139 mmc_cmd_map.insert (newpair);
141 newpair.first = 0x4A;
142 newpair.second = "GeneratorCommand";
143 mmc_cmd_map.insert (newpair);
145 newpair.first = 0x4B;
146 newpair.second = "MtcCommand";
147 mmc_cmd_map.insert (newpair);
149 newpair.first = 0x4C;
150 newpair.second = "Move";
151 mmc_cmd_map.insert (newpair);
153 newpair.first = 0x4D;
154 newpair.second = "Add";
155 mmc_cmd_map.insert (newpair);
157 newpair.first = 0x4E;
158 newpair.second = "Subtract";
159 mmc_cmd_map.insert (newpair);
161 newpair.first = 0x4F;
162 newpair.second = "DropFrameAdjust";
163 mmc_cmd_map.insert (newpair);
165 newpair.first = 0x50;
166 newpair.second = "Procedure";
167 mmc_cmd_map.insert (newpair);
169 newpair.first = 0x51;
170 newpair.second = "Event";
171 mmc_cmd_map.insert (newpair);
173 newpair.first = 0x52;
174 newpair.second = "Group";
175 mmc_cmd_map.insert (newpair);
177 newpair.first = 0x53;
178 newpair.second = "CommandSegment";
179 mmc_cmd_map.insert (newpair);
181 newpair.first = 0x54;
182 newpair.second = "DeferredVariablePlay";
183 mmc_cmd_map.insert (newpair);
185 newpair.first = 0x55;
186 newpair.second = "RecordStrobeVariable";
187 mmc_cmd_map.insert (newpair);
189 newpair.first = 0x7C;
190 newpair.second = "Wait";
191 mmc_cmd_map.insert (newpair);
193 newpair.first = 0x7F;
194 newpair.second = "Resume";
195 mmc_cmd_map.insert (newpair);
198 MachineControl::MachineControl ()
200 build_mmc_cmd_map ();
202 _receive_device_id = 0x7f;
203 _send_device_id = 0x7f;
207 MachineControl::set_ports (MIDI::Port* ip, MIDI::Port* op)
209 port_connections.drop_connections ();
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));
221 MachineControl::set_receive_device_id (byte id)
223 _receive_device_id = id & 0x7f;
227 MachineControl::set_send_device_id (byte id)
229 _send_device_id = id & 0x7f;
233 MachineControl::is_mmc (byte *sysex_buf, size_t len)
235 if (len < 4 || len > 48) {
239 if (sysex_buf[1] != 0x7f) {
243 if (sysex_buf[3] != 0x6 && /* MMC Command */
244 sysex_buf[3] != 0x7) { /* MMC Response */
252 MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
258 /* Reject if its not for us. 0x7f is the "all-call" device ID */
260 /* msg[0] = 0x7f (MMC sysex 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
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 << ' ';
275 if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
286 /* this works for all non-single-byte "counted"
287 commands. we set it to 1 for the exceptions.
290 std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
291 string cmdname = "unknown";
293 if (x != mmc_cmd_map.end()) {
294 cmdname = (*x).second;
298 cerr << "+++ MMC type "
302 << " \"" << cmdname << "\" "
309 /* SINGLE-BYTE, UNCOUNTED COMMANDS */
321 case cmdDeferredPlay:
322 DeferredPlay (*this);
336 case cmdRecordStrobe:
337 RecordStrobe (*this);
366 case cmdCommandErrorReset:
367 CommandErrorReset (*this);
376 case cmdIllegalMackieJogStart:
381 case cmdIllegalMackieJogStop:
386 /* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
389 do_masked_write (mmc_msg, len);
393 do_locate (mmc_msg, len);
397 do_shuttle (mmc_msg, len);
401 do_step (mmc_msg, len);
407 case cmdVariablePlay:
409 case cmdAssignSystemMaster:
410 case cmdGeneratorCommand:
415 case cmdDropFrameAdjust:
419 case cmdCommandSegment:
420 case cmdDeferredVariablePlay:
421 case cmdRecordStrobeVariable:
424 error << "MIDI::MachineControl: unimplemented MMC command "
425 << hex << (int) *mmc_msg << dec
431 error << "MIDI::MachineControl: unknown MMC command "
432 << hex << (int) *mmc_msg << dec
438 /* increase skiplen to cover the command byte and
439 count byte (if it existed).
443 skiplen = mmc_msg[1] + 2;
448 if (len <= skiplen) {
455 } while (len > 1); /* skip terminating EOX byte */
459 MachineControl::do_masked_write (byte *msg, size_t len)
461 /* return the number of bytes "consumed" */
463 int retval = msg[1] + 2; /* bytes following + 2 */
466 case 0x4f: /* Track Record Ready Status */
467 write_track_status (&msg[3], len - 3, msg[2]);
470 case 0x62: /* track mute */
471 write_track_status (&msg[3], len - 3, msg[2]);
475 warning << "MIDI::MachineControl: masked write to "
476 << hex << (int) msg[2] << dec
477 << " not implemented"
485 MachineControl::write_track_status (byte *msg, size_t /*len*/, byte reg)
490 /* Bits 0-4 of the first byte are for special tracks:
498 the format of the message (its an MMC Masked Write) is:
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
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
516 change track 1: msg[0] = 0; << first byte of track bitmap
517 msg[1] = 0100000; << binary: bit 5 set
519 change track 2: msg[0] = 0; << first byte of track bitmap
520 msg[1] = 1000000; << binary: bit 6 set
522 change track 3: msg[0] = 1; << second byte of track bitmap
523 msg[1] = 0000001; << binary: bit 0 set
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.
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
535 if msg[0] = 1, then the base track for any change is 2 (the third track), and so on.
538 /* XXX check needed to make sure we don't go outside the
539 supported number of tracks.
545 base_track = (msg[0] * 8) - 6;
548 for (n = 0; n < 7; n++) {
549 if (msg[1] & (1<<n)) {
551 /* Only touch tracks that have the "mask"
555 bool val = (msg[2] & (1<<n));
559 trackRecordStatus[base_track+n] = val;
560 TrackRecordStatusChange (*this, base_track+n, val);
564 trackMute[base_track+n] = val;
565 TrackMuteChange (*this, base_track+n, val);
574 MachineControl::do_locate (byte *msg, size_t /*msglen*/)
577 warning << "MIDI::MMC: locate [I/F] command not supported"
582 /* regular "target" locate command */
584 Locate (*this, &msg[3]);
589 MachineControl::do_step (byte *msg, size_t /*msglen*/)
591 int steps = msg[2] & 0x3f;
602 MachineControl::do_shuttle (byte *msg, size_t /*msglen*/)
619 left_shift = (sh & 0x38);
621 integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
622 fractional = ((sm << left_shift) << 7) | sl;
624 shuttle_speed = integral +
625 ((float)fractional / (1 << (14 - left_shift)));
627 Shuttle (*this, shuttle_speed, forward);
633 MachineControl::enable_send (bool yn)
638 /** Send a MMC command to a the MMC port.
642 MachineControl::send (MachineControlCommand const & c)
644 if (_output_port == 0 || !_enable_send) {
645 // cerr << "Not delivering MMC " << _mmc->port() << " - " << session_send_mmc << endl;
649 MIDI::byte buffer[32];
650 MIDI::byte* b = c.fill_buffer (this, buffer);
652 if (_output_port->midimsg (buffer, b - buffer, 0)) {
653 error << "MMC: cannot send command" << endmsg;
658 MachineControl::spp_start ()
660 SPPStart (); /* EMIT SIGNAL */
664 MachineControl::spp_continue ()
666 SPPContinue (); /* EMIT SIGNAL */
670 MachineControl::spp_stop ()
672 SPPStop (); /* EMIT SIGNAL */
675 MachineControlCommand::MachineControlCommand (MachineControl::Command c)
681 MachineControlCommand::MachineControlCommand (Timecode::Time t)
682 : _command (MachineControl::cmdLocate)
689 MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
691 *b++ = 0xf0; // SysEx
692 *b++ = 0x7f; // Real-time SysEx ID for MMC
693 *b++ = mmc->send_device_id();
694 *b++ = 0x6; // MMC command
698 if (_command == MachineControl::cmdLocate) {
699 *b++ = 0x6; // byte count
700 *b++ = 0x1; // "TARGET" subcommand
702 *b++ = _time.minutes;
703 *b++ = _time.seconds;
705 *b++ = _time.subframes;