[PD-dev] Variable number of control inlets in external

Ian Smith-Heisters heisters at 0x09.com
Wed Dec 29 02:29:01 CET 2004


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"));
}




More information about the Pd-dev mailing list