<commit_before>/*
    phisem.c:

    Copyright (C) 1997, 2000 <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 ublic
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

/**********************************************************/
/*  PhISEM (Physically Informed Stochastic Event Modeling */
/*    by <NAME>, Princeton, February 1997          */
/*                                                        */
/*  Meta-model that simulates all of:                     */
/*  Maraca Simulation by <NAME>, Princeton, 1996-7 */
/*  Sekere Simulation by <NAME>, Princeton, 1996-7 */
/*  Cabasa Simulation by <NAME>, Princeton, 1996-7 */
/*  Bamboo Windchime Simulation, by <NAME>, 1996-7 */
/*  Water Drops Simulation, by <NAME>, 1996-7      */
/*  Tambourine Simulation, by <NAME>, 1996-7       */
/*  Sleighbells Simulation, by <NAME>, 1996-7      */
/*  Guiro Simulation, by <NAME>, 1996-7            */
/*                                                        */
/**********************************************************/
/*  PhOLIES (Physically-Oriented Library of               */
/*    Imitated Environmental Sounds), <NAME>, 1997-9  */
/*                                                        */
/*  Stix1 (walking on brittle sticks)                     */
/*  Crunch1 (like new fallen snow, or not)                */
/*  Wrench (basic socket wrench, friend of guiro)         */
/*  Sandpapr (sandpaper)                                  */
/**********************************************************/

// #include "csdl.h"
#include "csoundCore.h"
#include "interlocks.h"
#include "phisem.h"
#include <math.h>

/* To do
   "10: Wrench", "12: CokeCan"};
*/

static inline int my_random(CSOUND *csound, int max)
{                                   /* Return Random Int Between 0 and max */
    return (csound->Rand31(&(csound->randSeed1)) % (max + 1));
}

static MYFLT noise_tick(CSOUND *csound)
{                         /* Return random MYFLT float between -1.0 and 1.0 */
    MYFLT temp;
    temp = (MYFLT) csound->Rand31(&(csound->randSeed1)) - FL(1073741823.5);
    return (temp * (MYFLT) (1.0 / 1073741823.0));
}

/************************* MARACA *****************************/
#define MARA_SOUND_DECAY  FL(0.95)
#define MARA_SYSTEM_DECAY FL(0.999)
#define MARA_GAIN         FL(20.0)
#define MARA_NUM_BEANS    25
#define MARA_CENTER_FREQ  FL(3200.0)
#define MARA_RESON        FL(0.96)
/***********************  SEKERE *****************************/
#define SEKE_SOUND_DECAY  FL(0.96)
#define SEKE_SYSTEM_DECAY FL(0.999)
#define SEKE_GAIN         FL(20.0)
#define SEKE_NUM_BEANS    FL(64.0)
#define SEKE_CENTER_FREQ  FL(5500.0)
#define SEKE_RESON        FL(0.6)
/***********************  SANDPAPER **************************/
#define SANDPAPR_SOUND_DECAY FL(0.999)
#define SANDPAPR_SYSTEM_DECAY FL(0.999)
#define SANDPAPR_GAIN     FL(0.5)
#define SANDPAPR_NUM_GRAINS FL(128.0)
#define SANDPAPR_CENTER_FREQ FL(4500.0)
#define SANDPAPR_RESON    FL(0.6)
/*(********************** CABASA *****************************/
#define CABA_SOUND_DECAY  FL(0.96)
#define CABA_SYSTEM_DECAY FL(0.997)
#define CABA_GAIN         FL(40.0)
#define CABA_NUM_BEADS    512
#define CABA_CENTER_FREQ  FL(3000.0)
#define CABA_RESON        FL(0.7)
/************************ Bamboo Wind Chimes *****************/
#define BAMB_SOUND_DECAY  FL(0.95)
#define BAMB_SYSTEM_DECAY FL(0.9999)
#define BAMB_GAIN         FL(2.0)
#define BAMB_NUM_TUBES    FL(1.25)
#define BAMB_CENTER_FREQ0 FL(2800.0)
#define BAMB_CENTER_FREQ1 (FL(0.8) * FL(2800.0))
#define BAMB_CENTER_FREQ2 (FL(1.2) * FL(2800.0))
#define BAMB_RESON        FL(0.995)
/******************* Water Drops  ****************************/
#define WUTR_SOUND_DECAY  FL(0.95)
#define WUTR_SYSTEM_DECAY FL(0.996)
#define WUTR_GAIN         FL(1.0)
#define WUTR_NUM_SOURCES  FL(10.0)
#define WUTR_CENTER_FREQ0 FL(450.0)
#define WUTR_CENTER_FREQ1 FL(600.0)
#define WUTR_CENTER_FREQ2 FL(750.0)
#define WUTR_RESON        FL(0.9985)
#define WUTR_FREQ_SWEEP   FL(1.0001)
/****************** TAMBOURINE  *****************************/
#define TAMB_SOUND_DECAY  FL(0.95)
#define TAMB_SYSTEM_DECAY FL(0.9985)
#define TAMB_GAIN         FL(5.0)
#define TAMB_NUM_TIMBRELS 32
#define TAMB_SHELL_FREQ   FL(2300.0)
#define TAMB_SHELL_GAIN   FL(0.1)
#define TAMB_SHELL_RESON  FL(0.96)
#define TAMB_CYMB_FREQ1   FL(5600.0)
#define TAMB_CYMB_FREQ2   FL(8100.0)
#define TAMB_CYMB_RESON   FL(0.99)
/********************** SLEIGHBELLS *************************/
#define SLEI_SOUND_DECAY  FL(0.97)
#define SLEI_SYSTEM_DECAY FL(0.9994)
#define SLEI_GAIN         FL(1.0)
#define SLEI_NUM_BELLS    32
#define SLEI_CYMB_FREQ0   FL(2500.0)
#define SLEI_CYMB_FREQ1   FL(5300.0)
#define SLEI_CYMB_FREQ2   FL(6500.0)
#define SLEI_CYMB_FREQ3   FL(8300.0)
#define SLEI_CYMB_FREQ4   FL(9800.0)
#define SLEI_CYMB_RESON   FL(0.99)
/***************************  GUIRO  ***********************/
#define GUIR_SOUND_DECAY  FL(0.95)
#define GUIR_GAIN         FL(10.0)
#define GUIR_NUM_PARTS    128
#define GUIR_GOURD_FREQ   FL(2500.0)
#define GUIR_GOURD_RESON  FL(0.97)
#define GUIR_GOURD_FREQ2  FL(4000.0)
#define GUIR_GOURD_RESON2 FL(0.97)
/**************************  WRENCH  ***********************/
#define WRENCH_SOUND_DECAY FL(0.95)
#define WRENCH_GAIN       5
#define WRENCH_NUM_PARTS  FL(128.0)
#define WRENCH_FREQ       FL(3200.0)
#define WRENCH_RESON      FL(0.99)
#define WRENCH_FREQ2      FL(8000.)
#define WRENCH_RESON2     FL(0.992)
/************************ COKECAN **************************/
#define COKECAN_SOUND_DECAY FL(0.97)
#define COKECAN_SYSTEM_DECAY FL(0.999)
#define COKECAN_GAIN      FL(0.8)
#define COKECAN_NUM_PARTS 48
#define COKECAN_HELMFREQ  FL(370.0)
#define COKECAN_HELM_RES  FL(0.99)
#define COKECAN_METLFREQ0 FL(1025.0)
#define COKECAN_METLFREQ1 FL(1424.0)
#define COKECAN_METLFREQ2 FL(2149.0)
#define COKECAN_METLFREQ3 FL(3596.0)
#define COKECAN_METL_RES  FL(0.992)
/************************************************************/
/*  PhOLIES (Physically-Oriented Library of                 */
/*    Imitated Environmental Sounds), <NAME>, 1997-8    */
/************************************************************/

/***********************  STIX1 *****************************/
#define STIX1_SOUND_DECAY FL(0.96)
#define STIX1_SYSTEM_DECAY FL(0.998)
#define STIX1_GAIN        FL(30.0)
#define STIX1_NUM_BEANS   FL(2.0)
#define STIX1_CENTER_FREQ FL(5500.0)
#define STIX1_RESON       FL(0.6)
/************************ Crunch1 ***************************/
#define CRUNCH1_SOUND_DECAY FL(0.95)
#define CRUNCH1_SYSTEM_DECAY FL(0.99806)
#define CRUNCH1_GAIN      FL(20.0)
#define CRUNCH1_NUM_BEADS 7
#define CRUNCH1_CENTER_FREQ FL(800.0)
#define CRUNCH1_RESON     FL(0.95)

#define MAX_SHAKE FL(2000.0)
#define MIN_ENERGY FL(0.0)          /* 0.1 or 0.3?? */

static int cabasaset(CSOUND *csound, CABASA *p)
{
    p->sndLevel = FL(0.0);
    p->kloop = (int)(p->h.insdshead->offtim * CS_EKR)
               - (int)(CS_EKR * *p->dettack);
    p->outputs0 = FL(0.0);
    p->outputs1 = FL(0.0);
    p->shake_maxSave = FL(0.0);
    p->totalEnergy  = FL(0.0);
    p->num_objects = CABA_NUM_BEADS;
    p->soundDecay = CABA_SOUND_DECAY;
    p->systemDecay = CABA_SYSTEM_DECAY;
    p->gain = LOG((MYFLT)CABA_NUM_BEADS)*CABA_GAIN/(MYFLT)CABA_NUM_BEADS;
    p->resons = CABA_RESON;
    p->coeffs1 = CABA_RESON * CABA_RESON;
    p->coeffs0 = - CABA_RESON * FL(2.0) * COS(CABA_CENTER_FREQ * csound->tpidsr);
                                /* Note On */
    p->shakeEnergy = *p->amp * csound->dbfs_to_float * MAX_SHAKE * FL(0.1);
    if (p->shakeEnergy > MAX_SHAKE) p->shakeEnergy = MAX_SHAKE;
    p->last_num = FL(0.0);
    return OK;
}

static int cabasa(CSOUND *csound, CABASA *p)
{
    MYFLT *ar = p->ar;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;
    MYFLT data;
                                /* Use locals for speed */
    MYFLT shakeEnergy = p->shakeEnergy;
    MYFLT systemDecay = p->systemDecay;
    MYFLT sndLevel    = p->sndLevel;
    MYFLT soundDecay  = p->soundDecay;
    MYFLT input;
    MYFLT outputs0    = p->outputs0;
    MYFLT outputs1    = p->outputs1;
    MYFLT coeff0      = p->coeffs0;
    MYFLT coeff1      = p->coeffs1;
    MYFLT gain        = p->gain;

    if (*p->num_beads != p->last_num) { /* # beans has changed */
      p->last_num = *p->num_beads;
      if ((int32)(*p->num_beads+FL(0.5)) != p->num_objects) {
        p->num_objects = (int32)(*p->num_beads+FL(0.5));
        if (p->num_objects >= 1) {
          gain = p->gain = LOG((MYFLT)p->num_objects) /
            FL(1.38629436111989061883) /* (MYFLT)log(4.0)*/ * FL(40.0) /
            (MYFLT) p->num_objects;
        }
      }
    }

    if (*p->damp != FL(0.0)) {
      systemDecay = p->systemDecay = FL(0.998) + (*p->damp * FL(0.002));
    }

    if (*p->shake_max != FL(0.0)) {
      shakeEnergy = p->shakeEnergy +=
        CS_KSMPS * *p->shake_max * MAX_SHAKE * FL(0.1);
      if (shakeEnergy > MAX_SHAKE) shakeEnergy = MAX_SHAKE;
    }

    if (p->kloop>0 && p->h.insdshead->relesing) p->kloop=1;
    if ((--p->kloop) == 0) {
      shakeEnergy = FL(0.0);
    }

    if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
    }
    for (n=offset;n<nsmps;n++) {
/*        if (shakeEnergy > MIN_ENERGY) { */
      shakeEnergy *= systemDecay;               /* Exponential system decay */
      if (my_random(csound, 1024) < p->num_objects) {
        sndLevel += gain * shakeEnergy;
      }
      input = sndLevel * noise_tick(csound);    /* Actual Sound is Random */
      sndLevel *= soundDecay;                   /* Exponential Sound decay  */
      input -= outputs0*coeff0; /* Do */
      input -= outputs1*coeff1; /* resonant */
      outputs1 = outputs0;      /* filter */
      outputs0 = input;         /* calculations */
      data =  outputs0 - outputs1;
/*          if (data > 10000.0f)        data = 10000.0f; */
/*          if (data < -10000.0f) data = -10000.0f; */
      ar[n] = data * FL(0.0005) * csound->e0dbfs ;
/*        } */
/*        else { */
/*          *ar++ = 0.0f; */
/*        } */
    }
    p->shakeEnergy = shakeEnergy;
    p->sndLevel = sndLevel;
    p->outputs0 = outputs0;
    p->outputs1 = outputs1;
    return OK;
}

static int sekereset(CSOUND *csound, SEKERE *p)
{
    p->sndLevel = FL(0.0);
    p->kloop = (int)(p->h.insdshead->offtim * CS_EKR)
               - (int)(CS_EKR * *p->dettack);
    p->outputs0 = FL(0.0);
    p->outputs1 = FL(0.0);
    p->finalZ2 = FL(0.0);
    p->finalZ1 = FL(0.0);
    p->finalZ0 = FL(0.0);
    p->shake_maxSave = FL(0.0);
    p->totalEnergy  = FL(0.0);
    p->num_objects = SEKE_NUM_BEANS;
    p->soundDecay = SEKE_SOUND_DECAY;
    p->systemDecay = SEKE_SYSTEM_DECAY;
    p->gain = LOG(SEKE_NUM_BEANS)*SEKE_GAIN/SEKE_NUM_BEANS;
    p->resons = SEKE_RESON;
    p->coeffs1 = SEKE_RESON * SEKE_RESON;
    p->coeffs0 = - SEKE_RESON * FL(2.0) *
      COS(SEKE_CENTER_FREQ * csound->tpidsr);
                                /* Note On */
    p->shakeEnergy = *p->amp * csound->dbfs_to_float * MAX_SHAKE * FL(0.1);
    if (p->shakeEnergy > MAX_SHAKE) p->shakeEnergy = MAX_SHAKE;
    p->last_num = FL(0.0);
    return OK;
}

static int sekere(CSOUND *csound, SEKERE *p)
{
    MYFLT *ar = p->ar;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;
    MYFLT data;
                                /* Use locals for speed */
    MYFLT shakeEnergy = p->shakeEnergy;
    MYFLT systemDecay = p->systemDecay;
    MYFLT sndLevel = p->sndLevel;
    MYFLT soundDecay = p->soundDecay;
    MYFLT input;
    MYFLT outputs0 = p->outputs0;
    MYFLT outputs1 = p->outputs1;
    MYFLT coeff0   = p->coeffs0;
    MYFLT coeff1   = p->coeffs1;
    MYFLT gain     = p->gain;

    if (*p->num_beads != p->last_num) {
      p->last_num = *p->num_beads;
      if ((int32)(*p->num_beads+FL(0.5)) != p->num_objects) {
        p->num_objects = *p->num_beads;
        if (p->num_objects >= 1) {
          gain = p->gain = LOG((MYFLT)p->num_objects) /
            FL(1.38629436111989061883) /* (MYFLT)log(4.0)*/ * FL(120.0) /
            (MYFLT) p->num_objects;
        }
      }
    }

    if (*p->damp != FL(0.0)) {
      systemDecay = p->systemDecay = FL(0.998) + (*p->damp * FL(0.002));
    }

    if (*p->shake_max != FL(0.0)) {
      shakeEnergy = p->shakeEnergy +=
        CS_KSMPS * *p->shake_max * MAX_SHAKE * FL(0.1);
      if (shakeEnergy > MAX_SHAKE) shakeEnergy = MAX_SHAKE;
    }

    if (p->kloop>0 && p->h.insdshead->relesing) p->kloop=1;
    if ((--p->kloop) == 0) {
      shakeEnergy = FL(0.0);
    }

    if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
    }
    for (n=offset;n<nsmps;n++) {
/*        if (shakeEnergy > MIN_ENERGY) { */
      shakeEnergy *= systemDecay;           /* Exponential system decay */
      if (my_random(csound, 1024) < p->num_objects) {
        sndLevel += gain * shakeEnergy;
      }
      input = sndLevel * noise_tick(csound);  /* Actual Sound is Random */
      sndLevel *= soundDecay;               /* Exponential Sound decay  */
      input -= outputs0*coeff0;             /* Do */
      input -= outputs1*coeff1;             /* resonant */
      outputs1 = outputs0;                  /* filter */
      outputs0 = input;                     /* calculations */
      p->finalZ2 = p->finalZ1;
      p->finalZ1 = p->finalZ0;
      p->finalZ0 = p->outputs1;
      data = p->finalZ0 - p->finalZ2;
/*          if (data > 10000.0f)        data = 10000.0f; */
/*          if (data < -10000.0f) data = -10000.0f; */
      ar[n] = data * FL(0.0005) * csound->e0dbfs ;
/*        } */
/*        else { */
/*          ar[n] = 0.0f; */
/*        } */
    }
    printf("%d/%d:\n", offset, early);
    p->shakeEnergy = shakeEnergy;
    p->sndLevel = sndLevel;
    p->outputs0 = outputs0;
    p->outputs1 = outputs1;
    return OK;
}

static int sandset(CSOUND *csound, SEKERE *p)
{
    p->sndLevel = FL(0.0);
    p->kloop = (int)(p->h.insdshead->offtim * CS_EKR)
               - (int)(CS_EKR * *p->dettack);
    p->outputs0 = FL(0.0);
    p->outputs1 = FL(0.0);
    p->finalZ2 = FL(0.0);
    p->finalZ1 = FL(0.0);
    p->finalZ0 = FL(0.0);
    p->shake_maxSave = FL(0.0);
    p->totalEnergy  = FL(0.0);
    p->num_objects = SANDPAPR_NUM_GRAINS;
    p->soundDecay = SANDPAPR_SOUND_DECAY;
    p->systemDecay = SANDPAPR_SYSTEM_DECAY;
    p->gain = LOG(SANDPAPR_NUM_GRAINS) *
      SANDPAPR_GAIN / SANDPAPR_NUM_GRAINS;
    p->resons = SANDPAPR_RESON;
    p->coeffs1 = SANDPAPR_RESON * SANDPAPR_RESON;
    p->coeffs0 = - SANDPAPR_RESON * FL(2.0) *
      COS(SANDPAPR_CENTER_FREQ * csound->tpidsr);
                                /* Note On */<commit_msg>fix cabasa and sekere<commit_after>/*
    phisem.c:

    Copyright (C) 1997, 2000 <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 ublic
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

/**********************************************************/
/*  PhISEM (Physically Informed Stochastic Event Modeling */
/*    by <NAME>, Princeton, February 1997          */
/*                                                        */
/*  Meta-model that simulates all of:                     */
/*  Maraca Simulation by <NAME>, Princeton, 1996-7 */
/*  Sekere Simulation by <NAME>, Princeton, 1996-7 */
/*  Cabasa Simulation by <NAME>, Princeton, 1996-7 */
/*  Bamboo Windchime Simulation, by <NAME>, 1996-7 */
/*  Water Drops Simulation, by <NAME>, 1996-7      */
/*  Tambourine Simulation, by <NAME>, 1996-7       */
/*  Sleighbells Simulation, by <NAME>, 1996-7      */
/*  Guiro Simulation, by <NAME>, 1996-7            */
/*                                                        */
/**********************************************************/
/*  PhOLIES (Physically-Oriented Library of               */
/*    Imitated Environmental Sounds), <NAME>, 1997-9  */
/*                                                        */
/*  Stix1 (walking on brittle sticks)                     */
/*  Crunch1 (like new fallen snow, or not)                */
/*  Wrench (basic socket wrench, friend of guiro)         */
/*  Sandpapr (sandpaper)                                  */
/**********************************************************/

// #include "csdl.h"
#include "csoundCore.h"
#include "interlocks.h"
#include "phisem.h"
#include <math.h>

/* To do
   "10: Wrench", "12: CokeCan"};
*/

static inline int my_random(CSOUND *csound, int max)
{                                   /* Return Random Int Between 0 and max */
    return (csound->Rand31(&(csound->randSeed1)) % (max + 1));
}

static MYFLT noise_tick(CSOUND *csound)
{                         /* Return random MYFLT float between -1.0 and 1.0 */
    MYFLT temp;
    temp = (MYFLT) csound->Rand31(&(csound->randSeed1)) - FL(1073741823.5);
    return (temp * (MYFLT) (1.0 / 1073741823.0));
}

/************************* MARACA *****************************/
#define MARA_SOUND_DECAY  FL(0.95)
#define MARA_SYSTEM_DECAY FL(0.999)
#define MARA_GAIN         FL(20.0)
#define MARA_NUM_BEANS    25
#define MARA_CENTER_FREQ  FL(3200.0)
#define MARA_RESON        FL(0.96)
/***********************  SEKERE *****************************/
#define SEKE_SOUND_DECAY  FL(0.96)
#define SEKE_SYSTEM_DECAY FL(0.999)
#define SEKE_GAIN         FL(20.0)
#define SEKE_NUM_BEANS    FL(64.0)
#define SEKE_CENTER_FREQ  FL(5500.0)
#define SEKE_RESON        FL(0.6)
/***********************  SANDPAPER **************************/
#define SANDPAPR_SOUND_DECAY FL(0.999)
#define SANDPAPR_SYSTEM_DECAY FL(0.999)
#define SANDPAPR_GAIN     FL(0.5)
#define SANDPAPR_NUM_GRAINS FL(128.0)
#define SANDPAPR_CENTER_FREQ FL(4500.0)
#define SANDPAPR_RESON    FL(0.6)
/*(********************** CABASA *****************************/
#define CABA_SOUND_DECAY  FL(0.96)
#define CABA_SYSTEM_DECAY FL(0.997)
#define CABA_GAIN         FL(40.0)
#define CABA_NUM_BEADS    512
#define CABA_CENTER_FREQ  FL(3000.0)
#define CABA_RESON        FL(0.7)
/************************ Bamboo Wind Chimes *****************/
#define BAMB_SOUND_DECAY  FL(0.95)
#define BAMB_SYSTEM_DECAY FL(0.9999)
#define BAMB_GAIN         FL(2.0)
#define BAMB_NUM_TUBES    FL(1.25)
#define BAMB_CENTER_FREQ0 FL(2800.0)
#define BAMB_CENTER_FREQ1 (FL(0.8) * FL(2800.0))
#define BAMB_CENTER_FREQ2 (FL(1.2) * FL(2800.0))
#define BAMB_RESON        FL(0.995)
/******************* Water Drops  ****************************/
#define WUTR_SOUND_DECAY  FL(0.95)
#define WUTR_SYSTEM_DECAY FL(0.996)
#define WUTR_GAIN         FL(1.0)
#define WUTR_NUM_SOURCES  FL(10.0)
#define WUTR_CENTER_FREQ0 FL(450.0)
#define WUTR_CENTER_FREQ1 FL(600.0)
#define WUTR_CENTER_FREQ2 FL(750.0)
#define WUTR_RESON        FL(0.9985)
#define WUTR_FREQ_SWEEP   FL(1.0001)
/****************** TAMBOURINE  *****************************/
#define TAMB_SOUND_DECAY  FL(0.95)
#define TAMB_SYSTEM_DECAY FL(0.9985)
#define TAMB_GAIN         FL(5.0)
#define TAMB_NUM_TIMBRELS 32
#define TAMB_SHELL_FREQ   FL(2300.0)
#define TAMB_SHELL_GAIN   FL(0.1)
#define TAMB_SHELL_RESON  FL(0.96)
#define TAMB_CYMB_FREQ1   FL(5600.0)
#define TAMB_CYMB_FREQ2   FL(8100.0)
#define TAMB_CYMB_RESON   FL(0.99)
/********************** SLEIGHBELLS *************************/
#define SLEI_SOUND_DECAY  FL(0.97)
#define SLEI_SYSTEM_DECAY FL(0.9994)
#define SLEI_GAIN         FL(1.0)
#define SLEI_NUM_BELLS    32
#define SLEI_CYMB_FREQ0   FL(2500.0)
#define SLEI_CYMB_FREQ1   FL(5300.0)
#define SLEI_CYMB_FREQ2   FL(6500.0)
#define SLEI_CYMB_FREQ3   FL(8300.0)
#define SLEI_CYMB_FREQ4   FL(9800.0)
#define SLEI_CYMB_RESON   FL(0.99)
/***************************  GUIRO  ***********************/
#define GUIR_SOUND_DECAY  FL(0.95)
#define GUIR_GAIN         FL(10.0)
#define GUIR_NUM_PARTS    128
#define GUIR_GOURD_FREQ   FL(2500.0)
#define GUIR_GOURD_RESON  FL(0.97)
#define GUIR_GOURD_FREQ2  FL(4000.0)
#define GUIR_GOURD_RESON2 FL(0.97)
/**************************  WRENCH  ***********************/
#define WRENCH_SOUND_DECAY FL(0.95)
#define WRENCH_GAIN       5
#define WRENCH_NUM_PARTS  FL(128.0)
#define WRENCH_FREQ       FL(3200.0)
#define WRENCH_RESON      FL(0.99)
#define WRENCH_FREQ2      FL(8000.)
#define WRENCH_RESON2     FL(0.992)
/************************ COKECAN **************************/
#define COKECAN_SOUND_DECAY FL(0.97)
#define COKECAN_SYSTEM_DECAY FL(0.999)
#define COKECAN_GAIN      FL(0.8)
#define COKECAN_NUM_PARTS 48
#define COKECAN_HELMFREQ  FL(370.0)
#define COKECAN_HELM_RES  FL(0.99)
#define COKECAN_METLFREQ0 FL(1025.0)
#define COKECAN_METLFREQ1 FL(1424.0)
#define COKECAN_METLFREQ2 FL(2149.0)
#define COKECAN_METLFREQ3 FL(3596.0)
#define COKECAN_METL_RES  FL(0.992)
/************************************************************/
/*  PhOLIES (Physically-Oriented Library of                 */
/*    Imitated Environmental Sounds), <NAME>, 1997-8    */
/************************************************************/

/***********************  STIX1 *****************************/
#define STIX1_SOUND_DECAY FL(0.96)
#define STIX1_SYSTEM_DECAY FL(0.998)
#define STIX1_GAIN        FL(30.0)
#define STIX1_NUM_BEANS   FL(2.0)
#define STIX1_CENTER_FREQ FL(5500.0)
#define STIX1_RESON       FL(0.6)
/************************ Crunch1 ***************************/
#define CRUNCH1_SOUND_DECAY FL(0.95)
#define CRUNCH1_SYSTEM_DECAY FL(0.99806)
#define CRUNCH1_GAIN      FL(20.0)
#define CRUNCH1_NUM_BEADS 7
#define CRUNCH1_CENTER_FREQ FL(800.0)
#define CRUNCH1_RESON     FL(0.95)

#define MAX_SHAKE FL(2000.0)
#define MIN_ENERGY FL(0.0)          /* 0.1 or 0.3?? */

static int cabasaset(CSOUND *csound, CABASA *p)
{
    p->sndLevel = FL(0.0);
    p->kloop = (int)(p->h.insdshead->offtim * CS_EKR)
               - (int)(CS_EKR * *p->dettack);
    p->outputs0 = FL(0.0);
    p->outputs1 = FL(0.0);
    p->shake_maxSave = FL(0.0);
    p->totalEnergy  = FL(0.0);
    p->num_objects = CABA_NUM_BEADS;
    p->soundDecay = CABA_SOUND_DECAY;
    p->systemDecay = CABA_SYSTEM_DECAY;
    p->gain = LOG((MYFLT)CABA_NUM_BEADS)*CABA_GAIN/(MYFLT)CABA_NUM_BEADS;
    p->resons = CABA_RESON;
    p->coeffs1 = CABA_RESON * CABA_RESON;
    p->coeffs0 = - CABA_RESON * FL(2.0) * COS(CABA_CENTER_FREQ * csound->tpidsr);
                                /* Note On */
    p->shakeEnergy = *p->amp * MAX_SHAKE * FL(0.1);
    if (p->shakeEnergy > MAX_SHAKE) p->shakeEnergy = MAX_SHAKE;
    p->last_num = FL(0.0);
    return OK;
}

static int cabasa(CSOUND *csound, CABASA *p)
{
    MYFLT *ar = p->ar;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;
    MYFLT data;
                                /* Use locals for speed */
    MYFLT shakeEnergy = p->shakeEnergy;
    MYFLT systemDecay = p->systemDecay;
    MYFLT sndLevel    = p->sndLevel;
    MYFLT soundDecay  = p->soundDecay;
    MYFLT input;
    MYFLT outputs0    = p->outputs0;
    MYFLT outputs1    = p->outputs1;
    MYFLT coeff0      = p->coeffs0;
    MYFLT coeff1      = p->coeffs1;
    MYFLT gain        = p->gain;

    if (*p->num_beads != p->last_num) { /* # beans has changed */
      p->last_num = *p->num_beads;
      if ((int32)(*p->num_beads+FL(0.5)) != p->num_objects) {
        p->num_objects = (int32)(*p->num_beads+FL(0.5));
        if (p->num_objects >= 1) {
          gain = p->gain = LOG((MYFLT)p->num_objects) /
            FL(1.38629436111989061883) /* (MYFLT)log(4.0)*/ * FL(40.0) /
            (MYFLT) p->num_objects;
        }
      }
    }

    if (*p->damp != FL(0.0)) {
      systemDecay = p->systemDecay = FL(0.998) + (*p->damp * FL(0.002));
    }

    if (*p->shake_max != FL(0.0)) {
      shakeEnergy = p->shakeEnergy +=
        CS_KSMPS * *p->shake_max * MAX_SHAKE * FL(0.1);
      if (shakeEnergy > MAX_SHAKE) shakeEnergy = MAX_SHAKE;
    }

    if (p->kloop>0 && p->h.insdshead->relesing) p->kloop=1;
    if ((--p->kloop) == 0) {
      shakeEnergy = FL(0.0);
    }

    if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
    }
    for (n=offset;n<nsmps;n++) {
/*        if (shakeEnergy > MIN_ENERGY) { */
      shakeEnergy *= systemDecay;               /* Exponential system decay */
      if (my_random(csound, 1024) < p->num_objects) {
        sndLevel += gain * shakeEnergy;
      }
      input = sndLevel * noise_tick(csound);    /* Actual Sound is Random */
      sndLevel *= soundDecay;                   /* Exponential Sound decay  */
      input -= outputs0*coeff0; /* Do */
      input -= outputs1*coeff1; /* resonant */
      outputs1 = outputs0;      /* filter */
      outputs0 = input;         /* calculations */
      data =  outputs0 - outputs1;
/*          if (data > 10000.0f)        data = 10000.0f; */
/*          if (data < -10000.0f) data = -10000.0f; */
      ar[n] = data * FL(0.0005) * csound->e0dbfs ;
/*        } */
/*        else { */
/*          *ar++ = 0.0f; */
/*        } */
    }
    p->shakeEnergy = shakeEnergy;
    p->sndLevel = sndLevel;
    p->outputs0 = outputs0;
    p->outputs1 = outputs1;
    return OK;
}

static int sekereset(CSOUND *csound, SEKERE *p)
{
    p->sndLevel = FL(0.0);
    p->kloop = (int)(p->h.insdshead->offtim * CS_EKR)
               - (int)(CS_EKR * *p->dettack);
    p->outputs0 = FL(0.0);
    p->outputs1 = FL(0.0);
    p->finalZ2 = FL(0.0);
    p->finalZ1 = FL(0.0);
    p->finalZ0 = FL(0.0);
    p->shake_maxSave = FL(0.0);
    p->totalEnergy  = FL(0.0);
    p->num_objects = SEKE_NUM_BEANS;
    p->soundDecay = SEKE_SOUND_DECAY;
    p->systemDecay = SEKE_SYSTEM_DECAY;
    p->gain = LOG(SEKE_NUM_BEANS)*SEKE_GAIN/SEKE_NUM_BEANS;
    p->resons = SEKE_RESON;
    p->coeffs1 = SEKE_RESON * SEKE_RESON;
    p->coeffs0 = - SEKE_RESON * FL(2.0) *
      COS(SEKE_CENTER_FREQ * csound->tpidsr);
                                /* Note On */
    p->shakeEnergy = *p->amp * MAX_SHAKE * FL(0.1);
    if (p->shakeEnergy > MAX_SHAKE) p->shakeEnergy = MAX_SHAKE;
    p->last_num = FL(0.0);
    return OK;
}

static int sekere(CSOUND *csound, SEKERE *p)
{
    MYFLT *ar = p->ar;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;
    MYFLT data;
                                /* Use locals for speed */
    MYFLT shakeEnergy = p->shakeEnergy;
    MYFLT systemDecay = p->systemDecay;
    MYFLT sndLevel = p->sndLevel;
    MYFLT soundDecay = p->soundDecay;
    MYFLT input;
    MYFLT outputs0 = p->outputs0;
    MYFLT outputs1 = p->outputs1;
    MYFLT coeff0   = p->coeffs0;
    MYFLT coeff1   = p->coeffs1;
    MYFLT gain     = p->gain;

    if (*p->num_beads != p->last_num) {
      p->last_num = *p->num_beads;
      if ((int32)(*p->num_beads+FL(0.5)) != p->num_objects) {
        p->num_objects = *p->num_beads;
        if (p->num_objects >= 1) {
          gain = p->gain = LOG((MYFLT)p->num_objects) /
            FL(1.38629436111989061883) /* (MYFLT)log(4.0)*/ * FL(120.0) /
            (MYFLT) p->num_objects;
        }
      }
    }

    if (*p->damp != FL(0.0)) {
      systemDecay = p->systemDecay = FL(0.998) + (*p->damp * FL(0.002));
    }

    if (*p->shake_max != FL(0.0)) {
      shakeEnergy = p->shakeEnergy +=
        CS_KSMPS * *p->shake_max * MAX_SHAKE * FL(0.1);
      if (shakeEnergy > MAX_SHAKE) shakeEnergy = MAX_SHAKE;
    }

    if (p->kloop>0 && p->h.insdshead->relesing) p->kloop=1;
    if ((--p->kloop) == 0) {
      shakeEnergy = FL(0.0);
    }

    if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
    }
    for (n=offset;n<nsmps;n++) {
/*        if (shakeEnergy > MIN_ENERGY) { */
      shakeEnergy *= systemDecay;           /* Exponential system decay */
      if (my_random(csound, 1024) < p->num_objects) {
        sndLevel += gain * shakeEnergy;
      }
      input = sndLevel * noise_tick(csound);  /* Actual Sound is Random */
      sndLevel *= soundDecay;               /* Exponential Sound decay  */
      input -= outputs0*coeff0;             /* Do */
      input -= outputs1*coeff1;             /* resonant */
      outputs1 = outputs0;                  /* filter */
      outputs0 = input;                     /* calculations */
      p->finalZ2 = p->finalZ1;
      p->finalZ1 = p->finalZ0;
      p->finalZ0 = p->outputs1;
      data = p->finalZ0 - p->finalZ2;
/*          if (data > 10000.0f)        data = 10000.0f; */
/*          if (data < -10000.0f) data = -10000.0f; */
      ar[n] = data * FL(0.0005) * csound->e0dbfs ;
/*        } */
/*        else { */
/*          ar[n] = 0.0f; */
/*        } */
    }
    //printf("%d/%d:\n", offset, early);
    p->shakeEnergy = shakeEnergy;
    p->sndLevel = sndLevel;
    p->outputs0 = outputs0;
    p->outputs1 = outputs1;
    return OK;
}

static int sandset(CSOUND *csound, SEKERE *p)
{
    p->sndLevel = FL(0.0);
    p->kloop = (int)(p->h.insdshead->offtim * CS_EKR)
               - (int)(CS_EKR * *p->dettack);
    p->outputs0 = FL(0.0);
    p->outputs1 = FL(0.0);
    p->finalZ2 = FL(0.0);
    p->finalZ1 = FL(0.0);
    p->finalZ0 = FL(0.0);
    p->shake_maxSave = FL(0.0);
    p->totalEnergy  = FL(0.0);
    p->num_objects = SANDPAPR_NUM_GRAINS;
    p->soundDecay = SANDPAPR_SOUND_DECAY;
    p->systemDecay = SANDPAPR_SYSTEM_DECAY;
    p->gain = LOG(SANDPAPR_NUM_GRAINS) *
      SANDPAPR_GAIN / SANDPAPR_NUM_GRAINS;
    p->resons = SANDPAPR_RESON;
    p->coeffs1 = SANDPAPR_RESON * SANDPAPR_RESON;
    p->coeffs0 = - SANDPAPR_RESON * FL(2.0) *
      COS(SANDPAPR_CENTER_FREQ * csound->tpidsr);
                                /* Note On */
{'fraction_non_alphanumeric': 0.1966266914641185, 'fraction_numerical': 0.0536779324055666, 'mean_word_length': 2.582242131863083, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 387, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2079010', 'n_tokens_mistral': 13050, 'n_tokens_neox': 12051, 'n_words': 2325}