Hi all,
I'm trying to write a simple external in C. I read IOhannes' tutorial, and got simple framework running that had four inlets and outlets and the ability to send a bang to output the values of the inlets to their respective outlets.
Next I want to be able to pass a single argument that will specify the number of inlets and outlets in addition to the first hot inlet. Below is the code I have, which certainly doesn't work. Summarily (is that a word?), I've just been trying to make n inlets and push the values of those inlets to the first outlet on a bang. I figure once I have that I can do the outlets pretty easily. The pertinent parts of the code are these:
// Initialize dataspace typedef struct _genmut { t_object x_obj; t_float rating, num_inlets; t_float *nums_vec; t_outlet *f_out1, *f_out2, *f_out3, *f_out4; } t_genmut;
//Methods void genmut_bang(t_genmut *x) { // just trying to get the right number of inlets first, then I'll work // on outlets. This should push the numbers from the inlets to the // first outlet. But it doesn't. for (i = 0; i < &x->i_numinlets; i++) outlet_float(x->f_out1, x->nums_vec[i]); }
// Constructor void *genmut_new(t_floatarg ni) { t_genmut *x = (t_genmut *)pd_new(genmut_class);
int i; x->num_inlets = ni; // set rating to default x->rating = 1;
// dynamically create the inlets for (i = 0; i < ni; i++) floatinlet_new(&x->x_obj, &x->nums_vec[i]);
x->f_out1 = outlet_new(&x->x_obj, &s_float); x->f_out2 = outlet_new(&x->x_obj, &s_float); x->f_out3 = outlet_new(&x->x_obj, &s_float); x->f_out4 = outlet_new(&x->x_obj, &s_float);
return (void *)x; }
My difficulty is only compounded by my obviously rudimentary and somewhat dusty C skills. I'm unclear on how to create the vector that would make a dynamically sizeable array for the inlets and outlets.
Thanks for any advice. The whole of the code is below.
-Ian
/* genmut.c - Genetic Mutator external for Pure Data Take n floats, mutate them w/ a genetic algorithm, searching for a higher rating, which is given by the user via inlet 1.
By Ian Smith-Heisters heisters [at] 0x09.com http://www.0x09.com
History: Dec 28, '04: started */
#include "m_pd.h"
static t_class *genmut_class;
// Initialize dataspace typedef struct _genmut { t_object x_obj; t_float rating, num_inlets; t_float *nums_vec; t_outlet *f_out1, *f_out2, *f_out3, *f_out4; } t_genmut;
//Methods void genmut_bang(t_genmut *x) { for (i = 0; i < &x->i_numinlets; i++) outlet_float(x->f_out1, x->nums_vec[i]); }
void genmut_rating(t_genmut *x, t_floatarg f) { x->rating = f; }
// Constructor void *genmut_new(t_floatarg ni) { t_genmut *x = (t_genmut *)pd_new(genmut_class);
int i; x->num_inlets = ni; // set rating to default x->rating = 1;
// dynamically create the inlets for (i = 0; i < ni; i++) floatinlet_new(&x->x_obj, &x->nums_vec[i]);
/* floatinlet_new(&x->x_obj, &x->num1); floatinlet_new(&x->x_obj, &x->num2); floatinlet_new(&x->x_obj, &x->num3); floatinlet_new(&x->x_obj, &x->num4); */
x->f_out1 = outlet_new(&x->x_obj, &s_float); x->f_out2 = outlet_new(&x->x_obj, &s_float); x->f_out3 = outlet_new(&x->x_obj, &s_float); x->f_out4 = outlet_new(&x->x_obj, &s_float);
return (void *)x; }
// Setup void genmut_setup(void) { genmut_class = class_new(gensym("genmut"), (t_newmethod)genmut_new, 0, sizeof(t_genmut), CLASS_DEFAULT, 0);
class_addbang (genmut_class, genmut_bang); class_addmethod(genmut_class, (t_method)genmut_rating, gensym("rating"), A_DEFFLOAT, 0);
class_sethelpsymbol(genmut_class, gensym("help-genmut")); }
Thanks. I figured it all out just after this, but my mailserver has been down for days. The dynamic outlets were tricky because I had to create a wrapper struct for the t_outlet struct. Not sure if it was actually necessary, but it works. Now I'm wondering if the code I have is sound in terms of memory leakage. If I've allocated two arrays with getbytes do I need to free them in a _free method to avoid memory leakage? Cheers, -Ian
Krzysztof Czaja wrote:
hi Ian,
could not find nums_vec's allocation in your code...
Krzysztof
Ian Smith-Heisters wrote: ...
// Initialize dataspace typedef struct _genmut { t_object x_obj; t_float rating, num_inlets; t_float *nums_vec;
PD-dev mailing list PD-dev@iem.at http://iem.at/cgi-bin/mailman/listinfo/pd-dev