added new FM synth code. not yet tested

master
Rachel Fae Fox (foxiepaws) 2019-10-31 03:04:12 -04:00
parent 3dc790d64c
commit f300a580a4
4 changed files with 137 additions and 0 deletions

32
src/synths/fm_2op.c Normal file
View File

@ -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;
}
}

47
src/synths/fm_2op.h Normal file
View File

@ -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

27
src/synths/fm_core.c Normal file
View File

@ -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 <math.h>
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;
}

31
src/synths/fm_core.h Normal file
View File

@ -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