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