File: complexRes.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.13.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,104 kB
  • sloc: cpp: 303,352; lisp: 9,589; ansic: 3,547; sh: 96; makefile: 87; haskell: 21
file content (259 lines) | stat: -rw-r--r-- 6,841 bytes parent folder | download | duplicates (5)
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
//-----------------------------------------------------
// name: "ComplexRes"
// version: "1.0"
// author: "Julian Parker & Till Bovermann"
// license: "GPL2+"
// copyright: "(c) Julian Parker & Till Bovermann 2013"
//-----------------------------------------------------

#include "SC_PlugIn.h"
#define TWOPI 6.283185307179586
#define PI 3.141592653589793

// InterfaceTable contains pointers to functions in the host (server).
static InterfaceTable *ft;

// declare struct to hold unit generator state
struct ComplexRes : public Unit
{
	double mFreq;
	double mcoeffX;
	double mcoeffY;
	double mDecay; // Exponential decay time constant in seconds
	double mRes; // Filter resonance coefficient (0...1)
	double mX; // First state (real part)
	double mY; // Second state (imaginary part)
	double mNormCoeff; // Normalisation gain
	double mAng;
};

// declare unit generator functions
static void ComplexRes_next_ak(ComplexRes *unit, int inNumSamples);
static void ComplexRes_next_aa(ComplexRes *unit, int inNumSamples);
static void ComplexRes_next_kk(ComplexRes *unit, int inNumSamples);
static void ComplexRes_next_ka(ComplexRes *unit, int inNumSamples);
static void ComplexRes_Ctor(ComplexRes* unit);


//////////////////////////////////////////////////////////////////
void ComplexRes_Ctor(ComplexRes* unit)
{
	// 1. set the calculation function.
	if (INRATE(1) == calc_FullRate) {
		// if the frequency argument is audio rate
		if (INRATE(2) == calc_FullRate) {
			SETCALC(ComplexRes_next_aa);
		} else {
			SETCALC(ComplexRes_next_ak);
		}
	} else {
		// if the frequency argument is control rate (or a scalar).
		if (INRATE(2) == calc_FullRate) {
			SETCALC(ComplexRes_next_ka);
		} else {
			SETCALC(ComplexRes_next_kk);
		}		
	}

	// 2. initialize the unit generator state variables.
	unit->mDecay = IN0(2);
	unit->mRes = exp(-1.0/(unit->mDecay * SAMPLERATE)); // Filter resonance coefficient (0...1)
	unit->mX = 0.0; // First state (real part)
	unit->mY = 0.0; // Second state (imaginary part)
	unit->mFreq = IN0(1);
	unit->mNormCoeff = (1.0-unit->mRes*unit->mRes)/unit->mRes;
	unit->mcoeffX = unit->mRes * cos(TWOPI * unit->mFreq / SAMPLERATE);
	unit->mcoeffY = unit->mRes * sin(TWOPI * unit->mFreq / SAMPLERATE);
	// unit->mNormCoeff = uninitializedControl;
	// unit->mcoeffX = uninitializedControl;
	// unit->mcoeffY = uninitializedControl;

	// 3. calculate one sample of output.
	ComplexRes_next_kk(unit, 1);
}


//////////////////////////////////////////////////////////////////
// audio rate frequency and audio rate decay
void ComplexRes_next_aa(ComplexRes *unit, int inNumSamples)
{
	float *out = OUT(0);

	float *in = IN(0);
	float *freq = IN(1);
	float *decay = IN(2);
	double oldX = unit->mX;
	double oldY = unit->mY;
	double res,ang,coeffX,coeffY,X,Y,normCoeff;
	
	for (int i=0; i < inNumSamples; ++i)
	{
		res = exp(-1.0/(decay[i]*SAMPLERATE));
		normCoeff = (1.0-res*res)/res;
		ang = (freq[i]/SAMPLERATE)*TWOPI;
		coeffX = res*cos(ang);
		coeffY = res*sin(ang);
		
		X = coeffX*oldX - coeffY*oldY + in[i];
		Y = coeffY*oldX + coeffX*oldY;
		out[i] = Y * normCoeff;
		
		oldX = X;
		oldY = Y;
	}

	// store the states back to the struct
	unit->mX = zapgremlins(oldX);
	unit->mY = zapgremlins(oldY);
}

//////////////////////////////////////////////////////////////////
// audio rate frequency and control rate decay
void ComplexRes_next_ak(ComplexRes *unit, int inNumSamples)
{
	float *out = OUT(0);

	float *in = IN(0);
	float *freq = IN(1);
	float decay = IN0(2);
	double oldX = unit->mX;
	double oldY = unit->mY;
	double res,ang,coeffX,coeffY,X,Y,normCoeff;
	
	// update params
	if (decay != unit->mDecay){
		res = exp(-1.0/((decay+unit->mDecay)*0.5*SAMPLERATE));
		normCoeff = (1.0-res*res)/res;
		unit->mDecay = decay;
		unit->mRes = exp(-1.0/(decay*SAMPLERATE));
		unit->mNormCoeff = normCoeff;
	} else {
		res = unit->mRes;
		normCoeff = unit->mNormCoeff;
	};

	// render signal
	for (int i=0; i < inNumSamples; ++i)
	{
		ang = (freq[i]/SAMPLERATE)*TWOPI;
		coeffX = res*cos(ang);
		coeffY = res*sin(ang);
		
		X = coeffX*oldX - coeffY*oldY + in[i];
		Y = coeffY*oldX + coeffX*oldY;
		out[i] = Y * normCoeff;
		
		oldX = X;
		oldY = Y;
	}

	// store states back to struct
	unit->mX = zapgremlins(oldX);
	unit->mY = zapgremlins(oldY);
}

//////////////////////////////////////////////////////////////////
// control rate frequency and audio rate decay
void ComplexRes_next_ka(ComplexRes *unit, int inNumSamples)
{
	float *out = OUT(0);

	float *in = IN(0);
	float freq = IN0(1);
	float *decay = IN(2);
	double oldX = unit->mX;
	double oldY = unit->mY;
	double res,ang,coeffX,coeffY,X,Y,normCoeff;
	
	// update params
	if (freq != unit->mFreq){
		ang = (freq+unit->mFreq)*0.5 * TWOPI / SAMPLERATE;
		unit->mFreq = freq;
		unit->mAng = freq * TWOPI / SAMPLERATE;
	} else {
		ang = unit->mAng;
	};

	// render signal
	for (int i=0; i < inNumSamples; ++i)
	{
		res = exp(-1.0/(decay[i]*SAMPLERATE));
		normCoeff = (1.0-res*res)/res;
		coeffX = res*cos(ang);
		coeffY = res*sin(ang);
		X = coeffX*oldX - coeffY*oldY + in[i];
		Y = coeffY*oldX + coeffX*oldY;
		out[i] = Y*normCoeff;
		
		oldX = X;
		oldY = Y;
	}

	// store states back to struct
	unit->mX = zapgremlins(oldX);
	unit->mY = zapgremlins(oldY);
	
}



//////////////////////////////////////////////////////////////////
// control rate frequency and control rate decay
void ComplexRes_next_kk(ComplexRes *unit, int inNumSamples)
{
	float *out = OUT(0);

	float *in = IN(0);
	float freq = IN0(1);
	float decay = IN0(2);
	double oldX = unit->mX;
	double oldY = unit->mY;
	double res,ang,coeffX,coeffY,X,Y,normCoeff;
	
	// update params
	if (decay != unit->mDecay || freq != unit->mFreq){
		res = exp(-1.0/((decay+unit->mDecay)*0.5*SAMPLERATE));
		normCoeff = (1.0-res*res)/res;
		ang = (freq+unit->mFreq)*0.5 * TWOPI / SAMPLERATE;
		coeffX = res*cos(ang);
		coeffY = res*sin(ang);
		unit->mDecay = decay; // If the parameters have changed, store them back in the struct
		unit->mRes = exp(-1.0/((decay)*SAMPLERATE));
		unit->mFreq = freq;
		unit->mAng =  freq * TWOPI / SAMPLERATE;
		unit->mcoeffX = res*cos(unit->mAng);
		unit->mcoeffY = res*sin(unit->mAng);
		unit->mNormCoeff = normCoeff;
	} else {
		res = unit->mRes;
		coeffX = unit->mcoeffX;
		coeffY = unit->mcoeffY;
		normCoeff = unit->mNormCoeff;
		ang = unit->mAng;
	};

	// render signal
	for (int i=0; i < inNumSamples; ++i)
	{
		X = coeffX*oldX - coeffY*oldY + in[i];
		Y = coeffY*oldX + coeffX*oldY;
		out[i] = Y*normCoeff;
		oldX = X;
		oldY = Y;
	}

	// store states back to struct
	unit->mX = zapgremlins(oldX);
	unit->mY = zapgremlins(oldY);
	
}


// the entry point is called by host when plug-in is loaded
PluginLoad(ComplexRes)
{
	// InterfaceTable *inTable implicitly given as argument to load function
	ft = inTable; // store pointer to InterfaceTable

	DefineSimpleUnit(ComplexRes);
}