Skip to content

Commit

Permalink
dust~
Browse files Browse the repository at this point in the history
  • Loading branch information
porres committed Aug 4, 2023
1 parent 288ae08 commit 4a7f839
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
45 changes: 31 additions & 14 deletions Code_source/Compiled/signal/dust~.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ typedef struct _dust{
t_float x_sample_dur;
t_random_state x_rstate;
t_float x_density;
t_float x_lastout;
t_float *x_lastout;
int x_id;
int x_nchans;
int x_ch;
}t_dust;

static void dust_seed(t_dust *x, t_symbol *s, int ac, t_atom *av){
Expand All @@ -27,39 +28,56 @@ static void dust_ch(t_dust *x, t_floatarg f){
static t_int *dust_perform(t_int *w){
t_dust *x = (t_dust *)(w[1]);
int n = (t_int)(w[2]);
t_float *in1 = (t_float *)(w[3]);
t_float *out = (t_sample *)(w[4]);
t_float lastout = x->x_lastout;
int chs = (t_int)(w[3]);
t_float *in1 = (t_float *)(w[4]);
t_float *out = (t_sample *)(w[5]);
t_float *lastout = x->x_lastout;
uint32_t *s1 = &x->x_rstate.s1;
uint32_t *s2 = &x->x_rstate.s2;
uint32_t *s3 = &x->x_rstate.s3;
while(n--){
t_float density = *in1++;
for(int i = 0; i < n; i++){
for(int j = 0; j < x->x_nchans; j++){
t_float density = chs == 1 ? in1[i] : in1[j*n + i];
t_float thresh = density * x->x_sample_dur / x->x_nchans;
t_float scale = thresh > 0 ? 1./thresh : 0;
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)
if(output != 0 && lastout[j] != 0)
output = 0;
*out++ = lastout = output;
out[j*n + i] = lastout[j] = output;
}
}
x->x_lastout = lastout;
return(w+5);
return(w+6);
}

static void dust_dsp(t_dust *x, t_signal **sp){
x->x_sample_dur = 1./sp[0]->s_sr;
int chs = sp[0]->s_nchans, n = sp[0]->s_n;
if(chs == 1)
chs = x->x_ch;
signal_setmultiout(&sp[1], chs);
if(x->x_nchans != chs){
x->x_lastout = (t_float *)resizebytes(x->x_lastout,
x->x_nchans * sizeof(t_float), chs * sizeof(t_float));
x->x_nchans = chs;
}
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);
dsp_add(dust_perform, 5, x, n, sp[0]->s_nchans, sp[0]->s_vec, sp[1]->s_vec);
}

static void *dust_free(t_dust *x){
freebytes(x->x_lastout, x->x_nchans * sizeof(*x->x_lastout));
return(void *)x;
}

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);
x->x_lastout = (t_float *)getbytes(sizeof(*x->x_lastout));
x->x_ch = 1;
while(av->a_type == A_SYMBOL){
if(ac >= 2 && atom_getsymbol(av) == gensym("-seed")){
t_atom at[1];
Expand All @@ -69,23 +87,22 @@ static void *dust_new(t_symbol *s, int ac, t_atom *av){
}
else if(ac >= 2 && atom_getsymbol(av) == gensym("-ch")){
int n = atom_getint(av+1);
x->x_nchans = n < 1 ? 1 : n;
x->x_ch = 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;
outlet_new(&x->x_obj, &s_signal);
return(x);
}

void dust_tilde_setup(void){
dust_class = class_new(gensym("dust~"), (t_newmethod)dust_new,
0, sizeof(t_dust), CLASS_MULTICHANNEL, A_GIMME, 0);
(t_method)dust_free, 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);
Expand Down
23 changes: 14 additions & 9 deletions Documentation/Help-files/dust~-help.pd
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,25 @@
#X connect 12 0 5 0;
#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.;
#N canvas 668 95 516 377 multichannel 0;
#X obj 78 77 hsl 128 15 1 5000 1 0 empty empty empty -2 -8 0 10 #dcdcdc #000000 #000000 0 1;
#X obj 75 106 nbx 8 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10 #dcdcdc #000000 #000000 0 256;
#X text 159 107 density;
#X obj 75 231 else/out.mc~;
#X msg 139 161 ch 2;
#X msg 94 160 ch 1;
#X obj 75 197 else/dust~ -ch 2 50;
#X text 37 41 An example with multichannel support.;
#X obj 303 208 else/out.mc~;
#X obj 303 170 else/dust~ -ch 2 50;
#X obj 303 125 else/sigs~ 1 11;
#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 connect 9 0 8 0;
#X connect 10 0 9 0;
#X restore 433 280 pd multichannel;
#X text 145 364 mc <float>;
#X text 217 364 - set number of output channels;
Expand Down

0 comments on commit 4a7f839

Please sign in to comment.