amalgam/src/FM/Envelope.h

82 lines
2.8 KiB
C++

/*
* Filename: Envelope.h
*
* Description:
*
*
* Version:
* Created: Sun Mar 8 07:48:47 2020
* Revision: None
* Author: Rachel Fae Fox (foxiepaws),fox@foxiepa.ws
*
*/
#pragma once
#include <stdlib.h>
#include <math.h>
namespace amalgam {
namespace FM {
class Envelope {
enum EnvState {_e_off,_e_attack, _e_attackrelease, _e_decay, _e_sustain,_e_release, _e_finished};
// Time Params
unsigned int attack = 0; // attack length in samples
unsigned int decay = 0; // decay length in samples
unsigned int release = 0; // release time in samples
bool sustain = true; // boolean if sustain is enabled.
bool modMode = false; // boolean if we just use SL instead of minlevel till reset.
// Level Params
float max_level = 1.0;
float sustain_level = 1.0; // level that decay phase drops to.
float min_level = 0.0;
bool* gate; // pointer to gate
// Internal use
unsigned long int t = 0; // samples;
unsigned int smpRate; // sample rate
EnvState envstate = _e_off; // envelope state.
public:
// getters
unsigned int getAttackSamples() { return attack; }
unsigned int getDecaySamples() { return decay; }
unsigned int getReleaseSamples() { return release; }
float getAttackTime() { return (double) attack / (double) smpRate; }
float getDecayTime() { return (double) decay / (double) smpRate; }
float getReleaseTime() { return (double) release / (double) smpRate; }
bool getSustain() { return sustain; }
float getMaxLevel() { return max_level; }
float getMinLevel() { return min_level; }
float getSustainLevel() { return sustain_level; }
bool getModMode() { return modMode; }
bool* getGate() { return gate; }
// setters
void setAttackSamples(unsigned int a) { attack = a; }
void setAttackTime(float a) { attack = floor(smpRate * a); }
void setDecaySamples(unsigned int d) { decay = d; }
void setDecayTime(float d) { decay = floor(smpRate * d); }
void setReleaseSamples(unsigned int r) { release = r; }
void setReleaseTime(float r) { release = floor(smpRate * r); }
void setSustain(bool s) { sustain = s; }
void setModMode(bool m) { modMode = m; }
void setMaxLevel(float ml) { max_level = ml; }
void setMinLevel(float ml) { min_level = ml; }
void setSustainLevel(float sl) { sustain_level = sl; }
void setGate(bool* g) { gate = g; }
void setRate(unsigned int sr) { smpRate = sr; }
// helpers
// note : if you make use of these functions YOU must manage their memory.
void makeGate() { gate = (bool*) malloc(sizeof(bool)); }
void freeGate() { free(gate); }
// constructors
Envelope(){}
Envelope(unsigned int sr, unsigned int a, unsigned int d, bool s, unsigned int r, float sl, bool* g);
// methods
float process();
float operator()() { return process(); }
};
}
}
/* Local Variables: */
/* mode: c++ */
/* End: */
/* vim: ft=cpp: */