revised svf.

master
Rachel Fae Fox (foxiepaws) 2019-10-30 21:22:41 -04:00
parent de368d0ee3
commit 93ef31fc7f
3 changed files with 12 additions and 35 deletions

View File

@ -14,48 +14,34 @@
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
typedef enum DBO { _6dbo, _12dbo, _18dbo, _24dbo, _36dbo = 6, _48dbo } DBO;
typedef struct SVF {
float notch;
float low;
float high;
float band;
float drive;
float freq;
float q;
DBO dbo;
float out;
float(*damp)(struct SVF*);
float(*process)(struct SVF*,float in);
void(*reset)(struct SVF*);
} SVF;
float svf_damp(SVF *self) {
float freq = 2.0*sin(M_PI * MIN(0.25, self->freq/(44100*2)));
return MIN(2.0 * (1.0 - powf(self->q,0.25)), MIN(2.0, 2.0/freq - freq * 0.5));
// return MIN(2.0 * (1.0 - pow(self->q,0.25)), MIN(2.0, (2.0/freq - freq * 0.5)));
}
float svf_process(SVF *self,float in) {
float a = in;
float freq = 2.0*sin(M_PI * MIN(0.25, self->freq/(44100*2)));
float damp = self->damp(self);
//printf("prefilt status: freq: %f, drive: %f, damp: %f, in: %f, low: %f, notch: %f, high: %f, band: %f\n",self->freq,self->drive, damp,in, self->low, self->notch, self->high, self->band);
for (unsigned int i = 0; i <= self->dbo; i++) {
self->notch = a - damp * self->band;
self->low = self->low + freq * self->band;
self->high = self->notch - self->low;
self->band = freq * self->high + self->band - self->drive * self->band * self->band * self->band;
a = self->low;
}
float q = 1-self->q;
float freq = 2.0*sin(M_PI * MIN(0.25, self->freq/(44100)));
self->low = self->low + freq * self->band;
self->high = q * a - self->low - q * self->band;
self->band = freq * self->high + self->band;
self->notch = self->high + self->low;
self->out = a;
//printf("postfilt status: freq: %f, drive: %f, damp: %f, in: %f, low: %f, notch: %f, high: %f, band: %f\n",self->freq,self->drive, damp,in, self->low, self->notch, self->high, self->band);
return self->out;
}
@ -67,15 +53,11 @@ void svf_reset(SVF* self){
self->out = 0;
}
SVF* svf_new(float freq,float q,float drive) {
SVF* svf_new(float freq,float q) {
SVF *self = malloc(sizeof(SVF));
self->freq = freq;
self->q = q;
self->drive = drive;
self->dbo = _12dbo;
// functions.
self->damp = svf_damp;
self->process = svf_process;
self->reset = svf_reset;
self->reset(self);

View File

@ -17,26 +17,21 @@
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
typedef enum DBO { _6dbo, _12dbo, _18dbo, _24dbo, _36dbo = 6, _48dbo } DBO;
typedef struct SVF {
float notch;
float low;
float high;
float band;
float drive;
float freq;
float q;
DBO dbo;
float out;
float(*damp)(struct SVF*);
float(*process)(struct SVF*,float in);
void(*reset)(struct SVF*);
} SVF;
float svf_damp(SVF *self);
float svf_process(SVF *self,float in);
void svf_reset(SVF* self);
SVF* svf_new(float freq,float q,float drive);
SVF* svf_new(float freq,float q);
#endif

View File

@ -86,7 +86,7 @@ int main(int argc, char**argv) {
Envelope b = *envelope_new();
saw = *basic_new(_waveform_saw, b);
saw.freq = 440;
filt = *svf_new(7000.0f, 0.99f, 0.000001f);
filt = *svf_new(7000.0f, 0.5f);
//data.sample_rate=SAMPLE_RATE;
PaStream *stream;
/* Open an audio I/O stream. */
@ -104,7 +104,7 @@ int main(int argc, char**argv) {
if( err != paNoError ) goto error;
/* start anything intersting */
saw.gate = true;
for (int freq = 7000; freq >= 60; freq--) {
for (int freq = 7000; freq >= 60; freq = freq - 0.0001) {
filt.freq = freq;
usleep(500);
}