]> git.sesse.net Git - c64tapwav/blobdiff - decode.cpp
Support any sample rate, not only 44100 Hz.
[c64tapwav] / decode.cpp
index 49be647ea2cc67442718f2765e66ff508cd91064..c5aa9669d0c473e8fe92911cc9cd348da3839159 100644 (file)
@@ -6,12 +6,12 @@
 #include <vector>
 #include <algorithm>
 
+#include "audioreader.h"
 #include "interpolate.h"
 #include "tap.h"
 
 #define BUFSIZE 4096
 #define HYSTERESIS_LIMIT 3000
-#define SAMPLE_RATE 44100
 #define C64_FREQUENCY 985248
 
 #define SYNC_PULSE_START 1000
@@ -55,14 +55,10 @@ int main(int argc, char **argv)
 {
        make_lanczos_weight_table();
        std::vector<short> pcm;
-
-       while (!feof(stdin)) {
-               short buf[BUFSIZE];
-               ssize_t ret = fread(buf, 2, BUFSIZE, stdin);
-               if (ret >= 0) {
-                       pcm.insert(pcm.end(), buf, buf + ret);
-               }
-       }       
+       int sample_rate;
+       if (!read_audio_file(argv[1], &pcm, &sample_rate)) {
+               exit(1);
+       }
 
 #if 0
        for (int i = 0; i < LEN; ++i) {
@@ -100,14 +96,14 @@ int main(int argc, char **argv)
                        if (!true_pulse) {
 #if 0
                                fprintf(stderr, "Ignored down-flank at %.6f seconds due to hysteresis (%d < %d).\n",
-                                       double(i) / SAMPLE_RATE, -min_level_after, HYSTERESIS_LIMIT);
+                                       double(i) / sample_rate, -min_level_after, HYSTERESIS_LIMIT);
 #endif
                                i = j;
                                continue;
                        } 
 
                        // down-flank!
-                       double t = find_zerocrossing(pcm, i - 1) * (1.0 / SAMPLE_RATE);
+                       double t = find_zerocrossing(pcm, i - 1) * (1.0 / sample_rate);
                        if (last_downflank > 0) {
                                pulse p;
                                p.time = t;
@@ -190,7 +186,7 @@ int main(int argc, char **argv)
        short zero = 0;
        unsigned pulsenum = 0;
        for (unsigned i = 0; i < pcm.size(); ++i) {
-               unsigned next_pulse = (pulsenum >= pulses.size()) ? INT_MAX : int(pulses[pulsenum].time * SAMPLE_RATE);
+               unsigned next_pulse = (pulsenum >= pulses.size()) ? INT_MAX : int(pulses[pulsenum].time * sample_rate);
                if (i >= next_pulse) {
                        fwrite(&one, sizeof(one), 1, fp);
                        ++pulsenum;