c - audio delay making it work -
i trying implement simple audio delay in c. made test delay program operated on printed sinewave , worked effectively. tried incorporating delay process in sfprocess - libsndfile- replacing sinewave inputs audio 'data' input.
i have instead of clean sample delay getting sorts of glitching , distortion.
any ideas on how correct this?
#include <stdio.h> #include </usr/local/include/sndfile.h>//libsamplerate libsamplerate //#include </usr/local/include/samplerate.h> #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]; // buffer carries samples double circular [44100] = {0}; // circular buffer delay (int = 0; < 44100; i++) { circular[i] = 0; } // 0 circular buffer int circular_pointer = 0; // in circular buffer //float myvolume; // volume entered user optional 3rd argument sndfile *infile, *outfile; sf_info sfinfo; int readcount; const char *infilename = null; const char *outfilename = null; if(argc < arg_nargs) { printf("usage: %s infile outfile\n",argv[dt_progname]); return 1; } //if(argc > arg_nargs) { // // myvolume = argv[dt_vol]; //}; infilename = argv[arg_infile]; outfilename = argv[arg_outfile]; if (! (infile = sf_open (infilename, sfm_read, &sfinfo))) {printf ("not able open input file %s.\n", infilename) ; puts (sf_strerror (null)) ; return 1 ; }; if (! (outfile = sf_open (outfilename, sfm_write, &sfinfo))) { printf ("not able 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 %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 (int ind = 0; ind < buffer_len; ind++){ circular_pointer = fmod(ind,wraparound); // wrap around pointer circular[circular_pointer] = data[ind]; playhead = fmod(ind-delay, wraparound); // read delayed signal data[ind] = circular[playhead]; // output delayed signal circular[ind] = data[ind]; // write incoming signal }; //volume /*for (j=0; j<numchannels; j++) { (k=0; k<count; k++){ data[k] = data[k]*-vol;*/ //}printf ("the volume %f", vol); return; }
at least 1 problem pass circular_pointer value, not reference. when update in function, it's same value next time call function.
i think on right track, here, if want that's structured bit better, might want checkout answer:
Comments
Post a Comment