dsp.c (1253B)
1 #include "dsp.h" 2 #include "osc.h" 3 #include <stdlib.h> 4 #include <math.h> 5 6 #ifndef TAU 7 #define TAU (6.283185307179586) 8 #endif 9 10 float next_sample(synthesizer *synth) 11 { 12 osc *vco1 = &synth->vco1; 13 osc *vco2 = &synth->vco2; 14 osc *vco3 = &synth->vco3; 15 16 float s = 0; 17 18 // float freq = 220 + 30 * osc_process(lfo2, SAMPLE_RATE); 19 // atomic_store(&data->vco1.freq_target, freq); 20 21 s += osc_process(vco1); 22 s += osc_process(vco2); 23 s += osc_process(vco3); 24 25 s *= (0.33); 26 s *= 0.4; 27 28 return s; 29 } 30 31 synthesizer *synth_create(float sample_rate) 32 { 33 synthesizer *synth = malloc(sizeof *synth); 34 if (!synth) 35 return NULL; 36 37 synth->sr = sample_rate; 38 39 for (int i = 0; i < 2048; i++) { 40 synth->wavetable[i] = sin((float)i * TAU/2048); 41 } 42 43 float base = 220; 44 osc_create(&synth->vco1, base, sample_rate, synth->wavetable); 45 osc_create(&synth->vco2, base * 5.0/4.0 * 1.003, sample_rate, synth->wavetable); 46 osc_create(&synth->vco3, base * 3.0/2.0 * 0.998, sample_rate, synth->wavetable); 47 osc_create(&synth->lfo1, 440, sample_rate, synth->wavetable); 48 osc_create(&synth->lfo2, 880, sample_rate, synth->wavetable); 49 50 return synth; 51 } 52 53 void synth_destroy(synthesizer *s) { 54 free(s); 55 }