9 #define LANCZOS_RADIUS 30
11 #define HYSTERESIS_LIMIT 3000
12 #define SAMPLE_RATE 44100
13 #define C64_FREQUENCY 985248
14 #define TAP_RESOLUTION 8
16 #define SYNC_PULSE_START 1000
17 #define SYNC_PULSE_END 15000
18 #define SYNC_PULSE_LENGTH 380.0
19 #define SYNC_TEST_TOLERANCE 1.10
25 unsigned int data_len;
31 return 1.0f - fabs(x);
38 double weight(double x)
40 if (fabs(x) > LANCZOS_RADIUS) {
43 return sinc(M_PI * x) * sinc(M_PI * x / LANCZOS_RADIUS);
46 double weight(double x)
51 return 1.0f - fabs(x);
55 double interpolate(const std::vector<short> &pcm, double i)
57 int lower = std::max<int>(ceil(i - LANCZOS_RADIUS), 0);
58 int upper = std::min<int>(floor(i + LANCZOS_RADIUS), pcm.size() - 1);
61 for (int x = lower; x <= upper; ++x) {
62 sum += pcm[x] * weight(i - x);
68 double find_zerocrossing(const std::vector<short> &pcm, int x)
73 if (pcm[x + 1] == 0) {
77 assert(pcm[x + 1] > 0);
82 while (upper - lower > 1e-6) {
83 double mid = 0.5f * (upper + lower);
84 if (interpolate(pcm, mid) > 0) {
91 return 0.5f * (upper + lower);
95 double time; // in seconds from start
96 double len; // in seconds
99 int main(int argc, char **argv)
101 std::vector<short> pcm;
103 while (!feof(stdin)) {
105 ssize_t ret = fread(buf, 2, BUFSIZE, stdin);
107 pcm.insert(pcm.end(), buf, buf + ret);
112 for (int i = 0; i < LEN; ++i) {
113 in[i] += rand() % 10000;
118 for (int i = 0; i < LEN; ++i) {
119 printf("%d\n", in[i]);
123 std::vector<pulse> pulses; // in seconds
127 double last_upflank = -1;
128 int last_max_level = 0;
129 for (unsigned i = 0; i < pcm.size(); ++i) {
130 int bit = (pcm[i] > 0) ? 1 : 0;
131 if (bit == 1 && last_bit == 0 && last_max_level > HYSTERESIS_LIMIT) {
133 double t = find_zerocrossing(pcm, i - 1) * (1.0 / SAMPLE_RATE);
134 if (last_upflank > 0) {
137 p.len = t - last_upflank;
143 last_max_level = std::max(last_max_level, abs(pcm[i]));
147 // Calibrate on the first ~25k pulses (skip a few, just to be sure).
148 double calibration_factor = 1.0f;
149 if (pulses.size() < SYNC_PULSE_END) {
150 fprintf(stderr, "Too few pulses, not calibrating!\n");
153 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
154 sum += pulses[i].len;
156 double mean_length = C64_FREQUENCY * sum / (SYNC_PULSE_END - SYNC_PULSE_START);
157 calibration_factor = SYNC_PULSE_LENGTH / mean_length;
158 fprintf(stderr, "Calibrated sync pulse length: %.2f -> 380.0 (change %+.2f%%)\n",
159 mean_length, 100.0 * (calibration_factor - 1.0));
161 // Check for pulses outside +/- 10% (sign of misdetection).
162 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
163 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
164 if (cycles < SYNC_PULSE_LENGTH / SYNC_TEST_TOLERANCE || cycles > SYNC_PULSE_LENGTH * SYNC_TEST_TOLERANCE) {
165 fprintf(stderr, "Sync cycle with upflank at %.6f was detected at %.0f cycles; misdetect?\n",
166 pulses[i].time, cycles);
170 // Compute the standard deviation (to check for uneven speeds).
172 for (int i = SYNC_PULSE_START; i < SYNC_PULSE_END; ++i) {
173 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
174 sum2 += (cycles - SYNC_PULSE_LENGTH) * (cycles - SYNC_PULSE_LENGTH);
176 double stddev = sqrt(sum2 / (SYNC_PULSE_END - SYNC_PULSE_START - 1));
177 fprintf(stderr, "Sync pulse length standard deviation: %.2f cycles\n",
181 std::vector<char> tap_data;
182 for (unsigned i = 0; i < pulses.size(); ++i) {
183 double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
184 int len = lrintf(cycles / TAP_RESOLUTION);
185 if (i > SYNC_PULSE_END && (cycles < 100 || cycles > 800)) {
186 fprintf(stderr, "Cycle with upflank at %.6f was detected at %.0f cycles; misdetect?\n",
187 pulses[i].time, cycles);
190 tap_data.push_back(len);
192 int overflow_len = lrintf(cycles);
193 tap_data.push_back(0);
194 tap_data.push_back(overflow_len & 0xff);
195 tap_data.push_back((overflow_len >> 8) & 0xff);
196 tap_data.push_back(overflow_len >> 16);
201 memcpy(hdr.identifier, "C64-TAPE-RAW", 12);
203 hdr.reserved[0] = hdr.reserved[1] = hdr.reserved[2] = 0;
204 hdr.data_len = tap_data.size();
206 fwrite(&hdr, sizeof(hdr), 1, stdout);
207 fwrite(tap_data.data(), tap_data.size(), 1, stdout);