title: audio delay making it work
body: <p>I am trying to implement a simple audio delay in C.
  i previously made a test delay program which operated on a printed sinewave and worked effectively.
  I tried incorporating my delay as the process in the SFProcess - libsndfile- replacing the sinewave inputs with my audio 'data' input.</p>
  
  <p>I nearly have it but instead of a clean sample delay I am getting all sorts of glitching and distortion.</p>
  
  <p>Any ideas on how to correct this?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;/usr/local/include/sndfile.h&gt;//libsamplerate libsamplerate
  //#include &lt;/usr/local/include/samplerate.h&gt;
  
  #define BUFFER_LEN 1024 //defines buffer length
  #define MAX_CHANNELS 2 //defines max channels 
  
  static void process_data (double *data, double*circular,int count, int numchannels, int circular_pointer );
  
  enum {DT_PROGNAME,ARG_INFILE,ARG_OUTFILE,ARG_NARGS, DT_VOL};
  
  int main (int argc, const char * argv[])//Main
  {
      static double data [BUFFER_LEN]; // the buffer that carries the samples
  
      double circular [44100] = {0}; // the circular buffer for the delay
      for (int i = 0; i &lt; 44100; i++) { circular[i] = 0; }  // zero the circular buffer
  
      int circular_pointer = 0;          // where we currently are in the circular buffer
  
      //float myvolume; // the volume entered by the user as optional 3rd argument
  
      SNDFILE *infile, *outfile;
      SF_INFO sfinfo;
  
      int readcount;
      const char *infilename = NULL;
      const char  *outfilename = NULL;
  
      if(argc &lt; ARG_NARGS) {
          printf("usage: %s infile outfile\n",argv[DT_PROGNAME]);
          return 1;
      }
  
      //if(argc &gt; ARG_NARGS) {
      //  
      //  myvolume = argv[DT_VOL];
      //};
      infilename = argv[ARG_INFILE];
      outfilename = argv[ARG_OUTFILE];
  
      if (! (infile = sf_open (infilename, SFM_READ, &amp;sfinfo)))
  
      {printf ("Not able to open input file %s.\n", infilename) ;
          puts (sf_strerror (NULL)) ;
          return  1 ;
      };
  
      if (! (outfile = sf_open (outfilename, SFM_WRITE, &amp;sfinfo)))
      {   printf ("Not able to open output file %s.\n", outfilename) ;
          puts (sf_strerror (NULL)) ;
          return  1 ;
      } ;
  
      while ((readcount = sf_read_double (infile, data, BUFFER_LEN)))
      {   process_data (data, circular, readcount, sfinfo.channels,  circular_pointer) ;
          sf_write_double (outfile, data, readcount) ;
      };
  
      sf_close (infile) ;
      sf_close (outfile) ;
  
      printf("the sample rate is %d\n", sfinfo.samplerate);
  
      return 0;
  }
  
  
  static void process_data (double *data, double *circular, int count, int numchannels, int circular_pointer) {
  
      //int j,k;
      //float vol = 1;
      int playhead;
      int wraparound = 10000;
  
      float delay = 1000;  // delay time in samples
  
      for (int ind = 0; ind &lt; BUFFER_LEN; ind++){
  
          circular_pointer = fmod(ind,wraparound);     // wrap around pointer
          circular[circular_pointer] = data[ind];
  
  
          playhead = fmod(ind-delay, wraparound);     // read the delayed signal
  
          data[ind] = circular[playhead];            // output delayed signal
  
          circular[ind] = data[ind];   // write the incoming signal
      };
  
  
      //volume
      /*for (j=0; j&lt;numchannels; j++) {
          for (k=0; k&lt;count; k++){ 
              data[k] = data[k]*-vol;*/
  
          //}printf ("the volume is %f", vol);
  
      return;
  }
  </code></pre>