Skip to content

Commit

Permalink
Small optmization to SLN, Progress uses standard sampletime fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoar committed Mar 27, 2018
1 parent 2996def commit 5afecea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
12 changes: 2 additions & 10 deletions src/Progress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,8 @@ struct Progress : AHModule {
NUM_LIGHTS
};

float sampleRate;

Progress() : AHModule(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {
sampleRate = engineGetSampleRate();
}
Progress() : AHModule(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) { }

void onSampleRateChange() override {
sampleRate = engineGetSampleRate();
}

void step() override;

enum ParamType {
Expand Down Expand Up @@ -228,7 +220,7 @@ void Progress::step() {
else {
// Internal clock
float clockTime = powf(2.0f, params[CLOCK_PARAM].value + inputs[CLOCK_INPUT].value);
phase += clockTime * engineGetSampleTime();
phase += clockTime * delta;
if (phase >= 1.0f) {
setIndex(index + 1, numSteps);
}
Expand Down
25 changes: 14 additions & 11 deletions src/SLN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ struct SLN : AHModule {
float target = 0.0f;
float current = 0.0f;

// minimum and maximum slopes in volts per second
const float slewMin = 0.1;
const float slewMax = 10000.0;

const float slewRatio = slewMin / slewMax;

// Amount of extra slew per voltage difference
const float shapeScale = 1.0/10.0;


};

void SLN::step() {
Expand Down Expand Up @@ -73,26 +83,19 @@ void SLN::step() {
}

float shape = params[SLOPE_PARAM].value;

// minimum and maximum slopes in volts per second
const float slewMin = 0.1;
const float slewMax = 10000.0;

// Amount of extra slew per voltage difference
const float shapeScale = 1.0/10.0;

float speed = params[SPEED_PARAM].value;
float slew = slewMax * powf(slewMin / slewMax, speed);

float slew = slewMax * powf(slewRatio, speed);

// Rise
if (target > current) {
current += slew * crossfade(1.0f, shapeScale * (target - current), shape) * engineGetSampleTime();
current += slew * crossfade(1.0f, shapeScale * (target - current), shape) * delta;
if (current > target) // Trap overshoot
current = target;
}
// Fall
else if (target < current) {
current -= slew * crossfade(1.0f, shapeScale * (current - target), shape) * engineGetSampleTime();
current -= slew * crossfade(1.0f, shapeScale * (current - target), shape) * delta;
if (current < target) // Trap overshoot
current = target;
}
Expand Down
7 changes: 5 additions & 2 deletions src/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ struct ParamEvent {
struct AHModule : Module {

float delta;
float rho;

AHModule(int numParams, int numInputs, int numOutputs, int numLights = 0) : Module(numParams, numInputs, numOutputs, numLights) {
delta = 1.0 / engineGetSampleRate();
delta = engineGetSampleTime();
rho = engineGetSampleRate();
}

void onSampleRateChange() override {
delta = 1.0 / engineGetSampleRate();
delta = engineGetSampleTime();
rho = engineGetSampleRate();
}

int stepX = 0;
Expand Down

0 comments on commit 5afecea

Please sign in to comment.