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"
32 #ifndef __INT_MAX__ // 'ssize_t' won't be defined yet
40 static std::map<int,string> mmc_cmd_map;
41 static void build_mmc_cmd_map ()
43 pair<int,string> newpair;
46 newpair.second = "Stop";
47 mmc_cmd_map.insert (newpair);
50 newpair.second = "Play";
51 mmc_cmd_map.insert (newpair);
54 newpair.second = "DeferredPlay";
55 mmc_cmd_map.insert (newpair);
58 newpair.second = "FastForward";
59 mmc_cmd_map.insert (newpair);
62 newpair.second = "Rewind";
63 mmc_cmd_map.insert (newpair);
66 newpair.second = "RecordStrobe";
67 mmc_cmd_map.insert (newpair);
70 newpair.second = "RecordExit";
71 mmc_cmd_map.insert (newpair);
74 newpair.second = "RecordPause";
75 mmc_cmd_map.insert (newpair);
78 newpair.second = "Pause";
79 mmc_cmd_map.insert (newpair);
82 newpair.second = "Eject";
83 mmc_cmd_map.insert (newpair);
86 newpair.second = "Chase";
87 mmc_cmd_map.insert (newpair);
90 newpair.second = "CommandErrorReset";
91 mmc_cmd_map.insert (newpair);
94 newpair.second = "MmcReset";
95 mmc_cmd_map.insert (newpair);
98 newpair.second = "Illegal Mackie Jog Start";
99 mmc_cmd_map.insert (newpair);
101 newpair.first = 0x21;
102 newpair.second = "Illegal Mackie Jog Stop";
103 mmc_cmd_map.insert (newpair);
105 newpair.first = 0x40;
106 newpair.second = "Write";
107 mmc_cmd_map.insert (newpair);
109 newpair.first = 0x41;
110 newpair.second = "MaskedWrite";
111 mmc_cmd_map.insert (newpair);
113 newpair.first = 0x42;
114 newpair.second = "Read";
115 mmc_cmd_map.insert (newpair);
117 newpair.first = 0x43;
118 newpair.second = "Update";
119 mmc_cmd_map.insert (newpair);
121 newpair.first = 0x44;
122 newpair.second = "Locate";
123 mmc_cmd_map.insert (newpair);
125 newpair.first = 0x45;
126 newpair.second = "VariablePlay";
127 mmc_cmd_map.insert (newpair);
129 newpair.first = 0x46;
130 newpair.second = "Search";
131 mmc_cmd_map.insert (newpair);
133 newpair.first = 0x47;
134 newpair.second = "Shuttle";
135 mmc_cmd_map.insert (newpair);
137 newpair.first = 0x48;
138 newpair.second = "Step";
139 mmc_cmd_map.insert (newpair);
141 newpair.first = 0x49;
142 newpair.second = "AssignSystemMaster";
143 mmc_cmd_map.insert (newpair);
145 newpair.first = 0x4A;
146 newpair.second = "GeneratorCommand";
147 mmc_cmd_map.insert (newpair);
149 newpair.first = 0x4B;
150 newpair.second = "MtcCommand";
151 mmc_cmd_map.insert (newpair);
153 newpair.first = 0x4C;
154 newpair.second = "Move";
155 mmc_cmd_map.insert (newpair);
157 newpair.first = 0x4D;
158 newpair.second = "Add";
159 mmc_cmd_map.insert (newpair);
161 newpair.first = 0x4E;
162 newpair.second = "Subtract";
163 mmc_cmd_map.insert (newpair);
165 newpair.first = 0x4F;
166 newpair.second = "DropFrameAdjust";
167 mmc_cmd_map.insert (newpair);
169 newpair.first = 0x50;
170 newpair.second = "Procedure";
171 mmc_cmd_map.insert (newpair);
173 newpair.first = 0x51;
174 newpair.second = "Event";
175 mmc_cmd_map.insert (newpair);
177 newpair.first = 0x52;
178 newpair.second = "Group";
179 mmc_cmd_map.insert (newpair);
181 newpair.first = 0x53;
182 newpair.second = "CommandSegment";
183 mmc_cmd_map.insert (newpair);
185 newpair.first = 0x54;
186 newpair.second = "DeferredVariablePlay";
187 mmc_cmd_map.insert (newpair);
189 newpair.first = 0x55;
190 newpair.second = "RecordStrobeVariable";
191 mmc_cmd_map.insert (newpair);
193 newpair.first = 0x7C;
194 newpair.second = "Wait";
195 mmc_cmd_map.insert (newpair);
197 newpair.first = 0x7F;
198 newpair.second = "Resume";
199 mmc_cmd_map.insert (newpair);
202 MachineControl::MachineControl ()
204 build_mmc_cmd_map ();
206 _receive_device_id = 0x7f;
207 _send_device_id = 0x7f;
211 MachineControl::set_ports (MIDI::Port* ip, MIDI::Port* op)
213 port_connections.drop_connections ();
218 _input_port->parser()->mmc.connect_same_thread (port_connections, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
219 _input_port->parser()->start.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_start, this));
220 _input_port->parser()->contineu.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_continue, this));
221 _input_port->parser()->stop.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_stop, this));
225 MachineControl::set_receive_device_id (MIDI::byte id)
227 _receive_device_id = id & 0x7f;
231 MachineControl::set_send_device_id (MIDI::byte id)
233 _send_device_id = id & 0x7f;
237 MachineControl::is_mmc (MIDI::byte *sysex_buf, size_t len)
239 if (len < 4 || len > 48) {
243 if (sysex_buf[1] != 0x7f) {
247 if (sysex_buf[3] != 0x6 && /* MMC Command */
248 sysex_buf[3] != 0x7) { /* MMC Response */
256 MachineControl::process_mmc_message (Parser &, MIDI::byte *msg, size_t len)
262 /* Reject if its not for us. 0x7f is the "all-call" device ID */
264 /* msg[0] = 0x7f (MMC sysex ID(
266 msg[2] = 0x6 (MMC command) or 0x7 (MMC response)
267 msg[3] = MMC command code
268 msg[4] = (typically) byte count for following part of command
272 cerr << "*** me = " << (int) _receive_device_id << " MMC message: len = " << len << "\n\t";
273 for (size_t i = 0; i < len; i++) {
274 cerr << hex << (int) msg[i] << dec << ' ';
279 if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
290 /* this works for all non-single-byte "counted"
291 commands. we set it to 1 for the exceptions.
294 std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
295 string cmdname = "unknown";
297 if (x != mmc_cmd_map.end()) {
298 cmdname = (*x).second;
302 cerr << "+++ MMC type "
306 << " \"" << cmdname << "\" "
313 /* SINGLE-BYTE, UNCOUNTED COMMANDS */
325 case cmdDeferredPlay:
326 DeferredPlay (*this);
340 case cmdRecordStrobe:
341 RecordStrobe (*this);
370 case cmdCommandErrorReset:
371 CommandErrorReset (*this);
380 case cmdIllegalMackieJogStart:
385 case cmdIllegalMackieJogStop:
390 /* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
393 do_masked_write (mmc_msg, len);
397 do_locate (mmc_msg, len);
401 do_shuttle (mmc_msg, len);
405 do_step (mmc_msg, len);
411 case cmdVariablePlay:
413 case cmdAssignSystemMaster:
414 case cmdGeneratorCommand:
419 case cmdDropFrameAdjust:
423 case cmdCommandSegment:
424 case cmdDeferredVariablePlay:
425 case cmdRecordStrobeVariable:
428 error << "MIDI::MachineControl: unimplemented MMC command "
429 << hex << (int) *mmc_msg << dec
435 error << "MIDI::MachineControl: unknown MMC command "
436 << hex << (int) *mmc_msg << dec
442 /* increase skiplen to cover the command byte and
443 count byte (if it existed).
447 skiplen = mmc_msg[1] + 2;
452 if (len <= skiplen) {
459 } while (len > 1); /* skip terminating EOX byte */
463 MachineControl::do_masked_write (MIDI::byte *msg, size_t len)
465 /* return the number of bytes "consumed" */
467 int retval = msg[1] + 2; /* bytes following + 2 */
470 case 0x4f: /* Track Record Ready Status */
471 write_track_status (&msg[3], len - 3, msg[2]);
474 case 0x62: /* track mute */
475 write_track_status (&msg[3], len - 3, msg[2]);
479 warning << "MIDI::MachineControl: masked write to "
480 << hex << (int) msg[2] << dec
481 << " not implemented"
489 MachineControl::write_track_status (MIDI::byte *msg, size_t /*len*/, MIDI::byte reg)
494 /* Bits 0-4 of the first byte are for special tracks:
502 the format of the message (its an MMC Masked Write) is:
505 <count> byte count of following data
506 <name> byte value of the field being written
507 <byte #> byte number of target byte in the
508 bitmap being written to
509 <mask> ones in the mask indicate which bits will be changed
510 <data> new data for the byte being written
512 by the time this code is executing, msg[0] is the
513 byte number of the target byte. if its zero, we
514 are writing to a special byte in the standard
515 track bitmap, in which the first 5 bits are
516 special. hence the bits for tracks 1 + 2 are bits
517 5 and 6 of the first byte of the track
520 change track 1: msg[0] = 0; << first byte of track bitmap
521 msg[1] = 0100000; << binary: bit 5 set
523 change track 2: msg[0] = 0; << first byte of track bitmap
524 msg[1] = 1000000; << binary: bit 6 set
526 change track 3: msg[0] = 1; << second byte of track bitmap
527 msg[1] = 0000001; << binary: bit 0 set
529 the (msg[0] * 8) - 6 computation is an attempt to
530 extract the value of the first track: ie. the one
531 that would be indicated by bit 0 being set.
533 so, if msg[0] = 0, msg[1] = 0100000 (binary),
534 what happens is that base_track = -5, but by the
535 time we check the correct bit, n = 5, and so the
536 computed track for the status change is 0 (first
539 if msg[0] = 1, then the base track for any change is 2 (the third track), and so on.
542 /* XXX check needed to make sure we don't go outside the
543 supported number of tracks.
549 base_track = (msg[0] * 8) - 6;
552 for (n = 0; n < 7; n++) {
553 if (msg[1] & (1<<n)) {
555 /* Only touch tracks that have the "mask"
559 bool val = (msg[2] & (1<<n));
563 trackRecordStatus[base_track+n] = val;
564 TrackRecordStatusChange (*this, base_track+n, val);
568 trackMute[base_track+n] = val;
569 TrackMuteChange (*this, base_track+n, val);
578 MachineControl::do_locate (MIDI::byte *msg, size_t /*msglen*/)
581 warning << "MIDI::MMC: locate [I/F] command not supported"
586 /* regular "target" locate command */
588 Locate (*this, &msg[3]);
593 MachineControl::do_step (MIDI::byte *msg, size_t /*msglen*/)
595 int steps = msg[2] & 0x3f;
606 MachineControl::do_shuttle (MIDI::byte *msg, size_t /*msglen*/)
623 left_shift = (sh & 0x38);
625 integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
626 fractional = ((sm << left_shift) << 7) | sl;
628 shuttle_speed = integral +
629 ((float)fractional / (1 << (14 - left_shift)));
631 Shuttle (*this, shuttle_speed, forward);
637 MachineControl::enable_send (bool yn)
642 /** Send a MMC command to a the MMC port.
646 MachineControl::send (MachineControlCommand const & c)
648 if (_output_port == 0 || !_enable_send) {
649 // cerr << "Not delivering MMC " << _mmc->port() << " - " << session_send_mmc << endl;
653 MIDI::byte buffer[32];
654 MIDI::byte* b = c.fill_buffer (this, buffer);
656 if (_output_port->midimsg (buffer, b - buffer, 0)) {
657 error << "MMC: cannot send command" << endmsg;
662 MachineControl::spp_start ()
664 SPPStart (); /* EMIT SIGNAL */
668 MachineControl::spp_continue ()
670 SPPContinue (); /* EMIT SIGNAL */
674 MachineControl::spp_stop ()
676 SPPStop (); /* EMIT SIGNAL */
679 MachineControlCommand::MachineControlCommand (MachineControl::Command c)
685 MachineControlCommand::MachineControlCommand (Timecode::Time t)
686 : _command (MachineControl::cmdLocate)
693 MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
695 *b++ = 0xf0; // SysEx
696 *b++ = 0x7f; // Real-time SysEx ID for MMC
697 *b++ = mmc->send_device_id();
698 *b++ = 0x6; // MMC command
702 if (_command == MachineControl::cmdLocate) {
703 *b++ = 0x6; // byte count
704 *b++ = 0x1; // "TARGET" subcommand
706 *b++ = _time.minutes;
707 *b++ = _time.seconds;
709 *b++ = _time.subframes;