]> git.sesse.net Git - audiosync/blob - do-skew.c
Make the skew generator tons worse.
[audiosync] / do-skew.c
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 int main(int argc, char **argv)
12 {
13         FILE *in, *skew, *out;
14
15         // open input
16         if (strcmp(argv[1], "-") == 0) {
17                 in = stdin;
18         } else {
19                 in = fopen(argv[1], "rb");
20                 if (in == NULL) {
21                         perror(argv[1]);
22                         exit(1);
23                 }
24         }
25
26         // open skew
27         if (strcmp(argv[2], "-") == 0) {
28                 skew = stdin;
29         } else {
30                 skew = fopen(argv[2], "rb");
31                 if (skew == NULL) {
32                         perror(argv[2]);
33                         exit(1);
34                 }
35         }
36
37         // open output
38         if (strcmp(argv[3], "-") == 0) {
39                 out = stderr;
40         } else {
41                 out = fopen(argv[3], "wb");
42                 if (in == NULL) {
43                         perror(argv[3]);
44                         exit(1);
45                 }
46         }
47
48         short prev_sample, sample = 0;
49         int in_pos = -1;
50         double p = -1.0;
51
52         while (!feof(in) && !feof(skew)) {
53                 double delta_p;
54                 if (fread(&delta_p, sizeof(double), 1, skew) != 1) {
55                         exit(0);
56                 }
57
58                 p += delta_p;
59
60                 // read samples until we're at the right position
61                 while ((int)(ceil(p)) > in_pos) {
62                         prev_sample = sample;
63                         if (fread(&sample, sizeof(short), 1, in) != 1) {
64                                 exit(0);
65                         }
66                         ++in_pos;
67                 }
68
69                 // linear interpolation (works well since delta_p varies so slowly)
70                 double t = frac(p);
71                 short intp_sample = prev_sample * (1.0 - t) + sample * t;
72
73                 fwrite(&intp_sample, sizeof(short), 1, out);
74         }
75 }