Browse Source

Tweaks to math.hpp and its docstrings

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
0547a194a9
6 changed files with 36 additions and 24 deletions
  1. +21
    -15
      include/math.hpp
  2. +3
    -1
      include/rack.hpp
  3. +2
    -0
      include/rack0.hpp
  4. +6
    -6
      src/Core/AudioInterface.cpp
  5. +3
    -1
      src/dsp/minblep.cpp
  6. +1
    -1
      src/ui/ScrollWidget.cpp

+ 21
- 15
include/math.hpp View File

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

+ 3
- 1
include/rack.hpp View File

@@ -92,7 +92,9 @@

namespace rack {

// Import namespace for convenience

// Import math:: namespace for convenience
using namespace math;


} // namespace rack

+ 2
- 0
include/rack0.hpp View File

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


+ 6
- 6
src/Core/AudioInterface.cpp View File

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


+ 3
- 1
src/dsp/minblep.cpp View File

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

+ 1
- 1
src/ui/ScrollWidget.cpp View File

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


Loading…
Cancel
Save