refactor config classes

This commit is contained in:
2026-07-04 13:22:25 -05:00
parent 269648e021
commit 9365e12bb4
26 changed files with 266 additions and 172 deletions

View File

@@ -0,0 +1,48 @@
#include "Instrument.hpp"
#include "string"
Instrument::Instrument(ConfigService* config, LoggerService* logger) :
config_(config), logger_(logger) {
}
void Instrument::noteOn(float frequency, float velocity) {
phaseIncrement_ = 2.0f * pi * frequency / sampleRate_;
envelope_ += 0.01f; // so it triggers as active
active_ = true;
}
void Instrument::noteOff() {
active_ = false;
}
bool Instrument::isActive() {
return (envelope_ > 0.0f);
}
float Instrument::process(bool& scopeTrigger) {
if(active_ && envelope_ < 1.0f) envelope_ += 0.01f;
if(!active_ && envelope_ > 0.0f) envelope_ -= 0.0004f;
if(!isActive()) return 0.0f;
phase_ += phaseIncrement_;
if(phase_ > 2.0f * pi) {
phase_ -= 2.0f * pi;
scopeTrigger = true;
}
// float sample = sin(phase_);
targetSample = phase_ / pi - 1.0f; // saw
currentSample = (1.0f - responsiveness_) * currentSample + responsiveness_ * targetSample;
return currentSample * envelope_;
}

View File

@@ -0,0 +1,41 @@
#pragma once
#include "config/ConfigService.hpp"
#include "LoggerService.hpp"
// an instrument is our state space model. it calculates discrete samples as a function of time, the current states, and the inputs
// an instrumeent is owned by a voice
class Instrument {
public:
Instrument() = default;
Instrument(ConfigService* config, LoggerService* logger);
~Instrument() = default;
virtual void noteOn(float frequency, float velocity);
virtual void noteOff();
virtual bool isActive();
virtual float process(bool& scopeTrigger);
protected:
float sampleRate_ = 44100.0f;
bool active_ = false;
ConfigService* config_;
LoggerService* logger_;
static constexpr float pi = 3.14159265358979323846f;
float phase_ = 0.0f;
float phaseIncrement_ = 0.0f;
float envelope_ = 0.0f;
float targetSample = 0.0f;
float currentSample = 0.0f;
static constexpr float responsiveness_ = 0.1f;
};

View File

@@ -103,6 +103,7 @@ void PianoString::recalculateConstants() {
waveVelocity_ = 2.0f * frequency_ * L_;
// calculate number of super-sampling steps needed to maintain string stability
samplingSteps_ = 0;
do {
samplingSteps_++;
@@ -117,8 +118,8 @@ void PianoString::recalculateConstants() {
segmentCount_ = static_cast<size_t>(std::sqrt((-a+std::sqrt(a*a+4.0*b))/(2.0*b))) - 1;
}
} while (segmentCount_ < 60);
} while (segmentCount_ < 80);
// cap segment count at 80 if above
segmentCount_ = std::min(segmentCount_, static_cast<size_t>(80));
dx_ = L_ / static_cast<float>(segmentCount_);

View File

@@ -3,7 +3,7 @@
#include <cmath>
#include "synth/Instrument.hpp"
#include "synth/Instruments/Instrument.hpp"
class PianoString : public Instrument {