File: Dual_Flange.C

package info (click to toggle)
rakarrack 0.6.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 10,136 kB
  • sloc: cpp: 27,705; sh: 794; makefile: 377
file content (442 lines) | stat: -rw-r--r-- 11,243 bytes parent folder | download | duplicates (4)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
  Rakarrack Audio FX
 
  Dual_Flange.C - Super Flanger
  Copyright (C) 2010 Ryan Billing
  Authors: 
  Ryan Billing (a.k.a Transmogrifox)  --  Signal Processing
  Copyright (C) 2010 Ryan Billing

  Nasca Octavian Paul -- Remnants of ZynAddSubFX Echo.h structure and utility routines common to ZynSubFX source
  Copyright (C) 2002-2005 Nasca Octavian Paul

  Higher intensity flanging accomplished by picking two points out of the delay line to create a wider notch filter.
    
  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 "Dual_Flange.h"

Dflange::Dflange (float * efxoutl_, float * efxoutr_)
{
  efxoutl = efxoutl_;
  efxoutr = efxoutr_;
  
  period_const = 1.0f/fPERIOD;
  
  //default values
  Ppreset = 0;

  ldelay = NULL;
  rdelay = NULL;

  
  maxx_delay = (int) SAMPLE_RATE * D_FLANGE_MAX_DELAY;
  ldelay = new float[maxx_delay];  
  rdelay = new float[maxx_delay];
  zldelay = new float[maxx_delay];  
  zrdelay = new float[maxx_delay];  
  fsubtract = 0.5f;
  fhidamp = 1.0f;
  fwidth = 800;
  fdepth = 50;
  zcenter = (int) floorf(0.5f * (fdepth + fwidth));
  base = 7.0f;		//sets curve of modulation to frequency relationship
  ibase = 1.0f/base;
  //default values
  Ppreset = 0;  
  rsA = 0.0f;
  rsB = 0.0f; 
  lsA  = 0.0f;
  lsB = 0.0f;
  setpreset (Ppreset);
  cleanup ();
};

Dflange::~Dflange ()
{
};

/*
 * Cleanup the effect
 */
void
Dflange::cleanup ()
{
  int i;
  for (i = 0; i < maxx_delay; i++)
  {
    ldelay[i] = 0.0;
    rdelay[i] = 0.0;   
    zldelay[i] = 0.0;
    zrdelay[i] = 0.0;       
  };
  
  //loop variables
  l = 0.0f;
  r = 0.0f;
  ldl = 0.0f;
  rdl = 0.0f;
  rflange0 = 0.0f;
  lflange0 = 0.0f;
  rflange1 = 0.0f;
  lflange1 = 0.0f;
  
};



/*
 * Effect output
 */
void
Dflange::out (float * smpsl, float * smpsr)
{
  int i;
  //deal with LFO's
    int tmp0, tmp1;

    float lfol, lfor, lmod, rmod, lmodfreq, rmodfreq, rx0, rx1, lx0, lx1;
    float ldif0, ldif1, rdif0, rdif1;  //Difference between fractional delay and floor(fractional delay)
    float drA, drB, dlA, dlB;	//LFO inside the loop.
    
  lfo.effectlfoout (&lfol, &lfor);
  lmod = lfol;
  rmod = lfor;
//  lmod = (powf (2.0f, lmod*LOG_FMAX) - 1.0f) * LFO_CONSTANT;  //2^x type sweep for musical interpretation of moving delay line.
//  rmod = (powf (2.0f, rmod*LOG_FMAX) - 1.0f) * LFO_CONSTANT;

  lmodfreq = fdepth + fwidth*(powf(base, lmod) - 1.0f)*ibase;	//sets frequency of lowest notch. // 20 <= fdepth <= 4000 // 20 <= width <= 16000 //
  rmodfreq = fdepth + fwidth*(powf(base, rmod) - 1.0f)*ibase;


  if (lmodfreq > 10000.0f)
    lmodfreq = 10000.0f;
  else if (lmodfreq < 10.0f)
    lmodfreq = 10.0f;
  if (rmodfreq > 10000.0)
    rmodfreq = 10000.0f;
  else if (rmodfreq < 10.0f)
    rmodfreq = 10.0f;

 rflange0 = fSAMPLE_RATE * 0.5f/rmodfreq;		//Turn the notch frequency into a number for delay
 rflange1 = rflange0 * foffset;				//Set relationship of second delay line
 lflange0 = fSAMPLE_RATE * 0.5f/lmodfreq;
 lflange1 = lflange0 * foffset;
 
 //now is a delay expressed in number of samples.  Number here
 //will be fractional, but will use linear interpolation inside the loop to make a decent guess at 
 //the numbers between samples.
 
 rx0 = (rflange0 - oldrflange0) * period_const;  //amount to add each time around the loop.  Less processing of linear LFO interp inside the loop.
 rx1 =  (rflange1 - oldrflange1) * period_const;
 lx0 = (lflange0 - oldlflange0) * period_const;
 lx1  = (lflange1 - oldlflange1) * period_const; 
 // Now there is a fractional amount to add 
 
 drA = oldrflange0;
 drB = oldrflange1;
 dlA = oldlflange0;
 dlB = oldlflange1;
  // dr, dl variables are the LFO inside the loop.

 oldrflange0 = rflange0;
 oldrflange1 = rflange1;
 oldlflange0 = lflange0;
 oldlflange1 = lflange1;
  //lfo ready...


  for (i = 0; i < PERIOD; i++)
    {
    
    //Delay line utility
      ldl = ldelay[kl];
      rdl = rdelay[kr];
      l = ldl * flrcross + rdl * frlcross;
      r = rdl * flrcross + ldl * frlcross;
      ldl = l;
      rdl = r;
      ldl = smpsl[i] * lpan - ldl * ffb;
      rdl = smpsr[i] * rpan - rdl * ffb;
      
      
      //LowPass Filter
      ldelay[kl] = ldl = ldl * (1.0f - fhidamp) + oldl * fhidamp;
      rdelay[kr] = rdl = rdl * (1.0f - fhidamp) + oldr * fhidamp;
      oldl = ldl + DENORMAL_GUARD;
      oldr = rdl + DENORMAL_GUARD;
  
      if(Pzero)
      {    
      //Offset zero reference delay
      zdl = zldelay[zl];
      zdr = zrdelay[zr];
      zldelay[zl] = smpsl[i];
      zrdelay[zr] = smpsr[i];   
      if (--zl < 0)   //Cycle delay buffer in reverse so delay time can be indexed directly with addition 
	zl =  zcenter;
      if (--zr < 0)
	zr =  zcenter;  
      }

	//End delay line management, start flanging:
	
	//Right Channel, delay A
	rdif0 = drA - floor(drA);
	tmp0 = (kr + (int) floor(drA)) %  maxx_delay;
	tmp1 = tmp0 + 1;
	if (tmp1 >= maxx_delay) tmp1 =  0;	
	//rsA = rdelay[tmp0] + rdif0 * (rdelay[tmp1] - rdelay[tmp0] );	//here is the first right channel delay
	rsA = rdelay[tmp1] + rdif0 * (rdelay[tmp0] - rsA );	//All-pass interpolator
	
	//Right Channel, delay B	
	rdif1 = drB - floor(drB);
	tmp0 = (kr + (int) floor(drB)) %  maxx_delay;
	tmp1 = tmp0 + 1;
	if (tmp1 >= maxx_delay) tmp1 =  0;
	//rsB = rdelay[tmp0] + rdif1 * (rdelay[tmp1] - rdelay[tmp0]);	//here is the second right channel delay	
	rsB = rdelay[tmp1] + rdif1 * (rdelay[tmp0] - rsB );
		
		//Left Channel, delay A
	ldif0 = dlA - floor(dlA);
	tmp0 = (kl + (int) floor(dlA)) %  maxx_delay;
	tmp1 = tmp0 + 1;
	if (tmp1 >= maxx_delay) tmp1 =  0;
	//lsA = ldelay[tmp0] + ldif0 * (ldelay[tmp1] - ldelay[tmp0]);	//here is the first left channel delay
	lsA = ldelay[tmp1] + ldif0 * (ldelay[tmp0] - lsA );	
	
	//Left Channel, delay B	
	ldif1 = dlB - floor(dlB);
	tmp0 = (kl + (int) floor(dlB)) %  maxx_delay;
	tmp1 = tmp0 + 1;
	if (tmp1 >= maxx_delay) tmp1 =  0;
	//lsB = ldelay[tmp0] + ldif1 * (ldelay[tmp1] - ldelay[tmp0]);	//here is the second leftt channel delay
	lsB = ldelay[tmp1] + ldif1 * (ldelay[tmp0] - lsB );		
	//End flanging, next process outputs

	if(Pzero)
	{
      efxoutl[i]= dry * smpsl[i] +  fsubtract * wet * (fsubtract * (lsA + lsB)  + zdl);    // Make final FX out mix
      efxoutr[i]= dry * smpsr[i] +  fsubtract * wet * (fsubtract * (rsA + rsB)  + zdr);
       }
       else
       {
      efxoutl[i]= dry * smpsl[i] +  wet * fsubtract * (lsA + lsB);    // Make final FX out mix
      efxoutr[i]= dry * smpsr[i] +  wet * fsubtract * (rsA + rsB);       
       }
     


      
      if (--kl < 0)   //Cycle delay buffer in reverse so delay time can be indexed directly with addition 
	kl =  maxx_delay;
      if (--kr < 0)
	kr =  maxx_delay;



// Increment LFO
 drA += rx0;
 drB += rx1;
 dlA += lx0;
 dlB += lx1;
 
    };
    


};


/*
 * Parameter control
 */

void
Dflange::changepar (int npar, int value)
{
  switch (npar)
    {
    case 0:
      Pwetdry = value;
      dry = (float) (Pwetdry+64) /128.0f;
      wet = 1.0f - dry;
      break;
    case 1:
      Ppanning = value;
      if (value < 0)
      {
      rpan = 1.0f + (float) Ppanning/64.0;     
      lpan = 1.0f;
      }
      else 
      {
      lpan = 1.0f - (float) Ppanning/64.0;   
      rpan = 1.0f;
      };
      break;
    case 2:
      Plrcross = value;
      flrcross = (float) Plrcross/127.0;
      frlcross = 1.0f - flrcross;	//keep this out of the DSP loop
      break;
    case 3:
      Pdepth = value;
      fdepth =  (float) Pdepth;
        zcenter = (int) floor(0.5f * (fdepth + fwidth));

      break;
    case 4:
      Pwidth = value;
      fwidth = (float) Pwidth;
        zcenter = (int) floor(0.5f * (fdepth + fwidth));

      break;
    case 5:
      Poffset = value;
      foffset = 0.5f + (float) Poffset/255.0; 
      break;
    case 6:
      Pfb = value;
      ffb = (float) Pfb/64.5f;  
      break;
    case 7:
      Phidamp = value;
      fhidamp = expf(-D_PI * (float) Phidamp/fSAMPLE_RATE);
      break;
    case 8:
      Psubtract = value;
      fsubtract = 0.5f;
      if(Psubtract) fsubtract = -0.5f;  //In loop a mult by 0.5f is necessary, so this kills 2 birds with 1...
      break;      
     case 9:
      Pzero = value;
      if (Pzero) fzero = 1.0f;
      break;   
     case 10:
      lfo.Pfreq = value;
      lfo.updateparams ();
      break;      
     case 11:
      lfo.Pstereo = value;
      lfo.updateparams ();
      break;   
     case 12:
      lfo.PLFOtype = value;
      lfo.updateparams ();
      break;   
     case 13:
      lfo.Prandomness = value;
      lfo.updateparams ();  
      break;              
    };
};

int
Dflange::getpar (int npar)
{
  switch (npar)
    {
    case 0:
      return (Pwetdry);
      break;
    case 1:
      return (Ppanning);
      break;
    case 2:
      return (Plrcross);
      break;
    case 3:
      return (Pdepth);
      break;
    case 4:
      return (Pwidth);
      break;
    case 5:
      return (Poffset);
      break;
    case 6:
      return (Pfb);
      break;
    case 7:
      return (Phidamp);
      break;
    case 8:
      return (Psubtract);
      break;      
     case 9:
      return (Pzero);
      break;
     case 10:
      return (lfo.Pfreq);
      break;      
     case 11:
      return (lfo.Pstereo);
      break;      
     case 12:
      return (lfo.PLFOtype);
      break;            
     case 13:
      return (lfo.Prandomness);
      break;
    };
  return (0);			//in case of bogus parameter number
};


void
Dflange::setpreset (int npreset)
{
  const int PRESET_SIZE = 14;
  const int NUM_PRESETS = 9;
  int presets[NUM_PRESETS][PRESET_SIZE] = {
    //Preset 1
    {-32, 0, 0, 110, 800, 10, -27, 16000, 1, 0, 24, 64, 1, 10},
    //Flange-Wha
    {0, 0, 64, 500, 3000, 50, -40, 8000, 1, 0, 196, 96, 0, 0},
    //FbFlange
    {0, 0, 64, 68, 75, 50, -50, 8000, 0, 1, 23, 96, 5, 0},
    //SoftFlange
    {-32, 0, 64, 60, 10, 100, 20, 16000, 0, 0, 16, 90, 4, 0},
    //Flanger
    {-32, 0, 64, 170, 1200, 50, 0, 16000, 1, 0, 68, 127, 0, 0},
    //Chorus 1
    {-15, 0, 0, 42, 12, 50, -10, 1500, 0, 0, 120, 0, 0, 20},
    //Chorus 2
    {-40, 0, 0, 35, 9, 67, 12, 4700, 1, 1, 160, 75, 0, 60},
    //Preset 8
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    //Preset 9
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  };

  if(npreset>NUM_PRESETS-1)  
    {   
     Fpre->ReadPreset(20,npreset-NUM_PRESETS+1);    
     for (int n = 0; n < PRESET_SIZE; n++)    
     changepar (n, pdata[n]);    
    }    
  else                                      
  {     

  for (int n = 0; n < PRESET_SIZE; n++)
    changepar (n, presets[npreset][n]);
  }
  Ppreset = npreset;
};