summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/synti/zynaddsubfx/Effects/Distorsion.C
blob: fa1b1163eb4790cb2eb493acd73dbec89f2a358e (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
/*
  ZynAddSubFX - a software synthesizer
 
  Distorsion.C - Distorsion effect
  Copyright (C) 2002-2005 Nasca Octavian Paul
  Author: Nasca Octavian Paul

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License 
  as published by the Free Software Foundation.

  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 (version 2) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Distorsion.h"


/*
 * Waveshape (this is called by OscilGen::waveshape and Distorsion::process)
 */

void waveshapesmps(int n,REALTYPE *smps,unsigned char type,unsigned char drive){
    int i;
    REALTYPE ws=drive/127.0;
    REALTYPE tmpv;

    switch(type){
	case 1:	ws=pow(10,ws*ws*3.0)-1.0+0.001;//Arctangent
		for (i=0;i<n;i++) 
		    smps[i]=atan(smps[i]*ws)/atan(ws);
		break;
	case 2:	ws=ws*ws*32.0+0.0001;//Asymmetric
		if (ws<1.0) tmpv=sin(ws)+0.1;
			else tmpv=1.1;
		for (i=0;i<n;i++) {
		    smps[i]=sin(smps[i]*(0.1+ws-ws*smps[i]))/tmpv;
		};
		break;
	case 3:	ws=ws*ws*ws*20.0+0.0001;//Pow
		for (i=0;i<n;i++) {
		    smps[i]*=ws;
		    if (fabs(smps[i])<1.0) {
			smps[i]=(smps[i]-pow(smps[i],3.0))*3.0;
			if (ws<1.0) smps[i]/=ws;
		    } else smps[i]=0.0;
		};
		break;
	case 4:	ws=ws*ws*ws*32.0+0.0001;//Sine
		if (ws<1.57) tmpv=sin(ws);
			else tmpv=1.0;
		for (i=0;i<n;i++) smps[i]=sin(smps[i]*ws)/tmpv;
		break;
	case 5:	ws=ws*ws+0.000001;//Quantisize
		for (i=0;i<n;i++) 
		    smps[i]=floor(smps[i]/ws+0.5)*ws;
		break;
	case 6:	ws=ws*ws*ws*32+0.0001;//Zigzag
		if (ws<1.0) tmpv=sin(ws);
			else tmpv=1.0;
		for (i=0;i<n;i++) 
  		    smps[i]=asin(sin(smps[i]*ws))/tmpv;
		break;
	case 7:	ws=pow(2.0,-ws*ws*8.0); //Limiter
		for (i=0;i<n;i++) {
		    REALTYPE tmp=smps[i];
		    if (fabs(tmp)>ws) {
			if (tmp>=0.0) smps[i]=1.0;
			    else smps[i]=-1.0;
		    } else smps[i]/=ws;
		};
		break;
	case 8:	ws=pow(2.0,-ws*ws*8.0); //Upper Limiter
		for (i=0;i<n;i++) {
		    REALTYPE tmp=smps[i];
		    if (tmp>ws) smps[i]=ws;
		    smps[i]*=2.0;
		};
		break;
	case 9:	ws=pow(2.0,-ws*ws*8.0); //Lower Limiter
		for (i=0;i<n;i++) {
		    REALTYPE tmp=smps[i];
		    if (tmp<-ws) smps[i]=-ws;
		    smps[i]*=2.0;
		};
		break;
	case 10:ws=(pow(2.0,ws*6.0)-1.0)/pow(2.0,6.0); //Inverse Limiter
		for (i=0;i<n;i++) {
		    REALTYPE tmp=smps[i];
		    if (fabs(tmp)>ws) {
			if (tmp>=0.0) smps[i]=tmp-ws;
			    else smps[i]=tmp+ws;
		    } else smps[i]=0;
		};
		break;
	case 11:ws=pow(5,ws*ws*1.0)-1.0;//Clip
		for (i=0;i<n;i++) 
		    smps[i]=smps[i]*(ws+0.5)*0.9999-floor(0.5+smps[i]*(ws+0.5)*0.9999);
		break;
	case 12:ws=ws*ws*ws*30+0.001;//Asym2
		if (ws<0.3) tmpv=ws;
		   else tmpv=1.0;
		for (i=0;i<n;i++) {
		    REALTYPE tmp=smps[i]*ws;
		    if ((tmp>-2.0) && (tmp<1.0)) smps[i]=tmp*(1.0-tmp)*(tmp+2.0)/tmpv;
			else smps[i]=0.0;
		};
		break;
	case 13:ws=ws*ws*ws*32.0+0.0001;//Pow2
		if (ws<1.0) tmpv=ws*(1+ws)/2.0;
			else tmpv=1.0;
		for (i=0;i<n;i++) {
		    REALTYPE tmp=smps[i]*ws;
		    if ((tmp>-1.0)&&(tmp<1.618034)) smps[i]=tmp*(1.0-tmp)/tmpv;
			else if (tmp>0.0) smps[i]=-1.0;
				else smps[i]=-2.0;
		};
		break;
	case 14:ws=pow(ws,5.0)*80.0+0.0001;//sigmoid
		if (ws>10.0) tmpv=0.5;
		    else tmpv=0.5-1.0/(exp(ws)+1.0);
		for (i=0;i<n;i++) {
		    REALTYPE tmp=smps[i]*ws;
		    if (tmp<-10.0) tmp=-10.0;
			else if (tmp>10.0) tmp=10.0;
		    tmp=0.5-1.0/(exp(tmp)+1.0);
		    smps[i]=tmp/tmpv;
		};
		break;
	//update to Distorsion::changepar (Ptype max) if there is added more waveshapings functions
    };

};


Distorsion::Distorsion(int insertion_,REALTYPE *efxoutl_,REALTYPE *efxoutr_){
    efxoutl=efxoutl_;
    efxoutr=efxoutr_;

    lpfl=new AnalogFilter(2,22000,1,0);
    lpfr=new AnalogFilter(2,22000,1,0);
    hpfl=new AnalogFilter(3,20,1,0);
    hpfr=new AnalogFilter(3,20,1,0);
    
    filterpars=NULL;

    insertion=insertion_;
    //default values
    Ppreset=0;
    Pvolume=50;
    Plrcross=40; 
    Pdrive=90;
    Plevel=64;
    Ptype=0;
    Pnegate=0;
    Plpf=127;
    Phpf=0;
    Pstereo=0;
    Pprefiltering=0;

    setpreset(Ppreset);    	   
    cleanup();
};

Distorsion::~Distorsion(){
    delete (lpfl);
    delete (lpfr);
    delete (hpfl);
    delete (hpfr);
    
};

/*
 * Cleanup the effect
 */
void Distorsion::cleanup(){
    lpfl->cleanup();
    hpfl->cleanup();
    lpfr->cleanup();
    hpfr->cleanup();
};


/*
 * Apply the filters
 */

void Distorsion::applyfilters(REALTYPE *efxoutl,REALTYPE *efxoutr){
    lpfl->filterout(efxoutl);
    hpfl->filterout(efxoutl);
    if (Pstereo!=0){//stereo
	lpfr->filterout(efxoutr);
        hpfr->filterout(efxoutr);
    };

};


/*
 * Effect output
 */
void Distorsion::out(REALTYPE *smpsl,REALTYPE *smpsr){
    int i;
    REALTYPE l,r,lout,rout;

    REALTYPE inputvol=pow(5.0,(Pdrive-32.0)/127.0);
    if (Pnegate!=0) inputvol*=-1.0;
    
    if (Pstereo!=0){//Stereo
	for (i=0;i<SOUND_BUFFER_SIZE;i++){
	    efxoutl[i]=smpsl[i]*inputvol*panning;
	    efxoutr[i]=smpsr[i]*inputvol*(1.0-panning);
	};
    } else {
	for (i=0;i<SOUND_BUFFER_SIZE;i++){
	    efxoutl[i]=( smpsl[i]*panning + smpsr[i]*(1.0-panning) ) * inputvol;
	};
    };
    
    if (Pprefiltering!=0) applyfilters(efxoutl,efxoutr);

    //no optimised, yet (no look table)
    waveshapesmps(SOUND_BUFFER_SIZE,efxoutl,Ptype+1,Pdrive);
    if (Pstereo!=0) waveshapesmps(SOUND_BUFFER_SIZE,efxoutr,Ptype+1,Pdrive);

    if (Pprefiltering==0) applyfilters(efxoutl,efxoutr);
    
    if (Pstereo==0) for (i=0;i<SOUND_BUFFER_SIZE;i++) efxoutr[i]=efxoutl[i];
    
    REALTYPE level=dB2rap(60.0*Plevel/127.0-40.0);
    for (i=0;i<SOUND_BUFFER_SIZE;i++){
	lout=efxoutl[i];
	rout=efxoutr[i];
	l=lout*(1.0-lrcross)+rout*lrcross;
	r=rout*(1.0-lrcross)+lout*lrcross;
	lout=l;rout=r;
	
	efxoutl[i]=lout*2.0*level;
	efxoutr[i]=rout*2.0*level;

    };
    
};


/*
 * Parameter control
 */
void Distorsion::setvolume(unsigned char Pvolume){
    this->Pvolume=Pvolume;

    if (insertion==0) {
	outvolume=pow(0.01,(1.0-Pvolume/127.0))*4.0;
	volume=1.0;
    } else {
	volume=outvolume=Pvolume/127.0;
    };
    if (Pvolume==0) cleanup();

};

void Distorsion::setpanning(unsigned char Ppanning){
    this->Ppanning=Ppanning;
    panning=(Ppanning+0.5)/127.0;
};


void Distorsion::setlrcross(unsigned char Plrcross){
    this->Plrcross=Plrcross;
    lrcross=Plrcross/127.0*1.0;
};

void Distorsion::setlpf(unsigned char Plpf){
    this->Plpf=Plpf;
    REALTYPE fr=exp(pow(Plpf/127.0,0.5)*log(25000.0))+40;
    lpfl->setfreq(fr);
    lpfr->setfreq(fr);
};

void Distorsion::sethpf(unsigned char Phpf){
    this->Phpf=Phpf;
    REALTYPE fr=exp(pow(Phpf/127.0,0.5)*log(25000.0))+20.0;
    hpfl->setfreq(fr);
    hpfr->setfreq(fr);
};


void Distorsion::setpreset(unsigned char npreset){
    const int PRESET_SIZE=11;
    const int NUM_PRESETS=6;
    unsigned char presets[NUM_PRESETS][PRESET_SIZE]={
	//Overdrive 1
	{127,64,35,56,70,0,0,96,0,0,0},
	//Overdrive 2
	{127,64,35,29,75,1,0,127,0,0,0},
	//A. Exciter 1
	{64,64,35,75,80,5,0,127,105,1,0},
	//A. Exciter 2
	{64,64,35,85,62,1,0,127,118,1,0},
	//Guitar Amp
	{127,64,35,63,75,2,0,55,0,0,0},
	//Quantisize
	{127,64,35,88,75,4,0,127,0,1,0}};


    if (npreset>=NUM_PRESETS) npreset=NUM_PRESETS-1;
    for (int n=0;n<PRESET_SIZE;n++) changepar(n,presets[npreset][n]);
    if (insertion==0) changepar(0,(int) (presets[npreset][0]/1.5));//lower the volume if this is system effect
    Ppreset=npreset;
    cleanup();
};


void Distorsion::changepar(int npar,unsigned char value){
    switch (npar){
	case 0: setvolume(value);
		break;
	case 1: setpanning(value);
		break;
	case 2: setlrcross(value);
		break;
	case 3: Pdrive=value;
		break;
	case 4: Plevel=value;
		break;
	case 5: if (value>13) value=13;//this must be increased if more distorsion types are added
		Ptype=value;
		break;
	case 6: if (value>1) value=1;
		Pnegate=value;
		break;
	case 7: setlpf(value);
		break;
	case 8: sethpf(value);
		break;
	case 9: if (value>1) value=1;
		Pstereo=value;
		break;
	case 10:Pprefiltering=value;
		break;
    };
};

unsigned char Distorsion::getpar(int npar){
    switch (npar){
	case 0: return(Pvolume);
		break;
	case 1: return(Ppanning);
		break;
	case 2: return(Plrcross);
		break;
	case 3: return(Pdrive);
		break;
	case 4: return(Plevel);
		break;
	case 5: return(Ptype);
		break;
	case 6: return(Pnegate);
		break;
	case 7: return(Plpf);
		break;
	case 8: return(Phpf);
		break;
	case 9: return(Pstereo);
		break;
	case 10:return(Pprefiltering);
		break;
    };
    return(0);//in case of bogus parameter number
};