From f300a580a43a61c1dee34248c9ac8f3158b55fa1 Mon Sep 17 00:00:00 2001 From: "Rachel Fae Fox (foxiepaws)" Date: Thu, 31 Oct 2019 03:04:12 -0400 Subject: [PATCH] added new FM synth code. not yet tested --- src/synths/fm_2op.c | 32 ++++++++++++++++++++++++++++++ src/synths/fm_2op.h | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/synths/fm_core.c | 27 +++++++++++++++++++++++++ src/synths/fm_core.h | 31 +++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 src/synths/fm_2op.c create mode 100644 src/synths/fm_2op.h create mode 100644 src/synths/fm_core.c create mode 100644 src/synths/fm_core.h diff --git a/src/synths/fm_2op.c b/src/synths/fm_2op.c new file mode 100644 index 0000000..1bdd2e5 --- /dev/null +++ b/src/synths/fm_2op.c @@ -0,0 +1,32 @@ +/* + * Filename: fm_2op.c + * + * Description: + * + * + * Version: + * Created: Thu Oct 31 02:02:19 2019 + * Revision: None + * Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws + * + */ + +#include "../common.h" +#include "fm_2op.h" + +float fm2_process(struct FM2 *self, EngineState *t) { + self->Carrier.gate=self->gate; + self->Modulator.gate=self->gate; + switch (self->Algorithm) { + case _fm2_0: + __asm ("nop"); + float v1 = (self->Carrier).run(&self->Carrier, t, 0.0); + float v2 = (self->Modulator).run(&self->Modulator, t, 0.0); + double z = (double) v1 + (double) v2 - ((double)v1*(double)v2); + return z; + break; + case _fm2_1: + return (self->Carrier).run(&self->Carrier, t, (self->Modulator).run(&self->Modulator,t,0.0)); + break; + } +} diff --git a/src/synths/fm_2op.h b/src/synths/fm_2op.h new file mode 100644 index 0000000..3c143e7 --- /dev/null +++ b/src/synths/fm_2op.h @@ -0,0 +1,47 @@ +/* + * Filename: fm_2op.h + * + * Description: + * + * + * Version: + * Created: Thu Oct 31 02:02:19 2019 + * Revision: None + * Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws + * + */ + +#ifndef _H_FM2OP +#define _H_FM2OP +#include "../common.h" +#include "fm_core.h" + +typedef enum FM2_ALG {_fm2_0, _fm2_1 } FM2_Alg; +/* FM Algorithms + +alg 0 + + [ out ] + | | + [ 1] [ 2]-[fb] + +alg 1 + [fb] -> [2]-> [1] -> [ out ] +*/ +typedef struct FM2 { + Operator Carrier; + Operator Modulator; + FM2_Alg Algorithm; + float freq; + float gate; + float(*process)(struct FM2 *, EngineState *); +} FM2; + +float fm2_process(struct FM2 *, EngineState *); + + + + + + +#endif diff --git a/src/synths/fm_core.c b/src/synths/fm_core.c new file mode 100644 index 0000000..d0d7232 --- /dev/null +++ b/src/synths/fm_core.c @@ -0,0 +1,27 @@ +/* + * Filename: fm_core.c + * + * Description: + * + * + * Version: + * Created: Thu Oct 31 02:06:11 2019 + * Revision: None + * Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws + * + */ + +#include "../common.h" +#include "../utils/envelope.h" +#include "fm_core.h" +#include + +float core_fm_run(Operator* self, EngineState * t,float phase) { + self->e.gate = self->gate; + float out = 0; + out = sin(2 * M_PI * t->t * (self->mul * self->freq) / 44100 + ((self->feedback_level > 0.0) ? self->feedback_buffer : phase)); + out *= (self->level * self->e.process(&self->e,t)); + self->feedback_buffer = out; + return out; +} + diff --git a/src/synths/fm_core.h b/src/synths/fm_core.h new file mode 100644 index 0000000..8dfed97 --- /dev/null +++ b/src/synths/fm_core.h @@ -0,0 +1,31 @@ +/* + * Filename: fm_core.h + * + * Description: + * + * + * Version: + * Created: Thu Oct 31 02:06:11 2019 + * Revision: None + * Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws + * + */ + +#ifndef _H_FM_CORE +#define _H_FM_CORE +#include "../common.h" +#include "../utils/envelope.h" + +typedef struct Operator { + float level; + Envelope e; + bool gate; + float freq; + float mul; + float feedback_level; + float feedback_buffer; + float(*run)(struct Operator*,EngineState *,float); +} Operator; +float core_fm_run(Operator*, EngineState *,float); + +#endif