commit 834cfd73fd741e9984a8c5c36acd95ce158290bb Author: Rachel Fae Fox (foxiepaws) Date: Fri Nov 1 21:44:46 2019 -0400 first commit diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..bc93528 --- /dev/null +++ b/src/common.h @@ -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 diff --git a/src/filters/svf.cpp b/src/filters/svf.cpp new file mode 100644 index 0000000..ba036ed --- /dev/null +++ b/src/filters/svf.cpp @@ -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 +#define MAX(a,b) (a>b?a:b) +#define MIN(a,b) (amode = 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; + } + +} + diff --git a/src/filters/svf.h b/src/filters/svf.h new file mode 100644 index 0000000..bf04d0c --- /dev/null +++ b/src/filters/svf.h @@ -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: */ diff --git a/src/synths/basic.cpp b/src/synths/basic.cpp new file mode 100644 index 0000000..41bcabd --- /dev/null +++ b/src/synths/basic.cpp @@ -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 +#include + +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; +} + diff --git a/src/synths/basic.h b/src/synths/basic.h new file mode 100644 index 0000000..e0a5366 --- /dev/null +++ b/src/synths/basic.h @@ -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 diff --git a/src/synths/fm/core.cpp b/src/synths/fm/core.cpp new file mode 100644 index 0000000..bef98f0 --- /dev/null +++ b/src/synths/fm/core.cpp @@ -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 + +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; +} diff --git a/src/synths/fm/core.h b/src/synths/fm/core.h new file mode 100644 index 0000000..eec637f --- /dev/null +++ b/src/synths/fm/core.h @@ -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 diff --git a/src/synths/fm2.c b/src/synths/fm2.c new file mode 100644 index 0000000..e4aa1f6 --- /dev/null +++ b/src/synths/fm2.c @@ -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"; + + diff --git a/src/synths/fm2.cpp b/src/synths/fm2.cpp new file mode 100644 index 0000000..823a408 --- /dev/null +++ b/src/synths/fm2.cpp @@ -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)); + } +} diff --git a/src/synths/fm2.h b/src/synths/fm2.h new file mode 100644 index 0000000..7ddbea7 --- /dev/null +++ b/src/synths/fm2.h @@ -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 diff --git a/src/synththing.cpp b/src/synththing.cpp new file mode 100644 index 0000000..aaaf2fb --- /dev/null +++ b/src/synththing.cpp @@ -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 + * + */ + diff --git a/src/utils/envelope.cpp b/src/utils/envelope.cpp new file mode 100644 index 0000000..a76e411 --- /dev/null +++ b/src/utils/envelope.cpp @@ -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; +} diff --git a/src/utils/envelope.h b/src/utils/envelope.h new file mode 100644 index 0000000..d6dde29 --- /dev/null +++ b/src/utils/envelope.h @@ -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: