+
+// Calibrate on the first ~25k pulses (skip a few, just to be sure).
+double calibrate(const std::vector<pulse> &pulses) {
+ if (pulses.size() < SYNC_PULSE_END) {
+ fprintf(stderr, "Too few pulses, not calibrating!\n");
+ return 1.0;
+ }
+
+ int sync_pulse_end = -1;
+ double sync_pulse_stddev = -1.0;
+
+ // Compute the standard deviation (to check for uneven speeds).
+ // If it suddenly skyrockets, we assume that sync ended earlier
+ // than we thought (it should be 25000 cycles), and that we should
+ // calibrate on fewer cycles.
+ for (int try_end : { 2000, 4000, 5000, 7500, 10000, 15000, SYNC_PULSE_END }) {
+ double sum2 = 0.0;
+ for (int i = SYNC_PULSE_START; i < try_end; ++i) {
+ double cycles = pulses[i].len * C64_FREQUENCY;
+ sum2 += (cycles - SYNC_PULSE_LENGTH) * (cycles - SYNC_PULSE_LENGTH);
+ }
+ double stddev = sqrt(sum2 / (try_end - SYNC_PULSE_START - 1));
+ if (sync_pulse_end != -1 && stddev > 5.0 && stddev / sync_pulse_stddev > 1.3) {
+ fprintf(stderr, "Stopping at %d sync pulses because standard deviation would be too big (%.2f cycles); shorter-than-usual trailer?\n",
+ sync_pulse_end, stddev);
+ break;
+ }
+ sync_pulse_end = try_end;
+ sync_pulse_stddev = stddev;
+ }
+ fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
+ sync_pulse_stddev);
+
+ double sum = 0.0;
+ for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
+ sum += pulses[i].len;
+ }
+ double mean_length = C64_FREQUENCY * sum / (sync_pulse_end - SYNC_PULSE_START);
+ double calibration_factor = SYNC_PULSE_LENGTH / mean_length;
+ fprintf(stderr, "Calibrated sync pulse length: %.2f -> %.2f (change %+.2f%%)\n",
+ mean_length, SYNC_PULSE_LENGTH, 100.0 * (calibration_factor - 1.0));
+
+ // Check for pulses outside +/- 10% (sign of misdetection).
+ for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
+ double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
+ if (cycles < SYNC_PULSE_LENGTH / SYNC_TEST_TOLERANCE || cycles > SYNC_PULSE_LENGTH * SYNC_TEST_TOLERANCE) {
+ fprintf(stderr, "Sync cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
+ pulses[i].time, cycles);
+ }
+ }
+
+ return calibration_factor;
+}
+
+void output_tap(const std::vector<pulse>& pulses, double calibration_factor)
+{
+ std::vector<char> tap_data;
+ for (unsigned i = 0; i < pulses.size(); ++i) {
+ double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
+ int len = lrintf(cycles / TAP_RESOLUTION);
+ if (i > SYNC_PULSE_END && (cycles < 100 || cycles > 800)) {
+ fprintf(stderr, "Cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
+ pulses[i].time, cycles);
+ }
+ if (len <= 255) {
+ tap_data.push_back(len);
+ } else {
+ int overflow_len = lrintf(cycles);
+ tap_data.push_back(0);
+ tap_data.push_back(overflow_len & 0xff);
+ tap_data.push_back((overflow_len >> 8) & 0xff);
+ tap_data.push_back(overflow_len >> 16);
+ }
+ }
+
+ tap_header hdr;
+ memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
+ hdr.version = 1;
+ hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
+ hdr.data_len = tap_data.size();
+
+ fwrite(&hdr, sizeof(hdr), 1, stdout);
+ fwrite(tap_data.data(), tap_data.size(), 1, stdout);
+}
+
+static struct option long_options[] = {
+ {"no-calibrate", 0, 0, 's' },
+ {"plot-cycles", 0, 0, 'p' },
+ {"hysteresis-limit", required_argument, 0, 'l' },
+ {"filter", required_argument, 0, 'f' },
+ {"output-filtered", 0, 0, 'F' },
+ {"help", 0, 0, 'h' },
+ {0, 0, 0, 0 }
+};
+
+void help()