rlm@23: #include rlm@23: #include rlm@23: #include rlm@23: #include rlm@23: #include rlm@23: #include "SndfileWavUtil.h" rlm@23: rlm@23: const int Taps = 9; rlm@23: const int Points = 8; rlm@23: const int log_Points = 3; rlm@23: static int newest; rlm@23: static short int x[Taps] = {0,0,0,0,0,0,0,0,0}; rlm@23: static short int fir(short int); rlm@23: rlm@23: // foreward declaration. This is defined in DFT.cpp rlm@23: int DFT(int dir,int m,double *x1,double *y1); rlm@23: rlm@23: // lifted from the bluespec code rlm@23: static double h[Taps] = {-0.0124, rlm@23: 0.0, rlm@23: -0.0133, rlm@23: 0.0, rlm@23: 0.8181, rlm@23: 0.0, rlm@23: -0.0133, rlm@23: 0.0, rlm@23: -0.0124}; rlm@23: rlm@23: int rlm@23: main (int argc, char * argv []) rlm@23: { rlm@23: const char* inputWavFileName; rlm@23: const char* outputWavFileName; rlm@23: rlm@23: FILE *inputPcmFile; rlm@23: FILE *firPcmFile; rlm@23: FILE *outputPcmFile; rlm@23: rlm@23: short int sample; rlm@23: short int samples[Points]; rlm@23: rlm@23: inputWavFileName = argv[1]; rlm@23: outputWavFileName = argv[2]; rlm@23: rlm@23: // Convert input wav to pcm rlm@23: generate_pcm(inputWavFileName, "input.pcm"); rlm@23: rlm@23: inputPcmFile = fopen("input.pcm", "r"); rlm@23: firPcmFile = fopen("fir.pcm", "w"); rlm@23: rlm@23: assert(inputPcmFile); rlm@23: assert(firPcmFile); rlm@23: rlm@23: newest = 0; rlm@23: memset(x, 0, sizeof(x)); rlm@23: rlm@23: while(fread(&sample, 2, 1, inputPcmFile)) { rlm@23: sample = fir(sample); rlm@23: assert(fwrite(&sample,2,1,firPcmFile)); rlm@23: } rlm@23: rlm@23: fclose(inputPcmFile); rlm@23: fclose(firPcmFile); rlm@23: rlm@23: firPcmFile = fopen("fir.pcm", "r"); rlm@23: outputPcmFile = fopen("output.pcm", "w"); rlm@23: rlm@23: assert(firPcmFile); rlm@23: assert(outputPcmFile); rlm@23: rlm@23: // we will do an Points point fft, and then undo it rlm@23: while(true) { rlm@23: rlm@23: int read = fread(samples, 2,Points,firPcmFile); rlm@23: rlm@23: if(read == 0) rlm@23: break; rlm@23: rlm@23: // pad out the rlm@23: if(read>log_Points); rlm@23: dimag[i] = 0.0; rlm@23: } rlm@23: rlm@23: DFT(1,Points,dsamples,dimag); rlm@23: DFT(-1,Points,dsamples,dimag); rlm@23: rlm@23: for(int i = 0; i < Points; i++){ rlm@23: samples[i] = (int)dsamples[i]; rlm@23: } rlm@23: rlm@23: if(read < Points){ rlm@23: assert(fwrite(samples,2,read,outputPcmFile)==read); rlm@23: break; rlm@23: } else { rlm@23: assert(fwrite(samples,2,Points,outputPcmFile)==Points); rlm@23: } rlm@23: } rlm@23: rlm@23: fclose(firPcmFile); rlm@23: fclose(outputPcmFile); rlm@23: rlm@23: generate_wav("output.pcm", inputWavFileName, outputWavFileName); rlm@23: rlm@23: } rlm@23: rlm@23: short int rlm@23: fir(short int sample) rlm@23: { rlm@23: rlm@23: x[newest] = sample; rlm@23: double y = 0; rlm@23: rlm@23: for (int k = 0; k < Taps; k++) { rlm@23: rlm@23: short int bits = x[(newest+k)%Taps]; rlm@23: double x = (double)bits; rlm@23: y += h[k] * x; rlm@23: rlm@23: } rlm@23: rlm@23: newest = (newest == 0) ? (Taps-1) : (newest-1); rlm@23: rlm@23: short int rv = ((short int)y)&0x0000FFFF; rlm@23: return rv; rlm@23: } rlm@23: