WaveProcess.cpp
资源名称:AudioWave.rar [点击查看]
上传用户:huifengb
上传日期:2007-12-27
资源大小:334k
文件大小:11k
源码类别:
多媒体
开发平台:
Visual C++
- // WaveProcess.cpp: implementation of the CWaveProcess class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Audio.h"
- #include "WaveProcess.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- #include "DCT.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CWaveProcess::CWaveProcess()
- {
- }
- CWaveProcess::~CWaveProcess()
- {
- }
- void CWaveProcess::WaveSignalFFT(float *pfSignal, DWORD dwLenSignal, int nPower, double *pdReFFT, double *pdImFFT)
- {
- int nSamples = 1<<nPower;
- CFFT *pFFT = new CFFT;
- double *pdReSignal, *pdImSignal;
- int nBlock = (dwLenSignal+nSamples-1)/nSamples;
- int i,j;
- pdReSignal = new double[nSamples];
- pdImSignal = new double[nSamples];
- for(i=0; i<nBlock; i++)
- {
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSignal)
- {
- pdReSignal[j] = pfSignal[i*nSamples+j];
- }else
- {
- pdReSignal[j] = 0.0;
- }
- pdImSignal[j] = 0.0;
- }
- pFFT->FFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT+i*nSamples, pdImFFT+i*nSamples);
- }
- delete[] pdReSignal;
- delete[] pdImSignal;
- delete pFFT;
- }
- void CWaveProcess::WaveSignalFFT_I(float *pfSignal, DWORD dwLenSignal, int nPower, double *pdReFFT, double *pdImFFT)
- {
- int nSamples = 1<<nPower;
- CFFT *pFFT = new CFFT;
- double *pdReSignal, *pdImSignal;
- int nBlock = (dwLenSignal+nSamples-1)/nSamples;
- int i,j;
- pdReSignal = new double[nSamples];
- pdImSignal = new double[nSamples];
- for(i=0; i<nBlock; i++)
- {
- pFFT->IFFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT+i*nSamples, pdImFFT+i*nSamples);
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSignal)
- {
- pfSignal[i*nSamples+j] = (float)pdReSignal[j];
- }
- }
- }
- delete[] pdReSignal;
- delete[] pdImSignal;
- delete pFFT;
- }
- void CWaveProcess::WaveSignalDCTIV(float *pfSignal, float *pfDCT, DWORD dwLenSignal, int nPower)
- {
- int nSamples = 1<<nPower;
- CDCT *pDCT = new CDCT;
- int nBlock = (dwLenSignal+nSamples-1)/nSamples;
- int i,j;
- for(i=0; i<nBlock; i++)
- {
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSignal)
- {
- pfDCT[i*nSamples+j] = pfSignal[i*nSamples+j];
- }
- }
- pDCT->DCTIV(pfDCT+i*nSamples,nPower);
- }
- delete pDCT;
- }
- void CWaveProcess::WaveSignalIDCTIV(float *pfSignal, float *pfDCT, DWORD dwLenSignal, int nPower)
- {
- int nSamples = 1<<nPower;
- CDCT *pDCT = new CDCT;
- int nBlock = (dwLenSignal+nSamples-1)/nSamples;
- int i,j;
- for(i=0; i<nBlock; i++)
- {
- pDCT->IDCTIV(pfDCT+i*nSamples,nPower);
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSignal)
- {
- pfSignal[i*nSamples+j] = pfDCT[i*nSamples+j];
- }
- }
- }
- delete pDCT;
- }
- void CWaveProcess::LowPass(float *pfSignal, DWORD dwLenSignal, int nStep)
- {
- float *pfData = new float[dwLenSignal];
- memcpy(pfData, pfSignal, dwLenSignal*sizeof(float));
- float fMaxS, fMaxD;
- fMaxS = MaxData(pfSignal, dwLenSignal);
- DWORD i;
- int k;
- float fQ = (float)nStep*2+1;
- float fSum;
- for(i=nStep; i<dwLenSignal-nStep; i++)
- {
- fSum = 0;
- for(k=-nStep; k<=nStep; k++)
- {
- fSum = fSum +pfData[i+k];
- }
- pfSignal[i] = fSum/fQ;
- }
- fMaxD = MaxData(pfSignal, dwLenSignal);
- for(i=0; i<(int)dwLenSignal; i++)
- {
- pfSignal[i] = pfSignal[i]*(fMaxS/fMaxD+0.25f);
- // pfSignal[i] = pfSignal[i]*2;
- }
- delete[] pfData;
- }
- void CWaveProcess::HighPass(float *pfSignal, DWORD dwLenSignal, int nStep, int nTh, float fRate)
- {
- float *pfData = new float[dwLenSignal];
- memcpy(pfData, pfSignal, dwLenSignal*sizeof(float));
- float fMaxS, fMaxD;
- fMaxS = MaxData(pfSignal, dwLenSignal);
- DWORD i;
- int k;
- float fSum;
- for(i=nStep; i<dwLenSignal-nStep; i++)
- {
- fSum = 0;
- for(k=-nStep; k<=nStep; k++)
- {
- fSum = fSum +(float)fabs(pfData[i+k]-pfData[i]);
- }
- if(fSum>nTh)
- {
- pfSignal[i] = pfSignal[i]*fRate;
- }
- }
- fMaxD = MaxData(pfSignal, dwLenSignal);
- // for(i=0; i<(int)dwLenSignal; i++)
- // {
- // pfSignal[i] = pfSignal[i]*(fMaxS/fMaxD);
- // }
- delete[] pfData;
- }
- void CWaveProcess::FFTLowFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
- {
- int nSamples = 1<<nPower;
- CFFT *pFFT = new CFFT;
- int nBlock = (dwLenSound+nSamples-1)/nSamples;
- int i,j;
- double *pdReSignal = new double[nSamples];
- double *pdImSignal = new double[nSamples];
- double *pdReFFT = new double[nSamples];
- double *pdImFFT = new double[nSamples];
- float fMaxS, fMaxD;
- fMaxS = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<nBlock; i++)
- {
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pdReSignal[j] = pfSoundData[i*nSamples+j];
- }else
- {
- pdReSignal[j] = 0.0;
- }
- pdImSignal[j] = 0.0;
- }
- pFFT->FFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
- for(j=0; j<nSamples-nShift; j++)
- {
- pdReFFT[j] = pdReFFT[j+nShift];
- pdImFFT[j] = pdImFFT[j+nShift];
- }
- for(j=nSamples-nShift; j<nSamples; j++)
- {
- pdReFFT[j] = 0.0;
- pdImFFT[j] = 0.0;
- }
- pFFT->IFFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfSoundData[i*nSamples+j] = (float)pdReSignal[j];
- }
- }
- }
- fMaxD = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<(int)dwLenSound; i++)
- {
- pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
- }
- delete[] pdReSignal;
- delete[] pdImSignal;
- delete[] pdReFFT;
- delete[] pdImFFT;
- delete pFFT;
- }
- void CWaveProcess::FFTHighFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
- {
- int nSamples = 1<<nPower;
- CFFT *pFFT = new CFFT;
- int nBlock = (dwLenSound+nSamples-1)/nSamples;
- int i,j;
- double *pdReSignal = new double[nSamples];
- double *pdImSignal = new double[nSamples];
- double *pdReFFT = new double[nSamples];
- double *pdImFFT = new double[nSamples];
- float fMaxS, fMaxD;
- fMaxS = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<nBlock; i++)
- {
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pdReSignal[j] = pfSoundData[i*nSamples+j];
- }else
- {
- pdReSignal[j] = 0.0;
- }
- pdImSignal[j] = 0.0;
- }
- pFFT->FFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
- for(j=0; j<nSamples-nShift; j++)
- {
- pdReFFT[nSamples-1-j] = pdReFFT[nSamples-1-j-nShift];
- pdImFFT[nSamples-1-j] = pdImFFT[nSamples-1-j-nShift];
- }
- for(j=0; j<nShift; j++)
- {
- pdReFFT[j] = 0.0;
- pdImFFT[j] = 0.0;
- }
- pFFT->IFFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfSoundData[i*nSamples+j] = (float)pdReSignal[j];
- }
- }
- }
- fMaxD = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<(int)dwLenSound; i++)
- {
- pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
- }
- delete[] pdReSignal;
- delete[] pdImSignal;
- delete[] pdReFFT;
- delete[] pdImFFT;
- delete pFFT;
- }
- float CWaveProcess::MaxData(float *pfData, DWORD dwLenData)
- {
- DWORD i;
- float fMax = (float)fabs(pfData[0]);
- for(i=0; i<dwLenData; i++)
- {
- if(fabs(pfData[i])>fMax)
- {
- fMax = (float)fabs(pfData[i]);
- }
- }
- return fMax;
- }
- void CWaveProcess::DCTLowFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
- {
- int nSamples = 1<<nPower;
- CDCT *pDCT = new CDCT;
- int nBlock = (dwLenSound+nSamples-1)/nSamples;
- int i,j;
- float *pfDCT = new float[nSamples];
- float fMaxS, fMaxD;
- fMaxS = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<nBlock; i++)
- {
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfDCT[j] = pfSoundData[i*nSamples+j];
- }
- }
- pDCT->DCTIV(pfDCT, nPower);
- for(j=0; j<nSamples-nShift; j++)
- {
- pfDCT[j] = pfDCT[j+nShift];
- }
- for(j=nSamples-nShift; j<nSamples; j++)
- {
- pfDCT[j] = 0.0;
- }
- pDCT->IDCTIV(pfDCT, nPower);
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfSoundData[i*nSamples+j] = (float)pfDCT[j];
- }
- }
- }
- fMaxD = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<(int)dwLenSound; i++)
- {
- pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
- }
- delete[] pfDCT;
- delete pDCT;
- }
- void CWaveProcess::DCTHighFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
- {
- int nSamples = 1<<nPower;
- CDCT *pDCT = new CDCT;
- int nBlock = (dwLenSound+nSamples-1)/nSamples;
- int i,j;
- float *pfDCT = new float[nSamples];
- float fMaxS, fMaxD;
- fMaxS = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<nBlock; i++)
- {
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfDCT[j] = pfSoundData[i*nSamples+j];
- }
- }
- pDCT->DCTIV(pfDCT, nPower);
- for(j=0; j<nSamples-nShift; j++)
- {
- pfDCT[nSamples-1-j] = pfDCT[nSamples-1-j-nShift];
- }
- for(j=nSamples-nShift; j<nSamples; j++)
- {
- pfDCT[j] = 0.0;
- }
- pDCT->IDCTIV(pfDCT, nPower);
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfSoundData[i*nSamples+j] = (float)pfDCT[j];
- }
- }
- }
- fMaxD = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<(int)dwLenSound; i++)
- {
- pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
- }
- delete[] pfDCT;
- delete pDCT;
- }
- void CWaveProcess::DCTMidFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower, float fRate)
- {
- int nSamples = 1<<nPower;
- CDCT *pDCT = new CDCT;
- int nBlock = (dwLenSound+nSamples-1)/nSamples;
- int i,j;
- int nStart = int(nSamples*fRate);
- float *pfDCT = new float[nSamples];
- float fMaxS, fMaxD;
- fMaxS = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<nBlock; i++)
- {
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfDCT[j] = pfSoundData[i*nSamples+j];
- }
- }
- pDCT->DCTIV(pfDCT, nPower);
- for(j=0; j<nStart-nShift; j++)
- {
- pfDCT[nStart-j-1] = pfDCT[nStart-j-1-nShift];
- }
- for(j=0; j<nShift; j++)
- {
- pfDCT[j] = 0.0;
- }
- pDCT->IDCTIV(pfDCT, nPower);
- for(j=0; j<nSamples; j++)
- {
- if((DWORD)(i*nSamples+j)<dwLenSound)
- {
- pfSoundData[i*nSamples+j] = (float)pfDCT[j];
- }
- }
- }
- fMaxD = MaxData(pfSoundData, dwLenSound);
- for(i=0; i<(int)dwLenSound; i++)
- {
- pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
- }
- delete[] pfDCT;
- delete pDCT;
- }
- void CWaveProcess::Contraction(float *pfSoundData, DWORD dwLenSound, float fRate)
- {
- float *pfData = new float[dwLenSound];
- memcpy(pfData, pfSoundData, dwLenSound*sizeof(float));
- int nNum = int(1/fRate);
- int nSum = int(dwLenSound*fRate);
- float fSum;
- int i,k;
- memset(pfSoundData, 0, dwLenSound*sizeof(float));
- for(i=0; i<nSum; i++)
- {
- fSum = 0.0;
- for(k=0; k<nNum; k++)
- {
- fSum = fSum+pfData[i*nNum+k];
- }
- pfSoundData[i] = fSum/(float)nNum;
- }
- }