File: example_encode.c

package info (click to toggle)
libltc 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 492 kB
  • sloc: ansic: 1,509; makefile: 66
file content (176 lines) | stat: -rw-r--r-- 4,403 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
   @brief example code for libltc LTCEncoder
   @file example_encode.c
   @author Robin Gareus <robin@gareus.org>

   Copyright (C) 2006-2016 Robin Gareus <robin@gareus.org>
   Copyright (C) 2008-2009 Jan <jan@geheimwerk.de>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

/*
 * compile with
 *  gcc -o ltc-encoder example_encode.c -lltc -lm
 *
 * from the source-folder this can be compiled after building libltc:
 *  gcc -o ltc-encoder example_encode.c ../src/.libs/libltc.a -lm -I../src
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ltc.h>

#ifdef _WIN32
#include <fcntl.h> // for _fmode
#endif

/* define "USE_LOCAL_BUFFER" to
 * use a local buffer instead of a pointer to
 * libltc's internal buffer
 */
//#define USE_LOCAL_BUFFER

int main(int argc, char **argv) {
	FILE* file;
	double length = 2; // in seconds
	double fps = 25;
	double sample_rate = 48000;
	char *filename;

	int vframe_cnt;
	int vframe_last;

	int total = 0;
	ltcsnd_sample_t *buf;

	LTCEncoder *encoder;
	SMPTETimecode st;

	/* start encoding at this timecode */
	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;

	/* parse commandline args */
	if (argc > 1) {
		filename = argv[1];
		if (argc > 2) {
			sample_rate = atof(argv[2]);
		}
		if (argc > 3) {
			fps = atof(argv[3]);
		}
		if (argc > 4) {
			length = atof(argv[4]);
		}
	} else {
		printf("ltc-encoder - test/example application to encode LTC to a file\n\n");
		printf("Usage: ltc-encoder <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

	/* open output file */
	file = fopen(filename, "wb");
	if (!file) {
		fprintf(stderr, "Error: can not open file '%s' for writing.\n", filename);
		return 1;
	}

	/* prepare encoder */
	encoder = ltc_encoder_create(sample_rate, fps,
			fps==25?LTC_TV_625_50:LTC_TV_525_60, LTC_USE_DATE);
	ltc_encoder_set_timecode(encoder, &st);

#ifdef USE_LOCAL_BUFFER
	buf = calloc(ltc_encoder_get_buffersize(encoder), sizeof(ltcsnd_sample_t));
	if (!buf) {
		return -1;
	}
#endif

	/* ready to go, print some info first */

	printf("sample rate: %.2f\n", sample_rate);
	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) {
#if 1 /* encode and write each of the 80 LTC frame bits (10 bytes) */
		int byte_cnt;
		for (byte_cnt = 0 ; byte_cnt < 10 ; byte_cnt++) {
			ltc_encoder_encode_byte(encoder, byte_cnt, 1.0);

#ifdef USE_LOCAL_BUFFER
			int len = ltc_encoder_copy_buffer(encoder, buf);
#else
			int len = ltc_encoder_get_bufferptr (encoder, &buf, 1);
#endif
			if (len > 0) {
				fwrite(buf, sizeof(ltcsnd_sample_t), len, file);
				total+=len;
			}
		}
#else /* encode a complete LTC frame in one step */
		ltc_encoder_encode_frame(encoder);

#ifdef USE_LOCAL_BUFFER
		int len = ltc_encoder_copy_buffer(encoder, buf);
#else
		int len = ltc_encoder_get_bufferptr(encoder, &buf, 1);
#endif

		if (len > 0) {
			fwrite(buf, sizeof(ltcsnd_sample_t), len, file);
			total+=len;
		}
#endif

		ltc_encoder_inc_timecode(encoder);
	}
	fclose(file);
	ltc_encoder_free(encoder);

	printf("Done: wrote %d samples to '%s'\n", total, filename);

#ifdef USE_LOCAL_BUFFER
	free(buf);
#endif

	return 0;
}