Split the synth part into a library, with a Makefile.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 3 May 2013 21:33:06 +0000 (23:33 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 3 May 2013 21:33:06 +0000 (23:33 +0200)
Makefile [new file with mode: 0644]
common.h [new file with mode: 0644]
synth.cpp
synth.h [new file with mode: 0644]
synth_main.cpp [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..50610c3
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,15 @@
+all: synth
+
+%.o: %.cpp
+       $(CXX) -MMD -MP $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
+
+OBJS=synth.o synth_main.o
+
+DEPS=$(OBJS:.o=.d)
+-include $(DEPS)
+
+synth: synth.o synth_main.o
+       $(CXX) -o $@ $^ $(LDFLAGS)
+
+clean:
+       $(RM) synth $(OBJS) $(DEPS)
diff --git a/common.h b/common.h
new file mode 100644 (file)
index 0000000..32cc6b5
--- /dev/null
+++ b/common.h
@@ -0,0 +1,7 @@
+#ifndef _COMMON_H
+#define _COMMON_H 1
+
+#define WAVE_FREQ 44100
+#define C64_FREQ 985248
+
+#endif  // !defined(_COMMON_H)
index 244b717..4c039c2 100644 (file)
--- a/synth.cpp
+++ b/synth.cpp
@@ -4,12 +4,12 @@
 #include <vector>
 #include <assert.h>
 
+#include "common.h"
+
 using namespace std;
 
 #define LANCZOS_RADIUS 10
 #define RESOLUTION 256
-#define WAVE_FREQ 44100
-#define C64_FREQ 985248
 
 // Cutoff frequency of low-pass filter (must be max. WAVE_FREQ / 2 or you
 // will get aliasing)
@@ -121,37 +121,13 @@ static float filter_update(float in)
        return out;
 }
 
-int main(int argc, char **argv)
+vector<float> synth(const vector<pulse> &pulses)
 {
        make_table();
 
-       FILE *fp = fopen(argv[1], "rb");
-       fseek(fp, 14, SEEK_SET);
-
-       int x = 0;
-
-       while (!feof(fp)) {
-               int len = getc(fp);
-               int cycles;
-               if (len == 0) {
-                       int a = getc(fp);
-                       int b = getc(fp);
-                       int c = getc(fp);
-                       cycles = a | (b << 8) | (c << 16);
-               } else {
-                       cycles = len * 8;
-               }
-               pulse p;
-               p.start = float(x) * WAVE_FREQ / C64_FREQ;
-               p.end = (float(x) + cycles * 0.5) * WAVE_FREQ / C64_FREQ;
-               pulses.push_back(p);
-               x += cycles;
-       }
-
-       int len_cycles = x;
-       int len_samples = int(ceil(float(len_cycles) * WAVE_FREQ / C64_FREQ));
+       int len_samples = int(ceil(pulses.back().start + (pulses.back().end - pulses.back().start) * 2));
        
-       fprintf(stderr, "%d pulses, total %.2f seconds (%d samples)\n", pulses.size(), len_cycles / float(C64_FREQ), len_samples);
+       fprintf(stderr, "%d pulses, total %.2f seconds (%d samples)\n", pulses.size(), len_samples / float(WAVE_FREQ), len_samples);
 
        int pulse_begin = 0;
 
@@ -190,9 +166,6 @@ int main(int argc, char **argv)
        for (int i = len_samples; i --> 0; ) {
                refiltered_samples[i] = filter_update(filtered_samples[i]);
        }
-       for (int i = 0; i < len_samples; ++i) {
-               //printf("%f %f\n", samples[i], refiltered_samples[i]);
-               short s = lrintf(refiltered_samples[i] * 16384.0f);
-               fwrite(&s, 2, 1, stdout);
-       }
+
+       return refiltered_samples;
 }
diff --git a/synth.h b/synth.h
new file mode 100644 (file)
index 0000000..3b71489
--- /dev/null
+++ b/synth.h
@@ -0,0 +1,13 @@
+#ifndef _SYNTH_H
+#define _SYNTH_H 1
+
+#include <vector>
+
+struct pulse {
+       // all values in samples
+       double start, end;
+};
+
+std::vector<float> synth(const std::vector<pulse> &pulses);
+
+#endif // !defined(_SYNTH_H)
diff --git a/synth_main.cpp b/synth_main.cpp
new file mode 100644 (file)
index 0000000..9f2ca96
--- /dev/null
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <math.h>
+
+#include "common.h"
+#include "synth.h"
+
+using namespace std;
+
+int main(int argc, char **argv)
+{
+       FILE *fp = fopen(argv[1], "rb");
+       fseek(fp, 14, SEEK_SET);
+
+       int x = 0;
+       
+       vector<pulse> pulses;
+       while (!feof(fp)) {
+               int len = getc(fp);
+               int cycles;
+               if (len == 0) {
+                       int a = getc(fp);
+                       int b = getc(fp);
+                       int c = getc(fp);
+                       cycles = a | (b << 8) | (c << 16);
+               } else {
+                       cycles = len * 8;
+               }
+               pulse p;
+               p.start = float(x) * WAVE_FREQ / C64_FREQ;
+               p.end = (float(x) + cycles * 0.5) * WAVE_FREQ / C64_FREQ;
+               pulses.push_back(p);
+               x += cycles;
+       }
+
+       vector<float> samples = synth(pulses);
+
+       for (int i = 0; i < samples.size(); ++i) {
+               //printf("%f %f\n", samples[i], refiltered_samples[i]);
+               short s = lrintf(samples[i] * 16384.0f);
+               fwrite(&s, 2, 1, stdout);
+       }
+}