From 981f641ef44fe4d72dcb0e1248a2f96ba53cee69 Mon Sep 17 00:00:00 2001 From: Miller Puckette Date: Sat, 6 Nov 2004 16:07:34 +0000 Subject: 0.38 test 9 (mostly bug fixes) svn path=/trunk/; revision=2226 --- pd/extra/bonk~/bonk~.c | 956 ++++++------ pd/extra/choice/choice.c | 52 +- pd/extra/expr~/fts_to_pd.h | 16 +- pd/extra/expr~/makefile | 2 +- pd/extra/expr~/vexp.c | 3432 +++++++++++++++++++++--------------------- pd/extra/expr~/vexp.h | 258 ++-- pd/extra/expr~/vexp_fun.c | 1590 +++++++++---------- pd/extra/expr~/vexp_if.c | 1696 ++++++++++----------- pd/extra/fiddle~/fiddle~.c | 1527 ++++++++++--------- pd/extra/loop~/loop~.c | 98 +- pd/extra/lrshift~/lrshift~.c | 24 +- pd/extra/pique/pique.c | 222 +-- 12 files changed, 4929 insertions(+), 4944 deletions(-) (limited to 'pd/extra') diff --git a/pd/extra/bonk~/bonk~.c b/pd/extra/bonk~/bonk~.c index 6b8cfa76..209ddbcc 100644 --- a/pd/extra/bonk~/bonk~.c +++ b/pd/extra/bonk~/bonk~.c @@ -19,12 +19,10 @@ IN CONNECTION WITH THIS SOFTWARE! #include "ext.h" #include "z_dsp.h" #include "math.h" -//#include "stdio.h" #include "ext_support.h" -//#include "ext_strings.h" #include "ext_proto.h" -typedef double t_floatarg; // from m_pd.h +typedef double t_floatarg; /* from m_pd.h */ #define flog log #define fexp exp #define fsqrt sqrt @@ -34,8 +32,8 @@ typedef double t_floatarg; // from m_pd.h #define fexp exp #define fsqrt sqrt -#define FILE_DIALOG 1 /* use dialogs to get file name */ -#define FILE_NAMED 2 /* symbol specifies file name */ +#define FILE_DIALOG 1 /* use dialogs to get file name */ +#define FILE_NAMED 2 /* symbol specifies file name */ #define DUMTAB1SIZE 256 #define DUMTAB2SIZE 1024 @@ -74,7 +72,7 @@ typedef struct _filterkernel float *k_stuff; } t_filterkernel; -#if 0 /* this is the design for 1.0: */ +#if 0 /* this is the design for 1.0: */ static t_filterkernel bonk_filterkernels[] = {{256, 2, .01562}, {256, 4, .01562}, {256, 6, .01562}, {180, 6, .02222}, {128, 6, .01803}, {90, 6, .02222}, {64, 6, .02362}, {46, 6, .02773}, @@ -108,15 +106,15 @@ typedef struct template typedef struct _insig { - t_hist g_hist[NFILTERS]; /* history for each filter */ + t_hist g_hist[NFILTERS]; /* history for each filter */ #ifdef PD - t_outlet *g_outlet; /* outlet for raw data */ + t_outlet *g_outlet; /* outlet for raw data */ #endif #ifdef MSP - void *g_outlet; /* outlet for raw data */ + void *g_outlet; /* outlet for raw data */ #endif - float *g_inbuf; /* buffered input samples */ - t_float *g_invec; /* new input samples */ + float *g_inbuf; /* buffered input samples */ + t_float *g_invec; /* new input samples */ } t_insig; typedef struct _bonk @@ -128,14 +126,14 @@ typedef struct _bonk #endif /* PD */ #ifdef MSP t_pxobject x_obj; - void *x_cookedout; // t_outlet *x_cookedout; - void *x_clock; // t_clock *x_clock; - short x_vol; // to store the volume reference number. + void *x_cookedout; + void *x_clock; + short x_vol; #endif /* MSP */ t_hist x_hist[NFILTERS]; t_template *x_template; - t_insig *x_insig; + t_insig *x_insig; int x_ninsig; int x_ntemplate; int x_infill; @@ -148,10 +146,10 @@ typedef struct _bonk int x_masktime; float x_maskdecay; int x_learn; - double x_learndebounce; /* debounce time for "learn" mode */ - int x_learncount; /* countup for "learn" mode */ + double x_learndebounce; /* debounce time for "learn" mode */ + int x_learncount; /* countup for "learn" mode */ float x_debouncedecay; - float x_minvel; /* minimum velocity we output */ + float x_minvel; /* minimum velocity we output */ float x_debouncevel; } t_bonk; @@ -193,11 +191,11 @@ static void bonk_donew(t_bonk *x, int period, int nsig) for (j = 0, g = x->x_insig; j < nsig; j++, g++) { - for (i = 0, h = g->g_hist; i--; h++) - h->h_power = h->h_mask = h->h_before = 0, h->h_countup = 0; - /* we ought to check for failure to allocate memory here */ - g->g_inbuf = (float *)getbytes(NPOINTS * sizeof(float)); - for (i = NPOINTS, fp = g->g_inbuf; i--; fp++) *fp = 0; + for (i = 0, h = g->g_hist; i--; h++) + h->h_power = h->h_mask = h->h_before = 0, h->h_countup = 0; + /* we ought to check for failure to allocate memory here */ + g->g_inbuf = (float *)getbytes(NPOINTS * sizeof(float)); + for (i = NPOINTS, fp = g->g_inbuf; i--; fp++) *fp = 0; } x->x_ninsig = nsig; x->x_template = (t_template *)getbytes(0); @@ -239,135 +237,135 @@ static void bonk_dotick(t_bonk *x, int hit) for (i = ninsig, pp = powerout, gp = x->x_insig; i--; gp++) { - for (j = 0, h = gp->g_hist; j < NFILTERS; j++, h++, pp++) - { - float power = (hit ? h->h_mask - h->h_before : h->h_power); - float intensity = *pp = - (power > 0 ? 100. * qrsqrt(qrsqrt(power)) : 0); - vel += intensity; - temperature += intensity * (float)j; - } + for (j = 0, h = gp->g_hist; j < NFILTERS; j++, h++, pp++) + { + float power = (hit ? h->h_mask - h->h_before : h->h_power); + float intensity = *pp = + (power > 0 ? 100. * qrsqrt(qrsqrt(power)) : 0); + vel += intensity; + temperature += intensity * (float)j; + } } if (vel > 0) temperature /= vel; else temperature = 0; - vel *= 0.5 / ninsig; /* fudge factor */ + vel *= 0.5 / ninsig; /* fudge factor */ if (hit) { - /* if hit nonzero it's a clock callback. if in "learn" mode update the - template list; in any event match the hit to known templates. */ - - if (vel < x->x_debouncevel) - { - if (x->x_debug) - post("bounce cancelled: vel %f debounce %f", - vel, x->x_debouncevel); - return; - } - if (vel < x->x_minvel) - { - if (x->x_debug) - post("low velocity cancelled: vel %f, minvel %f", - vel, x->x_minvel); - return; - } - x->x_debouncevel = vel; - if (x->x_learn) - { - double lasttime = x->x_learndebounce; - double msec = clock_gettimesince(lasttime); - if ((!ntemplate) || (msec > 200)) - { - int countup = x->x_learncount; - /* normalize to 100 */ - float norm; - for (i = NFILTERS * ninsig, norm = 0, pp = powerout; i--; pp++) - norm += *pp * *pp; - if (norm < 1.0e-15) norm = 1.0e-15; - norm = 100.f * qrsqrt(norm); - /* check if this is the first strike for a new template */ - if (!countup) - { - int oldn = ntemplate; - x->x_ntemplate = ntemplate = oldn + ninsig; - x->x_template = (t_template *)t_resizebytes(x->x_template, - oldn * sizeof(x->x_template[0]), - ntemplate * sizeof(x->x_template[0])); - for (i = ninsig, pp = powerout; i--; oldn++) - for (j = NFILTERS, fp = x->x_template[oldn].t_amp; j--; - pp++, fp++) - *fp = *pp * norm; - } - else - { - int oldn = ntemplate - ninsig; - if (oldn < 0) post("bonk_tick bug"); - for (i = ninsig, pp = powerout; i--; oldn++) - { - for (j = NFILTERS, fp = x->x_template[oldn].t_amp; j--; - pp++, fp++) - *fp = (countup * *fp + *pp * norm) - /(countup + 1.0f); - } - } - countup++; - if (countup == x->x_learn) countup = 0; - x->x_learncount = countup; - } - else return; - } - x->x_learndebounce = clock_getsystime(); - if (ntemplate) - { - float bestfit = -1e30; - int templatecount; - nfit = -1; - for (i = 0, templatecount = 0, tp = x->x_template; - templatecount < ntemplate; i++) - { - float dotprod = 0; - for (k = 0, pp = powerout; - k < ninsig && templatecount < ntemplate; - k++, tp++, templatecount++) - { - for (j = NFILTERS, fp = tp->t_amp; - j--; fp++, pp++) - { - if (*fp < 0 || *pp < 0) post("bonk_tick bug 2"); - dotprod += *fp * *pp; - } - } - if (dotprod > bestfit) - { - bestfit = dotprod; - nfit = i; - } - } - if (nfit < 0) post("bonk_tick bug"); - } - else nfit = 0; + /* if hit nonzero it's a clock callback. if in "learn" mode update the + template list; in any event match the hit to known templates. */ + + if (vel < x->x_debouncevel) + { + if (x->x_debug) + post("bounce cancelled: vel %f debounce %f", + vel, x->x_debouncevel); + return; + } + if (vel < x->x_minvel) + { + if (x->x_debug) + post("low velocity cancelled: vel %f, minvel %f", + vel, x->x_minvel); + return; + } + x->x_debouncevel = vel; + if (x->x_learn) + { + double lasttime = x->x_learndebounce; + double msec = clock_gettimesince(lasttime); + if ((!ntemplate) || (msec > 200)) + { + int countup = x->x_learncount; + /* normalize to 100 */ + float norm; + for (i = NFILTERS * ninsig, norm = 0, pp = powerout; i--; pp++) + norm += *pp * *pp; + if (norm < 1.0e-15) norm = 1.0e-15; + norm = 100.f * qrsqrt(norm); + /* check if this is the first strike for a new template */ + if (!countup) + { + int oldn = ntemplate; + x->x_ntemplate = ntemplate = oldn + ninsig; + x->x_template = (t_template *)t_resizebytes(x->x_template, + oldn * sizeof(x->x_template[0]), + ntemplate * sizeof(x->x_template[0])); + for (i = ninsig, pp = powerout; i--; oldn++) + for (j = NFILTERS, fp = x->x_template[oldn].t_amp; j--; + pp++, fp++) + *fp = *pp * norm; + } + else + { + int oldn = ntemplate - ninsig; + if (oldn < 0) post("bonk_tick bug"); + for (i = ninsig, pp = powerout; i--; oldn++) + { + for (j = NFILTERS, fp = x->x_template[oldn].t_amp; j--; + pp++, fp++) + *fp = (countup * *fp + *pp * norm) + /(countup + 1.0f); + } + } + countup++; + if (countup == x->x_learn) countup = 0; + x->x_learncount = countup; + } + else return; + } + x->x_learndebounce = clock_getsystime(); + if (ntemplate) + { + float bestfit = -1e30; + int templatecount; + nfit = -1; + for (i = 0, templatecount = 0, tp = x->x_template; + templatecount < ntemplate; i++) + { + float dotprod = 0; + for (k = 0, pp = powerout; + k < ninsig && templatecount < ntemplate; + k++, tp++, templatecount++) + { + for (j = NFILTERS, fp = tp->t_amp; + j--; fp++, pp++) + { + if (*fp < 0 || *pp < 0) post("bonk_tick bug 2"); + dotprod += *fp * *pp; + } + } + if (dotprod > bestfit) + { + bestfit = dotprod; + nfit = i; + } + } + if (nfit < 0) post("bonk_tick bug"); + } + else nfit = 0; } - else nfit = -1; /* hit is zero; this is the "bang" method. */ + else nfit = -1; /* hit is zero; this is the "bang" method. */ if (x->x_debug) - post("bonk out: number %d, vel %f, temperature %f", - nfit, vel, temperature); + post("bonk out: number %d, vel %f, temperature %f", + nfit, vel, temperature); SETFLOAT(at2, nfit); SETFLOAT(at2+1, vel); SETFLOAT(at2+2, temperature); outlet_list(x->x_cookedout, 0, 3, at2); for (n = 0, gp = x->x_insig + (ninsig-1), - pp = powerout + NFILTERS * (ninsig-1); - n < ninsig; n++, gp--, pp -= NFILTERS) + pp = powerout + NFILTERS * (ninsig-1); + n < ninsig; n++, gp--, pp -= NFILTERS) { - float *pp2; - for (i = 0, ap = at, pp2 = pp; i < NFILTERS; - i++, ap++, pp2++) - { - ap->a_type = A_FLOAT; - ap->a_w.w_float = *pp2; - } - outlet_list(gp->g_outlet, 0, NFILTERS, at); + float *pp2; + for (i = 0, ap = at, pp2 = pp; i < NFILTERS; + i++, ap++, pp2++) + { + ap->a_type = A_FLOAT; + ap->a_w.w_float = *pp2; + } + outlet_list(gp->g_outlet, 0, NFILTERS, at); } } @@ -389,88 +387,88 @@ static void bonk_doit(t_bonk *x) for (n = 0, gp = x->x_insig; n < ninsig; n++, gp++) { - for (i = NPOINTS, fp1 = gp->g_inbuf, fp2 = bonk_hanningwindow, - fp3 = windowbuf; i--; fp1++, fp2++, fp3++) - *fp3 = *fp1 * *fp2; - - for (i = 0, k = bonk_filterkernels, h = gp->g_hist; - i < NFILTERS; i++, k++, h++) - { - float power = 0, maskpow = h->h_mask; - int countup = h->h_countup; - int npoints = k->k_npoints; - /* special case: the fourth filter is centered */ - float *inbuf = gp->g_inbuf + - (i == 3 ? ((NPOINTS - npoints) / 2) : 0); - - /* run the filter repeatedly, sliding it forward by half its - length, stopping when it runs past the end of the buffer */ - for (fp1 = inbuf, fp2 = fp1 + NPOINTS - k->k_npoints; - fp1 <= fp2; fp1 += npoints/2) - { - float rsum = 0, isum = 0; - for (fp3 = fp1, fp4 = k->k_stuff, j = npoints; j--;) - { - float g = *fp3++; - rsum += g * *fp4++; - isum += g * *fp4++; - } - power += rsum * rsum + isum * isum; - } - - if (!x->x_willattack) h->h_before = maskpow; - - if (power > maskpow) - growth += power/(maskpow + 1.0e-15) - 1.f; - if (!x->x_willattack && countup >= x->x_masktime) - maskpow *= x->x_maskdecay; - - if (power > maskpow) - { - maskpow = power; - countup = 0; - } - countup++; - h->h_countup = countup; - h->h_mask = maskpow; - h->h_power = power; - } + for (i = NPOINTS, fp1 = gp->g_inbuf, fp2 = bonk_hanningwindow, + fp3 = windowbuf; i--; fp1++, fp2++, fp3++) + *fp3 = *fp1 * *fp2; + + for (i = 0, k = bonk_filterkernels, h = gp->g_hist; + i < NFILTERS; i++, k++, h++) + { + float power = 0, maskpow = h->h_mask; + int countup = h->h_countup; + int npoints = k->k_npoints; + /* special case: the fourth filter is centered */ + float *inbuf = gp->g_inbuf + + (i == 3 ? ((NPOINTS - npoints) / 2) : 0); + + /* run the filter repeatedly, sliding it forward by half its + length, stopping when it runs past the end of the buffer */ + for (fp1 = inbuf, fp2 = fp1 + NPOINTS - k->k_npoints; + fp1 <= fp2; fp1 += npoints/2) + { + float rsum = 0, isum = 0; + for (fp3 = fp1, fp4 = k->k_stuff, j = npoints; j--;) + { + float g = *fp3++; + rsum += g * *fp4++; + isum += g * *fp4++; + } + power += rsum * rsum + isum * isum; + } + + if (!x->x_willattack) h->h_before = maskpow; + + if (power > maskpow) + growth += power/(maskpow + 1.0e-15) - 1.f; + if (!x->x_willattack && countup >= x->x_masktime) + maskpow *= x->x_maskdecay; + + if (power > maskpow) + { + maskpow = power; + countup = 0; + } + countup++; + h->h_countup = countup; + h->h_mask = maskpow; + h->h_power = power; + } } if (x->x_willattack > 4) { - /* if it takes more than 4 analyses for the energy to stop growing, - forget it; we would rather miss the note than report it late. */ - if (x->x_debug) post("soft attack cancelled"); - x->x_willattack = 0; + /* if it takes more than 4 analyses for the energy to stop growing, + forget it; we would rather miss the note than report it late. */ + if (x->x_debug) post("soft attack cancelled"); + x->x_willattack = 0; } else if (x->x_willattack) { - if (growth < x->x_lothresh) - clock_delay(x->x_clock, 0); - else x->x_willattack++; + if (growth < x->x_lothresh) + clock_delay(x->x_clock, 0); + else x->x_willattack++; } else if (growth > x->x_hithresh) { - if (x->x_debug) post("attack; growth = %f", growth); - x->x_willattack = 1; - for (n = 0, gp = x->x_insig; n < ninsig; n++, gp++) - for (i = NFILTERS, h = gp->g_hist; i--; h++) - h->h_mask = h->h_power, h->h_countup = 0; + if (x->x_debug) post("attack; growth = %f", growth); + x->x_willattack = 1; + for (n = 0, gp = x->x_insig; n < ninsig; n++, gp++) + for (i = NFILTERS, h = gp->g_hist; i--; h++) + h->h_mask = h->h_power, h->h_countup = 0; } x->x_debouncevel *= x->x_debouncedecay; - /* shift the input buffer and update counters */ + /* shift the input buffer and update counters */ if (x->x_period > NPOINTS) x->x_countdown = x->x_period - NPOINTS; else x->x_countdown = 0; if (x->x_period < NPOINTS) { - int overlap = NPOINTS - x->x_period; - - for (n = 0, gp = x->x_insig; n < ninsig; n++, gp++) - for (i = overlap, fp1 = gp->g_inbuf, fp2 = fp1 + x->x_period; i--;) - *fp1++ = *fp2++; - x->x_infill = overlap; + int overlap = NPOINTS - x->x_period; + + for (n = 0, gp = x->x_insig; n < ninsig; n++, gp++) + for (i = overlap, fp1 = gp->g_inbuf, fp2 = fp1 + x->x_period; i--;) + *fp1++ = *fp2++; + x->x_infill = overlap; } else x->x_infill = 0; poodle = 1; @@ -484,18 +482,18 @@ static t_int *bonk_perform(t_int *w) if (x->x_countdown > 0) x->x_countdown -= n; else { - int i, j, infill = x->x_infill, ninsig = x->x_ninsig; - t_insig *gp; - for (i = 0, gp = x->x_insig; i < ninsig; i++, gp++) - { - float *fp = gp->g_inbuf + infill; - t_float *in1 = gp->g_invec + onset; - for (j = 0; j < n; j++) - *fp++ = *in1++; - } - infill += n; - x->x_infill = infill; - if (infill == NPOINTS) bonk_doit(x); + int i, j, infill = x->x_infill, ninsig = x->x_ninsig; + t_insig *gp; + for (i = 0, gp = x->x_insig; i < ninsig; i++, gp++) + { + float *fp = gp->g_inbuf + infill; + t_float *in1 = gp->g_invec + onset; + for (j = 0; j < n; j++) + *fp++ = *in1++; + } + infill += n; + x->x_infill = infill; + if (infill == NPOINTS) bonk_doit(x); } return (w+4); } @@ -507,16 +505,16 @@ static void bonk_dsp(t_bonk *x, t_signal **sp) if (vsize > n) vsize = n; for (i = 0, gp = x->x_insig; i < ninsig; i++, gp++) - gp->g_invec = (*(sp++))->s_vec; + gp->g_invec = (*(sp++))->s_vec; for (i = 0; i < n; i += vsize) - dsp_add(bonk_perform, 3, x, vsize, i); + dsp_add(bonk_perform, 3, x, vsize, i); } static void bonk_thresh(t_bonk *x, t_floatarg f1, t_floatarg f2) { if (f1 > f2) - post("bonk: warning: low threshold greater than hi threshold"); + post("bonk: warning: low threshold greater than hi threshold"); x->x_lothresh = f1; x->x_hithresh = f2; } @@ -553,36 +551,36 @@ static void bonk_print(t_bonk *x, t_floatarg f) post("minvel %f", x->x_minvel); if (x->x_ntemplate) { - post("templates:"); - for (i = 0; i < x->x_ntemplate; i++) - post("%2d \ + post("templates:"); + for (i = 0; i < x->x_ntemplate; i++) + post("%2d \ %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f", i, - x->x_template[i].t_amp[0], - x->x_template[i].t_amp[1], - x->x_template[i].t_amp[2], - x->x_template[i].t_amp[3], - x->x_template[i].t_amp[4], - x->x_template[i].t_amp[5], - x->x_template[i].t_amp[6], - x->x_template[i].t_amp[7], - x->x_template[i].t_amp[8], - x->x_template[i].t_amp[9], - x->x_template[i].t_amp[10]); + x->x_template[i].t_amp[0], + x->x_template[i].t_amp[1], + x->x_template[i].t_amp[2], + x->x_template[i].t_amp[3], + x->x_template[i].t_amp[4], + x->x_template[i].t_amp[5], + x->x_template[i].t_amp[6], + x->x_template[i].t_amp[7], + x->x_template[i].t_amp[8], + x->x_template[i].t_amp[9], + x->x_template[i].t_amp[10]); } else post("no templates"); if (x->x_learn) post("learn mode"); if (f != 0) { - int j, ninsig = x->x_ninsig; - t_insig *gp; - for (j = 0, gp = x->x_insig; j < ninsig; j++, gp++) - { - t_hist *h; - if (ninsig > 1) post("input %d:", j+1); - for (i = NFILTERS, h = gp->g_hist; i--; h++) - post("pow %f mask %f before %f count %d", - h->h_power, h->h_mask, h->h_before, h->h_countup); - } + int j, ninsig = x->x_ninsig; + t_insig *gp; + for (j = 0, gp = x->x_insig; j < ninsig; j++, gp++) + { + t_hist *h; + if (ninsig > 1) post("input %d:", j+1); + for (i = NFILTERS, h = gp->g_hist; i--; h++) + post("pow %f mask %f before %f count %d", + h->h_power, h->h_mask, h->h_before, h->h_countup); + } } if (x->x_debug) post("debug mode"); } @@ -598,9 +596,9 @@ static void bonk_learn(t_bonk *x, t_floatarg f) if (n < 0) n = 0; if (n) { - x->x_template = (t_template *)t_resizebytes(x->x_template, - x->x_ntemplate * sizeof(x->x_template[0]), 0); - x->x_ntemplate = 0; + x->x_template = (t_template *)t_resizebytes(x->x_template, + x->x_ntemplate * sizeof(x->x_template[0]), 0); + x->x_ntemplate = 0; } x->x_learn = n; x->x_learncount = 0; @@ -611,8 +609,8 @@ static void bonk_forget(t_bonk *x) int ntemplate = x->x_ntemplate, newn = ntemplate - x->x_ninsig; if (newn < 0) newn = 0; x->x_template = (t_template *)t_resizebytes(x->x_template, - x->x_ntemplate * sizeof(x->x_template[0]), - newn * sizeof(x->x_template[0])); + x->x_ntemplate * sizeof(x->x_template[0]), + newn * sizeof(x->x_template[0])); x->x_ntemplate = newn; x->x_learncount = 0; } @@ -630,12 +628,12 @@ static void bonk_bang(t_bonk *x) outlet_list(x->x_cookedout, 0L, 3, at2); for (i = 0, gp = x->x_insig + (ninsig-1); i < ninsig; i++, gp--) { - for (j = 0; j < NFILTERS; j++) - { - at[j].a_type = A_FLOAT; - at[j].a_w.w_float = 100 * qrsqrt(qrsqrt(gp->g_hist[j].h_power)); - } - outlet_list(gp->g_outlet, 0L, NFILTERS, at); + for (j = 0; j < NFILTERS; j++) + { + at[j].a_type = A_FLOAT; + at[j].a_w.w_float = 100 * qrsqrt(qrsqrt(gp->g_hist[j].h_power)); + } + outlet_list(gp->g_outlet, 0L, NFILTERS, at); } } #endif @@ -651,22 +649,22 @@ static void bonk_setupkernels(void) float *fp; for (i = 0; i < NFILTERS; i++) { - int npoints = bonk_filterkernels[i].k_npoints; - float freq = bonk_filterkernels[i].k_freq; - float normalize = bonk_filterkernels[i].k_normalize; - float phaseinc = (2.f * 3.14159f) / npoints; - bonk_filterkernels[i].k_stuff = - (float *)getbytes(2 * sizeof(float) * npoints); - for (fp = bonk_filterkernels[i].k_stuff, j = npoints; j--;) - { - float phase = j * phaseinc; - float window = normalize * (0.5f - 0.5f * cos(phase)); - *fp++ = window * cos(freq * phase); - *fp++ = window * sin(freq * phase); - } + int npoints = bonk_filterkernels[i].k_npoints; + float freq = bonk_filterkernels[i].k_freq; + float normalize = bonk_filterkernels[i].k_normalize; + float phaseinc = (2.f * 3.14159f) / npoints; + bonk_filterkernels[i].k_stuff = + (float *)getbytes(2 * sizeof(float) * npoints); + for (fp = bonk_filterkernels[i].k_stuff, j = npoints; j--;) + { + float phase = j * phaseinc; + float window = normalize * (0.5f - 0.5f * cos(phase)); + *fp++ = window * cos(freq * phase); + *fp++ = window * sin(freq * phase); + } } for (i = 0; i < NPOINTS; i++) - bonk_hanningwindow[i] = (0.5f - 0.5f * cos(i * (2*3.14159)/NPOINTS)); + bonk_hanningwindow[i] = (0.5f - 0.5f * cos(i * (2*3.14159)/NPOINTS)); } #ifdef PD @@ -678,31 +676,31 @@ static void bonk_read(t_bonk *x, t_symbol *s) float *fp, *fp2; if (!fd) { - post("%s: open failed", s->s_name); - return; + post("%s: open failed", s->s_name); + return; } x->x_template = (t_template *)t_resizebytes(x->x_template, - x->x_ntemplate * sizeof(t_template), 0); + x->x_ntemplate * sizeof(t_template), 0); while (1) { - for (i = NFILTERS, fp = vec; i--; fp++) - if (fscanf(fd, "%f", fp) < 1) goto nomore; - x->x_template = (t_template *)t_resizebytes(x->x_template, - ntemplate * sizeof(t_template), - (ntemplate + 1) * sizeof(t_template)); - for (i = NFILTERS, fp = vec, - fp2 = x->x_template[ntemplate].t_amp; i--;) - *fp2++ = *fp++; - ntemplate++; + for (i = NFILTERS, fp = vec; i--; fp++) + if (fscanf(fd, "%f", fp) < 1) goto nomore; + x->x_template = (t_template *)t_resizebytes(x->x_template, + ntemplate * sizeof(t_template), + (ntemplate + 1) * sizeof(t_template)); + for (i = NFILTERS, fp = vec, + fp2 = x->x_template[ntemplate].t_amp; i--;) + *fp2++ = *fp++; + ntemplate++; } nomore: if (remaining = (ntemplate % x->x_ninsig)) { - post("bonk_read: %d templates not a multiple of %d; dropping extras"); - x->x_template = (t_template *)t_resizebytes(x->x_template, - ntemplate * sizeof(t_template), - (ntemplate - remaining) * sizeof(t_template)); - ntemplate = ntemplate - remaining; + post("bonk_read: %d templates not a multiple of %d; dropping extras"); + x->x_template = (t_template *)t_resizebytes(x->x_template, + ntemplate * sizeof(t_template), + (ntemplate - remaining) * sizeof(t_template)); + ntemplate = ntemplate - remaining; } post("bonk: read %d templates\n", ntemplate); x->x_ntemplate = ntemplate; @@ -711,7 +709,7 @@ nomore: #endif /* PD */ #ifdef MSP -static void bonk_read(t_bonk *x, t_symbol *s) // MSP +static void bonk_read(t_bonk *x, t_symbol *s) { SFTypeList types; short vol = 0; @@ -723,125 +721,122 @@ static void bonk_read(t_bonk *x, t_symbol *s) // MSP int i, ntemplate = 0; float vec[NFILTERS]; float *fp, *fp2; - if (s->s_name[0]) // if it is named + if (s->s_name[0]) { - vol = defvolume(); - strcpy (name, s->s_name); - - if (readtohandle (name, vol, &buf, &size) != 0) - - { - post("bonk~: problem with reading file."); - return; - - } - else - { - post("bonk~: template read successfully."); - //post("bonk~: size of file is %d", size); - } - for (eaten = 0; ;) - { - for (i = NFILTERS, fp = vec; i--; fp++) - { - while (eaten < size && ( - (*buf)[eaten] == ' ' || - (*buf)[eaten] == '\t' || - (*buf)[eaten] == '\n' || - (*buf)[eaten] == ';' || - (*buf)[eaten] == '\r')) - eaten++; - if (eaten >= size) goto nomore; - if (sscanf(&(*buf)[eaten], "%f", fp) < 1) goto nomore; - - while (eaten < size && !( - (*buf)[eaten] == ' ' || - (*buf)[eaten] == '\t' || - (*buf)[eaten] == '\n' || - (*buf)[eaten] == ';' || - (*buf)[eaten] == '\r')) - eaten++; - } - x->x_template = (t_template *)t_resizebytes(x->x_template, - - ntemplate * sizeof(t_template), - (ntemplate + 1) * sizeof(t_template)); - - for (i = NFILTERS, fp = vec, - fp2 = x->x_template[ntemplate].t_amp; i--;) - *fp2++ = *fp++; - ntemplate++; - post("bonk~: fp = %f", fp); - } + vol = defvolume(); + strcpy (name, s->s_name); + + if (readtohandle (name, vol, &buf, &size) != 0) + + { + post("bonk~: problem with reading file."); + return; + + } + else + { + post("bonk~: template read successfully."); + } + for (eaten = 0; ;) + { + for (i = NFILTERS, fp = vec; i--; fp++) + { + while (eaten < size && ( + (*buf)[eaten] == ' ' || + (*buf)[eaten] == '\t' || + (*buf)[eaten] == '\n' || + (*buf)[eaten] == ';' || + (*buf)[eaten] == '\r')) + eaten++; + if (eaten >= size) goto nomore; + if (sscanf(&(*buf)[eaten], "%f", fp) < 1) goto nomore; + + while (eaten < size && !( + (*buf)[eaten] == ' ' || + (*buf)[eaten] == '\t' || + (*buf)[eaten] == '\n' || + (*buf)[eaten] == ';' || + (*buf)[eaten] == '\r')) + eaten++; + } + x->x_template = (t_template *)t_resizebytes(x->x_template, + + ntemplate * sizeof(t_template), + (ntemplate + 1) * sizeof(t_template)); + + for (i = NFILTERS, fp = vec, + fp2 = x->x_template[ntemplate].t_amp; i--;) + *fp2++ = *fp++; + ntemplate++; + post("bonk~: fp = %f", fp); + } } else { - name[0] = 0; - types[0]='TEXT'; - types[1]='maxb'; - - open_promptset("Select template for reading."); - - if (open_dialog(name, &vol, &type, types, 2)) - { - post("bonk~: open canceled"); - return; - } - x->x_template = (t_template *)t_resizebytes(x->x_template, - - x->x_ntemplate * sizeof(t_template), 0); - - // post("bonk~: the file name is %s", name); - - if (readtohandle (name, vol, &buf, &size) != 0) - - { - post("bonk~: problem with reading file."); - return; - - } - else - { - post("bonk~: template read successfully."); - // post("bonk~: size of file is %d", size); - } - for (eaten = 0; ;) - { - for (i = NFILTERS, fp = vec; i--; fp++) - { - while (eaten < size && ( - (*buf)[eaten] == ' ' || - (*buf)[eaten] == '\t' || - (*buf)[eaten] == '\n' || - (*buf)[eaten] == ';' || - (*buf)[eaten] == '\r')) - eaten++; - if (eaten >= size) goto nomore; - if (sscanf(&(*buf)[eaten], "%f", fp) < 1) goto nomore; - - while (eaten < size && !( - (*buf)[eaten] == ' ' || - (*buf)[eaten] == '\t' || - (*buf)[eaten] == '\n' || - (*buf)[eaten] == ';' || - (*buf)[eaten] == '\r')) - eaten++; - } - x->x_template = (t_template *)t_resizebytes(x->x_template, - - ntemplate * sizeof(t_template), - (ntemplate + 1) * sizeof(t_template)); - - for (i = NFILTERS, fp = vec, - fp2 = x->x_template[ntemplate].t_amp; i--;) - *fp2++ = *fp++; - ntemplate++; - } - nomore: - post("bonk~: read %d templates", ntemplate); - - x->x_ntemplate = ntemplate; - } // end of else + name[0] = 0; + types[0]='TEXT'; + types[1]='maxb'; + + open_promptset("Select template for reading."); + + if (open_dialog(name, &vol, &type, types, 2)) + { + post("bonk~: open canceled"); + return; + } + x->x_template = (t_template *)t_resizebytes(x->x_template, + + x->x_ntemplate * sizeof(t_template), 0); + + + if (readtohandle (name, vol, &buf, &size) != 0) + + { + post("bonk~: problem with reading file."); + return; + + } + else + { + post("bonk~: template read successfully."); + } + for (eaten = 0; ;) + { + for (i = NFILTERS, fp = vec; i--; fp++) + { + while (eaten < size && ( + (*buf)[eaten] == ' ' || + (*buf)[eaten] == '\t' || + (*buf)[eaten] == '\n' || + (*buf)[eaten] == ';' || + (*buf)[eaten] == '\r')) + eaten++; + if (eaten >= size) goto nomore; + if (sscanf(&(*buf)[eaten], "%f", fp) < 1) goto nomore; + + while (eaten < size && !( + (*buf)[eaten] == ' ' || + (*buf)[eaten] == '\t' || + (*buf)[eaten] == '\n' || + (*buf)[eaten] == ';' || + (*buf)[eaten] == '\r')) + eaten++; + } + x->x_template = (t_template *)t_resizebytes(x->x_template, + + ntemplate * sizeof(t_template), + (ntemplate + 1) * sizeof(t_template)); + + for (i = NFILTERS, fp = vec, + fp2 = x->x_template[ntemplate].t_amp; i--;) + *fp2++ = *fp++; + ntemplate++; + } + nomore: + post("bonk~: read %d templates", ntemplate); + + x->x_ntemplate = ntemplate; + } } #endif /* MSP */ @@ -854,14 +849,14 @@ static void bonk_write(t_bonk *x, t_symbol *s) float *fp; if (!fd) { - post("%s: couldn't create", s->s_name); - return; + post("%s: couldn't create", s->s_name); + return; } for (; ntemplate--; tp++) { - for (i = NFILTERS, fp = tp->t_amp; i--; fp++) - fprintf(fd, "%6.2f ", *fp); - fprintf(fd, "\n"); + for (i = NFILTERS, fp = tp->t_amp; i--; fp++) + fprintf(fd, "%6.2f ", *fp); + fprintf(fd, "\n"); } post("bonk: wrote %d templates\n", x->x_ntemplate); fclose(fd); @@ -869,54 +864,54 @@ static void bonk_write(t_bonk *x, t_symbol *s) #endif /* PD */ #ifdef MSP -static void bonk_write(t_bonk *x, t_symbol *s) // MSP +static void bonk_write(t_bonk *x, t_symbol *s) { char fn[236]; short vol; - short bin = 0; // 0 = text + short bin = 0; void* b; int i, ntemplate = x->x_ntemplate; t_template *tp = x->x_template; - if (s->s_name[0]) // if it is named + if (s->s_name[0]) { - strcpy (fn, s->s_name); - vol = defvolume(); - b = binbuf_new(); - for (; ntemplate--; tp++) - { - int i; - Atom at[11]; - for (i = 0; i < 11; i++) - at[i].a_type = A_FLOAT, at[i].a_w.w_float = tp->t_amp[i]; - binbuf_insert(b, 0L, 11, at); - } - binbuf_write(b, fn, vol, bin); - freeobject(b); - post("bonk~: wrote file %s", fn); + strcpy (fn, s->s_name); + vol = defvolume(); + b = binbuf_new(); + for (; ntemplate--; tp++) + { + int i; + Atom at[11]; + for (i = 0; i < 11; i++) + at[i].a_type = A_FLOAT, at[i].a_w.w_float = tp->t_amp[i]; + binbuf_insert(b, 0L, 11, at); + } + binbuf_write(b, fn, vol, bin); + freeobject(b); + post("bonk~: wrote file %s", fn); } else { - saveas_promptset("Save Template file as"); - strcpy(fn, ""); - if (!saveas_dialog(fn, &vol, 0L)) - { - b = binbuf_new(); - for (; ntemplate--; tp++) - { - int i; - Atom at[11]; - for (i = 0; i < 11; i++) - at[i].a_type = A_FLOAT, at[i].a_w.w_float = - tp->t_amp[i]; - binbuf_insert(b, 0L, 11, at); - } - binbuf_write(b, fn, vol, bin); - freeobject(b); - post("bonk~: wrote file %s", fn); - } - } // end of else + saveas_promptset("Save Template file as"); + strcpy(fn, ""); + if (!saveas_dialog(fn, &vol, 0L)) + { + b = binbuf_new(); + for (; ntemplate--; tp++) + { + int i; + Atom at[11]; + for (i = 0; i < 11; i++) + at[i].a_type = A_FLOAT, at[i].a_w.w_float = + tp->t_amp[i]; + binbuf_insert(b, 0L, 11, at); + } + binbuf_write(b, fn, vol, bin); + freeobject(b); + post("bonk~: wrote file %s", fn); + } + } } #endif /* MSP */ @@ -925,7 +920,7 @@ static void bonk_free(t_bonk *x) int i, ninsig = x->x_ninsig; t_insig *gp = x->x_insig; for (i = 0, gp = x->x_insig; i < ninsig; i++, gp++) - freebytes(gp->g_inbuf, NPOINTS * sizeof(float)); + freebytes(gp->g_inbuf, NPOINTS * sizeof(float)); clock_free(x->x_clock); } @@ -944,9 +939,9 @@ static void *bonk_new(t_floatarg fperiod, t_floatarg fnsig) x->x_insig = (t_insig *)getbytes(nsig * sizeof(*x->x_insig)); for (j = 0, g = x->x_insig; j < nsig; j++, g++) { - g->g_outlet = outlet_new(&x->x_obj, gensym("list")); - if (j) - inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); + g->g_outlet = outlet_new(&x->x_obj, gensym("list")); + if (j) + inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); } x->x_cookedout = outlet_new(&x->x_obj, gensym("list")); bonk_donew(x, fperiod, nsig); @@ -956,29 +951,29 @@ static void *bonk_new(t_floatarg fperiod, t_floatarg fnsig) void bonk_tilde_setup(void) { bonk_class = class_new(gensym("bonk~"), (t_newmethod)bonk_new, 0, - sizeof(t_bonk), 0, A_DEFFLOAT, A_DEFFLOAT, 0); + sizeof(t_bonk), 0, A_DEFFLOAT, A_DEFFLOAT, 0); class_addmethod(bonk_class, nullfn, gensym("signal"), 0); class_addmethod(bonk_class, (t_method)bonk_dsp, gensym("dsp"), 0); class_addbang(bonk_class, bonk_bang); class_addmethod(bonk_class, (t_method)bonk_learn, gensym("learn"), - A_FLOAT, 0); + A_FLOAT, 0); class_addmethod(bonk_class, (t_method)bonk_forget, gensym("forget"), 0); class_addmethod(bonk_class, (t_method)bonk_thresh, gensym("thresh"), - A_FLOAT, A_FLOAT, 0); + A_FLOAT, A_FLOAT, 0); class_addmethod(bonk_class, (t_method)bonk_mask, gensym("mask"), - A_FLOAT, A_FLOAT, 0); + A_FLOAT, A_FLOAT, 0); class_addmethod(bonk_class, (t_method)bonk_debounce, gensym("debounce"), - A_FLOAT, 0); + A_FLOAT, 0); class_addmethod(bonk_class, (t_method)bonk_minvel, gensym("minvel"), - A_FLOAT, 0); + A_FLOAT, 0); class_addmethod(bonk_class, (t_method)bonk_print, gensym("print"), - A_DEFFLOAT, 0); + A_DEFFLOAT, 0); class_addmethod(bonk_class, (t_method)bonk_debug, gensym("debug"), - A_DEFFLOAT, 0); + A_DEFFLOAT, 0); class_addmethod(bonk_class, (t_method)bonk_read, gensym("read"), - A_SYMBOL, 0); + A_SYMBOL, 0); class_addmethod(bonk_class, (t_method)bonk_write, gensym("write"), - A_SYMBOL, 0); + A_SYMBOL, 0); bonk_setupkernels(); post("bonk version 1.1 TEST 3"); } @@ -992,8 +987,8 @@ static int ilog2(int n) int ret = -1; while (n) { - n >>= 1; - ret++; + n >>= 1; + ret++; } return (ret); } @@ -1003,7 +998,7 @@ static char *strcpy(char *s1, const char *s2) char *ret = s1; while ((*s1++ = *s2++) != 0) - ; + ; return ret; } @@ -1019,11 +1014,11 @@ static void *bonk_new(int period, int nsig) if (nsig < 1) nsig = 1; if (nsig > MAXCHANNELS) nsig = MAXCHANNELS; x->x_insig = (t_insig *)getbytes(nsig * sizeof(*x->x_insig)); - dsp_setup((t_pxobject *)x, nsig); // nsig inputs + dsp_setup((t_pxobject *)x, nsig); x->x_cookedout = listout((t_object *)x); for (j = 0, g = x->x_insig + nsig-1; j < nsig; j++, g--) { - g->g_outlet = listout((t_object *)x); + g->g_outlet = listout((t_object *)x); } x->x_cookedout = listout((t_object *)x); x->x_clock = clock_new(x, (method)bonk_tick); @@ -1034,31 +1029,30 @@ static void *bonk_new(int period, int nsig) void main() { - setup(&bonk_class, bonk_new, (method)bonk_free, - (short)sizeof(t_bonk), 0L, A_DEFLONG, A_DEFLONG, 0); - addmess((method)bonk_dsp, "dsp", 0); - addbang((method)bonk_bang); - addmess((method)bonk_forget, "forget", 0); - addmess((method)bonk_learn, "learn", A_FLOAT, 0); - addmess((method)bonk_thresh, "thresh", A_FLOAT, A_FLOAT, 0); - addmess((method)bonk_mask, "mask", A_FLOAT, A_FLOAT, 0); - addmess((method)bonk_minvel, "minvel", A_FLOAT, 0); - addmess((method)bonk_debounce, "debounce", A_FLOAT, 0); - addmess((method)bonk_print, "print", A_DEFFLOAT, 0); - addmess((method)bonk_read, "read", A_DEFSYM, 0); - addmess((method)bonk_write, "write", A_DEFSYM, 0); - addmess((method)bonk_assist, "assist", A_CANT, 0); - addmess((method)bonk_debug, "debug", A_FLOAT, 0); - bonk_setupkernels(); -// post("bonk~ v1.00 Miller Puckette, Ted Apel"); - post("bonk~ v1.00"); - dsp_initclass(); - rescopy('STR#',3747); + setup(&bonk_class, bonk_new, (method)bonk_free, + (short)sizeof(t_bonk), 0L, A_DEFLONG, A_DEFLONG, 0); + addmess((method)bonk_dsp, "dsp", 0); + addbang((method)bonk_bang); + addmess((method)bonk_forget, "forget", 0); + addmess((method)bonk_learn, "learn", A_FLOAT, 0); + addmess((method)bonk_thresh, "thresh", A_FLOAT, A_FLOAT, 0); + addmess((method)bonk_mask, "mask", A_FLOAT, A_FLOAT, 0); + addmess((method)bonk_minvel, "minvel", A_FLOAT, 0); + addmess((method)bonk_debounce, "debounce", A_FLOAT, 0); + addmess((method)bonk_print, "print", A_DEFFLOAT, 0); + addmess((method)bonk_read, "read", A_DEFSYM, 0); + addmess((method)bonk_write, "write", A_DEFSYM, 0); + addmess((method)bonk_assist, "assist", A_CANT, 0); + addmess((method)bonk_debug, "debug", A_FLOAT, 0); + bonk_setupkernels(); + post("bonk~ v1.00"); + dsp_initclass(); + rescopy('STR#',3747); } void bonk_assist(t_bonk *x, void *b, long m, long a, char *s) { - assist_string(3747,m,a,1,2,s); + assist_string(3747,m,a,1,2,s); } /* get current system time */ @@ -1077,7 +1071,7 @@ double clock_gettimesince(double prevsystime) float qrsqrt(float f) { - return 1/sqrt(f); + return 1/sqrt(f); } #endif /* MSP */ diff --git a/pd/extra/choice/choice.c b/pd/extra/choice/choice.c index 002dac40..b7bf23ab 100644 --- a/pd/extra/choice/choice.c +++ b/pd/extra/choice/choice.c @@ -50,12 +50,12 @@ static void choice_print(t_choice *x) int j; for (j = 0; j < x->x_n; j++) { - t_elem *e = x->x_vec + j; - t_float *w = e->e_weight; - post("%2d age %2d \ + t_elem *e = x->x_vec + j; + t_float *w = e->e_weight; + post("%2d age %2d \ w %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f", - j, (int)(e->e_age), w[0], w[1], w[2], w[3], w[4], w[5], - w[6], w[7], w[8], w[9]); + j, (int)(e->e_age), w[0], w[1], w[2], w[3], w[4], w[5], + w[6], w[7], w[8], w[9]); } } @@ -65,20 +65,20 @@ static void choice_add(t_choice *x, t_symbol *s, int argc, t_atom *argv) t_elem *e; float sum, normal; x->x_vec = (t_elem *)resizebytes(x->x_vec, oldn * sizeof(t_elem), - newn * sizeof(t_elem)); + newn * sizeof(t_elem)); x->x_n = newn; e = x->x_vec + oldn; e->e_age = 2; for (i = 0, sum = 0; i < DIMENSION; i++) { - float f = atom_getfloatarg(i, argc, argv); - e->e_weight[i] = f; - sum += f*f; + float f = atom_getfloatarg(i, argc, argv); + e->e_weight[i] = f; + sum += f*f; } normal = (float)(sum > 0 ? 1./sqrt(sum) : 1); for (i = 0; i < DIMENSION; i++) - e->e_weight[i] *= normal; + e->e_weight[i] *= normal; } static void choice_list(t_choice *x, t_symbol *s, int argc, t_atom *argv) @@ -88,26 +88,26 @@ static void choice_list(t_choice *x, t_symbol *s, int argc, t_atom *argv) int bestindex = -1; t_float invec[DIMENSION]; for (i = 0; i < DIMENSION; i++) - invec[i] = atom_getfloatarg(i, argc, argv); + invec[i] = atom_getfloatarg(i, argc, argv); for (j = 0; j < x->x_n; j++) { - t_elem *e = x->x_vec + j; - float sum; - for (i = 0, sum = 0; i < DIMENSION; i++) - sum += e->e_weight[i] * invec[i]; - if (x->x_nonrepeat) sum *= (float)(log(e->e_age)); - if (sum > bestsum) - { - bestsum = sum; - sum = 1; - bestindex = j; - } + t_elem *e = x->x_vec + j; + float sum; + for (i = 0, sum = 0; i < DIMENSION; i++) + sum += e->e_weight[i] * invec[i]; + if (x->x_nonrepeat) sum *= (float)(log(e->e_age)); + if (sum > bestsum) + { + bestsum = sum; + sum = 1; + bestindex = j; + } } if (bestindex >= 0) { - for (j = 0; j < x->x_n; j++) - x->x_vec[j].e_age += 1.; - x->x_vec[bestindex].e_age = 1; + for (j = 0; j < x->x_n; j++) + x->x_vec[j].e_age += 1.; + x->x_vec[bestindex].e_age = 1; } outlet_float(x->x_obj.ob_outlet, (float)bestindex); } @@ -120,7 +120,7 @@ static void choice_free(t_choice *x) void choice_setup(void) { choice_class = class_new(gensym("choice"), (t_newmethod)choice_new, - (t_method)choice_free, sizeof(t_choice), 0, A_DEFFLOAT, 0); + (t_method)choice_free, sizeof(t_choice), 0, A_DEFFLOAT, 0); class_addmethod(choice_class, (t_method)choice_add, gensym("add"), A_GIMME, 0); class_addmethod(choice_class, (t_method)choice_clear, gensym("clear"), 0); class_addmethod(choice_class, (t_method)choice_print, gensym("print"), 0); diff --git a/pd/extra/expr~/fts_to_pd.h b/pd/extra/expr~/fts_to_pd.h index 57b0382c..9ca2fc42 100644 --- a/pd/extra/expr~/fts_to_pd.h +++ b/pd/extra/expr~/fts_to_pd.h @@ -14,14 +14,14 @@ permission is granted to use this file for any purpose. typedef t_symbol *fts_symbol_t; #ifdef MSP -#define t_atom Atom -#define t_symbol Symbol -#define pd_new(x) newobject(x); -#define pd_free(x) freeobject(x); -#define t_outlet void -#define t_binbuf void -typedef t_class *t_pd; -typedef float t_floatarg; +#define t_atom Atom +#define t_symbol Symbol +#define pd_new(x) newobject(x); +#define pd_free(x) freeobject(x); +#define t_outlet void +#define t_binbuf void +typedef t_class *t_pd; +typedef float t_floatarg; #include #include diff --git a/pd/extra/expr~/makefile b/pd/extra/expr~/makefile index 042380dd..ff1dae4b 100644 --- a/pd/extra/expr~/makefile +++ b/pd/extra/expr~/makefile @@ -81,7 +81,7 @@ LINUXOBJ = vexp.pd_linux_o vexp_fun.pd_linux_o vexp_if.pd_linux_o LINUXCFLAGS = -DPD -O2 -funroll-loops -fomit-frame-pointer -fPIC \ -Wall -W -Wshadow -Wstrict-prototypes \ - -Wno-unused -Wno-parentheses -Wno-switch $(CFLAGS) + -Wno-unused -Wno-parentheses -Wno-switch LINUXINCLUDE = -I../../src diff --git a/pd/extra/expr~/vexp.c b/pd/extra/expr~/vexp.c index c0001e1f..7d4d7b52 100644 --- a/pd/extra/expr~/vexp.c +++ b/pd/extra/expr~/vexp.c @@ -28,16 +28,16 @@ /* "expr~" and "fexpr~" conversion by Shahrokh Yadegari c. 1999,2000 */ /* - * Feb 2002 - added access to variables - * multiple expression support - * new short hand forms for fexpr~ - * now $y or $y1 = $y1[-1] and $y2 = $y2[-1] - * --sdy + * Feb 2002 - added access to variables + * multiple expression support + * new short hand forms for fexpr~ + * now $y or $y1 = $y1[-1] and $y2 = $y2[-1] + * --sdy * * July 2002 - * fixed bugs introduced in last changes in store and ET_EQ - * --sdy - * + * fixed bugs introduced in last changes in store and ET_EQ + * --sdy + * */ /* @@ -73,7 +73,7 @@ #include "vexp.h" #ifdef MSP #undef isdigit -#define isdigit(x) (x >= '0' && x <= '9') +#define isdigit(x) (x >= '0' && x <= '9') #endif char *atoif(char *s, long int *value, long int *type); @@ -81,30 +81,30 @@ char *atoif(char *s, long int *value, long int *type); static struct ex_ex *ex_lex(struct expr *expr, long int *n); struct ex_ex *ex_match(struct ex_ex *eptr, long int op); struct ex_ex *ex_parse(struct expr *expr, struct ex_ex *iptr, - struct ex_ex *optr, long int *argc); + struct ex_ex *optr, long int *argc); struct ex_ex *ex_eval(struct expr *expr, struct ex_ex *eptr, - struct ex_ex *optr, int i); + struct ex_ex *optr, int i); int expr_donew(struct expr *exprr, int ac, t_atom *av); struct ex_ex *eval_func(struct expr *expr,struct ex_ex *eptr, - struct ex_ex *optr, int i); + struct ex_ex *optr, int i); struct ex_ex *eval_tab(struct expr *expr, struct ex_ex *eptr, - struct ex_ex *optr, int i); + struct ex_ex *optr, int i); struct ex_ex *eval_var(struct expr *expr, struct ex_ex *eptr, - struct ex_ex *optr, int i); + struct ex_ex *optr, int i); struct ex_ex *eval_store(struct expr *expr, struct ex_ex *eptr, - struct ex_ex *optr, int i); + struct ex_ex *optr, int i); struct ex_ex *eval_sigidx(struct expr *expr, struct ex_ex *eptr, - struct ex_ex *optr, int i); -static int cal_sigidx(struct ex_ex *optr, /* The output value */ - int i, float rem_i, /* integer and fractinal part of index */ - int idx, /* index of current fexpr~ processing */ - int vsize, /* vector size */ - float *curvec, float *prevec); /* current and previous table */ + struct ex_ex *optr, int i); +static int cal_sigidx(struct ex_ex *optr, /* The output value */ + int i, float rem_i, /* integer and fractinal part of index */ + int idx, /* index of current fexpr~ processing */ + int vsize, /* vector size */ + float *curvec, float *prevec); /* current and previous table */ t_ex_func *find_func(char *s); void ex_dzdetect(struct expr *expr); -#define MAX_ARGS 10 +#define MAX_ARGS 10 extern t_ex_func ex_funcs[]; struct ex_ex nullex; @@ -181,7 +181,7 @@ void atom_string(t_atom *a, char *buf, unsigned int bufsize) break; case A_DOLLSYM: sprintf(buf, "$%s", a->a_w.w_symbol->s_name); - break; + break; #else /* MAX */ case A_DOLLAR: sprintf(buf, "$%s", a->a_w.w_symbol->s_name); @@ -194,24 +194,24 @@ void atom_string(t_atom *a, char *buf, unsigned int bufsize) #endif /* MSP */ /* * expr_donew -- create a new "expr" object. - * returns 1 on failure, 0 on success. + * returns 1 on failure, 0 on success. */ int expr_donew(struct expr *expr, int ac, t_atom *av) { - struct ex_ex *list; - struct ex_ex *ret; - long max_node = 0; /* maximum number of nodes needed */ - char *exp_string; - int exp_strlen; - t_binbuf *b; - int i; - - memset(expr->exp_var, 0, MAX_VARS * sizeof (*expr->exp_var)); + struct ex_ex *list; + struct ex_ex *ret; + long max_node = 0; /* maximum number of nodes needed */ + char *exp_string; + int exp_strlen; + t_binbuf *b; + int i; + + memset(expr->exp_var, 0, MAX_VARS * sizeof (*expr->exp_var)); #ifdef PD - b = binbuf_new(); - binbuf_add(b, ac, av); - binbuf_gettext(b, &exp_string, &exp_strlen); + b = binbuf_new(); + binbuf_add(b, ac, av); + binbuf_gettext(b, &exp_string, &exp_strlen); #else /* MSP */ { @@ -246,657 +246,657 @@ expr_donew(struct expr *expr, int ac, t_atom *av) exp_strlen = length; } #endif - exp_string = (char *)t_resizebytes(exp_string, exp_strlen,exp_strlen+1); - exp_string[exp_strlen] = 0; - expr->exp_string = exp_string; - expr->exp_str = exp_string; - expr->exp_nexpr = 0; - ret = (struct ex_ex *) 0; - /* - * if ret == 0 it means that we have no expression - * so we let the pass go through to build a single null stack - */ - while (*expr->exp_str || !ret) { - list = ex_lex(expr, &max_node); - if (!list) { /* syntax error */ - goto error; - } - expr->exp_stack[expr->exp_nexpr] = - (struct ex_ex *)fts_malloc(max_node * sizeof (struct ex_ex)); - expr->exp_nexpr++; - ret = ex_match(list, (long)0); - if (!ret) /* syntax error */ - goto error; - ret = ex_parse(expr, - list, expr->exp_stack[expr->exp_nexpr - 1], (long *)0); - if (!ret) - goto error; - } - *ret = nullex; - t_freebytes(exp_string, exp_strlen+1); - return (0); + exp_string = (char *)t_resizebytes(exp_string, exp_strlen,exp_strlen+1); + exp_string[exp_strlen] = 0; + expr->exp_string = exp_string; + expr->exp_str = exp_string; + expr->exp_nexpr = 0; + ret = (struct ex_ex *) 0; + /* + * if ret == 0 it means that we have no expression + * so we let the pass go through to build a single null stack + */ + while (*expr->exp_str || !ret) { + list = ex_lex(expr, &max_node); + if (!list) { /* syntax error */ + goto error; + } + expr->exp_stack[expr->exp_nexpr] = + (struct ex_ex *)fts_malloc(max_node * sizeof (struct ex_ex)); + expr->exp_nexpr++; + ret = ex_match(list, (long)0); + if (!ret) /* syntax error */ + goto error; + ret = ex_parse(expr, + list, expr->exp_stack[expr->exp_nexpr - 1], (long *)0); + if (!ret) + goto error; + } + *ret = nullex; + t_freebytes(exp_string, exp_strlen+1); + return (0); error: - for (i = 0; i < expr->exp_nexpr; i++) { - fts_free(expr->exp_stack[i]); - expr->exp_stack[i] = 0; - } - expr->exp_nexpr = 0; - if (list) - fts_free(list); - t_freebytes(exp_string, exp_strlen+1); - return (1); + for (i = 0; i < expr->exp_nexpr; i++) { + fts_free(expr->exp_stack[i]); + expr->exp_stack[i] = 0; + } + expr->exp_nexpr = 0; + if (list) + fts_free(list); + t_freebytes(exp_string, exp_strlen+1); + return (1); } /* * ex_lex -- This routine is a bit more than a lexical parser since it will - * also do some syntax checking. It reads the string s and will - * return a linked list of struct ex_ex. - * It will also put the number of the nodes in *n. + * also do some syntax checking. It reads the string s and will + * return a linked list of struct ex_ex. + * It will also put the number of the nodes in *n. */ struct ex_ex * ex_lex(struct expr *expr, long int *n) { - struct ex_ex *list_arr; - struct ex_ex *exptr; - long non = 0; /* number of nodes */ - long maxnode = 0; - - list_arr = (struct ex_ex *)fts_malloc(sizeof (struct ex_ex) * MINODES); - if (! list_arr) { - post("ex_lex: no mem\n"); - return ((struct ex_ex *)0); - } - exptr = list_arr; - maxnode = MINODES; - - while (8) - { - if (non >= maxnode) { - maxnode += MINODES; - - list_arr = fts_realloc((void *)list_arr, - sizeof (struct ex_ex) * maxnode); - if (!list_arr) { - post("ex_lex: no mem\n"); - return ((struct ex_ex *)0); - } - exptr = &(list_arr)[non]; - } - - if (getoken(expr, exptr)) { - fts_free(list_arr); - return ((struct ex_ex *)0); - } - non++; - - if (!exptr->ex_type) - break; - - exptr++; - } - *n = non; - - return list_arr; + struct ex_ex *list_arr; + struct ex_ex *exptr; + long non = 0; /* number of nodes */ + long maxnode = 0; + + list_arr = (struct ex_ex *)fts_malloc(sizeof (struct ex_ex) * MINODES); + if (! list_arr) { + post("ex_lex: no mem\n"); + return ((struct ex_ex *)0); + } + exptr = list_arr; + maxnode = MINODES; + + while (8) + { + if (non >= maxnode) { + maxnode += MINODES; + + list_arr = fts_realloc((void *)list_arr, + sizeof (struct ex_ex) * maxnode); + if (!list_arr) { + post("ex_lex: no mem\n"); + return ((struct ex_ex *)0); + } + exptr = &(list_arr)[non]; + } + + if (getoken(expr, exptr)) { + fts_free(list_arr); + return ((struct ex_ex *)0); + } + non++; + + if (!exptr->ex_type) + break; + + exptr++; + } + *n = non; + + return list_arr; } /* * ex_match -- this routine walks through the eptr and matches the * perentheses and brackets, it also converts the function - * names to a pointer to the describing structure of the - * specified function + * names to a pointer to the describing structure of the + * specified function */ /* operator to match */ struct ex_ex * ex_match(struct ex_ex *eptr, long int op) { - int firstone = 1; - struct ex_ex *ret; - t_ex_func *fun; - - for (; 8; eptr++, firstone = 0) { - switch (eptr->ex_type) { - case 0: - if (!op) - return (eptr); - post("expr syntax error: an open %s not matched\n", - op == OP_RP ? "parenthesis" : "bracket"); - return (exNULL); - case ET_INT: - case ET_FLT: - case ET_II: - case ET_FI: - case ET_SI: - case ET_VI: - case ET_SYM: - case ET_VSYM: - continue; - case ET_YO: - if (eptr[1].ex_type != ET_OP || eptr[1].ex_op != OP_LB) - eptr->ex_type = ET_YOM1; - continue; - case ET_XI: - if (eptr[1].ex_type != ET_OP || eptr[1].ex_op != OP_LB) - eptr->ex_type = ET_XI0; - continue; - case ET_TBL: - case ET_FUNC: - case ET_LP: - /* CHANGE - case ET_RP: - */ - case ET_LB: - /* CHANGE - case ET_RB: - */ - post("ex_match: unexpected type, %ld\n", eptr->ex_type); - return (exNULL); - case ET_OP: - if (op == eptr->ex_op) - return (eptr); - /* - * if we are looking for a right peranthesis - * or a right bracket and find the other kind, - * it has to be a syntax error - */ - if ((eptr->ex_op == OP_RP && op == OP_RB) || - (eptr->ex_op == OP_RB && op == OP_RP)) { - post("expr syntax error: prenthesis or brackets not matched\n"); - return (exNULL); - } - /* - * Up to now we have marked the unary minuses as - * subrtacts. Any minus that is the first one in - * chain or is preceeded by anything except ')' and - * ']' is a unary minus. - */ - if (eptr->ex_op == OP_SUB) { - ret = eptr - 1; - if (firstone || (ret->ex_type == ET_OP && - ret->ex_op != OP_RB && ret->ex_op != OP_RP)) - eptr->ex_op = OP_UMINUS; - } else if (eptr->ex_op == OP_LP) { - ret = ex_match(eptr + 1, OP_RP); - if (!ret) - return (ret); - eptr->ex_type = ET_LP; - eptr->ex_ptr = (char *) ret; - eptr = ret; - } else if (eptr->ex_op == OP_LB) { - ret = ex_match(eptr + 1, OP_RB); - if (!ret) - return (ret); - eptr->ex_type = ET_LB; - eptr->ex_ptr = (char *) ret; - eptr = ret; - } - continue; - case ET_STR: - if (eptr[1].ex_op == OP_LB) { - char *tmp; - - eptr->ex_type = ET_TBL; - tmp = eptr->ex_ptr; - if (ex_getsym(tmp, (t_symbol **)&(eptr->ex_ptr))) { - post("expr: syntax error: problms with ex_getsym\n"); - return (exNULL); - } - fts_free((void *)tmp); - } else if (eptr[1].ex_op == OP_LP) { - fun = find_func(eptr->ex_ptr); - if (!fun) { - post( - "expr: error: function %s not found\n", - eptr->ex_ptr); - return (exNULL); - } - eptr->ex_type = ET_FUNC; - eptr->ex_ptr = (char *) fun; - } else { - char *tmp; - - if (eptr[1].ex_type && eptr[1].ex_type!=ET_OP){ - post("expr: syntax error: bad string '%s'\n", eptr->ex_ptr); - return (exNULL); - } - /* it is a variable */ - eptr->ex_type = ET_VAR; - tmp = eptr->ex_ptr; - if (ex_getsym(tmp, - (t_symbol **)&(eptr->ex_ptr))) { - post("expr: variable '%s' not found",tmp); - return (exNULL); - } - } - continue; - default: - post("ex_match: bad type\n"); - return (exNULL); - } - } - /* NOTREACHED */ + int firstone = 1; + struct ex_ex *ret; + t_ex_func *fun; + + for (; 8; eptr++, firstone = 0) { + switch (eptr->ex_type) { + case 0: + if (!op) + return (eptr); + post("expr syntax error: an open %s not matched\n", + op == OP_RP ? "parenthesis" : "bracket"); + return (exNULL); + case ET_INT: + case ET_FLT: + case ET_II: + case ET_FI: + case ET_SI: + case ET_VI: + case ET_SYM: + case ET_VSYM: + continue; + case ET_YO: + if (eptr[1].ex_type != ET_OP || eptr[1].ex_op != OP_LB) + eptr->ex_type = ET_YOM1; + continue; + case ET_XI: + if (eptr[1].ex_type != ET_OP || eptr[1].ex_op != OP_LB) + eptr->ex_type = ET_XI0; + continue; + case ET_TBL: + case ET_FUNC: + case ET_LP: + /* CHANGE + case ET_RP: + */ + case ET_LB: + /* CHANGE + case ET_RB: + */ + post("ex_match: unexpected type, %ld\n", eptr->ex_type); + return (exNULL); + case ET_OP: + if (op == eptr->ex_op) + return (eptr); + /* + * if we are looking for a right peranthesis + * or a right bracket and find the other kind, + * it has to be a syntax error + */ + if ((eptr->ex_op == OP_RP && op == OP_RB) || + (eptr->ex_op == OP_RB && op == OP_RP)) { + post("expr syntax error: prenthesis or brackets not matched\n"); + return (exNULL); + } + /* + * Up to now we have marked the unary minuses as + * subrtacts. Any minus that is the first one in + * chain or is preceeded by anything except ')' and + * ']' is a unary minus. + */ + if (eptr->ex_op == OP_SUB) { + ret = eptr - 1; + if (firstone || (ret->ex_type == ET_OP && + ret->ex_op != OP_RB && ret->ex_op != OP_RP)) + eptr->ex_op = OP_UMINUS; + } else if (eptr->ex_op == OP_LP) { + ret = ex_match(eptr + 1, OP_RP); + if (!ret) + return (ret); + eptr->ex_type = ET_LP; + eptr->ex_ptr = (char *) ret; + eptr = ret; + } else if (eptr->ex_op == OP_LB) { + ret = ex_match(eptr + 1, OP_RB); + if (!ret) + return (ret); + eptr->ex_type = ET_LB; + eptr->ex_ptr = (char *) ret; + eptr = ret; + } + continue; + case ET_STR: + if (eptr[1].ex_op == OP_LB) { + char *tmp; + + eptr->ex_type = ET_TBL; + tmp = eptr->ex_ptr; + if (ex_getsym(tmp, (t_symbol **)&(eptr->ex_ptr))) { + post("expr: syntax error: problms with ex_getsym\n"); + return (exNULL); + } + fts_free((void *)tmp); + } else if (eptr[1].ex_op == OP_LP) { + fun = find_func(eptr->ex_ptr); + if (!fun) { + post( + "expr: error: function %s not found\n", + eptr->ex_ptr); + return (exNULL); + } + eptr->ex_type = ET_FUNC; + eptr->ex_ptr = (char *) fun; + } else { + char *tmp; + + if (eptr[1].ex_type && eptr[1].ex_type!=ET_OP){ + post("expr: syntax error: bad string '%s'\n", eptr->ex_ptr); + return (exNULL); + } + /* it is a variable */ + eptr->ex_type = ET_VAR; + tmp = eptr->ex_ptr; + if (ex_getsym(tmp, + (t_symbol **)&(eptr->ex_ptr))) { + post("expr: variable '%s' not found",tmp); + return (exNULL); + } + } + continue; + default: + post("ex_match: bad type\n"); + return (exNULL); + } + } + /* NOTREACHED */ } /* * ex_parse -- This function if called when we have already done some - * parsing on the expression, and we have already matched - * our brackets and parenthesis. The main job of this - * function is to convert the infix expression to the - * prefix form. - * First we find the operator with the lowest precedence and - * put it on the stack ('optr', it is really just an array), then - * we call ourself (ex_parse()), on its arguments (unary operators - * only have one operator.) - * When "argc" is set it means that we are parsing the arguments - * of a function and we will increment *argc anytime we find - * a a segment that can qualify as an argument (counting commas). + * parsing on the expression, and we have already matched + * our brackets and parenthesis. The main job of this + * function is to convert the infix expression to the + * prefix form. + * First we find the operator with the lowest precedence and + * put it on the stack ('optr', it is really just an array), then + * we call ourself (ex_parse()), on its arguments (unary operators + * only have one operator.) + * When "argc" is set it means that we are parsing the arguments + * of a function and we will increment *argc anytime we find + * a a segment that can qualify as an argument (counting commas). * - * returns 0 on syntax error + * returns 0 on syntax error */ /* number of argument separated by comma */ struct ex_ex * ex_parse(struct expr *x, struct ex_ex *iptr, struct ex_ex *optr, long int *argc) { - struct ex_ex *eptr; - struct ex_ex *lowpre = 0; /* pointer to the lowest precedence */ - struct ex_ex savex; - long pre = HI_PRE; - long count; - - if (!iptr) { - post("ex_parse: input is null, iptr = 0x%lx\n", iptr); - return (exNULL); - } - if (!iptr->ex_type) - return (exNULL); - - /* - * the following loop finds the lowest precedence operator in the - * the input token list, comma is explicitly checked here since - * that is a special operator and is only legal in functions - */ - for (eptr = iptr, count = 0; eptr->ex_type; eptr++, count++) - switch (eptr->ex_type) { - case ET_SYM: - case ET_VSYM: - if (!argc) { - post("expr: syntax error: symbols allowed for functions only\n"); - ex_print(eptr); - return (exNULL); - } - case ET_INT: - case ET_FLT: - case ET_II: - case ET_FI: - case ET_XI0: - case ET_YOM1: - case ET_VI: - case ET_VAR: - if (!count && !eptr[1].ex_type) { - *optr++ = *eptr; - return (optr); - } - break; - case ET_XI: - case ET_YO: - case ET_SI: - case ET_TBL: - if (eptr[1].ex_type != ET_LB) { - post("expr: syntax error: brackets missing\n"); - ex_print(eptr); - return (exNULL); - } - /* if this table is the only token, parse the table */ - if (!count && - !((struct ex_ex *) eptr[1].ex_ptr)[1].ex_type) { - savex = *((struct ex_ex *) eptr[1].ex_ptr); - *((struct ex_ex *) eptr[1].ex_ptr) = nullex; - *optr++ = *eptr; - lowpre = ex_parse(x, &eptr[2], optr, (long *)0); - *((struct ex_ex *) eptr[1].ex_ptr) = savex; - return(lowpre); - } - eptr = (struct ex_ex *) eptr[1].ex_ptr; - break; - case ET_OP: - if (eptr->ex_op == OP_COMMA) { - if (!argc || !count || !eptr[1].ex_type) { - post("expr: syntax error: illegal comma\n"); - ex_print(eptr[1].ex_type ? eptr : iptr); - return (exNULL); - } - } - if (!eptr[1].ex_type) { - post("expr: syntax error: missing operand\n"); - ex_print(iptr); - return (exNULL); - } - if ((eptr->ex_op & PRE_MASK) <= pre) { - pre = eptr->ex_op & PRE_MASK; - lowpre = eptr; - } - break; - case ET_FUNC: - if (eptr[1].ex_type != ET_LP) { - post("expr: ex_parse: no parenthesis\n"); - return (exNULL); - } - /* if this function is the only token, parse it */ - if (!count && - !((struct ex_ex *) eptr[1].ex_ptr)[1].ex_type) { - long ac; - - if (eptr[1].ex_ptr == (char *) &eptr[2]) { - post("expr: syntax error: missing argument\n"); - ex_print(eptr); - return (exNULL); - } - ac = 0; - savex = *((struct ex_ex *) eptr[1].ex_ptr); - *((struct ex_ex *) eptr[1].ex_ptr) = nullex; - *optr++ = *eptr; - lowpre = ex_parse(x, &eptr[2], optr, &ac); - if (!lowpre) - return (exNULL); - ac++; - if (ac != - ((t_ex_func *)eptr->ex_ptr)->f_argc){ - post("expr: syntax error: function '%s' needs %ld arguments\n", - ((t_ex_func *)eptr->ex_ptr)->f_name, - ((t_ex_func *)eptr->ex_ptr)->f_argc); - return (exNULL); - } - *((struct ex_ex *) eptr[1].ex_ptr) = savex; - return (lowpre); - } - eptr = (struct ex_ex *) eptr[1].ex_ptr; - break; - case ET_LP: - case ET_LB: - if (!count && - !((struct ex_ex *) eptr->ex_ptr)[1].ex_type) { - if (eptr->ex_ptr == (char *)(&eptr[1])) { - post("expr: syntax error: empty '%s'\n", - eptr->ex_type==ET_LP?"()":"[]"); - ex_print(eptr); - return (exNULL); - } - savex = *((struct ex_ex *) eptr->ex_ptr); - *((struct ex_ex *) eptr->ex_ptr) = nullex; - lowpre = ex_parse(x, &eptr[1], optr, (long *)0); - *((struct ex_ex *) eptr->ex_ptr) = savex; - return (lowpre); - } - eptr = (struct ex_ex *)eptr->ex_ptr; - break; - case ET_STR: - default: - ex_print(eptr); - post("expr: ex_parse: type = 0x%lx\n", eptr->ex_type); - return (exNULL); - } - - if (pre == HI_PRE) { - post("expr: syntax error: missing operation\n"); - ex_print(iptr); - return (exNULL); - } - if (count < 2) { - post("expr: syntax error: mission operand\n"); - ex_print(iptr); - return (exNULL); - } - if (count == 2) { - if (lowpre != iptr) { - post("expr: ex_parse: unary operator should be first\n"); - return (exNULL); - } - if (!unary_op(lowpre->ex_op)) { - post("expr: syntax error: not a uniary operator\n"); - ex_print(iptr); - return (exNULL); - } - *optr++ = *lowpre; - eptr = ex_parse(x, &lowpre[1], optr, argc); - return (eptr); - } - if (lowpre == iptr) { - post("expr: syntax error: mission operand\n"); - ex_print(iptr); - return (exNULL); - } - savex = *lowpre; - *lowpre = nullex; - if (savex.ex_op != OP_COMMA) - *optr++ = savex; - else - (*argc)++; - eptr = ex_parse(x, iptr, optr, argc); - if (eptr) { - eptr = ex_parse(x, &lowpre[1], eptr, argc); - *lowpre = savex; - } - return (eptr); + struct ex_ex *eptr; + struct ex_ex *lowpre = 0; /* pointer to the lowest precedence */ + struct ex_ex savex; + long pre = HI_PRE; + long count; + + if (!iptr) { + post("ex_parse: input is null, iptr = 0x%lx\n", iptr); + return (exNULL); + } + if (!iptr->ex_type) + return (exNULL); + + /* + * the following loop finds the lowest precedence operator in the + * the input token list, comma is explicitly checked here since + * that is a special operator and is only legal in functions + */ + for (eptr = iptr, count = 0; eptr->ex_type; eptr++, count++) + switch (eptr->ex_type) { + case ET_SYM: + case ET_VSYM: + if (!argc) { + post("expr: syntax error: symbols allowed for functions only\n"); + ex_print(eptr); + return (exNULL); + } + case ET_INT: + case ET_FLT: + case ET_II: + case ET_FI: + case ET_XI0: + case ET_YOM1: + case ET_VI: + case ET_VAR: + if (!count && !eptr[1].ex_type) { + *optr++ = *eptr; + return (optr); + } + break; + case ET_XI: + case ET_YO: + case ET_SI: + case ET_TBL: + if (eptr[1].ex_type != ET_LB) { + post("expr: syntax error: brackets missing\n"); + ex_print(eptr); + return (exNULL); + } + /* if this table is the only token, parse the table */ + if (!count && + !((struct ex_ex *) eptr[1].ex_ptr)[1].ex_type) { + savex = *((struct ex_ex *) eptr[1].ex_ptr); + *((struct ex_ex *) eptr[1].ex_ptr) = nullex; + *optr++ = *eptr; + lowpre = ex_parse(x, &eptr[2], optr, (long *)0); + *((struct ex_ex *) eptr[1].ex_ptr) = savex; + return(lowpre); + } + eptr = (struct ex_ex *) eptr[1].ex_ptr; + break; + case ET_OP: + if (eptr->ex_op == OP_COMMA) { + if (!argc || !count || !eptr[1].ex_type) { + post("expr: syntax error: illegal comma\n"); + ex_print(eptr[1].ex_type ? eptr : iptr); + return (exNULL); + } + } + if (!eptr[1].ex_type) { + post("expr: syntax error: missing operand\n"); + ex_print(iptr); + return (exNULL); + } + if ((eptr->ex_op & PRE_MASK) <= pre) { + pre = eptr->ex_op & PRE_MASK; + lowpre = eptr; + } + break; + case ET_FUNC: + if (eptr[1].ex_type != ET_LP) { + post("expr: ex_parse: no parenthesis\n"); + return (exNULL); + } + /* if this function is the only token, parse it */ + if (!count && + !((struct ex_ex *) eptr[1].ex_ptr)[1].ex_type) { + long ac; + + if (eptr[1].ex_ptr == (char *) &eptr[2]) { + post("expr: syntax error: missing argument\n"); + ex_print(eptr); + return (exNULL); + } + ac = 0; + savex = *((struct ex_ex *) eptr[1].ex_ptr); + *((struct ex_ex *) eptr[1].ex_ptr) = nullex; + *optr++ = *eptr; + lowpre = ex_parse(x, &eptr[2], optr, &ac); + if (!lowpre) + return (exNULL); + ac++; + if (ac != + ((t_ex_func *)eptr->ex_ptr)->f_argc){ + post("expr: syntax error: function '%s' needs %ld arguments\n", + ((t_ex_func *)eptr->ex_ptr)->f_name, + ((t_ex_func *)eptr->ex_ptr)->f_argc); + return (exNULL); + } + *((struct ex_ex *) eptr[1].ex_ptr) = savex; + return (lowpre); + } + eptr = (struct ex_ex *) eptr[1].ex_ptr; + break; + case ET_LP: + case ET_LB: + if (!count && + !((struct ex_ex *) eptr->ex_ptr)[1].ex_type) { + if (eptr->ex_ptr == (char *)(&eptr[1])) { + post("expr: syntax error: empty '%s'\n", + eptr->ex_type==ET_LP?"()":"[]"); + ex_print(eptr); + return (exNULL); + } + savex = *((struct ex_ex *) eptr->ex_ptr); + *((struct ex_ex *) eptr->ex_ptr) = nullex; + lowpre = ex_parse(x, &eptr[1], optr, (long *)0); + *((struct ex_ex *) eptr->ex_ptr) = savex; + return (lowpre); + } + eptr = (struct ex_ex *)eptr->ex_ptr; + break; + case ET_STR: + default: + ex_print(eptr); + post("expr: ex_parse: type = 0x%lx\n", eptr->ex_type); + return (exNULL); + } + + if (pre == HI_PRE) { + post("expr: syntax error: missing operation\n"); + ex_print(iptr); + return (exNULL); + } + if (count < 2) { + post("expr: syntax error: mission operand\n"); + ex_print(iptr); + return (exNULL); + } + if (count == 2) { + if (lowpre != iptr) { + post("expr: ex_parse: unary operator should be first\n"); + return (exNULL); + } + if (!unary_op(lowpre->ex_op)) { + post("expr: syntax error: not a uniary operator\n"); + ex_print(iptr); + return (exNULL); + } + *optr++ = *lowpre; + eptr = ex_parse(x, &lowpre[1], optr, argc); + return (eptr); + } + if (lowpre == iptr) { + post("expr: syntax error: mission operand\n"); + ex_print(iptr); + return (exNULL); + } + savex = *lowpre; + *lowpre = nullex; + if (savex.ex_op != OP_COMMA) + *optr++ = savex; + else + (*argc)++; + eptr = ex_parse(x, iptr, optr, argc); + if (eptr) { + eptr = ex_parse(x, &lowpre[1], eptr, argc); + *lowpre = savex; + } + return (eptr); } /* * this is the devide zero check for a a non devide operator */ -#define DZC(ARG1,OPR,ARG2) (ARG1 OPR ARG2) - -#define EVAL(OPR); \ -eptr = ex_eval(expr, ex_eval(expr, eptr, &left, idx), &right, idx); \ -switch (left.ex_type) { \ -case ET_INT: \ - switch(right.ex_type) { \ - case ET_INT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = (float)DZC(left.ex_int, OPR, right.ex_int); \ - for (j = 0; j < expr->exp_vsize; j++) \ - *op++ = scalar; \ - } else { \ - optr->ex_type = ET_INT; \ - optr->ex_int = DZC(left.ex_int, OPR, right.ex_int); \ - } \ - break; \ - case ET_FLT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = DZC(((float)left.ex_int), OPR, right.ex_flt);\ - for (j = 0; j < expr->exp_vsize; j++) \ - *op++ = scalar; \ - } else { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = DZC(((float)left.ex_int), OPR, \ - right.ex_flt); \ - } \ - break; \ - case ET_VEC: \ - case ET_VI: \ - if (optr->ex_type != ET_VEC) { \ - if (optr->ex_type == ET_VI) { \ - post("expr~: Int. error %d", __LINE__); \ - abort(); \ - } \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float)*expr->exp_vsize); \ - } \ - scalar = left.ex_int; \ - rp = right.ex_vec; \ - op = optr->ex_vec; \ - for (i = 0; i < expr->exp_vsize; i++) { \ - *op++ = DZC (scalar, OPR, *rp); \ - rp++; \ - } \ - break; \ - case ET_SYM: \ - default: \ - post_error((fts_object_t *) expr, \ - "expr: ex_eval(%d): bad right type %ld\n", \ - __LINE__, right.ex_type); \ - nullret = 1; \ - } \ - break; \ -case ET_FLT: \ - switch(right.ex_type) { \ - case ET_INT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = DZC((float) left.ex_flt, OPR, right.ex_int); \ - for (j = 0; j < expr->exp_vsize; j++) \ - *op++ = scalar; \ - } else { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = DZC(left.ex_flt, OPR, right.ex_int); \ - } \ - break; \ - case ET_FLT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = DZC(left.ex_flt, OPR, right.ex_flt); \ - for (j = 0; j < expr->exp_vsize; j++) \ - *op++ = scalar; \ - } else { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt= DZC(left.ex_flt, OPR, right.ex_flt); \ - } \ - break; \ - case ET_VEC: \ - case ET_VI: \ - if (optr->ex_type != ET_VEC) { \ - if (optr->ex_type == ET_VI) { \ - post("expr~: Int. error %d", __LINE__); \ - abort(); \ - } \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float)*expr->exp_vsize); \ - } \ - scalar = left.ex_flt; \ - rp = right.ex_vec; \ - op = optr->ex_vec; \ - for (i = 0; i < expr->exp_vsize; i++) { \ - *op++ = DZC(scalar, OPR, *rp); \ - rp++; \ - } \ - break; \ - case ET_SYM: \ - default: \ - post_error((fts_object_t *) expr, \ - "expr: ex_eval(%d): bad right type %ld\n", \ - __LINE__, right.ex_type); \ - nullret = 1; \ - } \ - break; \ -case ET_VEC: \ -case ET_VI: \ - if (optr->ex_type != ET_VEC) { \ - if (optr->ex_type == ET_VI) { \ - post("expr~: Int. error %d", __LINE__); \ - abort(); \ - } \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float)*expr->exp_vsize); \ - } \ - op = optr->ex_vec; \ - lp = left.ex_vec; \ - switch(right.ex_type) { \ - case ET_INT: \ - scalar = right.ex_int; \ - for (i = 0; i < expr->exp_vsize; i++) { \ - *op++ = DZC(*lp, OPR, scalar); \ - lp++; \ - } \ - break; \ - case ET_FLT: \ - scalar = right.ex_flt; \ - for (i = 0; i < expr->exp_vsize; i++) { \ - *op++ = DZC(*lp, OPR, scalar); \ - lp++; \ - } \ - break; \ - case ET_VEC: \ - case ET_VI: \ - rp = right.ex_vec; \ - for (i = 0; i < expr->exp_vsize; i++) { \ - /* \ - * on a RISC processor one could copy \ - * 8 times in each round to get a considerable \ - * improvement \ - */ \ - *op++ = DZC(*lp, OPR, *rp); \ - rp++; lp++; \ - } \ - break; \ - case ET_SYM: \ - default: \ - post_error((fts_object_t *) expr, \ - "expr: ex_eval(%d): bad right type %ld\n", \ - __LINE__, right.ex_type); \ - nullret = 1; \ - } \ - break; \ -case ET_SYM: \ -default: \ - post_error((fts_object_t *) expr, \ - "expr: ex_eval(%d): bad left type %ld\n", \ - __LINE__, left.ex_type); \ -} \ +#define DZC(ARG1,OPR,ARG2) (ARG1 OPR ARG2) + +#define EVAL(OPR); \ +eptr = ex_eval(expr, ex_eval(expr, eptr, &left, idx), &right, idx); \ +switch (left.ex_type) { \ +case ET_INT: \ + switch(right.ex_type) { \ + case ET_INT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = (float)DZC(left.ex_int, OPR, right.ex_int); \ + for (j = 0; j < expr->exp_vsize; j++) \ + *op++ = scalar; \ + } else { \ + optr->ex_type = ET_INT; \ + optr->ex_int = DZC(left.ex_int, OPR, right.ex_int); \ + } \ + break; \ + case ET_FLT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = DZC(((float)left.ex_int), OPR, right.ex_flt);\ + for (j = 0; j < expr->exp_vsize; j++) \ + *op++ = scalar; \ + } else { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = DZC(((float)left.ex_int), OPR, \ + right.ex_flt); \ + } \ + break; \ + case ET_VEC: \ + case ET_VI: \ + if (optr->ex_type != ET_VEC) { \ + if (optr->ex_type == ET_VI) { \ + post("expr~: Int. error %d", __LINE__); \ + abort(); \ + } \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float)*expr->exp_vsize); \ + } \ + scalar = left.ex_int; \ + rp = right.ex_vec; \ + op = optr->ex_vec; \ + for (i = 0; i < expr->exp_vsize; i++) { \ + *op++ = DZC (scalar, OPR, *rp); \ + rp++; \ + } \ + break; \ + case ET_SYM: \ + default: \ + post_error((fts_object_t *) expr, \ + "expr: ex_eval(%d): bad right type %ld\n", \ + __LINE__, right.ex_type); \ + nullret = 1; \ + } \ + break; \ +case ET_FLT: \ + switch(right.ex_type) { \ + case ET_INT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = DZC((float) left.ex_flt, OPR, right.ex_int); \ + for (j = 0; j < expr->exp_vsize; j++) \ + *op++ = scalar; \ + } else { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = DZC(left.ex_flt, OPR, right.ex_int); \ + } \ + break; \ + case ET_FLT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = DZC(left.ex_flt, OPR, right.ex_flt); \ + for (j = 0; j < expr->exp_vsize; j++) \ + *op++ = scalar; \ + } else { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt= DZC(left.ex_flt, OPR, right.ex_flt); \ + } \ + break; \ + case ET_VEC: \ + case ET_VI: \ + if (optr->ex_type != ET_VEC) { \ + if (optr->ex_type == ET_VI) { \ + post("expr~: Int. error %d", __LINE__); \ + abort(); \ + } \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float)*expr->exp_vsize); \ + } \ + scalar = left.ex_flt; \ + rp = right.ex_vec; \ + op = optr->ex_vec; \ + for (i = 0; i < expr->exp_vsize; i++) { \ + *op++ = DZC(scalar, OPR, *rp); \ + rp++; \ + } \ + break; \ + case ET_SYM: \ + default: \ + post_error((fts_object_t *) expr, \ + "expr: ex_eval(%d): bad right type %ld\n", \ + __LINE__, right.ex_type); \ + nullret = 1; \ + } \ + break; \ +case ET_VEC: \ +case ET_VI: \ + if (optr->ex_type != ET_VEC) { \ + if (optr->ex_type == ET_VI) { \ + post("expr~: Int. error %d", __LINE__); \ + abort(); \ + } \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float)*expr->exp_vsize); \ + } \ + op = optr->ex_vec; \ + lp = left.ex_vec; \ + switch(right.ex_type) { \ + case ET_INT: \ + scalar = right.ex_int; \ + for (i = 0; i < expr->exp_vsize; i++) { \ + *op++ = DZC(*lp, OPR, scalar); \ + lp++; \ + } \ + break; \ + case ET_FLT: \ + scalar = right.ex_flt; \ + for (i = 0; i < expr->exp_vsize; i++) { \ + *op++ = DZC(*lp, OPR, scalar); \ + lp++; \ + } \ + break; \ + case ET_VEC: \ + case ET_VI: \ + rp = right.ex_vec; \ + for (i = 0; i < expr->exp_vsize; i++) { \ + /* \ + * on a RISC processor one could copy \ + * 8 times in each round to get a considerable \ + * improvement \ + */ \ + *op++ = DZC(*lp, OPR, *rp); \ + rp++; lp++; \ + } \ + break; \ + case ET_SYM: \ + default: \ + post_error((fts_object_t *) expr, \ + "expr: ex_eval(%d): bad right type %ld\n", \ + __LINE__, right.ex_type); \ + nullret = 1; \ + } \ + break; \ +case ET_SYM: \ +default: \ + post_error((fts_object_t *) expr, \ + "expr: ex_eval(%d): bad left type %ld\n", \ + __LINE__, left.ex_type); \ +} \ break; /* * evaluate a unary operator, TYPE is applied to float operands */ -#define EVAL_UNARY(OPR, TYPE) \ - eptr = ex_eval(expr, eptr, &left, idx); \ - switch(left.ex_type) { \ - case ET_INT: \ - if (optr->ex_type == ET_VEC) { \ - ex_mkvector(optr->ex_vec,(float)(OPR left.ex_int),\ - expr->exp_vsize);\ - break; \ - } \ - optr->ex_type = ET_INT; \ - optr->ex_int = OPR left.ex_int; \ - break; \ - case ET_FLT: \ - if (optr->ex_type == ET_VEC) { \ - ex_mkvector(optr->ex_vec, OPR (TYPE left.ex_flt),\ - expr->exp_vsize);\ - break; \ - } \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = OPR (TYPE left.ex_flt); \ - break; \ - case ET_VI: \ - case ET_VEC: \ - j = expr->exp_vsize; \ - if (optr->ex_type != ET_VEC) { \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float)*expr->exp_vsize); \ - } \ - op = optr->ex_vec; \ - lp = left.ex_vec; \ - j = expr->exp_vsize; \ - for (i = 0; i < j; i++) \ - *op++ = OPR (TYPE *lp++); \ - break; \ - default: \ - post_error((fts_object_t *) expr, \ - "expr: ex_eval(%d): bad left type %ld\n", \ - __LINE__, left.ex_type); \ - nullret++; \ - } \ - break; +#define EVAL_UNARY(OPR, TYPE) \ + eptr = ex_eval(expr, eptr, &left, idx); \ + switch(left.ex_type) { \ + case ET_INT: \ + if (optr->ex_type == ET_VEC) { \ + ex_mkvector(optr->ex_vec,(float)(OPR left.ex_int),\ + expr->exp_vsize);\ + break; \ + } \ + optr->ex_type = ET_INT; \ + optr->ex_int = OPR left.ex_int; \ + break; \ + case ET_FLT: \ + if (optr->ex_type == ET_VEC) { \ + ex_mkvector(optr->ex_vec, OPR (TYPE left.ex_flt),\ + expr->exp_vsize);\ + break; \ + } \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = OPR (TYPE left.ex_flt); \ + break; \ + case ET_VI: \ + case ET_VEC: \ + j = expr->exp_vsize; \ + if (optr->ex_type != ET_VEC) { \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float)*expr->exp_vsize); \ + } \ + op = optr->ex_vec; \ + lp = left.ex_vec; \ + j = expr->exp_vsize; \ + for (i = 0; i < j; i++) \ + *op++ = OPR (TYPE *lp++); \ + break; \ + default: \ + post_error((fts_object_t *) expr, \ + "expr: ex_eval(%d): bad left type %ld\n", \ + __LINE__, left.ex_type); \ + nullret++; \ + } \ + break; void ex_mkvector(t_float *fp, t_float x, int size) { - while (size--) - *fp++ = x; + while (size--) + *fp++ = x; } /* @@ -905,29 +905,29 @@ ex_mkvector(t_float *fp, t_float x, int size) void ex_dzdetect(struct expr *expr) { - char *etype; - - if (!expr->exp_error & EE_DZ) { - if (IS_EXPR(expr)) - etype = "expr"; - else if (IS_EXPR_TILDE(expr)) - etype = "expr~"; - else if (IS_FEXPR_TILDE(expr)) - etype = "fexpr~"; - else { - post ("expr -- ex_dzdetect internal error"); - etype = ""; - } - post ("%s divide by zero detected", etype); - expr->exp_error |= EE_DZ; - } + char *etype; + + if (!expr->exp_error & EE_DZ) { + if (IS_EXPR(expr)) + etype = "expr"; + else if (IS_EXPR_TILDE(expr)) + etype = "expr~"; + else if (IS_FEXPR_TILDE(expr)) + etype = "fexpr~"; + else { + post ("expr -- ex_dzdetect internal error"); + etype = ""; + } + post ("%s divide by zero detected", etype); + expr->exp_error |= EE_DZ; + } } - + /* * ex_eval -- evaluate the array of prefix expression - * ex_eval returns the pointer to the first unevaluated node - * in the array. This is a recursive routine. + * ex_eval returns the pointer to the first unevaluated node + * in the array. This is a recursive routine. */ /* SDY @@ -944,270 +944,270 @@ ex_eval(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) /* the result pointer */ /* the sample numnber processed for fexpr~ */ { - int i, j; - t_float *lp, *rp, *op; /* left, right, and out pointer to vectors */ - t_float scalar; - int nullret = 0; /* did we have an error */ - struct ex_ex left, right; /* left and right operands */ - - left.ex_type = 0; - left.ex_int = 0; - right.ex_type = 0; - right.ex_int = 0; - - if (!eptr) - return (exNULL); - switch (eptr->ex_type) { - case ET_INT: - if (optr->ex_type == ET_VEC) - ex_mkvector(optr->ex_vec, (float) eptr->ex_int, - expr->exp_vsize); - else - *optr = *eptr; - return (++eptr); - - case ET_FLT: - - if (optr->ex_type == ET_VEC) - ex_mkvector(optr->ex_vec, eptr->ex_flt, expr->exp_vsize); - else - *optr = *eptr; - return (++eptr); - - case ET_SYM: - if (optr->ex_type == ET_VEC) { - post_error((fts_object_t *) expr, - "expr: ex_eval: cannot turn string to vector\n"); - return (exNULL); - } - *optr = *eptr; - return (++eptr); - case ET_II: - if (eptr->ex_int == -1) { - post_error((fts_object_t *) expr, - "expr: ex_eval: inlet number not set\n"); - return (exNULL); - } - if (optr->ex_type == ET_VEC) { - ex_mkvector(optr->ex_vec, - (t_float)expr->exp_var[eptr->ex_int].ex_int, - expr->exp_vsize); - } else { - optr->ex_type = ET_INT; - optr->ex_int = expr->exp_var[eptr->ex_int].ex_int; - } - return (++eptr); - case ET_FI: - if (eptr->ex_int == -1) { - post_error((fts_object_t *) expr, - "expr: ex_eval: inlet number not set\n"); - return (exNULL); - } - if (optr->ex_type == ET_VEC) { - ex_mkvector(optr->ex_vec, - expr->exp_var[eptr->ex_int].ex_flt, expr->exp_vsize); - } else { - optr->ex_type = ET_FLT; - optr->ex_flt = expr->exp_var[eptr->ex_int].ex_flt; - } - return (++eptr); - - case ET_VSYM: - if (optr->ex_type == ET_VEC) { - post_error((fts_object_t *) expr, - "expr: IntErr. vsym in for vec out\n"); - return (exNULL); - } - if (eptr->ex_int == -1) { - post_error((fts_object_t *) expr, - "expr: ex_eval: inlet number not set\n"); - return (exNULL); - } - optr->ex_type = ET_SYM; - optr->ex_ptr = expr->exp_var[eptr->ex_int].ex_ptr; - return(++eptr); - - case ET_VI: - if (optr->ex_type != ET_VEC) - *optr = expr->exp_var[eptr->ex_int]; - else if (optr->ex_vec != expr->exp_var[eptr->ex_int].ex_vec) - memcpy(optr->ex_vec, expr->exp_var[eptr->ex_int].ex_vec, - expr->exp_vsize * sizeof (t_float)); - return(++eptr); - case ET_VEC: - if (optr->ex_type != ET_VEC) { - optr->ex_type = ET_VEC; - optr->ex_vec = eptr->ex_vec; - eptr->ex_type = ET_INT; - eptr->ex_int = 0; - } else if (optr->ex_vec != eptr->ex_vec) { - memcpy(optr->ex_vec, eptr->ex_vec, - expr->exp_vsize * sizeof (t_float)); + int i, j; + t_float *lp, *rp, *op; /* left, right, and out pointer to vectors */ + t_float scalar; + int nullret = 0; /* did we have an error */ + struct ex_ex left, right; /* left and right operands */ + + left.ex_type = 0; + left.ex_int = 0; + right.ex_type = 0; + right.ex_int = 0; + + if (!eptr) + return (exNULL); + switch (eptr->ex_type) { + case ET_INT: + if (optr->ex_type == ET_VEC) + ex_mkvector(optr->ex_vec, (float) eptr->ex_int, + expr->exp_vsize); + else + *optr = *eptr; + return (++eptr); + + case ET_FLT: + + if (optr->ex_type == ET_VEC) + ex_mkvector(optr->ex_vec, eptr->ex_flt, expr->exp_vsize); + else + *optr = *eptr; + return (++eptr); + + case ET_SYM: + if (optr->ex_type == ET_VEC) { + post_error((fts_object_t *) expr, + "expr: ex_eval: cannot turn string to vector\n"); + return (exNULL); + } + *optr = *eptr; + return (++eptr); + case ET_II: + if (eptr->ex_int == -1) { + post_error((fts_object_t *) expr, + "expr: ex_eval: inlet number not set\n"); + return (exNULL); + } + if (optr->ex_type == ET_VEC) { + ex_mkvector(optr->ex_vec, + (t_float)expr->exp_var[eptr->ex_int].ex_int, + expr->exp_vsize); + } else { + optr->ex_type = ET_INT; + optr->ex_int = expr->exp_var[eptr->ex_int].ex_int; + } + return (++eptr); + case ET_FI: + if (eptr->ex_int == -1) { + post_error((fts_object_t *) expr, + "expr: ex_eval: inlet number not set\n"); + return (exNULL); + } + if (optr->ex_type == ET_VEC) { + ex_mkvector(optr->ex_vec, + expr->exp_var[eptr->ex_int].ex_flt, expr->exp_vsize); + } else { + optr->ex_type = ET_FLT; + optr->ex_flt = expr->exp_var[eptr->ex_int].ex_flt; + } + return (++eptr); + + case ET_VSYM: + if (optr->ex_type == ET_VEC) { + post_error((fts_object_t *) expr, + "expr: IntErr. vsym in for vec out\n"); + return (exNULL); + } + if (eptr->ex_int == -1) { + post_error((fts_object_t *) expr, + "expr: ex_eval: inlet number not set\n"); + return (exNULL); + } + optr->ex_type = ET_SYM; + optr->ex_ptr = expr->exp_var[eptr->ex_int].ex_ptr; + return(++eptr); + + case ET_VI: + if (optr->ex_type != ET_VEC) + *optr = expr->exp_var[eptr->ex_int]; + else if (optr->ex_vec != expr->exp_var[eptr->ex_int].ex_vec) + memcpy(optr->ex_vec, expr->exp_var[eptr->ex_int].ex_vec, + expr->exp_vsize * sizeof (t_float)); + return(++eptr); + case ET_VEC: + if (optr->ex_type != ET_VEC) { + optr->ex_type = ET_VEC; + optr->ex_vec = eptr->ex_vec; + eptr->ex_type = ET_INT; + eptr->ex_int = 0; + } else if (optr->ex_vec != eptr->ex_vec) { + memcpy(optr->ex_vec, eptr->ex_vec, + expr->exp_vsize * sizeof (t_float)); /* do we need to free here? or can we free higher up */ /* SDY the next lines do not make sense */ post("calling fts_free\n"); abort(); - fts_free(optr->ex_vec); - optr->ex_type = ET_INT; - eptr->ex_int = 0; - } else { /* this should not happen */ - post("expr int. error, optr->ex_vec = %d",optr->ex_vec); - abort(); - } - return(++eptr); - case ET_XI0: - /* short hand for $x?[0] */ - - /* SDY delete the following check */ - if (!IS_FEXPR_TILDE(expr) || optr->ex_type==ET_VEC) { - post("%d:exp->exp_flags = %d", __LINE__,expr->exp_flags); - abort(); - } - optr->ex_type = ET_FLT; - optr->ex_flt = expr->exp_var[eptr->ex_int].ex_vec[idx]; - return(++eptr); - case ET_YOM1: - /* - * short hand for $y?[-1] - * if we are calculating the first sample of the vector - * we need to look at the previous results buffer - */ - optr->ex_type = ET_FLT; - if (idx == 0) - optr->ex_flt = - expr->exp_p_res[eptr->ex_int][expr->exp_vsize - 1]; - else - optr->ex_flt=expr->exp_tmpres[eptr->ex_int][idx-1]; - return(++eptr); - - case ET_YO: - case ET_XI: - /* SDY delete the following */ - if (!IS_FEXPR_TILDE(expr) || optr->ex_type==ET_VEC) { - post("%d:expr->exp_flags = %d", __LINE__,expr->exp_flags); - abort(); - } - return (eval_sigidx(expr, eptr, optr, idx)); - - case ET_TBL: - case ET_SI: - return (eval_tab(expr, eptr, optr, idx)); - case ET_FUNC: - return (eval_func(expr, eptr, optr, idx)); - case ET_VAR: - return (eval_var(expr, eptr, optr, idx)); - case ET_OP: - break; - case ET_STR: - case ET_LP: - case ET_LB: - default: - post_error((fts_object_t *) expr, - "expr: ex_eval: unexpected type %d\n", eptr->ex_type); - return (exNULL); - } - if (!eptr[1].ex_type) { - post_error((fts_object_t *) expr, - "expr: ex_eval: not enough nodes 1\n"); - return (exNULL); - } - if (!unary_op(eptr->ex_op) && !eptr[2].ex_type) { - post_error((fts_object_t *) expr, - "expr: ex_eval: not enough nodes 2\n"); - return (exNULL); - } - - switch((eptr++)->ex_op) { - case OP_STORE: - return (eval_store(expr, eptr, optr, idx)); - case OP_NOT: - EVAL_UNARY(!, +); - case OP_NEG: - EVAL_UNARY(~, (long)); - case OP_UMINUS: - EVAL_UNARY(-, +); - case OP_MUL: - EVAL(*); - case OP_ADD: - EVAL(+); - case OP_SUB: - EVAL(-); - case OP_LT: - EVAL(<); - case OP_LE: - EVAL(<=); - case OP_GT: - EVAL(>); - case OP_GE: - EVAL(>=); - case OP_EQ: - EVAL(==); - case OP_NE: - EVAL(!=); + fts_free(optr->ex_vec); + optr->ex_type = ET_INT; + eptr->ex_int = 0; + } else { /* this should not happen */ + post("expr int. error, optr->ex_vec = %d",optr->ex_vec); + abort(); + } + return(++eptr); + case ET_XI0: + /* short hand for $x?[0] */ + + /* SDY delete the following check */ + if (!IS_FEXPR_TILDE(expr) || optr->ex_type==ET_VEC) { + post("%d:exp->exp_flags = %d", __LINE__,expr->exp_flags); + abort(); + } + optr->ex_type = ET_FLT; + optr->ex_flt = expr->exp_var[eptr->ex_int].ex_vec[idx]; + return(++eptr); + case ET_YOM1: + /* + * short hand for $y?[-1] + * if we are calculating the first sample of the vector + * we need to look at the previous results buffer + */ + optr->ex_type = ET_FLT; + if (idx == 0) + optr->ex_flt = + expr->exp_p_res[eptr->ex_int][expr->exp_vsize - 1]; + else + optr->ex_flt=expr->exp_tmpres[eptr->ex_int][idx-1]; + return(++eptr); + + case ET_YO: + case ET_XI: + /* SDY delete the following */ + if (!IS_FEXPR_TILDE(expr) || optr->ex_type==ET_VEC) { + post("%d:expr->exp_flags = %d", __LINE__,expr->exp_flags); + abort(); + } + return (eval_sigidx(expr, eptr, optr, idx)); + + case ET_TBL: + case ET_SI: + return (eval_tab(expr, eptr, optr, idx)); + case ET_FUNC: + return (eval_func(expr, eptr, optr, idx)); + case ET_VAR: + return (eval_var(expr, eptr, optr, idx)); + case ET_OP: + break; + case ET_STR: + case ET_LP: + case ET_LB: + default: + post_error((fts_object_t *) expr, + "expr: ex_eval: unexpected type %d\n", eptr->ex_type); + return (exNULL); + } + if (!eptr[1].ex_type) { + post_error((fts_object_t *) expr, + "expr: ex_eval: not enough nodes 1\n"); + return (exNULL); + } + if (!unary_op(eptr->ex_op) && !eptr[2].ex_type) { + post_error((fts_object_t *) expr, + "expr: ex_eval: not enough nodes 2\n"); + return (exNULL); + } + + switch((eptr++)->ex_op) { + case OP_STORE: + return (eval_store(expr, eptr, optr, idx)); + case OP_NOT: + EVAL_UNARY(!, +); + case OP_NEG: + EVAL_UNARY(~, (long)); + case OP_UMINUS: + EVAL_UNARY(-, +); + case OP_MUL: + EVAL(*); + case OP_ADD: + EVAL(+); + case OP_SUB: + EVAL(-); + case OP_LT: + EVAL(<); + case OP_LE: + EVAL(<=); + case OP_GT: + EVAL(>); + case OP_GE: + EVAL(>=); + case OP_EQ: + EVAL(==); + case OP_NE: + EVAL(!=); /* * following operators convert their argument to integer */ #undef DZC -#define DZC(ARG1,OPR,ARG2) (((int)ARG1) OPR ((int)ARG2)) - case OP_SL: - EVAL(<<); - case OP_SR: - EVAL(>>); - case OP_AND: - EVAL(&); - case OP_XOR: - EVAL(^); - case OP_OR: - EVAL(|); - case OP_LAND: - EVAL(&&); - case OP_LOR: - EVAL(||); +#define DZC(ARG1,OPR,ARG2) (((int)ARG1) OPR ((int)ARG2)) + case OP_SL: + EVAL(<<); + case OP_SR: + EVAL(>>); + case OP_AND: + EVAL(&); + case OP_XOR: + EVAL(^); + case OP_OR: + EVAL(|); + case OP_LAND: + EVAL(&&); + case OP_LOR: + EVAL(||); /* * for modulo we need to convert to integer and check for divide by zero */ #undef DZC -#define DZC(ARG1,OPR,ARG2) (((ARG2)?(((int)ARG1) OPR ((int)ARG2)) \ - : (ex_dzdetect(expr),0))) - case OP_MOD: - EVAL(%); +#define DZC(ARG1,OPR,ARG2) (((ARG2)?(((int)ARG1) OPR ((int)ARG2)) \ + : (ex_dzdetect(expr),0))) + case OP_MOD: + EVAL(%); /* * define the divide by zero check for divide */ #undef DZC -#define DZC(ARG1,OPR,ARG2) (((ARG2)?(ARG1 OPR ARG2):(ex_dzdetect(expr),0))) - case OP_DIV: - EVAL(/); - case OP_LP: - case OP_RP: - case OP_LB: - case OP_RB: - case OP_COMMA: - case OP_SEMI: - default: - post_error((fts_object_t *) expr, "expr: ex_print: bad op 0x%x\n", eptr->ex_op); - return (exNULL); - } - - - /* - * the left and right nodes could have been transformed to vectors - * down the chain - */ - if (left.ex_type == ET_VEC) - fts_free(left.ex_vec); - if (right.ex_type == ET_VEC) - fts_free(right.ex_vec); - if (nullret) - return (exNULL); - else - return (eptr); +#define DZC(ARG1,OPR,ARG2) (((ARG2)?(ARG1 OPR ARG2):(ex_dzdetect(expr),0))) + case OP_DIV: + EVAL(/); + case OP_LP: + case OP_RP: + case OP_LB: + case OP_RB: + case OP_COMMA: + case OP_SEMI: + default: + post_error((fts_object_t *) expr, "expr: ex_print: bad op 0x%x\n", eptr->ex_op); + return (exNULL); + } + + + /* + * the left and right nodes could have been transformed to vectors + * down the chain + */ + if (left.ex_type == ET_VEC) + fts_free(left.ex_vec); + if (right.ex_type == ET_VEC) + fts_free(right.ex_vec); + if (nullret) + return (exNULL); + else + return (eptr); } /* * eval_func -- evaluate a function, call ex_eval() on all the arguments - * so that all of them are terminal nodes. The call the - * appropriate function + * so that all of them are terminal nodes. The call the + * appropriate function */ struct ex_ex * eval_func(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) @@ -1215,37 +1215,37 @@ eval_func(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) /* the operation stack */ /* the result pointer */ { - int i; - struct ex_ex args[MAX_ARGS]; - t_ex_func *f; - - f = (t_ex_func *)(eptr++)->ex_ptr; - if (!f || !f->f_name) { - return (exNULL); - } - if (f->f_argc > MAX_ARGS) { - post_error((fts_object_t *) expr, "expr: eval_func: asking too many arguments\n"); - return (exNULL); - } - - for (i = 0; i < f->f_argc; i++) { - args[i].ex_type = 0; - args[i].ex_int = 0; - eptr = ex_eval(expr, eptr, &args[i], idx); - } - (*f->f_func)(expr, f->f_argc, args, optr); - for (i = 0; i < f->f_argc; i++) { - if (args[i].ex_type == ET_VEC) - fts_free(args[i].ex_vec); - } - return (eptr); + int i; + struct ex_ex args[MAX_ARGS]; + t_ex_func *f; + + f = (t_ex_func *)(eptr++)->ex_ptr; + if (!f || !f->f_name) { + return (exNULL); + } + if (f->f_argc > MAX_ARGS) { + post_error((fts_object_t *) expr, "expr: eval_func: asking too many arguments\n"); + return (exNULL); + } + + for (i = 0; i < f->f_argc; i++) { + args[i].ex_type = 0; + args[i].ex_int = 0; + eptr = ex_eval(expr, eptr, &args[i], idx); + } + (*f->f_func)(expr, f->f_argc, args, optr); + for (i = 0; i < f->f_argc; i++) { + if (args[i].ex_type == ET_VEC) + fts_free(args[i].ex_vec); + } + return (eptr); } /* * eval_store -- evaluate the '=' operator, - * make sure the first operator is a legal left operator - * and call ex_eval on the right operator + * make sure the first operator is a legal left operator + * and call ex_eval on the right operator */ struct ex_ex * eval_store(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) @@ -1253,11 +1253,11 @@ eval_store(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) /* the operation stack */ /* the result pointer */ { - struct ex_ex arg; - int isvalue; - char *tbl = (char *) 0; - char *var = (char *) 0; - int badleft = 0; + struct ex_ex arg; + int isvalue; + char *tbl = (char *) 0; + char *var = (char *) 0; + int badleft = 0; post("store called\n"); ex_print(eptr); @@ -1265,41 +1265,41 @@ eptr = ex_eval(expr, ++eptr, optr, idx); return (eptr); #ifdef notdef /* SDY */ - arg.ex_type = ET_INT; - arg.ex_int = 0; - if (eptr->ex_type == ET_VAR) { - var = (char *) eptr->ex_ptr; - - eptr = ex_eval(expr, ++eptr, &arg, idx); - (void)max_ex_var_store(expr, (t_symbol *)var, &arg, optr); - if (arg.ex_type == ET_VEC) - fts_free(arg.ex_vec); - } - - - if (eptr->ex_type == ET_SI) { - eptr++; - if (eptr->ex_type = - } - - /* the left operator should either be a value or a array member */ - switch (eptr->ex_type) { - case ET_SI: - if ((eptr + 1)->ex_type == OP_LB) { - } - if (!expr->exp_var[eptr->ex_int].ex_ptr) { - if (!(expr->exp_error & EE_NOTABLE)) { - post("expr: syntax error: no string for inlet %d", eptr->ex_int + 1); - post("expr: No more table errors will be reported"); - post("expr: till the next reset"); - expr->exp_error |= EE_NOTABLE; - } - badleft++; - } else - tbl = (char *) expr->exp_var[eptr->ex_int].ex_ptr; - break; - case ET_TBL: - } + arg.ex_type = ET_INT; + arg.ex_int = 0; + if (eptr->ex_type == ET_VAR) { + var = (char *) eptr->ex_ptr; + + eptr = ex_eval(expr, ++eptr, &arg, idx); + (void)max_ex_var_store(expr, (t_symbol *)var, &arg, optr); + if (arg.ex_type == ET_VEC) + fts_free(arg.ex_vec); + } + + + if (eptr->ex_type == ET_SI) { + eptr++; + if (eptr->ex_type = + } + + /* the left operator should either be a value or a array member */ + switch (eptr->ex_type) { + case ET_SI: + if ((eptr + 1)->ex_type == OP_LB) { + } + if (!expr->exp_var[eptr->ex_int].ex_ptr) { + if (!(expr->exp_error & EE_NOTABLE)) { + post("expr: syntax error: no string for inlet %d", eptr->ex_int + 1); + post("expr: No more table errors will be reported"); + post("expr: till the next reset"); + expr->exp_error |= EE_NOTABLE; + } + badleft++; + } else + tbl = (char *) expr->exp_var[eptr->ex_int].ex_ptr; + break; + case ET_TBL: + } #endif /* SDY */ } @@ -1312,43 +1312,43 @@ eval_tab(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) /* the operation stack */ /* the result pointer */ { - struct ex_ex arg; - char *tbl = (char *) 0; - int notable = 0; - - if (eptr->ex_type == ET_SI) { - if (!expr->exp_var[eptr->ex_int].ex_ptr) { + struct ex_ex arg; + char *tbl = (char *) 0; + int notable = 0; + + if (eptr->ex_type == ET_SI) { + if (!expr->exp_var[eptr->ex_int].ex_ptr) { /* SDY post_error() does not work in MAX/MSP yet - post_error((fts_object_t *) expr, - "expr: syntax error: no string for inlet %d\n", eptr->ex_int + 1); + post_error((fts_object_t *) expr, + "expr: syntax error: no string for inlet %d\n", eptr->ex_int + 1); */ - if (!(expr->exp_error & EE_NOTABLE)) { - post("expr: syntax error: no string for inlet %d", eptr->ex_int + 1); - post("expr: No more table errors will be reported"); - post("expr: till the next reset"); - expr->exp_error |= EE_NOTABLE; - } - notable++; - } else - tbl = (char *) expr->exp_var[eptr->ex_int].ex_ptr; - } else if (eptr->ex_type == ET_TBL) - tbl = (char *) eptr->ex_ptr; - else { - post_error((fts_object_t *) expr, "expr: eval_tbl: bad type %ld\n", eptr->ex_type); - notable++; - - } - arg.ex_type = 0; - arg.ex_int = 0; - eptr = ex_eval(expr, ++eptr, &arg, idx); - - optr->ex_type = ET_INT; - optr->ex_int = 0; - if (!notable) - (void)max_ex_tab(expr, (t_symbol *)tbl, &arg, optr); - if (arg.ex_type == ET_VEC) - fts_free(arg.ex_vec); - return (eptr); + if (!(expr->exp_error & EE_NOTABLE)) { + post("expr: syntax error: no string for inlet %d", eptr->ex_int + 1); + post("expr: No more table errors will be reported"); + post("expr: till the next reset"); + expr->exp_error |= EE_NOTABLE; + } + notable++; + } else + tbl = (char *) expr->exp_var[eptr->ex_int].ex_ptr; + } else if (eptr->ex_type == ET_TBL) + tbl = (char *) eptr->ex_ptr; + else { + post_error((fts_object_t *) expr, "expr: eval_tbl: bad type %ld\n", eptr->ex_type); + notable++; + + } + arg.ex_type = 0; + arg.ex_int = 0; + eptr = ex_eval(expr, ++eptr, &arg, idx); + + optr->ex_type = ET_INT; + optr->ex_int = 0; + if (!notable) + (void)max_ex_tab(expr, (t_symbol *)tbl, &arg, optr); + if (arg.ex_type == ET_VEC) + fts_free(arg.ex_vec); + return (eptr); } /* @@ -1360,38 +1360,38 @@ eval_var(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) /* the operation stack */ /* the result pointer */ { - struct ex_ex arg; - char *var = (char *) 0; - int novar = 0; - - if (eptr->ex_type == ET_SI) { - if (!expr->exp_var[eptr->ex_int].ex_ptr) { + struct ex_ex arg; + char *var = (char *) 0; + int novar = 0; + + if (eptr->ex_type == ET_SI) { + if (!expr->exp_var[eptr->ex_int].ex_ptr) { /* SDY post_error() does not work in MAX/MSP yet post_error((fts_object_t *) expr, "expr: syntax error: no string for inlet %d\n", eptr->ex_int + 1); */ - if (!(expr->exp_error & EE_NOVAR)) { - post("expr: syntax error: no string for inlet %d", eptr->ex_int + 1); - post("expr: No more table errors will be reported"); - post("expr: till the next reset"); - expr->exp_error |= EE_NOVAR; - } - novar++; - } else - var = (char *) expr->exp_var[eptr->ex_int].ex_ptr; - } else if (eptr->ex_type == ET_VAR) - var = (char *) eptr->ex_ptr; - else { - post_error((fts_object_t *) expr, "expr: eval_tbl: bad type %ld\n", eptr->ex_type); - novar++; - - } - - optr->ex_type = ET_INT; - optr->ex_int = 0; - if (!novar) - (void)max_ex_var(expr, (t_symbol *)var, optr); - return (++eptr); + if (!(expr->exp_error & EE_NOVAR)) { + post("expr: syntax error: no string for inlet %d", eptr->ex_int + 1); + post("expr: No more table errors will be reported"); + post("expr: till the next reset"); + expr->exp_error |= EE_NOVAR; + } + novar++; + } else + var = (char *) expr->exp_var[eptr->ex_int].ex_ptr; + } else if (eptr->ex_type == ET_VAR) + var = (char *) eptr->ex_ptr; + else { + post_error((fts_object_t *) expr, "expr: eval_tbl: bad type %ld\n", eptr->ex_type); + novar++; + + } + + optr->ex_type = ET_INT; + optr->ex_int = 0; + if (!novar) + (void)max_ex_var(expr, (t_symbol *)var, optr); + return (++eptr); } /* @@ -1404,148 +1404,148 @@ eval_sigidx(struct expr *expr, struct ex_ex *eptr, struct ex_ex *optr, int idx) /* the result pointer */ /* the index */ { - struct ex_ex arg; - struct ex_ex *reteptr; - int i = 0, j = 0; - float fi = 0, /* index in float */ - rem_i = 0; /* remains of the float */ - char *tbl; - - arg.ex_type = 0; - arg.ex_int = 0; - reteptr = ex_eval(expr, eptr + 1, &arg, idx); - if (arg.ex_type == ET_FLT) { - fi = arg.ex_flt; /* float index */ - i = (int) arg.ex_flt; /* integer index */ - rem_i = arg.ex_flt - i; /* remains of integer */ - } else if (arg.ex_type == ET_INT) { - fi = arg.ex_int; /* float index */ - i = arg.ex_int; - rem_i = 0; - } else { - post("eval_sigidx: bad res type (%d)", arg.ex_type); - } - optr->ex_type = ET_FLT; - /* - * indexing an input vector - */ - if (eptr->ex_type == ET_XI) { - if (fi > 0) { - if (!(expr->exp_error & EE_BI_INPUT)) { - expr->exp_error |= EE_BI_INPUT; - post("expr: input vector index > 0, (vector x%d[%f])", - eptr->ex_int + 1, i + rem_i); - post("fexpr~: index assumed to be = 0"); - post("fexpr~: no error report till next reset"); - ex_print(eptr); - } - /* just replace it with zero */ - i = 0; - rem_i = 0; - } - if (cal_sigidx(optr, i, rem_i, idx, expr->exp_vsize, - expr->exp_var[eptr->ex_int].ex_vec, - expr->exp_p_var[eptr->ex_int])) { - if (!(expr->exp_error & EE_BI_INPUT)) { - expr->exp_error |= EE_BI_INPUT; - post("expr: input vector index < -VectorSize, (vector x%d[%f])", eptr->ex_int + 1, fi); - ex_print(eptr); - post("fexpr~: index assumed to be = -%d", - expr->exp_vsize); - post("fexpr~: no error report till next reset"); - } - } - - /* - * indexing an output vector - */ - } else if (eptr->ex_type == ET_YO) { - /* for output vectors index of zero is not legal */ - if (fi >= 0) { - if (!(expr->exp_error & EE_BI_OUTPUT)) { - expr->exp_error |= EE_BI_OUTPUT; - post("fexpr~: bad output index, (%f)", fi); - ex_print(eptr); - post("fexpr~: no error report till next reset"); - post("fexpr~: index assumed to be = -1"); - } - i = -1; - } - if (eptr->ex_int >= expr->exp_nexpr) { - post("fexpr~: $y%d illegal: not that many exprs", - eptr->ex_int); - optr->ex_flt = 0; - return (reteptr); - } - if (cal_sigidx(optr, i, rem_i, idx, expr->exp_vsize, - expr->exp_tmpres[eptr->ex_int], - expr->exp_p_res[eptr->ex_int])) { - if (!(expr->exp_error & EE_BI_OUTPUT)) { - expr->exp_error |= EE_BI_OUTPUT; - post("fexpr~: bad output index, (%f)", fi); - ex_print(eptr); - post("fexpr~: index assumed to be = -%d", - expr->exp_vsize); - } - } - } else { - optr->ex_flt = 0; - post("fexpr~:eval_sigidx: internal error - unknown vector (%d)", - eptr->ex_type); - } - return (reteptr); + struct ex_ex arg; + struct ex_ex *reteptr; + int i = 0, j = 0; + float fi = 0, /* index in float */ + rem_i = 0; /* remains of the float */ + char *tbl; + + arg.ex_type = 0; + arg.ex_int = 0; + reteptr = ex_eval(expr, eptr + 1, &arg, idx); + if (arg.ex_type == ET_FLT) { + fi = arg.ex_flt; /* float index */ + i = (int) arg.ex_flt; /* integer index */ + rem_i = arg.ex_flt - i; /* remains of integer */ + } else if (arg.ex_type == ET_INT) { + fi = arg.ex_int; /* float index */ + i = arg.ex_int; + rem_i = 0; + } else { + post("eval_sigidx: bad res type (%d)", arg.ex_type); + } + optr->ex_type = ET_FLT; + /* + * indexing an input vector + */ + if (eptr->ex_type == ET_XI) { + if (fi > 0) { + if (!(expr->exp_error & EE_BI_INPUT)) { + expr->exp_error |= EE_BI_INPUT; + post("expr: input vector index > 0, (vector x%d[%f])", + eptr->ex_int + 1, i + rem_i); + post("fexpr~: index assumed to be = 0"); + post("fexpr~: no error report till next reset"); + ex_print(eptr); + } + /* just replace it with zero */ + i = 0; + rem_i = 0; + } + if (cal_sigidx(optr, i, rem_i, idx, expr->exp_vsize, + expr->exp_var[eptr->ex_int].ex_vec, + expr->exp_p_var[eptr->ex_int])) { + if (!(expr->exp_error & EE_BI_INPUT)) { + expr->exp_error |= EE_BI_INPUT; + post("expr: input vector index < -VectorSize, (vector x%d[%f])", eptr->ex_int + 1, fi); + ex_print(eptr); + post("fexpr~: index assumed to be = -%d", + expr->exp_vsize); + post("fexpr~: no error report till next reset"); + } + } + + /* + * indexing an output vector + */ + } else if (eptr->ex_type == ET_YO) { + /* for output vectors index of zero is not legal */ + if (fi >= 0) { + if (!(expr->exp_error & EE_BI_OUTPUT)) { + expr->exp_error |= EE_BI_OUTPUT; + post("fexpr~: bad output index, (%f)", fi); + ex_print(eptr); + post("fexpr~: no error report till next reset"); + post("fexpr~: index assumed to be = -1"); + } + i = -1; + } + if (eptr->ex_int >= expr->exp_nexpr) { + post("fexpr~: $y%d illegal: not that many exprs", + eptr->ex_int); + optr->ex_flt = 0; + return (reteptr); + } + if (cal_sigidx(optr, i, rem_i, idx, expr->exp_vsize, + expr->exp_tmpres[eptr->ex_int], + expr->exp_p_res[eptr->ex_int])) { + if (!(expr->exp_error & EE_BI_OUTPUT)) { + expr->exp_error |= EE_BI_OUTPUT; + post("fexpr~: bad output index, (%f)", fi); + ex_print(eptr); + post("fexpr~: index assumed to be = -%d", + expr->exp_vsize); + } + } + } else { + optr->ex_flt = 0; + post("fexpr~:eval_sigidx: internal error - unknown vector (%d)", + eptr->ex_type); + } + return (reteptr); } /* * cal_sigidx -- given two tables (one current one previous) calculate an * evaluation of a float index into the vectors by linear - * interpolation - * return 0 on success, 1 on failure (index out of bound) + * interpolation + * return 0 on success, 1 on failure (index out of bound) */ static int -cal_sigidx(struct ex_ex *optr, /* The output value */ - int i, float rem_i, /* integer and fractinal part of index */ - int idx, /* index of current fexpr~ processing */ - int vsize, /* vector size */ - float *curvec, float *prevec) /* current and previous table */ +cal_sigidx(struct ex_ex *optr, /* The output value */ + int i, float rem_i, /* integer and fractinal part of index */ + int idx, /* index of current fexpr~ processing */ + int vsize, /* vector size */ + float *curvec, float *prevec) /* current and previous table */ { - int n; - - n = i + idx; - if (n > 0) { - /* from the curvec */ - if (rem_i) - optr->ex_flt = curvec[n] + - rem_i * (curvec[n] - curvec[n - 1]); - else - optr->ex_flt = curvec[n]; - return (0); - } - if (n == 0) { - /* - * this is the case that the remaining float - * is between two tables - */ - if (rem_i) - optr->ex_flt = *curvec + - rem_i * (*curvec - prevec[vsize - 1]); - else - optr->ex_flt = *curvec; - return (0); - } - /* find the index in the saved buffer */ - n = vsize + n; - if (n > 0) { - if (rem_i) - optr->ex_flt = prevec[n] + - rem_i * (prevec[n] - prevec[n - 1]); - else - optr->ex_flt = prevec[n]; - return (0); - } - /* out of bound */ - optr->ex_flt = *prevec; - return (1); + int n; + + n = i + idx; + if (n > 0) { + /* from the curvec */ + if (rem_i) + optr->ex_flt = curvec[n] + + rem_i * (curvec[n] - curvec[n - 1]); + else + optr->ex_flt = curvec[n]; + return (0); + } + if (n == 0) { + /* + * this is the case that the remaining float + * is between two tables + */ + if (rem_i) + optr->ex_flt = *curvec + + rem_i * (*curvec - prevec[vsize - 1]); + else + optr->ex_flt = *curvec; + return (0); + } + /* find the index in the saved buffer */ + n = vsize + n; + if (n > 0) { + if (rem_i) + optr->ex_flt = prevec[n] + + rem_i * (prevec[n] - prevec[n - 1]); + else + optr->ex_flt = prevec[n]; + return (0); + } + /* out of bound */ + optr->ex_flt = *prevec; + return (1); } /* @@ -1554,314 +1554,314 @@ cal_sigidx(struct ex_ex *optr, /* The output value */ int getoken(struct expr *expr, struct ex_ex *eptr) { - char *p; - long i; + char *p; + long i; - if (!expr->exp_str) { - post("expr: getoken: expression string not set\n"); - return (0); - } + if (!expr->exp_str) { + post("expr: getoken: expression string not set\n"); + return (0); + } retry: - if (!*expr->exp_str) { - eptr->ex_type = 0; - eptr->ex_int = 0; - return (0); - } - if (*expr->exp_str == ';') { - expr->exp_str++; - eptr->ex_type = 0; - eptr->ex_int = 0; - return (0); - } - eptr->ex_type = ET_OP; - switch (*expr->exp_str++) { - case '\\': - case ' ': - case '\t': - goto retry; - case ';': - post("expr: syntax error: ';' not implemented\n"); - return (1); - case ',': - eptr->ex_op = OP_COMMA; - break; - case '(': - eptr->ex_op = OP_LP; - break; - case ')': - eptr->ex_op = OP_RP; - break; - case ']': - eptr->ex_op = OP_RB; - break; - case '~': - eptr->ex_op = OP_NEG; - break; - /* we will take care of unary minus later */ - case '*': - eptr->ex_op = OP_MUL; - break; - case '/': - eptr->ex_op = OP_DIV; - break; - case '%': - eptr->ex_op = OP_MOD; - break; - case '+': - eptr->ex_op = OP_ADD; - break; - case '-': - eptr->ex_op = OP_SUB; - break; - case '^': - eptr->ex_op = OP_XOR; - break; - case '[': - eptr->ex_op = OP_LB; - break; - case '!': - if (*expr->exp_str == '=') { - eptr->ex_op = OP_NE; - expr->exp_str++; - } else - eptr->ex_op = OP_NOT; - break; - case '<': - switch (*expr->exp_str) { - case '<': - eptr->ex_op = OP_SL; - expr->exp_str++; - break; - case '=': - eptr->ex_op = OP_LE; - expr->exp_str++; - break; - default: - eptr->ex_op = OP_LT; - break; - } - break; - case '>': - switch (*expr->exp_str) { - case '>': - eptr->ex_op = OP_SR; - expr->exp_str++; - break; - case '=': - eptr->ex_op = OP_GE; - expr->exp_str++; - break; - default: - eptr->ex_op = OP_GT; - break; - } - break; - case '=': - if (*expr->exp_str++ != '=') { - post("expr: syntax error: =\n"); - return (1); - } - eptr->ex_op = OP_EQ; - break; + if (!*expr->exp_str) { + eptr->ex_type = 0; + eptr->ex_int = 0; + return (0); + } + if (*expr->exp_str == ';') { + expr->exp_str++; + eptr->ex_type = 0; + eptr->ex_int = 0; + return (0); + } + eptr->ex_type = ET_OP; + switch (*expr->exp_str++) { + case '\\': + case ' ': + case '\t': + goto retry; + case ';': + post("expr: syntax error: ';' not implemented\n"); + return (1); + case ',': + eptr->ex_op = OP_COMMA; + break; + case '(': + eptr->ex_op = OP_LP; + break; + case ')': + eptr->ex_op = OP_RP; + break; + case ']': + eptr->ex_op = OP_RB; + break; + case '~': + eptr->ex_op = OP_NEG; + break; + /* we will take care of unary minus later */ + case '*': + eptr->ex_op = OP_MUL; + break; + case '/': + eptr->ex_op = OP_DIV; + break; + case '%': + eptr->ex_op = OP_MOD; + break; + case '+': + eptr->ex_op = OP_ADD; + break; + case '-': + eptr->ex_op = OP_SUB; + break; + case '^': + eptr->ex_op = OP_XOR; + break; + case '[': + eptr->ex_op = OP_LB; + break; + case '!': + if (*expr->exp_str == '=') { + eptr->ex_op = OP_NE; + expr->exp_str++; + } else + eptr->ex_op = OP_NOT; + break; + case '<': + switch (*expr->exp_str) { + case '<': + eptr->ex_op = OP_SL; + expr->exp_str++; + break; + case '=': + eptr->ex_op = OP_LE; + expr->exp_str++; + break; + default: + eptr->ex_op = OP_LT; + break; + } + break; + case '>': + switch (*expr->exp_str) { + case '>': + eptr->ex_op = OP_SR; + expr->exp_str++; + break; + case '=': + eptr->ex_op = OP_GE; + expr->exp_str++; + break; + default: + eptr->ex_op = OP_GT; + break; + } + break; + case '=': + if (*expr->exp_str++ != '=') { + post("expr: syntax error: =\n"); + return (1); + } + eptr->ex_op = OP_EQ; + break; /* do not allow the store till the function is fixed - if (*expr->exp_str != '=') - eptr->ex_op = OP_STORE; - else { - expr->exp_str++; - eptr->ex_op = OP_EQ; - } - break; + if (*expr->exp_str != '=') + eptr->ex_op = OP_STORE; + else { + expr->exp_str++; + eptr->ex_op = OP_EQ; + } + break; */ - case '&': - if (*expr->exp_str == '&') { - expr->exp_str++; - eptr->ex_op = OP_LAND; - } else - eptr->ex_op = OP_AND; - break; - - case '|': - if ((*expr->exp_str == '|')) { - expr->exp_str++; - eptr->ex_op = OP_LOR; - } else - eptr->ex_op = OP_OR; - break; - case '$': - switch (*expr->exp_str++) { - case 'I': - case 'i': - eptr->ex_type = ET_II; - break; - case 'F': - case 'f': - eptr->ex_type = ET_FI; - break; - case 'S': - case 's': - eptr->ex_type = ET_SI; - break; - case 'V': - case 'v': - if (IS_EXPR_TILDE(expr)) { - eptr->ex_type = ET_VI; - break; - } - post("$v? works only for expr~"); - post("expr: syntax error: %s\n", &expr->exp_str[-2]); - return (1); - case 'X': - case 'x': - if (IS_FEXPR_TILDE(expr)) { - eptr->ex_type = ET_XI; - if (isdigit(*expr->exp_str)) - break; - /* for $x[] is a shorhand for $x1[] */ - eptr->ex_int = 0; - goto noinletnum; - } - post("$x? works only for fexpr~"); - post("expr: syntax error: %s\n", &expr->exp_str[-2]); - return (1); - case 'y': - case 'Y': - if (IS_FEXPR_TILDE(expr)) { - eptr->ex_type = ET_YO; - /*$y takes no number */ - if (isdigit(*expr->exp_str)) - break; - /* for $y[] is a shorhand for $y1[] */ - eptr->ex_int = 0; - goto noinletnum; - } - post("$y works only for fexpr~"); - default: - post("expr: syntax error: %s\n", &expr->exp_str[-2]); - return (1); - } - p = atoif(expr->exp_str, &eptr->ex_op, &i); - if (!p) { - post("expr: syntax error: %s\n", &expr->exp_str[-2]); - return (1); - } - if (i != ET_INT) { - post("expr: syntax error: %s\n", expr->exp_str); - return (1); - } - /* - * make the user inlets one based rather than zero based - * therefore we decrement the number that user has supplied - */ - if (!eptr->ex_op || (eptr->ex_op)-- > MAX_VARS) { - post("expr: syntax error: inlet or outlet out of range: %s\n", - expr->exp_str); - return (1); - } - - /* - * until we can change the input type of inlets on - * the fly (at pd_new() - * time) the first input to expr~ is always a vectore - * and $f1 or $i1 is - * illegal for fexr~ - */ - if (eptr->ex_op == 0 && - (IS_FEXPR_TILDE(expr) || IS_EXPR_TILDE(expr)) && - (eptr->ex_type==ET_II || eptr->ex_type==ET_FI || - eptr->ex_type==ET_SI)) { - post("first inlet of expr~/fexpr~ can only be a vector"); - return (1); - } - /* record the inlet or outlet type and check for consistency */ - if (eptr->ex_type == ET_YO ) { - /* it is an outlet for fexpr~*/ - /* no need to do anything */ - ; - } else if (!expr->exp_var[eptr->ex_op].ex_type) - expr->exp_var[eptr->ex_op].ex_type = eptr->ex_type; - else if (expr->exp_var[eptr->ex_op].ex_type != eptr->ex_type) { - post("expr: syntax error: inlets can only have one type: %s\n", expr->exp_str); - return (1); - } - expr->exp_str = p; + case '&': + if (*expr->exp_str == '&') { + expr->exp_str++; + eptr->ex_op = OP_LAND; + } else + eptr->ex_op = OP_AND; + break; + + case '|': + if ((*expr->exp_str == '|')) { + expr->exp_str++; + eptr->ex_op = OP_LOR; + } else + eptr->ex_op = OP_OR; + break; + case '$': + switch (*expr->exp_str++) { + case 'I': + case 'i': + eptr->ex_type = ET_II; + break; + case 'F': + case 'f': + eptr->ex_type = ET_FI; + break; + case 'S': + case 's': + eptr->ex_type = ET_SI; + break; + case 'V': + case 'v': + if (IS_EXPR_TILDE(expr)) { + eptr->ex_type = ET_VI; + break; + } + post("$v? works only for expr~"); + post("expr: syntax error: %s\n", &expr->exp_str[-2]); + return (1); + case 'X': + case 'x': + if (IS_FEXPR_TILDE(expr)) { + eptr->ex_type = ET_XI; + if (isdigit(*expr->exp_str)) + break; + /* for $x[] is a shorhand for $x1[] */ + eptr->ex_int = 0; + goto noinletnum; + } + post("$x? works only for fexpr~"); + post("expr: syntax error: %s\n", &expr->exp_str[-2]); + return (1); + case 'y': + case 'Y': + if (IS_FEXPR_TILDE(expr)) { + eptr->ex_type = ET_YO; + /*$y takes no number */ + if (isdigit(*expr->exp_str)) + break; + /* for $y[] is a shorhand for $y1[] */ + eptr->ex_int = 0; + goto noinletnum; + } + post("$y works only for fexpr~"); + default: + post("expr: syntax error: %s\n", &expr->exp_str[-2]); + return (1); + } + p = atoif(expr->exp_str, &eptr->ex_op, &i); + if (!p) { + post("expr: syntax error: %s\n", &expr->exp_str[-2]); + return (1); + } + if (i != ET_INT) { + post("expr: syntax error: %s\n", expr->exp_str); + return (1); + } + /* + * make the user inlets one based rather than zero based + * therefore we decrement the number that user has supplied + */ + if (!eptr->ex_op || (eptr->ex_op)-- > MAX_VARS) { + post("expr: syntax error: inlet or outlet out of range: %s\n", + expr->exp_str); + return (1); + } + + /* + * until we can change the input type of inlets on + * the fly (at pd_new() + * time) the first input to expr~ is always a vectore + * and $f1 or $i1 is + * illegal for fexr~ + */ + if (eptr->ex_op == 0 && + (IS_FEXPR_TILDE(expr) || IS_EXPR_TILDE(expr)) && + (eptr->ex_type==ET_II || eptr->ex_type==ET_FI || + eptr->ex_type==ET_SI)) { + post("first inlet of expr~/fexpr~ can only be a vector"); + return (1); + } + /* record the inlet or outlet type and check for consistency */ + if (eptr->ex_type == ET_YO ) { + /* it is an outlet for fexpr~*/ + /* no need to do anything */ + ; + } else if (!expr->exp_var[eptr->ex_op].ex_type) + expr->exp_var[eptr->ex_op].ex_type = eptr->ex_type; + else if (expr->exp_var[eptr->ex_op].ex_type != eptr->ex_type) { + post("expr: syntax error: inlets can only have one type: %s\n", expr->exp_str); + return (1); + } + expr->exp_str = p; noinletnum: - break; - case '"': - { - struct ex_ex ex; - - p = expr->exp_str; - if (!*expr->exp_str || *expr->exp_str == '"') { - post("expr: syntax error: empty symbol: %s\n", --expr->exp_str); - return (1); - } - if (getoken(expr, &ex)) - return (1); - switch (ex.ex_type) { - case ET_STR: - if (ex_getsym(ex.ex_ptr, (t_symbol **)&(eptr->ex_ptr))) { - post("expr: syntax error: getoken: problms with ex_getsym\n"); - return (1); - } - eptr->ex_type = ET_SYM; - break; - case ET_SI: - *eptr = ex; - eptr->ex_type = ET_VSYM; - break; - default: - post("expr: syntax error: bad symbol name: %s\n", p); - return (1); - } - if (*expr->exp_str++ != '"') { - post("expr: syntax error: missing '\"'\n"); - return (1); - } - break; - } - case '.': - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - p = atoif(--expr->exp_str, &eptr->ex_int, &eptr->ex_type); - if (!p) - return (1); - expr->exp_str = p; - break; - - default: - /* - * has to be a string, it should either be a - * function or a table - */ - p = --expr->exp_str; - for (i = 0; name_ok(*p); i++) - p++; - if (!i) { - post("expr: syntax error: %s\n", expr->exp_str); - return (1); - } - eptr->ex_ptr = (char *)fts_malloc(i + 1); - strncpy(eptr->ex_ptr, expr->exp_str, (int) i); - (eptr->ex_ptr)[i] = 0; - expr->exp_str = p; - /* - * we mark this as a string and later we will change this - * to either a function or a table - */ - eptr->ex_type = ET_STR; - break; - } - return (0); + break; + case '"': + { + struct ex_ex ex; + + p = expr->exp_str; + if (!*expr->exp_str || *expr->exp_str == '"') { + post("expr: syntax error: empty symbol: %s\n", --expr->exp_str); + return (1); + } + if (getoken(expr, &ex)) + return (1); + switch (ex.ex_type) { + case ET_STR: + if (ex_getsym(ex.ex_ptr, (t_symbol **)&(eptr->ex_ptr))) { + post("expr: syntax error: getoken: problms with ex_getsym\n"); + return (1); + } + eptr->ex_type = ET_SYM; + break; + case ET_SI: + *eptr = ex; + eptr->ex_type = ET_VSYM; + break; + default: + post("expr: syntax error: bad symbol name: %s\n", p); + return (1); + } + if (*expr->exp_str++ != '"') { + post("expr: syntax error: missing '\"'\n"); + return (1); + } + break; + } + case '.': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + p = atoif(--expr->exp_str, &eptr->ex_int, &eptr->ex_type); + if (!p) + return (1); + expr->exp_str = p; + break; + + default: + /* + * has to be a string, it should either be a + * function or a table + */ + p = --expr->exp_str; + for (i = 0; name_ok(*p); i++) + p++; + if (!i) { + post("expr: syntax error: %s\n", expr->exp_str); + return (1); + } + eptr->ex_ptr = (char *)fts_malloc(i + 1); + strncpy(eptr->ex_ptr, expr->exp_str, (int) i); + (eptr->ex_ptr)[i] = 0; + expr->exp_str = p; + /* + * we mark this as a string and later we will change this + * to either a function or a table + */ + eptr->ex_type = ET_STR; + break; + } + return (0); } /* @@ -1870,100 +1870,100 @@ noinletnum: char * atoif(char *s, long int *value, long int *type) { - char *p; - long int_val = 0; - int flt = 0; - float pos = 0; - float flt_val = 0; - int base = 10; - - p = s; - if (*p == '0' && (p[1] == 'x' || p[1] == 'X')) { - base = 16; - p += 2; - } - while (8) { - switch (*p) { - case '.': - if (flt || base != 10) { - post("expr: syntax error: %s\n", s); - return ((char *) 0); - } - flt++; - pos = 10; - flt_val = int_val; - break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (flt) { - flt_val += (*p - '0') / pos; - pos *= 10; - } else { - int_val *= base; - int_val += (*p - '0'); - } - break; - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - if (base != 16 || flt) { - post("expr: syntax error: %s\n", s); - return ((char *) 0); - } - int_val *= base; - int_val += (*p - 'a' + 10); - break; - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - if (base != 16 || flt) { - post("expr: syntax error: %s\n", s); - return ((char *) 0); - } - int_val *= base; - int_val += (*p - 'A' + 10); - break; - default: - if (flt) { - *type = ET_FLT; - *((float *) value) = flt_val; - } else { - *type = ET_INT; - *value = int_val; - } - return (p); - } - p++; - } + char *p; + long int_val = 0; + int flt = 0; + float pos = 0; + float flt_val = 0; + int base = 10; + + p = s; + if (*p == '0' && (p[1] == 'x' || p[1] == 'X')) { + base = 16; + p += 2; + } + while (8) { + switch (*p) { + case '.': + if (flt || base != 10) { + post("expr: syntax error: %s\n", s); + return ((char *) 0); + } + flt++; + pos = 10; + flt_val = int_val; + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (flt) { + flt_val += (*p - '0') / pos; + pos *= 10; + } else { + int_val *= base; + int_val += (*p - '0'); + } + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + if (base != 16 || flt) { + post("expr: syntax error: %s\n", s); + return ((char *) 0); + } + int_val *= base; + int_val += (*p - 'a' + 10); + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + if (base != 16 || flt) { + post("expr: syntax error: %s\n", s); + return ((char *) 0); + } + int_val *= base; + int_val += (*p - 'A' + 10); + break; + default: + if (flt) { + *type = ET_FLT; + *((float *) value) = flt_val; + } else { + *type = ET_INT; + *value = int_val; + } + return (p); + } + p++; + } } /* * find_func -- returns a pointer to the found function structure - * otherwise it returns 0 + * otherwise it returns 0 */ t_ex_func * find_func(char *s) { - t_ex_func *f; + t_ex_func *f; - for (f = ex_funcs; f->f_name; f++) - if (!strcmp(f->f_name, s)) - return (f); - return ((t_ex_func *) 0); + for (f = ex_funcs; f->f_name; f++) + if (!strcmp(f->f_name, s)) + return (f); + return ((t_ex_func *) 0); } @@ -1975,166 +1975,166 @@ void ex_print(struct ex_ex *eptr) { - while (eptr->ex_type) { - switch (eptr->ex_type) { - case ET_INT: - post("%ld ", eptr->ex_int); - break; - case ET_FLT: - post("%f ", eptr->ex_flt); - break; - case ET_STR: - post("%s ", eptr->ex_ptr); - break; - case ET_TBL: - case ET_VAR: - post("%s ", ex_symname((fts_symbol_t )eptr->ex_ptr)); - break; - case ET_SYM: - post("\"%s\" ", ex_symname((fts_symbol_t )eptr->ex_ptr)); - break; - case ET_VSYM: - post("\"$s%ld\" ", eptr->ex_int + 1); - break; - case ET_FUNC: - post("%s ", - ((t_ex_func *)eptr->ex_ptr)->f_name); - break; - case ET_LP: - post("%c", '('); - break; - /* CHANGE - case ET_RP: - post("%c ", ')'); - break; - */ - case ET_LB: - post("%c", '['); - break; - /* CHANGE - case ET_RB: - post("%c ", ']'); - break; - */ - case ET_II: - post("$i%ld ", eptr->ex_int + 1); - break; - case ET_FI: - post("$f%ld ", eptr->ex_int + 1); - break; - case ET_SI: - post("$s%lx ", eptr->ex_ptr); - break; - case ET_VI: - post("$v%lx ", eptr->ex_vec); - break; - case ET_VEC: - post("vec = %ld ", eptr->ex_vec); - break; - case ET_YOM1: - case ET_YO: - post("$y%d", eptr->ex_int + 1); - break; - case ET_XI: - case ET_XI0: - post("$x%d", eptr->ex_int + 1); - break; - case ET_OP: - switch (eptr->ex_op) { - case OP_LP: - post("%c", '('); - break; - case OP_RP: - post("%c ", ')'); - break; - case OP_LB: - post("%c", '['); - break; - case OP_RB: - post("%c ", ']'); - break; - case OP_NOT: - post("%c", '!'); - break; - case OP_NEG: - post("%c", '~'); - break; - case OP_UMINUS: - post("%c", '-'); - break; - case OP_MUL: - post("%c", '*'); - break; - case OP_DIV: - post("%c", '/'); - break; - case OP_MOD: - post("%c", '%'); - break; - case OP_ADD: - post("%c", '+'); - break; - case OP_SUB: - post("%c", '-'); - break; - case OP_SL: - post("%s", "<<"); - break; - case OP_SR: - post("%s", ">>"); - break; - case OP_LT: - post("%c", '<'); - break; - case OP_LE: - post("%s", "<="); - break; - case OP_GT: - post("%c", '>'); - break; - case OP_GE: - post("%s", ">="); - break; - case OP_EQ: - post("%s", "=="); - break; - case OP_STORE: - post("%s", "="); - break; - case OP_NE: - post("%s", "!="); - break; - case OP_AND: - post("%c", '&'); - break; - case OP_XOR: - post("%c", '^'); - break; - case OP_OR: - post("%c", '|'); - break; - case OP_LAND: - post("%s", "&&"); - break; - case OP_LOR: - post("%s", "||"); - break; - case OP_COMMA: - post("%c", ','); - break; - case OP_SEMI: - post("%c", ';'); - break; - default: - post("expr: ex_print: bad op 0x%lx\n", eptr->ex_op); - } - break; - default: - post("expr: ex_print: bad type 0x%lx\n", eptr->ex_type); - } - eptr++; - } - post("\n"); + while (eptr->ex_type) { + switch (eptr->ex_type) { + case ET_INT: + post("%ld ", eptr->ex_int); + break; + case ET_FLT: + post("%f ", eptr->ex_flt); + break; + case ET_STR: + post("%s ", eptr->ex_ptr); + break; + case ET_TBL: + case ET_VAR: + post("%s ", ex_symname((fts_symbol_t )eptr->ex_ptr)); + break; + case ET_SYM: + post("\"%s\" ", ex_symname((fts_symbol_t )eptr->ex_ptr)); + break; + case ET_VSYM: + post("\"$s%ld\" ", eptr->ex_int + 1); + break; + case ET_FUNC: + post("%s ", + ((t_ex_func *)eptr->ex_ptr)->f_name); + break; + case ET_LP: + post("%c", '('); + break; + /* CHANGE + case ET_RP: + post("%c ", ')'); + break; + */ + case ET_LB: + post("%c", '['); + break; + /* CHANGE + case ET_RB: + post("%c ", ']'); + break; + */ + case ET_II: + post("$i%ld ", eptr->ex_int + 1); + break; + case ET_FI: + post("$f%ld ", eptr->ex_int + 1); + break; + case ET_SI: + post("$s%lx ", eptr->ex_ptr); + break; + case ET_VI: + post("$v%lx ", eptr->ex_vec); + break; + case ET_VEC: + post("vec = %ld ", eptr->ex_vec); + break; + case ET_YOM1: + case ET_YO: + post("$y%d", eptr->ex_int + 1); + break; + case ET_XI: + case ET_XI0: + post("$x%d", eptr->ex_int + 1); + break; + case ET_OP: + switch (eptr->ex_op) { + case OP_LP: + post("%c", '('); + break; + case OP_RP: + post("%c ", ')'); + break; + case OP_LB: + post("%c", '['); + break; + case OP_RB: + post("%c ", ']'); + break; + case OP_NOT: + post("%c", '!'); + break; + case OP_NEG: + post("%c", '~'); + break; + case OP_UMINUS: + post("%c", '-'); + break; + case OP_MUL: + post("%c", '*'); + break; + case OP_DIV: + post("%c", '/'); + break; + case OP_MOD: + post("%c", '%'); + break; + case OP_ADD: + post("%c", '+'); + break; + case OP_SUB: + post("%c", '-'); + break; + case OP_SL: + post("%s", "<<"); + break; + case OP_SR: + post("%s", ">>"); + break; + case OP_LT: + post("%c", '<'); + break; + case OP_LE: + post("%s", "<="); + break; + case OP_GT: + post("%c", '>'); + break; + case OP_GE: + post("%s", ">="); + break; + case OP_EQ: + post("%s", "=="); + break; + case OP_STORE: + post("%s", "="); + break; + case OP_NE: + post("%s", "!="); + break; + case OP_AND: + post("%c", '&'); + break; + case OP_XOR: + post("%c", '^'); + break; + case OP_OR: + post("%c", '|'); + break; + case OP_LAND: + post("%s", "&&"); + break; + case OP_LOR: + post("%s", "||"); + break; + case OP_COMMA: + post("%c", ','); + break; + case OP_SEMI: + post("%c", ';'); + break; + default: + post("expr: ex_print: bad op 0x%lx\n", eptr->ex_op); + } + break; + default: + post("expr: ex_print: bad type 0x%lx\n", eptr->ex_type); + } + eptr++; + } + post("\n"); } #ifdef NT diff --git a/pd/extra/expr~/vexp.h b/pd/extra/expr~/vexp.h index e90c8409..92dfb06e 100644 --- a/pd/extra/expr~/vexp.h +++ b/pd/extra/expr~/vexp.h @@ -43,9 +43,9 @@ /* This is put in fts_to_pd.h #ifdef MSP -#define t_atom Atom -#define t_symbol Symbol -#define pd_new(x) newobject(x); +#define t_atom Atom +#define t_symbol Symbol +#define pd_new(x) newobject(x); #define t_outlet void #endif */ @@ -55,8 +55,8 @@ * is 10. */ -#define MAX_VARS 9 -#define MINODES 10 /* was 200 */ +#define MAX_VARS 9 +#define MINODES 10 /* was 200 */ /* terminal defines */ @@ -67,156 +67,156 @@ * separators are defines as operators just for convenience */ -#define OP_SEMI ((long)(1<<16|1)) /* ; */ -#define OP_COMMA ((long)(2<<16|2)) /* , */ -#define OP_LOR ((long)(3<<16|3)) /* || */ -#define OP_LAND ((long)(4<<16|4)) /* && */ -#define OP_OR ((long)(5<<16|5)) /* | */ -#define OP_XOR ((long)(6<<16|6)) /* ^ */ -#define OP_AND ((long)(7<<16|7)) /* & */ -#define OP_NE ((long)(8<<16|8)) /* != */ -#define OP_EQ ((long)(8<<16|9)) /* == */ -#define OP_GE ((long)(9<<16|10)) /* >= */ -#define OP_GT ((long)(9<<16|11)) /* > */ -#define OP_LE ((long)(9<<16|12)) /* <= */ -#define OP_LT ((long)(9<<16|13)) /* < */ -#define OP_SR ((long)(10<<16|14)) /* >> */ -#define OP_SL ((long)(10<<16|15)) /* << */ -#define OP_SUB ((long)(11<<16|16)) /* - */ -#define OP_ADD ((long)(11<<16|17)) /* + */ -#define OP_MOD ((long)(12<<16|18)) /* % */ -#define OP_DIV ((long)(12<<16|19)) /* / */ -#define OP_MUL ((long)(12<<16|20)) /* * */ -#define OP_UMINUS ((long)(13<<16|21)) /* - unary minus */ -#define OP_NEG ((long)(13<<16|22)) /* ~ one complement */ -#define OP_NOT ((long)(13<<16|23)) /* ! */ -#define OP_RB ((long)(14<<16|24)) /* ] */ -#define OP_LB ((long)(14<<16|25)) /* [ */ -#define OP_RP ((long)(14<<16|26)) /* ) */ -#define OP_LP ((long)(14<<16|27)) /* ( */ -#define OP_STORE ((long)(15<<16|28)) /* = */ -#define HI_PRE ((long)(100<<16)) /* infinite precedence */ -#define PRE_MASK ((long)0xffff0000) /* precedence level mask */ +#define OP_SEMI ((long)(1<<16|1)) /* ; */ +#define OP_COMMA ((long)(2<<16|2)) /* , */ +#define OP_LOR ((long)(3<<16|3)) /* || */ +#define OP_LAND ((long)(4<<16|4)) /* && */ +#define OP_OR ((long)(5<<16|5)) /* | */ +#define OP_XOR ((long)(6<<16|6)) /* ^ */ +#define OP_AND ((long)(7<<16|7)) /* & */ +#define OP_NE ((long)(8<<16|8)) /* != */ +#define OP_EQ ((long)(8<<16|9)) /* == */ +#define OP_GE ((long)(9<<16|10)) /* >= */ +#define OP_GT ((long)(9<<16|11)) /* > */ +#define OP_LE ((long)(9<<16|12)) /* <= */ +#define OP_LT ((long)(9<<16|13)) /* < */ +#define OP_SR ((long)(10<<16|14)) /* >> */ +#define OP_SL ((long)(10<<16|15)) /* << */ +#define OP_SUB ((long)(11<<16|16)) /* - */ +#define OP_ADD ((long)(11<<16|17)) /* + */ +#define OP_MOD ((long)(12<<16|18)) /* % */ +#define OP_DIV ((long)(12<<16|19)) /* / */ +#define OP_MUL ((long)(12<<16|20)) /* * */ +#define OP_UMINUS ((long)(13<<16|21)) /* - unary minus */ +#define OP_NEG ((long)(13<<16|22)) /* ~ one complement */ +#define OP_NOT ((long)(13<<16|23)) /* ! */ +#define OP_RB ((long)(14<<16|24)) /* ] */ +#define OP_LB ((long)(14<<16|25)) /* [ */ +#define OP_RP ((long)(14<<16|26)) /* ) */ +#define OP_LP ((long)(14<<16|27)) /* ( */ +#define OP_STORE ((long)(15<<16|28)) /* = */ +#define HI_PRE ((long)(100<<16)) /* infinite precedence */ +#define PRE_MASK ((long)0xffff0000) /* precedence level mask */ struct ex_ex; -#define name_ok(c) (((c)=='_') || ((c)>='a' && (c)<='z') || \ - ((c)>='A' && (c)<='Z') || ((c) >= '0' && (c) <= '9')) -#define unary_op(x) ((x) == OP_NOT || (x) == OP_NEG || (x) == OP_UMINUS) +#define name_ok(c) (((c)=='_') || ((c)>='a' && (c)<='z') || \ + ((c)>='A' && (c)<='Z') || ((c) >= '0' && (c) <= '9')) +#define unary_op(x) ((x) == OP_NOT || (x) == OP_NEG || (x) == OP_UMINUS) struct ex_ex { - union { - long v_int; - float v_flt; - t_float *v_vec; /* this is an for allocated vector */ - long op; - char *ptr; - } ex_cont; /* content */ -#define ex_int ex_cont.v_int -#define ex_flt ex_cont.v_flt -#define ex_vec ex_cont.v_vec -#define ex_op ex_cont.op -#define ex_ptr ex_cont.ptr - long ex_type; /* type of the node */ + union { + long v_int; + float v_flt; + t_float *v_vec; /* this is an for allocated vector */ + long op; + char *ptr; + } ex_cont; /* content */ +#define ex_int ex_cont.v_int +#define ex_flt ex_cont.v_flt +#define ex_vec ex_cont.v_vec +#define ex_op ex_cont.op +#define ex_ptr ex_cont.ptr + long ex_type; /* type of the node */ }; -#define exNULL ((struct ex_ex *)0) +#define exNULL ((struct ex_ex *)0) /* defines for ex_type */ -#define ET_INT 1 /* an int */ -#define ET_FLT 2 /* a float */ -#define ET_OP 3 /* operator */ -#define ET_STR 4 /* string */ -#define ET_TBL 5 /* a table, the content is a pointer */ -#define ET_FUNC 6 /* a function */ -#define ET_SYM 7 /* symbol ("string") */ -#define ET_VSYM 8 /* variable symbol ("$s?") */ - /* we treat parenthesis and brackets */ - /* special to keep a pointer to their */ - /* match in the content */ -#define ET_LP 9 /* left parenthesis */ -#define ET_LB 10 /* left bracket */ -#define ET_II 11 /* and integer inlet */ -#define ET_FI 12 /* float inlet */ -#define ET_SI 13 /* string inlet */ -#define ET_VI 14 /* signal inlet */ -#define ET_VEC 15 /* allocated signal vector */ - /* special types for fexpr~ */ -#define ET_YO 16 /* vector output for fexpr~ */ -#define ET_YOM1 17 /* shorthand for $y?[-1] */ -#define ET_XI 18 /* vector input for fexpr~ */ -#define ET_XI0 20 /* shorthand for $x?[0] */ -#define ET_VAR 21 /* variable */ +#define ET_INT 1 /* an int */ +#define ET_FLT 2 /* a float */ +#define ET_OP 3 /* operator */ +#define ET_STR 4 /* string */ +#define ET_TBL 5 /* a table, the content is a pointer */ +#define ET_FUNC 6 /* a function */ +#define ET_SYM 7 /* symbol ("string") */ +#define ET_VSYM 8 /* variable symbol ("$s?") */ + /* we treat parenthesis and brackets */ + /* special to keep a pointer to their */ + /* match in the content */ +#define ET_LP 9 /* left parenthesis */ +#define ET_LB 10 /* left bracket */ +#define ET_II 11 /* and integer inlet */ +#define ET_FI 12 /* float inlet */ +#define ET_SI 13 /* string inlet */ +#define ET_VI 14 /* signal inlet */ +#define ET_VEC 15 /* allocated signal vector */ + /* special types for fexpr~ */ +#define ET_YO 16 /* vector output for fexpr~ */ +#define ET_YOM1 17 /* shorthand for $y?[-1] */ +#define ET_XI 18 /* vector input for fexpr~ */ +#define ET_XI0 20 /* shorthand for $x?[0] */ +#define ET_VAR 21 /* variable */ /* defines for ex_flags */ -#define EF_TYPE_MASK 0x07 /* first three bits define the type of expr */ -#define EF_EXPR 0x01 /* expr - control in and out */ -#define EF_EXPR_TILDE 0x02 /* expr~ signal and control in, signal out */ -#define EF_FEXPR_TILDE 0x04 /* fexpr~ filter expression */ +#define EF_TYPE_MASK 0x07 /* first three bits define the type of expr */ +#define EF_EXPR 0x01 /* expr - control in and out */ +#define EF_EXPR_TILDE 0x02 /* expr~ signal and control in, signal out */ +#define EF_FEXPR_TILDE 0x04 /* fexpr~ filter expression */ -#define EF_STOP 0x08 /* is it stopped used for expr~ and fexpr~ */ -#define EF_VERBOSE 0x10 /* verbose mode */ +#define EF_STOP 0x08 /* is it stopped used for expr~ and fexpr~ */ +#define EF_VERBOSE 0x10 /* verbose mode */ -#define IS_EXPR(x) ((((x)->exp_flags&EF_TYPE_MASK)|EF_EXPR) == EF_EXPR) -#define IS_EXPR_TILDE(x) \ +#define IS_EXPR(x) ((((x)->exp_flags&EF_TYPE_MASK)|EF_EXPR) == EF_EXPR) +#define IS_EXPR_TILDE(x) \ ((((x)->exp_flags&EF_TYPE_MASK)|EF_EXPR_TILDE)==EF_EXPR_TILDE) -#define IS_FEXPR_TILDE(x) \ +#define IS_FEXPR_TILDE(x) \ ((((x)->exp_flags&EF_TYPE_MASK)|EF_FEXPR_TILDE)==EF_FEXPR_TILDE) -#define SET_EXPR(x) (x)->exp_flags |= EF_EXPR; \ - (x)->exp_flags &= ~EF_EXPR_TILDE; \ - (x)->exp_flags &= ~EF_FEXPR_TILDE; +#define SET_EXPR(x) (x)->exp_flags |= EF_EXPR; \ + (x)->exp_flags &= ~EF_EXPR_TILDE; \ + (x)->exp_flags &= ~EF_FEXPR_TILDE; -#define SET_EXPR_TILDE(x) (x)->exp_flags &= ~EF_EXPR; \ - (x)->exp_flags |= EF_EXPR_TILDE; \ - (x)->exp_flags &= ~EF_FEXPR_TILDE; +#define SET_EXPR_TILDE(x) (x)->exp_flags &= ~EF_EXPR; \ + (x)->exp_flags |= EF_EXPR_TILDE; \ + (x)->exp_flags &= ~EF_FEXPR_TILDE; -#define SET_FEXPR_TILDE(x) (x)->exp_flags &= ~EF_EXPR; \ - (x)->exp_flags &= ~EF_EXPR_TILDE; \ - (x)->exp_flags |= EF_FEXPR_TILDE; +#define SET_FEXPR_TILDE(x) (x)->exp_flags &= ~EF_EXPR; \ + (x)->exp_flags &= ~EF_EXPR_TILDE; \ + (x)->exp_flags |= EF_FEXPR_TILDE; /* * defines for expr_error */ -#define EE_DZ 0x01 /* divide by zero error */ -#define EE_BI_OUTPUT 0x02 /* Bad output index */ -#define EE_BI_INPUT 0x04 /* Bad input index */ -#define EE_NOTABLE 0x08 /* NO TABLE */ -#define EE_NOVAR 0x10 /* NO VARIABLE */ +#define EE_DZ 0x01 /* divide by zero error */ +#define EE_BI_OUTPUT 0x02 /* Bad output index */ +#define EE_BI_INPUT 0x04 /* Bad input index */ +#define EE_NOTABLE 0x08 /* NO TABLE */ +#define EE_NOVAR 0x10 /* NO VARIABLE */ typedef struct expr { #ifdef PD - t_object exp_ob; + t_object exp_ob; #else /* MSP */ - t_pxobject exp_ob; + t_pxobject exp_ob; #endif - int exp_flags; /* are we expr~, fexpr~, or expr */ - int exp_error; /* reported errors */ - int exp_nexpr; /* number of expressions */ - char *exp_string; /* the full expression string */ - char *exp_str; /* current parsing position */ - t_outlet *exp_outlet[MAX_VARS]; + int exp_flags; /* are we expr~, fexpr~, or expr */ + int exp_error; /* reported errors */ + int exp_nexpr; /* number of expressions */ + char *exp_string; /* the full expression string */ + char *exp_str; /* current parsing position */ + t_outlet *exp_outlet[MAX_VARS]; #ifdef PD - struct _exprproxy *exp_proxy; + struct _exprproxy *exp_proxy; #else /* MAX */ - void *exp_proxy[MAX_VARS]; - long exp_proxy_id; + void *exp_proxy[MAX_VARS]; + long exp_proxy_id; #endif - struct ex_ex *exp_stack[MAX_VARS]; - struct ex_ex exp_var[MAX_VARS]; - struct ex_ex exp_res[MAX_VARS]; /* the evluation result */ - t_float *exp_p_var[MAX_VARS]; - t_float *exp_p_res[MAX_VARS]; /* the previous evaluation result */ - t_float *exp_tmpres[MAX_VARS]; /* temporty result for fexpr~ */ - int exp_vsize; /* the size of the signal vector */ - int exp_nivec; /* # of vector inlets */ - float exp_f; /* control value to be transformed to signal */ + struct ex_ex *exp_stack[MAX_VARS]; + struct ex_ex exp_var[MAX_VARS]; + struct ex_ex exp_res[MAX_VARS]; /* the evluation result */ + t_float *exp_p_var[MAX_VARS]; + t_float *exp_p_res[MAX_VARS]; /* the previous evaluation result */ + t_float *exp_tmpres[MAX_VARS]; /* temporty result for fexpr~ */ + int exp_vsize; /* the size of the signal vector */ + int exp_nivec; /* # of vector inlets */ + float exp_f; /* control value to be transformed to signal */ } t_expr; typedef struct ex_funcs { - char *f_name; /* function name */ - void (*f_func)(t_expr *, long, struct ex_ex *, struct ex_ex *); - /* the real function performing the function (void, no return!!!) */ - long f_argc; /* number of arguments */ + char *f_name; /* function name */ + void (*f_func)(t_expr *, long, struct ex_ex *, struct ex_ex *); + /* the real function performing the function (void, no return!!!) */ + long f_argc; /* number of arguments */ } t_ex_func; /* function prototypes for pd-related functions called withing vexp.h */ @@ -227,12 +227,12 @@ extern int ex_getsym(char *p, t_symbol **s); extern const char *ex_symname(t_symbol *s); void ex_mkvector(t_float *fp, t_float x, int size); extern void ex_size(t_expr *expr, long int argc, struct ex_ex *argv, - struct ex_ex *optr); -extern void ex_sum(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); -extern void ex_Sum(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); -extern void ex_avg(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); -extern void ex_Avg(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); -extern void ex_store(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); + struct ex_ex *optr); +extern void ex_sum(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); +extern void ex_Sum(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); +extern void ex_avg(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); +extern void ex_Avg(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); +extern void ex_store(t_expr *expr, long int argc, struct ex_ex *argv, struct ex_ex *optr); int value_getonly(t_symbol *s, t_float *f); diff --git a/pd/extra/expr~/vexp_fun.c b/pd/extra/expr~/vexp_fun.c index 53e9092f..fba49b18 100644 --- a/pd/extra/expr~/vexp_fun.c +++ b/pd/extra/expr~/vexp_fun.c @@ -27,55 +27,55 @@ /* "expr" was written by Shahrokh Yadegari c. 1989. -msp * * Nov. 2001 --sdy - * conversion for expr~ + * conversion for expr~ * * Jan, 2002 --sdy - * added fmod() + * added fmod() * * May 2002 - * added floor and ceil for expr -- Orm Finnendahl + * added floor and ceil for expr -- Orm Finnendahl * * July 2002 --sdy - * added the following math funtions: - * cbrt - cube root - * erf - error function - * erfc - complementary error function - * expm1 - exponential minus 1, - * log1p - logarithm of 1 plus - * isinf - is the value infinite, - * finite - is the value finite - * isnan -- is the resut a nan (Not a number) - * copysign - copy sign of a number - * ldexp - multiply floating-point number by integral power of 2 - * imodf - get signed integral value from floating-point number - * modf - get signed fractional value from floating-point number - * drem - floating-point remainder function + * added the following math funtions: + * cbrt - cube root + * erf - error function + * erfc - complementary error function + * expm1 - exponential minus 1, + * log1p - logarithm of 1 plus + * isinf - is the value infinite, + * finite - is the value finite + * isnan -- is the resut a nan (Not a number) + * copysign - copy sign of a number + * ldexp - multiply floating-point number by integral power of 2 + * imodf - get signed integral value from floating-point number + * modf - get signed fractional value from floating-point number + * drem - floating-point remainder function * - * The following are done but not popular enough in math libss - * to be included yet - * hypoth - Euclidean distance function - * trunc - * round - * nearbyint - + * The following are done but not popular enough in math libss + * to be included yet + * hypoth - Euclidean distance function + * trunc + * round + * nearbyint - */ /* * vexp_func.c -- this file include all the functions for vexp. - * the first two arguments to the function are the number - * of argument and an array of arguments (argc, argv) - * the last argument is a pointer to a struct ex_ex for - * the result. Up do this point, the content of the - * struct ex_ex that these functions receive are either - * ET_INT (long), ET_FLT (float), or ET_SYM (char **, it is - * char ** and not char * since NewHandle of Mac returns - * a char ** for relocatability.) The common practice in - * these functions is that they figure out the type of their - * result according to the type of the arguments. In general - * the ET_SYM is used an ET_INT when we expect a value. - * It is the users responsibility not to pass strings to the - * function. + * the first two arguments to the function are the number + * of argument and an array of arguments (argc, argv) + * the last argument is a pointer to a struct ex_ex for + * the result. Up do this point, the content of the + * struct ex_ex that these functions receive are either + * ET_INT (long), ET_FLT (float), or ET_SYM (char **, it is + * char ** and not char * since NewHandle of Mac returns + * a char ** for relocatability.) The common practice in + * these functions is that they figure out the type of their + * result according to the type of the arguments. In general + * the ET_SYM is used an ET_INT when we expect a value. + * It is the users responsibility not to pass strings to the + * function. */ #include @@ -145,68 +145,68 @@ static void ex_hypoth(t_expr *expr, long argc, struct ex_ex *argv, struct ex_ex t_ex_func ex_funcs[] = { - {"min", ex_min, 2}, - {"max", ex_max, 2}, - {"int", ex_toint, 1}, - {"rint", ex_rint, 1}, - {"float", ex_tofloat, 1}, - {"fmod", ex_fmod, 2}, - {"floor", ex_floor, 2}, - {"ceil", ex_ceil, 2}, - {"pow", ex_pow, 2}, - {"sqrt", ex_sqrt, 1}, - {"exp", ex_exp, 1}, - {"log10", ex_log, 1}, - {"ln", ex_ln, 1}, - {"log", ex_ln, 1}, - {"sin", ex_sin, 1}, - {"cos", ex_cos, 1}, - {"tan", ex_tan, 1}, - {"asin", ex_asin, 1}, - {"acos", ex_acos, 1}, - {"atan", ex_atan, 1}, - {"atan2", ex_atan2, 2}, - {"sinh", ex_sinh, 1}, - {"cosh", ex_cosh, 1}, - {"tanh", ex_tanh, 1}, - {"fact", ex_fact, 1}, - {"random", ex_random, 2}, /* random number */ - {"abs", ex_abs, 1}, - {"if", ex_if, 3}, - {"ldexp ", ex_ldexp, 1}, - {"imodf ", ex_imodf, 1}, - {"modf", ex_modf, 1}, + {"min", ex_min, 2}, + {"max", ex_max, 2}, + {"int", ex_toint, 1}, + {"rint", ex_rint, 1}, + {"float", ex_tofloat, 1}, + {"fmod", ex_fmod, 2}, + {"floor", ex_floor, 2}, + {"ceil", ex_ceil, 2}, + {"pow", ex_pow, 2}, + {"sqrt", ex_sqrt, 1}, + {"exp", ex_exp, 1}, + {"log10", ex_log, 1}, + {"ln", ex_ln, 1}, + {"log", ex_ln, 1}, + {"sin", ex_sin, 1}, + {"cos", ex_cos, 1}, + {"tan", ex_tan, 1}, + {"asin", ex_asin, 1}, + {"acos", ex_acos, 1}, + {"atan", ex_atan, 1}, + {"atan2", ex_atan2, 2}, + {"sinh", ex_sinh, 1}, + {"cosh", ex_cosh, 1}, + {"tanh", ex_tanh, 1}, + {"fact", ex_fact, 1}, + {"random", ex_random, 2}, /* random number */ + {"abs", ex_abs, 1}, + {"if", ex_if, 3}, + {"ldexp ", ex_ldexp, 1}, + {"imodf ", ex_imodf, 1}, + {"modf", ex_modf, 1}, #ifndef NT - {"cbrt", ex_cbrt, 1}, - {"erf", ex_erf, 1}, - {"erfc", ex_erfc, 1}, - {"expm1", ex_expm1, 1}, - {"log1p", ex_log1p, 1}, - {"isinf", ex_isinf, 1}, - {"finite", ex_finite, 1}, - {"isnan", ex_isnan, 1}, - {"copysig", ex_copysign, 1}, - {"drem", ex_drem, 1}, - {"asinh", ex_asinh, 1}, - {"acosh", ex_acosh, 1}, - {"atanh", ex_atanh, 1}, /* hyperbolic atan */ + {"cbrt", ex_cbrt, 1}, + {"erf", ex_erf, 1}, + {"erfc", ex_erfc, 1}, + {"expm1", ex_expm1, 1}, + {"log1p", ex_log1p, 1}, + {"isinf", ex_isinf, 1}, + {"finite", ex_finite, 1}, + {"isnan", ex_isnan, 1}, + {"copysig", ex_copysign, 1}, + {"drem", ex_drem, 1}, + {"asinh", ex_asinh, 1}, + {"acosh", ex_acosh, 1}, + {"atanh", ex_atanh, 1}, /* hyperbolic atan */ #endif #ifdef PD - {"size", ex_size, 1}, - {"sum", ex_sum, 1}, - {"Sum", ex_Sum, 3}, - {"avg", ex_avg, 1}, - {"Avg", ex_Avg, 3}, - {"store", ex_store, 3}, + {"size", ex_size, 1}, + {"sum", ex_sum, 1}, + {"Sum", ex_Sum, 3}, + {"avg", ex_avg, 1}, + {"Avg", ex_Avg, 3}, + {"store", ex_store, 3}, #endif #ifdef notdef /* the following will be added once they are more popular in math libraries */ - {"round", ex_round, 1}, - {"trunc", ex_trunc, 1}, - {"nearbyint", ex_nearbyint, 1}, - {"hypoth", ex_hypoth, 1}, + {"round", ex_round, 1}, + {"trunc", ex_trunc, 1}, + {"nearbyint", ex_nearbyint, 1}, + {"hypoth", ex_hypoth, 1}, #endif - {0, 0, 0} + {0, 0, 0} }; /* @@ -214,189 +214,189 @@ t_ex_func ex_funcs[] = { * if fltret is set return float * otherwise return value based on regular typechecking, */ -#define FUNC_EVAL(left, right, func, leftfuncast, rightfuncast, optr, fltret) \ -switch (left->ex_type) { \ -case ET_INT: \ - switch(right->ex_type) { \ - case ET_INT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = (float)func(leftfuncast left->ex_int, \ - rightfuncast right->ex_int); \ - j = e->exp_vsize; \ - while (j--) \ - *op++ = scalar; \ - } else { \ - if (fltret) { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = (float)func(leftfuncast \ - left->ex_int, rightfuncast right->ex_int); \ - } else { \ - optr->ex_type = ET_INT; \ - optr->ex_int = (int)func(leftfuncast \ - left->ex_int, rightfuncast right->ex_int); \ - } \ - } \ - break; \ - case ET_FLT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = (float)func(leftfuncast left->ex_int, \ - rightfuncast right->ex_flt); \ - j = e->exp_vsize; \ - while (j--) \ - *op++ = scalar; \ - } else { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = (float)func(leftfuncast left->ex_int, \ - rightfuncast right->ex_flt); \ - } \ - break; \ - case ET_VEC: \ - case ET_VI: \ - if (optr->ex_type != ET_VEC) { \ - if (optr->ex_type == ET_VI) { \ - post("expr~: Int. error %d", __LINE__); \ - abort(); \ - } \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float)*e->exp_vsize); \ - } \ - scalar = left->ex_int; \ - rp = right->ex_vec; \ - op = optr->ex_vec; \ - j = e->exp_vsize; \ - while (j--) { \ - *op++ = (float)func(leftfuncast scalar, \ - rightfuncast *rp); \ - rp++; \ - } \ - break; \ - case ET_SYM: \ - default: \ - post_error((fts_object_t *) e, \ - "expr: FUNC_EVAL(%d): bad right type %ld\n", \ - __LINE__, right->ex_type);\ - } \ - break; \ -case ET_FLT: \ - switch(right->ex_type) { \ - case ET_INT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = (float)func(leftfuncast left->ex_flt, \ - rightfuncast right->ex_int); \ - j = e->exp_vsize; \ - while (j--) \ - *op++ = scalar; \ - } else { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = (float)func(leftfuncast left->ex_flt, \ - rightfuncast right->ex_int); \ - } \ - break; \ - case ET_FLT: \ - if (optr->ex_type == ET_VEC) { \ - op = optr->ex_vec; \ - scalar = (float)func(leftfuncast left->ex_flt, \ - rightfuncast right->ex_flt); \ - j = e->exp_vsize; \ - while (j--) \ - *op++ = scalar; \ - } else { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = (float)func(leftfuncast left->ex_flt, \ - rightfuncast right->ex_flt); \ - } \ - break; \ - case ET_VEC: \ - case ET_VI: \ - if (optr->ex_type != ET_VEC) { \ - if (optr->ex_type == ET_VI) { \ - post("expr~: Int. error %d", __LINE__); \ - abort(); \ - } \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float) * e->exp_vsize);\ - } \ - scalar = left->ex_flt; \ - rp = right->ex_vec; \ - op = optr->ex_vec; \ - j = e->exp_vsize; \ - while (j--) { \ - *op++ = (float)func(leftfuncast scalar, \ - rightfuncast *rp); \ - rp++; \ - } \ - break; \ - case ET_SYM: \ - default: \ - post_error((fts_object_t *) e, \ - "expr: FUNC_EVAL(%d): bad right type %ld\n", \ - __LINE__, right->ex_type);\ - } \ - break; \ -case ET_VEC: \ -case ET_VI: \ - if (optr->ex_type != ET_VEC) { \ - if (optr->ex_type == ET_VI) { \ - post("expr~: Int. error %d", __LINE__); \ - abort(); \ - } \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float) * e->exp_vsize); \ - } \ - op = optr->ex_vec; \ - lp = left->ex_vec; \ - switch(right->ex_type) { \ - case ET_INT: \ - scalar = right->ex_int; \ - j = e->exp_vsize; \ - while (j--) { \ - *op++ = (float)func(leftfuncast *lp, \ - rightfuncast scalar); \ - lp++; \ - } \ - break; \ - case ET_FLT: \ - scalar = right->ex_flt; \ - j = e->exp_vsize; \ - while (j--) { \ - *op++ = (float)func(leftfuncast *lp, \ - rightfuncast scalar); \ - lp++; \ - } \ - break; \ - case ET_VEC: \ - case ET_VI: \ - rp = right->ex_vec; \ - j = e->exp_vsize; \ - while (j--) { \ - /* \ - * on a RISC processor one could copy \ - * 8 times in each round to get a considerable \ - * improvement \ - */ \ - *op++ = (float)func(leftfuncast *lp, \ - rightfuncast *rp); \ - rp++; lp++; \ - } \ - break; \ - case ET_SYM: \ - default: \ - post_error((fts_object_t *) e, \ - "expr: FUNC_EVAL(%d): bad right type %ld\n", \ - __LINE__, right->ex_type);\ - } \ - break; \ -case ET_SYM: \ -default: \ - post_error((fts_object_t *) e, \ - "expr: FUNC_EVAL(%d): bad left type %ld\n", \ - __LINE__, left->ex_type); \ +#define FUNC_EVAL(left, right, func, leftfuncast, rightfuncast, optr, fltret) \ +switch (left->ex_type) { \ +case ET_INT: \ + switch(right->ex_type) { \ + case ET_INT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = (float)func(leftfuncast left->ex_int, \ + rightfuncast right->ex_int); \ + j = e->exp_vsize; \ + while (j--) \ + *op++ = scalar; \ + } else { \ + if (fltret) { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = (float)func(leftfuncast \ + left->ex_int, rightfuncast right->ex_int); \ + } else { \ + optr->ex_type = ET_INT; \ + optr->ex_int = (int)func(leftfuncast \ + left->ex_int, rightfuncast right->ex_int); \ + } \ + } \ + break; \ + case ET_FLT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = (float)func(leftfuncast left->ex_int, \ + rightfuncast right->ex_flt); \ + j = e->exp_vsize; \ + while (j--) \ + *op++ = scalar; \ + } else { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = (float)func(leftfuncast left->ex_int, \ + rightfuncast right->ex_flt); \ + } \ + break; \ + case ET_VEC: \ + case ET_VI: \ + if (optr->ex_type != ET_VEC) { \ + if (optr->ex_type == ET_VI) { \ + post("expr~: Int. error %d", __LINE__); \ + abort(); \ + } \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float)*e->exp_vsize); \ + } \ + scalar = left->ex_int; \ + rp = right->ex_vec; \ + op = optr->ex_vec; \ + j = e->exp_vsize; \ + while (j--) { \ + *op++ = (float)func(leftfuncast scalar, \ + rightfuncast *rp); \ + rp++; \ + } \ + break; \ + case ET_SYM: \ + default: \ + post_error((fts_object_t *) e, \ + "expr: FUNC_EVAL(%d): bad right type %ld\n", \ + __LINE__, right->ex_type);\ + } \ + break; \ +case ET_FLT: \ + switch(right->ex_type) { \ + case ET_INT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = (float)func(leftfuncast left->ex_flt, \ + rightfuncast right->ex_int); \ + j = e->exp_vsize; \ + while (j--) \ + *op++ = scalar; \ + } else { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = (float)func(leftfuncast left->ex_flt, \ + rightfuncast right->ex_int); \ + } \ + break; \ + case ET_FLT: \ + if (optr->ex_type == ET_VEC) { \ + op = optr->ex_vec; \ + scalar = (float)func(leftfuncast left->ex_flt, \ + rightfuncast right->ex_flt); \ + j = e->exp_vsize; \ + while (j--) \ + *op++ = scalar; \ + } else { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = (float)func(leftfuncast left->ex_flt, \ + rightfuncast right->ex_flt); \ + } \ + break; \ + case ET_VEC: \ + case ET_VI: \ + if (optr->ex_type != ET_VEC) { \ + if (optr->ex_type == ET_VI) { \ + post("expr~: Int. error %d", __LINE__); \ + abort(); \ + } \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float) * e->exp_vsize);\ + } \ + scalar = left->ex_flt; \ + rp = right->ex_vec; \ + op = optr->ex_vec; \ + j = e->exp_vsize; \ + while (j--) { \ + *op++ = (float)func(leftfuncast scalar, \ + rightfuncast *rp); \ + rp++; \ + } \ + break; \ + case ET_SYM: \ + default: \ + post_error((fts_object_t *) e, \ + "expr: FUNC_EVAL(%d): bad right type %ld\n", \ + __LINE__, right->ex_type);\ + } \ + break; \ +case ET_VEC: \ +case ET_VI: \ + if (optr->ex_type != ET_VEC) { \ + if (optr->ex_type == ET_VI) { \ + post("expr~: Int. error %d", __LINE__); \ + abort(); \ + } \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float) * e->exp_vsize); \ + } \ + op = optr->ex_vec; \ + lp = left->ex_vec; \ + switch(right->ex_type) { \ + case ET_INT: \ + scalar = right->ex_int; \ + j = e->exp_vsize; \ + while (j--) { \ + *op++ = (float)func(leftfuncast *lp, \ + rightfuncast scalar); \ + lp++; \ + } \ + break; \ + case ET_FLT: \ + scalar = right->ex_flt; \ + j = e->exp_vsize; \ + while (j--) { \ + *op++ = (float)func(leftfuncast *lp, \ + rightfuncast scalar); \ + lp++; \ + } \ + break; \ + case ET_VEC: \ + case ET_VI: \ + rp = right->ex_vec; \ + j = e->exp_vsize; \ + while (j--) { \ + /* \ + * on a RISC processor one could copy \ + * 8 times in each round to get a considerable \ + * improvement \ + */ \ + *op++ = (float)func(leftfuncast *lp, \ + rightfuncast *rp); \ + rp++; lp++; \ + } \ + break; \ + case ET_SYM: \ + default: \ + post_error((fts_object_t *) e, \ + "expr: FUNC_EVAL(%d): bad right type %ld\n", \ + __LINE__, right->ex_type);\ + } \ + break; \ +case ET_SYM: \ +default: \ + post_error((fts_object_t *) e, \ + "expr: FUNC_EVAL(%d): bad left type %ld\n", \ + __LINE__, left->ex_type); \ } /* @@ -404,124 +404,124 @@ default: \ * if fltret is set return float * otherwise return value based on regular typechecking, */ -#define FUNC_EVAL_UNARY(left, func, leftcast, optr, fltret) \ -switch(left->ex_type) { \ -case ET_INT: \ - if (optr->ex_type == ET_VEC) { \ - ex_mkvector(optr->ex_vec, \ - (float)(func (leftcast left->ex_int)), e->exp_vsize);\ - break; \ - } \ - if (fltret) { \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = (float) func(leftcast left->ex_int); \ - break; \ - } \ - optr->ex_type = ET_INT; \ - optr->ex_int = (int) func(leftcast left->ex_int); \ - break; \ -case ET_FLT: \ - if (optr->ex_type == ET_VEC) { \ - ex_mkvector(optr->ex_vec, \ - (float)(func (leftcast left->ex_flt)), e->exp_vsize);\ - break; \ - } \ - optr->ex_type = ET_FLT; \ - optr->ex_flt = (float) func(leftcast left->ex_flt); \ - break; \ -case ET_VI: \ -case ET_VEC: \ - if (optr->ex_type != ET_VEC) { \ - optr->ex_type = ET_VEC; \ - optr->ex_vec = (t_float *) \ - fts_malloc(sizeof (t_float)*e->exp_vsize); \ - } \ - op = optr->ex_vec; \ - lp = left->ex_vec; \ - j = e->exp_vsize; \ - while (j--) \ - *op++ = (float)(func (leftcast *lp++)); \ - break; \ -default: \ - post_error((fts_object_t *) e, \ - "expr: FUNV_EVAL_UNARY(%d): bad left type %ld\n",\ - __LINE__, left->ex_type); \ +#define FUNC_EVAL_UNARY(left, func, leftcast, optr, fltret) \ +switch(left->ex_type) { \ +case ET_INT: \ + if (optr->ex_type == ET_VEC) { \ + ex_mkvector(optr->ex_vec, \ + (float)(func (leftcast left->ex_int)), e->exp_vsize);\ + break; \ + } \ + if (fltret) { \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = (float) func(leftcast left->ex_int); \ + break; \ + } \ + optr->ex_type = ET_INT; \ + optr->ex_int = (int) func(leftcast left->ex_int); \ + break; \ +case ET_FLT: \ + if (optr->ex_type == ET_VEC) { \ + ex_mkvector(optr->ex_vec, \ + (float)(func (leftcast left->ex_flt)), e->exp_vsize);\ + break; \ + } \ + optr->ex_type = ET_FLT; \ + optr->ex_flt = (float) func(leftcast left->ex_flt); \ + break; \ +case ET_VI: \ +case ET_VEC: \ + if (optr->ex_type != ET_VEC) { \ + optr->ex_type = ET_VEC; \ + optr->ex_vec = (t_float *) \ + fts_malloc(sizeof (t_float)*e->exp_vsize); \ + } \ + op = optr->ex_vec; \ + lp = left->ex_vec; \ + j = e->exp_vsize; \ + while (j--) \ + *op++ = (float)(func (leftcast *lp++)); \ + break; \ +default: \ + post_error((fts_object_t *) e, \ + "expr: FUNV_EVAL_UNARY(%d): bad left type %ld\n",\ + __LINE__, left->ex_type); \ } #undef min #undef max -#define min(x,y) (x > y ? y : x) -#define max(x,y) (x > y ? x : y) +#define min(x,y) (x > y ? y : x) +#define max(x,y) (x > y ? x : y) -#define FUNC_DEF(ex_func, func, castleft, castright, fltret); \ -static void \ +#define FUNC_DEF(ex_func, func, castleft, castright, fltret); \ +static void \ ex_func(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr)\ -{ \ - struct ex_ex *left, *right; \ - float *op; /* output pointer */ \ - float *lp, *rp; /* left and right vector pointers */ \ - float scalar; \ - int j; \ - \ - left = argv++; \ - right = argv; \ - FUNC_EVAL(left, right, func, castleft, castright, optr, fltret); \ +{ \ + struct ex_ex *left, *right; \ + float *op; /* output pointer */ \ + float *lp, *rp; /* left and right vector pointers */ \ + float scalar; \ + int j; \ + \ + left = argv++; \ + right = argv; \ + FUNC_EVAL(left, right, func, castleft, castright, optr, fltret); \ } -#define FUNC_DEF_UNARY(ex_func, func, cast, fltret); \ -static void \ +#define FUNC_DEF_UNARY(ex_func, func, cast, fltret); \ +static void \ ex_func(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr)\ -{ \ - struct ex_ex *left; \ - float *op; /* output pointer */ \ - float *lp, *rp; /* left and right vector pointers */ \ - float scalar; \ - int j; \ - \ - left = argv++; \ - \ - FUNC_EVAL_UNARY(left, func, cast, optr, fltret); \ +{ \ + struct ex_ex *left; \ + float *op; /* output pointer */ \ + float *lp, *rp; /* left and right vector pointers */ \ + float scalar; \ + int j; \ + \ + left = argv++; \ + \ + FUNC_EVAL_UNARY(left, func, cast, optr, fltret); \ } /* * ex_min -- if any of the arguments are or the output are vectors, a vector - * of floats is generated otherwise the type of the result is the - * type of the smaller value + * of floats is generated otherwise the type of the result is the + * type of the smaller value */ static void ex_min(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left, *right; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left, *right; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; - right = argv; + left = argv++; + right = argv; - FUNC_EVAL(left, right, min, (double), (double), optr, 0); + FUNC_EVAL(left, right, min, (double), (double), optr, 0); } /* * ex_max -- if any of the arguments are or the output are vectors, a vector - * of floats is generated otherwise the type of the result is the - * type of the larger value + * of floats is generated otherwise the type of the result is the + * type of the larger value */ static void ex_max(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left, *right; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left, *right; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; - right = argv; + left = argv++; + right = argv; - FUNC_EVAL(left, right, max, (double), (double), optr, 0); + FUNC_EVAL(left, right, max, (double), (double), optr, 0); } /* @@ -530,17 +530,17 @@ ex_max(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_toint(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; -#define toint(x) ((int)(x)) - FUNC_EVAL_UNARY(left, toint, (int), optr, 0); - } +#define toint(x) ((int)(x)) + FUNC_EVAL_UNARY(left, toint, (int), optr, 0); + } #ifdef NT /* No rint in NT land ??? */ @@ -549,27 +549,27 @@ double rint(double x); double rint(double x) { - return (floor(x + 0.5)); + return (floor(x + 0.5)); } #endif /* * ex_rint -- rint() round to the nearest int according to the common - * rounding mechanism + * rounding mechanism */ static void ex_rint(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, rint, (double), optr, 1); + FUNC_EVAL_UNARY(left, rint, (double), optr, 1); } /* @@ -578,16 +578,16 @@ ex_rint(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_tofloat(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; -#define tofloat(x) ((float)(x)) - FUNC_EVAL_UNARY(left, tofloat, (int), optr, 1); +#define tofloat(x) ((float)(x)) + FUNC_EVAL_UNARY(left, tofloat, (int), optr, 1); } @@ -597,15 +597,15 @@ ex_tofloat(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_pow(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left, *right; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; - - left = argv++; - right = argv; - FUNC_EVAL(left, right, pow, (double), (double), optr, 1); + struct ex_ex *left, *right; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; + + left = argv++; + right = argv; + FUNC_EVAL(left, right, pow, (double), (double), optr, 1); } /* @@ -614,15 +614,15 @@ ex_pow(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_sqrt(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, sqrt, (double), optr, 1); + FUNC_EVAL_UNARY(left, sqrt, (double), optr, 1); } /* @@ -631,15 +631,15 @@ ex_sqrt(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_exp(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, exp, (double), optr, 1); + FUNC_EVAL_UNARY(left, exp, (double), optr, 1); } /* @@ -648,15 +648,15 @@ ex_exp(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_log(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, log10, (double), optr, 1); + FUNC_EVAL_UNARY(left, log10, (double), optr, 1); } /* @@ -665,101 +665,101 @@ ex_log(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_ln(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, log, (double), optr, 1); + FUNC_EVAL_UNARY(left, log, (double), optr, 1); } static void ex_sin(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, sin, (double), optr, 1); + FUNC_EVAL_UNARY(left, sin, (double), optr, 1); } static void ex_cos(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, cos, (double), optr, 1); + FUNC_EVAL_UNARY(left, cos, (double), optr, 1); } static void ex_tan(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, tan, (double), optr, 1); + FUNC_EVAL_UNARY(left, tan, (double), optr, 1); } static void ex_asin(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, asin, (double), optr, 1); + FUNC_EVAL_UNARY(left, asin, (double), optr, 1); } static void ex_acos(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, acos, (double), optr, 1); + FUNC_EVAL_UNARY(left, acos, (double), optr, 1); } static void ex_atan(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, atan, (double), optr, 1); + FUNC_EVAL_UNARY(left, atan, (double), optr, 1); } /* @@ -768,15 +768,15 @@ ex_atan(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_atan2(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left, *right; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; - - left = argv++; - right = argv; - FUNC_EVAL(left, right, atan2, (double), (double), optr, 1); + struct ex_ex *left, *right; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; + + left = argv++; + right = argv; + FUNC_EVAL(left, right, atan2, (double), (double), optr, 1); } /* @@ -785,15 +785,15 @@ ex_atan2(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_fmod(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left, *right; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; - - left = argv++; - right = argv; - FUNC_EVAL(left, right, fmod, (double), (double), optr, 1); + struct ex_ex *left, *right; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; + + left = argv++; + right = argv; + FUNC_EVAL(left, right, fmod, (double), (double), optr, 1); } @@ -803,14 +803,14 @@ ex_fmod(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_floor(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; - - left = argv++; - FUNC_EVAL_UNARY(left, floor, (double), optr, 1); + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; + + left = argv++; + FUNC_EVAL_UNARY(left, floor, (double), optr, 1); } @@ -820,57 +820,57 @@ ex_floor(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_ceil(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; - - left = argv++; - FUNC_EVAL_UNARY(left, ceil, (double), optr, 1); + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; + + left = argv++; + FUNC_EVAL_UNARY(left, ceil, (double), optr, 1); } static void ex_sinh(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, sinh, (double), optr, 1); + FUNC_EVAL_UNARY(left, sinh, (double), optr, 1); } static void ex_cosh(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, cosh, (double), optr, 1); + FUNC_EVAL_UNARY(left, cosh, (double), optr, 1); } static void ex_tanh(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, tanh, (double), optr, 1); + FUNC_EVAL_UNARY(left, tanh, (double), optr, 1); } @@ -878,81 +878,81 @@ ex_tanh(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_asinh(t_expr *e, long argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, asinh, (double), optr, 1); + FUNC_EVAL_UNARY(left, asinh, (double), optr, 1); } static void ex_acosh(t_expr *e, long argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, acosh, (double), optr, 1); + FUNC_EVAL_UNARY(left, acosh, (double), optr, 1); } static void ex_atanh(t_expr *e, long argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, atanh, (double), optr, 1); + FUNC_EVAL_UNARY(left, atanh, (double), optr, 1); } #endif static int ex_dofact(int i) { - int ret = 0; + int ret = 0; - if (i) - ret = 1; - else - return (0); + if (i) + ret = 1; + else + return (0); - do { - ret *= i; - } while (--i); + do { + ret *= i; + } while (--i); - return(ret); + return(ret); } static void ex_fact(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, ex_dofact, (int), optr, 0); + FUNC_EVAL_UNARY(left, ex_dofact, (int), optr, 0); } static int ex_dorandom(int i1, int i2) { - return(i1 + (((i2 - i1) * (rand() & 0x7fffL)) >> 15)); + return(i1 + (((i2 - i1) * (rand() & 0x7fffL)) >> 15)); } /* * ex_random -- return a random number @@ -960,30 +960,30 @@ ex_dorandom(int i1, int i2) static void ex_random(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left, *right; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; - - left = argv++; - right = argv; - FUNC_EVAL(left, right, ex_dorandom, (int), (int), optr, 0); + struct ex_ex *left, *right; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; + + left = argv++; + right = argv; + FUNC_EVAL(left, right, ex_dorandom, (int), (int), optr, 0); } static void ex_abs(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float scalar; - int j; + struct ex_ex *left; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float scalar; + int j; - left = argv++; + left = argv++; - FUNC_EVAL_UNARY(left, fabs, (double), optr, 0); + FUNC_EVAL_UNARY(left, fabs, (double), optr, 0); } /* @@ -992,231 +992,231 @@ ex_abs(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) static void ex_if(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - struct ex_ex *left, *right, *cond, *res; - float *op; /* output pointer */ - float *lp, *rp; /* left and right vector pointers */ - float *cp; /* condition pointer */ - float leftvalue, rightvalue; - int j; - - cond = argv++; - left = argv++; - right = argv; - - switch (cond->ex_type) { - case ET_VEC: - case ET_VI: - if (optr->ex_type != ET_VEC) { - if (optr->ex_type == ET_VI) { - /* SDY remove this test */ - post("expr~: Int. error %d", __LINE__); - return; - } - optr->ex_type = ET_VEC; - optr->ex_vec = (t_float *) - fts_malloc(sizeof (t_float) * e->exp_vsize); - } - op = optr->ex_vec; - j = e->exp_vsize; - cp = cond->ex_vec; - switch (left->ex_type) { - case ET_INT: - leftvalue = left->ex_int; - switch (right->ex_type) { - case ET_INT: - rightvalue = right->ex_int; - while (j--) { - if (*cp++) - *op++ = leftvalue; - else - *op++ = rightvalue; - } - return; - case ET_FLT: - rightvalue = right->ex_flt; - while (j--) { - if (*cp++) - *op++ = leftvalue; - else - *op++ = rightvalue; - } - return; - case ET_VEC: - case ET_VI: - rp = right->ex_vec; - while (j--) { - if (*cp++) - *op++ = leftvalue; - else - *op++ = *rp; - rp++; - } - return; - case ET_SYM: - default: - post_error((fts_object_t *) e, - "expr: FUNC_EVAL(%d): bad right type %ld\n", - __LINE__, right->ex_type); - return; - } - case ET_FLT: - leftvalue = left->ex_flt; - switch (right->ex_type) { - case ET_INT: - rightvalue = right->ex_int; - while (j--) { - if (*cp++) - *op++ = leftvalue; - else - *op++ = rightvalue; - } - return; - case ET_FLT: - rightvalue = right->ex_flt; - while (j--) { - if (*cp++) - *op++ = leftvalue; - else - *op++ = rightvalue; - } - return; - case ET_VEC: - case ET_VI: - rp = right->ex_vec; - while (j--) { - if (*cp++) - *op++ = leftvalue; - else - *op++ = *rp; - rp++; - } - return; - case ET_SYM: - default: - post_error((fts_object_t *) e, - "expr: FUNC_EVAL(%d): bad right type %ld\n", - __LINE__, right->ex_type); - return; - } - case ET_VEC: - case ET_VI: - lp = left->ex_vec; - switch (right->ex_type) { - case ET_INT: - rightvalue = right->ex_int; - while (j--) { - if (*cp++) - *op++ = *lp; - else - *op++ = rightvalue; - lp++; - } - return; - case ET_FLT: - rightvalue = right->ex_flt; - while (j--) { - if (*cp++) - *op++ = *lp; - else - *op++ = rightvalue; - lp++; - } - return; - case ET_VEC: - case ET_VI: - rp = right->ex_vec; - while (j--) { - if (*cp++) - *op++ = *lp; - else - *op++ = *rp; - lp++; rp++; - } - return; - case ET_SYM: - default: - post_error((fts_object_t *) e, - "expr: FUNC_EVAL(%d): bad right type %ld\n", - __LINE__, right->ex_type); - return; - } - case ET_SYM: - default: - post_error((fts_object_t *) e, - "expr: FUNC_EVAL(%d): bad left type %ld\n", - __LINE__, left->ex_type); - return; - } - case ET_INT: - if (cond->ex_int) - res = left; - else - res = right; - break; - case ET_FLT: - if (cond->ex_flt) - res = left; - else - res = right; - break; - case ET_SYM: - default: - post_error((fts_object_t *) e, - "expr: FUNC_EVAL(%d): bad condition type %ld\n", - __LINE__, cond->ex_type); - return; - } - switch(res->ex_type) { - case ET_INT: - if (optr->ex_type == ET_VEC) { - ex_mkvector(optr->ex_vec, (float)res->ex_int, - e->exp_vsize); - return; - } - *optr = *res; - return; - case ET_FLT: - if (optr->ex_type == ET_VEC) { - ex_mkvector(optr->ex_vec, (float)res->ex_flt, - e->exp_vsize); - return; - } - *optr = *res; - return; - case ET_VEC: - case ET_VI: - if (optr->ex_type != ET_VEC) { - if (optr->ex_type == ET_VI) { - /* SDY remove this test */ - post("expr~: Int. error %d", __LINE__); - return; - } - optr->ex_type = ET_VEC; - optr->ex_vec = (t_float *) - fts_malloc(sizeof (t_float) * e->exp_vsize); - } - memcpy(optr->ex_vec, res->ex_vec, e->exp_vsize*sizeof(t_float)); - return; - case ET_SYM: - default: - post_error((fts_object_t *) e, - "expr: FUNC_EVAL(%d): bad res type %ld\n", - __LINE__, res->ex_type); - return; - } - + struct ex_ex *left, *right, *cond, *res; + float *op; /* output pointer */ + float *lp, *rp; /* left and right vector pointers */ + float *cp; /* condition pointer */ + float leftvalue, rightvalue; + int j; + + cond = argv++; + left = argv++; + right = argv; + + switch (cond->ex_type) { + case ET_VEC: + case ET_VI: + if (optr->ex_type != ET_VEC) { + if (optr->ex_type == ET_VI) { + /* SDY remove this test */ + post("expr~: Int. error %d", __LINE__); + return; + } + optr->ex_type = ET_VEC; + optr->ex_vec = (t_float *) + fts_malloc(sizeof (t_float) * e->exp_vsize); + } + op = optr->ex_vec; + j = e->exp_vsize; + cp = cond->ex_vec; + switch (left->ex_type) { + case ET_INT: + leftvalue = left->ex_int; + switch (right->ex_type) { + case ET_INT: + rightvalue = right->ex_int; + while (j--) { + if (*cp++) + *op++ = leftvalue; + else + *op++ = rightvalue; + } + return; + case ET_FLT: + rightvalue = right->ex_flt; + while (j--) { + if (*cp++) + *op++ = leftvalue; + else + *op++ = rightvalue; + } + return; + case ET_VEC: + case ET_VI: + rp = right->ex_vec; + while (j--) { + if (*cp++) + *op++ = leftvalue; + else + *op++ = *rp; + rp++; + } + return; + case ET_SYM: + default: + post_error((fts_object_t *) e, + "expr: FUNC_EVAL(%d): bad right type %ld\n", + __LINE__, right->ex_type); + return; + } + case ET_FLT: + leftvalue = left->ex_flt; + switch (right->ex_type) { + case ET_INT: + rightvalue = right->ex_int; + while (j--) { + if (*cp++) + *op++ = leftvalue; + else + *op++ = rightvalue; + } + return; + case ET_FLT: + rightvalue = right->ex_flt; + while (j--) { + if (*cp++) + *op++ = leftvalue; + else + *op++ = rightvalue; + } + return; + case ET_VEC: + case ET_VI: + rp = right->ex_vec; + while (j--) { + if (*cp++) + *op++ = leftvalue; + else + *op++ = *rp; + rp++; + } + return; + case ET_SYM: + default: + post_error((fts_object_t *) e, + "expr: FUNC_EVAL(%d): bad right type %ld\n", + __LINE__, right->ex_type); + return; + } + case ET_VEC: + case ET_VI: + lp = left->ex_vec; + switch (right->ex_type) { + case ET_INT: + rightvalue = right->ex_int; + while (j--) { + if (*cp++) + *op++ = *lp; + else + *op++ = rightvalue; + lp++; + } + return; + case ET_FLT: + rightvalue = right->ex_flt; + while (j--) { + if (*cp++) + *op++ = *lp; + else + *op++ = rightvalue; + lp++; + } + return; + case ET_VEC: + case ET_VI: + rp = right->ex_vec; + while (j--) { + if (*cp++) + *op++ = *lp; + else + *op++ = *rp; + lp++; rp++; + } + return; + case ET_SYM: + default: + post_error((fts_object_t *) e, + "expr: FUNC_EVAL(%d): bad right type %ld\n", + __LINE__, right->ex_type); + return; + } + case ET_SYM: + default: + post_error((fts_object_t *) e, + "expr: FUNC_EVAL(%d): bad left type %ld\n", + __LINE__, left->ex_type); + return; + } + case ET_INT: + if (cond->ex_int) + res = left; + else + res = right; + break; + case ET_FLT: + if (cond->ex_flt) + res = left; + else + res = right; + break; + case ET_SYM: + default: + post_error((fts_object_t *) e, + "expr: FUNC_EVAL(%d): bad condition type %ld\n", + __LINE__, cond->ex_type); + return; + } + switch(res->ex_type) { + case ET_INT: + if (optr->ex_type == ET_VEC) { + ex_mkvector(optr->ex_vec, (float)res->ex_int, + e->exp_vsize); + return; + } + *optr = *res; + return; + case ET_FLT: + if (optr->ex_type == ET_VEC) { + ex_mkvector(optr->ex_vec, (float)res->ex_flt, + e->exp_vsize); + return; + } + *optr = *res; + return; + case ET_VEC: + case ET_VI: + if (optr->ex_type != ET_VEC) { + if (optr->ex_type == ET_VI) { + /* SDY remove this test */ + post("expr~: Int. error %d", __LINE__); + return; + } + optr->ex_type = ET_VEC; + optr->ex_vec = (t_float *) + fts_malloc(sizeof (t_float) * e->exp_vsize); + } + memcpy(optr->ex_vec, res->ex_vec, e->exp_vsize*sizeof(t_float)); + return; + case ET_SYM: + default: + post_error((fts_object_t *) e, + "expr: FUNC_EVAL(%d): bad res type %ld\n", + __LINE__, res->ex_type); + return; + } + } /* - * ex_imodf - extract signed integral value from floating-point number + * ex_imodf - extract signed integral value from floating-point number */ static double imodf(double x) { - double xx; + double xx; - modf(x, &xx); - return (xx); + modf(x, &xx); + return (xx); } FUNC_DEF_UNARY(ex_imodf, imodf, (double), 1); @@ -1228,9 +1228,9 @@ FUNC_DEF_UNARY(ex_imodf, imodf, (double), 1); static double fracmodf(double x) { - double xx; + double xx; - return(modf(x, &xx)); + return(modf(x, &xx)); } FUNC_DEF_UNARY(ex_modf, fracmodf, (double), 1); diff --git a/pd/extra/expr~/vexp_if.c b/pd/extra/expr~/vexp_if.c index b183b620..08dc55c3 100644 --- a/pd/extra/expr~/vexp_if.c +++ b/pd/extra/expr~/vexp_if.c @@ -30,8 +30,8 @@ /* * Feb 2002 - added access to variables * multiple expression support - * new short hand forms for fexpr~ - * now $y or $y1 = $y1[-1] and $y2 = $y2[-1] + * new short hand forms for fexpr~ + * now $y or $y1 = $y1[-1] and $y2 = $y2[-1] * --sdy */ @@ -44,7 +44,7 @@ static char *exp_version = "0.4"; extern struct ex_ex *ex_eval(struct expr *expr, struct ex_ex *eptr, - struct ex_ex *optr, int n); + struct ex_ex *optr, int n); #ifdef PD static t_class *expr_class; @@ -68,51 +68,51 @@ t_int *expr_perform(t_int *w); static void expr_list(t_expr *x, t_symbol *s, int argc, const fts_atom_t *argv) { - int i; - - if (argc > MAX_VARS) argc = MAX_VARS; - - for (i = 0; i < argc; i++) - { - if (argv[i].a_type == A_FLOAT) - { - if (x->exp_var[i].ex_type == ET_FI) - x->exp_var[i].ex_flt = argv[i].a_w.w_float; - else if (x->exp_var[i].ex_type == ET_II) - x->exp_var[i].ex_int = argv[i].a_w.w_float; - else if (x->exp_var[i].ex_type) - pd_error(x, "expr: type mismatch"); - } - else if (argv[i].a_type == A_SYMBOL) - { - if (x->exp_var[i].ex_type == ET_SI) - x->exp_var[i].ex_ptr = (char *)argv[i].a_w.w_symbol; - else if (x->exp_var[i].ex_type) - pd_error(x, "expr: type mismatch"); - } - } - expr_bang(x); + int i; + + if (argc > MAX_VARS) argc = MAX_VARS; + + for (i = 0; i < argc; i++) + { + if (argv[i].a_type == A_FLOAT) + { + if (x->exp_var[i].ex_type == ET_FI) + x->exp_var[i].ex_flt = argv[i].a_w.w_float; + else if (x->exp_var[i].ex_type == ET_II) + x->exp_var[i].ex_int = argv[i].a_w.w_float; + else if (x->exp_var[i].ex_type) + pd_error(x, "expr: type mismatch"); + } + else if (argv[i].a_type == A_SYMBOL) + { + if (x->exp_var[i].ex_type == ET_SI) + x->exp_var[i].ex_ptr = (char *)argv[i].a_w.w_symbol; + else if (x->exp_var[i].ex_type) + pd_error(x, "expr: type mismatch"); + } + } + expr_bang(x); } static void expr_flt(t_expr *x, t_float f, int in) { - if (in > MAX_VARS) - return; + if (in > MAX_VARS) + return; - if (x->exp_var[in].ex_type == ET_FI) - x->exp_var[in].ex_flt = f; - else if (x->exp_var[in].ex_type == ET_II) - x->exp_var[in].ex_int = f; + if (x->exp_var[in].ex_type == ET_FI) + x->exp_var[in].ex_flt = f; + else if (x->exp_var[in].ex_type == ET_II) + x->exp_var[in].ex_int = f; } static t_class *exprproxy_class; typedef struct _exprproxy { - t_pd p_pd; - int p_index; - t_expr *p_owner; - struct _exprproxy *p_next; + t_pd p_pd; + int p_index; + t_expr *p_owner; + struct _exprproxy *p_next; } t_exprproxy; t_exprproxy *exprproxy_new(t_expr *owner, int indx); @@ -121,63 +121,63 @@ void exprproxy_float(t_exprproxy *p, t_floatarg f); t_exprproxy * exprproxy_new(t_expr *owner, int indx) { - t_exprproxy *x = (t_exprproxy *)pd_new(exprproxy_class); - x->p_owner = owner; - x->p_index = indx; - x->p_next = owner->exp_proxy; - owner->exp_proxy = x; - return (x); + t_exprproxy *x = (t_exprproxy *)pd_new(exprproxy_class); + x->p_owner = owner; + x->p_index = indx; + x->p_next = owner->exp_proxy; + owner->exp_proxy = x; + return (x); } void exprproxy_float(t_exprproxy *p, t_floatarg f) { - t_expr *x = p->p_owner; - int in = p->p_index; + t_expr *x = p->p_owner; + int in = p->p_index; - if (in > MAX_VARS) - return; + if (in > MAX_VARS) + return; - if (x->exp_var[in].ex_type == ET_FI) - x->exp_var[in].ex_flt = f; - else if (x->exp_var[in].ex_type == ET_II) - x->exp_var[in].ex_int = f; + if (x->exp_var[in].ex_type == ET_FI) + x->exp_var[in].ex_flt = f; + else if (x->exp_var[in].ex_type == ET_II) + x->exp_var[in].ex_int = f; } /* method definitions */ static void expr_ff(t_expr *x) { - t_exprproxy *y; - int i; - - y = x->exp_proxy; - while (y) - { - x->exp_proxy = y->p_next; + t_exprproxy *y; + int i; + + y = x->exp_proxy; + while (y) + { + x->exp_proxy = y->p_next; #ifdef PD - pd_free(&y->p_pd); + pd_free(&y->p_pd); #else /*MSP */ - /* SDY find out what needs to be called for MSP */ + /* SDY find out what needs to be called for MSP */ #endif - y = x->exp_proxy; - } - for (i = 0 ; i < x->exp_nexpr; i++); - if (x->exp_stack[i]) - fts_free(x->exp_stack[i]); + y = x->exp_proxy; + } + for (i = 0 ; i < x->exp_nexpr; i++); + if (x->exp_stack[i]) + fts_free(x->exp_stack[i]); /* * SDY free all the allocated buffers here for expr~ and fexpr~ * check to see if there are others */ - for (i = 0; i < MAX_VARS; i++) { - if (x->exp_p_var[i]) - fts_free(x->exp_p_var[i]); - if (x->exp_p_res[i]) - fts_free(x->exp_p_res[i]); - if (x->exp_tmpres[i]) - fts_free(x->exp_tmpres[i]); - } + for (i = 0; i < MAX_VARS; i++) { + if (x->exp_p_var[i]) + fts_free(x->exp_p_var[i]); + if (x->exp_p_res[i]) + fts_free(x->exp_p_res[i]); + if (x->exp_tmpres[i]) + fts_free(x->exp_tmpres[i]); + } } @@ -185,61 +185,61 @@ expr_ff(t_expr *x) static void expr_bang(t_expr *x) { - int i; + int i; #ifdef EXPR_DEBUG - { - struct ex_ex *eptr; - - for (i = 0, eptr = x->exp_var; ; eptr++, i++) - { - if (!eptr->ex_type) - break; - switch (eptr->ex_type) - { - case ET_II: - fprintf(stderr,"ET_II: %d \n", eptr->ex_int); - break; - - case ET_FI: - fprintf(stderr,"ET_FT: %f \n", eptr->ex_flt); - break; - - default: - fprintf(stderr,"oups\n"); - } - } - } + { + struct ex_ex *eptr; + + for (i = 0, eptr = x->exp_var; ; eptr++, i++) + { + if (!eptr->ex_type) + break; + switch (eptr->ex_type) + { + case ET_II: + fprintf(stderr,"ET_II: %d \n", eptr->ex_int); + break; + + case ET_FI: + fprintf(stderr,"ET_FT: %f \n", eptr->ex_flt); + break; + + default: + fprintf(stderr,"oups\n"); + } + } + } #endif - /* banging a signal or filter object means nothing */ - if (!IS_EXPR(x)) - return; - - for (i = x->exp_nexpr - 1; i > -1 ; i--) { - if (!ex_eval(x, x->exp_stack[i], &x->exp_res[i], 0)) { - /*fprintf(stderr,"expr_bang(error evaluation)\n"); */ - /* SDY now that we have mutiple ones, on error we should - * continue - return; - */ - } - switch(x->exp_res[i].ex_type) { - case ET_INT: - outlet_float(x->exp_outlet[i], - (t_float) x->exp_res[i].ex_int); - break; - - case ET_FLT: - outlet_float(x->exp_outlet[i], x->exp_res[i].ex_flt); - break; - - case ET_SYM: - /* CHANGE this will have to be taken care of */ - - default: - post("expr: bang: unrecognized result %ld\n", x->exp_res[i].ex_type); - } - } + /* banging a signal or filter object means nothing */ + if (!IS_EXPR(x)) + return; + + for (i = x->exp_nexpr - 1; i > -1 ; i--) { + if (!ex_eval(x, x->exp_stack[i], &x->exp_res[i], 0)) { + /*fprintf(stderr,"expr_bang(error evaluation)\n"); */ + /* SDY now that we have mutiple ones, on error we should + * continue + return; + */ + } + switch(x->exp_res[i].ex_type) { + case ET_INT: + outlet_float(x->exp_outlet[i], + (t_float) x->exp_res[i].ex_int); + break; + + case ET_FLT: + outlet_float(x->exp_outlet[i], x->exp_res[i].ex_flt); + break; + + case ET_SYM: + /* CHANGE this will have to be taken care of */ + + default: + post("expr: bang: unrecognized result %ld\n", x->exp_res[i].ex_type); + } + } } static t_expr * @@ -249,320 +249,320 @@ expr_new(t_symbol *s, int ac, t_atom *av) Nexpr_new(t_symbol *s, int ac, t_atom *av) #endif { - struct expr *x; - int i, ninlet; - struct ex_ex *eptr; - t_atom fakearg; - int dsp_index; /* keeping track of the dsp inlets */ + struct expr *x; + int i, ninlet; + struct ex_ex *eptr; + t_atom fakearg; + int dsp_index; /* keeping track of the dsp inlets */ /* * SDY - we may need to call dsp_setup() in this function */ - if (!ac) - { - ac = 1; - av = &fakearg; - SETFLOAT(&fakearg, 0); - } + if (!ac) + { + ac = 1; + av = &fakearg; + SETFLOAT(&fakearg, 0); + } #ifdef PD - /* - * figure out if we are expr, expr~, or fexpr~ - */ - if (!strcmp("expr", s->s_name)) { - x = (t_expr *)pd_new(expr_class); - SET_EXPR(x); - } else if (!strcmp("expr~", s->s_name)) { - x = (t_expr *)pd_new(expr_tilde_class); - SET_EXPR_TILDE(x); - } else if (!strcmp("fexpr~", s->s_name)) { - x = (t_expr *)pd_new(fexpr_tilde_class); - SET_FEXPR_TILDE(x); - } else { - post("expr_new: bad object name '%s'"); - /* assume expr */ - x = (t_expr *)pd_new(expr_class); - SET_EXPR(x); - } + /* + * figure out if we are expr, expr~, or fexpr~ + */ + if (!strcmp("expr", s->s_name)) { + x = (t_expr *)pd_new(expr_class); + SET_EXPR(x); + } else if (!strcmp("expr~", s->s_name)) { + x = (t_expr *)pd_new(expr_tilde_class); + SET_EXPR_TILDE(x); + } else if (!strcmp("fexpr~", s->s_name)) { + x = (t_expr *)pd_new(fexpr_tilde_class); + SET_FEXPR_TILDE(x); + } else { + post("expr_new: bad object name '%s'"); + /* assume expr */ + x = (t_expr *)pd_new(expr_class); + SET_EXPR(x); + } #else /* MSP */ - /* for now assume an expr~ */ - x = (t_expr *)pd_new(expr_tilde_class); - SET_EXPR_TILDE(x); -#endif - - /* - * initialize the newly allocated object - */ - x->exp_proxy = 0; - x->exp_nivec = 0; - x->exp_nexpr = 0; - x->exp_error = 0; - for (i = 0; i < MAX_VARS; i++) { - x->exp_stack[i] = (struct ex_ex *)0; - x->exp_outlet[i] = (t_outlet *)0; - x->exp_res[i].ex_type = 0; - x->exp_res[i].ex_int = 0; - x->exp_p_res[i] = (t_float *)0; - x->exp_var[i].ex_type = 0; - x->exp_var[i].ex_int = 0; - x->exp_p_var[i] = (t_float *)0; - x->exp_tmpres[i] = (t_float *)0; - x->exp_vsize = 0; - } - x->exp_f = 0; /* save the control value to be transformed to signal */ - - - if (expr_donew(x, ac, av)) - { - pd_error(x, "expr: syntax error"); + /* for now assume an expr~ */ + x = (t_expr *)pd_new(expr_tilde_class); + SET_EXPR_TILDE(x); +#endif + + /* + * initialize the newly allocated object + */ + x->exp_proxy = 0; + x->exp_nivec = 0; + x->exp_nexpr = 0; + x->exp_error = 0; + for (i = 0; i < MAX_VARS; i++) { + x->exp_stack[i] = (struct ex_ex *)0; + x->exp_outlet[i] = (t_outlet *)0; + x->exp_res[i].ex_type = 0; + x->exp_res[i].ex_int = 0; + x->exp_p_res[i] = (t_float *)0; + x->exp_var[i].ex_type = 0; + x->exp_var[i].ex_int = 0; + x->exp_p_var[i] = (t_float *)0; + x->exp_tmpres[i] = (t_float *)0; + x->exp_vsize = 0; + } + x->exp_f = 0; /* save the control value to be transformed to signal */ + + + if (expr_donew(x, ac, av)) + { + pd_error(x, "expr: syntax error"); /* SDY the following coredumps why? - pd_free(&x->exp_ob.ob_pd); + pd_free(&x->exp_ob.ob_pd); */ - return (0); - } - - ninlet = 1; - for (i = 0, eptr = x->exp_var; i < MAX_VARS ; i++, eptr++) - if (eptr->ex_type) { - ninlet = i + 1; - } - - /* - * create the new inlets - */ - for (i = 1, eptr = x->exp_var + 1, dsp_index=1; iex_type) - { - case 0: - /* nothing is using this inlet */ - if (i < ninlet) + return (0); + } + + ninlet = 1; + for (i = 0, eptr = x->exp_var; i < MAX_VARS ; i++, eptr++) + if (eptr->ex_type) { + ninlet = i + 1; + } + + /* + * create the new inlets + */ + for (i = 1, eptr = x->exp_var + 1, dsp_index=1; iex_type) + { + case 0: + /* nothing is using this inlet */ + if (i < ninlet) #ifdef PD - floatinlet_new(&x->exp_ob, &eptr->ex_flt); + floatinlet_new(&x->exp_ob, &eptr->ex_flt); #else /* MSP */ - inlet_new(&x->exp_ob, "float"); + inlet_new(&x->exp_ob, "float"); #endif - break; + break; - case ET_II: - case ET_FI: - p = exprproxy_new(x, i); + case ET_II: + case ET_FI: + p = exprproxy_new(x, i); #ifdef PD - inlet_new(&x->exp_ob, &p->p_pd, &s_float, &s_float); + inlet_new(&x->exp_ob, &p->p_pd, &s_float, &s_float); #else /* MSP */ - inlet_new(&x->exp_ob, "float"); + inlet_new(&x->exp_ob, "float"); #endif - break; + break; - case ET_SI: + case ET_SI: #ifdef PD - symbolinlet_new(&x->exp_ob, (t_symbol **)&eptr->ex_ptr); + symbolinlet_new(&x->exp_ob, (t_symbol **)&eptr->ex_ptr); #else /* MSP */ - inlet_new(&x->exp_ob, "symbol"); + inlet_new(&x->exp_ob, "symbol"); #endif - break; + break; - case ET_XI: - case ET_VI: - if (!IS_EXPR(x)) { - dsp_index++; + case ET_XI: + case ET_VI: + if (!IS_EXPR(x)) { + dsp_index++; #ifdef PD - inlet_new(&x->exp_ob, &x->exp_ob.ob_pd, - &s_signal, &s_signal); + inlet_new(&x->exp_ob, &x->exp_ob.ob_pd, + &s_signal, &s_signal); #else /* MSP */ - inlet_new(&x->exp_ob, "signal"); + inlet_new(&x->exp_ob, "signal"); #endif - break; - } else - post("expr: internal error expr_new"); - default: - pd_error(x, "expr: bad type (%lx) inlet = %d\n", - eptr->ex_type, i + 1, 0, 0, 0); - break; - } - } - if (IS_EXPR(x)) { - for (i = 0; i < x->exp_nexpr; i++) - x->exp_outlet[i] = outlet_new(&x->exp_ob, 0); - } else { - for (i = 0; i < x->exp_nexpr; i++) - x->exp_outlet[i] = outlet_new(&x->exp_ob, - gensym("signal")); - x->exp_nivec = dsp_index; - } - /* - * for now assume a 64 sample size block but this may change once - * expr_dsp is called - */ - x->exp_vsize = 64; - for (i = 0; i < x->exp_nexpr; i++) { - x->exp_p_res[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); - x->exp_tmpres[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); - } - for (i = 0; i < MAX_VARS; i++) - x->exp_p_var[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); - - return (x); + break; + } else + post("expr: internal error expr_new"); + default: + pd_error(x, "expr: bad type (%lx) inlet = %d\n", + eptr->ex_type, i + 1, 0, 0, 0); + break; + } + } + if (IS_EXPR(x)) { + for (i = 0; i < x->exp_nexpr; i++) + x->exp_outlet[i] = outlet_new(&x->exp_ob, 0); + } else { + for (i = 0; i < x->exp_nexpr; i++) + x->exp_outlet[i] = outlet_new(&x->exp_ob, + gensym("signal")); + x->exp_nivec = dsp_index; + } + /* + * for now assume a 64 sample size block but this may change once + * expr_dsp is called + */ + x->exp_vsize = 64; + for (i = 0; i < x->exp_nexpr; i++) { + x->exp_p_res[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); + x->exp_tmpres[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); + } + for (i = 0; i < MAX_VARS; i++) + x->exp_p_var[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); + + return (x); } t_int * expr_perform(t_int *w) { - int i, j; - t_expr *x = (t_expr *)w[1]; - struct ex_ex res; - int n; - - /* sanity check */ - if (IS_EXPR(x)) { - post("expr_perform: bad x->exp_flags = %d", x->exp_flags); - abort(); - } - - if (x->exp_flags & EF_STOP) { - for (i = 0; i < x->exp_nexpr; i++) - memset(x->exp_res[i].ex_vec, 0, - x->exp_vsize * sizeof (float)); - return (w + 2); - } - - if (IS_EXPR_TILDE(x)) { - /* - * if we have only one expression, we can right on - * on the output directly, otherwise we have to copy - * the data because, outputs could be the same buffer as - * inputs - */ - if ( x->exp_nexpr == 1) - ex_eval(x, x->exp_stack[0], &x->exp_res[0], 0); - else { - res.ex_type = ET_VEC; - for (i = 0; i < x->exp_nexpr; i++) { - res.ex_vec = x->exp_tmpres[i]; - ex_eval(x, x->exp_stack[i], &res, 0); - } - n = x->exp_vsize * sizeof(t_float); - for (i = 0; i < x->exp_nexpr; i++) - memcpy(x->exp_res[i].ex_vec, x->exp_tmpres[i], - n); - } - return (w + 2); - } - - if (!IS_FEXPR_TILDE(x)) { - post("expr_perform: bad x->exp_flags = %d - expecting fexpr", - x->exp_flags); - return (w + 2); - } - /* - * since the output buffer could be the same as one of the inputs - * we need to keep the output in a different buffer - */ - for (i = 0; i < x->exp_vsize; i++) for (j = 0; j < x->exp_nexpr; j++) { - res.ex_type = 0; - res.ex_int = 0; - ex_eval(x, x->exp_stack[j], &res, i); - switch (res.ex_type) { - case ET_INT: - x->exp_tmpres[j][i] = (t_float) res.ex_int; - break; - case ET_FLT: - x->exp_tmpres[j][i] = res.ex_flt; - break; - default: - post("expr_perform: bad result type %d", res.ex_type); - } - } - /* - * copy inputs and results to the save buffers - * inputs need to be copied first as the output buffer can be - * same as an input buffer - */ - n = x->exp_vsize * sizeof(t_float); - for (i = 0; i < MAX_VARS; i++) - if (x->exp_var[i].ex_type == ET_XI) - memcpy(x->exp_p_var[i], x->exp_var[i].ex_vec, n); - for (i = 0; i < x->exp_nexpr; i++) { - memcpy(x->exp_p_res[i], x->exp_tmpres[i], n); - memcpy(x->exp_res[i].ex_vec, x->exp_tmpres[i], n); - } - return (w + 2); + int i, j; + t_expr *x = (t_expr *)w[1]; + struct ex_ex res; + int n; + + /* sanity check */ + if (IS_EXPR(x)) { + post("expr_perform: bad x->exp_flags = %d", x->exp_flags); + abort(); + } + + if (x->exp_flags & EF_STOP) { + for (i = 0; i < x->exp_nexpr; i++) + memset(x->exp_res[i].ex_vec, 0, + x->exp_vsize * sizeof (float)); + return (w + 2); + } + + if (IS_EXPR_TILDE(x)) { + /* + * if we have only one expression, we can right on + * on the output directly, otherwise we have to copy + * the data because, outputs could be the same buffer as + * inputs + */ + if ( x->exp_nexpr == 1) + ex_eval(x, x->exp_stack[0], &x->exp_res[0], 0); + else { + res.ex_type = ET_VEC; + for (i = 0; i < x->exp_nexpr; i++) { + res.ex_vec = x->exp_tmpres[i]; + ex_eval(x, x->exp_stack[i], &res, 0); + } + n = x->exp_vsize * sizeof(t_float); + for (i = 0; i < x->exp_nexpr; i++) + memcpy(x->exp_res[i].ex_vec, x->exp_tmpres[i], + n); + } + return (w + 2); + } + + if (!IS_FEXPR_TILDE(x)) { + post("expr_perform: bad x->exp_flags = %d - expecting fexpr", + x->exp_flags); + return (w + 2); + } + /* + * since the output buffer could be the same as one of the inputs + * we need to keep the output in a different buffer + */ + for (i = 0; i < x->exp_vsize; i++) for (j = 0; j < x->exp_nexpr; j++) { + res.ex_type = 0; + res.ex_int = 0; + ex_eval(x, x->exp_stack[j], &res, i); + switch (res.ex_type) { + case ET_INT: + x->exp_tmpres[j][i] = (t_float) res.ex_int; + break; + case ET_FLT: + x->exp_tmpres[j][i] = res.ex_flt; + break; + default: + post("expr_perform: bad result type %d", res.ex_type); + } + } + /* + * copy inputs and results to the save buffers + * inputs need to be copied first as the output buffer can be + * same as an input buffer + */ + n = x->exp_vsize * sizeof(t_float); + for (i = 0; i < MAX_VARS; i++) + if (x->exp_var[i].ex_type == ET_XI) + memcpy(x->exp_p_var[i], x->exp_var[i].ex_vec, n); + for (i = 0; i < x->exp_nexpr; i++) { + memcpy(x->exp_p_res[i], x->exp_tmpres[i], n); + memcpy(x->exp_res[i].ex_vec, x->exp_tmpres[i], n); + } + return (w + 2); } static void expr_dsp(t_expr *x, t_signal **sp) { - int i, nv; - int newsize; - - x->exp_error = 0; /* reset all errors */ - newsize = (x->exp_vsize != sp[0]->s_n); - x->exp_vsize = sp[0]->s_n; /* record the vector size */ - for (i = 0; i < x->exp_nexpr; i++) { - x->exp_res[i].ex_type = ET_VEC; - x->exp_res[i].ex_vec = sp[x->exp_nivec + i]->s_vec; - } - for (i = 0, nv = 0; i < MAX_VARS; i++) - /* - * the first inlet is always a signal - * - * SDY We are warning the user till this limitation - * is taken away from pd - */ - if (!i || x->exp_var[i].ex_type == ET_VI || - x->exp_var[i].ex_type == ET_XI) { - if (nv >= x->exp_nivec) { - post("expr_dsp int. err nv = %d, x->exp_nive = %d", - nv, x->exp_nivec); - abort(); - } - x->exp_var[i].ex_vec = sp[nv]->s_vec; - nv++; - } - /* we always have one inlet but we may not use it */ - if (nv != x->exp_nivec && (nv != 0 || x->exp_nivec != 1)) { - post("expr_dsp internal error 2 nv = %d, x->exp_nive = %d", - nv, x->exp_nivec); - abort(); - } - - dsp_add(expr_perform, 1, (t_int *) x); - - /* - * The buffer are now being allocated for expr~ and fexpr~ - * because if we have more than one expression we need the - * temporary buffers, The save buffers are not really needed - if (!IS_FEXPR_TILDE(x)) - return; - */ - /* - * if we have already allocated the buffers and we have a - * new size free all the buffers - */ - if (x->exp_p_res[0]) { - if (!newsize) - return; - /* - * if new size, reallocate all the previous buffers for fexpr~ - */ - for (i = 0; i < x->exp_nexpr; i++) { - fts_free(x->exp_p_res[i]); - fts_free(x->exp_tmpres[i]); - } - for (i = 0; i < MAX_VARS; i++) - fts_free(x->exp_p_var[i]); - - } - for (i = 0; i < x->exp_nexpr; i++) { - x->exp_p_res[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); - x->exp_tmpres[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); - } - for (i = 0; i < MAX_VARS; i++) - x->exp_p_var[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); + int i, nv; + int newsize; + + x->exp_error = 0; /* reset all errors */ + newsize = (x->exp_vsize != sp[0]->s_n); + x->exp_vsize = sp[0]->s_n; /* record the vector size */ + for (i = 0; i < x->exp_nexpr; i++) { + x->exp_res[i].ex_type = ET_VEC; + x->exp_res[i].ex_vec = sp[x->exp_nivec + i]->s_vec; + } + for (i = 0, nv = 0; i < MAX_VARS; i++) + /* + * the first inlet is always a signal + * + * SDY We are warning the user till this limitation + * is taken away from pd + */ + if (!i || x->exp_var[i].ex_type == ET_VI || + x->exp_var[i].ex_type == ET_XI) { + if (nv >= x->exp_nivec) { + post("expr_dsp int. err nv = %d, x->exp_nive = %d", + nv, x->exp_nivec); + abort(); + } + x->exp_var[i].ex_vec = sp[nv]->s_vec; + nv++; + } + /* we always have one inlet but we may not use it */ + if (nv != x->exp_nivec && (nv != 0 || x->exp_nivec != 1)) { + post("expr_dsp internal error 2 nv = %d, x->exp_nive = %d", + nv, x->exp_nivec); + abort(); + } + + dsp_add(expr_perform, 1, (t_int *) x); + + /* + * The buffer are now being allocated for expr~ and fexpr~ + * because if we have more than one expression we need the + * temporary buffers, The save buffers are not really needed + if (!IS_FEXPR_TILDE(x)) + return; + */ + /* + * if we have already allocated the buffers and we have a + * new size free all the buffers + */ + if (x->exp_p_res[0]) { + if (!newsize) + return; + /* + * if new size, reallocate all the previous buffers for fexpr~ + */ + for (i = 0; i < x->exp_nexpr; i++) { + fts_free(x->exp_p_res[i]); + fts_free(x->exp_tmpres[i]); + } + for (i = 0; i < MAX_VARS; i++) + fts_free(x->exp_p_var[i]); + + } + for (i = 0; i < x->exp_nexpr; i++) { + x->exp_p_res[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); + x->exp_tmpres[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); + } + for (i = 0; i < MAX_VARS; i++) + x->exp_p_var[i] = fts_calloc(x->exp_vsize, sizeof (t_float)); } /* @@ -571,13 +571,13 @@ expr_dsp(t_expr *x, t_signal **sp) static void expr_verbose(t_expr *x) { - if (x->exp_flags & EF_VERBOSE) { - x->exp_flags &= ~EF_VERBOSE; - post ("verbose off"); - } else { - x->exp_flags |= EF_VERBOSE; - post ("verbose on"); - } + if (x->exp_flags & EF_VERBOSE) { + x->exp_flags &= ~EF_VERBOSE; + post ("verbose off"); + } else { + x->exp_flags |= EF_VERBOSE; + post ("verbose on"); + } } /* @@ -586,7 +586,7 @@ expr_verbose(t_expr *x) static void expr_start(t_expr *x) { - x->exp_flags &= ~EF_STOP; + x->exp_flags &= ~EF_STOP; } /* @@ -595,120 +595,120 @@ expr_start(t_expr *x) static void expr_stop(t_expr *x) { - x->exp_flags |= EF_STOP; + x->exp_flags |= EF_STOP; } static void fexpr_set_usage(void) { - post("fexpr~: set val ..."); - post("fexpr~: set {xy}[#] val ..."); + post("fexpr~: set val ..."); + post("fexpr~: set {xy}[#] val ..."); } /* * fexpr_tilde_set -- set previous values of the buffers - * set val val ... - sets the first elements of output buffers - * set x val ... - sets the elements of the first input buffer - * set x# val ... - sets the elements of the #th input buffers - * set y val ... - sets the elements of the first output buffer - * set y# val ... - sets the elements of the #th output buffers + * set val val ... - sets the first elements of output buffers + * set x val ... - sets the elements of the first input buffer + * set x# val ... - sets the elements of the #th input buffers + * set y val ... - sets the elements of the first output buffer + * set y# val ... - sets the elements of the #th output buffers */ static void fexpr_tilde_set(t_expr *x, t_symbol *s, int argc, t_atom *argv) { - t_symbol *sx; - int vecno; - int i, nargs; - - if (!argc) - return; - sx = atom_getsymbolarg(0, argc, argv); - switch(sx->s_name[0]) { - case 'x': - if (!sx->s_name[1]) - vecno = 0; - else { - vecno = atoi(sx->s_name + 1); - if (!vecno) { - post("fexpr~.set: bad set x vector number"); - fexpr_set_usage(); - return; - } - if (vecno >= MAX_VARS) { - post("fexpr~.set: no more than %d inlets", - MAX_VARS); - return; - } - vecno--; - } - if (x->exp_var[vecno].ex_type != ET_XI) { - post("fexpr~-set: no signal at inlet %d", vecno + 1); - return; - } - nargs = argc - 1; - if (!nargs) { - post("fexpr~-set: no argument to set"); - return; - } - if (nargs > x->exp_vsize) { - post("fexpr~.set: %d set values larger than vector size(%d)", - nargs, x->exp_vsize); - post("fexpr~.set: only the first %d values will be set", - x->exp_vsize); - nargs = x->exp_vsize; - } - for (i = 0; i < nargs; i++) { - x->exp_p_var[vecno][x->exp_vsize - i - 1] = - atom_getfloatarg(i + 1, argc, argv); - } - return; - case 'y': - if (!sx->s_name[1]) - vecno = 0; - else { - vecno = atoi(sx->s_name + 1); - if (!vecno) { - post("fexpr~.set: bad set y vector number"); - fexpr_set_usage(); - return; - } - vecno--; - } - if (vecno >= x->exp_nexpr) { - post("fexpr~.set: only %d outlets", x->exp_nexpr); - return; - } - nargs = argc - 1; - if (!nargs) { - post("fexpr~-set: no argument to set"); - return; - } - if (nargs > x->exp_vsize) { - post("fexpr~-set: %d set values larger than vector size(%d)", - nargs, x->exp_vsize); - post("fexpr~.set: only the first %d values will be set", - x->exp_vsize); - nargs = x->exp_vsize; - } - for (i = 0; i < nargs; i++) { - x->exp_p_res[vecno][x->exp_vsize - i - 1] = - atom_getfloatarg(i + 1, argc, argv); - } - return; - case 0: - if (argc > x->exp_nexpr) { - post("fexpr~.set: only %d outlets available", - x->exp_nexpr); - post("fexpr~.set: the extra set values are ignored"); - } - for (i = 0; i < x->exp_nexpr && i < argc; i++) - x->exp_p_res[i][x->exp_vsize - 1] = - atom_getfloatarg(i, argc, argv); - return; - default: - fexpr_set_usage(); - return; - } - return; + t_symbol *sx; + int vecno; + int i, nargs; + + if (!argc) + return; + sx = atom_getsymbolarg(0, argc, argv); + switch(sx->s_name[0]) { + case 'x': + if (!sx->s_name[1]) + vecno = 0; + else { + vecno = atoi(sx->s_name + 1); + if (!vecno) { + post("fexpr~.set: bad set x vector number"); + fexpr_set_usage(); + return; + } + if (vecno >= MAX_VARS) { + post("fexpr~.set: no more than %d inlets", + MAX_VARS); + return; + } + vecno--; + } + if (x->exp_var[vecno].ex_type != ET_XI) { + post("fexpr~-set: no signal at inlet %d", vecno + 1); + return; + } + nargs = argc - 1; + if (!nargs) { + post("fexpr~-set: no argument to set"); + return; + } + if (nargs > x->exp_vsize) { + post("fexpr~.set: %d set values larger than vector size(%d)", + nargs, x->exp_vsize); + post("fexpr~.set: only the first %d values will be set", + x->exp_vsize); + nargs = x->exp_vsize; + } + for (i = 0; i < nargs; i++) { + x->exp_p_var[vecno][x->exp_vsize - i - 1] = + atom_getfloatarg(i + 1, argc, argv); + } + return; + case 'y': + if (!sx->s_name[1]) + vecno = 0; + else { + vecno = atoi(sx->s_name + 1); + if (!vecno) { + post("fexpr~.set: bad set y vector number"); + fexpr_set_usage(); + return; + } + vecno--; + } + if (vecno >= x->exp_nexpr) { + post("fexpr~.set: only %d outlets", x->exp_nexpr); + return; + } + nargs = argc - 1; + if (!nargs) { + post("fexpr~-set: no argument to set"); + return; + } + if (nargs > x->exp_vsize) { + post("fexpr~-set: %d set values larger than vector size(%d)", + nargs, x->exp_vsize); + post("fexpr~.set: only the first %d values will be set", + x->exp_vsize); + nargs = x->exp_vsize; + } + for (i = 0; i < nargs; i++) { + x->exp_p_res[vecno][x->exp_vsize - i - 1] = + atom_getfloatarg(i + 1, argc, argv); + } + return; + case 0: + if (argc > x->exp_nexpr) { + post("fexpr~.set: only %d outlets available", + x->exp_nexpr); + post("fexpr~.set: the extra set values are ignored"); + } + for (i = 0; i < x->exp_nexpr && i < argc; i++) + x->exp_p_res[i][x->exp_vsize - 1] = + atom_getfloatarg(i, argc, argv); + return; + default: + fexpr_set_usage(); + return; + } + return; } /* @@ -717,74 +717,74 @@ fexpr_tilde_set(t_expr *x, t_symbol *s, int argc, t_atom *argv) static void fexpr_tilde_clear(t_expr *x, t_symbol *s, int argc, t_atom *argv) { - t_symbol *sx; - int vecno; - int i, nargs; - - /* - * if no arguement clear all input and output buffers - */ - if (!argc) { - for (i = 0; i < x->exp_nexpr; i++) - memset(x->exp_p_res[i], 0, x->exp_vsize*sizeof(float)); - for (i = 0; i < MAX_VARS; i++) - if (x->exp_var[i].ex_type == ET_XI) - memset(x->exp_p_var[i], 0, - x->exp_vsize*sizeof(float)); - return; - } - if (argc > 1) { - post("fexpr~ usage: 'clear' or 'clear {xy}[#]'"); - return; - } - - sx = atom_getsymbolarg(0, argc, argv); - switch(sx->s_name[0]) { - case 'x': - if (!sx->s_name[1]) - vecno = 0; - else { - vecno = atoi(sx->s_name + 1); - if (!vecno) { - post("fexpr~.clear: bad clear x vector number"); - return; - } - if (vecno >= MAX_VARS) { - post("fexpr~.clear: no more than %d inlets", - MAX_VARS); - return; - } - vecno--; - } - if (x->exp_var[vecno].ex_type != ET_XI) { - post("fexpr~-clear: no signal at inlet %d", vecno + 1); - return; - } - memset(x->exp_p_var[vecno], 0, x->exp_vsize*sizeof(float)); - return; - case 'y': - if (!sx->s_name[1]) - vecno = 0; - else { - vecno = atoi(sx->s_name + 1); - if (!vecno) { - post("fexpr~.clear: bad clear y vector number"); - return; - } - vecno--; - } - if (vecno >= x->exp_nexpr) { - post("fexpr~.clear: only %d outlets", x->exp_nexpr); - return; - } - memset(x->exp_p_res[vecno], 0, x->exp_vsize*sizeof(float)); - return; - return; - default: - post("fexpr~ usage: 'clear' or 'clear {xy}[#]'"); - return; - } - return; + t_symbol *sx; + int vecno; + int i, nargs; + + /* + * if no arguement clear all input and output buffers + */ + if (!argc) { + for (i = 0; i < x->exp_nexpr; i++) + memset(x->exp_p_res[i], 0, x->exp_vsize*sizeof(float)); + for (i = 0; i < MAX_VARS; i++) + if (x->exp_var[i].ex_type == ET_XI) + memset(x->exp_p_var[i], 0, + x->exp_vsize*sizeof(float)); + return; + } + if (argc > 1) { + post("fexpr~ usage: 'clear' or 'clear {xy}[#]'"); + return; + } + + sx = atom_getsymbolarg(0, argc, argv); + switch(sx->s_name[0]) { + case 'x': + if (!sx->s_name[1]) + vecno = 0; + else { + vecno = atoi(sx->s_name + 1); + if (!vecno) { + post("fexpr~.clear: bad clear x vector number"); + return; + } + if (vecno >= MAX_VARS) { + post("fexpr~.clear: no more than %d inlets", + MAX_VARS); + return; + } + vecno--; + } + if (x->exp_var[vecno].ex_type != ET_XI) { + post("fexpr~-clear: no signal at inlet %d", vecno + 1); + return; + } + memset(x->exp_p_var[vecno], 0, x->exp_vsize*sizeof(float)); + return; + case 'y': + if (!sx->s_name[1]) + vecno = 0; + else { + vecno = atoi(sx->s_name + 1); + if (!vecno) { + post("fexpr~.clear: bad clear y vector number"); + return; + } + vecno--; + } + if (vecno >= x->exp_nexpr) { + post("fexpr~.clear: only %d outlets", x->exp_nexpr); + return; + } + memset(x->exp_p_res[vecno], 0, x->exp_vsize*sizeof(float)); + return; + return; + default: + post("fexpr~ usage: 'clear' or 'clear {xy}[#]'"); + return; + } + return; } #ifdef PD @@ -792,68 +792,68 @@ fexpr_tilde_clear(t_expr *x, t_symbol *s, int argc, t_atom *argv) void expr_setup(void) { - /* - * expr initialization - */ - expr_class = class_new(gensym("expr"), (t_newmethod)expr_new, - (t_method)expr_ff, sizeof(t_expr), 0, A_GIMME, 0); - class_addlist(expr_class, expr_list); - exprproxy_class = class_new(gensym("exprproxy"), 0, - 0, sizeof(t_exprproxy), CLASS_PD, 0); - class_addfloat(exprproxy_class, exprproxy_float); - - /* - * expr~ initialization - */ - expr_tilde_class = class_new(gensym("expr~"), (t_newmethod)expr_new, - (t_method)expr_ff, sizeof(t_expr), 0, A_GIMME, 0); - class_addmethod(expr_tilde_class, nullfn, gensym("signal"), 0); - CLASS_MAINSIGNALIN(expr_tilde_class, t_expr, exp_f); - class_addmethod(expr_tilde_class,(t_method)expr_dsp, gensym("dsp"), 0); - class_sethelpsymbol(expr_tilde_class, gensym("expr")); - /* - * fexpr~ initialization - */ - fexpr_tilde_class = class_new(gensym("fexpr~"), (t_newmethod)expr_new, - (t_method)expr_ff, sizeof(t_expr), 0, A_GIMME, 0); - class_addmethod(fexpr_tilde_class, nullfn, gensym("signal"), 0); - class_addmethod(fexpr_tilde_class,(t_method)expr_start, - gensym("start"), 0); - class_addmethod(fexpr_tilde_class,(t_method)expr_stop, - gensym("stop"), 0); - - class_addmethod(fexpr_tilde_class,(t_method)expr_dsp,gensym("dsp"), 0); - class_addmethod(fexpr_tilde_class, (t_method)fexpr_tilde_set, - gensym("set"), A_GIMME, 0); - class_addmethod(fexpr_tilde_class, (t_method)fexpr_tilde_clear, - gensym("clear"), A_GIMME, 0); - class_addmethod(fexpr_tilde_class,(t_method)expr_verbose, - gensym("verbose"), 0); - class_sethelpsymbol(fexpr_tilde_class, gensym("expr")); - - - - post("expr, expr~, fexpr~ version %s under GNU General Public License ", exp_version); + /* + * expr initialization + */ + expr_class = class_new(gensym("expr"), (t_newmethod)expr_new, + (t_method)expr_ff, sizeof(t_expr), 0, A_GIMME, 0); + class_addlist(expr_class, expr_list); + exprproxy_class = class_new(gensym("exprproxy"), 0, + 0, sizeof(t_exprproxy), CLASS_PD, 0); + class_addfloat(exprproxy_class, exprproxy_float); + + /* + * expr~ initialization + */ + expr_tilde_class = class_new(gensym("expr~"), (t_newmethod)expr_new, + (t_method)expr_ff, sizeof(t_expr), 0, A_GIMME, 0); + class_addmethod(expr_tilde_class, nullfn, gensym("signal"), 0); + CLASS_MAINSIGNALIN(expr_tilde_class, t_expr, exp_f); + class_addmethod(expr_tilde_class,(t_method)expr_dsp, gensym("dsp"), 0); + class_sethelpsymbol(expr_tilde_class, gensym("expr")); + /* + * fexpr~ initialization + */ + fexpr_tilde_class = class_new(gensym("fexpr~"), (t_newmethod)expr_new, + (t_method)expr_ff, sizeof(t_expr), 0, A_GIMME, 0); + class_addmethod(fexpr_tilde_class, nullfn, gensym("signal"), 0); + class_addmethod(fexpr_tilde_class,(t_method)expr_start, + gensym("start"), 0); + class_addmethod(fexpr_tilde_class,(t_method)expr_stop, + gensym("stop"), 0); + + class_addmethod(fexpr_tilde_class,(t_method)expr_dsp,gensym("dsp"), 0); + class_addmethod(fexpr_tilde_class, (t_method)fexpr_tilde_set, + gensym("set"), A_GIMME, 0); + class_addmethod(fexpr_tilde_class, (t_method)fexpr_tilde_clear, + gensym("clear"), A_GIMME, 0); + class_addmethod(fexpr_tilde_class,(t_method)expr_verbose, + gensym("verbose"), 0); + class_sethelpsymbol(fexpr_tilde_class, gensym("expr")); + + + + post("expr, expr~, fexpr~ version %s under GNU General Public License ", exp_version); } void expr_tilde_setup(void) { - expr_setup(); + expr_setup(); } void fexpr_tilde_setup(void) { - expr_setup(); + expr_setup(); } #else /* MSP */ void main(void) { setup((t_messlist **)&expr_tilde_class, (method)Nexpr_new, - (method)expr_ff, (short)sizeof(t_expr), 0L, A_GIMME, 0); + (method)expr_ff, (short)sizeof(t_expr), 0L, A_GIMME, 0); addmess((method)expr_dsp, "dsp", A_CANT, 0); // dsp method dsp_initclass(); } @@ -866,21 +866,21 @@ main(void) int ex_getsym(char *p, fts_symbol_t *s) { - *s = gensym(p); - return (0); + *s = gensym(p); + return (0); } const char * ex_symname(fts_symbol_t s) { - return (fts_symbol_name(s)); + return (fts_symbol_name(s)); } /* * max_ex_tab -- evaluate this table access - * eptr is the name of the table and arg is the index we - * have to put the result in optr - * return 1 on error and 0 otherwise + * eptr is the name of the table and arg is the index we + * have to put the result in optr + * return 1 on error and 0 otherwise * * Arguments: * the expr object @@ -893,70 +893,70 @@ max_ex_tab(struct expr *expr, fts_symbol_t s, struct ex_ex *arg, struct ex_ex *optr) { #ifdef PD - t_garray *garray; - int size, indx; - t_float *vec; - - if (!s || !(garray = (t_garray *)pd_findbyclass(s, garray_class)) || - !garray_getfloatarray(garray, &size, &vec)) - { - optr->ex_type = ET_FLT; - optr->ex_flt = 0; - pd_error(expr, "no such table '%s'", s->s_name); - return (1); - } - optr->ex_type = ET_FLT; - - switch (arg->ex_type) { - case ET_INT: - indx = arg->ex_int; - break; - case ET_FLT: - /* strange interpolation code deleted here -msp */ - indx = arg->ex_flt; - break; - - default: /* do something with strings */ - pd_error(expr, "expr: bad argument for table '%s'\n", fts_symbol_name(s)); - indx = 0; - } - if (indx < 0) indx = 0; - else if (indx >= size) indx = size - 1; - optr->ex_flt = vec[indx]; + t_garray *garray; + int size, indx; + t_float *vec; + + if (!s || !(garray = (t_garray *)pd_findbyclass(s, garray_class)) || + !garray_getfloatarray(garray, &size, &vec)) + { + optr->ex_type = ET_FLT; + optr->ex_flt = 0; + pd_error(expr, "no such table '%s'", s->s_name); + return (1); + } + optr->ex_type = ET_FLT; + + switch (arg->ex_type) { + case ET_INT: + indx = arg->ex_int; + break; + case ET_FLT: + /* strange interpolation code deleted here -msp */ + indx = arg->ex_flt; + break; + + default: /* do something with strings */ + pd_error(expr, "expr: bad argument for table '%s'\n", fts_symbol_name(s)); + indx = 0; + } + if (indx < 0) indx = 0; + else if (indx >= size) indx = size - 1; + optr->ex_flt = vec[indx]; #else /* MSP */ - /* - * table lookup not done for MSP yet - */ - post("max_ex_tab: not complete for MSP yet!"); - optr->ex_type = ET_FLT; - optr->ex_flt = 0; -#endif - return (0); + /* + * table lookup not done for MSP yet + */ + post("max_ex_tab: not complete for MSP yet!"); + optr->ex_type = ET_FLT; + optr->ex_flt = 0; +#endif + return (0); } int max_ex_var(struct expr *expr, fts_symbol_t var, struct ex_ex *optr) { - optr->ex_type = ET_FLT; - if (value_getfloat(var, &(optr->ex_flt))) { - optr->ex_type = ET_FLT; - optr->ex_flt = 0; - pd_error(expr, "no such var '%s'", var->s_name); - return (1); - } - return (0); + optr->ex_type = ET_FLT; + if (value_getfloat(var, &(optr->ex_flt))) { + optr->ex_type = ET_FLT; + optr->ex_flt = 0; + pd_error(expr, "no such var '%s'", var->s_name); + return (1); + } + return (0); } #ifdef PD /* this goes to the end of this file as the following functions * should be defined in the expr object in MSP */ -#define ISTABLE(sym, garray, size, vec) \ +#define ISTABLE(sym, garray, size, vec) \ if (!sym || !(garray = (t_garray *)pd_findbyclass(sym, garray_class)) || \ - !garray_getfloatarray(garray, &size, &vec)) { \ - optr->ex_type = ET_FLT; \ - optr->ex_int = 0; \ - error("no such table '%s'", sym->s_name); \ - return; \ + !garray_getfloatarray(garray, &size, &vec)) { \ + optr->ex_type = ET_FLT; \ + optr->ex_int = 0; \ + error("no such table '%s'", sym->s_name); \ + return; \ } /* @@ -965,25 +965,25 @@ if (!sym || !(garray = (t_garray *)pd_findbyclass(sym, garray_class)) || \ void ex_size(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - t_symbol *s; - t_garray *garray; - int size; - t_float *vec; + t_symbol *s; + t_garray *garray; + int size; + t_float *vec; - if (argv->ex_type != ET_SYM) - { - post("expr: size: need a table name\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - return; - } + if (argv->ex_type != ET_SYM) + { + post("expr: size: need a table name\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + return; + } - s = (fts_symbol_t ) argv->ex_ptr; + s = (fts_symbol_t ) argv->ex_ptr; - ISTABLE(s, garray, size, vec); + ISTABLE(s, garray, size, vec); - optr->ex_type = ET_INT; - optr->ex_int = size; + optr->ex_type = ET_INT; + optr->ex_int = size; } /* @@ -993,29 +993,29 @@ ex_size(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) void ex_sum(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - t_symbol *s; - t_garray *garray; - int size; - t_float *vec, sum; - int indx; + t_symbol *s; + t_garray *garray; + int size; + t_float *vec, sum; + int indx; - if (argv->ex_type != ET_SYM) - { - post("expr: sum: need a table name\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - return; - } + if (argv->ex_type != ET_SYM) + { + post("expr: sum: need a table name\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + return; + } - s = (fts_symbol_t ) argv->ex_ptr; + s = (fts_symbol_t ) argv->ex_ptr; - ISTABLE(s, garray, size, vec); + ISTABLE(s, garray, size, vec); - for (indx = 0, sum = 0; indx < size; indx++) - sum += vec[indx]; + for (indx = 0, sum = 0; indx < size; indx++) + sum += vec[indx]; - optr->ex_type = ET_FLT; - optr->ex_flt = sum; + optr->ex_type = ET_FLT; + optr->ex_flt = sum; } @@ -1026,40 +1026,40 @@ ex_sum(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) void ex_Sum(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { - t_symbol *s; - t_garray *garray; - int size; - t_float *vec, sum; - int indx, n1, n2; - - if (argv->ex_type != ET_SYM) - { - post("expr: sum: need a table name\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - return; - } - - s = (fts_symbol_t ) argv->ex_ptr; - - ISTABLE(s, garray, size, vec); - - if (argv->ex_type != ET_INT || argv[1].ex_type != ET_INT) - { - post("expr: Sum: boundries have to be fix values\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - return; - } - n1 = argv->ex_int; - n2 = argv[1].ex_int; - - for (indx = n1, sum = 0; indx < n2; indx++) - if (indx >= 0 && indx < size) - sum += vec[indx]; - - optr->ex_type = ET_FLT; - optr->ex_flt = sum; + t_symbol *s; + t_garray *garray; + int size; + t_float *vec, sum; + int indx, n1, n2; + + if (argv->ex_type != ET_SYM) + { + post("expr: sum: need a table name\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + return; + } + + s = (fts_symbol_t ) argv->ex_ptr; + + ISTABLE(s, garray, size, vec); + + if (argv->ex_type != ET_INT || argv[1].ex_type != ET_INT) + { + post("expr: Sum: boundries have to be fix values\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + return; + } + n1 = argv->ex_int; + n2 = argv[1].ex_int; + + for (indx = n1, sum = 0; indx < n2; indx++) + if (indx >= 0 && indx < size) + sum += vec[indx]; + + optr->ex_type = ET_FLT; + optr->ex_flt = sum; } /* @@ -1071,35 +1071,35 @@ ex_avg(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { /* SDY - look into this function */ #if 0 - fts_symbol_t s; - fts_integer_vector_t *tw = 0; - - if (argv->ex_type != ET_SYM) - { - post("expr: avg: need a table name\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - } - - s = (fts_symbol_t ) argv->ex_ptr; - - tw = table_integer_vector_get_by_name(s); - - if (tw) - { - optr->ex_type = ET_INT; - - if (! fts_integer_vector_get_size(tw)) - optr->ex_int = 0; - else - optr->ex_int = fts_integer_vector_get_sum(tw) / fts_integer_vector_get_size(tw); - } - else - { - optr->ex_type = ET_INT; - optr->ex_int = 0; - post("expr: avg: no such table %s\n", fts_symbol_name(s)); - } + fts_symbol_t s; + fts_integer_vector_t *tw = 0; + + if (argv->ex_type != ET_SYM) + { + post("expr: avg: need a table name\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + } + + s = (fts_symbol_t ) argv->ex_ptr; + + tw = table_integer_vector_get_by_name(s); + + if (tw) + { + optr->ex_type = ET_INT; + + if (! fts_integer_vector_get_size(tw)) + optr->ex_int = 0; + else + optr->ex_int = fts_integer_vector_get_sum(tw) / fts_integer_vector_get_size(tw); + } + else + { + optr->ex_type = ET_INT; + optr->ex_int = 0; + post("expr: avg: no such table %s\n", fts_symbol_name(s)); + } #endif } @@ -1113,50 +1113,50 @@ ex_Avg(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { /* SDY - look into this function */ #if 0 - fts_symbol_t s; - fts_integer_vector_t *tw = 0; - - if (argv->ex_type != ET_SYM) - { - post("expr: Avg: need a table name\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - } - - s = (fts_symbol_t ) (argv++)->ex_ptr; - - tw = table_integer_vector_get_by_name(s); - - if (! tw) - { - optr->ex_type = ET_INT; - optr->ex_int = 0; - post("expr: Avg: no such table %s\n", fts_symbol_name(s)); - return; - } - - if (argv->ex_type != ET_INT || argv[1].ex_type != ET_INT) - { - post("expr: Avg: boundries have to be fix values\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - return; - } - - optr->ex_type = ET_INT; - - if (argv[1].ex_int - argv->ex_int <= 0) - optr->ex_int = 0; - else - optr->ex_int = (fts_integer_vector_get_sub_sum(tw, argv->ex_int, argv[1].ex_int) / - (argv[1].ex_int - argv->ex_int)); + fts_symbol_t s; + fts_integer_vector_t *tw = 0; + + if (argv->ex_type != ET_SYM) + { + post("expr: Avg: need a table name\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + } + + s = (fts_symbol_t ) (argv++)->ex_ptr; + + tw = table_integer_vector_get_by_name(s); + + if (! tw) + { + optr->ex_type = ET_INT; + optr->ex_int = 0; + post("expr: Avg: no such table %s\n", fts_symbol_name(s)); + return; + } + + if (argv->ex_type != ET_INT || argv[1].ex_type != ET_INT) + { + post("expr: Avg: boundries have to be fix values\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + return; + } + + optr->ex_type = ET_INT; + + if (argv[1].ex_int - argv->ex_int <= 0) + optr->ex_int = 0; + else + optr->ex_int = (fts_integer_vector_get_sub_sum(tw, argv->ex_int, argv[1].ex_int) / + (argv[1].ex_int - argv->ex_int)); #endif } /* * ex_store -- store a value in a table - * if the index is greater the size of the table, - * we will make a modulo the size of the table + * if the index is greater the size of the table, + * we will make a modulo the size of the table */ void @@ -1164,35 +1164,35 @@ ex_store(t_expr *e, long int argc, struct ex_ex *argv, struct ex_ex *optr) { /* SDY - look into this function */ #if 0 - fts_symbol_t s; - fts_integer_vector_t *tw = 0; - - if (argv->ex_type != ET_SYM) - { - post("expr: store: need a table name\n"); - } - - s = (fts_symbol_t ) (argv++)->ex_ptr; - - tw = table_integer_vector_get_by_name(s); - - if (! tw) - { - optr->ex_type = ET_INT; - optr->ex_int = 0; - post("expr: store: no such table %s\n", fts_symbol_name(s)); - return; - } - - if (argv->ex_type != ET_INT || argv[1].ex_type != ET_INT) - { - post("expr: store: arguments have to be integer\n"); - optr->ex_type = ET_INT; - optr->ex_int = 0; - } - - fts_integer_vector_set_element(tw, argv->ex_int < 0 ? 0 : argv->ex_int % fts_integer_vector_get_size(tw), argv[1].ex_int); - *optr = argv[1]; + fts_symbol_t s; + fts_integer_vector_t *tw = 0; + + if (argv->ex_type != ET_SYM) + { + post("expr: store: need a table name\n"); + } + + s = (fts_symbol_t ) (argv++)->ex_ptr; + + tw = table_integer_vector_get_by_name(s); + + if (! tw) + { + optr->ex_type = ET_INT; + optr->ex_int = 0; + post("expr: store: no such table %s\n", fts_symbol_name(s)); + return; + } + + if (argv->ex_type != ET_INT || argv[1].ex_type != ET_INT) + { + post("expr: store: arguments have to be integer\n"); + optr->ex_type = ET_INT; + optr->ex_int = 0; + } + + fts_integer_vector_set_element(tw, argv->ex_int < 0 ? 0 : argv->ex_int % fts_integer_vector_get_size(tw), argv[1].ex_int); + *optr = argv[1]; #endif } @@ -1209,7 +1209,7 @@ pd_error(void *object, char *fmt, ...) /* SDY vsprintf(error_string, fmt, ap); */ post(fmt, ap); - va_end(ap); + va_end(ap); /* SDY fprintf(stderr, "error: %s\n", error_string); error_object = object; diff --git a/pd/extra/fiddle~/fiddle~.c b/pd/extra/fiddle~/fiddle~.c index 594c2ff4..d959b00f 100644 --- a/pd/extra/fiddle~/fiddle~.c +++ b/pd/extra/fiddle~/fiddle~.c @@ -102,15 +102,10 @@ static fts_symbol_t *dsp_symbol = 0; #endif /* MSP */ #ifdef MSP -#define t_floatarg double // this is a guess based on MAX26 +#define t_floatarg double #include "ext.h" #include "z_dsp.h" #include "fft_mayer.proto.h" -//#include "fiddle_header.h" -// #include "MacHeaders.h" -//#include - -//#include "fiddledoit.h" #endif /* MSP */ @@ -129,25 +124,25 @@ static fts_symbol_t *dsp_symbol = 0; #define MINFREQINBINS 5 /* minimum frequency in bins for reliable output */ #define MAXNPITCH 3 -#define MAXHIST 3 /* find N hottest peaks in histogram */ +#define MAXHIST 3 /* find N hottest peaks in histogram */ #define MAXPOINTS 8192 #define MINPOINTS 128 #define DEFAULTPOINTS 1024 #define HISTORY 20 -#define MAXPEAK 100 /* maximum number of peaks */ -#define DEFNPEAK 20 /* default number of peaks */ +#define MAXPEAK 100 /* maximum number of peaks */ +#define DEFNPEAK 20 /* default number of peaks */ #define MAXNPEAK (MAXLOWPEAK + MAXSTRONGPEAK) -#define MINBW (0.03f) /* consider BW >= 0.03 FFT bins */ +#define MINBW (0.03f) /* consider BW >= 0.03 FFT bins */ -#define BINPEROCT 48 /* bins per octave */ -#define BPERO_OVER_LOG2 69.24936196f /* BINSPEROCT/log(2) */ -#define FACTORTOBINS (float)(4/0.0145453) /* 4 / (pow(2.,1/48.) - 1) */ -#define BINGUARD 10 /* extra bins to throw in front */ -#define PARTIALDEVIANCE 0.023f /* acceptable partial detuning in % */ -#define LOGTODB 4.34294481903f /* 20/log(10) */ +#define BINPEROCT 48 /* bins per octave */ +#define BPERO_OVER_LOG2 69.24936196f /* BINSPEROCT/log(2) */ +#define FACTORTOBINS (float)(4/0.0145453) /* 4 / (pow(2.,1/48.) - 1) */ +#define BINGUARD 10 /* extra bins to throw in front */ +#define PARTIALDEVIANCE 0.023f /* acceptable partial detuning in % */ +#define LOGTODB 4.34294481903f /* 20/log(10) */ #define KNOCKTHRESH 10.f /* don't know how to describe this */ @@ -207,79 +202,79 @@ sample rate. I got these by trial and error. */ #define FILT5 ((float)(.5 * 0.002533)) #define FILTSIZE 5 -typedef struct peakout /* a peak for output */ +typedef struct peakout /* a peak for output */ { - float po_freq; /* frequency in hz */ - float po_amp; /* amplitude */ + float po_freq; /* frequency in hz */ + float po_amp; /* amplitude */ } t_peakout; -typedef struct peak /* a peak for analysis */ +typedef struct peak /* a peak for analysis */ { - float p_freq; /* frequency in bins */ - float p_width; /* peak width in bins */ - float p_pow; /* peak power */ - float p_loudness; /* 4th root of power */ - float *p_fp; /* pointer back to spectrum */ + float p_freq; /* frequency in bins */ + float p_width; /* peak width in bins */ + float p_pow; /* peak power */ + float p_loudness; /* 4th root of power */ + float *p_fp; /* pointer back to spectrum */ } t_peak; typedef struct histopeak { - float h_pitch; /* estimated pitch */ - float h_value; /* value of peak */ - float h_loud; /* combined strength of found partials */ - int h_index; /* index of bin holding peak */ - int h_used; /* true if an x_hist entry points here */ + float h_pitch; /* estimated pitch */ + float h_value; /* value of peak */ + float h_loud; /* combined strength of found partials */ + int h_index; /* index of bin holding peak */ + int h_used; /* true if an x_hist entry points here */ } t_histopeak; -typedef struct pitchhist /* struct for keeping history by pitch */ +typedef struct pitchhist /* struct for keeping history by pitch */ { - float h_pitch; /* pitch to output */ - float h_amps[HISTORY]; /* past amplitudes */ - float h_pitches[HISTORY]; /* past pitches */ - float h_noted; /* last pitch output */ - int h_age; /* number of frames pitch has been there */ - t_histopeak *h_wherefrom; /* new histogram peak to incorporate */ + float h_pitch; /* pitch to output */ + float h_amps[HISTORY]; /* past amplitudes */ + float h_pitches[HISTORY]; /* past pitches */ + float h_noted; /* last pitch output */ + int h_age; /* number of frames pitch has been there */ + t_histopeak *h_wherefrom; /* new histogram peak to incorporate */ void *h_outlet; } t_pitchhist; -typedef struct sigfiddle /* instance struct */ +typedef struct sigfiddle /* instance struct */ { #ifdef JMAX - fts_object_t x_h; /* object header */ - fts_alarm_t x_clock; /* callback for timeouts */ + fts_object_t x_h; /* object header */ + fts_alarm_t x_clock; /* callback for timeouts */ #endif #ifdef MAX26 - t_head x_h; /* header for tilde objects */ - t_sig *x_io[IN1+OUT0]; /* number of signal inputs and outputs */ - void *x_clock; /* a "clock" object */ + t_head x_h; /* header for tilde objects */ + t_sig *x_io[IN1+OUT0]; /* number of signal inputs and outputs */ + void *x_clock; /* a "clock" object */ #endif #ifdef PD - t_object x_ob; /* object header */ - t_clock *x_clock; /* callback for timeouts */ + t_object x_ob; /* object header */ + t_clock *x_clock; /* callback for timeouts */ #endif #ifdef MSP - t_pxobject x_obj; - void *x_clock; - long x_downsample; // downsample feature because of - // MSP's large sig vector sizes + t_pxobject x_obj; + void *x_clock; + long x_downsample; /* downsample feature because of + MSP's large sig vector sizes */ #endif - float *x_inbuf; /* buffer to analyze, npoints/2 elems */ - float *x_lastanalysis; /* FT of last buffer (see main comment) */ - float *x_spiral; /* 1/4-wave complex exponential */ - t_peakout *x_peakbuf; /* spectral peaks for output */ - int x_npeakout; /* number of spectral peaks to output */ - int x_npeakanal; /* number of spectral peaks to analyze */ - int x_phase; /* number of points since last output */ - int x_histphase; /* phase into amplitude history vector */ - int x_hop; /* period of output, npoints/2 */ - float x_sr; /* sample rate */ + float *x_inbuf; /* buffer to analyze, npoints/2 elems */ + float *x_lastanalysis; /* FT of last buffer (see main comment) */ + float *x_spiral; /* 1/4-wave complex exponential */ + t_peakout *x_peakbuf; /* spectral peaks for output */ + int x_npeakout; /* number of spectral peaks to output */ + int x_npeakanal; /* number of spectral peaks to analyze */ + int x_phase; /* number of points since last output */ + int x_histphase; /* phase into amplitude history vector */ + int x_hop; /* period of output, npoints/2 */ + float x_sr; /* sample rate */ t_pitchhist x_hist[MAXNPITCH]; /* history of current pitches */ - int x_nprint; /* how many periods to print */ - int x_npitch; /* number of simultaneous pitches */ - float x_dbs[HISTORY]; /* DB history, indexed by "histphase" */ - float x_peaked; /* peak since last attack */ - int x_dbage; /* number of bins DB has met threshold */ - int x_auto; /* true if generating continuous output */ + int x_nprint; /* how many periods to print */ + int x_npitch; /* number of simultaneous pitches */ + float x_dbs[HISTORY]; /* DB history, indexed by "histphase" */ + float x_peaked; /* peak since last attack */ + int x_dbage; /* number of bins DB has met threshold */ + int x_auto; /* true if generating continuous output */ /* parameters */ float x_amplo; float x_amphi; @@ -303,7 +298,7 @@ float fiddle_checker[1024]; #endif #ifdef MSP -// Mac compiler requires prototypes for everything +/* Mac compiler requires prototypes for everything */ int sigfiddle_ilog2(int n); float fiddle_mtof(float f); @@ -327,7 +322,6 @@ void sigfiddle_dsp(t_sigfiddle *x, t_signal **sp); void sigfiddle_tick(t_sigfiddle *x); void sigfiddle_bang(t_sigfiddle *x); void sigfiddle_ff(t_sigfiddle *x); -//void *sigfiddle_new(long npoints, long npitch); void *sigfiddle_new(long npoints, long npitch, long npeakanal, long npeakout); void msp_fft(float *buf, long np, long inv); @@ -340,20 +334,20 @@ int sigfiddle_ilog2(int n) int ret = -1; while (n) { - n >>= 1; - ret++; + n >>= 1; + ret++; } return (ret); } float fiddle_mtof(float f) { - return (8.17579891564 * exp(.0577622650 * f)); + return (8.17579891564 * exp(.0577622650 * f)); } float fiddle_ftom(float f) { - return (17.3123405046 * log(.12231220585 * f)); + return (17.3123405046 * log(.12231220585 * f)); } #define ftom fiddle_ftom #define mtof fiddle_mtof @@ -361,7 +355,7 @@ float fiddle_ftom(float f) void sigfiddle_doit(t_sigfiddle *x) { #ifdef MSP - // prevents interrupt-level stack overflow crash with Netscape. + /* prevents interrupt-level stack overflow crash with Netscape. */ static float spect1[4*MAXPOINTS]; static float spect2[MAXPOINTS + 4*FILTSIZE]; #else @@ -389,13 +383,13 @@ void sigfiddle_doit(t_sigfiddle *x) if (newphase == HISTORY) newphase = 0; x->x_histphase = newphase; - /* - * multiply the H points by a 1/4-wave complex exponential, - * and take FFT of the result. - */ + /* + * multiply the H points by a 1/4-wave complex exponential, + * and take FFT of the result. + */ for (i = 0, fp1 = x->x_inbuf, fp2 = x->x_spiral, fp3 = spect1; - i < hop; i++, fp1++, fp2 += 2, fp3 += 2) - fp3[0] = fp1[0] * fp2[0], fp3[1] = fp1[0] * fp2[1]; + i < hop; i++, fp1++, fp2 += 2, fp3 += 2) + fp3[0] = fp1[0] * fp2[0], fp3[1] = fp1[0] * fp2[1]; #ifdef MAX26 fft(spect1, hop, 0); @@ -407,123 +401,123 @@ void sigfiddle_doit(t_sigfiddle *x) fts_cfft_inplc((complex *)spect1, hop); #endif #ifdef MSP - msp_fft(spect1,hop,0); + msp_fft(spect1,hop,0); #endif - /* - * now redistribute the points to get in effect the odd-numbered - * points of the FFT of the H points, zero padded to 4*H in length. - */ + /* + * now redistribute the points to get in effect the odd-numbered + * points of the FFT of the H points, zero padded to 4*H in length. + */ for (i = 0, fp1 = spect1, fp2 = spect2 + (2*FILTSIZE); - i < (hop>>1); i++, fp1 += 2, fp2 += 4) - fp2[0] = fp1[0], fp2[1] = fp1[1]; + i < (hop>>1); i++, fp1 += 2, fp2 += 4) + fp2[0] = fp1[0], fp2[1] = fp1[1]; for (i = 0, fp1 = spect1 + n - 2, fp2 = spect2 + (2*FILTSIZE+2); - i < (hop>>1); i++, fp1 -= 2, fp2 += 4) - fp2[0] = fp1[0], fp2[1] = -fp1[1]; + i < (hop>>1); i++, fp1 -= 2, fp2 += 4) + fp2[0] = fp1[0], fp2[1] = -fp1[1]; for (i = 0, fp1 = spect2 + (2*FILTSIZE), fp2 = spect2 + (2*FILTSIZE-2); - ix_lastanalysis + 2*FILTSIZE, - fp3 = spect2 + 2*FILTSIZE; - i < (hop>>1); i++) + fp3 = spect2 + 2*FILTSIZE; + i < (hop>>1); i++) { - float re, im; - - re= FILT1 * ( fp2[ -2] -fp2[ 1] +fp3[ -2] -fp3[ 1]) + - FILT2 * ( fp2[ -3] -fp2[ 2] +fp3[ -3] -fp3[ 2]) + - FILT3 * (-fp2[ -6] +fp2[ 5] -fp3[ -6] +fp3[ 5]) + - FILT4 * (-fp2[ -7] +fp2[ 6] -fp3[ -7] +fp3[ 6]) + - FILT5 * ( fp2[-10] -fp2[ 9] +fp3[-10] -fp3[ 9]); - - im= FILT1 * ( fp2[ -1] +fp2[ 0] +fp3[ -1] +fp3[ 0]) + - FILT2 * (-fp2[ -4] -fp2[ 3] -fp3[ -4] -fp3[ 3]) + - FILT3 * (-fp2[ -5] -fp2[ 4] -fp3[ -5] -fp3[ 4]) + - FILT4 * ( fp2[ -8] +fp2[ 7] +fp3[ -8] +fp3[ 7]) + - FILT5 * ( fp2[ -9] +fp2[ 8] +fp3[ -9] +fp3[ 8]); - - fp1[0] = 0.7071f * (re + im); - fp1[1] = 0.7071f * (im - re); - fp1[4] = fp2[0] + fp3[1]; - fp1[5] = fp2[1] - fp3[0]; - - fp1 += 8, fp2 += 2, fp3 += 2; - re= FILT1 * ( fp2[ -2] -fp2[ 1] -fp3[ -2] +fp3[ 1]) + - FILT2 * ( fp2[ -3] -fp2[ 2] -fp3[ -3] +fp3[ 2]) + - FILT3 * (-fp2[ -6] +fp2[ 5] +fp3[ -6] -fp3[ 5]) + - FILT4 * (-fp2[ -7] +fp2[ 6] +fp3[ -7] -fp3[ 6]) + - FILT5 * ( fp2[-10] -fp2[ 9] -fp3[-10] +fp3[ 9]); - - im= FILT1 * ( fp2[ -1] +fp2[ 0] -fp3[ -1] -fp3[ 0]) + - FILT2 * (-fp2[ -4] -fp2[ 3] +fp3[ -4] +fp3[ 3]) + - FILT3 * (-fp2[ -5] -fp2[ 4] +fp3[ -5] +fp3[ 4]) + - FILT4 * ( fp2[ -8] +fp2[ 7] -fp3[ -8] -fp3[ 7]) + - FILT5 * ( fp2[ -9] +fp2[ 8] -fp3[ -9] -fp3[ 8]); - - fp1[0] = 0.7071f * (re + im); - fp1[1] = 0.7071f * (im - re); - fp1[4] = fp2[0] - fp3[1]; - fp1[5] = fp2[1] + fp3[0]; - - fp1 += 8, fp2 += 2, fp3 += 2; + float re, im; + + re= FILT1 * ( fp2[ -2] -fp2[ 1] +fp3[ -2] -fp3[ 1]) + + FILT2 * ( fp2[ -3] -fp2[ 2] +fp3[ -3] -fp3[ 2]) + + FILT3 * (-fp2[ -6] +fp2[ 5] -fp3[ -6] +fp3[ 5]) + + FILT4 * (-fp2[ -7] +fp2[ 6] -fp3[ -7] +fp3[ 6]) + + FILT5 * ( fp2[-10] -fp2[ 9] +fp3[-10] -fp3[ 9]); + + im= FILT1 * ( fp2[ -1] +fp2[ 0] +fp3[ -1] +fp3[ 0]) + + FILT2 * (-fp2[ -4] -fp2[ 3] -fp3[ -4] -fp3[ 3]) + + FILT3 * (-fp2[ -5] -fp2[ 4] -fp3[ -5] -fp3[ 4]) + + FILT4 * ( fp2[ -8] +fp2[ 7] +fp3[ -8] +fp3[ 7]) + + FILT5 * ( fp2[ -9] +fp2[ 8] +fp3[ -9] +fp3[ 8]); + + fp1[0] = 0.7071f * (re + im); + fp1[1] = 0.7071f * (im - re); + fp1[4] = fp2[0] + fp3[1]; + fp1[5] = fp2[1] - fp3[0]; + + fp1 += 8, fp2 += 2, fp3 += 2; + re= FILT1 * ( fp2[ -2] -fp2[ 1] -fp3[ -2] +fp3[ 1]) + + FILT2 * ( fp2[ -3] -fp2[ 2] -fp3[ -3] +fp3[ 2]) + + FILT3 * (-fp2[ -6] +fp2[ 5] +fp3[ -6] -fp3[ 5]) + + FILT4 * (-fp2[ -7] +fp2[ 6] +fp3[ -7] -fp3[ 6]) + + FILT5 * ( fp2[-10] -fp2[ 9] -fp3[-10] +fp3[ 9]); + + im= FILT1 * ( fp2[ -1] +fp2[ 0] -fp3[ -1] -fp3[ 0]) + + FILT2 * (-fp2[ -4] -fp2[ 3] +fp3[ -4] +fp3[ 3]) + + FILT3 * (-fp2[ -5] -fp2[ 4] +fp3[ -5] +fp3[ 4]) + + FILT4 * ( fp2[ -8] +fp2[ 7] -fp3[ -8] -fp3[ 7]) + + FILT5 * ( fp2[ -9] +fp2[ 8] -fp3[ -9] -fp3[ 8]); + + fp1[0] = 0.7071f * (re + im); + fp1[1] = 0.7071f * (im - re); + fp1[4] = fp2[0] - fp3[1]; + fp1[5] = fp2[1] + fp3[0]; + + fp1 += 8, fp2 += 2, fp3 += 2; } #if 0 if (x->x_nprint) { - for (i = 0, fp = spect1; i < 16; i++, fp+= 4) - post("spect %d %f %f --> %f", i, fp[0], fp[1], - sqrt(fp[0] * fp[0] + fp[1] * fp[1])); + for (i = 0, fp = spect1; i < 16; i++, fp+= 4) + post("spect %d %f %f --> %f", i, fp[0], fp[1], + sqrt(fp[0] * fp[0] + fp[1] * fp[1])); } #endif - /* copy new spectrum out */ + /* copy new spectrum out */ for (i = 0, fp1 = spect2, fp2 = x->x_lastanalysis; - i < n + 4*FILTSIZE; i++) *fp2++ = *fp1++; + i < n + 4*FILTSIZE; i++) *fp2++ = *fp1++; for (i = 0; i < MINBIN; i++) spect1[4*i + 2] = spect1[4*i + 3] = 0; - /* starting at bin MINBIN, compute hanning windowed power spectrum */ + /* starting at bin MINBIN, compute hanning windowed power spectrum */ for (i = MINBIN, fp1 = spect1+4*MINBIN, total_power = 0; - i < n-2; i++, fp1 += 4) + i < n-2; i++, fp1 += 4) { - float re = fp1[0] - 0.5f * (fp1[-8] + fp1[8]); - float im = fp1[1] - 0.5f * (fp1[-7] + fp1[9]); - fp1[3] = (total_power += (fp1[2] = re * re + im * im)); + float re = fp1[0] - 0.5f * (fp1[-8] + fp1[8]); + float im = fp1[1] - 0.5f * (fp1[-7] + fp1[9]); + fp1[3] = (total_power += (fp1[2] = re * re + im * im)); } if (total_power > 1e-9f) { - total_db = (100.f - DBFUDGE) + LOGTODB * log(total_power/n); - total_loudness = fsqrt(fsqrt(total_power)); - if (total_db < 0) total_db = 0; + total_db = (100.f - DBFUDGE) + LOGTODB * log(total_power/n); + total_loudness = fsqrt(fsqrt(total_power)); + if (total_db < 0) total_db = 0; } else total_db = total_loudness = 0; - /* store new db in history vector */ + /* store new db in history vector */ x->x_dbs[newphase] = total_db; if (total_db < x->x_amplo) goto nopow; #if 1 @@ -531,467 +525,467 @@ void sigfiddle_doit(t_sigfiddle *x) #endif #if CHECKER - /* verify that our FFT resampling thing is putting out good results */ + /* verify that our FFT resampling thing is putting out good results */ for (i = 0; i < hop; i++) { - checker3[2*i] = fiddle_checker[i]; - checker3[2*i + 1] = 0; - checker3[n + 2*i] = fiddle_checker[i] = x->x_inbuf[i]; - checker3[n + 2*i + 1] = 0; + checker3[2*i] = fiddle_checker[i]; + checker3[2*i + 1] = 0; + checker3[n + 2*i] = fiddle_checker[i] = x->x_inbuf[i]; + checker3[n + 2*i + 1] = 0; } for (i = 2*n; i < 4*n; i++) checker3[i] = 0; fft(checker3, 2*n, 0); if (x->x_nprint) { - for (i = 0, fp = checker3; i < 16; i++, fp += 2) - post("spect %d %f %f --> %f", i, fp[0], fp[1], - sqrt(fp[0] * fp[0] + fp[1] * fp[1])); + for (i = 0, fp = checker3; i < 16; i++, fp += 2) + post("spect %d %f %f --> %f", i, fp[0], fp[1], + sqrt(fp[0] * fp[0] + fp[1] * fp[1])); } #endif npeak = 0; - /* search for peaks */ + /* search for peaks */ for (i = MINBIN, fp = spect1+4*MINBIN, pk1 = peaklist; - i < n-2 && npeak < npeaktot; i++, fp += 4) + i < n-2 && npeak < npeaktot; i++, fp += 4) { - float height = fp[2], h1 = fp[-2], h2 = fp[6]; - float totalfreq, pfreq, f1, f2, m, var, stdev; - - if (height < h1 || height < h2 || - h1 < 0.00001f*total_power || h2 < 0.00001f*total_power) - continue; - - /* use an informal phase vocoder to estimate the frequency. - Do this for the two adjacent bins too. */ - pfreq= ((fp[-8] - fp[8]) * (2.0f * fp[0] - fp[8] - fp[-8]) + - (fp[-7] - fp[9]) * (2.0f * fp[1] - fp[9] - fp[-7])) / - (2.0f * height); - f1= ((fp[-12] - fp[4]) * (2.0f * fp[-4] - fp[4] - fp[-12]) + - (fp[-11] - fp[5]) * (2.0f * fp[-3] - fp[5] - fp[-11])) / - (2.0f * h1) - 1; - f2= ((fp[-4] - fp[12]) * (2.0f * fp[4] - fp[12] - fp[-4]) + - (fp[-3] - fp[13]) * (2.0f * fp[5] - fp[13] - fp[-3])) / - (2.0f * h2) + 1; - - /* get sample mean and variance of the three */ - m = 0.333333f * (pfreq + f1 + f2); - var = 0.5f * ((pfreq-m)*(pfreq-m) + (f1-m)*(f1-m) + (f2-m)*(f2-m)); - - totalfreq = i + m; - if (var * total_power > KNOCKTHRESH * height || var < 1e-30) - { + float height = fp[2], h1 = fp[-2], h2 = fp[6]; + float totalfreq, pfreq, f1, f2, m, var, stdev; + + if (height < h1 || height < h2 || + h1 < 0.00001f*total_power || h2 < 0.00001f*total_power) + continue; + + /* use an informal phase vocoder to estimate the frequency. + Do this for the two adjacent bins too. */ + pfreq= ((fp[-8] - fp[8]) * (2.0f * fp[0] - fp[8] - fp[-8]) + + (fp[-7] - fp[9]) * (2.0f * fp[1] - fp[9] - fp[-7])) / + (2.0f * height); + f1= ((fp[-12] - fp[4]) * (2.0f * fp[-4] - fp[4] - fp[-12]) + + (fp[-11] - fp[5]) * (2.0f * fp[-3] - fp[5] - fp[-11])) / + (2.0f * h1) - 1; + f2= ((fp[-4] - fp[12]) * (2.0f * fp[4] - fp[12] - fp[-4]) + + (fp[-3] - fp[13]) * (2.0f * fp[5] - fp[13] - fp[-3])) / + (2.0f * h2) + 1; + + /* get sample mean and variance of the three */ + m = 0.333333f * (pfreq + f1 + f2); + var = 0.5f * ((pfreq-m)*(pfreq-m) + (f1-m)*(f1-m) + (f2-m)*(f2-m)); + + totalfreq = i + m; + if (var * total_power > KNOCKTHRESH * height || var < 1e-30) + { #if 0 - if (x->x_nprint) - post("cancel: %.2f hz, index %.1f, power %.5f, stdev=%.2f", - totalfreq * hzperbin, BPERO_OVER_LOG2 * log(totalfreq) - 96, - height, sqrt(var)); + if (x->x_nprint) + post("cancel: %.2f hz, index %.1f, power %.5f, stdev=%.2f", + totalfreq * hzperbin, BPERO_OVER_LOG2 * log(totalfreq) - 96, + height, sqrt(var)); #endif - continue; - } - stdev = fsqrt(var); - if (totalfreq < 4) - { - if (x->x_nprint) post("oops: was %d, freq %f, m %f, stdev %f h %f", - i, totalfreq, m, stdev, height); - totalfreq = 4; - } - pk1->p_width = stdev; - - pk1->p_pow = height; - pk1->p_loudness = fsqrt(fsqrt(height)); - pk1->p_fp = fp; - pk1->p_freq = totalfreq; - npeak++; + continue; + } + stdev = fsqrt(var); + if (totalfreq < 4) + { + if (x->x_nprint) post("oops: was %d, freq %f, m %f, stdev %f h %f", + i, totalfreq, m, stdev, height); + totalfreq = 4; + } + pk1->p_width = stdev; + + pk1->p_pow = height; + pk1->p_loudness = fsqrt(fsqrt(height)); + pk1->p_fp = fp; + pk1->p_freq = totalfreq; + npeak++; #if 1 - if (x->x_nprint) - { - post("peak: %.2f hz. index %.1f, power %.5f, stdev=%.2f", - pk1->p_freq * hzperbin, - BPERO_OVER_LOG2 * log(pk1->p_freq) - 96, - height, stdev); - } + if (x->x_nprint) + { + post("peak: %.2f hz. index %.1f, power %.5f, stdev=%.2f", + pk1->p_freq * hzperbin, + BPERO_OVER_LOG2 * log(pk1->p_freq) - 96, + height, stdev); + } #endif - pk1++; + pk1++; } - /* prepare the raw peaks for output */ + /* prepare the raw peaks for output */ for (i = 0, pk1 = peaklist, pk2 = x->x_peakbuf; i < npeak; - i++, pk1++, pk2++) + i++, pk1++, pk2++) { - float loudness = pk1->p_loudness; - if (i >= npeakout) break; - pk2->po_freq = hzperbin * pk1->p_freq; - pk2->po_amp = (2.f / (float)n) * (loudness * loudness); + float loudness = pk1->p_loudness; + if (i >= npeakout) break; + pk2->po_freq = hzperbin * pk1->p_freq; + pk2->po_amp = (2.f / (float)n) * (loudness * loudness); } for (; i < npeakout; i++, pk2++) pk2->po_amp = pk2->po_freq = 0; - /* now, working back into spect2, make a sort of "liklihood" - * spectrum. Proceeding in 48ths of an octave, from 2 to - * n/2 (in bins), the likelihood of each pitch range is contributed - * to by every peak in peaklist that's an integer multiple of it - * in frequency. - */ + /* now, working back into spect2, make a sort of "liklihood" + * spectrum. Proceeding in 48ths of an octave, from 2 to + * n/2 (in bins), the likelihood of each pitch range is contributed + * to by every peak in peaklist that's an integer multiple of it + * in frequency. + */ if (npeak > npeakanal) npeak = npeakanal; /* max # peaks to analyze */ for (i = 0, fp1 = histogram; i < maxbin; i++) *fp1++ = 0; for (i = 0, pk1 = peaklist; i < npeak; i++, pk1++) { - float pit = BPERO_OVER_LOG2 * flog(pk1->p_freq) - 96.0f; - float binbandwidth = FACTORTOBINS * pk1->p_width/pk1->p_freq; - float putbandwidth = (binbandwidth < 2 ? 2 : binbandwidth); - float weightbandwidth = (binbandwidth < 1.0f ? 1.0f : binbandwidth); - /* float weightamp = 1.0f + 3.0f * pk1->p_pow / pow; */ - float weightamp = 4. * pk1->p_loudness / total_loudness; - for (j = 0, fp2 = sigfiddle_partialonset; j < NPARTIALONSET; j++, fp2++) - { - float bin = pit - *fp2; - if (bin < maxbin) - { - float para, pphase, score = 30.0f * weightamp / - ((j+x->x_npartial) * weightbandwidth); - int firstbin = bin + 0.5f - 0.5f * putbandwidth; - int lastbin = bin + 0.5f + 0.5f * putbandwidth; - int ibw = lastbin - firstbin; - if (firstbin < -BINGUARD) break; - para = 1.0f / (putbandwidth * putbandwidth); - for (k = 0, fp3 = histogram + firstbin, - pphase = firstbin-bin; k <= ibw; - k++, fp3++, pphase += 1.0f) - { - *fp3 += score * (1.0f - para * pphase * pphase); - } - } - } + float pit = BPERO_OVER_LOG2 * flog(pk1->p_freq) - 96.0f; + float binbandwidth = FACTORTOBINS * pk1->p_width/pk1->p_freq; + float putbandwidth = (binbandwidth < 2 ? 2 : binbandwidth); + float weightbandwidth = (binbandwidth < 1.0f ? 1.0f : binbandwidth); + /* float weightamp = 1.0f + 3.0f * pk1->p_pow / pow; */ + float weightamp = 4. * pk1->p_loudness / total_loudness; + for (j = 0, fp2 = sigfiddle_partialonset; j < NPARTIALONSET; j++, fp2++) + { + float bin = pit - *fp2; + if (bin < maxbin) + { + float para, pphase, score = 30.0f * weightamp / + ((j+x->x_npartial) * weightbandwidth); + int firstbin = bin + 0.5f - 0.5f * putbandwidth; + int lastbin = bin + 0.5f + 0.5f * putbandwidth; + int ibw = lastbin - firstbin; + if (firstbin < -BINGUARD) break; + para = 1.0f / (putbandwidth * putbandwidth); + for (k = 0, fp3 = histogram + firstbin, + pphase = firstbin-bin; k <= ibw; + k++, fp3++, pphase += 1.0f) + { + *fp3 += score * (1.0f - para * pphase * pphase); + } + } + } } #if 1 if (x->x_nprint) { - for (i = 0; i < 6*5; i++) - { - float fhz = hzperbin * exp ((8*i + 96) * (1./BPERO_OVER_LOG2)); - if (!(i % 6)) post("-- bin %d pitch %f freq %f----", 8*i, - ftom(fhz), fhz);; - post("%3d %3d %3d %3d %3d %3d %3d %3d", - (int)(histogram[8*i]), - (int)(histogram[8*i+1]), - (int)(histogram[8*i+2]), - (int)(histogram[8*i+3]), - (int)(histogram[8*i+4]), - (int)(histogram[8*i+5]), - (int)(histogram[8*i+6]), - (int)(histogram[8*i+7])); - } + for (i = 0; i < 6*5; i++) + { + float fhz = hzperbin * exp ((8*i + 96) * (1./BPERO_OVER_LOG2)); + if (!(i % 6)) post("-- bin %d pitch %f freq %f----", 8*i, + ftom(fhz), fhz);; + post("%3d %3d %3d %3d %3d %3d %3d %3d", + (int)(histogram[8*i]), + (int)(histogram[8*i+1]), + (int)(histogram[8*i+2]), + (int)(histogram[8*i+3]), + (int)(histogram[8*i+4]), + (int)(histogram[8*i+5]), + (int)(histogram[8*i+6]), + (int)(histogram[8*i+7])); + } } #endif - /* - * Next we find up to NPITCH strongest peaks in the histogram. - * if a peak is related to a stronger one via an interval in - * the sigfiddle_partialonset array, we suppress it. - */ + /* + * Next we find up to NPITCH strongest peaks in the histogram. + * if a peak is related to a stronger one via an interval in + * the sigfiddle_partialonset array, we suppress it. + */ for (npitch = 0; npitch < x->x_npitch; npitch++) { - int indx; - float best; - if (npitch) - { - for (best = 0, indx = -1, j=1; j < maxbin-1; j++) - { - if (histogram[j] > best && histogram[j] > histogram[j-1] && - histogram[j] > histogram[j+1]) - { - for (k = 0; k < npitch; k++) - if (histvec[k].h_index == j) - goto peaknogood; - for (k = 0; k < NPARTIALONSET; k++) - { - if (j - sigfiddle_intpartialonset[k] < 0) break; - if (histogram[j - sigfiddle_intpartialonset[k]] - > histogram[j]) goto peaknogood; - } - for (k = 0; k < NPARTIALONSET; k++) - { - if (j + sigfiddle_intpartialonset[k] >= maxbin) break; - if (histogram[j + sigfiddle_intpartialonset[k]] - > histogram[j]) goto peaknogood; - } - indx = j; - best = histogram[j]; - } - peaknogood: ; - } - } - else - { - for (best = 0, indx = -1, j=0; j < maxbin; j++) - if (histogram[j] > best) - indx = j, best = histogram[j]; - } - if (indx < 0) break; - histvec[npitch].h_value = best; - histvec[npitch].h_index = indx; + int indx; + float best; + if (npitch) + { + for (best = 0, indx = -1, j=1; j < maxbin-1; j++) + { + if (histogram[j] > best && histogram[j] > histogram[j-1] && + histogram[j] > histogram[j+1]) + { + for (k = 0; k < npitch; k++) + if (histvec[k].h_index == j) + goto peaknogood; + for (k = 0; k < NPARTIALONSET; k++) + { + if (j - sigfiddle_intpartialonset[k] < 0) break; + if (histogram[j - sigfiddle_intpartialonset[k]] + > histogram[j]) goto peaknogood; + } + for (k = 0; k < NPARTIALONSET; k++) + { + if (j + sigfiddle_intpartialonset[k] >= maxbin) break; + if (histogram[j + sigfiddle_intpartialonset[k]] + > histogram[j]) goto peaknogood; + } + indx = j; + best = histogram[j]; + } + peaknogood: ; + } + } + else + { + for (best = 0, indx = -1, j=0; j < maxbin; j++) + if (histogram[j] > best) + indx = j, best = histogram[j]; + } + if (indx < 0) break; + histvec[npitch].h_value = best; + histvec[npitch].h_index = indx; } #if 1 if (x->x_nprint) { - for (i = 0; i < npitch; i++) - { - post("index %d freq %f --> value %f", histvec[i].h_index, - exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 96)), - histvec[i].h_value); - post("next %f , prev %f", - exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 97)), - exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 95)) ); - } + for (i = 0; i < npitch; i++) + { + post("index %d freq %f --> value %f", histvec[i].h_index, + exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 96)), + histvec[i].h_value); + post("next %f , prev %f", + exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 97)), + exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 95)) ); + } } #endif - /* for each histogram peak, we now search back through the - * FFT peaks. A peak is a pitch if either there are several - * harmonics that match it, or else if (a) the fundamental is - * present, and (b) the sum of the powers of the contributing peaks - * is at least 1/100 of the total power. - * - * A peak is a contributor if its frequency is within 25 cents of - * a partial from 1 to 16. - * - * Finally, we have to be at least 5 bins in frequency, which - * corresponds to 2-1/5 periods fitting in the analysis window. - */ + /* for each histogram peak, we now search back through the + * FFT peaks. A peak is a pitch if either there are several + * harmonics that match it, or else if (a) the fundamental is + * present, and (b) the sum of the powers of the contributing peaks + * is at least 1/100 of the total power. + * + * A peak is a contributor if its frequency is within 25 cents of + * a partial from 1 to 16. + * + * Finally, we have to be at least 5 bins in frequency, which + * corresponds to 2-1/5 periods fitting in the analysis window. + */ for (i = 0; i < npitch; i++) { - float cumpow = 0, cumstrength = 0, freqnum = 0, freqden = 0; - int npartials = 0, nbelow8 = 0; - /* guessed-at frequency in bins */ - float putfreq = fexp((1.0f / BPERO_OVER_LOG2) * - (histvec[i].h_index + 96.0f)); - for (j = 0; j < npeak; j++) - { - float fpnum = peaklist[j].p_freq/putfreq; - int pnum = fpnum + 0.5f; - float fipnum = pnum; - float deviation; - if (pnum > 16 || pnum < 1) continue; - deviation = 1.0f - fpnum/fipnum; - if (deviation > -PARTIALDEVIANCE && deviation < PARTIALDEVIANCE) - { - /* - * we figure this is a partial since it's within 1/4 of - * a halftone of a multiple of the putative frequency. - */ - - float stdev, weight; - npartials++; - if (pnum < 8) nbelow8++; - cumpow += peaklist[j].p_pow; - cumstrength += fsqrt(fsqrt(peaklist[j].p_pow)); - stdev = (peaklist[j].p_width > MINBW ? - peaklist[j].p_width : MINBW); - weight = 1.0f / ((stdev*fipnum) * (stdev*fipnum)); - freqden += weight; - freqnum += weight * peaklist[j].p_freq/fipnum; + float cumpow = 0, cumstrength = 0, freqnum = 0, freqden = 0; + int npartials = 0, nbelow8 = 0; + /* guessed-at frequency in bins */ + float putfreq = fexp((1.0f / BPERO_OVER_LOG2) * + (histvec[i].h_index + 96.0f)); + for (j = 0; j < npeak; j++) + { + float fpnum = peaklist[j].p_freq/putfreq; + int pnum = fpnum + 0.5f; + float fipnum = pnum; + float deviation; + if (pnum > 16 || pnum < 1) continue; + deviation = 1.0f - fpnum/fipnum; + if (deviation > -PARTIALDEVIANCE && deviation < PARTIALDEVIANCE) + { + /* + * we figure this is a partial since it's within 1/4 of + * a halftone of a multiple of the putative frequency. + */ + + float stdev, weight; + npartials++; + if (pnum < 8) nbelow8++; + cumpow += peaklist[j].p_pow; + cumstrength += fsqrt(fsqrt(peaklist[j].p_pow)); + stdev = (peaklist[j].p_width > MINBW ? + peaklist[j].p_width : MINBW); + weight = 1.0f / ((stdev*fipnum) * (stdev*fipnum)); + freqden += weight; + freqnum += weight * peaklist[j].p_freq/fipnum; #if 1 - if (x->x_nprint) - { - post("peak %d partial %d f=%f w=%f", - j, pnum, peaklist[j].p_freq/fipnum, weight); - } + if (x->x_nprint) + { + post("peak %d partial %d f=%f w=%f", + j, pnum, peaklist[j].p_freq/fipnum, weight); + } #endif - } + } #if 1 - else if (x->x_nprint) post("peak %d partial %d dev %f", - j, pnum, deviation); + else if (x->x_nprint) post("peak %d partial %d dev %f", + j, pnum, deviation); #endif - } - if ((nbelow8 < 4 || npartials < 7) && cumpow < 0.01f * total_power) - histvec[i].h_value = 0; - else - { - float pitchpow = (cumstrength * cumstrength) * - (cumstrength * cumstrength); - float freqinbins = freqnum/freqden; - /* check for minimum output frequency */ - - if (freqinbins < MINFREQINBINS) - histvec[i].h_value = 0; - else - { - /* we passed all tests... save the values we got */ - histvec[i].h_pitch = ftom(hzperbin * freqnum/freqden); - histvec[i].h_loud = (100.0f -DBFUDGE) + - (LOGTODB) * log(pitchpow/n); - } - } + } + if ((nbelow8 < 4 || npartials < 7) && cumpow < 0.01f * total_power) + histvec[i].h_value = 0; + else + { + float pitchpow = (cumstrength * cumstrength) * + (cumstrength * cumstrength); + float freqinbins = freqnum/freqden; + /* check for minimum output frequency */ + + if (freqinbins < MINFREQINBINS) + histvec[i].h_value = 0; + else + { + /* we passed all tests... save the values we got */ + histvec[i].h_pitch = ftom(hzperbin * freqnum/freqden); + histvec[i].h_loud = (100.0f -DBFUDGE) + + (LOGTODB) * log(pitchpow/n); + } + } } #if 1 if (x->x_nprint) { - for (i = 0; i < npitch; i++) - { - if (histvec[i].h_value > 0) - post("index %d pit %f loud %f", histvec[i].h_index, - histvec[i].h_pitch, histvec[i].h_loud); - else post("-- cancelled --"); - } + for (i = 0; i < npitch; i++) + { + if (histvec[i].h_value > 0) + post("index %d pit %f loud %f", histvec[i].h_index, + histvec[i].h_pitch, histvec[i].h_loud); + else post("-- cancelled --"); + } } #endif - /* now try to find continuous pitch tracks that match the new - * pitches. First mark each peak unmatched. - */ + /* now try to find continuous pitch tracks that match the new + * pitches. First mark each peak unmatched. + */ for (i = 0, hp1 = histvec; i < npitch; i++, hp1++) - hp1->h_used = 0; + hp1->h_used = 0; - /* for each old pitch, try to match a new one to it. */ + /* for each old pitch, try to match a new one to it. */ for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++) { - float thispitch = phist->h_pitches[oldphase]; - phist->h_pitch = 0; /* no output, thanks */ - phist->h_wherefrom = 0; - if (thispitch == 0.0f) continue; - for (j = 0, hp1 = histvec; j < npitch; j++, hp1++) - if ((hp1->h_value > 0) && hp1->h_pitch > thispitch - GLISS - && hp1->h_pitch < thispitch + GLISS) - { - phist->h_wherefrom = hp1; - hp1->h_used = 1; - } + float thispitch = phist->h_pitches[oldphase]; + phist->h_pitch = 0; /* no output, thanks */ + phist->h_wherefrom = 0; + if (thispitch == 0.0f) continue; + for (j = 0, hp1 = histvec; j < npitch; j++, hp1++) + if ((hp1->h_value > 0) && hp1->h_pitch > thispitch - GLISS + && hp1->h_pitch < thispitch + GLISS) + { + phist->h_wherefrom = hp1; + hp1->h_used = 1; + } } for (i = 0, hp1 = histvec; i < npitch; i++, hp1++) - if ((hp1->h_value > 0) && !hp1->h_used) + if ((hp1->h_value > 0) && !hp1->h_used) { - for (j = 0, phist = x->x_hist; j < x->x_npitch; j++, phist++) - if (!phist->h_wherefrom) - { - phist->h_wherefrom = hp1; - phist->h_age = 0; - phist->h_noted = 0; - hp1->h_used = 1; - goto happy; - } - break; + for (j = 0, phist = x->x_hist; j < x->x_npitch; j++, phist++) + if (!phist->h_wherefrom) + { + phist->h_wherefrom = hp1; + phist->h_age = 0; + phist->h_noted = 0; + hp1->h_used = 1; + goto happy; + } + break; happy: ; } - /* copy the pitch info into the history vector */ + /* copy the pitch info into the history vector */ for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++) { - if (phist->h_wherefrom) - { - phist->h_amps[newphase] = phist->h_wherefrom->h_loud; - phist->h_pitches[newphase] = - phist->h_wherefrom->h_pitch; - (phist->h_age)++; - } - else - { - phist->h_age = 0; - phist->h_amps[newphase] = phist->h_pitches[newphase] = 0; - } + if (phist->h_wherefrom) + { + phist->h_amps[newphase] = phist->h_wherefrom->h_loud; + phist->h_pitches[newphase] = + phist->h_wherefrom->h_pitch; + (phist->h_age)++; + } + else + { + phist->h_age = 0; + phist->h_amps[newphase] = phist->h_pitches[newphase] = 0; + } } #if 1 if (x->x_nprint) { - post("vibrato %d %f", x->x_vibbins, x->x_vibdepth); - for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++) - { - post("noted %f, age %d", phist->h_noted, phist->h_age); + post("vibrato %d %f", x->x_vibbins, x->x_vibdepth); + for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++) + { + post("noted %f, age %d", phist->h_noted, phist->h_age); #ifndef I860 - post("values %f %f %f %f %f", - phist->h_pitches[newphase], - phist->h_pitches[(newphase + HISTORY-1)%HISTORY], - phist->h_pitches[(newphase + HISTORY-2)%HISTORY], - phist->h_pitches[(newphase + HISTORY-3)%HISTORY], - phist->h_pitches[(newphase + HISTORY-4)%HISTORY]); + post("values %f %f %f %f %f", + phist->h_pitches[newphase], + phist->h_pitches[(newphase + HISTORY-1)%HISTORY], + phist->h_pitches[(newphase + HISTORY-2)%HISTORY], + phist->h_pitches[(newphase + HISTORY-3)%HISTORY], + phist->h_pitches[(newphase + HISTORY-4)%HISTORY]); #endif - } + } } #endif - /* look for envelope attacks */ + /* look for envelope attacks */ x->x_attackvalue = 0; if (x->x_peaked) { - if (total_db > x->x_amphi) - { - int binlook = newphase - x->x_attackbins; - if (binlook < 0) binlook += HISTORY; - if (total_db > x->x_dbs[binlook] + x->x_attackthresh) - { - x->x_attackvalue = 1; - x->x_peaked = 0; - } - } + if (total_db > x->x_amphi) + { + int binlook = newphase - x->x_attackbins; + if (binlook < 0) binlook += HISTORY; + if (total_db > x->x_dbs[binlook] + x->x_attackthresh) + { + x->x_attackvalue = 1; + x->x_peaked = 0; + } + } } else { - int binlook = newphase - x->x_attackbins; - if (binlook < 0) binlook += HISTORY; - if (x->x_dbs[binlook] > x->x_amphi && x->x_dbs[binlook] > total_db) - x->x_peaked = 1; + int binlook = newphase - x->x_attackbins; + if (binlook < 0) binlook += HISTORY; + if (x->x_dbs[binlook] > x->x_amphi && x->x_dbs[binlook] > total_db) + x->x_peaked = 1; } - /* for each current frequency track, test for a new note using a - * stability criterion. Later perhaps we should also do as in - * pitch~ and check for unstable notes a posteriori when - * there's a new attack with no note found since the last onset; - * but what's an attack &/or onset when we're polyphonic? - */ + /* for each current frequency track, test for a new note using a + * stability criterion. Later perhaps we should also do as in + * pitch~ and check for unstable notes a posteriori when + * there's a new attack with no note found since the last onset; + * but what's an attack &/or onset when we're polyphonic? + */ for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++) { - /* - * if we've found a pitch but we've now strayed from it turn - * it off. - */ - if (phist->h_noted) - { - if (phist->h_pitches[newphase] > phist->h_noted + x->x_vibdepth - || phist->h_pitches[newphase] < phist->h_noted - x->x_vibdepth) - phist->h_noted = 0; - } - else - { - if (phist->h_wherefrom && phist->h_age >= x->x_vibbins) - { - float centroid = 0; - int not = 0; - for (j = 0, k = newphase; j < x->x_vibbins; j++) - { - centroid += phist->h_pitches[k]; - k--; - if (k < 0) k = HISTORY-1; - } - centroid /= x->x_vibbins; - for (j = 0, k = newphase; j < x->x_vibbins; j++) - { - /* calculate deviation from norm */ - float dev = centroid - phist->h_pitches[k]; - k--; - if (k < 0) k = HISTORY-1; - if (dev > x->x_vibdepth || - -dev > x->x_vibdepth) not = 1; - } - if (!not) - { - phist->h_pitch = phist->h_noted = centroid; - } - } - } + /* + * if we've found a pitch but we've now strayed from it turn + * it off. + */ + if (phist->h_noted) + { + if (phist->h_pitches[newphase] > phist->h_noted + x->x_vibdepth + || phist->h_pitches[newphase] < phist->h_noted - x->x_vibdepth) + phist->h_noted = 0; + } + else + { + if (phist->h_wherefrom && phist->h_age >= x->x_vibbins) + { + float centroid = 0; + int not = 0; + for (j = 0, k = newphase; j < x->x_vibbins; j++) + { + centroid += phist->h_pitches[k]; + k--; + if (k < 0) k = HISTORY-1; + } + centroid /= x->x_vibbins; + for (j = 0, k = newphase; j < x->x_vibbins; j++) + { + /* calculate deviation from norm */ + float dev = centroid - phist->h_pitches[k]; + k--; + if (k < 0) k = HISTORY-1; + if (dev > x->x_vibdepth || + -dev > x->x_vibdepth) not = 1; + } + if (!not) + { + phist->h_pitch = phist->h_noted = centroid; + } + } + } } return; nopow: for (i = 0; i < x->x_npitch; i++) { - x->x_hist[i].h_pitch = x->x_hist[i].h_noted = - x->x_hist[i].h_pitches[newphase] = - x->x_hist[i].h_amps[newphase] = 0; - x->x_hist[i].h_age = 0; + x->x_hist[i].h_pitch = x->x_hist[i].h_noted = + x->x_hist[i].h_pitches[newphase] = + x->x_hist[i].h_amps[newphase] = 0; + x->x_hist[i].h_age = 0; } x->x_peaked = 1; x->x_dbage = 0; @@ -1057,19 +1051,19 @@ static void sigfiddle_freebird(t_sigfiddle *x) { if (x->x_inbuf) { - freebytes(x->x_inbuf, sizeof(float) * x->x_hop); - x->x_inbuf = 0; + freebytes(x->x_inbuf, sizeof(float) * x->x_hop); + x->x_inbuf = 0; } if (x->x_lastanalysis) { - freebytes(x->x_lastanalysis, - sizeof(float) * (2 * x->x_hop + 4 * FILTSIZE)); - x->x_lastanalysis = 0; + freebytes(x->x_lastanalysis, + sizeof(float) * (2 * x->x_hop + 4 * FILTSIZE)); + x->x_lastanalysis = 0; } if (x->x_spiral) { - freebytes(x->x_spiral, sizeof(float) * 2 * x->x_hop); - x->x_spiral = 0; + freebytes(x->x_spiral, sizeof(float) * 2 * x->x_hop); + x->x_spiral = 0; } x->x_hop = 0; } @@ -1080,29 +1074,29 @@ int sigfiddle_setnpoints(t_sigfiddle *x, t_floatarg fnpoints) sigfiddle_freebird(x); if (npoints < MINPOINTS || npoints > MAXPOINTS) { - error("fiddle~: npoints out of range; using %d", - npoints = DEFAULTPOINTS); + error("fiddle~: npoints out of range; using %d", + npoints = DEFAULTPOINTS); } if (npoints != (1 << sigfiddle_ilog2(npoints))) { - error("fiddle~: npoints not a power of 2; using %d", - npoints = (1 << sigfiddle_ilog2(npoints))); + error("fiddle~: npoints not a power of 2; using %d", + npoints = (1 << sigfiddle_ilog2(npoints))); } x->x_hop = npoints >> 1; if (!(x->x_inbuf = (float *)getbytes(sizeof(float) * x->x_hop))) - goto fail; + goto fail; if (!(x->x_lastanalysis = (float *)getbytes( - sizeof(float) * (2 * x->x_hop + 4 * FILTSIZE)))) - goto fail; + sizeof(float) * (2 * x->x_hop + 4 * FILTSIZE)))) + goto fail; if (!(x->x_spiral = (float *)getbytes(sizeof(float) * 2 * x->x_hop))) - goto fail; + goto fail; for (i = 0; i < x->x_hop; i++) - x->x_inbuf[i] = 0; + x->x_inbuf[i] = 0; for (i = 0; i < npoints + 4 * FILTSIZE; i++) - x->x_lastanalysis[i] = 0; + x->x_lastanalysis[i] = 0; for (i = 0; i < x->x_hop; i++) - x->x_spiral[2*i] = cos((3.14159*i)/(npoints)), - x->x_spiral[2*i+1] = -sin((3.14159*i)/(npoints)); + x->x_spiral[2*i] = cos((3.14159*i)/(npoints)), + x->x_spiral[2*i+1] = -sin((3.14159*i)/(npoints)); x->x_phase = 0; return (1); fail: @@ -1126,36 +1120,36 @@ int sigfiddle_doinit(t_sigfiddle *x, long npoints, long npitch, else if (npitch > MAXNPITCH) npitch = MAXNPITCH; if (npeakanal && !npitch) npitch = 1; if (!npoints) - npoints = DEFAULTPOINTS; + npoints = DEFAULTPOINTS; if (!sigfiddle_setnpoints(x, npoints)) { - error("fiddle~: out of memory"); - return (0); + error("fiddle~: out of memory"); + return (0); } if (!(buf4 = (t_peakout *)getbytes(sizeof(*buf4) * npeakout))) { - sigfiddle_freebird(x); - error("fiddle~: out of memory"); - return (0); + sigfiddle_freebird(x); + error("fiddle~: out of memory"); + return (0); } for (i = 0; i < npeakout; i++) - buf4[i].po_freq = buf4[i].po_amp = 0; + buf4[i].po_freq = buf4[i].po_amp = 0; x->x_peakbuf = buf4; x->x_npeakout = npeakout; x->x_npeakanal = npeakanal; x->x_phase = 0; x->x_histphase = 0; - x->x_sr = 44100; /* this and the next are filled in later */ + x->x_sr = 44100; /* this and the next are filled in later */ for (i = 0; i < MAXNPITCH; i++) { - int j; - x->x_hist[i].h_pitch = x->x_hist[i].h_noted = 0; - x->x_hist[i].h_age = 0; - x->x_hist[i].h_wherefrom = 0; - x->x_hist[i].h_outlet = 0; - for (j = 0; j < HISTORY; j++) - x->x_hist[i].h_amps[j] = x->x_hist[i].h_pitches[j] = 0; + int j; + x->x_hist[i].h_pitch = x->x_hist[i].h_noted = 0; + x->x_hist[i].h_age = 0; + x->x_hist[i].h_wherefrom = 0; + x->x_hist[i].h_outlet = 0; + for (j = 0; j < HISTORY; j++) + x->x_hist[i].h_amps[j] = x->x_hist[i].h_pitches[j] = 0; } x->x_nprint = 0; x->x_npitch = npitch; @@ -1166,10 +1160,10 @@ int sigfiddle_doinit(t_sigfiddle *x, long npoints, long npitch, x->x_amplo = DEFAMPLO; x->x_amphi = DEFAMPHI; x->x_attacktime = DEFATTACKTIME; - x->x_attackbins = 1; /* real value calculated afterward */ + x->x_attackbins = 1; /* real value calculated afterward */ x->x_attackthresh = DEFATTACKTHRESH; x->x_vibtime = DEFVIBTIME; - x->x_vibbins = 1; /* real value calculated afterward */ + x->x_vibbins = 1; /* real value calculated afterward */ x->x_vibdepth = DEFVIBDEPTH; x->x_npartial = 7; x->x_attackvalue = 0; @@ -1238,15 +1232,15 @@ void ftl_sigfiddle(fts_word_t *a) int count; float *fp, *fp2; for (count = 0, fp = x->x_inbuf + x->x_phase; - count < n_tick; count++) *fp++ = *in++; + count < n_tick; count++) *fp++ = *in++; if (fp == x->x_inbuf + x->x_hop) { - sigfiddle_doit(x); - x->x_phase = 0; + sigfiddle_doit(x); + x->x_phase = 0; fts_alarm_set_delay(&x->x_clock, 0L); /* output bang */ fts_alarm_arm(&x->x_clock); - if (x->x_nprint) x->x_nprint--; + if (x->x_nprint) x->x_nprint--; } else x->x_phase += n_tick; } @@ -1277,14 +1271,14 @@ void sigfiddle_tick(fts_alarm_t *alarm, void *p) fts_outlet_float(o, OUTLETpower, x->x_dbs[x->x_histphase]); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) { - fts_atom_t at[2]; - fts_set_float(at, ph->h_pitches[x->x_histphase]); - fts_set_float(at+1, ph->h_amps[x->x_histphase]); - fts_outlet_list(o, OUTLETmicropitch3 - i, 2, at); + fts_atom_t at[2]; + fts_set_float(at, ph->h_pitches[x->x_histphase]); + fts_set_float(at+1, ph->h_amps[x->x_histphase]); + fts_outlet_list(o, OUTLETmicropitch3 - i, 2, at); } if (x->x_attackvalue) fts_outlet_bang(o, OUTLETattack); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) - if (ph->h_pitch) fts_outlet_float(o, OUTLETpitch, ph->h_pitch); + if (ph->h_pitch) fts_outlet_float(o, OUTLETpitch, ph->h_pitch); } static void sigfiddle_delete(fts_object_t *o, int winlet, fts_symbol_t *s, int ac, @@ -1310,12 +1304,12 @@ static void sigfiddle_init(fts_object_t *o, int winlet, fts_symbol_t *s, int ac, if (!sigfiddle_doinit(x, npoints, npitch, npeakanal, npeakout)) { - post("fiddle~: initialization failed"); - return; + post("fiddle~: initialization failed"); + return; } hop = npoints>>1; if (fts_fft_declaresize(hop) != fts_Success) - post("fiddle~: bad FFT size"); + post("fiddle~: bad FFT size"); fts_alarm_init(&(x->x_clock), 0, sigfiddle_tick, x); dsp_list_insert(o); @@ -1344,13 +1338,13 @@ static fts_status_t sigfiddle_instantiate(fts_class_t *cl, int ac, fts_method_define(cl, 0, fts_new_symbol("print"), sigfiddle_print13, 0, a); fts_method_define(cl, 0, fts_new_symbol("debug"), sigfiddle_debug13, 0, a); fts_method_define(cl, 0, fts_new_symbol("amp-range"), sigfiddle_amprange13, - 0, a); + 0, a); fts_method_define(cl, 0, fts_new_symbol("reattack"), sigfiddle_reattack13, - 0, a); + 0, a); fts_method_define(cl, 0, fts_new_symbol("vibrato"), sigfiddle_vibrato13, - 0, a); + 0, a); fts_method_define(cl, 0, fts_new_symbol("npartial"), sigfiddle_npartial13, - 0, a); + 0, a); /* classes signal inlets */ dsp_sig_inlet(cl, 0); /* declare signal input #0 */ @@ -1385,7 +1379,7 @@ void fiddle_config(void) fts_module_t fiddle_module = {"fiddle", "sonic meat fiddle", fiddle_config, 0}; -#endif /* JMAX */ +#endif /* JMAX */ #ifdef PD @@ -1397,15 +1391,15 @@ static t_int *fiddle_perform(t_int *w) int count; float *fp; if (!x->x_hop) - goto nono; + goto nono; for (count = 0, fp = x->x_inbuf + x->x_phase; count < n; count++) - *fp++ = *in++; + *fp++ = *in++; if (fp == x->x_inbuf + x->x_hop) { - sigfiddle_doit(x); - x->x_phase = 0; - if (x->x_auto) clock_delay(x->x_clock, 0L); - if (x->x_nprint) x->x_nprint--; + sigfiddle_doit(x); + x->x_phase = 0; + if (x->x_auto) clock_delay(x->x_clock, 0L); + if (x->x_nprint) x->x_nprint--; } else x->x_phase += n; nono: @@ -1430,39 +1424,39 @@ void sigfiddle_bang(t_sigfiddle *x) t_pitchhist *ph; if (x->x_npeakout) { - int npeakout = x->x_npeakout; - t_peakout *po; - for (i = 0, po = x->x_peakbuf; i < npeakout; i++, po++) - { - t_atom at[3]; - SETFLOAT(at, i+1); - SETFLOAT(at+1, po->po_freq); - SETFLOAT(at+2, po->po_amp); - outlet_list(x->x_peakout, 0, 3, at); - } + int npeakout = x->x_npeakout; + t_peakout *po; + for (i = 0, po = x->x_peakbuf; i < npeakout; i++, po++) + { + t_atom at[3]; + SETFLOAT(at, i+1); + SETFLOAT(at+1, po->po_freq); + SETFLOAT(at+2, po->po_amp); + outlet_list(x->x_peakout, 0, 3, at); + } } outlet_float(x->x_envout, x->x_dbs[x->x_histphase]); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) { - t_atom at[2]; - SETFLOAT(at, ph->h_pitches[x->x_histphase]); - SETFLOAT(at+1, ph->h_amps[x->x_histphase]); - outlet_list(ph->h_outlet, 0, 2, at); + t_atom at[2]; + SETFLOAT(at, ph->h_pitches[x->x_histphase]); + SETFLOAT(at+1, ph->h_amps[x->x_histphase]); + outlet_list(ph->h_outlet, 0, 2, at); } if (x->x_attackvalue) outlet_bang(x->x_attackout); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) - if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); + if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); } -void sigfiddle_ff(t_sigfiddle *x) /* cleanup on free */ +void sigfiddle_ff(t_sigfiddle *x) /* cleanup on free */ { if (x->x_inbuf) { - freebytes(x->x_inbuf, sizeof(float) * x->x_hop); - freebytes(x->x_lastanalysis, sizeof(float) * (2*x->x_hop + 4 * FILTSIZE)); - freebytes(x->x_spiral, sizeof(float) * 2*x->x_hop); - freebytes(x->x_peakbuf, sizeof(*x->x_peakbuf) * x->x_npeakout); - clock_free(x->x_clock); + freebytes(x->x_inbuf, sizeof(float) * x->x_hop); + freebytes(x->x_lastanalysis, sizeof(float) * (2*x->x_hop + 4 * FILTSIZE)); + freebytes(x->x_spiral, sizeof(float) * 2*x->x_hop); + freebytes(x->x_peakbuf, sizeof(*x->x_peakbuf) * x->x_npeakout); + clock_free(x->x_clock); } } @@ -1477,19 +1471,19 @@ void *sigfiddle_new(t_floatarg npoints, t_floatarg npitch, if (!sigfiddle_doinit(x, npoints, npitch, - npeakanal, npeakout)) + npeakanal, npeakout)) { - x->x_inbuf = 0; /* prevent the free routine from cleaning up */ - pd_free(&x->x_ob.ob_pd); - return (0); + x->x_inbuf = 0; /* prevent the free routine from cleaning up */ + pd_free(&x->x_ob.ob_pd); + return (0); } x->x_noteout = outlet_new(&x->x_ob, gensym("float")); x->x_attackout = outlet_new(&x->x_ob, gensym("bang")); for (i = 0; i < x->x_npitch; i++) - x->x_hist[i].h_outlet = outlet_new(&x->x_ob, gensym("list")); + x->x_hist[i].h_outlet = outlet_new(&x->x_ob, gensym("list")); x->x_envout = outlet_new(&x->x_ob, gensym("float")); if (x->x_npeakout) - x->x_peakout = outlet_new(&x->x_ob, gensym("list")); + x->x_peakout = outlet_new(&x->x_ob, gensym("list")); else x->x_peakout = 0; x->x_clock = clock_new(&x->x_ob.ob_pd, (t_method)sigfiddle_bang); return (x); @@ -1498,30 +1492,30 @@ void *sigfiddle_new(t_floatarg npoints, t_floatarg npitch, void fiddle_tilde_setup(void) { sigfiddle_class = class_new(gensym("fiddle~"), (t_newmethod)sigfiddle_new, - (t_method)sigfiddle_ff, sizeof(t_sigfiddle), 0, - A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); + (t_method)sigfiddle_ff, sizeof(t_sigfiddle), 0, + A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_dsp, - gensym("dsp"), 0); + gensym("dsp"), 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_debug, - gensym("debug"), 0); + gensym("debug"), 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_setnpoints, - gensym("npoints"), A_FLOAT, 0); + gensym("npoints"), A_FLOAT, 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_amprange, - gensym("amp-range"), A_FLOAT, A_FLOAT, 0); + gensym("amp-range"), A_FLOAT, A_FLOAT, 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_reattack, - gensym("reattack"), A_FLOAT, A_FLOAT, 0); + gensym("reattack"), A_FLOAT, A_FLOAT, 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_vibrato, - gensym("vibrato"), A_FLOAT, A_FLOAT, 0); + gensym("vibrato"), A_FLOAT, A_FLOAT, 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_npartial, - gensym("npartial"), A_FLOAT, 0); + gensym("npartial"), A_FLOAT, 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_auto, - gensym("auto"), A_FLOAT, 0); + gensym("auto"), A_FLOAT, 0); class_addmethod(sigfiddle_class, (t_method)sigfiddle_print, - gensym("print"), 0); + gensym("print"), 0); class_addmethod(sigfiddle_class, nullfn, gensym("signal"), 0); class_addbang(sigfiddle_class, sigfiddle_bang); class_addcreator((t_newmethod)sigfiddle_new, gensym("fiddle"), - A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); + A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); post(fiddle_version); } @@ -1538,13 +1532,13 @@ void cu_fiddle(float *in1, t_sigfiddle *x, int n) int count; float *fp, *fp2; for (count = 0, fp = x->x_inbuf + x->x_phase; - count < n; count++) *fp++ = *in1++; + count < n; count++) *fp++ = *in1++; if (fp == x->x_inbuf + x->x_hop) { - sigfiddle_doit(x); - x->x_phase = 0; - if (x->x_auto) clock_delay(x->x_clock, 0L); - if (x->x_nprint) x->x_nprint--; + sigfiddle_doit(x); + x->x_phase = 0; + if (x->x_auto) clock_delay(x->x_clock, 0L); + if (x->x_nprint) x->x_nprint--; } else x->x_phase += n; } @@ -1553,41 +1547,41 @@ void sigfiddle_put(t_sigfiddle *x, long whether) { if (whether) { - u_stdout(x); - x->x_sr = x->x_io[0]->s_sr; - sigfiddle_reattack(x, x->x_attacktime, x->x_attackthresh); - sigfiddle_vibrato(x, x->x_vibtime, x->x_vibdepth); - dspchain_addc(cu_fiddle, 3, - x->x_io[0]->s_shit, x, x->x_io[0]->s_n); + u_stdout(x); + x->x_sr = x->x_io[0]->s_sr; + sigfiddle_reattack(x, x->x_attacktime, x->x_attackthresh); + sigfiddle_vibrato(x, x->x_vibtime, x->x_vibdepth); + dspchain_addc(cu_fiddle, 3, + x->x_io[0]->s_shit, x, x->x_io[0]->s_n); } } -void sigfiddle_tick(t_sigfiddle *x) /* callback function for the clock */ +void sigfiddle_tick(t_sigfiddle *x) /* callback function for the clock */ { int i; t_pitchhist *ph; outlet_float(x->x_envout, x->x_dbs[x->x_histphase]); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) { - t_atom at[2]; - SETFLOAT(at, ph->h_pitches[x->x_histphase]); - SETFLOAT(at+1, ph->h_amps[x->x_histphase]); - outlet_list(ph->h_outlet, NIL, 2, at); + t_atom at[2]; + SETFLOAT(at, ph->h_pitches[x->x_histphase]); + SETFLOAT(at+1, ph->h_amps[x->x_histphase]); + outlet_list(ph->h_outlet, NIL, 2, at); } if (x->x_attackvalue) outlet_bang(x->x_attackout); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) - if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); + if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); } -void sigfiddle_ff(t_sigfiddle *x) /* cleanup on free */ +void sigfiddle_ff(t_sigfiddle *x) /* cleanup on free */ { if (x->x_inbuf) { - freebytes(x->x_inbuf, sizeof(float) * x->x_hop); - freebytes(x->x_lastanalysis, sizeof(float) * (2*x->x_hop + 4 * FILTSIZE)); - freebytes(x->x_spiral, sizeof(float) * 2*x->x_hop); - clock_free(x->x_clock); - u_clean(x); + freebytes(x->x_inbuf, sizeof(float) * x->x_hop); + freebytes(x->x_lastanalysis, sizeof(float) * (2*x->x_hop + 4 * FILTSIZE)); + freebytes(x->x_spiral, sizeof(float) * 2*x->x_hop); + clock_free(x->x_clock); + u_clean(x); } } @@ -1601,14 +1595,14 @@ void *sigfiddle_new(long npoints, long npitch, if (!sigfiddle_doinit(x, npoints, npitch, npeakanal, npeakout)) { - x->x_inbuf = 0; /* prevent the free routine from cleaning up */ - obj_free(x); - return (0); + x->x_inbuf = 0; /* prevent the free routine from cleaning up */ + obj_free(x); + return (0); } u_setup(x, IN1, OUT0); x->x_envout = outlet_new(x, gensym("float")); for (i = 0; i < x->x_npitch; i++) - x->x_hist[i].h_outlet = outlet_new(x, gensym("list")); + x->x_hist[i].h_outlet = outlet_new(x, gensym("list")); x->x_attackout = outlet_new(x, gensym("bang")); x->x_noteout = outlet_new(x, gensym("float")); x->x_clock = clock_new(x, sigfiddle_tick); @@ -1618,8 +1612,8 @@ void *sigfiddle_new(long npoints, long npitch, void fiddle_setup() { c_extern(&sigfiddle_class, sigfiddle_new, sigfiddle_ff, - gensym("fiddle"), sizeof(t_sigfiddle), 0, A_DEFLONG, A_DEFLONG, - A_DEFLONG, A_DEFLONG, 0); + gensym("fiddle"), sizeof(t_sigfiddle), 0, A_DEFLONG, A_DEFLONG, + A_DEFLONG, A_DEFLONG, 0); c_addmess(sigfiddle_put, gensym("put"), A_CANT, 0); c_addmess(sigfiddle_debug, gensym("debug"), 0); c_addmess(sigfiddle_amprange, gensym("amp-range"), A_FLOAT, A_FLOAT, 0); @@ -1627,7 +1621,7 @@ void fiddle_setup() c_addmess(sigfiddle_vibrato, gensym("vibrato"), A_LONG, A_FLOAT, 0); c_addmess(sigfiddle_npartial, gensym("npartial"), A_FLOAT, 0); c_addmess(sigfiddle_print, gensym("print"), 0); - u_inletmethod(0); /* one signal input */ + u_inletmethod(0); /* one signal input */ #ifdef MAX post(fiddle_version); #endif @@ -1648,17 +1642,17 @@ static t_int *fiddle_perform(t_int *w) float *fp; if (x->x_obj.z_disabled) - goto skip; + goto skip; for (count = 0, fp = x->x_inbuf + x->x_phase; count < n; count+=inc) { - *fp++ = *in; - in += inc; + *fp++ = *in; + in += inc; } if (fp == x->x_inbuf + x->x_hop) { - sigfiddle_doit(x); - x->x_phase = 0; - if (x->x_auto) clock_delay(x->x_clock, 0L); - if (x->x_nprint) x->x_nprint--; + sigfiddle_doit(x); + x->x_phase = 0; + if (x->x_auto) clock_delay(x->x_clock, 0L); + if (x->x_nprint) x->x_nprint--; } else x->x_phase += n; skip: @@ -1668,147 +1662,142 @@ skip: void sigfiddle_dsp(t_sigfiddle *x, t_signal **sp) { if (sp[0]->s_n > x->x_hop) { - x->x_downsample = sp[0]->s_n / x->x_hop; - post("* warning: fiddle~: will downsample input by %ld",x->x_downsample); - x->x_sr = sp[0]->s_sr / x->x_downsample; + x->x_downsample = sp[0]->s_n / x->x_hop; + post("* warning: fiddle~: will downsample input by %ld",x->x_downsample); + x->x_sr = sp[0]->s_sr / x->x_downsample; } else { - x->x_downsample = 1; - x->x_sr = sp[0]->s_sr; - } - sigfiddle_reattack(x, x->x_attacktime, x->x_attackthresh); + x->x_downsample = 1; + x->x_sr = sp[0]->s_sr; + } + sigfiddle_reattack(x, x->x_attacktime, x->x_attackthresh); sigfiddle_vibrato(x, x->x_vibtime, x->x_vibdepth); dsp_add(fiddle_perform, 3, sp[0]->s_vec, x, sp[0]->s_n); } -void sigfiddle_tick(t_sigfiddle *x) /* callback function for the clock MSP*/ +void sigfiddle_tick(t_sigfiddle *x) /* callback function for the clock MSP*/ { int i; t_pitchhist *ph; if (x->x_npeakout) { - int npeakout = x->x_npeakout; - t_peakout *po; - for (i = 0, po = x->x_peakbuf; i < npeakout; i++, po++) - { - t_atom at[3]; - SETINT(at, i+1); - SETFLOAT(at+1, po->po_freq); - SETFLOAT(at+2, po->po_amp); - outlet_list(x->x_peakout, 0, 3, at); - } + int npeakout = x->x_npeakout; + t_peakout *po; + for (i = 0, po = x->x_peakbuf; i < npeakout; i++, po++) + { + t_atom at[3]; + SETINT(at, i+1); + SETFLOAT(at+1, po->po_freq); + SETFLOAT(at+2, po->po_amp); + outlet_list(x->x_peakout, 0, 3, at); + } } outlet_float(x->x_envout, x->x_dbs[x->x_histphase]); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) { - t_atom at[2]; - SETFLOAT(at, ph->h_pitches[x->x_histphase]); - SETFLOAT(at+1, ph->h_amps[x->x_histphase]); - outlet_list(ph->h_outlet, 0, 2, at); + t_atom at[2]; + SETFLOAT(at, ph->h_pitches[x->x_histphase]); + SETFLOAT(at+1, ph->h_amps[x->x_histphase]); + outlet_list(ph->h_outlet, 0, 2, at); } if (x->x_attackvalue) outlet_bang(x->x_attackout); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) - if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); + if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); } -void sigfiddle_bang(t_sigfiddle *x) // MSP +void sigfiddle_bang(t_sigfiddle *x) { int i; t_pitchhist *ph; if (x->x_npeakout) { - int npeakout = x->x_npeakout; - t_peakout *po; - for (i = 0, po = x->x_peakbuf; i < npeakout; i++, po++) - { - t_atom at[3]; - SETLONG(at, i+1); - SETFLOAT(at+1, po->po_freq); - SETFLOAT(at+2, po->po_amp); - outlet_list(x->x_peakout, 0, 3, at); - } + int npeakout = x->x_npeakout; + t_peakout *po; + for (i = 0, po = x->x_peakbuf; i < npeakout; i++, po++) + { + t_atom at[3]; + SETLONG(at, i+1); + SETFLOAT(at+1, po->po_freq); + SETFLOAT(at+2, po->po_amp); + outlet_list(x->x_peakout, 0, 3, at); + } } outlet_float(x->x_envout, x->x_dbs[x->x_histphase]); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) { - t_atom at[2]; - SETFLOAT(at, ph->h_pitches[x->x_histphase]); - SETFLOAT(at+1, ph->h_amps[x->x_histphase]); - outlet_list(ph->h_outlet, 0, 2, at); + t_atom at[2]; + SETFLOAT(at, ph->h_pitches[x->x_histphase]); + SETFLOAT(at+1, ph->h_amps[x->x_histphase]); + outlet_list(ph->h_outlet, 0, 2, at); } if (x->x_attackvalue) outlet_bang(x->x_attackout); for (i = 0, ph = x->x_hist; i < x->x_npitch; i++, ph++) - if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); + if (ph->h_pitch) outlet_float(x->x_noteout, ph->h_pitch); } -void sigfiddle_ff(t_sigfiddle *x) /* cleanup on free MSP */ +void sigfiddle_ff(t_sigfiddle *x) /* cleanup on free MSP */ { if (x->x_inbuf) { - t_freebytes(x->x_inbuf, sizeof(float) * x->x_hop); - t_freebytes(x->x_lastanalysis, sizeof(float) * (2*x->x_hop + 4 * + t_freebytes(x->x_inbuf, sizeof(float) * x->x_hop); + t_freebytes(x->x_lastanalysis, sizeof(float) * (2*x->x_hop + 4 * FILTSIZE)); - t_freebytes(x->x_spiral, sizeof(float) * 2*x->x_hop); - t_freebytes(x->x_peakbuf, sizeof(*x->x_peakbuf) * x->x_npeakout); - //clock_free(x->x_clock); + t_freebytes(x->x_spiral, sizeof(float) * 2*x->x_hop); + t_freebytes(x->x_peakbuf, sizeof(*x->x_peakbuf) * x->x_npeakout); } - dsp_free((t_pxobject *)x); // Free the object + dsp_free((t_pxobject *)x); } void *sigfiddle_class; -void *sigfiddle_new(long npoints, long npitch, // MSP +void *sigfiddle_new(long npoints, long npitch, long npeakanal, long npeakout) { t_sigfiddle *x = (t_sigfiddle *)newobject(sigfiddle_class); int i; if (!sigfiddle_doinit(x, npoints, npitch, npeakanal, npeakout)) - // MSP { - x->x_inbuf = 0; /* prevent the free routine from cleaning up */ - return (0); + x->x_inbuf = 0; /* prevent the free routine from cleaning up */ + return (0); } - // post("npeak %d, npitch %d", npeakout, npitch); - // set up the inlets and outlets. - dsp_setup((t_pxobject *)x,1); // 1 input + dsp_setup((t_pxobject *)x,1); x->x_clock = clock_new(x, (method)sigfiddle_tick); if (x->x_npeakout) - x->x_peakout = listout((t_object *)x); // listout? + x->x_peakout = listout((t_object *)x); else x->x_peakout = 0; x->x_envout = floatout((t_object *)x); for (i = 0; i < x->x_npitch; i++) - x->x_hist[i].h_outlet = listout((t_object *)x); - x->x_attackout = bangout((t_object *)x); - x->x_noteout = floatout((t_object *)x); - return (x); + x->x_hist[i].h_outlet = listout((t_object *)x); + x->x_attackout = bangout((t_object *)x); + x->x_noteout = floatout((t_object *)x); + return (x); } -void main() // this can be called fiddle_setup if that name is the "Main" -in PPC Linker prefs +void main() { - setup(&sigfiddle_class, sigfiddle_new, (method)sigfiddle_ff, - (short)sizeof(t_sigfiddle), 0L, A_DEFLONG, A_DEFLONG, + setup(&sigfiddle_class, sigfiddle_new, (method)sigfiddle_ff, + (short)sizeof(t_sigfiddle), 0L, A_DEFLONG, A_DEFLONG, A_DEFLONG, A_DEFLONG, 0); - addmess((method)sigfiddle_dsp, "dsp", - A_CANT, 0); - addmess((method)sigfiddle_debug, "debug", 0); - addmess((method)sigfiddle_setnpoints, "npoints", A_FLOAT, 0); - addmess((method)sigfiddle_amprange, "amp-range", A_FLOAT, A_FLOAT, 0); - addmess((method)sigfiddle_reattack, "reattack", A_FLOAT, A_FLOAT, 0); - addmess((method)sigfiddle_vibrato, "vibrato", A_FLOAT, + addmess((method)sigfiddle_dsp, "dsp", + A_CANT, 0); + addmess((method)sigfiddle_debug, "debug", 0); + addmess((method)sigfiddle_setnpoints, "npoints", A_FLOAT, 0); + addmess((method)sigfiddle_amprange, "amp-range", A_FLOAT, A_FLOAT, 0); + addmess((method)sigfiddle_reattack, "reattack", A_FLOAT, A_FLOAT, 0); + addmess((method)sigfiddle_vibrato, "vibrato", A_FLOAT, A_FLOAT, 0); - addmess((method)sigfiddle_npartial, "npartial", A_FLOAT, 0); - addmess((method)sigfiddle_auto, "auto", - A_FLOAT, 0); - addmess((method)sigfiddle_print, "print", 0); - addmess((method)sigfiddle_assist, "assist", - A_CANT, 0); - addbang((method)sigfiddle_bang); + addmess((method)sigfiddle_npartial, "npartial", A_FLOAT, 0); + addmess((method)sigfiddle_auto, "auto", + A_FLOAT, 0); + addmess((method)sigfiddle_print, "print", 0); + addmess((method)sigfiddle_assist, "assist", + A_CANT, 0); + addbang((method)sigfiddle_bang); dsp_initclass(); rescopy('STR#',3748); post(fiddle_version); @@ -1816,39 +1805,41 @@ A_FLOAT, 0); void sigfiddle_assist(t_sigfiddle *x, void *b, long m, long a, char *s) { - assist_string(3748,m,a,1,2,s); + assist_string(3748,m,a,1,2,s); } void msp_fft(float *buf, long np, long inv) { - float *src,*real,*rp,*imag,*ip; - long i; - - // because this fft algorithm uses separate real and imaginary - // buffers - // we must split the real and imaginary parts into two buffers, - // then do the opposite on output - // a more ambitious person would either do an in-place conversion - // or rewrite the fft algorithm - - real = rp = msp_ffttemp; - imag = ip = real + MAXPOINTS; - src = buf; - for (i = 0; i < np; i++) { - *rp++ = *src++; - *ip++ = *src++; - } - if (inv) - ifft(np,real,imag); - else - fft(np,real,imag); - rp = real; - ip = imag; - src = buf; - for (i = 0; i < np; i++) { - *src++ = *rp++; - *src++ = *ip++; - } + float *src,*real,*rp,*imag,*ip; + long i; + + /* + // because this fft algorithm uses separate real and imaginary + // buffers + // we must split the real and imaginary parts into two buffers, + // then do the opposite on output + // a more ambitious person would either do an in-place conversion + // or rewrite the fft algorithm + */ + + real = rp = msp_ffttemp; + imag = ip = real + MAXPOINTS; + src = buf; + for (i = 0; i < np; i++) { + *rp++ = *src++; + *ip++ = *src++; + } + if (inv) + ifft(np,real,imag); + else + fft(np,real,imag); + rp = real; + ip = imag; + src = buf; + for (i = 0; i < np; i++) { + *src++ = *rp++; + *src++ = *ip++; + } } #endif /* MSP */ diff --git a/pd/extra/loop~/loop~.c b/pd/extra/loop~/loop~.c index 85588ba8..2f440030 100644 --- a/pd/extra/loop~/loop~.c +++ b/pd/extra/loop~/loop~.c @@ -24,61 +24,61 @@ typedef struct _loopctl } t_loopctl; static void loopctl_run(t_loopctl *x, float *transposein, - float *windowin, float *rawout, float *windowout, int n) + float *windowin, float *rawout, float *windowout, int n) { float window, invwindow; double phase = x->l_phase; if (x->l_resync) { - window = *windowin; - if (window < 0) - { - if (window > -1) - window = -1; - invwindow = -1/window; - } - else - { - if (window < 1) - window = 1; - invwindow = 1/window; - } - x->l_resync = 0; + window = *windowin; + if (window < 0) + { + if (window > -1) + window = -1; + invwindow = -1/window; + } + else + { + if (window < 1) + window = 1; + invwindow = 1/window; + } + x->l_resync = 0; } else { - window = x->l_window; - phase = x->l_phase; - invwindow = x->l_invwindow; + window = x->l_window; + phase = x->l_phase; + invwindow = x->l_invwindow; } while (n--) { - double phaseinc = invwindow * *transposein++; - double newphase; - float nwind = *windowin++; - if (phaseinc >= 1 || phaseinc < 0) - phaseinc = 0; - newphase = phase + phaseinc; - if (newphase >= 1) - { - window = nwind; - if (window < 0) - { - if (window > -1) - window = -1; - invwindow = -1/window; - } - else - { - if (window < 1) - window = 1; - invwindow = 1/window; - } - newphase -= 1.; - } - phase = newphase; - *rawout++ = (float)phase; - *windowout++ = window; + double phaseinc = invwindow * *transposein++; + double newphase; + float nwind = *windowin++; + if (phaseinc >= 1 || phaseinc < 0) + phaseinc = 0; + newphase = phase + phaseinc; + if (newphase >= 1) + { + window = nwind; + if (window < 0) + { + if (window > -1) + window = -1; + invwindow = -1/window; + } + else + { + if (window < 1) + window = 1; + invwindow = 1/window; + } + newphase -= 1.; + } + phase = newphase; + *rawout++ = (float)phase; + *windowout++ = window; } x->l_invwindow = invwindow; x->l_window = window; @@ -95,7 +95,7 @@ static void loopctl_init(t_loopctl *x) static void loopctl_set(t_loopctl *x, float val) { if (val < 0 || val > 1) - val = 0; + val = 0; x->l_phase = val; x->l_resync = 1; } @@ -136,8 +136,8 @@ static t_int *loop_perform(t_int *w) static void loop_dsp(t_loop *x, t_signal **sp) { dsp_add(loop_perform, 6, - &x->x_loopctl, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, - sp[0]->s_n); + &x->x_loopctl, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, + sp[0]->s_n); } static void loop_set(t_loop *x, t_floatarg val) @@ -153,11 +153,11 @@ static void loop_bang(t_loop *x) void loop_tilde_setup(void) { loop_class = class_new(gensym("loop~"), (t_newmethod)loop_new, 0, - sizeof(t_loop), 0, 0); + sizeof(t_loop), 0, 0); class_addmethod(loop_class, (t_method)loop_dsp, gensym("dsp"), A_CANT, 0); CLASS_MAINSIGNALIN(loop_class, t_loop, x_f); class_addmethod(loop_class, (t_method)loop_set, gensym("set"), - A_DEFFLOAT, 0); + A_DEFFLOAT, 0); class_addbang(loop_class, loop_bang); } diff --git a/pd/extra/lrshift~/lrshift~.c b/pd/extra/lrshift~/lrshift~.c index 285696e2..ba1d5eeb 100644 --- a/pd/extra/lrshift~/lrshift~.c +++ b/pd/extra/lrshift~/lrshift~.c @@ -19,9 +19,9 @@ static t_int *leftshift_perform(t_int *w) in += shift; n -= shift; while (n--) - *out++ = *in++; + *out++ = *in++; while (shift--) - *out++ = 0; + *out++ = 0; return (w+5); } @@ -34,9 +34,9 @@ static t_int *rightshift_perform(t_int *w) n -= shift; in -= shift; while (n--) - *--out = *--in; + *--out = *--in; while (shift--) - *--out = 0; + *--out = 0; return (w+5); } @@ -45,14 +45,14 @@ static void lrshift_tilde_dsp(t_lrshift_tilde *x, t_signal **sp) int n = sp[0]->s_n; int shift = x->x_n; if (shift > n) - shift = n; + shift = n; if (shift < -n) - shift = -n; + shift = -n; if (shift < 0) - dsp_add(rightshift_perform, 4, - sp[0]->s_vec + n, sp[1]->s_vec + n, n, -shift); + dsp_add(rightshift_perform, 4, + sp[0]->s_vec + n, sp[1]->s_vec + n, n, -shift); else dsp_add(leftshift_perform, 4, - sp[0]->s_vec, sp[1]->s_vec, n, shift); + sp[0]->s_vec, sp[1]->s_vec, n, shift); } static void *lrshift_tilde_new(t_floatarg f) @@ -66,9 +66,9 @@ static void *lrshift_tilde_new(t_floatarg f) void lrshift_tilde_setup(void) { lrshift_tilde_class = class_new(gensym("lrshift~"), - (t_newmethod)lrshift_tilde_new, 0, sizeof(t_lrshift_tilde), 0, - A_DEFFLOAT, 0); + (t_newmethod)lrshift_tilde_new, 0, sizeof(t_lrshift_tilde), 0, + A_DEFFLOAT, 0); class_addmethod(lrshift_tilde_class, nullfn, gensym("signal"), 0); class_addmethod(lrshift_tilde_class, (t_method)lrshift_tilde_dsp, - gensym("dsp"), 0); + gensym("dsp"), 0); } diff --git a/pd/extra/pique/pique.c b/pd/extra/pique/pique.c index aaf80350..f4cae5e1 100644 --- a/pd/extra/pique/pique.c +++ b/pd/extra/pique/pique.c @@ -47,7 +47,7 @@ static float hanning(float pidetune, float sinpidetune) else if (pidetune > 3.14 && pidetune < 3.143) return (0.5); else if (pidetune < -3.14 && pidetune > -3.143) return (0.5); else return (sinpidetune/pidetune - 0.5 * - (sinpidetune/(pidetune+pi) + sinpidetune/(pidetune-pi))); + (sinpidetune/(pidetune+pi) + sinpidetune/(pidetune-pi))); } static float peakerror(float *fpreal, float *fpimag, float pidetune, @@ -57,23 +57,23 @@ static float peakerror(float *fpreal, float *fpimag, float pidetune, float cospidetune = cos(pidetune); float windowshould = hanning(pidetune, sinpidetune); float realshould = windowshould * ( - peakreal * cospidetune + peakimag * sinpidetune); + peakreal * cospidetune + peakimag * sinpidetune); float imagshould = windowshould * ( - peakimag * cospidetune - peakreal * sinpidetune); + peakimag * cospidetune - peakreal * sinpidetune); float realgot = norm * (fpreal[0] - 0.5 * (fpreal[1] + fpreal[-1])); float imaggot = norm * (fpimag[0] - 0.5 * (fpimag[1] + fpimag[-1])); float realdev = realshould - realgot, imagdev = imagshould - imaggot; /* post("real %f->%f; imag %f->%f", realshould, realgot, - imagshould, imaggot); */ + imagshould, imaggot); */ return (realdev * realdev + imagdev * imagdev); } static void pique_doit(int npts, t_float *fpreal, t_float *fpimag, int npeak, int *nfound, t_float *fpfreq, t_float *fpamp, - t_float *fpampre, t_float *fpampim, float errthresh) + t_float *fpampre, t_float *fpampim, float errthresh) { - float srate = sys_getsr(); /* not sure how to get this correctly */ + float srate = sys_getsr(); /* not sure how to get this correctly */ float oneovern = 1.0/ (float)npts; float fperbin = srate * oneovern; float pow1, pow2 = 0, pow3 = 0, pow4 = 0, pow5 = 0; @@ -82,91 +82,91 @@ static void pique_doit(int npts, t_float *fpreal, t_float *fpimag, int count, peakcount = 0, n2 = (npts >> 1); float *fp1, *fp2; for (count = n2, fp1 = fpreal, fp2 = fpimag, powthresh = 0; - count--; fp1++, fp2++) - powthresh += (*fp1) * (*fp1) + (*fp2) * (*fp2) ; + count--; fp1++, fp2++) + powthresh += (*fp1) * (*fp1) + (*fp2) * (*fp2) ; powthresh *= 0.00001; for (count = 1; count < n2; count++) { - float windreal, windimag, pi = 3.14159; - float detune, pidetune, sinpidetune, cospidetune, - ampcorrect, freqout, ampout, ampoutreal, ampoutimag; - float rpeak, rpeaknext, rpeakprev; - float ipeak, ipeaknext, ipeakprev; - float errleft, errright; - fpreal++; - fpimag++; - re1 = re2; - re2 = re3; - re3 = *fpreal; - im1 = im2; - im2 = im3; - im3 = *fpimag; - if (count < 2) continue; - pow1 = pow2; - pow2 = pow3; - pow3 = pow4; - pow4 = pow5; - /* get Hanning-windowed spectrum by convolution */ - windreal = re2 - 0.5 * (re1 + re3); - windimag = im2 - 0.5 * (im1 + im3); - pow5 = windreal * windreal + windimag * windimag; - /* if (count < 30) post("power %f", pow5); */ - if (count < 5) continue; - /* check for a peak. The actual bin is count-3. */ - if (pow3 <= pow2 || pow3 <= pow4 || pow3 <= pow1 || pow3 <= pow5 - || pow3 < powthresh) - continue; - /* go back for the raw FFT values around the peak. */ - rpeak = fpreal[-3]; - rpeaknext = fpreal[-2]; - rpeakprev = fpreal[-4]; - ipeak = fpimag[-3]; - ipeaknext = fpimag[-2]; - ipeakprev = fpimag[-4]; - /* recalculate Hanning-windowed spectrum by convolution */ - windreal = rpeak - 0.5 * (rpeaknext + rpeakprev); - windimag = ipeak - 0.5 * (ipeaknext + ipeakprev); - - detune = ((rpeakprev - rpeaknext) * - (2.0 * rpeak - rpeakprev - rpeaknext) + - (ipeakprev - ipeaknext) * - (2.0 * ipeak - ipeakprev - ipeaknext)) / - (4.0 * pow3); - /* if (count < 30) post("detune %f", detune); */ - if (detune > 0.7 || detune < -0.7) continue; - /* the frequency is the sum of the bin frequency and detuning */ - freqout = fperbin * ((float)(count-3) + detune); - pidetune = pi * detune; - sinpidetune = sin(pidetune); - cospidetune = cos(pidetune); - ampcorrect = 1.0 / hanning(pidetune, sinpidetune); - /* Multiply by 2 to get real-sinusoid peak amplitude - and divide by N to normalize FFT */ - ampcorrect *= 2. * oneovern; - /* amplitude is peak height, corrected for Hanning window shape */ + float windreal, windimag, pi = 3.14159; + float detune, pidetune, sinpidetune, cospidetune, + ampcorrect, freqout, ampout, ampoutreal, ampoutimag; + float rpeak, rpeaknext, rpeakprev; + float ipeak, ipeaknext, ipeakprev; + float errleft, errright; + fpreal++; + fpimag++; + re1 = re2; + re2 = re3; + re3 = *fpreal; + im1 = im2; + im2 = im3; + im3 = *fpimag; + if (count < 2) continue; + pow1 = pow2; + pow2 = pow3; + pow3 = pow4; + pow4 = pow5; + /* get Hanning-windowed spectrum by convolution */ + windreal = re2 - 0.5 * (re1 + re3); + windimag = im2 - 0.5 * (im1 + im3); + pow5 = windreal * windreal + windimag * windimag; + /* if (count < 30) post("power %f", pow5); */ + if (count < 5) continue; + /* check for a peak. The actual bin is count-3. */ + if (pow3 <= pow2 || pow3 <= pow4 || pow3 <= pow1 || pow3 <= pow5 + || pow3 < powthresh) + continue; + /* go back for the raw FFT values around the peak. */ + rpeak = fpreal[-3]; + rpeaknext = fpreal[-2]; + rpeakprev = fpreal[-4]; + ipeak = fpimag[-3]; + ipeaknext = fpimag[-2]; + ipeakprev = fpimag[-4]; + /* recalculate Hanning-windowed spectrum by convolution */ + windreal = rpeak - 0.5 * (rpeaknext + rpeakprev); + windimag = ipeak - 0.5 * (ipeaknext + ipeakprev); + + detune = ((rpeakprev - rpeaknext) * + (2.0 * rpeak - rpeakprev - rpeaknext) + + (ipeakprev - ipeaknext) * + (2.0 * ipeak - ipeakprev - ipeaknext)) / + (4.0 * pow3); + /* if (count < 30) post("detune %f", detune); */ + if (detune > 0.7 || detune < -0.7) continue; + /* the frequency is the sum of the bin frequency and detuning */ + freqout = fperbin * ((float)(count-3) + detune); + pidetune = pi * detune; + sinpidetune = sin(pidetune); + cospidetune = cos(pidetune); + ampcorrect = 1.0 / hanning(pidetune, sinpidetune); + /* Multiply by 2 to get real-sinusoid peak amplitude + and divide by N to normalize FFT */ + ampcorrect *= 2. * oneovern; + /* amplitude is peak height, corrected for Hanning window shape */ - ampout = ampcorrect * sqrt(pow3); - ampoutreal = ampcorrect * - (windreal * cospidetune - windimag * sinpidetune); - ampoutimag = ampcorrect * - (windreal * sinpidetune + windimag * cospidetune); - if (errthresh > 0) - { - /* post("peak %f %f", freqout, ampout); */ - errleft = peakerror(fpreal-4, fpimag-4, pidetune+pi, - 2. * oneovern, ampoutreal, ampoutimag); - errright = peakerror(fpreal-2, fpimag-2, pidetune-pi, - 2. * oneovern, ampoutreal, ampoutimag); - relativeerror = (errleft + errright)/(ampout * ampout); - if (relativeerror > errthresh) continue; - } - /* post("power %f, error %f, relative %f", - pow3, errleft + errright, relativeerror); */ - *fpfreq++ = freqout; - *fpamp++ = ampout; - *fpampre++ = ampoutreal; - *fpampim++ = ampoutimag; - if (++peakcount == npeak) break; + ampout = ampcorrect * sqrt(pow3); + ampoutreal = ampcorrect * + (windreal * cospidetune - windimag * sinpidetune); + ampoutimag = ampcorrect * + (windreal * sinpidetune + windimag * cospidetune); + if (errthresh > 0) + { + /* post("peak %f %f", freqout, ampout); */ + errleft = peakerror(fpreal-4, fpimag-4, pidetune+pi, + 2. * oneovern, ampoutreal, ampoutimag); + errright = peakerror(fpreal-2, fpimag-2, pidetune-pi, + 2. * oneovern, ampoutreal, ampoutimag); + relativeerror = (errleft + errright)/(ampout * ampout); + if (relativeerror > errthresh) continue; + } + /* post("power %f, error %f, relative %f", + pow3, errleft + errright, relativeerror); */ + *fpfreq++ = freqout; + *fpamp++ = ampout; + *fpampre++ = ampoutreal; + *fpampim++ = ampoutimag; + if (++peakcount == npeak) break; } *nfound = peakcount; } @@ -183,32 +183,32 @@ static void pique_list(t_pique *x, t_symbol *s, int argc, t_atom *argv) if (npts < 8 || npeak < 1) error("pique: bad npoints or npeak"); if (npeak > x->x_n) npeak = x->x_n; if (!(a = (t_garray *)pd_findbyclass(symreal, garray_class)) || - !garray_getfloatarray(a, &n, &fpreal) || - n < npts) - error("%s: missing or bad array", symreal->s_name); + !garray_getfloatarray(a, &n, &fpreal) || + n < npts) + error("%s: missing or bad array", symreal->s_name); else if (!(a = (t_garray *)pd_findbyclass(symimag, garray_class)) || - !garray_getfloatarray(a, &n, &fpimag) || - n < npts) - error("%s: missing or bad array", symimag->s_name); + !garray_getfloatarray(a, &n, &fpimag) || + n < npts) + error("%s: missing or bad array", symimag->s_name); else { - int nfound, i; - float *fpfreq = x->x_freq; - float *fpamp = x->x_amp; - float *fpampre = x->x_ampre; - float *fpampim = x->x_ampim; - pique_doit(npts, fpreal, fpimag, npeak, - &nfound, fpfreq, fpamp, fpampre, fpampim, x->x_errthresh); - for (i = 0; i < nfound; i++, fpamp++, fpfreq++, fpampre++, fpampim++) - { - t_atom at[5]; - SETFLOAT(at, (float)i); - SETFLOAT(at+1, *fpfreq); - SETFLOAT(at+2, *fpamp); - SETFLOAT(at+3, *fpampre); - SETFLOAT(at+4, *fpampim); - outlet_list(x->x_obj.ob_outlet, &s_list, 5, at); - } + int nfound, i; + float *fpfreq = x->x_freq; + float *fpamp = x->x_amp; + float *fpampre = x->x_ampre; + float *fpampim = x->x_ampim; + pique_doit(npts, fpreal, fpimag, npeak, + &nfound, fpfreq, fpamp, fpampre, fpampim, x->x_errthresh); + for (i = 0; i < nfound; i++, fpamp++, fpfreq++, fpampre++, fpampim++) + { + t_atom at[5]; + SETFLOAT(at, (float)i); + SETFLOAT(at+1, *fpfreq); + SETFLOAT(at+2, *fpamp); + SETFLOAT(at+3, *fpampre); + SETFLOAT(at+4, *fpampim); + outlet_list(x->x_obj.ob_outlet, &s_list, 5, at); + } } } @@ -232,7 +232,7 @@ void pique_setup(void) (t_method)pique_free, sizeof(t_pique),0, A_DEFFLOAT, 0); class_addlist(pique_class, pique_list); class_addmethod(pique_class, (t_method)pique_errthresh, - gensym("errthresh"), A_FLOAT, 0); + gensym("errthresh"), A_FLOAT, 0); post("pique 0.1 for PD version 23"); } -- cgit v1.2.1