2 Copyright (C) 2012 Paul Davis
3 Witten by 2012 Robin Gareus <robin@gareus.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <sys/types.h>
26 #include "pbd/error.h"
28 #include "ardour/debug.h"
29 #include "ardour/slave.h"
30 #include "ardour/session.h"
31 #include "ardour/audioengine.h"
32 #include "ardour/audio_port.h"
37 using namespace ARDOUR;
40 using namespace Timecode;
42 #define FLYWHEEL_TIMEOUT ( 1 * session.frame_rate() )
44 LTC_Slave::LTC_Slave (Session& s)
47 frames_per_ltc_frame = session.frames_per_timecode_frame();
48 timecode.rate = session.timecode_frames_per_second();
49 timecode.drop = session.timecode_drop_frames();
51 did_reset_tc_format = false;
56 ltc_timecode = session.config.get_timecode_format();
57 a3e_timecode = session.config.get_timecode_format();
58 printed_timecode_warning = false;
59 ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
60 memset(&prev_frame, 0, sizeof(LTCFrameExt));
62 decoder = ltc_decoder_create((int) frames_per_ltc_frame, 128 /*queue size*/);
65 session.Xrun.connect_same_thread (port_connections, boost::bind (<C_Slave::resync_xrun, this));
66 session.engine().GraphReordered.connect_same_thread (port_connections, boost::bind (<C_Slave::resync_latency, this));
69 LTC_Slave::~LTC_Slave()
71 port_connections.drop_connections();
73 if (did_reset_tc_format) {
74 session.config.set_timecode_format (saved_tc_format);
77 ltc_decoder_free(decoder);
81 LTC_Slave::resolution () const
83 return (framecnt_t) (session.frame_rate() / 1000);
87 LTC_Slave::locked () const
89 return (delayedlocked < 5);
99 LTC_Slave::resync_xrun()
101 DEBUG_TRACE (DEBUG::LTC, "LTC resync_xrun()\n");
102 engine_dll_initstate = 0;
106 LTC_Slave::resync_latency()
108 DEBUG_TRACE (DEBUG::LTC, "LTC resync_latency()\n");
109 engine_dll_initstate = 0;
111 if (!session.deletion_in_progress() && session.ltc_output_io()) { /* check if Port exits */
112 boost::shared_ptr<Port> ltcport = session.ltc_input_port();
113 ltcport->get_connected_latency_range(ltc_slave_latency, false);
120 DEBUG_TRACE (DEBUG::LTC, "LTC reset()\n");
123 transport_direction = 0;
125 engine_dll_initstate = 0;
129 LTC_Slave::parse_ltc(const jack_nframes_t nframes, const jack_default_audio_sample_t * const in, const framecnt_t posinfo)
132 unsigned char sound[8192];
133 if (nframes > 8192) {
134 /* TODO warn once or wrap, loop conversion below
135 * does jack/A3 support > 8192 spp anyway?
140 for (i = 0; i < nframes; i++) {
141 const int snd=(int)rint((127.0*in[i])+128.0);
142 sound[i] = (unsigned char) (snd&0xff);
144 ltc_decoder_write(decoder, sound, nframes, posinfo);
149 LTC_Slave::equal_ltc_frame_time(LTCFrame *a, LTCFrame *b) {
150 if ( a->frame_units != b->frame_units
151 || a->frame_tens != b->frame_tens
152 || a->dfbit != b->dfbit
153 || a->secs_units != b->secs_units
154 || a->secs_tens != b->secs_tens
155 || a->mins_units != b->mins_units
156 || a->mins_tens != b->mins_tens
157 || a->hours_units != b->hours_units
158 || a->hours_tens != b->hours_tens
166 LTC_Slave::detect_discontinuity(LTCFrameExt *frame, int fps, bool fuzzy) {
167 bool discontinuity_detected = false;
170 ( frame->reverse && prev_frame.ltc.frame_units == 0)
171 ||(!frame->reverse && frame->ltc.frame_units == 0)
173 memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
177 if (frame->reverse) {
178 ltc_frame_decrement(&prev_frame.ltc, fps , 0);
180 ltc_frame_increment(&prev_frame.ltc, fps , 0);
182 if (!equal_ltc_frame_time(&prev_frame.ltc, &frame->ltc)) {
183 discontinuity_detected = true;
186 memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
187 return discontinuity_detected;
191 LTC_Slave::detect_ltc_fps(int frameno, bool df)
193 bool fps_changed = false;
194 double detected_fps = 0;
195 if (frameno > ltc_detect_fps_max)
197 ltc_detect_fps_max = frameno;
199 ltc_detect_fps_cnt++;
201 if (ltc_detect_fps_cnt > 40) {
202 if (ltc_detect_fps_cnt > ltc_detect_fps_max) {
203 detected_fps = ltc_detect_fps_max + 1;
205 /* LTC df -> indicates fractional framerate */
206 if (Config->get_timecode_source_2997()) {
207 detected_fps = detected_fps * 999.0 / 1000.0;
209 detected_fps = detected_fps * 1000.0 / 1001.0;
213 if (timecode.rate != detected_fps || timecode.drop != df) {
214 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC detected FPS: %1%2\n", detected_fps, df?"df":"ndf"));
216 detected_fps = 0; /* no cange */
219 ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
223 if (detected_fps != 0 && (detected_fps != timecode.rate || df != timecode.drop)) {
224 timecode.rate = detected_fps;
226 frames_per_ltc_frame = double(session.frame_rate()) / timecode.rate;
227 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC reset to FPS: %1%2 ; audio-frames per LTC: %3\n",
228 detected_fps, df?"df":"ndf", frames_per_ltc_frame));
232 /* poll and check session TC */
233 TimecodeFormat tc_format = apparent_timecode_format();
234 TimecodeFormat cur_timecode = session.config.get_timecode_format();
236 if (Config->get_timecode_sync_frame_rate()) {
237 /* enforce time-code */
238 if (!did_reset_tc_format) {
239 saved_tc_format = cur_timecode;
240 did_reset_tc_format = true;
242 if (cur_timecode != tc_format) {
243 warning << string_compose(_("Session framerate adjusted from %1 to LTC's %2."),
244 Timecode::timecode_format_name(cur_timecode),
245 Timecode::timecode_format_name(tc_format))
247 session.config.set_timecode_format (tc_format);
250 /* only warn about TC mismatch */
251 if (ltc_timecode != tc_format) printed_timecode_warning = false;
252 if (a3e_timecode != cur_timecode) printed_timecode_warning = false;
254 if (cur_timecode != tc_format && ! printed_timecode_warning) {
255 warning << string_compose(_("Session and LTC framerate mismatch: LTC:%1 Session:%2."),
256 Timecode::timecode_format_name(tc_format),
257 Timecode::timecode_format_name(cur_timecode))
259 printed_timecode_warning = true;
262 ltc_timecode = tc_format;
263 a3e_timecode = cur_timecode;
269 LTC_Slave::process_ltc(framepos_t const now)
272 while (ltc_decoder_read(decoder, &frame)) {
275 ltc_frame_to_time(&stime, &frame.ltc, 0);
276 timecode.negative = false;
277 timecode.subframes = 0;
279 /* set timecode.rate and timecode.drop: */
280 bool ltc_is_static = equal_ltc_frame_time(&prev_frame.ltc, &frame.ltc);
282 if (detect_discontinuity(&frame, ceil(timecode.rate), !fps_detected)) {
283 if (fps_detected) { ltc_detect_fps_cnt = ltc_detect_fps_max = 0; }
287 if (!ltc_is_static && detect_ltc_fps(stime.frame, (frame.ltc.dfbit)? true : false)) {
293 fprintf(stdout, "LTC %02d:%02d:%02d%c%02d | %8lld %8lld%s\n",
297 (frame.ltc.dfbit) ? '.' : ':',
301 frame.reverse ? " R" : " "
305 /* when a full LTC frame is decoded, the timecode the LTC frame
306 * is referring has just passed.
307 * So we send the _next_ timecode which
308 * is expected to start at the end of the current frame
310 int fps_i = ceil(timecode.rate);
311 if (!frame.reverse) {
312 ltc_frame_increment(&frame.ltc, fps_i , 0);
313 ltc_frame_to_time(&stime, &frame.ltc, 0);
314 transport_direction = 1;
316 ltc_frame_decrement(&frame.ltc, fps_i , 0);
317 int off = frame.off_end - frame.off_start;
318 frame.off_start += off;
319 frame.off_end += off;
320 transport_direction = -1;
323 timecode.hours = stime.hours;
324 timecode.minutes = stime.mins;
325 timecode.seconds = stime.secs;
326 timecode.frames = stime.frame;
328 /* map LTC timecode to session TC setting */
329 framepos_t ltc_frame; ///< audio-frame corresponding to LTC frame
330 Timecode::timecode_to_sample (timecode, ltc_frame, true, false,
331 double(session.frame_rate()),
332 session.config.get_subframes_per_frame(),
333 session.config.get_timecode_offset_negative(), session.config.get_timecode_offset()
336 framepos_t cur_timestamp = frame.off_end + 1;
337 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC F: %1 LF: %2 N: %3 L: %4\n", ltc_frame, last_ltc_frame, cur_timestamp, last_timestamp));
338 if (frame.off_end + 1 <= last_timestamp || last_timestamp == 0) {
339 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: UNCHANGED: %1\n", ltc_speed));
341 ltc_speed = double(ltc_frame - last_ltc_frame) / double(cur_timestamp - last_timestamp);
342 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: %1\n", ltc_speed));
345 if (fabs(ltc_speed) > 10.0) {
349 last_timestamp = frame.off_end + 1;
350 last_ltc_frame = ltc_frame;
351 } /* end foreach decoded LTC frame */
355 LTC_Slave::init_engine_dll (framepos_t pos, int32_t inc)
357 double omega = 2.0 * M_PI * double(inc) / double(session.frame_rate());
358 b = 1.4142135623730950488 * omega;
361 e2 = double(ltc_speed * inc);
364 DEBUG_TRACE (DEBUG::LTC, string_compose ("[re-]init Engine DLL %1 %2 %3\n", t0, t1, e2));
367 /* main entry point from session_process.cc
368 * called from jack_process callback context
369 * so it is OK to use jack_port_get_buffer()
372 LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
374 bool engine_init_called = false;
375 framepos_t now = session.engine().frame_time_at_cycle_start();
376 framepos_t sess_pos = session.transport_frame(); // corresponds to now
377 framecnt_t nframes = session.engine().frames_per_cycle();
379 jack_default_audio_sample_t *in;
381 boost::shared_ptr<Port> ltcport = session.ltc_input_port();
383 in = (jack_default_audio_sample_t*) jack_port_get_buffer (ltcport->jack_port(), nframes);
385 frameoffset_t skip = now - (monotonic_cnt + nframes);
387 DEBUG_TRACE (DEBUG::LTC, string_compose ("speed_and_position - TID:%1 | latency: %2 | skip %3\n", ::pthread_self(), ltc_slave_latency.max, skip));
389 if (last_timestamp == 0) {
390 engine_dll_initstate = 0;
393 else if (engine_dll_initstate != transport_direction && ltc_speed != 0) {
394 engine_dll_initstate = transport_direction;
395 init_engine_dll(last_ltc_frame + rint(ltc_speed * double(2 * nframes + now - last_timestamp)),
396 session.engine().frames_per_cycle());
397 engine_init_called = true;
401 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC Process eng-tme: %1 eng-pos: %2\n", now, sess_pos));
402 /* when the jack-graph changes and if ardour performs
403 * locates, the audioengine is stopped (skipping frames) while
404 * jack [time] moves along.
407 DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 frames. Feeding silence to LTC parser.\n", skip));
408 if (skip >= 8192) skip = 8192;
409 unsigned char sound[8192];
410 memset(sound, 0, sizeof(char) * skip);
411 ltc_decoder_write(decoder, sound, nframes, now);
412 } else if (skip != 0) {
413 /* this should never happen. it may if monotonic_cnt, now overflow on 64bit */
414 DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 frames\n", skip));
418 parse_ltc(nframes, in, now - ltc_slave_latency.max );
422 if (last_timestamp == 0) {
423 DEBUG_TRACE (DEBUG::LTC, "last timestamp == 0\n");
425 pos = session.transport_frame();
427 } else if (ltc_speed != 0) {
431 if (abs(now - last_timestamp) > FLYWHEEL_TIMEOUT) {
432 DEBUG_TRACE (DEBUG::LTC, "flywheel timeout\n");
435 pos = session.transport_frame();
439 /* it take 2 cycles from naught to rolling.
440 * during these to initial cycles the speed == 0
443 * DEBUG::Slave: slave stopped, move to NNN
444 * DEBUG::Transport: Request forced locate to NNN
445 * DEBUG::Slave: slave state 0 @ NNN speed 0 cur delta VERY-LARGE-DELTA avg delta 1800
446 * DEBUG::Slave: silent motion
447 * DEBUG::Transport: realtime stop @ NNN
448 * DEBUG::Transport: Butler transport work, todo = PostTransportStop,PostTransportLocate,PostTransportClearSubstate
450 * [engine skips frames to locate, jack time keeps rolling on]
454 * DEBUG::LTC: [re-]init Engine DLL
455 * DEBUG::Slave: slave stopped, move to NNN+
458 * we need to seek two cycles ahead: 2 * nframes
460 if (engine_dll_initstate == 0) {
461 DEBUG_TRACE (DEBUG::LTC, "engine DLL not initialized. ltc_speed\n");
463 pos = last_ltc_frame + rint(ltc_speed * double(2 * nframes + now - last_timestamp));
467 /* interpolate position according to speed and time since last LTC-frame*/
468 double speed_flt = ltc_speed;
469 double elapsed = (now - last_timestamp) * speed_flt;
471 if (!engine_init_called) {
472 const double e = elapsed + double (last_ltc_frame - sess_pos);
476 speed_flt = (t1 - t0) / double(session.engine().frames_per_cycle());
477 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC engine DLL t0:%1 t1:%2 err:%3 spd:%4 ddt:%5\n", t0, t1, e, speed_flt, e2 - session.engine().frames_per_cycle() ));
479 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC adjusting elapsed (no DLL) from %1 by %2\n", elapsed, (2 * nframes * ltc_speed)));
481 elapsed += 2.0 * nframes * ltc_speed; /* see note above */
484 pos = last_ltc_frame + rint(elapsed);
486 current_delta = (pos - sess_pos);
488 if (((pos < 0) || (labs(current_delta) > 2 * session.frame_rate()))) {
489 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC large drift: %1\n", current_delta));
492 pos = session.transport_frame();
496 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTCsync spd: %1 pos: %2 | last-pos: %3 elapsed: %4 delta: %5\n",
497 speed, pos, last_ltc_frame, elapsed, current_delta));
499 /* provide a .1% deadzone to lock the speed */
500 if (fabs(speed - 1.0) <= 0.001) {
507 Timecode::TimecodeFormat
508 LTC_Slave::apparent_timecode_format () const
510 if (timecode.rate == 24 && !timecode.drop)
512 else if (timecode.rate == 25 && !timecode.drop)
514 else if (rint(timecode.rate * 100) == 2997 && !timecode.drop)
515 return (Config->get_timecode_source_2997() ? timecode_2997000 : timecode_2997);
516 else if (rint(timecode.rate * 100) == 2997 && timecode.drop)
517 return (Config->get_timecode_source_2997() ? timecode_2997000drop : timecode_2997drop);
518 else if (timecode.rate == 30 && timecode.drop)
519 return timecode_2997drop; // timecode_30drop; // LTC counting to 30 frames w/DF *means* 29.97 df
520 else if (timecode.rate == 30 && !timecode.drop)
523 /* XXX - unknown timecode format */
524 return session.config.get_timecode_format();
528 LTC_Slave::approximate_current_position() const
530 if (last_timestamp == 0) {
531 return " --:--:--:--";
533 return Timecode::timecode_format_time(timecode);
537 LTC_Slave::approximate_current_delta() const
540 if (last_timestamp == 0 || engine_dll_initstate == 0) {
541 snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
542 } else if ((monotonic_cnt - last_timestamp) > 2 * frames_per_ltc_frame) {
543 snprintf(delta, sizeof(delta), "flywheel");
545 snprintf(delta, sizeof(delta), "%s%4" PRIi64 " sm",
546 PLUSMINUS(-current_delta), abs(current_delta));
548 return std::string(delta);