2
* Copyright (C) 2010 Mark Hills <mark@pogo.org.uk>
2
* Copyright (C) 2012 Mark Hills <mark@pogo.org.uk>
4
4
* This program is free software; you can redistribute it and/or
5
5
* modify it under the terms of the GNU General Public License
6
6
* version 2, as published by the Free Software Foundation.
8
8
* This program is distributed in the hope that it will be useful, but
9
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
11
* General Public License version 2 for more details.
13
13
* You should have received a copy of the GNU General Public License
14
14
* version 2 along with this program; if not, write to the Free
15
15
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32
33
#define REF_PEAKS_AVG 48 /* in wave cycles */
34
/* The number of correct bits which come in before the timecode
35
* is declared valid. Set this too low, and risk the record skipping around
36
* (often to blank areas of track) during scratching */
35
/* The number of correct bits which come in before the timecode is
36
* declared valid. Set this too low, and risk the record skipping
37
* around (often to blank areas of track) during scratching */
38
39
#define VALID_BITS 24
40
41
#define MONITOR_DECAY_EVERY 512 /* in samples */
42
43
#define SQ(x) ((x)*(x))
44
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
45
46
/* Timecode definitions */
48
49
#define SWITCH_PRIMARY 0x2 /* use left channel (not right) as primary */
49
50
#define SWITCH_POLARITY 0x4 /* read bit values in negative (not positive) */
52
static struct timecode_def_t timecode_def[] = {
52
static struct timecode_def timecodes[] = {
54
54
.name = "serato_2a",
55
55
.desc = "Serato 2nd Ed., side A",
181
185
return ((current << 1) & mask) | l;
185
static struct timecode_def_t* find_definition(const char *name)
187
struct timecode_def_t *def;
189
def = &timecode_def[0];
191
if (!strcmp(def->name, name))
199
/* Where necessary, build the lookup table required for this timecode */
201
static int build_lookup(struct timecode_def_t *def)
189
* Where necessary, build the lookup table required for this timecode
191
* Return: -1 if not enough memory could be allocated, otherwise 0
194
static int build_lookup(struct timecode_def *def)
204
197
bits_t current, last;
215
208
current = def->seed;
217
210
for (n = 0; n < def->length; n++) {
218
211
/* timecode must not wrap */
219
assert(lut_lookup(&def->lut, current) == (unsigned)-1);
212
dassert(lut_lookup(&def->lut, current) == (unsigned)-1);
220
213
lut_push(&def->lut, current);
222
215
current = fwd(current, def);
223
assert(rev(current, def) == last);
216
dassert(rev(current, def) == last);
226
219
def->lookup = true;
232
/* Free the timecoder lookup tables when they are no longer needed */
225
* Find a timecode definition by name
227
* Return: pointer to timecode definition, or NULL if not found
230
struct timecode_def* timecoder_find_definition(const char *name)
232
struct timecode_def *def, *end;
235
end = def + ARRAY_SIZE(timecodes);
238
if (!strcmp(def->name, name))
247
if (build_lookup(def) == -1)
254
* Free the timecoder lookup tables when they are no longer needed
234
257
void timecoder_free_lookup(void) {
235
struct timecode_def_t *def;
237
def = &timecode_def[0];
258
struct timecode_def *def, *end;
261
end = def + ARRAY_SIZE(timecodes);
240
265
lut_clear(&def->lut);
271
* Initialise filter values for one channel
246
static void init_channel(struct timecoder_channel_t *ch)
274
static void init_channel(struct timecoder_channel *ch)
276
ch->positive = false;
253
/* Initialise a timecode decoder at the given reference speed */
255
int timecoder_init(struct timecoder_t *tc, const char *def_name, double speed,
256
unsigned int sample_rate)
281
* Initialise a timecode decoder at the given reference speed
283
* Return: -1 if the timecoder could not be initialised, otherwise 0
286
void timecoder_init(struct timecoder *tc, struct timecode_def *def,
287
double speed, unsigned int sample_rate)
258
291
/* A definition contains a lookup table which can be shared
259
292
* across multiple timecoders */
261
tc->def = find_definition(def_name);
262
if (tc->def == NULL) {
263
fprintf(stderr, "Timecode definition '%s' is not known.\n", def_name);
266
if (build_lookup(tc->def) == -1)
268
296
tc->speed = speed;
270
298
tc->dt = 1.0 / sample_rate;
282
310
tc->timecode_ticker = 0;
290
/* Clear a timecode decoder */
292
void timecoder_clear(struct timecoder_t *tc)
294
timecoder_monitor_clear(tc);
298
/* The monitor (otherwise known as 'scope' in the interface) is the
299
* display of the incoming audio. Initialise one for the given
302
int timecoder_monitor_init(struct timecoder_t *tc, int size)
316
* Clear resources associated with a timecode decoder
319
void timecoder_clear(struct timecoder *tc)
321
assert(tc->mon == NULL);
325
* Initialise a raster display of the incoming audio
327
* The monitor (otherwise known as 'scope' in the interface) is an x-y
328
* display of the post-calibrated incoming audio.
330
* Return: -1 if not enough memory could be allocated, otherwise 0
333
int timecoder_monitor_init(struct timecoder *tc, int size)
335
assert(tc->mon == NULL);
304
336
tc->mon_size = size;
305
337
tc->mon = malloc(SQ(tc->mon_size));
306
338
if (tc->mon == NULL) {
316
/* Clear the monitor on the given timecoder */
318
void timecoder_monitor_clear(struct timecoder_t *tc)
348
* Clear the monitor on the given timecoder
351
void timecoder_monitor_clear(struct timecoder *tc)
353
assert(tc->mon != NULL);
359
* Update channel information with axis-crossings
327
static void detect_zero_crossing(struct timecoder_channel_t *ch,
328
signed int v, float alpha)
362
static void detect_zero_crossing(struct timecoder_channel *ch,
363
signed int v, double alpha)
330
365
ch->crossing_ticker++;
333
368
if (v > ch->zero + ZERO_THRESHOLD && !ch->positive) {
336
371
ch->crossing_ticker = 0;
337
372
} else if (v < ch->zero - ZERO_THRESHOLD && ch->positive) {
374
ch->positive = false;
340
375
ch->crossing_ticker = 0;
343
378
ch->zero += alpha * (v - ch->zero);
347
/* Plot the given sample value in the monitor (scope) */
349
static void update_monitor(struct timecoder_t *tc, signed int x, signed int y)
382
* Plot the given sample value in the x-y monitor
385
static void update_monitor(struct timecoder *tc, signed int x, signed int y)
357
393
/* Decay the pixels already in the montior */
359
395
if (++tc->mon_counter % MONITOR_DECAY_EVERY == 0) {
360
396
for (p = 0; p < SQ(tc->mon_size); p++) {
362
398
tc->mon[p] = tc->mon[p] * 7 / 8;
366
v = (float)x / tc->ref_level / 2;
367
w = (float)y / tc->ref_level / 2;
402
v = (double)x / tc->ref_level / 2;
403
w = (double)y / tc->ref_level / 2;
369
405
px = tc->mon_size / 2 + (v * tc->mon_size / 2);
370
406
py = tc->mon_size / 2 + (w * tc->mon_size / 2);
372
408
/* Set the pixel value to white */
374
410
if (px > 0 && px < tc->mon_size && py > 0 && py < tc->mon_size)
375
411
tc->mon[py * tc->mon_size + px] = 0xff;
379
/* Process a single bitstream reading */
381
static void process_bitstream(struct timecoder_t *tc, signed int m)
415
* Extract the bitstream from the sample value
418
static void process_bitstream(struct timecoder *tc, signed int m)
413
450
/* Take note of the last time we read a valid timecode */
415
452
tc->timecode_ticker = 0;
417
454
/* Adjust the reference level based on this new peak */
419
456
tc->ref_level = (tc->ref_level * (REF_PEAKS_AVG - 1) + m) / REF_PEAKS_AVG;
421
#ifdef DEBUG_BITSTREAM
422
fprintf(stderr, "%+6d zero, %+6d (ref %+6d)\t= %d%c (%5d)\n",
427
tc->valid_counter == 0 ? 'x' : ' ',
458
debug("%+6d zero, %+6d (ref %+6d)\t= %d%c (%5d)\n",
461
b, tc->valid_counter == 0 ? 'x' : ' ',
433
/* Process a single sample from the incoming audio */
435
static void process_sample(struct timecoder_t *tc,
466
* Process a single sample from the incoming audio
469
static void process_sample(struct timecoder *tc,
436
470
signed int primary, signed int secondary)
438
472
signed int m; /* pcm sample, sum of two shorts */
445
479
/* If an axis has been crossed, use the direction of the crossing
446
480
* to work out the direction of the vinyl */
448
if (tc->primary.swapped) {
449
tc->forwards = (tc->primary.positive != tc->secondary.positive);
450
if (tc->def->flags & SWITCH_PHASE)
451
tc->forwards = !tc->forwards;
452
} if (tc->secondary.swapped) {
453
tc->forwards = (tc->primary.positive == tc->secondary.positive);
454
if (tc->def->flags & SWITCH_PHASE)
455
tc->forwards = !tc->forwards;
482
if (tc->primary.swapped || tc->secondary.swapped) {
485
if (tc->primary.swapped) {
486
forwards = (tc->primary.positive != tc->secondary.positive);
488
forwards = (tc->primary.positive == tc->secondary.positive);
491
if (tc->def->flags & SWITCH_PHASE)
492
forwards = !forwards;
494
if (forwards != tc->forwards) { /* direction has changed */
495
tc->forwards = forwards;
496
tc->valid_counter = 0;
458
500
/* If any axis has been crossed, register movement using the pitch
481
523
tc->timecode_ticker++;
485
/* Submit and decode a block of PCM audio data to the timecoder */
487
void timecoder_submit(struct timecoder_t *tc, const signed short *pcm, size_t npcm)
527
* Cycle to the next timecode definition which has a valid lookup
529
* Return: pointer to timecode definition
532
static struct timecode_def* next_definition(struct timecode_def *def)
539
if (def > timecodes + ARRAY_SIZE(timecodes))
542
} while (!def->lookup);
548
* Change the timecode definition to the next available
551
void timecoder_cycle_definition(struct timecoder *tc)
553
tc->def = next_definition(tc->def);
554
tc->valid_counter = 0;
555
tc->timecode_ticker = 0;
559
* Submit and decode a block of PCM audio data to the timecode decoder
562
void timecoder_submit(struct timecoder *tc, const signed short *pcm, size_t npcm)
490
565
signed int primary, secondary;
508
/* Return the known position in the timecode, or -1 if not known. If
509
* two few bits have been error-checked, then this also counts as
510
* invalid. If 'when' is given, return the time, in seconds since this
513
signed int timecoder_get_position(struct timecoder_t *tc, float *when)
583
* Get the last-known position of the timecode
585
* If now data is available or if too few bits have been error
586
* checked, then this counts as invalid. The last known position is
587
* given along with the time elapsed since the position stamp was
590
* Return: the known position of the timecode, or -1 if not known
591
* Post: if when != NULL, *when is the elapsed time in seconds
594
signed int timecoder_get_position(struct timecoder *tc, double *when)
518
599
r = lut_lookup(&tc->def->lut, tc->bitstream);
521
//normalize position to milliseconds, not timecode steps -- Owen
522
r = (float)r * (1000.0 / (tc->def->resolution * tc->speed));
602
//normalize position to milliseconds, not timecode steps -- Owen
603
r = (float)r * (1000.0 / (tc->def->resolution * tc->speed));
524
605
*when = tc->timecode_ticker * tc->dt;