aboutsummaryrefslogtreecommitdiff
path: root/zeroxpos~/zeroxpos~.c
diff options
context:
space:
mode:
Diffstat (limited to 'zeroxpos~/zeroxpos~.c')
-rw-r--r--zeroxpos~/zeroxpos~.c138
1 files changed, 117 insertions, 21 deletions
diff --git a/zeroxpos~/zeroxpos~.c b/zeroxpos~/zeroxpos~.c
index d6c15c5..54373f0 100644
--- a/zeroxpos~/zeroxpos~.c
+++ b/zeroxpos~/zeroxpos~.c
@@ -1,4 +1,5 @@
#include "m_pd.h"
+#include <math.h>
static t_class *zeroxpos_tilde_class;
@@ -12,8 +13,8 @@ typedef struct _zeroxpos_tilde
{
t_object x_obj;
t_zeroxpos_control x_ctl;
- t_float f_num, f_dummy;
- t_int i_bang, i_pol, i_count, i_ndx;
+ t_float f_num, f_dummy, f_dir;
+ t_int i_bang, i_pol, i_count, i_ndx, i_mode;
t_outlet *f_pos, *f_pol;
} t_zeroxpos_tilde;
@@ -23,7 +24,8 @@ t_int *zeroxpos_tilde_perform(t_int *w)
t_zeroxpos_control *ctl = (t_zeroxpos_control *)(w[2]);
int n = (int)(w[3]);
t_float *in = ctl->c_input;
- int number = (int)x->f_num;
+ x->f_dir = x->f_num >= 0 ? 1 : -1;
+ int number = x->f_num >= 1 || x->f_num <= -1 ? (int)fabs(x->f_num) : 1;
int count = x->i_count;
int polarity = 1;
int i = 0;
@@ -31,33 +33,125 @@ t_int *zeroxpos_tilde_perform(t_int *w)
x->i_ndx = -1;
int prev = ctl->final_pol == 0 ? in[0] : ctl->final_pol;
- for(i=0;i<n;i++)
+ if(x->i_mode == 0)
{
- polarity = in[i] >= 0 ? 1 : -1;
- if((polarity < prev || polarity > prev) && count == number && x->i_bang == 1)
+ if(x->f_dir > 0)
{
- x->i_ndx = i;
- x->i_pol = polarity;
- count += 1e+06;
- x->i_bang = 0;
- outlet_float(x->f_pol, (float)x->i_pol);
- outlet_float(x->f_pos, (float)x->i_ndx);
+ for(i=0;i<n;i++)
+ {
+ polarity = in[i] >= 0 ? 1 : -1;
+ if((polarity < prev || polarity > prev) && count == number && x->i_bang == 1)
+ {
+ x->i_ndx = i;
+ x->i_pol = polarity;
+ count += 1e+06;
+ x->i_bang = 0;
+ outlet_float(x->f_pol, (float)x->i_pol);
+ outlet_float(x->f_pos, (float)x->i_ndx);
+ }
+ if((polarity < prev || polarity > prev) && count < number) count++;
+ if(i==n-1&&count<number)
+ {
+ ctl->final_pol = polarity;
+ x->i_count = count;
+ }
+ prev = polarity;
+ }
}
- if((polarity < prev || polarity > prev) && count < number) count++;
- if(i==n-1)
+ else if(x->f_dir < 0)
{
- ctl->final_pol = polarity;
- x->i_count = count;
+ for(i=n-1;i>=0;i--)
+ {
+ polarity = in[i] >= 0 ? 1 : -1;
+ if((polarity < prev || polarity > prev) && count == number && x->i_bang == 1)
+ {
+ x->i_ndx = i;
+ x->i_pol = polarity;
+ count += 1e+06;
+ x->i_bang = 0;
+ outlet_float(x->f_pol, (float)(x->i_pol*-1));
+ outlet_float(x->f_pos, (float)x->i_ndx+1);
+ }
+ if((polarity < prev || polarity > prev) && count < number) count++;
+ if(i==0&&count<number)
+ {
+ ctl->final_pol = polarity;
+ x->i_count = count;
+ }
+ prev = polarity;
+ }
+ }
+ }
+ else if(x->i_mode != 0)
+ {
+ if(x->f_dir > 0)
+ {
+ for(i=0;i<n;i++)
+ {
+ polarity = in[i] >= 0 ? 1 : -1;
+ if((polarity < prev || polarity > prev) && count == number)
+ {
+ x->i_ndx = i;
+ x->i_pol = polarity;
+ count += 1e+06;
+ x->i_bang = 0;
+ }
+ if((polarity < prev || polarity > prev) && count < number) count++;
+ if(i==n-1&&count<number)
+ {
+ ctl->final_pol = polarity;
+ x->i_count = count;
+ }
+ prev = polarity;
+ }
+ }
+ else if(x->f_dir < 0)
+ {
+ for(i=n-1;i>=0;i--)
+ {
+ polarity = in[i] >= 0 ? 1 : -1;
+ if((polarity < prev || polarity > prev) && count == number)
+ {
+ x->i_ndx = i;
+ x->i_pol = polarity;
+ count += 1e+06;
+ x->i_bang = 0;
+ }
+ if((polarity < prev || polarity > prev) && count < number) count++;
+ if(i==0&&count<number)
+ {
+ ctl->final_pol = polarity;
+ x->i_count = count;
+ }
+ prev = polarity;
+ }
}
- prev = polarity;
}
return(w+4);
}
void zeroxpos_tilde_bang(t_zeroxpos_tilde *x)
{
- x->i_bang = 1;
- x->i_count = 0;
+ if(x->i_mode == 0)
+ {
+ x->i_bang = 1;
+ x->i_count = 0;
+ }
+ else if(x->i_mode != 0 && x->f_dir > 0)
+ {
+ outlet_float(x->f_pol, (float)x->i_pol);
+ outlet_float(x->f_pos, (float)x->i_ndx);
+ }
+ else if(x->i_mode != 0 && x->f_dir < 0)
+ {
+ outlet_float(x->f_pol, (float)(x->i_pol*-1));
+ outlet_float(x->f_pos, (float)x->i_ndx+1);
+ }
+}
+
+void zeroxpos_tilde_mode(t_zeroxpos_tilde *x, t_floatarg fmode)
+{
+ x->i_mode = (int)fmode;
}
void *zeroxpos_tilde_dsp(t_zeroxpos_tilde *x, t_signal **sp)
@@ -70,8 +164,9 @@ void *zeroxpos_tilde_dsp(t_zeroxpos_tilde *x, t_signal **sp)
void *zeroxpos_tilde_new(t_floatarg f)
{
t_zeroxpos_tilde *x = (t_zeroxpos_tilde *)pd_new(zeroxpos_tilde_class);
- x->f_num = f > 0 ? f : 1;
- x->x_ctl.final_pol = 1;
+ x->f_dir = f >= 0 ? 1 : -1;
+ x->f_num = f >= 1 || f <= -1 ? (int)fabs(f) : 1;
+ x->x_ctl.final_pol = 0;
x->i_count = 0;
floatinlet_new (&x->x_obj, &x->f_num);
x->f_pos = outlet_new(&x->x_obj, gensym("float"));
@@ -93,6 +188,7 @@ void zeroxpos_tilde_setup(void)
class_sethelpsymbol(zeroxpos_tilde_class, gensym("help-zeroxpos~"));
class_addbang(zeroxpos_tilde_class, zeroxpos_tilde_bang);
class_addmethod(zeroxpos_tilde_class, (t_method)zeroxpos_tilde_dsp, gensym("dsp"), 0);
+ class_addmethod(zeroxpos_tilde_class, (t_method)zeroxpos_tilde_mode, gensym("mode"), A_DEFFLOAT, 0);
CLASS_MAINSIGNALIN(zeroxpos_tilde_class, t_zeroxpos_tilde, f_dummy);
}