-
Notifications
You must be signed in to change notification settings - Fork 1
/
nead~.c
225 lines (212 loc) · 7.33 KB
/
nead~.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
/*
* nead~.c - exponential attack decay envelope
* Copyright (c) 2000-2014 by Tom Schouten & Seb Shader
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/
#include "shadylib.h"
#include <math.h>
typedef struct neadctl {
t_shadylib_stage c_attack;
t_shadylib_stage c_decay;
t_sample c_linr; //holds the state to release from
t_sample c_state;
int c_target;
} t_neadctl;
typedef struct nead {
t_object x_obj;
t_float x_sr;
t_neadctl x_ctl;
} t_nead;
static void nead_float(t_nead *x, t_floatarg f) {
if(f == 0.f) {
if(x->x_ctl.c_target) {
x->x_ctl.c_target = 0;
x->x_ctl.c_linr = x->x_ctl.c_state*x->x_ctl.c_decay.base;
}
} else {
x->x_ctl.c_target = 1;
if(f < 0.f) x->x_ctl.c_state = 0.f;
}
}
static void nead_attack(t_nead* x, t_symbol* SHADYLIB_UNUSED(s), int argc,
t_atom *argv) {
t_int samps;
int abool;
if(argc > 0) {
samps = shadylib_ms2samps(atom_getfloat(argv), x->x_sr);
abool = samps == x->x_ctl.c_attack.nsamp;
if(argc > 1) {
x->x_ctl.c_attack.nsamp = samps;
shadylib_f2axfade(atom_getfloat(argv + 1), &(x->x_ctl.c_attack),
abool);
} else if(!abool) {
x->x_ctl.c_attack.nsamp = samps;
shadylib_ms2axfade(&(x->x_ctl.c_attack));
}
}
}
static void nead_decay(t_nead* x, t_symbol* SHADYLIB_UNUSED(s), int argc, t_atom *argv) {
t_int samps;
int abool;
if(argc > 0) {
samps = shadylib_ms2samps(atom_getfloat(argv), x->x_sr);
abool = samps == x->x_ctl.c_decay.nsamp;
if(argc > 1) {
x->x_ctl.c_decay.nsamp = samps;
shadylib_f2rxfade(atom_getfloat(argv + 1), &(x->x_ctl.c_decay),
abool);
} else if(!abool) {
x->x_ctl.c_decay.nsamp = samps;
shadylib_ms2rxfade(&(x->x_ctl.c_decay));
}
}
}
static void nead_any(t_nead *x, t_symbol *s, int argc, t_atom *argv) {
if(s == gensym("curves")) {
switch(argc) {
default:;
case 2: if(argv[1].a_type == A_FLOAT) {
shadylib_f2rxfade(atom_getfloat(argv + 1),
&(x->x_ctl.c_decay), 1);
}
case 1: if(argv[0].a_type == A_FLOAT) {
shadylib_f2axfade(atom_getfloat(argv), &(x->x_ctl.c_attack), 1);
}
case 0:;
}
} else {
t_int samps;
switch(argc) {
default:;
case 2:
if(argv[1].a_type == A_FLOAT) {
samps = shadylib_ms2samps(atom_getfloat(argv + 1), x->x_sr);
if(samps != x->x_ctl.c_decay.nsamp){
x->x_ctl.c_decay.nsamp = samps;
shadylib_ms2rxfade(&(x->x_ctl.c_decay));
}
}
case 1:
if(argv[0].a_type == A_FLOAT) {
samps = shadylib_ms2samps(atom_getfloat(argv), x->x_sr);
if(samps != x->x_ctl.c_attack.nsamp) {
x->x_ctl.c_attack.nsamp = samps;
shadylib_ms2axfade(&(x->x_ctl.c_attack));
}
}
case 0:;
}
}
}
t_int *nead_perform(t_int *w)
{
t_sample *out = (t_float *)(w[3]);
t_neadctl *ctl = (t_neadctl *)(w[1]);
t_sample state = ctl->c_state;
int n = (int)(w[2]);
int target = ctl->c_target;
t_shadylib_stage stage;
if (target){
/* attack */
stage = ctl->c_attack;
while(n){
n--;/*put inside of while so n != -1*/
*out++ = state;
#ifdef FP_FAST_FMAF
state = fmaf(state, stage.op, stage.base);
#else
state = state*stage.op + stage.base;
#endif
if(state >= 1.f) {
state = 1.f;
target = 0;
ctl->c_linr = ctl->c_decay.base;
break;
}
}
}
if (!target) {
/*decay*/
if(state == 0.f) while(n--) *out++ = 0.f;
else {
stage = ctl->c_decay;
stage.base = ctl->c_linr;
while(n--){
*out++ = state;
#ifdef FP_FAST_FMAF
state = fmaf(state, stage.op, stage.base);
#else
state = state*stage.op + stage.base;
#endif
if(state <= 0.f) {
state = 0.f;
for(;n;n--) *out++ = state;
}
}
}
}
/* save state */
ctl->c_state = SHADYLIB_IS_NOT_DENORMAL(state)*state;
ctl->c_target = target;
return (w+4);
}
void nead_dsp(t_nead *x, t_signal **sp)
{
if(sp[0]->s_sr != x->x_sr) {/*need to recalculate everything*/
t_shadylib_stage thistage;
double factor = sp[0]->s_sr/(double)x->x_sr;
x->x_sr = sp[0]->s_sr;
thistage = x->x_ctl.c_attack;
thistage.nsamp *= factor;
thistage.op = pow(thistage.op, 1.0/factor);
thistage.base = (1 - thistage.op)/(1 - thistage.lin);
x->x_ctl.c_attack = thistage;
thistage = x->x_ctl.c_decay;
thistage.nsamp *= factor;
thistage.op = pow(thistage.op, 1.0/factor);
thistage.base = thistage.lin*(1 - thistage.op)/(1 - thistage.lin);
x->x_ctl.c_decay = thistage;
}
dsp_add(nead_perform, 3, &x->x_ctl, sp[0]->s_n, sp[0]->s_vec);
}
t_class *nead_class;
void *nead_new(t_floatarg attack, t_floatarg decay) {
t_nead *x = (t_nead *)pd_new(nead_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("attack"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("decay"));
outlet_new(&x->x_obj, &s_signal);
x->x_ctl.c_state = 0.f;
x->x_ctl.c_target = 0;
x->x_sr = sys_getsr();
x->x_ctl.c_attack.nsamp = shadylib_ms2samps(attack, x->x_sr);
shadylib_f2axfade(1.0-(log(1.0/3.0)/log(SHADYLIB_ENVELOPE_RANGE)),
&(x->x_ctl.c_attack), 0); /* 1/3 by default */
x->x_ctl.c_decay.nsamp = shadylib_ms2samps(decay, x->x_sr);
shadylib_f2rxfade(0.f, &(x->x_ctl.c_decay), 0);
return (void *)x;
}
void nead_tilde_setup(void)
{
nead_class = class_new(gensym("nead~"), (t_newmethod)nead_new,
0, sizeof(t_nead), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
class_addmethod(nead_class, (t_method)nead_float,
&s_float, A_FLOAT, 0);
class_addmethod(nead_class, (t_method)nead_dsp, gensym("dsp"), A_CANT, 0);
class_addmethod(nead_class, (t_method)nead_attack,
gensym("attack"), A_GIMME, 0);
class_addmethod(nead_class, (t_method)nead_decay,
gensym("decay"), A_GIMME, 0);
class_addanything(nead_class, (t_method)nead_any);
}