aboutsummaryrefslogtreecommitdiff
path: root/wahwah~.c
blob: 2c23e324c03973fc8e0d0767b4c95b7e502cc660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Copyright (c) 2002 Yves Degoyon.                                             */
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL     */
/* WARRANTIES, see the file, "COPYING"  in this distribution.                   */
/*                                                                              */
/* wahwah~ -- a kind of effect used in psychedelic music                        */
/* the algorithm was taken from Digital Effects,                                */
/* a guitar effects software for DOS which rocks, written by Alexey Smoli       */
/* ( http://st.karelia.ru/~smlalx/ )                                            */
/*                                                                              */
/* This program is free software; you can redistribute it and/or                */
/* modify it under the terms of the GNU General Public License                  */
/* as published by the Free Software Foundation; either version 2               */
/* of the License, or (at your option) any later version.                       */
/*                                                                              */
/* See file LICENSE for further informations on licensing terms.                */
/*                                                                              */
/* This program is distributed in the hope that it will be useful,              */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of               */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
/* GNU General Public License for more details.                                 */
/*                                                                              */
/* You should have received a copy of the GNU General Public License            */
/* along with this program; if not, write to the Free Software                  */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */
/*                                                                              */
/* Based on PureData by Miller Puckette and others.                             */
/*                                                                              */
/* Made while listening to :                                                    */
/*                                                                              */
/* Crass -- "Shaved Women ( Collaborators )"                                    */
/* Zounds -- "Can't Cheat Karma"                                                */
/*                                                                              */
/* Special message for the french :                                             */
/* "Delay all your work...and go vote against national front"                   */
/* ---------------------------------------------------------------------------- */

#include "m_pd.h"
#include <stdlib.h>
#include <math.h>

#ifdef _MSC_VER
#define M_PI 3.14159265358979323846
#endif

#define BFSZ            4096   /* main delay buffer                  */
#define BLOCK_DELAY     10     /* number of blocks to delay          */
#define NA              0.0    /* param not applicable               */
#define COEFFSIZE       3000   /* coefficients size                  */
#define NBCOEFFS        5      /* number of coefficients             */

static char   *wahwah_version = "wahwah~: an audio wahwah, version 0.1 (ydegoyon@free.fr)";

typedef struct _wahwah
{
    t_object x_obj;
    double *x_buf;
    t_int x_samplerate;
    t_float *x_coeffs;
    double x_minfc;       /* min frequency Hz */                                            /* 0< <3000 */
    double x_maxfc;       /* max frequency Hz */                                            /* 0< <3000 */
    double x_sense;       /* increase/decrease frequency change by one mouse movement Hz */ /* 0< <=1000 */
    double x_maxstep;     /* maximum frequency change step KHz */                           /* 0< <=100 */
    double x_dbgain;      /* peaking filter gain dB */                                      /* -15<= <=15 */
    double x_bandwidth;   /* bandwidth in octaves (between midpoint (dBgain/2)
                             gain frequencies) or */                                        /* 0< <10 */
    double x_Q;           /* the EE kinda definition */                                     /* 0< <=1 */

    /* variables for audio computation */
    double min_coef,max_coef,cur_coef,step,maxstep;
    t_int down;
    /* amplitudes */
    short x0,x1,x2;
    t_int y0,y1,y2;

    t_float x_f;
} t_wahwah;

static t_class *wahwah_class;

static void wahwah_set_coeffs (t_wahwah *x)
{
    t_int i;
    double omega;
    double sn,cs;
    double alpha = 0.0;
    /* filter coefficients */
    double a0,a1,a2,b0,b1,b2;
    double A;

    A   = exp(x->x_dbgain/40.0)*log(10.0);
    for (i = 0; i < COEFFSIZE; i++)
    {
        omega = 2.0*M_PI*(double)i/(double)x->x_samplerate;
        sn    = sin(omega);
        cs    = cos(omega);
        if (x->x_bandwidth)
            alpha = sn*sin(log(2.0)/2.0*x->x_bandwidth*omega/sn);
        else
            /* if Q is specified instead of bandwidth */
            if (x->x_Q)
                alpha = sn/(2.0*x->x_Q);

        /* then compute the coefs for whichever filter type you want */
        b0 =   1.0 + alpha*A;
        b1 =  -2.0*cs       ;
        b2 =   1.0 - alpha*A;
        a0 =   1.0 + alpha/A;
        a1 =  -2.0*cs       ;
        a2 =   1.0 - alpha/A;

        *(x->x_coeffs+i*NBCOEFFS) = (b0/a0);
        *(x->x_coeffs+i*NBCOEFFS+1) = (b1/a0);
        *(x->x_coeffs+i*NBCOEFFS+2) = (b2/a0);
        *(x->x_coeffs+i*NBCOEFFS+3) = (a1/a0);
        *(x->x_coeffs+i*NBCOEFFS+4) = (a2/a0);
        // post( "wahwah~ : coeff : %f", *(x->x_coeffs+i*NBCOEFFS+4) );
    }
    x->min_coef = x->x_minfc;
    x->max_coef = x->x_maxfc;
    x->cur_coef = x->min_coef;
    x->down = 1;
    x->step = 0;
    x->maxstep = x->x_maxstep/(double)x->x_samplerate*1000.0;
}

static void wahwah_bandwidth(t_wahwah *x, t_floatarg fbandwidth )
{
    if ( fbandwidth > 10.0 )
    {
        fbandwidth = 10.0;
    }
    if ( fbandwidth < 0.0 )
    {
        fbandwidth = 0.0;
    }
    x->x_bandwidth = fbandwidth;
    // post( "wahwah~ : bandwidth: %f", x->x_bandwidth );
    wahwah_set_coeffs( x );
}

static void wahwah_dbgain(t_wahwah *x, t_floatarg fdbgain )
{
    if ( fdbgain > 15.0 )
    {
        fdbgain = 15.0;
    }
    if ( fdbgain < -15.0 )
    {
        fdbgain = -15.0;
    }
    x->x_dbgain = fdbgain;
    // post( "wahwah~ : dbgain: %f", x->x_dbgain );
    wahwah_set_coeffs( x );
}

static void wahwah_maxstep(t_wahwah *x, t_floatarg fmaxstep )
{
    if ( fmaxstep > 100.0 )
    {
        fmaxstep = 100.0;
    }
    if ( fmaxstep < 0.0 )
    {
        fmaxstep = 0.0;
    }
    x->x_maxstep = fmaxstep;
    // post( "wahwah~ : maxstep: %f", x->x_maxstep );
    wahwah_set_coeffs( x );
}

static void wahwah_sensibility(t_wahwah *x, t_floatarg fsensibility )
{
    if ( fsensibility > 1000.0 )
    {
        fsensibility = 1000.0;
    }
    if ( fsensibility < 0.0 )
    {
        fsensibility = 0.0;
    }
    x->x_sense = fsensibility;
    // post( "wahwah~ : sensibility: %f", x->x_sense );
    wahwah_set_coeffs( x );
}

static void wahwah_maxfreq(t_wahwah *x, t_floatarg fmaxfreq )
{
    if ( fmaxfreq > 3000.0 )
    {
        fmaxfreq = 3000.0;
    }
    if ( fmaxfreq < 0.0 )
    {
        fmaxfreq = 0.0;
    }
    x->x_maxfc = fmaxfreq;
    // post( "wahwah~ : maxfreq: %f", x->x_maxfc );
    wahwah_set_coeffs( x );
}

static void wahwah_minfreq(t_wahwah *x, t_floatarg fminfreq )
{
    if ( fminfreq > 3000.0 )
    {
        fminfreq = 3000.0;
    }
    if ( fminfreq < 0.0 )
    {
        fminfreq = 0.0;
    }
    x->x_minfc = fminfreq;
    // post( "wahwah~ : minfreq: %f", x->x_minfc );
    wahwah_set_coeffs( x );
}

static void wahwah_step(t_wahwah *x, t_floatarg fstep )
{
    if ( fstep > x->x_maxstep )
    {
        fstep = x->x_maxstep;
    }
    if ( fstep < 0 )
    {
        fstep = 0;
    }
    x->step = fstep*x->x_sense/x->x_samplerate;
}

static t_int *wahwah_perform(t_int *w)
{
    t_float *in = (t_float *)(w[1]);
    t_float *out = (t_float *)(w[2]);
    t_int n = (int)(w[3]);
    t_wahwah *x = (t_wahwah*)(w[4]);
    t_int i;

    for (i=0; i<n; i++)
    {
        /* read input */
        x->x0 = (int)((*(in+i))*32768);

        x->y0 = (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS))*x->x0
                + (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+1))*x->x1
                + (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+2))*x->x2
                - (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+3))*x->y1
                - (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+4))*x->y2;

        x->y2 = x->y1;
        x->y1 = x->y0;
        x->x2 = x->x1;
        x->x1 = x->x0;

        if(x->y0 > 32767.0)
            x->y0 = 32767.0;
        else if(x->y0 < -32768.0)
            x->y0 = -32768.0;

        *(out+i) = (t_float)(x->y0) / 32768.0;

        x->cur_coef = x->cur_coef+x->down*x->step*(abs(x->x0)/(0.5*32768.0));
        // post ( "wahwah~ : cur coeff : %f", x->cur_coef );
        if(x->cur_coef > x->max_coef)
        {
            x->cur_coef = x->max_coef;
            x->down = -1;
        }
        else if(x->cur_coef < x->min_coef)
        {
            x->cur_coef = x->min_coef;
            x->down = 1;
        }

    }

    return (w+5);
}

static void wahwah_preset(t_wahwah *x, t_float pnumber)
{
    switch ( (int)pnumber )
    {
        /* fast change medium wah wah */
    case 1:
        x->x_minfc = 100.0;
        x->x_maxfc = 1600.0;
        x->x_sense = 100.0;
        x->x_maxstep = 60.0;
        x->x_dbgain = 15.0;
        x->x_Q = 1.0;
        break;

        /* slow change medium wah wah */
    case 2:
        x->x_minfc = 100.0;
        x->x_maxfc = 1600.0;
        x->x_sense = 50.0;
        x->x_maxstep = 36.0;
        x->x_dbgain = 15.0;
        x->x_Q = 1.0;
        break;

        /* fast wah wah */
    case 3:
        x->x_minfc = 100.0;
        x->x_maxfc = 600.0;
        x->x_sense = 100.0;
        x->x_maxstep = 66.0;
        x->x_dbgain = 15.0;
        x->x_Q = 1.0;
        break;

        /* ranged wah wah */
    case 4:
        x->x_minfc = 10.0;
        x->x_maxfc = 2900.0;
        x->x_sense = 100.0;
        x->x_maxstep = 66.0;
        x->x_dbgain = 15.0;
        x->x_Q = 1.0;
        break;

        /* wah wah 400 - 2000 */
    case 5:
        x->x_minfc = 400.0;
        x->x_maxfc = 2000.0;
        x->x_sense = 100.0;
        x->x_maxstep = 66.0;
        x->x_dbgain = 15.0;
        x->x_Q = 1.0;
        break;

    default:
        post( "wahwah~ : unknown preset requested : %d", pnumber );
        return;
        break;
    }
    wahwah_set_coeffs( x );
}

static void wahwah_dsp(t_wahwah *x, t_signal **sp)
{
    dsp_add(wahwah_perform, 4, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n, x );
}

/* clean up */
static void wahwah_free(t_wahwah *x)
{
    if ( x->x_buf != NULL )
    {
        freebytes(x->x_buf, BFSZ*sizeof( double ) );
        post( "Freed %d bytes",  BFSZ*sizeof( double ) );
        x->x_buf = NULL;
    }
    if ( x->x_coeffs != NULL )
    {
        freebytes(x->x_coeffs, COEFFSIZE*NBCOEFFS*sizeof( t_float ) );
        post( "Freed %d bytes", COEFFSIZE*NBCOEFFS*sizeof( t_float ));
        x->x_coeffs = NULL;
    }
}

static void *wahwah_new(void)
{
    t_wahwah *x = (t_wahwah *)pd_new(wahwah_class);
    outlet_new(&x->x_obj, &s_signal);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("minfreq"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("maxfreq"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("sensibility"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("maxstep"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("dbgain"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("bandwidth"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("step"));

    x->x_samplerate = (int)sys_getsr();

    if ( !( x->x_buf = ( double* ) getbytes( BFSZ*sizeof( double ) ) ) )
    {
        post ("wahwah~ : could not allocate buffer" );
        return NULL;
    }
    if ( !( x->x_coeffs = ( t_float* ) getbytes( COEFFSIZE*NBCOEFFS*sizeof( t_float ) ) ) )
    {
        post ("wahwah~ : could not allocate coeffs" );
        return NULL;
    }

    // set default parameters
    wahwah_preset( x, 1 );
    wahwah_set_coeffs( x );

    return (x);
}

void wahwah_tilde_setup(void)
{
    logpost(NULL, 4, "%s",  wahwah_version );
    wahwah_class = class_new(gensym("wahwah~"), (t_newmethod)wahwah_new, (t_method)wahwah_free,
                             sizeof(t_wahwah), 0, 0);

    CLASS_MAINSIGNALIN( wahwah_class, t_wahwah, x_f );
    class_addmethod(wahwah_class, (t_method)wahwah_step, gensym("step"), A_FLOAT, 0);
    class_addmethod(wahwah_class, (t_method)wahwah_minfreq, gensym("minfreq"), A_FLOAT, 0);
    class_addmethod(wahwah_class, (t_method)wahwah_maxfreq, gensym("maxfreq"), A_FLOAT, 0);
    class_addmethod(wahwah_class, (t_method)wahwah_sensibility, gensym("sensibility"), A_FLOAT, 0);
    class_addmethod(wahwah_class, (t_method)wahwah_maxstep, gensym("maxstep"), A_FLOAT, 0);
    class_addmethod(wahwah_class, (t_method)wahwah_dbgain, gensym("dbgain"), A_FLOAT, 0);
    class_addmethod(wahwah_class, (t_method)wahwah_bandwidth, gensym("bandwidth"), A_FLOAT, 0);
    class_addmethod(wahwah_class, (t_method)wahwah_dsp, gensym("dsp"), 0);
    class_addmethod(wahwah_class, (t_method)wahwah_preset, gensym("preset"), A_FLOAT, 0);
}