2 Copyright (C) 2012 Paul Davis
3 Written by Robin Gareus <robin@gareus.org>
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.
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.
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.
21 #include "timecode/time.h"
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"
33 using namespace ARDOUR;
35 using namespace Timecode;
37 /* really verbose timing debug */
38 //#define LTC_GEN_FRAMEDBUG
39 //#define LTC_GEN_TXDBUG
42 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
45 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
48 /* LTC signal should have a rise time of 25 us +/- 5 us.
49 * yet with most sound-cards a square-wave of 1-2 sample
50 * introduces ringing and small oscillations.
51 * https://en.wikipedia.org/wiki/Gibbs_phenomenon
52 * A low-pass filter in libltc can reduce this at
53 * the cost of being slightly out of spec WRT to rise-time.
55 * This filter is adaptive so that fast vari-speed signals
56 * will not be affected by it.
58 #define LTC_RISE_TIME(speed) MIN (100, MAX(40, (4000000 / ((speed==0)?1:speed) / engine().sample_rate())))
60 #define TV_STANDARD(tcf) \
61 (timecode_to_frames_per_second(tcf)==25.0 ? LTC_TV_625_50 : \
62 timecode_has_drop_frames(tcf)? LTC_TV_525_60 : LTC_TV_FILM_24)
65 Session::ltc_tx_initialize()
67 ltc_enc_tcformat = config.get_timecode_format();
69 ltc_tx_parse_offset();
70 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)));
71 ltc_encoder = ltc_encoder_create(nominal_frame_rate(),
72 timecode_to_frames_per_second(ltc_enc_tcformat),
73 TV_STANDARD(ltc_enc_tcformat), 0);
75 ltc_encoder_set_bufsize(ltc_encoder, nominal_frame_rate(), 23.0);
76 ltc_encoder_set_filter(ltc_encoder, LTC_RISE_TIME(1.0));
78 /* buffersize for 1 LTC frame: (1 + sample-rate / fps) bytes
79 * usually returned by ltc_encoder_get_buffersize(encoder)
81 * since the fps can change and A3's min fps: 24000/1001 */
82 ltc_enc_buf = (ltcsnd_sample_t*) calloc((nominal_frame_rate() / 23), sizeof(ltcsnd_sample_t));
86 ltc_tx_resync_latency();
87 Xrun.connect_same_thread (ltc_tx_connections, boost::bind (&Session::ltc_tx_reset, this));
88 engine().GraphReordered.connect_same_thread (ltc_tx_connections, boost::bind (&Session::ltc_tx_resync_latency, this));
93 Session::ltc_tx_cleanup()
95 DEBUG_TRACE (DEBUG::LTC, "LTC TX cleanup\n");
98 ltc_encoder_free(ltc_encoder);
100 ltc_tx_connections.drop_connections ();
104 Session::ltc_tx_resync_latency()
106 DEBUG_TRACE (DEBUG::LTC, "LTC TX resync latency\n");
107 if (!deletion_in_progress()) {
108 boost::shared_ptr<Port> ltcport = ltc_output_port();
110 ltcport->get_connected_latency_range(ltc_out_latency, true);
116 Session::ltc_tx_reset()
118 DEBUG_TRACE (DEBUG::LTC, "LTC TX reset\n");
119 assert (ltc_encoder);
120 ltc_enc_pos = -9999; // force re-start
126 ltc_encoder_reset(ltc_encoder);
130 Session::ltc_tx_parse_offset() {
131 Timecode::Time offset_tc;
132 Timecode::parse_timecode_format(config.get_timecode_generator_offset(), offset_tc);
133 offset_tc.rate = timecode_frames_per_second();
134 offset_tc.drop = timecode_drop_frames();
135 timecode_to_sample(offset_tc, ltc_timecode_offset, false, false);
136 ltc_timecode_negative_offset = !offset_tc.negative;
141 Session::ltc_tx_recalculate_position()
145 ltc_encoder_get_timecode(ltc_encoder, &enctc);
147 a3tc.hours = enctc.hours;
148 a3tc.minutes = enctc.mins;
149 a3tc.seconds = enctc.secs;
150 a3tc.frames = enctc.frame;
151 a3tc.rate = timecode_to_frames_per_second(ltc_enc_tcformat);
152 a3tc.drop = timecode_has_drop_frames(ltc_enc_tcformat);
154 Timecode::timecode_to_sample (a3tc, ltc_enc_pos, true, false,
155 (double)frame_rate(),
156 config.get_subframes_per_frame(),
157 ltc_timecode_negative_offset, ltc_timecode_offset
163 Session::ltc_tx_send_time_code_for_cycle (framepos_t start_frame, framepos_t end_frame,
164 double target_speed, double current_speed,
167 assert (nframes > 0);
171 boost::shared_ptr<Port> ltcport = ltc_output_port();
173 Buffer& buf (ltcport->get_buffer (nframes));
175 if (!ltc_encoder || !ltc_enc_buf) {
179 SyncSource sync_src = Config->get_sync_source();
180 if (engine().freewheeling() || !Config->get_send_ltc()
182 * decide which time-sources we can generated LTC from.
183 * Internal, JACK or sample-synced slaves should be fine.
186 || (config.get_external_sync() && sync_src == LTC)
187 || (config.get_external_sync() && sync_src == MTC)
189 ||(config.get_external_sync() && sync_src == MIDIClock)
194 out = dynamic_cast<AudioBuffer*>(&buf)->data ();
196 /* range from libltc (38..218) || - 128.0 -> (-90..90) */
197 const float ltcvol = Config->get_ltc_output_volume()/(90.0); // pow(10, db/20.0)/(90.0);
199 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX %1 to %2 / %3 | lat: %4\n", start_frame, end_frame, nframes, ltc_out_latency.max));
201 /* all systems go. Now here's the plan:
203 * 1) check if fps has changed
204 * 2) check direction of encoding, calc speed, re-sample existing buffer
205 * 3) calculate frame and byte to send aligned to jack-period size
206 * 4) check if it's the frame/byte that is already in the queue
207 * 5) if (4) mismatch, re-calculate offset of LTC frame relative to period size
208 * 6) actual LTC audio output
209 * 6a) send remaining part of already queued frame; break on nframes
210 * 6b) encode new LTC-frame byte
216 TimecodeFormat cur_timecode = config.get_timecode_format();
217 if (cur_timecode != ltc_enc_tcformat) {
218 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)));
219 if (ltc_encoder_reinit(ltc_encoder, nominal_frame_rate(),
220 timecode_to_frames_per_second(cur_timecode),
221 TV_STANDARD(cur_timecode), 0
223 PBD::error << _("LTC encoder: invalid framerate - LTC encoding is disabled for the remainder of this session.") << endmsg;
227 ltc_encoder_set_filter(ltc_encoder, LTC_RISE_TIME(ltc_speed));
228 ltc_enc_tcformat = cur_timecode;
229 ltc_tx_parse_offset();
233 /* LTC is max. 30 fps */
234 if (timecode_to_frames_per_second(cur_timecode) > 30) {
238 // (2) speed & direction
240 /* speed 0 aka transport stopped is interpreted as rolling forward.
241 * keep repeating current frame
243 #define SIGNUM(a) ( (a) < 0 ? -1 : 1)
244 bool speed_changed = false;
246 /* port latency compensation:
247 * The _generated timecode_ is offset by the port-latency,
248 * therefore the offset depends on the direction of transport.
250 * latency is compensated by adding it to the timecode to
251 * be generated. e.g. if the signal will reach the output in
252 * N samples time from now, generate the timecode for (now + N).
254 * sample-sync is achieved by further calculating the difference
255 * between the timecode and the session-transport and offsetting the
258 * The timecode is generated directly in the Session process callback
259 * using _transport_frame. It requires that the session has set the
260 * port's playback latency to worst_playback_latency() prior to
261 * calling ltc_tx_send_time_code_for_cycle().
263 framepos_t cycle_start_frame;
265 if (current_speed < 0) {
266 cycle_start_frame = (start_frame - ltc_out_latency.max + worst_playback_latency());
267 } else if (current_speed > 0) {
268 cycle_start_frame = (start_frame + ltc_out_latency.max - worst_playback_latency());
270 /* There is no need to compensate for latency when not rolling
271 * rather send the accurate NOW timecode
272 * (LTC encoder compenates latency by sending earlier timecode)
274 cycle_start_frame = start_frame;
277 /* LTC TV standard offset */
278 if (current_speed != 0) {
279 /* ditto - send "NOW" if not rolling */
280 cycle_start_frame -= ltc_frame_alignment(samples_per_timecode_frame(), TV_STANDARD(cur_timecode));
283 /* cycle-start may become negative due to latency compensation */
284 if (cycle_start_frame < 0) { cycle_start_frame = 0; }
286 double new_ltc_speed = (double)(labs(end_frame - start_frame) * SIGNUM(current_speed)) / (double)nframes;
287 if (nominal_frame_rate() != frame_rate()) {
288 new_ltc_speed *= (double)nominal_frame_rate() / (double)frame_rate();
291 if (SIGNUM(new_ltc_speed) != SIGNUM (ltc_speed)) {
292 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport changed direction\n");
296 if (ltc_speed != new_ltc_speed
297 /* but only once if, current_speed changes to 0. In that case
298 * new_ltc_speed is > 0 because (end_frame - start_frame) == jack-period for no-roll
299 * but ltc_speed will still be 0
301 && (current_speed != 0 || ltc_speed != current_speed)
303 /* check ./libs/ardour/interpolation.cc CubicInterpolation::interpolate
304 * if target_speed != current_speed we should interpolate, too.
306 * However, currency in A3 target_speed == current_speed for each process cycle
307 * (except for the sign and if target_speed > 8.0).
308 * Besides, above speed calculation uses the difference (end_frame - start_frame).
309 * end_frame is calculated from 'frames_moved' which includes the interpolation.
312 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, new_ltc_speed));
313 speed_changed = true;
314 ltc_encoder_set_filter(ltc_encoder, LTC_RISE_TIME(new_ltc_speed));
317 if (end_frame == start_frame || fabs(current_speed) < 0.1 ) {
318 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport is not rolling or absolute-speed < 0.1\n");
319 /* keep repeating current frame
321 * an LTC generator must be able to continue generating LTC when Ardours transport is in stop
322 * some machines do odd things if LTC goes away:
323 * e.g. a tape based machine (video or audio), some think they have gone into park if LTC goes away,
324 * so unspool the tape from the playhead. That might be inconvenient.
325 * If LTC keeps arriving they remain in a stop position with the tape on the playhead.
328 if (!Config->get_ltc_send_continuously()) {
329 ltc_speed = new_ltc_speed;
332 if (start_frame != ltc_prev_cycle) {
333 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: no-roll seek from %1 to %2 (%3)\n", ltc_prev_cycle, start_frame, cycle_start_frame));
338 if (fabs(new_ltc_speed) > 10.0) {
339 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: speed is out of bounds.\n");
344 if (ltc_speed == 0 && new_ltc_speed != 0) {
345 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport started rolling - reset\n");
349 /* the timecode duration corresponding to the samples that are still
350 * in the buffer. Here, the speed of previous cycle is used to calculate
351 * the alignment at the beginning of this cycle later.
353 double poff = (ltc_buf_len - ltc_buf_off) * ltc_speed;
355 if (speed_changed && new_ltc_speed != 0) {
356 /* we need to re-sample the existing buffer.
357 * "make space for the en-coder to catch up to the new speed"
359 * since the LTC signal is a rectangular waveform we can simply squeeze it
360 * by removing samples or duplicating samples /here and there/.
362 * There may be a more elegant way to do this, in fact one could
363 * simply re-render the buffer using ltc_encoder_encode_byte()
364 * but that'd require some timecode offset buffer magic,
365 * which is left for later..
368 double oldbuflen = (double)(ltc_buf_len - ltc_buf_off);
369 double newbuflen = (double)(ltc_buf_len - ltc_buf_off) * fabs(ltc_speed / new_ltc_speed);
371 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: bufOld %1 bufNew %2 | diff %3\n",
372 (ltc_buf_len - ltc_buf_off), newbuflen, newbuflen - oldbuflen
375 double bufrspdiff = rint(newbuflen - oldbuflen);
377 if (abs(bufrspdiff) > newbuflen || abs(bufrspdiff) > oldbuflen) {
378 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: resampling buffer would destroy information.\n");
381 } else if (bufrspdiff != 0 && newbuflen > oldbuflen) {
383 double samples_to_insert = ceil(newbuflen - oldbuflen);
384 double avg_distance = newbuflen / samples_to_insert;
385 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: resample buffer insert: %1\n", samples_to_insert));
387 for (int rp = ltc_buf_off; rp < ltc_buf_len - 1; ++rp) {
388 const int ro = rp - ltc_buf_off;
389 if (ro < (incnt*avg_distance)) continue;
390 const ltcsnd_sample_t v1 = ltc_enc_buf[rp];
391 const ltcsnd_sample_t v2 = ltc_enc_buf[rp+1];
392 if (v1 != v2 && ro < ((incnt+1)*avg_distance)) continue;
393 memmove(<c_enc_buf[rp+1], <c_enc_buf[rp], ltc_buf_len-rp);
397 } else if (bufrspdiff != 0 && newbuflen < oldbuflen) {
398 double samples_to_remove = ceil(oldbuflen - newbuflen);
399 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: resample buffer - remove: %1\n", samples_to_remove));
400 if (oldbuflen <= samples_to_remove) {
401 ltc_buf_off = ltc_buf_len= 0;
403 double avg_distance = newbuflen / samples_to_remove;
405 for (int rp = ltc_buf_off; rp < ltc_buf_len - 1; ++rp) {
406 const int ro = rp - ltc_buf_off;
407 if (ro < (rmcnt*avg_distance)) continue;
408 const ltcsnd_sample_t v1 = ltc_enc_buf[rp];
409 const ltcsnd_sample_t v2 = ltc_enc_buf[rp+1];
410 if (v1 != v2 && ro < ((rmcnt+1)*avg_distance)) continue;
411 memmove(<c_enc_buf[rp], <c_enc_buf[rp+1], ltc_buf_len-rp-1);
419 ltc_prev_cycle = start_frame;
420 ltc_speed = new_ltc_speed;
421 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: transport speed %1.\n", ltc_speed));
423 // (3) bit/sample alignment
424 Timecode::Time tc_start;
425 framepos_t tc_sample_start;
427 /* calc timecode frame from current position - round down to nearest timecode */
428 Timecode::sample_to_timecode(cycle_start_frame, tc_start, true, false,
429 timecode_frames_per_second(),
430 timecode_drop_frames(),
431 (double)frame_rate(),
432 config.get_subframes_per_frame(),
433 ltc_timecode_negative_offset, ltc_timecode_offset
436 /* convert timecode back to sample-position */
437 Timecode::timecode_to_sample (tc_start, tc_sample_start, true, false,
438 (double)frame_rate(),
439 config.get_subframes_per_frame(),
440 ltc_timecode_negative_offset, ltc_timecode_offset
443 /* difference between current frame and TC frame in samples */
444 frameoffset_t soff = cycle_start_frame - tc_sample_start;
445 if (current_speed == 0) {
448 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX3: A3cycle: %1 = A3tc: %2 +off: %3\n",
449 cycle_start_frame, tc_sample_start, soff));
452 // (4) check if alignment matches
453 const double fptcf = samples_per_timecode_frame();
455 /* maximum difference of bit alignment in audio-samples.
457 * if transport and LTC generator differs more than this, the LTC
458 * generator will be re-initialized
460 * due to rounding error and variations in LTC-bit duration depending
461 * on the speed, it can be off by +- ltc_speed audio-samples.
462 * When the playback speed changes, it can actually reach +- 2 * ltc_speed
463 * in the cycle _after_ the speed changed. The average delta however is 0.
467 if (config.get_external_sync() && slave()) {
468 maxdiff = slave()->resolution();
470 maxdiff = ceil(fabs(ltc_speed))*2.0;
471 if (nominal_frame_rate() != frame_rate()) {
474 if (ltc_enc_tcformat == Timecode::timecode_23976 || ltc_enc_tcformat == Timecode::timecode_24976) {
479 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: enc: %1 + %2 - %3 || buf-bytes: %4 enc-byte: %5\n",
480 ltc_enc_pos, ltc_enc_cnt, poff, (ltc_buf_len - ltc_buf_off), poff, ltc_enc_byte));
482 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: enc-pos: %1 | d: %2\n",
483 ltc_enc_pos + ltc_enc_cnt - poff,
484 rint(ltc_enc_pos + ltc_enc_cnt - poff) - cycle_start_frame
487 const framecnt_t wrap24h = 86400. * frame_rate();
489 || (ltc_speed != 0 && fabs(fmod(ceil(ltc_enc_pos + ltc_enc_cnt - poff), wrap24h) - (cycle_start_frame % wrap24h)) > maxdiff)
495 /* set frame to encode */
497 tc.hours = tc_start.hours % 24;
498 tc.mins = tc_start.minutes;
499 tc.secs = tc_start.seconds;
500 tc.frame = tc_start.frames;
501 ltc_encoder_set_timecode(ltc_encoder, &tc);
503 /* workaround for libltc recognizing 29.97 and 30000/1001 as drop-frame TC.
504 * In A3 30000/1001 or 30 fps can be drop-frame.
507 ltc_encoder_get_frame(ltc_encoder, <cframe);
508 ltcframe.dfbit = timecode_has_drop_frames(cur_timecode)?1:0;
509 ltc_encoder_set_frame(ltc_encoder, <cframe);
512 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: now: %1 trs: %2 toff %3\n", cycle_start_frame, tc_sample_start, soff));
515 if (soff < 0 || soff >= fptcf) {
516 /* session framerate change between (2) and now */
521 if (ltc_speed < 0 ) {
522 /* calculate the byte that starts at or after the current position */
523 ltc_enc_byte = floor((10.0 * soff) / (fptcf));
524 ltc_enc_cnt = ltc_enc_byte * fptcf / 10.0;
526 /* calculate difference between the current position and the byte to send */
527 cyc_off = soff- ceil(ltc_enc_cnt);
530 /* calculate the byte that starts at or after the current position */
531 ltc_enc_byte = ceil((10.0 * soff) / fptcf);
532 ltc_enc_cnt = ltc_enc_byte * fptcf / 10.0;
534 /* calculate difference between the current position and the byte to send */
535 cyc_off = ceil(ltc_enc_cnt) - soff;
537 if (ltc_enc_byte == 10) {
539 ltc_encoder_inc_timecode(ltc_encoder);
543 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX5 restart encoder: soff %1 byte %2 cycoff %3\n",
544 soff, ltc_enc_byte, cyc_off));
546 if ( (ltc_speed < 0 && ltc_enc_byte !=9 ) || (ltc_speed >= 0 && ltc_enc_byte !=0 ) ) {
550 if (cyc_off >= 0 && cyc_off <= (int32_t) nframes) {
551 /* offset in this cycle */
552 txf= rint(cyc_off / fabs(ltc_speed));
553 memset(out, 0, cyc_off * sizeof(Sample));
555 /* resync next cycle */
556 memset(out, 0, nframes * sizeof(Sample));
560 ltc_enc_pos = tc_sample_start % wrap24h;
562 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX5 restart @ %1 + %2 - %3 | byte %4\n",
563 ltc_enc_pos, ltc_enc_cnt, cyc_off, ltc_enc_byte));
565 else if (ltc_speed != 0 && (fptcf / ltc_speed / 80) > 3 ) {
566 /* reduce (low freq) jitter.
567 * The granularity of the LTC encoder speed is 1 byte =
568 * (frames-per-timecode-frame / 10) audio-samples.
569 * Thus, tiny speed changes [as produced by some slaves]
570 * may not have any effect in the cycle when they occur,
571 * but they will add up over time.
573 * This is a linear approx to compensate for this jitter
574 * and prempt re-sync when the drift builds up.
576 * However, for very fast speeds - when 1 LTC bit is
577 * <= 3 audio-sample - adjusting speed may lead to
580 * To do better than this, resampling (or a rewrite of the
581 * encoder) is required.
583 ltc_speed -= fmod(((ltc_enc_pos + ltc_enc_cnt - poff) - cycle_start_frame), wrap24h) / engine().sample_rate();
587 // (6) encode and output
589 #ifdef LTC_GEN_TXDBUG
590 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.1 @%1 [ %2 / %3 ]\n", txf, ltc_buf_off, ltc_buf_len));
592 // (6a) send remaining buffer
593 while ((ltc_buf_off < ltc_buf_len) && (txf < nframes)) {
594 const float v1 = ltc_enc_buf[ltc_buf_off++] - 128.0;
595 const Sample val = (Sample) (v1*ltcvol);
598 #ifdef LTC_GEN_TXDBUG
599 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.2 @%1 [ %2 / %3 ]\n", txf, ltc_buf_off, ltc_buf_len));
602 if (txf >= nframes) {
603 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX7 enc: %1 [ %2 / %3 ] byte: %4 spd %5 fpp %6 || nf: %7\n",
604 ltc_enc_pos, ltc_buf_off, ltc_buf_len, ltc_enc_byte, ltc_speed, nframes, txf));
611 // (6b) encode LTC, bump timecode
614 ltc_enc_byte = (ltc_enc_byte + 9)%10;
615 if (ltc_enc_byte == 9) {
616 ltc_encoder_dec_timecode(ltc_encoder);
617 ltc_tx_recalculate_position();
625 /* write zero bytes -- don't touch encoder until we're at a frame-boundary
626 * otherwise the biphase polarity may be inverted.
628 enc_frames = fptcf / 10.0;
629 memset(<c_enc_buf[ltc_buf_len], 127, enc_frames * sizeof(ltcsnd_sample_t));
631 if (ltc_encoder_encode_byte(ltc_encoder, ltc_enc_byte, (ltc_speed==0)?1.0:(1.0/ltc_speed))) {
632 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.3 encoder error byte %1\n", ltc_enc_byte));
633 ltc_encoder_buffer_flush(ltc_encoder);
637 enc_frames = ltc_encoder_get_buffer(ltc_encoder, &(ltc_enc_buf[ltc_buf_len]));
640 #ifdef LTC_GEN_FRAMEDBUG
641 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));
643 if (enc_frames <=0) {
644 DEBUG_TRACE (DEBUG::LTC, "LTC TX6.3 encoder empty buffer.\n");
645 ltc_encoder_buffer_flush(ltc_encoder);
650 ltc_buf_len += enc_frames;
652 ltc_enc_cnt -= fptcf/10.0;
654 ltc_enc_cnt += fptcf/10.0;
656 if (ltc_speed >= 0) {
657 ltc_enc_byte = (ltc_enc_byte + 1)%10;
658 if (ltc_enc_byte == 0 && ltc_speed != 0) {
659 ltc_encoder_inc_timecode(ltc_encoder);
660 #if 0 /* force fixed parity -- scope debug */
662 ltc_encoder_get_frame(ltc_encoder, &f);
663 f.biphase_mark_phase_correction=0;
664 ltc_encoder_set_frame(ltc_encoder, &f);
666 ltc_tx_recalculate_position();
668 } else if (ltc_enc_byte == 0) {
673 #ifdef LTC_GEN_FRAMEDBUG
674 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));
678 dynamic_cast<AudioBuffer*>(&buf)->set_written (true);