9 #include "audioreader.h"
10 #include "interpolate.h"
14 #define HYSTERESIS_LIMIT 3000
15 #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 double find_zerocrossing(const std::vector<short> &pcm, int x)
28 if (pcm[x + 1] == 0) {
32 assert(pcm[x + 1] < 0);
37 while (lower - upper > 1e-3) {
38 double mid = 0.5f * (upper + lower);
39 if (lanczos_interpolate(pcm, mid) > 0) {
46 return 0.5f * (upper + lower);
50 double time; // in seconds from start
51 double len; // in seconds
54 // Calibrate on the first ~25k pulses (skip a few, just to be sure).
55 double calibrate(const std::vector<pulse> &pulses) {
56 if (pulses.size() < SYNC_PULSE_END) {
57 fprintf(stderr, "Too few pulses, not calibrating!\n");
61 int sync_pulse_end = -1;
62 double sync_pulse_stddev = -1.0;
64 // Compute the standard deviation (to check for uneven speeds).
65 // If it suddenly skyrockets, we assume that sync ended earlier
66 // than we thought (it should be 25000 cycles), and that we should
67 // calibrate on fewer cycles.
68 for (int try_end : { 2000, 4000, 5000, 7500, 10000, 15000, SYNC_PULSE_END }) {
70 for (int i = SYNC_PULSE_START; i < try_end; ++i) {
71 double cycles = pulses[i].len * C64_FREQUENCY;
72 sum2 += (cycles - SYNC_PULSE_LENGTH) * (cycles - SYNC_PULSE_LENGTH);
74 double stddev = sqrt(sum2 / (try_end - SYNC_PULSE_START - 1));
75 if (sync_pulse_end != -1 && stddev > 5.0 && stddev / sync_pulse_stddev > 1.3) {
76 fprintf(stderr, "Stopping at %d sync pulses because standard deviation would be too big (%.2f cycles); shorter-than-usual trailer?\n",
77 sync_pulse_end, stddev);
80 sync_pulse_end = try_end;
81 sync_pulse_stddev = stddev;
83 fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
87 for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
90 double mean_length = C64_FREQUENCY * sum / (sync_pulse_end - SYNC_PULSE_START);
91 double calibration_factor = SYNC_PULSE_LENGTH / mean_length;
92 fprintf(stderr, "Calibrated sync pulse length: %.2f -> %.2f (change %+.2f%%)\n",
93 mean_length, SYNC_PULSE_LENGTH, 100.0 * (calibration_factor - 1.0));
95 // Check for pulses outside +/- 10% (sign of misdetection).
96 for (int i = SYNC_PULSE_START; i < sync_pulse_end; ++i) {
97 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
98 if (cycles < SYNC_PULSE_LENGTH / SYNC_TEST_TOLERANCE || cycles > SYNC_PULSE_LENGTH * SYNC_TEST_TOLERANCE) {
99 fprintf(stderr, "Sync cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
100 pulses[i].time, cycles);
104 return calibration_factor;
107 int main(int argc, char **argv)
109 make_lanczos_weight_table();
110 std::vector<short> pcm;
112 if (!read_audio_file(argv[1], &pcm, &sample_rate)) {
117 for (int i = 0; i < LEN; ++i) {
118 in[i] += rand() % 10000;
123 for (int i = 0; i < LEN; ++i) {
124 printf("%d\n", in[i]);
128 std::vector<pulse> pulses; // in seconds
132 double last_downflank = -1;
133 for (unsigned i = 0; i < pcm.size(); ++i) {
134 int bit = (pcm[i] > 0) ? 1 : 0;
135 if (bit == 0 && last_bit == 1) {
136 // Check if we ever go up above HYSTERESIS_LIMIT before we dip down again.
137 bool true_pulse = false;
139 int min_level_after = 32767;
140 for (j = i; j < pcm.size(); ++j) {
141 min_level_after = std::min<int>(min_level_after, pcm[j]);
142 if (pcm[j] > 0) break;
143 if (pcm[j] < -HYSTERESIS_LIMIT) {
151 fprintf(stderr, "Ignored down-flank at %.6f seconds due to hysteresis (%d < %d).\n",
152 double(i) / sample_rate, -min_level_after, HYSTERESIS_LIMIT);
159 double t = find_zerocrossing(pcm, i - 1) * (1.0 / sample_rate);
160 if (last_downflank > 0) {
163 p.len = t - last_downflank;
171 double calibration_factor = calibrate(pulses);
173 FILE *fp = fopen("cycles.plot", "w");
174 std::vector<char> tap_data;
175 for (unsigned i = 0; i < pulses.size(); ++i) {
176 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
177 fprintf(fp, "%f %f\n", pulses[i].time, cycles);
178 int len = lrintf(cycles / TAP_RESOLUTION);
179 if (i > SYNC_PULSE_END && (cycles < 100 || cycles > 800)) {
180 fprintf(stderr, "Cycle with downflank at %.6f was detected at %.0f cycles; misdetect?\n",
181 pulses[i].time, cycles);
184 tap_data.push_back(len);
186 int overflow_len = lrintf(cycles);
187 tap_data.push_back(0);
188 tap_data.push_back(overflow_len & 0xff);
189 tap_data.push_back((overflow_len >> 8) & 0xff);
190 tap_data.push_back(overflow_len >> 16);
196 memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
198 hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
199 hdr.data_len = tap_data.size();
201 fwrite(&hdr, sizeof(hdr), 1, stdout);
202 fwrite(tap_data.data(), tap_data.size(), 1, stdout);
204 // Output a debug raw file with pulse detection points.
205 fp = fopen("debug.raw", "wb");
208 unsigned pulsenum = 0;
209 for (unsigned i = 0; i < pcm.size(); ++i) {
210 unsigned next_pulse = (pulsenum >= pulses.size()) ? INT_MAX : int(pulses[pulsenum].time * sample_rate);
211 if (i >= next_pulse) {
212 fwrite(&one, sizeof(one), 1, fp);
215 fwrite(&zero, sizeof(zero), 1, fp);