[PD-cvs] pd/extra/choice choice.c, NONE, 1.2.4.2 makefile, NONE, 1.2.8.2

Tim Blechmann timblech at users.sourceforge.net
Sat Jul 16 18:21:00 CEST 2005


Update of /cvsroot/pure-data/pd/extra/choice
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21746/choice

Added Files:
      Tag: devel_0_39
	choice.c makefile 
Log Message:
missing extra files

--- NEW FILE: choice.c ---
/* choice -- match incoming list against a collection of stored templates. */

/*  Copyright 1999 Miller Puckette.
Permission is granted to use this software for any purpose provided you
keep this copyright notice intact.

THE AUTHOR AND HIS EMPLOYERS MAKE NO WARRANTY, EXPRESS OR IMPLIED,
IN CONNECTION WITH THIS SOFTWARE.
 
This file is downloadable from http://www.crca.ucsd.edu/~msp .
*/

#include "m_pd.h"
#include <math.h>
static t_class *choice_class;
#define DIMENSION 10

typedef struct _elem
{
    float e_age;
    float e_weight[DIMENSION];
} t_elem;

typedef struct _choice
{
    t_object x_obj;
    t_elem *x_vec;
    int x_n;
    int x_nonrepeat;
} t_choice;

static void *choice_new(t_float fnonrepeat)
{
    t_choice *x = (t_choice *)pd_new(choice_class);
    outlet_new(&x->x_obj, gensym("float"));
    x->x_vec = (t_elem *)getbytes(0);
    x->x_n = 0;
    x->x_nonrepeat = (fnonrepeat != 0);
    return (x);
}

static void choice_clear(t_choice *x)
{
    x->x_vec = (t_elem *)resizebytes(x->x_vec, x->x_n * sizeof(t_elem), 0);
    x->x_n = 0;
}

static void choice_print(t_choice *x)
{
    int j;
    for (j = 0; j < x->x_n; j++)
    {
        t_elem *e = x->x_vec + j;
        t_float *w = e->e_weight;
        post("%2d age %2d \
w %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f",
            j, (int)(e->e_age), w[0], w[1], w[2], w[3], w[4], w[5],
                w[6], w[7], w[8], w[9]);
    }
}

static void choice_add(t_choice *x, t_symbol *s, int argc, t_atom *argv)
{
    int oldn = x->x_n, newn = oldn + 1, i;
    t_elem *e;
    float sum, normal;
    x->x_vec = (t_elem *)resizebytes(x->x_vec, oldn * sizeof(t_elem),
        newn * sizeof(t_elem));
    x->x_n = newn;
    e = x->x_vec + oldn;
    e->e_age = 2;
    
    for (i = 0, sum = 0; i < DIMENSION; i++)
    {
        float f = atom_getfloatarg(i, argc, argv);
        e->e_weight[i] = f;
        sum += f*f;
    }
    normal = (float)(sum > 0 ? 1./sqrt(sum) : 1);
    for (i = 0; i < DIMENSION; i++)
        e->e_weight[i] *= normal;
}

static void choice_list(t_choice *x, t_symbol *s, int argc, t_atom *argv)
{
    int i, j;
    float bestsum = 0;
    int bestindex = -1;
    t_float invec[DIMENSION];
    for (i = 0; i < DIMENSION; i++)
        invec[i] = atom_getfloatarg(i, argc, argv);
    for (j = 0; j < x->x_n; j++)
    {
        t_elem *e = x->x_vec + j;
        float sum;
        for (i = 0, sum = 0; i < DIMENSION; i++)
            sum += e->e_weight[i] * invec[i];
        if (x->x_nonrepeat) sum *= (float)(log(e->e_age));
        if (sum > bestsum)
        {
            bestsum = sum;
            sum = 1;
            bestindex = j;
        }
    }
    if (bestindex >= 0)
    {
        for (j = 0; j < x->x_n; j++)
            x->x_vec[j].e_age += 1.;
        x->x_vec[bestindex].e_age = 1;
    }
    outlet_float(x->x_obj.ob_outlet, (float)bestindex);
}

static void choice_free(t_choice *x)
{
    freebytes(x->x_vec, x->x_n * sizeof(t_elem));
}

void choice_setup(void)
{
    choice_class = class_new(gensym("choice"), (t_newmethod)choice_new,
        (t_method)choice_free, sizeof(t_choice), 0, A_DEFFLOAT, 0);
    class_addmethod(choice_class, (t_method)choice_add, gensym("add"), A_GIMME, 0);
    class_addmethod(choice_class, (t_method)choice_clear, gensym("clear"), 0);
    class_addmethod(choice_class, (t_method)choice_print, gensym("print"), 0);
    class_addlist(choice_class, choice_list);
}

--- NEW FILE: makefile ---
NAME=choice
CSYM=choice

current: pd_linux

# ----------------------- NT -----------------------

pd_nt: $(NAME).dll

.SUFFIXES: .dll

PDNTCFLAGS = /W3 /WX /DNT /DPD /nologo
VC="C:\Program Files\Microsoft Visual Studio\Vc98"

PDNTINCLUDE = /I. /I..\..\src /I$(VC)\include

PDNTLDIR = $(VC)\lib
PDNTLIB = $(PDNTLDIR)\libc.lib \
	$(PDNTLDIR)\oldnames.lib \
	$(PDNTLDIR)\kernel32.lib \
	..\..\bin\pd.lib 

.c.dll:
	cl $(PDNTCFLAGS) $(PDNTINCLUDE) /c $*.c
	link /dll /export:$(CSYM)_setup $*.obj $(PDNTLIB)

# ----------------------- IRIX 5.x -----------------------

pd_irix5: $(NAME).pd_irix5

.SUFFIXES: .pd_irix5

SGICFLAGS5 = -o32 -DPD -DUNIX -DIRIX -O2

SGIINCLUDE =  -I../../src

.c.pd_irix5:
	$(CC) $(SGICFLAGS5) $(SGIINCLUDE) -o $*.o -c $*.c
	ld -elf -shared -rdata_shared -o $*.pd_irix5 $*.o
	rm $*.o

# ----------------------- IRIX 6.x -----------------------

pd_irix6: $(NAME).pd_irix6

.SUFFIXES: .pd_irix6

SGICFLAGS6 = -n32 -DPD -DUNIX -DIRIX -DN32 -woff 1080,1064,1185 \
	-OPT:roundoff=3 -OPT:IEEE_arithmetic=3 -OPT:cray_ivdep=true \
	-Ofast=ip32

.c.pd_irix6:
	$(CC) $(SGICFLAGS6) $(SGIINCLUDE) -o $*.o -c $*.c
	ld -n32 -IPA -shared -rdata_shared -o $*.pd_irix6 $*.o
	rm $*.o

# ----------------------- LINUX i386 -----------------------

pd_linux: $(NAME).pd_linux

.SUFFIXES: .pd_linux

LINUXCFLAGS = -DPD -O2 -funroll-loops -fomit-frame-pointer  -fPIC \
    -Wall -W -Wshadow -Wstrict-prototypes \
    -Wno-unused -Wno-parentheses -Wno-switch $(CFLAGS)

LINUXINCLUDE =  -I../../src

.c.pd_linux:
	$(CC) $(LINUXCFLAGS) $(LINUXINCLUDE) -o $*.o -c $*.c
	ld -export_dynamic  -shared -o $*.pd_linux $*.o -lc -lm
	strip --strip-unneeded $*.pd_linux
	rm -f $*.o

# ----------------------- Mac OSX -----------------------

pd_darwin: $(NAME).pd_darwin

.SUFFIXES: .pd_darwin

DARWINCFLAGS = -DPD -O2 -Wall -W -Wshadow -Wstrict-prototypes \
    -Wno-unused -Wno-parentheses -Wno-switch

.c.pd_darwin:
	$(CC) $(DARWINCFLAGS) $(LINUXINCLUDE) -o $*.o -c $*.c
	$(CC) -bundle -undefined suppress -flat_namespace -o $*.pd_darwin $*.o 
	rm -f $*.o

# ----------------------------------------------------------

clean:
	rm -f *.o *.pd_* so_locations





More information about the Pd-cvs mailing list