'libs/ardour' - Main body of changes required for building with MSVC
[ardour.git] / libs / ardour / ltc_slave.cc
index f176c206a31f6220cdb1a1d2007a4c4f703c6750..ac93ccdb4068e3fbca50789fcc7091d6c5c90264 100644 (file)
 */
 #include <iostream>
 #include <errno.h>
-#include <poll.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include "pbd/error.h"
+#include "pbd/pthread_utils.h"
 
 #include "ardour/debug.h"
 #include "ardour/slave.h"
@@ -53,13 +53,16 @@ LTC_Slave::LTC_Slave (Session& s)
        monotonic_cnt = 0;
        fps_detected=false;
 
-       ltc_timecode = timecode_60; // track changes of LTC fps
-       a3e_timecode = timecode_60; // track changes of Ardour's fps
+       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*/);
+
+       session.config.ParameterChanged.connect_same_thread (config_connection, boost::bind (&LTC_Slave::parameter_changed, this, _1));
+       parse_timecode_offset();
        reset();
        resync_latency();
        session.Xrun.connect_same_thread (port_connections, boost::bind (&LTC_Slave::resync_xrun, this));
@@ -69,6 +72,7 @@ LTC_Slave::LTC_Slave (Session& s)
 LTC_Slave::~LTC_Slave()
 {
        port_connections.drop_connections();
+       config_connection.disconnect();
 
        if (did_reset_tc_format) {
                session.config.set_timecode_format (saved_tc_format);
@@ -77,10 +81,30 @@ LTC_Slave::~LTC_Slave()
        ltc_decoder_free(decoder);
 }
 
+void
+LTC_Slave::parse_timecode_offset() {
+       Timecode::Time offset_tc;
+       Timecode::parse_timecode_format(session.config.get_slave_timecode_offset(), offset_tc);
+       offset_tc.rate = session.timecode_frames_per_second();
+       offset_tc.drop = session.timecode_drop_frames();
+       session.timecode_to_sample(offset_tc, timecode_offset, false, false);
+       timecode_negative_offset = offset_tc.negative;
+}
+
+void
+LTC_Slave::parameter_changed (std::string const & p)
+{
+       if (p == "slave-timecode-offset"
+                       || p == "timecode-format"
+                       ) {
+               parse_timecode_offset();
+       }
+}
+
 ARDOUR::framecnt_t
 LTC_Slave::resolution () const
 {
-       return (framecnt_t) (frames_per_ltc_frame);
+       return (framecnt_t) (session.frame_rate() / 1000);
 }
 
 bool
@@ -108,7 +132,7 @@ LTC_Slave::resync_latency()
        DEBUG_TRACE (DEBUG::LTC, "LTC resync_latency()\n");
        engine_dll_initstate = 0;
 
-       if (session.ltc_output_io()) { /* check if Port exits */
+       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);
        }
@@ -126,7 +150,7 @@ LTC_Slave::reset()
 }
 
 void
-LTC_Slave::parse_ltc(const jack_nframes_t nframes, const jack_default_audio_sample_t * const in, const framecnt_t posinfo)
+LTC_Slave::parse_ltc(const jack_nframes_t nframes, const jack_default_audio_sample_t * const in, const ARDOUR::framecnt_t posinfo)
 {
        jack_nframes_t i;
        unsigned char sound[8192];
@@ -175,9 +199,9 @@ LTC_Slave::detect_discontinuity(LTCFrameExt *frame, int fps, bool fuzzy) {
        }
 
        if (frame->reverse) {
-               ltc_frame_decrement(&prev_frame.ltc, fps , 0);
+               ltc_frame_decrement(&prev_frame.ltc, fps, LTC_TV_525_60, 0);
        } else {
-               ltc_frame_increment(&prev_frame.ltc, fps , 0);
+               ltc_frame_increment(&prev_frame.ltc, fps, LTC_TV_525_60, 0);
        }
        if (!equal_ltc_frame_time(&prev_frame.ltc, &frame->ltc)) {
                discontinuity_detected = true;
@@ -198,22 +222,23 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
        }
        ltc_detect_fps_cnt++;
 
-       if (ltc_detect_fps_cnt > 40)
-       {
-               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;
        }
@@ -231,6 +256,7 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
        /* poll and check session TC */
        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) {
@@ -238,10 +264,12 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
                        did_reset_tc_format = true;
                }
                if (cur_timecode != 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;
+                       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 {
@@ -250,10 +278,12 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
                if (a3e_timecode != cur_timecode) printed_timecode_warning = false;
 
                if (cur_timecode != tc_format && ! printed_timecode_warning) {
-                       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;
+                       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;
                }
        }
@@ -264,9 +294,10 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
 }
 
 void
-LTC_Slave::process_ltc(framepos_t const now)
+LTC_Slave::process_ltc(framepos_t const /*now*/)
 {
        LTCFrameExt frame;
+       enum LTC_TV_STANDARD tv_standard = LTC_TV_625_50;
        while (ltc_decoder_read(decoder, &frame)) {
                SMPTETimecode stime;
 
@@ -306,15 +337,34 @@ LTC_Slave::process_ltc(framepos_t const now)
                 * is expected to start at the end of the current frame
                 */
                int fps_i = ceil(timecode.rate);
+
+               switch(fps_i) {
+                       case 30:
+                               if (timecode.drop) {
+                                       tv_standard = LTC_TV_525_60;
+                               } else {
+                                       tv_standard = LTC_TV_1125_60;
+                               }
+                               break;
+                       case 25:
+                               tv_standard = LTC_TV_625_50;
+                               break;
+                       default:
+                               tv_standard = LTC_TV_FILM_24; /* == LTC_TV_1125_60 == no offset, 24,30fps BGF */
+                               break;
+               }
+
                if (!frame.reverse) {
-                       ltc_frame_increment(&frame.ltc, fps_i , 0);
+                       ltc_frame_increment(&frame.ltc, fps_i, tv_standard, 0);
                        ltc_frame_to_time(&stime, &frame.ltc, 0);
                        transport_direction = 1;
+                       frame.off_start -= ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
+                       frame.off_end -= ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
                } else {
-                       ltc_frame_decrement(&frame.ltc, fps_i , 0);
+                       ltc_frame_decrement(&frame.ltc, fps_i, tv_standard, 0);
                        int off = frame.off_end - frame.off_start;
-                       frame.off_start += off;
-                       frame.off_end += off;
+                       frame.off_start += off - ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
+                       frame.off_end += off - ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
                        transport_direction = -1;
                }
 
@@ -328,7 +378,7 @@ LTC_Slave::process_ltc(framepos_t const now)
                Timecode::timecode_to_sample (timecode, ltc_frame, true, false,
                        double(session.frame_rate()),
                        session.config.get_subframes_per_frame(),
-                       session.config.get_timecode_offset_negative(), session.config.get_timecode_offset()
+                       timecode_negative_offset, timecode_offset
                        );
 
                framepos_t cur_timestamp = frame.off_end + 1;
@@ -378,24 +428,11 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
 
        boost::shared_ptr<Port> ltcport = session.ltc_input_port();
 
-#if 1
-       /* TODO read layency only on demand -> ::reset()
-        * this is already prepared.
-        *
-        * ..but first fix jack2 issue with re-computing latency
-        * in the correct order. Until then, querying it in the
-        * process-callback is the only way to get the current value
-        *
-        * update: fix for this issue is known -- common/JackEngine.cpp
-        * but not yet applied to jack2 git.
-        */
-       ltcport->get_connected_latency_range(ltc_slave_latency, false);
-#endif
        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_slave_latency.max, skip));
+       DEBUG_TRACE (DEBUG::LTC, string_compose ("speed_and_position - TID:%1 | latency: %2 | skip %3\n", pthread_name(), ltc_slave_latency.max, skip));
 
        if (last_timestamp == 0) {
                engine_dll_initstate = 0;
@@ -426,7 +463,7 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
                        reset();
                }
 
-               parse_ltc(nframes, in, now + ltc_slave_latency.max );
+               parse_ltc(nframes, in, now - ltc_slave_latency.max );
                process_ltc(now);
        }
 
@@ -507,10 +544,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;
 }
@@ -523,9 +560,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)
@@ -547,14 +584,14 @@ 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",
-                               PLUSMINUS(-current_delta), abs(current_delta));
+               snprintf(delta, sizeof(delta), "\u0394<span foreground=\"green\" face=\"monospace\" >%s%s%" PRIi64 "</span>sm",
+                               LEADINGZERO(llabs(current_delta)), PLUSMINUS(-current_delta), llabs(current_delta));
        }
        return std::string(delta);
 }