libltc: ltcencode.c

| libltc 1.3.2 Linear/Logitudinal Time Code (LTC) Library

ltcencode.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ltc.h>
#ifdef _WIN32
#include <fcntl.h> // for _fmode
#endif
int main(int argc, char **argv) {
FILE* file;
double length = 2; // in seconds
double fps = 25;
double sampleRate = 48000;
char *filename;
int total = 0;
int vframe_cnt;
int vframe_last;
LTCEncoder *encoder;
SMPTETimecode st;
const char timezone[6] = "+0100";
strcpy(st. timezone, timezone);
st. years = 8;
st. months = 12;
st. days = 31;
st. hours = 23;
st. mins = 59;
st. secs = 59;
st. frame = 0;
if (argc > 1) {
filename = argv[1];
if (argc > 2) {
sampleRate = atof(argv[2]);
}
if (argc > 3) {
fps = atof(argv[3]);
}
if (argc > 4) {
length = atof(argv[4]);
}
} else {
printf("ltcencode - test/example application to encode LTC to a file\n\n");
printf("Usage: ltcencode <filename> [sample rate [frame rate [duration in s]]]\n\n");
printf("default-values:\n");
printf(" sample rate: 48000.0 [SPS], frame rate: 25.0 [fps], duration: 2.0 [sec]\n");
printf("Report bugs to Robin Gareus <robin@gareus.org>\n");
return 1;
}
#ifdef _WIN32
// see https://msdn.microsoft.com/en-us/library/ktss1a9b.aspx and
// https://github.com/x42/libltc/issues/18
_set_fmode(_O_BINARY);
#endif
file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "Error: can not open file '%s' for writing.\n", filename);
return 1;
}
encoder = ltc_encoder_create(1, 1, 0, LTC_USE_DATE);
ltc_encoder_set_buffersize(encoder, sampleRate, fps);
ltc_encoder_reinit(encoder, sampleRate, fps,
fps==25? LTC_TV_625_50: LTC_TV_525_60, LTC_USE_DATE);
ltc_encoder_set_filter(encoder, 0);
ltc_encoder_set_filter(encoder, 25.0);
ltc_encoder_set_volume(encoder, -18.0);
ltc_encoder_set_timecode(encoder, &st);
printf("sample rate: %.2f\n", sampleRate);
printf("frames/sec: %.2f\n", fps);
printf("secs to write: %.2f\n", length);
printf("sample format: 8bit unsigned mono\n");
vframe_cnt = 0;
vframe_last = length * fps;
while (vframe_cnt++ < vframe_last) {
int len;
ltcsnd_sample_t *buf;
ltc_encoder_encode_frame(encoder);
len = ltc_encoder_get_bufferptr(encoder, &buf, 1);
if (len > 0) {
fwrite(buf, sizeof(ltcsnd_sample_t), len, file);
total+=len;
}
ltc_encoder_inc_timecode(encoder);
}
fclose(file);
ltc_encoder_free(encoder);
printf("Done: wrote %d samples to '%s'\n", total, filename);
return 0;
}
ltc.h
libltc - en+decode linear timecode
ltc_encoder_free
void ltc_encoder_free(LTCEncoder *e)
SMPTETimecode::frame
unsigned char frame
sub-second frame 0..(FPS - 1)
Definition ltc.h:327
ltcsnd_sample_t
unsigned char ltcsnd_sample_t
Definition ltc.h:88
LTC_USE_DATE
@ LTC_USE_DATE
LTCFrame <> SMPTETimecode converter and LTCFrame increment/decrement use date, also set BGF2 to '1' w...
Definition ltc.h:272
ltc_encoder_inc_timecode
int ltc_encoder_inc_timecode(LTCEncoder *e)
ltc_encoder_set_timecode
void ltc_encoder_set_timecode(LTCEncoder *e, SMPTETimecode *t)
SMPTETimecode::months
unsigned char months
valid months are 1..12
Definition ltc.h:321
SMPTETimecode::mins
unsigned char mins
minute 0..60
Definition ltc.h:325
LTCEncoder
struct LTCEncoder LTCEncoder
Definition ltc.h:346
SMPTETimecode::hours
unsigned char hours
hour 0..23
Definition ltc.h:324
ltc_encoder_set_filter
void ltc_encoder_set_filter(LTCEncoder *e, double rise_time)
LTC_TV_625_50
@ LTC_TV_625_50
25fps
Definition ltc.h:265
LTC_TV_525_60
@ LTC_TV_525_60
30fps
Definition ltc.h:264
ltc_encoder_create
LTCEncoder * ltc_encoder_create(double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags)
ltc_encoder_reinit
int ltc_encoder_reinit(LTCEncoder *e, double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags)
SMPTETimecode::days
unsigned char days
day of month 1..31
Definition ltc.h:322
SMPTETimecode::timezone
char timezone[6]
the timezone 6bytes: "+HHMM" textual representation
Definition ltc.h:319
SMPTETimecode::secs
unsigned char secs
second 0..60
Definition ltc.h:326
ltc_encoder_get_bufferptr
int ltc_encoder_get_bufferptr(LTCEncoder *e, ltcsnd_sample_t **buf, int flush)
ltc_encoder_encode_frame
void ltc_encoder_encode_frame(LTCEncoder *e)
SMPTETimecode::years
unsigned char years
LTC-date uses 2-digit year 00.99.
Definition ltc.h:320
ltc_encoder_set_volume
int ltc_encoder_set_volume(LTCEncoder *e, double dBFS)
ltc_encoder_set_buffersize
int ltc_encoder_set_buffersize(LTCEncoder *e, double sample_rate, double fps)
SMPTETimecode
Definition ltc.h:318