-
Notifications
You must be signed in to change notification settings - Fork 4
/
+chebyshev~.c
242 lines (203 loc) · 5.26 KB
/
+chebyshev~.c
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
/*
May 20, 2010: using a single makefile for all sources that should work on both mac and windows. removing the sethelpsymbol() and going with the standard whatever~-help.pd convention. also adding static to all function names (except setup).
Nov 27, 2009: 12:28PM - removing unused variables and envelope follower stuff entirely.
*/
#include <math.h>
#include "m_pd.h"
#define ENVARRAYSIZE 16384
static t_class *chebyshev_tilde_class;
typedef struct _chebyshev_tilde
{
t_object x_obj;
float x_sr;
float x_n;
float x_dbOne;
float x_dbTwo;
float x_dbThree;
float x_dbFour;
float x_dbFive;
float x_dbSeven;
float x_gain;
float x_f;
} t_chebyshev_tilde;
static void chebyshev_tilde_p1(t_chebyshev_tilde *x, t_floatarg p1)
{
if(p1 > 0.0 || p1 < -60.0)
error("value must be >= -60.0dB and <= 0.0dB.");
else
x->x_dbOne = pow(10.0f, p1 * 0.05);
}
static void chebyshev_tilde_p2(t_chebyshev_tilde *x, t_floatarg p2)
{
if(p2 > 00.0 || p2 < -60.0)
error("value must be >= -60.0dB and <= 0.0dB.");
else
x->x_dbTwo = pow(10.0f, p2 * 0.05);
}
static void chebyshev_tilde_p3(t_chebyshev_tilde *x, t_floatarg p3)
{
if(p3 > 00.0 || p3 < -60.0)
error("value must be >= -60.0dB and <= 0.0dB.");
else
x->x_dbThree = pow(10.0f, p3 * 0.05);
}
static void chebyshev_tilde_p4(t_chebyshev_tilde *x, t_floatarg p4)
{
if(p4 > 00.0 || p4 < -60.0)
error("value must be >= -60.0dB and <= 0.0dB.");
else
x->x_dbFour = pow(10.0f, p4 * 0.05);
}
static void chebyshev_tilde_p5(t_chebyshev_tilde *x, t_floatarg p5)
{
if(p5 > 00.0 || p5 < -60.0)
error("value must be >= -60.0dB and <= 0.0dB.");
else
x->x_dbFive = pow(10.0f, p5 * 0.05);
}
static void chebyshev_tilde_p7(t_chebyshev_tilde *x, t_floatarg p7)
{
if(p7 > 00.0 || p7 < -60.0)
error("value must be >= -60.0dB and <= 0.0dB.");
else
x->x_dbSeven = pow(10.0f, p7 * 0.05);
}
static void *chebyshev_tilde_new()
{
t_chebyshev_tilde *x = (t_chebyshev_tilde *)pd_new(chebyshev_tilde_class);
outlet_new(&x->x_obj, &s_signal);
x->x_dbOne = 1.0;
x->x_dbTwo = 0.0;
x->x_dbThree = 0.0;
x->x_dbFour = 0.0;
x->x_dbFive = 0.0;
x->x_dbSeven = 0.0;
x->x_gain = 0.0;
return(void *)x;
};
static t_int *chebyshev_tilde_perform(t_int *w)
{
t_chebyshev_tilde *x = (t_chebyshev_tilde *)(w[1]);
t_sample *in = (t_sample *)(w[2]);
t_sample *out = (t_sample *)(w[3]);
int n = (int)(w[4]);
float gainOne, gainTwo, gainThree, gainFour, gainFive, gainSeven, gainSum;
float poly1, poly2, poly3, poly4, poly5, poly7, square;
float dbOne, dbTwo, dbThree, dbFour, dbFive, dbSeven;
float gain;
//local copies of dataspace variables:
dbOne = x->x_dbOne;
dbTwo = x->x_dbTwo;
dbThree = x->x_dbThree;
dbFour = x->x_dbFour;
dbFive = x->x_dbFive;
dbSeven = x->x_dbSeven;
gain = x->x_gain;
gainOne = pow(10.0f, ((dbOne * 3.f) - 3.f));
gainTwo = pow(10.0f, ((dbTwo * 3.f) - 3.f));
gainThree = pow(10.0f, ((dbThree * 3.f) - 3.f));
gainFour = pow(10.0f, ((dbFour * 3.f) - 3.f));
gainFive = pow(10.0f, ((dbFive * 3.f) - 3.f));
gainSeven = pow(10.0f, ((dbSeven * 3.f) - 3.f));
gainSum = gainOne + gainTwo + gainThree + gainFour + gainFive + gainSeven;
if(gainSum == 0.0f)
x->x_gain = gain = 0.0f;
else
x->x_gain = gain = 1.0f/gainSum;
while(n--)
{
float inputSample = *in++;
float outputSample;
// here's where you do your DSP work
square = inputSample * inputSample;
poly1 = inputSample;
poly2 = (2.f * square) - 1.f;
poly3 = ((4.f * square) - 3.f) * inputSample;
poly4 = (((8.f * square) - 8.f) * square) + 1.f;
poly5 = ((((16.f * square) - 20.f) * square) + 5.f) * inputSample;
// poly6 = (((((32.f * square) - 48.f) * square) + 18.f) * square) - 1.f;
poly7 = ((((((64.f * square) - 112.f) * square) + 56.f) * square) - 7.f) * inputSample;
outputSample = (gainOne * poly1) + (gainTwo * poly2) + (gainThree * poly3) + (gainFour * poly4)
+ (gainFive * poly5) + (gainSeven * poly7);
outputSample *= gain;
*out++ = outputSample;
};
return(w + 5);
};
static void chebyshev_tilde_dsp(t_chebyshev_tilde *x, t_signal **sp)
{
dsp_add(
chebyshev_tilde_perform,
4,
x,
sp[0]->s_vec,
sp[1]->s_vec,
sp[0]->s_n
);
if(sp[0]->s_n != x->x_n || sp[0]->s_sr != x->x_sr)
{
x->x_n = sp[0]->s_n;
x->x_sr = sp[0]->s_sr;
};
};
void setup_0x2bchebyshev_tilde(void)
{
chebyshev_tilde_class =
class_new(
gensym("+chebyshev~"),
(t_newmethod)chebyshev_tilde_new,
0,
sizeof(t_chebyshev_tilde),
CLASS_DEFAULT,
0
);
CLASS_MAINSIGNALIN(chebyshev_tilde_class, t_chebyshev_tilde, x_f);
class_addmethod(
chebyshev_tilde_class,
(t_method)chebyshev_tilde_dsp,
gensym("dsp"),
0
);
class_addmethod(
chebyshev_tilde_class,
(t_method)chebyshev_tilde_p1,
gensym("p1"),
A_DEFFLOAT,
0
);
class_addmethod(
chebyshev_tilde_class,
(t_method)chebyshev_tilde_p2,
gensym("p2"),
A_DEFFLOAT,
0
);
class_addmethod(
chebyshev_tilde_class,
(t_method)chebyshev_tilde_p3,
gensym("p3"),
A_DEFFLOAT,
0
);
class_addmethod(
chebyshev_tilde_class,
(t_method)chebyshev_tilde_p4,
gensym("p4"),
A_DEFFLOAT,
0
);
class_addmethod(
chebyshev_tilde_class,
(t_method)chebyshev_tilde_p5,
gensym("p5"),
A_DEFFLOAT,
0
);
class_addmethod(
chebyshev_tilde_class,
(t_method)chebyshev_tilde_p7,
gensym("p7"),
A_DEFFLOAT,
0
);
}