forked from x42/ltc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjltcdump-simple.c
304 lines (249 loc) · 7.09 KB
/
jltcdump-simple.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// gcc -o jltcdump-simple jltcdump-simple.c `pkg-config --cflags --libs jack ltc`/
/* jack linear time code decoder
* Copyright (C) 2006, 2012, 2013 Robin Gareus
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define LTC_QUEUE_LEN (42) // should be >> ( max(jack period size) * max-speedup / (duration of LTC-frame) )
#define _GNU_SOURCE
#ifndef VERSION
#define VERSION "1"
#endif
#ifdef WIN32
#include <windows.h>
#include <pthread.h>
#define pthread_t //< override jack.h def
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <getopt.h>
#include <sys/mman.h>
#include <jack/jack.h>
#include <ltc.h>
#ifndef WIN32
#include <signal.h>
#include <pthread.h>
#endif
static int keep_running = 1;
static jack_port_t *input_port = NULL;
static jack_client_t *j_client = NULL;
static uint32_t j_samplerate = 48000;
static LTCDecoder *decoder = NULL;
static pthread_mutex_t ltc_thread_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
static int fps_num = 25;
static int fps_den = 1;
/**
* jack audio process callback
*/
int process (jack_nframes_t nframes, void *arg) {
jack_default_audio_sample_t *in;
in = jack_port_get_buffer (input_port, nframes);
ltc_decoder_write_float(decoder, in, nframes, 0);
/* notify reader thread */
if (pthread_mutex_trylock (<c_thread_lock) == 0) {
pthread_cond_signal (&data_ready);
pthread_mutex_unlock (<c_thread_lock);
}
return 0;
}
void jack_shutdown (void *arg) {
fprintf(stderr,"recv. shutdown request from jackd.\n");
keep_running=0;
pthread_cond_signal (&data_ready);
}
/**
* open a client connection to the JACK server
*/
static int init_jack(const char *client_name) {
jack_status_t status;
j_client = jack_client_open (client_name, JackNullOption, &status);
if (j_client == NULL) {
fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n", status);
if (status & JackServerFailed) {
fprintf (stderr, "Unable to connect to JACK server\n");
}
return (-1);
}
if (status & JackServerStarted) {
fprintf (stderr, "JACK server started\n");
}
if (status & JackNameNotUnique) {
client_name = jack_get_client_name(j_client);
fprintf (stderr, "jack-client name: `%s'\n", client_name);
}
jack_set_process_callback (j_client, process, 0);
#ifndef WIN32
jack_on_shutdown (j_client, jack_shutdown, NULL);
#endif
j_samplerate=jack_get_sample_rate (j_client);
return (0);
}
static int jack_portsetup(void) {
/* Allocate data structures that depend on the number of ports. */
decoder = ltc_decoder_create(j_samplerate * fps_den / fps_num, LTC_QUEUE_LEN);
if (!decoder) {
fprintf (stderr, "cannot create LTC decoder (out of memory).\n");
return (-1);
}
/* create jack port */
if ((input_port = jack_port_register (j_client, "input_1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
fprintf (stderr, "cannot register input port \"input_1\"!\n");
return (-1);
}
return (0);
}
static void jack_port_connect(char **jack_port, int argc) {
int i;
for (i=0; i < argc; i++) {
if (!jack_port[i]) continue;
if (jack_connect(j_client, jack_port[i], jack_port_name(input_port))) {
fprintf(stderr, "cannot connect port %s to %s\n", jack_port[i], jack_port_name(input_port));
}
}
}
/**
*
*/
static void my_decoder_read(LTCDecoder *d) {
LTCFrameExt frame;
while (ltc_decoder_read(d, &frame)) {
SMPTETimecode stime;
ltc_frame_to_time(&stime, &frame.ltc, /* use_date? LTC_USE_DATE : */ 0);
if (/* use_date */ 0)
printf("%02d-%02d-%02d ",
stime.years,
stime.months,
stime.days);
printf("%02d:%02d:%02d%c%02d | %8lld %8lld%s | %.1fdB\n",
stime.hours,
stime.mins,
stime.secs,
(frame.ltc.dfbit) ? '.' : ':',
stime.frame,
frame.off_start,
frame.off_end,
frame.reverse ? " R" : " ",
frame.volume
);
}
fflush(stdout);
}
/**
*
*/
static void main_loop(void) {
pthread_mutex_lock (<c_thread_lock);
while (keep_running) {
my_decoder_read(decoder);
if (!keep_running) break;
pthread_cond_wait (&data_ready, <c_thread_lock);
}
pthread_mutex_unlock (<c_thread_lock);
}
void catchsig (int sig) {
#ifndef _WIN32
signal(SIGHUP, catchsig);
#endif
fprintf(stderr,"caught signal - shutting down.\n");
keep_running=0;
pthread_cond_signal (&data_ready);
}
/**************************
* main application code
*/
static struct option const long_options[] =
{
{"help", no_argument, 0, 'h'},
{"fps", required_argument, 0, 'f'},
{"version", no_argument, 0, 'V'},
{NULL, 0, NULL, 0}
};
static void usage (int status) {
printf ("jltcdump - very simple JACK client to parse linear time code.\n\n");
printf ("Usage: jltcdump [ OPTIONS ] [ JACK-PORTS ]\n\n");
printf ("Options:\n\
-f, --fps <num>[/den] set expected framerate (default 25/1)\n\
-h, --help display this help and exit\n\
-V, --version print version information and exit\n\
\n\n");
printf ("Report bugs to Robin Gareus <[email protected]>\n"
"Website and manual: <https://github.com/x42/ltc-tools>\n"
);
exit (status);
}
static int decode_switches (int argc, char **argv) {
int c;
while ((c = getopt_long (argc, argv,
"h" /* help */
"f:" /* fps */
"V", /* version */
long_options, (int *) 0)) != EOF)
{
switch (c)
{
case 'f':
{
fps_num = atoi(optarg);
char *tmp = strchr(optarg, '/');
if (tmp) fps_den=atoi(++tmp);
}
break;
case 'V':
printf ("jltcdump-simple version %s\n\n", VERSION);
printf ("Copyright (C) GPL 2006,2012,2013 Robin Gareus <[email protected]>\n");
exit (0);
case 'h':
usage (0);
default:
usage (EXIT_FAILURE);
}
}
return optind;
}
int main (int argc, char **argv) {
int i;
i = decode_switches (argc, argv);
// -=-=-= INITIALIZE =-=-=-
if (init_jack("jack-ltc-dump"))
goto out;
if (jack_portsetup())
goto out;
if (mlockall (MCL_CURRENT | MCL_FUTURE)) {
fprintf(stderr, "Warning: Can not lock memory.\n");
}
if (jack_activate (j_client)) {
fprintf (stderr, "cannot activate client.\n");
goto out;
}
jack_port_connect(&(argv[i]), argc-i);
#ifndef _WIN32
signal (SIGHUP, catchsig);
signal (SIGINT, catchsig);
#endif
printf("## SMPTE | audio-sample-num REV| \n");
printf("##time-code | start end ERS| \n");
main_loop();
out:
if (j_client) {
jack_client_close (j_client);
j_client=NULL;
}
ltc_decoder_free(decoder);
fprintf(stderr, "bye.\n");
return(0);
}
/* vi:set ts=8 sts=2 sw=2: */