X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fmidi%2B%2B2%2Fmtc.cc;h=1a477c3b8caaf9f1aaacfabf86d2c4aa2431ec84;hb=456fb789d3adcdb52de3abd863c1bf64c973effa;hp=1da9ae6e36550d311e13b42957a9b26e7e09df2f;hpb=3ecf8cd00838293b79832b6124312c0b294fdc0f;p=ardour.git diff --git a/libs/midi++2/mtc.cc b/libs/midi++2/mtc.cc index 1da9ae6e36..1a477c3b8c 100644 --- a/libs/midi++2/mtc.cc +++ b/libs/midi++2/mtc.cc @@ -20,21 +20,23 @@ #include #include -#include +#include #include -#include -#include -#include -#include -#include +#include "midi++/types.h" +#include "midi++/parser.h" +#include "midi++/port.h" +#include "midi++/mmc.h" +#include "pbd/transmitter.h" using namespace std; using namespace sigc; using namespace MIDI; +#undef DEBUG_MTC + bool -Parser::possible_mtc (byte *sysex_buf, size_t msglen) +Parser::possible_mtc (MIDI::byte *sysex_buf, size_t msglen) { byte fake_mtc_time[5]; @@ -50,7 +52,6 @@ Parser::possible_mtc (byte *sysex_buf, size_t msglen) fake_mtc_time[3] = (sysex_buf[5] & 0x1f); // hours _mtc_fps = MTC_FPS ((sysex_buf[5] & 0x60) >> 5); // fps - fake_mtc_time[4] = (byte) _mtc_fps; /* wait for first quarter frame, which could indicate forwards @@ -62,7 +63,10 @@ Parser::possible_mtc (byte *sysex_buf, size_t msglen) /* emit signals */ mtc (*this, &sysex_buf[1], msglen - 1); - mtc_time (fake_mtc_time, true); + mtc_time (fake_mtc_time, true, _timestamp); +#ifdef DEBUG_MTC + cerr << "New full-MTC message marks state stopped" << endl; +#endif mtc_status (MTC_Stopped); return true; @@ -71,6 +75,11 @@ Parser::possible_mtc (byte *sysex_buf, size_t msglen) void Parser::reset_mtc_state () { +#ifdef DEBUG_MTC + cerr << "MTC state reset" << endl; +#endif + /* MUST REMAIN RT-SAFE */ + _mtc_forward = false; _mtc_running = MTC_Stopped; _mtc_locked = false; @@ -82,7 +91,7 @@ Parser::reset_mtc_state () } void -Parser::process_mtc_quarter_frame (byte *msg) +Parser::process_mtc_quarter_frame (MIDI::byte *msg) { int which_quarter_frame = (msg[1] & 0xf0) >> 4; @@ -91,7 +100,7 @@ Parser::process_mtc_quarter_frame (byte *msg) depending on the direction of the MTC generator ... */ -#if 0 +#ifdef DEBUG_MTC cerr << "MTC: (state = " << _mtc_running << ") " << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code << " consecutive ? " << consecutive_qtr_frame_cnt @@ -111,7 +120,7 @@ Parser::process_mtc_quarter_frame (byte *msg) last_qtr_frame = which_quarter_frame; consecutive_qtr_frame_cnt++; } - + // cerr << "first seen qframe = " << (int) last_qtr_frame << endl; return; @@ -119,15 +128,18 @@ Parser::process_mtc_quarter_frame (byte *msg) } else if (consecutive_qtr_frame_cnt == 1) { /* third quarter frame */ - - // cerr << "second seen qframe = " << (int) which_quarter_frame << endl; - + +#ifdef DEBUG_MTC + cerr << "second seen qframe = " << (int) which_quarter_frame << endl; +#endif if (last_qtr_frame < which_quarter_frame) { _mtc_running = MTC_Forward; } else if (last_qtr_frame > which_quarter_frame) { _mtc_running = MTC_Backward; } - +#ifdef DEBUG_MTC + cerr << "Send MTC status as " << _mtc_running << endl; +#endif mtc_status (_mtc_running); } @@ -173,7 +185,9 @@ Parser::process_mtc_quarter_frame (byte *msg) true, just ignore this in terms of it being an error. */ - if (1) { /* mtc_skipped () */ + boost::optional res = mtc_skipped (); + + if (res.get_value_or (false)) { /* no error, reset next expected frame */ @@ -205,13 +219,12 @@ Parser::process_mtc_quarter_frame (byte *msg) return; } - /* go back to waiting for the first frame */ + /* skip counts as an error ... go back to waiting for the first frame */ - expected_mtc_quarter_frame_code = 0; - memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time)); - - _mtc_running = MTC_Stopped; - _mtc_locked = false; +#ifdef DEBUG_MTC + cerr << "Skipped MTC qtr frame, return to stopped state" << endl; +#endif + reset_mtc_state (); mtc_status (MTC_Stopped); return; @@ -226,6 +239,10 @@ Parser::process_mtc_quarter_frame (byte *msg) /* time code is looking good */ +#ifdef DEBUG_MTC + cerr << "for quarter frame " << which_quarter_frame << " byte = " << hex << (int) msg[1] << dec << endl; +#endif + switch (which_quarter_frame) { case 0: // frames LS nibble _qtr_mtc_time[0] |= msg[1] & 0xf; @@ -264,6 +281,7 @@ Parser::process_mtc_quarter_frame (byte *msg) _qtr_mtc_time[3] |= ((msg[1] & 0x1) << 4); _mtc_fps = MTC_FPS ((msg[1] & 0x6) >> 1); + _qtr_mtc_time[4] = _mtc_fps; break; default: @@ -272,13 +290,17 @@ Parser::process_mtc_quarter_frame (byte *msg) } - mtc_qtr (*this); /* EMIT_SIGNAL */ +#ifdef DEBUG_MTC + cerr << "Emit MTC Qtr\n"; +#endif + + mtc_qtr (*this, which_quarter_frame, _timestamp); /* EMIT_SIGNAL */ // mtc (*this, &msg[1], msglen - 1); switch (_mtc_running) { case MTC_Forward: - if ((which_quarter_frame == 7)) { + if (which_quarter_frame == 7) { /* we've reached the final of 8 quarter frame messages. store the time, reset the pending time holder, @@ -291,7 +313,8 @@ Parser::process_mtc_quarter_frame (byte *msg) if (!_mtc_locked) { _mtc_locked = true; } - mtc_time (_mtc_time, false); + + mtc_time (_mtc_time, false, _timestamp); } expected_mtc_quarter_frame_code = 0; @@ -314,7 +337,7 @@ Parser::process_mtc_quarter_frame (byte *msg) if (!_mtc_locked) { _mtc_locked = true; } - mtc_time (_mtc_time, false); + mtc_time (_mtc_time, false, _timestamp); } expected_mtc_quarter_frame_code = 7;