WaveProcess.cpp
上传用户:huifengb
上传日期:2007-12-27
资源大小:334k
文件大小:11k
源码类别:

多媒体

开发平台:

Visual C++

  1. // WaveProcess.cpp: implementation of the CWaveProcess class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Audio.h"
  6. #include "WaveProcess.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. #include "DCT.h"
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CWaveProcess::CWaveProcess()
  17. {
  18. }
  19. CWaveProcess::~CWaveProcess()
  20. {
  21. }
  22. void CWaveProcess::WaveSignalFFT(float *pfSignal, DWORD dwLenSignal, int nPower, double *pdReFFT, double *pdImFFT)
  23. {
  24. int nSamples = 1<<nPower;
  25. CFFT *pFFT = new CFFT;
  26. double *pdReSignal, *pdImSignal;
  27. int nBlock = (dwLenSignal+nSamples-1)/nSamples;
  28. int i,j;
  29. pdReSignal = new double[nSamples];
  30. pdImSignal = new double[nSamples];
  31. for(i=0; i<nBlock; i++)
  32. {
  33. for(j=0; j<nSamples; j++)
  34. {
  35. if((DWORD)(i*nSamples+j)<dwLenSignal)
  36. {
  37. pdReSignal[j] = pfSignal[i*nSamples+j];
  38. }else
  39. {
  40. pdReSignal[j] = 0.0;
  41. }
  42. pdImSignal[j] = 0.0;
  43. }
  44. pFFT->FFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT+i*nSamples, pdImFFT+i*nSamples);
  45. }
  46. delete[] pdReSignal;
  47. delete[] pdImSignal;
  48. delete pFFT;
  49. }
  50. void CWaveProcess::WaveSignalFFT_I(float *pfSignal, DWORD dwLenSignal, int nPower, double *pdReFFT, double *pdImFFT)
  51. {
  52. int nSamples = 1<<nPower;
  53. CFFT *pFFT = new CFFT;
  54. double *pdReSignal, *pdImSignal;
  55. int nBlock = (dwLenSignal+nSamples-1)/nSamples;
  56. int i,j;
  57. pdReSignal = new double[nSamples];
  58. pdImSignal = new double[nSamples];
  59. for(i=0; i<nBlock; i++)
  60. {
  61. pFFT->IFFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT+i*nSamples, pdImFFT+i*nSamples);
  62. for(j=0; j<nSamples; j++)
  63. {
  64. if((DWORD)(i*nSamples+j)<dwLenSignal)
  65. {
  66. pfSignal[i*nSamples+j] = (float)pdReSignal[j];
  67. }
  68. }
  69. }
  70. delete[] pdReSignal;
  71. delete[] pdImSignal;
  72. delete pFFT;
  73. }
  74. void CWaveProcess::WaveSignalDCTIV(float *pfSignal, float *pfDCT, DWORD dwLenSignal, int nPower)
  75. {
  76. int nSamples = 1<<nPower;
  77. CDCT *pDCT = new CDCT;
  78. int nBlock = (dwLenSignal+nSamples-1)/nSamples;
  79. int i,j;
  80. for(i=0; i<nBlock; i++)
  81. {
  82. for(j=0; j<nSamples; j++)
  83. {
  84. if((DWORD)(i*nSamples+j)<dwLenSignal)
  85. {
  86. pfDCT[i*nSamples+j] = pfSignal[i*nSamples+j];
  87. }
  88. }
  89. pDCT->DCTIV(pfDCT+i*nSamples,nPower);
  90. }
  91. delete pDCT;
  92. }
  93. void CWaveProcess::WaveSignalIDCTIV(float *pfSignal, float *pfDCT, DWORD dwLenSignal, int nPower)
  94. {
  95. int nSamples = 1<<nPower;
  96. CDCT *pDCT = new CDCT;
  97. int nBlock = (dwLenSignal+nSamples-1)/nSamples;
  98. int i,j;
  99. for(i=0; i<nBlock; i++)
  100. {
  101. pDCT->IDCTIV(pfDCT+i*nSamples,nPower);
  102. for(j=0; j<nSamples; j++)
  103. {
  104. if((DWORD)(i*nSamples+j)<dwLenSignal)
  105. {
  106. pfSignal[i*nSamples+j] = pfDCT[i*nSamples+j];
  107. }
  108. }
  109. }
  110. delete pDCT;
  111. }
  112. void CWaveProcess::LowPass(float *pfSignal, DWORD dwLenSignal, int nStep)
  113. {
  114. float *pfData = new float[dwLenSignal];
  115. memcpy(pfData, pfSignal, dwLenSignal*sizeof(float));
  116. float fMaxS, fMaxD;
  117. fMaxS = MaxData(pfSignal, dwLenSignal);
  118. DWORD i;
  119. int k;
  120. float fQ = (float)nStep*2+1;
  121. float fSum;
  122. for(i=nStep; i<dwLenSignal-nStep; i++)
  123. {
  124. fSum = 0;
  125. for(k=-nStep; k<=nStep; k++)
  126. {
  127. fSum = fSum +pfData[i+k];
  128. }
  129. pfSignal[i] = fSum/fQ;
  130. }
  131. fMaxD = MaxData(pfSignal, dwLenSignal);
  132. for(i=0; i<(int)dwLenSignal; i++)
  133. {
  134. pfSignal[i] = pfSignal[i]*(fMaxS/fMaxD+0.25f);
  135. // pfSignal[i] = pfSignal[i]*2;
  136. }
  137. delete[] pfData;
  138. }
  139. void CWaveProcess::HighPass(float *pfSignal, DWORD dwLenSignal, int nStep, int nTh, float fRate)
  140. {
  141. float *pfData = new float[dwLenSignal];
  142. memcpy(pfData, pfSignal, dwLenSignal*sizeof(float));
  143. float fMaxS, fMaxD;
  144. fMaxS = MaxData(pfSignal, dwLenSignal);
  145. DWORD i;
  146. int k;
  147. float fSum;
  148. for(i=nStep; i<dwLenSignal-nStep; i++)
  149. {
  150. fSum = 0;
  151. for(k=-nStep; k<=nStep; k++)
  152. {
  153. fSum = fSum +(float)fabs(pfData[i+k]-pfData[i]);
  154. }
  155. if(fSum>nTh)
  156. {
  157. pfSignal[i] = pfSignal[i]*fRate;
  158. }
  159. }
  160. fMaxD = MaxData(pfSignal, dwLenSignal);
  161. // for(i=0; i<(int)dwLenSignal; i++)
  162. // {
  163. // pfSignal[i] = pfSignal[i]*(fMaxS/fMaxD);
  164. // }
  165. delete[] pfData;
  166. }
  167. void CWaveProcess::FFTLowFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
  168. {
  169. int nSamples = 1<<nPower;
  170. CFFT *pFFT = new CFFT;
  171. int nBlock = (dwLenSound+nSamples-1)/nSamples;
  172. int i,j;
  173. double *pdReSignal = new double[nSamples];
  174. double *pdImSignal = new double[nSamples];
  175. double *pdReFFT    = new double[nSamples];
  176. double *pdImFFT    = new double[nSamples];
  177. float fMaxS, fMaxD;
  178. fMaxS = MaxData(pfSoundData, dwLenSound);
  179. for(i=0; i<nBlock; i++)
  180. {
  181. for(j=0; j<nSamples; j++)
  182. {
  183. if((DWORD)(i*nSamples+j)<dwLenSound)
  184. {
  185. pdReSignal[j] = pfSoundData[i*nSamples+j];
  186. }else
  187. {
  188. pdReSignal[j] = 0.0;
  189. }
  190. pdImSignal[j] = 0.0;
  191. }
  192. pFFT->FFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
  193. for(j=0; j<nSamples-nShift; j++)
  194. {
  195. pdReFFT[j] = pdReFFT[j+nShift];
  196. pdImFFT[j] = pdImFFT[j+nShift];
  197. }
  198. for(j=nSamples-nShift; j<nSamples; j++)
  199. {
  200. pdReFFT[j] = 0.0;
  201. pdImFFT[j] = 0.0;
  202. }
  203. pFFT->IFFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
  204. for(j=0; j<nSamples; j++)
  205. {
  206. if((DWORD)(i*nSamples+j)<dwLenSound)
  207. {
  208. pfSoundData[i*nSamples+j] = (float)pdReSignal[j];
  209. }
  210. }
  211. }
  212. fMaxD = MaxData(pfSoundData, dwLenSound);
  213. for(i=0; i<(int)dwLenSound; i++)
  214. {
  215. pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
  216. }
  217. delete[] pdReSignal;
  218. delete[] pdImSignal;
  219. delete[] pdReFFT;
  220. delete[] pdImFFT;
  221. delete pFFT;
  222. }
  223. void CWaveProcess::FFTHighFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
  224. {
  225. int nSamples = 1<<nPower;
  226. CFFT *pFFT = new CFFT;
  227. int nBlock = (dwLenSound+nSamples-1)/nSamples;
  228. int i,j;
  229. double *pdReSignal = new double[nSamples];
  230. double *pdImSignal = new double[nSamples];
  231. double *pdReFFT    = new double[nSamples];
  232. double *pdImFFT    = new double[nSamples];
  233. float fMaxS, fMaxD;
  234. fMaxS = MaxData(pfSoundData, dwLenSound);
  235. for(i=0; i<nBlock; i++)
  236. {
  237. for(j=0; j<nSamples; j++)
  238. {
  239. if((DWORD)(i*nSamples+j)<dwLenSound)
  240. {
  241. pdReSignal[j] = pfSoundData[i*nSamples+j];
  242. }else
  243. {
  244. pdReSignal[j] = 0.0;
  245. }
  246. pdImSignal[j] = 0.0;
  247. }
  248. pFFT->FFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
  249. for(j=0; j<nSamples-nShift; j++)
  250. {
  251. pdReFFT[nSamples-1-j] = pdReFFT[nSamples-1-j-nShift];
  252. pdImFFT[nSamples-1-j] = pdImFFT[nSamples-1-j-nShift];
  253. }
  254. for(j=0; j<nShift; j++)
  255. {
  256. pdReFFT[j] = 0.0;
  257. pdImFFT[j] = 0.0;
  258. }
  259. pFFT->IFFTSignal(nPower, pdReSignal, pdImSignal, pdReFFT, pdImFFT);
  260. for(j=0; j<nSamples; j++)
  261. {
  262. if((DWORD)(i*nSamples+j)<dwLenSound)
  263. {
  264. pfSoundData[i*nSamples+j] = (float)pdReSignal[j];
  265. }
  266. }
  267. }
  268. fMaxD = MaxData(pfSoundData, dwLenSound);
  269. for(i=0; i<(int)dwLenSound; i++)
  270. {
  271. pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
  272. }
  273. delete[] pdReSignal;
  274. delete[] pdImSignal;
  275. delete[] pdReFFT;
  276. delete[] pdImFFT;
  277. delete pFFT;
  278. }
  279. float CWaveProcess::MaxData(float *pfData, DWORD dwLenData)
  280. {
  281. DWORD i;
  282. float fMax = (float)fabs(pfData[0]);
  283. for(i=0; i<dwLenData; i++)
  284. {
  285. if(fabs(pfData[i])>fMax)
  286. {
  287. fMax = (float)fabs(pfData[i]);
  288. }
  289. }
  290. return fMax;
  291. }
  292. void CWaveProcess::DCTLowFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
  293. {
  294. int nSamples = 1<<nPower;
  295. CDCT *pDCT = new CDCT;
  296. int nBlock = (dwLenSound+nSamples-1)/nSamples;
  297. int i,j;
  298. float *pfDCT = new float[nSamples];
  299. float fMaxS, fMaxD;
  300. fMaxS = MaxData(pfSoundData, dwLenSound);
  301. for(i=0; i<nBlock; i++)
  302. {
  303. for(j=0; j<nSamples; j++)
  304. {
  305. if((DWORD)(i*nSamples+j)<dwLenSound)
  306. {
  307. pfDCT[j] = pfSoundData[i*nSamples+j];
  308. }
  309. }
  310. pDCT->DCTIV(pfDCT, nPower);
  311. for(j=0; j<nSamples-nShift; j++)
  312. {
  313. pfDCT[j] = pfDCT[j+nShift];
  314. }
  315. for(j=nSamples-nShift; j<nSamples; j++)
  316. {
  317. pfDCT[j] = 0.0;
  318. }
  319. pDCT->IDCTIV(pfDCT, nPower);
  320. for(j=0; j<nSamples; j++)
  321. {
  322. if((DWORD)(i*nSamples+j)<dwLenSound)
  323. {
  324. pfSoundData[i*nSamples+j] = (float)pfDCT[j];
  325. }
  326. }
  327. }
  328. fMaxD = MaxData(pfSoundData, dwLenSound);
  329. for(i=0; i<(int)dwLenSound; i++)
  330. {
  331. pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
  332. }
  333. delete[] pfDCT;
  334. delete pDCT;
  335. }
  336. void CWaveProcess::DCTHighFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower)
  337. {
  338. int nSamples = 1<<nPower;
  339. CDCT *pDCT = new CDCT;
  340. int nBlock = (dwLenSound+nSamples-1)/nSamples;
  341. int i,j;
  342. float *pfDCT = new float[nSamples];
  343. float fMaxS, fMaxD;
  344. fMaxS = MaxData(pfSoundData, dwLenSound);
  345. for(i=0; i<nBlock; i++)
  346. {
  347. for(j=0; j<nSamples; j++)
  348. {
  349. if((DWORD)(i*nSamples+j)<dwLenSound)
  350. {
  351. pfDCT[j] = pfSoundData[i*nSamples+j];
  352. }
  353. }
  354. pDCT->DCTIV(pfDCT, nPower);
  355. for(j=0; j<nSamples-nShift; j++)
  356. {
  357. pfDCT[nSamples-1-j] = pfDCT[nSamples-1-j-nShift];
  358. }
  359. for(j=nSamples-nShift; j<nSamples; j++)
  360. {
  361. pfDCT[j] = 0.0;
  362. }
  363. pDCT->IDCTIV(pfDCT, nPower);
  364. for(j=0; j<nSamples; j++)
  365. {
  366. if((DWORD)(i*nSamples+j)<dwLenSound)
  367. {
  368. pfSoundData[i*nSamples+j] = (float)pfDCT[j];
  369. }
  370. }
  371. }
  372. fMaxD = MaxData(pfSoundData, dwLenSound);
  373. for(i=0; i<(int)dwLenSound; i++)
  374. {
  375. pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
  376. }
  377. delete[] pfDCT;
  378. delete pDCT;
  379. }
  380. void CWaveProcess::DCTMidFrequencyShift(float *pfSoundData, DWORD dwLenSound, int nShift, int nPower, float fRate)
  381. {
  382. int nSamples = 1<<nPower;
  383. CDCT *pDCT = new CDCT;
  384. int nBlock = (dwLenSound+nSamples-1)/nSamples;
  385. int i,j;
  386. int nStart = int(nSamples*fRate);
  387. float *pfDCT = new float[nSamples];
  388. float fMaxS, fMaxD;
  389. fMaxS = MaxData(pfSoundData, dwLenSound);
  390. for(i=0; i<nBlock; i++)
  391. {
  392. for(j=0; j<nSamples; j++)
  393. {
  394. if((DWORD)(i*nSamples+j)<dwLenSound)
  395. {
  396. pfDCT[j] = pfSoundData[i*nSamples+j];
  397. }
  398. }
  399. pDCT->DCTIV(pfDCT, nPower);
  400. for(j=0; j<nStart-nShift; j++)
  401. {
  402. pfDCT[nStart-j-1] = pfDCT[nStart-j-1-nShift];
  403. }
  404. for(j=0; j<nShift; j++)
  405. {
  406. pfDCT[j] = 0.0;
  407. }
  408. pDCT->IDCTIV(pfDCT, nPower);
  409. for(j=0; j<nSamples; j++)
  410. {
  411. if((DWORD)(i*nSamples+j)<dwLenSound)
  412. {
  413. pfSoundData[i*nSamples+j] = (float)pfDCT[j];
  414. }
  415. }
  416. }
  417. fMaxD = MaxData(pfSoundData, dwLenSound);
  418. for(i=0; i<(int)dwLenSound; i++)
  419. {
  420. pfSoundData[i] = pfSoundData[i]*fMaxS/fMaxD;
  421. }
  422. delete[] pfDCT;
  423. delete pDCT;
  424. }
  425. void CWaveProcess::Contraction(float *pfSoundData, DWORD dwLenSound, float fRate)
  426. {
  427. float *pfData = new float[dwLenSound];
  428. memcpy(pfData, pfSoundData, dwLenSound*sizeof(float));
  429. int nNum = int(1/fRate);
  430. int nSum = int(dwLenSound*fRate);
  431. float fSum;
  432. int i,k;
  433. memset(pfSoundData, 0, dwLenSound*sizeof(float));
  434. for(i=0; i<nSum; i++)
  435. {
  436. fSum = 0.0;
  437. for(k=0; k<nNum; k++)
  438. {
  439. fSum = fSum+pfData[i*nNum+k];
  440. }
  441. pfSoundData[i] = fSum/(float)nNum;
  442. }
  443. }