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"
25 #include "timecode/bbt_time.h"
27 #include "pbd/error.h"
29 #include "midi++/mmc.h"
30 #include "midi++/port.h"
31 #include "midi++/parser.h"
38 * As libtimecode is linked statically to libmidi++ this
39 * is necessary to pull in all the symbols from libtimecode
40 * so they are exported for other users of libtimecode.
42 double tmp = Timecode::BBT_Time::ticks_per_beat;
44 static std::map<int,string> mmc_cmd_map;
45 static void build_mmc_cmd_map ()
47 pair<int,string> newpair;
50 newpair.second = "Stop";
51 mmc_cmd_map.insert (newpair);
54 newpair.second = "Play";
55 mmc_cmd_map.insert (newpair);
58 newpair.second = "DeferredPlay";
59 mmc_cmd_map.insert (newpair);
62 newpair.second = "FastForward";
63 mmc_cmd_map.insert (newpair);
66 newpair.second = "Rewind";
67 mmc_cmd_map.insert (newpair);
70 newpair.second = "RecordStrobe";
71 mmc_cmd_map.insert (newpair);
74 newpair.second = "RecordExit";
75 mmc_cmd_map.insert (newpair);
78 newpair.second = "RecordPause";
79 mmc_cmd_map.insert (newpair);
82 newpair.second = "Pause";
83 mmc_cmd_map.insert (newpair);
86 newpair.second = "Eject";
87 mmc_cmd_map.insert (newpair);
90 newpair.second = "Chase";
91 mmc_cmd_map.insert (newpair);
94 newpair.second = "CommandErrorReset";
95 mmc_cmd_map.insert (newpair);
98 newpair.second = "MmcReset";
99 mmc_cmd_map.insert (newpair);
101 newpair.first = 0x20;
102 newpair.second = "Illegal Mackie Jog Start";
103 mmc_cmd_map.insert (newpair);
105 newpair.first = 0x21;
106 newpair.second = "Illegal Mackie Jog Stop";
107 mmc_cmd_map.insert (newpair);
109 newpair.first = 0x40;
110 newpair.second = "Write";
111 mmc_cmd_map.insert (newpair);
113 newpair.first = 0x41;
114 newpair.second = "MaskedWrite";
115 mmc_cmd_map.insert (newpair);
117 newpair.first = 0x42;
118 newpair.second = "Read";
119 mmc_cmd_map.insert (newpair);
121 newpair.first = 0x43;
122 newpair.second = "Update";
123 mmc_cmd_map.insert (newpair);
125 newpair.first = 0x44;
126 newpair.second = "Locate";
127 mmc_cmd_map.insert (newpair);
129 newpair.first = 0x45;
130 newpair.second = "VariablePlay";
131 mmc_cmd_map.insert (newpair);
133 newpair.first = 0x46;
134 newpair.second = "Search";
135 mmc_cmd_map.insert (newpair);
137 newpair.first = 0x47;
138 newpair.second = "Shuttle";
139 mmc_cmd_map.insert (newpair);
141 newpair.first = 0x48;
142 newpair.second = "Step";
143 mmc_cmd_map.insert (newpair);
145 newpair.first = 0x49;
146 newpair.second = "AssignSystemMaster";
147 mmc_cmd_map.insert (newpair);
149 newpair.first = 0x4A;
150 newpair.second = "GeneratorCommand";
151 mmc_cmd_map.insert (newpair);
153 newpair.first = 0x4B;
154 newpair.second = "MtcCommand";
155 mmc_cmd_map.insert (newpair);
157 newpair.first = 0x4C;
158 newpair.second = "Move";
159 mmc_cmd_map.insert (newpair);
161 newpair.first = 0x4D;
162 newpair.second = "Add";
163 mmc_cmd_map.insert (newpair);
165 newpair.first = 0x4E;
166 newpair.second = "Subtract";
167 mmc_cmd_map.insert (newpair);
169 newpair.first = 0x4F;
170 newpair.second = "DropFrameAdjust";
171 mmc_cmd_map.insert (newpair);
173 newpair.first = 0x50;
174 newpair.second = "Procedure";
175 mmc_cmd_map.insert (newpair);
177 newpair.first = 0x51;
178 newpair.second = "Event";
179 mmc_cmd_map.insert (newpair);
181 newpair.first = 0x52;
182 newpair.second = "Group";
183 mmc_cmd_map.insert (newpair);
185 newpair.first = 0x53;
186 newpair.second = "CommandSegment";
187 mmc_cmd_map.insert (newpair);
189 newpair.first = 0x54;
190 newpair.second = "DeferredVariablePlay";
191 mmc_cmd_map.insert (newpair);
193 newpair.first = 0x55;
194 newpair.second = "RecordStrobeVariable";
195 mmc_cmd_map.insert (newpair);
197 newpair.first = 0x7C;
198 newpair.second = "Wait";
199 mmc_cmd_map.insert (newpair);
201 newpair.first = 0x7F;
202 newpair.second = "Resume";
203 mmc_cmd_map.insert (newpair);
206 MachineControl::MachineControl ()
208 build_mmc_cmd_map ();
210 _receive_device_id = 0x7f;
211 _send_device_id = 0x7f;
215 MachineControl::set_ports (MIDI::Port* ip, MIDI::Port* op)
217 port_connections.drop_connections ();
222 _input_port->parser()->mmc.connect_same_thread (port_connections, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
223 _input_port->parser()->start.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_start, this));
224 _input_port->parser()->contineu.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_continue, this));
225 _input_port->parser()->stop.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_stop, this));
229 MachineControl::set_receive_device_id (byte id)
231 _receive_device_id = id & 0x7f;
235 MachineControl::set_send_device_id (byte id)
237 _send_device_id = id & 0x7f;
241 MachineControl::is_mmc (byte *sysex_buf, size_t len)
243 if (len < 4 || len > 48) {
247 if (sysex_buf[1] != 0x7f) {
251 if (sysex_buf[3] != 0x6 && /* MMC Command */
252 sysex_buf[3] != 0x7) { /* MMC Response */
260 MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
266 /* Reject if its not for us. 0x7f is the "all-call" device ID */
268 /* msg[0] = 0x7f (MMC sysex ID(
270 msg[2] = 0x6 (MMC command) or 0x7 (MMC response)
271 msg[3] = MMC command code
272 msg[4] = (typically) byte count for following part of command
276 cerr << "*** me = " << (int) _receive_device_id << " MMC message: len = " << len << "\n\t";
277 for (size_t i = 0; i < len; i++) {
278 cerr << hex << (int) msg[i] << dec << ' ';
283 if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
294 /* this works for all non-single-byte "counted"
295 commands. we set it to 1 for the exceptions.
298 std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
299 string cmdname = "unknown";
301 if (x != mmc_cmd_map.end()) {
302 cmdname = (*x).second;
306 cerr << "+++ MMC type "
310 << " \"" << cmdname << "\" "
317 /* SINGLE-BYTE, UNCOUNTED COMMANDS */
329 case cmdDeferredPlay:
330 DeferredPlay (*this);
344 case cmdRecordStrobe:
345 RecordStrobe (*this);
374 case cmdCommandErrorReset:
375 CommandErrorReset (*this);
384 case cmdIllegalMackieJogStart:
389 case cmdIllegalMackieJogStop:
394 /* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
397 do_masked_write (mmc_msg, len);
401 do_locate (mmc_msg, len);
405 do_shuttle (mmc_msg, len);
409 do_step (mmc_msg, len);
415 case cmdVariablePlay:
417 case cmdAssignSystemMaster:
418 case cmdGeneratorCommand:
423 case cmdDropFrameAdjust:
427 case cmdCommandSegment:
428 case cmdDeferredVariablePlay:
429 case cmdRecordStrobeVariable:
432 error << "MIDI::MachineControl: unimplemented MMC command "
433 << hex << (int) *mmc_msg << dec
439 error << "MIDI::MachineControl: unknown MMC command "
440 << hex << (int) *mmc_msg << dec
446 /* increase skiplen to cover the command byte and
447 count byte (if it existed).
451 skiplen = mmc_msg[1] + 2;
456 if (len <= skiplen) {
463 } while (len > 1); /* skip terminating EOX byte */
467 MachineControl::do_masked_write (byte *msg, size_t len)
469 /* return the number of bytes "consumed" */
471 int retval = msg[1] + 2; /* bytes following + 2 */
474 case 0x4f: /* Track Record Ready Status */
475 write_track_status (&msg[3], len - 3, msg[2]);
478 case 0x62: /* track mute */
479 write_track_status (&msg[3], len - 3, msg[2]);
483 warning << "MIDI::MachineControl: masked write to "
484 << hex << (int) msg[2] << dec
485 << " not implemented"
493 MachineControl::write_track_status (byte *msg, size_t /*len*/, byte reg)
498 /* Bits 0-4 of the first byte are for special tracks:
506 the format of the message (its an MMC Masked Write) is:
509 <count> byte count of following data
510 <name> byte value of the field being written
511 <byte #> byte number of target byte in the
512 bitmap being written to
513 <mask> ones in the mask indicate which bits will be changed
514 <data> new data for the byte being written
516 by the time this code is executing, msg[0] is the
517 byte number of the target byte. if its zero, we
518 are writing to a special byte in the standard
519 track bitmap, in which the first 5 bits are
520 special. hence the bits for tracks 1 + 2 are bits
521 5 and 6 of the first byte of the track
524 change track 1: msg[0] = 0; << first byte of track bitmap
525 msg[1] = 0100000; << binary: bit 5 set
527 change track 2: msg[0] = 0; << first byte of track bitmap
528 msg[1] = 1000000; << binary: bit 6 set
530 change track 3: msg[0] = 1; << second byte of track bitmap
531 msg[1] = 0000001; << binary: bit 0 set
533 the (msg[0] * 8) - 6 computation is an attempt to
534 extract the value of the first track: ie. the one
535 that would be indicated by bit 0 being set.
537 so, if msg[0] = 0, msg[1] = 0100000 (binary),
538 what happens is that base_track = -5, but by the
539 time we check the correct bit, n = 5, and so the
540 computed track for the status change is 0 (first
543 if msg[0] = 1, then the base track for any change is 2 (the third track), and so on.
546 /* XXX check needed to make sure we don't go outside the
547 supported number of tracks.
553 base_track = (msg[0] * 8) - 6;
556 for (n = 0; n < 7; n++) {
557 if (msg[1] & (1<<n)) {
559 /* Only touch tracks that have the "mask"
563 bool val = (msg[2] & (1<<n));
567 trackRecordStatus[base_track+n] = val;
568 TrackRecordStatusChange (*this, base_track+n, val);
572 trackMute[base_track+n] = val;
573 TrackMuteChange (*this, base_track+n, val);
582 MachineControl::do_locate (byte *msg, size_t /*msglen*/)
585 warning << "MIDI::MMC: locate [I/F] command not supported"
590 /* regular "target" locate command */
592 Locate (*this, &msg[3]);
597 MachineControl::do_step (byte *msg, size_t /*msglen*/)
599 int steps = msg[2] & 0x3f;
610 MachineControl::do_shuttle (byte *msg, size_t /*msglen*/)
627 left_shift = (sh & 0x38);
629 integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
630 fractional = ((sm << left_shift) << 7) | sl;
632 shuttle_speed = integral +
633 ((float)fractional / (1 << (14 - left_shift)));
635 Shuttle (*this, shuttle_speed, forward);
641 MachineControl::enable_send (bool yn)
646 /** Send a MMC command to a the MMC port.
650 MachineControl::send (MachineControlCommand const & c)
652 if (_output_port == 0 || !_enable_send) {
653 // cerr << "Not delivering MMC " << _mmc->port() << " - " << session_send_mmc << endl;
657 MIDI::byte buffer[32];
658 MIDI::byte* b = c.fill_buffer (this, buffer);
660 if (_output_port->midimsg (buffer, b - buffer, 0)) {
661 error << "MMC: cannot send command" << endmsg;
666 MachineControl::spp_start ()
668 SPPStart (); /* EMIT SIGNAL */
672 MachineControl::spp_continue ()
674 SPPContinue (); /* EMIT SIGNAL */
678 MachineControl::spp_stop ()
680 SPPStop (); /* EMIT SIGNAL */
683 MachineControlCommand::MachineControlCommand (MachineControl::Command c)
689 MachineControlCommand::MachineControlCommand (Timecode::Time t)
690 : _command (MachineControl::cmdLocate)
697 MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
699 *b++ = 0xf0; // SysEx
700 *b++ = 0x7f; // Real-time SysEx ID for MMC
701 *b++ = mmc->send_device_id();
702 *b++ = 0x6; // MMC command
706 if (_command == MachineControl::cmdLocate) {
707 *b++ = 0x6; // byte count
708 *b++ = 0x1; // "TARGET" subcommand
710 *b++ = _time.minutes;
711 *b++ = _time.seconds;
713 *b++ = _time.subframes;