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
);