change is the only constant, again
[ardour.git] / libs / ardour / ltc_slave.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3     Witten by 2012 Robin Gareus <robin@gareus.org>
4
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.
9
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.
14
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.
18
19 */
20 #include <iostream>
21 #include <errno.h>
22 #include <poll.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "pbd/error.h"
27
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"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace MIDI;
39 using namespace PBD;
40 using namespace Timecode;
41
42 #define FLYWHEEL_TIMEOUT ( 1 * session.frame_rate() )
43
44 LTC_Slave::LTC_Slave (Session& s)
45         : session (s)
46 {
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();
50
51         did_reset_tc_format = false;
52         delayedlocked = 10;
53         monotonic_cnt = 0;
54         fps_detected=false;
55
56         ltc_timecode = timecode_60; // track changes of LTC fps
57         a3e_timecode = timecode_60; // track changes of Ardour's fps
58         printed_timecode_warning = false;
59         ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
60         memset(&prev_frame, 0, sizeof(LTCFrameExt));
61
62         decoder = ltc_decoder_create((int) frames_per_ltc_frame, 128 /*queue size*/);
63         reset();
64         session.Xrun.connect_same_thread (port_connections, boost::bind (&LTC_Slave::resync_latency, this));
65         session.engine().GraphReordered.connect_same_thread (port_connections, boost::bind (&LTC_Slave::resync_latency, this));
66 }
67
68 LTC_Slave::~LTC_Slave()
69 {
70         port_connections.drop_connections();
71
72         if (did_reset_tc_format) {
73                 session.config.set_timecode_format (saved_tc_format);
74         }
75
76         ltc_decoder_free(decoder);
77 }
78
79 ARDOUR::framecnt_t
80 LTC_Slave::resolution () const
81 {
82         return (framecnt_t) (frames_per_ltc_frame);
83 }
84
85 bool
86 LTC_Slave::locked () const
87 {
88         return (delayedlocked < 5);
89 }
90
91 bool
92 LTC_Slave::ok() const
93 {
94         return true;
95 }
96
97 void
98 LTC_Slave::resync_latency()
99 {
100         DEBUG_TRACE (DEBUG::LTC, "LTC resync()\n");
101         engine_dll_initstate = 0;
102
103         if (session.ltc_output_io()) { /* check if Port exits */
104                 boost::shared_ptr<Port> ltcport = session.ltc_input_port();
105                 ltcport->get_connected_latency_range(ltc_slave_latency, false);
106         }
107 }
108
109 void
110 LTC_Slave::reset()
111 {
112         DEBUG_TRACE (DEBUG::LTC, "LTC reset()\n");
113         last_timestamp = 0;
114         current_delta = 0;
115         transport_direction = 0;
116         ltc_speed = 0;
117         engine_dll_initstate = 0;
118         resync_latency();
119 }
120
121 void
122 LTC_Slave::parse_ltc(const jack_nframes_t nframes, const jack_default_audio_sample_t * const in, const framecnt_t posinfo)
123 {
124         jack_nframes_t i;
125         unsigned char sound[8192];
126         if (nframes > 8192) {
127                 /* TODO warn once or wrap, loop conversion below
128                  * does jack/A3 support > 8192 spp anyway?
129                  */
130                 return;
131         }
132
133         for (i = 0; i < nframes; i++) {
134                 const int snd=(int)rint((127.0*in[i])+128.0);
135                 sound[i] = (unsigned char) (snd&0xff);
136         }
137         ltc_decoder_write(decoder, sound, nframes, posinfo);
138         return;
139 }
140
141 bool
142 LTC_Slave::equal_ltc_frame_time(LTCFrame *a, LTCFrame *b) {
143         if (       a->frame_units != b->frame_units
144                 || a->frame_tens  != b->frame_tens
145                 || a->dfbit       != b->dfbit
146                 || a->secs_units  != b->secs_units
147                 || a->secs_tens   != b->secs_tens
148                 || a->mins_units  != b->mins_units
149                 || a->mins_tens   != b->mins_tens
150                 || a->hours_units != b->hours_units
151                 || a->hours_tens  != b->hours_tens
152              ) {
153                 return false;
154         }
155         return true;
156 }
157
158 bool
159 LTC_Slave::detect_discontinuity(LTCFrameExt *frame, int fps, bool fuzzy) {
160         bool discontinuity_detected = false;
161
162         if (fuzzy && (
163                   ( frame->reverse && prev_frame.ltc.frame_units == 0)
164                 ||(!frame->reverse && frame->ltc.frame_units == 0)
165                 )) {
166                 memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
167                 return false;
168         }
169
170         if (frame->reverse) {
171                 ltc_frame_decrement(&prev_frame.ltc, fps , 0);
172         } else {
173                 ltc_frame_increment(&prev_frame.ltc, fps , 0);
174         }
175         if (!equal_ltc_frame_time(&prev_frame.ltc, &frame->ltc)) {
176                 discontinuity_detected = true;
177         }
178
179     memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
180     return discontinuity_detected;
181 }
182
183 bool
184 LTC_Slave::detect_ltc_fps(int frameno, bool df)
185 {
186         bool fps_changed = false;
187         double detected_fps = 0;
188         if (frameno > ltc_detect_fps_max)
189         {
190                 ltc_detect_fps_max = frameno;
191         }
192         ltc_detect_fps_cnt++;
193
194         if (ltc_detect_fps_cnt > 40)
195         {
196                 if (ltc_detect_fps_cnt > ltc_detect_fps_max
197                     && (   ceil(timecode.rate) != (ltc_detect_fps_max + 1)
198                         || timecode.drop != df
199                         )
200                     )
201                 {
202                         DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC detected FPS %1%2",
203                                         ltc_detect_fps_max + 1, timecode.drop ? "df" : ""));
204                         detected_fps = ltc_detect_fps_max + 1;
205                         if (df) {
206                                 /* LTC df -> indicates fractional framerate */
207                                 detected_fps = detected_fps * 1000.0 / 1001.0;
208                         }
209                         DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC detected FPS: %1%2\n", detected_fps, df?"df":"ndf"));
210                 }
211                 ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
212         }
213
214         /* when changed */
215         if (detected_fps != 0 && (detected_fps != timecode.rate || df != timecode.drop)) {
216                 timecode.rate = detected_fps;
217                 timecode.drop = df;
218                 frames_per_ltc_frame = double(session.frame_rate()) / timecode.rate;
219                 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC reset to FPS: %1%2 ; audio-frames per LTC: %3\n",
220                                 detected_fps, df?"df":"ndf", frames_per_ltc_frame));
221                 fps_changed=true;
222         }
223
224         /* poll and check session TC */
225         TimecodeFormat tc_format = apparent_timecode_format();
226         TimecodeFormat cur_timecode = session.config.get_timecode_format();
227         if (Config->get_timecode_sync_frame_rate()) {
228                 /* enforce time-code */
229                 if (!did_reset_tc_format) {
230                         saved_tc_format = cur_timecode;
231                         did_reset_tc_format = true;
232                 }
233                 if (cur_timecode != tc_format) {
234                         warning << string_compose(_("Session framerate adjusted from %1 to LTC's %2."),
235                                         Timecode::timecode_format_name(cur_timecode),
236                                         Timecode::timecode_format_name(tc_format))
237                                 << endmsg;
238                         session.config.set_timecode_format (tc_format);
239                 }
240         } else {
241                 /* only warn about TC mismatch */
242                 if (ltc_timecode != tc_format) printed_timecode_warning = false;
243                 if (a3e_timecode != cur_timecode) printed_timecode_warning = false;
244
245                 if (cur_timecode != tc_format && ! printed_timecode_warning) {
246                         warning << string_compose(_("Session and LTC framerate mismatch: LTC:%1 Session:%2."),
247                                         Timecode::timecode_format_name(tc_format),
248                                         Timecode::timecode_format_name(cur_timecode))
249                                 << endmsg;
250                         printed_timecode_warning = true;
251                 }
252         }
253         ltc_timecode = tc_format;
254         a3e_timecode = cur_timecode;
255
256         return fps_changed;
257 }
258
259 void
260 LTC_Slave::process_ltc(framepos_t const now)
261 {
262         LTCFrameExt frame;
263         while (ltc_decoder_read(decoder, &frame)) {
264                 SMPTETimecode stime;
265
266                 ltc_frame_to_time(&stime, &frame.ltc, 0);
267                 timecode.negative  = false;
268                 timecode.subframes  = 0;
269
270                 /* set timecode.rate and timecode.drop: */
271                 bool ltc_is_static = equal_ltc_frame_time(&prev_frame.ltc, &frame.ltc);
272
273                 if (detect_discontinuity(&frame, ceil(timecode.rate), !fps_detected)) {
274                         if (fps_detected) { ltc_detect_fps_cnt = ltc_detect_fps_max = 0; }
275                         fps_detected=false;
276                 }
277
278                 if (!ltc_is_static && detect_ltc_fps(stime.frame, (frame.ltc.dfbit)? true : false)) {
279                         reset();
280                         fps_detected=true;
281                 }
282
283 #if 0 // Devel/Debug
284                 fprintf(stdout, "LTC %02d:%02d:%02d%c%02d | %8lld %8lld%s\n",
285                         stime.hours,
286                         stime.mins,
287                         stime.secs,
288                         (frame.ltc.dfbit) ? '.' : ':',
289                         stime.frame,
290                         frame.off_start,
291                         frame.off_end,
292                         frame.reverse ? " R" : "  "
293                         );
294 #endif
295
296                 /* when a full LTC frame is decoded, the timecode the LTC frame
297                  * is referring has just passed.
298                  * So we send the _next_ timecode which
299                  * is expected to start at the end of the current frame
300                  */
301                 int fps_i = ceil(timecode.rate);
302                 if (!frame.reverse) {
303                         ltc_frame_increment(&frame.ltc, fps_i , 0);
304                         ltc_frame_to_time(&stime, &frame.ltc, 0);
305                         transport_direction = 1;
306                 } else {
307                         ltc_frame_decrement(&frame.ltc, fps_i , 0);
308                         int off = frame.off_end - frame.off_start;
309                         frame.off_start += off;
310                         frame.off_end += off;
311                         transport_direction = -1;
312                 }
313
314                 timecode.hours   = stime.hours;
315                 timecode.minutes = stime.mins;
316                 timecode.seconds = stime.secs;
317                 timecode.frames  = stime.frame;
318
319                 /* map LTC timecode to session TC setting */
320                 framepos_t ltc_frame; ///< audio-frame corresponding to LTC frame
321                 Timecode::timecode_to_sample (timecode, ltc_frame, true, false,
322                         double(session.frame_rate()),
323                         session.config.get_subframes_per_frame(),
324                         session.config.get_timecode_offset_negative(), session.config.get_timecode_offset()
325                         );
326
327                 framepos_t cur_timestamp = frame.off_end + 1;
328                 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));
329                 if (frame.off_end + 1 <= last_timestamp || last_timestamp == 0) {
330                         DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: UNCHANGED: %1\n", ltc_speed));
331                 } else {
332                         ltc_speed = double(ltc_frame - last_ltc_frame) / double(cur_timestamp - last_timestamp);
333                         DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: %1\n", ltc_speed));
334                 }
335
336                 if (fabs(ltc_speed) > 10.0) {
337                         ltc_speed = 0;
338                 }
339
340                 last_timestamp = frame.off_end + 1;
341                 last_ltc_frame = ltc_frame;
342         } /* end foreach decoded LTC frame */
343 }
344
345 void
346 LTC_Slave::init_engine_dll (framepos_t pos, int32_t inc)
347 {
348         double omega = 2.0 * M_PI * double(inc) / double(session.frame_rate());
349         b = 1.4142135623730950488 * omega;
350         c = omega * omega;
351
352         e2 = double(ltc_speed * inc);
353         t0 = double(pos);
354         t1 = t0 + e2;
355         DEBUG_TRACE (DEBUG::LTC, string_compose ("[re-]init Engine DLL %1 %2 %3\n", t0, t1, e2));
356 }
357
358 /* main entry point from session_process.cc
359  * called from jack_process callback context
360  * so it is OK to use jack_port_get_buffer()
361  */
362 bool
363 LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
364 {
365         bool engine_init_called = false;
366         framepos_t now = session.engine().frame_time_at_cycle_start();
367         framepos_t sess_pos = session.transport_frame(); // corresponds to now
368         framecnt_t nframes = session.engine().frames_per_cycle();
369
370         jack_default_audio_sample_t *in;
371
372         boost::shared_ptr<Port> ltcport = session.ltc_input_port();
373
374 #if 1
375         /* TODO read layency only on demand -> ::reset()
376          * this is already prepared.
377          *
378          * ..but first fix jack2 issue with re-computing latency
379          * in the correct order. Until then, querying it in the
380          * process-callback is the only way to get the current value
381          */
382         ltcport->get_connected_latency_range(ltc_slave_latency, false);
383 #endif
384         in = (jack_default_audio_sample_t*) jack_port_get_buffer (ltcport->jack_port(), nframes);
385
386         frameoffset_t skip = now - (monotonic_cnt + nframes);
387         monotonic_cnt = now;
388         DEBUG_TRACE (DEBUG::LTC, string_compose ("speed_and_position - TID:%1 | latency: %2 | skip %3\n", ::pthread_self(), ltc_slave_latency.max, skip));
389
390         if (last_timestamp == 0) {
391                 engine_dll_initstate = 0;
392                 delayedlocked++;
393         }
394         else if (engine_dll_initstate != transport_direction && ltc_speed != 0) {
395                 engine_dll_initstate = transport_direction;
396                 init_engine_dll(last_ltc_frame + rint(ltc_speed * double(2 * nframes + now - last_timestamp)),
397                                 session.engine().frames_per_cycle());
398                 engine_init_called = true;
399         }
400
401         if (in) {
402                 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC Process eng-tme: %1 eng-pos: %2\n", now, sess_pos));
403                 /* when the jack-graph changes and if ardour performs
404                  * locates, the audioengine is stopped (skipping frames) while
405                  * jack [time] moves along.
406                  */
407                 if (skip > 0) {
408                         DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 frames. Feeding silence to LTC parser.\n", skip));
409                         if (skip >= 8192) skip = 8192;
410                         unsigned char sound[8192];
411                         memset(sound, 0, sizeof(char) * skip);
412                         ltc_decoder_write(decoder, sound, nframes, now);
413                 } else if (skip != 0) {
414                         /* this should never happen. it may if monotonic_cnt, now overflow on 64bit */
415                         DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 frames\n", skip));
416                         reset();
417                 }
418
419                 parse_ltc(nframes, in, now + ltc_slave_latency.max );
420                 process_ltc(now);
421         }
422
423         if (last_timestamp == 0) {
424                 DEBUG_TRACE (DEBUG::LTC, "last timestamp == 0\n");
425                 speed = 0;
426                 pos = session.transport_frame();
427                 return true;
428         } else if (ltc_speed != 0) {
429                 delayedlocked = 0;
430         }
431
432         if (abs(now - last_timestamp) > FLYWHEEL_TIMEOUT) {
433                 DEBUG_TRACE (DEBUG::LTC, "flywheel timeout\n");
434                 reset();
435                 speed = 0;
436                 pos = session.transport_frame();
437                 return true;
438         }
439
440         /* it take 2 cycles from naught to rolling.
441          * during these to initial cycles the speed == 0
442          *
443          * the first cycle:
444          * DEBUG::Slave: slave stopped, move to NNN
445          * DEBUG::Transport: Request forced locate to NNN
446          * DEBUG::Slave: slave state 0 @ NNN speed 0 cur delta VERY-LARGE-DELTA avg delta 1800
447          * DEBUG::Slave: silent motion
448          * DEBUG::Transport: realtime stop @ NNN
449          * DEBUG::Transport: Butler transport work, todo = PostTransportStop,PostTransportLocate,PostTransportClearSubstate
450          *
451          * [engine skips frames to locate, jack time keeps rolling on]
452          *
453          * the second cycle:
454          *
455          * DEBUG::LTC: [re-]init Engine DLL
456          * DEBUG::Slave: slave stopped, move to NNN+
457          * ...
458          *
459          * we need to seek two cycles ahead: 2 * nframes
460          */
461         if (engine_dll_initstate == 0) {
462                 DEBUG_TRACE (DEBUG::LTC, "engine DLL not initialized. ltc_speed\n");
463                 speed = 0;
464                 pos = last_ltc_frame + rint(ltc_speed * double(2 * nframes + now - last_timestamp));
465                 return true;
466         }
467
468         /* interpolate position according to speed and time since last LTC-frame*/
469         double speed_flt = ltc_speed;
470         double elapsed = (now - last_timestamp) * speed_flt;
471
472         if (!engine_init_called) {
473                 const double e = elapsed + double (last_ltc_frame - sess_pos);
474                 t0 = t1;
475                 t1 += b * e + e2;
476                 e2 += c * e;
477                 speed_flt = (t1 - t0) / double(session.engine().frames_per_cycle());
478                 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         } else {
480                 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC adjusting elapsed (no DLL) from %1 by %2\n", elapsed, (2 * nframes * ltc_speed)));
481                 speed_flt = 0;
482                 elapsed += 2.0 * nframes * ltc_speed; /* see note above */
483         }
484
485         pos = last_ltc_frame + rint(elapsed);
486         speed = speed_flt;
487         current_delta = (pos - sess_pos);
488
489         if (((pos < 0) || (labs(current_delta) > 2 * session.frame_rate()))) {
490                 DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC large drift: %1\n", current_delta));
491                 reset();
492                 speed = 0;
493                 pos = session.transport_frame();
494                 return true;
495         }
496
497         DEBUG_TRACE (DEBUG::LTC, string_compose ("LTCsync spd: %1 pos: %2 | last-pos: %3 elapsed: %4 delta: %5\n",
498                                                  speed, pos, last_ltc_frame, elapsed, current_delta));
499
500 #if 1 /* provide a .1% deadzone to lock the speed */
501         if (fabs(speed - 1.0) <= 0.001)
502                 speed = 1.0;
503 #endif
504
505         return true;
506 }
507
508 Timecode::TimecodeFormat
509 LTC_Slave::apparent_timecode_format () const
510 {
511         if      (timecode.rate == 24 && !timecode.drop)
512                 return timecode_24;
513         else if (timecode.rate == 25 && !timecode.drop)
514                 return timecode_25;
515         else if (rint(timecode.rate * 100) == 2997 && !timecode.drop)
516                 return timecode_2997;
517         else if (rint(timecode.rate * 100) == 2997 &&  timecode.drop)
518                 return timecode_2997drop;
519         else if (timecode.rate == 30 &&  timecode.drop)
520                 return timecode_2997drop; // timecode_30drop; // LTC counting to 30 frames w/DF *means* 29.97 df
521         else if (timecode.rate == 30 && !timecode.drop)
522                 return timecode_30;
523
524         /* XXX - unknown timecode format */
525         return session.config.get_timecode_format();
526 }
527
528 std::string
529 LTC_Slave::approximate_current_position() const
530 {
531         if (last_timestamp == 0) {
532                 return " --:--:--:--";
533         }
534         return Timecode::timecode_format_time(timecode);
535 }
536
537 std::string
538 LTC_Slave::approximate_current_delta() const
539 {
540         char delta[24];
541         if (last_timestamp == 0 || engine_dll_initstate == 0) {
542                 snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
543         } else if ((monotonic_cnt - last_timestamp) > 2 * frames_per_ltc_frame) {
544                 snprintf(delta, sizeof(delta), "flywheel");
545         } else {
546                 snprintf(delta, sizeof(delta), "%s%4" PRIi64 " sm",
547                                 PLUSMINUS(-current_delta), abs(current_delta));
548         }
549         return std::string(delta);
550 }