polyphony working checkpoint

This commit is contained in:
2026-01-12 19:02:56 -06:00
parent f30c2d00cb
commit 1121dedcee
3 changed files with 26 additions and 13 deletions

View File

@@ -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(&params_[0].current, triggered);
mix += v.process(params, triggered);
}
sampleOut = mix;
// write to buffer
out[2*i] = sampleOut; // left
out[2*i+1] = sampleOut; // right

View File

@@ -2,7 +2,7 @@
#include "Voice.h"
#include <cmath>
Voice::Voice(std::array<SmoothedParam, PARAM_COUNT>* 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<float>(note - SYNTH_MIDI_HOME) / static_cast<float>(SYNTH_NOTES_PER_OCTAVE));
}
inline float Voice::getParam(ParamId id) {
return params_[static_cast<size_t>(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<float>(sampleRate);
float phaseInc = pitchOffset * 2.0f * M_PI * frequency_ / static_cast<float>(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;
}

View File

@@ -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<SmoothedParam, PARAM_COUNT>* 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<SmoothedParam, PARAM_COUNT>* params_;
SmoothedParam* params_;
};