| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include <sys/types.h> |
| 31 |
#include <string.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <errno.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <fcntl.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#ifndef __APPLE__ |
| 38 |
#include <malloc.h> |
| 39 |
#endif |
| 40 |
#include <ctype.h> |
| 41 |
#include <math.h> |
| 42 |
#ifdef _MSC_VER |
| 43 |
# define M_PI 3.14159265358979323846 |
| 44 |
#else |
| 45 |
# include <unistd.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#include <m_pd.h> |
| 49 |
|
| 50 |
static char *formant_version = "formant~: formant synthesis version 0.1, written by Yves Degoyon"; |
| 51 |
|
| 52 |
|
| 53 |
static t_class *formant_class; |
| 54 |
|
| 55 |
typedef struct _formant |
| 56 |
{ |
| 57 |
t_object x_obj; |
| 58 |
|
| 59 |
t_int x_size; |
| 60 |
t_float x_central_freq; |
| 61 |
t_float x_filter_width; |
| 62 |
t_float x_skirt_width; |
| 63 |
t_float x_samplerate; |
| 64 |
t_float x_time_stretch; |
| 65 |
t_int x_readpos; |
| 66 |
t_int x_play; |
| 67 |
t_outlet *x_end; |
| 68 |
t_float x_gendata; |
| 69 |
t_float* x_data; |
| 70 |
|
| 71 |
} t_formant; |
| 72 |
|
| 73 |
|
| 74 |
static void formant_free(t_formant *x) |
| 75 |
{ |
| 76 |
if ( x->x_data != NULL ) |
| 77 |
{ |
| 78 |
freebytes(x->x_data, x->x_size*sizeof(float) ); |
| 79 |
x->x_data = NULL; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
static t_int formant_gendata(t_formant *x) |
| 85 |
{ |
| 86 |
t_float t, b, fs; |
| 87 |
|
| 88 |
if ( x->x_size <= 0 || x->x_central_freq <= 0 || x->x_filter_width <= 0 || x->x_skirt_width <= 0 ) |
| 89 |
{ |
| •90 |
pd_error(x, "formant~ : error generating data : negative or null parameter(s)" ); |
| 91 |
return -1; |
| 92 |
} |
| 93 |
|
| 94 |
x->x_gendata = 1; |
| 95 |
|
| 96 |
|
| 97 |
formant_free( x ); |
| 98 |
if ( !( x->x_data = (float*) getbytes( x->x_size*sizeof(float) ) ) ) |
| 99 |
{ |
| 100 |
post( "formant~ : error generating data : cannot allocate table" ); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
|
| 104 |
fs = 0; |
| 105 |
while( fs < x->x_size-1 ) |
| 106 |
{ |
| 107 |
t = (fs/x->x_samplerate) * x->x_time_stretch; |
| 108 |
b = M_PI / x->x_skirt_width ; |
| 109 |
if ( t < b ) |
| 110 |
{ |
| 111 |
*(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); |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
*(x->x_data+(int)fs) = exp(- x->x_filter_width*t )*sin( x->x_central_freq*t); |
| 116 |
} |
| 117 |
fs++; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
x->x_gendata = 0; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
static t_int *formant_perform(t_int *w) |
| 127 |
{ |
| 128 |
t_formant *x = (t_formant *)(w[1]); |
| 129 |
t_float *out = (t_float *)(w[2]); |
| 130 |
int n = (int)(w[3]); |
| 131 |
|
| 132 |
while (n--) |
| 133 |
{ |
| 134 |
if ( !x->x_gendata && x->x_play) |
| 135 |
{ |
| 136 |
*out=*(x->x_data+x->x_readpos); |
| 137 |
x->x_readpos = (x->x_readpos+1)%x->x_size; |
| 138 |
if ( x->x_readpos == 0 ) |
| 139 |
{ |
| 140 |
x->x_play=0; |
| 141 |
outlet_bang(x->x_end); |
| 142 |
} |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
*out=0.0; |
| 147 |
} |
| 148 |
out++; |
| 149 |
} |
| 150 |
return (w+4); |
| 151 |
} |
| 152 |
|
| 153 |
static void formant_dsp(t_formant *x, t_signal **sp) |
| 154 |
{ |
| 155 |
dsp_add(formant_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
static void formant_bang(t_formant *x, t_floatarg fsize) |
| 160 |
{ |
| 161 |
x->x_play=1; |
| 162 |
x->x_readpos=0; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
static void formant_size(t_formant *x, t_floatarg fsize) |
| 167 |
{ |
| 168 |
if ( fsize <= 0 ) |
| 169 |
{ |
| 170 |
post( "formant~ : error : sample size should be >0" ); |
| 171 |
return; |
| 172 |
} |
| 173 |
x->x_size = fsize; |
| 174 |
formant_gendata( x ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
static void formant_central_freq(t_formant *x, t_floatarg ffreq) |
| 179 |
{ |
| 180 |
if ( ffreq <= 0 ) |
| 181 |
{ |
| 182 |
post( "formant~ : error : filter central frequency should be >0" ); |
| 183 |
return; |
| 184 |
} |
| 185 |
x->x_central_freq = ffreq; |
| 186 |
formant_gendata( x ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
static void formant_filter_width(t_formant *x, t_floatarg fwidth) |
| 191 |
{ |
| 192 |
if ( fwidth <= 0 ) |
| 193 |
{ |
| 194 |
post( "formant~ : error : filter width should be >0" ); |
| 195 |
return; |
| 196 |
} |
| 197 |
x->x_filter_width = fwidth; |
| 198 |
formant_gendata( x ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
static void formant_skirt_width(t_formant *x, t_floatarg swidth) |
| 203 |
{ |
| 204 |
if ( swidth <= 0 ) |
| 205 |
{ |
| 206 |
post( "formant~ : error : skirt width should be >0" ); |
| 207 |
return; |
| 208 |
} |
| 209 |
x->x_skirt_width = swidth; |
| 210 |
formant_gendata( x ); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static void formant_time_stretch(t_formant *x, t_floatarg fstretch) |
| 215 |
{ |
| 216 |
if ( fstretch <= 0 ) |
| 217 |
{ |
| 218 |
post( "formant~ : error : time stretch should be >0" ); |
| 219 |
return; |
| 220 |
} |
| 221 |
x->x_time_stretch = fstretch; |
| 222 |
formant_gendata( x ); |
| 223 |
} |
| 224 |
|
| 225 |
static void *formant_new(t_floatarg fsize, t_floatarg ffreq, t_floatarg ffwidth, t_floatarg fswidth) |
| 226 |
{ |
| 227 |
t_formant *x = (t_formant *)pd_new(formant_class); |
| 228 |
outlet_new(&x->x_obj, &s_signal); |
| 229 |
|
| 230 |
if ( fsize <= 0 || ffreq <= 0 || ffwidth <= 0 || fswidth <= 0 ) |
| 231 |
{ |
| •232 |
pd_error(x, "formant~ : missing or negative creation arguments" ); |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
x->x_size = fsize; |
| 237 |
x->x_central_freq = ffreq; |
| 238 |
x->x_filter_width = ffwidth; |
| 239 |
x->x_skirt_width = fswidth; |
| 240 |
x->x_readpos = 0; |
| 241 |
x->x_data = NULL; |
| 242 |
x->x_time_stretch = 1.0; |
| 243 |
x->x_samplerate = sys_getsr(); |
| 244 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("size")); |
| 245 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("freq")); |
| 246 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("fwidth")); |
| 247 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("swidth")); |
| 248 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("stretch")); |
| 249 |
x->x_end = outlet_new( &x->x_obj, &s_bang ); |
| 250 |
if ( formant_gendata( x ) ) |
| 251 |
{ |
| 252 |
post( "formant~ : error generating data" ); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
return(x); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
void formant_tilde_setup(void) |
| 262 |
{ |
| •263 |
logpost(NULL, 4, "%s", formant_version); |
| 264 |
formant_class = class_new(gensym("formant~"), (t_newmethod)formant_new, (t_method)formant_free, |
| 265 |
sizeof(t_formant), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); |
| 266 |
class_addmethod(formant_class, (t_method)formant_dsp, gensym("dsp"), 0); |
| 267 |
class_addmethod(formant_class, (t_method)formant_size, gensym("size"), A_FLOAT, 0); |
| 268 |
class_addmethod(formant_class, (t_method)formant_bang, gensym("bang"), 0); |
| 269 |
class_addmethod(formant_class, (t_method)formant_central_freq, gensym("freq"), A_FLOAT, 0); |
| 270 |
class_addmethod(formant_class, (t_method)formant_filter_width, gensym("fwidth"), A_FLOAT, 0); |
| 271 |
class_addmethod(formant_class, (t_method)formant_skirt_width, gensym("swidth"), A_FLOAT, 0); |
| 272 |
class_addmethod(formant_class, (t_method)formant_time_stretch, gensym("stretch"), A_FLOAT, 0); |
| 273 |
} |