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.
 
 
psyn/osc.c

44 lines
1.1 KiB

#include <stdint.h>
#include <math.h>
#include "psyn.h"
#include "osc.h"
double _lookup_table[6][LOOKUP_SAMPLES + 1];
void osc_init()
{
int i;
for (i = 0; i <= LOOKUP_SAMPLES; i++) {
double ph = (double)i / LOOKUP_SAMPLES;
_lookup_table[0][i] = sin(2.0 * M_PI * ph);
_lookup_table[1][i] = -1.0 + ph * 2.0;
if (ph <= 0.25) _lookup_table[2][i] = ph * 4.0;
else if (ph <= 0.75) _lookup_table[2][i] = 2.0 - ph * 4.0;
else _lookup_table[2][i] = ph * 4.0 - 4.0;
//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];
_lookup_table[3][i] = (ph >= 0.5) ? -1.0 : 1.0;
if (ph < 0.5) _lookup_table[4][i] = -1.0 + ph * 4.0;
else _lookup_table[4][i] = 1.0 - 2.0 * ph;
if (ph > 0.5) _lookup_table[5][i] = 1.0 - ph;
else _lookup_table[5][i] = -1.0 + 8.0 * ph * ph;
}
}
void osc_setfreq(struct osc_t *osc, double freq)
{
osc->freq = freq;
osc->step = freq / _sample_rate * LOOKUP_SAMPLES;
/* Ensure step does not go out of bound */
while (osc->step > LOOKUP_SAMPLES) osc->step -= LOOKUP_SAMPLES;
}