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