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