forked from klang-modular/synthapp
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.
65 lines
1.3 KiB
65 lines
1.3 KiB
#ifndef __UTIL_H__
|
|
#define __UTIL_H__
|
|
|
|
#include <cstdio>
|
|
#include <cmath>
|
|
#include <cfloat>
|
|
#include <chrono>
|
|
|
|
#include "../globals.h"
|
|
|
|
#define PIx2 (2 * M_PI)
|
|
#define PIx2_INV (1.0 / PIx2)
|
|
|
|
#define CV_FREQ_MIN 32.703195662574829 // MIDI note 24 (C1)
|
|
|
|
inline float noteToCV(float note) {
|
|
return (note - 24) / 12.f;
|
|
}
|
|
|
|
inline float clamp(float x, float a, float b) {
|
|
return fminf(b, fmaxf(a, x));
|
|
}
|
|
|
|
inline float triangle(float phase) {
|
|
if(phase < (float) M_PI) {
|
|
return phase / (float) M_PI_2 - 1;
|
|
} else {
|
|
return 1 - (phase - (float) M_PI) / (float) M_PI_2;
|
|
}
|
|
}
|
|
|
|
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 / (float) 0x100000000;
|
|
x2 += x1;
|
|
|
|
return value;
|
|
}
|
|
|
|
inline float randPhase() {
|
|
return (float) PIx2 * randFrac();
|
|
}
|
|
|
|
// Get time stamp in nanoseconds.
|
|
inline uint64_t nanos() {
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
std::chrono::high_resolution_clock::now().time_since_epoch())
|
|
.count();
|
|
}
|
|
|
|
#endif |