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.
53 lines
1.0 KiB
53 lines
1.0 KiB
#ifndef __UTIL_H__
|
|
#define __UTIL_H__
|
|
|
|
#include <cmath>
|
|
#include <cfloat>
|
|
|
|
#include "../globals.h"
|
|
|
|
#define PIx2 (2 * M_PI)
|
|
|
|
#ifdef USE_LUTS
|
|
|
|
#define NOTE_LUT_SIZE 128
|
|
#define CENT_LUT_SIZE 128
|
|
|
|
extern float noteLUT[NOTE_LUT_SIZE];
|
|
extern float centLUT[CENT_LUT_SIZE];
|
|
|
|
inline float noteToFreq(float note) {
|
|
float intPart, fracPart = modf(note, &intPart);
|
|
int index = (int) intPart;
|
|
return fracPart < FLT_EPSILON ? noteLUT[index] : noteLUT[index] * centLUT[(int) (fracPart * 127.0)];
|
|
}
|
|
|
|
#else
|
|
|
|
inline float noteToFreq(float note) {
|
|
return 440 * pow(2, (note - 69) / 12.0);
|
|
}
|
|
|
|
#endif
|
|
|
|
inline float clamp(float x, float a, float b) {
|
|
return fminf(b, fmaxf(a, x));
|
|
}
|
|
|
|
inline float triangle(float phase) {
|
|
if(phase < M_PI) {
|
|
return phase / M_PI_2 - 1;
|
|
} else {
|
|
return 1 - (phase - M_PI) / M_PI_2;
|
|
}
|
|
}
|
|
|
|
inline float randFrac() {
|
|
return (float) rand() / RAND_MAX;
|
|
}
|
|
|
|
inline float randPhase() {
|
|
return PIx2 * randFrac();
|
|
}
|
|
|
|
#endif |