<commit_before>/*
    vdelay.c:

    Copyright (C) 1994, 1998, 2000, 2001 <NAME>, <NAME>,
                                         <NAME>, <NAME>

    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
*/

/*      vdelay, multitap, reverb2 coded by <NAME> 1994 */
/*      Berklee College of Music Csound development team        */
/*      Copyright (c) May 1994.  All rights reserved            */

#include "csoundCore.h"

#include <math.h>
#include "vdelay.h"

#define ESR     (csound->esr/FL(1000.0))

int vdelset(CSOUND *csound, VDEL *p)            /*  vdelay set-up   */
{
    uint32 n = (int32)(*p->imaxd * ESR)+1;

    if (!*p->istod) {
      if (p->aux.auxp == NULL || (int32)(n * sizeof(MYFLT)) > p->aux.size)
        /* allocate space for delay buffer */
        csound->AuxAlloc(csound, n * sizeof(MYFLT), &p->aux);
      else {     /*    make sure buffer is empty       */
        memset(p->aux.auxp, 0, n*sizeof(MYFLT));
      }
      p->left = 0;
    }
    return OK;
}

int vdelay(CSOUND *csound, VDEL *p)               /*      vdelay  routine */
{
    int32  nn, nsmps = csound->ksmps, maxd, indx;
    MYFLT *out = p->sr;     /* assign object data to local variables   */
    MYFLT *in = p->ain;
    MYFLT *del = p->adel;
    MYFLT *buf = (MYFLT *)p->aux.auxp;

    if (UNLIKELY(buf==NULL)) goto err1;        /* RWD fix */
    maxd = (uint32) (1+*p->imaxd * ESR);
    indx = p->left;

    if (XINARG2) {          /*      if delay is a-rate      */
      for (nn=0; nn<nsmps; nn++) {
        MYFLT  fv1, fv2;
        int32   v1, v2;

        buf[indx] = in[nn];
        fv1 = indx - (del[nn]) * ESR;
        /* Make sure Inside the buffer      */
        /*
         * The following has been fixed by adding a cast and making a
         * ">=" instead of a ">" comparison. The order of the comparisons
         * has been swapped as well (a bit of a nit, but comparing a
         * possibly negative number to an unsigned isn't a good idea--and
         * broke on Alpha).
         * heh 981101
         */
        while (UNLIKELY(fv1 < FL(0.0)))
          fv1 += (MYFLT)maxd;
        while (UNLIKELY(fv1 >= (MYFLT)maxd))
          fv1 -= (MYFLT)maxd;

        if (fv1 < maxd - 1) /* Find next sample for interpolation      */
          fv2 = fv1 + FL(1.0);
        else
          fv2 = FL(0.0);

        v1 = (int32)fv1;
        v2 = (int32)fv2;
        out[nn] = buf[v1] + (fv1 - v1) * ( buf[v2] - buf[v1]);

        if (UNLIKELY(++indx == maxd))
          indx = 0;             /* Advance current pointer */

      }
    }
    else {                      /* and, if delay is k-rate */
      MYFLT fdel=*del;
      for (nn=0; nn<nsmps; nn++) {
        MYFLT  fv1, fv2;
        int32   v1, v2;

        buf[indx] = in[nn];
        fv1 = indx - fdel * ESR;
        /* Make sure inside the buffer      */
        /*
         * See comment above--same fix applied here.  heh 981101
         */
        while (UNLIKELY(fv1 < FL(0.0)))
          fv1 += (MYFLT)maxd;
        while (UNLIKELY(fv1 >= (MYFLT)maxd))
          fv1 -= (MYFLT)maxd;

        if (fv1 < maxd - 1) /* Find next sample for interpolation      */
          fv2 = fv1 + FL(1.0);
        else<commit_msg>minor optimisations<commit_after>/*
    vdelay.c:

    Copyright (C) 1994, 1998, 2000, 2001 <NAME>, <NAME>,
                                         <NAME>, <NAME>

    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
*/

/*      vdelay, multitap, reverb2 coded by <NAME> 1994 */
/*      Berklee College of Music Csound development team        */
/*      Copyright (c) May 1994.  All rights reserved            */

#include "csoundCore.h"

#include <math.h>
#include "vdelay.h"

#define ESR     (csound->esr/FL(1000.0))

int vdelset(CSOUND *csound, VDEL *p)            /*  vdelay set-up   */
{
    uint32 n = (int32)(*p->imaxd * ESR)+1;

    if (!*p->istod) {
      if (p->aux.auxp == NULL || (int32)(n * sizeof(MYFLT)) > p->aux.size)
        /* allocate space for delay buffer */
        csound->AuxAlloc(csound, n * sizeof(MYFLT), &p->aux);
      else {     /*    make sure buffer is empty       */
        memset(p->aux.auxp, 0, n*sizeof(MYFLT));
      }
      p->left = 0;
    }
    return OK;
}

int vdelay(CSOUND *csound, VDEL *p)               /*      vdelay  routine */
{
    int32  nn, nsmps = csound->ksmps, maxd, indx;
    MYFLT *out = p->sr;     /* assign object data to local variables   */
    MYFLT *in = p->ain;
    MYFLT *del = p->adel;
    MYFLT *buf = (MYFLT *)p->aux.auxp;

    if (UNLIKELY(buf==NULL)) goto err1;        /* RWD fix */
    maxd = (uint32) (1+*p->imaxd * ESR);
    indx = p->left;

    if (XINARG2) {          /*      if delay is a-rate      */
      for (nn=0; nn<nsmps; nn++) {
        MYFLT  fv1, fv2;
        int32   v1, v2;

        buf[indx] = in[nn];
        fv1 = indx - (del[nn]) * ESR;
        /* Make sure Inside the buffer      */
        /*
         * The following has been fixed by adding a cast and making a
         * ">=" instead of a ">" comparison. The order of the comparisons
         * has been swapped as well (a bit of a nit, but comparing a
         * possibly negative number to an unsigned isn't a good idea--and
         * broke on Alpha).
         * heh 981101
         */
        while (UNLIKELY(fv1 < FL(0.0)))
          fv1 += (MYFLT)maxd;
        while (UNLIKELY(fv1 >= (MYFLT)maxd))
          fv1 -= (MYFLT)maxd;

        if (LIKELY(fv1 < maxd - 1)) /* Find next sample for interpolation      */
          fv2 = fv1 + FL(1.0);
        else
          fv2 = FL(0.0);

        v1 = (int32)fv1;
        v2 = (int32)fv2;
        out[nn] = buf[v1] + (fv1 - v1) * ( buf[v2] - buf[v1]);

        if (UNLIKELY(++indx == maxd))
          indx = 0;             /* Advance current pointer */

      }
    }
    else {                      /* and, if delay is k-rate */
      MYFLT fdel=*del;
      for (nn=0; nn<nsmps; nn++) {
        MYFLT  fv1, fv2;
        int32   v1, v2;

        buf[indx] = in[nn];
        fv1 = indx - fdel * ESR;
        /* Make sure inside the buffer      */
        /*
         * See comment above--same fix applied here.  heh 981101
         */
        while (UNLIKELY(fv1 < FL(0.0)))
          fv1 += (MYFLT)maxd;
        while (UNLIKELY(fv1 >= (MYFLT)maxd))
          fv1 -= (MYFLT)maxd;

        if (LIKELY(fv1 < maxd - 1)) /* Find next sample for interpolation      */
          fv2 = fv1 + FL(1.0);
        else
{'fraction_non_alphanumeric': 0.12426489388903093, 'fraction_numerical': 0.03349526975198159, 'mean_word_length': 1.7565186751233264, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2676742', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2606, 'n_words': 854}