first round of audio-clock font rendering update
[ardour.git] / libs / ardour / ltc_slave.cc
index 39a97677a63f033319cac91e6038512d3c345918..8cc12c2b5d2e75fdaa96171c98ecdcfe6bea4269 100644 (file)
@@ -51,18 +51,25 @@ LTC_Slave::LTC_Slave (Session& s)
        did_reset_tc_format = false;
        delayedlocked = 10;
        monotonic_cnt = 0;
+       fps_detected=false;
 
-       ltc_timecode = timecode_60; // track changes of LTC timecode
-       a3e_timecode = timecode_60; // track canges of Ardour's timecode
+       ltc_timecode = session.config.get_timecode_format();
+       a3e_timecode = session.config.get_timecode_format();
        printed_timecode_warning = false;
        ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
+       memset(&prev_frame, 0, sizeof(LTCFrameExt));
 
        decoder = ltc_decoder_create((int) frames_per_ltc_frame, 128 /*queue size*/);
        reset();
+       resync_latency();
+       session.Xrun.connect_same_thread (port_connections, boost::bind (&LTC_Slave::resync_xrun, this));
+       session.engine().GraphReordered.connect_same_thread (port_connections, boost::bind (&LTC_Slave::resync_latency, this));
 }
 
 LTC_Slave::~LTC_Slave()
 {
+       port_connections.drop_connections();
+
        if (did_reset_tc_format) {
                session.config.set_timecode_format (saved_tc_format);
        }
@@ -73,13 +80,7 @@ LTC_Slave::~LTC_Slave()
 ARDOUR::framecnt_t
 LTC_Slave::resolution () const
 {
-       return (framecnt_t) (frames_per_ltc_frame);
-}
-
-ARDOUR::framecnt_t
-LTC_Slave::seekahead_distance () const
-{
-       return 0;
+       return (framecnt_t) (session.frame_rate() / 1000);
 }
 
 bool
@@ -94,6 +95,25 @@ LTC_Slave::ok() const
        return true;
 }
 
+void
+LTC_Slave::resync_xrun()
+{
+       DEBUG_TRACE (DEBUG::LTC, "LTC resync_xrun()\n");
+       engine_dll_initstate = 0;
+}
+
+void
+LTC_Slave::resync_latency()
+{
+       DEBUG_TRACE (DEBUG::LTC, "LTC resync_latency()\n");
+       engine_dll_initstate = 0;
+
+       if (!session.deletion_in_progress() && session.ltc_output_io()) { /* check if Port exits */
+               boost::shared_ptr<Port> ltcport = session.ltc_input_port();
+               ltcport->get_connected_latency_range(ltc_slave_latency, false);
+       }
+}
+
 void
 LTC_Slave::reset()
 {
@@ -112,7 +132,7 @@ LTC_Slave::parse_ltc(const jack_nframes_t nframes, const jack_default_audio_samp
        unsigned char sound[8192];
        if (nframes > 8192) {
                /* TODO warn once or wrap, loop conversion below
-                * does A3 support > 8192 spp anyway?
+                * does jack/A3 support > 8192 spp anyway?
                 */
                return;
        }
@@ -125,9 +145,52 @@ LTC_Slave::parse_ltc(const jack_nframes_t nframes, const jack_default_audio_samp
        return;
 }
 
+bool
+LTC_Slave::equal_ltc_frame_time(LTCFrame *a, LTCFrame *b) {
+       if (       a->frame_units != b->frame_units
+               || a->frame_tens  != b->frame_tens
+               || a->dfbit       != b->dfbit
+               || a->secs_units  != b->secs_units
+               || a->secs_tens   != b->secs_tens
+               || a->mins_units  != b->mins_units
+               || a->mins_tens   != b->mins_tens
+               || a->hours_units != b->hours_units
+               || a->hours_tens  != b->hours_tens
+            ) {
+               return false;
+       }
+       return true;
+}
+
+bool
+LTC_Slave::detect_discontinuity(LTCFrameExt *frame, int fps, bool fuzzy) {
+       bool discontinuity_detected = false;
+
+       if (fuzzy && (
+                 ( frame->reverse && prev_frame.ltc.frame_units == 0)
+               ||(!frame->reverse && frame->ltc.frame_units == 0)
+               )) {
+               memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
+               return false;
+       }
+
+       if (frame->reverse) {
+               ltc_frame_decrement(&prev_frame.ltc, fps , 0);
+       } else {
+               ltc_frame_increment(&prev_frame.ltc, fps , 0);
+       }
+       if (!equal_ltc_frame_time(&prev_frame.ltc, &frame->ltc)) {
+               discontinuity_detected = true;
+       }
+
+    memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
+    return discontinuity_detected;
+}
+
 bool
 LTC_Slave::detect_ltc_fps(int frameno, bool df)
 {
+       bool fps_changed = false;
        double detected_fps = 0;
        if (frameno > ltc_detect_fps_max)
        {
@@ -135,22 +198,23 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
        }
        ltc_detect_fps_cnt++;
 
-       if (ltc_detect_fps_cnt > 60)
-       {
-               if (ltc_detect_fps_cnt > ltc_detect_fps_max
-                   && (   ceil(timecode.rate) != (ltc_detect_fps_max + 1)
-                       || timecode.drop != df
-                       )
-                   )
-               {
-                       DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC detected FPS %1%2",
-                                       ltc_detect_fps_max + 1, timecode.drop ? "df" : ""));
+       if (ltc_detect_fps_cnt > 40) {
+               if (ltc_detect_fps_cnt > ltc_detect_fps_max) {
                        detected_fps = ltc_detect_fps_max + 1;
                        if (df) {
                                /* LTC df -> indicates fractional framerate */
-                               detected_fps = detected_fps * 1000.0 / 1001.0;
+                               if (Config->get_timecode_source_2997()) {
+                                       detected_fps = detected_fps * 999.0 / 1000.0;
+                               } else {
+                                       detected_fps = detected_fps * 1000.0 / 1001.0;
+                               }
+                       }
+
+                       if (timecode.rate != detected_fps || timecode.drop != df) {
+                               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC detected FPS: %1%2\n", detected_fps, df?"df":"ndf"));
+                       } else {
+                               detected_fps = 0; /* no cange */
                        }
-                       DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC detected FPS: %1%2\n", detected_fps, df?"df":"ndf"));
                }
                ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
        }
@@ -162,43 +226,47 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
                frames_per_ltc_frame = double(session.frame_rate()) / timecode.rate;
                DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC reset to FPS: %1%2 ; audio-frames per LTC: %3\n",
                                detected_fps, df?"df":"ndf", frames_per_ltc_frame));
-               return true; // reset()
+               fps_changed=true;
        }
 
        /* poll and check session TC */
-       if (1) {
-               TimecodeFormat tc_format = apparent_timecode_format();
-               TimecodeFormat cur_timecode = session.config.get_timecode_format();
-               if (Config->get_timecode_sync_frame_rate()) {
-                       /* enforce time-code */
-                       if (!did_reset_tc_format) {
-                               saved_tc_format = cur_timecode;
-                               did_reset_tc_format = true;
-                       }
-                       if (cur_timecode != tc_format) {
-                               warning << string_compose(_("Session framerate adjusted from %1 TO: LTC's %2."),
+       TimecodeFormat tc_format = apparent_timecode_format();
+       TimecodeFormat cur_timecode = session.config.get_timecode_format();
+
+       if (Config->get_timecode_sync_frame_rate()) {
+               /* enforce time-code */
+               if (!did_reset_tc_format) {
+                       saved_tc_format = cur_timecode;
+                       did_reset_tc_format = true;
+               }
+               if (cur_timecode != tc_format) {
+                       if (ceil(Timecode::timecode_to_frames_per_second(cur_timecode)) != ceil(Timecode::timecode_to_frames_per_second(tc_format))) {
+                               warning << string_compose(_("Session framerate adjusted from %1 to LTC's %2."),
                                                Timecode::timecode_format_name(cur_timecode),
                                                Timecode::timecode_format_name(tc_format))
                                        << endmsg;
-                               session.config.set_timecode_format (tc_format);
                        }
-               } else {
-                       /* only warn about TC mismatch */
-                       if (ltc_timecode != tc_format) printed_timecode_warning = false;
-                       if (a3e_timecode != cur_timecode) printed_timecode_warning = false;
+                       session.config.set_timecode_format (tc_format);
+               }
+       } else {
+               /* only warn about TC mismatch */
+               if (ltc_timecode != tc_format) printed_timecode_warning = false;
+               if (a3e_timecode != cur_timecode) printed_timecode_warning = false;
 
-                       if (cur_timecode != tc_format && ! printed_timecode_warning) {
+               if (cur_timecode != tc_format && ! printed_timecode_warning) {
+                       if (ceil(Timecode::timecode_to_frames_per_second(cur_timecode)) != ceil(Timecode::timecode_to_frames_per_second(tc_format))) {
                                warning << string_compose(_("Session and LTC framerate mismatch: LTC:%1 Session:%2."),
                                                Timecode::timecode_format_name(tc_format),
                                                Timecode::timecode_format_name(cur_timecode))
                                        << endmsg;
-                               printed_timecode_warning = true;
                        }
+                       printed_timecode_warning = true;
                }
-               ltc_timecode = tc_format;
-               a3e_timecode = cur_timecode;
        }
-       return false;
+       ltc_timecode = tc_format;
+       a3e_timecode = cur_timecode;
+
+       return fps_changed;
 }
 
 void
@@ -213,9 +281,16 @@ LTC_Slave::process_ltc(framepos_t const now)
                timecode.subframes  = 0;
 
                /* set timecode.rate and timecode.drop: */
-               if (detect_ltc_fps(stime.frame, (frame.ltc.dfbit)? true : false)) {
+               bool ltc_is_static = equal_ltc_frame_time(&prev_frame.ltc, &frame.ltc);
+
+               if (detect_discontinuity(&frame, ceil(timecode.rate), !fps_detected)) {
+                       if (fps_detected) { ltc_detect_fps_cnt = ltc_detect_fps_max = 0; }
+                       fps_detected=false;
+               }
+
+               if (!ltc_is_static && detect_ltc_fps(stime.frame, (frame.ltc.dfbit)? true : false)) {
                        reset();
-                       last_timestamp = 0;
+                       fps_detected=true;
                }
 
 #if 0 // Devel/Debug
@@ -256,7 +331,7 @@ LTC_Slave::process_ltc(framepos_t const now)
 
                /* map LTC timecode to session TC setting */
                framepos_t ltc_frame; ///< audio-frame corresponding to LTC frame
-               Timecode::timecode_to_sample (timecode, ltc_frame, true, false,
+               Timecode::timecode_to_sample (timecode, ltc_frame, false, false,
                        double(session.frame_rate()),
                        session.config.get_subframes_per_frame(),
                        session.config.get_timecode_offset_negative(), session.config.get_timecode_offset()
@@ -306,15 +381,14 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
        framecnt_t nframes = session.engine().frames_per_cycle();
 
        jack_default_audio_sample_t *in;
-       jack_latency_range_t ltc_latency;
 
-       boost::shared_ptr<Port> ltcport = session.engine().ltc_input_port();
-       ltcport->get_connected_latency_range(ltc_latency, false);
+       boost::shared_ptr<Port> ltcport = session.ltc_input_port();
+
        in = (jack_default_audio_sample_t*) jack_port_get_buffer (ltcport->jack_port(), nframes);
 
        frameoffset_t skip = now - (monotonic_cnt + nframes);
        monotonic_cnt = now;
-       DEBUG_TRACE (DEBUG::LTC, string_compose ("speed_and_position - TID:%1 | latency: %2 | skip %3\n", ::pthread_self(), ltc_latency.max, skip));
+       DEBUG_TRACE (DEBUG::LTC, string_compose ("speed_and_position - TID:%1 | latency: %2 | skip %3\n", ::pthread_self(), ltc_slave_latency.max, skip));
 
        if (last_timestamp == 0) {
                engine_dll_initstate = 0;
@@ -345,7 +419,7 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
                        reset();
                }
 
-               parse_ltc(nframes, in, now + ltc_latency.max );
+               parse_ltc(nframes, in, now - ltc_slave_latency.max );
                process_ltc(now);
        }
 
@@ -368,7 +442,7 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
 
        /* it take 2 cycles from naught to rolling.
         * during these to initial cycles the speed == 0
-        * 
+        *
         * the first cycle:
         * DEBUG::Slave: slave stopped, move to NNN
         * DEBUG::Transport: Request forced locate to NNN
@@ -426,10 +500,10 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
        DEBUG_TRACE (DEBUG::LTC, string_compose ("LTCsync spd: %1 pos: %2 | last-pos: %3 elapsed: %4 delta: %5\n",
                                                 speed, pos, last_ltc_frame, elapsed, current_delta));
 
-#if 1 /* provide a .1% deadzone to lock the speed */
-       if (fabs(speed - 1.0) <= 0.001)
+       /* provide a .1% deadzone to lock the speed */
+       if (fabs(speed - 1.0) <= 0.001) {
                speed = 1.0;
-#endif
+       }
 
        return true;
 }
@@ -442,9 +516,9 @@ LTC_Slave::apparent_timecode_format () const
        else if (timecode.rate == 25 && !timecode.drop)
                return timecode_25;
        else if (rint(timecode.rate * 100) == 2997 && !timecode.drop)
-               return timecode_2997;
+               return (Config->get_timecode_source_2997() ? timecode_2997000 : timecode_2997);
        else if (rint(timecode.rate * 100) == 2997 &&  timecode.drop)
-               return timecode_2997drop;
+               return (Config->get_timecode_source_2997() ? timecode_2997000drop : timecode_2997drop);
        else if (timecode.rate == 30 &&  timecode.drop)
                return timecode_2997drop; // timecode_30drop; // LTC counting to 30 frames w/DF *means* 29.97 df
        else if (timecode.rate == 30 && !timecode.drop)
@@ -458,7 +532,7 @@ std::string
 LTC_Slave::approximate_current_position() const
 {
        if (last_timestamp == 0) {
-               return " --:--:--:--";
+               return " \u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012";
        }
        return Timecode::timecode_format_time(timecode);
 }
@@ -466,13 +540,13 @@ LTC_Slave::approximate_current_position() const
 std::string
 LTC_Slave::approximate_current_delta() const
 {
-       char delta[24];
+       char delta[80];
        if (last_timestamp == 0 || engine_dll_initstate == 0) {
                snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
        } else if ((monotonic_cnt - last_timestamp) > 2 * frames_per_ltc_frame) {
-               snprintf(delta, sizeof(delta), "flywheel");
+               snprintf(delta, sizeof(delta), _("flywheel"));
        } else {
-               snprintf(delta, sizeof(delta), "%s%4" PRIi64 " sm",
+               snprintf(delta, sizeof(delta), "\u0394<span foreground=\"green\" face=\"monospace\" >%s%5" PRIi64 "</span>sm",
                                PLUSMINUS(-current_delta), abs(current_delta));
        }
        return std::string(delta);