e24a233b7b632976fb3768eb33520a987205acc2
[ardour.git] / libs / ardour / session_ltc.cc
1 /*
2   Copyright (C) 2012 Paul Davis
3   Written by 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
21 #include "timecode/time.h"
22
23 #include "ardour/audioengine.h"
24 #include "ardour/audio_port.h"
25 #include "ardour/debug.h"
26 #include "ardour/io.h"
27 #include "ardour/session.h"
28 #include "ardour/slave.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33 using namespace ARDOUR;
34 using namespace MIDI;
35 using namespace PBD;
36 using namespace Timecode;
37
38 /* really verbose timing debug */
39 //#define LTC_GEN_FRAMEDBUG
40 //#define LTC_GEN_TXDBUG
41
42 #ifndef MAX
43 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
44 #endif
45 #ifndef MIN
46 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
47 #endif
48
49 /* LTC signal should have a rise time of 25 us +/- 5 us.
50  * yet with most sound-cards a square-wave of 1-2 sample
51  * introduces rather some ringing and small oscillations.
52  * so we low-pass filter the signal a bit, depending
53  * on the sample-rate
54  */
55 #define LTC_RISE_TIME MIN (150, MAX(25, (4000000 / engine().frame_rate())))
56
57 void
58 Session::ltc_tx_initialize()
59 {
60         ltc_enc_tcformat = config.get_timecode_format();
61
62         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX init sr: %1 fps: %2\n", nominal_frame_rate(), timecode_to_frames_per_second(ltc_enc_tcformat)));
63         ltc_encoder = ltc_encoder_create(nominal_frame_rate(),
64                         timecode_to_frames_per_second(ltc_enc_tcformat),
65                         0);
66
67         ltc_encoder_set_bufsize(ltc_encoder, nominal_frame_rate(), 23.0);
68         ltc_encoder_set_filter(ltc_encoder, LTC_RISE_TIME);
69
70         /* buffersize for 1 LTC frame: (1 + sample-rate / fps) bytes
71          * usually returned by ltc_encoder_get_buffersize(encoder)
72          *
73          * since the fps can change and A3's  min fps: 24000/1001 */
74         ltc_enc_buf = (ltcsnd_sample_t*) calloc((nominal_frame_rate() / 23), sizeof(ltcsnd_sample_t));
75         ltc_speed = 0;
76         ltc_tx_reset();
77         ltc_tx_resync_latency();
78         Xrun.connect_same_thread (*this, boost::bind (&Session::ltc_tx_reset, this));
79         engine().GraphReordered.connect_same_thread (*this, boost::bind (&Session::ltc_tx_resync_latency, this));
80 }
81
82 void
83 Session::ltc_tx_cleanup()
84 {
85         DEBUG_TRACE (DEBUG::LTC, "LTC TX cleanup\n");
86         if (ltc_enc_buf) free(ltc_enc_buf);
87         ltc_encoder_free(ltc_encoder);
88         ltc_encoder = NULL;
89 }
90
91 void
92 Session::ltc_tx_resync_latency()
93 {
94         DEBUG_TRACE (DEBUG::LTC, "LTC TX resync latency\n");
95         if (!deletion_in_progress()) {
96                 boost::shared_ptr<Port> ltcport = ltc_output_port();
97                 if (ltcport) {
98                         ltcport->get_connected_latency_range(ltc_out_latency, true);
99                 }
100         }
101 }
102
103 void
104 Session::ltc_tx_reset()
105 {
106         DEBUG_TRACE (DEBUG::LTC, "LTC TX reset\n");
107         ltc_enc_pos = -9999; // force re-start
108         ltc_buf_len = 0;
109         ltc_buf_off = 0;
110         ltc_enc_byte = 0;
111         ltc_enc_cnt = 0;
112 }
113
114 void
115 Session::ltc_tx_recalculate_position()
116 {
117         SMPTETimecode enctc;
118         Timecode::Time a3tc;
119         ltc_encoder_get_timecode(ltc_encoder, &enctc);
120
121         a3tc.hours   = enctc.hours;
122         a3tc.minutes = enctc.mins;
123         a3tc.seconds = enctc.secs;
124         a3tc.frames  = enctc.frame;
125         a3tc.rate = timecode_to_frames_per_second(ltc_enc_tcformat);
126         a3tc.drop = timecode_has_drop_frames(ltc_enc_tcformat);
127
128         Timecode::timecode_to_sample (a3tc, ltc_enc_pos, true, false,
129                 double(frame_rate()),
130                 config.get_subframes_per_frame(),
131                 config.get_timecode_offset_negative(), config.get_timecode_offset()
132                 );
133 }
134
135 void
136 Session::ltc_tx_send_time_code_for_cycle (framepos_t start_frame, framepos_t end_frame,
137                                           double target_speed, double current_speed,
138                                           pframes_t nframes)
139 {
140         assert (nframes > 0);
141
142         Sample *out;
143         pframes_t txf = 0;
144         boost::shared_ptr<Port> ltcport = ltc_output_port();
145
146         Buffer& buf (ltcport->get_buffer (nframes));
147
148         if (!ltc_encoder || !ltc_enc_buf) {
149                 return;
150         }
151
152         SyncSource sync_src = Config->get_sync_source();
153         if (engine().freewheeling() || !Config->get_send_ltc()
154             /* TODO
155              * decide which time-sources we can generated LTC from.
156              * Internal, JACK or sample-synced slaves should be fine.
157              * talk to oofus.
158              *
159              || (config.get_external_sync() && sync_src == LTC)
160              || (config.get_external_sync() && sync_src == MTC)
161             */
162              ||(config.get_external_sync() && sync_src == MIDIClock)
163                 ) {
164                 return;
165         }
166
167         out = dynamic_cast<AudioBuffer*>(&buf)->data ();
168
169         /* range from libltc (38..218) || - 128.0  -> (-90..90) */
170         const float ltcvol = Config->get_ltc_output_volume()/(90.0); // pow(10, db/20.0)/(90.0);
171
172         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX %1 to %2 / %3 | lat: %4\n", start_frame, end_frame, nframes, ltc_out_latency.max));
173
174         /* all systems go. Now here's the plan:
175          *
176          *  1) check if fps has changed
177          *  2) check direction of encoding, calc speed, re-sample existing buffer
178          *  3) calculate frame and byte to send aligned to jack-period size
179          *  4) check if it's the frame/byte that is already in the queue
180          *  5) if (4) mismatch, re-calculate offset of LTC frame relative to period size
181          *  6) actual LTC audio output
182          *  6a) send remaining part of already queued frame; break on nframes
183          *  6b) encode new LTC-frame byte
184          *  6c) goto 6a
185          *  7) done
186          */
187
188         // (1) check fps
189         TimecodeFormat cur_timecode = config.get_timecode_format();
190         if (cur_timecode != ltc_enc_tcformat) {
191                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX1: TC format mismatch - reinit sr: %1 fps: %2\n", nominal_frame_rate(), timecode_to_frames_per_second(cur_timecode)));
192                 if (ltc_encoder_reinit(ltc_encoder, nominal_frame_rate(), timecode_to_frames_per_second(cur_timecode), 0)) {
193                         PBD::error << _("LTC encoder: invalid framerate - LTC encoding is disabled for the remainder of this session.") << endmsg;
194                         ltc_tx_cleanup();
195                         return;
196                 }
197                 if (nominal_frame_rate() <= 48000) {
198                         ltc_encoder_set_filter(ltc_encoder, LTC_RISE_TIME);
199                 }
200                 ltc_enc_tcformat = cur_timecode;
201                 ltc_tx_reset();
202         }
203
204         /* LTC is max. 30 fps */
205         if (timecode_to_frames_per_second(cur_timecode) > 30) {
206                 return;
207         }
208
209         // (2) speed & direction
210
211         /* speed 0 aka transport stopped is interpreted as rolling forward.
212          * keep repeating current frame
213          */
214 #define SIGNUM(a) ( (a) < 0 ? -1 : 1)
215         bool speed_changed = false;
216
217         /* port latency compensation:
218          * The _generated timecode_ is offset by the port-latency,
219          * therefore the offset depends on the direction of transport.
220          */
221         framepos_t cycle_start_frame = (current_speed < 0) ? (start_frame - ltc_out_latency.max) : (start_frame + ltc_out_latency.max);
222
223         /* cycle-start may become negative due to latency compensation */
224         if (cycle_start_frame < 0) { cycle_start_frame = 0; }
225
226         double new_ltc_speed = double(labs(end_frame - start_frame) * SIGNUM(current_speed)) / double(nframes);
227
228         if (SIGNUM(new_ltc_speed) != SIGNUM (ltc_speed)) {
229                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport changed direction\n");
230                 ltc_tx_reset();
231         }
232
233         if (ltc_speed != new_ltc_speed) {
234                 /* check ./libs/ardour/interpolation.cc  CubicInterpolation::interpolate
235                  * if target_speed != current_speed we should interpolate, too.
236                  *
237                  * However, currency in A3 target_speed == current_speed for each process cycle
238                  * (except for the sign and if target_speed > 8.0).
239                  * Besides, above speed calculation uses the difference (end_frame - start_frame).
240                  * end_frame is calculated from 'frames_moved' which includes the interpolation.
241                  * so we're good.
242                  */
243                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: speed change old: %1 cur: %2 tgt: %3 ctd: %4\n", ltc_speed, current_speed, target_speed, fabs(current_speed) - target_speed));
244                 speed_changed = true;
245         }
246
247         if (end_frame == start_frame || fabs(current_speed) < 0.1 ) {
248                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport is not rolling or absolute-speed < 0.1\n");
249                 /* keep repeating current frame
250                  *
251                  * an LTC generator must be able to continue generating LTC when Ardours transport is in stop
252                  * some machines do odd things if LTC goes away:
253                  * e.g. a tape based machine (video or audio), some think they have gone into park if LTC goes away,
254                  * so unspool the tape from the playhead. That might be inconvenient.
255                  * If LTC keeps arriving they remain in a stop position with the tape on the playhead.
256                  */
257                 new_ltc_speed = 0;
258                 if (!Config->get_ltc_send_continuously()) {
259                         ltc_speed = new_ltc_speed;
260                         return;
261                 }
262         }
263
264         if (fabs(new_ltc_speed) > 10.0) {
265                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: speed is out of bounds.\n");
266                 ltc_tx_reset();
267                 return;
268         }
269
270         if (ltc_speed == 0 && new_ltc_speed != 0) {
271                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport started rolling - reset\n");
272                 ltc_tx_reset();
273         }
274
275         /* the timecode duration corresponding to the samples that are still
276          * in the buffer. Here, the speed of previous cycle is used to calculate
277          * the alignment at the beginning of this cycle later.
278          */
279         double poff = (ltc_buf_len - ltc_buf_off) * ltc_speed;
280
281         if (speed_changed && new_ltc_speed != 0) {
282                 /* we need to re-sample the existing buffer.
283                  * "make space for the en-coder to catch up to the new speed"
284                  *
285                  * since the LTC signal is a rectangular waveform we can simply squeeze it
286                  * by removing samples or duplicating samples /here and there/.
287                  *
288                  * There may be a more elegant way to do this, in fact one could
289                  * simply re-render the buffer using ltc_encoder_encode_byte()
290                  * but that'd require some timecode offset buffer magic,
291                  * which is left for later..
292                  */
293
294                 double oldbuflen = double(ltc_buf_len - ltc_buf_off);
295                 double newbuflen = double(ltc_buf_len - ltc_buf_off) * fabs(ltc_speed / new_ltc_speed);
296
297                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: bufOld %1 bufNew %2 | diff %3\n",
298                                         (ltc_buf_len - ltc_buf_off), newbuflen, newbuflen - oldbuflen
299                                         ));
300
301                 double bufrspdiff = rint(newbuflen - oldbuflen);
302
303                 if (abs(bufrspdiff) > newbuflen || abs(bufrspdiff) > oldbuflen) {
304                         DEBUG_TRACE (DEBUG::LTC, "LTC TX2: resampling buffer would destroy information.\n");
305                         ltc_tx_reset();
306                         poff = 0;
307                 } else if (bufrspdiff != 0 && newbuflen > oldbuflen) {
308                         int incnt = 0;
309                         double samples_to_insert = ceil(newbuflen - oldbuflen);
310                         double avg_distance = newbuflen / samples_to_insert;
311                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: resample buffer insert: %1\n", samples_to_insert));
312
313                         for (int rp = ltc_buf_off; rp < ltc_buf_len - 1; ++rp) {
314                                 const int ro = rp - ltc_buf_off;
315                                 if (ro < (incnt*avg_distance)) continue;
316                                 const ltcsnd_sample_t v1 = ltc_enc_buf[rp];
317                                 const ltcsnd_sample_t v2 = ltc_enc_buf[rp+1];
318                                 if (v1 != v2 && ro < ((incnt+1)*avg_distance)) continue;
319                                 memmove(&ltc_enc_buf[rp+1], &ltc_enc_buf[rp], ltc_buf_len-rp);
320                                 incnt++;
321                                 ltc_buf_len++;
322                         }
323                 } else if (bufrspdiff != 0 && newbuflen < oldbuflen) {
324                         double samples_to_remove = ceil(oldbuflen - newbuflen);
325                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: resample buffer - remove: %1\n", samples_to_remove));
326                         if (oldbuflen <= samples_to_remove) {
327                                 ltc_buf_off = ltc_buf_len= 0;
328                         } else {
329                                 double avg_distance = newbuflen / samples_to_remove;
330                                 int rmcnt = 0;
331                                 for (int rp = ltc_buf_off; rp < ltc_buf_len - 1; ++rp) {
332                                         const int ro = rp - ltc_buf_off;
333                                         if (ro < (rmcnt*avg_distance)) continue;
334                                         const ltcsnd_sample_t v1 = ltc_enc_buf[rp];
335                                         const ltcsnd_sample_t v2 = ltc_enc_buf[rp+1];
336                                         if (v1 != v2 && ro < ((rmcnt+1)*avg_distance)) continue;
337                                         memmove(&ltc_enc_buf[rp], &ltc_enc_buf[rp+1], ltc_buf_len-rp-1);
338                                         ltc_buf_len--;
339                                         rmcnt++;
340                                 }
341                         }
342                 }
343         }
344
345         ltc_speed = new_ltc_speed;
346         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: transport speed %1.\n", ltc_speed));
347
348         // (3) bit/sample alignment
349         Timecode::Time tc_start;
350         framepos_t tc_sample_start;
351
352         /* calc timecode frame from current position - round down to nearest timecode */
353         sample_to_timecode(cycle_start_frame, tc_start, true, false);
354
355         /* convert timecode back to sample-position */
356         Timecode::timecode_to_sample (tc_start, tc_sample_start, true, false,
357                 double(frame_rate()),
358                 config.get_subframes_per_frame(),
359                 config.get_timecode_offset_negative(), config.get_timecode_offset()
360                 );
361
362         /* difference between current frame and TC frame in samples */
363         frameoffset_t soff = cycle_start_frame - tc_sample_start;
364         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX3: A3cycle: %1 = A3tc: %2 +off: %3\n",
365                                 cycle_start_frame, tc_sample_start, soff));
366
367
368         // (4) check if alignment matches
369         const double fptcf = frames_per_timecode_frame();
370
371         /* maximum difference of bit alignment in audio-samples.
372          *
373          * if transport and LTC generator differs more than this, the LTC
374          * generator will be re-initialized
375          *
376          * due to rounding error and variations in LTC-bit duration depending
377          * on the speed, it can be off by +- ltc_speed audio-samples.
378          * When the playback speed changes, it can actually reach +- 2 * ltc_speed
379          * in the cycle _after_ the speed changed. The average delta however is 0.
380          */
381         double maxdiff;
382
383         if (config.get_external_sync() && slave()) {
384                 maxdiff = slave()->resolution();
385         } else {
386                 maxdiff = ceil(fabs(ltc_speed))*2.0;
387         }
388
389         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: enc: %1 + %2 - %3 || buf-bytes: %4 enc-byte: %5\n",
390                                 ltc_enc_pos, ltc_enc_cnt, poff, (ltc_buf_len - ltc_buf_off), poff, ltc_enc_byte));
391
392         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: enc-pos: %1  | d: %2\n",
393                                 ltc_enc_pos + ltc_enc_cnt - poff,
394                                 rint(ltc_enc_pos + ltc_enc_cnt - poff) - cycle_start_frame
395                                 ));
396
397         if (ltc_speed != 0 && fabs(ceil(ltc_enc_pos + ltc_enc_cnt - poff) - cycle_start_frame) > maxdiff) {
398
399                 // (5) re-align
400                 ltc_tx_reset();
401
402                 /* set frame to encode */
403                 SMPTETimecode tc;
404                 tc.hours = tc_start.hours;
405                 tc.mins = tc_start.minutes;
406                 tc.secs = tc_start.seconds;
407                 tc.frame = tc_start.frames;
408                 ltc_encoder_set_timecode(ltc_encoder, &tc);
409
410                 /* workaround for libltc recognizing 29.97 and 30000/1001 as drop-frame TC.
411                  * In A3 30000/1001 or 30 fps can be drop-frame.
412                  */
413                 LTCFrame ltcframe;
414                 ltc_encoder_get_frame(ltc_encoder, &ltcframe);
415                 ltcframe.dfbit = timecode_has_drop_frames(cur_timecode)?1:0;
416                 ltc_encoder_set_frame(ltc_encoder, &ltcframe);
417
418
419                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: now: %1 trs: %2 toff %3\n", cycle_start_frame, tc_sample_start, soff));
420
421                 uint32_t cyc_off;
422                 if (soff < 0 || soff >= fptcf) {
423                         /* session framerate change between (2) and now */
424                         ltc_tx_reset();
425                         return;
426                 }
427
428                 if (ltc_speed < 0 ) {
429                         /* calculate the byte that starts at or after the current position */
430                         ltc_enc_byte = floor((10.0 * soff) / (fptcf));
431                         ltc_enc_cnt = double(ltc_enc_byte * fptcf / 10.0);
432
433                         /* calculate difference between the current position and the byte to send */
434                         cyc_off = soff- ceil(ltc_enc_cnt);
435
436                 } else {
437                         /* calculate the byte that starts at or after the current position */
438                         ltc_enc_byte = ceil((10.0 * soff) / fptcf);
439                         ltc_enc_cnt = double(ltc_enc_byte * fptcf / 10.0);
440
441                         /* calculate difference between the current position and the byte to send */
442                         cyc_off = ceil(ltc_enc_cnt) - soff;
443
444                         if (ltc_enc_byte == 10) {
445                                 ltc_enc_byte = 0;
446                                 ltc_encoder_inc_timecode(ltc_encoder);
447                         }
448                 }
449
450                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX5 restart encoder: soff %1 byte %2 cycoff %3\n",
451                                         soff, ltc_enc_byte, cyc_off));
452
453                 if (cyc_off > 0 && cyc_off <= nframes) {
454                         /* offset in this cycle */
455                         txf= rint(cyc_off / fabs(ltc_speed));
456                         memset(out, 0, cyc_off * sizeof(Sample));
457                 } else {
458                         /* resync next cycle */
459                         memset(out, 0, nframes * sizeof(Sample));
460                         return;
461                 }
462
463                 ltc_enc_pos = tc_sample_start;
464
465                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX5 restart @ %1 + %2 - %3 |  byte %4\n",
466                                         ltc_enc_pos, ltc_enc_cnt, cyc_off, ltc_enc_byte));
467         }
468
469
470         // (6) encode and output
471         while (1) {
472 #ifdef LTC_GEN_TXDBUG
473                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.1 @%1  [ %2 / %3 ]\n", txf, ltc_buf_off, ltc_buf_len));
474 #endif
475                 // (6a) send remaining buffer
476                 while ((ltc_buf_off < ltc_buf_len) && (txf < nframes)) {
477                         const float v1 = ltc_enc_buf[ltc_buf_off++] - 128.0;
478                         const Sample val = (Sample) (v1*ltcvol);
479                         out[txf++] = val;
480                 }
481 #ifdef LTC_GEN_TXDBUG
482                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.2 @%1  [ %2 / %3 ]\n", txf, ltc_buf_off, ltc_buf_len));
483 #endif
484
485                 if (txf >= nframes) {
486                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX7 enc: %1 [ %2 / %3 ] byte: %4 spd %5 fpp %6 || nf: %7\n",
487                                                 ltc_enc_pos, ltc_buf_off, ltc_buf_len, ltc_enc_byte, ltc_speed, nframes, txf));
488                         break;
489                 }
490
491                 ltc_buf_len = 0;
492                 ltc_buf_off = 0;
493
494                 // (6b) encode LTC, bump timecode
495
496                 if (ltc_speed < 0) {
497                         ltc_enc_byte = (ltc_enc_byte + 9)%10;
498                         if (ltc_enc_byte == 9) {
499                                 ltc_encoder_dec_timecode(ltc_encoder);
500                                 ltc_tx_recalculate_position();
501                                 ltc_enc_cnt = fptcf;
502                         }
503                 }
504
505                 if (ltc_encoder_encode_byte(ltc_encoder, ltc_enc_byte, (ltc_speed==0)?1.0:(1.0/ltc_speed))) {
506                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.3 encoder error byte %1\n", ltc_enc_byte));
507                         ltc_encoder_buffer_flush(ltc_encoder);
508                         ltc_tx_reset();
509                         return;
510                 }
511                 int enc_frames = ltc_encoder_get_buffer(ltc_encoder, &(ltc_enc_buf[ltc_buf_len]));
512 #ifdef LTC_GEN_FRAMEDBUG
513                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.3 encoded %1 bytes for LTC-byte %2 at spd %3\n", enc_frames, ltc_enc_byte, ltc_speed));
514 #endif
515                 if (enc_frames <=0) {
516                         DEBUG_TRACE (DEBUG::LTC, "LTC TX6.3 encoder empty buffer.\n");
517                         ltc_encoder_buffer_flush(ltc_encoder);
518                         ltc_tx_reset();
519                         return;
520                 }
521
522                 ltc_buf_len += enc_frames;
523                 if (ltc_speed < 0)
524                         ltc_enc_cnt -= fptcf/10.0;
525                 else
526                         ltc_enc_cnt += fptcf/10.0;
527
528                 if (ltc_speed >= 0) {
529                         ltc_enc_byte = (ltc_enc_byte + 1)%10;
530                         if (ltc_enc_byte == 0 && ltc_speed != 0) {
531                                 ltc_encoder_inc_timecode(ltc_encoder);
532                                 ltc_tx_recalculate_position();
533                                 ltc_enc_cnt = 0;
534                         } else if (ltc_enc_byte == 0) {
535                                 ltc_enc_cnt = 0;
536                         }
537                 }
538 #ifdef LTC_GEN_FRAMEDBUG
539                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.4 enc-pos: %1 + %2 [ %4 / %5 ] spd %6\n", ltc_enc_pos, ltc_enc_cnt, ltc_buf_off, ltc_buf_len, ltc_speed));
540 #endif
541         }
542
543         dynamic_cast<AudioBuffer*>(&buf)->set_written (true);
544         return;
545 }