]> git.sesse.net Git - c64tapwav/blob - decode.c
Add calibration support.
[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 #define SAMPLE_RATE 44100
12 #define C64_FREQUENCY 985248
13 #define TAP_RESOLUTION 8
14
15 double sinc(double x)
16 {
17         if (fabs(x) < 1e-6) {
18                 return 1.0f - fabs(x);
19         } else {
20                 return sin(x) / x;
21         }
22 }
23
24 #if 1
25 double weight(double x)
26 {
27         if (fabs(x) > LANCZOS_RADIUS) {
28                 return 0.0f;
29         }
30         return sinc(M_PI * x) * sinc(M_PI * x / LANCZOS_RADIUS);
31 }
32 #else
33 double weight(double x)
34 {
35         if (fabs(x) > 1.0f) {
36                 return 0.0f;
37         }
38         return 1.0f - fabs(x);
39 }
40 #endif
41
42 double interpolate(const std::vector<short> &pcm, double i)
43 {
44         int lower = std::max<int>(ceil(i - LANCZOS_RADIUS), 0);
45         int upper = std::min<int>(floor(i + LANCZOS_RADIUS), pcm.size() - 1);
46         double sum = 0.0f;
47
48         for (int x = lower; x <= upper; ++x) {
49                 sum += pcm[x] * weight(i - x);
50         }
51         return sum;
52 }
53         
54 // between [x,x+1]
55 double find_zerocrossing(const std::vector<short> &pcm, int x)
56 {
57         if (pcm[x] == 0) {
58                 return x;
59         }
60         if (pcm[x + 1] == 0) {
61                 return x + 1;
62         }
63
64         assert(pcm[x + 1] > 0);
65         assert(pcm[x] < 0);
66
67         double lower = x;
68         double upper = x + 1;
69         while (upper - lower > 1e-6) {
70                 double mid = 0.5f * (upper + lower);
71                 if (interpolate(pcm, mid) > 0) {
72                         upper = mid;
73                 } else {
74                         lower = mid;
75                 }
76         }
77
78         return 0.5f * (upper + lower);
79 }
80
81 int main(int argc, char **argv)
82 {
83         std::vector<short> pcm;
84
85         while (!feof(stdin)) {
86                 short buf[BUFSIZE];
87                 ssize_t ret = fread(buf, 2, BUFSIZE, stdin);
88                 if (ret >= 0) {
89                         pcm.insert(pcm.end(), buf, buf + ret);
90                 }
91         }       
92
93 #if 0
94         for (int i = 0; i < LEN; ++i) {
95                 in[i] += rand() % 10000;
96         }
97 #endif
98
99 #if 0
100         for (int i = 0; i < LEN; ++i) {
101                 printf("%d\n", in[i]);
102         }
103 #endif
104
105         std::vector<float> pulse_lengths;  // in seconds
106
107         // Find the flanks.
108         int last_bit = -1;
109         double last_upflank = -1;
110         int last_max_level = 0;
111         for (int i = 0; i < pcm.size(); ++i) {
112                 int bit = (pcm[i] > 0) ? 1 : 0;
113                 if (bit == 1 && last_bit == 0 && last_max_level > HYSTERESIS_LIMIT) {
114                         // up-flank!
115                         double t = find_zerocrossing(pcm, i - 1) * (1.0 / SAMPLE_RATE);
116                         if (last_upflank > 0) {
117                                 pulse_lengths.push_back(t - last_upflank);
118                         }
119                         last_upflank = t;
120                         last_max_level = 0;
121                 }
122                 last_max_level = std::max(last_max_level, abs(pcm[i]));
123                 last_bit = bit;
124         }
125
126         // Calibrate on the first ~15k pulses (skip a few, just to be sure).
127         float calibration_factor = 1.0f;
128         if (pulse_lengths.size() < 20000) {
129                 fprintf(stderr, "Too few pulses, not calibrating!\n");
130         } else {
131                 double sum = 0.0;
132                 for (int i = 1000; i < 16000; ++i) {
133                         sum += pulse_lengths[i];
134                 }
135                 double mean_length = C64_FREQUENCY * sum / 15000.0f;
136                 calibration_factor = 380.0 / mean_length;
137                 fprintf(stderr, "Cycle length: %.2f -> 380.0 (change %+.2f%%)\n",
138                         mean_length, 100.0 * (calibration_factor - 1.0));
139         }
140
141         for (int i = 0; i < pulse_lengths.size(); ++i) {
142                 int len = lrintf(pulse_lengths[i] * calibration_factor * C64_FREQUENCY / TAP_RESOLUTION);
143                 //fprintf(stderr, "length: %f (0x%x)\n", t - last_upflank, len);
144                 printf("0x%x\n", len);
145         }
146 }