Debian Codegraph @

probalizer.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* ---------------------------------------------------------------------------- */
2 /* Copyright (c) 2002 Yves Degoyon. */
3 /* For information on usage and redistribution, and for a DISCLAIMER OF ALL */
4 /* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
5 /* */
6 /* probalizer : outputs integer values according to a drawn probability curve */
7 /* */
8 /* This program is free software; you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation; either version 2 */
11 /* of the License, or (at your option) any later version. */
12 /* */
13 /* See file LICENSE for further informations on licensing terms. */
14 /* */
15 /* This program is distributed in the hope that it will be useful, */
16 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
17 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
18 /* GNU General Public License for more details. */
19 /* */
20 /* You should have received a copy of the GNU General Public License */
21 /* along with this program; if not, write to the Free Software */
22 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* */
24 /* Based on PureData by Miller Puckette and others. */
25 /* */
26 /* Made while listening to : */
27 /* */
28 /* "I am a product, I am a sample" */
29 /* "I'm hopeless, aimless" */
30 /* Crass -- Feeding of the 5000 */
31 /* ---------------------------------------------------------------------------- */
32
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <math.h>
38 #include <ctype.h>
39 #include "m_pd.h"
40 #include "m_imp.h"
41 #include "g_canvas.h"
42
43 #include "probalizer.h"
44
45 #ifdef _WIN32
46 # include <io.h>
47 # define random rand
48 #endif
49
50 #ifndef _MSC_VER /* only MSVC doesn't have unistd.h */
51 #include <unistd.h>
52 #endif
53
54 #define DEFAULT_PROBALIZER_WIDTH 200
55 #define DEFAULT_PROBALIZER_HEIGHT 200
56 #define DEFAULT_PROBALIZER_NBVALUES 100
57 #define DEFAULT_PROBALIZER_NBOCCURRENCES 100
58 #define DEFAULT_PROB_VALUE 10
59
60 static char *probalizer_version = "probalizer : outputs integer values according to a drawn probability curve , version 0.4 ([email protected])";
61
62 t_widgetbehavior probalizer_widgetbehavior;
63 static t_class *probalizer_class;
64
65 static int guidebug=0;
66 static int pointsize = 5;
67
68 #define SYS_VGUI2(a,b) if (guidebug) \
69 post(a,b);\
70 sys_vgui(a,b)
71
72 #define SYS_VGUI3(a,b,c) if (guidebug) \
73 post(a,b,c);\
74 sys_vgui(a,b,c)
75
76 #define SYS_VGUI4(a,b,c,d) if (guidebug) \
77 post(a,b,c,d);\
78 sys_vgui(a,b,c,d)
79
80 #define SYS_VGUI5(a,b,c,d,e) if (guidebug) \
81 post(a,b,c,d,e);\
82 sys_vgui(a,b,c,d,e)
83
84 #define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \
85 post(a,b,c,d,e,f);\
86 sys_vgui(a,b,c,d,e,f)
87
88 #define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
89 post(a,b,c,d,e,f,g);\
90 sys_vgui(a,b,c,d,e,f,g)
91
92 #define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \
93 post(a,b,c,d,e,f,g,h);\
94 sys_vgui(a,b,c,d,e,f,g,h)
95
96 #define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
97 post(a,b,c,d,e,f,g,h,i);\
98 sys_vgui(a,b,c,d,e,f,g,h,i)
99
100 #define SYS_VGUI13(a,b,c,d,e,f,g,h,i,j,k,l,m) if (guidebug) \
101 post(a,b,c,d,e,f,g,h,i,j,k,l,m);\
102 sys_vgui(a,b,c,d,e,f,g,h,i,j,k,l,m)
103
104 /* drawing functions */
105 static void probalizer_draw_new(t_probalizer *x, t_glist *glist)
106 {
107 t_canvas *canvas=glist_getcanvas(glist);
108 t_int ei;
109
110 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #6790E2 -tags %xPROBALIZER\n",
111 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
112 text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist)+x->x_height,
113 x);
114 SYS_VGUI5(".x%lx.c create text %d %d -font -*-courier-bold--normal--10-* -text \"0\" -tags %xLTCAPTION\n",
115 canvas, text_xpix(&x->x_obj, glist)-15, text_ypix(&x->x_obj, glist) + x->x_height, x );
116 SYS_VGUI6(".x%lx.c create text %d %d -font -*-courier-bold--normal--10-* -text \"%d\" -tags %xLBCAPTION\n",
117 canvas, text_xpix(&x->x_obj, glist)-15, text_ypix(&x->x_obj, glist), x->x_noccurrences, x );
118 SYS_VGUI5(".x%lx.c create text %d %d -font -*-courier-bold--normal--10-* -text \"1\" -tags %xBLCAPTION\n",
119 canvas, text_xpix(&x->x_obj, glist)+2, text_ypix(&x->x_obj, glist) + x->x_height + 10, x );
120 SYS_VGUI6(".x%lx.c create text %d %d -font -*-courier-bold--normal--10-* -text \"%d\" -tags %xBRCAPTION\n",
121 canvas, text_xpix(&x->x_obj, glist) + x->x_width-5, text_ypix(&x->x_obj, glist) + x->x_height + 10, x->x_nvalues, x );
122 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xIN\n",
123 canvas, text_xpix(&x->x_obj, glist),
124 text_ypix(&x->x_obj, glist) - 2,
125 text_xpix(&x->x_obj, glist) + 5,
126 text_ypix(&x->x_obj, glist) ,
127 x);
128 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xOUT\n",
129 canvas, text_xpix(&x->x_obj, glist),
130 text_ypix(&x->x_obj, glist) + x->x_height,
131 text_xpix(&x->x_obj, glist) + 5,
132 text_ypix(&x->x_obj, glist) + x->x_height + 2,
133 x);
134 SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xOUT2\n",
135 canvas, text_xpix(&x->x_obj, glist) + x->x_width -5,
136 text_ypix(&x->x_obj, glist) + x->x_height,
137 text_xpix(&x->x_obj, glist) + x->x_width,
138 text_ypix(&x->x_obj, glist) + x->x_height + 2,
139 x);
140 for ( ei=0; ei<x->x_nvalues; ei++ )
141 {
142 SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d -outline #000000 -fill #118373 -tags %xPROB%d\n",
143 canvas,
144 text_xpix(&x->x_obj, glist) + ei * x->x_width/x->x_nvalues,
145 text_ypix(&x->x_obj, glist) + x->x_height - ( *(x->x_probs+ei) * x->x_height / x->x_noccurrences ),
146 text_xpix(&x->x_obj, glist) + (ei+1) * x->x_width/x->x_nvalues,
147 text_ypix(&x->x_obj, glist) + x->x_height,
148 x, ei);
149 }
150 canvas_fixlinesfor( canvas, (t_text*)x );
151 }
152
153 static void probalizer_draw_update(t_probalizer *x)
154 {
155 t_canvas *canvas=glist_getcanvas(x->x_glist);
156 t_int ei;
157
158 for ( ei=0; ei<x->x_nvalues; ei++ )
159 {
160 SYS_VGUI8(".x%lx.c coords %xPROB%d %d %d %d %d\n",
161 canvas, x, ei,
162 text_xpix(&x->x_obj, x->x_glist) + ei * x->x_width / x->x_nvalues,
163 text_ypix(&x->x_obj, x->x_glist) + x->x_height - ( *(x->x_probs+ei) * x->x_height / x->x_noccurrences ),
164 text_xpix(&x->x_obj, x->x_glist) + (ei+1) * x->x_width / x->x_nvalues,
165 text_ypix(&x->x_obj, x->x_glist) + x->x_height );
166 }
167 canvas_fixlinesfor( canvas, (t_text*)x );
168 }
169
170 static void probalizer_draw_move(t_probalizer *x, t_glist *glist)
171 {
172 t_canvas *canvas=glist_getcanvas(glist);
173 t_int ei;
174
175 SYS_VGUI7(".x%lx.c coords %xPROBALIZER %d %d %d %d \n",
176 canvas, x, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
177 text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist)+x->x_height
178 );
179 SYS_VGUI7(".x%lx.c coords %xIN %d %d %d %d\n",
180 canvas, x, text_xpix(&x->x_obj, glist),
181 text_ypix(&x->x_obj, glist) - 2,
182 text_xpix(&x->x_obj, glist) + 5,
183 text_ypix(&x->x_obj, glist)
184 );
185 SYS_VGUI7(".x%lx.c coords %xOUT %d %d %d %d\n",
186 canvas, x, text_xpix(&x->x_obj, glist),
187 text_ypix(&x->x_obj, glist) + x->x_height,
188 text_xpix(&x->x_obj, glist) + 5,
189 text_ypix(&x->x_obj, glist) + x->x_height + 2
190 );
191 SYS_VGUI7(".x%lx.c coords %xOUT2 %d %d %d %d\n",
192 canvas, x, text_xpix(&x->x_obj, glist) + x->x_width - 5,
193 text_ypix(&x->x_obj, glist) + x->x_height,
194 text_xpix(&x->x_obj, glist) + x->x_width,
195 text_ypix(&x->x_obj, glist) + x->x_height + 2
196 );
197 SYS_VGUI5(".x%lx.c coords %xLTCAPTION %d %d\n",
198 canvas, x, text_xpix(&x->x_obj, glist)-15,
199 text_ypix(&x->x_obj, glist) + x->x_height
200 );
201 SYS_VGUI5(".x%lx.c coords %xLBCAPTION %d %d\n",
202 canvas, x, text_xpix(&x->x_obj, glist)-15,
203 text_ypix(&x->x_obj, glist)
204 );
205 SYS_VGUI5(".x%lx.c coords %xBLCAPTION %d %d\n",
206 canvas, x, text_xpix(&x->x_obj, glist)+2,
207 text_ypix(&x->x_obj, glist) + x->x_height + 10
208 );
209 SYS_VGUI5(".x%lx.c coords %xBRCAPTION %d %d\n",
210 canvas, x, text_xpix(&x->x_obj, glist) + x->x_width - 5,
211 text_ypix(&x->x_obj, glist) + x->x_height + 10
212 );
213 for ( ei=0; ei<x->x_nvalues; ei++ )
214 {
215 SYS_VGUI8(".x%lx.c coords %xPROB%d %d %d %d %d\n",
216 canvas, x, ei,
217 text_xpix(&x->x_obj, glist) + ei * x->x_width / x->x_nvalues,
218 text_ypix(&x->x_obj, glist) + x->x_height - ( *(x->x_probs+ei) * x->x_height / x->x_noccurrences ),
219 text_xpix(&x->x_obj, glist) + (ei+1) * x->x_width / x->x_nvalues,
220 text_ypix(&x->x_obj, glist) + x->x_height );
221 }
222 canvas_fixlinesfor( canvas, (t_text*)x );
223 }
224
225 static void probalizer_draw_erase(t_probalizer* x,t_glist* glist)
226 {
227 t_canvas *canvas=glist_getcanvas(glist);
228 int i, ei, gi;
229
230 SYS_VGUI3(".x%lx.c delete %xPROBALIZER\n", canvas, x );
231 SYS_VGUI3(".x%lx.c delete %xIN\n", canvas, x );
232 SYS_VGUI3(".x%lx.c delete %xOUT\n", canvas, x );
233 SYS_VGUI3(".x%lx.c delete %xOUT2\n", canvas, x );
234 SYS_VGUI3(".x%lx.c delete %xLTCAPTION\n", canvas, x );
235 SYS_VGUI3(".x%lx.c delete %xLBCAPTION\n", canvas, x );
236 SYS_VGUI3(".x%lx.c delete %xBLCAPTION\n", canvas, x );
237 SYS_VGUI3(".x%lx.c delete %xBRCAPTION\n", canvas, x );
238 for ( ei=0; ei<x->x_nvalues; ei++ )
239 {
240 SYS_VGUI4(".x%lx.c delete %xPROB%d\n", canvas, x, ei );
241 }
242 }
243
244 static void probalizer_draw_select(t_probalizer* x,t_glist* glist)
245 {
246 t_canvas *canvas=glist_getcanvas(glist);
247
248 if(x->x_selected)
249 {
250 /* sets the main item in blue */
251 SYS_VGUI3(".x%lx.c itemconfigure %xPROBALIZER -outline #0000FF\n", glist_getcanvas(glist), x);
252
253 }
254 else
255 {
256 /* sets the main item in black */
257 SYS_VGUI3(".x%lx.c itemconfigure %xPROBALIZER -outline #000000\n", glist_getcanvas(glist), x);
258 }
259 }
260
261 /* ------------------------ probalizer widgetbehaviour----------------------------- */
262
263
264 static void probalizer_getrect(t_gobj *z, t_glist *owner,
265 int *xp1, int *yp1, int *xp2, int *yp2)
266 {
267 t_probalizer* x = (t_probalizer*)z;
268
269 *xp1 = text_xpix(&x->x_obj, owner);
270 *yp1 = text_ypix(&x->x_obj, owner);
271 *xp2 = text_xpix(&x->x_obj, owner)+x->x_width;
272 *yp2 = text_ypix(&x->x_obj, owner)+x->x_height;
273 }
274
275 static void probalizer_save(t_gobj *z, t_binbuf *b)
276 {
277 t_probalizer *x = (t_probalizer *)z;
278 int ei,gi;
279
280 binbuf_addv(b, "ssiisiiiii", gensym("#X"),gensym("obj"),
281 (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
282 atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
283 x->x_width, x->x_height,
284 x->x_nvalues, x->x_noccurrences, x->x_save );
285 if ( x->x_save )
286 {
287 for ( ei=0; ei<x->x_nvalues; ei++ )
288 {
289 // post( "probalizer : saving ( %d, %d )", ei, gi );
290 binbuf_addv(b, "ii", ei, *(x->x_probs+ei) );
291 }
292 }
293 binbuf_addv(b, ";");
294 }
295
296 static void probalizer_properties(t_gobj *z, t_glist *owner)
297 {
298 char buf[800];
299 t_probalizer *x=(t_probalizer *)z;
300
301 sprintf(buf, "pdtk_probalizer_dialog %%s %d %d %d %d %d\n",
302 x->x_width, x->x_height, x->x_nvalues, x->x_noccurrences, x->x_save );
303 // post("probalizer_properties : %s", buf );
304 gfxstub_new(&x->x_obj.ob_pd, x, buf);
305 }
306
307 static void probalizer_select(t_gobj *z, t_glist *glist, int selected)
308 {
309 t_probalizer *x = (t_probalizer *)z;
310
311 x->x_selected = selected;
312 probalizer_draw_select( x, glist );
313 }
314
315 static void probalizer_vis(t_gobj *z, t_glist *glist, int vis)
316 {
317 t_probalizer *x = (t_probalizer *)z;
318 t_rtext *y;
319
320 // post("probalizer_vis : %d", vis );
321 if (vis)
322 {
323 probalizer_draw_new( x, glist );
324 }
325 else
326 {
327 probalizer_draw_erase( x, glist );
328 }
329 }
330
331 /* handle parameters sent by the property dialog */
332 static void probalizer_dialog(t_probalizer *x, t_symbol *s, int argc, t_atom *argv)
333 {
334 t_int onvalues, owidth, ei, gi, bi;
335 t_canvas *canvas=glist_getcanvas(x->x_glist);
336
337 if ( !x )
338 {
339 post( "probalizer : error :tried to set properties on an unexisting object" );
340 }
341 onvalues = x->x_nvalues;
342 owidth = x->x_width;
343 probalizer_draw_erase(x, x->x_glist);
344 if ( argc < 5 )
345 {
346 post( "probalizer : error in the number of arguments ( %d )", argc );
347 return;
348 }
349 if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT ||
350 argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
351 argv[4].a_type != A_FLOAT )
352 {
353 post( "probalizer : wrong arguments" );
354 return;
355 }
356
357 x->x_width = argv[0].a_w.w_float;
358 if ( x->x_width <= 0 ) x->x_width = 100;
359 x->x_height = argv[1].a_w.w_float;
360 if ( x->x_height <= 0 ) x->x_height = 100;
361 x->x_nvalues = argv[2].a_w.w_float;
362 if ( x->x_nvalues < 1 ) x->x_nvalues = 1;
363 x->x_noccurrences = argv[3].a_w.w_float;
364 if ( x->x_noccurrences < 1 ) x->x_noccurrences = 1;
365 x->x_save = argv[4].a_w.w_float;
366
367 // re-allocate arrays of values
368 post( "probalizer : re-allocate values" );
369 if ( onvalues != x->x_nvalues )
370 {
371 t_int mvalues = ( onvalues > x->x_nvalues ) ? x->x_nvalues : onvalues;
372 t_int *newprobs, *newovalues;
373
374 newprobs = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) );
375 newovalues = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) );
376 for ( ei=0; ei<x->x_nvalues; ei++ )
377 {
378 *(newprobs+ei)=DEFAULT_PROB_VALUE;
379 }
380 memset( newovalues, 0x0, x->x_nvalues*sizeof(t_int) );
381 for ( ei=0; ei<mvalues; ei++ )
382 {
383 *(newprobs+ei ) = *(x->x_probs+ei);
384 }
385 if ( x->x_probs )
386 freebytes( x->x_probs, onvalues*sizeof(t_int) );
387 x->x_probs = newprobs;
388 if ( x->x_ovalues )
389 freebytes( x->x_ovalues, onvalues*sizeof(t_int) );
390 x->x_ovalues = newovalues;
391 }
392
393 probalizer_draw_new(x, x->x_glist);
394 canvas_fixlinesfor( canvas, (t_text*)x );
395 }
396
397 static void probalizer_delete(t_gobj *z, t_glist *glist)
398 {
399 canvas_deletelinesfor(glist, (t_text *)z);
400 }
401
402 static void probalizer_displace(t_gobj *z, t_glist *glist, int dx, int dy)
403 {
404 t_probalizer *x = (t_probalizer *)z;
405 int xold = text_xpix(&x->x_obj, glist);
406 int yold = text_ypix(&x->x_obj, glist);
407
408 // post( "probalizer_displace dx=%d dy=%d", dx, dy );
409
410 x->x_obj.te_xpix += dx;
411 x->x_obj.te_ypix += dy;
412 if(xold != x->x_obj.te_xpix || yold != x->x_obj.te_ypix)
413 {
414 probalizer_draw_move(x, x->x_glist);
415 }
416 }
417
418 static int probalizer_click(t_gobj *z, struct _glist *glist,
419 int xpix, int ypix, int shift, int alt, int dbl, int doit)
420 {
421 t_probalizer* x = (t_probalizer *)z;
422 t_canvas *canvas=glist_getcanvas(glist);
423 t_int nevent, npix;
424
425 if ( doit)
426 {
427 int nevent;
428 int newvalue;
429
430 nevent = ((float)( xpix - text_xpix(&x->x_obj, glist) ))/((float)x->x_width/(float)x->x_nvalues);
431 newvalue = ((float)(text_ypix(&x->x_obj, glist) + x->x_height - ypix))/( (float)x->x_height/(float)x->x_noccurrences);
432
433 // post( "changed %d to %d", nevent, newvalue );
434
435 // consistency check
436 if ( nevent < 0 )
437 {
438 post( "probalizer : warning : negative event : %d", nevent );
439 nevent = 0;
440 }
441 if ( nevent > x->x_nvalues - 1 )
442 {
443 post( "probalizer : warning : event bigger than number of values: %d", nevent );
444 nevent = x->x_nvalues - 1 ;
445 }
446 if ( newvalue < 0 )
447 {
448 post( "probalizer : warning : negative value : %d", newvalue );
449 newvalue = 0;
450 }
451 if ( newvalue > x->x_noccurrences )
452 {
453 post( "probalizer : warning : value bigger than max occurrences : %d", newvalue );
454 newvalue = x->x_noccurrences ;
455 }
456
457 *( x->x_probs + nevent ) = newvalue;
458 // reset all counters
459 memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) );
460 probalizer_draw_update( x );
461 }
462 return (1);
463 }
464
465 static t_probalizer *probalizer_new(t_symbol *s, int argc, t_atom *argv)
466 {
467 int bi, i, ei;
468 t_probalizer *x;
469 t_pd *x2;
470
471 // post( "probalizer_new : create : %s argc =%d", s->s_name, argc );
472
473 x = (t_probalizer *)pd_new(probalizer_class);
474 // create float output
475 outlet_new(&x->x_obj, &s_float);
476 // create bang output
477 x->x_endoutlet = outlet_new(&x->x_obj, &s_bang);
478
479 // new probalizer created from the gui
480 if ( argc != 0 )
481 {
482 if ( argc < 5 )
483 {
484 post( "probalizer : error in the number of arguments ( %d )", argc );
485 return NULL;
486 }
487 if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT ||
488 argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
489 argv[4].a_type != A_FLOAT )
490 {
491 post( "probalizer : wrong arguments" );
492 return NULL;
493 }
494
495 x->x_width = argv[0].a_w.w_float;
496 x->x_height = argv[1].a_w.w_float;
497 x->x_nvalues = argv[2].a_w.w_float;
498 if ( x->x_nvalues < 1 ) x->x_nvalues = 1;
499 x->x_noccurrences = argv[3].a_w.w_float;
500 if ( x->x_noccurrences < 1 ) x->x_noccurrences = 1;
501 x->x_save = argv[4].a_w.w_float;
502 }
503 else
504 {
505 x->x_width = DEFAULT_PROBALIZER_WIDTH;
506 x->x_height = DEFAULT_PROBALIZER_HEIGHT;
507 x->x_nvalues = DEFAULT_PROBALIZER_NBVALUES;
508 x->x_noccurrences = DEFAULT_PROBALIZER_NBOCCURRENCES;
509 x->x_save = 1;
510 }
511 // common fields for new and restored probalizers
512 x->x_probs = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) );
513 if ( !x->x_probs )
514 {
515 error( "probalizer : could not allocate buffer" );
516 return NULL;
517 }
518 x->x_ovalues = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) );
519 if ( !x->x_ovalues )
520 {
521 error( "probalizer : could not allocate buffer" );
522 return NULL;
523 }
524 for ( ei=0; ei<x->x_nvalues; ei++ )
525 {
526 *(x->x_probs+ei)=DEFAULT_PROB_VALUE;
527 }
528 memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) );
529 x->x_selected = 0;
530 x->x_glist = (t_glist *) canvas_getcurrent();
531
532 // post( "probalizer : argc : %d", argc );
533 if ( ( argc != 0 ) && ( x->x_save ) )
534 {
535 int ai = 5;
536 int si = 0;
537
538 while ( ai < argc - 1 )
539 {
540 *(x->x_probs + (int)argv[ai].a_w.w_float) = (int)argv[ai+1].a_w.w_float;
541 ai += 2;
542 }
543 }
544 // post( "probalizer_new width: %d height : %d", x->x_width, x->x_height );
545
546 return (x);
547 }
548
549 /* zero counters */
550 static void probalizer_zero(t_probalizer *x)
551 {
552 t_int ei;
553
554 for ( ei=0; ei<x->x_nvalues; ei++ )
555 {
556 *(x->x_probs+ei)=0;
557 }
558 memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) );
559 probalizer_draw_update( x );
560 }
561
562 /* the core of it, output an integer value out of the serial */
563 static void probalizer_bang(t_probalizer *x)
564 {
565 t_int ei, ci;
566 t_int *candidates;
567 t_int nbcandidates = 0;
568 t_int nevalues=0;
569
570 // calculate number of eligible values
571 for ( ei=0; ei<x->x_nvalues; ei++ )
572 {
573 nevalues += ( *(x->x_probs+ei) - *(x->x_ovalues+ei) );
574 }
575
576 if ( nevalues == 0 )
577 {
578 error( "probalizer : probabilities are null, sorry" );
579 return;
580 }
581
582 candidates = ( t_int* ) getbytes( nevalues*sizeof( t_int ) );
583 if ( !candidates )
584 {
585 error( "probalizer : could not allocate buffer for internal computation" );
586 return;
587 }
588
589 // select eligible values
590 for ( ei=0; ei<x->x_nvalues; ei++ )
591 {
592 if( *(x->x_ovalues+ei) < *(x->x_probs+ei) )
593 {
594 for ( ci=0; ci<*(x->x_probs+ei) - *(x->x_ovalues+ei); ci++)
595 {
596 *(candidates+nbcandidates) = ei;
597 nbcandidates++;
598 }
599 }
600 }
601
602 // output one of the values
603 {
604 int chosen = random() % nbcandidates;
605 int volue = *(candidates+chosen);
606
607 outlet_float( x->x_obj.ob_outlet, volue+1 );
608 *(x->x_ovalues+volue) = *(x->x_ovalues+volue) + 1;
609 // for ( ei=0; ei<nbcandidates; ei++ )
610 // {
611 // post( "probalizer : output : %d", *(x->x_ovalues+*(candidates+ei)) );
612 // }
613
614 // test end of the serial
615 if (nbcandidates == 1 )
616 {
617 // post( "probalizer : end of the serial" );
618 outlet_bang( x->x_endoutlet );
619 memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) );
620 }
621 }
622
623 if ( candidates )
624 {
625 freebytes( candidates, nevalues*sizeof( t_int ) );
626 }
627 }
628
629 /* increase a probability if possible */
630 static void probalizer_float(t_probalizer *x, t_float fvalue)
631 {
632 if ( ( (fvalue-1 ) < 0 ) || ( (fvalue-1 ) >= x->x_nvalues ) )
633 {
634 post( "probalizer : wrong float value : %d", (int)fvalue );
635 return;
636 }
637 if ( ( *(x->x_probs+(int)(fvalue-1))+1 ) <= x->x_noccurrences )
638 {
639 *(x->x_probs+(int)(fvalue-1))+=1;
640 probalizer_draw_update( x );
641 }
642 }
643
644 /* reset output */
645 static void probalizer_reset(t_probalizer *x)
646 {
647 memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) );
648 }
649
650 /* equi probability */
651 static void probalizer_equi(t_probalizer *x)
652 {
653 t_int ei;
654
655 for ( ei=0; ei<x->x_nvalues; ei++ )
656 {
657 *(x->x_probs+ei)=1;
658 }
659 memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) );
660 probalizer_draw_update( x );
661 }
662
663 static void probalizer_free(t_probalizer *x)
664 {
665 t_int ei;
666
667 if ( x->x_probs )
668 {
669 freebytes( x->x_probs, x->x_nvalues*sizeof(int) );
670 }
671 if ( x->x_ovalues )
672 {
673 freebytes( x->x_ovalues, x->x_nvalues*sizeof(int) );
674 }
675 }
676
677 void probalizer_setup(void)
678 {
679 logpost(NULL, 4, probalizer_version );
680 probalizer_class = class_new(gensym("probalizer"), (t_newmethod)probalizer_new,
681 (t_method)probalizer_free, sizeof(t_probalizer), 0, A_GIMME, 0);
682 class_addmethod(probalizer_class, (t_method)probalizer_dialog, gensym("dialog"), A_GIMME, 0);
683 class_addmethod(probalizer_class, (t_method)probalizer_float, &s_float, A_FLOAT, 0);
684 class_addmethod(probalizer_class, (t_method)probalizer_bang, &s_bang, 0);
685 class_addmethod(probalizer_class, (t_method)probalizer_zero, gensym("zero"), 0);
686 class_addmethod(probalizer_class, (t_method)probalizer_equi, gensym("equi"), 0);
687 class_addmethod(probalizer_class, (t_method)probalizer_reset, gensym("reset"), 0);
688 probalizer_widgetbehavior.w_getrectfn = probalizer_getrect;
689 probalizer_widgetbehavior.w_displacefn = probalizer_displace;
690 probalizer_widgetbehavior.w_selectfn = probalizer_select;
691 probalizer_widgetbehavior.w_activatefn = NULL;
692 probalizer_widgetbehavior.w_deletefn = probalizer_delete;
693 probalizer_widgetbehavior.w_visfn = probalizer_vis;
694 probalizer_widgetbehavior.w_clickfn = probalizer_click;
695
696 #if PD_MINOR_VERSION >= 37
697 class_setpropertiesfn(probalizer_class, probalizer_properties);
698 class_setsavefn(probalizer_class, probalizer_save);
699 #else
700 probalizer_widgetbehavior.w_propertiesfn = probalizer_properties;
701 probalizer_widgetbehavior.w_savefn = probalizer_save;
702 #endif
703
704 class_setwidget(probalizer_class, &probalizer_widgetbehavior);
705
706
707 sys_vgui("eval [read [open {%s/%s.tcl}]]\n",
708 probalizer_class->c_externdir->s_name,
709 probalizer_class->c_name->s_name);
710 }