LPF/HPF changed. WIP new_filter
authorslack <slack@codemaniacs.com>
Tue, 16 Jun 2009 18:09:05 +0000 (20:09 +0200)
committerslack <slack@codemaniacs.com>
Tue, 16 Jun 2009 18:09:05 +0000 (20:09 +0200)
lolailo.c
synth.c
thesong.c

index 8f4e4846830d5ed1d30b0f80e3a411610c5776e2..6897770e83c6d94f6c6684ca2538cb4c77b0a6cb 100644 (file)
--- a/lolailo.c
+++ b/lolailo.c
@@ -75,9 +75,9 @@ void _start()
             {
                 float v = drawbuf[i];
                 if (v>1.0f) v = 1.0f;
-                if (v<0.0f) v = 0.0f;
+                if (v<-1.0f) v = -1.0f;
 
-                int y1 = (1.0f-v)*479.0f;
+                int y1 = (1.0f-(v*0.5f+0.5f))*479.0f;
                 float ymin, ymax;
                 if (y0 < y1) { ymin=y0; ymax=y1; }
                 else { ymin=y1; ymax=y0; }
diff --git a/synth.c b/synth.c
index bd1f3f1a73795f01ee9ffeb94e23691804ab0786..cfd0d8c0d5311dbe40236492d75b7d09420a1afb 100644 (file)
--- a/synth.c
+++ b/synth.c
@@ -1,6 +1,7 @@
 #include "synth.h"
 #include <string.h>
 #include <math.h>
+#include <stdio.h>
 #include <stdlib.h>
 
 float detune(float base, int coarse, int fine)
@@ -72,49 +73,28 @@ uint8 synth(Voice *v, int len, float *mix_buf)
                 {
                     // TODO: cutoff should be a wave
                     // op1, op2: cutoff, resonance
-                    float cutoff = detune(1.0f, op1, 0);
-                    float r = (op2)/100.0f;
-
+                    float cutoff = v->waves[op1][i];
+                    float r = (op2)/5.0f;
                     //printf("op1=%d, op2=%d, cutoff=%f; r=%f\n", op1, op2, cutoff, r);
                     
-                    float sample = v->waves[wave-1][i];
-
-                    // Compute filter coefficients
-                    float c, c2,a1,a2,b1,b2;
-
-                    // Extraemos algunos calculos que son redundantes en HP y LP
-                    float param_tan = PI_samplerate * cutoff;
-
-                    if (ins==LPF) { // Low-pass
-                        c=1.0f / tanf(param_tan);
-                        c2 = c*c;
-
-                        a1 = 1.0f / ( 1.0f + r * c + c2);
-                        a2 = 2.0f * a1;
-                        b1 = 2.0f * ( 1.0f - c2) * a1;
-                    } else { // High-pass
-                        c = tanf(-param_tan);
-                        c2 = c*c;
-
-                        a1 = 1.0f / ( 1.0f + r * c + c2);
-                        a2 = -2*a1;
-                        b1 = 2.0f * ( c2 - 1.0f) * a1;
-                    }
-                    
-                    b2 = ( 1.0f - r*c + c2) * a1;
-
-                    // data [0,1,2] = y(0), y(-1), y(-2)
-                    // data [3,4]   = x(-1), x(-2)
+                    float input = v->waves[wave-1][i];
                     float *data = (float*)v->data[wave];
-                    data[0] = a1*sample + a2*data[3] + a1*data[4] -
-                                    b1*data[1] - b2*data[2];
-
-                    data[4]=data[3];
-                    data[3]=sample;
-                    data[2]=data[1];
-                    data[1]=data[0];
-
-                    v->waves[wave][i] = data[0];
+                    // data[0-3] == pole1..pole4
+                    // Arif Ove Karlsen's 24dB Ladder Approximation, 3.nov 2007
+                    // http://musicdsp.org/showArchiveComment.php?ArchiveID=141
+                    //printf("BEFORE: input=%f, pole1=%f, pole2=%f, pole3=%f, pole4=%f\n", input, data[0], data[1], data[2], data[3]);
+                    float rez = data[3]*r; if (rez > 1) rez = 1;
+                    input = input-rez;
+                    data[0] = data[0] + ((-data[0]+input)   * cutoff);
+                    data[1] = data[1] + ((-data[1]+data[0]) * cutoff);
+                    data[2] = data[2] + ((-data[2]+data[1]) * cutoff);
+                    data[3] = data[3] + ((-data[3]+data[2]) * cutoff);
+
+                    if (ins == LPF)
+                        v->waves[wave][i] = data[3];
+                    else
+                        v->waves[wave][i] = data[3] - input;
+                    //printf("AFTER: input=%f, pole1=%f, pole2=%f, pole3=%f, pole4=%f\n", input, data[0], data[1], data[2], data[3]);
                     break;
                 }
                 default:
index bbeda3ee41ed6ec4837cef50d863f92d37b54a2e..6cc4a08c4d8c8b968a9b6c3005263276543609c2 100644 (file)
--- a/thesong.c
+++ b/thesong.c
@@ -12,10 +12,10 @@ Song thesong={
         {{45,0,0,0,43,0,0,0,41,0,0,0,40,0,0,0}, 0},
     }, // end tracks
     {  // begin instrument data
-        SINE,24,0,
-        SQUARE,0,10,
-        SUM,0,1,
-        LPF,100,10,
+        SINE,-80,0,
+        SQUARE,0,50,
+        LPF,0,6,
+        MUL,0,2,
         END,
     }, // end instrument data
     {  // begin playlist