Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2195 lines
48 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // pcm.c - pcm functions
  24. ////
  25. #include "winlocal.h"
  26. #include "pcm.h"
  27. #include "calc.h"
  28. #include "mem.h"
  29. #include "trace.h"
  30. ////
  31. // private definitions
  32. ////
  33. // macros to convert PCM samples to/from other sizes
  34. //
  35. #define _Pcm8To16(pcm8) (((PCM16) (pcm8) - 128) << 8)
  36. #define _Pcm16To8(pcm16) ((PCM8) (((PCM16) (pcm16) >> 8) + 128))
  37. #define BYTESPERSAMPLE(nBitsPerSample) (nBitsPerSample > 8 ? 2 : 1)
  38. // pcm control struct
  39. //
  40. typedef struct PCM
  41. {
  42. DWORD dwVersion;
  43. HINSTANCE hInst;
  44. HTASK hTask;
  45. DWORD dwFlags;
  46. short nCounter;
  47. PCM16 pcm16Prev;
  48. PCM16 pcm16Prev0F;
  49. PCM16 pcm16Prev1F;
  50. PCM16 pcm16Prev2F;
  51. PCM16 pcm16Prev0;
  52. PCM16 pcm16Prev1;
  53. PCM16 pcm16Prev2;
  54. } PCM, FAR *LPPCM;
  55. // helper functions
  56. //
  57. static UINT PcmResampleCalcDstMax(HPCM hPcm,
  58. long nSamplesPerSecSrc, long nSamplesPerSecDst, UINT uSamples);
  59. static UINT PcmResample6Kto8K(LPPCM lpPcm,
  60. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  61. static UINT PcmResample6Kto11K(LPPCM lpPcm,
  62. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  63. static UINT PcmResample6Kto22K(LPPCM lpPcm,
  64. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  65. static UINT PcmResample6Kto44K(LPPCM lpPcm,
  66. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  67. static UINT PcmResample8Kto6K(LPPCM lpPcm,
  68. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  69. static UINT PcmResample8Kto11K(LPPCM lpPcm,
  70. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  71. static UINT PcmResample8Kto22K(LPPCM lpPcm,
  72. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  73. static UINT PcmResample8Kto44K(LPPCM lpPcm,
  74. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  75. static UINT PcmResample11Kto6K(LPPCM lpPcm,
  76. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  77. static UINT PcmResample11Kto8K(LPPCM lpPcm,
  78. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  79. static UINT PcmResample11Kto22K(LPPCM lpPcm,
  80. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  81. static UINT PcmResample11Kto44K(LPPCM lpPcm,
  82. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  83. static UINT PcmResample22Kto6K(LPPCM lpPcm,
  84. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  85. static UINT PcmResample22Kto8K(LPPCM lpPcm,
  86. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  87. static UINT PcmResample22Kto11K(LPPCM lpPcm,
  88. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  89. static UINT PcmResample22Kto44K(LPPCM lpPcm,
  90. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  91. static UINT PcmResample44Kto6K(LPPCM lpPcm,
  92. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  93. static UINT PcmResample44Kto8K(LPPCM lpPcm,
  94. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  95. static UINT PcmResample44Kto11K(LPPCM lpPcm,
  96. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  97. static UINT PcmResample44Kto22K(LPPCM lpPcm,
  98. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples);
  99. static LPPCM PcmGetPtr(HPCM hPcm);
  100. static HPCM PcmGetHandle(LPPCM lpPcm);
  101. ////
  102. // public functions
  103. ////
  104. // PcmInit - initialize pcm engine
  105. // <dwVersion> (i) must be PCM_VERSION
  106. // <hInst> (i) instance handle of calling module
  107. // <dwFlags> (i) reserved; must be 0
  108. // return handle (NULL if error)
  109. //
  110. HPCM DLLEXPORT WINAPI PcmInit(DWORD dwVersion, HINSTANCE hInst, DWORD dwFlags)
  111. {
  112. BOOL fSuccess = TRUE;
  113. LPPCM lpPcm = NULL;
  114. if (dwVersion != PCM_VERSION)
  115. fSuccess = TraceFALSE(NULL);
  116. else if (hInst == NULL)
  117. fSuccess = TraceFALSE(NULL);
  118. else if ((lpPcm = (LPPCM) MemAlloc(NULL, sizeof(PCM), 0)) == NULL)
  119. fSuccess = TraceFALSE(NULL);
  120. else
  121. {
  122. lpPcm->dwVersion = dwVersion;
  123. lpPcm->hInst = hInst;
  124. lpPcm->hTask = GetCurrentTask();
  125. lpPcm->dwFlags = dwFlags;
  126. if (PcmReset(PcmGetHandle(lpPcm)) != 0)
  127. fSuccess = TraceFALSE(NULL);
  128. }
  129. if (!fSuccess)
  130. {
  131. PcmTerm(PcmGetHandle(lpPcm));
  132. lpPcm = NULL;
  133. }
  134. return fSuccess ? PcmGetHandle(lpPcm) : NULL;
  135. }
  136. // PcmTerm - shut down pcm engine
  137. // <hPcm> (i) handle returned from PcmInit
  138. // return 0 if success
  139. //
  140. int DLLEXPORT WINAPI PcmTerm(HPCM hPcm)
  141. {
  142. BOOL fSuccess = TRUE;
  143. LPPCM lpPcm;
  144. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  145. fSuccess = TraceFALSE(NULL);
  146. else if ((lpPcm = MemFree(NULL, lpPcm)) != NULL)
  147. fSuccess = TraceFALSE(NULL);
  148. return fSuccess ? 0 : -1;
  149. }
  150. // PcmReset - reset pcm engine
  151. // <hPcm> (i) handle returned from PcmInit
  152. // return 0 if success
  153. //
  154. int DLLEXPORT WINAPI PcmReset(HPCM hPcm)
  155. {
  156. BOOL fSuccess = TRUE;
  157. LPPCM lpPcm;
  158. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  159. fSuccess = TraceFALSE(NULL);
  160. else
  161. {
  162. lpPcm->nCounter = -1;
  163. lpPcm->pcm16Prev = 0;
  164. lpPcm->pcm16Prev0F = 0;
  165. lpPcm->pcm16Prev1F = 0;
  166. lpPcm->pcm16Prev2F = 0;
  167. lpPcm->pcm16Prev0 = 0;
  168. lpPcm->pcm16Prev1 = 0;
  169. lpPcm->pcm16Prev2 = 0;
  170. }
  171. return fSuccess ? 0 : -1;
  172. }
  173. // PcmCalcSizBufSrc - calculate source buffer size
  174. // <hPcm> (i) handle returned from PcmInit
  175. // <sizBufDst> (i) size of destination buffer in bytes
  176. // <lpwfxSrc> (i) source wav format
  177. // <lpwfxDst> (i) destination wav format
  178. // return source buffer size, -1 if error
  179. //
  180. long DLLEXPORT WINAPI PcmCalcSizBufSrc(HPCM hPcm, long sizBufDst,
  181. LPWAVEFORMATEX lpwfxSrc, LPWAVEFORMATEX lpwfxDst)
  182. {
  183. BOOL fSuccess = TRUE;
  184. LPPCM lpPcm;
  185. long sizBufSrc;
  186. UINT uSamplesDst;
  187. UINT uSamplesSrc;
  188. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  189. fSuccess = TraceFALSE(NULL);
  190. // calc how many samples can fit in destination buffer
  191. //
  192. else if ((uSamplesDst = (UINT) (sizBufDst /
  193. BYTESPERSAMPLE(lpwfxDst->wBitsPerSample))) <= 0)
  194. {
  195. fSuccess = TraceFALSE(NULL);
  196. }
  197. // calc how many samples can fit in source buffer
  198. //
  199. else if ((uSamplesSrc = PcmResampleCalcDstMax(hPcm,
  200. lpwfxDst->nSamplesPerSec,
  201. lpwfxSrc->nSamplesPerSec, uSamplesDst)) <= 0)
  202. {
  203. fSuccess = TraceFALSE(NULL);
  204. }
  205. // calc size of source buffer
  206. //
  207. else if ((sizBufSrc = (long) (uSamplesSrc *
  208. BYTESPERSAMPLE(lpwfxSrc->wBitsPerSample))) <= 0)
  209. {
  210. fSuccess = TraceFALSE(NULL);
  211. }
  212. return fSuccess ? sizBufSrc : -1;
  213. }
  214. // PcmCalcSizBufDst - calculate destination buffer size
  215. // <hPcm> (i) handle returned from PcmInit
  216. // <sizBufSrc> (i) size of source buffer in bytes
  217. // <lpwfxSrc> (i) source wav format
  218. // <lpwfxDst> (i) destination wav format
  219. // return destination buffer size, -1 if error
  220. //
  221. long DLLEXPORT WINAPI PcmCalcSizBufDst(HPCM hPcm, long sizBufSrc,
  222. LPWAVEFORMATEX lpwfxSrc, LPWAVEFORMATEX lpwfxDst)
  223. {
  224. BOOL fSuccess = TRUE;
  225. LPPCM lpPcm;
  226. long sizBufDst;
  227. UINT uSamplesSrc;
  228. UINT uSamplesDst;
  229. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  230. fSuccess = TraceFALSE(NULL);
  231. // calc how many samples can fit in source buffer
  232. //
  233. else if ((uSamplesSrc = (UINT) (sizBufSrc /
  234. BYTESPERSAMPLE(lpwfxSrc->wBitsPerSample))) <= 0)
  235. {
  236. fSuccess = TraceFALSE(NULL);
  237. }
  238. // calc how many samples can fit in destination buffer
  239. //
  240. else if ((uSamplesDst = PcmResampleCalcDstMax(hPcm,
  241. lpwfxSrc->nSamplesPerSec,
  242. lpwfxDst->nSamplesPerSec, uSamplesSrc)) <= 0)
  243. {
  244. fSuccess = TraceFALSE(NULL);
  245. }
  246. // calc size of destination buffer
  247. //
  248. else if ((sizBufDst = (long) (uSamplesDst *
  249. BYTESPERSAMPLE(lpwfxDst->wBitsPerSample))) <= 0)
  250. {
  251. fSuccess = TraceFALSE(NULL);
  252. }
  253. return fSuccess ? sizBufDst : -1;
  254. }
  255. // PcmConvert - convert pcm data from one format to another
  256. // <hPcm> (i) handle returned from PcmInit
  257. // <hpBufSrc> (i) buffer containing bytes to reformat
  258. // <sizBufSrc> (i) size of buffer in bytes
  259. // <lpwfxSrc> (i) source wav format
  260. // <hpBufDst> (o) buffer to contain new format
  261. // <sizBufDst> (i) size of buffer in bytes
  262. // <lpwfxDst> (i) destination wav format
  263. // <dwFlags> (i) control flags
  264. // PCMFILTER_LOWPASS perform lowpass filter
  265. // return count of bytes in destination buffer (-1 if error)
  266. //
  267. // NOTE: the destination buffer must be large enough to hold the result
  268. //
  269. long DLLEXPORT WINAPI PcmConvert(HPCM hPcm,
  270. void _huge *hpBufSrc, long sizBufSrc, LPWAVEFORMATEX lpwfxSrc,
  271. void _huge *hpBufDst, long sizBufDst, LPWAVEFORMATEX lpwfxDst,
  272. DWORD dwFlags)
  273. {
  274. BOOL fSuccess = TRUE;
  275. LPPCM lpPcm;
  276. BOOL fSampleRateChange = (BOOL)
  277. (lpwfxSrc->nSamplesPerSec != lpwfxDst->nSamplesPerSec);
  278. BOOL fSampleSizeChange = (BOOL)
  279. (lpwfxSrc->wBitsPerSample != lpwfxDst->wBitsPerSample);
  280. BOOL fChannelsChange = (BOOL)
  281. (lpwfxSrc->nChannels != lpwfxDst->nChannels);
  282. BOOL fLowPassFilter = (BOOL) (dwFlags & PCMFILTER_LOWPASS);
  283. BOOL fFormatChange = (BOOL) (fSampleRateChange ||
  284. fSampleSizeChange || fChannelsChange || fLowPassFilter);
  285. UINT uSamples = (UINT) (sizBufSrc / BYTESPERSAMPLE(lpwfxSrc->wBitsPerSample));
  286. UINT uSamplesDst = uSamples;
  287. BOOL f8To16Bits = (BOOL) (lpwfxSrc->wBitsPerSample <= 8 &&
  288. (lpwfxDst->wBitsPerSample > 8 || fSampleRateChange || fLowPassFilter));
  289. BOOL f16To8Bits = (BOOL) (lpwfxDst->wBitsPerSample <= 8 &&
  290. (lpwfxSrc->wBitsPerSample > 8 || f8To16Bits));
  291. void _huge *hpBufSrcTmp = hpBufSrc;
  292. void _huge *hpBufDstTmp1 = NULL;
  293. void _huge *hpBufDstTmp2 = NULL;
  294. void _huge *hpBufDstTmp3 = NULL;
  295. if (!fFormatChange)
  296. {
  297. // nothing to do but copy
  298. //
  299. MemCpy(hpBufDst, hpBufSrc, min(sizBufDst, sizBufSrc));
  300. }
  301. else if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  302. fSuccess = TraceFALSE(NULL);
  303. else if (hpBufSrc == NULL)
  304. fSuccess = TraceFALSE(NULL);
  305. else if (!WavFormatIsValid(lpwfxSrc))
  306. fSuccess = TraceFALSE(NULL);
  307. else if (hpBufDst == NULL)
  308. fSuccess = TraceFALSE(NULL);
  309. else if (!WavFormatIsValid(lpwfxDst))
  310. fSuccess = TraceFALSE(NULL);
  311. // $FIXUP - we cannot handle reformatting of stereo
  312. //
  313. else if (lpwfxSrc->nChannels != 1)
  314. fSuccess = TraceFALSE(NULL);
  315. else if (lpwfxDst->nChannels != 1)
  316. fSuccess = TraceFALSE(NULL);
  317. // $FIXUP - we cannot handle reformatting of non-PCM data
  318. //
  319. else if (lpwfxSrc->wFormatTag != WAVE_FORMAT_PCM)
  320. fSuccess = TraceFALSE(NULL);
  321. else if (lpwfxDst->wFormatTag != WAVE_FORMAT_PCM)
  322. fSuccess = TraceFALSE(NULL);
  323. else
  324. {
  325. // convert to 16 bit samples if necessary
  326. //
  327. if (f8To16Bits)
  328. {
  329. long sizBufTmp = uSamples * 2;
  330. // assume this is the last stage of the format
  331. //
  332. hpBufDstTmp1 = hpBufDst;
  333. // allocate temporary buffer if this is not the last stage
  334. //
  335. if ((fSampleRateChange || fLowPassFilter || f16To8Bits) &&
  336. (hpBufDstTmp1 = (void _huge *) MemAlloc(NULL,
  337. sizBufTmp, 0)) == NULL)
  338. {
  339. fSuccess = TraceFALSE(NULL);
  340. }
  341. else if (Pcm8To16(hPcm,
  342. (LPPCM8) hpBufSrcTmp, (LPPCM16) hpBufDstTmp1, uSamples) != 0)
  343. {
  344. fSuccess = TraceFALSE(NULL);
  345. }
  346. else
  347. {
  348. // the source of the next stage will be this destination
  349. //
  350. hpBufSrcTmp = hpBufDstTmp1;
  351. }
  352. }
  353. // convert to new sample rate if necessary
  354. //
  355. if (fSuccess && fSampleRateChange)
  356. {
  357. long sizBufTmp;
  358. // assume this is the last stage of the format
  359. //
  360. hpBufDstTmp2 = hpBufDst;
  361. // calc size of temp buffer if this is not the last stage
  362. //
  363. if ((fLowPassFilter || f16To8Bits) &&
  364. (sizBufTmp = 2 * PcmResampleCalcDstMax(hPcm,
  365. lpwfxSrc->nSamplesPerSec, lpwfxDst->nSamplesPerSec, uSamples)) <= 0)
  366. {
  367. fSuccess = TraceFALSE(NULL);
  368. }
  369. // allocate temporary buffer if this is not the last stage
  370. //
  371. else if ((fLowPassFilter || f16To8Bits) &&
  372. (hpBufDstTmp2 = (void _huge *) MemAlloc(NULL,
  373. sizBufTmp, 0)) == NULL)
  374. {
  375. fSuccess = TraceFALSE(NULL);
  376. }
  377. // do the sample rate change
  378. //
  379. else if ((uSamplesDst = PcmResample(hPcm,
  380. (LPPCM16) hpBufSrcTmp, lpwfxSrc->nSamplesPerSec,
  381. (LPPCM16) hpBufDstTmp2, lpwfxDst->nSamplesPerSec, uSamples, 0)) <= 0)
  382. {
  383. fSuccess = TraceFALSE(NULL);
  384. }
  385. else
  386. {
  387. // the source of the next stage will be this destination
  388. //
  389. hpBufSrcTmp = hpBufDstTmp2;
  390. }
  391. }
  392. // perform lowpass filter if necessary
  393. //
  394. if (fSuccess && fLowPassFilter)
  395. {
  396. long sizBufTmp = uSamplesDst * 2;
  397. // assume this is the last stage of the format
  398. //
  399. hpBufDstTmp3 = hpBufDst;
  400. // allocate temporary buffer if this is not the last stage
  401. //
  402. if (f16To8Bits &&
  403. (hpBufDstTmp3 = (void _huge *) MemAlloc(NULL,
  404. sizBufTmp, 0)) == NULL)
  405. {
  406. fSuccess = TraceFALSE(NULL);
  407. }
  408. else if (PcmFilter(hPcm, (LPPCM16) hpBufSrcTmp,
  409. (LPPCM16) hpBufDstTmp3, uSamples, PCMFILTER_LOWPASS) != 0)
  410. {
  411. fSuccess = TraceFALSE(NULL);
  412. }
  413. else
  414. {
  415. // the source of the next stage will be this destination
  416. //
  417. hpBufSrcTmp = hpBufDstTmp3;
  418. }
  419. }
  420. // convert to 8 bit samples if necessary
  421. //
  422. if (fSuccess && f16To8Bits)
  423. {
  424. long sizBufTmp = uSamples;
  425. // this is the last stage of the format
  426. //
  427. if (Pcm16To8(hPcm,
  428. (LPPCM16) hpBufSrcTmp, (LPPCM8) hpBufDst, uSamples) != 0)
  429. {
  430. fSuccess = TraceFALSE(NULL);
  431. }
  432. }
  433. // clean up
  434. //
  435. if (hpBufDstTmp1 != NULL && hpBufDstTmp1 != hpBufDst &&
  436. (hpBufDstTmp1 = MemFree(NULL, hpBufDstTmp1)) != NULL)
  437. {
  438. fSuccess = TraceFALSE(NULL);
  439. }
  440. if (hpBufDstTmp2 != NULL && hpBufDstTmp2 != hpBufDst &&
  441. (hpBufDstTmp2 = MemFree(NULL, hpBufDstTmp2)) != NULL)
  442. {
  443. fSuccess = TraceFALSE(NULL);
  444. }
  445. if (hpBufDstTmp3 != NULL && hpBufDstTmp3 != hpBufDst &&
  446. (hpBufDstTmp3 = MemFree(NULL, hpBufDstTmp3)) != NULL)
  447. {
  448. fSuccess = TraceFALSE(NULL);
  449. }
  450. }
  451. return fSuccess ? (long) uSamplesDst *
  452. BYTESPERSAMPLE(lpwfxDst->wBitsPerSample) : -1;
  453. }
  454. // Pcm16To8 - convert 16-bit samples to 8-bit samples
  455. // <hPcm> (i) handle returned from PcmInit
  456. // <lppcm16Src> (i) buffer of source samples
  457. // <lppcm8Dst> (o) buffer to hold destination samples
  458. // <uSamples> (i) count of source samples to convert
  459. // return 0 if success
  460. //
  461. int DLLEXPORT WINAPI Pcm16To8(HPCM hPcm,
  462. LPPCM16 lppcm16Src, LPPCM8 lppcm8Dst, UINT uSamples)
  463. {
  464. BOOL fSuccess = TRUE;
  465. LPPCM lpPcm;
  466. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  467. fSuccess = TraceFALSE(NULL);
  468. else if (lppcm16Src == NULL || lppcm8Dst == NULL)
  469. fSuccess = TraceFALSE(NULL);
  470. else while (uSamples-- > 0)
  471. *lppcm8Dst++ = _Pcm16To8(*lppcm16Src++);
  472. return fSuccess ? 0 : -1;
  473. }
  474. // Pcm8To16 - convert 8-bit samples to 16-bit samples
  475. // <hPcm> (i) handle returned from PcmInit
  476. // <lppcm8Src> (i) buffer of source samples
  477. // <lppcm16Dst> (o) buffer to hold destination samples
  478. // <uSamples> (i) count of source samples to convert
  479. // return 0 if success
  480. //
  481. int DLLEXPORT WINAPI Pcm8To16(HPCM hPcm,
  482. LPPCM8 lppcm8Src, LPPCM16 lppcm16Dst, UINT uSamples)
  483. {
  484. BOOL fSuccess = TRUE;
  485. LPPCM lpPcm;
  486. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  487. fSuccess = TraceFALSE(NULL);
  488. else if (lppcm8Src == NULL || lppcm16Dst == NULL)
  489. fSuccess = TraceFALSE(NULL);
  490. else while (uSamples-- > 0)
  491. *lppcm16Dst++ = _Pcm8To16(*lppcm8Src++);
  492. return fSuccess ? 0 : -1;
  493. }
  494. // PcmFilter - filter pcm samples
  495. // <hPcm> (i) handle returned from PcmInit
  496. // <lppcm16Src> (i) buffer of source samples
  497. // <lppcm16Dst> (o) buffer to hold destination samples
  498. // <uSamples> (i) count of source samples to filter
  499. // <dwFlags> (i) control flags
  500. // PCMFILTER_LOWPASS perform a low pass filter
  501. // return 0 if success
  502. //
  503. // NOTE: <lppcm16Src> and <lppcm16Dst> can point to the same buffer
  504. //
  505. int DLLEXPORT WINAPI PcmFilter(HPCM hPcm,
  506. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples, DWORD dwFlags)
  507. {
  508. BOOL fSuccess = TRUE;
  509. LPPCM lpPcm;
  510. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  511. fSuccess = TraceFALSE(NULL);
  512. else if (lppcm16Src == NULL)
  513. fSuccess = TraceFALSE(NULL);
  514. else if (lppcm16Dst == NULL)
  515. fSuccess = TraceFALSE(NULL);
  516. else
  517. {
  518. PCM16 pcm16Prev0F = lpPcm->pcm16Prev0F;
  519. PCM16 pcm16Prev1F = lpPcm->pcm16Prev1F;
  520. PCM16 pcm16Prev2F = lpPcm->pcm16Prev2F;
  521. while (uSamples-- > 0)
  522. {
  523. pcm16Prev2F = pcm16Prev1F;
  524. pcm16Prev1F = pcm16Prev0F;
  525. pcm16Prev0F = *lppcm16Src++;
  526. *lppcm16Dst++ = (PCM16) ((__int32) pcm16Prev0F +
  527. ((__int32) pcm16Prev1F * 2) + (__int32) pcm16Prev2F) / 4;
  528. }
  529. lpPcm->pcm16Prev0F = pcm16Prev0F;
  530. lpPcm->pcm16Prev1F = pcm16Prev1F;
  531. lpPcm->pcm16Prev2F = pcm16Prev2F;
  532. }
  533. return fSuccess ? 0 : -1;
  534. }
  535. // PcmResample - resample pcm samples
  536. // <hPcm> (i) handle returned from PcmInit
  537. // <lppcm16Src> (i) buffer of source samples
  538. // <nSamplesPerSecSrc> (i) sample rate of source samples
  539. // <lppcm16Dst> (o) buffer to hold destination samples
  540. // <nSamplesPerSecDst> (i) sample rate of destination samples
  541. // <uSamples> (i) count of source samples to resample
  542. // <dwFlags> (i) control flags
  543. // 0 reserved, must be zero
  544. // return count of samples in destination buffer (0 if error)
  545. //
  546. // NOTE: the destination buffer must be large enough to hold the result
  547. //
  548. UINT DLLEXPORT WINAPI PcmResample(HPCM hPcm,
  549. LPPCM16 lppcm16Src, long nSamplesPerSecSrc,
  550. LPPCM16 lppcm16Dst, long nSamplesPerSecDst,
  551. UINT uSamples, DWORD dwFlags)
  552. {
  553. BOOL fSuccess = TRUE;
  554. LPPCM lpPcm;
  555. UINT uSamplesDst;
  556. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  557. fSuccess = TraceFALSE(NULL);
  558. else if (lppcm16Src == NULL)
  559. fSuccess = TraceFALSE(NULL);
  560. else if (lppcm16Dst == NULL)
  561. fSuccess = TraceFALSE(NULL);
  562. else switch (nSamplesPerSecSrc)
  563. {
  564. case 6000:
  565. {
  566. switch (nSamplesPerSecDst)
  567. {
  568. case 6000:
  569. // nothing to do but copy
  570. //
  571. MemCpy(lppcm16Dst, lppcm16Dst, uSamples * 2);
  572. uSamplesDst = uSamples;
  573. break;
  574. case 8000:
  575. uSamplesDst = PcmResample6Kto8K(lpPcm,
  576. lppcm16Src, lppcm16Dst, uSamples);
  577. break;
  578. case 11025:
  579. uSamplesDst = PcmResample6Kto11K(lpPcm,
  580. lppcm16Src, lppcm16Dst, uSamples);
  581. break;
  582. case 22050:
  583. uSamplesDst = PcmResample6Kto22K(lpPcm,
  584. lppcm16Src, lppcm16Dst, uSamples);
  585. break;
  586. case 44100:
  587. uSamplesDst = PcmResample6Kto44K(lpPcm,
  588. lppcm16Src, lppcm16Dst, uSamples);
  589. break;
  590. default:
  591. fSuccess = TraceFALSE(NULL);
  592. break;
  593. }
  594. }
  595. break;
  596. case 8000:
  597. switch (nSamplesPerSecDst)
  598. {
  599. case 6000:
  600. uSamplesDst = PcmResample8Kto6K(lpPcm,
  601. lppcm16Src, lppcm16Dst, uSamples);
  602. break;
  603. case 8000:
  604. // nothing to do but copy
  605. //
  606. MemCpy(lppcm16Dst, lppcm16Dst, uSamples * 2);
  607. uSamplesDst = uSamples;
  608. break;
  609. case 11025:
  610. uSamplesDst = PcmResample8Kto11K(lpPcm,
  611. lppcm16Src, lppcm16Dst, uSamples);
  612. break;
  613. case 22050:
  614. uSamplesDst = PcmResample8Kto22K(lpPcm,
  615. lppcm16Src, lppcm16Dst, uSamples);
  616. break;
  617. case 44100:
  618. uSamplesDst = PcmResample8Kto44K(lpPcm,
  619. lppcm16Src, lppcm16Dst, uSamples);
  620. break;
  621. default:
  622. fSuccess = TraceFALSE(NULL);
  623. break;
  624. }
  625. break;
  626. case 11025:
  627. switch (nSamplesPerSecDst)
  628. {
  629. case 6000:
  630. uSamplesDst = PcmResample11Kto6K(lpPcm,
  631. lppcm16Src, lppcm16Dst, uSamples);
  632. break;
  633. case 8000:
  634. uSamplesDst = PcmResample11Kto8K(lpPcm,
  635. lppcm16Src, lppcm16Dst, uSamples);
  636. break;
  637. case 11025:
  638. // nothing to do but copy
  639. //
  640. MemCpy(lppcm16Dst, lppcm16Dst, uSamples * 2);
  641. uSamplesDst = uSamples;
  642. break;
  643. case 22050:
  644. uSamplesDst = PcmResample11Kto22K(lpPcm,
  645. lppcm16Src, lppcm16Dst, uSamples);
  646. break;
  647. case 44100:
  648. uSamplesDst = PcmResample11Kto44K(lpPcm,
  649. lppcm16Src, lppcm16Dst, uSamples);
  650. break;
  651. default:
  652. fSuccess = TraceFALSE(NULL);
  653. break;
  654. }
  655. break;
  656. case 22050:
  657. switch (nSamplesPerSecDst)
  658. {
  659. case 6000:
  660. uSamplesDst = PcmResample22Kto6K(lpPcm,
  661. lppcm16Src, lppcm16Dst, uSamples);
  662. break;
  663. case 8000:
  664. uSamplesDst = PcmResample22Kto8K(lpPcm,
  665. lppcm16Src, lppcm16Dst, uSamples);
  666. break;
  667. case 11025:
  668. uSamplesDst = PcmResample22Kto11K(lpPcm,
  669. lppcm16Src, lppcm16Dst, uSamples);
  670. break;
  671. case 22050:
  672. // nothing to do but copy
  673. //
  674. MemCpy(lppcm16Dst, lppcm16Dst, uSamples * 2);
  675. uSamplesDst = uSamples;
  676. break;
  677. case 44100:
  678. uSamplesDst = PcmResample22Kto44K(lpPcm,
  679. lppcm16Src, lppcm16Dst, uSamples);
  680. break;
  681. default:
  682. fSuccess = TraceFALSE(NULL);
  683. break;
  684. }
  685. break;
  686. case 44100:
  687. switch (nSamplesPerSecDst)
  688. {
  689. case 6000:
  690. uSamplesDst = PcmResample44Kto6K(lpPcm,
  691. lppcm16Src, lppcm16Dst, uSamples);
  692. break;
  693. case 8000:
  694. uSamplesDst = PcmResample44Kto8K(lpPcm,
  695. lppcm16Src, lppcm16Dst, uSamples);
  696. break;
  697. case 11025:
  698. uSamplesDst = PcmResample44Kto11K(lpPcm,
  699. lppcm16Src, lppcm16Dst, uSamples);
  700. break;
  701. case 22050:
  702. uSamplesDst = PcmResample44Kto22K(lpPcm,
  703. lppcm16Src, lppcm16Dst, uSamples);
  704. break;
  705. case 44100:
  706. // nothing to do but copy
  707. //
  708. MemCpy(lppcm16Dst, lppcm16Dst, uSamples * 2);
  709. uSamplesDst = uSamples;
  710. break;
  711. default:
  712. fSuccess = TraceFALSE(NULL);
  713. break;
  714. }
  715. break;
  716. default:
  717. fSuccess = TraceFALSE(NULL);
  718. break;
  719. }
  720. return fSuccess ? uSamplesDst : 0;
  721. }
  722. ////
  723. // helper functions
  724. ////
  725. static UINT PcmResampleCalcDstMax(HPCM hPcm,
  726. long nSamplesPerSecSrc, long nSamplesPerSecDst, UINT uSamples)
  727. {
  728. BOOL fSuccess = TRUE;
  729. LPPCM lpPcm;
  730. UINT uSamplesDst;
  731. if ((lpPcm = PcmGetPtr(hPcm)) == NULL)
  732. fSuccess = TraceFALSE(NULL);
  733. else switch (nSamplesPerSecSrc)
  734. {
  735. case 6000:
  736. {
  737. switch (nSamplesPerSecDst)
  738. {
  739. case 6000:
  740. uSamplesDst = uSamples;
  741. break;
  742. case 8000:
  743. uSamplesDst = (UINT) (((long) uSamples * 8) / 6);
  744. break;
  745. case 11025:
  746. uSamplesDst = (UINT) (((long) uSamples * 11) / 6);
  747. break;
  748. case 22050:
  749. uSamplesDst = (UINT) (((long) uSamples * 22) / 6);
  750. break;
  751. case 44100:
  752. uSamplesDst = (UINT) (((long) uSamples * 44) / 6);
  753. break;
  754. default:
  755. fSuccess = TraceFALSE(NULL);
  756. break;
  757. }
  758. }
  759. break;
  760. case 8000:
  761. switch (nSamplesPerSecDst)
  762. {
  763. case 6000:
  764. uSamplesDst = (UINT) (((long) uSamples * 6) / 8);
  765. break;
  766. case 8000:
  767. uSamplesDst = uSamples;
  768. break;
  769. case 11025:
  770. uSamplesDst = (UINT) (((long) uSamples * 11) / 8);
  771. break;
  772. case 22050:
  773. uSamplesDst = (UINT) (((long) uSamples * 22) / 8);
  774. break;
  775. case 44100:
  776. uSamplesDst = (UINT) (((long) uSamples * 44) / 8);
  777. break;
  778. default:
  779. fSuccess = TraceFALSE(NULL);
  780. break;
  781. }
  782. break;
  783. case 11025:
  784. switch (nSamplesPerSecDst)
  785. {
  786. case 6000:
  787. uSamplesDst = (UINT) (((long) uSamples * 6) / 11);
  788. break;
  789. case 8000:
  790. uSamplesDst = (UINT) (((long) uSamples * 8) / 11);
  791. break;
  792. case 11025:
  793. uSamplesDst = uSamples;
  794. break;
  795. case 22050:
  796. uSamplesDst = (UINT) (((long) uSamples * 22) / 11);
  797. break;
  798. case 44100:
  799. uSamplesDst = (UINT) (((long) uSamples * 44) / 11);
  800. break;
  801. default:
  802. fSuccess = TraceFALSE(NULL);
  803. break;
  804. }
  805. break;
  806. case 22050:
  807. switch (nSamplesPerSecDst)
  808. {
  809. case 6000:
  810. uSamplesDst = (UINT) (((long) uSamples * 6) / 22);
  811. break;
  812. case 8000:
  813. uSamplesDst = (UINT) (((long) uSamples * 8) / 22);
  814. break;
  815. case 11025:
  816. uSamplesDst = (UINT) (((long) uSamples * 11) / 22);
  817. break;
  818. case 22050:
  819. uSamplesDst = uSamples;
  820. break;
  821. case 44100:
  822. uSamplesDst = (UINT) (((long) uSamples * 44) / 22);
  823. break;
  824. default:
  825. fSuccess = TraceFALSE(NULL);
  826. break;
  827. }
  828. break;
  829. case 44100:
  830. switch (nSamplesPerSecDst)
  831. {
  832. case 6000:
  833. uSamplesDst = (UINT) (((long) uSamples * 6) / 44);
  834. break;
  835. case 8000:
  836. uSamplesDst = (UINT) (((long) uSamples * 8) / 44);
  837. break;
  838. case 11025:
  839. uSamplesDst = (UINT) (((long) uSamples * 11) / 44);
  840. break;
  841. case 22050:
  842. uSamplesDst = (UINT) (((long) uSamples * 22) / 44);
  843. break;
  844. case 44100:
  845. uSamplesDst = uSamples;
  846. break;
  847. default:
  848. fSuccess = TraceFALSE(NULL);
  849. break;
  850. }
  851. break;
  852. default:
  853. fSuccess = TraceFALSE(NULL);
  854. break;
  855. }
  856. return fSuccess ? uSamplesDst : 0;
  857. }
  858. static UINT PcmResample6Kto8K(LPPCM lpPcm,
  859. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  860. {
  861. BOOL fSuccess = TRUE;
  862. LPPCM16 lppcm16DstSave = lppcm16Dst;
  863. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  864. short nCounter = lpPcm->nCounter;
  865. while (uSamples-- > 0)
  866. {
  867. PCM16 pcm16Src = *lppcm16Src++;
  868. if (++nCounter == 3)
  869. nCounter = 0;
  870. switch(nCounter)
  871. {
  872. case 0:
  873. *lppcm16Dst++ = pcm16Prev +
  874. MULDIV16(2, (pcm16Src - pcm16Prev), 8);
  875. *lppcm16Dst++ = pcm16Src;
  876. break;
  877. case 1:
  878. *lppcm16Dst++ = pcm16Prev +
  879. MULDIV16(6, (pcm16Src - pcm16Prev), 8);
  880. break;
  881. case 2:
  882. *lppcm16Dst++ = pcm16Prev +
  883. MULDIV16(4, (pcm16Src - pcm16Prev), 8);
  884. break;
  885. }
  886. pcm16Prev = pcm16Src;
  887. }
  888. lpPcm->pcm16Prev = pcm16Prev;
  889. lpPcm->nCounter = nCounter;
  890. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  891. }
  892. static UINT PcmResample6Kto11K(LPPCM lpPcm,
  893. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  894. {
  895. BOOL fSuccess = TRUE;
  896. LPPCM16 lppcm16DstSave = lppcm16Dst;
  897. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  898. short nCounter = lpPcm->nCounter;
  899. while (uSamples-- > 0)
  900. {
  901. PCM16 pcm16Src = *lppcm16Src++;
  902. if (++nCounter == 6)
  903. nCounter = 0;
  904. switch(nCounter)
  905. {
  906. case 0:
  907. *lppcm16Dst++ = pcm16Prev +
  908. MULDIV16(5, (pcm16Src - pcm16Prev), 11);
  909. *lppcm16Dst++ = pcm16Src;
  910. break;
  911. case 1:
  912. *lppcm16Dst++ = pcm16Prev +
  913. MULDIV16(6, (pcm16Src - pcm16Prev), 11);
  914. break;
  915. case 2:
  916. {
  917. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  918. *lppcm16Dst++ = pcm16Prev +
  919. (pcm16Delta) / 11;
  920. *lppcm16Dst++ = pcm16Prev +
  921. MULDIV16(7, (pcm16Delta), 11);
  922. }
  923. break;
  924. case 3:
  925. {
  926. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  927. *lppcm16Dst++ = pcm16Prev +
  928. MULDIV16(2, (pcm16Delta), 11);
  929. *lppcm16Dst++ = pcm16Prev +
  930. MULDIV16(8, (pcm16Delta), 11);
  931. }
  932. break;
  933. case 4:
  934. {
  935. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  936. *lppcm16Dst++ = pcm16Prev +
  937. MULDIV16(3, (pcm16Delta), 11);
  938. *lppcm16Dst++ = pcm16Prev +
  939. MULDIV16(9, (pcm16Delta), 11);
  940. }
  941. break;
  942. case 5:
  943. {
  944. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  945. *lppcm16Dst++ = pcm16Prev +
  946. MULDIV16(4, (pcm16Delta), 11);
  947. *lppcm16Dst++ = pcm16Prev +
  948. MULDIV16(10, (pcm16Delta), 11);
  949. }
  950. break;
  951. }
  952. pcm16Prev = pcm16Src;
  953. }
  954. lpPcm->pcm16Prev = pcm16Prev;
  955. lpPcm->nCounter = nCounter;
  956. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  957. }
  958. static UINT PcmResample6Kto22K(LPPCM lpPcm,
  959. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  960. {
  961. BOOL fSuccess = TRUE;
  962. LPPCM16 lppcm16DstSave = lppcm16Dst;
  963. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  964. short nCounter = lpPcm->nCounter;
  965. while (uSamples-- > 0)
  966. {
  967. PCM16 pcm16Src = *lppcm16Src++;
  968. if (++nCounter == 3)
  969. nCounter = 0;
  970. switch(nCounter)
  971. {
  972. case 0:
  973. {
  974. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  975. *lppcm16Dst++ = pcm16Prev +
  976. MULDIV16(2, (pcm16Delta), 11);
  977. *lppcm16Dst++ = pcm16Prev +
  978. MULDIV16(5, (pcm16Delta), 11);
  979. *lppcm16Dst++ = pcm16Prev +
  980. MULDIV16(8, (pcm16Delta), 11);
  981. *lppcm16Dst++ = pcm16Src;
  982. }
  983. break;
  984. case 1:
  985. {
  986. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  987. *lppcm16Dst++ = pcm16Prev +
  988. MULDIV16(3, (pcm16Delta), 11);
  989. *lppcm16Dst++ = pcm16Prev +
  990. MULDIV16(6, (pcm16Delta), 11);
  991. *lppcm16Dst++ = pcm16Prev +
  992. MULDIV16(9, (pcm16Delta), 11);
  993. }
  994. break;
  995. case 2:
  996. {
  997. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  998. *lppcm16Dst++ = pcm16Prev +
  999. (pcm16Delta) / 11;
  1000. *lppcm16Dst++ = pcm16Prev +
  1001. MULDIV16(4, (pcm16Delta), 11);
  1002. *lppcm16Dst++ = pcm16Prev +
  1003. MULDIV16(7, (pcm16Delta), 11);
  1004. *lppcm16Dst++ = pcm16Prev +
  1005. MULDIV16(10, (pcm16Delta), 11);
  1006. }
  1007. break;
  1008. }
  1009. pcm16Prev = pcm16Src;
  1010. }
  1011. lpPcm->pcm16Prev = pcm16Prev;
  1012. lpPcm->nCounter = nCounter;
  1013. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1014. }
  1015. static UINT PcmResample6Kto44K(LPPCM lpPcm,
  1016. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1017. {
  1018. BOOL fSuccess = TRUE;
  1019. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1020. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1021. short nCounter = lpPcm->nCounter;
  1022. while (uSamples-- > 0)
  1023. {
  1024. PCM16 pcm16Src = *lppcm16Src++;
  1025. if (++nCounter == 3)
  1026. nCounter = 0;
  1027. switch(nCounter)
  1028. {
  1029. case 0:
  1030. {
  1031. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1032. *lppcm16Dst++ = pcm16Prev +
  1033. (pcm16Delta) / 22;
  1034. *lppcm16Dst++ = pcm16Prev +
  1035. MULDIV16(4, (pcm16Delta), 22);
  1036. *lppcm16Dst++ = pcm16Prev +
  1037. MULDIV16(7, (pcm16Delta), 22);
  1038. *lppcm16Dst++ = pcm16Prev +
  1039. MULDIV16(10, (pcm16Delta), 22);
  1040. *lppcm16Dst++ = pcm16Prev +
  1041. MULDIV16(13, (pcm16Delta), 22);
  1042. *lppcm16Dst++ = pcm16Prev +
  1043. MULDIV16(16, (pcm16Delta), 22);
  1044. *lppcm16Dst++ = pcm16Prev +
  1045. MULDIV16(19, (pcm16Delta), 22);
  1046. *lppcm16Dst++ = pcm16Src;
  1047. }
  1048. break;
  1049. case 1:
  1050. {
  1051. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1052. *lppcm16Dst++ = pcm16Prev +
  1053. MULDIV16(3, (pcm16Delta), 22);
  1054. *lppcm16Dst++ = pcm16Prev +
  1055. MULDIV16(6, (pcm16Delta), 22);
  1056. *lppcm16Dst++ = pcm16Prev +
  1057. MULDIV16(9, (pcm16Delta), 22);
  1058. *lppcm16Dst++ = pcm16Prev +
  1059. MULDIV16(12, (pcm16Delta), 22);
  1060. *lppcm16Dst++ = pcm16Prev +
  1061. MULDIV16(15, (pcm16Delta), 22);
  1062. *lppcm16Dst++ = pcm16Prev +
  1063. MULDIV16(18, (pcm16Delta), 22);
  1064. *lppcm16Dst++ = pcm16Prev +
  1065. MULDIV16(21, (pcm16Delta), 22);
  1066. }
  1067. break;
  1068. case 2:
  1069. {
  1070. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1071. *lppcm16Dst++ = pcm16Prev +
  1072. MULDIV16(2, (pcm16Delta), 22);
  1073. *lppcm16Dst++ = pcm16Prev +
  1074. MULDIV16(5, (pcm16Delta), 22);
  1075. *lppcm16Dst++ = pcm16Prev +
  1076. MULDIV16(8, (pcm16Delta), 22);
  1077. *lppcm16Dst++ = pcm16Prev +
  1078. MULDIV16(11, (pcm16Delta), 22);
  1079. *lppcm16Dst++ = pcm16Prev +
  1080. MULDIV16(14, (pcm16Delta), 22);
  1081. *lppcm16Dst++ = pcm16Prev +
  1082. MULDIV16(17, (pcm16Delta), 22);
  1083. *lppcm16Dst++ = pcm16Prev +
  1084. MULDIV16(20, (pcm16Delta), 22);
  1085. }
  1086. break;
  1087. }
  1088. pcm16Prev = pcm16Src;
  1089. }
  1090. lpPcm->pcm16Prev = pcm16Prev;
  1091. lpPcm->nCounter = nCounter;
  1092. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1093. }
  1094. static UINT PcmResample8Kto6K(LPPCM lpPcm,
  1095. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1096. {
  1097. BOOL fSuccess = TRUE;
  1098. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1099. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1100. short nCounter = lpPcm->nCounter;
  1101. while (uSamples-- > 0)
  1102. {
  1103. PCM16 pcm16Src = *lppcm16Src++;
  1104. if (++nCounter == 4)
  1105. nCounter = 0;
  1106. switch(nCounter)
  1107. {
  1108. case 0:
  1109. *lppcm16Dst++ = pcm16Src;
  1110. break;
  1111. case 1:
  1112. break;
  1113. case 2:
  1114. *lppcm16Dst++ = pcm16Prev +
  1115. MULDIV16(2, (pcm16Src - pcm16Prev), 6);
  1116. break;
  1117. case 3:
  1118. *lppcm16Dst++ = pcm16Prev +
  1119. MULDIV16(4, (pcm16Src - pcm16Prev), 6);
  1120. break;
  1121. }
  1122. pcm16Prev = pcm16Src;
  1123. }
  1124. lpPcm->pcm16Prev = pcm16Prev;
  1125. lpPcm->nCounter = nCounter;
  1126. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1127. }
  1128. static UINT PcmResample8Kto11K(LPPCM lpPcm,
  1129. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1130. {
  1131. BOOL fSuccess = TRUE;
  1132. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1133. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1134. short nCounter = lpPcm->nCounter;
  1135. while (uSamples-- > 0)
  1136. {
  1137. PCM16 pcm16Src = *lppcm16Src++;
  1138. if (++nCounter == 8)
  1139. nCounter = 0;
  1140. switch(nCounter)
  1141. {
  1142. case 0:
  1143. *lppcm16Dst++ = pcm16Prev +
  1144. MULDIV16(3, (pcm16Src - pcm16Prev), 11);
  1145. *lppcm16Dst++ = pcm16Src;
  1146. break;
  1147. case 1:
  1148. *lppcm16Dst++ = pcm16Prev +
  1149. MULDIV16(8, (pcm16Src - pcm16Prev), 11);
  1150. break;
  1151. case 2:
  1152. *lppcm16Dst++ = pcm16Prev +
  1153. MULDIV16(5, (pcm16Src - pcm16Prev), 11);
  1154. break;
  1155. case 3:
  1156. {
  1157. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1158. *lppcm16Dst++ = pcm16Prev +
  1159. MULDIV16(2, (pcm16Delta), 11);
  1160. *lppcm16Dst++ = pcm16Prev +
  1161. MULDIV16(10, (pcm16Delta), 11);
  1162. }
  1163. break;
  1164. case 4:
  1165. *lppcm16Dst++ = pcm16Prev +
  1166. MULDIV16(7, (pcm16Src - pcm16Prev), 11);
  1167. break;
  1168. case 5:
  1169. *lppcm16Dst++ = pcm16Prev +
  1170. MULDIV16(4, (pcm16Src - pcm16Prev), 11);
  1171. break;
  1172. case 6:
  1173. {
  1174. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1175. *lppcm16Dst++ = pcm16Prev +
  1176. (pcm16Delta) / 11;
  1177. *lppcm16Dst++ = pcm16Prev +
  1178. MULDIV16(9, (pcm16Delta), 11);
  1179. }
  1180. break;
  1181. case 7:
  1182. *lppcm16Dst++ = pcm16Prev +
  1183. MULDIV16(6, (pcm16Src - pcm16Prev), 11);
  1184. break;
  1185. }
  1186. pcm16Prev = pcm16Src;
  1187. }
  1188. lpPcm->pcm16Prev = pcm16Prev;
  1189. lpPcm->nCounter = nCounter;
  1190. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1191. }
  1192. static UINT PcmResample8Kto22K(LPPCM lpPcm,
  1193. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1194. {
  1195. BOOL fSuccess = TRUE;
  1196. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1197. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1198. short nCounter = lpPcm->nCounter;
  1199. while (uSamples-- > 0)
  1200. {
  1201. PCM16 pcm16Src = *lppcm16Src++;
  1202. if (++nCounter == 4)
  1203. nCounter = 0;
  1204. switch(nCounter)
  1205. {
  1206. case 0:
  1207. {
  1208. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1209. *lppcm16Dst++ = pcm16Prev +
  1210. MULDIV16(3, (pcm16Delta), 11);
  1211. *lppcm16Dst++ = pcm16Prev +
  1212. MULDIV16(7, (pcm16Delta), 11);
  1213. *lppcm16Dst++ = pcm16Src;
  1214. }
  1215. break;
  1216. case 1:
  1217. {
  1218. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1219. *lppcm16Dst++ = pcm16Prev +
  1220. MULDIV16(4, (pcm16Delta), 11);
  1221. *lppcm16Dst++ = pcm16Prev +
  1222. MULDIV16(8, (pcm16Delta), 11);
  1223. }
  1224. break;
  1225. case 2:
  1226. {
  1227. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1228. *lppcm16Dst++ = pcm16Prev +
  1229. (pcm16Delta) / 11;
  1230. *lppcm16Dst++ = pcm16Prev +
  1231. MULDIV16(5, (pcm16Delta), 11);
  1232. *lppcm16Dst++ = pcm16Prev +
  1233. MULDIV16(9, (pcm16Delta), 11);
  1234. }
  1235. break;
  1236. case 3:
  1237. {
  1238. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1239. *lppcm16Dst++ = pcm16Prev +
  1240. MULDIV16(2, (pcm16Delta), 11);
  1241. *lppcm16Dst++ = pcm16Prev +
  1242. MULDIV16(6, (pcm16Delta), 11);
  1243. *lppcm16Dst++ = pcm16Prev +
  1244. MULDIV16(10, (pcm16Delta), 11);
  1245. }
  1246. break;
  1247. }
  1248. pcm16Prev = pcm16Src;
  1249. }
  1250. lpPcm->pcm16Prev = pcm16Prev;
  1251. lpPcm->nCounter = nCounter;
  1252. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1253. }
  1254. static UINT PcmResample8Kto44K(LPPCM lpPcm,
  1255. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1256. {
  1257. BOOL fSuccess = TRUE;
  1258. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1259. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1260. short nCounter = lpPcm->nCounter;
  1261. while (uSamples-- > 0)
  1262. {
  1263. PCM16 pcm16Src = *lppcm16Src++;
  1264. if (++nCounter == 2)
  1265. nCounter = 0;
  1266. switch(nCounter)
  1267. {
  1268. case 0:
  1269. {
  1270. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1271. *lppcm16Dst++ = pcm16Prev +
  1272. (pcm16Delta) / 11;
  1273. *lppcm16Dst++ = pcm16Prev +
  1274. MULDIV16(3, (pcm16Delta), 11);
  1275. *lppcm16Dst++ = pcm16Prev +
  1276. MULDIV16(5, (pcm16Delta), 11);
  1277. *lppcm16Dst++ = pcm16Prev +
  1278. MULDIV16(7, (pcm16Delta), 11);
  1279. *lppcm16Dst++ = pcm16Prev +
  1280. MULDIV16(9, (pcm16Delta), 11);
  1281. *lppcm16Dst++ = pcm16Src;
  1282. }
  1283. break;
  1284. case 1:
  1285. {
  1286. PCM16 pcm16Delta = pcm16Src - pcm16Prev;
  1287. *lppcm16Dst++ = pcm16Prev +
  1288. MULDIV16(2, (pcm16Delta), 11);
  1289. *lppcm16Dst++ = pcm16Prev +
  1290. MULDIV16(4, (pcm16Delta), 11);
  1291. *lppcm16Dst++ = pcm16Prev +
  1292. MULDIV16(6, (pcm16Delta), 11);
  1293. *lppcm16Dst++ = pcm16Prev +
  1294. MULDIV16(8, (pcm16Delta), 11);
  1295. *lppcm16Dst++ = pcm16Prev +
  1296. MULDIV16(10, (pcm16Delta), 11);
  1297. }
  1298. break;
  1299. }
  1300. pcm16Prev = pcm16Src;
  1301. }
  1302. lpPcm->pcm16Prev = pcm16Prev;
  1303. lpPcm->nCounter = nCounter;
  1304. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1305. }
  1306. static UINT PcmResample11Kto6K(LPPCM lpPcm,
  1307. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1308. {
  1309. BOOL fSuccess = TRUE;
  1310. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1311. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1312. short nCounter = lpPcm->nCounter;
  1313. while (uSamples-- > 0)
  1314. {
  1315. PCM16 pcm16Src = *lppcm16Src++;
  1316. if (++nCounter == 11)
  1317. nCounter = 0;
  1318. switch(nCounter)
  1319. {
  1320. case 0:
  1321. *lppcm16Dst++ = pcm16Src;
  1322. break;
  1323. case 1:
  1324. break;
  1325. case 2:
  1326. *lppcm16Dst++ = pcm16Prev +
  1327. MULDIV16(5, (pcm16Src - pcm16Prev), 6);
  1328. break;
  1329. case 3:
  1330. break;
  1331. case 4:
  1332. *lppcm16Dst++ = pcm16Prev +
  1333. MULDIV16(4, (pcm16Src - pcm16Prev), 6);
  1334. break;
  1335. case 5:
  1336. break;
  1337. case 6:
  1338. *lppcm16Dst++ = pcm16Prev +
  1339. MULDIV16(3, (pcm16Src - pcm16Prev), 6);
  1340. break;
  1341. case 7:
  1342. break;
  1343. case 8:
  1344. *lppcm16Dst++ = pcm16Prev +
  1345. MULDIV16(2, (pcm16Src - pcm16Prev), 6);
  1346. break;
  1347. case 9:
  1348. break;
  1349. case 10:
  1350. *lppcm16Dst++ = pcm16Prev +
  1351. (pcm16Src - pcm16Prev) / 6;
  1352. break;
  1353. }
  1354. pcm16Prev = pcm16Src;
  1355. }
  1356. lpPcm->pcm16Prev = pcm16Prev;
  1357. lpPcm->nCounter = nCounter;
  1358. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1359. }
  1360. static UINT PcmResample11Kto8K(LPPCM lpPcm,
  1361. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1362. {
  1363. BOOL fSuccess = TRUE;
  1364. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1365. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1366. short nCounter = lpPcm->nCounter;
  1367. while (uSamples-- > 0)
  1368. {
  1369. PCM16 pcm16Src = *lppcm16Src++;
  1370. if (++nCounter == 11)
  1371. nCounter = 0;
  1372. switch(nCounter)
  1373. {
  1374. case 0:
  1375. *lppcm16Dst++ = pcm16Src;
  1376. break;
  1377. case 1:
  1378. break;
  1379. case 2:
  1380. *lppcm16Dst++ = pcm16Prev +
  1381. MULDIV16(3, (pcm16Src - pcm16Prev), 8);
  1382. break;
  1383. case 3:
  1384. *lppcm16Dst++ = pcm16Prev +
  1385. MULDIV16(6, (pcm16Src - pcm16Prev), 8);
  1386. break;
  1387. case 4:
  1388. break;
  1389. case 5:
  1390. *lppcm16Dst++ = pcm16Prev +
  1391. (pcm16Src - pcm16Prev) / 8;
  1392. break;
  1393. case 6:
  1394. *lppcm16Dst++ = pcm16Prev +
  1395. MULDIV16(4, (pcm16Src - pcm16Prev), 8);
  1396. break;
  1397. case 7:
  1398. *lppcm16Dst++ = pcm16Prev +
  1399. MULDIV16(7, (pcm16Src - pcm16Prev), 8);
  1400. break;
  1401. case 8:
  1402. break;
  1403. case 9:
  1404. *lppcm16Dst++ = pcm16Prev +
  1405. MULDIV16(2, (pcm16Src - pcm16Prev), 8);
  1406. break;
  1407. case 10:
  1408. *lppcm16Dst++ = pcm16Prev +
  1409. MULDIV16(5, (pcm16Src - pcm16Prev), 8);
  1410. break;
  1411. }
  1412. pcm16Prev = pcm16Src;
  1413. }
  1414. lpPcm->pcm16Prev = pcm16Prev;
  1415. lpPcm->nCounter = nCounter;
  1416. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1417. }
  1418. static UINT PcmResample11Kto22K(LPPCM lpPcm,
  1419. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1420. {
  1421. BOOL fSuccess = TRUE;
  1422. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1423. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1424. while (uSamples-- > 0)
  1425. {
  1426. PCM16 pcm16Src = *lppcm16Src++;
  1427. *lppcm16Dst++ = pcm16Prev +
  1428. (pcm16Src - pcm16Prev) / 2;
  1429. *lppcm16Dst++ = pcm16Src;
  1430. pcm16Prev = pcm16Src;
  1431. }
  1432. lpPcm->pcm16Prev = pcm16Prev;
  1433. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1434. }
  1435. static UINT PcmResample11Kto44K(LPPCM lpPcm,
  1436. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1437. {
  1438. BOOL fSuccess = TRUE;
  1439. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1440. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1441. while (uSamples-- > 0)
  1442. {
  1443. PCM16 pcm16Src = *lppcm16Src++;
  1444. *lppcm16Dst++ = pcm16Prev +
  1445. (pcm16Src - pcm16Prev) / 4;
  1446. *lppcm16Dst++ = pcm16Prev +
  1447. MULDIV16(2, (pcm16Src - pcm16Prev), 4);
  1448. *lppcm16Dst++ = pcm16Prev +
  1449. MULDIV16(3, (pcm16Src - pcm16Prev), 4);
  1450. *lppcm16Dst++ = pcm16Src;
  1451. pcm16Prev = pcm16Src;
  1452. }
  1453. lpPcm->pcm16Prev = pcm16Prev;
  1454. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1455. }
  1456. static UINT PcmResample22Kto6K(LPPCM lpPcm,
  1457. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1458. {
  1459. BOOL fSuccess = TRUE;
  1460. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1461. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1462. short nCounter = lpPcm->nCounter;
  1463. while (uSamples-- > 0)
  1464. {
  1465. PCM16 pcm16Src = *lppcm16Src++;
  1466. if (++nCounter == 11)
  1467. nCounter = 0;
  1468. switch(nCounter)
  1469. {
  1470. case 0:
  1471. *lppcm16Dst++ = pcm16Src;
  1472. break;
  1473. case 1:
  1474. break;
  1475. case 2:
  1476. break;
  1477. case 3:
  1478. break;
  1479. case 4:
  1480. *lppcm16Dst++ = pcm16Prev +
  1481. MULDIV16(2, (pcm16Src - pcm16Prev), 3);
  1482. break;
  1483. case 5:
  1484. break;
  1485. case 6:
  1486. break;
  1487. case 7:
  1488. break;
  1489. case 8:
  1490. *lppcm16Dst++ = pcm16Prev +
  1491. (pcm16Src - pcm16Prev) / 3;
  1492. break;
  1493. case 9:
  1494. break;
  1495. case 10:
  1496. break;
  1497. }
  1498. pcm16Prev = pcm16Src;
  1499. }
  1500. lpPcm->pcm16Prev = pcm16Prev;
  1501. lpPcm->nCounter = nCounter;
  1502. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1503. }
  1504. static UINT PcmResample22Kto8K(LPPCM lpPcm,
  1505. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1506. {
  1507. BOOL fSuccess = TRUE;
  1508. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1509. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1510. short nCounter = lpPcm->nCounter;
  1511. while (uSamples-- > 0)
  1512. {
  1513. PCM16 pcm16Src = *lppcm16Src++;
  1514. if (++nCounter == 11)
  1515. nCounter = 0;
  1516. switch(nCounter)
  1517. {
  1518. case 0:
  1519. *lppcm16Dst++ = pcm16Src;
  1520. break;
  1521. case 1:
  1522. break;
  1523. case 2:
  1524. break;
  1525. case 3:
  1526. *lppcm16Dst++ = pcm16Prev +
  1527. MULDIV16(3, (pcm16Src - pcm16Prev), 4);
  1528. break;
  1529. case 4:
  1530. break;
  1531. case 5:
  1532. break;
  1533. case 6:
  1534. *lppcm16Dst++ = pcm16Prev +
  1535. MULDIV16(2, (pcm16Src - pcm16Prev), 4);
  1536. break;
  1537. case 7:
  1538. break;
  1539. case 8:
  1540. break;
  1541. case 9:
  1542. *lppcm16Dst++ = pcm16Prev +
  1543. (pcm16Src - pcm16Prev) / 4;
  1544. break;
  1545. case 10:
  1546. break;
  1547. }
  1548. pcm16Prev = pcm16Src;
  1549. }
  1550. lpPcm->pcm16Prev = pcm16Prev;
  1551. lpPcm->nCounter = nCounter;
  1552. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1553. }
  1554. static UINT PcmResample22Kto11K(LPPCM lpPcm,
  1555. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1556. {
  1557. BOOL fSuccess = TRUE;
  1558. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1559. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1560. short nCounter = lpPcm->nCounter;
  1561. while (uSamples-- > 0)
  1562. {
  1563. PCM16 pcm16Src = *lppcm16Src++;
  1564. if (++nCounter == 2)
  1565. nCounter = 0;
  1566. switch(nCounter)
  1567. {
  1568. case 0:
  1569. break;
  1570. case 1:
  1571. *lppcm16Dst++ = pcm16Prev +
  1572. (pcm16Src - pcm16Prev) / 2;
  1573. break;
  1574. }
  1575. pcm16Prev = pcm16Src;
  1576. }
  1577. lpPcm->pcm16Prev = pcm16Prev;
  1578. lpPcm->nCounter = nCounter;
  1579. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1580. }
  1581. static UINT PcmResample22Kto44K(LPPCM lpPcm,
  1582. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1583. {
  1584. BOOL fSuccess = TRUE;
  1585. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1586. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1587. while (uSamples-- > 0)
  1588. {
  1589. PCM16 pcm16Src = *lppcm16Src++;
  1590. *lppcm16Dst++ = pcm16Prev +
  1591. (pcm16Src - pcm16Prev) / 2;
  1592. *lppcm16Dst++ = pcm16Src;
  1593. pcm16Prev = pcm16Src;
  1594. }
  1595. lpPcm->pcm16Prev = pcm16Prev;
  1596. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1597. }
  1598. static UINT PcmResample44Kto6K(LPPCM lpPcm,
  1599. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1600. {
  1601. BOOL fSuccess = TRUE;
  1602. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1603. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1604. short nCounter = lpPcm->nCounter;
  1605. while (uSamples-- > 0)
  1606. {
  1607. PCM16 pcm16Src = *lppcm16Src++;
  1608. if (++nCounter == 22)
  1609. nCounter = 0;
  1610. switch(nCounter)
  1611. {
  1612. case 0:
  1613. *lppcm16Dst++ = pcm16Src;
  1614. break;
  1615. case 1:
  1616. break;
  1617. case 2:
  1618. break;
  1619. case 3:
  1620. break;
  1621. case 4:
  1622. break;
  1623. case 5:
  1624. break;
  1625. case 6:
  1626. break;
  1627. case 7:
  1628. break;
  1629. case 8:
  1630. *lppcm16Dst++ = pcm16Prev +
  1631. (pcm16Src - pcm16Prev) / 3;
  1632. break;
  1633. case 9:
  1634. break;
  1635. case 10:
  1636. break;
  1637. case 11:
  1638. break;
  1639. case 12:
  1640. break;
  1641. case 13:
  1642. break;
  1643. case 14:
  1644. break;
  1645. case 15:
  1646. *lppcm16Dst++ = pcm16Prev +
  1647. MULDIV16(2, (pcm16Src - pcm16Prev), 3);
  1648. break;
  1649. case 16:
  1650. break;
  1651. case 17:
  1652. break;
  1653. case 18:
  1654. break;
  1655. case 19:
  1656. break;
  1657. case 20:
  1658. break;
  1659. case 21:
  1660. break;
  1661. }
  1662. pcm16Prev = pcm16Src;
  1663. }
  1664. lpPcm->pcm16Prev = pcm16Prev;
  1665. lpPcm->nCounter = nCounter;
  1666. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1667. }
  1668. static UINT PcmResample44Kto8K(LPPCM lpPcm,
  1669. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1670. {
  1671. BOOL fSuccess = TRUE;
  1672. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1673. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1674. short nCounter = lpPcm->nCounter;
  1675. while (uSamples-- > 0)
  1676. {
  1677. PCM16 pcm16Src = *lppcm16Src++;
  1678. if (++nCounter == 11)
  1679. nCounter = 0;
  1680. switch(nCounter)
  1681. {
  1682. case 0:
  1683. *lppcm16Dst++ = pcm16Src;
  1684. break;
  1685. case 1:
  1686. break;
  1687. case 2:
  1688. break;
  1689. case 3:
  1690. break;
  1691. case 4:
  1692. break;
  1693. case 5:
  1694. break;
  1695. case 6:
  1696. *lppcm16Dst++ = pcm16Prev +
  1697. (pcm16Src - pcm16Prev) / 2;
  1698. break;
  1699. case 7:
  1700. break;
  1701. case 8:
  1702. break;
  1703. case 9:
  1704. break;
  1705. case 10:
  1706. break;
  1707. }
  1708. pcm16Prev = pcm16Src;
  1709. }
  1710. lpPcm->pcm16Prev = pcm16Prev;
  1711. lpPcm->nCounter = nCounter;
  1712. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1713. }
  1714. static UINT PcmResample44Kto11K(LPPCM lpPcm,
  1715. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1716. {
  1717. BOOL fSuccess = TRUE;
  1718. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1719. PCM16 pcm16Prev0 = lpPcm->pcm16Prev0;
  1720. PCM16 pcm16Prev1 = lpPcm->pcm16Prev1;
  1721. PCM16 pcm16Prev2 = lpPcm->pcm16Prev2;
  1722. short nCounter = lpPcm->nCounter;
  1723. while (uSamples-- > 0)
  1724. {
  1725. PCM16 pcm16Src = *lppcm16Src++;
  1726. if (++nCounter == 4)
  1727. nCounter = 0;
  1728. switch(nCounter)
  1729. {
  1730. case 0:
  1731. pcm16Prev0 = pcm16Src;
  1732. break;
  1733. case 1:
  1734. pcm16Prev1 = pcm16Src;
  1735. break;
  1736. case 2:
  1737. pcm16Prev2 = pcm16Src;
  1738. break;
  1739. case 3:
  1740. *lppcm16Dst++ = (PCM16) ((__int32) pcm16Prev0 +
  1741. (__int32) pcm16Prev1 + (__int32) pcm16Prev2 +
  1742. (__int32) pcm16Src) / 4;
  1743. break;
  1744. }
  1745. }
  1746. lpPcm->pcm16Prev0 = pcm16Prev0;
  1747. lpPcm->pcm16Prev1 = pcm16Prev1;
  1748. lpPcm->pcm16Prev2 = pcm16Prev2;
  1749. lpPcm->nCounter = nCounter;
  1750. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1751. }
  1752. static UINT PcmResample44Kto22K(LPPCM lpPcm,
  1753. LPPCM16 lppcm16Src, LPPCM16 lppcm16Dst, UINT uSamples)
  1754. {
  1755. BOOL fSuccess = TRUE;
  1756. LPPCM16 lppcm16DstSave = lppcm16Dst;
  1757. PCM16 pcm16Prev = lpPcm->pcm16Prev;
  1758. short nCounter = lpPcm->nCounter;
  1759. while (uSamples-- > 0)
  1760. {
  1761. PCM16 pcm16Src = *lppcm16Src++;
  1762. if (++nCounter == 2)
  1763. nCounter = 0;
  1764. switch(nCounter)
  1765. {
  1766. case 0:
  1767. break;
  1768. case 1:
  1769. *lppcm16Dst++ = pcm16Prev +
  1770. (pcm16Src - pcm16Prev) / 2;
  1771. break;
  1772. }
  1773. pcm16Prev = pcm16Src;
  1774. }
  1775. lpPcm->pcm16Prev = pcm16Prev;
  1776. lpPcm->nCounter = nCounter;
  1777. return fSuccess ? (UINT) (lppcm16Dst - lppcm16DstSave) : 0;
  1778. }
  1779. // PcmGetPtr - verify that pcm handle is valid,
  1780. // <hPcm> (i) handle returned from PcmInit
  1781. // return corresponding pcm pointer (NULL if error)
  1782. //
  1783. static LPPCM PcmGetPtr(HPCM hPcm)
  1784. {
  1785. BOOL fSuccess = TRUE;
  1786. LPPCM lpPcm;
  1787. if ((lpPcm = (LPPCM) hPcm) == NULL)
  1788. fSuccess = TraceFALSE(NULL);
  1789. else if (IsBadWritePtr(lpPcm, sizeof(PCM)))
  1790. fSuccess = TraceFALSE(NULL);
  1791. #ifdef CHECKTASK
  1792. // make sure current task owns the pcm handle
  1793. //
  1794. else if (lpPcm->hTask != GetCurrentTask())
  1795. fSuccess = TraceFALSE(NULL);
  1796. #endif
  1797. return fSuccess ? lpPcm : NULL;
  1798. }
  1799. // PcmGetHandle - verify that pcm pointer is valid,
  1800. // <lpPcm> (i) pointer to PCM struct
  1801. // return corresponding pcm handle (NULL if error)
  1802. //
  1803. static HPCM PcmGetHandle(LPPCM lpPcm)
  1804. {
  1805. BOOL fSuccess = TRUE;
  1806. HPCM hPcm;
  1807. if ((hPcm = (HPCM) lpPcm) == NULL)
  1808. fSuccess = TraceFALSE(NULL);
  1809. return fSuccess ? hPcm : NULL;
  1810. }