File: DistortionUGens.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.7.1~repack-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,332 kB
  • ctags: 11,704
  • sloc: cpp: 140,180; lisp: 14,746; ansic: 2,133; xml: 86; makefile: 82; haskell: 21; sh: 8
file content (350 lines) | stat: -rw-r--r-- 7,840 bytes parent folder | download
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
#include "SC_PlugIn.h"
#include "math.h"

#define fSampleRate (float)SAMPLERATE // simple cast for samplerate to be float
#define LIN_INTERP(f,a,b) ((a) + (f) * ((b) - (a))) // from ladspa-util.h


static InterfaceTable *ft;

//----------------------------------------------------------

struct CrossoverDistortion : public Unit
{
  // needed ? - yes... especially since SCs control period tends to be large
  float mAmp;
  float mSmooth;
    float mRfade;
};

struct SmoothDecimator  : public Unit
{
  float mAccum;
  float *mBuffer;
  int mBufferPos;
};


struct Decimator : public Unit
{
  float mCount;
  float mLastOut;
};

struct SineShaper : public Unit
{
  // anything needed ?
};

struct Disintegrator : public Unit
{
  float mLastInput;
  bool mActive;
};

//----------------------------------------------------------

extern "C"

{
  void load(InterfaceTable *inTable);


  // from ladspa-util.h - should me make an extra header file for this?
  static inline float f_clamp(float x, float a, float b);
  static inline float cube_interp(const float fr, const float inm1,
				  const float in, const float inp1, const float inp2);

  // Crossover Distortion
  void CrossoverDistortion_next(CrossoverDistortion *unit, int inNumSamples);
  void CrossoverDistortion_Ctor(CrossoverDistortion* unit);

  // Smooth Decimator
  void SmoothDecimator_next(SmoothDecimator *unit, int inNumSamples);
  void SmoothDecimator_Ctor(SmoothDecimator* unit);
  void SmoothDecimator_Dtor(SmoothDecimator* unit);

  // Decimator
  void Decimator_next(Decimator *unit, int inNumSamples);
  void Decimator_Ctor(Decimator* unit);

  // SineShaper
  void SineShaper_next(SineShaper *unit, int inNumSamples);
  void SineShaper_Ctor(SineShaper* unit);

  // Disintegrator
  void Disintegrator_next(Disintegrator *unit, int inNumSamples);
  void Disintegrator_Ctor(Disintegrator* unit);
};

//----------------------------------------------------------
// utilities
//----------------------------------------------------------

// from ladspa-util.h
static inline float f_clamp(float x, float a, float b)
{
	const float x1 = fabs(x - a);
	const float x2 = fabs(x - b);

	x = x1 + a + b;
	x -= x2;
	x *= 0.5;

	return x;
}

// from ladspa-util.h
static inline float cube_interp(const float fr, const float inm1,
				const float in, const float inp1, const float inp2)
{
  return in + 0.5f * fr * (inp1 - inm1 +
			   fr * (4.0f * inp1 + 2.0f * inm1 - 5.0f * in - inp2 +
				 fr * (3.0f * (in - inp1) - inm1 + inp2)));
}

//----------------------------------------------------------

void CrossoverDistortion_Ctor(CrossoverDistortion* unit)

{
  SETCALC(CrossoverDistortion_next);
    unit->mAmp = IN0(1);
    unit->mSmooth = IN0(2);
    unit->mRfade = 1 / (fabs(unit->mAmp * unit->mSmooth));
  CrossoverDistortion_next(unit, 1);
}

void SmoothDecimator_Ctor(SmoothDecimator* unit)
{
  unit->mAccum = 0.0f;
  unit->mBuffer = (float*)RTAlloc(unit->mWorld, 8 * sizeof(float));
  unit->mBufferPos = 0;

  SETCALC(SmoothDecimator_next);
  SmoothDecimator_next(unit, 1);
}

void SmoothDecimator_Dtor(SmoothDecimator* unit)
{
  RTFree(unit->mWorld,unit->mBuffer);
}

// Decimator
void Decimator_Ctor(Decimator* unit)
{
  unit->mCount = 0.0f;
  unit->mLastOut = 0.0f;

  SETCALC(Decimator_next);
  Decimator_next(unit,1);
}

// SineShaper
void SineShaper_Ctor(SineShaper* unit)
{
  SETCALC(SineShaper_next);
  SineShaper_next(unit,1);
}

// Disintegrator
void Disintegrator_Ctor(Disintegrator* unit)
{
  unit->mLastInput = 0.0f;
  unit->mActive = false;

  SETCALC(Disintegrator_next);
  Disintegrator_next(unit,1);
}

//----------------------------------------------------------

void CrossoverDistortion_next(CrossoverDistortion *unit, int inNumSamples)
{

  float *out = OUT(0);
  float *in = IN(0);
  float nextAmp = IN0(1);
  float nextSmooth = IN0(2);
  float sig;
    float amp, smooth, rfadeSlope, ampSlope, smoothSlope;
    ampSlope = smoothSlope = rfadeSlope = 0.0f;
    amp = unit->mAmp;
    smooth = unit->mSmooth;
    float rfade = unit->mRfade;

//    float fade = fabs(amp * smooth);
//    float rfade = 1./fade;
    // see if we need to interpolate over the control period, and avoid the constant 1/fade divide!
    if((nextAmp != amp) || (nextSmooth != smooth)){
	float fade = fabs(amp * smooth);
	unit->mRfade = 1./fade;
	rfadeSlope = CALCSLOPE(unit->mRfade, rfade);
	ampSlope = CALCSLOPE(nextAmp, amp);
	smoothSlope = CALCSLOPE(nextSmooth, smooth);
	unit->mAmp = nextAmp;
	unit->mSmooth = nextSmooth;
	}

  for (int i=0; i < inNumSamples; ++i)
    {
	if((nextAmp != amp) || (nextSmooth != smooth)){


	    sig = fabs(in[i]) - amp;

	    if (sig < 0.0f) {
		sig *= (1.0f + (sig*rfade)) * smooth;
	    }

	    if (in[i] < 0.0f) {
		out[i] = -sig;
	    } else {
		out[i] = sig;
	    }
	    rfade += rfadeSlope;
	    amp += ampSlope;
	    smooth += smoothSlope;
	} else {


	sig = fabs(in[i]) - amp;

      if (sig < 0.0f) {
	sig *= (1.0f + (sig*rfade)) * smooth;
      }

      if (in[i] < 0.0f) {
	out[i] = -sig;
      } else {
	out[i] = sig;
      }
	}
    }
}


void SmoothDecimator_next(SmoothDecimator *unit, int inNumSamples)
{
  float *in = IN(0); //our signal input
  float *out = OUT(0); //out signal output
  float rate = IN0(1);
  float smooth = IN0(2);

  float smoothed;
  float accum = unit->mAccum;
  float * buffer = unit->mBuffer;
  int buffer_pos = unit->mBufferPos;

  float inc = (rate / fSampleRate);
  inc = f_clamp(inc, 0.0f, 1.0f);

  for (int pos = 0; pos < inNumSamples; pos++) {
    accum += inc;
    if (accum >= 1.0f) {
      accum -= 1.0f;
      buffer_pos = (buffer_pos + 1) & 7;
      buffer[buffer_pos] = in[pos];
    }
    smoothed = cube_interp(accum, buffer[(buffer_pos - 3) & 7],
			   buffer[(buffer_pos - 2) & 7],
			   buffer[(buffer_pos - 1) & 7],
			   buffer[buffer_pos]);
    out[pos] = LIN_INTERP(smooth, buffer[(buffer_pos - 3) & 7], smoothed);
  }

  unit->mAccum = accum;
  unit->mBufferPos = buffer_pos;
}

void Decimator_next(Decimator *unit, int inNumSamples)
{
  float *in = IN(0);
  float *out = OUT(0);
  float rate = IN0(1);
  float bits = IN0(2);

  float count = unit->mCount;
  float last_out = unit->mLastOut;
  long sample_rate = fSampleRate;
  float step, stepr, delta, ratio;
  double dummy;

  if (bits >= 31.0f || bits < 1.0f) {
    step = 0.0f;
    stepr = 1.0f;
  } else {
    step = pow(0.5f, bits - 0.999f);
    stepr = 1/step;
  }

  if (rate >= sample_rate) {
    ratio = 1.0f;
  } else {
    ratio = rate/sample_rate;
  }

  for (int pos = 0; pos < inNumSamples; pos++) {
    count += ratio;

    if (count >= 1.0f) {
      count -= 1.0f;
      delta = modf((in[pos] + (in[pos]<0?-1.0:1.0)*step*0.5) * stepr, &dummy) * step;
      last_out = in[pos] - delta;
      out[pos] = last_out;
    } else {
      out[pos] = last_out;
    }
  }

  unit->mLastOut = last_out;
  unit->mCount = count;
}

void SineShaper_next(SineShaper *unit, int inNumSamples)
{
  float *in = IN(0);
  float *out = OUT(0);
  float limit = IN0(1);

  float oneOverLimit = 1.f / limit;
  for(int i = 0; i < inNumSamples; i++)
    {
      out[i] = limit * sin(in[i] * oneOverLimit);
    }
}

void Disintegrator_next(Disintegrator *unit, int inNumSamples)
{
  float *in = IN(0);
  float *out = OUT(0);
  float prob = IN0(1);
  float mult = IN0(2);

  for(int i = 0; i < inNumSamples; i++)
    {
      if( (unit->mLastInput>0 && in[i]<0) || (unit->mLastInput<0 && in[i]>0))
	unit->mActive = rand() < prob*RAND_MAX;

      unit->mLastInput = in[i];

      if(unit->mActive)
	out[i] = in[i] * mult;
      else
	out[i] = in[i];
    }
}


//----------------------------------------------------------

PluginLoad(Ladspa)
{
  ft = inTable;

  DefineSimpleUnit(CrossoverDistortion);
  DefineSimpleUnit(SmoothDecimator);
  DefineSimpleUnit(Decimator);
  DefineSimpleUnit(SineShaper);
  DefineSimpleUnit(Disintegrator);
}