diff options
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/d_fft.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/src/d_fft.c | 688 |
1 files changed, 688 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/d_fft.c b/apps/plugins/pdbox/PDa/src/d_fft.c new file mode 100644 index 0000000000..9916fdcb88 --- /dev/null +++ b/apps/plugins/pdbox/PDa/src/d_fft.c | |||
@@ -0,0 +1,688 @@ | |||
1 | /* Copyright (c) 1997-1999 Miller Puckette and others. | ||
2 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
3 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ | ||
4 | |||
5 | #include "m_pd.h" | ||
6 | |||
7 | /* ------------------------ fft~ and ifft~ -------------------------------- */ | ||
8 | static t_class *sigfft_class, *sigifft_class; | ||
9 | |||
10 | typedef struct fft | ||
11 | { | ||
12 | t_object x_obj; | ||
13 | float x_f; | ||
14 | } t_sigfft; | ||
15 | |||
16 | static void *sigfft_new(void) | ||
17 | { | ||
18 | t_sigfft *x = (t_sigfft *)pd_new(sigfft_class); | ||
19 | outlet_new(&x->x_obj, gensym("signal")); | ||
20 | outlet_new(&x->x_obj, gensym("signal")); | ||
21 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
22 | x->x_f = 0; | ||
23 | return (x); | ||
24 | } | ||
25 | |||
26 | static void *sigifft_new(void) | ||
27 | { | ||
28 | t_sigfft *x = (t_sigfft *)pd_new(sigifft_class); | ||
29 | outlet_new(&x->x_obj, gensym("signal")); | ||
30 | outlet_new(&x->x_obj, gensym("signal")); | ||
31 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
32 | x->x_f = 0; | ||
33 | return (x); | ||
34 | } | ||
35 | |||
36 | static t_int *sigfft_swap(t_int *w) | ||
37 | { | ||
38 | t_sample *in1 = (t_sample *)(w[1]); | ||
39 | t_sample *in2 = (t_sample *)(w[2]); | ||
40 | int n = w[3]; | ||
41 | for (;n--; in1++, in2++) | ||
42 | { | ||
43 | float f = *in1; | ||
44 | *in1 = *in2; | ||
45 | *in2 = f; | ||
46 | } | ||
47 | return (w+4); | ||
48 | } | ||
49 | |||
50 | static t_int *sigfft_perform(t_int *w) | ||
51 | { | ||
52 | t_sample *in1 = (t_sample *)(w[1]); | ||
53 | t_sample *in2 = (t_sample *)(w[2]); | ||
54 | int n = w[3]; | ||
55 | mayer_fft(n, in1, in2); | ||
56 | return (w+4); | ||
57 | } | ||
58 | |||
59 | static t_int *sigifft_perform(t_int *w) | ||
60 | { | ||
61 | t_sample *in1 = (t_sample *)(w[1]); | ||
62 | t_sample *in2 = (t_sample *)(w[2]); | ||
63 | int n = w[3]; | ||
64 | mayer_ifft(n, in1, in2); | ||
65 | return (w+4); | ||
66 | } | ||
67 | |||
68 | static void sigfft_dspx(t_sigfft *x, t_signal **sp, t_int *(*f)(t_int *w)) | ||
69 | { | ||
70 | int n = sp[0]->s_n; | ||
71 | t_sample *in1 = sp[0]->s_vec; | ||
72 | t_sample *in2 = sp[1]->s_vec; | ||
73 | t_sample *out1 = sp[2]->s_vec; | ||
74 | t_sample *out2 = sp[3]->s_vec; | ||
75 | if (out1 == in2 && out2 == in1) | ||
76 | dsp_add(sigfft_swap, 3, out1, out2, n); | ||
77 | else if (out1 == in2) | ||
78 | { | ||
79 | dsp_add(copy_perform, 3, in2, out2, n); | ||
80 | dsp_add(copy_perform, 3, in1, out1, n); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | if (out1 != in1) dsp_add(copy_perform, 3, in1, out1, n); | ||
85 | if (out2 != in2) dsp_add(copy_perform, 3, in2, out2, n); | ||
86 | } | ||
87 | dsp_add(f, 3, sp[2]->s_vec, sp[3]->s_vec, n); | ||
88 | } | ||
89 | |||
90 | static void sigfft_dsp(t_sigfft *x, t_signal **sp) | ||
91 | { | ||
92 | sigfft_dspx(x, sp, sigfft_perform); | ||
93 | } | ||
94 | |||
95 | static void sigifft_dsp(t_sigfft *x, t_signal **sp) | ||
96 | { | ||
97 | sigfft_dspx(x, sp, sigifft_perform); | ||
98 | } | ||
99 | |||
100 | static void sigfft_setup(void) | ||
101 | { | ||
102 | sigfft_class = class_new(gensym("fft~"), sigfft_new, 0, | ||
103 | sizeof(t_sigfft), 0, 0); | ||
104 | CLASS_MAINSIGNALIN(sigfft_class, t_sigfft, x_f); | ||
105 | class_addmethod(sigfft_class, (t_method)sigfft_dsp, gensym("dsp"), 0); | ||
106 | |||
107 | sigifft_class = class_new(gensym("ifft~"), sigifft_new, 0, | ||
108 | sizeof(t_sigfft), 0, 0); | ||
109 | CLASS_MAINSIGNALIN(sigifft_class, t_sigfft, x_f); | ||
110 | class_addmethod(sigifft_class, (t_method)sigifft_dsp, gensym("dsp"), 0); | ||
111 | class_sethelpsymbol(sigifft_class, gensym("fft~")); | ||
112 | } | ||
113 | |||
114 | /* ----------------------- rfft~ -------------------------------- */ | ||
115 | |||
116 | static t_class *sigrfft_class; | ||
117 | |||
118 | typedef struct rfft | ||
119 | { | ||
120 | t_object x_obj; | ||
121 | float x_f; | ||
122 | } t_sigrfft; | ||
123 | |||
124 | static void *sigrfft_new(void) | ||
125 | { | ||
126 | t_sigrfft *x = (t_sigrfft *)pd_new(sigrfft_class); | ||
127 | outlet_new(&x->x_obj, gensym("signal")); | ||
128 | outlet_new(&x->x_obj, gensym("signal")); | ||
129 | x->x_f = 0; | ||
130 | return (x); | ||
131 | } | ||
132 | |||
133 | static t_int *sigrfft_flip(t_int *w) | ||
134 | { | ||
135 | t_sample *in = (t_sample *)(w[1]); | ||
136 | t_sample *out = (t_sample *)(w[2]); | ||
137 | int n = w[3]; | ||
138 | while (n--) *(--out) = *in++; | ||
139 | *(--out) = 0; /* to hell with it */ | ||
140 | return (w+4); | ||
141 | } | ||
142 | |||
143 | static t_int *sigrfft_perform(t_int *w) | ||
144 | { | ||
145 | t_sample *in = (t_sample *)(w[1]); | ||
146 | int n = w[2]; | ||
147 | mayer_realfft(n, in); | ||
148 | return (w+3); | ||
149 | } | ||
150 | |||
151 | static void sigrfft_dsp(t_sigrfft *x, t_signal **sp) | ||
152 | { | ||
153 | int n = sp[0]->s_n, n2 = (n>>1); | ||
154 | t_sample *in1 = sp[0]->s_vec; | ||
155 | t_sample *out1 = sp[1]->s_vec; | ||
156 | t_sample *out2 = sp[2]->s_vec; | ||
157 | if (n < 4) | ||
158 | { | ||
159 | error("fft: minimum 4 points"); | ||
160 | return; | ||
161 | } | ||
162 | if (in1 == out2) /* this probably never happens */ | ||
163 | { | ||
164 | dsp_add(sigrfft_perform, 2, out2, n); | ||
165 | dsp_add(copy_perform, 3, out2, out1, n2); | ||
166 | dsp_add(sigrfft_flip, 3, out2 + (n2+1), out2 + n2, n2-1); | ||
167 | } | ||
168 | else | ||
169 | { | ||
170 | if (in1 != out1) dsp_add(copy_perform, 3, in1, out1, n); | ||
171 | dsp_add(sigrfft_perform, 2, out1, n); | ||
172 | dsp_add(sigrfft_flip, 3, out1 + (n2+1), out2 + n2, n2-1); | ||
173 | } | ||
174 | dsp_add_zero(out1 + n2, n2); | ||
175 | dsp_add_zero(out2 + n2, n2); | ||
176 | } | ||
177 | |||
178 | static void sigrfft_setup(void) | ||
179 | { | ||
180 | sigrfft_class = class_new(gensym("rfft~"), sigrfft_new, 0, | ||
181 | sizeof(t_sigrfft), 0, 0); | ||
182 | CLASS_MAINSIGNALIN(sigrfft_class, t_sigrfft, x_f); | ||
183 | class_addmethod(sigrfft_class, (t_method)sigrfft_dsp, gensym("dsp"), 0); | ||
184 | class_sethelpsymbol(sigrfft_class, gensym("fft~")); | ||
185 | } | ||
186 | |||
187 | /* ----------------------- rifft~ -------------------------------- */ | ||
188 | |||
189 | static t_class *sigrifft_class; | ||
190 | |||
191 | typedef struct rifft | ||
192 | { | ||
193 | t_object x_obj; | ||
194 | float x_f; | ||
195 | } t_sigrifft; | ||
196 | |||
197 | static void *sigrifft_new(void) | ||
198 | { | ||
199 | t_sigrifft *x = (t_sigrifft *)pd_new(sigrifft_class); | ||
200 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
201 | outlet_new(&x->x_obj, gensym("signal")); | ||
202 | x->x_f = 0; | ||
203 | return (x); | ||
204 | } | ||
205 | |||
206 | static t_int *sigrifft_perform(t_int *w) | ||
207 | { | ||
208 | t_sample *in = (t_sample *)(w[1]); | ||
209 | int n = w[2]; | ||
210 | mayer_realifft(n, in); | ||
211 | return (w+3); | ||
212 | } | ||
213 | |||
214 | static void sigrifft_dsp(t_sigrifft *x, t_signal **sp) | ||
215 | { | ||
216 | int n = sp[0]->s_n, n2 = (n>>1); | ||
217 | t_sample *in1 = sp[0]->s_vec; | ||
218 | t_sample *in2 = sp[1]->s_vec; | ||
219 | t_sample *out1 = sp[2]->s_vec; | ||
220 | if (n < 4) | ||
221 | { | ||
222 | error("fft: minimum 4 points"); | ||
223 | return; | ||
224 | } | ||
225 | if (in2 == out1) | ||
226 | { | ||
227 | dsp_add(sigrfft_flip, 3, out1+1, out1 + n, (n2-1)); | ||
228 | dsp_add(copy_perform, 3, in1, out1, n2); | ||
229 | } | ||
230 | else | ||
231 | { | ||
232 | if (in1 != out1) dsp_add(copy_perform, 3, in1, out1, n2); | ||
233 | dsp_add(sigrfft_flip, 3, in2+1, out1 + n, n2-1); | ||
234 | } | ||
235 | dsp_add(sigrifft_perform, 2, out1, n); | ||
236 | } | ||
237 | |||
238 | static void sigrifft_setup(void) | ||
239 | { | ||
240 | sigrifft_class = class_new(gensym("rifft~"), sigrifft_new, 0, | ||
241 | sizeof(t_sigrifft), 0, 0); | ||
242 | CLASS_MAINSIGNALIN(sigrifft_class, t_sigrifft, x_f); | ||
243 | class_addmethod(sigrifft_class, (t_method)sigrifft_dsp, gensym("dsp"), 0); | ||
244 | class_sethelpsymbol(sigrifft_class, gensym("fft~")); | ||
245 | } | ||
246 | |||
247 | /* ----------------------- framp~ -------------------------------- */ | ||
248 | |||
249 | #if 0 | ||
250 | static t_class *sigframp_class; | ||
251 | |||
252 | typedef struct framp | ||
253 | { | ||
254 | t_object x_obj; | ||
255 | float x_f; | ||
256 | } t_sigframp; | ||
257 | |||
258 | static void *sigframp_new(void) | ||
259 | { | ||
260 | t_sigframp *x = (t_sigframp *)pd_new(sigframp_class); | ||
261 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
262 | outlet_new(&x->x_obj, gensym("signal")); | ||
263 | outlet_new(&x->x_obj, gensym("signal")); | ||
264 | x->x_f = 0; | ||
265 | return (x); | ||
266 | } | ||
267 | |||
268 | static t_int *sigframp_perform(t_int *w) | ||
269 | { | ||
270 | float *inreal = (t_float *)(w[1]); | ||
271 | float *inimag = (t_float *)(w[2]); | ||
272 | float *outfreq = (t_float *)(w[3]); | ||
273 | float *outamp = (t_float *)(w[4]); | ||
274 | float lastreal = 0, currentreal = inreal[0], nextreal = inreal[1]; | ||
275 | float lastimag = 0, currentimag = inimag[0], nextimag = inimag[1]; | ||
276 | int n = w[5]; | ||
277 | int m = n + 1; | ||
278 | float fbin = 1, oneovern2 = 1.f/((float)n * (float)n); | ||
279 | |||
280 | inreal += 2; | ||
281 | inimag += 2; | ||
282 | *outamp++ = *outfreq++ = 0; | ||
283 | n -= 2; | ||
284 | while (n--) | ||
285 | { | ||
286 | float re, im, pow, freq; | ||
287 | lastreal = currentreal; | ||
288 | currentreal = nextreal; | ||
289 | nextreal = *inreal++; | ||
290 | lastimag = currentimag; | ||
291 | currentimag = nextimag; | ||
292 | nextimag = *inimag++; | ||
293 | re = currentreal - 0.5f * (lastreal + nextreal); | ||
294 | im = currentimag - 0.5f * (lastimag + nextimag); | ||
295 | pow = re * re + im * im; | ||
296 | if (pow > 1e-19) | ||
297 | { | ||
298 | float detune = ((lastreal - nextreal) * re + | ||
299 | (lastimag - nextimag) * im) / (2.0f * pow); | ||
300 | if (detune > 2 || detune < -2) freq = pow = 0; | ||
301 | else freq = fbin + detune; | ||
302 | } | ||
303 | else freq = pow = 0; | ||
304 | *outfreq++ = freq; | ||
305 | *outamp++ = oneovern2 * pow; | ||
306 | fbin += 1.0f; | ||
307 | } | ||
308 | while (m--) *outamp++ = *outfreq++ = 0; | ||
309 | return (w+6); | ||
310 | } | ||
311 | |||
312 | t_int *sigsqrt_perform(t_int *w); | ||
313 | |||
314 | static void sigframp_dsp(t_sigframp *x, t_signal **sp) | ||
315 | { | ||
316 | int n = sp[0]->s_n, n2 = (n>>1); | ||
317 | if (n < 4) | ||
318 | { | ||
319 | error("framp: minimum 4 points"); | ||
320 | return; | ||
321 | } | ||
322 | dsp_add(sigframp_perform, 5, sp[0]->s_vec, sp[1]->s_vec, | ||
323 | sp[2]->s_vec, sp[3]->s_vec, n2); | ||
324 | dsp_add(sigsqrt_perform, 3, sp[3]->s_vec, sp[3]->s_vec, n2); | ||
325 | } | ||
326 | |||
327 | static void sigframp_setup(void) | ||
328 | { | ||
329 | sigframp_class = class_new(gensym("framp~"), sigframp_new, 0, | ||
330 | sizeof(t_sigframp), 0, 0); | ||
331 | CLASS_MAINSIGNALIN(sigframp_class, t_sigframp, x_f); | ||
332 | class_addmethod(sigframp_class, (t_method)sigframp_dsp, gensym("dsp"), 0); | ||
333 | } | ||
334 | #endif | ||
335 | |||
336 | /* ------------------------ global setup routine ------------------------- */ | ||
337 | |||
338 | void d_fft_setup(void) | ||
339 | { | ||
340 | sigfft_setup(); | ||
341 | sigrfft_setup(); | ||
342 | sigrifft_setup(); | ||
343 | // sigframp_setup(); | ||
344 | } | ||
345 | /* Copyright (c) 1997-1999 Miller Puckette and others. | ||
346 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
347 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ | ||
348 | |||
349 | #include "m_pd.h" | ||
350 | |||
351 | /* ------------------------ fft~ and ifft~ -------------------------------- */ | ||
352 | static t_class *sigfft_class, *sigifft_class; | ||
353 | |||
354 | typedef struct fft | ||
355 | { | ||
356 | t_object x_obj; | ||
357 | float x_f; | ||
358 | } t_sigfft; | ||
359 | |||
360 | static void *sigfft_new(void) | ||
361 | { | ||
362 | t_sigfft *x = (t_sigfft *)pd_new(sigfft_class); | ||
363 | outlet_new(&x->x_obj, gensym("signal")); | ||
364 | outlet_new(&x->x_obj, gensym("signal")); | ||
365 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
366 | x->x_f = 0; | ||
367 | return (x); | ||
368 | } | ||
369 | |||
370 | static void *sigifft_new(void) | ||
371 | { | ||
372 | t_sigfft *x = (t_sigfft *)pd_new(sigifft_class); | ||
373 | outlet_new(&x->x_obj, gensym("signal")); | ||
374 | outlet_new(&x->x_obj, gensym("signal")); | ||
375 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
376 | x->x_f = 0; | ||
377 | return (x); | ||
378 | } | ||
379 | |||
380 | static t_int *sigfft_swap(t_int *w) | ||
381 | { | ||
382 | t_sample *in1 = (t_sample *)(w[1]); | ||
383 | t_sample *in2 = (t_sample *)(w[2]); | ||
384 | int n = w[3]; | ||
385 | for (;n--; in1++, in2++) | ||
386 | { | ||
387 | float f = *in1; | ||
388 | *in1 = *in2; | ||
389 | *in2 = f; | ||
390 | } | ||
391 | return (w+4); | ||
392 | } | ||
393 | |||
394 | static t_int *sigfft_perform(t_int *w) | ||
395 | { | ||
396 | t_sample *in1 = (t_sample *)(w[1]); | ||
397 | t_sample *in2 = (t_sample *)(w[2]); | ||
398 | int n = w[3]; | ||
399 | mayer_fft(n, in1, in2); | ||
400 | return (w+4); | ||
401 | } | ||
402 | |||
403 | static t_int *sigifft_perform(t_int *w) | ||
404 | { | ||
405 | t_sample *in1 = (t_sample *)(w[1]); | ||
406 | t_sample *in2 = (t_sample *)(w[2]); | ||
407 | int n = w[3]; | ||
408 | mayer_ifft(n, in1, in2); | ||
409 | return (w+4); | ||
410 | } | ||
411 | |||
412 | static void sigfft_dspx(t_sigfft *x, t_signal **sp, t_int *(*f)(t_int *w)) | ||
413 | { | ||
414 | int n = sp[0]->s_n; | ||
415 | t_sample *in1 = sp[0]->s_vec; | ||
416 | t_sample *in2 = sp[1]->s_vec; | ||
417 | t_sample *out1 = sp[2]->s_vec; | ||
418 | t_sample *out2 = sp[3]->s_vec; | ||
419 | if (out1 == in2 && out2 == in1) | ||
420 | dsp_add(sigfft_swap, 3, out1, out2, n); | ||
421 | else if (out1 == in2) | ||
422 | { | ||
423 | dsp_add(copy_perform, 3, in2, out2, n); | ||
424 | dsp_add(copy_perform, 3, in1, out1, n); | ||
425 | } | ||
426 | else | ||
427 | { | ||
428 | if (out1 != in1) dsp_add(copy_perform, 3, in1, out1, n); | ||
429 | if (out2 != in2) dsp_add(copy_perform, 3, in2, out2, n); | ||
430 | } | ||
431 | dsp_add(f, 3, sp[2]->s_vec, sp[3]->s_vec, n); | ||
432 | } | ||
433 | |||
434 | static void sigfft_dsp(t_sigfft *x, t_signal **sp) | ||
435 | { | ||
436 | sigfft_dspx(x, sp, sigfft_perform); | ||
437 | } | ||
438 | |||
439 | static void sigifft_dsp(t_sigfft *x, t_signal **sp) | ||
440 | { | ||
441 | sigfft_dspx(x, sp, sigifft_perform); | ||
442 | } | ||
443 | |||
444 | static void sigfft_setup(void) | ||
445 | { | ||
446 | sigfft_class = class_new(gensym("fft~"), sigfft_new, 0, | ||
447 | sizeof(t_sigfft), 0, 0); | ||
448 | CLASS_MAINSIGNALIN(sigfft_class, t_sigfft, x_f); | ||
449 | class_addmethod(sigfft_class, (t_method)sigfft_dsp, gensym("dsp"), 0); | ||
450 | |||
451 | sigifft_class = class_new(gensym("ifft~"), sigifft_new, 0, | ||
452 | sizeof(t_sigfft), 0, 0); | ||
453 | CLASS_MAINSIGNALIN(sigifft_class, t_sigfft, x_f); | ||
454 | class_addmethod(sigifft_class, (t_method)sigifft_dsp, gensym("dsp"), 0); | ||
455 | class_sethelpsymbol(sigifft_class, gensym("fft~")); | ||
456 | } | ||
457 | |||
458 | /* ----------------------- rfft~ -------------------------------- */ | ||
459 | |||
460 | static t_class *sigrfft_class; | ||
461 | |||
462 | typedef struct rfft | ||
463 | { | ||
464 | t_object x_obj; | ||
465 | float x_f; | ||
466 | } t_sigrfft; | ||
467 | |||
468 | static void *sigrfft_new(void) | ||
469 | { | ||
470 | t_sigrfft *x = (t_sigrfft *)pd_new(sigrfft_class); | ||
471 | outlet_new(&x->x_obj, gensym("signal")); | ||
472 | outlet_new(&x->x_obj, gensym("signal")); | ||
473 | x->x_f = 0; | ||
474 | return (x); | ||
475 | } | ||
476 | |||
477 | static t_int *sigrfft_flip(t_int *w) | ||
478 | { | ||
479 | t_sample *in = (t_sample *)(w[1]); | ||
480 | t_sample *out = (t_sample *)(w[2]); | ||
481 | int n = w[3]; | ||
482 | while (n--) *(--out) = *in++; | ||
483 | *(--out) = 0; /* to hell with it */ | ||
484 | return (w+4); | ||
485 | } | ||
486 | |||
487 | static t_int *sigrfft_perform(t_int *w) | ||
488 | { | ||
489 | t_sample *in = (t_sample *)(w[1]); | ||
490 | int n = w[2]; | ||
491 | mayer_realfft(n, in); | ||
492 | return (w+3); | ||
493 | } | ||
494 | |||
495 | static void sigrfft_dsp(t_sigrfft *x, t_signal **sp) | ||
496 | { | ||
497 | int n = sp[0]->s_n, n2 = (n>>1); | ||
498 | t_sample *in1 = sp[0]->s_vec; | ||
499 | t_sample *out1 = sp[1]->s_vec; | ||
500 | t_sample *out2 = sp[2]->s_vec; | ||
501 | if (n < 4) | ||
502 | { | ||
503 | error("fft: minimum 4 points"); | ||
504 | return; | ||
505 | } | ||
506 | if (in1 == out2) /* this probably never happens */ | ||
507 | { | ||
508 | dsp_add(sigrfft_perform, 2, out2, n); | ||
509 | dsp_add(copy_perform, 3, out2, out1, n2); | ||
510 | dsp_add(sigrfft_flip, 3, out2 + (n2+1), out2 + n2, n2-1); | ||
511 | } | ||
512 | else | ||
513 | { | ||
514 | if (in1 != out1) dsp_add(copy_perform, 3, in1, out1, n); | ||
515 | dsp_add(sigrfft_perform, 2, out1, n); | ||
516 | dsp_add(sigrfft_flip, 3, out1 + (n2+1), out2 + n2, n2-1); | ||
517 | } | ||
518 | dsp_add_zero(out1 + n2, n2); | ||
519 | dsp_add_zero(out2 + n2, n2); | ||
520 | } | ||
521 | |||
522 | static void sigrfft_setup(void) | ||
523 | { | ||
524 | sigrfft_class = class_new(gensym("rfft~"), sigrfft_new, 0, | ||
525 | sizeof(t_sigrfft), 0, 0); | ||
526 | CLASS_MAINSIGNALIN(sigrfft_class, t_sigrfft, x_f); | ||
527 | class_addmethod(sigrfft_class, (t_method)sigrfft_dsp, gensym("dsp"), 0); | ||
528 | class_sethelpsymbol(sigrfft_class, gensym("fft~")); | ||
529 | } | ||
530 | |||
531 | /* ----------------------- rifft~ -------------------------------- */ | ||
532 | |||
533 | static t_class *sigrifft_class; | ||
534 | |||
535 | typedef struct rifft | ||
536 | { | ||
537 | t_object x_obj; | ||
538 | float x_f; | ||
539 | } t_sigrifft; | ||
540 | |||
541 | static void *sigrifft_new(void) | ||
542 | { | ||
543 | t_sigrifft *x = (t_sigrifft *)pd_new(sigrifft_class); | ||
544 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
545 | outlet_new(&x->x_obj, gensym("signal")); | ||
546 | x->x_f = 0; | ||
547 | return (x); | ||
548 | } | ||
549 | |||
550 | static t_int *sigrifft_perform(t_int *w) | ||
551 | { | ||
552 | t_sample *in = (t_sample *)(w[1]); | ||
553 | int n = w[2]; | ||
554 | mayer_realifft(n, in); | ||
555 | return (w+3); | ||
556 | } | ||
557 | |||
558 | static void sigrifft_dsp(t_sigrifft *x, t_signal **sp) | ||
559 | { | ||
560 | int n = sp[0]->s_n, n2 = (n>>1); | ||
561 | t_sample *in1 = sp[0]->s_vec; | ||
562 | t_sample *in2 = sp[1]->s_vec; | ||
563 | t_sample *out1 = sp[2]->s_vec; | ||
564 | if (n < 4) | ||
565 | { | ||
566 | error("fft: minimum 4 points"); | ||
567 | return; | ||
568 | } | ||
569 | if (in2 == out1) | ||
570 | { | ||
571 | dsp_add(sigrfft_flip, 3, out1+1, out1 + n, (n2-1)); | ||
572 | dsp_add(copy_perform, 3, in1, out1, n2); | ||
573 | } | ||
574 | else | ||
575 | { | ||
576 | if (in1 != out1) dsp_add(copy_perform, 3, in1, out1, n2); | ||
577 | dsp_add(sigrfft_flip, 3, in2+1, out1 + n, n2-1); | ||
578 | } | ||
579 | dsp_add(sigrifft_perform, 2, out1, n); | ||
580 | } | ||
581 | |||
582 | static void sigrifft_setup(void) | ||
583 | { | ||
584 | sigrifft_class = class_new(gensym("rifft~"), sigrifft_new, 0, | ||
585 | sizeof(t_sigrifft), 0, 0); | ||
586 | CLASS_MAINSIGNALIN(sigrifft_class, t_sigrifft, x_f); | ||
587 | class_addmethod(sigrifft_class, (t_method)sigrifft_dsp, gensym("dsp"), 0); | ||
588 | class_sethelpsymbol(sigrifft_class, gensym("fft~")); | ||
589 | } | ||
590 | |||
591 | /* ----------------------- framp~ -------------------------------- */ | ||
592 | |||
593 | #if 0 | ||
594 | static t_class *sigframp_class; | ||
595 | |||
596 | typedef struct framp | ||
597 | { | ||
598 | t_object x_obj; | ||
599 | float x_f; | ||
600 | } t_sigframp; | ||
601 | |||
602 | static void *sigframp_new(void) | ||
603 | { | ||
604 | t_sigframp *x = (t_sigframp *)pd_new(sigframp_class); | ||
605 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
606 | outlet_new(&x->x_obj, gensym("signal")); | ||
607 | outlet_new(&x->x_obj, gensym("signal")); | ||
608 | x->x_f = 0; | ||
609 | return (x); | ||
610 | } | ||
611 | |||
612 | static t_int *sigframp_perform(t_int *w) | ||
613 | { | ||
614 | float *inreal = (t_float *)(w[1]); | ||
615 | float *inimag = (t_float *)(w[2]); | ||
616 | float *outfreq = (t_float *)(w[3]); | ||
617 | float *outamp = (t_float *)(w[4]); | ||
618 | float lastreal = 0, currentreal = inreal[0], nextreal = inreal[1]; | ||
619 | float lastimag = 0, currentimag = inimag[0], nextimag = inimag[1]; | ||
620 | int n = w[5]; | ||
621 | int m = n + 1; | ||
622 | float fbin = 1, oneovern2 = 1.f/((float)n * (float)n); | ||
623 | |||
624 | inreal += 2; | ||
625 | inimag += 2; | ||
626 | *outamp++ = *outfreq++ = 0; | ||
627 | n -= 2; | ||
628 | while (n--) | ||
629 | { | ||
630 | float re, im, pow, freq; | ||
631 | lastreal = currentreal; | ||
632 | currentreal = nextreal; | ||
633 | nextreal = *inreal++; | ||
634 | lastimag = currentimag; | ||
635 | currentimag = nextimag; | ||
636 | nextimag = *inimag++; | ||
637 | re = currentreal - 0.5f * (lastreal + nextreal); | ||
638 | im = currentimag - 0.5f * (lastimag + nextimag); | ||
639 | pow = re * re + im * im; | ||
640 | if (pow > 1e-19) | ||
641 | { | ||
642 | float detune = ((lastreal - nextreal) * re + | ||
643 | (lastimag - nextimag) * im) / (2.0f * pow); | ||
644 | if (detune > 2 || detune < -2) freq = pow = 0; | ||
645 | else freq = fbin + detune; | ||
646 | } | ||
647 | else freq = pow = 0; | ||
648 | *outfreq++ = freq; | ||
649 | *outamp++ = oneovern2 * pow; | ||
650 | fbin += 1.0f; | ||
651 | } | ||
652 | while (m--) *outamp++ = *outfreq++ = 0; | ||
653 | return (w+6); | ||
654 | } | ||
655 | |||
656 | t_int *sigsqrt_perform(t_int *w); | ||
657 | |||
658 | static void sigframp_dsp(t_sigframp *x, t_signal **sp) | ||
659 | { | ||
660 | int n = sp[0]->s_n, n2 = (n>>1); | ||
661 | if (n < 4) | ||
662 | { | ||
663 | error("framp: minimum 4 points"); | ||
664 | return; | ||
665 | } | ||
666 | dsp_add(sigframp_perform, 5, sp[0]->s_vec, sp[1]->s_vec, | ||
667 | sp[2]->s_vec, sp[3]->s_vec, n2); | ||
668 | dsp_add(sigsqrt_perform, 3, sp[3]->s_vec, sp[3]->s_vec, n2); | ||
669 | } | ||
670 | |||
671 | static void sigframp_setup(void) | ||
672 | { | ||
673 | sigframp_class = class_new(gensym("framp~"), sigframp_new, 0, | ||
674 | sizeof(t_sigframp), 0, 0); | ||
675 | CLASS_MAINSIGNALIN(sigframp_class, t_sigframp, x_f); | ||
676 | class_addmethod(sigframp_class, (t_method)sigframp_dsp, gensym("dsp"), 0); | ||
677 | } | ||
678 | #endif | ||
679 | |||
680 | /* ------------------------ global setup routine ------------------------- */ | ||
681 | |||
682 | void d_fft_setup(void) | ||
683 | { | ||
684 | sigfft_setup(); | ||
685 | sigrfft_setup(); | ||
686 | sigrifft_setup(); | ||
687 | // sigframp_setup(); | ||
688 | } | ||