Skip to content

Commit

Permalink
dust~ mc
Browse files Browse the repository at this point in the history
  • Loading branch information
porres committed Aug 3, 2023
1 parent c7c0469 commit b082104
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 75 deletions.
36 changes: 28 additions & 8 deletions Code_source/Compiled/signal/dust~.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ typedef struct _dust{
t_float x_density;
t_float x_lastout;
int x_id;
int x_nchans;
}t_dust;

static void dust_seed(t_dust *x, t_symbol *s, int ac, t_atom *av){
random_init(&x->x_rstate, get_seed(s, ac, av, x->x_id));
}

static void dust_ch(t_dust *x, t_floatarg f){
x->x_nchans = f < 1 ? 1 : (int)f;
canvas_update_dsp();
}

static t_int *dust_perform(t_int *w){
t_dust *x = (t_dust *)(w[1]);
int n = (t_int)(w[2]);
Expand All @@ -29,34 +35,47 @@ static t_int *dust_perform(t_int *w){
uint32_t *s3 = &x->x_rstate.s3;
while(n--){
t_float density = *in1++;
t_float thresh = density * x->x_sample_dur;
t_float thresh = density * x->x_sample_dur / x->x_nchans;
t_float scale = thresh > 0 ? 1./thresh : 0;
t_float random = (t_float)(random_frand(s1, s2, s3) * 0.5 + 0.5);
t_float output = random < thresh ? random * scale : 0;
if(output != 0 && lastout != 0)
output = 0;
*out++ = lastout = output;
for(int i = 0; i < x->x_nchans; i++){
t_float random = (t_float)(random_frand(s1, s2, s3) * 0.5 + 0.5);
t_float output = random < thresh ? random * scale : 0;
if(output != 0 && lastout != 0)
output = 0;
*out++ = lastout = output;
}
}
x->x_lastout = lastout;
return(w+5);
}

static void dust_dsp(t_dust *x, t_signal **sp){
x->x_sample_dur = 1./sp[0]->s_sr;
signal_setmultiout(&sp[1], x->x_nchans);
dsp_add(dust_perform, 4, x, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);
}

static void *dust_new(t_symbol *s, int ac, t_atom *av){
t_dust *x = (t_dust *)pd_new(dust_class);
x->x_id = random_get_id();
x->x_nchans = 1;
dust_seed(x, s, 0, NULL);
if(av->a_type == A_SYMBOL){
while(av->a_type == A_SYMBOL){
if(ac >= 2 && atom_getsymbol(av) == gensym("-seed")){
t_atom at[1];
SETFLOAT(at, atom_getfloat(av+1));
ac-=2, av+=2;
dust_seed(x, s, 1, at);
}
else if(ac >= 2 && atom_getsymbol(av) == gensym("-ch")){
int n = atom_getint(av+1);
x->x_nchans = n < 1 ? 1 : n;
ac-=2, av+=2;
}
else{
pd_error(x, "[dust~]: improper args");
return(NULL);
}
}
x->x_lastout = 0;
x->x_density = ac ? atom_getfloat(av) : 0;
Expand All @@ -66,8 +85,9 @@ static void *dust_new(t_symbol *s, int ac, t_atom *av){

void dust_tilde_setup(void){
dust_class = class_new(gensym("dust~"), (t_newmethod)dust_new,
0, sizeof(t_dust), CLASS_DEFAULT, A_GIMME, 0);
0, sizeof(t_dust), CLASS_MULTICHANNEL, A_GIMME, 0);
CLASS_MAINSIGNALIN(dust_class, t_dust, x_density);
class_addmethod(dust_class, (t_method)dust_dsp, gensym("dsp"), A_CANT, 0);
class_addmethod(dust_class, (t_method)dust_seed, gensym("seed"), A_GIMME, 0);
class_addmethod(dust_class, (t_method)dust_ch, gensym("c"), A_DEFFLOAT, 0);
}
120 changes: 53 additions & 67 deletions Documentation/Help-files/dust~-help.pd
Original file line number Diff line number Diff line change
@@ -1,73 +1,43 @@
#N canvas 439 23 561 501 10;
#X obj 238 173 hsl 128 15 1 5000 1 0 empty empty empty -2 -8 0 10 #dcdcdc
#000000 #000000 0 1;
#X obj 235 255 else/out~;
#X obj 3 4 cnv 15 301 42 empty empty dust~ 20 20 2 37 #e0e0e0 #000000
0;
#X obj 307 5 cnv 15 250 40 empty empty empty 12 13 0 18 #7c7c7c #e0e4dc
0;
#N canvas 439 23 561 540 10;
#X obj 218 173 hsl 128 15 1 5000 1 0 empty empty empty -2 -8 0 10 #dcdcdc #000000 #000000 0 1;
#X obj 215 255 else/out~;
#X obj 3 4 cnv 15 301 42 empty empty dust~ 20 20 2 37 #e0e0e0 #000000 0;
#X obj 307 5 cnv 15 250 40 empty empty empty 12 13 0 18 #7c7c7c #e0e4dc 0;
#N canvas 0 22 450 278 (subpatch) 0;
#X coords 0 1 100 -1 302 42 1 0 0;
#X restore 3 4 graph;
#N canvas 382 141 749 319 (subpatch) 0;
#X coords 0 -1 1 1 252 42 2 100 100;
#X restore 306 4 pd;
#X obj 346 12 cnv 10 10 10 empty empty ELSE 0 15 2 30 #7c7c7c #e0e4dc
0;
#X obj 24 41 cnv 4 4 4 empty empty Random 0 28 2 18 #e0e0e0 #000000
0;
#X obj 459 12 cnv 10 10 10 empty empty EL 0 6 2 13 #7c7c7c #e0e4dc
0;
#X obj 479 12 cnv 10 10 10 empty empty Locus 0 6 2 13 #7c7c7c #e0e4dc
0;
#X obj 465 27 cnv 10 10 10 empty empty ELSE 0 6 2 13 #7c7c7c #e0e4dc
0;
#X obj 93 41 cnv 4 4 4 empty empty impulses 0 28 2 18 #e0e0e0 #000000
0;
#X obj 4 467 cnv 15 552 21 empty empty empty 20 12 0 14 #e0e0e0 #202020
0;
#X obj 4 316 cnv 3 550 3 empty empty inlets 8 12 0 13 #dcdcdc #000000
0;
#X obj 4 365 cnv 3 550 3 empty empty outlets 8 12 0 13 #dcdcdc #000000
0;
#X obj 4 434 cnv 3 550 3 empty empty arguments 8 12 0 13 #dcdcdc #000000
0;
#X obj 107 374 cnv 17 3 17 empty empty 0 5 9 0 16 #dcdcdc #9c9c9c 0
;
#X obj 106 325 cnv 17 3 33 empty empty 0 5 9 0 16 #dcdcdc #9c9c9c 0
;
#X text 169 374 signal;
#X text 177 443 1) float;
#X obj 235 202 nbx 8 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10
#dcdcdc #000000 #000000 0 256;
#X text 319 203 density;
#X text 218 325 - density (rate) of random impulses;
#X text 238 442 - density (default 0);
#X obj 235 228 else/dust~ 1;
#X text 133 326 float/signal;
#X text 133 344 seed <float>;
#X obj 346 12 cnv 10 10 10 empty empty ELSE 0 15 2 30 #7c7c7c #e0e4dc 0;
#X obj 24 41 cnv 4 4 4 empty empty Random 0 28 2 18 #e0e0e0 #000000 0;
#X obj 459 12 cnv 10 10 10 empty empty EL 0 6 2 13 #7c7c7c #e0e4dc 0;
#X obj 479 12 cnv 10 10 10 empty empty Locus 0 6 2 13 #7c7c7c #e0e4dc 0;
#X obj 465 27 cnv 10 10 10 empty empty ELSE 0 6 2 13 #7c7c7c #e0e4dc 0;
#X obj 93 41 cnv 4 4 4 empty empty impulses 0 28 2 18 #e0e0e0 #000000 0;
#X obj 4 510 cnv 15 552 21 empty empty empty 20 12 0 14 #e0e0e0 #202020 0;
#X obj 4 321 cnv 3 550 3 empty empty inlets 8 12 0 13 #dcdcdc #000000 0;
#X obj 4 390 cnv 3 550 3 empty empty outlets 8 12 0 13 #dcdcdc #000000 0;
#X obj 4 477 cnv 3 550 3 empty empty arguments 8 12 0 13 #dcdcdc #000000 0;
#X obj 107 399 cnv 17 3 17 empty empty 0 5 9 0 16 #dcdcdc #9c9c9c 0;
#X obj 106 330 cnv 17 3 50 empty empty 0 5 9 0 16 #dcdcdc #9c9c9c 0;
#X text 169 399 signal;
#X text 177 486 1) float;
#X obj 215 202 nbx 8 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10 #dcdcdc #000000 #000000 0 256;
#X text 299 203 density;
#X text 218 330 - density (rate) of random impulses;
#X text 238 485 - density (default 0);
#X obj 215 228 else/dust~ 1;
#X text 133 331 float/signal;
#X text 133 347 seed <float>;
#X text 40 247 see also:;
#X obj 37 269 else/dust2~;
#X text 103 90 [dust~] is based on SuperCollider's "Dust" UGEN and
outputs random impulse values (only positive values up to 1) at random
times according to a density parameter. The difference to SuperCollider's
is that it only produces actual impulses (one non zero value in between
0 valued samples)., f 55;
#X obj 6 401 cnv 3 550 3 empty empty flags 8 12 0 13 #dcdcdc #000000
0;
#X text 135 410 -seed <float>: seed value (default: unique internal)
;
#X text 217 344 - a float sets seed \, no float sets a unique internal
;
#X obj 6 426 cnv 3 550 3 empty empty flags 8 12 0 13 #dcdcdc #000000 0;
#X text 135 435 -seed <float>: seed value (default: unique internal);
#X text 217 347 - a float sets seed \, no float sets a unique internal;
#N canvas 777 84 466 384 seed 0;
#X text 64 15 Pseudo random number generators aren't true random number
generators. Instead \, an algorithm is used to provide a sequence of
numbers that seems random. The same sequence can be reproduced if you
set a "seed" value \, which can be any integer number., f 57;
#X text 64 129 You can set a seed with the '-seed' flag. If you don't
supply it \, each object gets its own seed internal seed. If you send
a 'seed' message without float \, the object also gets a unique seed
value., f 57;
#X text 64 15 Pseudo random number generators aren't true random number generators. Instead \, an algorithm is used to provide a sequence of numbers that seems random. The same sequence can be reproduced if you set a "seed" value \, which can be any integer number., f 57;
#X text 64 129 You can set a seed with the '-seed' flag. If you don't supply it \, each object gets its own seed internal seed. If you send a 'seed' message without float \, the object also gets a unique seed value., f 57;
#X msg 377 276 seed;
#X msg 367 247 seed 123;
#X obj 47 294 else/trighold~;
Expand All @@ -79,10 +49,7 @@ value., f 57;
#X obj 197 230 r \$0-dust;
#X obj 47 260 else/dust~ -seed 123 1;
#X obj 197 260 else/dust~ -seed 123 1;
#X text 64 78 Seeds are kept locally \, which means that if two [dust~]
objects are seeded the same they will have the same output. Conversely
\, you can seed the same [dust~] object twice with the same seed to
repeat the output., f 57;
#X text 64 78 Seeds are kept locally \, which means that if two [dust~] objects are seeded the same they will have the same output. Conversely \, you can seed the same [dust~] object twice with the same seed to repeat the output., f 57;
#X connect 2 0 8 0;
#X connect 3 0 8 0;
#X connect 4 0 6 0;
Expand All @@ -91,8 +58,27 @@ repeat the output., f 57;
#X connect 10 0 12 0;
#X connect 11 0 4 0;
#X connect 12 0 5 0;
#X restore 466 273 pd seed;
#X text 218 375 - random positive impulses;
#X restore 481 254 pd seed;
#X text 218 400 - random positive impulses;
#N canvas 777 84 346 340 multichannel 0;
#X obj 109 77 hsl 128 15 1 5000 1 0 empty empty empty -2 -8 0 10 #dcdcdc #000000 #000000 0 1;
#X obj 106 106 nbx 8 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10 #dcdcdc #000000 #000000 0 256;
#X text 190 107 density;
#X obj 106 231 else/out.mc~;
#X msg 170 161 ch 2;
#X msg 125 160 ch 1;
#X obj 106 197 else/dust~ -ch 2 50;
#X text 68 41 An example with multichannel support.;
#X connect 0 0 1 0;
#X connect 1 0 6 0;
#X connect 4 0 6 0;
#X connect 5 0 6 0;
#X connect 6 0 3 0;
#X restore 433 280 pd multichannel;
#X text 145 364 mc <float>;
#X text 217 364 - set number of output channels;
#X text 53 95 [dust~] is based on SuperCollider's "Dust" UGEN and outputs random impulse values (only positive values up to 1) at random times according to a density parameter. The difference to SuperCollider's is that it only produces actual impulses (one non zero value in between 0 valued samples). It has support for multichannel output., f 71;
#X text 147 451 -ch <float>: number of output channels (default 1);
#X connect 0 0 20 0;
#X connect 20 0 24 0;
#X connect 24 0 1 0;

0 comments on commit b082104

Please sign in to comment.