From 2b60d55c919e7588f5aff15936e83c300b3660bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Tue, 22 Mar 2005 20:58:25 +0000 Subject: zexy-2.0: - use of abstractions for objects that allow it - some objects are build both as externals and abstractions (as slower fallbacks) - code-layout is now 1:1 c-file<->object (this should allow for building of zexy as a collection of externals instead as a big library) - matrix-objects have moved to iemmatrix !! svn path=/trunk/externals/zexy/; revision=2641 --- src/envrms_tilde.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/envrms_tilde.c (limited to 'src/envrms_tilde.c') diff --git a/src/envrms_tilde.c b/src/envrms_tilde.c new file mode 100644 index 0000000..e9dc302 --- /dev/null +++ b/src/envrms_tilde.c @@ -0,0 +1,150 @@ +/****************************************************** + * + * zexy - implementation file + * + * copyleft (c) IOhannes m zmölnig + * + * 1999:forum::für::umläute:2004 + * + * institute of electronic music and acoustics (iem) + * + ****************************************************** + * + * license: GNU General Public License v.2 + * + ******************************************************/ + +/* ---------------- envrms~ - simple envelope follower. ----------------- */ +/* this is exactly the same as msp's env~-object, but does not output dB but RMS !! */ +/* i found env~+dbtorms most inconvenient (and expensive...) */ + +#include "zexy.h" +#include + +#define MAXOVERLAP 10 +#define MAXVSTAKEN 64 + +t_class *sigenvrms_class; + +typedef struct sigenvrms +{ + t_object x_obj; /* header */ + void *x_outlet; /* a "float" outlet */ + void *x_clock; /* a "clock" object */ + float *x_buf; /* a Hanning window */ + int x_phase; /* number of points since last output */ + int x_period; /* requested period of output */ + int x_realperiod; /* period rounded up to vecsize multiple */ + int x_npoints; /* analysis window size in samples */ + float x_result; /* result to output */ + float x_sumbuf[MAXOVERLAP]; /* summing buffer */ +} t_sigenvrms; + +static void sigenvrms_tick(t_sigenvrms *x); + +static void *sigenvrms_new(t_floatarg fnpoints, t_floatarg fperiod) +{ + int npoints = fnpoints; + int period = fperiod; + t_sigenvrms *x; + float *buf; + int i; + + if (npoints < 1) npoints = 1024; + if (period < 1) period = npoints/2; + if (period < npoints / MAXOVERLAP + 1) + period = npoints / MAXOVERLAP + 1; + if (!(buf = getbytes(sizeof(float) * (npoints + MAXVSTAKEN)))) + { + error("env: couldn't allocate buffer"); + return (0); + } + x = (t_sigenvrms *)pd_new(sigenvrms_class); + x->x_buf = buf; + x->x_npoints = npoints; + x->x_phase = 0; + x->x_period = period; + for (i = 0; i < MAXOVERLAP; i++) x->x_sumbuf[i] = 0; + for (i = 0; i < npoints; i++) + buf[i] = (1. - cos((2 * 3.141592654 * i) / npoints))/npoints; + for (; i < npoints+MAXVSTAKEN; i++) buf[i] = 0; + x->x_clock = clock_new(x, (t_method)sigenvrms_tick); + x->x_outlet = outlet_new(&x->x_obj, gensym("float")); + return (x); +} + +static t_int *sigenvrms_perform(t_int *w) +{ + t_sigenvrms *x = (t_sigenvrms *)(w[1]); + t_float *in = (t_float *)(w[2]); + int n = (int)(w[3]); + int count; + float *sump; + in += n; + for (count = x->x_phase, sump = x->x_sumbuf; + count < x->x_npoints; count += x->x_realperiod, sump++) + { + float *hp = x->x_buf + count; + float *fp = in; + float sum = *sump; + int i; + + for (i = 0; i < n; i++) + { + fp--; + sum += *hp++ * (*fp * *fp); + } + *sump = sum; + } + sump[0] = 0; + x->x_phase -= n; + if (x->x_phase < 0) + { + x->x_result = x->x_sumbuf[0]; + for (count = x->x_realperiod, sump = x->x_sumbuf; + count < x->x_npoints; count += x->x_realperiod, sump++) + sump[0] = sump[1]; + sump[0] = 0; + x->x_phase = x->x_realperiod - n; + clock_delay(x->x_clock, 0L); + } + return (w+4); +} + +static void sigenvrms_dsp(t_sigenvrms *x, t_signal **sp) +{ + if (x->x_period % sp[0]->s_n) x->x_realperiod = + x->x_period + sp[0]->s_n - (x->x_period % sp[0]->s_n); + else x->x_realperiod = x->x_period; + dsp_add(sigenvrms_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); + if (sp[0]->s_n > MAXVSTAKEN) bug("sigenvrms_dsp"); +} + +static void sigenvrms_tick(t_sigenvrms *x) /* callback function for the clock */ +{ + outlet_float(x->x_outlet, sqrtf(x->x_result)); +} + +static void sigenvrms_ff(t_sigenvrms *x) /* cleanup on free */ +{ + clock_free(x->x_clock); + freebytes(x->x_buf, (x->x_npoints + MAXVSTAKEN) * sizeof(float)); +} + +static void sigenvrms_help(void) +{ + post("envrms~\t:: envelope follower that does output rms instead of dB"); +} + + +void envrms_tilde_setup(void) +{ + sigenvrms_class = class_new(gensym("envrms~"), (t_newmethod)sigenvrms_new, + (t_method)sigenvrms_ff, sizeof(t_sigenvrms), 0, A_DEFFLOAT, A_DEFFLOAT, 0); + class_addmethod(sigenvrms_class, nullfn, gensym("signal"), 0); + class_addmethod(sigenvrms_class, (t_method)sigenvrms_dsp, gensym("dsp"), 0); + + class_addmethod(sigenvrms_class, (t_method)sigenvrms_help, gensym("help"), 0); + class_sethelpsymbol(sigenvrms_class, gensym("zexy/envrms~")); + zexy_register("envrms~"); +} -- cgit v1.2.1