Browse Source

Refactoring, remove "f" and "i" prefixes from math.hpp functions

tags/v0.6.0
Andrew Belt 6 years ago
parent
commit
9a443f07c8
23 changed files with 122 additions and 92 deletions
  1. +11
    -2
      include/app.hpp
  2. +14
    -12
      include/componentlibrary.hpp
  3. +1
    -1
      include/dsp/filter.hpp
  4. +1
    -1
      include/dsp/ringbuffer.hpp
  5. +1
    -1
      include/dsp/vumeter.hpp
  6. +9
    -7
      include/util/common.hpp
  7. +61
    -44
      include/util/math.hpp
  8. +4
    -4
      src/app/LightWidget.cpp
  9. +1
    -1
      src/app/ModuleLightWidget.cpp
  10. +1
    -1
      src/app/ParamWidget.cpp
  11. +2
    -2
      src/app/RackWidget.cpp
  12. +1
    -1
      src/app/SVGFader.cpp
  13. +1
    -1
      src/app/SVGKnob.cpp
  14. +1
    -1
      src/app/SVGPanel.cpp
  15. +2
    -2
      src/app/SVGSwitch.cpp
  16. +1
    -1
      src/app/SpriteKnob.cpp
  17. +2
    -2
      src/audio.cpp
  18. +2
    -2
      src/core/MidiClockToCV.cpp
  19. +1
    -1
      src/settings.cpp
  20. +1
    -1
      src/widgets/ProgressBar.cpp
  21. +1
    -1
      src/widgets/QuantityWidget.cpp
  22. +1
    -1
      src/widgets/Slider.cpp
  23. +2
    -2
      src/widgets/TextField.cpp

+ 11
- 2
include/app.hpp View File

@@ -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);
}




+ 14
- 12
include/componentlibrary.hpp View File

@@ -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;
}
};



+ 1
- 1
include/dsp/filter.hpp View File

@@ -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;
}


+ 1
- 1
include/dsp/ringbuffer.hpp View File

@@ -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));



+ 1
- 1
include/dsp/vumeter.hpp View File

@@ -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);
}
}
};


+ 9
- 7
include/util/common.hpp View File

@@ -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 {




+ 61
- 44
include/util/math.hpp View File

@@ -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

+ 4
- 4
src/app/LightWidget.cpp View File

@@ -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);


+ 1
- 1
src/app/ModuleLightWidget.cpp View File

@@ -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);


+ 1
- 1
src/app/ParamWidget.cpp View File

@@ -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) {


+ 2
- 2
src/app/RackWidget.cpp View File

@@ -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));
}
}


+ 1
- 1
src/app/SVGFader.cpp View File

@@ -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();


+ 1
- 1
src/app/SVGKnob.cpp View File

@@ -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));


+ 1
- 1
src/app/SVGPanel.cpp View File

@@ -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;
}


+ 2
- 2
src/app/SVGSwitch.cpp View File

@@ -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);


+ 1
- 1
src/app/SpriteKnob.cpp View File

@@ -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

+ 2
- 2
src/audio.cpp View File

@@ -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");


+ 2
- 2
src/core/MidiClockToCV.cpp View File

@@ -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) {


+ 1
- 1
src/settings.cpp View File

@@ -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);
}



+ 1
- 1
src/widgets/ProgressBar.cpp View File

@@ -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);
}



+ 1
- 1
src/widgets/QuantityWidget.cpp View File

@@ -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);
}


+ 1
- 1
src/widgets/Slider.cpp View File

@@ -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);
}



+ 2
- 2
src/widgets/TextField.cpp View File

@@ -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;
}



Loading…
Cancel
Save