| @@ -15,24 +15,42 @@ using std::isnormal; | |||||
| namespace rack { | namespace rack { | ||||
| //////////////////// | //////////////////// | ||||
| // basic integer functions | |||||
| // generic functions | |||||
| //////////////////// | //////////////////// | ||||
| inline int min(int a, int b) { | |||||
| /** Returns the minimum of `a` and `b` */ | |||||
| template <typename T> | |||||
| inline T min(T a, T b) { | |||||
| return (a < b) ? a : b; | return (a < b) ? a : b; | ||||
| } | } | ||||
| inline int max(int a, int b) { | |||||
| /** Returns the maximum of `a` and `b` */ | |||||
| template <typename T> | |||||
| inline T max(T a, T b) { | |||||
| return (a > b) ? a : b; | return (a > b) ? a : b; | ||||
| } | } | ||||
| /** Limits `x` between `a` and `b` | /** Limits `x` between `a` and `b` | ||||
| Assumes a <= b | Assumes a <= b | ||||
| */ | */ | ||||
| inline int clamp(int x, int a, int b) { | |||||
| template <typename T> | |||||
| inline T clamp(T x, T a, T b) { | |||||
| return min(max(x, a), b); | return min(max(x, a), b); | ||||
| } | } | ||||
| /** Limits `x` between `a` and `b` | |||||
| If a > b, switches the two values | |||||
| */ | |||||
| template <typename T> | |||||
| inline T clamp2(T x, T a, T b) { | |||||
| return clamp(x, min(a, b), max(a, b)); | |||||
| } | |||||
| //////////////////// | |||||
| // basic integer functions | |||||
| //////////////////// | |||||
| /** Euclidean modulus, always returns 0 <= mod < base for positive base. | /** Euclidean modulus, always returns 0 <= mod < base for positive base. | ||||
| */ | */ | ||||
| inline int eucmod(int a, int base) { | inline int eucmod(int a, int base) { | ||||
| @@ -58,14 +76,6 @@ inline bool ispow2(int n) { | |||||
| // basic float functions | // basic float functions | ||||
| //////////////////// | //////////////////// | ||||
| inline float min(float a, float b) { | |||||
| return (a < b) ? a : b; | |||||
| } | |||||
| inline float max(float a, float b) { | |||||
| return (a > b) ? a : b; | |||||
| } | |||||
| /** Returns 1.f for positive numbers and -1.f for negative numbers (including positive/negative zero) */ | /** Returns 1.f for positive numbers and -1.f for negative numbers (including positive/negative zero) */ | ||||
| inline float sgn(float x) { | inline float sgn(float x) { | ||||
| return copysignf(1.0f, x); | return copysignf(1.0f, x); | ||||
| @@ -80,20 +90,6 @@ inline bool isNear(float a, float b, float epsilon = 1.0e-6f) { | |||||
| return fabsf(a - b) <= epsilon; | return fabsf(a - b) <= epsilon; | ||||
| } | } | ||||
| /** Limits `x` between `a` and `b` | |||||
| Assumes a <= b | |||||
| */ | |||||
| inline float clamp(float x, float a, float b) { | |||||
| return min(max(x, a), b); | |||||
| } | |||||
| /** Limits `x` between `a` and `b` | |||||
| If a > b, switches the two values | |||||
| */ | |||||
| inline float clamp2(float x, float a, float b) { | |||||
| return clamp(x, min(a, b), max(a, b)); | |||||
| } | |||||
| /** If the magnitude of x if less than eps, return 0 */ | /** If the magnitude of x if less than eps, return 0 */ | ||||
| inline float chop(float x, float eps) { | inline float chop(float x, float eps) { | ||||
| return (-eps < x && x < eps) ? 0.0f : x; | return (-eps < x && x < eps) ? 0.0f : x; | ||||
| @@ -21,7 +21,7 @@ void SVGSwitch::addFrame(std::shared_ptr<SVG> svg) { | |||||
| void SVGSwitch::onChange(EventChange &e) { | void SVGSwitch::onChange(EventChange &e) { | ||||
| assert(frames.size() > 0); | assert(frames.size() > 0); | ||||
| float valueScaled = rescale(value, minValue, maxValue, 0, frames.size() - 1); | float valueScaled = rescale(value, minValue, maxValue, 0, frames.size() - 1); | ||||
| int index = clamp((int) roundf(valueScaled), 0, frames.size() - 1); | |||||
| int index = clamp((int) roundf(valueScaled), 0, (int) frames.size() - 1); | |||||
| sw->setSVG(frames[index]); | sw->setSVG(frames[index]); | ||||
| dirty = true; | dirty = true; | ||||
| ParamWidget::onChange(e); | ParamWidget::onChange(e); | ||||
| @@ -138,11 +138,11 @@ std::string AudioIO::getDeviceDetail(int device, int offset) { | |||||
| if (getDeviceInfo(device, &deviceInfo)) { | if (getDeviceInfo(device, &deviceInfo)) { | ||||
| std::string deviceDetail = stringf("%s (", deviceInfo.name.c_str()); | std::string deviceDetail = stringf("%s (", deviceInfo.name.c_str()); | ||||
| if (offset < (int) deviceInfo.inputChannels) | if (offset < (int) deviceInfo.inputChannels) | ||||
| deviceDetail += stringf("%d-%d in", offset + 1, min(offset + maxChannels, deviceInfo.inputChannels)); | |||||
| deviceDetail += stringf("%d-%d in", offset + 1, min(offset + maxChannels, (int) deviceInfo.inputChannels)); | |||||
| if (offset < (int) deviceInfo.inputChannels && offset < (int) deviceInfo.outputChannels) | if (offset < (int) deviceInfo.inputChannels && offset < (int) deviceInfo.outputChannels) | ||||
| deviceDetail += ", "; | deviceDetail += ", "; | ||||
| if (offset < (int) deviceInfo.outputChannels) | if (offset < (int) deviceInfo.outputChannels) | ||||
| deviceDetail += stringf("%d-%d out", offset + 1, min(offset + maxChannels, deviceInfo.outputChannels)); | |||||
| deviceDetail += stringf("%d-%d out", offset + 1, min(offset + maxChannels, (int) deviceInfo.outputChannels)); | |||||
| deviceDetail += ")"; | deviceDetail += ")"; | ||||
| return deviceDetail; | return deviceDetail; | ||||
| } | } | ||||
| @@ -105,7 +105,7 @@ static void settingsFromJson(json_t *rootJ) { | |||||
| // zoom | // zoom | ||||
| json_t *zoomJ = json_object_get(rootJ, "zoom"); | json_t *zoomJ = json_object_get(rootJ, "zoom"); | ||||
| if (zoomJ) { | if (zoomJ) { | ||||
| gRackScene->zoomWidget->setZoom(clamp(json_number_value(zoomJ), 0.25f, 4.0f)); | |||||
| gRackScene->zoomWidget->setZoom(clamp((float) json_number_value(zoomJ), 0.25f, 4.0f)); | |||||
| gToolbar->zoomSlider->setValue(json_number_value(zoomJ) * 100.0); | gToolbar->zoomSlider->setValue(json_number_value(zoomJ) * 100.0); | ||||
| } | } | ||||
| @@ -166,8 +166,8 @@ void TextField::onKey(EventKey &e) { | |||||
| } break; | } break; | ||||
| } | } | ||||
| cursor = clamp(cursor, 0, text.size()); | |||||
| selection = clamp(selection, 0, text.size()); | |||||
| cursor = clamp(cursor, 0, (int) text.size()); | |||||
| selection = clamp(selection, 0, (int) text.size()); | |||||
| e.consumed = true; | e.consumed = true; | ||||
| } | } | ||||