File: mass.c

package info (click to toggle)
pd-pmpd 0.9-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,884 kB
  • sloc: ansic: 5,316; makefile: 280
file content (186 lines) | stat: -rw-r--r-- 4,154 bytes parent folder | download | duplicates (2)
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
#include "m_pd.h"
#include "math.h"

#define max(a,b) ( ((a) > (b)) ? (a) : (b) ) 
#define min(a,b) ( ((a) < (b)) ? (a) : (b) ) 


static t_class *mass_class;

typedef struct _mass {
  t_object  x_obj;
  t_float pos_old_1, pos_old_2, Xinit;
  t_float force, mass, dX;
  t_float minX, maxX;
  t_outlet *position_new, *vitesse_out, *force_out;
  t_symbol *x_sym; // receive
  unsigned int x_state; // random
  t_float x_f; // random

} t_mass;

static int makeseed(void)
{
    static unsigned int random_nextseed = 1489853723;
    random_nextseed = random_nextseed * 435898247 + 938284287;
    return (random_nextseed & 0x7fffffff);
}

static float random_bang(t_mass *x)
{
    int nval;
    int range = 2000000;
	float rnd;
	unsigned int randval = x->x_state;
	x->x_state = randval = randval * 472940017 + 832416023;
    nval = ((double)range) * ((double)randval)
    	* (1./4294967296.);
    if (nval >= range) nval = range-1;

	rnd=nval;

	rnd-=1000000;
	rnd=rnd/1000000.;	//pour mettre entre -1 et 1;
    return (rnd);
}

void mass_minX(t_mass *x, t_floatarg f1)
{
  x->minX = f1;
}

void mass_maxX(t_mass *x, t_floatarg f1)
{
  x->maxX = f1;
}

void mass_float(t_mass *x, t_floatarg f1)
{
  x->force += f1;
}

void mass_bang(t_mass *x)
{
  t_float pos_new;

	if (x->mass > 0)
  pos_new = x->force/x->mass + 2*x->pos_old_1 - x->pos_old_2;
	else pos_new = x->pos_old_1;

  pos_new = max(min(x->maxX, pos_new), x->minX);

  pos_new += x->dX;

  x->pos_old_1 += x->dX; // pour ne pas avoir d'inertie suplementaire du a ce deplacement
 
  outlet_float(x->vitesse_out, x->pos_old_1 - x->pos_old_2);
  outlet_float(x->force_out, x->force);
  outlet_float(x->position_new, pos_new);

  x->pos_old_2 = x->pos_old_1;
  x->pos_old_1 = pos_new;

//  x->force = 0;

  x->force = random_bang(x)*1e-25; // avoiding denormal problem by adding low amplitude noise

  x->dX = 0;

}

void mass_reset(t_mass *x)
{
  x->pos_old_2 = x->Xinit;
  x->pos_old_1 = x->Xinit;

  x->force=0;

  outlet_float(x->position_new, x->Xinit);
}

void mass_resetF(t_mass *x)
{
  x->force=0;

}

void mass_dX(t_mass *x, t_float posX)
{
  x->dX += posX;
}

void mass_setX(t_mass *x, t_float posX)
{
  x->pos_old_2 = posX;			// clear history for stability (instability) problem
  x->pos_old_1 = posX;

  x->force=0;

  outlet_float(x->position_new, posX);
}

void mass_loadbang(t_mass *x)
{
  outlet_float(x->position_new, x->Xinit);
}

void mass_set_mass(t_mass *x, t_float mass)
{
  x->mass=mass;
}

static void mass_free(t_mass *x)
{
    pd_unbind(&x->x_obj.ob_pd, x->x_sym);
}

void *mass_new(t_symbol *s, t_floatarg M, t_floatarg X)
{
  
  t_mass *x = (t_mass *)pd_new(mass_class);

  x->x_sym = s;
  pd_bind(&x->x_obj.ob_pd, s);

  x->position_new=outlet_new(&x->x_obj, 0);
  x->force_out=outlet_new(&x->x_obj, 0);
  x->vitesse_out=outlet_new(&x->x_obj, 0); 

  x->Xinit=X;

  x->pos_old_1 = X;
  x->pos_old_2 = X;
  x->force=0;
  x->mass=M;

  x->minX = -100000;
  x->maxX = 100000;

  if (x->mass<=0) x->mass=1;

  makeseed();

  return (void *)x;
}

void mass_setup(void) 
{

  mass_class = class_new(gensym("mass"),
        (t_newmethod)mass_new,
        (t_method)mass_free,
		sizeof(t_mass),
        CLASS_DEFAULT, A_DEFSYM, A_DEFFLOAT, A_DEFFLOAT,0);
  class_addcreator((t_newmethod)mass_new, gensym("masse"), A_DEFSYM, A_DEFFLOAT, A_DEFFLOAT,0);
  class_addfloat(mass_class, mass_float);
  class_addbang(mass_class, mass_bang);
  class_addmethod(mass_class, (t_method)mass_set_mass, gensym("setM"), A_DEFFLOAT, 0);
  class_addmethod(mass_class, (t_method)mass_setX, gensym("setX"), A_DEFFLOAT, 0);
  class_addmethod(mass_class, (t_method)mass_dX, gensym("dX"), A_DEFFLOAT, 0);
  class_addmethod(mass_class, (t_method)mass_reset, gensym("reset"), 0);
  class_addmethod(mass_class, (t_method)mass_resetF, gensym("resetF"), 0);
  class_addmethod(mass_class, (t_method)mass_minX, gensym("setXmin"), A_DEFFLOAT, 0);
  class_addmethod(mass_class, (t_method)mass_maxX, gensym("setXmax"), A_DEFFLOAT, 0);
  class_addmethod(mass_class, (t_method)mass_loadbang, gensym("loadbang"), 0);
}