From 42750d74dc03be5fc10e56f3c534e854680c7d17 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Sat, 16 Aug 2003 08:42:27 +0000 Subject: added files straight from Yves' sources svn path=/trunk/externals/unauthorized/; revision=859 --- probalizer/CHANGES.LOG | 2 + probalizer/INSTALL | 14 + probalizer/README | 20 ++ probalizer/help-probalizer.pd | 53 ++++ probalizer/probalizer.c | 693 ++++++++++++++++++++++++++++++++++++++++++ probalizer/probalizer.h | 26 ++ probalizer/probalizer.tk | 113 +++++++ 7 files changed, 921 insertions(+) create mode 100644 probalizer/CHANGES.LOG create mode 100644 probalizer/INSTALL create mode 100644 probalizer/README create mode 100644 probalizer/help-probalizer.pd create mode 100644 probalizer/probalizer.c create mode 100644 probalizer/probalizer.h create mode 100644 probalizer/probalizer.tk (limited to 'probalizer') diff --git a/probalizer/CHANGES.LOG b/probalizer/CHANGES.LOG new file mode 100644 index 0000000..6f7036d --- /dev/null +++ b/probalizer/CHANGES.LOG @@ -0,0 +1,2 @@ +0.1 + first implementation diff --git a/probalizer/INSTALL b/probalizer/INSTALL new file mode 100644 index 0000000..17ecb34 --- /dev/null +++ b/probalizer/INSTALL @@ -0,0 +1,14 @@ +untar in /my/pd/dir/externs + +cd /my/pd/dir/externs/probalizer + +make clean + +make + +make install + +open help-probalizer.pd + +comments and bugs @ ydegoyon@free.fr +Yves/ diff --git a/probalizer/README b/probalizer/README new file mode 100644 index 0000000..aec5a49 --- /dev/null +++ b/probalizer/README @@ -0,0 +1,20 @@ +***************************************************************************** +Version 0.01 +copyright 2002 by Yves Degoyon. + +tarballs and updates available @ http://ydegoyon.free.fr + +probalizer : outputs integer values according to a drawn probability curve + +To install probalizer, follow the steps from INSTALL + +This software is published under GPL terms. + +This is software with ABSOLUTELY NO WARRANTY. +Use it at your OWN RISK. It's possible to damage e.g. hardware or your hearing +due to a bug or for other reasons. +We do not warrant that the program is free of infringement of any third-party +patents. + +***************************************************************************** + diff --git a/probalizer/help-probalizer.pd b/probalizer/help-probalizer.pd new file mode 100644 index 0000000..b3baa89 --- /dev/null +++ b/probalizer/help-probalizer.pd @@ -0,0 +1,53 @@ +#N canvas 191 -6 970 685 10; +#X text 374 167 (invoke with Properties ); +#X text 375 156 You can set the following properties :; +#X text 395 188 * Width : graphical x size; +#X text 395 200 * Height : graphical y size; +#X text 442 506 bugs and comments @ ydegoyon@free.fr; +#X msg 404 603 \; pd dsp 0; +#X msg 470 603 \; pd dsp 1; +#X text 376 110 Probalizer output serial of integers according to the +probability curve; +#X obj 470 581 loadbang; +#X obj 64 92 probalizer 200 200 10 100 1 0 42 1 66 2 86 3 67 4 36 5 +40 6 2 7 53 8 63 9 14; +#X text 395 213 * Values : number of values ( range is [ 1 \, nvalues +] ); +#X msg 93 54 bang; +#X msg 93 4 bang; +#X text 389 458 but without repetitions; +#X msg 295 16 zero; +#X msg 294 40 equi; +#X text 389 433 Note = If the probabilities are all equal to 1 \,; +#X msg 293 64 reset; +#X text 338 63 Reset output; +#X text 396 227 * Max Occurrences : max number of occurences in a serial +; +#X obj 68 358 probalizer 200 200 10 100 1 0 0 1 0 2 0 3 0 4 0 5 0 6 +0 7 0 8 0 9 0; +#X obj 193 331 loadbang; +#X msg 145 331 zero; +#X text 306 345 The output of first probalizer; +#X text 306 357 is stored in the second one; +#X floatatom 187 30 5 0 0; +#X msg 187 5 bang; +#X text 276 281 End of the serial; +#X text 390 445 probalizer behaves as a random object; +#X obj 276 308 print probalizer::end; +#X text 47 6 Start; +#X text 332 42 Set equal probabilities; +#X obj 93 29 metro 10; +#X connect 8 0 6 0; +#X connect 9 0 20 0; +#X connect 9 1 22 0; +#X connect 9 1 29 0; +#X connect 11 0 9 0; +#X connect 12 0 32 0; +#X connect 14 0 9 0; +#X connect 15 0 9 0; +#X connect 17 0 9 0; +#X connect 21 0 22 0; +#X connect 22 0 20 0; +#X connect 25 0 9 0; +#X connect 26 0 25 0; +#X connect 32 0 11 0; diff --git a/probalizer/probalizer.c b/probalizer/probalizer.c new file mode 100644 index 0000000..b145a86 --- /dev/null +++ b/probalizer/probalizer.c @@ -0,0 +1,693 @@ +/* Copyright (c) 2002 Yves Degoyon. */ +/* For information on usage and redistribution, and for a DISCLAIMER OF ALL */ +/* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ +/* */ +/* probalizer : outputs integer values according to a drawn probability curve */ +/* */ +/* This program is free software; you can redistribute it and/or */ +/* modify it under the terms of the GNU General Public License */ +/* as published by the Free Software Foundation; either version 2 */ +/* of the License, or (at your option) any later version. */ +/* */ +/* See file LICENSE for further informations on licensing terms. */ +/* */ +/* This program is distributed in the hope that it will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU General Public License for more details. */ +/* */ +/* You should have received a copy of the GNU General Public License */ +/* along with this program; if not, write to the Free Software */ +/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/* */ +/* Based on PureData by Miller Puckette and others. */ +/* */ +/* Made while listening to : */ +/* */ +/* "I am a product, I am a sample" */ +/* "I'm hopeless, aimless" */ +/* Crass -- Feeding of the 5000 */ +/* ---------------------------------------------------------------------------- */ + + +#include +#include +#include +#include +#include +#include "m_imp.h" +#include "g_canvas.h" +#include "t_tk.h" +#include "probalizer.h" + +#ifdef NT +#include +#else +#include +#endif + +#define DEFAULT_PROBALIZER_WIDTH 200 +#define DEFAULT_PROBALIZER_HEIGHT 200 +#define DEFAULT_PROBALIZER_NBVALUES 100 +#define DEFAULT_PROBALIZER_NBOCCURRENCES 100 +#define DEFAULT_PROB_VALUE 10 + +static char *probalizer_version = "probalizer : outputs integer values according to a drawn probability curve , version 0.1 (ydegoyon@free.fr)"; + +t_widgetbehavior probalizer_widgetbehavior; +static t_class *probalizer_class; + +static int guidebug=0; +static int pointsize = 5; + +#define SYS_VGUI2(a,b) if (guidebug) \ + post(a,b);\ + sys_vgui(a,b) + +#define SYS_VGUI3(a,b,c) if (guidebug) \ + post(a,b,c);\ + sys_vgui(a,b,c) + +#define SYS_VGUI4(a,b,c,d) if (guidebug) \ + post(a,b,c,d);\ + sys_vgui(a,b,c,d) + +#define SYS_VGUI5(a,b,c,d,e) if (guidebug) \ + post(a,b,c,d,e);\ + sys_vgui(a,b,c,d,e) + +#define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \ + post(a,b,c,d,e,f);\ + sys_vgui(a,b,c,d,e,f) + +#define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \ + post(a,b,c,d,e,f,g);\ + sys_vgui(a,b,c,d,e,f,g) + +#define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \ + post(a,b,c,d,e,f,g,h);\ + sys_vgui(a,b,c,d,e,f,g,h) + +#define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \ + post(a,b,c,d,e,f,g,h,i);\ + sys_vgui(a,b,c,d,e,f,g,h,i) + +#define SYS_VGUI13(a,b,c,d,e,f,g,h,i,j,k,l,m) if (guidebug) \ + post(a,b,c,d,e,f,g,h,i,j,k,l,m);\ + sys_vgui(a,b,c,d,e,f,g,h,i,j,k,l,m) + +/* drawing functions */ +static void probalizer_draw_new(t_probalizer *x, t_glist *glist) +{ + t_canvas *canvas=glist_getcanvas(glist); + t_int ei; + + SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -outline #000000 -fill #6790E2 -tags %xPROBALIZER\n", + canvas, x->x_obj.te_xpix, x->x_obj.te_ypix, + x->x_obj.te_xpix+x->x_width, x->x_obj.te_ypix+x->x_height, + x); + SYS_VGUI5(".x%x.c create text %d %d -font -*-courier-bold--normal--10-* -text \"0\" -tags %xLTCAPTION\n", + canvas, x->x_obj.te_xpix-15, x->x_obj.te_ypix + x->x_height, x ); + SYS_VGUI6(".x%x.c create text %d %d -font -*-courier-bold--normal--10-* -text \"%d\" -tags %xLBCAPTION\n", + canvas, x->x_obj.te_xpix-15, x->x_obj.te_ypix, x->x_noccurrences, x ); + SYS_VGUI5(".x%x.c create text %d %d -font -*-courier-bold--normal--10-* -text \"1\" -tags %xBLCAPTION\n", + canvas, x->x_obj.te_xpix+2, x->x_obj.te_ypix + x->x_height + 10, x ); + SYS_VGUI6(".x%x.c create text %d %d -font -*-courier-bold--normal--10-* -text \"%d\" -tags %xBRCAPTION\n", + canvas, x->x_obj.te_xpix + x->x_width-5, x->x_obj.te_ypix + x->x_height + 10, x->x_nvalues, x ); + SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xIN\n", + canvas, x->x_obj.te_xpix, + x->x_obj.te_ypix - 2, + x->x_obj.te_xpix + 5, + x->x_obj.te_ypix , + x); + SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xOUT\n", + canvas, x->x_obj.te_xpix, + x->x_obj.te_ypix + x->x_height, + x->x_obj.te_xpix + 5, + x->x_obj.te_ypix + x->x_height + 2, + x); + SYS_VGUI7(".x%x.c create rectangle %d %d %d %d -outline #000000 -fill #000000 -tags %xOUT2\n", + canvas, x->x_obj.te_xpix + x->x_width -5, + x->x_obj.te_ypix + x->x_height, + x->x_obj.te_xpix + x->x_width, + x->x_obj.te_ypix + x->x_height + 2, + x); + for ( ei=0; eix_nvalues; ei++ ) + { + SYS_VGUI8(".x%x.c create rectangle %d %d %d %d -outline #000000 -fill #118373 -tags %xPROB%d\n", + canvas, + x->x_obj.te_xpix + ei * x->x_width/x->x_nvalues, + x->x_obj.te_ypix + x->x_height - ( *(x->x_probs+ei) * x->x_height / x->x_noccurrences ), + x->x_obj.te_xpix + (ei+1) * x->x_width/x->x_nvalues, + x->x_obj.te_ypix + x->x_height, + x, ei); + } + canvas_fixlinesfor( canvas, (t_text*)x ); +} + +static void probalizer_draw_update(t_probalizer *x) +{ + t_canvas *canvas=glist_getcanvas(x->x_glist); + t_int ei; + + for ( ei=0; eix_nvalues; ei++ ) + { + SYS_VGUI8(".x%x.c coords %xPROB%d %d %d %d %d\n", + canvas, x, ei, + x->x_obj.te_xpix + ei * x->x_width / x->x_nvalues, + x->x_obj.te_ypix + x->x_height - ( *(x->x_probs+ei) * x->x_height / x->x_noccurrences ), + x->x_obj.te_xpix + (ei+1) * x->x_width / x->x_nvalues, + x->x_obj.te_ypix + x->x_height ); + } + canvas_fixlinesfor( canvas, (t_text*)x ); +} + +static void probalizer_draw_move(t_probalizer *x, t_glist *glist) +{ + t_canvas *canvas=glist_getcanvas(glist); + t_int ei; + + SYS_VGUI7(".x%x.c coords %xPROBALIZER %d %d %d %d \n", + canvas, x, x->x_obj.te_xpix, x->x_obj.te_ypix, + x->x_obj.te_xpix+x->x_width, x->x_obj.te_ypix+x->x_height + ); + SYS_VGUI7(".x%x.c coords %xIN %d %d %d %d\n", + canvas, x, x->x_obj.te_xpix, + x->x_obj.te_ypix - 2, + x->x_obj.te_xpix + 5, + x->x_obj.te_ypix + ); + SYS_VGUI7(".x%x.c coords %xOUT %d %d %d %d\n", + canvas, x, x->x_obj.te_xpix, + x->x_obj.te_ypix + x->x_height, + x->x_obj.te_xpix + 5, + x->x_obj.te_ypix + x->x_height + 2 + ); + SYS_VGUI7(".x%x.c coords %xOUT2 %d %d %d %d\n", + canvas, x, x->x_obj.te_xpix + x->x_width - 5, + x->x_obj.te_ypix + x->x_height, + x->x_obj.te_xpix + x->x_width, + x->x_obj.te_ypix + x->x_height + 2 + ); + SYS_VGUI5(".x%x.c coords %xLTCAPTION %d %d\n", + canvas, x, x->x_obj.te_xpix-15, + x->x_obj.te_ypix + x->x_height + ); + SYS_VGUI5(".x%x.c coords %xLBCAPTION %d %d\n", + canvas, x, x->x_obj.te_xpix-15, + x->x_obj.te_ypix + ); + SYS_VGUI5(".x%x.c coords %xBLCAPTION %d %d\n", + canvas, x, x->x_obj.te_xpix+2, + x->x_obj.te_ypix + x->x_height + 10 + ); + SYS_VGUI5(".x%x.c coords %xBRCAPTION %d %d\n", + canvas, x, x->x_obj.te_xpix + x->x_width - 5, + x->x_obj.te_ypix + x->x_height + 10 + ); + for ( ei=0; eix_nvalues; ei++ ) + { + SYS_VGUI8(".x%x.c coords %xPROB%d %d %d %d %d\n", + canvas, x, ei, + x->x_obj.te_xpix + ei * x->x_width / x->x_nvalues, + x->x_obj.te_ypix + x->x_height - ( *(x->x_probs+ei) * x->x_height / x->x_noccurrences ), + x->x_obj.te_xpix + (ei+1) * x->x_width / x->x_nvalues, + x->x_obj.te_ypix + x->x_height ); + } + canvas_fixlinesfor( canvas, (t_text*)x ); +} + +static void probalizer_draw_erase(t_probalizer* x,t_glist* glist) +{ + t_canvas *canvas=glist_getcanvas(glist); + int i, ei, gi; + + SYS_VGUI3(".x%x.c delete %xPROBALIZER\n", canvas, x ); + SYS_VGUI3(".x%x.c delete %xIN\n", canvas, x ); + SYS_VGUI3(".x%x.c delete %xOUT\n", canvas, x ); + SYS_VGUI3(".x%x.c delete %xOUT2\n", canvas, x ); + SYS_VGUI3(".x%x.c delete %xLTCAPTION\n", canvas, x ); + SYS_VGUI3(".x%x.c delete %xLBCAPTION\n", canvas, x ); + SYS_VGUI3(".x%x.c delete %xBLCAPTION\n", canvas, x ); + SYS_VGUI3(".x%x.c delete %xBRCAPTION\n", canvas, x ); + for ( ei=0; eix_nvalues; ei++ ) + { + SYS_VGUI4(".x%x.c delete %xPROB%d\n", canvas, x, ei ); + } +} + +static void probalizer_draw_select(t_probalizer* x,t_glist* glist) +{ + t_canvas *canvas=glist_getcanvas(glist); + + if(x->x_selected) + { + /* sets the main item in blue */ + SYS_VGUI3(".x%x.c itemconfigure %xPROBALIZER -outline #0000FF\n", glist_getcanvas(glist), x); + + } + else + { + /* sets the main item in black */ + SYS_VGUI3(".x%x.c itemconfigure %xPROBALIZER -outline #000000\n", glist_getcanvas(glist), x); + } +} + +/* ------------------------ probalizer widgetbehaviour----------------------------- */ + + +static void probalizer_getrect(t_gobj *z, t_glist *owner, + int *xp1, int *yp1, int *xp2, int *yp2) +{ + t_probalizer* x = (t_probalizer*)z; + + *xp1 = x->x_obj.te_xpix; + *yp1 = x->x_obj.te_ypix; + *xp2 = x->x_obj.te_xpix+x->x_width; + *yp2 = x->x_obj.te_ypix+x->x_height; +} + +static void probalizer_save(t_gobj *z, t_binbuf *b) +{ + t_probalizer *x = (t_probalizer *)z; + int ei,gi; + + binbuf_addv(b, "ssiisiiiii", gensym("#X"),gensym("obj"), + (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix, + gensym("probalizer"), x->x_width, x->x_height, + x->x_nvalues, x->x_noccurrences, x->x_save ); + if ( x->x_save ) + { + for ( ei=0; eix_nvalues; ei++ ) + { + // post( "probalizer : saving ( %d, %d )", ei, gi ); + binbuf_addv(b, "ii", ei, *(x->x_probs+ei) ); + } + } + binbuf_addv(b, ";"); +} + +static void probalizer_properties(t_gobj *z, t_glist *owner) +{ + char buf[800]; + t_probalizer *x=(t_probalizer *)z; + + sprintf(buf, "pdtk_probalizer_dialog %%s %d %d %d %d %d\n", + x->x_width, x->x_height, x->x_nvalues, x->x_noccurrences, x->x_save ); + // post("probalizer_properties : %s", buf ); + gfxstub_new(&x->x_obj.ob_pd, x, buf); +} + +static void probalizer_select(t_gobj *z, t_glist *glist, int selected) +{ + t_probalizer *x = (t_probalizer *)z; + + x->x_selected = selected; + probalizer_draw_select( x, glist ); +} + +static void probalizer_vis(t_gobj *z, t_glist *glist, int vis) +{ + t_probalizer *x = (t_probalizer *)z; + t_rtext *y; + + // post("probalizer_vis : %d", vis ); + if (vis) + { + probalizer_draw_new( x, glist ); + } + else + { + probalizer_draw_erase( x, glist ); + } +} + + /* handle parameters sent by the property dialog */ +static void probalizer_dialog(t_probalizer *x, t_symbol *s, int argc, t_atom *argv) +{ + t_int onvalues, owidth, ei, gi, bi; + t_canvas *canvas=glist_getcanvas(x->x_glist); + + if ( !x ) { + post( "probalizer : error :tried to set properties on an unexisting object" ); + } + onvalues = x->x_nvalues; + owidth = x->x_width; + probalizer_draw_erase(x, x->x_glist); + if ( argc < 5 ) + { + post( "probalizer : error in the number of arguments ( %d )", argc ); + return; + } + if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT || + argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT || + argv[4].a_type != A_FLOAT ) + { + post( "probalizer : wrong arguments" ); + return; + } + + x->x_width = argv[0].a_w.w_float; + if ( x->x_width <= 0 ) x->x_width = 100; + x->x_height = argv[1].a_w.w_float; + if ( x->x_height <= 0 ) x->x_height = 100; + x->x_nvalues = argv[2].a_w.w_float; + if ( x->x_nvalues < 1 ) x->x_nvalues = 1; + x->x_noccurrences = argv[3].a_w.w_float; + if ( x->x_noccurrences < 1 ) x->x_noccurrences = 1; + x->x_save = argv[4].a_w.w_float; + + // re-allocate arrays of values + post( "probalizer : re-allocate values" ); + if ( onvalues != x->x_nvalues ) + { + t_int mvalues = ( onvalues > x->x_nvalues ) ? x->x_nvalues : onvalues; + t_int *newprobs, *newovalues; + + newprobs = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) ); + newovalues = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) ); + for ( ei=0; eix_nvalues; ei++ ) + { + *(newprobs+ei)=DEFAULT_PROB_VALUE; + } + memset( newovalues, 0x0, x->x_nvalues*sizeof(t_int) ); + for ( ei=0; eix_probs+ei); + } + if ( x->x_probs ) + freebytes( x->x_probs, onvalues*sizeof(t_int) ); + x->x_probs = newprobs; + if ( x->x_ovalues ) + freebytes( x->x_ovalues, onvalues*sizeof(t_int) ); + x->x_ovalues = newovalues; + } + + probalizer_draw_new(x, x->x_glist); + canvas_fixlinesfor( canvas, (t_text*)x ); +} + +static void probalizer_delete(t_gobj *z, t_glist *glist) +{ + canvas_deletelinesfor( glist_getcanvas(glist), (t_text *)z); +} + +static void probalizer_displace(t_gobj *z, t_glist *glist, int dx, int dy) +{ + t_probalizer *x = (t_probalizer *)z; + int xold = x->x_obj.te_xpix; + int yold = x->x_obj.te_ypix; + + // post( "probalizer_displace dx=%d dy=%d", dx, dy ); + + x->x_obj.te_xpix += dx; + x->x_obj.te_ypix += dy; + if(xold != x->x_obj.te_xpix || yold != x->x_obj.te_ypix) + { + probalizer_draw_move(x, x->x_glist); + } +} + +static int probalizer_click(t_gobj *z, struct _glist *glist, + int xpix, int ypix, int shift, int alt, int dbl, int doit) +{ + t_probalizer* x = (t_probalizer *)z; + t_canvas *canvas=glist_getcanvas(glist); + t_int nevent, npix; + + if ( doit) + { + int nevent; + int newvalue; + + nevent = ((float)( xpix - x->x_obj.te_xpix ))/((float)x->x_width/(float)x->x_nvalues); + newvalue = ((float)(x->x_obj.te_ypix + x->x_height - ypix))/( (float)x->x_height/(float)x->x_noccurrences); + + // post( "changed %d to %d", nevent, newvalue ); + + // consistency check + if ( nevent < 0 ) + { + post( "probalizer : warning : negative event : %d", nevent ); + nevent = 0; + } + if ( nevent > x->x_nvalues - 1 ) + { + post( "probalizer : warning : event bigger than number of values: %d", nevent ); + nevent = x->x_nvalues - 1 ; + } + if ( newvalue < 0 ) + { + post( "probalizer : warning : negative value : %d", newvalue ); + newvalue = 0; + } + if ( newvalue > x->x_noccurrences ) + { + post( "probalizer : warning : value bigger than max occurrences : %d", newvalue ); + newvalue = x->x_noccurrences ; + } + + *( x->x_probs + nevent ) = newvalue; + // reset all counters + memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) ); + probalizer_draw_update( x ); + } + return (1); +} + +static t_probalizer *probalizer_new(t_symbol *s, int argc, t_atom *argv) +{ + int bi, i, ei; + t_probalizer *x; + t_pd *x2; + + // post( "probalizer_new : create : %s argc =%d", s->s_name, argc ); + + x = (t_probalizer *)pd_new(probalizer_class); + // create float output + outlet_new(&x->x_obj, &s_float); + // create bang output + x->x_endoutlet = outlet_new(&x->x_obj, &s_bang); + + // new probalizer created from the gui + if ( argc != 0 ) + { + if ( argc < 5 ) + { + post( "probalizer : error in the number of arguments ( %d )", argc ); + return NULL; + } + if ( argv[0].a_type != A_FLOAT || argv[1].a_type != A_FLOAT || + argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT || + argv[4].a_type != A_FLOAT ) + { + post( "probalizer : wrong arguments" ); + return NULL; + } + + x->x_width = argv[0].a_w.w_float; + x->x_height = argv[1].a_w.w_float; + x->x_nvalues = argv[2].a_w.w_float; + if ( x->x_nvalues < 1 ) x->x_nvalues = 1; + x->x_noccurrences = argv[3].a_w.w_float; + if ( x->x_noccurrences < 1 ) x->x_noccurrences = 1; + x->x_save = argv[4].a_w.w_float; + } + else + { + x->x_width = DEFAULT_PROBALIZER_WIDTH; + x->x_height = DEFAULT_PROBALIZER_HEIGHT; + x->x_nvalues = DEFAULT_PROBALIZER_NBVALUES; + x->x_noccurrences = DEFAULT_PROBALIZER_NBOCCURRENCES; + x->x_save = 1; + } + // common fields for new and restored probalizers + x->x_probs = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) ); + if ( !x->x_probs ) + { + error( "probalizer : could not allocate buffer" ); + return NULL; + } + x->x_ovalues = (t_int*) getbytes( x->x_nvalues*sizeof(t_int) ); + if ( !x->x_ovalues ) + { + error( "probalizer : could not allocate buffer" ); + return NULL; + } + for ( ei=0; eix_nvalues; ei++ ) + { + *(x->x_probs+ei)=DEFAULT_PROB_VALUE; + } + memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) ); + x->x_selected = 0; + x->x_glist = (t_glist *) canvas_getcurrent(); + + // post( "probalizer : argc : %d", argc ); + if ( ( argc != 0 ) && ( x->x_save ) ) + { + int ai = 5; + int si = 0; + + while ( ai < argc - 1 ) + { + *(x->x_probs + (int)argv[ai].a_w.w_float) = (int)argv[ai+1].a_w.w_float; + ai += 2; + } + } + // post( "probalizer_new width: %d height : %d", x->x_width, x->x_height ); + + return (x); +} + +/* zero counters */ +static void probalizer_zero(t_probalizer *x) +{ + t_int ei; + + for ( ei=0; eix_nvalues; ei++ ) + { + *(x->x_probs+ei)=0; + } + memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) ); + probalizer_draw_update( x ); +} + +/* the core of it, output an integer value out of the serial */ +static void probalizer_bang(t_probalizer *x) +{ + t_int ei, ci; + t_int *candidates; + t_int nbcandidates = 0; + t_int nevalues=0; + + // calculate number of eligible values + for ( ei=0; eix_nvalues; ei++ ) + { + nevalues += ( *(x->x_probs+ei) - *(x->x_ovalues+ei) ); + } + + if ( nevalues == 0 ) + { + error( "probalizer : probabilities are null, sorry" ); + return; + } + + candidates = ( t_int* ) getbytes( nevalues*sizeof( t_int ) ); + if ( !candidates ) + { + error( "probalizer : could not allocate buffer for internal computation" ); + return; + } + + // select eligible values + for ( ei=0; eix_nvalues; ei++ ) + { + if( *(x->x_ovalues+ei) < *(x->x_probs+ei) ) + { + for ( ci=0; ci<*(x->x_probs+ei) - *(x->x_ovalues+ei); ci++) + { + *(candidates+nbcandidates) = ei; + nbcandidates++; + } + } + } + + // output one of the values + { + int chosen = random() % nbcandidates; + int volue = *(candidates+chosen); + + outlet_float( x->x_obj.ob_outlet, volue+1 ); + *(x->x_ovalues+volue) = *(x->x_ovalues+volue) + 1; + // for ( ei=0; eix_ovalues+*(candidates+ei)) ); + // } + + // test end of the serial + if (nbcandidates == 1 ) + { + // post( "probalizer : end of the serial" ); + outlet_bang( x->x_endoutlet ); + memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) ); + } + } + + if ( candidates ) + { + freebytes( candidates, nevalues*sizeof( t_int ) ); + } +} + +/* increase a probability if possible */ +static void probalizer_float(t_probalizer *x, t_float fvalue) +{ + if ( ( (fvalue-1 ) < 0 ) || ( (fvalue-1 ) >= x->x_nvalues ) ) + { + post( "probalizer : wrong float value : %d", (int)fvalue ); + return; + } + if ( ( *(x->x_probs+(int)(fvalue-1))+1 ) <= x->x_noccurrences ) + { + *(x->x_probs+(int)(fvalue-1))+=1; + probalizer_draw_update( x ); + } +} + +/* reset output */ +static void probalizer_reset(t_probalizer *x) +{ + memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) ); +} + +/* equi probability */ +static void probalizer_equi(t_probalizer *x) +{ + t_int ei; + + for ( ei=0; eix_nvalues; ei++ ) + { + *(x->x_probs+ei)=1; + } + memset( x->x_ovalues, 0x0, x->x_nvalues*sizeof(t_int) ); + probalizer_draw_update( x ); +} + +static void probalizer_free(t_probalizer *x) +{ + t_int ei; + + if ( x->x_probs ) + { + freebytes( x->x_probs, x->x_nvalues*sizeof(int) ); + } + if ( x->x_ovalues ) + { + freebytes( x->x_ovalues, x->x_nvalues*sizeof(int) ); + } +} + +void probalizer_setup(void) +{ + post( probalizer_version ); +#include "probalizer.tk2c" + probalizer_class = class_new(gensym("probalizer"), (t_newmethod)probalizer_new, + (t_method)probalizer_free, sizeof(t_probalizer), 0, A_GIMME, 0); + class_addmethod(probalizer_class, (t_method)probalizer_dialog, gensym("dialog"), A_GIMME, 0); + class_addmethod(probalizer_class, (t_method)probalizer_float, &s_float, A_FLOAT, 0); + class_addmethod(probalizer_class, (t_method)probalizer_bang, &s_bang, 0); + class_addmethod(probalizer_class, (t_method)probalizer_zero, gensym("zero"), 0); + class_addmethod(probalizer_class, (t_method)probalizer_equi, gensym("equi"), 0); + class_addmethod(probalizer_class, (t_method)probalizer_reset, gensym("reset"), 0); + probalizer_widgetbehavior.w_getrectfn = probalizer_getrect; + probalizer_widgetbehavior.w_displacefn = probalizer_displace; + probalizer_widgetbehavior.w_selectfn = probalizer_select; + probalizer_widgetbehavior.w_activatefn = NULL; + probalizer_widgetbehavior.w_deletefn = probalizer_delete; + probalizer_widgetbehavior.w_visfn = probalizer_vis; + probalizer_widgetbehavior.w_clickfn = probalizer_click; + probalizer_widgetbehavior.w_propertiesfn = probalizer_properties; + probalizer_widgetbehavior.w_savefn = probalizer_save; + class_setwidget(probalizer_class, &probalizer_widgetbehavior); + class_sethelpsymbol(probalizer_class, gensym("help-probalizer.pd")); +} diff --git a/probalizer/probalizer.h b/probalizer/probalizer.h new file mode 100644 index 0000000..51c9711 --- /dev/null +++ b/probalizer/probalizer.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2002 Yves Degoyon +* For information on usage and redistribution, and for a DISCLAIMER OF ALL +* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +/* probalizer : outputs integer values according to a drawn probability curve +*/ + +#ifndef __G_PROBALIZER_H +#define __G_PROBALIZER_H + +typedef struct _probalizer +{ + t_object x_obj; + t_glist *x_glist; + t_int x_height; /* height of the probalizer */ + t_int x_width; /* width of the probalizer */ + t_int x_nvalues; /* number of values */ + t_int x_noccurrences; /* max number of occurrences in a serial */ + t_int *x_probs; /* probability of each event */ + t_int *x_ovalues; /* number of outputs of each event */ + int x_selected; /* stores selected state */ + int x_save; /* saving contents flag */ + t_outlet *x_endoutlet; /* outlet to signal the end of the serial */ +} t_probalizer; + +#endif diff --git a/probalizer/probalizer.tk b/probalizer/probalizer.tk new file mode 100644 index 0000000..2f069e9 --- /dev/null +++ b/probalizer/probalizer.tk @@ -0,0 +1,113 @@ +############ probalizer procedures -- ydegoyon@free.fr ######### + +proc probalizer_apply {id} { +# strip "." from the TK id to make a variable name suffix + set vid [string trimleft $id .] +# for each variable, make a local variable to hold its name... + set var_graph_width [concat graph_width_$vid] + global $var_graph_width + set var_graph_height [concat graph_height_$vid] + global $var_graph_height + set var_graph_nvalues [concat graph_nvalues_$vid] + global $var_graph_nvalues + set var_graph_noccurrences [concat graph_noccurrences_$vid] + global $var_graph_noccurrences + set var_graph_save [concat graph_save_$vid] + global $var_graph_save + + set cmd [concat $id dialog \ + [eval concat $$var_graph_width] \ + [eval concat $$var_graph_height] \ + [eval concat $$var_graph_nvalues] \ + [eval concat $$var_graph_noccurrences] \ + [eval concat $$var_graph_save] \;] + #puts stderr $cmd + pd $cmd +} + +proc probalizer_cancel {id} { + set cmd [concat $id cancel \;] + #puts stderr $cmd + pd $cmd +} + +proc probalizer_ok {id} { + probalizer_apply $id + probalizer_cancel $id +} + +proc pdtk_probalizer_dialog {id width height nvalues noccurrences save } { + set vid [string trimleft $id .] + + set var_graph_width [concat graph_width_$vid] + global $var_graph_width + set var_graph_height [concat graph_height_$vid] + global $var_graph_height + set var_graph_nvalues [concat graph_nvalues_$vid] + global $var_graph_nvalues + set var_graph_noccurrences [concat graph_noccurrences_$vid] + global $var_graph_noccurrences + set var_graph_save [concat graph_save_$vid] + global $var_graph_save + + set $var_graph_width $width + set $var_graph_height $height + set $var_graph_nvalues $nvalues + set $var_graph_noccurrences $noccurrences + set $var_graph_save $save + + toplevel $id + wm title $id {probalizer} + wm protocol $id WM_DELETE_WINDOW [concat probalizer_cancel $id] + + label $id.label -text {PROBALIZER PROPERTIES} + pack $id.label -side top + + frame $id.buttonframe + pack $id.buttonframe -side bottom -fill x -pady 2m + button $id.buttonframe.cancel -text {Cancel}\ + -command "probalizer_cancel $id" + button $id.buttonframe.apply -text {Apply}\ + -command "probalizer_apply $id" + button $id.buttonframe.ok -text {OK}\ + -command "probalizer_ok $id" + pack $id.buttonframe.cancel -side left -expand 1 + pack $id.buttonframe.apply -side left -expand 1 + pack $id.buttonframe.ok -side left -expand 1 + + frame $id.1rangef + pack $id.1rangef -side top + label $id.1rangef.lwidth -text "Width :" + entry $id.1rangef.width -textvariable $var_graph_width -width 7 + pack $id.1rangef.lwidth $id.1rangef.width -side left + + frame $id.2rangef + pack $id.2rangef -side top + label $id.2rangef.lheight -text "Height :" + entry $id.2rangef.height -textvariable $var_graph_height -width 7 + pack $id.2rangef.lheight $id.2rangef.height -side left + + frame $id.3rangef + pack $id.3rangef -side top + label $id.3rangef.lnvalues -text "Values :" + entry $id.3rangef.nvalues -textvariable $var_graph_nvalues -width 7 + pack $id.3rangef.lnvalues $id.3rangef.nvalues -side left + + frame $id.4rangef + pack $id.4rangef -side top + label $id.4rangef.lnoccurrences -text "Max Occurrences :" + entry $id.4rangef.noccurrences -textvariable $var_graph_noccurrences -width 7 + pack $id.4rangef.lnoccurrences $id.4rangef.noccurrences -side left + + checkbutton $id.save -text {Save contents} -variable $var_graph_save \ + -anchor w + pack $id.save -side top + + bind $id.1rangef.width [concat probalizer_ok $id] + bind $id.2rangef.height [concat probalizer_ok $id] + bind $id.3rangef.nvalues [concat probalizer_ok $id] + bind $id.4rangef.noccurrences [concat probalizer_ok $id] + focus $id.1rangef.width +} + +############ probalizer procedures END -- ydegoyon@free.fr ######### -- cgit v1.2.1