Logo Search packages:      
Sourcecode: csound version File versions

ugens6.c

/*
    ugens6.c:

    Copyright (C) 1991-2000 Barry Vercoe, John ffitch, Jens Groh,
                            Hans Mikelson, Istvan Varga

    This file is part of Csound.

    The Csound Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    Csound is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

#include "csoundCore.h" /*                              UGENS6.C        */
#include "ugens6.h"
#include <math.h>

#define log001 (-FL(6.9078))    /* log(.001) */

int downset(CSOUND *csound, DOWNSAMP *p)
{
    if ((p->len = (int)*p->ilen) > csound->ksmps)
      return csound->InitError(csound, "ilen > ksmps");
    return OK;
}

int downsamp(CSOUND *csound, DOWNSAMP *p)
{
    MYFLT       *asig, sum;
    int len, n;

    if (p->len <= 1)
      *p->kr = *p->asig;
    else {
      asig = p->asig;
      sum = FL(0.0);
      len = p->len;
      for (n=0; n<len; n++) {
        sum += asig[n];
      }
      *p->kr = sum / p->len;
    }
    return OK;
}

int upsamp(CSOUND *csound, UPSAMP *p)
{
    MYFLT kval = *p->ksig;
    MYFLT *ar = p->ar;
    int   n, nsmps = csound->ksmps;

    for (n = 0; n < nsmps; n++)
      ar[n] = kval;
    return OK;
}

int a_k_set(CSOUND *csound, INTERP *p)
{
    p->prev = FL(0.0);
    p->init_k = 0;              /* IV - Sep 5 2002 */
    return OK;
}

int interpset(CSOUND *csound, INTERP *p)
{
    if (*p->istor == FL(0.0)) {
      p->prev = FL(0.0);
      p->init_k = (*p->imode == FL(0.0) ? 0 : 1);       /* IV - Sep 5 2002 */
    }
    return OK;
}

int interp(CSOUND *csound, INTERP *p)
{
    MYFLT *ar, val, incr;
    int   n, nsmps=csound->ksmps;

    ar = p->rslt;
    if (p->init_k) {
      p->init_k = 0;
      p->prev = *p->xsig;
    }
    val = p->prev;
    incr = (*p->xsig - val) * csound->onedksmps;
    for (n=0; n<nsmps; n++) {
      ar[n] = val += incr;
    }
    p->prev = val;
    return OK;
}

int indfset(CSOUND *csound, INDIFF *p)
{
    if (*p->istor == FL(0.0))   /* IV - Sep 5 2002 */
      p->prev = FL(0.0);
    return OK;
}

int kntegrate(CSOUND *csound, INDIFF *p)
{
    *p->rslt = p->prev += *p->xsig;
    return OK;
}

int integrate(CSOUND *csound, INDIFF *p)
{
    MYFLT       *rslt, *asig, sum;
    int n, nsmps=csound->ksmps;

    rslt = p->rslt;
    asig = p->xsig;
    sum = p->prev;
    for (n=0; n<nsmps; n++) {
      rslt[n] = sum += asig[n];
    }
    p->prev = sum;
    return OK;
}

int kdiff(CSOUND *csound, INDIFF *p)
{
    MYFLT       tmp;
    tmp = *p->xsig;             /* IV - Sep 5 2002: fix to make */
    *p->rslt = tmp - p->prev;   /* diff work when the input and */
    p->prev = tmp;              /* output argument is the same  */
    return OK;
}

int diff(CSOUND *csound, INDIFF *p)
{
    MYFLT       *ar, *asig, prev, tmp;
    int n, nsmps=csound->ksmps;

    ar = p->rslt;
    asig = p->xsig;
    prev = p->prev;
    for (n=0; n<nsmps; n++) {
      tmp = asig[n];            /* IV - Sep 5 2002: fix to make */
      ar[n] = tmp - prev;       /* diff work when the input and */
      prev = tmp;               /* output argument is the same  */
    }
    p->prev = prev;
    return OK;
}

int samphset(CSOUND *csound, SAMPHOLD *p)
{
    if (!(*p->istor))
      p->state = *p->ival;
    p->audiogate = (p->XINCODE & 02) ? 1 : 0;
    return OK;
}

int ksmphold(CSOUND *csound, SAMPHOLD *p)
{
    if (*p->xgate > FL(0.0))
      p->state = *p->xsig;
    *p->xr = p->state;
    return OK;
}

int samphold(CSOUND *csound, SAMPHOLD *p)
{
    MYFLT       *ar, *asig, *agate, state;
    int n, nsmps=csound->ksmps;

    ar = p->xr;
    asig = p->xsig;
    state = p->state;
    if (p->audiogate) {
      agate = p->xgate;
      for (n=0; n<nsmps; n++) {
        if (agate[n] > FL(0.0))
          state = asig[n];
        ar[n] = state;
      }
    }
    else {
      if (*p->xgate > FL(0.0)) {
        for (n=0; n<nsmps; n++) {
          ar[n] = state = asig[n];
        }
      }
      else {
        for (n=0; n<nsmps; n++) {
          ar[n] = state;
        }
      }
    }
    p->state = state;
    return OK;
}

int delset(CSOUND *csound, DELAY *p)
{
    int32      npts;
    char        *auxp;

    if (*p->istor && p->auxch.auxp != NULL)
      return OK;
    /* Round not truncate */
    if (UNLIKELY((npts = (int32) (FL(0.5) + *p->idlt * csound->esr)) <= 0)) {
      return csound->InitError(csound, Str("illegal delay time"));
    }

    if ((auxp = p->auxch.auxp) == NULL ||
        npts != p->npts) { /* new space if reqd */
      csound->AuxAlloc(csound, (int32)npts*sizeof(MYFLT), &p->auxch);
      auxp = p->auxch.auxp;
      p->npts = npts;
    }
    else if (!(*p->istor)) {                    /* else if requested */
      memset(auxp, 0, npts*sizeof(MYFLT));
    }
    p->curp = (MYFLT *) auxp;

    return OK;
}

int delrset(CSOUND *csound, DELAYR *p)
{
    int32       npts;
    MYFLT       *auxp;

    if (UNLIKELY(p->XOUTCODE != 1))
      return csound->InitError(csound, Str("delayr: invalid outarg type"));
    /* fifo for delayr pointers by Jens Groh: */
    /* append structadr for delayw to fifo: */
    if (csound->first_delayr != NULL)       /* fifo not empty */
      ((DELAYR*) csound->last_delayr)->next_delayr = p;
    else                                    /* fifo empty */
      csound->first_delayr = (void*) p;
    csound->last_delayr = (void*) p;
    csound->delayr_stack_depth++;
    p->next_delayr = NULL;
    if (p->OUTOCOUNT > 1) {
      /* set optional output arg if specified */
      *(p->indx) = (MYFLT)-(csound->delayr_stack_depth);
    }

    if (*p->istor != FL(0.0) && p->auxch.auxp != NULL)
      return OK;
    /* ksmps is min dely */
    if (UNLIKELY((npts=(int32)(FL(0.5) + *p->idlt*csound->esr)) < csound->ksmps)) {
      return csound->InitError(csound, Str("illegal delay time"));
    }
    if ((auxp = (MYFLT*)p->auxch.auxp) == NULL ||       /* new space if reqd */
        npts != p->npts) {
      csound->AuxAlloc(csound, (int32)npts*sizeof(MYFLT), &p->auxch);
      auxp = (MYFLT*)p->auxch.auxp;
      p->npts = npts;
    }
    else if (*p->istor == FL(0.0)) {            /* else if requested */
      memset(auxp, 0, npts*sizeof(MYFLT));
    }
    p->curp = auxp;
    return OK;
}

int delwset(CSOUND *csound, DELAYW *p)
{
   /* fifo for delayr pointers by Jens Groh: */
    if (UNLIKELY(csound->first_delayr == NULL)) {
      return csound->InitError(csound,
                               Str("delayw: associated delayr not found"));
    }
    p->delayr = (DELAYR*) csound->first_delayr;         /* adr delayr struct */
    /* remove structadr from fifo */
    if (csound->last_delayr == csound->first_delayr) {  /* fifo will be empty */
      csound->first_delayr = NULL;
    }
    else    /* fifo will not be empty */
      csound->first_delayr = ((DELAYR*) csound->first_delayr)->next_delayr;
    csound->delayr_stack_depth--;
    return OK;
}

static DELAYR *delayr_find(CSOUND *csound, MYFLT *ndx)
{
    DELAYR  *d = (DELAYR*) csound->first_delayr;
    int     n = (int)MYFLT2LRND(*ndx);

    if (d == NULL) {
      csound->InitError(csound, Str("deltap: associated delayr not found"));
      return NULL;
    }
    if (!n)
      return (DELAYR*) csound->last_delayr;     /* default: use last delayr */
    else if (n > 0)
      n = csound->delayr_stack_depth - n;       /* ndx > 0: LIFO index mode */
    else
      n = -n;                                   /* ndx < 0: FIFO index mode */
    if (UNLIKELY(n < 1 || n > csound->delayr_stack_depth)) {
      csound->InitError(csound,
                        Str("deltap: delayr index %.0f is out of range"),
                        (double)*ndx);
      return NULL;
    }
    /* find delay line */
    while (--n)
      d = d->next_delayr;
    return d;
}

int tapset(CSOUND *csound, DELTAP *p)
{
    p->delayr = delayr_find(csound, p->indx);
    return (p->delayr != NULL ? OK : NOTOK);
}

int delay(CSOUND *csound, DELAY *p)
{
    MYFLT       *ar, *asig, *curp, *endp;
    int n, nsmps = csound->ksmps;


    if (UNLIKELY(p->auxch.auxp==NULL)) goto err1;  /* RWD fix */
    ar = p->ar;
    asig = p->asig;
    curp = p->curp;
    endp = (MYFLT *) p->auxch.endp;
    for (n=0; n<nsmps; n++) {
      MYFLT in = asig[n];       /* Allow overwriting form */
      ar[n] = *curp;
      *curp = in;
      if (UNLIKELY(++curp >= endp))
        curp = (MYFLT *) p->auxch.auxp;
    }
    p->curp = curp;             /* sav the new curp */

    return OK;
 err1:
      return csound->PerfError(csound, Str("delay: not initialised"));
}

int delayr(CSOUND *csound, DELAYR *p)
{
    MYFLT       *ar, *curp, *endp;
    int n, nsmps = csound->ksmps;

    if (UNLIKELY(p->auxch.auxp==NULL)) goto err1; /* RWD fix */
    ar = p->ar;
    curp = p->curp;
    endp = (MYFLT *) p->auxch.endp;
    for (n=0; n<nsmps; n++) {
      ar[n] = *curp++;
      if (UNLIKELY(curp >= endp))
        curp = (MYFLT *) p->auxch.auxp;
    }
    return OK;
 err1:
      return csound->PerfError(csound, Str("delayr: not initialised"));
}

int delayw(CSOUND *csound, DELAYW *p)
{
    DELAYR      *q = p->delayr;
    MYFLT       *asig, *curp, *endp;
    int n, nsmps = csound->ksmps;

    if (UNLIKELY(q->auxch.auxp==NULL)) goto err1; /* RWD fix */
    asig = p->asig;
    curp = q->curp;
    endp = (MYFLT *) q->auxch.endp;
    for (n=0; n<nsmps; n++) {
      *curp = asig[n];
      if (UNLIKELY(++curp >= endp))
        curp = (MYFLT *) q->auxch.auxp;
    }
    q->curp = curp;                                     /* now sav new curp */
    return OK;
 err1:
    return csound->PerfError(csound, Str("delayw: not initialised"));
}

int deltap(CSOUND *csound, DELTAP *p)
{
    DELAYR      *q = p->delayr;
    MYFLT       *ar, *tap, *endp;
    int n, nsmps = csound->ksmps;

    if (UNLIKELY(q->auxch.auxp==NULL)) goto err1; /* RWD fix */
    ar = p->ar;
    tap = q->curp - (int32) (FL(0.5) + *p->xdlt * csound->esr);
    while (tap < (MYFLT *) q->auxch.auxp)
      tap += q->npts;
    endp = (MYFLT *) q->auxch.endp;
    for (n=0; n<nsmps; n++) {
      if (UNLIKELY(tap >= endp))
        tap -= q->npts;
      ar[n] = *tap++;
    }
    return OK;
 err1:
    return csound->PerfError(csound, Str("deltap: not initialised"));
}

int deltapi(CSOUND *csound, DELTAP *p)
{
    DELAYR      *q = p->delayr;
    MYFLT       *ar, *tap, *prv, *begp, *endp;
    int         n, nsmps = csound->ksmps;
    int32       idelsmps;
    MYFLT       delsmps, delfrac;

    if (q->auxch.auxp==NULL) goto err1;
    ar = p->ar;
    begp = (MYFLT *) q->auxch.auxp;
    endp = (MYFLT *) q->auxch.endp;
    if (!p->XINCODE) {
      delsmps = *p->xdlt * csound->esr;
      idelsmps = (int32)delsmps;
      delfrac = delsmps - idelsmps;
      tap = q->curp - idelsmps;
      while (tap < begp) tap += q->npts;
      for (n=0; n<nsmps; n++) {
        if (tap >= endp)
          tap -= q->npts;
        if ((prv = tap - 1) < begp)
          prv += q->npts;
        ar[n] = *tap + (*prv - *tap) * delfrac;
        tap++;
      }
    }
    else {
      MYFLT *timp = p->xdlt, *curq = q->curp;
      for (n=0; n<nsmps; n++) {
        delsmps = timp[n] * csound->esr;
        idelsmps = (int32)delsmps;
        delfrac = delsmps - idelsmps;
        tap = curq++ - idelsmps;
        if (tap < begp) tap += q->npts;
        else if (tap >= endp)
          tap -= q->npts;
        if ((prv = tap - 1) < begp)
          prv += q->npts;
        ar[n] = *tap + (*prv - *tap) * delfrac;
      }
    }
    return OK;
 err1:
    return csound->PerfError(csound, Str("deltapi: not initialised"));
}

/* ***** From Hans Mikelson ************* */
/* Delay N samples */
int deltapn(CSOUND *csound, DELTAP *p)
{
    DELAYR *q = p->delayr;
    MYFLT  *ar, *tap, *begp, *endp;
    int    n, nsmps = csound->ksmps;
    int32  idelsmps;
    MYFLT  delsmps;

    if (UNLIKELY(q->auxch.auxp==NULL)) goto err1;
    ar = p->ar;
    begp = (MYFLT *) q->auxch.auxp;
    endp = (MYFLT *) q->auxch.endp;
    if (!p->XINCODE) {
      delsmps = *p->xdlt;
      idelsmps = (int32)delsmps;
      tap = q->curp - idelsmps;
      while (tap < begp) tap += q->npts;
      for (n=0; n<nsmps; n++) {
        if (UNLIKELY(tap >= endp ))
          tap -= q->npts;
        if (UNLIKELY(tap < begp))
          tap += q->npts;
        ar[n] = *tap;
        tap++;
      }
    }
    else {
      MYFLT *timp = p->xdlt, *curq = q->curp;
      for (n=0; n<nsmps; n++) {
        delsmps = timp[n];
        idelsmps = (int32)delsmps;
        if (UNLIKELY((tap = curq++ - idelsmps) < begp))
          tap += q->npts;
        else if (UNLIKELY(tap >= endp))
          tap -= q->npts;
        ar[n] = *tap;
      }
    }
    return OK;
 err1:
    return csound->PerfError(csound, Str("deltapn: not initialised"));
}

/* **** JPff **** */
int deltap3(CSOUND *csound, DELTAP *p)
{
    DELAYR      *q = p->delayr;
    MYFLT       *ar, *tap, *prv, *begp, *endp;
    int n, nsmps = csound->ksmps;
    int32       idelsmps;
    MYFLT       delsmps, delfrac;

    if (UNLIKELY(q->auxch.auxp==NULL)) goto err1;
    ar = p->ar;
    begp = (MYFLT *) q->auxch.auxp;
    endp = (MYFLT *) q->auxch.endp;
    if (!p->XINCODE) {
      delsmps = *p->xdlt * csound->esr;
      idelsmps = (int32)delsmps;
      delfrac = delsmps - idelsmps;
      tap = q->curp - idelsmps;
      while (tap < begp) tap += q->npts;
      for (n=0; n<nsmps; n++) {
        MYFLT ym1, y0, y1, y2;
        if (UNLIKELY(tap >= endp))
          tap -= q->npts;
        if (UNLIKELY((prv = tap - 1) < begp))
          prv += q->npts;
        if (UNLIKELY(prv - 1 < begp))
          y2 = *(prv-1+q->npts);
        else
          y2 = *(prv-1);
        if (UNLIKELY(tap + 1 >= endp))
          ym1 = *(tap+1-q->npts);
        else
          ym1 = *(tap+1);
        y0 = *tap; y1 = *prv;
        {
          MYFLT w, x, y, z;
          z = delfrac * delfrac; z--; z *= FL(0.16666666666667);
          y = delfrac; y++; w = (y *= FL(0.5)); w--;
          x = FL(3.0) * z; y -= x; w -= z; x -= delfrac;
          ar[n] = (w*ym1 + x*y0 + y*y1 + z*y2) * delfrac + y0;
        }
        tap++;
      }
    }
    else {
      MYFLT *timp = p->xdlt, *curq = q->curp;
      for (n=0; n<nsmps; n++) {
        MYFLT ym1, y0, y1, y2;
        delsmps = *timp++ * csound->esr;
        idelsmps = (int32)delsmps;
        delfrac = delsmps - idelsmps;
        if (UNLIKELY((tap = curq++ - idelsmps) < begp))
          tap += q->npts;
        else if (UNLIKELY(tap >= endp))
          tap -= q->npts;
        if ((prv = tap - 1) < begp)
          prv += q->npts;
        if (prv - 1 < begp) y2 = *(prv-1+q->npts);
        else                y2 = *(prv-1);
        if (tap + 1 >= endp) ym1 = *(tap+1-q->npts);
        else                ym1 = *(tap+1);
        y0 = *tap; y1 = *prv;
        {
          MYFLT w, x, y, z;
          z = delfrac * delfrac; z--; z *= FL(0.1666666667);
          y = delfrac; y++; w = (y *= FL(0.5)); w--;
          x = FL(3.0) * z; y -= x; w -= z; x -= delfrac;
          ar[n] = (w*ym1 + x*y0 + y*y1 + z*y2) * delfrac + y0;
        }
      }
    }
    return OK;
 err1:
    return csound->PerfError(csound, Str("deltap3: not initialised"));
}


/* deltapx and deltapxw opcodes by Istvan Varga */

int tapxset(CSOUND *csound, DELTAPX *p)
{
    p->delayr = delayr_find(csound, p->indx);
    if (UNLIKELY(p->delayr == NULL))
      return NOTOK;
    p->wsize = (int)(*(p->iwsize) + FL(0.5));          /* window size */
    p->wsize = ((p->wsize + 2) >> 2) << 2;
    if (p->wsize < 4) p->wsize = 4;
    if (p->wsize > 1024) p->wsize = 1024;
    /* wsize = 4: d2x = 1 - 1/3, wsize = 64: d2x = 1 - 1/36 */
    p->d2x = 1.0 - pow((double)p->wsize * 0.85172, -0.89624);
    p->d2x /= (double)((p->wsize * p->wsize) >> 2);
    return OK;
}

int deltapx(CSOUND *csound, DELTAPX *p)                 /* deltapx opcode */
{
    DELAYR  *q = p->delayr;
    MYFLT   *out1, *del, *buf1, *bufp, *bufend;
    int     nn = csound->ksmps;
    int32   indx, maxd, xpos;

    if (UNLIKELY(q->auxch.auxp == NULL)) goto err1; /* RWD fix */
    out1 = p->ar; del = p->adlt;
    buf1 = (MYFLT *) q->auxch.auxp;
    indx = (int32) (q->curp - buf1);
    maxd = q->npts; bufend = buf1 + maxd;

    if (p->wsize != 4) {                /* window size >= 8 */
      double  d, x1, n1, w, d2x;
      int     i2, i;
      i2 = (p->wsize >> 1);
      /* wsize = 4: d2x = 1 - 1/3, wsize = 64: d2x = 1 - 1/36 */
      d2x = p->d2x;
      do {
        /* x1: fractional part of delay time */
        /* x2: sine of x1 (for interpolation) */
        /* xpos: integer part of delay time (buffer position to read from) */

        x1 = (double)indx - (double)*(del++) * (double)csound->esr;
        while (x1 < 0.0) x1 += (double)maxd;
        xpos = (int32)x1; x1 -= (double)xpos;
        while (xpos >= maxd) xpos -= maxd;

        if (x1 > 0.00000001 && x1 < 0.99999999) {
          xpos -= i2;
          while (xpos < 0) xpos += maxd;
          d = (double)(1 - i2) - x1;
          bufp = buf1 + xpos;
          i = i2;
          n1 = 0.0;
          do {
            w = 1.0 - d * d * d2x;
            if (UNLIKELY(++bufp >= bufend)) bufp = buf1;
            n1 += w * w * (double)*bufp / d; d++;
            w = 1.0 - d * d * d2x;
            if (UNLIKELY(++bufp >= bufend)) bufp = buf1;
            n1 -= w * w * (double)*bufp / d; d++;
          } while (--i);
          *out1 = (MYFLT)(n1 * sin(PI * x1) / PI);
        }
        else {                                          /* integer sample */
          xpos = (int32) ((double)xpos + x1 + 0.5);     /* position */
          if (xpos >= maxd) xpos -= maxd;
          *out1 = buf1[xpos];
        }
        out1++; indx++;
      } while (--nn);
    }
    else {                          /* window size = 4, cubic interpolation */
      double  x, am1, a0, a1, a2;
      do {
        am1 = (double)indx - (double)*(del++) * (double)csound->esr;
        while (am1 < 0.0) am1 += (double)maxd;
        xpos = (int32) am1; am1 -= (double)xpos;

        a0  = am1 * am1; a2 = 0.16666667 * (am1 * a0 - am1);    /* sample +2 */
        a1  = 0.5 * (a0 + am1) - 3.0 * a2;                      /* sample +1 */
        am1 = 0.5 * (a0 - am1) - a2;                            /* sample -1 */
        a0  = 3.0 * a2 - a0; a0++;                              /* sample 0  */

        bufp = (xpos ? (buf1 + (xpos - 1L)) : (bufend - 1));
        while (bufp >= bufend) bufp -= maxd;
        x = am1 * (double)*bufp;   if (UNLIKELY(++bufp >= bufend)) bufp = buf1;
        x += a0 * (double)*bufp;   if (UNLIKELY(++bufp >= bufend)) bufp = buf1;
        x += a1 * (double)*bufp;   if (UNLIKELY(++bufp >= bufend)) bufp = buf1;
        x += a2 * (double)*bufp;

        indx++; *out1++ = (MYFLT)x;
      } while (--nn);
    }
    return OK;
 err1:
    return csound->PerfError(csound, Str("deltap: not initialised"));
}

int deltapxw(CSOUND *csound, DELTAPX *p)                /* deltapxw opcode */
{
    DELAYR  *q = p->delayr;
    MYFLT   *in1, *del, *buf1, *bufp, *bufend;
    int     nn = csound->ksmps;
    int32   indx, maxd, xpos;

    if (UNLIKELY(q->auxch.auxp == NULL)) goto err1; /* RWD fix */
    in1 = p->ar; del = p->adlt;
    buf1 = (MYFLT *) q->auxch.auxp;
    indx = (int32) (q->curp - buf1);
    maxd = q->npts; bufend = buf1 + maxd;

    if (p->wsize != 4) {                /* window size >= 8 */
      double  d, x1, n1, w, d2x;
      int     i2, i;
      i2 = (p->wsize >> 1);
      /* wsize = 4: d2x = 1 - 1/3, wsize = 64: d2x = 1 - 1/36 */
      d2x = p->d2x;
      do {
        /* x1: fractional part of delay time */
        /* x2: sine of x1 (for interpolation) */
        /* xpos: integer part of delay time (buffer position to read from) */

        x1 = (double)indx - (double)*(del++) * (double)csound->esr;
        while (x1 < 0.0) x1 += (double)maxd;
        xpos = (int32) x1; x1 -= (double)xpos;
        while (xpos >= maxd) xpos -= maxd;

        if (x1 > 0.00000001 && x1 < 0.99999999) {
          n1 = (double)*in1 * (sin(PI * x1) / PI);
          xpos -= i2;
          while (xpos < 0) xpos += maxd;
          d = (double)(1 - i2) - x1;
          bufp = buf1 + xpos;
          i = i2;
          do {
            w = 1.0 - d * d * d2x;
            if (++bufp >= bufend) bufp = buf1;
            *bufp = (MYFLT)((double)*bufp + w * w * n1 / d); d++;
            w = 1.0 - d * d * d2x;
            if (++bufp >= bufend) bufp = buf1;
            *bufp = (MYFLT)((double)*bufp - w * w * n1 / d); d++;
          } while (--i);
        }
        else {                                          /* integer sample */
          xpos = (int32) ((double)xpos + x1 + 0.5);     /* position */
          if (xpos >= maxd) xpos -= maxd;
          buf1[xpos] += *in1;
        }
        in1++; indx++;
      } while (--nn);
    }
    else {                          /* window size = 4, cubic interpolation */
      double  x, am1, a0, a1, a2;
      do {
        am1 = (double)indx - (double)*(del++) * (double)csound->esr;
        while (am1 < 0.0) am1 += (double)maxd;
        xpos = (int32) am1; am1 -= (double)xpos;

        a0  = am1 * am1; a2 = 0.16666667 * (am1 * a0 - am1);    /* sample +2 */
        a1  = 0.5 * (a0 + am1) - 3.0 * a2;                      /* sample +1 */
        am1 = 0.5 * (a0 - am1) - a2;                            /* sample -1 */
        a0  = 3.0 * a2 - a0; a0++;                              /* sample 0  */

        x = (double)*(in1++);
        bufp = (xpos ? (buf1 + (xpos - 1L)) : (bufend - 1));
        while (bufp >= bufend) bufp -= maxd;
        *bufp += (MYFLT)(am1 * x); if (++bufp >= bufend) bufp = buf1;
        *bufp += (MYFLT)(a0 * x);  if (++bufp >= bufend) bufp = buf1;
        *bufp += (MYFLT)(a1 * x);  if (++bufp >= bufend) bufp = buf1;
        *bufp += (MYFLT)(a2 * x);

        indx++;
      } while (--nn);
    }
    return OK;
 err1:
    return csound->PerfError(csound, Str("deltap: not initialised"));
}

int del1set(CSOUND *csound, DELAY1 *p)
{
    if (!(*p->istor))
      p->sav1 = FL(0.0);
    return OK;
}

int delay1(CSOUND *csound, DELAY1 *p)
{
    MYFLT       *ar, *asig;
    int         n, nsmps = csound->ksmps;

    ar = p->ar;
    asig = p->asig - 1;
    ar[0] = p->sav1;
    /* memmove(&ar[1], asig, sizeof(MYFLT)*(nsmps-1)); */
    for (n = 1; n < nsmps; n++) {
      ar[n] = asig[n];
    }
    p->sav1 = asig[n];
    return OK;
}

int cmbset(CSOUND *csound, COMB *p)
{
    int32       lpsiz, nbytes;

    if (*p->insmps != 0) {
      if ((lpsiz = (int32)(FL(0.5)+*p->ilpt)) <= 0) {
        return csound->InitError(csound, Str("illegal loop time"));
      }
    }
    else if ((lpsiz = (int32)(FL(0.5) + *p->ilpt * csound->esr)) <= 0) {
      return csound->InitError(csound, Str("illegal loop time"));
    }
    nbytes = lpsiz * sizeof(MYFLT);
    if (p->auxch.auxp == NULL || nbytes != p->auxch.size) {
      csound->AuxAlloc(csound, (int32)nbytes, &p->auxch);
      p->pntr = (MYFLT *) p->auxch.auxp;
      p->prvt = FL(0.0);
      p->coef = FL(0.0);
    }
    else if (!(*p->istor)) {
      /* Does this assume sizeof(MYFLT)==sizeof(int32)?? */
      p->pntr = (MYFLT *) p->auxch.auxp;
      memset(p->auxch.auxp, '\0', nbytes);
      p->prvt = FL(0.0);
      p->coef = FL(0.0);
    }
    return OK;
}

int comb(CSOUND *csound, COMB *p)
{
    int n, nsmps = csound->ksmps;
    MYFLT       *ar, *asig, *xp, *endp;
    MYFLT       coef = p->coef;

    if (p->auxch.auxp==NULL) goto err1; /* RWD fix */
    if (p->prvt != *p->krvt) {
      p->prvt = *p->krvt;
#ifdef __alpha__
      /*
       * The argument to exp() in the following is sometimes a small
       * enough negative number to result in a denormal (or worse)
       * on Alpha. So if the result would be less than 1.0e-16, we
       * just say it's zero and don't call exp().  heh 981101
       */
      double exp_arg = (double)(log001 * *p->ilpt / p->prvt);
      if (exp_arg < -36.8413615)    /* ln(1.0e-16) */
        coef = p->coef = FL(0.0);
      else
        coef = p->coef = (MYFLT)exp(exp_arg);
#else
      coef = p->coef = (MYFLT)exp((double)(log001 * *p->ilpt / p->prvt));
#endif
    }
    xp = p->pntr;
    endp = (MYFLT *) p->auxch.endp;
    ar = p->ar;
    asig = p->asig;
    for (n=0; n<nsmps; n++) {
      ar[n] = *xp;
      *xp *= coef;
      *xp += asig[n];
      if (++xp >= endp)
        xp = (MYFLT *) p->auxch.auxp;
    }
    p->pntr = xp;
    return OK;
 err1:
    return csound->PerfError(csound, Str("comb: not initialised"));
}

int alpass(CSOUND *csound, COMB *p)
{
    int n, nsmps = csound->ksmps;
    MYFLT       *ar, *asig, *xp, *endp;
    MYFLT       y, z;
    MYFLT       coef = p->coef;

    if (p->auxch.auxp==NULL) goto err1; /* RWD fix */
    if (p->prvt != *p->krvt) {
      p->prvt = *p->krvt;
      coef = p->coef = EXP(log001 * *p->ilpt / p->prvt);
    }
    xp = p->pntr;
    endp = (MYFLT *) p->auxch.endp;
    ar = p->ar;
    asig = p->asig;
    for (n=0; n<nsmps; n++) {
      y = *xp;
      *xp++ = z = coef * y + asig[n];
      ar[n] = y - coef * z;
      if (xp >= endp)
        xp = (MYFLT *) p->auxch.auxp;
    }
    p->pntr = xp;
    return OK;
 err1:
    return csound->PerfError(csound, Str("alpass: not initialised"));
}

static const MYFLT revlptimes[6] = {FL(0.0297), FL(0.0371), FL(0.0411),
                                    FL(0.0437), FL(0.0050), FL(0.0017)};

void reverbinit(CSOUND *csound)         /* called once by oload */
{                                       /*  to init reverb data */
    const MYFLT *lptimp = revlptimes;
    int32       *lpsizp = csound->revlpsiz;
    int n = 6;

    if (csound->revlpsum==0) {
      csound->revlpsum = 0;
      for (n=0; n<6; n++) {
        lpsizp[n] = (int32) (lptimp[n] * csound->esr + 0.5);
        csound->revlpsum += lpsizp[n];
      }
    }
}

int rvbset(CSOUND *csound, REVERB *p)
{
    if (p->auxch.auxp == NULL) {                        /* if no space yet, */
      int32      *sizp = csound->revlpsiz;               /*    allocate it   */
      csound->AuxAlloc(csound, csound->revlpsum * sizeof(MYFLT), &p->auxch);
      p->adr1 = p->p1 = (MYFLT *) p->auxch.auxp;
      p->adr2 = p->p2 = p->adr1 + *sizp++;
      p->adr3 = p->p3 = p->adr2 + *sizp++;              /*    & init ptrs   */
      p->adr4 = p->p4 = p->adr3 + *sizp++;
      p->adr5 = p->p5 = p->adr4 + *sizp++;
      p->adr6 = p->p6 = p->adr5 + *sizp++;
      if (p->adr6 + *sizp != (MYFLT *) p->auxch.endp) {
        csound->Die(csound, Str("revlpsiz inconsistent\n"));
      }
      p->prvt = FL(0.0);
    }
    else if (!(*p->istor)) {                    /* else if istor = 0 */
      memset(p->adr1, '\0', csound->revlpsum * sizeof(MYFLT));
      p->p1 = p->adr1;                          /*  and reset   */
      p->p2 = p->adr2;
      p->p3 = p->adr3;
      p->p4 = p->adr4;
      p->p5 = p->adr5;
      p->p6 = p->adr6;
      p->prvt = FL(0.0);
    }
    return OK;
}

int reverb(CSOUND *csound, REVERB *p)
{
    MYFLT       *asig, *p1, *p2, *p3, *p4, *p5, *p6, *ar, *endp;
    MYFLT       c1,c2,c3,c4,c5,c6;
    int n, nsmps = csound->ksmps;

    if (p->auxch.auxp==NULL) goto err1; /* RWD fix */
    if (p->prvt != *p->krvt) {
      const MYFLT *lptimp = revlptimes;
      MYFLT       logdrvt = log001 / *p->krvt;
      c1=p->c1 = EXP(logdrvt * *lptimp++);
      c2=p->c2 = EXP(logdrvt * *lptimp++);
      c3=p->c3 = EXP(logdrvt * *lptimp++);
      c4=p->c4 = EXP(logdrvt * *lptimp++);
      c5=p->c5 = EXP(logdrvt * *lptimp++);
      c6=p->c6 = EXP(logdrvt * *lptimp++);
    }
    else {
      c1=p->c1;
      c2=p->c2;
      c3=p->c3;
      c4=p->c4;
      c5=p->c5;
      c6=p->c6;
   }

    p1 = p->p1;
    p2 = p->p2;
    p3 = p->p3;
    p4 = p->p4;
    p5 = p->p5;
    p6 = p->p6;
    endp = (MYFLT *) p->auxch.endp;

    ar = p->ar;
    asig = p->asig;
    for (n=0; n<nsmps; n++) {
      MYFLT     cmbsum, y1, y2, z;
      MYFLT     sig = asig[n];
      cmbsum = *p1 + *p2 + *p3 + *p4;
      *p1 = c1 * *p1 + sig;
      *p2 = c2 * *p2 + sig;
      *p3 = c3 * *p3 + sig;
      *p4 = c4 * *p4 + sig;
      p1++; p2++; p3++; p4++;
      y1 = *p5;
      *p5++ = z = c5 * y1 + cmbsum;
      y1 -= c5 * z;
      y2 = *p6;
      *p6++ = z = c6 * y2 + y1;
      ar[n] = y2 - c6 * z;
      if (p1 >= p->adr2) p1 = p->adr1;
      if (p2 >= p->adr3) p2 = p->adr2;
      if (p3 >= p->adr4) p3 = p->adr3;
      if (p4 >= p->adr5) p4 = p->adr4;
      if (p5 >= p->adr6) p5 = p->adr5;
      if (p6 >= endp)    p6 = p->adr6;
    }
    p->p1 = p1;
    p->p2 = p2;
    p->p3 = p3;
    p->p4 = p4;
    p->p5 = p5;
    p->p6 = p6;
    return OK;
 err1:
    return csound->PerfError(csound, Str("reverb: not intialised"));
}

int panset(CSOUND *csound, PAN *p)
{
    FUNC  *ftp;

    if ((ftp = csound->FTFind(csound, p->ifn)) == NULL)
      return NOTOK;
    p->ftp = ftp;
    p->xmul = (*p->imode == FL(0.0) ? FL(1.0) : (MYFLT)ftp->flen);
    p->xoff = (*p->ioffset == FL(0.0) ? (MYFLT)ftp->flen * FL(0.5) : FL(0.0));

    return OK;
}

int pan(CSOUND *csound, PAN *p)
{
    MYFLT   flend2, xndx_f, yndx_f, xt, yt, ch1, ch2, ch3, ch4;
    int32   xndx, yndx, flen;
    int     n, nsmps = csound->ksmps;
    FUNC    *ftp;

    ftp = p->ftp;
    if (ftp == NULL) goto err1;          /* RWD fix */
    xndx_f = (*p->kx * p->xmul) - p->xoff;
    yndx_f = (*p->ky * p->xmul) - p->xoff;
    flen = ftp->flen;
    flend2 = (MYFLT)flen * FL(0.5);
    xt = (MYFLT)fabs(xndx_f);
    yt = (MYFLT)fabs(yndx_f);
    if (xt > flend2 || yt > flend2) {
      if (xt > yt)
        yndx_f *= (flend2 / xt);
      else
        xndx_f *= (flend2 / yt);
    }
    xndx_f += flend2;
    yndx_f += flend2;
    xndx = MYFLT2LRND(xndx_f);
    yndx = MYFLT2LRND(yndx_f);
    xndx = (xndx >= 0L ? (xndx < flen ? xndx : flen) : 0L);
    yndx = (yndx >= 0L ? (yndx < flen ? yndx : flen) : 0L);
    ch1 = ftp->ftable[flen - xndx] * ftp->ftable[yndx];
    ch2 = ftp->ftable[xndx]        * ftp->ftable[yndx];
    ch3 = ftp->ftable[flen - xndx] * ftp->ftable[flen - yndx];
    ch4 = ftp->ftable[xndx]        * ftp->ftable[flen - yndx];
    for (n=0; n<nsmps; n++) {
      MYFLT sig = p->asig[n];
      p->r1[n] = sig * ch1;
      p->r2[n] = sig * ch2;
      p->r3[n] = sig * ch3;
      p->r4[n] = sig * ch4;
    }

    return OK;
 err1:
    return csound->PerfError(csound, Str("pan: not initialised"));
}


Generated by  Doxygen 1.6.0   Back to index