]> git.sesse.net Git - c64tapwav/blob - decode.c
e20c8c9d39743f3b0c011fe1d25bc754f81f9152
[c64tapwav] / decode.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <unistd.h>
4 #include <assert.h>
5 #include <vector>
6 #include <algorithm>
7
8 #define LANCZOS_RADIUS 30
9 #define BUFSIZE 4096
10 #define HYSTERESIS_LIMIT 1000
11
12 double sinc(double x)
13 {
14         if (fabs(x) < 1e-6) {
15                 return 1.0f - fabs(x);
16         } else {
17                 return sin(x) / x;
18         }
19 }
20
21 #if 1
22 double weight(double x)
23 {
24         if (fabs(x) > LANCZOS_RADIUS) {
25                 return 0.0f;
26         }
27         return sinc(M_PI * x) * sinc(M_PI * x / LANCZOS_RADIUS);
28 }
29 #else
30 double weight(double x)
31 {
32         if (fabs(x) > 1.0f) {
33                 return 0.0f;
34         }
35         return 1.0f - fabs(x);
36 }
37 #endif
38
39 double interpolate(const std::vector<short> &pcm, double i)
40 {
41         int lower = std::max<int>(ceil(i - LANCZOS_RADIUS), 0);
42         int upper = std::min<int>(floor(i + LANCZOS_RADIUS), pcm.size() - 1);
43         double sum = 0.0f;
44
45         for (int x = lower; x <= upper; ++x) {
46                 sum += pcm[x] * weight(i - x);
47         }
48         return sum;
49 }
50         
51 // between [x,x+1]
52 double find_zerocrossing(const std::vector<short> &pcm, int x)
53 {
54         if (pcm[x] == 0) {
55                 return x;
56         }
57         if (pcm[x + 1] == 0) {
58                 return x + 1;
59         }
60
61         assert(pcm[x + 1] > 0);
62         assert(pcm[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(pcm, 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         std::vector<short> pcm;
81
82         while (!feof(stdin)) {
83                 short buf[BUFSIZE];
84                 ssize_t ret = fread(buf, 2, BUFSIZE, stdin);
85                 if (ret >= 0) {
86                         pcm.insert(pcm.end(), buf, buf + ret);
87                 }
88         }       
89
90 #if 0
91         for (int i = 0; i < LEN; ++i) {
92                 in[i] += rand() % 10000;
93         }
94 #endif
95
96 #if 0
97         for (int i = 0; i < LEN; ++i) {
98                 printf("%d\n", in[i]);
99         }
100 #endif
101         int last_bit = -1;
102         double last_upflank = -1;
103         int last_max_level = 0;
104         for (int i = 0; i < pcm.size(); ++i) {
105                 int bit = (pcm[i] > 0) ? 1 : 0;
106                 if (bit == 1 && last_bit == 0 && last_max_level > HYSTERESIS_LIMIT) {
107                         // up-flank!
108                         double t = find_zerocrossing(pcm, i - 1) * (123156.0/44100.0);
109                         if (last_upflank > 0) {
110 //                              fprintf(stderr, "length: %f (0x%x)\n", t - last_upflank, lrintf(t - last_upflank));
111                                 int len = lrintf(t - last_upflank);
112                                 printf("0x%x\n", len);
113                         }
114                         last_upflank = t;
115                         last_max_level = 0;
116                 }
117                 last_max_level = std::max(last_max_level, abs(pcm[i]));
118                 last_bit = bit;
119         }
120 }