osc.c (728B)
1 #include <stdatomic.h> 2 #include "osc.h" 3 #include "dsp.h" 4 #include <stdio.h> 5 6 float osc_process(osc *o) 7 { 8 o->phase += o->inc; 9 if (o->phase >= 1.0) { 10 o->phase -= 1.0; 11 } 12 13 float target = atomic_load(&o->inc_target); 14 o->inc += 0.001f * (target - o->inc); 15 16 float index = o->phase * 2048; 17 int i0 = (int)(index); 18 int i1 = (i0 + 1) & (2048 - 1); 19 float s0 = o->wavetable[i0]; 20 float s1 = o->wavetable[i1]; 21 22 float frac = index - i0; 23 return s0 + (s1-s0) * frac; 24 } 25 26 void osc_create(osc *o, float freq, float sample_rate, float *wavetable) 27 { 28 o->phase = 0.0; 29 float inc = freq / sample_rate; 30 o->inc = inc; 31 o->wavetable = wavetable; 32 atomic_store(&o->inc_target, inc); 33 }