first commit

master
Rachel Fae Fox (foxiepaws) 2019-11-01 21:44:46 -04:00
commit 834cfd73fd
13 changed files with 526 additions and 0 deletions

17
src/common.h Normal file
View File

@ -0,0 +1,17 @@
/*
* Filename: common.h
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 00:25:00 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#ifndef _H_COMMON
#define _H_COMMON
enum Waveform {_waveform_sine, _waveform_saw, _waveform_square};
#endif

56
src/filters/svf.cpp Normal file
View File

@ -0,0 +1,56 @@
/*
* Filename: svf.cpp
*
* Description:
*
*
* Version:
* Created: Thu Oct 31 23:24:34 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#include "svf.h"
#include <math.h>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
SVF::SVF(FilterMode f) {
this->mode = f;
this->reset();
this->freq = 0.0f;
this->q = 0.0f;
this->sample_rate=44100;
}
void SVF::reset() {
this->notch = 0;
this->high = 0;
this->low = 0;
this->band = 0;
}
float SVF::run(float in) {
float a = in;
float q = 1-this->q;
float freq = 2.0*sin(M_PI * MIN(0.25, this->freq/this->sample_rate));
this->low = this->low + freq * this->band;
this->high = q * a - this->low - q * this->band;
this->band = freq * this->high + this->band;
this->notch = this->high + this->low;
switch (mode) {
case _low:
return this->low;
case _band:
return this->band;
case _high:
return this->high;
case _notch:
return this->notch;
}
}

43
src/filters/svf.h Normal file
View File

@ -0,0 +1,43 @@
/*
* Filename: svf.h
*
* Description:
*
*
* Version:
* Created: Thu Oct 31 23:24:28 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#ifndef _H_SVF
#define _H_SVF
enum FilterMode { _low, _band, _high, _notch };
class SVF {
public:
float freq;
float q;
FilterMode mode;
float notch;
float band;
float high;
float low;
float run(float);
SVF(FilterMode);
protected:
void reset();
private:
int sample_rate;
};
#endif
/* Local Variables: */
/* mode: c++ */
/* End: */

49
src/synths/basic.cpp Normal file
View File

@ -0,0 +1,49 @@
/*
* Filename: basic.cpp
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 00:04:23 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#include "../common.h"
#include "../utils/envelope.h"
#include "basic.h"
#include <stdlib.h>
#include <math.h>
float Basic::run() {
float out;
this->env.gate = this->gate;
switch (this->shape) {
case _waveform_saw:
out = this->amp * this->env.run() * fmod((this->freq+1) * this->t / 44100,1.0);
break;
case _waveform_sine:
out = this->amp * this->env.run() * sin(2 * M_PI * this->t * this->freq / 44100);
break;
case _waveform_square:
out = this->amp * this->env.run() * fmod((this->freq+1) * this->t / 44100,1.0) > this->pwm;
break;
default:
out = 0.0F;
break;
}
return out;
}
Basic::Basic(Waveform w, Envelope e) {
this->shape = w;
this->env = e;
this->amp = 1.0;
this->pwm = 0.5f;
this->gate = false;
this->t = 0;
}

33
src/synths/basic.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Filename: basic.h
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 01:24:58 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#ifndef _H_BASIC
#define _H_BASIC
#include "../common.h"
#include "../utils/envelope.h"
class Basic {
public:
float freq;
Waveform shape;
int gate;
Envelope env;
float pwm;
float amp;
Basic(Waveform,Envelope);
float run();
private:
unsigned long t;
};
#endif

31
src/synths/fm/core.cpp Normal file
View File

@ -0,0 +1,31 @@
/*
* Filename: core.c
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 01:38:00 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#include "core.h"
#include <math.h>
FM::Operator() {
this->t = 0;
this->sample_rate = 44100;
this->gate = false;
this->feedback_buffer = 0.0;
}
float FM::Operator::run(float phase) {
this->e.gate = this->gate;
float out = 0;
out = sin(2 * M_PI * this->t * (this->mul * this->freq) / this->sample_rate + ((this->feedback_level > 0.0) ? this->feedback_buffer : phase));
out *= (this->level * this->e.run());
this->feedback_buffer = out;
return out;
}

33
src/synths/fm/core.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Filename: core.h
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 01:34:36 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#ifndef _H_FM_CORE
#define _H_FM_CORE
#include "../../utils/envelope.h"
class FM::Operator {
public:
float level;
float mul;
float freq;
float feedback_level;
bool gate;
Envelope env;
float run(float);
FM::Operator();
private:
float feedback_buffer;
unsigned int t;
unsigned int sample_rate;
}
#endif

16
src/synths/fm2.c Normal file
View File

@ -0,0 +1,16 @@
/*
* Filename: fm2.c
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 03:04:40 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#include "fm2.h";

38
src/synths/fm2.cpp Normal file
View File

@ -0,0 +1,38 @@
/*
* Filename: fm2.cpp
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 03:05:59 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#include "fm2.h"
FM2::FM2 ( ) {
this->carrier = FM::Operator;
this->modulator = FM::Operator;
this->gate = false;
this->algorithm = 1;
}
FM2::run() {
float v1, v2, z;
this->carrier->freq=this->modulator->freq=this->freq;
this->carrier->gate=this->modulator->gate=this->gate;
switch(this->algorithm) {
case 0:
v1 = this->carrier->run(0.0);
v2 = this->modulator->run(0.0);
z = (double) v1 + (double) v2 - ((double) v1 * (double) v2);
return z;
case 1:
return this->carrier->run(this->modulator->run(0.0));
}
}

34
src/synths/fm2.h Normal file
View File

@ -0,0 +1,34 @@
/*
* Filename: fm2.h
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 01:43:31 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#ifndef _H_FM2
#define _H_FM2
#include "fm/core.h"
class FM2 {
public:
FM::Operator carrier;
FM::Operator modulator;
bool gate;
float freq;
unsigned int algorithm;
float run();
FM2();
private:
unsigned int sample_rate;
unsigned long t;
}
#endif

13
src/synththing.cpp Normal file
View File

@ -0,0 +1,13 @@
/*
* Filename: synththing.cpp
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 03:16:02 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/

117
src/utils/envelope.cpp Normal file
View File

@ -0,0 +1,117 @@
/*
* Filename: envelope.cpp
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 00:06:43 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#include "envelope.h"
float Envelope::run() {
switch (this->envstate) {
case _e_off:
if (this->gate) {
this->t = 0;
this->envstate = _e_attack;
return this->run();
}
break;
case _e_attack:
if (this->gate) {
if ( this->t < this->attack ) {
float stepsize = (this->max_level - this->min_level) / this->attack;
this->t++;
return this->t * stepsize;
} else {
if (this->ar) {
this->t = 0;
this->envstate = _e_sustain;
return this->max_level;
} else {
this->t = 0;
this->envstate = _e_decay;
return this->max_level;
}
}
} else {
this->envstate = _e_attackrelease;
return this->run();
}
break;
case _e_decay:
if (this->gate) {
if (this->t < this->decay) {
float stepsize = (this->max_level - this->sustain_level) / this->decay;
this->t++;
return (this->max_level - (this->t * stepsize));
} else {
this->envstate = _e_sustain;
return this->sustain_level;
}
} else {
this->envstate = _e_attackrelease;
return this->run();
}
break;
case _e_sustain:
if (!this->sustain) {
this->envstate = _e_release;
this->t = 0;
return this->run();;
}
if (!this->gate) {
this->t = 0;
this->envstate = _e_release;
if (this->ar)
return this->max_level;
return this->sustain_level;
}
if (this->ar)
return this->max_level;
return this->sustain_level;
break;
case _e_release:
if (this->gate && this->sustain) {
// reset envelope.
this->envstate=_e_off;
return this->run();;
}
if (this->t < this->release) {
float stepsize = ((this->ar ? this->max_level : this->sustain_level) - this->min_level) / this->release;
this->t++;
return (this->ar ? this->max_level : this->sustain_level) - (stepsize * this->t);
} else {
this->envstate = _e_off;
return this->min_level;
}
break;
case _e_attackrelease:
if (this->gate) {
//reset envelope
this->envstate=_e_off;
return this->run();;
}
return 0.0f;
break;
}
return 0.0f; // if this is reached, something went wrong or an unhandled function was used.
}
Envelope::Envelope() {
this->envstate = _e_off;
this->t = 0;
this->max_level = 1.0f;
this->sustain_level = 1.0f;
this->min_level = 0.0f;
}

46
src/utils/envelope.h Normal file
View File

@ -0,0 +1,46 @@
/*
* Filename: envelope.h
*
* Description:
*
*
* Version:
* Created: Fri Nov 1 00:22:22 2019
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#ifndef _H_ENVELOPE
#define _H_ENVELOPE
class Envelope {
// these values are in samples.
public:
unsigned int attack;
unsigned int decay;
unsigned int release;
float sustain_level;
float max_level;
float min_level;
float level;
bool gate;
bool ar;
bool sustain;
Envelope();
float run();
private:
enum EnvState {_e_off,_e_attack, _e_attackrelease, _e_decay, _e_sustain,_e_release, _e_finished};
unsigned long int t;
EnvState envstate;
};
#endif
// Local Variables:
// mode: c++
// End: