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.
23 #include "control_protocol/timecode.h"
24 #include "pbd/error.h"
25 #include "midi++/mmc.h"
26 #include "midi++/port.h"
27 #include "midi++/parser.h"
33 static std::map<int,string> mmc_cmd_map;
34 static void build_mmc_cmd_map ()
36 pair<int,string> newpair;
39 newpair.second = "Stop";
40 mmc_cmd_map.insert (newpair);
43 newpair.second = "Play";
44 mmc_cmd_map.insert (newpair);
47 newpair.second = "DeferredPlay";
48 mmc_cmd_map.insert (newpair);
51 newpair.second = "FastForward";
52 mmc_cmd_map.insert (newpair);
55 newpair.second = "Rewind";
56 mmc_cmd_map.insert (newpair);
59 newpair.second = "RecordStrobe";
60 mmc_cmd_map.insert (newpair);
63 newpair.second = "RecordExit";
64 mmc_cmd_map.insert (newpair);
67 newpair.second = "RecordPause";
68 mmc_cmd_map.insert (newpair);
71 newpair.second = "Pause";
72 mmc_cmd_map.insert (newpair);
75 newpair.second = "Eject";
76 mmc_cmd_map.insert (newpair);
79 newpair.second = "Chase";
80 mmc_cmd_map.insert (newpair);
83 newpair.second = "CommandErrorReset";
84 mmc_cmd_map.insert (newpair);
87 newpair.second = "MmcReset";
88 mmc_cmd_map.insert (newpair);
91 newpair.second = "Illegal Mackie Jog Start";
92 mmc_cmd_map.insert (newpair);
95 newpair.second = "Illegal Mackie Jog Stop";
96 mmc_cmd_map.insert (newpair);
99 newpair.second = "Write";
100 mmc_cmd_map.insert (newpair);
102 newpair.first = 0x41;
103 newpair.second = "MaskedWrite";
104 mmc_cmd_map.insert (newpair);
106 newpair.first = 0x42;
107 newpair.second = "Read";
108 mmc_cmd_map.insert (newpair);
110 newpair.first = 0x43;
111 newpair.second = "Update";
112 mmc_cmd_map.insert (newpair);
114 newpair.first = 0x44;
115 newpair.second = "Locate";
116 mmc_cmd_map.insert (newpair);
118 newpair.first = 0x45;
119 newpair.second = "VariablePlay";
120 mmc_cmd_map.insert (newpair);
122 newpair.first = 0x46;
123 newpair.second = "Search";
124 mmc_cmd_map.insert (newpair);
126 newpair.first = 0x47;
127 newpair.second = "Shuttle";
128 mmc_cmd_map.insert (newpair);
130 newpair.first = 0x48;
131 newpair.second = "Step";
132 mmc_cmd_map.insert (newpair);
134 newpair.first = 0x49;
135 newpair.second = "AssignSystemMaster";
136 mmc_cmd_map.insert (newpair);
138 newpair.first = 0x4A;
139 newpair.second = "GeneratorCommand";
140 mmc_cmd_map.insert (newpair);
142 newpair.first = 0x4B;
143 newpair.second = "MtcCommand";
144 mmc_cmd_map.insert (newpair);
146 newpair.first = 0x4C;
147 newpair.second = "Move";
148 mmc_cmd_map.insert (newpair);
150 newpair.first = 0x4D;
151 newpair.second = "Add";
152 mmc_cmd_map.insert (newpair);
154 newpair.first = 0x4E;
155 newpair.second = "Subtract";
156 mmc_cmd_map.insert (newpair);
158 newpair.first = 0x4F;
159 newpair.second = "DropFrameAdjust";
160 mmc_cmd_map.insert (newpair);
162 newpair.first = 0x50;
163 newpair.second = "Procedure";
164 mmc_cmd_map.insert (newpair);
166 newpair.first = 0x51;
167 newpair.second = "Event";
168 mmc_cmd_map.insert (newpair);
170 newpair.first = 0x52;
171 newpair.second = "Group";
172 mmc_cmd_map.insert (newpair);
174 newpair.first = 0x53;
175 newpair.second = "CommandSegment";
176 mmc_cmd_map.insert (newpair);
178 newpair.first = 0x54;
179 newpair.second = "DeferredVariablePlay";
180 mmc_cmd_map.insert (newpair);
182 newpair.first = 0x55;
183 newpair.second = "RecordStrobeVariable";
184 mmc_cmd_map.insert (newpair);
186 newpair.first = 0x7C;
187 newpair.second = "Wait";
188 mmc_cmd_map.insert (newpair);
190 newpair.first = 0x7F;
191 newpair.second = "Resume";
192 mmc_cmd_map.insert (newpair);
196 MachineControl::MachineControl ()
199 build_mmc_cmd_map ();
201 _receive_device_id = 0;
202 _send_device_id = 0x7f;
206 MachineControl::set_port (Port* p)
210 port_connections.drop_connections ();
212 if (_port->input()) {
213 _port->input()->mmc.connect_same_thread (port_connections, boost::bind (&MachineControl::process_mmc_message, this, _1, _2, _3));
214 _port->input()->start.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_start, this, _1, _2));
215 _port->input()->contineu.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_continue, this, _1, _2));
216 _port->input()->stop.connect_same_thread (port_connections, boost::bind (&MachineControl::spp_stop, this, _1, _2));
218 warning << "MMC connected to a non-input port: useless!" << endmsg;
223 MachineControl::set_receive_device_id (byte id)
225 _receive_device_id = id & 0x7f;
229 MachineControl::set_send_device_id (byte id)
231 _send_device_id = id & 0x7f;
235 MachineControl::is_mmc (byte *sysex_buf, size_t len)
237 if (len < 4 || len > 48) {
241 if (sysex_buf[1] != 0x7f) {
245 if (sysex_buf[3] != 0x6 && /* MMC Command */
246 sysex_buf[3] != 0x7) { /* MMC Response */
254 MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
260 /* Reject if its not for us. 0x7f is the "all-call" device ID */
262 /* msg[0] = 0x7f (MMC sysex ID(
264 msg[2] = 0x6 (MMC command) or 0x7 (MMC response)
265 msg[3] = MMC command code
266 msg[4] = (typically) byte count for following part of command
270 cerr << "*** me = " << (int) _receive_device_id << " MMC message: len = " << len << "\n\t";
271 for (size_t i = 0; i < len; i++) {
272 cerr << hex << (int) msg[i] << dec << ' ';
277 if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
288 /* this works for all non-single-byte "counted"
289 commands. we set it to 1 for the exceptions.
292 std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
293 string cmdname = "unknown";
295 if (x != mmc_cmd_map.end()) {
296 cmdname = (*x).second;
300 cerr << "+++ MMC type "
304 << " \"" << cmdname << "\" "
311 /* SINGLE-BYTE, UNCOUNTED COMMANDS */
323 case cmdDeferredPlay:
324 DeferredPlay (*this);
338 case cmdRecordStrobe:
339 RecordStrobe (*this);
368 case cmdCommandErrorReset:
369 CommandErrorReset (*this);
378 case cmdIllegalMackieJogStart:
383 case cmdIllegalMackieJogStop:
388 /* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
391 do_masked_write (mmc_msg, len);
395 do_locate (mmc_msg, len);
399 do_shuttle (mmc_msg, len);
403 do_step (mmc_msg, len);
409 case cmdVariablePlay:
411 case cmdAssignSystemMaster:
412 case cmdGeneratorCommand:
417 case cmdDropFrameAdjust:
421 case cmdCommandSegment:
422 case cmdDeferredVariablePlay:
423 case cmdRecordStrobeVariable:
426 error << "MIDI::MachineControl: unimplemented MMC command "
427 << hex << (int) *mmc_msg << dec
433 error << "MIDI::MachineControl: unknown MMC command "
434 << hex << (int) *mmc_msg << dec
440 /* increase skiplen to cover the command byte and
441 count byte (if it existed).
445 skiplen = mmc_msg[1] + 2;
450 if (len <= skiplen) {
457 } while (len > 1); /* skip terminating EOX byte */
461 MachineControl::do_masked_write (byte *msg, size_t len)
463 /* return the number of bytes "consumed" */
465 int retval = msg[1] + 2; /* bytes following + 2 */
468 case 0x4f: /* Track Record Ready Status */
469 write_track_status (&msg[3], len - 3, msg[2]);
472 case 0x62: /* track mute */
473 write_track_status (&msg[3], len - 3, msg[2]);
477 warning << "MIDI::MachineControl: masked write to "
478 << hex << (int) msg[2] << dec
479 << " not implemented"
487 MachineControl::write_track_status (byte *msg, size_t /*len*/, byte reg)
492 /* Bits 0-4 of the first byte are for special tracks:
500 the format of the message (its an MMC Masked Write) is:
503 <count> byte count of following data
504 <name> byte value of the field being written
505 <byte #> byte number of target byte in the
506 bitmap being written to
507 <mask> ones in the mask indicate which bits will be changed
508 <data> new data for the byte being written
510 by the time this code is executing, msg[0] is the
511 byte number of the target byte. if its zero, we
512 are writing to a special byte in the standard
513 track bitmap, in which the first 5 bits are
514 special. hence the bits for tracks 1 + 2 are bits
515 5 and 6 of the first byte of the track
518 change track 1: msg[0] = 0; << first byte of track bitmap
519 msg[1] = 0100000; << binary: bit 5 set
521 change track 2: msg[0] = 0; << first byte of track bitmap
522 msg[1] = 1000000; << binary: bit 6 set
524 change track 3: msg[0] = 1; << second byte of track bitmap
525 msg[1] = 0000001; << binary: bit 0 set
527 the (msg[0] * 8) - 6 computation is an attempt to
528 extract the value of the first track: ie. the one
529 that would be indicated by bit 0 being set.
531 so, if msg[0] = 0, msg[1] = 0100000 (binary),
532 what happens is that base_track = -5, but by the
533 time we check the correct bit, n = 5, and so the
534 computed track for the status change is 0 (first
537 if msg[0] = 1, then the base track for any change is 2 (the third track), and so on.
540 /* XXX check needed to make sure we don't go outside the
541 supported number of tracks.
547 base_track = (msg[0] * 8) - 6;
550 for (n = 0; n < 7; n++) {
551 if (msg[1] & (1<<n)) {
553 /* Only touch tracks that have the "mask"
557 bool val = (msg[2] & (1<<n));
561 trackRecordStatus[base_track+n] = val;
562 TrackRecordStatusChange (*this, base_track+n, val);
566 trackMute[base_track+n] = val;
567 TrackMuteChange (*this, base_track+n, val);
576 MachineControl::do_locate (byte *msg, size_t /*msglen*/)
579 warning << "MIDI::MMC: locate [I/F] command not supported"
584 /* regular "target" locate command */
586 Locate (*this, &msg[3]);
591 MachineControl::do_step (byte *msg, size_t /*msglen*/)
593 int steps = msg[2] & 0x3f;
604 MachineControl::do_shuttle (byte *msg, size_t /*msglen*/)
621 left_shift = (sh & 0x38);
623 integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
624 fractional = ((sm << left_shift) << 7) | sl;
626 shuttle_speed = integral +
627 ((float)fractional / (1 << (14 - left_shift)));
629 Shuttle (*this, shuttle_speed, forward);
635 MachineControl::enable_send (bool yn)
640 /** Send a MMC command to a the MMC port.
644 MachineControl::send (MachineControlCommand const & c)
646 if (_port == 0 || !_enable_send) {
647 // cerr << "Not delivering MMC " << _mmc->port() << " - " << session_send_mmc << endl;
651 MIDI::byte buffer[32];
652 MIDI::byte* b = c.fill_buffer (this, buffer);
654 if (_port->midimsg (buffer, b - buffer, 0)) {
655 error << "MMC: cannot send command" << endmsg;
660 MachineControl::spp_start (Parser& parser, nframes_t timestamp)
662 SPPStart (parser, timestamp); /* EMIT SIGNAL */
666 MachineControl::spp_continue (Parser& parser, nframes_t timestamp)
668 SPPContinue (parser, timestamp); /* EMIT SIGNAL */
672 MachineControl::spp_stop (Parser& parser, nframes_t timestamp)
674 SPPStop (parser, timestamp); /* EMIT SIGNAL */
677 MachineControlCommand::MachineControlCommand (MachineControl::Command c)
683 MachineControlCommand::MachineControlCommand (Timecode::Time t)
684 : _command (MachineControl::cmdLocate)
691 MachineControlCommand::fill_buffer (MachineControl* mmc, MIDI::byte* b) const
693 *b++ = 0xf0; // SysEx
694 *b++ = 0x7f; // Real-time SysEx ID for MMC
695 *b++ = mmc->send_device_id();
696 *b++ = 0x6; // MMC command
700 if (_command == MachineControl::cmdLocate) {
701 *b++ = 0x6; // byte count
702 *b++ = 0x1; // "TARGET" subcommand
704 *b++ = _time.minutes;
705 *b++ = _time.seconds;
707 *b++ = _time.subframes;