Skip to content

Commit f057924

Browse files
committed
Range -> Interval
1 parent b93284b commit f057924

File tree

7 files changed

+54
-57
lines changed

7 files changed

+54
-57
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ add_library(plugin SHARED
2222
src/plugin/dhe-modules.h
2323
src/stage/stage.cpp
2424
src/stage/stage.h
25-
src/stage/stage-widget.cpp src/stage/stage-widget.h src/util/range.h src/upstage/upstage.h)
25+
src/stage/stage-widget.cpp src/stage/stage-widget.h src/util/interval.h src/upstage/upstage.h)

src/stage/stage.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void Stage::start_envelope() {
4848
}
4949

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

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

66-
return Range::scale(shaped, stage_input_follower.value(), level_knob_voltage());
66+
return Interval::scale(shaped, stage_input_follower.value(), level_knob_voltage());
6767
}
6868
} // namespace DHE

src/stage/stage.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ struct Stage : rack::Module {
4545
void start_envelope();
4646
float envelope_voltage() const;
4747

48-
float active_out_voltage() const { return UNIPOLAR_VOLTAGE.scale(is_active()); }
48+
float active_out_voltage() const { return UNIPOLAR_CV.scale(is_active()); }
4949
float duration_knob_rotation() const { return params[DURATION_KNOB].value; }
50-
float eoc_out_voltage() const { return UNIPOLAR_VOLTAGE.scale(end_of_cycle_pulse.is_active()); }
50+
float eoc_out_voltage() const { return UNIPOLAR_CV.scale(end_of_cycle_pulse.is_active()); }
5151
bool is_active() const { return defer_gate.is_high() || envelope_ramp.is_active(); }
5252
float level_knob_rotation() const { return params[LEVEL_KNOB].value; }
53-
float level_knob_voltage() const { return UNIPOLAR_VOLTAGE.scale(level_knob_rotation()); }
53+
float level_knob_voltage() const { return UNIPOLAR_CV.scale(level_knob_rotation()); }
5454
float out_voltage() const { return defer_gate.is_high() ? stage_input_follower.value() : envelope_voltage(); }
5555
float shape_knob_rotation() const { return params[SHAPE_KNOB].value; }
5656
float shape_position() const { return BIPOLAR_NORMAL.scale(shape_knob_rotation()); }

src/upstage/upstage.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <algorithm>
99
#include <engine.hpp>
1010

11-
#include "util/range.h"
11+
#include "util/interval.h"
1212

1313
namespace DHE {
1414

@@ -40,10 +40,10 @@ struct Upstage : rack::Module {
4040
bool is_sending_triggers() const { return wait_port_in() < 1.0f and not wait_button_is_pressed(); }
4141
float level_cv_in() const { return inputs[LEVEL_CV_INPUT].value; }
4242
float level_knob_rotation() const { return params[LEVEL_KNOB].value; }
43-
float level_knob_voltage() const { return UNIPOLAR_VOLTAGE.scale(level_knob_rotation()); }
44-
float level_voltage() const { return UNIPOLAR_VOLTAGE.clamp(level_knob_voltage() + level_cv_in()); }
43+
float level_knob_voltage() const { return UNIPOLAR_CV.scale(level_knob_rotation()); }
44+
float level_voltage() const { return UNIPOLAR_CV.clamp(level_knob_voltage() + level_cv_in()); }
4545
bool trigger_button_is_pressed() const { return params[TRIG_BUTTON].value > 0.0f; }
46-
float trigger_button_voltage() const { return UNIPOLAR_VOLTAGE.scale(trigger_button_is_pressed()); }
46+
float trigger_button_voltage() const { return UNIPOLAR_CV.scale(trigger_button_is_pressed()); }
4747
float trigger_port_in() const { return inputs[TRIG_INPUT].value; }
4848
float trigger_out_voltage() const { return is_sending_triggers() ? trigger_voltage() : 0.0f; }
4949
float trigger_voltage() const { return std::max(trigger_port_in(), trigger_button_voltage()); }

src/util/interval.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef DHE_UTIL_RANGE_H
2+
#define DHE_UTIL_RANGE_H
3+
4+
namespace DHE {
5+
6+
struct Interval {
7+
const float lower_bound;
8+
const float upper_bound;
9+
10+
constexpr Interval(float lower_bound, float upper_bound) noexcept : lower_bound(lower_bound), upper_bound(upper_bound) {}
11+
12+
static float scale(float proportion, float lower_bound, float upper_bound) {
13+
return proportion*(upper_bound - lower_bound) + lower_bound;
14+
}
15+
16+
float scale(float proportion) const {
17+
return scale(proportion, lower_bound, upper_bound);
18+
}
19+
20+
float scale(bool state) const {
21+
return state ? upper_bound : lower_bound;
22+
}
23+
24+
float normalize(float member) const {
25+
return (member - lower_bound)/(upper_bound - lower_bound);
26+
}
27+
28+
float clamp(float f) const {
29+
if (f < lower_bound)
30+
return lower_bound;
31+
if (f > upper_bound)
32+
return upper_bound;
33+
return f;
34+
}
35+
};
36+
37+
constexpr auto NORMAL = Interval{0.0f, 1.0f};
38+
constexpr auto BIPOLAR_NORMAL = Interval{-1.0f, 1.0f};
39+
constexpr auto UNIPOLAR_CV = Interval{0.0f, 10.0f};
40+
constexpr auto BIPOLAR_CV = Interval{-5.0f, 5.0f};
41+
}
42+
#endif

src/util/range.h

-45
This file was deleted.

src/util/sigmoid.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
#include <cmath>
55
#include <functional>
66

7-
#include "range.h"
7+
#include "interval.h"
88

99
namespace DHE {
1010

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

1616
curvature = curvature_range.clamp(curvature);
1717
x = BIPOLAR_NORMAL.clamp(x);

0 commit comments

Comments
 (0)