9 #include "interpolate.h"
12 #define HYSTERESIS_LIMIT 3000
13 #define SAMPLE_RATE 44100
14 #define C64_FREQUENCY 985248
15 #define TAP_RESOLUTION 8
17 #define SYNC_PULSE_START 1000
18 #define SYNC_PULSE_END 15000
19 #define SYNC_PULSE_LENGTH 378.0
20 #define SYNC_TEST_TOLERANCE 1.10
26 unsigned int data_len;
30 double find_zerocrossing(const std::vector<short> &pcm, int x)
35 if (pcm[x + 1] == 0) {
39 assert(pcm[x + 1] < 0);
44 while (lower - upper > 1e-3) {
45 double mid = 0.5f * (upper + lower);
46 if (lanczos_interpolate(pcm, mid) > 0) {
53 return 0.5f * (upper + lower);
57 double time; // in seconds from start
58 double len; // in seconds
61 int main(int argc, char **argv)
63 make_lanczos_weight_table();
64 std::vector<short> pcm;
66 while (!feof(stdin)) {
68 ssize_t ret = fread(buf, 2, BUFSIZE, stdin);
70 pcm.insert(pcm.end(), buf, buf + ret);
75 for (int i = 0; i < LEN; ++i) {
76 in[i] += rand() % 10000;
81 for (int i = 0; i < LEN; ++i) {
82 printf("%d\n", in[i]);
86 std::vector<pulse> pulses; // in seconds
90 double last_downflank = -1;
91 for (unsigned i = 0; i < pcm.size(); ++i) {
92 int bit = (pcm[i] > 0) ? 1 : 0;
93 if (bit == 0 && last_bit == 1) {
94 // Check if we ever go up above HYSTERESIS_LIMIT before we dip down again.
95 bool true_pulse = false;
97 int min_level_after = 32767;
98 for (j = i; j < pcm.size(); ++j) {
99 min_level_after = std::min<int>(min_level_after, pcm[j]);
100 if (pcm[j] > 0) break;
101 if (pcm[j] < -HYSTERESIS_LIMIT) {
109 fprintf(stderr, "Ignored down-flank at %.6f seconds due to hysteresis (%d < %d).\n",
110 double(i) / SAMPLE_RATE, -min_level_after, HYSTERESIS_LIMIT);
117 double t = find_zerocrossing(pcm, i - 1) * (1.0 / SAMPLE_RATE);
118 if (last_downflank > 0) {
121 p.len = t - last_downflank;
129 // Calibrate on the first ~25k pulses (skip a few, just to be sure).
130 double calibration_factor = 1.0f;
131 if (pulses.size() < SYNC_PULSE_END) {
132 fprintf(stderr, "Too few pulses, not calibrating!\n");
135 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
136 sum += pulses[i].len;
138 double mean_length = C64_FREQUENCY * sum / (SYNC_PULSE_END - SYNC_PULSE_START);
139 calibration_factor = SYNC_PULSE_LENGTH / mean_length;
140 fprintf(stderr, "Calibrated sync pulse length: %.2f -> %.2f (change %+.2f%%)\n",
141 mean_length, SYNC_PULSE_LENGTH, 100.0 * (calibration_factor - 1.0));
143 // Check for pulses outside +/- 10% (sign of misdetection).
144 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
145 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
146 if (cycles < SYNC_PULSE_LENGTH / SYNC_TEST_TOLERANCE || cycles > SYNC_PULSE_LENGTH * SYNC_TEST_TOLERANCE) {
147 fprintf(stderr, "Sync cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
148 pulses[i].time, cycles);
152 // Compute the standard deviation (to check for uneven speeds).
154 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
155 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
156 sum2 += (cycles - SYNC_PULSE_LENGTH) * (cycles - SYNC_PULSE_LENGTH);
158 double stddev = sqrt(sum2 / (SYNC_PULSE_END - SYNC_PULSE_START - 1));
159 fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
163 FILE *fp = fopen("cycles.plot", "w");
164 std::vector<char> tap_data;
165 for (unsigned i = 0; i < pulses.size(); ++i) {
166 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
167 fprintf(fp, "%f %f\n", pulses[i].time, cycles);
168 int len = lrintf(cycles / TAP_RESOLUTION);
169 if (i > SYNC_PULSE_END && (cycles < 100 || cycles > 800)) {
170 fprintf(stderr, "Cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
171 pulses[i].time, cycles);
174 tap_data.push_back(len);
176 int overflow_len = lrintf(cycles);
177 tap_data.push_back(0);
178 tap_data.push_back(overflow_len & 0xff);
179 tap_data.push_back((overflow_len >> 8) & 0xff);
180 tap_data.push_back(overflow_len >> 16);
186 memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
188 hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
189 hdr.data_len = tap_data.size();
191 fwrite(&hdr, sizeof(hdr), 1, stdout);
192 fwrite(tap_data.data(), tap_data.size(), 1, stdout);
194 // Output a debug raw file with pulse detection points.
195 fp = fopen("debug.raw", "wb");
198 unsigned pulsenum = 0;
199 for (unsigned i = 0; i < pcm.size(); ++i) {
200 unsigned next_pulse = (pulsenum >= pulses.size()) ? INT_MAX : int(pulses[pulsenum].time * SAMPLE_RATE);
201 if (i >= next_pulse) {
202 fwrite(&one, sizeof(one), 1, fp);
205 fwrite(&zero, sizeof(zero), 1, fp);