From f6b9572bfd86537422b7cb033e83ae1651f30816 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sun, 14 Oct 2012 16:17:42 +0000 Subject: [PATCH] LTC Slave, add support for variable framerates git-svn-id: svn://localhost/ardour2/branches/3.0@13273 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/ardour/slave.h | 8 +++ libs/ardour/ltc_slave.cc | 121 ++++++++++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 10 deletions(-) diff --git a/libs/ardour/ardour/slave.h b/libs/ardour/ardour/slave.h index 0ba06b3ba5..8f4ac1384b 100644 --- a/libs/ardour/ardour/slave.h +++ b/libs/ardour/ardour/slave.h @@ -340,12 +340,14 @@ public: int parse_ltc(const jack_nframes_t, const jack_default_audio_sample_t * const, const framecnt_t); bool process_ltc(framepos_t, framecnt_t); void init_ltc_dll(framepos_t, double); + void detect_ltc_fps(int, bool); Session& session; bool did_reset_tc_format; Timecode::TimecodeFormat saved_tc_format; LTCDecoder *decoder; + Timecode::Time timecode; double frames_per_ltc_frame; framecnt_t last_timestamp; @@ -353,6 +355,12 @@ public: framepos_t ltc_transport_pos; double ltc_speed; + int ltc_detect_fps_cnt; + int ltc_detect_fps_max; + Timecode::TimecodeFormat ltc_timecode; + Timecode::TimecodeFormat a3e_timecode; + bool printed_timecode_warning; + /* DLL - chase MTC */ double t0; ///< time at the beginning of the MTC quater frame double t1; ///< calculated end of the MTC quater frame diff --git a/libs/ardour/ltc_slave.cc b/libs/ardour/ltc_slave.cc index 01dd3cdfae..664e774342 100644 --- a/libs/ardour/ltc_slave.cc +++ b/libs/ardour/ltc_slave.cc @@ -42,10 +42,20 @@ using namespace Timecode; LTC_Slave::LTC_Slave (Session& s) : session (s) { - frames_per_ltc_frame = 1920.0; // samplerate / framerate + frames_per_ltc_frame = session.frames_per_timecode_frame(); + timecode.rate = session.timecode_frames_per_second(); + timecode.drop = session.timecode_drop_frames(); + + printf("LTC initial rate: %f %f\n", timecode.rate, frames_per_ltc_frame); + ltc_transport_pos = 0; ltc_speed = 1.0; last_timestamp = 0; + ltc_detect_fps_cnt = ltc_detect_fps_max = 0; + + ltc_timecode = timecode_60; // track changes of LTC timecode + a3e_timecode = timecode_60; // track canges of Ardour's timecode + printed_timecode_warning = false; decoder = ltc_decoder_create((int) frames_per_ltc_frame, 128 /*queue size*/); } @@ -81,7 +91,7 @@ LTC_Slave::seekahead_distance () const bool LTC_Slave::locked () const { - return true; + return true; // TODO check if >2 sequential LTC frames have been received } bool @@ -105,10 +115,78 @@ LTC_Slave::parse_ltc(const jack_nframes_t nframes, const jack_default_audio_samp return 0; } +void +LTC_Slave::detect_ltc_fps(int frameno, bool df) +{ + double detected_fps = 0; + if (frameno > ltc_detect_fps_max) + { + ltc_detect_fps_max = frameno; + } + 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" : "")); + detected_fps = ltc_detect_fps_max + 1; + if (df) { + /* LTC df -> indicates fractional framerate */ + detected_fps = detected_fps * 1000.0 / 1001.0; + } + 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; + } + + /* when changed */ + if (detected_fps != 0 && (detected_fps != timecode.rate || df != timecode.drop)) { + timecode.rate = detected_fps; + timecode.drop = 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)); + last_timestamp = 0; // re-init + } + + /* 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 << _("Session and LTC framerate mismatch.") << 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; + + if (cur_timecode != tc_format && ! printed_timecode_warning) { + warning << _("Session and LTC framerate mismatch.") << endmsg; + printed_timecode_warning = true; + } + } + ltc_timecode = tc_format; + a3e_timecode = cur_timecode; + } + +} + bool LTC_Slave::process_ltc(framepos_t now, framecnt_t nframes) { - Time timecode; bool have_frame = false; framepos_t sess_pos = session.transport_frame(); // corresponds to now @@ -120,7 +198,8 @@ LTC_Slave::process_ltc(framepos_t now, framecnt_t nframes) while (ltc_decoder_read(decoder,&frame)) { SMPTETimecode stime; ltc_frame_to_time(&stime, &frame.ltc, 0); -#if 1 + +#if 0 // Devel/Debug fprintf(stdout, "LTC %02d:%02d:%02d%c%02d | %8lld %8lld%s\n", stime.hours, stime.mins, @@ -135,8 +214,8 @@ LTC_Slave::process_ltc(framepos_t now, framecnt_t nframes) timecode.negative = false; timecode.subframes = 0; - timecode.drop = (frame.ltc.dfbit)? true : false; - timecode.rate = 25.0; // XXX + /* set timecode.rate and timecode.drop: */ + detect_ltc_fps(stime.frame, (frame.ltc.dfbit)? true : false); /* when a full LTC frame is decoded, the timecode the LTC frame * is referring has just passed. @@ -160,7 +239,11 @@ LTC_Slave::process_ltc(framepos_t now, framecnt_t nframes) timecode.frames = stime.frame; framepos_t ltc_frame; - session.timecode_to_sample (timecode, ltc_frame, true, false); + 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() + ); double poff = (frame.off_end - now); @@ -207,6 +290,7 @@ LTC_Slave::init_ltc_dll(framepos_t tme, double dt) /* main entry point from session_process.cc * called from jack_process callback context + * so it is OK to use jack_port_get_buffer() etc */ bool LTC_Slave::speed_and_position (double& speed, framepos_t& pos) @@ -238,6 +322,7 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos) speed = ltc_speed = 0; pos = session.transport_frame(); last_timestamp = 0; + ltc_detect_fps_cnt = ltc_detect_fps_max = 0; return true; } @@ -250,12 +335,28 @@ LTC_Slave::speed_and_position (double& speed, framepos_t& pos) Timecode::TimecodeFormat LTC_Slave::apparent_timecode_format () const { - /* XXX to be computed, determined from incoming stream */ - return timecode_25; + if (timecode.rate == 24 && !timecode.drop) + return timecode_24; + else if (timecode.rate == 25 && !timecode.drop) + return timecode_25; + else if (rint(timecode.rate * 100) == 2997 && !timecode.drop) + return timecode_2997; + else if (rint(timecode.rate * 100) == 2997 && timecode.drop) + return 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) + return timecode_30; + + /* XXX - unknown timecode format */ + return session.config.get_timecode_format(); } std::string LTC_Slave::approximate_current_position() const { - return "88:88:88:88"; + if (last_timestamp == 0) { + return "--:--:--:--"; + } + return Timecode::timecode_format_time(timecode); }