diff --git a/src/spicelib/devices/vsrc/vsjack.c b/src/spicelib/devices/vsrc/vsjack.c index 4da16835d..a1197fe2c 100644 --- a/src/spicelib/devices/vsrc/vsjack.c +++ b/src/spicelib/devices/vsrc/vsjack.c @@ -12,13 +12,13 @@ #include "ngspice/ngspice.h"" #define MAX_D 6 -char *(sources[MAX_D]); +char* (sources[MAX_D]); -SNDFILE * m_sndfile[MAX_D]; +SNDFILE* m_sndfile[MAX_D]; int m_channels[MAX_D]; //< number of channles in src-file uint32_t m_samplerate[MAX_D]; //< samplerate of source uint32_t m_frames[MAX_D]; //< duration of source in frames -float *(interleaved[MAX_D]); //< internal soundfile buffer +float* (interleaved[MAX_D]); //< internal soundfile buffer uint32_t ilb_start[MAX_D]; //< first sample in buffer uint32_t ilb_end[MAX_D]; //< last sample in buffer @@ -26,158 +26,159 @@ uint32_t ilb_end[MAX_D]; //< last sample in buffer #ifdef HAVE_SRC #include -double src_ratio=64.0; +double src_ratio = 64.0; #define SRC_RATIO src_ratio -SRC_STATE *rabbit[MAX_D]; +SRC_STATE* rabbit[MAX_D]; int rabbit_err[MAX_D]; -float *(resampled[MAX_D]); //< internal soundfile buffer +float* (resampled[MAX_D]); //< internal soundfile buffer #endif void vsjack_initialize(void) { - int d; - for (d=0;d0) { - ilb_end[d] = ilb_start[d] + nframes; - } else { - ilb_end[d] = ilb_start[d]; - printf ("Decoder error.\n"); - } + sf_seek(m_sndfile[d], sample, SEEK_SET); + ilb_start[d] = sample; + uint32_t nframes; + if ((nframes = sf_readf_float(m_sndfile[d], (interleaved[d]), VS_BUFSIZ)) > 0) { + ilb_end[d] = ilb_start[d] + nframes; + } + else { + ilb_end[d] = ilb_start[d]; + printf("Decoder error.\n"); + } #ifdef HAVE_SRC - SRC_DATA src_data; - src_data.data_in = interleaved[d]; - src_data.data_out = resampled[d]; - src_data.input_frames = VS_BUFSIZ; - src_data.output_frames = VS_BUFSIZ*SRC_RATIO; - src_data.end_of_input = ((ilb_end[d]-ilb_start[d]) < VS_BUFSIZ); - src_data.src_ratio = SRC_RATIO; - src_data.input_frames_used = 0; - src_data.output_frames_gen = 0; + SRC_DATA src_data; + src_data.data_in = interleaved[d]; + src_data.data_out = resampled[d]; + src_data.input_frames = VS_BUFSIZ; + src_data.output_frames = VS_BUFSIZ * SRC_RATIO; + src_data.end_of_input = ((ilb_end[d] - ilb_start[d]) < VS_BUFSIZ); + src_data.src_ratio = SRC_RATIO; + src_data.input_frames_used = 0; + src_data.output_frames_gen = 0; - src_process(rabbit[d], &src_data); + src_process(rabbit[d], &src_data); #endif } double get_value(int d, double time, int channel) { - uint32_t sample = (uint32_t) floor(time*((double)m_samplerate[d])); + uint32_t sample = (uint32_t)floor(time * ((double)m_samplerate[d])); - // TODO: print EOF warning (once). FIXME move to load_buffer - if (sample > m_frames[d] ) return (0.0); + // TODO: print EOF warning (once). FIXME move to load_buffer + if (sample > m_frames[d]) return (0.0); - if (sample < ilb_start[d] || sample >= ilb_end[d]) - load_buffer(d, sample); + if (sample < ilb_start[d] || sample >= ilb_end[d]) + load_buffer(d, sample); - if (sample < ilb_start[d] || sample >= ilb_end[d]) { - printf ("no such value buffered for file:%i.\n",d); - return (0.0); // nan ? - } + if (sample < ilb_start[d] || sample >= ilb_end[d]) { + printf("no such value buffered for file:%i.\n", d); + return (0.0); // nan ? + } #ifdef HAVE_SRC - int offset = floor((sample - ilb_start[d])*SRC_RATIO); - if (offset > VS_BUFSIZ*SRC_RATIO || offset < 0) { - printf ("value not in buffer:%i.\n",d); - return (0.0); // nan ? - } - float val = ((float *)(resampled[d]))[m_channels[d]*offset+channel]; + int offset = floor((sample - ilb_start[d]) * SRC_RATIO); + if (offset > VS_BUFSIZ * SRC_RATIO || offset < 0) { + printf("value not in buffer:%i.\n", d); + return (0.0); // nan ? + } + float val = ((float*)(resampled[d]))[m_channels[d] * offset + channel]; # if 0 // DEBUG # define SQUARE(A) ((A)*(A)) - static double stride =0; - static double last =0; - static double deviation =0; - static int dev_cnt = 0; - if (channel == 0) { - stride += (SRC_RATIO*time*((double)m_samplerate[d])) - last; - last = (SRC_RATIO*time*((double)m_samplerate[d])); - deviation += SQUARE((SRC_RATIO*time*((double)m_samplerate[d])) - floor(SRC_RATIO*time*((double)m_samplerate[d]))); - dev_cnt ++; - if ((dev_cnt%(12000))==0) - printf ("read time dev= %f - stride= %f\n", sqrt(deviation/(double)dev_cnt), stride/(double)dev_cnt); - } + static double stride = 0; + static double last = 0; + static double deviation = 0; + static int dev_cnt = 0; + if (channel == 0) { + stride += (SRC_RATIO * time * ((double)m_samplerate[d])) - last; + last = (SRC_RATIO * time * ((double)m_samplerate[d])); + deviation += SQUARE((SRC_RATIO * time * ((double)m_samplerate[d])) - floor(SRC_RATIO * time * ((double)m_samplerate[d]))); + dev_cnt++; + if ((dev_cnt % (12000)) == 0) + printf("read time dev= %f - stride= %f\n", sqrt(deviation / (double)dev_cnt), stride / (double)dev_cnt); + } # endif # if 0 // zero order hold. - return((double)val); + return((double)val); # else - // linear interpolation - float val1 = ((float *)(resampled[d]))[(m_channels[d]*(offset+1))+channel]; - double diff = (SRC_RATIO*time*((double)m_samplerate[d])) - - floor(SRC_RATIO*time*((double)m_samplerate[d])); - double rv = ((double)val)*(1.0-diff) + ((double)val1)*diff; - return(rv); + // linear interpolation + float val1 = ((float*)(resampled[d]))[(m_channels[d] * (offset + 1)) + channel]; + double diff = (SRC_RATIO * time * ((double)m_samplerate[d])) - + floor(SRC_RATIO * time * ((double)m_samplerate[d])); + double rv = ((double)val) * (1.0 - diff) + ((double)val1) * diff; + return(rv); # endif #else // no upsampling. - int offset = sample - ilb_start[d]; - if (offset > VS_BUFSIZ || offset < 0) { - printf ("value not in buffer:%i.\n",d); - return (0.0); // nan ? - } - return((double)(((float *)(interleaved[d]))[m_channels[d]*offset+channel])) + int offset = sample - ilb_start[d]; + if (offset > VS_BUFSIZ || offset < 0) { + printf("value not in buffer:%i.\n", d); + return (0.0); // nan ? + } + return((double)(((float*)(interleaved[d]))[m_channels[d] * offset + channel])); #endif } @@ -186,35 +187,35 @@ double get_value(int d, double time, int channel) { * "public" functions */ -double vsjack_get_value (int d, double time, double time_offset, int channel, double oversampling) { - assert (d>=0 && d< MAX_D ); - if (m_sndfile[d] == NULL) return (0.0); // FIXME - if (oversampling > 0) src_ratio = oversampling; +double vsjack_get_value(int d, double time, double time_offset, int channel, double oversampling) { + assert(d >= 0 && d < MAX_D); + if (m_sndfile[d] == NULL) return (0.0); // FIXME + if (oversampling > 0) src_ratio = oversampling; - double value = get_value(d, time+time_offset, channel); - return (value); + double value = get_value(d, time + time_offset, channel); + return (value); } -void vsjack_set_file (int d , char *fn) { - assert (d>=0 && d< MAX_D ); - if (sources[d] != NULL) free (sources[d]); - sources[d] = strdup(fn); +void vsjack_set_file(int d, char* fn) { + assert(d >= 0 && d < MAX_D); + if (sources[d] != NULL) free(sources[d]); + sources[d] = strdup(fn); } -int vsjack_open (int d) { - static int initialized =0; - if (!initialized) { - initialized=1; - vsjack_initialize(); - } - if (d==-1) return -1;// initialize only - assert (d>=0 && d< MAX_D ); - assert (sources[d]!=NULL); - if (openfile_sf(d, sources[d])) { - fprintf (stderr, "could not open '%s'\n", sources[d]); - controlled_exit (1); - } - return (d); +int vsjack_open(int d) { + static int initialized = 0; + if (!initialized) { + initialized = 1; + vsjack_initialize(); + } + if (d == -1) return -1;// initialize only + assert(d >= 0 && d < MAX_D); + assert(sources[d] != NULL); + if (openfile_sf(d, sources[d])) { + fprintf(stderr, "could not open '%s'\n", sources[d]); + controlled_exit(1); + } + return (d); } /* vi:set ts=8 sts=4 sw=4: */