-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistortion~.c
226 lines (180 loc) · 4.76 KB
/
distortion~.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
#include "MSPd.h"
static t_class *distortion_class;
#define OBJECT_NAME "distortion~"
typedef struct _distortion
{
t_object x_obj;
float x_f;
float knee;
float cut;
float rescale ;
short mute ;
short case1;
} t_distortion;
void *distortion_new(t_floatarg knee, t_floatarg cut);
t_int *distortion1_perform(t_int *w);
t_int *distortion2_perform(t_int *w);
t_int *distortion3_perform(t_int *w);
void distortion_dsp(t_distortion *x, t_signal **sp);
void distortion_float(t_distortion *x, double f);
void distortion_mute(t_distortion *x, t_floatarg f);
// no freeing function needed
void distortion_tilde_setup(void){
distortion_class = class_new(gensym("distortion~"), (t_newmethod)distortion_new,
0,sizeof(t_distortion), 0,A_DEFFLOAT,A_DEFFLOAT,0);
CLASS_MAINSIGNALIN(distortion_class, t_distortion, x_f);
class_addmethod(distortion_class,(t_method)distortion_dsp,gensym("dsp"),0);
class_addmethod(distortion_class,(t_method)distortion_mute,gensym("mute"),A_FLOAT,0);
potpourri_announce(OBJECT_NAME);
}
void *distortion_new(t_floatarg knee, t_floatarg cut)
{
t_distortion *x = (t_distortion *)pd_new(distortion_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
outlet_new(&x->x_obj, gensym("signal"));
if( knee >= cut || knee <= 0 || cut <= 0 ) {
// post("setting defaults");
x->knee = .1;
x->cut = .3 ;
} else {
x->knee = knee;
x->cut = cut;
// post("User defined values: knee %f cut %f", knee, cut);
}
x->rescale = 1.0 / x->cut ;
x->mute = 0;
return x;
}
// use when neither signal is connected
t_int *distortion1_perform(t_int *w)
{
float rectified_sample, in_sample;
t_distortion *x = (t_distortion *) (w[1]);
float *in = (t_float *)(w[2]);
float *out = (t_float *)(w[5]);
int n = (int)(w[6]);
float knee = x->knee;
float cut = x->cut;
float rescale = x->rescale;
if( x->mute ){
while( n-- ){
*out++ = 0;
}
return (w+7);
}
while (n--) {
in_sample = *in++;
rectified_sample = fabs( in_sample );
if( rectified_sample < knee ){
*out++ = in_sample;
} else {
if( in_sample > 0.0 ){
*out++ = rescale * (knee + (rectified_sample - knee) * (cut - knee));
} else {
*out++ = rescale * (-(knee + (rectified_sample - knee) * (cut - knee)));
}
}
}
return (w+7);
}
// use when both signals are connected
t_int *distortion2_perform(t_int *w)
{
float rectified_sample, in_sample;
t_distortion *x = (t_distortion *) (w[1]);
float *in = (t_float *)(w[2]);
float *data1 = (t_float *)(w[3]);
float *data2 = (t_float *)(w[4]);
float *out = (t_float *)(w[5]);
int n = (int)(w[6]);
// double fabs();
float knee = x->knee;
float cut = x->cut;
float rescale = x->rescale;
if( x->mute ){
while( n-- ){
*out++ = 0;
}
return (w+7);
}
while (n--) {
in_sample = *in++;
knee = *data1++;
cut = *data2++;
if( cut > 0.000001 )
rescale = 1.0 / cut;
else
rescale = 1.0;
rectified_sample = fabs( in_sample );
if( rectified_sample < knee ){
*out++ = in_sample;
} else {
if( in_sample > 0.0 ){
*out++ = rescale * (knee + (rectified_sample - knee) * (cut - knee));
} else {
*out++ = rescale * (-(knee + (rectified_sample - knee) * (cut - knee)));
}
}
}
x->knee = knee;
x->cut = cut;
x->rescale = rescale;
return (w+7);
}
t_int *distortion3_perform(t_int *w)
{
float rectified_sample, in_sample;
t_distortion *x = (t_distortion *) (w[1]);
float *in = (t_float *)(w[2]);
float *data1 = (t_float *)(w[3]);
float *data2 = (t_float *)(w[4]);
float *out = (t_float *)(w[5]);
int n = (int)(w[6]);
// double fabs();
float knee = x->knee;
float cut = x->cut;
float rescale = x->rescale;
short case1 = x->case1;
if( x->mute ){
while( n-- ){
*out++ = 0;
}
return (w+7);
}
while (n--) {
// first case, knee is connected, otherwise cut is connected
in_sample = *in++;
if( case1 ){
knee = *data1++;
}
else {
cut = *data2++;
}
if( cut > 0.000001 )
rescale = 1.0 / cut;
else
rescale = 1.0;
rectified_sample = fabs( in_sample );
if( rectified_sample < knee ){
*out++ = in_sample;
} else {
if( in_sample > 0.0 ){
*out++ = rescale * (knee + (rectified_sample - knee) * (cut - knee));
} else {
*out++ = rescale * (-(knee + (rectified_sample - knee) * (cut - knee)));
}
}
}
x->knee = knee;
x->cut = cut;
x->rescale = rescale;
return (w+7);
}
void distortion_mute(t_distortion *x, t_floatarg f) {
x->mute = f;
}
void distortion_dsp(t_distortion *x, t_signal **sp)
{
dsp_add(distortion2_perform, 6, x, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec,sp[0]->s_n);
}