Skip to content

Commit

Permalink
Range -> Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
dhemery committed Feb 1, 2018
1 parent b93284b commit f057924
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 57 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ add_library(plugin SHARED
src/plugin/dhe-modules.h
src/stage/stage.cpp
src/stage/stage.h
src/stage/stage-widget.cpp src/stage/stage-widget.h src/util/range.h src/upstage/upstage.h)
src/stage/stage-widget.cpp src/stage/stage-widget.h src/util/interval.h src/upstage/upstage.h)
4 changes: 2 additions & 2 deletions src/stage/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void Stage::start_envelope() {
}

float Stage::duration() const {
static const Range range{1e-3, 10.0f};
static const Interval range{1e-3, 10.0f};
static constexpr float curvature{0.8f}; // Gives ~1s at center position

return range.scale(sigmoid(duration_knob_rotation(), curvature));
Expand All @@ -63,6 +63,6 @@ float Stage::shape() const {
float Stage::envelope_voltage() const {
auto shaped{sigmoid(envelope_ramp.phase(), shape())};

return Range::scale(shaped, stage_input_follower.value(), level_knob_voltage());
return Interval::scale(shaped, stage_input_follower.value(), level_knob_voltage());
}
} // namespace DHE
6 changes: 3 additions & 3 deletions src/stage/stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ struct Stage : rack::Module {
void start_envelope();
float envelope_voltage() const;

float active_out_voltage() const { return UNIPOLAR_VOLTAGE.scale(is_active()); }
float active_out_voltage() const { return UNIPOLAR_CV.scale(is_active()); }
float duration_knob_rotation() const { return params[DURATION_KNOB].value; }
float eoc_out_voltage() const { return UNIPOLAR_VOLTAGE.scale(end_of_cycle_pulse.is_active()); }
float eoc_out_voltage() const { return UNIPOLAR_CV.scale(end_of_cycle_pulse.is_active()); }
bool is_active() const { return defer_gate.is_high() || envelope_ramp.is_active(); }
float level_knob_rotation() const { return params[LEVEL_KNOB].value; }
float level_knob_voltage() const { return UNIPOLAR_VOLTAGE.scale(level_knob_rotation()); }
float level_knob_voltage() const { return UNIPOLAR_CV.scale(level_knob_rotation()); }
float out_voltage() const { return defer_gate.is_high() ? stage_input_follower.value() : envelope_voltage(); }
float shape_knob_rotation() const { return params[SHAPE_KNOB].value; }
float shape_position() const { return BIPOLAR_NORMAL.scale(shape_knob_rotation()); }
Expand Down
8 changes: 4 additions & 4 deletions src/upstage/upstage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <algorithm>
#include <engine.hpp>

#include "util/range.h"
#include "util/interval.h"

namespace DHE {

Expand Down Expand Up @@ -40,10 +40,10 @@ struct Upstage : rack::Module {
bool is_sending_triggers() const { return wait_port_in() < 1.0f and not wait_button_is_pressed(); }
float level_cv_in() const { return inputs[LEVEL_CV_INPUT].value; }
float level_knob_rotation() const { return params[LEVEL_KNOB].value; }
float level_knob_voltage() const { return UNIPOLAR_VOLTAGE.scale(level_knob_rotation()); }
float level_voltage() const { return UNIPOLAR_VOLTAGE.clamp(level_knob_voltage() + level_cv_in()); }
float level_knob_voltage() const { return UNIPOLAR_CV.scale(level_knob_rotation()); }
float level_voltage() const { return UNIPOLAR_CV.clamp(level_knob_voltage() + level_cv_in()); }
bool trigger_button_is_pressed() const { return params[TRIG_BUTTON].value > 0.0f; }
float trigger_button_voltage() const { return UNIPOLAR_VOLTAGE.scale(trigger_button_is_pressed()); }
float trigger_button_voltage() const { return UNIPOLAR_CV.scale(trigger_button_is_pressed()); }
float trigger_port_in() const { return inputs[TRIG_INPUT].value; }
float trigger_out_voltage() const { return is_sending_triggers() ? trigger_voltage() : 0.0f; }
float trigger_voltage() const { return std::max(trigger_port_in(), trigger_button_voltage()); }
Expand Down
42 changes: 42 additions & 0 deletions src/util/interval.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef DHE_UTIL_RANGE_H
#define DHE_UTIL_RANGE_H

namespace DHE {

struct Interval {
const float lower_bound;
const float upper_bound;

constexpr Interval(float lower_bound, float upper_bound) noexcept : lower_bound(lower_bound), upper_bound(upper_bound) {}

static float scale(float proportion, float lower_bound, float upper_bound) {
return proportion*(upper_bound - lower_bound) + lower_bound;
}

float scale(float proportion) const {
return scale(proportion, lower_bound, upper_bound);
}

float scale(bool state) const {
return state ? upper_bound : lower_bound;
}

float normalize(float member) const {
return (member - lower_bound)/(upper_bound - lower_bound);
}

float clamp(float f) const {
if (f < lower_bound)
return lower_bound;
if (f > upper_bound)
return upper_bound;
return f;
}
};

constexpr auto NORMAL = Interval{0.0f, 1.0f};
constexpr auto BIPOLAR_NORMAL = Interval{-1.0f, 1.0f};
constexpr auto UNIPOLAR_CV = Interval{0.0f, 10.0f};
constexpr auto BIPOLAR_CV = Interval{-5.0f, 5.0f};
}
#endif
45 changes: 0 additions & 45 deletions src/util/range.h

This file was deleted.

4 changes: 2 additions & 2 deletions src/util/sigmoid.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <cmath>
#include <functional>

#include "range.h"
#include "interval.h"

namespace DHE {

inline float sigmoid(float x, float curvature) {
static constexpr auto precision{1e-4f};
static constexpr auto max_curvature{1.0f - precision};
static const auto curvature_range = Range{-max_curvature, max_curvature};
static const auto curvature_range = Interval{-max_curvature, max_curvature};

curvature = curvature_range.clamp(curvature);
x = BIPOLAR_NORMAL.clamp(x);
Expand Down

0 comments on commit f057924

Please sign in to comment.