summaryrefslogtreecommitdiffstats
path: root/src/osc.h
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2023-07-07 01:09:12 +0300
committergramanas <anastasis.gramm2@gmail.com>2023-07-07 01:09:12 +0300
commitc8cd7f9298de876f2046fddd2e322a63c421a505 (patch)
tree1b764fed928592f8f3465e9ef28552b451efad8a /src/osc.h
parentf170fb058a07175e6e753f2a6d20283dc7200a88 (diff)
downloadsynth-project-c8cd7f9298de876f2046fddd2e322a63c421a505.tar.gz
synth-project-c8cd7f9298de876f2046fddd2e322a63c421a505.tar.bz2
synth-project-c8cd7f9298de876f2046fddd2e322a63c421a505.zip
suppa
Diffstat (limited to 'src/osc.h')
-rw-r--r--src/osc.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/osc.h b/src/osc.h
new file mode 100644
index 0000000..3915e5a
--- /dev/null
+++ b/src/osc.h
@@ -0,0 +1,72 @@
+#ifndef OSC_H
+#define OSC_H
+
+#include <math.h>
+
+/**
+ * The main idea of the osc is the following:
+ * The root of the sound is a periodic wave function.
+ * This can be a stored period of a wave in a wavetable
+ * or a function like sin(). In order to play a sound with
+ * the wave an external float offset is kept that points to the
+ * next sample, a function is provided to advance this offset
+ * a calucalted step amount, depending on the desired frequency.
+ * Linear interpolation is used so that a float offset can work
+ * and provide autosmoothing.
+ */
+
+enum osc_type {
+ WAVE,
+ SAMPLE
+};
+
+typedef struct osc_t {
+ char name[16];
+ float * data;
+ long len;
+ long start;
+ long end;
+ enum osc_type type;
+} osc_t;
+
+/* Initializes the wavetables and functions for the oscilators */
+void init_osc();
+
+float osc_interpolate(float offset, float start, float end);
+float osc_next_offset(osc_t * osc, float f, float offset);
+int osc_next_index(osc_t * osc, float offset);
+
+int osc_load_wav(osc_t * osc, const char * path);
+
+/***************/
+/* OSCILATORS */
+/***************/
+
+#define OSC_COMMON(osc) \
+ void \
+ set_##osc##_start(long start) \
+ { OSC_##osc.start = start; } \
+ void \
+ set_##osc##_end(long end) \
+ { OSC_##osc.end = end; }
+
+#define OSC_COMMON_H(osc) \
+ void \
+ set_##osc##_start(long start); \
+ void \
+ set_##osc##_end(long end); \
+ float \
+ osc_##osc(float offset); \
+ float \
+ osc_##osc##_next(float f, float offset);
+
+/*****************/
+/* osc functions */
+/*****************/
+
+OSC_COMMON_H(tri)
+OSC_COMMON_H(sin)
+OSC_COMMON_H(weird)
+OSC_COMMON_H(sound)
+
+#endif /* OSC_H */