Skip to content

Commit

Permalink
xgate.mc~
Browse files Browse the repository at this point in the history
  • Loading branch information
porres committed Nov 28, 2023
1 parent 5c3f341 commit cf5d1a9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
54 changes: 39 additions & 15 deletions Code_source/Compiled/audio/xgate.mc~.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ static t_class *xgatemc_class;
typedef struct _xgatemc{
t_object x_obj;
int x_lastout;
int x_n_outs;
t_float *x_input;
int x_ch_outs;
double x_n_fade; // fade length in samples
float x_sr_khz;
int x_active_out[MAXOUTS];
int x_count[MAXOUTS];
int x_ch;
int x_block;
}t_xgatemc;

void xgatemc_float(t_xgatemc *x, t_floatarg f){
int out = f < 0 ? 0 : f > x->x_n_outs ? x->x_n_outs : (int)f;
int out = f < 0 ? 0 : f > x->x_ch_outs ? x->x_ch_outs : (int)f;
if(x->x_lastout != out){
if(out)
x->x_active_out[out - 1] = 1;
Expand All @@ -33,42 +36,62 @@ void xgatemc_float(t_xgatemc *x, t_floatarg f){
static void xgatemc_time(t_xgatemc *x, t_floatarg f){
double last_fade_n = x->x_n_fade;
x->x_n_fade = (x->x_sr_khz * (f < 0 ? 0 : f)) + 1;
for(int n = 0; n < x->x_n_outs; n++)
for(int n = 0; n < x->x_ch_outs; n++)
if(x->x_count[n]) // adjust counters
x->x_count[n] = (x->x_count[n] / last_fade_n) * x->x_n_fade;
}

static t_int *xgatemc_perform(t_int *w){
t_xgatemc *x = (t_xgatemc *)(w[1]);
int n = (int)(w[2]);
t_float *in = (t_float *)(w[3]);
t_float *inputsig = (t_float *)(w[3]);
t_float *out = (t_float *)(w[4]);
for(int i = 0; i < n; i++){
float input = in[i];
for(int j = 0; j < x->x_n_outs; j++){
if(x->x_active_out[j] && x->x_count[j] < x->x_n_fade)
x->x_count[j]++;
else if(!x->x_active_out[j] && x->x_count[j] > 0)
x->x_count[j]--;
out[j*n + i] = input * sin((x->x_count[j] / x->x_n_fade) * HALF_PI);
t_float *in = x->x_input;
int i;
for(i = 0; i < n * x->x_ch; i++) // copy input
in[i] = inputsig[i];
for(i = 0; i < n; i++){
for(int ch = 0; ch < x->x_ch; ch++){
for(int j = 0; j < x->x_ch_outs; j++){
if(x->x_active_out[j] && x->x_count[j] < x->x_n_fade)
x->x_count[j]++;
else if(!x->x_active_out[j] && x->x_count[j] > 0)
x->x_count[j]--;
float fade = sin((x->x_count[j] / x->x_n_fade) * HALF_PI);
out[j*x->x_ch*n + ch*n + i] = in[ch*n + i] * fade;
}
}
}
return(w+5);
}

static void xgatemc_dsp(t_xgatemc *x, t_signal **sp){
signal_setmultiout(&sp[1], x->x_n_outs);
int block = sp[0]->s_n;
int ch = sp[0]->s_nchans;
signal_setmultiout(&sp[1], x->x_ch_outs*ch);
if(x->x_block != block || x->x_ch != ch){
x->x_input = (t_float *)resizebytes(x->x_input,
x->x_block*x->x_ch * sizeof(t_float), ch*block * sizeof(t_float));
x->x_block = block, x->x_ch = ch;
}
dsp_add(xgatemc_perform, 4, x, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);
}

void xgatemc_free(t_xgatemc *x){
freebytes(x->x_input, x->x_block*x->x_ch * sizeof(*x->x_input));
}

static void *xgatemc_new(t_floatarg f1, t_floatarg f2, t_floatarg f3){
t_xgatemc *x = (t_xgatemc *)pd_new(xgatemc_class);
for(int n = 0; n < MAXOUTS; n++){
x->x_active_out[n] = 0;
x->x_count[n] = 0;
}
x->x_ch = 1;
x->x_block = sys_getblksize();
x->x_input = (t_float *)getbytes(x->x_block*sizeof(*x->x_input));
t_float out = f1, ms = f2, init_out = f3;
x->x_n_outs = out < 1 ? 1 : out > MAXOUTS ? MAXOUTS : (int)out;
x->x_ch_outs = out < 1 ? 1 : out > MAXOUTS ? MAXOUTS : (int)out;
x->x_sr_khz = sys_getsr() * 0.001;
x->x_n_fade = x->x_sr_khz * (ms > 0 ? ms : 0) + 1;
x->x_lastout = 0;
Expand All @@ -78,7 +101,8 @@ static void *xgatemc_new(t_floatarg f1, t_floatarg f2, t_floatarg f3){
}

void setup_xgate0x2emc_tilde(void) {
xgatemc_class = class_new(gensym("xgate.mc~"), (t_newmethod)xgatemc_new, 0,
xgatemc_class = class_new(gensym("xgate.mc~"), (t_newmethod)xgatemc_new,
(t_method)xgatemc_free,
sizeof(t_xgatemc), CLASS_MULTICHANNEL, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
class_addfloat(xgatemc_class, (t_method)xgatemc_float);
class_addmethod(xgatemc_class, nullfn, gensym("signal"), 0);
Expand Down
6 changes: 3 additions & 3 deletions Code_source/Compiled/audio/xgate2~.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static t_int *xgate2_perform(t_int *w){
if(spread < 0.1)
spread = 0.1;
spread *= 2;
float range = x->x_n_outlets / spread;
float range = n_outlets / spread;
if(!x->x_index)
pos *= n_outlets;
if(pos < 0)
Expand Down Expand Up @@ -70,7 +70,7 @@ static void xgate2_dsp(t_xgate2 *x, t_signal **sp){
static void *xgate2_new(t_symbol *s, int ac, t_atom *av){
s = NULL;
t_xgate2 *x = (t_xgate2 *)pd_new(xgate2_class);
t_float n_outlets = 2; //inlets not counting xgate2 input
t_float n_outlets = 2;
float spread = 1;
if(ac){
if(av->a_type == A_SYMBOL){
Expand Down Expand Up @@ -106,7 +106,7 @@ static void *xgate2_new(t_symbol *s, int ac, t_atom *av){
return(NULL);
}

void * xgate2_free(t_xgate2 *x){
void *xgate2_free(t_xgate2 *x){
freebytes(x->x_ovecs, x->x_n_outlets * sizeof(*x->x_ovecs));
inlet_free(x->x_inlet_spread);
return(void *)x;
Expand Down

0 comments on commit cf5d1a9

Please sign in to comment.