1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
/*
Copyright (C) 2000 Paul Barton-Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id$
*/
#include <map>
#include <../pbd/pbd/error.h>
#include <midi++/mmc.h>
#include <midi++/port.h>
#include <midi++/parser.h>
using namespace std;
using namespace MIDI;
static std::map<int,string> mmc_cmd_map;
static void build_mmc_cmd_map ()
{
pair<int,string> newpair;
newpair.first = 0x1;
newpair.second = "Stop";
mmc_cmd_map.insert (newpair);
newpair.first = 0x2;
newpair.second = "Play";
mmc_cmd_map.insert (newpair);
newpair.first = 0x3;
newpair.second = "DeferredPlay";
mmc_cmd_map.insert (newpair);
newpair.first = 0x4;
newpair.second = "FastForward";
mmc_cmd_map.insert (newpair);
newpair.first = 0x5;
newpair.second = "Rewind";
mmc_cmd_map.insert (newpair);
newpair.first = 0x6;
newpair.second = "RecordStrobe";
mmc_cmd_map.insert (newpair);
newpair.first = 0x7;
newpair.second = "RecordExit";
mmc_cmd_map.insert (newpair);
newpair.first = 0x8;
newpair.second = "RecordPause";
mmc_cmd_map.insert (newpair);
newpair.first = 0x9;
newpair.second = "Pause";
mmc_cmd_map.insert (newpair);
newpair.first = 0xA;
newpair.second = "Eject";
mmc_cmd_map.insert (newpair);
newpair.first = 0xB;
newpair.second = "Chase";
mmc_cmd_map.insert (newpair);
newpair.first = 0xC;
newpair.second = "CommandErrorReset";
mmc_cmd_map.insert (newpair);
newpair.first = 0xD;
newpair.second = "MmcReset";
mmc_cmd_map.insert (newpair);
newpair.first = 0x20;
newpair.second = "Illegal Mackie Jog Start";
mmc_cmd_map.insert (newpair);
newpair.first = 0x21;
newpair.second = "Illegal Mackie Jog Stop";
mmc_cmd_map.insert (newpair);
newpair.first = 0x40;
newpair.second = "Write";
mmc_cmd_map.insert (newpair);
newpair.first = 0x41;
newpair.second = "MaskedWrite";
mmc_cmd_map.insert (newpair);
newpair.first = 0x42;
newpair.second = "Read";
mmc_cmd_map.insert (newpair);
newpair.first = 0x43;
newpair.second = "Update";
mmc_cmd_map.insert (newpair);
newpair.first = 0x44;
newpair.second = "Locate";
mmc_cmd_map.insert (newpair);
newpair.first = 0x45;
newpair.second = "VariablePlay";
mmc_cmd_map.insert (newpair);
newpair.first = 0x46;
newpair.second = "Search";
mmc_cmd_map.insert (newpair);
newpair.first = 0x47;
newpair.second = "Shuttle";
mmc_cmd_map.insert (newpair);
newpair.first = 0x48;
newpair.second = "Step";
mmc_cmd_map.insert (newpair);
newpair.first = 0x49;
newpair.second = "AssignSystemMaster";
mmc_cmd_map.insert (newpair);
newpair.first = 0x4A;
newpair.second = "GeneratorCommand";
mmc_cmd_map.insert (newpair);
newpair.first = 0x4B;
newpair.second = "MtcCommand";
mmc_cmd_map.insert (newpair);
newpair.first = 0x4C;
newpair.second = "Move";
mmc_cmd_map.insert (newpair);
newpair.first = 0x4D;
newpair.second = "Add";
mmc_cmd_map.insert (newpair);
newpair.first = 0x4E;
newpair.second = "Subtract";
mmc_cmd_map.insert (newpair);
newpair.first = 0x4F;
newpair.second = "DropFrameAdjust";
mmc_cmd_map.insert (newpair);
newpair.first = 0x50;
newpair.second = "Procedure";
mmc_cmd_map.insert (newpair);
newpair.first = 0x51;
newpair.second = "Event";
mmc_cmd_map.insert (newpair);
newpair.first = 0x52;
newpair.second = "Group";
mmc_cmd_map.insert (newpair);
newpair.first = 0x53;
newpair.second = "CommandSegment";
mmc_cmd_map.insert (newpair);
newpair.first = 0x54;
newpair.second = "DeferredVariablePlay";
mmc_cmd_map.insert (newpair);
newpair.first = 0x55;
newpair.second = "RecordStrobeVariable";
mmc_cmd_map.insert (newpair);
newpair.first = 0x7C;
newpair.second = "Wait";
mmc_cmd_map.insert (newpair);
newpair.first = 0x7F;
newpair.second = "Resume";
mmc_cmd_map.insert (newpair);
}
MachineControl::MachineControl (Port &p, float version,
CommandSignature &csig,
ResponseSignature &rsig)
: _port (p)
{
Parser *parser;
build_mmc_cmd_map ();
_device_id = 1;
if ((parser = _port.input()) != 0) {
parser->mmc.connect
(mem_fun (*this, &MachineControl::process_mmc_message));
} else {
warning << "MMC connected to a non-input port: useless!"
<< endmsg;
}
}
void
MachineControl::set_device_id (byte id)
{
_device_id = id & 0x7f;
}
bool
MachineControl::is_mmc (byte *sysex_buf, size_t len)
{
if (len < 4 || len > 48) {
return false;
}
if (sysex_buf[1] != 0x7f) {
return false;
}
if (sysex_buf[3] != 0x6 && /* MMC Command */
sysex_buf[3] != 0x7) { /* MMC Response */
return false;
}
return true;
}
void
MachineControl::process_mmc_message (Parser &p, byte *msg, size_t len)
{
size_t skiplen;
byte *mmc_msg;
bool single_byte;
/* Reject if its not for us. 0x7f is the "all-call" device ID */
/* msg[0] = 0x7f (MMC sysex ID(
msg[1] = device ID
msg[2] = 0x6 (MMC command) or 0x7 (MMC response)
msg[3] = MMC command code
msg[4] = (typically) byte count for following part of command
*/
#if 0
cerr << "*** MMC message: len = " << len << "\n\t";
for (size_t i = 0; i < len; i++) {
cerr << hex << (int) msg[i] << dec << ' ';
}
cerr << endl;
#endif
if (msg[1] != 0x7f && msg[1] != _device_id) {
return;
}
mmc_msg = &msg[3];
len -= 3;
do {
single_byte = false;
/* this works for all non-single-byte "counted"
commands. we set it to 1 for the exceptions.
*/
std::map<int,string>::iterator x = mmc_cmd_map.find ((int)mmc_msg[0]);
string cmdname = "unknown";
if (x != mmc_cmd_map.end()) {
cmdname = (*x).second;
}
#if 0
cerr << "+++ MMC type "
<< hex
<< ((int) *mmc_msg)
<< dec
<< " \"" << cmdname << "\" "
<< " len = " << len
<< endl;
#endif
switch (*mmc_msg) {
/* SINGLE-BYTE, UNCOUNTED COMMANDS */
case cmdStop:
Stop (*this);
single_byte = true;
break;
case cmdPlay:
Play (*this);
single_byte = true;
break;
case cmdDeferredPlay:
DeferredPlay (*this);
single_byte = true;
break;
case cmdFastForward:
FastForward (*this);
single_byte = true;
break;
case cmdRewind:
Rewind (*this);
single_byte = true;
break;
case cmdRecordStrobe:
RecordStrobe (*this);
single_byte = true;
break;
case cmdRecordExit:
RecordExit (*this);
single_byte = true;
break;
case cmdRecordPause:
RecordPause (*this);
single_byte = true;
break;
case cmdPause:
Pause (*this);
single_byte = true;
break;
case cmdEject:
Eject (*this);
single_byte = true;
break;
case cmdChase:
Chase (*this);
single_byte = true;
break;
case cmdCommandErrorReset:
CommandErrorReset (*this);
single_byte = true;
break;
case cmdMmcReset:
MmcReset (*this);
single_byte = true;
break;
case cmdIllegalMackieJogStart:
JogStart (*this);
single_byte = true;
break;
case cmdIllegalMackieJogStop:
JogStop (*this);
single_byte = true;
break;
/* END OF SINGLE-BYTE, UNCOUNTED COMMANDS */
case cmdMaskedWrite:
do_masked_write (mmc_msg, len);
break;
case cmdLocate:
do_locate (mmc_msg, len);
break;
case cmdShuttle:
do_shuttle (mmc_msg, len);
break;
case cmdStep:
do_step (mmc_msg, len);
break;
case cmdWrite:
case cmdRead:
case cmdUpdate:
case cmdVariablePlay:
case cmdSearch:
case cmdAssignSystemMaster:
case cmdGeneratorCommand:
case cmdMtcCommand:
case cmdMove:
case cmdAdd:
case cmdSubtract:
case cmdDropFrameAdjust:
case cmdProcedure:
case cmdEvent:
case cmdGroup:
case cmdCommandSegment:
case cmdDeferredVariablePlay:
case cmdRecordStrobeVariable:
case cmdWait:
case cmdResume:
error << "MIDI::MachineControl: unimplemented MMC command "
<< hex << (int) *mmc_msg << dec
<< endmsg;
break;
default:
error << "MIDI::MachineControl: unknown MMC command "
<< hex << (int) *mmc_msg << dec
<< endmsg;
break;
}
/* increase skiplen to cover the command byte and
count byte (if it existed).
*/
if (!single_byte) {
skiplen = mmc_msg[1] + 2;
} else {
skiplen = 1;
}
if (len <= skiplen) {
break;
}
mmc_msg += skiplen;
len -= skiplen;
} while (len > 1); /* skip terminating EOX byte */
}
int
MachineControl::do_masked_write (byte *msg, size_t len)
{
/* return the number of bytes "consumed" */
int retval = msg[1] + 2; /* bytes following + 2 */
switch (msg[2]) {
case 0x4f: /* Track Record Ready Status */
write_track_record_ready (&msg[3], len - 3);
break;
default:
warning << "MIDI::MachineControl: masked write to "
<< hex << (int) msg[2] << dec
<< " not implemented"
<< endmsg;
}
return retval;
}
void
MachineControl::write_track_record_ready (byte *msg, size_t len)
{
size_t n;
size_t base_track;
/* Bits 0-4 of the first byte are for special tracks:
bit 0: video
bit 1: reserved
bit 2: time code
bit 3: aux track a
bit 4: aux track b
*/
/* XXX check needed to make sure we don't go outside the
support number of tracks.
*/
base_track = (msg[0] * 7) - 5;
for (n = 0; n < 7; n++) {
if (msg[1] & (1<<n)) {
/* Only touch tracks that have the "mask"
bit set.
*/
if (msg[2] & (1<<n)) {
trackRecordStatus[base_track+n] = true;
TrackRecordStatusChange (*this, base_track+n,
true);
} else {
trackRecordStatus[base_track+n] = false;
TrackRecordStatusChange (*this, base_track+n,
false);
}
}
}
}
int
MachineControl::do_locate (byte *msg, size_t msglen)
{
if (msg[2] == 0) {
warning << "MIDI::MMC: locate [I/F] command not supported"
<< endmsg;
return 0;
}
/* regular "target" locate command */
Locate (*this, &msg[3]);
return 0;
}
int
MachineControl::do_step (byte *msg, size_t msglen)
{
int steps = msg[2] & 0x3f;
if (msg[2] & 0x40) {
steps = -steps;
}
Step (*this, steps);
return 0;
}
int
MachineControl::do_shuttle (byte *msg, size_t msglen)
{
size_t forward;
byte sh = msg[2];
byte sm = msg[3];
byte sl = msg[4];
size_t left_shift;
size_t integral;
size_t fractional;
float shuttle_speed;
if (sh & (1<<6)) {
forward = false;
} else {
forward = true;
}
left_shift = (sh & 0x38);
integral = ((sh & 0x7) << left_shift) | (sm >> (7 - left_shift));
fractional = ((sm << left_shift) << 7) | sl;
shuttle_speed = integral +
((float)fractional / (1 << (14 - left_shift)));
Shuttle (*this, shuttle_speed, forward);
return 0;
}
|