diff options
author | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
---|---|---|
committer | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
commit | 513389b4c1bc8afe4b2dc9947c534bfeb105e3da (patch) | |
tree | 10e673b35651ac567fed2eda0c679c7ade64cbc6 /apps/plugins/pdbox/PDa/intern/threshold~.c | |
parent | 95fa7f6a2ef466444fbe3fe87efc6d5db6b77b36 (diff) | |
download | rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.tar.gz rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.zip |
Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin project of Wincent Balin. Stripped some non-sourcefiles and added a rockbox readme that needs a bit more info from Wincent. Is added to CATEGORIES and viewers, but not yet to SUBDIRS (ie doesn't build yet)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21044 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/pdbox/PDa/intern/threshold~.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/intern/threshold~.c | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/intern/threshold~.c b/apps/plugins/pdbox/PDa/intern/threshold~.c new file mode 100644 index 0000000000..78b15f7342 --- /dev/null +++ b/apps/plugins/pdbox/PDa/intern/threshold~.c | |||
@@ -0,0 +1,270 @@ | |||
1 | #include <m_pd.h> | ||
2 | #include <m_fixed.h> | ||
3 | |||
4 | static t_class *threshold_tilde_class; | ||
5 | |||
6 | typedef struct _threshold_tilde | ||
7 | { | ||
8 | t_object x_obj; | ||
9 | t_outlet *x_outlet1; /* bang out for high thresh */ | ||
10 | t_outlet *x_outlet2; /* bang out for low thresh */ | ||
11 | t_clock *x_clock; /* wakeup for message output */ | ||
12 | float x_f; /* scalar inlet */ | ||
13 | int x_state; /* 1 = high, 0 = low */ | ||
14 | t_sample x_hithresh; /* value of high threshold */ | ||
15 | t_sample x_lothresh; /* value of low threshold */ | ||
16 | float x_deadwait; /* msec remaining in dead period */ | ||
17 | float x_msecpertick; /* msec per DSP tick */ | ||
18 | float x_hideadtime; /* hi dead time in msec */ | ||
19 | float x_lodeadtime; /* lo dead time in msec */ | ||
20 | } t_threshold_tilde; | ||
21 | |||
22 | static void threshold_tilde_tick(t_threshold_tilde *x); | ||
23 | static void threshold_tilde_set(t_threshold_tilde *x, | ||
24 | t_floatarg hithresh, t_floatarg hideadtime, | ||
25 | t_floatarg lothresh, t_floatarg lodeadtime); | ||
26 | |||
27 | static t_threshold_tilde *threshold_tilde_new(t_floatarg hithresh, | ||
28 | t_floatarg hideadtime, t_floatarg lothresh, t_floatarg lodeadtime) | ||
29 | { | ||
30 | t_threshold_tilde *x = (t_threshold_tilde *) | ||
31 | pd_new(threshold_tilde_class); | ||
32 | x->x_state = 0; /* low state */ | ||
33 | x->x_deadwait = 0; /* no dead time */ | ||
34 | x->x_clock = clock_new(x, (t_method)threshold_tilde_tick); | ||
35 | x->x_outlet1 = outlet_new(&x->x_obj, &s_bang); | ||
36 | x->x_outlet2 = outlet_new(&x->x_obj, &s_bang); | ||
37 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1")); | ||
38 | x->x_msecpertick = 0.; | ||
39 | x->x_f = 0; | ||
40 | threshold_tilde_set(x, hithresh, hideadtime, lothresh, lodeadtime); | ||
41 | return (x); | ||
42 | } | ||
43 | |||
44 | /* "set" message to specify thresholds and dead times */ | ||
45 | static void threshold_tilde_set(t_threshold_tilde *x, | ||
46 | t_floatarg hithresh, t_floatarg hideadtime, | ||
47 | t_floatarg lothresh, t_floatarg lodeadtime) | ||
48 | { | ||
49 | if (lothresh > hithresh) | ||
50 | lothresh = hithresh; | ||
51 | x->x_hithresh = ftofix(hithresh); | ||
52 | x->x_hideadtime = hideadtime; | ||
53 | x->x_lothresh = ftofix(lothresh); | ||
54 | x->x_lodeadtime = lodeadtime; | ||
55 | } | ||
56 | |||
57 | /* number in inlet sets state -- note incompatible with JMAX which used | ||
58 | "int" message for this, impossible here because of auto signal conversion */ | ||
59 | static void threshold_tilde_ft1(t_threshold_tilde *x, t_floatarg f) | ||
60 | { | ||
61 | x->x_state = (f != 0); | ||
62 | x->x_deadwait = 0; | ||
63 | } | ||
64 | |||
65 | static void threshold_tilde_tick(t_threshold_tilde *x) | ||
66 | { | ||
67 | if (x->x_state) | ||
68 | outlet_bang(x->x_outlet1); | ||
69 | else outlet_bang(x->x_outlet2); | ||
70 | } | ||
71 | |||
72 | static t_int *threshold_tilde_perform(t_int *w) | ||
73 | { | ||
74 | t_sample *in1 = (t_sample *)(w[1]); | ||
75 | t_threshold_tilde *x = (t_threshold_tilde *)(w[2]); | ||
76 | int n = (t_int)(w[3]); | ||
77 | if (x->x_deadwait > 0) | ||
78 | x->x_deadwait -= x->x_msecpertick; | ||
79 | else if (x->x_state) | ||
80 | { | ||
81 | /* we're high; look for low sample */ | ||
82 | for (; n--; in1++) | ||
83 | { | ||
84 | if (*in1 < x->x_lothresh) | ||
85 | { | ||
86 | clock_delay(x->x_clock, 0L); | ||
87 | x->x_state = 0; | ||
88 | x->x_deadwait = x->x_lodeadtime; | ||
89 | goto done; | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | /* we're low; look for high sample */ | ||
96 | for (; n--; in1++) | ||
97 | { | ||
98 | if (*in1 >= x->x_hithresh) | ||
99 | { | ||
100 | clock_delay(x->x_clock, 0L); | ||
101 | x->x_state = 1; | ||
102 | x->x_deadwait = x->x_hideadtime; | ||
103 | goto done; | ||
104 | } | ||
105 | } | ||
106 | } | ||
107 | done: | ||
108 | return (w+4); | ||
109 | } | ||
110 | |||
111 | void threshold_tilde_dsp(t_threshold_tilde *x, t_signal **sp) | ||
112 | { | ||
113 | x->x_msecpertick = 1000. * sp[0]->s_n / sp[0]->s_sr; | ||
114 | dsp_add(threshold_tilde_perform, 3, sp[0]->s_vec, x, sp[0]->s_n); | ||
115 | } | ||
116 | |||
117 | static void threshold_tilde_ff(t_threshold_tilde *x) | ||
118 | { | ||
119 | clock_free(x->x_clock); | ||
120 | } | ||
121 | |||
122 | void threshold_tilde_setup( void) | ||
123 | { | ||
124 | threshold_tilde_class = class_new(gensym("threshold~"), | ||
125 | (t_newmethod)threshold_tilde_new, (t_method)threshold_tilde_ff, | ||
126 | sizeof(t_threshold_tilde), 0, | ||
127 | A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); | ||
128 | CLASS_MAINSIGNALIN(threshold_tilde_class, t_threshold_tilde, x_f); | ||
129 | class_addmethod(threshold_tilde_class, (t_method)threshold_tilde_set, | ||
130 | gensym("set"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); | ||
131 | class_addmethod(threshold_tilde_class, (t_method)threshold_tilde_ft1, | ||
132 | gensym("ft1"), A_FLOAT, 0); | ||
133 | class_addmethod(threshold_tilde_class, (t_method)threshold_tilde_dsp, | ||
134 | gensym("dsp"), 0); | ||
135 | } | ||
136 | #include <m_pd.h> | ||
137 | #include <m_fixed.h> | ||
138 | |||
139 | static t_class *threshold_tilde_class; | ||
140 | |||
141 | typedef struct _threshold_tilde | ||
142 | { | ||
143 | t_object x_obj; | ||
144 | t_outlet *x_outlet1; /* bang out for high thresh */ | ||
145 | t_outlet *x_outlet2; /* bang out for low thresh */ | ||
146 | t_clock *x_clock; /* wakeup for message output */ | ||
147 | float x_f; /* scalar inlet */ | ||
148 | int x_state; /* 1 = high, 0 = low */ | ||
149 | t_sample x_hithresh; /* value of high threshold */ | ||
150 | t_sample x_lothresh; /* value of low threshold */ | ||
151 | float x_deadwait; /* msec remaining in dead period */ | ||
152 | float x_msecpertick; /* msec per DSP tick */ | ||
153 | float x_hideadtime; /* hi dead time in msec */ | ||
154 | float x_lodeadtime; /* lo dead time in msec */ | ||
155 | } t_threshold_tilde; | ||
156 | |||
157 | static void threshold_tilde_tick(t_threshold_tilde *x); | ||
158 | static void threshold_tilde_set(t_threshold_tilde *x, | ||
159 | t_floatarg hithresh, t_floatarg hideadtime, | ||
160 | t_floatarg lothresh, t_floatarg lodeadtime); | ||
161 | |||
162 | static t_threshold_tilde *threshold_tilde_new(t_floatarg hithresh, | ||
163 | t_floatarg hideadtime, t_floatarg lothresh, t_floatarg lodeadtime) | ||
164 | { | ||
165 | t_threshold_tilde *x = (t_threshold_tilde *) | ||
166 | pd_new(threshold_tilde_class); | ||
167 | x->x_state = 0; /* low state */ | ||
168 | x->x_deadwait = 0; /* no dead time */ | ||
169 | x->x_clock = clock_new(x, (t_method)threshold_tilde_tick); | ||
170 | x->x_outlet1 = outlet_new(&x->x_obj, &s_bang); | ||
171 | x->x_outlet2 = outlet_new(&x->x_obj, &s_bang); | ||
172 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1")); | ||
173 | x->x_msecpertick = 0.; | ||
174 | x->x_f = 0; | ||
175 | threshold_tilde_set(x, hithresh, hideadtime, lothresh, lodeadtime); | ||
176 | return (x); | ||
177 | } | ||
178 | |||
179 | /* "set" message to specify thresholds and dead times */ | ||
180 | static void threshold_tilde_set(t_threshold_tilde *x, | ||
181 | t_floatarg hithresh, t_floatarg hideadtime, | ||
182 | t_floatarg lothresh, t_floatarg lodeadtime) | ||
183 | { | ||
184 | if (lothresh > hithresh) | ||
185 | lothresh = hithresh; | ||
186 | x->x_hithresh = ftofix(hithresh); | ||
187 | x->x_hideadtime = hideadtime; | ||
188 | x->x_lothresh = ftofix(lothresh); | ||
189 | x->x_lodeadtime = lodeadtime; | ||
190 | } | ||
191 | |||
192 | /* number in inlet sets state -- note incompatible with JMAX which used | ||
193 | "int" message for this, impossible here because of auto signal conversion */ | ||
194 | static void threshold_tilde_ft1(t_threshold_tilde *x, t_floatarg f) | ||
195 | { | ||
196 | x->x_state = (f != 0); | ||
197 | x->x_deadwait = 0; | ||
198 | } | ||
199 | |||
200 | static void threshold_tilde_tick(t_threshold_tilde *x) | ||
201 | { | ||
202 | if (x->x_state) | ||
203 | outlet_bang(x->x_outlet1); | ||
204 | else outlet_bang(x->x_outlet2); | ||
205 | } | ||
206 | |||
207 | static t_int *threshold_tilde_perform(t_int *w) | ||
208 | { | ||
209 | t_sample *in1 = (t_sample *)(w[1]); | ||
210 | t_threshold_tilde *x = (t_threshold_tilde *)(w[2]); | ||
211 | int n = (t_int)(w[3]); | ||
212 | if (x->x_deadwait > 0) | ||
213 | x->x_deadwait -= x->x_msecpertick; | ||
214 | else if (x->x_state) | ||
215 | { | ||
216 | /* we're high; look for low sample */ | ||
217 | for (; n--; in1++) | ||
218 | { | ||
219 | if (*in1 < x->x_lothresh) | ||
220 | { | ||
221 | clock_delay(x->x_clock, 0L); | ||
222 | x->x_state = 0; | ||
223 | x->x_deadwait = x->x_lodeadtime; | ||
224 | goto done; | ||
225 | } | ||
226 | } | ||
227 | } | ||
228 | else | ||
229 | { | ||
230 | /* we're low; look for high sample */ | ||
231 | for (; n--; in1++) | ||
232 | { | ||
233 | if (*in1 >= x->x_hithresh) | ||
234 | { | ||
235 | clock_delay(x->x_clock, 0L); | ||
236 | x->x_state = 1; | ||
237 | x->x_deadwait = x->x_hideadtime; | ||
238 | goto done; | ||
239 | } | ||
240 | } | ||
241 | } | ||
242 | done: | ||
243 | return (w+4); | ||
244 | } | ||
245 | |||
246 | void threshold_tilde_dsp(t_threshold_tilde *x, t_signal **sp) | ||
247 | { | ||
248 | x->x_msecpertick = 1000. * sp[0]->s_n / sp[0]->s_sr; | ||
249 | dsp_add(threshold_tilde_perform, 3, sp[0]->s_vec, x, sp[0]->s_n); | ||
250 | } | ||
251 | |||
252 | static void threshold_tilde_ff(t_threshold_tilde *x) | ||
253 | { | ||
254 | clock_free(x->x_clock); | ||
255 | } | ||
256 | |||
257 | void threshold_tilde_setup( void) | ||
258 | { | ||
259 | threshold_tilde_class = class_new(gensym("threshold~"), | ||
260 | (t_newmethod)threshold_tilde_new, (t_method)threshold_tilde_ff, | ||
261 | sizeof(t_threshold_tilde), 0, | ||
262 | A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); | ||
263 | CLASS_MAINSIGNALIN(threshold_tilde_class, t_threshold_tilde, x_f); | ||
264 | class_addmethod(threshold_tilde_class, (t_method)threshold_tilde_set, | ||
265 | gensym("set"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); | ||
266 | class_addmethod(threshold_tilde_class, (t_method)threshold_tilde_ft1, | ||
267 | gensym("ft1"), A_FLOAT, 0); | ||
268 | class_addmethod(threshold_tilde_class, (t_method)threshold_tilde_dsp, | ||
269 | gensym("dsp"), 0); | ||
270 | } | ||