File: zeroxpos~.c

package info (click to toggle)
pd-ekext 0.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,408 kB
  • sloc: ansic: 2,700; makefile: 280
file content (194 lines) | stat: -rw-r--r-- 4,923 bytes parent folder | download | duplicates (3)
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
#include "m_pd.h"
#include <math.h>

static t_class *zeroxpos_tilde_class;

typedef struct _zeroxpos_control
{
  t_float *c_input;
  t_int final_pol;
} t_zeroxpos_control;

typedef struct _zeroxpos_tilde 
{
  t_object x_obj;
  t_zeroxpos_control x_ctl;
  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;

t_int *zeroxpos_tilde_perform(t_int *w) 
{
  t_zeroxpos_tilde     *x =   (t_zeroxpos_tilde *)(w[1]);
  t_zeroxpos_control *ctl = (t_zeroxpos_control *)(w[2]);  
  int                   n =                  (int)(w[3]);
  t_float            *in = ctl->c_input;
  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;
  x->i_pol = 0;
  x->i_ndx = -1;
  int prev = ctl->final_pol == 0 ? in[0] : ctl->final_pol;

  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_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;
	    }
	}
      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_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;
	    }
	}
    }
  return(w+4);
}

void zeroxpos_tilde_bang(t_zeroxpos_tilde *x)
{
  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) 
{
  x->x_ctl.c_input = sp[0]->s_vec;
  dsp_add(zeroxpos_tilde_perform, 3, x, &x->x_ctl, sp[0]->s_n);
  return (void *)x;
}

void *zeroxpos_tilde_new(t_floatarg f) 
{
  t_zeroxpos_tilde *x = (t_zeroxpos_tilde *)pd_new(zeroxpos_tilde_class);
  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"));
  x->f_pol = outlet_new(&x->x_obj, gensym("float"));
  return (void *)x;
}

void zeroxpos_tilde_setup(void) 
{
  zeroxpos_tilde_class = class_new(gensym("zeroxpos~"), 
  (t_newmethod)zeroxpos_tilde_new, 
  0, sizeof(t_zeroxpos_tilde),
  CLASS_DEFAULT, A_DEFFLOAT, 0);

  post("|¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬zeroxpos~``````````````````````|");
  post("|find 1st, 2nd or 3rd etc zero crossing point in frame|");
  post("|````edward¬¬¬¬¬¬¬¬¬¬¬¬kelly``````````````````2005¬¬¬¬|");


  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);
}