10 #include "audioreader.h"
11 #include "interpolate.h"
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
23 #define NUM_FILTER_COEFF 32
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.
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;
46 double find_zerocrossing(const std::vector<float> &pcm, int x)
51 if (pcm[x + 1] == 0) {
55 assert(pcm[x + 1] < 0);
60 while (lower - upper > 1e-3) {
61 double mid = 0.5f * (upper + lower);
62 if (lanczos_interpolate(pcm, mid) > 0) {
69 return 0.5f * (upper + lower);
73 double time; // in seconds from start
74 double len; // in seconds
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");
84 int sync_pulse_end = -1;
85 double sync_pulse_stddev = -1.0;
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 }) {
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);
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);
103 sync_pulse_end = try_end;
104 sync_pulse_stddev = stddev;
107 fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
112 for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
113 sum += pulses[i].len;
115 double mean_length = C64_FREQUENCY * sum / (sync_pulse_end - SYNC_PULSE_START);
116 double calibration_factor = SYNC_PULSE_LENGTH / mean_length;
118 fprintf(stderr, "Calibrated sync pulse length: %.2f -> %.2f (change %+.2f%%)\n",
119 mean_length, SYNC_PULSE_LENGTH, 100.0 * (calibration_factor - 1.0));
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);
131 return calibration_factor;
134 void output_tap(const std::vector<pulse>& pulses, double calibration_factor)
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);
145 tap_data.push_back(len);
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);
156 memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
158 hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
159 hdr.data_len = tap_data.size();
161 fwrite(&hdr, sizeof(hdr), 1, stdout);
162 fwrite(tap_data.data(), tap_data.size(), 1, stdout);
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' },
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");
198 void parse_options(int argc, char **argv)
201 int option_index = 0;
202 int c = getopt_long(argc, argv, "aAspl:f:Fc:t:qh", long_options, &option_index);
208 do_auto_level = true;
212 output_leveled = true;
216 do_calibrate = false;
220 output_cycles_plot = true;
224 hysteresis_limit = atof(optarg) / 32768.0;
228 const char *coeffstr = strtok(optarg, ": ");
230 while (coeff_index < NUM_FILTER_COEFF && coeffstr != NULL) {
231 filter_coeff[coeff_index++] = atof(coeffstr);
232 coeffstr = strtok(NULL, ": ");
239 output_filtered = true;
243 const char *cropstr = strtok(optarg, ":");
244 crop_start = atof(cropstr);
245 cropstr = strtok(NULL, ":");
246 if (cropstr == NULL) {
249 crop_end = atof(cropstr);
256 const char *cyclestr = strtok(optarg, ":");
257 while (cyclestr != NULL) {
258 train_snap_points.push_back(atof(cyclestr));
259 cyclestr = strtok(NULL, ":");
263 // Set reasonable defaults (can be overridden later on the command line).
264 do_calibrate = false;
281 std::vector<float> crop(const std::vector<float>& pcm, float crop_start, float crop_end, int sample_rate)
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());
287 if (crop_end >= 0.0f) {
288 end_sample = std::min<size_t>(lrintf(crop_end * sample_rate), pcm.size());
290 return std::vector<float>(pcm.begin() + start_sample, pcm.begin() + end_sample);
293 // TODO: Support AVX here.
294 std::vector<float> do_filter(const std::vector<float>& pcm, const float* filter)
296 std::vector<float> filtered_pcm;
297 filtered_pcm.reserve(pcm.size());
298 for (unsigned i = NUM_FILTER_COEFF; i < pcm.size(); ++i) {
300 for (int j = 0; j < NUM_FILTER_COEFF; ++j) {
301 s += filter[j] * pcm[i - j];
303 filtered_pcm.push_back(s);
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);
315 std::vector<pulse> detect_pulses(const std::vector<float> &pcm, int sample_rate)
317 std::vector<pulse> pulses;
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;
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) {
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);
348 double t = find_zerocrossing(pcm, i - 1) * (1.0 / sample_rate) + crop_start;
349 if (last_downflank > 0) {
352 p.len = t - last_downflank;
362 void output_cycle_plot(const std::vector<pulse> &pulses, double calibration_factor)
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);
372 float eval_badness(const std::vector<pulse>& pulses, double calibration_factor)
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]));
382 sum_badness += badness;
384 return sqrt(sum_badness / (pulses.size() - 1));
387 void spsa_train(std::vector<float> &pcm, int sample_rate)
390 float filter[NUM_FILTER_COEFF] = { 1.0f }; // The rest is filled with 0.
392 float start_c = INITIAL_C;
393 double best_badness = HUGE_VAL;
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);
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);
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);
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);
420 if (badness2 < badness1) {
421 std::swap(badness1, badness2);
422 std::swap(filter1, filter2);
423 std::swap(pulses1, pulses2);
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]);
430 best_badness = badness1;
433 if (output_cycles_plot) {
434 output_cycle_plot(pulses1, 1.0);
442 int main(int argc, char **argv)
444 parse_options(argc, argv);
446 make_lanczos_weight_table();
447 std::vector<float> pcm;
449 if (!read_audio_file(argv[optind], &pcm, &sample_rate)) {
454 pcm = crop(pcm, crop_start, crop_end, sample_rate);
458 pcm = do_filter(pcm, filter_coeff);
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);
471 for (int i = 0; i < LEN; ++i) {
472 in[i] += rand() % 10000;
477 for (int i = 0; i < LEN; ++i) {
478 printf("%d\n", in[i]);
483 spsa_train(pcm, sample_rate);
487 std::vector<pulse> pulses = detect_pulses(pcm, sample_rate);
489 double calibration_factor = 1.0;
491 calibration_factor = calibrate(pulses);
494 if (output_cycles_plot) {
495 output_cycle_plot(pulses, calibration_factor);
498 output_tap(pulses, calibration_factor);