ctimecodes

Correct sleep function of libltc


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?


Solution

  • 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.