diff --git a/src/synth/Synth.cpp b/src/synth/Synth.cpp index a8ed231..126e6ee 100644 --- a/src/synth/Synth.cpp +++ b/src/synth/Synth.cpp @@ -8,13 +8,8 @@ #define M_PI 3.14159265358979323846 #endif -// TODO: you get it, also in a yml config -#define SYNTH_PITCH_STANDARD 432.0f // frequency of home pitch -#define SYNTH_MIDI_HOME 69 // midi note index of home pitch -#define SYNTH_NOTES_PER_OCTAVE 12 - Synth::Synth(const ParameterStore& params) : paramStore_(params) { - + voices_.fill(Voice(params_.data())); } void Synth::updateParams() { @@ -76,13 +71,19 @@ void Synth::process(float* out, uint32_t nFrames, uint32_t sampleRate) { for(auto& p : params_) p.update(); // TODO: profile this // assemble float array of parameters so that its easier for voices to retrieve - + float params[PARAM_COUNT] = {0.0f}; + for(int i = 0; i < PARAM_COUNT; i++) { + params[i] = params_[i].current; + } + // foreach voice, process... float mix = 0.0f; for(Voice& v : voices_) { - mix += v.process(¶ms_[0].current, triggered); + mix += v.process(params, triggered); } + sampleOut = mix; + // write to buffer out[2*i] = sampleOut; // left out[2*i+1] = sampleOut; // right diff --git a/src/synth/Voice.cpp b/src/synth/Voice.cpp index 2fb4851..e2bfc4b 100644 --- a/src/synth/Voice.cpp +++ b/src/synth/Voice.cpp @@ -2,7 +2,7 @@ #include "Voice.h" #include -Voice::Voice(std::array* params) : params_(params) { +Voice::Voice(SmoothedParam* params) : params_(params) { } @@ -26,6 +26,10 @@ inline float Voice::noteToFrequency(uint8_t note) { return SYNTH_PITCH_STANDARD * pow(2.0f, static_cast(note - SYNTH_MIDI_HOME) / static_cast(SYNTH_NOTES_PER_OCTAVE)); } +inline float Voice::getParam(ParamId id) { + return params_[static_cast(id)].current; +} + void Voice::noteOn(int midiNote, float velocity) { note_ = midiNote; velocity_ = velocity; @@ -43,6 +47,7 @@ void Voice::noteOff() { gainEnvelope_.noteOff(); cutoffEnvelope_.noteOff(); resonanceEnvelope_.noteOff(); + active_ = false; } bool Voice::isActive() { @@ -71,7 +76,7 @@ float Voice::process(float* params, bool& scopeTrigger) { // TODO: make pitchOffset variable for each oscillator (maybe three values like octave, semitone offset, and pitch offset in cents) float pitchOffset = 1.0f; - float phaseInc = pitchOffset * 2.0f * M_PI * frequency_ / static_cast(sampleRate); + float phaseInc = pitchOffset * 2.0f * M_PI * frequency_ / static_cast(sampleRate_); float gain = gainEnv * getParam(ParamId::Osc1VolumeDepth); float sampleOut = 0.0f; @@ -109,5 +114,10 @@ float Voice::process(float* params, bool& scopeTrigger) { sampleOut = filter1_.biquadProcess(sampleOut); sampleOut = filter2_.biquadProcess(sampleOut); + phase_ += phaseInc; + if (phase_ > 2.0f * M_PI) { + phase_ -= 2.0f * M_PI; + } + return sampleOut; } \ No newline at end of file diff --git a/src/synth/Voice.h b/src/synth/Voice.h index 78b9ec2..991f7c3 100644 --- a/src/synth/Voice.h +++ b/src/synth/Voice.h @@ -4,7 +4,7 @@ #include "Oscillator.h" #include "Envelope.h" #include "Filter.h" -#include "ParameterStore.h" +#include "../ParameterStore.h" #ifndef M_PI // I hate my stupid chungus life #define M_PI 3.14159265358979323846 @@ -26,7 +26,8 @@ struct SmoothedParam { class Voice { public: - Voice(std::array* params); + Voice() = default; + Voice(SmoothedParam* params); ~Voice() = default; void setSampleRate(float sampleRate); @@ -45,6 +46,7 @@ private: float sampleRate_ = 44100.0f; inline float noteToFrequency(uint8_t note); + inline float getParam(ParamId id); uint8_t note_ = 0; float velocity_ = 1.0f; @@ -67,6 +69,6 @@ private: Filter filter2_; // paramstore pointer - std::array* params_; + SmoothedParam* params_; };