You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.4 KiB
69 lines
1.4 KiB
#ifndef __UTIL_H__
|
|
#define __UTIL_H__
|
|
|
|
#include <cmath>
|
|
#include <cfloat>
|
|
|
|
#include "globals.h"
|
|
|
|
#define unlikely(cond) __builtin_expect((cond), 0)
|
|
#define likely(cond) __builtin_expect((cond), 1)
|
|
|
|
#define PIx2 (2 * M_PI)
|
|
#define PIx2_INV (1.0 / PIx2)
|
|
|
|
#define CV_FREQ_MIN 32.703195662574829 // MIDI note 24 (C1)
|
|
#define MAX_SAMPLE_PERIOD ((int) (SAMPLE_RATE / CV_FREQ_MIN + 0.5))
|
|
|
|
inline float fakeSin(float x) {
|
|
x = 4.f * (x - 0.5f);
|
|
return x * (2.f - fabsf(x));
|
|
}
|
|
|
|
inline float clamp(float x, float a, float b) {
|
|
return fminf(b, fmaxf(a, x));
|
|
}
|
|
|
|
inline float whiteNoise() {
|
|
static int32_t x1 = 0x70F4F854;
|
|
static int32_t x2 = 0xE1E9F0A7;
|
|
|
|
x1 ^= x2;
|
|
float value = x2 * (2.0f / 0xFFFFFFFF);
|
|
x2 += x1;
|
|
|
|
return value;
|
|
}
|
|
|
|
inline float randFrac() {
|
|
static uint32_t x1 = 0x70F4F854;
|
|
static uint32_t x2 = 0xE1E9F0A7;
|
|
|
|
x1 ^= x2;
|
|
float value = x2 * (1.f / 0x100000000);
|
|
x2 += x1;
|
|
|
|
return value;
|
|
}
|
|
|
|
inline float triangle(float phase) {
|
|
if(phase < 0.5f) {
|
|
return 2.f * phase - 1;
|
|
} else {
|
|
return 1 - 2.f * (phase - 0.5f);
|
|
}
|
|
}
|
|
|
|
inline float cvToStep(float cv) {
|
|
return (float) CV_FREQ_MIN * exp2f(cv) * (float) SAMPLE_RATE_INV;
|
|
}
|
|
|
|
inline float noteToCV(float note) {
|
|
return note / 12.f;
|
|
}
|
|
|
|
inline float cvToPeriod(float cv) {
|
|
return (float) SAMPLE_RATE / ((float) CV_FREQ_MIN * exp2f(cv));
|
|
}
|
|
|
|
#endif |