File: isoWrap~.c

package info (click to toggle)
pd-ekext 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,804 kB
  • sloc: ansic: 6,548; makefile: 102
file content (276 lines) | stat: -rw-r--r-- 7,659 bytes parent folder | download
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* isoWrap~ - an isorhythmic phasor~ wrapper
 * Copyright (c) 2005-2023 Edward Kelly
 * Forinformaion on usage and distribution, and for a DICLAIMER OF ALL
 * WARRANTIES, see the file "LICENSE.txt," in this distribution. */

#ifdef __APPLE__
#include <sys/types.h>
#include <sys/time.h>
#include <sys/math.h>
#endif

#include <stdlib.h>
#include <time.h>
#include <math.h>

#include "m_pd.h"

static t_class *isoWrap_tilde_class;

typedef struct _isoWrap_tilde {
  t_object x_obj;

  t_float den, num, nuMult, inMult;
  t_float increment, off1;
  //lcmgcd calculations:
  int lcm, gcd, result, b, iMult;

  int myBug;

  //momentary value adjustments:
  int theCycle, k_i, k_s;//, ratioBal, theLimit, overShoot;
  t_float theOffset, deNorm;

  int resetNextPhase;//, resetNextOffset;

  int deNormalize, prevDeN, reNormFlag, waitPhase;// direction;
  t_float f_s, f_i, f_o, f_prev;
  t_outlet *phOff;
} t_isoWrap_tilde;

void *isoWrap_tilde_new(void)
{
  t_isoWrap_tilde *x = (t_isoWrap_tilde *)pd_new(isoWrap_tilde_class);
  outlet_new(&x->x_obj, gensym("signal"));
  x->phOff = outlet_new(&x->x_obj, &s_float);
  x->deNormalize = 0;
  x->deNorm = 1;
  x->prevDeN = 0;
  //x->ratioBal = 0;
  x->reNormFlag = 0;
  x->f_s = x->f_i = x->f_prev = 0;
  x->num = 4;
  x->den = 4;
  x->lcm = 4;
  x->result = 1;
  x->b = 1;
  x->theCycle = 0;
  //x->theLimit = 1;
  x->theOffset = 0.0;
  x->increment = 0.0;
  x->waitPhase = 0;
  x->myBug = 0;
  return (x);
}

/*static void isoWrap_tilde_floatRes(t_isoWrap_tilde *x, t_floatarg f)
  {
  if(f > 0 && f < 0.1) x->fRes = f;
  else x->fRes = 0;
  }*/

void isoWrap_tilde_calculate_lcm(t_isoWrap_tilde *x)
{
  int a, b, t;

  a = x->result;
  b = x->b;

  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }

  x->gcd = a;
  x->lcm = (x->result*x->b)/a;
}

/* It's definitely harder to overshoot this one,
 * since it uses wrapping for values greater than 1 to generate
 * the final wrapped signal. But maybe there's no reason to...
 */
//static void isoWrap_tilde_overShoot(t_isoWrap_tilde *x, t_floatarg f)
//{
//  if(f != 0.0) x->overShoot = 1;
//  else x->overShoot = 0;
//}

void isoWrap_tilde_resetNextPhase(t_isoWrap_tilde *x)
{
  x->resetNextPhase = 1;
}

void isoWrap_tilde_deNormalize(t_isoWrap_tilde *x, t_floatarg f)
{
  x->prevDeN = x->deNormalize;
  x->deNormalize = f == 0 ? 0 : f == 1.0? 1 : f == 2.0 ? 2 : 3;
  if(x->deNormalize == 3)
    {
      x->theCycle = 0;
      x->theOffset = 0;
      x->reNormFlag = 0;
    }
  if(x->prevDeN == 3 && x->deNormalize < 3) x->reNormFlag = 1;
}

void isoWrap_tilde_waitPhase(t_isoWrap_tilde *x, t_floatarg f)
{
  x->waitPhase = (int)f;
}

void isoWrap_tilde_debug(t_isoWrap_tilde *x, t_floatarg f)
{
  x->myBug = f != 0 ? 1 : 0;
}

void isoWrap_tilde_setFraction(t_isoWrap_tilde *x, t_symbol *s, int argc, t_atom *argv)
{
  int numIn, denIn;
  if(argc == 2)
    {
      numIn = (int)atom_getfloat(argv);
      denIn = (int)atom_getfloat(argv+1);
      if(numIn <= 0 || denIn <= 0)
        {
          pd_error(x, "Numerator and denominator of fraction must be integers > 0!");
        }
      else
        {
          //x->ratioBal = x->num > x->den ? 1 : x->den > x->num ? -1 : 0;
          x->num = (t_float)numIn;
          x->den = (t_float)denIn;
          x->result = (int)x->num;
          x->b = (int)x->den;
          isoWrap_tilde_calculate_lcm(x);
          //x->theLimit = x->lcm - 1;
          x->nuMult = (t_float)x->lcm / (t_float)x->den;
          x->inMult = ((t_float)x->num * x->nuMult) / (t_float)x->lcm;
          if(x->myBug)post("x->inMult = %f",x->inMult);
          x->deNorm = 1 / x->inMult;
          if(x->myBug)post("x->deNorm = %f",x->deNorm);
          x->iMult = (int)x->inMult;
          x->off1 = x->inMult > (t_float)x->iMult ? 1.0 : 0.0;
          x->increment = 1 - ((t_float)x->iMult + x->off1 - x->inMult);
        }
    }
}

t_int *isoWrap_tilde_perform(t_int *w)
{
  t_isoWrap_tilde *x = (t_isoWrap_tilde *)(w[1]);
  t_sample *in  = (t_sample *)(w[2]);
  t_sample *out = (t_sample *)(w[3]);
  int   n       = (int)(w[4]);
  while(n--)
    {
      x->f_i = *in++ * x->inMult;
      if(x->f_i < x->f_prev)
        {
          if(x->waitPhase && x->reNormFlag)
            {
              x->theCycle = 0;
              x->theOffset = 0;
              x->reNormFlag = 0;
            }
          //resetNextPhase and resetNextOffset? need to be dealt with here
          else if(x->resetNextPhase)// || x->reNormFlag)
            {
              x->theCycle = 0;
              x->theOffset = 0;
              x->resetNextPhase = 0;
            }
          else if(x->deNormalize == 3 && x->num >= x->den)// || x->reNormFlag)
            {
              x->theCycle = 0;
              x->theOffset = 0;
            }
          else if(x->deNormalize < 3 || x->num < x->den)// && !x->reNormFlag)
            {
              x->theCycle++;
              x->theCycle = x->theCycle % x->lcm;
              x->k_s = (int)((t_float)x->theCycle * x->increment);
              x->theOffset = ((t_float)x->theCycle * x->increment) - x->k_s;
              if(x->myBug) post("The increment: %f",x->increment);
            }
        }
      x->f_s = x->f_i + x->theOffset;
      x->k_i = x->f_s;
      if(x->deNormalize < 3 && (!x->waitPhase || !x->reNormFlag))
        {
          x->f_o = x->f_s - x->k_i;
        }
      else if(x->deNormalize == 3)
        {
          if(x->num >= x->den)
            {
              if(x->f_s < 1.0)
                {
                  x->f_o = x->f_s;
                }
              else
                {
                  x->f_o = 1.0;
                }
            }
          else if(x->num < x->den)
            {
              x->f_o = x->f_s - x->k_i;
            }
        }
      else
        {
          x->f_o = x->f_s - x->k_i;
        }
      if(x->num >= x->den)
        {
          if(x->deNormalize >= 2)// && x->num >= x->den)
            {
              x->f_o = x->f_o * x->deNorm;
            }
          else if(x->deNormalize == 1)
            {
              x->f_o = x->f_o * x->inMult;
            }
        }
      else if(x->num < x->den)
        {
          if(x->deNormalize == 3)
            {
              x->f_o = x->f_o * x->deNorm;
              if(x->f_o > 1.0)
                {
                  x->f_o = 1.0;
                }
            }
          else if(x->deNormalize == 2)
            {
              x->f_o = x->f_o * x->inMult;
            }
          else if(x->deNormalize == 1)
            {
              x->f_o = x->f_o * x->deNorm;
            }
        }
      *out++ = x->f_o;
      x->f_prev = x->f_i;
    }
  outlet_float(x->phOff, x->theOffset);
  return(w+5);
}

void isoWrap_tilde_dsp(t_isoWrap_tilde *x, t_signal **sp) {
  dsp_add(isoWrap_tilde_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

void isoWrap_tilde_setup(void)
{
  isoWrap_tilde_class = class_new(gensym("isoWrap~"),  (t_newmethod)isoWrap_tilde_new,
                                  0, sizeof(t_isoWrap_tilde), CLASS_DEFAULT, A_DEFFLOAT, 0);
  CLASS_MAINSIGNALIN(isoWrap_tilde_class, t_isoWrap_tilde, f_s);
  class_addmethod(isoWrap_tilde_class, (t_method)isoWrap_tilde_dsp, gensym("dsp"), A_CANT, 0);
  class_addmethod(isoWrap_tilde_class, (t_method)isoWrap_tilde_setFraction, gensym("setFraction"), A_GIMME, 0);
  class_addmethod(isoWrap_tilde_class, (t_method)isoWrap_tilde_resetNextPhase, gensym("resetNextPhase"), A_DEFFLOAT, 0);
  class_addmethod(isoWrap_tilde_class, (t_method)isoWrap_tilde_deNormalize, gensym("deNormalize"), A_DEFFLOAT, 0);
  class_addmethod(isoWrap_tilde_class, (t_method)isoWrap_tilde_debug, gensym("debug"), A_DEFFLOAT, 0);
}