summaryrefslogtreecommitdiff
path: root/libs/libltc/encoder.c
blob: 4e6168ea9bddc97a6bb0f3cf35829cdb9908d5f4 (plain)
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
/*
   libltc - en+decode linear timecode

   Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 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 Lesser 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, see <http://www.gnu.org/licenses/>.
*/

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

#include "ltc/encoder.h"

/**
 * add values to the output buffer
 */
static int addvalues(LTCEncoder *e, int n) {
	const ltcsnd_sample_t tgtval = e->state ? e->enc_hi : e->enc_lo;

	if (e->offset + n >= e->bufsize) {
#if 0
		fprintf(stderr, "libltc: buffer overflow: %d/%lu\n", (int) e->offset, (unsigned long) e->bufsize);
#endif
		return 1;
	}

	ltcsnd_sample_t * const wave = &(e->buf[e->offset]);
	const double tcf =  e->filter_const;
	if (tcf > 0) {
		/* low-pass-filter
		 * LTC signal should have a rise time of 40 us +/- 10 us.
		 *
		 * rise-time means from <10% to >90% of the signal.
		 * in each call to addvalues() we start at 50%, so
		 * here we need half-of it. (0.000020 sec)
		 *
		 * e->cutoff = 1.0 -exp( -1.0 / (sample_rate * .000020 / exp(1.0)) );
		 */
		int i;
		ltcsnd_sample_t val = SAMPLE_CENTER;
		int m = (n+1)>>1;
		for (i = 0 ; i < m ; i++) {
			val = val + tcf * (tgtval - val);
			wave[n-i-1] = wave[i] = val;
		}
	} else {
		/* perfect square wave */
		memset(wave, tgtval, n);
	}

	e->offset += n;
	return 0;
}

int encode_byte(LTCEncoder *e, int byte, double speed) {
	if (byte < 0 || byte > 9) return -1;
	if (speed ==0) return -1;

	int err = 0;
	const unsigned char c = ((unsigned char*)&e->f)[byte];
	unsigned char b = (speed < 0)?128:1; // bit
	const double spc = e->samples_per_clock * fabs(speed);
	const double sph = e->samples_per_clock_2 * fabs(speed);

	do
	{
		int n;
		if ((c & b) == 0) {
			n = (int)(spc + e->sample_remainder);
			e->sample_remainder = spc + e->sample_remainder - n;
			e->state = !e->state;
			err |= addvalues(e, n);
		} else {
			n = (int)(sph + e->sample_remainder);
			e->sample_remainder = sph + e->sample_remainder - n;
			e->state = !e->state;
			err |= addvalues(e, n);

			n = (int)(sph + e->sample_remainder);
			e->sample_remainder = sph + e->sample_remainder - n;
			e->state = !e->state;
			err |= addvalues(e, n);
		}
		/* this is based on the assumption that with every compiler
		 * ((unsigned char) 128)<<1 == ((unsigned char 1)>>1) == 0
		 */
		if (speed < 0)
			b >>= 1;
		else
			b <<= 1;
	} while (b);

	return err;
}