Fix build.
[ardour.git] / libs / midi++2 / mtc.cc
index da85aa11fd0f84f8bf9c49ca15d4703e7534bc7f..d1bb7c9bad673c814c3f5ea1098418b5e7e7b629 100644 (file)
@@ -33,6 +33,8 @@ using namespace std;
 using namespace sigc;
 using namespace MIDI;
 
+#undef DEBUG_MTC
+
 bool
 Parser::possible_mtc (byte *sysex_buf, size_t msglen)
 {
@@ -61,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;
@@ -70,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;
@@ -90,7 +100,6 @@ Parser::process_mtc_quarter_frame (byte *msg)
           depending on the direction of the MTC generator ...
        */
 
-#define DEBUG_MTC
 #ifdef DEBUG_MTC
         cerr << "MTC: (state = " << _mtc_running << ") " 
              << which_quarter_frame << " vs. " << expected_mtc_quarter_frame_code
@@ -112,7 +121,7 @@ Parser::process_mtc_quarter_frame (byte *msg)
                                consecutive_qtr_frame_cnt++;
                        }
                        
-                       cerr << "first seen qframe = " << (int) last_qtr_frame << endl;
+                       // cerr << "first seen qframe = " << (int) last_qtr_frame << endl;
 
                        return;
 
@@ -120,14 +129,17 @@ Parser::process_mtc_quarter_frame (byte *msg)
 
                        /* third quarter frame */
                        
+#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<bool> 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 */
-
-                       expected_mtc_quarter_frame_code = 0;
-                       memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
+                       /* skip counts as an error ... go back to waiting for the first frame */
 
-                       _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,7 +239,9 @@ Parser::process_mtc_quarter_frame (byte *msg)
 
        /* time code is looking good */
 
-       cerr << "for quarter frame " << which_quarter_frame << " byte = " << hex << (int) msg[1] << dec << endl;
+#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
@@ -275,7 +290,11 @@ 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);
 
@@ -289,18 +308,13 @@ Parser::process_mtc_quarter_frame (byte *msg)
                        */
                        
                        if (consecutive_qtr_frame_cnt >= 8) {
-                               cerr << hex;
-                               for (size_t xx = 0; xx < sizeof (_qtr_mtc_time); ++xx) {
-                                       cerr << (int) _qtr_mtc_time[xx] << ' ';
-                               } 
-                               cerr << dec << endl;
                                memcpy (_mtc_time, _qtr_mtc_time, sizeof (_mtc_time));
                                memset (_qtr_mtc_time, 0, sizeof (_qtr_mtc_time));
                                if (!_mtc_locked) {
                                        _mtc_locked = true;
                                }
 
-                               mtc_time (_mtc_time, false);
+                               mtc_time (_mtc_time, false, _timestamp);
                        }
                        expected_mtc_quarter_frame_code = 0;
                        
@@ -323,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;