-
Notifications
You must be signed in to change notification settings - Fork 4
/
pvcompand~.c
326 lines (291 loc) · 9.35 KB
/
pvcompand~.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* FFTease for Pd */
#include "fftease.h"
static t_class *pvcompand_class;
#define OBJECT_NAME "pvcompand~"
typedef struct _pvcompand
{
t_object x_obj;
t_float x_f;
t_fftease *fft;
t_float rescale;
t_float *curthresh;
t_float *atten;
t_float *thresh;
int count;
t_float thresh_interval;
t_float max_atten;
t_float atten_interval;
t_float tstep;
t_float gstep;
t_float last_max_atten;
short norml;
short mute;
} t_pvcompand;
static void pvcompand_dsp(t_pvcompand *x, t_signal **sp);
static t_int *pvcompand_perform(t_int *w);
static void *pvcompand_new(t_symbol *s, int argc, t_atom *argv);
static void update_thresholds(t_pvcompand *x);
static void pvcompand_normalize(t_pvcompand *x, t_floatarg val);
static void pvcompand_free(t_pvcompand *x);
static t_float pvcompand_ampdb(t_float db);
static void pvcompand_init(t_pvcompand *x);
static void pvcompand_mute(t_pvcompand *x, t_floatarg f);
void pvcompand_tilde_setup(void)
{
t_class *c;
c = class_new(gensym("pvcompand~"), (t_newmethod)pvcompand_new,
(t_method)pvcompand_free,sizeof(t_pvcompand), 0,A_GIMME,0);
CLASS_MAINSIGNALIN(c, t_pvcompand, x_f);
class_addmethod(c,(t_method)pvcompand_dsp,gensym("dsp"), A_CANT, 0);
class_addmethod(c,(t_method)pvcompand_mute,gensym("mute"),A_FLOAT,0);
class_addmethod(c,(t_method)pvcompand_normalize,gensym("normalize"), A_FLOAT, 0);
pvcompand_class = c;
fftease_announce(OBJECT_NAME);
}
void pvcompand_mute(t_pvcompand *x, t_floatarg f)
{
x->mute = (short)f;
}
void pvcompand_free( t_pvcompand *x ){
if(x->fft->initialized){
free(x->curthresh);
free(x->atten);
free(x->thresh);
}
fftease_free(x->fft);
free(x->fft);
}
void *pvcompand_new(t_symbol *s, int argc, t_atom *argv)
{
t_fftease *fft;
t_pvcompand *x = (t_pvcompand *)pd_new(pvcompand_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
outlet_new(&x->x_obj, gensym("signal"));
x->fft = (t_fftease *) calloc(1,sizeof(t_fftease));
fft = x->fft;
fft->initialized = 0;
x->max_atten = -6.0;
fft->N = FFTEASE_DEFAULT_FFTSIZE;
fft->overlap = FFTEASE_DEFAULT_OVERLAP;
fft->winfac = FFTEASE_DEFAULT_WINFAC;
if(argc > 0){ fft->N = (int) atom_getfloatarg(0, argc, argv); }
if(argc > 1){ fft->overlap = (int) atom_getfloatarg(1, argc, argv); }
return x;
}
void pvcompand_init(t_pvcompand *x)
{
t_fftease *fft = x->fft;
short initialized = x->fft->initialized;
fftease_init(fft);
if(!initialized){
x->norml = 0;
x->mute = 0;
x->thresh_interval = 1.0;
x->last_max_atten = x->max_atten;
x->atten_interval = 2.0 ;
x->tstep = 1.0 ;
x->gstep = 2.0 ;
x->thresh = (t_float *) calloc((fft->N), sizeof(t_float));
x->atten = (t_float *) calloc(fft->N, sizeof(t_float) );
x->curthresh = (t_float *) calloc(fft->N, sizeof(t_float) );
} else if(initialized == 1) {
x->thresh = (t_float *) realloc(x->thresh, fft->N * sizeof(t_float));
x->atten = (t_float *) realloc(x->atten, fft->N * sizeof(t_float));
x->curthresh = (t_float *) realloc(x->curthresh, fft->N * sizeof(t_float));
}
update_thresholds(x);
}
void update_thresholds( t_pvcompand *x ) {
int i;
t_float nowamp = x->max_atten ;
t_float nowthresh = 0.0 ;
int N = x->fft->N;
x->count = 0;
if( nowamp < 0.0 ){
while( nowamp < 0.0 ){
x->atten[x->count] = pvcompand_ampdb( nowamp );
nowamp += x->gstep ;
++(x->count);
if(x->count >= N){
pd_error(0, "count exceeds %d",N);
x->count = N - 1;
break;
}
}
}
else if( nowamp > 0.0 ){
while( nowamp > 0.0 ){
x->atten[x->count] = pvcompand_ampdb( nowamp );
nowamp -= x->gstep ;
++(x->count);
if(x->count >= N){
pd_error(0, "count exceeds %d",N);
x->count = N - 1;
break;
}
}
}
for( i = 0; i < x->count; i++){
x->thresh[i] = pvcompand_ampdb( nowthresh );
nowthresh -= x->tstep ;
}
}
void pvcompand_normalize(t_pvcompand *x, t_floatarg val)
{
x->norml = (short)val;
}
static void do_pvcompand(t_pvcompand *x)
{
t_fftease *fft = x->fft;
t_float *channel = fft->channel;
int N = fft->N;
t_float *curthresh = x->curthresh;
t_float *thresh = x->thresh;
t_float *atten = x->atten;
int count = x->count;
t_float max_atten = x->max_atten;
int i,j;
t_float maxamp ;
t_float cutoff;
t_float avr, new_avr, rescale;
fftease_fold(fft);
fftease_rdft(fft,FFT_FORWARD);
fftease_leanconvert(fft);
maxamp = 0.;
avr = 0;
for( i = 0; i < N; i+= 2 ){
avr += channel[i];
if( maxamp < channel[i] ){
maxamp = channel[i] ;
}
}
if(count <= 1){
// post("count too low!");
count = 1;
}
for( i = 0; i < count; i++ ){
curthresh[i] = thresh[i]*maxamp ;
}
cutoff = curthresh[count-1];
new_avr = 0;
for( i = 0; i < N; i += 2){
if( channel[i] > cutoff ){
j = count-1;
while( channel[i] > curthresh[j] ){
j--;
if( j < 0 ){
j = 0;
break;
}
}
channel[i] *= atten[j];
}
new_avr += channel[i] ;
}
if( x->norml ) {
if( new_avr <= 0 ){
new_avr = .0001;
}
rescale = avr / new_avr ;
} else {
rescale = pvcompand_ampdb( max_atten * -.5);
}
for( i = 0; i < N; i += 2){
channel[i] *= rescale;
}
fftease_leanunconvert(fft);
fftease_rdft(fft, FFT_INVERSE);
fftease_overlapadd(fft);
}
t_int *pvcompand_perform(t_int *w)
{
int i,j;
t_pvcompand *x = (t_pvcompand *) (w[1]);
t_float *MSPInputVector = (t_float *)(w[2]);
t_float *in2 = (t_float *)(w[3]);
t_float *MSPOutputVector = (t_float *)(w[4]);
t_fftease *fft = x->fft;
int D = fft->D;
int Nw = fft->Nw;
t_float *input = fft->input;
t_float *output = fft->output;
t_float mult = fft->mult;
int MSPVectorSize = fft->MSPVectorSize;
t_float *internalInputVector = fft->internalInputVector;
t_float *internalOutputVector = fft->internalOutputVector;
int operationRepeat = fft->operationRepeat;
int operationCount = fft->operationCount;
if(x->mute){
for(i=0; i < MSPVectorSize; i++){ MSPOutputVector[i] = 0.0; }
return w+5;
}
x->max_atten = *in2;
if(x->max_atten != x->last_max_atten) {
x->last_max_atten = x->max_atten;
update_thresholds(x);
}
if( fft->bufferStatus == EQUAL_TO_MSP_VECTOR ){
memcpy(input, input + D, (Nw - D) * sizeof(t_float));
memcpy(input + (Nw - D), MSPInputVector, D * sizeof(t_float));
do_pvcompand(x);
for ( j = 0; j < D; j++ ){ *MSPOutputVector++ = output[j] * mult; }
memcpy(output, output + D, (Nw-D) * sizeof(t_float));
for(j = (Nw-D); j < Nw; j++){ output[j] = 0.0; }
}
else if( fft->bufferStatus == SMALLER_THAN_MSP_VECTOR ) {
for( i = 0; i < operationRepeat; i++ ){
memcpy(input, input + D, (Nw - D) * sizeof(t_float));
memcpy(input + (Nw-D), MSPInputVector + (D*i), D * sizeof(t_float));
do_pvcompand(x);
for ( j = 0; j < D; j++ ){ *MSPOutputVector++ = output[j] * mult; }
memcpy(output, output + D, (Nw-D) * sizeof(t_float));
for(j = (Nw-D); j < Nw; j++){ output[j] = 0.0; }
}
}
else if( fft->bufferStatus == BIGGER_THAN_MSP_VECTOR ) {
memcpy(internalInputVector + (operationCount * MSPVectorSize), MSPInputVector,MSPVectorSize * sizeof(t_float));
memcpy(MSPOutputVector, internalOutputVector + (operationCount * MSPVectorSize),MSPVectorSize * sizeof(t_float));
operationCount = (operationCount + 1) % operationRepeat;
if( operationCount == 0 ) {
memcpy(input, input + D, (Nw - D) * sizeof(t_float));
memcpy(input + (Nw - D), internalInputVector, D * sizeof(t_float));
do_pvcompand(x);
for ( j = 0; j < D; j++ ){ internalOutputVector[j] = output[j] * mult; }
memcpy(output, output + D, (Nw - D) * sizeof(t_float));
for(j = (Nw-D); j < Nw; j++){ output[j] = 0.0; }
}
fft->operationCount = operationCount;
}
return w+5;
}
t_float pvcompand_ampdb(t_float db)
{
t_float amp;
amp = pow((t_float)10.0, (t_float)(db/20.0)) ;
return(amp);
}
void pvcompand_dsp(t_pvcompand *x, t_signal **sp)
{
int reset_required = 0;
int maxvectorsize = sp[0]->s_n;
int samplerate = sp[0]->s_sr;
if(!samplerate)
return;
t_fftease *fft = x->fft;
if(fft->R != samplerate || fft->MSPVectorSize != maxvectorsize || fft->initialized == 0){
reset_required = 1;
}
if(fft->MSPVectorSize != maxvectorsize){
fft->MSPVectorSize = maxvectorsize;
fftease_set_fft_buffers(fft);
}
if(fft->R != samplerate){
fft->R = samplerate;
}
if(reset_required){
pvcompand_init(x);
}
if(fftease_msp_sanity_check(fft,OBJECT_NAME)) {
dsp_add(pvcompand_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec);
}
}