]> git.sesse.net Git - audiosync/blob - estimate-skew.cpp
f129c497e70623c82055ce87cd6e73e890b9da62
[audiosync] / estimate-skew.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <stdlib.h>
5
6 double frac(double x)
7 {
8         return x - (int)x;
9 }
10
11 class FlankDetector
12 {
13 private:
14         static const unsigned NUM_SAMPLES = 10;
15         static const int MIN_LEVEL = 8192;
16
17         bool status, flag;  // high/low
18         unsigned counter;
19
20 public:
21         FlankDetector() : status(false), flag(false), counter(0) {}
22         void update(short x)
23         {
24                 if (abs(x) < MIN_LEVEL) {
25                         counter = 0;
26                         return;
27                 }
28
29                 if (!status) {
30                         // low
31                         if (x >= MIN_LEVEL) {
32                                 ++counter;
33                         } else {
34                                 counter = 0;
35                         }
36                 } else {
37                         // high
38                         if (x <= -MIN_LEVEL) {
39                                 ++counter;
40                         } else {
41                                 counter = 0;
42                         }
43                 }
44
45                 if (counter >= NUM_SAMPLES) {
46                         status = !status;
47                         counter = 0;
48                         flag = true;
49                 }
50         }
51
52         bool check_flag()
53         {
54                 bool ret = flag;
55                 flag = false;
56                 return ret;
57         }
58
59         bool get_status() const
60         {
61                 return status;
62         }
63 };
64
65 class InterpolatingReader
66 {
67 private:
68         FILE *f;
69         double speed;
70         double p;
71         int in_pos;
72         short prev_sample;
73
74 public:
75         InterpolatingReader(FILE *f) : f(f), speed(1.0), p(0.0), in_pos(-1) {}
76
77         void update_speed(double speed)
78         {
79                 this->speed = speed;
80         }
81
82         double read_sample()
83         {
84                 short sample;
85
86                 p += speed;
87                 while ((int)(ceil(p)) > in_pos) {
88                         prev_sample = sample;
89                         if (fread(&sample, sizeof(short), 1, f) != 1) {
90                                 exit(0);
91                         }
92                         ++in_pos;
93                 }
94
95                 // linear interpolation (works OK since delta_p varies so slowly)
96                 double t = frac(p);
97                 return prev_sample * (1.0 - t) + sample * t;
98         }
99 };
100
101 double filter(double x)
102 {
103         static double l = 1.0;
104         l = 0.01 * x + 0.99 * l;
105         return l;
106 }
107
108 int main(int argc, char **argv)
109 {
110         FILE *in1, *in2, *skew;
111
112         // open input1 (reference)
113         if (strcmp(argv[1], "-") == 0) {
114                 in1 = stdin;
115         } else {
116                 in1 = fopen(argv[1], "rb");
117                 if (in1 == NULL) {
118                         perror(argv[1]);
119                         exit(1);
120                 }
121         }
122
123         // open input2
124         if (strcmp(argv[2], "-") == 0) {
125                 in2 = stdin;
126         } else {
127                 in2 = fopen(argv[2], "rb");
128                 if (in2 == NULL) {
129                         perror(argv[2]);
130                         exit(1);
131                 }
132         }
133
134         // open (estimated) skew
135         if (strcmp(argv[3], "-") == 0) {
136                 skew = stderr;
137         } else {
138                 skew = fopen(argv[3], "wb");
139                 if (skew == NULL) {
140                         perror(argv[3]);
141                         exit(1);
142                 }
143         }
144
145         FlankDetector f1, f2;
146
147         // initial sync, reference
148         do {
149                 short s;
150                 if (fread(&s, sizeof(short), 1, in1) != 1) {
151                         exit(0);
152                 }
153
154                 f1.update(s);
155         } while (!f1.check_flag() || !f1.get_status());
156         
157         // initial sync, input
158         do {
159                 short s;
160                 if (fread(&s, sizeof(short), 1, in2) != 1) {
161                         exit(0);
162                 }
163
164                 f2.update(s);
165         } while (!f2.check_flag() || !f2.get_status());
166
167         double speed = 1.000;
168
169         InterpolatingReader intp(in2);
170         intp.update_speed(speed);  // test to see if we can resync OK
171
172         while (!feof(in1) && !feof(in2)) {
173                 short refs;
174
175                 // read reference until the next triggering
176                 unsigned num_ref = 0;
177                 while (!feof(in1) && !f1.check_flag()) {
178                         if (fread(&refs, sizeof(short), 1, in1) != 1) {
179                                 exit(0);
180                         }
181                         f1.update(refs);
182                         ++num_ref;
183                 }
184
185                 // same here
186                 unsigned num_inp = 0;
187                 while (!feof(in2) && !f2.check_flag()) {
188                         double s = intp.read_sample();
189                         f2.update((short)s);
190                         ++num_inp;
191                 }
192
193                 double ns = filter(speed * double(num_inp) / double(num_ref));
194                 printf("%u vs. %u -- ratio %f, speed %f, filtered speed %f\n", num_inp, num_ref,
195                         double(num_inp) / double(num_ref), speed * double(num_inp) / double(num_ref), ns);
196                 speed = ns;
197                 intp.update_speed(ns);
198         }
199 }