[PD-dev] was [PD] Help with writing externals

thewade pdman at aproximation.org
Sun Apr 4 21:34:16 CEST 2004


Now I have another problem. When I hook up the output of rfft~ to both a [print~] object and my [fftbin~] object and bang them both, I get prittymuch the same value out of fftbin~, and this value does NOT correspond to any of the values printed by [print~]. Can you tell me why?

All I want to do is output from the outlet one value from the block of samples comming in the left inlet, as selected by the right inlet. If the right inlet says  \"1\", then when fftbin~ gets a bang in its left inlet, it will output sample 1 from the block of samples comming in its left inlet.

Thanks,
-thewade

Heres my code:
---------fftbin~.c-----------------------
#include <m_pd.h>
#include <math.h>

/*-------------------------------------------------------*/
/*fftbin~                                                */
/* A tool to extract specific bins from the output of    */
/* fft~. Inlets are (from left to right):                */
/* The output of fft~ and bang, the bin number to        */
/* capture.                                              */
/* The outlet is a control rate float equal to the value */
/* of the bin in question.                               */
/*-------------------------------------------------------*/

static t_class *fftbin_tilde_class;

typedef struct _fftbin {
  t_object  x_obj;
  t_sample dummy;
  t_float x_bin;
  t_float x_f;
} t_fftbin;

void *fftbin_tilde_new(t_floatarg f)
{
  t_fftbin *ref = (t_fftbin *)pd_new(fftbin_tilde_class);
  ref->dummy = 0;
  ref->x_bin = f;
  ref->x_f = 0;                             //Signal inlet exists by default
  floatinlet_new(&ref->x_obj, &ref->x_bin); //bin inlet
  outlet_new(&ref->x_obj, &s_float);        //bin value outlet
  return (void *)ref;
}

t_int *fftbin_tilde_perform(t_int *w)
{
  t_fftbin  *ref = (t_fftbin *)(w[1]);      //data structure
  t_sample  *in1 = (t_sample *)(w[2]);      //fft~ output
  int        n   = (int)(w[3]);             //blocksize

  float bin = truncf(ref->x_bin);
  if (bin>n-1) bin=n-1;
  if (bin<0) bin=0;
  ref->x_bin=bin;

  ref->x_f = (t_float)*in1+ref->x_bin;      //set datastructure

  return (w+4);
}

void fftbin_tilde_dsp(t_fftbin *ref, t_signal **sp)
{
  dsp_add(fftbin_tilde_perform, 3, ref, sp[0]->s_vec, sp[0]->s_n);
}

void fftbin_tilde_set(t_fftbin *x, t_floatarg f)
{
  x->x_bin = f;
}

void fftbin_tilde_bang(t_fftbin *x)
{
  outlet_float(x->x_obj.ob_outlet, x->x_f);
}

void fftbin_tilde_setup(void) {
  fftbin_tilde_class = class_new(gensym(\"fftbin~\"),
        (t_newmethod)fftbin_tilde_new,
        0, sizeof(t_fftbin),
        CLASS_DEFAULT,
        A_DEFFLOAT, 0);

  class_addmethod(fftbin_tilde_class,
        (t_method)fftbin_tilde_dsp, gensym(\"dsp\"), 0);
  class_addmethod(fftbin_tilde_class,
        (t_method)fftbin_tilde_set, gensym(\"set\"), A_DEFFLOAT, 0);
  class_addbang(fftbin_tilde_class, fftbin_tilde_bang);

  CLASS_MAINSIGNALIN(fftbin_tilde_class, t_fftbin, x_f);
  post(\"fftbin~: written by thewade with help from the PD list\");
}




More information about the Pd-dev mailing list