-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path+decimate~.c
268 lines (217 loc) · 5.98 KB
/
+decimate~.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
May 20, 2010: using a single makefile for all sources that should work on both mac and windows. removing the sethelpsymbol() and going with the standard whatever~-help.pd convention. also adding static to all function names (except setup).
Nov 27, 2009: 12:17PM - changed to int32_t instead of long for longTmp lMask and lMask2 in the perform routine. Also included stdint.h to enable this.
*/
#include <stdint.h>
#include <math.h>
#include "m_pd.h"
#define STDBLOCK 256
static t_class *decimate_tilde_class;
typedef struct _decimate_tilde
{
t_object x_obj;
float x_sr;
float x_n;
int x_block;
t_sample *x_inBuffer;
t_sample *x_outBuffer;
long x_lMask;
long x_lMask2;
float x_maskMix;
float x_averageScale;
float x_avg;
float x_f;
} t_decimate_tilde;
static void decimate_tilde_depth(t_decimate_tilde *x, t_floatarg d)
{
unsigned long mask, bit;
long i, bitDepth;
if(d>32.0 || d<1.0)
error("depth value must be >= 1.0 and <= 24.0.");
else
{
bitDepth = (long)d;
x->x_maskMix = d - bitDepth;
mask = 0;
for(i = 0, bit = 0x80000000UL; i < bitDepth; i++)
{
mask = mask | bit;
bit = bit>>1;
}
x->x_lMask = mask;
mask = 0;
for(i = 0, bit = 0x80000000UL; i < bitDepth+1; i++)
{
mask = mask | bit;
bit = bit>>1;
}
x->x_lMask2 = mask;
};
}
static void decimate_tilde_folds(t_decimate_tilde *x, t_floatarg f)
{
if(f>8.0 || f<0.0)
error("folds value must be between 0 and 8.");
else
{
x->x_avg = powf(2.0, f);
x->x_averageScale = 1.0/x->x_avg;
}
}
static void *decimate_tilde_new()
{
t_decimate_tilde *x = (t_decimate_tilde *)pd_new(decimate_tilde_class);
int i;
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("depth"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("folds"));
outlet_new(&x->x_obj, &s_signal);
x->x_inBuffer = (t_sample *)getbytes(0);
x->x_inBuffer = (t_sample *)t_resizebytes(x->x_inBuffer, 0, STDBLOCK * sizeof(t_sample));
// init signal buffer
for(i=0; i<STDBLOCK; i++)
x->x_inBuffer[i] = 0.0;
x->x_outBuffer = (t_sample *)t_resizebytes(x->x_outBuffer, 0, STDBLOCK * sizeof(t_sample));
// init out buffer
for(i=0; i<STDBLOCK; i++)
x->x_outBuffer[i] = 0.0;
x->x_n = 64;
x->x_block = STDBLOCK;
x->x_lMask = 0xffffffff;
x->x_lMask2 = 0xffffffff;
x->x_maskMix = 0.0;
x->x_avg = 1.0;
x->x_averageScale = 1.0/x->x_avg;
return(void *)x;
};
static t_int *decimate_tilde_perform(t_int *w)
{
int i, j, block, avg;
float tmp, maskMix, avgScale;
int32_t longTmp, lMask, lMask2;
t_decimate_tilde *x = (t_decimate_tilde *)(w[1]);
t_sample *in1 = (t_sample *)(w[2]);
t_sample *out = (t_sample *)(w[3]);
int n = (int)(w[4]);
t_sample *pOutBuffer;
// grab a pointer to outBuffer for the final while(n--)
pOutBuffer = x->x_outBuffer;
// rename dataspace variables locally
block = x->x_block;
avg = (int)x->x_avg;
avgScale = x->x_averageScale;
lMask = x->x_lMask;
lMask2 = x->x_lMask2;
maskMix = x->x_maskMix;
// init out buffer
for(i=0; i<n; i++)
x->x_outBuffer[i] = 0.0;
// shift signal buffer contents back.
for(i=0; i<(block-n); i++)
x->x_inBuffer[i] = x->x_inBuffer[i+n];
// write new block to end of signal buffer.
for(i=0; i<n; i++)
x->x_inBuffer[block-n+i] = in1[i];
for(i=0; i<n; i+=avg)
{
for(j=0, tmp=0; j<avg; j++)
tmp += x->x_inBuffer[i+j];
tmp *= avgScale; // avoid divides using precomputed avgScale
for(j=0, longTmp=0; j<avg; j++)
{
longTmp = (int32_t)(tmp * 2147483647.0f);
longTmp = (int32_t)((longTmp & lMask) + ((longTmp & lMask2) - (longTmp & lMask)) * maskMix);
x->x_outBuffer[i+j] = (float)longTmp * 0.0000000004656612875245797f;
};
};
while(n--)
*out++ = *pOutBuffer++;
return(w+5);
};
static void decimate_tilde_dsp(t_decimate_tilde *x, t_signal **sp)
{
int i;
dsp_add(
decimate_tilde_perform,
4,
x,
sp[0]->s_vec,
sp[1]->s_vec,
sp[0]->s_n
);
// update blocksize if it has changed
if(x->x_n != sp[0]->s_n)
{
if(sp[0]->s_n > STDBLOCK)
{
// resize signal buffer
x->x_inBuffer = (t_sample *)t_resizebytes(x->x_inBuffer, x->x_block * sizeof(t_sample), sp[0]->s_n * sizeof(t_sample));
// init signal buffer
for(i=0; i<sp[0]->s_n; i++)
x->x_inBuffer[i] = 0.0;
// resize out buffer
x->x_outBuffer = (t_sample *)t_resizebytes(x->x_outBuffer, x->x_block * sizeof(t_sample), sp[0]->s_n * sizeof(t_sample));
// init out buffer
for(i=0; i<sp[0]->s_n; i++)
x->x_outBuffer[i] = 0.0;
x->x_block = sp[0]->s_n;
}
else if( (sp[0]->s_n <= STDBLOCK) && (x->x_n > STDBLOCK) )
{
// resize signal buffer
x->x_inBuffer = (t_sample *)t_resizebytes(x->x_inBuffer, x->x_block * sizeof(t_sample), STDBLOCK * sizeof(t_sample));
// init signal buffer
for(i=0; i<STDBLOCK; i++)
x->x_inBuffer[i] = 0.0;
// resize out buffer
x->x_outBuffer = (t_sample *)t_resizebytes(x->x_outBuffer, x->x_block * sizeof(t_sample), STDBLOCK * sizeof(t_sample));
// init out buffer
for(i=0; i<STDBLOCK; i++)
x->x_outBuffer[i] = 0.0;
x->x_block = STDBLOCK;
};
x->x_n = sp[0]->s_n;
}
if(sp[0]->s_sr != x->x_sr)
x->x_sr = sp[0]->s_sr;
};
static void decimate_tilde_free(t_decimate_tilde *x)
{
// free the input buffer memory
t_freebytes(x->x_inBuffer, x->x_block*sizeof(t_sample));
// free the input buffer memory
t_freebytes(x->x_outBuffer, x->x_block*sizeof(t_sample));
};
// unusual name for setup routine to allow for "+".
void setup_0x2bdecimate_tilde(void)
{
decimate_tilde_class =
class_new(
gensym("+decimate~"),
(t_newmethod)decimate_tilde_new,
(t_method)decimate_tilde_free,
sizeof(t_decimate_tilde),
CLASS_DEFAULT,
0
);
CLASS_MAINSIGNALIN(decimate_tilde_class, t_decimate_tilde, x_f);
class_addmethod(
decimate_tilde_class,
(t_method)decimate_tilde_dsp,
gensym("dsp"),
0
);
class_addmethod(
decimate_tilde_class,
(t_method)decimate_tilde_depth,
gensym("depth"),
A_DEFFLOAT,
0
);
class_addmethod(
decimate_tilde_class,
(t_method)decimate_tilde_folds,
gensym("folds"),
A_DEFFLOAT,
0
);
}