Debian Codegraph @

disto~.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* Copyleft (c) 2002 Yves Degoyon. */
2 /* Copyright (c) 2001 Alexei Smoli */
3 /* For information on usage and redistribution, and for a DISCLAIMER OF ALL */
4 /* WARRANTIES, see the file, "COPYING" in this distribution. */
5 /* */
6 /* disto~ -- a kind of effect used in pop music */
7 /* the algorithm was taken from Digital Effects (DISTORT3), */
8 /* a guitar effects software for DOS which rocks, written by Alexey Smoli */
9 /* ( http://st.karelia.ru/~smlalx/ ) */
10 /* */
11 /* This program is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU General Public License */
13 /* as published by the Free Software Foundation; either version 2 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* See file LICENSE for further informations on licensing terms. */
17 /* */
18 /* This program is distributed in the hope that it will be useful, */
19 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
20 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
21 /* GNU General Public License for more details. */
22 /* */
23 /* You should have received a copy of the GNU General Public License */
24 /* along with this program; if not, write to the Free Software */
25 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 /* */
27 /* Based on PureData by Miller Puckette and others. */
28 /* */
29 /* Made while listening to : */
30 /* */
31 /* Bruce Gilbert -- Ab Ovo */
32 /* Poison Girls -- Promenade Immortelle */
33 /* */
34 /* Special message for the french : */
35 /* "Delay all your work...and go vote against national front" */
36 /* ---------------------------------------------------------------------------- */
37
38 #include "m_pd.h"
39 #include <stdlib.h>
40 #include <math.h>
41
42 #ifdef _MSC_VER
43 #define M_PI 3.14159265358979323846
44 #endif
45
46 #define BFSZ 4096 /* main delay buffer */
47 #define BLOCK_DELAY 10 /* number of blocks to delay */
48 #define NA 0.0 /* param not applicable */
49 #define NBEXPS 129
50
51 static char *disto_version = "disto~: distortion, version 0.1 ([email protected])";
52
53 struct hipass
54 {
55 /* few intermediate variables */
56 double omega;
57 double sn,cs;
58 double alpha;
59 /* filter coefficients */
60 double a0,a1,a2,b0,b1,b2;
61 double k0,k1,k2,k3,k4;
62 /* amplitudes */
63 double x0,x1,x2;
64 double y0,y1,y2;
65 } hipass;
66
67 struct lowpass
68 {
69 /* few intermediate variables */
70 double omega;
71 double sn,cs;
72 double alpha;
73 /* filter coefficients */
74 double a0,a1,a2,b0,b1,b2;
75 double k0,k1,k2,k3,k4;
76 /* amplitudes */
77 double x0,x1,x2;
78 double y0,y1,y2;
79 } lowpass;
80
81 typedef struct _disto
82 {
83 t_object x_obj;
84 double *x_buf;
85 t_int x_samplerate;
86 double x_drive; /* distortion drive */ /* 0<= <=25 */
87 double x_drymix; /* dry (unaffected) signal mix */ /* -5<= <=5 */
88 double x_wetmix; /* wet (affected) signal mix */ /* -5<= <=5 */
89 double x_feedback; /* feedback */ /* -10<= <=10 */
90 double x_volume; /* distortion volume */ /* 0=< <=5 */
91 double x_hipassfreq; /* cutoff frequency for hi pass filter */ /* 0< <RATE/2 */
92 double x_lowpassfreq; /* cutoff frequency for low pass filter */ /* 0< <RATE/2 */
93 double x_hipassQ; /* the EE kinda definition for hi pass filter */ /* 0.1< <=1 */
94 double x_lowpassQ; /* the EE kinda definition for low pass filter */ /* 0.1< <=1 */
95
96 /* audio processing data ( not setable ) */
97 double data,pred;
98 double outval,outvol;
99 double exps[NBEXPS];
100
101 /* filters data ( not setable ) */
102 struct hipass HPF;
103 struct lowpass LPF;
104
105 t_float x_f;
106 } t_disto;
107
108 static t_class *disto_class;
109
110 static void disto_init_filters (t_disto *x)
111 {
112 t_int i;
113
114 for (i=0; i<130; i++)
115 {
116 x->exps[i]=exp((double)i*x->x_drive)*x->x_wetmix;
117 }
118
119 x->HPF.omega = 2.0*M_PI*x->x_hipassfreq/(double)x->x_samplerate;
120 x->HPF.sn = sin(x->HPF.omega);
121 x->HPF.cs = cos(x->HPF.omega);
122 x->HPF.alpha = x->HPF.sn/(2.0*x->x_hipassQ);
123 x->HPF.b0 = (1.0 + x->HPF.cs)/2.0;
124 x->HPF.b1 = -(1.0 + x->HPF.cs) ;
125 x->HPF.b2 = (1.0 + x->HPF.cs)/2.0;
126 x->HPF.a0 = 1.0 + x->HPF.alpha ;
127 x->HPF.a1 = -2.0*x->HPF.cs ;
128 x->HPF.a2 = 1.0 - x->HPF.alpha ;
129 x->HPF.k0 = (x->HPF.b0/x->HPF.a0);
130 x->HPF.k1 = (x->HPF.b1/x->HPF.a0);
131 x->HPF.k2 = (x->HPF.b2/x->HPF.a0);
132 x->HPF.k3 = (x->HPF.a1/x->HPF.a0);
133 x->HPF.k4 = (x->HPF.a2/x->HPF.a0);
134
135 x->LPF.omega = 2.0*M_PI*x->x_lowpassfreq/(double)x->x_samplerate;
136 x->LPF.sn = sin(x->LPF.omega);
137 x->LPF.cs = cos(x->LPF.omega);
138 x->LPF.alpha = x->LPF.sn/(2.0*x->x_lowpassQ);
139 x->LPF.b0 = (1.0 - x->LPF.cs)/2.0;
140 x->LPF.b1 = 1.0 - x->LPF.cs ;
141 x->LPF.b2 = (1.0 - x->LPF.cs)/2.0;
142 x->LPF.a0 = 1.0 + x->LPF.alpha ;
143 x->LPF.a1 = -2.0*x->LPF.cs ;
144 x->LPF.a2 = 1.0 - x->LPF.alpha ;
145 x->LPF.k0 = (x->LPF.b0/x->LPF.a0);
146 x->LPF.k1 = (x->LPF.b1/x->LPF.a0);
147 x->LPF.k2 = (x->LPF.b2/x->LPF.a0);
148 x->LPF.k3 = (x->LPF.a1/x->LPF.a0);
149 x->LPF.k4 = (x->LPF.a2/x->LPF.a0);
150 }
151
152 static void disto_drive(t_disto *x, t_floatarg fdrive )
153 {
154 if ( fdrive > 25.0 )
155 {
156 fdrive = 25.0;
157 }
158 if ( fdrive < 0.0 )
159 {
160 fdrive = 0.0;
161 }
162 x->x_drive = fdrive;
163 // post( "disto~ : drive: %f", x->x_drive );
164 disto_init_filters( x );
165 }
166
167 static void disto_drymix(t_disto *x, t_floatarg fdrymix )
168 {
169 if ( fdrymix > 5.0 )
170 {
171 fdrymix = 5.0;
172 }
173 if ( fdrymix < -5.0 )
174 {
175 fdrymix = -5.0;
176 }
177 x->x_drymix = fdrymix;
178 // post( "disto~ : drymix: %f", x->x_drymix );
179 disto_init_filters( x );
180 }
181
182 static void disto_wetmix(t_disto *x, t_floatarg fwetmix )
183 {
184 if ( fwetmix > 5.0 )
185 {
186 fwetmix = 5.0;
187 }
188 if ( fwetmix < -5.0 )
189 {
190 fwetmix = -5.0;
191 }
192 x->x_wetmix = fwetmix;
193 // post( "disto~ : wetmix: %f", x->x_wetmix );
194 disto_init_filters( x );
195 }
196
197 static void disto_feedback(t_disto *x, t_floatarg ffeedback )
198 {
199 if ( ffeedback > 10.0 )
200 {
201 ffeedback = 10.0;
202 }
203 if ( ffeedback < -10.0 )
204 {
205 ffeedback = -10.0;
206 }
207 x->x_feedback = ffeedback;
208 // post( "disto~ : feedback: %f", x->x_feedback );
209 disto_init_filters( x );
210 }
211
212 static void disto_volume(t_disto *x, t_floatarg fvolume )
213 {
214 if ( fvolume > 5.0 )
215 {
216 fvolume = 5.0;
217 }
218 if ( fvolume < 0.0 )
219 {
220 fvolume = 0.0;
221 }
222 x->x_volume = fvolume;
223 // post( "disto~ : volume: %f", x->x_volume );
224 disto_init_filters( x );
225 }
226
227 static void disto_hipassfreq(t_disto *x, t_floatarg fhipassfreq )
228 {
229 if ( fhipassfreq > x->x_samplerate/2 )
230 {
231 fhipassfreq = x->x_samplerate/2;
232 }
233 if ( fhipassfreq < 0.0 )
234 {
235 fhipassfreq = 0.0;
236 }
237 x->x_hipassfreq = fhipassfreq;
238 // post( "disto~ : hipassfreq: %f", x->x_hipassfreq );
239 disto_init_filters( x );
240 }
241
242 static void disto_hipassQ(t_disto *x, t_floatarg fhipassQ )
243 {
244 if ( fhipassQ > 1.0 )
245 {
246 fhipassQ = 1.0;
247 }
248 if ( fhipassQ < 0.1 )
249 {
250 fhipassQ = 0.1;
251 }
252 x->x_hipassQ = fhipassQ;
253 // post( "disto~ : hipassQ: %f", x->x_hipassQ );
254 disto_init_filters( x );
255 }
256
257 static void disto_lowpassfreq(t_disto *x, t_floatarg flowpassfreq )
258 {
259 if ( flowpassfreq > x->x_samplerate/2 )
260 {
261 flowpassfreq = x->x_samplerate/2;
262 }
263 if ( flowpassfreq < 0.0 )
264 {
265 flowpassfreq = 0.0;
266 }
267 x->x_lowpassfreq = flowpassfreq;
268 // post( "disto~ : lowpassfreq: %f", x->x_lowpassfreq );
269 disto_init_filters( x );
270 }
271
272 static void disto_lowpassQ(t_disto *x, t_floatarg flowpassQ )
273 {
274 if ( flowpassQ > 1.0 )
275 {
276 flowpassQ = 1.0;
277 }
278 if ( flowpassQ < 0.1 )
279 {
280 flowpassQ = 0.1;
281 }
282 x->x_lowpassQ = flowpassQ;
283 // post( "disto~ : lowpassQ: %f", x->x_lowpassQ );
284 disto_init_filters( x );
285 }
286
287 static t_int *disto_perform(t_int *w)
288 {
289 t_float *in = (t_float *)(w[1]);
290 t_float *out = (t_float *)(w[2]);
291 t_int n = (int)(w[3]);
292 t_disto *x = (t_disto*)(w[4]);
293 t_int i;
294
295 for (i = 0; i < n; i++)
296 {
297
298 x->HPF.x0 = *(in++);
299 x->HPF.y0 = (x->HPF.k0*x->HPF.x0+x->HPF.k1*x->HPF.x1+x->HPF.k2*x->HPF.x2-x->HPF.k3*x->HPF.y1-x->HPF.k4*x->HPF.y2);
300 x->HPF.y2 = x->HPF.y1;
301 x->HPF.y1 = x->HPF.y0;
302 x->HPF.x2 = x->HPF.x1;
303 x->HPF.x1 = x->HPF.x0;
304 x->data = (int)x->HPF.y0;
305
306 if ((x->data-x->pred)>0)
307 x->outval += (x->data*x->x_drymix+ x->exps[abs(x->data)]);
308 else if ((x->data-x->pred)<0)
309 x->outval += (x->data*x->x_drymix- x->exps[abs(x->data)]);
310 x->pred = x->data;
311
312 x->LPF.x0 = *(out);
313 x->LPF.y0 = (x->LPF.k0*x->LPF.x0+x->LPF.k1*x->LPF.x1+x->LPF.k2*x->LPF.x2-x->LPF.k3*x->LPF.y1-x->LPF.k4*x->LPF.y2);
314 x->LPF.y2 = x->LPF.y1;
315 x->LPF.y1 = x->LPF.y0;
316 x->LPF.x2 = x->LPF.x1;
317 x->LPF.x1 = x->LPF.x0;
318
319 x->outvol = x->LPF.y0*x->x_volume;
320
321 if(x->outvol > 1.0)
322 x->data = 1.0;
323 else if(x->outvol < -1.0)
324 x->data = -1.0;
325 else
326 x->data = x->outvol;
327
328 *(out++) = x->data;
329
330 x->outval *= x->x_feedback;
331
332 }
333
334 return (w+5);
335 }
336
337 static void disto_preset(t_disto *x, t_float pnumber)
338 {
339 switch ( (int)pnumber )
340 {
341 /* "Hard Distortion 100-10000Hz" */
342 case 1:
343 x->x_drive = 1.5;
344 x->x_drymix = 1.0;
345 x->x_wetmix = 0.5;
346 x->x_feedback = 0.0;
347 x->x_volume = 1.0;
348 x->x_hipassfreq = 100.0;
349 x->x_hipassQ = 0.5;
350 x->x_lowpassfreq = 10000.0;
351 x->x_lowpassQ = 0.5;
352 break;
353
354 /* "Hard Distortion 100-6000Hz" */
355 case 2:
356 x->x_drive = 1.5;
357 x->x_drymix = 1.0;
358 x->x_wetmix = 0.5;
359 x->x_feedback = 0.0;
360 x->x_volume = 1.0;
361 x->x_hipassfreq = 100.0;
362 x->x_hipassQ = 0.5;
363 x->x_lowpassfreq = 2000.0;
364 x->x_lowpassQ = 0.5;
365 break;
366
367 /* "Very Hard Distortion" */
368 case 3:
369 x->x_drive = 2.0;
370 x->x_drymix = 0.0;
371 x->x_wetmix = 1.0;
372 x->x_feedback = 1.0;
373 x->x_volume = 5.0;
374 x->x_hipassfreq = 100.0;
375 x->x_hipassQ = 0.5;
376 x->x_lowpassfreq = 6000.0;
377 x->x_lowpassQ = 0.5;
378 break;
379
380 /* "Medium Distortion 0.2" */
381 case 4:
382 x->x_drive = 0.2;
383 x->x_drymix = 1.0;
384 x->x_wetmix = 1.0;
385 x->x_feedback = 0.1;
386 x->x_volume = 1.0;
387 x->x_hipassfreq = 100.0;
388 x->x_hipassQ = 0.5;
389 x->x_lowpassfreq = 6000.0;
390 x->x_lowpassQ = 0.5;
391 break;
392
393 /* "Medium Distortion 0.8" */
394 case 5:
395 x->x_drive = 0.8;
396 x->x_drymix = 1.0;
397 x->x_wetmix = 1.0;
398 x->x_feedback = 0.1;
399 x->x_volume = 1.0;
400 x->x_hipassfreq = 100.0;
401 x->x_hipassQ = 1.0;
402 x->x_lowpassfreq = 6000.0;
403 x->x_lowpassQ = 0.5;
404 break;
405
406 /* "Soft Distortion 0.8" */
407 case 6:
408 x->x_drive = 0.8;
409 x->x_drymix = 0.4;
410 x->x_wetmix = 0.8;
411 x->x_feedback = 0.0;
412 x->x_volume = 0.5;
413 x->x_hipassfreq = 100.0;
414 x->x_hipassQ = 1.0;
415 x->x_lowpassfreq = 10000.0;
416 x->x_lowpassQ = 0.5;
417 break;
418
419 default:
420 post( "disto~ : unknown preset requested : %d", pnumber );
421 return;
422 break;
423 }
424 disto_init_filters( x );
425 }
426
427 static void disto_dsp(t_disto *x, t_signal **sp)
428 {
429 dsp_add(disto_perform, 4, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n, x );
430 }
431
432 /* clean up */
433 static void disto_free(t_disto *x)
434 {
435 if ( x->x_buf != NULL )
436 {
437 freebytes(x->x_buf, BFSZ*sizeof( double ) );
438 post( "Freed %d bytes", BFSZ*sizeof( double ) );
439 x->x_buf = NULL;
440 }
441 }
442
443 static void *disto_new(void)
444 {
445 t_disto *x = (t_disto *)pd_new(disto_class);
446 outlet_new(&x->x_obj, &s_signal);
447 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("drive"));
448 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("drymix"));
449 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("wetmix"));
450 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("feedback"));
451 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("volume"));
452 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("hipassfreq"));
453 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("hipassQ"));
454 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("lowpassfreq"));
455 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("lowpassQ"));
456
457 x->x_samplerate = (int)sys_getsr();
458 x->pred = 0.0;
459 x->data = 0.0;
460
461 if ( !( x->x_buf = ( double* ) getbytes( BFSZ*sizeof( double ) ) ) )
462 {
463 post ("disto~ : could not allocate buffer" );
464 return NULL;
465 }
466
467 // set default parameters
468 disto_preset( x, 3 );
469 disto_init_filters( x );
470
471 return (x);
472 }
473
474 void disto_tilde_setup(void)
475 {
•476 logpost(NULL, 4, "%s", disto_version );
477 disto_class = class_new(gensym("disto~"), (t_newmethod)disto_new, (t_method)disto_free,
478 sizeof(t_disto), 0, 0);
479
480 CLASS_MAINSIGNALIN( disto_class, t_disto, x_f );
481 class_addmethod(disto_class, (t_method)disto_drive, gensym("drive"), A_FLOAT, 0);
482 class_addmethod(disto_class, (t_method)disto_drymix, gensym("drymix"), A_FLOAT, 0);
483 class_addmethod(disto_class, (t_method)disto_wetmix, gensym("wetmix"), A_FLOAT, 0);
484 class_addmethod(disto_class, (t_method)disto_feedback, gensym("feedback"), A_FLOAT, 0);
485 class_addmethod(disto_class, (t_method)disto_volume, gensym("volume"), A_FLOAT, 0);
486 class_addmethod(disto_class, (t_method)disto_hipassfreq, gensym("hipassfreq"), A_FLOAT, 0);
487 class_addmethod(disto_class, (t_method)disto_hipassQ, gensym("hipassQ"), A_FLOAT, 0);
488 class_addmethod(disto_class, (t_method)disto_lowpassfreq, gensym("lowpassfreq"), A_FLOAT, 0);
489 class_addmethod(disto_class, (t_method)disto_lowpassQ, gensym("lowpassQ"), A_FLOAT, 0);
490 class_addmethod(disto_class, (t_method)disto_dsp, gensym("dsp"), 0);
491 class_addmethod(disto_class, (t_method)disto_preset, gensym("preset"), A_FLOAT, 0);
492 }