diff options
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/osc.h | 35 | ||||
-rw-r--r-- | src/osc_saw.c | 28 | ||||
-rw-r--r-- | src/synth_engine.c | 4 |
4 files changed, 55 insertions, 13 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index f96d14e..77b275c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,6 +17,7 @@ common_sources = adsr.c \ osc.h \ osc_tri.c \ osc_sin.c \ + osc_saw.c \ osc_weird.c \ osc_sound.c \ raygui.h \ @@ -28,6 +28,15 @@ typedef struct osc_t { long end; enum osc_type type; } osc_t; + +#define MAKE_OSC(_name, _len, _type) {\ + .name = _name, \ + .data = 0, \ + .len = _len, \ + .start = 0, \ + .end = _len, \ + .type = _type, \ + }; /* Initializes the wavetables and functions for the oscilators */ void init_osc(); @@ -42,31 +51,35 @@ 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; } - +// function prototypes common for all oscs #define OSC_COMMON_H(osc) \ void \ set_##osc##_start(long start); \ void \ set_##osc##_end(long end); \ + void \ + set_##osc##_len(long len); \ float \ osc_##osc(float offset); \ float \ osc_##osc##_next(float f, float offset); -/*****************/ -/* osc functions */ -/*****************/ +// function definitions common for all oscs +#define OSC_COMMON(osc) \ + void \ + set_##osc##_start(long start) \ + { OSC_##osc.start = start; } \ + void \ + set_##osc##_end(long end) \ + { OSC_##osc.end = end; } \ + void \ + set_##osc##_len(long len) \ + { OSC_##osc.len = len; } OSC_COMMON_H(tri) OSC_COMMON_H(sin) OSC_COMMON_H(weird) OSC_COMMON_H(sound) +OSC_COMMON_H(saw) #endif /* OSC_H */ diff --git a/src/osc_saw.c b/src/osc_saw.c new file mode 100644 index 0000000..fc31f6f --- /dev/null +++ b/src/osc_saw.c @@ -0,0 +1,28 @@ +#include "osc.h" + +osc_t OSC_saw = MAKE_OSC("f_saw", 20000, WAVE); + +OSC_COMMON(saw) + +float +sawX(float sm, int offset, int len) +{ + float dOutput = 0.0; + for (float n = 1.0; n < sm; n++) + dOutput += (sinf(n * 2.0 * M_PI * offset / len)) / n; + return 0.5 * dOutput; +} + +float +osc_saw(float offset) +{ + return osc_interpolate(offset, + sawX(25, (int)offset, OSC_saw.len), + sawX(25, osc_next_index(&OSC_saw, offset), OSC_saw.len)); +} + +float +osc_saw_next(float f, float offset) +{ + return osc_next_offset(&OSC_saw, f, offset); +} diff --git a/src/synth_engine.c b/src/synth_engine.c index 5118c7d..d17ba49 100644 --- a/src/synth_engine.c +++ b/src/synth_engine.c @@ -92,8 +92,8 @@ gen3(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) //return sqr_sample(1, f, .5, midi_note->elapsed, sample_rate); //return wvt_next(wvt_sound, f, sample_rate, &midi_note->wvt_index); - float sample = osc_sound(midi_note->wvt_index); - midi_note->wvt_index = osc_sound_next(f, midi_note->wvt_index); + float sample = osc_saw(midi_note->wvt_index); + midi_note->wvt_index = osc_saw_next(f, midi_note->wvt_index); return sample; /* return sawX_sample(0.7, f, 5, midi_note->elapsed, sample_rate) */ |