forked from lodsb/vinylcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecoder.c
More file actions
599 lines (440 loc) · 15.7 KB
/
timecoder.c
File metadata and controls
599 lines (440 loc) · 15.7 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* Copyright (C) 2007 Mark Hills <mark@pogo.org.uk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "timecoder.h"
#define ZERO_THRESHOLD 128
#define SIGNAL_THRESHOLD 256
#define ZERO_AVG (TIMECODER_RATE / 1024)
#define SIGNAL_AVG (TIMECODER_RATE / 256)
#define MAX_BITS 32 /* bits in an int */
#define REF_PEAKS_AVG 48 /* in wave cycles */
/* The number of correct bits which come in before the timecode
* is declared valid. Set this too low, and risk the record skipping around
* (often to blank areas of track) during scratching */
#define VALID_BITS 24
#define MONITOR_DECAY_EVERY 512 /* in samples */
#define SQ(x) ((x)*(x))
/* Timecode definitions */
#define POLARITY_NEGATIVE 0
#define POLARITY_POSITIVE 1
struct timecode_def_t {
char *name, *desc;
int bits, /* number of bits in string */
resolution, /* wave cycles per second */
tap[MAX_BITS], ntaps, /* LFSR taps */
polarity; /* cycle begins POLARITY_POSITIVE or POLARITY_NEGATIVE */
unsigned int seed, /* LFSR value at timecode zero */
length, /* in cycles */
safe; /* last 'safe' timecode number (for auto disconnect) */
signed int *lookup; /* pointer to built lookup table */
};
struct timecode_def_t timecode_def[] = {
{
name: "serato_2a",
desc: "Serato 2nd Ed., side A",
resolution: 1000,
polarity: POLARITY_POSITIVE,
bits: 20,
seed: 0x59017,
tap: {2, 5, 6, 7, 8, 13, 14, 16, 17},
ntaps: 9,
length: 712000,
safe: 707000,
lookup: NULL
},
{
name: "serato_2b",
desc: "Serato 2nd Ed., side B",
seed: 0x8f3c6,
resolution: 1000,
polarity: POLARITY_POSITIVE,
bits: 20,
tap: {3, 4, 6, 7, 12, 13, 14, 15, 18}, /* reverse of side A */
ntaps: 9,
length: 922000,
safe: 917000,
lookup: NULL
},
{
name: "serato_cd",
desc: "Serato CD",
resolution: 1000,
polarity: POLARITY_POSITIVE,
bits: 20,
seed: 0x84c0c,
tap: {2, 4, 6, 8, 10, 11, 14, 16, 17},
ntaps: 9,
length: 940000,
safe: 930000,
lookup: NULL
},
{
name: "traktor_a",
desc: "Traktor Scratch, side A",
resolution: 2000,
polarity: POLARITY_NEGATIVE,
bits: 23,
seed: 0x134503,
tap: {6, 12, 18},
ntaps: 3,
length: 1500000,
safe: 1480000,
lookup: NULL
},
{
name: "traktor_b",
desc: "Traktor Scratch, side B",
resolution: 2000,
polarity: POLARITY_NEGATIVE,
bits: 23,
seed: 0x32066c,
tap: {6, 12, 18},
ntaps: 3,
length: 2110000,
safe: 2090000,
lookup: NULL
},
{
name: NULL
}
};
struct timecode_def_t *def;
/* Linear Feeback Shift Register in the forward direction. New values
* are generated at the least-significant bit. */
static inline int lfsr(unsigned int code)
{
unsigned int r;
char s, n;
r = code & 1;
for(n = 0; n < def->ntaps; n++) {
s = *(def->tap + n);
r += (code & (1 << s)) >> s;
}
return r & 0x1;
}
/* Linear Feeback Shift Register in the reverse direction. New values
* are generated at the most-significant bit. */
static inline int lfsr_rev(unsigned int code)
{
unsigned int r;
char s, n;
r = (code & (1 << (def->bits - 1))) >> (def->bits - 1);
for(n = 0; n < def->ntaps; n++) {
s = *(def->tap + n) - 1;
r += (code & (1 << s)) >> s;
}
return r & 0x1;
}
/* Setup globally, for a chosen timecode definition */
int timecoder_build_lookup(char *timecode_name) {
unsigned int n, current;
def = &timecode_def[0];
while(def->name) {
if(!strcmp(def->name, timecode_name))
break;
def++;
}
if(!def->name) {
fprintf(stderr, "Timecode definition '%s' is not known.\n",
timecode_name);
return -1;
}
fprintf(stderr, "Allocating %d slots (%zuKb) for %d bit timecode (%s)\n",
2 << def->bits, (2 << def->bits) * sizeof(unsigned int) / 1024,
def->bits, def->desc);
def->lookup = malloc((2 << def->bits) * sizeof(unsigned int));
if(!def->lookup) {
perror("malloc");
return 0;
}
for(n = 0; n < ((unsigned int)2 << def->bits); n++)
def->lookup[n] = -1;
current = def->seed;
for(n = 0; n < def->length; n++) {
if(def->lookup[current] != -1) {
fprintf(stderr, "Timecode has wrapped; finishing here.\n");
return -1;
}
def->lookup[current] = n;
current = (current >> 1) + (lfsr(current) << (def->bits - 1));
}
return 0;
}
/* Free the timecoder lookup table when it is no longer needed */
void timecoder_free_lookup(void) {
free(def->lookup);
}
/* Initialise a timecode decoder */
void timecoder_init(struct timecoder_t *tc)
{
int c;
struct timecoder_channel_t *st;
for(c = 0; c < TIMECODER_CHANNELS; c++) {
st = &tc->state[c];
st->zero = 0;
st->half_peak = 0;
st->wave_peak = 0;
st->ref_level = -1;
st->signal_level = 0;
st->positive = 0;
st->crossings = 0;
st->cycle_ticker = 0;
st->crossings_ticker = 0;
st->timecode_ticker = 0;
st->bitstream = 0;
st->timecode = 0;
st->valid_counter = 0;
}
tc->forwards = 1;
tc->mon = NULL;
tc->log_fd = -1;
}
/* Clear a timecode decoder */
void timecoder_clear(struct timecoder_t *tc)
{
timecoder_monitor_clear(tc);
}
/* The monitor (otherwise known as 'scope' in the interface) is the
* display of the incoming audio. Initialise one for the given
* timecoder */
void timecoder_monitor_init(struct timecoder_t *tc, int size, int scale)
{
tc->mon_size = size;
tc->mon_scale = scale;
tc->mon = malloc(SQ(tc->mon_size));
memset(tc->mon, 0, SQ(tc->mon_size));
tc->mon_counter = 0;
}
/* Clear the monitor on the given timecoder */
void timecoder_monitor_clear(struct timecoder_t *tc)
{
if(tc->mon) {
free(tc->mon);
tc->mon = NULL;
}
}
/* Submit and decode a block of PCM audio data to the timecoder */
int timecoder_submit(struct timecoder_t *tc, signed short *pcm, int samples)
{
int b, l, /* bitstream and timecode bits */
s, c, /* samples, channels */
x, y, p, /* monitor coordinates */
t, u, /* phase difference */
swapped,
monitor_centre;
signed short v, w; /* pcm sample values */
unsigned int mask;
struct timecoder_channel_t *st, *sto;
b = 0;
l = 0;
mask = ((1 << def->bits) - 1);
monitor_centre = tc->mon_size / 2;
for(s = 0; s < samples; s++) {
for(c = 0; c < TIMECODER_CHANNELS; c++) {
st = &tc->state[c];
v = pcm[s * TIMECODER_CHANNELS + c];
/* Work out if we've crossed through zero, based on a zero
* being a range rather than simply a value */
swapped = 0;
if(v >= st->zero + ZERO_THRESHOLD && !st->positive) {
swapped = 1;
st->positive = 1;
} else if(v < st->zero - ZERO_THRESHOLD && st->positive) {
swapped = 1;
st->positive = 0;
}
/* If a sign change in the (zero corrected) audio has
* happened, log the peak information */
if(swapped) {
/* Work out whether half way through a cycle we are
* looking for the wave to be positive or negative */
if(st->positive == (def->polarity ^ tc->forwards)) {
/* Entering the second half of a wave cycle */
st->half_peak = st->wave_peak;
} else {
/* Completed a full wave cycle, so time to analyse
* the level and work out whether it's a 1 or 0 */
b = st->wave_peak + st->half_peak > st->ref_level;
/* Log _all_ channels, interleaved */
if(tc->log_fd != -1 && c == 0)
write(tc->log_fd, b ? "1" : "0", 1);
/* Add it to the bitstream, and work out what we
* were expecting (timecode). */
/* st->bitstream is always in the order it is
* physically placed on the vinyl, regardless of
* the direction. */
if(tc->forwards) {
l = lfsr(st->timecode);
st->bitstream = (st->bitstream >> 1)
+ (b << (def->bits - 1));
st->timecode = (st->timecode >> 1)
+ (l << (def->bits - 1));
} else {
l = lfsr_rev(st->timecode);
st->bitstream = ((st->bitstream << 1) & mask) + b;
st->timecode = ((st->timecode << 1) & mask) + l;
}
if(b == l) {
st->valid_counter++;
} else {
st->timecode = st->bitstream;
st->valid_counter = 0;
}
/* Take note of the last time we read a valid
* timecode */
st->timecode_ticker = 0;
/* Adjust the reference level based on the peaks
* seen in this cycle */
if(st->ref_level == -1)
st->ref_level = st->half_peak + st->wave_peak;
else {
st->ref_level = (st->ref_level * (REF_PEAKS_AVG - 1)
+ st->half_peak + st->wave_peak)
/ REF_PEAKS_AVG;
}
}
/* Calculate the immediate direction based on phase
* difference of the two channels */
if(c == 0) {
sto = &tc->state[!c];
t = st->cycle_ticker;
u = sto->cycle_ticker;
if(t > u) {
if(st->positive == sto->positive) {
st->crossings++;
tc->forwards = 1;
}
if(st->positive != sto->positive) {
st->crossings--;
tc->forwards = 0;
}
}
}
/* Reset crossing couner */
st->crossings_ticker += st->cycle_ticker;
st->cycle_ticker = 0;
st->wave_peak = 0;
} /* swapped */
st->cycle_ticker++;
st->timecode_ticker++;
/* Find the zero-normalised sample of the peak value from
* the input */
w = abs(v - st->zero);
if(w > st->wave_peak)
st->wave_peak = w;
/* Take a rolling average of zero and signal level */
st->zero = (st->zero * (ZERO_AVG - 1) + v) / ZERO_AVG;
st->signal_level = (st->signal_level * (SIGNAL_AVG - 1) + w)
/ SIGNAL_AVG;
} /* for each channel */
/* Update the monitor to add the incoming sample */
if(tc->mon) {
// Decay the pixels already in the montior
if(++tc->mon_counter % MONITOR_DECAY_EVERY == 0) {
for(p = 0; p < SQ(tc->mon_size); p++) {
if(tc->mon[p])
tc->mon[p] = tc->mon[p] * 7 / 8;
}
}
v = pcm[s * TIMECODER_CHANNELS]; // first channel
w = pcm[s * TIMECODER_CHANNELS + 1]; // second channel
x = monitor_centre + (v * tc->mon_size * tc->mon_scale / 32768);
y = monitor_centre + (w * tc->mon_size * tc->mon_scale / 32768);
// Set the pixel value to white
if(x > 0 && x < tc->mon_size && y > 0 && y < tc->mon_size)
tc->mon[y * tc->mon_size + x] = 0xff;
}
} /* for each sample */
/* Print debugging information */
#if 0
st = &tc->state[0];
fprintf(stderr, "%+6d +/%4d -/%4d (%d)\t= %d (%d) %c %d"
"\t[crossings: %d %d]\n",
st->zero,
st->half_peak,
st->wave_peak,
st->ref_level >> 1,
b, l, b == l ? ' ' : 'x',
st->valid_counter,
st->crossings,
st->crossings_ticker);
#endif
return 0;
}
/* Return the timecode pitch, based on cycles of the sine wave. This
* function can only be called by one context, as it resets the state
* of the counter in the timecoder. */
int timecoder_get_pitch(struct timecoder_t *tc, float *pitch)
{
struct timecoder_channel_t *st;
st = &tc->state[0];
/* Let the caller know if there's no data to gather pitch from */
if(st->crossings_ticker == 0)
return -1;
/* Value of st->crossings may be negative */
*pitch = TIMECODER_RATE * (float)st->crossings / st->crossings_ticker
/ (def->resolution * 2);
st->crossings = 0;
st->crossings_ticker = 0;
return 0;
}
/* Return the known position in the timecode, or -1 if not known. If
* two few bits have been error-checked, then this also counts as
* invalid. If 'when' is given, return the time, in input samples since
* this value was read. */
signed int timecoder_get_position(struct timecoder_t *tc, int *when)
{
struct timecoder_channel_t *st;
signed int r;
st = &tc->state[0];
if(st->valid_counter > VALID_BITS) {
r = def->lookup[tc->state[0].bitstream];
if(r >= 0) {
if(when)
*when = st->timecode_ticker;
return r;
}
}
return -1;
}
/* Return non-zero if there is any timecode signal available */
int timecoder_get_alive(struct timecoder_t *tc)
{
struct timecoder_channel_t *st;
st = &tc->state[0];
if(st->signal_level < SIGNAL_THRESHOLD)
return 0;
return 1;
}
/* Return the last 'safe' timecode value on the record. Beyond this
* value, we probably want to ignore the timecode values, as we will
* hit the label of the record. */
unsigned int timecoder_get_safe(struct timecoder_t *tc)
{
return def->safe;
}
/* Return the resolution of the timecode. This is the number of bits
* per second, which corresponds to the frequency of the sine wave */
int timecoder_get_resolution(struct timecoder_t *tc)
{
return def->resolution;
}