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 "pbd/error.h"
24 #include "midi++/mmc.h"
25 #include "midi++/port.h"
26 #include "midi++/parser.h"
32 static std::map<int,string> mmc_cmd_map;
33 static void build_mmc_cmd_map ()
35 pair<int,string> newpair;
38 newpair.second = "Stop";
39 mmc_cmd_map.insert (newpair);
42 newpair.second = "Play";
43 mmc_cmd_map.insert (newpair);
46 newpair.second = "DeferredPlay";
47 mmc_cmd_map.insert (newpair);
50 newpair.second = "FastForward";
51 mmc_cmd_map.insert (newpair);
54 newpair.second = "Rewind";
55 mmc_cmd_map.insert (newpair);
58 newpair.second = "RecordStrobe";
59 mmc_cmd_map.insert (newpair);
62 newpair.second = "RecordExit";
63 mmc_cmd_map.insert (newpair);
66 newpair.second = "RecordPause";
67 mmc_cmd_map.insert (newpair);
70 newpair.second = "Pause";
71 mmc_cmd_map.insert (newpair);
74 newpair.second = "Eject";
75 mmc_cmd_map.insert (newpair);
78 newpair.second = "Chase";
79 mmc_cmd_map.insert (newpair);
82 newpair.second = "CommandErrorReset";
83 mmc_cmd_map.insert (newpair);
86 newpair.second = "MmcReset";
87 mmc_cmd_map.insert (newpair);
90 newpair.second = "Illegal Mackie Jog Start";
91 mmc_cmd_map.insert (newpair);
94 newpair.second = "Illegal Mackie Jog Stop";
95 mmc_cmd_map.insert (newpair);
98 newpair.second = "Write";
99 mmc_cmd_map.insert (newpair);
101 newpair.first = 0x41;
102 newpair.second = "MaskedWrite";
103 mmc_cmd_map.insert (newpair);
105 newpair.first = 0x42;
106 newpair.second = "Read";
107 mmc_cmd_map.insert (newpair);
109 newpair.first = 0x43;
110 newpair.second = "Update";
111 mmc_cmd_map.insert (newpair);
113 newpair.first = 0x44;
114 newpair.second = "Locate";
115 mmc_cmd_map.insert (newpair);
117 newpair.first = 0x45;
118 newpair.second = "VariablePlay";
119 mmc_cmd_map.insert (newpair);
121 newpair.first = 0x46;
122 newpair.second = "Search";
123 mmc_cmd_map.insert (newpair);
125 newpair.first = 0x47;
126 newpair.second = "Shuttle";
127 mmc_cmd_map.insert (newpair);
129 newpair.first = 0x48;
130 newpair.second = "Step";
131 mmc_cmd_map.insert (newpair);
133 newpair.first = 0x49;
134 newpair.second = "AssignSystemMaster";
135 mmc_cmd_map.insert (newpair);
137 newpair.first = 0x4A;
138 newpair.second = "GeneratorCommand";
139 mmc_cmd_map.insert (newpair);
141 newpair.first = 0x4B;
142 newpair.second = "MtcCommand";
143 mmc_cmd_map.insert (newpair);
145 newpair.first = 0x4C;
146 newpair.second = "Move";
147 mmc_cmd_map.insert (newpair);
149 newpair.first = 0x4D;
150 newpair.second = "Add";
151 mmc_cmd_map.insert (newpair);
153 newpair.first = 0x4E;
154 newpair.second = "Subtract";
155 mmc_cmd_map.insert (newpair);
157 newpair.first = 0x4F;
158 newpair.second = "DropFrameAdjust";
159 mmc_cmd_map.insert (newpair);
161 newpair.first = 0x50;
162 newpair.second = "Procedure";
163 mmc_cmd_map.insert (newpair);
165 newpair.first = 0x51;
166 newpair.second = "Event";
167 mmc_cmd_map.insert (newpair);
169 newpair.first = 0x52;
170 newpair.second = "Group";
171 mmc_cmd_map.insert (newpair);
173 newpair.first = 0x53;
174 newpair.second = "CommandSegment";
175 mmc_cmd_map.insert (newpair);
177 newpair.first = 0x54;
178 newpair.second = "DeferredVariablePlay";
179 mmc_cmd_map.insert (newpair);
181 newpair.first = 0x55;
182 newpair.second = "RecordStrobeVariable";
183 mmc_cmd_map.insert (newpair);
185 newpair.first = 0x7C;
186 newpair.second = "Wait";
187 mmc_cmd_map.insert (newpair);
189 newpair.first = 0x7F;
190 newpair.second = "Resume";
191 mmc_cmd_map.insert (newpair);
195 MachineControl::MachineControl (Port &p, float /*version*/,
196 CommandSignature & /*csig*/,
197 ResponseSignature & /*rsig*/)
203 build_mmc_cmd_map ();
205 _receive_device_id = 0;
206 _send_device_id = 0x7f;
208 if ((parser = _port.input()) != 0) {
210 (mem_fun (*this, &MachineControl::process_mmc_message));
212 warning << "MMC connected to a non-input port: useless!"
218 MachineControl::set_receive_device_id (byte id)
220 _receive_device_id = id & 0x7f;
224 MachineControl::set_send_device_id (byte id)
226 _send_device_id = id & 0x7f;
230 MachineControl::is_mmc (byte *sysex_buf, size_t len)
233 if (len < 4 || len > 48) {
237 if (sysex_buf[1] != 0x7f) {
241 if (sysex_buf[3] != 0x6 && /* MMC Command */
242 sysex_buf[3] != 0x7) { /* MMC Response */
250 MachineControl::process_mmc_message (Parser &, byte *msg, size_t len)
257 /* Reject if its not for us. 0x7f is the "all-call" device ID */
259 /* msg[0] = 0x7f (MMC sysex ID(
261 msg[2] = 0x6 (MMC command) or 0x7 (MMC response)
262 msg[3] = MMC command code
263 msg[4] = (typically) byte count for following part of command
267 cerr << "*** me = " << (int) _receive_device_id << " MMC message: len = " << len << "\n\t";
268 for (size_t i = 0; i < len; i++) {
269 cerr << hex << (int) msg[i] << dec << ' ';
274 if (msg[1] != 0x7f && msg[1] != _receive_device_id) {
285 /* this works for all non-single-byte "counted"
286 commands. we set it to 1 for the exceptions.
289 std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
290 string cmdname = "unknown";
292 if (x != mmc_cmd_map.end()) {
293 cmdname = (*x).second;
297 cerr << "+++ MMC type "
301 << " \"" << cmdname << "\" "
308 /* SINGLE-BYTE, UNCOUNTED COMMANDS */
320 case cmdDeferredPlay:
321 DeferredPlay (*this);
335 case cmdRecordStrobe:
336 RecordStrobe (*this);
365 case cmdCommandErrorReset:
366 CommandErrorReset (*this);
375 case cmdIllegalMackieJogStart:
380 case cmdIllegalMackieJogStop:
385 /* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
388 do_masked_write (mmc_msg, len);
392 do_locate (mmc_msg, len);
396 do_shuttle (mmc_msg, len);
400 do_step (mmc_msg, len);
406 case cmdVariablePlay:
408 case cmdAssignSystemMaster:
409 case cmdGeneratorCommand:
414 case cmdDropFrameAdjust:
418 case cmdCommandSegment:
419 case cmdDeferredVariablePlay:
420 case cmdRecordStrobeVariable:
423 error << "MIDI::MachineControl: unimplemented MMC command "
424 << hex << (int) *mmc_msg << dec
430 error << "MIDI::MachineControl: unknown MMC command "
431 << hex << (int) *mmc_msg << dec
437 /* increase skiplen to cover the command byte and
438 count byte (if it existed).
442 skiplen = mmc_msg[1] + 2;
447 if (len <= skiplen) {
454 } while (len > 1); /* skip terminating EOX byte */
458 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_record_ready (&msg[3], len - 3);
471 warning << "MIDI::MachineControl: masked write to "
472 << hex << (int) msg[2] << dec
473 << " not implemented"
481 MachineControl::write_track_record_ready (byte *msg, size_t /*len*/)
487 /* Bits 0-4 of the first byte are for special tracks:
495 the format of the message (its an MMC Masked Write) is:
498 <count> byte count of following data
499 <name> byte value of the field being written
500 <byte #> byte number of target byte in the
501 bitmap being written to
502 <mask> ones in the mask indicate which bits will be changed
503 <data> new data for the byte being written
505 by the time this code is executing, msg[0] is the
506 byte number of the target byte. if its zero, we
507 are writing to a special byte in the standard
508 track bitmap, in which the first 5 bits are
509 special. hence the bits for tracks 1 + 2 are bits
510 5 and 6 of the first byte of the track
513 change track 1: msg[0] = 0; << first byte of track bitmap
514 msg[1] = 0100000; << binary: bit 5 set
516 change track 2: msg[0] = 0; << first byte of track bitmap
517 msg[1] = 1000000; << binary: bit 6 set
519 change track 3: msg[0] = 1; << second byte of track bitmap
520 msg[1] = 0000001; << binary: bit 0 set
522 the (msg[0] * 8) - 6 computation is an attempt to
523 extract the value of the first track: ie. the one
524 that would be indicated by bit 0 being set.
526 so, if msg[0] = 0, msg[1] = 0100000 (binary),
527 what happens is that base_track = -5, but by the
528 time we check the correct bit, n = 5, and so the
529 computed track for the status change is 0 (first
532 if msg[0] = 1, then the base track for any change is 2 (the third track), and so on.
535 /* XXX check needed to make sure we don't go outside the
536 supported number of tracks.
542 base_track = (msg[0] * 8) - 6;
545 for (n = 0; n < 7; n++) {
546 if (msg[1] & (1<<n)) {
548 /* Only touch tracks that have the "mask"
552 if (msg[2] & (1<<n)) {
553 trackRecordStatus[base_track+n] = true;
554 TrackRecordStatusChange (*this, base_track+n,
557 trackRecordStatus[base_track+n] = false;
558 TrackRecordStatusChange (*this, base_track+n,
567 MachineControl::do_locate (byte *msg, size_t /*msglen*/)
571 warning << "MIDI::MMC: locate [I/F] command not supported"
576 /* regular "target" locate command */
578 Locate (*this, &msg[3]);
583 MachineControl::do_step (byte *msg, size_t /*msglen*/)
585 int steps = msg[2] & 0x3f;
596 MachineControl::do_shuttle (byte *msg, size_t /*msglen*/)
614 left_shift = (sh & 0x38);
616 integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
617 fractional = ((sm << left_shift) << 7) | sl;
619 shuttle_speed = integral +
620 ((float)fractional / (1 << (14 - left_shift)));
622 Shuttle (*this, shuttle_speed, forward);