]> git.sesse.net Git - c64tapwav/blob - decode.c
7e4c84b182bcea56530cda7bedcb9c3e334e3dd0
[c64tapwav] / decode.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <unistd.h>
4 #include <assert.h>
5 #include <algorithm>
6
7 #define LANCZOS_RADIUS 30
8 #define LEN 813440
9
10 double sinc(double x)
11 {
12         if (fabs(x) < 1e-6) {
13                 return 1.0f - fabs(x);
14         } else {
15                 return sin(x) / x;
16         }
17 }
18
19 #if 1
20 double weight(double x)
21 {
22         if (fabs(x) > LANCZOS_RADIUS) {
23                 return 0.0f;
24         }
25         return sinc(M_PI * x) * sinc(M_PI * x / LANCZOS_RADIUS);
26 }
27 #else
28 double weight(double x)
29 {
30         if (fabs(x) > 1.0f) {
31                 return 0.0f;
32         }
33         return 1.0f - fabs(x);
34 }
35 #endif
36
37 double interpolate(const short *in, double i)
38 {
39         int lower = std::max(int(ceil(i - LANCZOS_RADIUS)), 0);
40         int upper = std::min(int(floor(i + LANCZOS_RADIUS)), LEN - 1);
41         double sum = 0.0f;
42
43         for (int x = lower; x <= upper; ++x) {
44                 sum += in[x] * weight(i - x);
45         }
46         return sum;
47 }
48         
49 short in[LEN];
50
51 // between [x,x+1]
52 double find_zerocrossing(int x)
53 {
54         if (in[x] == 0) {
55                 return x;
56         }
57         if (in[x + 1] == 0) {
58                 return x + 1;
59         }
60
61         assert(in[x + 1] > 0);
62         assert(in[x] < 0);
63
64         double lower = x;
65         double upper = x + 1;
66         while (upper - lower > 1e-6) {
67                 double mid = 0.5f * (upper + lower);
68                 if (interpolate(in, mid) > 0) {
69                         upper = mid;
70                 } else {
71                         lower = mid;
72                 }
73         }
74
75         return 0.5f * (upper + lower);
76 }
77
78 int main(int argc, char **argv)
79 {
80         fread(in, LEN*2, 1, stdin);
81
82 #if 0
83         for (int i = 0; i < LEN; ++i) {
84                 in[i] += rand() % 10000;
85         }
86 #endif
87
88 #if 0
89         for (int i = 0; i < LEN; ++i) {
90                 printf("%d\n", in[i]);
91         }
92 #endif
93         int last_bit = -1;
94         double last_upflank = -1;
95         for (int i = 0; i < LEN; ++i) {
96                 int bit = (in[i] > 0) ? 1 : 0;
97                 if (bit == 1 && last_bit == 0) {
98                         // up-flank!
99                         double t = find_zerocrossing(i - 1) * (123156.0/44100.0);
100                         if (last_upflank > 0) {
101 //                              fprintf(stderr, "length: %f (0x%x)\n", t - last_upflank, lrintf(t - last_upflank));
102                                 int len = lrintf(t - last_upflank);
103                                 printf("0x%x\n", len);
104                         }
105                         last_upflank = t;
106                 }
107                 last_bit = bit;
108         }
109 }