| 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 |
#include "m_pd.h" |
| 38 |
#include <stdlib.h> |
| 39 |
#include <math.h> |
| 40 |
|
| 41 |
#ifdef _MSC_VER |
| 42 |
#define M_PI 3.14159265358979323846 |
| 43 |
#endif |
| 44 |
|
| 45 |
#define BFSZ 4096 |
| 46 |
#define BLOCK_DELAY 10 |
| 47 |
#define NA 0.0 |
| 48 |
#define COEFFSIZE 3000 |
| 49 |
#define NBCOEFFS 5 |
| 50 |
|
| 51 |
static char *wahwah_version = "wahwah~: an audio wahwah, version 0.1 ([email protected])"; |
| 52 |
|
| 53 |
typedef struct _wahwah |
| 54 |
{ |
| 55 |
t_object x_obj; |
| 56 |
double *x_buf; |
| 57 |
t_int x_samplerate; |
| 58 |
t_float *x_coeffs; |
| 59 |
double x_minfc; |
| 60 |
double x_maxfc; |
| 61 |
double x_sense; |
| 62 |
double x_maxstep; |
| 63 |
double x_dbgain; |
| 64 |
double x_bandwidth; |
| 65 |
|
| 66 |
double x_Q; |
| 67 |
|
| 68 |
|
| 69 |
double min_coef,max_coef,cur_coef,step,maxstep; |
| 70 |
t_int down; |
| 71 |
|
| 72 |
short x0,x1,x2; |
| 73 |
t_int y0,y1,y2; |
| 74 |
|
| 75 |
t_float x_f; |
| 76 |
} t_wahwah; |
| 77 |
|
| 78 |
static t_class *wahwah_class; |
| 79 |
|
| 80 |
static void wahwah_set_coeffs (t_wahwah *x) |
| 81 |
{ |
| 82 |
t_int i; |
| 83 |
double omega; |
| 84 |
double sn,cs; |
| 85 |
double alpha = 0.0; |
| 86 |
|
| 87 |
double a0,a1,a2,b0,b1,b2; |
| 88 |
double A; |
| 89 |
|
| 90 |
A = exp(x->x_dbgain/40.0)*log(10.0); |
| 91 |
for (i = 0; i < COEFFSIZE; i++) |
| 92 |
{ |
| 93 |
omega = 2.0*M_PI*(double)i/(double)x->x_samplerate; |
| 94 |
sn = sin(omega); |
| 95 |
cs = cos(omega); |
| 96 |
if (x->x_bandwidth) |
| 97 |
alpha = sn*sin(log(2.0)/2.0*x->x_bandwidth*omega/sn); |
| 98 |
else |
| 99 |
|
| 100 |
if (x->x_Q) |
| 101 |
alpha = sn/(2.0*x->x_Q); |
| 102 |
|
| 103 |
|
| 104 |
b0 = 1.0 + alpha*A; |
| 105 |
b1 = -2.0*cs ; |
| 106 |
b2 = 1.0 - alpha*A; |
| 107 |
a0 = 1.0 + alpha/A; |
| 108 |
a1 = -2.0*cs ; |
| 109 |
a2 = 1.0 - alpha/A; |
| 110 |
|
| 111 |
*(x->x_coeffs+i*NBCOEFFS) = (b0/a0); |
| 112 |
*(x->x_coeffs+i*NBCOEFFS+1) = (b1/a0); |
| 113 |
*(x->x_coeffs+i*NBCOEFFS+2) = (b2/a0); |
| 114 |
*(x->x_coeffs+i*NBCOEFFS+3) = (a1/a0); |
| 115 |
*(x->x_coeffs+i*NBCOEFFS+4) = (a2/a0); |
| 116 |
|
| 117 |
} |
| 118 |
x->min_coef = x->x_minfc; |
| 119 |
x->max_coef = x->x_maxfc; |
| 120 |
x->cur_coef = x->min_coef; |
| 121 |
x->down = 1; |
| 122 |
x->step = 0; |
| 123 |
x->maxstep = x->x_maxstep/(double)x->x_samplerate*1000.0; |
| 124 |
} |
| 125 |
|
| 126 |
static void wahwah_bandwidth(t_wahwah *x, t_floatarg fbandwidth ) |
| 127 |
{ |
| 128 |
if ( fbandwidth > 10.0 ) |
| 129 |
{ |
| 130 |
fbandwidth = 10.0; |
| 131 |
} |
| 132 |
if ( fbandwidth < 0.0 ) |
| 133 |
{ |
| 134 |
fbandwidth = 0.0; |
| 135 |
} |
| 136 |
x->x_bandwidth = fbandwidth; |
| 137 |
|
| 138 |
wahwah_set_coeffs( x ); |
| 139 |
} |
| 140 |
|
| 141 |
static void wahwah_dbgain(t_wahwah *x, t_floatarg fdbgain ) |
| 142 |
{ |
| 143 |
if ( fdbgain > 15.0 ) |
| 144 |
{ |
| 145 |
fdbgain = 15.0; |
| 146 |
} |
| 147 |
if ( fdbgain < -15.0 ) |
| 148 |
{ |
| 149 |
fdbgain = -15.0; |
| 150 |
} |
| 151 |
x->x_dbgain = fdbgain; |
| 152 |
|
| 153 |
wahwah_set_coeffs( x ); |
| 154 |
} |
| 155 |
|
| 156 |
static void wahwah_maxstep(t_wahwah *x, t_floatarg fmaxstep ) |
| 157 |
{ |
| 158 |
if ( fmaxstep > 100.0 ) |
| 159 |
{ |
| 160 |
fmaxstep = 100.0; |
| 161 |
} |
| 162 |
if ( fmaxstep < 0.0 ) |
| 163 |
{ |
| 164 |
fmaxstep = 0.0; |
| 165 |
} |
| 166 |
x->x_maxstep = fmaxstep; |
| 167 |
|
| 168 |
wahwah_set_coeffs( x ); |
| 169 |
} |
| 170 |
|
| 171 |
static void wahwah_sensibility(t_wahwah *x, t_floatarg fsensibility ) |
| 172 |
{ |
| 173 |
if ( fsensibility > 1000.0 ) |
| 174 |
{ |
| 175 |
fsensibility = 1000.0; |
| 176 |
} |
| 177 |
if ( fsensibility < 0.0 ) |
| 178 |
{ |
| 179 |
fsensibility = 0.0; |
| 180 |
} |
| 181 |
x->x_sense = fsensibility; |
| 182 |
|
| 183 |
wahwah_set_coeffs( x ); |
| 184 |
} |
| 185 |
|
| 186 |
static void wahwah_maxfreq(t_wahwah *x, t_floatarg fmaxfreq ) |
| 187 |
{ |
| 188 |
if ( fmaxfreq > 3000.0 ) |
| 189 |
{ |
| 190 |
fmaxfreq = 3000.0; |
| 191 |
} |
| 192 |
if ( fmaxfreq < 0.0 ) |
| 193 |
{ |
| 194 |
fmaxfreq = 0.0; |
| 195 |
} |
| 196 |
x->x_maxfc = fmaxfreq; |
| 197 |
|
| 198 |
wahwah_set_coeffs( x ); |
| 199 |
} |
| 200 |
|
| 201 |
static void wahwah_minfreq(t_wahwah *x, t_floatarg fminfreq ) |
| 202 |
{ |
| 203 |
if ( fminfreq > 3000.0 ) |
| 204 |
{ |
| 205 |
fminfreq = 3000.0; |
| 206 |
} |
| 207 |
if ( fminfreq < 0.0 ) |
| 208 |
{ |
| 209 |
fminfreq = 0.0; |
| 210 |
} |
| 211 |
x->x_minfc = fminfreq; |
| 212 |
|
| 213 |
wahwah_set_coeffs( x ); |
| 214 |
} |
| 215 |
|
| 216 |
static void wahwah_step(t_wahwah *x, t_floatarg fstep ) |
| 217 |
{ |
| 218 |
if ( fstep > x->x_maxstep ) |
| 219 |
{ |
| 220 |
fstep = x->x_maxstep; |
| 221 |
} |
| 222 |
if ( fstep < 0 ) |
| 223 |
{ |
| 224 |
fstep = 0; |
| 225 |
} |
| 226 |
x->step = fstep*x->x_sense/x->x_samplerate; |
| 227 |
} |
| 228 |
|
| 229 |
static t_int *wahwah_perform(t_int *w) |
| 230 |
{ |
| 231 |
t_float *in = (t_float *)(w[1]); |
| 232 |
t_float *out = (t_float *)(w[2]); |
| 233 |
t_int n = (int)(w[3]); |
| 234 |
t_wahwah *x = (t_wahwah*)(w[4]); |
| 235 |
t_int i; |
| 236 |
|
| 237 |
for (i=0; i<n; i++) |
| 238 |
{ |
| 239 |
|
| 240 |
x->x0 = (int)((*(in+i))*32768); |
| 241 |
|
| 242 |
x->y0 = (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS))*x->x0 |
| 243 |
+ (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+1))*x->x1 |
| 244 |
+ (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+2))*x->x2 |
| 245 |
- (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+3))*x->y1 |
| 246 |
- (*(x->x_coeffs+(int)x->cur_coef*NBCOEFFS+4))*x->y2; |
| 247 |
|
| 248 |
x->y2 = x->y1; |
| 249 |
x->y1 = x->y0; |
| 250 |
x->x2 = x->x1; |
| 251 |
x->x1 = x->x0; |
| 252 |
|
| 253 |
if(x->y0 > 32767.0) |
| 254 |
x->y0 = 32767.0; |
| 255 |
else if(x->y0 < -32768.0) |
| 256 |
x->y0 = -32768.0; |
| 257 |
|
| 258 |
*(out+i) = (t_float)(x->y0) / 32768.0; |
| 259 |
|
| 260 |
x->cur_coef = x->cur_coef+x->down*x->step*(abs(x->x0)/(0.5*32768.0)); |
| 261 |
|
| 262 |
if(x->cur_coef > x->max_coef) |
| 263 |
{ |
| 264 |
x->cur_coef = x->max_coef; |
| 265 |
x->down = -1; |
| 266 |
} |
| 267 |
else if(x->cur_coef < x->min_coef) |
| 268 |
{ |
| 269 |
x->cur_coef = x->min_coef; |
| 270 |
x->down = 1; |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
return (w+5); |
| 276 |
} |
| 277 |
|
| 278 |
static void wahwah_preset(t_wahwah *x, t_float pnumber) |
| 279 |
{ |
| 280 |
switch ( (int)pnumber ) |
| 281 |
{ |
| 282 |
|
| 283 |
case 1: |
| 284 |
x->x_minfc = 100.0; |
| 285 |
x->x_maxfc = 1600.0; |
| 286 |
x->x_sense = 100.0; |
| 287 |
x->x_maxstep = 60.0; |
| 288 |
x->x_dbgain = 15.0; |
| 289 |
x->x_Q = 1.0; |
| 290 |
break; |
| 291 |
|
| 292 |
|
| 293 |
case 2: |
| 294 |
x->x_minfc = 100.0; |
| 295 |
x->x_maxfc = 1600.0; |
| 296 |
x->x_sense = 50.0; |
| 297 |
x->x_maxstep = 36.0; |
| 298 |
x->x_dbgain = 15.0; |
| 299 |
x->x_Q = 1.0; |
| 300 |
break; |
| 301 |
|
| 302 |
|
| 303 |
case 3: |
| 304 |
x->x_minfc = 100.0; |
| 305 |
x->x_maxfc = 600.0; |
| 306 |
x->x_sense = 100.0; |
| 307 |
x->x_maxstep = 66.0; |
| 308 |
x->x_dbgain = 15.0; |
| 309 |
x->x_Q = 1.0; |
| 310 |
break; |
| 311 |
|
| 312 |
|
| 313 |
case 4: |
| 314 |
x->x_minfc = 10.0; |
| 315 |
x->x_maxfc = 2900.0; |
| 316 |
x->x_sense = 100.0; |
| 317 |
x->x_maxstep = 66.0; |
| 318 |
x->x_dbgain = 15.0; |
| 319 |
x->x_Q = 1.0; |
| 320 |
break; |
| 321 |
|
| 322 |
|
| 323 |
case 5: |
| 324 |
x->x_minfc = 400.0; |
| 325 |
x->x_maxfc = 2000.0; |
| 326 |
x->x_sense = 100.0; |
| 327 |
x->x_maxstep = 66.0; |
| 328 |
x->x_dbgain = 15.0; |
| 329 |
x->x_Q = 1.0; |
| 330 |
break; |
| 331 |
|
| 332 |
default: |
| 333 |
post( "wahwah~ : unknown preset requested : %d", pnumber ); |
| 334 |
return; |
| 335 |
break; |
| 336 |
} |
| 337 |
wahwah_set_coeffs( x ); |
| 338 |
} |
| 339 |
|
| 340 |
static void wahwah_dsp(t_wahwah *x, t_signal **sp) |
| 341 |
{ |
| 342 |
dsp_add(wahwah_perform, 4, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n, x ); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
static void wahwah_free(t_wahwah *x) |
| 347 |
{ |
| 348 |
if ( x->x_buf != NULL ) |
| 349 |
{ |
| 350 |
freebytes(x->x_buf, BFSZ*sizeof( double ) ); |
| 351 |
post( "Freed %d bytes", BFSZ*sizeof( double ) ); |
| 352 |
x->x_buf = NULL; |
| 353 |
} |
| 354 |
if ( x->x_coeffs != NULL ) |
| 355 |
{ |
| 356 |
freebytes(x->x_coeffs, COEFFSIZE*NBCOEFFS*sizeof( t_float ) ); |
| 357 |
post( "Freed %d bytes", COEFFSIZE*NBCOEFFS*sizeof( t_float )); |
| 358 |
x->x_coeffs = NULL; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
static void *wahwah_new(void) |
| 363 |
{ |
| 364 |
t_wahwah *x = (t_wahwah *)pd_new(wahwah_class); |
| 365 |
outlet_new(&x->x_obj, &s_signal); |
| 366 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("minfreq")); |
| 367 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("maxfreq")); |
| 368 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("sensibility")); |
| 369 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("maxstep")); |
| 370 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("dbgain")); |
| 371 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("bandwidth")); |
| 372 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("step")); |
| 373 |
|
| 374 |
x->x_samplerate = (int)sys_getsr(); |
| 375 |
|
| 376 |
if ( !( x->x_buf = ( double* ) getbytes( BFSZ*sizeof( double ) ) ) ) |
| 377 |
{ |
| 378 |
post ("wahwah~ : could not allocate buffer" ); |
| 379 |
return NULL; |
| 380 |
} |
| 381 |
if ( !( x->x_coeffs = ( t_float* ) getbytes( COEFFSIZE*NBCOEFFS*sizeof( t_float ) ) ) ) |
| 382 |
{ |
| 383 |
post ("wahwah~ : could not allocate coeffs" ); |
| 384 |
return NULL; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
wahwah_preset( x, 1 ); |
| 389 |
wahwah_set_coeffs( x ); |
| 390 |
|
| 391 |
return (x); |
| 392 |
} |
| 393 |
|
| 394 |
void wahwah_tilde_setup(void) |
| 395 |
{ |
| •396 |
logpost(NULL, 4, "%s", wahwah_version ); |
| 397 |
wahwah_class = class_new(gensym("wahwah~"), (t_newmethod)wahwah_new, (t_method)wahwah_free, |
| 398 |
sizeof(t_wahwah), 0, 0); |
| 399 |
|
| 400 |
CLASS_MAINSIGNALIN( wahwah_class, t_wahwah, x_f ); |
| 401 |
class_addmethod(wahwah_class, (t_method)wahwah_step, gensym("step"), A_FLOAT, 0); |
| 402 |
class_addmethod(wahwah_class, (t_method)wahwah_minfreq, gensym("minfreq"), A_FLOAT, 0); |
| 403 |
class_addmethod(wahwah_class, (t_method)wahwah_maxfreq, gensym("maxfreq"), A_FLOAT, 0); |
| 404 |
class_addmethod(wahwah_class, (t_method)wahwah_sensibility, gensym("sensibility"), A_FLOAT, 0); |
| 405 |
class_addmethod(wahwah_class, (t_method)wahwah_maxstep, gensym("maxstep"), A_FLOAT, 0); |
| 406 |
class_addmethod(wahwah_class, (t_method)wahwah_dbgain, gensym("dbgain"), A_FLOAT, 0); |
| 407 |
class_addmethod(wahwah_class, (t_method)wahwah_bandwidth, gensym("bandwidth"), A_FLOAT, 0); |
| 408 |
class_addmethod(wahwah_class, (t_method)wahwah_dsp, gensym("dsp"), 0); |
| 409 |
class_addmethod(wahwah_class, (t_method)wahwah_preset, gensym("preset"), A_FLOAT, 0); |
| 410 |
} |