diff options
Diffstat (limited to 'src/envrms~.c')
-rw-r--r-- | src/envrms~.c | 103 |
1 files changed, 58 insertions, 45 deletions
diff --git a/src/envrms~.c b/src/envrms~.c index 263c2a9..1c1db80 100644 --- a/src/envrms~.c +++ b/src/envrms~.c @@ -1,4 +1,4 @@ -/* +/* * envrms~: simple envelope follower * * (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem) @@ -7,12 +7,12 @@ * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program. If not, see <http://www.gnu.org/licenses/>. */ @@ -28,8 +28,7 @@ t_class *sigenvrms_class; -typedef struct sigenvrms -{ +typedef struct sigenvrms { t_object x_obj; /* header */ void *x_outlet; /* a "float" outlet */ void *x_clock; /* a "clock" object */ @@ -52,24 +51,33 @@ static void *sigenvrms_new(t_floatarg fnpoints, t_floatarg fperiod) t_sample *buf; int i; - if (npoints < 1) npoints = 1024; - if (period < 1) period = npoints/2; - if (period < npoints / MAXOVERLAP + 1) + if (npoints < 1) { + npoints = 1024; + } + if (period < 1) { + period = npoints/2; + } + if (period < npoints / MAXOVERLAP + 1) { period = npoints / MAXOVERLAP + 1; - if (!(buf = getbytes(sizeof(*buf) * (npoints + MAXVSTAKEN)))) - { - error("env: couldn't allocate buffer"); - return (0); - } + } + if (!(buf = getbytes(sizeof(*buf) * (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++) + 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; + } + 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); @@ -81,48 +89,51 @@ static t_int *sigenvrms_perform(t_int *w) t_sample *in = (t_sample *)(w[2]); int n = (int)(w[3]); int count; - t_sample *sump; + t_sample *sump; in += n; for (count = x->x_phase, sump = x->x_sumbuf; - count < x->x_npoints; count += x->x_realperiod, sump++) - { - t_sample *hp = x->x_buf + count; - t_sample *fp = in; - t_sample sum = *sump; - int i; - - for (i = 0; i < n; i++) - { - fp--; - sum += *hp++ * (*fp * *fp); - } - *sump = sum; + count < x->x_npoints; count += x->x_realperiod, sump++) { + t_sample *hp = x->x_buf + count; + t_sample *fp = in; + t_sample 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); + 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; + 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"); + if (sp[0]->s_n > MAXVSTAKEN) { + bug("sigenvrms_dsp"); + } } -static void sigenvrms_tick(t_sigenvrms *x) /* callback function for the clock */ +static void sigenvrms_tick(t_sigenvrms + *x) /* callback function for the clock */ { outlet_float(x->x_outlet, sqrtf(x->x_result)); } @@ -144,8 +155,10 @@ 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_dsp, gensym("dsp"), + 0); - class_addmethod(sigenvrms_class, (t_method)sigenvrms_help, gensym("help"), 0); + class_addmethod(sigenvrms_class, (t_method)sigenvrms_help, gensym("help"), + 0); zexy_register("envrms~"); } |