From 0547a194a9c2edbfc120fd209ffa3e89efd2ca86 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 12 Jan 2019 20:46:43 -0500 Subject: [PATCH] Tweaks to math.hpp and its docstrings --- include/math.hpp | 36 +++++++++++++++++++++--------------- include/rack.hpp | 4 +++- include/rack0.hpp | 2 ++ src/Core/AudioInterface.cpp | 12 ++++++------ src/dsp/minblep.cpp | 4 +++- src/ui/ScrollWidget.cpp | 2 +- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/include/math.hpp b/include/math.hpp index a379d2b4..db4b5099 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -6,6 +6,7 @@ namespace rack { namespace math { + //////////////////// // basic integer functions //////////////////// @@ -30,12 +31,13 @@ inline int clamp(int x, int a, int b) { /** Limits `x` between `a` and `b` If a > b, switches the two values */ -inline int clampBetween(int x, int a, int b) { +inline int clampSafe(int x, int a, int b) { return clamp(x, std::min(a, b), std::max(a, b)); } /** Euclidean modulus. Always returns 0 <= mod < b. b must be positive. +See https://en.wikipedia.org/wiki/Euclidean_division */ inline int eucMod(int a, int b) { int mod = a % b; @@ -76,6 +78,7 @@ inline int log2(int n) { return i; } +/** Returns whether `n` is a power of 2 */ inline bool isPow2(int n) { return n > 0 && (n & (n - 1)) == 0; } @@ -94,11 +97,13 @@ inline float clamp(float x, float a, float b) { /** Limits `x` between `a` and `b` If a > b, switches the two values */ -inline float clampBetween(float x, float a, float b) { +inline float clampSafe(float x, float a, float b) { return clamp(x, std::min(a, b), std::max(a, b)); } -/** Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero */ +/** Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero +See https://en.wikipedia.org/wiki/Sign_function +*/ inline float sgn(float x) { return x > 0.f ? 1.f : x < 0.f ? -1.f : 0.f; } @@ -108,6 +113,9 @@ inline float normalizeZero(float x) { return x + 0.f; } +/** Euclidean modulus. Always returns 0 <= mod < b. +See https://en.wikipedia.org/wiki/Euclidean_division +*/ inline float eucMod(float a, float base) { float mod = std::fmod(a, base); return (mod >= 0.0f) ? mod : mod + base; @@ -143,11 +151,12 @@ inline float interpolateLinear(const float *p, float x) { Arguments may be the same pointers i.e. cmultf(&ar, &ai, ar, ai, br, bi) */ -inline void cmult(float *cr, float *ci, float ar, float ai, float br, float bi) { +inline void complexMult(float *cr, float *ci, float ar, float ai, float br, float bi) { *cr = ar * br - ai * bi; *ci = ar * bi + ai * br; } + //////////////////// // 2D vector and rectangle //////////////////// @@ -228,8 +237,7 @@ struct Vec { return std::isfinite(x) && std::isfinite(y); } Vec clamp(Rect bound) const; - Vec clampBetween(Rect bound) const; - DEPRECATED Vec clamp2(Rect bound) const; + Vec clampSafe(Rect bound) const; }; @@ -286,8 +294,8 @@ struct Rect { /** Clamps the edges of the rectangle to fit within a bound */ Rect clamp(Rect bound) const { Rect r; - r.pos.x = clampBetween(pos.x, bound.pos.x, bound.pos.x + bound.size.x); - r.pos.y = clampBetween(pos.y, bound.pos.y, bound.pos.y + bound.size.y); + r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x); + r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y); r.size.x = math::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x; r.size.y = math::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y; return r; @@ -296,8 +304,8 @@ struct Rect { Rect nudge(Rect bound) const { Rect r; r.size = size; - r.pos.x = clampBetween(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x); - r.pos.y = clampBetween(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y); + r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x); + r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y); return r; } /** Expands this Rect to contain `other` */ @@ -331,14 +339,12 @@ inline Vec Vec::clamp(Rect bound) const { math::clamp(y, bound.pos.y, bound.pos.y + bound.size.y)); } -inline Vec Vec::clampBetween(Rect bound) const { +inline Vec Vec::clampSafe(Rect bound) const { return Vec( - math::clampBetween(x, bound.pos.x, bound.pos.x + bound.size.x), - math::clampBetween(y, bound.pos.y, bound.pos.y + bound.size.y)); + math::clampSafe(x, bound.pos.x, bound.pos.x + bound.size.x), + math::clampSafe(y, bound.pos.y, bound.pos.y + bound.size.y)); } -inline Vec Vec::clamp2(Rect bound) const {return clampBetween(bound);} - } // namespace math } // namespace rack diff --git a/include/rack.hpp b/include/rack.hpp index 762a71e3..48439aa7 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -92,7 +92,9 @@ namespace rack { -// Import namespace for convenience + +// Import math:: namespace for convenience using namespace math; + } // namespace rack diff --git a/include/rack0.hpp b/include/rack0.hpp index 74564631..0fc7ce83 100644 --- a/include/rack0.hpp +++ b/include/rack0.hpp @@ -16,6 +16,8 @@ namespace rack { // math //////////////////// +using namespace math; + DEPRECATED inline int min(int a, int b) {return std::min(a, b);} DEPRECATED inline int max(int a, int b) {return std::max(a, b);} DEPRECATED inline int eucmod(int a, int base) {return eucMod(a, base);} diff --git a/src/Core/AudioInterface.cpp b/src/Core/AudioInterface.cpp index c51f3d9c..d85594f9 100644 --- a/src/Core/AudioInterface.cpp +++ b/src/Core/AudioInterface.cpp @@ -8,8 +8,8 @@ #include "app.hpp" -#define AUDIO_OUTPUTS 8 -#define AUDIO_INPUTS 8 +static const int AUDIO_OUTPUTS = 8; +static const int AUDIO_INPUTS = 8; using namespace rack; @@ -45,8 +45,8 @@ struct AudioInterfaceIO : audio::IO { if (inputBuffer.full()) break; dsp::Frame inputFrame; - memset(&inputFrame, 0, sizeof(inputFrame)); - memcpy(&inputFrame, &input[numInputs * i], numInputs * sizeof(float)); + std::memset(&inputFrame, 0, sizeof(inputFrame)); + std::memcpy(&inputFrame, &input[numInputs * i], numInputs * sizeof(float)); inputBuffer.push(inputFrame); } } @@ -68,7 +68,7 @@ struct AudioInterfaceIO : audio::IO { } else { // Timed out, fill output with zeros - memset(output, 0, frames * numOutputs * sizeof(float)); + std::memset(output, 0, frames * numOutputs * sizeof(float)); // DEBUG("Audio Interface IO underflow"); } } @@ -180,7 +180,7 @@ void AudioInterface::step() { inputFrame = inputBuffer.shift(); } else { - memset(&inputFrame, 0, sizeof(inputFrame)); + std::memset(&inputFrame, 0, sizeof(inputFrame)); } for (int i = 0; i < audioIO.numInputs; i++) { outputs[AUDIO_OUTPUT + i].setVoltage(10.f * inputFrame.samples[i]); diff --git a/src/dsp/minblep.cpp b/src/dsp/minblep.cpp index 735aaa01..68ca9703 100644 --- a/src/dsp/minblep.cpp +++ b/src/dsp/minblep.cpp @@ -2,6 +2,7 @@ namespace rack { +namespace dsp { // TODO I should probably compute this on launch @@ -10,4 +11,5 @@ const float minblep_16_32[] = { }; -} // namespace rack +} // namespace dsp +} // namespace rack \ No newline at end of file diff --git a/src/ui/ScrollWidget.cpp b/src/ui/ScrollWidget.cpp index 070fbf61..803ba159 100644 --- a/src/ui/ScrollWidget.cpp +++ b/src/ui/ScrollWidget.cpp @@ -23,7 +23,7 @@ ScrollWidget::ScrollWidget() { void ScrollWidget::scrollTo(math::Rect r) { math::Rect bound = math::Rect::fromMinMax(r.getBottomRight().minus(box.size), r.pos); - offset = offset.clampBetween(bound); + offset = offset.clampSafe(bound); } void ScrollWidget::draw(NVGcontext *vg) {