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 (upper - lower > 1e-6) {
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 std::vector<short> pcm;
65 while (!feof(stdin)) {
67 ssize_t ret = fread(buf, 2, BUFSIZE, stdin);
69 pcm.insert(pcm.end(), buf, buf + ret);
74 for (int i = 0; i < LEN; ++i) {
75 in[i] += rand() % 10000;
80 for (int i = 0; i < LEN; ++i) {
81 printf("%d\n", in[i]);
85 std::vector<pulse> pulses; // in seconds
89 double last_upflank = -1;
90 for (unsigned i = 0; i < pcm.size(); ++i) {
91 int bit = (pcm[i] > 0) ? 1 : 0;
92 if (bit == 1 && last_bit == 0) {
93 // Check if we ever go up above HYSTERESIS_LIMIT before we dip down again.
94 bool true_pulse = false;
96 int max_level_after = -32768;
97 for (j = i; j < pcm.size(); ++j) {
98 max_level_after = std::max<int>(max_level_after, pcm[j]);
99 if (pcm[j] < 0) break;
100 if (pcm[j] > HYSTERESIS_LIMIT) {
108 fprintf(stderr, "Ignored up-flank at %.6f seconds due to hysteresis (%d < %d).\n",
109 double(i) / SAMPLE_RATE, max_level_after, HYSTERESIS_LIMIT);
116 double t = find_zerocrossing(pcm, i - 1) * (1.0 / SAMPLE_RATE);
117 if (last_upflank > 0) {
120 p.len = t - last_upflank;
128 // Calibrate on the first ~25k pulses (skip a few, just to be sure).
129 double calibration_factor = 1.0f;
130 if (pulses.size() < SYNC_PULSE_END) {
131 fprintf(stderr, "Too few pulses, not calibrating!\n");
134 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
135 sum += pulses[i].len;
137 double mean_length = C64_FREQUENCY * sum / (SYNC_PULSE_END - SYNC_PULSE_START);
138 calibration_factor = SYNC_PULSE_LENGTH / mean_length;
139 fprintf(stderr, "Calibrated sync pulse length: %.2f -> %.2f (change %+.2f%%)\n",
140 mean_length, SYNC_PULSE_LENGTH, 100.0 * (calibration_factor - 1.0));
142 // Check for pulses outside +/- 10% (sign of misdetection).
143 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
144 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
145 if (cycles < SYNC_PULSE_LENGTH / SYNC_TEST_TOLERANCE || cycles > SYNC_PULSE_LENGTH * SYNC_TEST_TOLERANCE) {
146 fprintf(stderr, "Sync cycle with upflank at %.6f was detected at %.0f cycles; misdetect?\n",
147 pulses[i].time, cycles);
151 // Compute the standard deviation (to check for uneven speeds).
153 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
154 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
155 sum2 += (cycles - SYNC_PULSE_LENGTH) * (cycles - SYNC_PULSE_LENGTH);
157 double stddev = sqrt(sum2 / (SYNC_PULSE_END - SYNC_PULSE_START - 1));
158 fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
162 FILE *fp = fopen("cycles.plot", "w");
163 std::vector<char> tap_data;
164 for (unsigned i = 0; i < pulses.size(); ++i) {
165 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
166 fprintf(fp, "%f %f\n", pulses[i].time, cycles);
167 int len = lrintf(cycles / TAP_RESOLUTION);
168 if (i > SYNC_PULSE_END && (cycles < 100 || cycles > 800)) {
169 fprintf(stderr, "Cycle with upflank at %.6f was detected at %.0f cycles; misdetect?\n",
170 pulses[i].time, cycles);
173 tap_data.push_back(len);
175 int overflow_len = lrintf(cycles);
176 tap_data.push_back(0);
177 tap_data.push_back(overflow_len & 0xff);
178 tap_data.push_back((overflow_len >> 8) & 0xff);
179 tap_data.push_back(overflow_len >> 16);
185 memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
187 hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
188 hdr.data_len = tap_data.size();
190 fwrite(&hdr, sizeof(hdr), 1, stdout);
191 fwrite(tap_data.data(), tap_data.size(), 1, stdout);