123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
-
- #include "smbPitchShift.h"
- #include <string.h>
- #include <math.h>
- #include <stdio.h>
- #define M_PI 3.14159265358979323846
- #if 0
- #define MAX_FRAME_LENGTH 8192
- #endif
- void smbFft(float *fftBuffer, long fftFrameSize, long sign);
- double smbAtan2(double x, double y);
- void smbPitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata)
- {
- static float gInFIFO[MAX_FRAME_LENGTH];
- static float gOutFIFO[MAX_FRAME_LENGTH];
- static float gFFTworksp[2*MAX_FRAME_LENGTH];
- static float gLastPhase[MAX_FRAME_LENGTH/2+1];
- static float gSumPhase[MAX_FRAME_LENGTH/2+1];
- static float gOutputAccum[2*MAX_FRAME_LENGTH];
- static float gAnaFreq[MAX_FRAME_LENGTH];
- static float gAnaMagn[MAX_FRAME_LENGTH];
- static float gSynFreq[MAX_FRAME_LENGTH];
- static float gSynMagn[MAX_FRAME_LENGTH];
- static long gRover = false, gInit = false;
- double magn, phase, tmp, window, real, imag;
- double freqPerBin, expct;
- long i,k, qpd, index, inFifoLatency, stepSize, fftFrameSize2;
-
- fftFrameSize2 = fftFrameSize/2;
- stepSize = fftFrameSize/osamp;
- freqPerBin = sampleRate/(double)fftFrameSize;
- expct = 2.*M_PI*(double)stepSize/(double)fftFrameSize;
- inFifoLatency = fftFrameSize-stepSize;
- if (gRover == false) gRover = inFifoLatency;
-
- if (gInit == false) {
- memset(gInFIFO, 0, MAX_FRAME_LENGTH*sizeof(float));
- memset(gOutFIFO, 0, MAX_FRAME_LENGTH*sizeof(float));
- memset(gFFTworksp, 0, 2*MAX_FRAME_LENGTH*sizeof(float));
- memset(gLastPhase, 0, (MAX_FRAME_LENGTH/2+1)*sizeof(float));
- memset(gSumPhase, 0, (MAX_FRAME_LENGTH/2+1)*sizeof(float));
- memset(gOutputAccum, 0, 2*MAX_FRAME_LENGTH*sizeof(float));
- memset(gAnaFreq, 0, MAX_FRAME_LENGTH*sizeof(float));
- memset(gAnaMagn, 0, MAX_FRAME_LENGTH*sizeof(float));
- gInit = true;
- }
-
- for (i = 0; i < numSampsToProcess; i++){
-
- gInFIFO[gRover] = indata[i];
- outdata[i] = gOutFIFO[gRover-inFifoLatency];
- gRover++;
-
- if (gRover >= fftFrameSize) {
- gRover = inFifoLatency;
-
- for (k = 0; k < fftFrameSize;k++) {
- window = -.5*cos(2.*M_PI*(double)k/(double)fftFrameSize)+.5;
- gFFTworksp[2*k] = gInFIFO[k] * window;
- gFFTworksp[2*k+1] = 0.;
- }
-
-
- smbFft(gFFTworksp, fftFrameSize, -1);
-
- for (k = 0; k <= fftFrameSize2; k++) {
-
- real = gFFTworksp[2*k];
- imag = gFFTworksp[2*k+1];
-
- magn = 2.*sqrt(real*real + imag*imag);
- phase = atan2(imag,real);
-
- tmp = phase - gLastPhase[k];
- gLastPhase[k] = phase;
-
- tmp -= (double)k*expct;
-
- qpd = tmp/M_PI;
- if (qpd >= 0) qpd += qpd&1;
- else qpd -= qpd&1;
- tmp -= M_PI*(double)qpd;
-
- tmp = osamp*tmp/(2.*M_PI);
-
- tmp = (double)k*freqPerBin + tmp*freqPerBin;
-
- gAnaMagn[k] = magn;
- gAnaFreq[k] = tmp;
- }
-
-
- memset(gSynMagn, 0, fftFrameSize*sizeof(float));
- memset(gSynFreq, 0, fftFrameSize*sizeof(float));
- for (k = 0; k <= fftFrameSize2; k++) {
- index = k*pitchShift;
- if (index <= fftFrameSize2) {
- gSynMagn[index] += gAnaMagn[k];
- gSynFreq[index] = gAnaFreq[k] * pitchShift;
- }
- }
-
-
-
- for (k = 0; k <= fftFrameSize2; k++) {
-
- magn = gSynMagn[k];
- tmp = gSynFreq[k];
-
- tmp -= (double)k*freqPerBin;
-
- tmp /= freqPerBin;
-
- tmp = 2.*M_PI*tmp/osamp;
-
- tmp += (double)k*expct;
-
- gSumPhase[k] += tmp;
- phase = gSumPhase[k];
-
- gFFTworksp[2*k] = magn*cos(phase);
- gFFTworksp[2*k+1] = magn*sin(phase);
- }
-
- for (k = fftFrameSize+2; k < 2*fftFrameSize; k++) gFFTworksp[k] = 0.;
-
- smbFft(gFFTworksp, fftFrameSize, 1);
-
- for(k=0; k < fftFrameSize; k++) {
- window = -.5*cos(2.*M_PI*(double)k/(double)fftFrameSize)+.5;
- gOutputAccum[k] += 2.*window*gFFTworksp[2*k]/(fftFrameSize2*osamp);
- }
- for (k = 0; k < stepSize; k++) gOutFIFO[k] = gOutputAccum[k];
-
- memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*sizeof(float));
-
- for (k = 0; k < inFifoLatency; k++) gInFIFO[k] = gInFIFO[k+stepSize];
- }
- }
- }
- void smbFft(float *fftBuffer, long fftFrameSize, long sign)
- {
- float wr, wi, arg, *p1, *p2, temp;
- float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;
- long i, bitm, j, le, le2, k;
- for (i = 2; i < 2*fftFrameSize-2; i += 2) {
- for (bitm = 2, j = 0; bitm < 2*fftFrameSize; bitm <<= 1) {
- if (i & bitm) j++;
- j <<= 1;
- }
- if (i < j) {
- p1 = fftBuffer+i; p2 = fftBuffer+j;
- temp = *p1; *(p1++) = *p2;
- *(p2++) = temp; temp = *p1;
- *p1 = *p2; *p2 = temp;
- }
- }
- #if 0
- for (k = 0, le = 2; k < (long)(log(fftFrameSize)/log(2.)+.5); k++) {
- #else
- for (k = 0, le = 2; k < (long)(log((double)fftFrameSize)/log(2.)+.5); k++) {
- #endif
- le <<= 1;
- le2 = le>>1;
- ur = 1.0;
- ui = 0.0;
- arg = M_PI / (le2>>1);
- wr = cos(arg);
- wi = sign*sin(arg);
- for (j = 0; j < le2; j += 2) {
- p1r = fftBuffer+j; p1i = p1r+1;
- p2r = p1r+le2; p2i = p2r+1;
- for (i = j; i < 2*fftFrameSize; i += le) {
- tr = *p2r * ur - *p2i * ui;
- ti = *p2r * ui + *p2i * ur;
- *p2r = *p1r - tr; *p2i = *p1i - ti;
- *p1r += tr; *p1i += ti;
- p1r += le; p1i += le;
- p2r += le; p2i += le;
- }
- tr = ur*wr - ui*wi;
- ui = ur*wi + ui*wr;
- ur = tr;
- }
- }
- }
- double smbAtan2(double x, double y)
- {
- double signx;
- if (x > 0.) signx = 1.;
- else signx = -1.;
-
- if (x == 0.) return 0.;
- if (y == 0.) return signx * M_PI / 2.;
-
- return atan2(x, y);
- }
|