AnsweredAssumed Answered

Somebody can help me correct this code.

Question asked by walkercc on Aug 19, 2013
Latest reply on Sep 1, 2013 by MaheshN

Hello, I have a ADSP-21369-ez-kit , and i'm design use visual audio, I found pitch shift of visual audio is sounds bad.

 

So, i want use other pitch shift algorithm from internet, but i replace code , it's can't work normal,

 

somebody can help me correct code ? thanks a  lot~

 


SEG_MOD_FAST_CODE void smbPitchShift(float pitchShift, unsigned int numSampsToProcess, unsigned int fftFrameSize, int osamp, float sampleRate, float *indata, float *outdata)
/*
          Routine smbPitchShift(). See top of file for explanation
          Purpose: doing pitch shifting while maintaining duration using the Short
          Time Fourier Transform.
          Author: (c)1999-2009 Stephan M. Bernsee <smb [AT] dspdimension [DOT] com>
*/
{


          float magn, phase, tmp, window, real, imag;
          float freqPerBin, expct;
          int i,k, qpd, index, inFifoLatency, stepSize, fftFrameSize2;
/* 
          for(i=0;i<numSampsToProcess;i++)
        outdata[i] = indata[i];
        
        return ;
        */
  


          /* set up some handy variables */
          fftFrameSize2 = fftFrameSize/2;
          stepSize = fftFrameSize/osamp;
          freqPerBin = sampleRate/(float)fftFrameSize;
          expct = 2.0*M_PI*(float)stepSize/(float)fftFrameSize;
          inFifoLatency = fftFrameSize-stepSize;
          if (gRover == false) gRover = inFifoLatency;


          /* main processing loop */
          for (i = 0; i < numSampsToProcess; i++){


                    /* As long as we have not yet collected enough data just read in */
                    gInFIFO[gRover] = indata[i];
                    outdata[i] = gOutFIFO[gRover-inFifoLatency];
                    gRover++;


                    /* now we have enough data for processing */
                    if (gRover >= fftFrameSize) {
                              gRover = inFifoLatency;


                              /* do windowing and re,im interleave */
                              for (k = 0; k < fftFrameSize;k++) {
                                        window = -.5*cos(2.*M_PI*(float)k/(float)fftFrameSize)+.5;
                                        gFFTworksp[2*k] = gInFIFO[k] * window;
                                        gFFTworksp[2*k+1] = 0.;
                              }




                              /* ***************** ANALYSIS ******************* */
                              /* do transform */
                              smbFft(gFFTworksp, fftFrameSize, -1);


                              /* this is the analysis step */
                              for (k = 0; k <= fftFrameSize2; k++) {


                                        /* de-interlace FFT buffer */
                                        real = gFFTworksp[2*k];
                                        imag = gFFTworksp[2*k+1];


                                        /* compute magnitude and phase */
                                        magn = 2.*sqrt(real*real + imag*imag);
                                        phase = atan2(imag,real);


                                        /* compute phase difference */
                                        tmp = phase - gLastPhase[k];
                                        gLastPhase[k] = phase;


                                        /* subtract expected phase difference */
                                        tmp -= (float)k*expct;


                                        /* map delta phase into +/- Pi interval */
                                        qpd = tmp/M_PI;
                                        if (qpd >= 0) qpd += qpd&1;
                                        else qpd -= qpd&1;
                                        tmp -= M_PI*(float)qpd;


                                        /* get deviation from bin frequency from the +/- Pi interval */
                                        tmp = osamp*tmp/(2.*M_PI);


                                        /* compute the k-th partials' true frequency */
                                        tmp = (float)k*freqPerBin + tmp*freqPerBin;


                                        /* store magnitude and true frequency in analysis arrays */
                                        gAnaMagn[k] = magn;
                                        gAnaFreq[k] = tmp;


                              }


                              /* ***************** PROCESSING ******************* */
                              /* this does the actual pitch shifting */
                              //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; 
                                        } 
                              }
  
                              /* ***************** SYNTHESIS ******************* */
                              /* this is the synthesis step */
                              for (k = 0; k <= fftFrameSize2; k++) {


                                        /* get magnitude and true frequency from synthesis arrays */
                                        magn = gSynMagn[k];
                                        tmp = gSynFreq[k];


                                        /* subtract bin mid frequency */
                                        tmp -= (float)k*freqPerBin;


                                        /* get bin deviation from freq deviation */
                                        tmp /= freqPerBin;


                                        /* take osamp into account */
                                        tmp = 2.*M_PI*tmp/osamp;


                                        /* add the overlap phase advance back in */
                                        tmp += (float)k*expct;


                                        /* accumulate delta phase to get bin phase */
                                        gSumPhase[k] += tmp;
                                        phase = gSumPhase[k];


                                        /* get real and imag part and re-interleave */
                                        gFFTworksp[2*k] = magn*cos(phase);
                                        gFFTworksp[2*k+1] = magn*sin(phase);
                              } 


                              /* zero negative frequencies */
                              for (k = fftFrameSize+2; k < 2*fftFrameSize; k++) gFFTworksp[k] = 0.;


                              /* do inverse transform */
                              smbFft(gFFTworksp, fftFrameSize, 1);


                              /* do windowing and add to output accumulator */ 
                              for(k=0; k < fftFrameSize; k++) {
                                        window = -.5*cos(2.*M_PI*(float)k/(float)fftFrameSize)+.5;
                                        gOutputAccum[k] += 2.*window*gFFTworksp[2*k]/(fftFrameSize2*osamp);
                              }
                              for (k = 0; k < stepSize; k++) gOutFIFO[k] = gOutputAccum[k];


                              /* shift accumulator */
                              //memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*sizeof(float));
  
                              memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*4);
  
                              /* move input FIFO */
                              for (k = 0; k < inFifoLatency; k++) gInFIFO[k] = gInFIFO[k+stepSize];
                    }
          }
}

Outcomes