@@ -4,7 +4,8 @@ | |||
#include "widgets.hpp" | |||
#define SVG_DPI 75.0 | |||
static const float SVG_DPI = 75.0; | |||
static const float MM_PER_IN = 25.4; | |||
#define CHECKMARK_STRING "✔" | |||
#define CHECKMARK(_cond) ((_cond) ? CHECKMARK_STRING : "") | |||
@@ -12,12 +13,20 @@ | |||
namespace rack { | |||
inline float in2px(float inches) { | |||
return inches * SVG_DPI; | |||
} | |||
inline Vec in2px(Vec inches) { | |||
return inches.mult(SVG_DPI); | |||
} | |||
inline float mm2px(float millimeters) { | |||
return millimeters * (SVG_DPI / MM_PER_IN); | |||
} | |||
inline Vec mm2px(Vec millimeters) { | |||
return millimeters.mult(SVG_DPI / 25.4); | |||
return millimeters.mult(SVG_DPI / MM_PER_IN); | |||
} | |||
@@ -10,16 +10,18 @@ namespace rack { | |||
// Colors | |||
//////////////////// | |||
#define COLOR_BLACK_TRANSPARENT nvgRGBA(0x00, 0x00, 0x00, 0x00) | |||
#define COLOR_BLACK nvgRGB(0x00, 0x00, 0x00) | |||
#define COLOR_WHITE nvgRGB(0xff, 0xff, 0xff) | |||
#define COLOR_RED nvgRGB(0xed, 0x2c, 0x24) | |||
#define COLOR_ORANGE nvgRGB(0xf2, 0xb1, 0x20) | |||
#define COLOR_YELLOW nvgRGB(0xf9, 0xdf, 0x1c) | |||
#define COLOR_GREEN nvgRGB(0x90, 0xc7, 0x3e) | |||
#define COLOR_CYAN nvgRGB(0x22, 0xe6, 0xef) | |||
#define COLOR_BLUE nvgRGB(0x29, 0xb2, 0xef) | |||
#define COLOR_PURPLE nvgRGB(0xd5, 0x2b, 0xed) | |||
static const NVGcolor COLOR_BLACK_TRANSPARENT = nvgRGBA(0x00, 0x00, 0x00, 0x00); | |||
static const NVGcolor COLOR_BLACK = nvgRGB(0x00, 0x00, 0x00); | |||
static const NVGcolor COLOR_WHITE = nvgRGB(0xff, 0xff, 0xff); | |||
static const NVGcolor COLOR_RED = nvgRGB(0xed, 0x2c, 0x24); | |||
static const NVGcolor COLOR_ORANGE = nvgRGB(0xf2, 0xb1, 0x20); | |||
static const NVGcolor COLOR_YELLOW = nvgRGB(0xf9, 0xdf, 0x1c); | |||
static const NVGcolor COLOR_GREEN = nvgRGB(0x90, 0xc7, 0x3e); | |||
static const NVGcolor COLOR_CYAN = nvgRGB(0x22, 0xe6, 0xef); | |||
static const NVGcolor COLOR_BLUE = nvgRGB(0x29, 0xb2, 0xef); | |||
static const NVGcolor COLOR_PURPLE = nvgRGB(0xd5, 0x2b, 0xed); | |||
static const NVGcolor COLOR_LIGHT_PANEL = nvgRGB(0xe6, 0xe6, 0xe6); | |||
static const NVGcolor COLOR_DARK_PANEL = nvgRGB(0x17, 0x17, 0x17); | |||
//////////////////// | |||
// Knobs | |||
@@ -553,13 +555,13 @@ struct ScrewBlack : SVGScrew { | |||
struct LightPanel : Panel { | |||
LightPanel() { | |||
backgroundColor = nvgRGB(0xe6, 0xe6, 0xe6); | |||
backgroundColor = COLOR_LIGHT_PANEL; | |||
} | |||
}; | |||
struct DarkPanel : Panel { | |||
DarkPanel() { | |||
backgroundColor = nvgRGB(0x17, 0x17, 0x17); | |||
backgroundColor = COLOR_DARK_PANEL; | |||
} | |||
}; | |||
@@ -57,7 +57,7 @@ struct SlewLimiter { | |||
fall = _fall; | |||
} | |||
float process(float in) { | |||
float delta = clampf(in - out, -fall, rise); | |||
float delta = clamp(in - out, -fall, rise); | |||
out += delta; | |||
return out; | |||
} | |||
@@ -89,7 +89,7 @@ struct DoubleRingBuffer { | |||
void endIncr(int n) { | |||
int e = mask(end); | |||
int e1 = e + n; | |||
int e2 = mini(e1, S); | |||
int e2 = min(e1, S); | |||
// Copy data forward | |||
memcpy(data + S + e, data + e, sizeof(T) * (e2 - e)); | |||
@@ -23,7 +23,7 @@ struct VUMeter { | |||
return (dBScaled >= 0.0) ? 1.0 : 0.0; | |||
} | |||
else { | |||
return clampf(dBScaled + i, 0.0, 1.0); | |||
return clamp(dBScaled + i, 0.0, 1.0); | |||
} | |||
} | |||
}; | |||
@@ -7,13 +7,11 @@ | |||
#include <string.h> | |||
#include <assert.h> | |||
#include <cmath> | |||
#include <string> | |||
#include <condition_variable> | |||
#include <mutex> | |||
// Include math utilities for convenience | |||
#include "util/math.hpp" | |||
/** Surrounds raw text with quotes | |||
Example: | |||
@@ -34,20 +32,24 @@ will expand to | |||
#define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0])) | |||
/** Reserve space for _count enums starting with _name. | |||
/** Reserve space for `count` enums starting with `name`. | |||
Example: | |||
enum Foo { | |||
ENUMS(BAR, 14) | |||
ENUMS(BAR, 14), | |||
BAZ | |||
}; | |||
BAR + 0 to BAR + 11 is reserved | |||
BAR + 0 to BAR + 13 is reserved. BAZ has a value of 14. | |||
*/ | |||
#define ENUMS(_name, _count) _name, _name ## _LAST = _name + (_count) - 1 | |||
#define ENUMS(name, count) name, name ## _LAST = name + (count) - 1 | |||
/** Deprecation notice for GCC */ | |||
#define DEPRECATED __attribute__ ((deprecated)) | |||
#include "util/math.hpp" | |||
namespace rack { | |||
@@ -1,7 +1,5 @@ | |||
#pragma once | |||
#include <stdint.h> | |||
#include <stdlib.h> | |||
#include <cmath> | |||
#include "util/common.hpp" | |||
namespace rack { | |||
@@ -10,39 +8,38 @@ namespace rack { | |||
// basic integer functions (suffixed with "i") | |||
//////////////////// | |||
inline int mini(int a, int b) { | |||
return a < b ? a : b; | |||
inline int min(int a, int b) { | |||
return (a < b) ? a : b; | |||
} | |||
inline int maxi(int a, int b) { | |||
return a > b ? a : b; | |||
inline int max(int a, int b) { | |||
return (a > b) ? a : b; | |||
} | |||
/** Limits a value between a minimum and maximum | |||
Assumes min <= max | |||
*/ | |||
inline int clampi(int x, int min, int max) { | |||
if (x < min) | |||
return min; | |||
if (x > max) | |||
return max; | |||
return x; | |||
inline int clamp(int x, int minimum, int maximum) { | |||
return min(max(x, minimum), maximum); | |||
} | |||
inline int absi(int a) { | |||
/** Absolute value of a | |||
Undefined for a == INT_MIN | |||
*/ | |||
inline int abs(int a) { | |||
return (a >= 0) ? a : -a; | |||
} | |||
/** Euclidean modulus, always returns 0 <= mod < base for positive base. | |||
*/ | |||
inline int eucmodi(int a, int base) { | |||
inline int eucmod(int a, int base) { | |||
int mod = a % base; | |||
return (mod >= 0) ? mod : mod + base; | |||
} | |||
/** Returns floor(log_2(n)), or 0 if n == 1. | |||
*/ | |||
inline int log2i(int n) { | |||
inline int log2(int n) { | |||
int i = 0; | |||
while (n >>= 1) { | |||
i++; | |||
@@ -50,7 +47,7 @@ inline int log2i(int n) { | |||
return i; | |||
} | |||
inline bool ispow2i(int n) { | |||
inline bool ispow2(int n) { | |||
return n > 0 && (n & (n - 1)) == 0; | |||
} | |||
@@ -58,69 +55,65 @@ inline bool ispow2i(int n) { | |||
// basic float functions (suffixed with "f") | |||
//////////////////// | |||
inline float absf(float x) { | |||
return (x < 0.f) ? -x : x; | |||
inline float abs(float x) { | |||
return (x >= 0.f) ? x : -x; | |||
} | |||
/** Returns 1.f for positive numbers and -1.f for negative numbers (including positive/negative zero) */ | |||
inline float sgnf(float x) { | |||
inline float sgn(float x) { | |||
return copysignf(1.f, x); | |||
} | |||
inline float eucmodf(float a, float base) { | |||
inline float eucmod(float a, float base) { | |||
float mod = fmodf(a, base); | |||
return (mod >= 0.f) ? mod : mod + base; | |||
} | |||
inline float nearf(float a, float b, float epsilon = 1e-6) { | |||
inline float near(float a, float b, float epsilon = 1e-6) { | |||
return fabsf(a - b) <= epsilon; | |||
} | |||
/** Limits a value between a minimum and maximum | |||
Assumes min <= max | |||
*/ | |||
inline float clampf(float x, float min, float max) { | |||
if (x < min) | |||
return min; | |||
if (x > max) | |||
return max; | |||
return x; | |||
inline float clamp(float x, float minimum, float maximum) { | |||
return fminf(fmaxf(x, minimum), maximum); | |||
} | |||
/** Limits a value between a minimum and maximum | |||
If min > max, switches the two values | |||
*/ | |||
inline float clamp2f(float x, float min, float max) { | |||
return clampf(x, fminf(min, max), fmaxf(min, max)); | |||
inline float clamp2(float x, float min, float max) { | |||
return clamp(x, fminf(min, max), fmaxf(min, max)); | |||
} | |||
/** If the magnitude of x if less than eps, return 0 */ | |||
inline float chopf(float x, float eps) { | |||
inline float chop(float x, float eps) { | |||
return (-eps < x && x < eps) ? 0.f : x; | |||
} | |||
inline float rescalef(float x, float xMin, float xMax, float yMin, float yMax) { | |||
inline float rescale(float x, float xMin, float xMax, float yMin, float yMax) { | |||
return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin); | |||
} | |||
inline float crossf(float a, float b, float frac) { | |||
inline float crossfade(float a, float b, float frac) { | |||
return a + frac * (b - a); | |||
} | |||
/** Linearly interpolate an array `p` with index `x` | |||
Assumes that the array at `p` is of length at least floor(x)+1. | |||
*/ | |||
inline float interpf(const float *p, float x) { | |||
inline float interp(const float *p, float x) { | |||
int xi = x; | |||
float xf = x - xi; | |||
return crossf(p[xi], p[xi+1], xf); | |||
return crossfade(p[xi], p[xi+1], xf); | |||
} | |||
/** Complex multiply c = a * b | |||
Arguments may be the same pointers | |||
i.e. cmultf(&ar, &ai, ar, ai, br, bi) | |||
*/ | |||
inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) { | |||
inline void cmult(float *cr, float *ci, float ar, float ai, float br, float bi) { | |||
*cr = ar * br - ai * bi; | |||
*ci = ar * bi + ai * br; | |||
} | |||
@@ -235,18 +228,18 @@ struct Rect { | |||
/** Clamps the edges of the rectangle to fit within a bound */ | |||
Rect clamp(Rect bound) { | |||
Rect r; | |||
r.pos.x = clamp2f(pos.x, bound.pos.x, bound.pos.x + bound.size.x); | |||
r.pos.y = clamp2f(pos.y, bound.pos.y, bound.pos.y + bound.size.y); | |||
r.size.x = clampf(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x; | |||
r.size.y = clampf(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y; | |||
r.pos.x = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x); | |||
r.pos.y = clamp2(pos.y, bound.pos.y, bound.pos.y + bound.size.y); | |||
r.size.x = rack::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x; | |||
r.size.y = rack::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y; | |||
return r; | |||
} | |||
/** Nudges the position to fix inside a bounding box */ | |||
Rect nudge(Rect bound) { | |||
Rect r; | |||
r.size = size; | |||
r.pos.x = clamp2f(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x); | |||
r.pos.y = clamp2f(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y); | |||
r.pos.x = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x); | |||
r.pos.y = clamp2(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y); | |||
return r; | |||
} | |||
/** Expands this Rect to contain `other` */ | |||
@@ -269,9 +262,33 @@ struct Rect { | |||
inline Vec Vec::clamp(Rect bound) { | |||
return Vec( | |||
clamp2f(x, bound.pos.x, bound.pos.x + bound.size.x), | |||
clamp2f(y, bound.pos.y, bound.pos.y + bound.size.y)); | |||
clamp2(x, bound.pos.x, bound.pos.x + bound.size.x), | |||
clamp2(y, bound.pos.y, bound.pos.y + bound.size.y)); | |||
} | |||
//////////////////// | |||
// Deprecated functions, will by removed in Rack 1.0 | |||
//////////////////// | |||
inline int DEPRECATED mini(int a, int b) {return min(a, b);} | |||
inline int DEPRECATED maxi(int a, int b) {return max(a, b);} | |||
inline int DEPRECATED clampi(int x, int min, int max) {return clamp(x, min, max);} | |||
inline int DEPRECATED absi(int a) {return abs(a);} | |||
inline int DEPRECATED eucmodi(int a, int base) {return eucmod(a, base);} | |||
inline int DEPRECATED log2i(int n) {return log2(n);} | |||
inline bool DEPRECATED ispow2i(int n) {return ispow2(n);} | |||
inline float DEPRECATED absf(float x) {return abs(x);} | |||
inline float DEPRECATED sgnf(float x) {return sgn(x);} | |||
inline float DEPRECATED eucmodf(float a, float base) {return eucmod(a, base);} | |||
inline float DEPRECATED nearf(float a, float b, float epsilon = 1e-6) {return near(a, b, epsilon);} | |||
inline float DEPRECATED clampf(float x, float min, float max) {return clamp(x, min, max);} | |||
inline float DEPRECATED clamp2f(float x, float min, float max) {return clamp2(x, min, max);} | |||
inline float DEPRECATED chopf(float x, float eps) {return chop(x, eps);} | |||
inline float DEPRECATED rescalef(float x, float xMin, float xMax, float yMin, float yMax) {return rescale(x, xMin, xMax, yMin, yMax);} | |||
inline float DEPRECATED crossf(float a, float b, float frac) {return crossfade(a, b, frac);} | |||
inline float DEPRECATED interpf(const float *p, float x) {return interp(p, x);} | |||
inline void DEPRECATED cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {return cmult(cr, ci, ar, ai, br, bi);} | |||
} // namespace rack |
@@ -5,10 +5,10 @@ namespace rack { | |||
void LightWidget::draw(NVGcontext *vg) { | |||
color.r = clampf(color.r, 0.0, 1.0); | |||
color.g = clampf(color.g, 0.0, 1.0); | |||
color.b = clampf(color.b, 0.0, 1.0); | |||
color.a = clampf(color.a, 0.0, 1.0); | |||
color.r = clamp(color.r, 0.0, 1.0); | |||
color.g = clamp(color.g, 0.0, 1.0); | |||
color.b = clamp(color.b, 0.0, 1.0); | |||
color.a = clamp(color.a, 0.0, 1.0); | |||
drawLight(vg); | |||
drawHalo(vg); | |||
@@ -12,7 +12,7 @@ void ModuleLightWidget::step() { | |||
for (size_t i = 0; i < baseColors.size(); i++) { | |||
float value = module->lights[firstLightId + i].getBrightness(); | |||
value = clampf(value, 0.0, 1.0); | |||
value = clamp(value, 0.0, 1.0); | |||
values[i] = value; | |||
} | |||
setValues(values); | |||
@@ -20,7 +20,7 @@ void ParamWidget::fromJson(json_t *rootJ) { | |||
void ParamWidget::randomize() { | |||
if (randomizable) | |||
setValue(rescalef(randomf(), 0.0, 1.0, minValue, maxValue)); | |||
setValue(rescale(randomf(), 0.0, 1.0, minValue, maxValue)); | |||
} | |||
void ParamWidget::onMouseDown(EventMouseDown &e) { | |||
@@ -374,8 +374,8 @@ bool RackWidget::requestModuleBoxNearest(ModuleWidget *m, Rect box) { | |||
int x0 = roundf(box.pos.x / RACK_GRID_WIDTH); | |||
int y0 = roundf(box.pos.y / RACK_GRID_HEIGHT); | |||
std::vector<Vec> positions; | |||
for (int y = maxi(0, y0 - 8); y < y0 + 8; y++) { | |||
for (int x = maxi(0, x0 - 400); x < x0 + 400; x++) { | |||
for (int y = max(0, y0 - 8); y < y0 + 8; y++) { | |||
for (int x = max(0, x0 - 400); x < x0 + 400; x++) { | |||
positions.push_back(Vec(x * RACK_GRID_WIDTH, y * RACK_GRID_HEIGHT)); | |||
} | |||
} | |||
@@ -15,7 +15,7 @@ SVGFader::SVGFader() { | |||
void SVGFader::step() { | |||
if (dirty) { | |||
// Update handle position | |||
Vec handlePos = Vec(rescalef(value, minValue, maxValue, minHandlePos.x, maxHandlePos.x), rescalef(value, minValue, maxValue, minHandlePos.y, maxHandlePos.y)); | |||
Vec handlePos = Vec(rescale(value, minValue, maxValue, minHandlePos.x, maxHandlePos.x), rescale(value, minValue, maxValue, minHandlePos.y, maxHandlePos.y)); | |||
handle->box.pos = handlePos; | |||
} | |||
FramebufferWidget::step(); | |||
@@ -25,7 +25,7 @@ void SVGKnob::step() { | |||
tw->box.size = box.size; | |||
float angle = 0.0; | |||
if (std::isfinite(minValue) && std::isfinite(maxValue)) | |||
angle = rescalef(value, minValue, maxValue, minAngle, maxAngle); | |||
angle = rescale(value, minValue, maxValue, minAngle, maxAngle); | |||
tw->identity(); | |||
// Scale SVG to box | |||
tw->scale(box.size.div(sw->box.size)); | |||
@@ -18,7 +18,7 @@ struct PanelBorder : TransparentWidget { | |||
void SVGPanel::step() { | |||
if (nearf(gPixelRatio, 1.0)) { | |||
if (near(gPixelRatio, 1.0)) { | |||
// Small details draw poorly at low DPI, so oversample when drawing to the framebuffer | |||
oversample = 2.0; | |||
} | |||
@@ -20,8 +20,8 @@ void SVGSwitch::addFrame(std::shared_ptr<SVG> svg) { | |||
void SVGSwitch::onChange(EventChange &e) { | |||
assert(frames.size() > 0); | |||
float valueScaled = rescalef(value, minValue, maxValue, 0, frames.size() - 1); | |||
int index = clampi((int) roundf(valueScaled), 0, frames.size() - 1); | |||
float valueScaled = rescale(value, minValue, maxValue, 0, frames.size() - 1); | |||
int index = clamp((int) roundf(valueScaled), 0, frames.size() - 1); | |||
sw->setSVG(frames[index]); | |||
dirty = true; | |||
Switch::onChange(e); | |||
@@ -4,7 +4,7 @@ | |||
namespace rack { | |||
void SpriteKnob::step() { | |||
index = eucmodi((int) roundf(rescalef(value, minValue, maxValue, minIndex, maxIndex)), spriteCount); | |||
index = eucmod((int) roundf(rescale(value, minValue, maxValue, minIndex, maxIndex)), spriteCount); | |||
} | |||
} // namespace rack |
@@ -132,8 +132,8 @@ void AudioIO::openStream() { | |||
return; | |||
} | |||
numOutputs = mini(deviceInfo.outputChannels, maxOutputs); | |||
numInputs = mini(deviceInfo.inputChannels, maxInputs); | |||
numOutputs = min(deviceInfo.outputChannels, maxOutputs); | |||
numInputs = min(deviceInfo.inputChannels, maxInputs); | |||
if (numOutputs == 0 && numInputs == 0) { | |||
warn("RtAudio device %d has 0 inputs and 0 outputs"); | |||
@@ -108,11 +108,11 @@ void MIDIClockToCVInterface::step() { | |||
} | |||
if (inputs[CLOCK1_RATIO].active) { | |||
clock1ratio = int(clampf(inputs[CLOCK1_RATIO].value, 0.0, 10.0) * (numratios - 1) / 10); | |||
clock1ratio = int(clamp(inputs[CLOCK1_RATIO].value, 0.0, 10.0) * (numratios - 1) / 10); | |||
} | |||
if (inputs[CLOCK2_RATIO].active) { | |||
clock2ratio = int(clampf(inputs[CLOCK2_RATIO].value, 0.0, 10.0) * (numratios - 1) / 10); | |||
clock2ratio = int(clamp(inputs[CLOCK2_RATIO].value, 0.0, 10.0) * (numratios - 1) / 10); | |||
} | |||
if (start) { | |||
@@ -102,7 +102,7 @@ static void settingsFromJson(json_t *rootJ) { | |||
// zoom | |||
json_t *zoomJ = json_object_get(rootJ, "zoom"); | |||
if (zoomJ) { | |||
gRackScene->zoomWidget->setZoom(clampf(json_number_value(zoomJ), 0.25, 4.0)); | |||
gRackScene->zoomWidget->setZoom(clamp(json_number_value(zoomJ), 0.25f, 4.0f)); | |||
gToolbar->zoomSlider->setValue(json_number_value(zoomJ) * 100.0); | |||
} | |||
@@ -4,7 +4,7 @@ | |||
namespace rack { | |||
void ProgressBar::draw(NVGcontext *vg) { | |||
float progress = rescalef(value, minValue, maxValue, 0.0, 1.0); | |||
float progress = rescale(value, minValue, maxValue, 0.0, 1.0); | |||
bndSlider(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_ALL, BND_DEFAULT, progress, getText().c_str(), NULL); | |||
} | |||
@@ -9,7 +9,7 @@ QuantityWidget::QuantityWidget() { | |||
} | |||
void QuantityWidget::setValue(float value) { | |||
this->value = clampf(value, fminf(minValue, maxValue), fmaxf(minValue, maxValue)); | |||
this->value = clamp(value, fminf(minValue, maxValue), fmaxf(minValue, maxValue)); | |||
EventChange e; | |||
onChange(e); | |||
} | |||
@@ -7,7 +7,7 @@ namespace rack { | |||
#define SLIDER_SENSITIVITY 0.001 | |||
void Slider::draw(NVGcontext *vg) { | |||
float progress = rescalef(value, minValue, maxValue, 0.0, 1.0); | |||
float progress = rescale(value, minValue, maxValue, 0.0, 1.0); | |||
bndSlider(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, progress, getText().c_str(), NULL); | |||
} | |||
@@ -117,8 +117,8 @@ void TextField::onKey(EventKey &e) { | |||
break; | |||
} | |||
begin = mini(maxi(begin, 0), text.size()); | |||
end = mini(maxi(end, 0), text.size()); | |||
begin = clamp(begin, 0, text.size()); | |||
end = clamp(end, 0, text.size()); | |||
e.consumed = true; | |||
} | |||