summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/d_resample.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/d_resample.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/d_resample.c450
1 files changed, 450 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/d_resample.c b/apps/plugins/pdbox/PDa/src/d_resample.c
new file mode 100644
index 0000000000..4e617ec4df
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/d_resample.c
@@ -0,0 +1,450 @@
1/* Copyright (c) 1997-1999 Miller Puckette.
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/* upsampling/downsampling methods for inlet~/outlet~
6 *
7 * mfg.gfd.uil
8 * IOhannes
9 *
10 * 2509:forum::für::umläute:2001
11 */
12
13
14
15#include "m_pd.h"
16
17/* --------------------- up/down-sampling --------------------- */
18t_int *downsampling_perform_0(t_int *w)
19{
20 t_sample *in = (t_sample *)(w[1]); /* original signal */
21 t_sample *out = (t_sample *)(w[2]); /* downsampled signal */
22 int down = (int)(w[3]); /* downsampling factor */
23 int parent = (int)(w[4]); /* original vectorsize */
24
25 int n=parent/down;
26
27 while(n--){
28 *out++=*in;
29 in+=down;
30 }
31
32 return (w+5);
33}
34
35t_int *upsampling_perform_0(t_int *w)
36{
37 t_sample *in = (t_sample *)(w[1]); /* original signal */
38 t_sample *out = (t_sample *)(w[2]); /* upsampled signal */
39 int up = (int)(w[3]); /* upsampling factor */
40 int parent = (int)(w[4]); /* original vectorsize */
41
42 int n=parent*up;
43 t_sample *dummy = out;
44
45 while(n--)*out++=0;
46
47 n = parent;
48 out = dummy;
49 while(n--){
50 *out=*in++;
51 out+=up;
52 }
53
54 return (w+5);
55}
56
57t_int *upsampling_perform_hold(t_int *w)
58{
59 t_sample *in = (t_sample *)(w[1]); /* original signal */
60 t_sample *out = (t_sample *)(w[2]); /* upsampled signal */
61 int up = (int)(w[3]); /* upsampling factor */
62 int parent = (int)(w[4]); /* original vectorsize */
63 int i=up;
64
65 int n=parent;
66 t_sample *dum_out = out;
67 t_sample *dum_in = in;
68
69 while (i--) {
70 n = parent;
71 out = dum_out+i;
72 in = dum_in;
73 while(n--){
74 *out=*in++;
75 out+=up;
76 }
77 }
78 return (w+5);
79}
80
81t_int *upsampling_perform_linear(t_int *w)
82{
83 t_resample *x= (t_resample *)(w[1]);
84 t_sample *in = (t_sample *)(w[2]); /* original signal */
85 t_sample *out = (t_sample *)(w[3]); /* upsampled signal */
86 int up = (int)(w[4]); /* upsampling factor */
87 int parent = (int)(w[5]); /* original vectorsize */
88 int length = parent*up;
89 int n;
90 t_sample *fp;
91 t_sample a=*x->buffer, b=*in;
92
93
94 for (n=0; n<length; n++) {
95 t_float findex = (t_float)(n+1)/up;
96 int index = findex;
97 t_sample frac=findex - index;
98 if (frac==0.)frac=1.;
99 *out++ = frac * b + (1.-frac) * a;
100 fp = in+index;
101 b=*fp;
102 a=(index)?*(fp-1):a;
103 }
104
105 *x->buffer = a;
106 return (w+6);
107}
108
109/* ----------------------- public -------------------------------- */
110
111/* utils */
112
113void resample_init(t_resample *x)
114{
115 x->method=0;
116
117 x->downsample=x->upsample=1;
118
119 x->s_n = x->coefsize = x->bufsize = 0;
120 x->s_vec = x->coeffs = x->buffer = 0;
121}
122
123void resample_free(t_resample *x)
124{
125 if (x->s_n) t_freebytes(x->s_vec, x->s_n*sizeof(*x->s_vec));
126 if (x->coefsize) t_freebytes(x->coeffs, x->coefsize*sizeof(*x->coeffs));
127 if (x->bufsize) t_freebytes(x->buffer, x->bufsize*sizeof(*x->buffer));
128
129 x->s_n = x->coefsize = x->bufsize = 0;
130 x->s_vec = x->coeffs = x->buffer = 0;
131}
132
133
134/* dsp-adding */
135
136void resample_dsp(t_resample *x,
137 t_sample* in, int insize,
138 t_sample* out, int outsize,
139 int method)
140{
141 if (insize == outsize){
142 bug("nothing to be done");
143 return;
144 }
145
146 if (insize > outsize) { /* downsampling */
147 if (insize % outsize) {
148 error("bad downsampling factor");
149 return;
150 }
151 switch (method) {
152 default:
153 dsp_add(downsampling_perform_0, 4, in, out, insize/outsize, insize);
154 }
155
156
157 } else { /* upsampling */
158 if (outsize % insize) {
159 error("bad upsampling factor");
160 return;
161 }
162 switch (method) {
163 case 1:
164 dsp_add(upsampling_perform_hold, 4, in, out, outsize/insize, insize);
165 break;
166 case 2:
167 if (x->bufsize != 1) {
168 t_freebytes(x->buffer, x->bufsize*sizeof(*x->buffer));
169 x->bufsize = 1;
170 x->buffer = t_getbytes(x->bufsize*sizeof(*x->buffer));
171 }
172 dsp_add(upsampling_perform_linear, 5, x, in, out, outsize/insize, insize);
173 break;
174 default:
175 dsp_add(upsampling_perform_0, 4, in, out, outsize/insize, insize);
176 }
177 }
178}
179
180void resamplefrom_dsp(t_resample *x,
181 t_sample *in,
182 int insize, int outsize, int method)
183{
184 if (insize==outsize) {
185 t_freebytes(x->s_vec, x->s_n * sizeof(*x->s_vec));
186 x->s_n = 0;
187 x->s_vec = in;
188 return;
189 }
190
191 if (x->s_n != outsize) {
192 t_sample *buf=x->s_vec;
193 t_freebytes(buf, x->s_n * sizeof(*buf));
194 buf = (t_sample *)t_getbytes(outsize * sizeof(*buf));
195 x->s_vec = buf;
196 x->s_n = outsize;
197 }
198
199 resample_dsp(x, in, insize, x->s_vec, x->s_n, method);
200 return;
201}
202
203void resampleto_dsp(t_resample *x,
204 t_sample *out,
205 int insize, int outsize, int method)
206{
207 if (insize==outsize) {
208 if (x->s_n)t_freebytes(x->s_vec, x->s_n * sizeof(*x->s_vec));
209 x->s_n = 0;
210 x->s_vec = out;
211 return;
212 }
213
214 if (x->s_n != insize) {
215 t_sample *buf=x->s_vec;
216 t_freebytes(buf, x->s_n * sizeof(*buf));
217 buf = (t_sample *)t_getbytes(insize * sizeof(*buf));
218 x->s_vec = buf;
219 x->s_n = insize;
220 }
221
222 resample_dsp(x, x->s_vec, x->s_n, out, outsize, method);
223
224 return;
225}
226/* Copyright (c) 1997-1999 Miller Puckette.
227 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
228 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
229
230/* upsampling/downsampling methods for inlet~/outlet~
231 *
232 * mfg.gfd.uil
233 * IOhannes
234 *
235 * 2509:forum::für::umläute:2001
236 */
237
238
239
240#include "m_pd.h"
241
242/* --------------------- up/down-sampling --------------------- */
243t_int *downsampling_perform_0(t_int *w)
244{
245 t_sample *in = (t_sample *)(w[1]); /* original signal */
246 t_sample *out = (t_sample *)(w[2]); /* downsampled signal */
247 int down = (int)(w[3]); /* downsampling factor */
248 int parent = (int)(w[4]); /* original vectorsize */
249
250 int n=parent/down;
251
252 while(n--){
253 *out++=*in;
254 in+=down;
255 }
256
257 return (w+5);
258}
259
260t_int *upsampling_perform_0(t_int *w)
261{
262 t_sample *in = (t_sample *)(w[1]); /* original signal */
263 t_sample *out = (t_sample *)(w[2]); /* upsampled signal */
264 int up = (int)(w[3]); /* upsampling factor */
265 int parent = (int)(w[4]); /* original vectorsize */
266
267 int n=parent*up;
268 t_sample *dummy = out;
269
270 while(n--)*out++=0;
271
272 n = parent;
273 out = dummy;
274 while(n--){
275 *out=*in++;
276 out+=up;
277 }
278
279 return (w+5);
280}
281
282t_int *upsampling_perform_hold(t_int *w)
283{
284 t_sample *in = (t_sample *)(w[1]); /* original signal */
285 t_sample *out = (t_sample *)(w[2]); /* upsampled signal */
286 int up = (int)(w[3]); /* upsampling factor */
287 int parent = (int)(w[4]); /* original vectorsize */
288 int i=up;
289
290 int n=parent;
291 t_sample *dum_out = out;
292 t_sample *dum_in = in;
293
294 while (i--) {
295 n = parent;
296 out = dum_out+i;
297 in = dum_in;
298 while(n--){
299 *out=*in++;
300 out+=up;
301 }
302 }
303 return (w+5);
304}
305
306t_int *upsampling_perform_linear(t_int *w)
307{
308 t_resample *x= (t_resample *)(w[1]);
309 t_sample *in = (t_sample *)(w[2]); /* original signal */
310 t_sample *out = (t_sample *)(w[3]); /* upsampled signal */
311 int up = (int)(w[4]); /* upsampling factor */
312 int parent = (int)(w[5]); /* original vectorsize */
313 int length = parent*up;
314 int n;
315 t_sample *fp;
316 t_sample a=*x->buffer, b=*in;
317
318
319 for (n=0; n<length; n++) {
320 t_float findex = (t_float)(n+1)/up;
321 int index = findex;
322 t_sample frac=findex - index;
323 if (frac==0.)frac=1.;
324 *out++ = frac * b + (1.-frac) * a;
325 fp = in+index;
326 b=*fp;
327 a=(index)?*(fp-1):a;
328 }
329
330 *x->buffer = a;
331 return (w+6);
332}
333
334/* ----------------------- public -------------------------------- */
335
336/* utils */
337
338void resample_init(t_resample *x)
339{
340 x->method=0;
341
342 x->downsample=x->upsample=1;
343
344 x->s_n = x->coefsize = x->bufsize = 0;
345 x->s_vec = x->coeffs = x->buffer = 0;
346}
347
348void resample_free(t_resample *x)
349{
350 if (x->s_n) t_freebytes(x->s_vec, x->s_n*sizeof(*x->s_vec));
351 if (x->coefsize) t_freebytes(x->coeffs, x->coefsize*sizeof(*x->coeffs));
352 if (x->bufsize) t_freebytes(x->buffer, x->bufsize*sizeof(*x->buffer));
353
354 x->s_n = x->coefsize = x->bufsize = 0;
355 x->s_vec = x->coeffs = x->buffer = 0;
356}
357
358
359/* dsp-adding */
360
361void resample_dsp(t_resample *x,
362 t_sample* in, int insize,
363 t_sample* out, int outsize,
364 int method)
365{
366 if (insize == outsize){
367 bug("nothing to be done");
368 return;
369 }
370
371 if (insize > outsize) { /* downsampling */
372 if (insize % outsize) {
373 error("bad downsampling factor");
374 return;
375 }
376 switch (method) {
377 default:
378 dsp_add(downsampling_perform_0, 4, in, out, insize/outsize, insize);
379 }
380
381
382 } else { /* upsampling */
383 if (outsize % insize) {
384 error("bad upsampling factor");
385 return;
386 }
387 switch (method) {
388 case 1:
389 dsp_add(upsampling_perform_hold, 4, in, out, outsize/insize, insize);
390 break;
391 case 2:
392 if (x->bufsize != 1) {
393 t_freebytes(x->buffer, x->bufsize*sizeof(*x->buffer));
394 x->bufsize = 1;
395 x->buffer = t_getbytes(x->bufsize*sizeof(*x->buffer));
396 }
397 dsp_add(upsampling_perform_linear, 5, x, in, out, outsize/insize, insize);
398 break;
399 default:
400 dsp_add(upsampling_perform_0, 4, in, out, outsize/insize, insize);
401 }
402 }
403}
404
405void resamplefrom_dsp(t_resample *x,
406 t_sample *in,
407 int insize, int outsize, int method)
408{
409 if (insize==outsize) {
410 t_freebytes(x->s_vec, x->s_n * sizeof(*x->s_vec));
411 x->s_n = 0;
412 x->s_vec = in;
413 return;
414 }
415
416 if (x->s_n != outsize) {
417 t_sample *buf=x->s_vec;
418 t_freebytes(buf, x->s_n * sizeof(*buf));
419 buf = (t_sample *)t_getbytes(outsize * sizeof(*buf));
420 x->s_vec = buf;
421 x->s_n = outsize;
422 }
423
424 resample_dsp(x, in, insize, x->s_vec, x->s_n, method);
425 return;
426}
427
428void resampleto_dsp(t_resample *x,
429 t_sample *out,
430 int insize, int outsize, int method)
431{
432 if (insize==outsize) {
433 if (x->s_n)t_freebytes(x->s_vec, x->s_n * sizeof(*x->s_vec));
434 x->s_n = 0;
435 x->s_vec = out;
436 return;
437 }
438
439 if (x->s_n != insize) {
440 t_sample *buf=x->s_vec;
441 t_freebytes(buf, x->s_n * sizeof(*buf));
442 buf = (t_sample *)t_getbytes(insize * sizeof(*buf));
443 x->s_vec = buf;
444 x->s_n = insize;
445 }
446
447 resample_dsp(x, x->s_vec, x->s_n, out, outsize, method);
448
449 return;
450}