]> git.sesse.net Git - c64tapwav/blob - decode.cpp
Add support for decoding only parts of the sample.
[c64tapwav] / decode.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <limits.h>
6 #include <getopt.h>
7 #include <vector>
8 #include <algorithm>
9
10 #include "audioreader.h"
11 #include "interpolate.h"
12 #include "tap.h"
13
14 #define BUFSIZE 4096
15 #define C64_FREQUENCY 985248
16 #define SYNC_PULSE_START 1000
17 #define SYNC_PULSE_END 20000
18 #define SYNC_PULSE_LENGTH 378.0
19 #define SYNC_TEST_TOLERANCE 1.10
20
21 #define NUM_FILTER_COEFF 32
22
23 static float hysteresis_limit = 3000.0 / 32768.0;
24 static bool do_calibrate = true;
25 static bool output_cycles_plot = false;
26 static bool use_filter = false;
27 static bool do_crop = false;
28 static float crop_start = 0.0f, crop_end = HUGE_VAL;
29 static float filter_coeff[NUM_FILTER_COEFF] = { 1.0f };  // The rest is filled with 0.
30 static bool output_filtered = false;
31 static bool quiet = false;
32
33 // between [x,x+1]
34 double find_zerocrossing(const std::vector<float> &pcm, int x)
35 {
36         if (pcm[x] == 0) {
37                 return x;
38         }
39         if (pcm[x + 1] == 0) {
40                 return x + 1;
41         }
42
43         assert(pcm[x + 1] < 0);
44         assert(pcm[x] > 0);
45
46         double upper = x;
47         double lower = x + 1;
48         while (lower - upper > 1e-3) {
49                 double mid = 0.5f * (upper + lower);
50                 if (lanczos_interpolate(pcm, mid) > 0) {
51                         upper = mid;
52                 } else {
53                         lower = mid;
54                 }
55         }
56
57         return 0.5f * (upper + lower);
58 }
59
60 struct pulse {
61         double time;  // in seconds from start
62         double len;   // in seconds
63 };
64
65 // Calibrate on the first ~25k pulses (skip a few, just to be sure).
66 double calibrate(const std::vector<pulse> &pulses) {
67         if (pulses.size() < SYNC_PULSE_END) {
68                 fprintf(stderr, "Too few pulses, not calibrating!\n");
69                 return 1.0;
70         }
71
72         int sync_pulse_end = -1;
73         double sync_pulse_stddev = -1.0;
74
75         // Compute the standard deviation (to check for uneven speeds).
76         // If it suddenly skyrockets, we assume that sync ended earlier
77         // than we thought (it should be 25000 cycles), and that we should
78         // calibrate on fewer cycles.
79         for (int try_end : { 2000, 4000, 5000, 7500, 10000, 15000, SYNC_PULSE_END }) {
80                 double sum2 = 0.0;
81                 for (int i = SYNC_PULSE_START; i < try_end; ++i) {
82                         double cycles = pulses[i].len * C64_FREQUENCY;
83                         sum2 += (cycles - SYNC_PULSE_LENGTH) * (cycles - SYNC_PULSE_LENGTH);
84                 }
85                 double stddev = sqrt(sum2 / (try_end - SYNC_PULSE_START - 1));
86                 if (sync_pulse_end != -1 && stddev > 5.0 && stddev / sync_pulse_stddev > 1.3) {
87                         fprintf(stderr, "Stopping at %d sync pulses because standard deviation would be too big (%.2f cycles); shorter-than-usual trailer?\n",
88                                 sync_pulse_end, stddev);
89                         break;
90                 }
91                 sync_pulse_end = try_end;
92                 sync_pulse_stddev = stddev;
93         }
94         if (!quiet) {
95                 fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
96                         sync_pulse_stddev);
97         }
98
99         double sum = 0.0;
100         for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
101                 sum += pulses[i].len;
102         }
103         double mean_length = C64_FREQUENCY * sum / (sync_pulse_end - SYNC_PULSE_START);
104         double calibration_factor = SYNC_PULSE_LENGTH / mean_length;
105         if (!quiet) {
106                 fprintf(stderr, "Calibrated sync pulse length: %.2f -> %.2f (change %+.2f%%)\n",
107                         mean_length, SYNC_PULSE_LENGTH, 100.0 * (calibration_factor - 1.0));
108         }
109
110         // Check for pulses outside +/- 10% (sign of misdetection).
111         for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
112                 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
113                 if (cycles < SYNC_PULSE_LENGTH / SYNC_TEST_TOLERANCE || cycles > SYNC_PULSE_LENGTH * SYNC_TEST_TOLERANCE) {
114                         fprintf(stderr, "Sync cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
115                                 pulses[i].time, cycles);
116                 }
117         }
118
119         return calibration_factor;
120 }
121
122 void output_tap(const std::vector<pulse>& pulses, double calibration_factor)
123 {
124         std::vector<char> tap_data;
125         for (unsigned i = 0; i < pulses.size(); ++i) {
126                 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
127                 int len = lrintf(cycles / TAP_RESOLUTION);
128                 if (i > SYNC_PULSE_END && (cycles < 100 || cycles > 800)) {
129                         fprintf(stderr, "Cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
130                                         pulses[i].time, cycles);
131                 }
132                 if (len <= 255) {
133                         tap_data.push_back(len);
134                 } else {
135                         int overflow_len = lrintf(cycles);
136                         tap_data.push_back(0);
137                         tap_data.push_back(overflow_len & 0xff);
138                         tap_data.push_back((overflow_len >> 8) & 0xff);
139                         tap_data.push_back(overflow_len >> 16);
140                 }
141         }
142
143         tap_header hdr;
144         memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
145         hdr.version = 1;
146         hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
147         hdr.data_len = tap_data.size();
148
149         fwrite(&hdr, sizeof(hdr), 1, stdout);
150         fwrite(tap_data.data(), tap_data.size(), 1, stdout);
151 }
152
153 static struct option long_options[] = {
154         {"no-calibrate",     0,                 0, 's' },
155         {"plot-cycles",      0,                 0, 'p' },
156         {"hysteresis-limit", required_argument, 0, 'l' },
157         {"filter",           required_argument, 0, 'f' },
158         {"output-filtered",  0,                 0, 'F' },
159         {"crop",             required_argument, 0, 'c' },
160         {"quiet",            0,                 0, 'q' },
161         {"help",             0,                 0, 'h' },
162         {0,                  0,                 0, 0   }
163 };
164
165 void help()
166 {
167         fprintf(stderr, "decode [OPTIONS] AUDIO-FILE > TAP-FILE\n");
168         fprintf(stderr, "\n");
169         fprintf(stderr, "  -s, --no-calibrate           do not try to calibrate on sync pulse length\n");
170         fprintf(stderr, "  -p, --plot-cycles            output debugging info to cycles.plot\n");
171         fprintf(stderr, "  -l, --hysteresis-limit VAL   change amplitude threshold for ignoring pulses (0..32768)\n");
172         fprintf(stderr, "  -f, --filter C1:C2:C3:...    specify FIR filter (up to %d coefficients)\n", NUM_FILTER_COEFF);
173         fprintf(stderr, "  -F, --output-filtered        output filtered waveform to filtered.raw\n");
174         fprintf(stderr, "  -c, --crop START[:END]       use only the given part of the file\n");
175         fprintf(stderr, "  -q, --quiet                  suppress some informational messages\n");
176         fprintf(stderr, "  -h, --help                   display this help, then exit\n");
177         exit(1);
178 }
179
180 void parse_options(int argc, char **argv)
181 {
182         for ( ;; ) {
183                 int option_index = 0;
184                 int c = getopt_long(argc, argv, "spl:f:Fc:qh", long_options, &option_index);
185                 if (c == -1)
186                         break;
187
188                 switch (c) {
189                 case 's':
190                         do_calibrate = false;
191                         break;
192
193                 case 'p':
194                         output_cycles_plot = true;
195                         break;
196
197                 case 'l':
198                         hysteresis_limit = atof(optarg) / 32768.0;
199                         break;
200
201                 case 'f': {
202                         const char *coeffstr = strtok(optarg, ":");
203                         int coeff_index = 0;
204                         while (coeff_index < NUM_FILTER_COEFF && coeffstr != NULL) {
205                                 filter_coeff[coeff_index++] = atof(coeffstr);
206                                 coeffstr = strtok(NULL, ":");
207                         }
208                         use_filter = true;
209                         break;
210                 }
211
212                 case 'F':
213                         output_filtered = true;
214                         break;
215
216                 case 'c': {
217                         const char *cropstr = strtok(optarg, ":");
218                         crop_start = atof(cropstr);
219                         cropstr = strtok(NULL, ":");
220                         if (cropstr == NULL) {
221                                 crop_end = HUGE_VAL;
222                         } else {
223                                 crop_end = atof(cropstr);
224                         }
225                         do_crop = true;
226                         break;
227                 }
228
229                 case 'q':
230                         quiet = true;
231                         break;
232
233                 case 'h':
234                 default:
235                         help();
236                         exit(1);
237                 }
238         }
239 }
240
241 std::vector<float> crop(const std::vector<float>& pcm, float crop_start, float crop_end, int sample_rate)
242 {
243         size_t start_sample, end_sample;
244         if (crop_start >= 0.0f) {
245                 start_sample = std::min<size_t>(lrintf(crop_start * sample_rate), pcm.size());
246         }
247         if (crop_end >= 0.0f) {
248                 end_sample = std::min<size_t>(lrintf(crop_end * sample_rate), pcm.size());
249         }
250         return std::vector<float>(pcm.begin() + start_sample, pcm.begin() + end_sample);
251 }
252
253 // TODO: Support AVX here.
254 std::vector<float> do_filter(const std::vector<float>& pcm, const float* filter)
255 {
256         std::vector<float> filtered_pcm;
257         filtered_pcm.reserve(pcm.size());
258         for (unsigned i = NUM_FILTER_COEFF; i < pcm.size(); ++i) {
259                 float s = 0.0f;
260                 for (int j = 0; j < NUM_FILTER_COEFF; ++j) {
261                         s += filter[j] * pcm[i - j];
262                 }
263                 filtered_pcm.push_back(s);
264         }
265
266         if (output_filtered) {
267                 FILE *fp = fopen("filtered.raw", "wb");
268                 fwrite(filtered_pcm.data(), filtered_pcm.size() * sizeof(filtered_pcm[0]), 1, fp);
269                 fclose(fp);
270         }
271
272         return filtered_pcm;
273 }
274
275 std::vector<pulse> detect_pulses(const std::vector<float> &pcm, int sample_rate)
276 {
277         std::vector<pulse> pulses;
278
279         // Find the flanks.
280         int last_bit = -1;
281         double last_downflank = -1;
282         for (unsigned i = 0; i < pcm.size(); ++i) {
283                 int bit = (pcm[i] > 0) ? 1 : 0;
284                 if (bit == 0 && last_bit == 1) {
285                         // Check if we ever go up above <hysteresis_limit> before we dip down again.
286                         bool true_pulse = false;
287                         unsigned j;
288                         int min_level_after = 32767;
289                         for (j = i; j < pcm.size(); ++j) {
290                                 min_level_after = std::min<int>(min_level_after, pcm[j]);
291                                 if (pcm[j] > 0) break;
292                                 if (pcm[j] < -hysteresis_limit) {
293                                         true_pulse = true;
294                                         break;
295                                 }
296                         }
297
298                         if (!true_pulse) {
299 #if 0
300                                 fprintf(stderr, "Ignored down-flank at %.6f seconds due to hysteresis (%d < %d).\n",
301                                         double(i) / sample_rate, -min_level_after, hysteresis_limit);
302 #endif
303                                 i = j;
304                                 continue;
305                         } 
306
307                         // down-flank!
308                         double t = find_zerocrossing(pcm, i - 1) * (1.0 / sample_rate) + crop_start;
309                         if (last_downflank > 0) {
310                                 pulse p;
311                                 p.time = t;
312                                 p.len = t - last_downflank;
313                                 pulses.push_back(p);
314                         }
315                         last_downflank = t;
316                 }
317                 last_bit = bit;
318         }
319         return pulses;
320 }
321
322 int main(int argc, char **argv)
323 {
324         parse_options(argc, argv);
325
326         make_lanczos_weight_table();
327         std::vector<float> pcm;
328         int sample_rate;
329         if (!read_audio_file(argv[optind], &pcm, &sample_rate)) {
330                 exit(1);
331         }
332
333         if (do_crop) {
334                 pcm = crop(pcm, crop_start, crop_end, sample_rate);
335         }
336
337         if (use_filter) {
338                 pcm = do_filter(pcm, filter_coeff);
339         }
340
341 #if 0
342         for (int i = 0; i < LEN; ++i) {
343                 in[i] += rand() % 10000;
344         }
345 #endif
346
347 #if 0
348         for (int i = 0; i < LEN; ++i) {
349                 printf("%d\n", in[i]);
350         }
351 #endif
352
353         std::vector<pulse> pulses = detect_pulses(pcm, sample_rate);
354
355         double calibration_factor = 1.0;
356         if (do_calibrate) {
357                 calibration_factor = calibrate(pulses);
358         }
359
360         if (output_cycles_plot) {
361                 FILE *fp = fopen("cycles.plot", "w");
362                 for (unsigned i = 0; i < pulses.size(); ++i) {
363                         double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
364                         fprintf(fp, "%f %f\n", pulses[i].time, cycles);
365                 }
366                 fclose(fp);
367         }
368
369         output_tap(pulses, calibration_factor);
370 }