0

I am playing around with libLTC to generate timecode. I have a rough working example below:

#include <curses.h>
#include <time.h>
#include <ltc.h>

int main() {
    initscr();
    nodelay(stdscr, TRUE);
    LTCFrame frame;
    LTCFrameExt Frame;
    SMPTETimecode stime;

    do {
        clear();
        ltc_frame_increment(&frame, 25, LTC_TV_625_50, LTC_USE_DATE);
        ltc_frame_to_time(&stime, &frame, LTC_USE_DATE);
        printw("%02d:%02d:%02d%c%02d | %8lld %8lld%s\n",
               stime.hours,
               stime.mins,
               stime.secs,
               (Frame.ltc.dfbit) ? '.' : ':',
               stime.frame,
               Frame.off_start,
               Frame.off_end,
               Frame.reverse ? "  R" : ""
        );

        refresh();
    } while (getch() != 'q');

    endwin();
    return 0;
}

The issue I have currently is that the loop runs too fast and as a result so does the TC, I wondered what the correct way to slow this down so that it runs at the correct rate? There is the sleep() function but would need to change for each frame rate?

hdcdigi
  • 75
  • 7

1 Answers1

1

There are many ways to approach this problem. The easiest approach would be the nanosleep() function, so you could calculate how many nanoseconds you want to wait to execute the next iteration at the bottom of your do loop.

A more sophisticated approach would use the settimer() function to use the RTC to raise SIGALRM at the appropriate time. Because this would be done with signal handling, there is no need at all for a do/while loop.

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56
  • for 25fps I did 1 / 25 which is 0.04 of a second or 40000000 nanoseconds, this seems to work. Thanks – hdcdigi Apr 19 '21 at 19:47
  • hmmm its not the most accurate, starts slipping quickly when setting TC from system clock. – hdcdigi Apr 19 '21 at 21:13
  • It's not sufficient to just sleep for a predetermined amount of time - you need to inspect the current time, and calculate how much time to the next event. Real time timing is hard. – PaulProgrammer Apr 19 '21 at 22:54