e8263eab4757ca30a903f96aa6c32cfbc3d1997e
[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 "level.h"
13 #include "tap.h"
14
15 #define BUFSIZE 4096
16 #define C64_FREQUENCY 985248
17 #define SYNC_PULSE_START 1000
18 #define SYNC_PULSE_END 20000
19 #define SYNC_PULSE_LENGTH 378.0
20 #define SYNC_TEST_TOLERANCE 1.10
21
22 // SPSA options
23 #define NUM_FILTER_COEFF 32
24 #define NUM_ITER 5000
25 #define A NUM_ITER/10  // approx
26 #define INITIAL_A 0.005 // A bit of trial and error...
27 #define INITIAL_C 0.02  // This too.
28 #define GAMMA 0.166
29 #define ALPHA 1.0
30
31 static float hysteresis_limit = 3000.0 / 32768.0;
32 static bool do_calibrate = true;
33 static bool output_cycles_plot = false;
34 static bool use_filter = false;
35 static bool do_crop = false;
36 static float crop_start = 0.0f, crop_end = HUGE_VAL;
37 static float filter_coeff[NUM_FILTER_COEFF] = { 1.0f };  // The rest is filled with 0.
38 static bool output_filtered = false;
39 static bool quiet = false;
40 static bool do_auto_level = false;
41 static bool output_leveled = false;
42 static std::vector<float> train_snap_points;
43 static bool do_train = false;
44
45 // between [x,x+1]
46 double find_zerocrossing(const std::vector<float> &pcm, int x)
47 {
48         if (pcm[x] == 0) {
49                 return x;
50         }
51         if (pcm[x + 1] == 0) {
52                 return x + 1;
53         }
54
55         assert(pcm[x + 1] < 0);
56         assert(pcm[x] > 0);
57
58         double upper = x;
59         double lower = x + 1;
60         while (lower - upper > 1e-3) {
61                 double mid = 0.5f * (upper + lower);
62                 if (lanczos_interpolate(pcm, mid) > 0) {
63                         upper = mid;
64                 } else {
65                         lower = mid;
66                 }
67         }
68
69         return 0.5f * (upper + lower);
70 }
71
72 struct pulse {
73         double time;  // in seconds from start
74         double len;   // in seconds
75 };
76
77 // Calibrate on the first ~25k pulses (skip a few, just to be sure).
78 double calibrate(const std::vector<pulse> &pulses) {
79         if (pulses.size() < SYNC_PULSE_END) {
80                 fprintf(stderr, "Too few pulses, not calibrating!\n");
81                 return 1.0;
82         }
83
84         int sync_pulse_end = -1;
85         double sync_pulse_stddev = -1.0;
86
87         // Compute the standard deviation (to check for uneven speeds).
88         // If it suddenly skyrockets, we assume that sync ended earlier
89         // than we thought (it should be 25000 cycles), and that we should
90         // calibrate on fewer cycles.
91         for (int try_end : { 2000, 4000, 5000, 7500, 10000, 15000, SYNC_PULSE_END }) {
92                 double sum2 = 0.0;
93                 for (int i = SYNC_PULSE_START; i < try_end; ++i) {
94                         double cycles = pulses[i].len * C64_FREQUENCY;
95                         sum2 += (cycles - SYNC_PULSE_LENGTH) * (cycles - SYNC_PULSE_LENGTH);
96                 }
97                 double stddev = sqrt(sum2 / (try_end - SYNC_PULSE_START - 1));
98                 if (sync_pulse_end != -1 && stddev > 5.0 && stddev / sync_pulse_stddev > 1.3) {
99                         fprintf(stderr, "Stopping at %d sync pulses because standard deviation would be too big (%.2f cycles); shorter-than-usual trailer?\n",
100                                 sync_pulse_end, stddev);
101                         break;
102                 }
103                 sync_pulse_end = try_end;
104                 sync_pulse_stddev = stddev;
105         }
106         if (!quiet) {
107                 fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
108                         sync_pulse_stddev);
109         }
110
111         double sum = 0.0;
112         for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
113                 sum += pulses[i].len;
114         }
115         double mean_length = C64_FREQUENCY * sum / (sync_pulse_end - SYNC_PULSE_START);
116         double calibration_factor = SYNC_PULSE_LENGTH / mean_length;
117         if (!quiet) {
118                 fprintf(stderr, "Calibrated sync pulse length: %.2f -> %.2f (change %+.2f%%)\n",
119                         mean_length, SYNC_PULSE_LENGTH, 100.0 * (calibration_factor - 1.0));
120         }
121
122         // Check for pulses outside +/- 10% (sign of misdetection).
123         for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
124                 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
125                 if (cycles < SYNC_PULSE_LENGTH / SYNC_TEST_TOLERANCE || cycles > SYNC_PULSE_LENGTH * SYNC_TEST_TOLERANCE) {
126                         fprintf(stderr, "Sync cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
127                                 pulses[i].time, cycles);
128                 }
129         }
130
131         return calibration_factor;
132 }
133
134 void output_tap(const std::vector<pulse>& pulses, double calibration_factor)
135 {
136         std::vector<char> tap_data;
137         for (unsigned i = 0; i < pulses.size(); ++i) {
138                 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
139                 int len = lrintf(cycles / TAP_RESOLUTION);
140                 if (i > SYNC_PULSE_END && (cycles < 100 || cycles > 800)) {
141                         fprintf(stderr, "Cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
142                                         pulses[i].time, cycles);
143                 }
144                 if (len <= 255) {
145                         tap_data.push_back(len);
146                 } else {
147                         int overflow_len = lrintf(cycles);
148                         tap_data.push_back(0);
149                         tap_data.push_back(overflow_len & 0xff);
150                         tap_data.push_back((overflow_len >> 8) & 0xff);
151                         tap_data.push_back(overflow_len >> 16);
152                 }
153         }
154
155         tap_header hdr;
156         memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
157         hdr.version = 1;
158         hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
159         hdr.data_len = tap_data.size();
160
161         fwrite(&hdr, sizeof(hdr), 1, stdout);
162         fwrite(tap_data.data(), tap_data.size(), 1, stdout);
163 }
164
165 static struct option long_options[] = {
166         {"auto-level",       0,                 0, 'a' },
167         {"output-leveled",   0,                 0, 'A' },
168         {"no-calibrate",     0,                 0, 's' },
169         {"plot-cycles",      0,                 0, 'p' },
170         {"hysteresis-limit", required_argument, 0, 'l' },
171         {"filter",           required_argument, 0, 'f' },
172         {"output-filtered",  0,                 0, 'F' },
173         {"crop",             required_argument, 0, 'c' },
174         {"quiet",            0,                 0, 'q' },
175         {"help",             0,                 0, 'h' },
176         {0,                  0,                 0, 0   }
177 };
178
179 void help()
180 {
181         fprintf(stderr, "decode [OPTIONS] AUDIO-FILE > TAP-FILE\n");
182         fprintf(stderr, "\n");
183         fprintf(stderr, "  -a, --auto-level             automatically adjust amplitude levels throughout the file\n");
184         fprintf(stderr, "  -A, --output-leveled         output leveled waveform to leveled.raw\n");
185         fprintf(stderr, "  -s, --no-calibrate           do not try to calibrate on sync pulse length\n");
186         fprintf(stderr, "  -p, --plot-cycles            output debugging info to cycles.plot\n");
187         fprintf(stderr, "  -l, --hysteresis-limit VAL   change amplitude threshold for ignoring pulses (0..32768)\n");
188         fprintf(stderr, "  -f, --filter C1:C2:C3:...    specify FIR filter (up to %d coefficients)\n", NUM_FILTER_COEFF);
189         fprintf(stderr, "  -F, --output-filtered        output filtered waveform to filtered.raw\n");
190         fprintf(stderr, "  -c, --crop START[:END]       use only the given part of the file\n");
191         fprintf(stderr, "  -t, --train LEN1:LEN2:...    train a filter for detecting any of the given number of cycles\n");
192         fprintf(stderr, "                               (implies --no-calibrate and --quiet unless overridden)\n");
193         fprintf(stderr, "  -q, --quiet                  suppress some informational messages\n");
194         fprintf(stderr, "  -h, --help                   display this help, then exit\n");
195         exit(1);
196 }
197
198 void parse_options(int argc, char **argv)
199 {
200         for ( ;; ) {
201                 int option_index = 0;
202                 int c = getopt_long(argc, argv, "aAspl:f:Fc:t:qh", long_options, &option_index);
203                 if (c == -1)
204                         break;
205
206                 switch (c) {
207                 case 'a':
208                         do_auto_level = true;
209                         break;
210
211                 case 'A':
212                         output_leveled = true;
213                         break;
214
215                 case 's':
216                         do_calibrate = false;
217                         break;
218
219                 case 'p':
220                         output_cycles_plot = true;
221                         break;
222
223                 case 'l':
224                         hysteresis_limit = atof(optarg) / 32768.0;
225                         break;
226
227                 case 'f': {
228                         const char *coeffstr = strtok(optarg, ": ");
229                         int coeff_index = 0;
230                         while (coeff_index < NUM_FILTER_COEFF && coeffstr != NULL) {
231                                 filter_coeff[coeff_index++] = atof(coeffstr);
232                                 coeffstr = strtok(NULL, ": ");
233                         }
234                         use_filter = true;
235                         break;
236                 }
237
238                 case 'F':
239                         output_filtered = true;
240                         break;
241
242                 case 'c': {
243                         const char *cropstr = strtok(optarg, ":");
244                         crop_start = atof(cropstr);
245                         cropstr = strtok(NULL, ":");
246                         if (cropstr == NULL) {
247                                 crop_end = HUGE_VAL;
248                         } else {
249                                 crop_end = atof(cropstr);
250                         }
251                         do_crop = true;
252                         break;
253                 }
254
255                 case 't': {
256                         const char *cyclestr = strtok(optarg, ":");
257                         while (cyclestr != NULL) {
258                                 train_snap_points.push_back(atof(cyclestr));
259                                 cyclestr = strtok(NULL, ":");
260                         }
261                         do_train = true;
262
263                         // Set reasonable defaults (can be overridden later on the command line).
264                         do_calibrate = false;
265                         quiet = true;
266                         break;
267                 }
268
269                 case 'q':
270                         quiet = true;
271                         break;
272
273                 case 'h':
274                 default:
275                         help();
276                         exit(1);
277                 }
278         }
279 }
280
281 std::vector<float> crop(const std::vector<float>& pcm, float crop_start, float crop_end, int sample_rate)
282 {
283         size_t start_sample, end_sample;
284         if (crop_start >= 0.0f) {
285                 start_sample = std::min<size_t>(lrintf(crop_start * sample_rate), pcm.size());
286         }
287         if (crop_end >= 0.0f) {
288                 end_sample = std::min<size_t>(lrintf(crop_end * sample_rate), pcm.size());
289         }
290         return std::vector<float>(pcm.begin() + start_sample, pcm.begin() + end_sample);
291 }
292
293 // TODO: Support AVX here.
294 std::vector<float> do_filter(const std::vector<float>& pcm, const float* filter)
295 {
296         std::vector<float> filtered_pcm;
297         filtered_pcm.reserve(pcm.size());
298         for (unsigned i = NUM_FILTER_COEFF; i < pcm.size(); ++i) {
299                 float s = 0.0f;
300                 for (int j = 0; j < NUM_FILTER_COEFF; ++j) {
301                         s += filter[j] * pcm[i - j];
302                 }
303                 filtered_pcm.push_back(s);
304         }
305
306         if (output_filtered) {
307                 FILE *fp = fopen("filtered.raw", "wb");
308                 fwrite(filtered_pcm.data(), filtered_pcm.size() * sizeof(filtered_pcm[0]), 1, fp);
309                 fclose(fp);
310         }
311
312         return filtered_pcm;
313 }
314
315 std::vector<pulse> detect_pulses(const std::vector<float> &pcm, int sample_rate)
316 {
317         std::vector<pulse> pulses;
318
319         // Find the flanks.
320         int last_bit = -1;
321         double last_downflank = -1;
322         for (unsigned i = 0; i < pcm.size(); ++i) {
323                 int bit = (pcm[i] > 0) ? 1 : 0;
324                 if (bit == 0 && last_bit == 1) {
325                         // Check if we ever go up above <hysteresis_limit> before we dip down again.
326                         bool true_pulse = false;
327                         unsigned j;
328                         int min_level_after = 32767;
329                         for (j = i; j < pcm.size(); ++j) {
330                                 min_level_after = std::min<int>(min_level_after, pcm[j]);
331                                 if (pcm[j] > 0) break;
332                                 if (pcm[j] < -hysteresis_limit) {
333                                         true_pulse = true;
334                                         break;
335                                 }
336                         }
337
338                         if (!true_pulse) {
339 #if 0
340                                 fprintf(stderr, "Ignored down-flank at %.6f seconds due to hysteresis (%d < %d).\n",
341                                         double(i) / sample_rate, -min_level_after, hysteresis_limit);
342 #endif
343                                 i = j;
344                                 continue;
345                         } 
346
347                         // down-flank!
348                         double t = find_zerocrossing(pcm, i - 1) * (1.0 / sample_rate) + crop_start;
349                         if (last_downflank > 0) {
350                                 pulse p;
351                                 p.time = t;
352                                 p.len = t - last_downflank;
353                                 pulses.push_back(p);
354                         }
355                         last_downflank = t;
356                 }
357                 last_bit = bit;
358         }
359         return pulses;
360 }
361
362 void output_cycle_plot(const std::vector<pulse> &pulses, double calibration_factor)
363 {
364         FILE *fp = fopen("cycles.plot", "w");
365         for (unsigned i = 0; i < pulses.size(); ++i) {
366                 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
367                 fprintf(fp, "%f %f\n", pulses[i].time, cycles);
368         }
369         fclose(fp);
370 }
371
372 float eval_badness(const std::vector<pulse>& pulses, double calibration_factor)
373 {
374         double sum_badness = 0.0;
375         for (unsigned i = 0; i < pulses.size(); ++i) {
376                 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
377                 if (cycles > 2000.0) cycles = 2000.0;  // Don't make pauses arbitrarily bad.
378                 double badness = (cycles - train_snap_points[0]) * (cycles - train_snap_points[0]);
379                 for (unsigned j = 1; j < train_snap_points.size(); ++j) {
380                         badness = std::min(badness, (cycles - train_snap_points[j]) * (cycles - train_snap_points[j]));
381                 }
382                 sum_badness += badness;
383         }
384         return sqrt(sum_badness / (pulses.size() - 1));
385 }
386
387 void spsa_train(std::vector<float> &pcm, int sample_rate)
388 {
389         // Train!
390         float filter[NUM_FILTER_COEFF] = { 1.0f };  // The rest is filled with 0.
391
392         float start_c = INITIAL_C;
393         double best_badness = HUGE_VAL;
394
395         for (int n = 1; n < NUM_ITER; ++n) {
396                 float a = INITIAL_A * pow(n + A, -ALPHA);
397                 float c = start_c * pow(n, -GAMMA);
398
399                 // find a random perturbation
400                 float p[NUM_FILTER_COEFF];
401                 float filter1[NUM_FILTER_COEFF], filter2[NUM_FILTER_COEFF];
402                 for (int i = 0; i < NUM_FILTER_COEFF; ++i) {
403                         p[i] = (rand() % 2) ? 1.0 : -1.0;
404                         filter1[i] = std::max(std::min(filter[i] - c * p[i], 1.0f), -1.0f);
405                         filter2[i] = std::max(std::min(filter[i] + c * p[i], 1.0f), -1.0f);
406                 }
407
408                 std::vector<pulse> pulses1 = detect_pulses(do_filter(pcm, filter1), sample_rate);
409                 std::vector<pulse> pulses2 = detect_pulses(do_filter(pcm, filter2), sample_rate);
410                 float badness1 = eval_badness(pulses1, 1.0);
411                 float badness2 = eval_badness(pulses2, 1.0);
412
413                 // Find the gradient estimator
414                 float g[NUM_FILTER_COEFF];
415                 for (int i = 0; i < NUM_FILTER_COEFF; ++i) {
416                         g[i] = (badness2 - badness1) / (2.0 * c * p[i]);
417                         filter[i] -= a * g[i];
418                         filter[i] = std::max(std::min(filter[i], 1.0f), -1.0f);
419                 }
420                 if (badness2 < badness1) {
421                         std::swap(badness1, badness2);
422                         std::swap(filter1, filter2);
423                         std::swap(pulses1, pulses2);
424                 }
425                 if (badness1 < best_badness) {
426                         printf("\nNew best filter (badness=%f):", badness1);
427                         for (int i = 0; i < NUM_FILTER_COEFF; ++i) {
428                                 printf(" %.5f", filter1[i]);
429                         }
430                         best_badness = badness1;
431                         printf("\n");
432
433                         if (output_cycles_plot) {
434                                 output_cycle_plot(pulses1, 1.0);
435                         }
436                 }
437                 printf("%d ", n);
438                 fflush(stdout);
439         }
440 }
441
442 int main(int argc, char **argv)
443 {
444         parse_options(argc, argv);
445
446         make_lanczos_weight_table();
447         std::vector<float> pcm;
448         int sample_rate;
449         if (!read_audio_file(argv[optind], &pcm, &sample_rate)) {
450                 exit(1);
451         }
452
453         if (do_crop) {
454                 pcm = crop(pcm, crop_start, crop_end, sample_rate);
455         }
456
457         if (use_filter) {
458                 pcm = do_filter(pcm, filter_coeff);
459         }
460
461         if (do_auto_level) {
462                 pcm = level_samples(pcm, sample_rate);
463                 if (output_leveled) {
464                         FILE *fp = fopen("leveled.raw", "wb");
465                         fwrite(pcm.data(), pcm.size() * sizeof(pcm[0]), 1, fp);
466                         fclose(fp);
467                 }
468         }
469
470 #if 0
471         for (int i = 0; i < LEN; ++i) {
472                 in[i] += rand() % 10000;
473         }
474 #endif
475
476 #if 0
477         for (int i = 0; i < LEN; ++i) {
478                 printf("%d\n", in[i]);
479         }
480 #endif
481
482         if (do_train) {
483                 spsa_train(pcm, sample_rate);
484                 exit(0);
485         }
486
487         std::vector<pulse> pulses = detect_pulses(pcm, sample_rate);
488
489         double calibration_factor = 1.0;
490         if (do_calibrate) {
491                 calibration_factor = calibrate(pulses);
492         }
493
494         if (output_cycles_plot) {
495                 output_cycle_plot(pulses, calibration_factor);
496         }
497
498         output_tap(pulses, calibration_factor);
499 }