#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ugens.h>
#include "strums.h"
#include <globals.h>

extern void squisher(int squish, strumq *q);


void randfill(float amp, int squish, strumq *q)

/* Fills plucked string structure q with random values, and intitialize things.
Call only after a call to sset.
Can be used with zero amplitude to just initialize things.
Squish models the softness of a plucking implement by filtering
the values put in the string with an averaging filter.
The filter makes squish passes. The loss of amplitude at the fundamental
frequency is compensated for, but the overall amplitude of the squished
string is lowered, as the energy at other frequencies is decreased. */

{


float total,average;
int i;
FILE *f;

if((f = fopen("/home/veronica/RTcmix-3.0.5/insts.std-3.0.5/VMSTRUM/randomvalues.dat", "w")) == NULL){

die("NULL", "Cannot open file\n");
exit(EXIT_FAILURE);

}
q->p = q->n;

q->dcz1=0;

/*set all values of array to zero*/
for(i=0;i<maxlen;i++) {

q->d[i]=0.;

}

/* fill with white noise and subtract any dc component */
total = 0.;
for( i=0; i < q->n; i++) {

/* generate random number and save to file */

q->d[i] = rrand() * amp;
fprintf(f, "%f\n", q->d[i]);
total = total + q->d[i];

}

average=total/((float)q->n);

for(i=0;i<q->n;i++) {

q->d[i]= q->d[i] - average;

}

squisher(squish,q);

fclose(f);

}