~mkas/mixxx/mysql

« back to all changes in this revision

Viewing changes to mixxx/lib/xwax/timecoder.c

  • Committer: MKas
  • Date: 2012-11-03 12:55:54 UTC
  • Revision ID: mkas@tux.lt-20121103125554-ez5ajqyk7bwehrp2
merge with trunk + sql fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2010 Mark Hills <mark@pogo.org.uk>
 
2
 * Copyright (C) 2012 Mark Hills <mark@pogo.org.uk>
3
3
 *
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.
7
 
 * 
 
7
 *
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.
12
 
 * 
 
12
 *
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,
23
23
#include <string.h>
24
24
#include <unistd.h>
25
25
 
 
26
#include "debug.h"
26
27
#include "timecoder.h"
27
28
 
28
29
#define ZERO_THRESHOLD 128
31
32
 
32
33
#define REF_PEAKS_AVG 48 /* in wave cycles */
33
34
 
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 */
37
38
 
38
39
#define VALID_BITS 24
39
40
 
40
41
#define MONITOR_DECAY_EVERY 512 /* in samples */
41
42
 
42
43
#define SQ(x) ((x)*(x))
43
 
 
 
44
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
44
45
 
45
46
/* Timecode definitions */
46
47
 
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) */
50
51
 
51
 
 
52
 
static struct timecode_def_t timecode_def[] = {
 
52
static struct timecode_def timecodes[] = {
53
53
    {
54
54
        .name = "serato_2a",
55
55
        .desc = "Serato 2nd Ed., side A",
71
71
        .seed = 0x8f3c6,
72
72
        .taps = 0x4f0d8, /* reverse of side A */
73
73
        .length = 922000,
74
 
        .safe = 905000,
 
74
        .safe = 908000,
75
75
        .lookup = false
76
76
    },
77
77
    {
95
95
        .seed = 0x134503,
96
96
        .taps = 0x041040,
97
97
        .length = 1500000,
98
 
        .safe = 1214000,
99
 
        .lookup = false        
 
98
        .safe = 605000,
 
99
        .lookup = false
100
100
    },
101
101
    {
102
102
        .name = "traktor_b",
107
107
        .seed = 0x32066c,
108
108
        .taps = 0x041040, /* same as side A */
109
109
        .length = 2110000,
110
 
        .safe = 1814000,
 
110
        .safe = 907000,
111
111
        .lookup = false
112
112
    },
113
113
    {
119
119
        .seed = 0x22c90,
120
120
        .taps = 0x00008,
121
121
        .length = 950000,
122
 
        .safe = 923000,
 
122
        .safe = 710000,
123
123
        .lookup = false
124
124
    },
125
125
    {
131
131
        .seed = 0x22c90,
132
132
        .taps = 0x00008,
133
133
        .length = 312000,
134
 
        .safe = 310000,
 
134
        .safe = 238000,
135
135
        .lookup = false
136
136
    },
137
 
    {
138
 
        .name = NULL
139
 
    }
140
137
};
141
138
 
142
 
 
143
 
/* Linear Feeback Shift Register in the forward direction. New values
144
 
 * are generated at the least-significant bit. */
 
139
/*
 
140
 * Calculate LFSR bit
 
141
 */
145
142
 
146
143
static inline bits_t lfsr(bits_t code, bits_t taps)
147
144
{
158
155
    return xrs & 0x1;
159
156
}
160
157
 
 
158
/*
 
159
 * Linear Feedback Shift Register in the forward direction. New values
 
160
 * are generated at the least-significant bit.
 
161
 */
161
162
 
162
 
static inline bits_t fwd(bits_t current, struct timecode_def_t *def)
 
163
static inline bits_t fwd(bits_t current, struct timecode_def *def)
163
164
{
164
165
    bits_t l;
165
166
 
169
170
    return (current >> 1) | (l << (def->bits - 1));
170
171
}
171
172
 
 
173
/*
 
174
 * Linear Feedback Shift Register in the reverse direction
 
175
 */
172
176
 
173
 
static inline bits_t rev(bits_t current, struct timecode_def_t *def)
 
177
static inline bits_t rev(bits_t current, struct timecode_def *def)
174
178
{
175
179
    bits_t l, mask;
176
180
 
181
185
    return ((current << 1) & mask) | l;
182
186
}
183
187
 
184
 
 
185
 
static struct timecode_def_t* find_definition(const char *name)
186
 
{
187
 
    struct timecode_def_t *def;
188
 
 
189
 
    def = &timecode_def[0];
190
 
    while (def->name) {
191
 
        if (!strcmp(def->name, name))
192
 
            return def;
193
 
        def++;
194
 
    }
195
 
    return NULL;
196
 
}
197
 
 
198
 
 
199
 
/* Where necessary, build the lookup table required for this timecode */
200
 
 
201
 
static int build_lookup(struct timecode_def_t *def)
 
188
/*
 
189
 * Where necessary, build the lookup table required for this timecode
 
190
 *
 
191
 * Return: -1 if not enough memory could be allocated, otherwise 0
 
192
 */
 
193
 
 
194
static int build_lookup(struct timecode_def *def)
202
195
{
203
196
    unsigned int n;
204
197
    bits_t current, last;
213
206
        return -1;
214
207
 
215
208
    current = def->seed;
216
 
    
 
209
 
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);
221
214
        last = current;
222
215
        current = fwd(current, def);
223
 
        assert(rev(current, def) == last);
 
216
        dassert(rev(current, def) == last);
224
217
    }
225
218
 
226
219
    def->lookup = true;
227
 
    
228
 
    return 0;    
229
 
}
230
 
 
231
 
 
232
 
/* Free the timecoder lookup tables when they are no longer needed */
 
220
 
 
221
    return 0;
 
222
}
 
223
 
 
224
/*
 
225
 * Find a timecode definition by name
 
226
 *
 
227
 * Return: pointer to timecode definition, or NULL if not found
 
228
 */
 
229
 
 
230
struct timecode_def* timecoder_find_definition(const char *name)
 
231
{
 
232
    struct timecode_def *def, *end;
 
233
 
 
234
    def = &timecodes[0];
 
235
    end = def + ARRAY_SIZE(timecodes);
 
236
 
 
237
    for (;;) {
 
238
        if (!strcmp(def->name, name))
 
239
            break;
 
240
 
 
241
        def++;
 
242
 
 
243
        if (def == end)
 
244
            return NULL;
 
245
    }
 
246
 
 
247
    if (build_lookup(def) == -1)
 
248
        return NULL;
 
249
 
 
250
    return def;
 
251
}
 
252
 
 
253
/*
 
254
 * Free the timecoder lookup tables when they are no longer needed
 
255
 */
233
256
 
234
257
void timecoder_free_lookup(void) {
235
 
    struct timecode_def_t *def;
236
 
 
237
 
    def = &timecode_def[0];
238
 
    while (def->name) {
 
258
    struct timecode_def *def, *end;
 
259
 
 
260
    def = &timecodes[0];
 
261
    end = def + ARRAY_SIZE(timecodes);
 
262
 
 
263
    while (def < end) {
239
264
        if (def->lookup)
240
265
            lut_clear(&def->lut);
241
266
        def++;
242
267
    }
243
268
}
244
269
 
 
270
/*
 
271
 * Initialise filter values for one channel
 
272
 */
245
273
 
246
 
static void init_channel(struct timecoder_channel_t *ch)
 
274
static void init_channel(struct timecoder_channel *ch)
247
275
{
248
 
    ch->positive = 0;
 
276
    ch->positive = false;
249
277
    ch->zero = 0;
250
278
}
251
279
 
252
 
 
253
 
/* Initialise a timecode decoder at the given reference speed */
254
 
 
255
 
int timecoder_init(struct timecoder_t *tc, const char *def_name, double speed,
256
 
                   unsigned int sample_rate)
 
280
/*
 
281
 * Initialise a timecode decoder at the given reference speed
 
282
 *
 
283
 * Return: -1 if the timecoder could not be initialised, otherwise 0
 
284
 */
 
285
 
 
286
void timecoder_init(struct timecoder *tc, struct timecode_def *def,
 
287
                    double speed, unsigned int sample_rate)
257
288
{
 
289
    assert(def != NULL);
 
290
 
258
291
    /* A definition contains a lookup table which can be shared
259
292
     * across multiple timecoders */
260
293
 
261
 
    tc->def = find_definition(def_name);
262
 
    if (tc->def == NULL) {
263
 
        fprintf(stderr, "Timecode definition '%s' is not known.\n", def_name);
264
 
        return -1;
265
 
    }
266
 
    if (build_lookup(tc->def) == -1)
267
 
        return -1;
 
294
    assert(def->lookup);
 
295
    tc->def = def;
268
296
    tc->speed = speed;
269
297
 
270
298
    tc->dt = 1.0 / sample_rate;
282
310
    tc->timecode_ticker = 0;
283
311
 
284
312
    tc->mon = NULL;
285
 
 
286
 
    return 0;
287
 
}
288
 
 
289
 
 
290
 
/* Clear a timecode decoder */
291
 
 
292
 
void timecoder_clear(struct timecoder_t *tc)
293
 
{
294
 
    timecoder_monitor_clear(tc);
295
 
}
296
 
 
297
 
 
298
 
/* The monitor (otherwise known as 'scope' in the interface) is the
299
 
 * display of the incoming audio. Initialise one for the given
300
 
 * timecoder */
301
 
 
302
 
int timecoder_monitor_init(struct timecoder_t *tc, int size)
303
 
{
 
313
}
 
314
 
 
315
/*
 
316
 * Clear resources associated with a timecode decoder
 
317
 */
 
318
 
 
319
void timecoder_clear(struct timecoder *tc)
 
320
{
 
321
    assert(tc->mon == NULL);
 
322
}
 
323
 
 
324
/*
 
325
 * Initialise a raster display of the incoming audio
 
326
 *
 
327
 * The monitor (otherwise known as 'scope' in the interface) is an x-y
 
328
 * display of the post-calibrated incoming audio.
 
329
 *
 
330
 * Return: -1 if not enough memory could be allocated, otherwise 0
 
331
 */
 
332
 
 
333
int timecoder_monitor_init(struct timecoder *tc, int size)
 
334
{
 
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) {
312
344
    return 0;
313
345
}
314
346
 
315
 
 
316
 
/* Clear the monitor on the given timecoder */
317
 
 
318
 
void timecoder_monitor_clear(struct timecoder_t *tc)
 
347
/*
 
348
 * Clear the monitor on the given timecoder
 
349
 */
 
350
 
 
351
void timecoder_monitor_clear(struct timecoder *tc)
319
352
{
320
 
    if (tc->mon) {
321
 
        free(tc->mon);
322
 
        tc->mon = NULL;
323
 
    }
 
353
    assert(tc->mon != NULL);
 
354
    free(tc->mon);
 
355
    tc->mon = NULL;
324
356
}
325
357
 
 
358
/*
 
359
 * Update channel information with axis-crossings
 
360
 */
326
361
 
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)
329
364
{
330
365
    ch->crossing_ticker++;
331
366
 
332
 
    ch->swapped = 0;
 
367
    ch->swapped = false;
333
368
    if (v > ch->zero + ZERO_THRESHOLD && !ch->positive) {
334
 
        ch->swapped = 1;
335
 
        ch->positive = 1;
 
369
        ch->swapped = true;
 
370
        ch->positive = true;
336
371
        ch->crossing_ticker = 0;
337
372
    } else if (v < ch->zero - ZERO_THRESHOLD && ch->positive) {
338
 
        ch->swapped = 1;
339
 
        ch->positive = 0;
 
373
        ch->swapped = true;
 
374
        ch->positive = false;
340
375
        ch->crossing_ticker = 0;
341
376
    }
342
 
    
 
377
 
343
378
    ch->zero += alpha * (v - ch->zero);
344
379
}
345
380
 
346
 
 
347
 
/* Plot the given sample value in the monitor (scope) */
348
 
 
349
 
static void update_monitor(struct timecoder_t *tc, signed int x, signed int y)
 
381
/*
 
382
 * Plot the given sample value in the x-y monitor
 
383
 */
 
384
 
 
385
static void update_monitor(struct timecoder *tc, signed int x, signed int y)
350
386
{
351
387
    int px, py, p;
352
 
    float v, w;
 
388
    double v, w;
353
389
 
354
390
    if (!tc->mon)
355
391
        return;
356
392
 
357
393
    /* Decay the pixels already in the montior */
358
 
        
 
394
 
359
395
    if (++tc->mon_counter % MONITOR_DECAY_EVERY == 0) {
360
396
        for (p = 0; p < SQ(tc->mon_size); p++) {
361
397
            if (tc->mon[p])
362
398
                tc->mon[p] = tc->mon[p] * 7 / 8;
363
399
        }
364
400
    }
365
 
        
366
 
    v = (float)x / tc->ref_level / 2;
367
 
    w = (float)y / tc->ref_level / 2;
368
 
        
 
401
 
 
402
    v = (double)x / tc->ref_level / 2;
 
403
    w = (double)y / tc->ref_level / 2;
 
404
 
369
405
    px = tc->mon_size / 2 + (v * tc->mon_size / 2);
370
406
    py = tc->mon_size / 2 + (w * tc->mon_size / 2);
371
407
 
372
408
    /* Set the pixel value to white */
373
 
            
 
409
 
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;
376
412
}
377
413
 
378
 
 
379
 
/* Process a single bitstream reading */
380
 
 
381
 
static void process_bitstream(struct timecoder_t *tc, signed int m)
 
414
/*
 
415
 * Extract the bitstream from the sample value
 
416
 */
 
417
 
 
418
static void process_bitstream(struct timecoder *tc, signed int m)
382
419
{
383
420
    bits_t b;
384
421
 
411
448
    }
412
449
 
413
450
    /* Take note of the last time we read a valid timecode */
414
 
    
 
451
 
415
452
    tc->timecode_ticker = 0;
416
453
 
417
454
    /* Adjust the reference level based on this new peak */
418
455
 
419
456
    tc->ref_level = (tc->ref_level * (REF_PEAKS_AVG - 1) + m) / REF_PEAKS_AVG;
420
457
 
421
 
#ifdef DEBUG_BITSTREAM
422
 
    fprintf(stderr, "%+6d zero, %+6d (ref %+6d)\t= %d%c (%5d)\n",
423
 
            tc->primary.zero,
424
 
            m,
425
 
            tc->ref_level,
426
 
            b,
427
 
            tc->valid_counter == 0 ? 'x' : ' ',
428
 
            tc->valid_counter);
429
 
#endif
 
458
    debug("%+6d zero, %+6d (ref %+6d)\t= %d%c (%5d)\n",
 
459
          tc->primary.zero,
 
460
          m, tc->ref_level,
 
461
          b, tc->valid_counter == 0 ? 'x' : ' ',
 
462
          tc->valid_counter);
430
463
}
431
464
 
432
 
 
433
 
/* Process a single sample from the incoming audio */
434
 
 
435
 
static void process_sample(struct timecoder_t *tc,
 
465
/*
 
466
 * Process a single sample from the incoming audio
 
467
 */
 
468
 
 
469
static void process_sample(struct timecoder *tc,
436
470
                           signed int primary, signed int secondary)
437
471
{
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 */
447
481
 
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) {
 
483
        bool forwards;
 
484
 
 
485
        if (tc->primary.swapped) {
 
486
            forwards = (tc->primary.positive != tc->secondary.positive);
 
487
        } else {
 
488
            forwards = (tc->primary.positive == tc->secondary.positive);
 
489
        }
 
490
 
 
491
        if (tc->def->flags & SWITCH_PHASE)
 
492
            forwards = !forwards;
 
493
 
 
494
        if (forwards != tc->forwards) { /* direction has changed */
 
495
            tc->forwards = forwards;
 
496
            tc->valid_counter = 0;
 
497
        }
456
498
    }
457
499
 
458
500
    /* If any axis has been crossed, register movement using the pitch
461
503
    if (!tc->primary.swapped && !tc->secondary.swapped)
462
504
        pitch_dt_observation(&tc->pitch, 0.0);
463
505
    else {
464
 
        float dx;
 
506
        double dx;
465
507
 
466
508
        dx = 1.0 / tc->def->resolution / 4;
467
509
        if (!tc->forwards)
481
523
    tc->timecode_ticker++;
482
524
}
483
525
 
484
 
 
485
 
/* Submit and decode a block of PCM audio data to the timecoder */
486
 
 
487
 
void timecoder_submit(struct timecoder_t *tc, const signed short *pcm, size_t npcm)
 
526
/*
 
527
 * Cycle to the next timecode definition which has a valid lookup
 
528
 *
 
529
 * Return: pointer to timecode definition
 
530
 */
 
531
 
 
532
static struct timecode_def* next_definition(struct timecode_def *def)
 
533
{
 
534
    assert(def != NULL);
 
535
 
 
536
    do {
 
537
        def++;
 
538
 
 
539
        if (def > timecodes + ARRAY_SIZE(timecodes))
 
540
            def = timecodes;
 
541
 
 
542
    } while (!def->lookup);
 
543
 
 
544
    return def;
 
545
}
 
546
 
 
547
/*
 
548
 * Change the timecode definition to the next available
 
549
 */
 
550
 
 
551
void timecoder_cycle_definition(struct timecoder *tc)
 
552
{
 
553
    tc->def = next_definition(tc->def);
 
554
    tc->valid_counter = 0;
 
555
    tc->timecode_ticker = 0;
 
556
}
 
557
 
 
558
/*
 
559
 * Submit and decode a block of PCM audio data to the timecode decoder
 
560
 */
 
561
 
 
562
void timecoder_submit(struct timecoder *tc, const signed short *pcm, size_t npcm)
488
563
{
489
564
    while (npcm--) {
490
565
        signed int primary, secondary;
504
579
    }
505
580
}
506
581
 
507
 
 
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
511
 
 * value was read. */
512
 
 
513
 
signed int timecoder_get_position(struct timecoder_t *tc, float *when)
 
582
/*
 
583
 * Get the last-known position of the timecode
 
584
 *
 
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
 
588
 * read.
 
589
 *
 
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
 
592
 */
 
593
 
 
594
signed int timecoder_get_position(struct timecoder *tc, double *when)
514
595
{
515
596
    signed int r;
516
597
 
518
599
        r = lut_lookup(&tc->def->lut, tc->bitstream);
519
600
 
520
601
        if (r >= 0) {
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));
523
604
            if (when)
524
605
                *when = tc->timecode_ticker * tc->dt;
525
606
            return r;
526
607
        }
527
608
    }
528
 
    
 
609
 
529
610
    return -1;
530
611
}