Hello list!
I may have gotten it working. THanks for telling me about how dsp only gets called once. I also cleaned up the code a bit as far as casting goes. In the end it came down to getting the nth element if *in by using square brackets instead of using pointer arithmatic.
This code should return the Nth element of a sample, though Im still verifying it.
Thanks for all the help!
-thewade
/*begin fftbin~.c code here-------------------*/
#include <m_pd.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_float x_bin;
t_float x_binval;
float x_f;
} t_fftbin;
void *fftbin_tilde_new(t_floatarg f)
{
t_fftbin *ref = (t_fftbin *)pd_new(fftbin_tilde_class);
ref->x_bin = f;
ref->x_binval=0;
ref->x_f = 0;
outlet_new(&ref->x_obj, &s_float); //bin value outlet
floatinlet_new(&ref->x_obj, &ref->x_bin);
return (void *)ref;
}
t_int *fftbin_tilde_perform(t_int *w)
{
t_fftbin *ref = (t_fftbin *)(w[1]);
t_sample *in1 = (t_float *)(w[2]); //fft~ output
int n = (int)(w[3]);
int bin = (int)ref->x_bin;
if (bin > n) bin = n;
if (bin < 0) bin = 0;
ref->x_binval = in1[bin];
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 *ref, t_floatarg f)
{
ref->x_bin = f;
}
void fftbin_tilde_bang(t_fftbin *ref)
{
outlet_float(ref->x_obj.ob_outlet, ref->x_binval);
}
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\");
}