psyn/osc.h

60 lines
1.1 KiB
C

#define LOOKUP_SAMPLES 3600
extern double _lookup_table[6][LOOKUP_SAMPLES + 1];
enum {
OSC_SINE,
OSC_SAW,
OSC_TRIANGLE,
OSC_SQUARE,
OSC_MOOGSAW,
OSC_EXP,
};
struct osc_t
{
double freq;
double step;
double ramp;
double level;
uint8_t shape;
};
void osc_init();
void osc_setfreq(struct osc_t *osc, double freq);
static inline void osc_setphase(struct osc_t *osc, double phase)
{
osc->ramp = LOOKUP_SAMPLES * phase;
}
static inline void osc_tick(struct osc_t *osc)
{
osc->ramp += osc->step;
if (osc->ramp > LOOKUP_SAMPLES) osc->ramp -= LOOKUP_SAMPLES;
//uint32_t pos = floor(osc->ramp);
//osc->sin = _sin_table[pos];
//osc->saw = _saw_table[pos];
//osc->tri = _tri_table[pos];
}
static inline double osc_getsample(const struct osc_t *osc)
{
uint32_t pos = osc->ramp;
return _lookup_table[osc->shape][pos] * osc->level;
}
static inline double osc_getsamplewithphase(const struct osc_t *osc, float phase)
{
double sample = osc->ramp + phase * LOOKUP_SAMPLES;
if (sample < 0.0) sample += LOOKUP_SAMPLES;
else if (sample > LOOKUP_SAMPLES) sample -= LOOKUP_SAMPLES;
uint32_t pos = sample;
return _lookup_table[osc->shape][pos] * osc->level;
}