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. */
5 /* upsampling/downsampling methods for inlet~/outlet~
10 * 2509:forum::für::umläute:2001
17 /* --------------------- up/down-sampling --------------------- */
18 t_int
*downsampling_perform_0(t_int
*w
)
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 */
35 t_int
*upsampling_perform_0(t_int
*w
)
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 */
43 t_sample
*dummy
= out
;
57 t_int
*upsampling_perform_hold(t_int
*w
)
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 */
66 t_sample
*dum_out
= out
;
67 t_sample
*dum_in
= in
;
81 t_int
*upsampling_perform_linear(t_int
*w
)
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
;
91 t_sample a
=*x
->buffer
, b
=*in
;
94 for (n
=0; n
<length
; n
++) {
95 t_float findex
= (t_float
)(n
+1)/up
;
97 t_sample frac
=findex
- index
;
99 *out
++ = frac
* b
+ (1.-frac
) * a
;
109 /* ----------------------- public -------------------------------- */
113 void resample_init(t_resample
*x
)
117 x
->downsample
=x
->upsample
=1;
119 x
->s_n
= x
->coefsize
= x
->bufsize
= 0;
120 x
->s_vec
= x
->coeffs
= x
->buffer
= 0;
123 void resample_free(t_resample
*x
)
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
));
129 x
->s_n
= x
->coefsize
= x
->bufsize
= 0;
130 x
->s_vec
= x
->coeffs
= x
->buffer
= 0;
136 void resample_dsp(t_resample
*x
,
137 t_sample
* in
, int insize
,
138 t_sample
* out
, int outsize
,
141 if (insize
== outsize
){
142 bug("nothing to be done");
146 if (insize
> outsize
) { /* downsampling */
147 if (insize
% outsize
) {
148 error("bad downsampling factor");
153 dsp_add(downsampling_perform_0
, 4, in
, out
, insize
/outsize
, insize
);
157 } else { /* upsampling */
158 if (outsize
% insize
) {
159 error("bad upsampling factor");
164 dsp_add(upsampling_perform_hold
, 4, in
, out
, outsize
/insize
, insize
);
167 if (x
->bufsize
!= 1) {
168 t_freebytes(x
->buffer
, x
->bufsize
*sizeof(*x
->buffer
));
170 x
->buffer
= t_getbytes(x
->bufsize
*sizeof(*x
->buffer
));
172 dsp_add(upsampling_perform_linear
, 5, x
, in
, out
, outsize
/insize
, insize
);
175 dsp_add(upsampling_perform_0
, 4, in
, out
, outsize
/insize
, insize
);
180 void resamplefrom_dsp(t_resample
*x
,
182 int insize
, int outsize
, int method
)
184 if (insize
==outsize
) {
185 t_freebytes(x
->s_vec
, x
->s_n
* sizeof(*x
->s_vec
));
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
));
199 resample_dsp(x
, in
, insize
, x
->s_vec
, x
->s_n
, method
);
203 void resampleto_dsp(t_resample
*x
,
205 int insize
, int outsize
, int method
)
207 if (insize
==outsize
) {
208 if (x
->s_n
)t_freebytes(x
->s_vec
, x
->s_n
* sizeof(*x
->s_vec
));
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
));
222 resample_dsp(x
, x
->s_vec
, x
->s_n
, out
, outsize
, method
);
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. */
230 /* upsampling/downsampling methods for inlet~/outlet~
235 * 2509:forum::für::umläute:2001
242 /* --------------------- up/down-sampling --------------------- */
243 t_int
*downsampling_perform_0(t_int
*w
)
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 */
260 t_int
*upsampling_perform_0(t_int
*w
)
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 */
268 t_sample
*dummy
= out
;
282 t_int
*upsampling_perform_hold(t_int
*w
)
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 */
291 t_sample
*dum_out
= out
;
292 t_sample
*dum_in
= in
;
306 t_int
*upsampling_perform_linear(t_int
*w
)
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
;
316 t_sample a
=*x
->buffer
, b
=*in
;
319 for (n
=0; n
<length
; n
++) {
320 t_float findex
= (t_float
)(n
+1)/up
;
322 t_sample frac
=findex
- index
;
323 if (frac
==0.)frac
=1.;
324 *out
++ = frac
* b
+ (1.-frac
) * a
;
334 /* ----------------------- public -------------------------------- */
338 void resample_init(t_resample
*x
)
342 x
->downsample
=x
->upsample
=1;
344 x
->s_n
= x
->coefsize
= x
->bufsize
= 0;
345 x
->s_vec
= x
->coeffs
= x
->buffer
= 0;
348 void resample_free(t_resample
*x
)
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
));
354 x
->s_n
= x
->coefsize
= x
->bufsize
= 0;
355 x
->s_vec
= x
->coeffs
= x
->buffer
= 0;
361 void resample_dsp(t_resample
*x
,
362 t_sample
* in
, int insize
,
363 t_sample
* out
, int outsize
,
366 if (insize
== outsize
){
367 bug("nothing to be done");
371 if (insize
> outsize
) { /* downsampling */
372 if (insize
% outsize
) {
373 error("bad downsampling factor");
378 dsp_add(downsampling_perform_0
, 4, in
, out
, insize
/outsize
, insize
);
382 } else { /* upsampling */
383 if (outsize
% insize
) {
384 error("bad upsampling factor");
389 dsp_add(upsampling_perform_hold
, 4, in
, out
, outsize
/insize
, insize
);
392 if (x
->bufsize
!= 1) {
393 t_freebytes(x
->buffer
, x
->bufsize
*sizeof(*x
->buffer
));
395 x
->buffer
= t_getbytes(x
->bufsize
*sizeof(*x
->buffer
));
397 dsp_add(upsampling_perform_linear
, 5, x
, in
, out
, outsize
/insize
, insize
);
400 dsp_add(upsampling_perform_0
, 4, in
, out
, outsize
/insize
, insize
);
405 void resamplefrom_dsp(t_resample
*x
,
407 int insize
, int outsize
, int method
)
409 if (insize
==outsize
) {
410 t_freebytes(x
->s_vec
, x
->s_n
* sizeof(*x
->s_vec
));
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
));
424 resample_dsp(x
, in
, insize
, x
->s_vec
, x
->s_n
, method
);
428 void resampleto_dsp(t_resample
*x
,
430 int insize
, int outsize
, int method
)
432 if (insize
==outsize
) {
433 if (x
->s_n
)t_freebytes(x
->s_vec
, x
->s_n
* sizeof(*x
->s_vec
));
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
));
447 resample_dsp(x
, x
->s_vec
, x
->s_n
, out
, outsize
, method
);