-
Notifications
You must be signed in to change notification settings - Fork 4
/
cavoc~.c
472 lines (414 loc) · 13.2 KB
/
cavoc~.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* FFTease for Pd */
#include "fftease.h"
static t_class *cavoc_class;
#define OBJECT_NAME "cavoc~"
typedef struct _cavoc
{
t_object x_obj;
t_float x_f;
t_fftease *fft;
t_float frame_duration;
int max_bin;
t_float fundamental;
short left;
short right;
short center;
short *rule;
t_float start_breakpoint;
int hold_frames;
int frames_left;
int set_count;
void *list_outlet;
t_atom *list_data;
short mute;
short external_trigger;
short trigger_value; // set to 1 when a bang is received
t_float topfreq; // highest to synthesize - Nyquist by default
t_float bottomfreq;
t_float *freqs;
t_float *amps;
t_float *cavoc;
t_float density; // treat as attribute
t_float hold_time; // treat as attribute
} t_cavoc;
static void *cavoc_new(t_symbol *msg, short argc, t_atom *argv);
static void cavoc_dsp(t_cavoc *x, t_signal **sp);
static t_int *cavoc_perform(t_int *w);
static void cavoc_free( t_cavoc *x );
static int cavoc_apply_rule( short left, short right, short center, short *rule);
static t_float cavoc_randf(t_float min, t_float max);
static void cavoc_rule (t_cavoc *x, t_symbol *msg, short argc, t_atom *argv);
static void cavoc_retune (t_cavoc *x, t_floatarg min, t_floatarg max);
static void cavoc_mute (t_cavoc *x, t_floatarg toggle);
static void cavoc_external_trigger(t_cavoc *x, t_floatarg toggle);
static void cavoc_init(t_cavoc *x);
static void cavoc_bang(t_cavoc *x);
static void cavoc_topfreq(t_cavoc *x, t_floatarg tf);
static void cavoc_oscbank(t_cavoc *x, t_floatarg flag);
static void cavoc_density(t_cavoc *x, t_floatarg f);
static void cavoc_hold_time(t_cavoc *x, t_floatarg f);
static void build_spectrum(t_cavoc *x, t_float min, t_float max);
static void cavoc_bottomfreq(t_cavoc *x, t_floatarg bf);
static void cavoc_fftinfo( t_cavoc *x );
void cavoc_tilde_setup(void)
{
t_class *c;
c = class_new(gensym("cavoc~"), (t_newmethod)cavoc_new,
(t_method)cavoc_free,sizeof(t_cavoc), 0,A_GIMME,0);
CLASS_MAINSIGNALIN(c, t_cavoc, x_f);
class_addmethod(c,(t_method)cavoc_dsp,gensym("dsp"), A_CANT, 0);
class_addmethod(c,(t_method)cavoc_mute,gensym("mute"),A_FLOAT,0);
class_addmethod(c,(t_method)cavoc_oscbank,gensym("oscbank"),A_FLOAT,0);
class_addmethod(c,(t_method)cavoc_rule,gensym("rule"),A_GIMME,0);
class_addmethod(c,(t_method)cavoc_external_trigger,gensym("external_trigger"),A_FLOAT,0);
class_addmethod(c,(t_method)cavoc_bang,gensym("bang"),0);
class_addmethod(c,(t_method)cavoc_retune,gensym("retune"),A_FLOAT,A_FLOAT,0);
class_addmethod(c,(t_method)cavoc_topfreq,gensym("topfreq"),A_FLOAT,0);
class_addmethod(c,(t_method)cavoc_bottomfreq,gensym("bottomfreq"),A_FLOAT,0);
class_addmethod(c,(t_method)cavoc_density,gensym("density"),A_FLOAT,0);
class_addmethod(c,(t_method)cavoc_hold_time,gensym("hold_time"),A_FLOAT,0);
cavoc_class = c;
fftease_announce(OBJECT_NAME);
}
void cavoc_fftinfo( t_cavoc *x )
{
t_fftease *fft = x->fft;
fftease_fftinfo( fft, OBJECT_NAME );
}
void cavoc_density(t_cavoc *x, t_floatarg density)
{
int i;
t_fftease *fft = x->fft;
if( density < 0.0001 ){
density = .0001;
} else if( density > .9999 ){
density = 1.0;
}
x->density = density;
x->start_breakpoint = 1.0 - x->density;
for( i = 0; i < fft->N2 + 1; i++ ){
if( cavoc_randf(0.0, 1.0) > x->start_breakpoint ){
x->amps[ i ] = 1;
++(x->set_count);
} else {
x->amps[ i ] = 0;
}
}
}
void cavoc_hold_time(t_cavoc *x, t_floatarg f)
{
if(f <= 0)
return;
x->hold_time = f;
x->hold_frames = (int) ((x->hold_time/1000.0) / x->frame_duration);
if( x->hold_frames < 1 )
x->hold_frames = 1;
x->frames_left = x->hold_frames;
}
void cavoc_external_trigger(t_cavoc *x, t_floatarg toggle)
{
x->external_trigger = (short)toggle;
}
void cavoc_mute (t_cavoc *x, t_floatarg toggle)
{
x->mute = (short)toggle;
}
void cavoc_retune(t_cavoc *x, t_floatarg min, t_floatarg max)
{
if( max <= 0 || min <= 0 || min > max ){
pd_error(0, "bad values for min and max multipliers");
return;
}
if( min < .1 )
min = 0.1;
if( max > 2.0 )
max = 2.0;
build_spectrum(x, (t_float)min, (t_float)max);
}
void cavoc_bang(t_cavoc *x)
{
if(x->external_trigger)
x->trigger_value = 1;
}
void cavoc_oscbank(t_cavoc *x, t_floatarg flag)
{
x->fft->obank_flag = (short) flag;
}
void cavoc_rule (t_cavoc *x, t_symbol *msg, short argc, t_atom *argv)
{
int i;
short *rule = x->rule;
if( argc != 8 ){
pd_error(0, "the rule must be size 8");
return;
}
for( i = 0; i < 8; i++ ){
rule[i] = (short) atom_getfloatarg(i, argc, argv);
}
}
void cavoc_free( t_cavoc *x ){
fftease_free(x->fft);
free(x->fft);
free(x->amps);
free(x->freqs);
free(x->rule);
}
void *cavoc_new(t_symbol *msg, short argc, t_atom *argv)
{
t_cavoc *x = (t_cavoc *)pd_new(cavoc_class);
t_fftease *fft;
outlet_new(&x->x_obj, gensym("signal"));
x->fft = (t_fftease *) calloc(1,sizeof(t_fftease));
fft = x->fft;
x->fft->initialized = 0;
x->density = 0.1;
x->hold_time = 500.0; // convert from ms
x->fft->obank_flag = 0;
x->fft->N = FFTEASE_DEFAULT_FFTSIZE;
x->fft->overlap = FFTEASE_DEFAULT_OVERLAP;
x->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); }
if(argc > 2){ x->density = atom_getfloatarg(2, argc, argv); }
if(argc > 3){ x->hold_time = atom_getfloatarg(3, argc, argv); }
return x;
}
void cavoc_init(t_cavoc *x)
{
t_fftease *fft = x->fft;
short initialized = fft->initialized;
fftease_init(fft);
fft->lo_bin = 0;
fft->hi_bin = fft->N2 - 1;
if(! fft->R ){
pd_error(0, "zero sampling rate!");
return;
}
x->frame_duration = (t_float)fft->D/(t_float) fft->R;
if(x->hold_time <= 0.0)
x->hold_time = 150;
x->hold_frames = (int) ((x->hold_time * 0.001) / x->frame_duration) ;
x->frames_left = x->hold_frames;
x->trigger_value = 0;
x->topfreq = fft->R / 2.0;
x->bottomfreq = 0.0;
if(!initialized){
srand(time(0));
x->mute = 0;
x->external_trigger = 0;
if( x->density < 0.0 ){
x->density = 0;
} else if( x->density > 1.0 ){
x->density = 1.0;
}
x->start_breakpoint = 1.0 - x->density;
x->freqs = (t_float *) calloc((fft->N2 + 1), sizeof(t_float));
x->amps = (t_float *) calloc((fft->N2 + 1), sizeof(t_float));
x->cavoc = (t_float *) calloc((fft->N + 2), sizeof(t_float));
x->rule = (short *) calloc(8, sizeof(short));
x->rule[2] = x->rule[3] = x->rule[5] = x->rule[6] = 1;
x->rule[0] = x->rule[1] = x->rule[4] = x->rule[7] = 0;
} else {
x->freqs = (t_float *) realloc(x->freqs, (fft->N2 + 1) * sizeof(t_float));
x->amps = (t_float *) realloc(x->amps, (fft->N2 + 1) * sizeof(t_float));
}
build_spectrum(x, 0.9, 1.1);
}
void build_spectrum(t_cavoc *x, t_float min, t_float max)
{
t_fftease *fft = x->fft;
t_float basefreq;
int i;
x->set_count = 0;
for(i = 0; i < fft->N2 + 1; i++){
if(cavoc_randf(0.0, 1.0) > x->start_breakpoint){
x->amps[i] = 1;
++(x->set_count);
} else {
x->amps[i] = 0;
}
basefreq = x->bottomfreq + (( (x->topfreq - x->bottomfreq) / (t_float) fft->N2 ) * (t_float) i );
x->freqs[i] = basefreq * cavoc_randf(min,max);
}
for( i = 0; i < fft->N2 + 1; i++ ){
fft->channel[i * 2] = x->amps[i];
fft->channel[i * 2 + 1] = x->freqs[i];
}
}
void cavoc_topfreq(t_cavoc *x, t_floatarg tf)
{
t_fftease *fft = x->fft;
if(tf < 100 || tf > fft->R / 2.0){
pd_error(0, "%s: top frequency out of range: %f",OBJECT_NAME, tf);
return;
}
x->topfreq = (t_float) tf;
build_spectrum(x, 0.9, 1.1);
}
void cavoc_bottomfreq(t_cavoc *x, t_floatarg bf)
{
if(bf < 0 && bf > x->topfreq){
pd_error(0, "%s: bottom frequency out of range: %f",OBJECT_NAME, bf);
return;
}
x->bottomfreq = (t_float) bf;
build_spectrum(x, 0.9, 1.1);
}
static void do_cavoc(t_cavoc *x)
{
int i;
t_fftease *fft = x->fft;
int N = fft->N;
int N2 = fft->N2;
t_float *channel = fft->channel;
int hold_frames = x->hold_frames;
short *rule = x->rule;
short left = x->left;
short right = x->right;
short center = x->center;
short external_trigger = x->external_trigger;
short new_event = 0;
t_float *amps = x->amps;
t_float *freqs = x->freqs;
if(external_trigger){// only accurate to within a vector because of FFT
if(x->trigger_value){
x->trigger_value = 0;
new_event = 1;
}
} else if(--(x->frames_left) <= 0){
x->frames_left = hold_frames;
new_event = 1;
}
if(new_event){
for( i = 1; i < N2; i++ ){
left = amps[i - 1];
center = amps[i] ;
right = amps[i + 1];
channel[i * 2] = cavoc_apply_rule(left, right, center, rule);
}
center = amps[0];
right = amps[1];
left = amps[N2];
channel[0] = cavoc_apply_rule(left, right, center, rule);
center = amps[N2];
right = amps[0];
left = amps[N2 - 1];
channel[N] = cavoc_apply_rule(left, right, center, rule);
for(i = 0; i < N2 + 1; i++){
channel[(i*2) + 1] = freqs[i];
amps[i] = channel[i * 2];
}
}
if(fft->obank_flag){
for(i = 0; i < N2 + 1; i++){
channel[(i*2) + 1] = freqs[i];
channel[i * 2] = amps[i];
}
fftease_oscbank(fft);
} else {
fftease_unconvert(fft);
fftease_rdft(fft, -1);
fftease_overlapadd(fft);
}
}
t_int *cavoc_perform(t_int *w)
{
int i,j;
t_cavoc *x = (t_cavoc *) (w[1]);
t_float *MSPOutputVector = (t_float *)(w[2]);
t_fftease *fft = x->fft;
int D = fft->D;
int Nw = fft->Nw;
t_float *output = fft->output;
t_float mult = fft->mult ;
int operationRepeat = fft->operationRepeat;
int operationCount = fft->operationCount;
t_float *internalOutputVector = fft->internalOutputVector;
int MSPVectorSize = fft->MSPVectorSize;
if(fft->obank_flag){
mult *= FFTEASE_OSCBANK_SCALAR;
}
if( x->mute){
for(i=0; i < MSPVectorSize; i++){ MSPOutputVector[i] = 0.0; }
return w+3;
}
if( fft->bufferStatus == EQUAL_TO_MSP_VECTOR ){
do_cavoc(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++ ){
do_cavoc(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(MSPOutputVector, internalOutputVector + (operationCount * MSPVectorSize), MSPVectorSize * sizeof(t_float));
operationCount = (operationCount + 1) % operationRepeat;
if( operationCount == 0 ) {
do_cavoc(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+3;
}
int cavoc_apply_rule(short left, short right, short center, short *rule){
if( ! center ){
if( ! left && ! right){
return rule[0];
} else if ( ! left && right ){
return rule[1];
} else if ( left && ! right ) {
return rule[2];
} else if (left && right) {
return rule[3];
}
} else {
if( ! left && ! right){
return rule[4];
} else if ( ! left && right ){
return rule[5];
} else if ( left && ! right ) {
return rule[6];
} else if (left && right) {
return rule[7];
}
}
return 0;
}
t_float cavoc_randf(t_float min, t_float max)
{
t_float randv;
randv = (t_float) (rand() % 32768) / 32768.0 ;
return (min + ((max-min) * randv)) ;
}
void cavoc_dsp(t_cavoc *x, t_signal **sp)
{
int reset_required = 0;
int maxvectorsize = sp[0]->s_n;
int samplerate = sp[0]->s_sr;
t_fftease *fft = x->fft;
if(fft->R != samplerate || fft->MSPVectorSize != maxvectorsize || fft->initialized == 0){
reset_required = 1;
}
if(!samplerate)
return;
if(fft->MSPVectorSize != maxvectorsize){
fft->MSPVectorSize = maxvectorsize;
fftease_set_fft_buffers(fft);
}
if(fft->R != samplerate ){
fft->R = samplerate;
}
if(reset_required){
cavoc_init(x);
}
dsp_add(cavoc_perform, 2, x, sp[1]->s_vec);
}