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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
#include "m_pd.h"
#include "ext13.h"
#include <sys/types.h>
#include <string.h>
#ifndef _WIN32
#include <netinet/in.h>
#include <netdb.h>
#include <sys/errno.h>
#include <sys/socket.h>
#else
#include <stdlib.h>
#endif
/* ------------------------ scramble_tilde~ ----------------------------- */
static t_class *scramble_tilde_class;
typedef struct _scramble_grain
{
t_float* L;
t_float* R;
int size;
t_float maximum;
struct _scramble_grain *next;
}t_scramble_grain;
typedef struct _scramble_tilde
{
t_object x_obj;
int x_n;
t_int x_channels;
t_float play, analize;
t_float dir, current_dir, pitch, actual_pitch, grain_r, autopitch;
t_float valsum, valavg;
int valsumcount, valsummax;
int autofollow, playmode, semitones;
t_scramble_grain *firstgrain;
t_scramble_grain *workgrain;
t_scramble_grain *ring;
int grains, newgrains, w_grain, r_grain, n_grain, gotagrain, flush;
int r_d, w_d, dist, mindist, lowptr, hiptr, lastlowptr;
t_float lowval, hival, prevval, lowborder, normalize;
int nsamples;
t_outlet *trigger1;
t_outlet *trigger2;
} t_scramble_tilde;
/* grain functions*/
static t_scramble_grain* scramble_tilde_newgrain(){
t_scramble_grain* thegrain;
thegrain = getbytes( sizeof(t_scramble_grain));
thegrain->L = NULL;
thegrain->R = NULL;
thegrain->size = 0;
thegrain->next = NULL;
return (thegrain);
}
static t_scramble_grain* scramble_tilde_getgrain(t_scramble_grain* firstgrain, int n){
t_scramble_grain* thegrain = firstgrain;
while (n--){
if (thegrain->next){
thegrain = thegrain->next;
}else
return (NULL);
}
return (thegrain);
}
static int scramble_tilde_getsomegrain(t_scramble_grain* firstgrain,int g){
t_scramble_grain* thegrain ;
int r;
do{
r = rand() % g;
thegrain = scramble_tilde_getgrain(firstgrain, r);
}while (thegrain->size == 0);
return (r);
}
static void scramble_tilde_grainbuf(t_scramble_grain* grain, int c, int n){
if (!grain->size)
grain->L = getbytes(n * sizeof(t_float));
else
grain->L = resizebytes(grain->L, grain->size * sizeof(t_float), n * sizeof(t_float));
if (c == 2){
if (!grain->size)
grain->R = getbytes(n * sizeof(t_float));
else
grain->R = resizebytes(grain->R, grain->size * sizeof(t_float), n * sizeof(t_float));
}
grain->size = n;
}
static void scramble_tilde_freegrain(t_scramble_grain* grain, int c){
if (grain->size){
freebytes(grain->L, grain->size * sizeof(t_float));
if (c == 2) freebytes(grain->R, grain->size * sizeof(t_float));
grain->size = 0;
grain->next = NULL;
}
}
t_int *scramble_tilde_perform(t_int *w)
{
t_scramble_tilde* x = (t_scramble_tilde*)(w[1]);
int i;
int erg=0;
int n;
t_float val, valL, valR, killval;
#ifndef _WIN32
t_float* out[x->x_channels];
t_float* in[x->x_channels];
#else
t_float** out = (t_float**)malloc(x->x_channels*sizeof(t_float*));
t_float** in = (t_float**)malloc(x->x_channels*sizeof(t_float*));
#endif
float n_factor, frac, a, b, c, d, cminusb;
int index;
float *fp;
t_atom at[2];
/* anything changed?*/
if (x->flush){
int i = x->grains;
x->flush = 0;
x->gotagrain = 0;
while (i--)
scramble_tilde_grainbuf(scramble_tilde_getgrain(x->firstgrain,i),x->x_channels,0);
}
if (x->newgrains){
int tmp = x->grains;
if (x->newgrains > x->grains){
x->workgrain = scramble_tilde_getgrain(x->firstgrain,x->grains - 1); /*-1 ???*/
tmp = x->newgrains;
x->newgrains -= x->grains;
x->grains = tmp;
while (x->newgrains--){
x->workgrain->next = scramble_tilde_newgrain();
x->workgrain = x->workgrain->next;
}
// post ("now %d grains",x->grains);
}else{
if (x->newgrains < x->grains){
t_scramble_grain* tmpgrain;
x->grains = x->newgrains;
x->workgrain = scramble_tilde_getgrain(x->firstgrain,x->grains - 1);
/* delete old grains */
while (x->workgrain->next){
tmpgrain = x->workgrain->next;
scramble_tilde_freegrain(x->workgrain,x->x_channels);
x->workgrain = tmpgrain;
}
/* reset readpointer if needed*/
if (x->r_grain >= x->grains){
x->r_grain = 0;
x->grain_r = -1;
}
// post ("now %d grains",x->grains);
}
}
x->newgrains=0;
}
if ((x->ring->size > x->x_n) || (x->ring->size < x->x_n) ){
// post ("scramble~: new size for ringbuffer:%d samples, %d channels, oldsize:%d",x->x_n,x->x_channels,x->ring->size);
scramble_tilde_grainbuf(x->ring, x->x_channels ,x->x_n);
x->x_n = x->ring->size;
x->dist = 0;
x->lowptr = x->r_d;
x->lastlowptr = -1;
x->lowval = x->lowborder;
}
for (i = 0; i < x->x_channels ;i++)
in[i] = (t_float *)(w[2 + i]);
for (i = 0;i < x->x_channels ;i++)
out[i] = (t_float *)(w[2 + x->x_channels + i]);
n = (int)(w[2 + x->x_channels * 2]);/*number of samples*/
// post ("n:%d",n);
while (n--){
/*read from input*/
if (++x->r_d > x->x_n){
x->r_d = 0;
}
valL = *(t_float*)(x->ring->L + x->r_d) = *in[0]++;
if (valL < 0) valL *= -1;
if (x->x_channels == 2){
valR = *(t_float*)(x->ring->R + x->r_d) = *in[1]++;
if (valR < 0) valR *= -1;
val = valL + valR / 2.0;
if (valL > x->hival){
x->hiptr = x->r_d;
x->hival = valL;
}
if (valR > x->hival){
x->hiptr = x->r_d;
x->hival = valR;
}
}else {
val = valL;
if (valL > x->hival){
x->hiptr = x->r_d;
x->hival = valL;
}
}
// if (val < 0) val *= -1;
x->valsum += val;
// if (x->valsumcount++ > x->mindist * 10){
if (x->valsumcount++ && (x->r_d == 0)){
x->valavg = x->valsum / x->valsumcount;
x->valsumcount = 0;
x->valsum = 0;
if (x->autofollow && ( x->valavg > 0.003)) {
x->lowborder = x->valavg;
// post ("lowborder = %f",x->lowborder);
}
}
if ((val < x->lowborder) && (x->prevval > x->lowborder)){
/* a new low-period */
x->dist = -1;
x->lowptr = x->r_d;
x->lowval = val;
// post ("low");
}
if ((x->r_d + 1) == x->lastlowptr){
/* one round without a point to cut */
x->lastlowptr = -1;
x->lowval = x->lowborder;
x->hival = 0;
// post ("lastlowptr: reset");
}
if (val < x->lowborder){x->dist++;}
if (val <= x->lowval) {
x->lowptr = x->r_d;
x->lowval = val;
/*found a point to cut*/
}
if ((val > x->lowborder) && (x->prevval < x->lowborder) && ( x->dist < x->mindist)){
/*too short low-period*/
x->dist = 0;
x->lowptr = x->r_d;
x->lowval = x->lowborder;
// post ("low too short");
}
if ((val > x->lowborder) && (x->prevval < x->lowborder) && ( x->dist > x->mindist)){
/*found a low-period to cut */
if ((x->lastlowptr != -1) ){
int grainlen = 0;
int i = 0;
int wp = 1; /*first and last sample of grain should be 0.0*/
x->gotagrain = 1;
/* how long is the new grain */
if (x->lastlowptr > x->lowptr){
grainlen = x->x_n - x->lastlowptr + x->lowptr;
}else{
grainlen = x->lowptr - x->lastlowptr;
}
if (x->analize){
/*find and prepare the grain*/
if (++x->w_grain >= x->grains ) x->w_grain = 0;
x->workgrain = scramble_tilde_getgrain (x->firstgrain, x->w_grain);
scramble_tilde_grainbuf(x->workgrain, x->x_channels, grainlen + 2);
*(t_float*)(x->workgrain->L) = 0.0;
*(t_float*)(x->workgrain->L + x->workgrain->size -1) = 0.0;
if (x->x_channels == 2){
*(t_float*)(x->workgrain->R) = 0.0;
*(t_float*)(x->workgrain->R + x->workgrain->size -1) = 0.0;
}
x->workgrain->maximum = x->hival;
/*notify the world*/
SETFLOAT(at, grainlen);
SETFLOAT(at+1, x->w_grain + 1);
outlet_list(x->trigger1, 0, 2, at);
/*copy to the grain*/
i = x->lastlowptr;
while (grainlen--){
if (++i >= x->x_n) i = 0;
*(t_float*)(x->workgrain->L + wp ) = *(t_float*)(x->ring->L + i);
if (x->x_channels == 2)
*(t_float*)(x->workgrain->R + wp ) = *(t_float*)(x->ring->R + i);
wp++;
}
}/*end if analize*/
// post ("copied: w_grain: %d",x->w_grain);
}/* end lastlowptr != -1*/
x->dist = 0;
x->hival = 0;
x->lastlowptr = x->lowptr;
}/*end found a low-period to cut */
x->prevval = val;
}/*end while n-- (read from input)*/
/*--------------------playback--------------*/
n = (int)(w[2 + x->x_channels * 2]);/*number of samples*/
x->workgrain = scramble_tilde_getgrain (x->firstgrain, x->r_grain);
if (x->normalize && x->workgrain) n_factor = x->normalize / x->workgrain->maximum;
else n_factor = 1;
while (n--){
int wgs;
if (x->workgrain) wgs = x->workgrain->size - 2;
else wgs = 0;
if (( (x->grain_r >= wgs) || (x->grain_r < 1) || (x->workgrain == NULL) ) && x->play && x->gotagrain){
if (x->playmode < 2){
x->r_grain = scramble_tilde_getsomegrain(x->firstgrain, x->grains);
x->workgrain = scramble_tilde_getgrain (x->firstgrain, x->r_grain);
}else{
if (x->n_grain == -1){
x->play = 0;
x->r_grain = 0;
x->workgrain = NULL;
}else{
x->r_grain = x->n_grain;
x->workgrain = scramble_tilde_getgrain (x->firstgrain, x->r_grain);
x->n_grain = -1;
if ((x->r_grain == x->w_grain) || (x->workgrain == NULL)){
x->play = 0;
x->r_grain = 0;
x->workgrain = NULL;
} else if (!x->workgrain->size){
x->play = 0;
x->r_grain = 0;
x->workgrain = NULL;
}
}
}/*end if playmode < 2*/
if (x->workgrain){
if (((rand() % 200) / 100.0 - 1.0 ) < x->dir){
x->current_dir = 1;
x->grain_r = 1;
}
else{
x->current_dir = -1;
x->grain_r = x->workgrain->size -3;
}
if ( ( (x->autopitch >= 1.) && (x->semitones) ) || ( (x->autopitch) && (! x->semitones) ) ){
if (x->semitones){
int ap = (int)x->autopitch;
int rauf = 0;
int count ;
if (rand() % 2 == 1){ rauf = 1;}
/* post ("rauf:%d",rauf); */
x->actual_pitch = x->pitch;
for (count = (rand() % ap); count >= 0; count--){
/*1.05946 = 12te wurzel aus 2 */
if (rauf){
x->actual_pitch = x->actual_pitch * 1.05946;
}else{
x->actual_pitch = x->actual_pitch / 1.05946;
}
}
}else{
if (((rand() % 200) / 100.0 - 1.0 ) > 0){
x->actual_pitch = x->pitch + x->pitch * ((rand() % 100 ) / 100.0 * x->autopitch);
}else{
x->actual_pitch = x->pitch - x->pitch / ((rand() % 100 ) / 100.0 * x->autopitch);
}
}/*end if semitones*/
} else {
x->actual_pitch = x->pitch;
}/* end if autopitch*/
/* post ("x->actual_pitch:%f, x->autopitch:%f",x->actual_pitch,x->autopitch); */
if (x->normalize) n_factor = x->normalize / x->workgrain->maximum;
else n_factor = 1;
SETFLOAT(at, (x->workgrain->size - 2) / x->actual_pitch);
SETFLOAT(at+1, x->r_grain + 1);
outlet_list(x->trigger2, 0, 2, at);
}/*end if workgrain !=NULL */
}/* end finding a new grain*/
if (x->play && x->gotagrain){
/*write graincontent to output*/
/* 4 point interpolation taken from ../src/d_array.c tabread4~ */
index = x->grain_r;
if (index < 1)
index = 1, frac = 0;
else if (index > x->workgrain->size - 3)
index = x->workgrain->size - 3, frac = 1;
else
frac = x->grain_r - index;
fp = (t_float*)(x->workgrain->L + index);
a = fp[-1];
b = fp[0];
c = fp[1];
d = fp[2];
cminusb = c-b;
*out[0]++ = (b + frac * (
cminusb - 0.5f * (frac-1.) * (
(a - d + 3.0f * cminusb) * frac + (b - a - cminusb)
)
)) * n_factor;
if (x->x_channels == 2){
fp = (t_float*)(x->workgrain->R + index);
a = fp[-1];
b = fp[0];
c = fp[1];
d = fp[2];
cminusb = c-b;
*out[1]++ = (b + frac * (
cminusb - 0.5f * (frac-1.) * (
(a - d + 3.0f * cminusb) * frac + (b - a - cminusb)
)
)) * n_factor;
}
x->grain_r += x->current_dir * x->actual_pitch;
}else/* if play*/{
*out[0]++ = 0;
if (x->x_channels == 2)
*out[1]++ = 0;
}/*end if play */
}/*end while n-- */
#ifdef _WIN32
free(in);
free(out);
#endif
return (w + x->x_channels * 2 + 3);
}
static void scramble_tilde_dsp(t_scramble_tilde *x, t_signal **sp)
{
switch (x->x_channels) {
case 1:
dsp_add(scramble_tilde_perform, 4, x, sp[0]->s_vec,
sp[1]->s_vec, sp[0]->s_n);
// post ("1 channel");
break;
case 2:
dsp_add(scramble_tilde_perform, 6, x, sp[0]->s_vec,
sp[1]->s_vec,sp[2]->s_vec, sp[3]->s_vec, sp[0]->s_n);
// post ("2 channels");
break;
}
}
static void scramble_tilde_free(t_scramble_tilde *x){
int n = x->grains - 1;
while (n--){
scramble_tilde_freegrain (scramble_tilde_getgrain(x->firstgrain,n),x->x_channels);
scramble_tilde_freegrain (x->ring,x->x_channels);
}
}
static void *scramble_tilde_new(t_floatarg c,t_floatarg b)
{
t_scramble_tilde *x = (t_scramble_tilde *)pd_new(scramble_tilde_class);
int i;
// x->bufL = NULL;
// x->bufR = NULL;
x->x_channels = (t_int)c;
if (x->x_channels > 2) {
x->x_channels = 2;
post ("maximum: 2 channels");
}
if (x->x_channels < 1) x->x_channels = 1;
outlet_new(&x->x_obj, gensym("signal"));
if (x->x_channels == 2){
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
outlet_new(&x->x_obj, gensym("signal"));
}
x->trigger1 = outlet_new(&x->x_obj, &s_float);
x->trigger2 = outlet_new(&x->x_obj, &s_float);
x->dir = 1;
x->pitch = 1;
x->actual_pitch = 1;
x->autopitch = 0;
x->semitones = 1;
x->autofollow = 1;
x->playmode = 1;
x->normalize = 0;
x->analize = 1;
x->flush = 0;
x->x_n = (int)b;
if (x->x_n >882000 ){x->x_n = 882000;}
if (x->x_n < 88200 ){x->x_n = 88200;}
/* x->rR = x->rL = x->wR = x->wL = NULL;*/
x->lowptr = 0;
x->lastlowptr = x->r_d = x->grain_r = -1;
x->mindist = 1024;
x->lowborder = 0.35;
// scramble_tilde_tempbuf(x,x->x_n);
x->ring = scramble_tilde_newgrain();
scramble_tilde_grainbuf(x->ring,x->x_channels,x->x_n);
x->valsum = x->valavg = x->valsumcount = 0;
x->valsummax = 1024;
/* the grains:*/
x->grains = 50;
x->r_grain = 0;
x->w_grain = x->n_grain = -1;
x->firstgrain = x->workgrain = scramble_tilde_newgrain();
for (i = 1;i < x->grains;i++){
x->workgrain->next = scramble_tilde_newgrain();
x->workgrain = x->workgrain->next;
}
return (x);
}
void scramble_tilde_float(t_scramble_tilde* x, t_float n){
x->play = n;
if (x->playmode == 2) {
x->n_grain = (int)n - 1;
x->grain_r = -1;
}
}
void scramble_tilde_buffer(t_scramble_tilde* x, t_float n){
if (n > 64) x->x_n = (int)n;
// post ("buffersize now:%d",x->x_n);
}
void scramble_tilde_threshold(t_scramble_tilde* x, t_float t){
if (t >0) {
x->lowborder = t;
x->autofollow = 0;
}else{
post ("threshold must be a positive value (0.1 - 0.8 makes sense)");
}
}
void scramble_tilde_grains(t_scramble_tilde* x, t_float g){
if ((g > 1) && (g < 2048) ) x->newgrains = (int)g;
else post ("scramble~: minimum # of grains must be 2 an maximum # is 2048");
}
void scramble_tilde_mindist(t_scramble_tilde* x, t_float t){
if ((t > 0) && (t < x->x_n)) x->mindist = (int)t;
else post ("scramble~: minimum distance must be positive value lower than buffersize");
}
void scramble_tilde_direction(t_scramble_tilde* x, t_float d){
if (d > 1) d = 1;
if (d < -1) d = -1;
x->dir = d;
}
void scramble_tilde_autofollow(t_scramble_tilde* x){
x->autofollow = 1;
}
void scramble_tilde_pitch(t_scramble_tilde* x, t_float p){
if (p > 0) x->pitch = p;
else post ("scramble~: pitch must be > 0");
}
void scramble_tilde_autopitch(t_scramble_tilde* x, t_float p){
x->autopitch = p;
}
void scramble_tilde_semitones(t_scramble_tilde* x, t_float p){
x->semitones = (int)p;
}
void scramble_tilde_normalize(t_scramble_tilde* x, t_float n){
x->normalize = n;
}
void scramble_tilde_analize(t_scramble_tilde* x, t_float f){
x->analize = f;
}
void scramble_tilde_flush(t_scramble_tilde* x){
x->flush = 1;
}
void scramble_tilde_playmode(t_scramble_tilde* x, t_float p){
x->playmode = (int)p;
if (x->playmode < 0) x->playmode = 0;
if (x->playmode < 1) x->playmode = 2;
switch (x->playmode){
case 0: post ("scramble~: playmode off");
break;
case 1: post ("scramble~: active playmode");
break;
case 2: post ("scramble~: passive playmode");
break;
default: post ("scramble~: invalid playmode");
}
}
void scramble_tilde_setup(void)
{
scramble_tilde_class = class_new(gensym("scramble~"), (t_newmethod) scramble_tilde_new, 0,
sizeof(t_scramble_tilde), 0, A_DEFFLOAT,A_DEFFLOAT, 0);
class_addfloat(scramble_tilde_class,scramble_tilde_float);
class_addmethod(scramble_tilde_class, nullfn, gensym("signal"), 0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_dsp, gensym("dsp"), 0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_buffer, gensym("buffer"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_threshold, gensym("threshold"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_grains, gensym("grains"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_mindist, gensym("min_length"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_direction, gensym("direction"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_autofollow, gensym("autofollow"),0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_pitch, gensym("pitch"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_autopitch, gensym("autopitch"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_semitones, gensym("semitones"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_flush, gensym("flush"), 0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_normalize, gensym("normalize"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_analize, gensym("analize"), A_DEFFLOAT,0);
class_addmethod(scramble_tilde_class, (t_method) scramble_tilde_playmode, gensym("playmode"), A_DEFFLOAT,0);
}
|