aboutsummaryrefslogtreecommitdiff
path: root/formant~.c
diff options
context:
space:
mode:
Diffstat (limited to 'formant~.c')
-rw-r--r--formant~.c151
1 files changed, 86 insertions, 65 deletions
diff --git a/formant~.c b/formant~.c
index 265d868..b2c2c33 100644
--- a/formant~.c
+++ b/formant~.c
@@ -70,44 +70,51 @@ typedef struct _formant
} t_formant;
- /* clean up */
-static void formant_free(t_formant *x)
+/* clean up */
+static void formant_free(t_formant *x)
{
- if ( x->x_data != NULL ) {
- freebytes(x->x_data, x->x_size*sizeof(float) );
- x->x_data = NULL;
+ if ( x->x_data != NULL )
+ {
+ freebytes(x->x_data, x->x_size*sizeof(float) );
+ x->x_data = NULL;
}
}
- /* generate sample data */
+/* generate sample data */
static t_int formant_gendata(t_formant *x)
{
t_float t, b, fs;
- if ( x->x_size <= 0 || x->x_central_freq <= 0 || x->x_filter_width <= 0 || x->x_skirt_width <= 0 ) {
- error( "formant~ : error generating data : negative or null parameter(s)" );
- return -1;
+ if ( x->x_size <= 0 || x->x_central_freq <= 0 || x->x_filter_width <= 0 || x->x_skirt_width <= 0 )
+ {
+ error( "formant~ : error generating data : negative or null parameter(s)" );
+ return -1;
}
-
+
x->x_gendata = 1;
/* freeing data */
formant_free( x );
- if ( !( x->x_data = (float*) getbytes( x->x_size*sizeof(float) ) ) ) {
- post( "formant~ : error generating data : cannot allocate table" );
- return -1;
+ if ( !( x->x_data = (float*) getbytes( x->x_size*sizeof(float) ) ) )
+ {
+ post( "formant~ : error generating data : cannot allocate table" );
+ return -1;
}
-
+
fs = 0;
- while( fs < x->x_size-1 ) {
- t = (fs/x->x_samplerate) * x->x_time_stretch; /* time taken from zero */
- b = M_PI / x->x_skirt_width ;
- if ( t < b ) {
- *(x->x_data+(int)fs) = 0.5*(1-cos(x->x_skirt_width*t))*exp(- x->x_filter_width*t )*sin( x->x_central_freq*t);
- } else {
- *(x->x_data+(int)fs) = exp(- x->x_filter_width*t )*sin( x->x_central_freq*t);
- }
- fs++;
+ while( fs < x->x_size-1 )
+ {
+ t = (fs/x->x_samplerate) * x->x_time_stretch; /* time taken from zero */
+ b = M_PI / x->x_skirt_width ;
+ if ( t < b )
+ {
+ *(x->x_data+(int)fs) = 0.5*(1-cos(x->x_skirt_width*t))*exp(- x->x_filter_width*t )*sin( x->x_central_freq*t);
+ }
+ else
+ {
+ *(x->x_data+(int)fs) = exp(- x->x_filter_width*t )*sin( x->x_central_freq*t);
+ }
+ fs++;
}
/* everything went fine */
@@ -115,25 +122,30 @@ static t_int formant_gendata(t_formant *x)
return 0;
}
- /* generates a formant */
+/* generates a formant */
static t_int *formant_perform(t_int *w)
{
t_formant *x = (t_formant *)(w[1]);
t_float *out = (t_float *)(w[2]);
int n = (int)(w[3]); /* number of samples */
- while (n--) {
- if ( !x->x_gendata && x->x_play) {
- *out=*(x->x_data+x->x_readpos);
- x->x_readpos = (x->x_readpos+1)%x->x_size;
- if ( x->x_readpos == 0 ) {
- x->x_play=0;
- outlet_bang(x->x_end);
- }
- } else {
- *out=0.0;
- }
- out++;
+ while (n--)
+ {
+ if ( !x->x_gendata && x->x_play)
+ {
+ *out=*(x->x_data+x->x_readpos);
+ x->x_readpos = (x->x_readpos+1)%x->x_size;
+ if ( x->x_readpos == 0 )
+ {
+ x->x_play=0;
+ outlet_bang(x->x_end);
+ }
+ }
+ else
+ {
+ *out=0.0;
+ }
+ out++;
}
return (w+4);
}
@@ -143,63 +155,68 @@ static void formant_dsp(t_formant *x, t_signal **sp)
dsp_add(formant_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
}
- /* replay the sample */
+/* replay the sample */
static void formant_bang(t_formant *x, t_floatarg fsize)
{
x->x_play=1;
x->x_readpos=0;
}
- /* set size of the sample */
+/* set size of the sample */
static void formant_size(t_formant *x, t_floatarg fsize)
{
- if ( fsize <= 0 ) {
- post( "formant~ : error : sample size should be >0" );
- return;
+ if ( fsize <= 0 )
+ {
+ post( "formant~ : error : sample size should be >0" );
+ return;
}
x->x_size = fsize;
formant_gendata( x );
}
- /* set central frequency of the formant */
+/* set central frequency of the formant */
static void formant_central_freq(t_formant *x, t_floatarg ffreq)
{
- if ( ffreq <= 0 ) {
- post( "formant~ : error : filter central frequency should be >0" );
- return;
+ if ( ffreq <= 0 )
+ {
+ post( "formant~ : error : filter central frequency should be >0" );
+ return;
}
x->x_central_freq = ffreq;
formant_gendata( x );
}
- /* set filter width of the formant */
+/* set filter width of the formant */
static void formant_filter_width(t_formant *x, t_floatarg fwidth)
{
- if ( fwidth <= 0 ) {
- post( "formant~ : error : filter width should be >0" );
- return;
+ if ( fwidth <= 0 )
+ {
+ post( "formant~ : error : filter width should be >0" );
+ return;
}
x->x_filter_width = fwidth;
formant_gendata( x );
}
- /* set skirt width of the formant */
+/* set skirt width of the formant */
static void formant_skirt_width(t_formant *x, t_floatarg swidth)
{
- if ( swidth <= 0 ) {
- post( "formant~ : error : skirt width should be >0" );
- return;
+ if ( swidth <= 0 )
+ {
+ post( "formant~ : error : skirt width should be >0" );
+ return;
}
x->x_skirt_width = swidth;
formant_gendata( x );
}
- /* set time stretch factor */
+/* set time stretch factor */
static void formant_time_stretch(t_formant *x, t_floatarg fstretch)
{
- if ( fstretch <= 0 ) {
- post( "formant~ : error : time stretch should be >0" );
- return;
+ if ( fstretch <= 0 )
+ {
+ post( "formant~ : error : time stretch should be >0" );
+ return;
}
x->x_time_stretch = fstretch;
formant_gendata( x );
@@ -209,12 +226,13 @@ static void *formant_new(t_floatarg fsize, t_floatarg ffreq, t_floatarg ffwidth,
{
t_formant *x = (t_formant *)pd_new(formant_class);
outlet_new(&x->x_obj, &s_signal);
-
- if ( fsize <= 0 || ffreq <= 0 || ffwidth <= 0 || fswidth <= 0 ) {
- error( "formant~ : missing or negative creation arguments" );
- return NULL;
+
+ if ( fsize <= 0 || ffreq <= 0 || ffwidth <= 0 || fswidth <= 0 )
+ {
+ error( "formant~ : missing or negative creation arguments" );
+ return NULL;
}
-
+
x->x_size = fsize;
x->x_central_freq = ffreq;
x->x_filter_width = ffwidth;
@@ -229,10 +247,13 @@ static void *formant_new(t_floatarg fsize, t_floatarg ffreq, t_floatarg ffwidth,
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("swidth"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("stretch"));
x->x_end = outlet_new( &x->x_obj, &s_bang );
- if ( formant_gendata( x ) ) {
+ if ( formant_gendata( x ) )
+ {
post( "formant~ : error generating data" );
return NULL;
- } else {
+ }
+ else
+ {
return(x);
}
}
@@ -241,7 +262,7 @@ void formant_tilde_setup(void)
{
verbose(0, formant_version);
formant_class = class_new(gensym("formant~"), (t_newmethod)formant_new, (t_method)formant_free,
- sizeof(t_formant), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
+ sizeof(t_formant), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
class_addmethod(formant_class, (t_method)formant_dsp, gensym("dsp"), 0);
class_addmethod(formant_class, (t_method)formant_size, gensym("size"), A_FLOAT, 0);
class_addmethod(formant_class, (t_method)formant_bang, gensym("bang"), 0);