psyn/osc.c

33 lines
782 B
C

#include <stdint.h>
#include <math.h>
#include "psyn.h"
#include "osc.h"
double _sin_table[LOOKUP_SAMPLES + 1];
double _saw_table[LOOKUP_SAMPLES + 1];
double _tri_table[LOOKUP_SAMPLES + 1];
void osc_init()
{
int i;
for (i = 0; i <= LOOKUP_SAMPLES; i++) {
_sin_table[i] = sin(2 * M_PI * (double)i / LOOKUP_SAMPLES);
}
for (i = 0; i <= LOOKUP_SAMPLES; i++) {
_saw_table[i] = 1.0 - ((double)i / (LOOKUP_SAMPLES / 2));
}
for (i = 0; i <= LOOKUP_SAMPLES; i++) {
_tri_table[i] = (double)i / (LOOKUP_SAMPLES / 4);
if (_tri_table[i] > 1.0) _tri_table[i] = 2.0 - _tri_table[i];
if (_tri_table[i] < -1.0) _tri_table[i] = -2.0 - _tri_table[i];
}
}
void osc_setfreq(struct osc_t *osc, double freq)
{
osc->freq = freq;
osc->step = freq / _sample_rate * LOOKUP_SAMPLES;
}