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.

1355 lines
52 KiB

  1. /*
  2. ** Copyright (c) 1985-1998 Microsoft Corporation
  3. **
  4. ** Title: mwrec.c - Multimedia Systems Media Control Interface
  5. ** waveform digital audio driver for RIFF wave files.
  6. ** Routines for recording wave files
  7. */
  8. /*
  9. ** Change log:
  10. **
  11. ** DATE REV DESCRIPTION
  12. ** ----------- ----- ------------------------------------------
  13. ** 18-APR-1990 ROBWI Original
  14. ** 19-JUN-1990 ROBWI Added wave in
  15. ** 13-Jan-1992 MikeTri Ported to NT
  16. ** Aug-1994 Lauriegr This is all out of date
  17. */
  18. /*******************************************************************************
  19. ** !!READ THIS!! *
  20. ** !!READ THIS!! *
  21. ** !!READ THIS!! *
  22. ** *
  23. ** SEE MWREC.NEW FOR A SLIGHTLY BETTER PATCHED UP VERSION WITH MORE EXPLANATION
  24. ** ADDED. YOU MIGHT WANT TO START FROM THERE INSTEAD
  25. **
  26. ** As far as I can make out, this code was never finished.
  27. ** The scheme (which I tried to start writing up in MCIWAVE.H) is that there are
  28. ** a series of NODEs which describe a wave file. As long as there is in fact
  29. ** only one NODE for the file (which is probably the only common case) then this
  30. ** all works fine. If there are multiple NODEs (which you arrive at by inserting
  31. ** bits or deleting bits from the middle) then it all falls apart.
  32. **
  33. ** We're pretty sure nobody's ever used this stuff as it's been broken for years
  34. ** in 16 and 32 bit. We've discovered it just as Daytona is about to ship (that's
  35. ** Windows/NT version 3.5). Maybe NMM wil replace it all anyway.
  36. **
  37. ** This is a half-patched up version with several questions left outstanding.
  38. **
  39. */
  40. #define UNICODE
  41. #define NOGDICAPMASKS
  42. #define NOVIRTUALKEYCODES
  43. #define NOWINSTYLES
  44. #define NOSYSMETRICS
  45. #define NOMENUS
  46. #define NOICONS
  47. #define NOKEYSTATES
  48. #define NOSYSCOMMANDS
  49. #define NORASTEROPS
  50. #define NOSHOWWINDOW
  51. #define OEMRESOURCE
  52. #define NOATOM
  53. #define NOCLIPBOARD
  54. #define NOCOLOR
  55. #define NOCTLMGR
  56. #define NODRAWTEXT
  57. #define NOGDI
  58. #define NOKERNEL
  59. #define NONLS
  60. #define NOMB
  61. #define NOMEMMGR
  62. #define NOMETAFILE
  63. #define NOOPENFILE
  64. #define NOSCROLL
  65. #define NOTEXTMETRIC
  66. #define NOWH
  67. #define NOWINOFFSETS
  68. #define NOCOMM
  69. #define NOKANJI
  70. #define NOHELP
  71. #define NOPROFILER
  72. #define NODEFERWINDOWPOS
  73. #include <windows.h>
  74. #include "mciwave.h"
  75. #include <mmddk.h>
  76. #include <gmem.h> // 'cos of GAllocPtrF etc.
  77. /************************************************************************/
  78. /*
  79. @doc INTERNAL MCIWAVE
  80. @func int | abs |
  81. This macro returns the absolute value of the signed integer passed to
  82. it.
  83. @parm int | x |
  84. Contains the integer whose absolute value is to be returned.
  85. @rdesc Returns the absolute value of the signed parameter passed.
  86. */
  87. #define abs(x) ((x) > 0 ? (x) : -(x))
  88. /************************************************************************/
  89. /*
  90. @doc INTERNAL MCIWAVE
  91. @func DWORD | mwFindThisFreeDataNode |
  92. Attempts to locate a free wave data node whose temporary data points to
  93. <p>dDataStart<d>. This allows data from one node to be expanded to
  94. adjacent free data of another node. Note that this depends upon any
  95. free data nodes that previously pointed to original data to have their
  96. total length zeroed when freed.
  97. @parm <t>PWAVEDESC<d> | pwd |
  98. Pointer to the wave device descriptor.
  99. @parm DWORD | dDataStart |
  100. Indicates the data start position to match.
  101. @rdesc Returns the free data node with adjacent free temporary data, else -1
  102. if there is none.
  103. */
  104. PRIVATE DWORD PASCAL NEAR mwFindThisFreeDataNode(
  105. PWAVEDESC pwd,
  106. DWORD dDataStart)
  107. {
  108. LPWAVEDATANODE lpwdn;
  109. DWORD dBlockNode;
  110. for (lpwdn = LPWDN(pwd, 0), dBlockNode = 0; dBlockNode < pwd->dWaveDataNodes; lpwdn++, dBlockNode++)
  111. if (ISFREEBLOCKNODE(lpwdn) && lpwdn->dTotalLength && (UNMASKDATASTART(lpwdn) == dDataStart))
  112. return dBlockNode;
  113. return (DWORD)-1;
  114. }
  115. /************************************************************************/
  116. /*
  117. @doc INTERNAL MCIWAVE
  118. @func DWORD | mwFindAnyFreeBlockNode |
  119. Locates a free node with no data attached. If there is none, it forces
  120. more to be allocated.
  121. @parm <t>PWAVEDESC<d> | pwd |
  122. Pointer to the wave device descriptor.
  123. @rdesc Returns a node with no data attached, else -1 if no memory is available.
  124. The node returned is marked as a free node, and need not be discarded if
  125. not used.
  126. */
  127. PRIVATE DWORD PASCAL NEAR mwFindAnyFreeBlockNode(
  128. PWAVEDESC pwd)
  129. {
  130. LPWAVEDATANODE lpwdn;
  131. DWORD dCurBlockNode;
  132. for (lpwdn = LPWDN(pwd, 0), dCurBlockNode = 0; dCurBlockNode < pwd->dWaveDataNodes; lpwdn++, dCurBlockNode++)
  133. if (ISFREEBLOCKNODE(lpwdn) && !lpwdn->dTotalLength)
  134. return dCurBlockNode;
  135. return mwAllocMoreBlockNodes(pwd);
  136. }
  137. /************************************************************************/
  138. /*
  139. @doc INTERNAL MCIWAVE
  140. @func BOOL | CopyBlockData |
  141. Copies <p>wLength<d> bytes of data pointed to by the <p>lpwdnSrc<d>
  142. node to the data pointed to by the <p>lpwdnDst<d> node, starting at
  143. <p>dSrc<d> to <p>dDst<d>.
  144. @parm <t>PWAVEDESC<d> | pwd |
  145. Pointer to the wave device descriptor.
  146. @parm <t>LPWAVEDATANODE<d> | lpwdnSrc |
  147. Points to the source node.
  148. @parm <t>LPWAVEDATANODE<d> | lpwdnDst |
  149. Points to the destination node.
  150. @parm DWORD | dSrc |
  151. Indicates the starting offset at which the data is located.
  152. @parm DWORD | dDst |
  153. Indicates the starting offset at which to place the data.
  154. @parm DWORD | dLength |
  155. Indicates the number of bytes of data to move.
  156. @rdesc Returns TRUE if the data was copied, else FALSE if no memory is
  157. available, or if a read or write error occured. If an error occurs,
  158. the task error state is set.
  159. @comm Note that this function will not compile with C 6.00A -Ox.
  160. */
  161. PRIVATE BOOL PASCAL NEAR CopyBlockData(
  162. PWAVEDESC pwd,
  163. LPWAVEDATANODE lpwdnSrc,
  164. LPWAVEDATANODE lpwdnDst,
  165. DWORD dSrc,
  166. DWORD dDst,
  167. DWORD dLength)
  168. {
  169. LPBYTE lpbBuffer;
  170. UINT wError;
  171. if (0 != (lpbBuffer = GlobalAlloc(GMEM_FIXED, dLength))) {
  172. if (!MySeekFile(pwd->hTempBuffers, UNMASKDATASTART(lpwdnSrc) + dSrc) ||
  173. !MyReadFile(pwd->hTempBuffers, lpbBuffer, dLength, NULL) ||
  174. !MySeekFile(pwd->hTempBuffers, UNMASKDATASTART(lpwdnDst) + dDst))
  175. {
  176. wError = MCIERR_FILE_READ;
  177. } else {
  178. if (MyWriteFile(pwd->hTempBuffers, lpbBuffer, dLength, NULL))
  179. {
  180. wError = 0;
  181. } else {
  182. wError = MCIERR_FILE_WRITE;
  183. }
  184. }
  185. GlobalFree(lpbBuffer);
  186. } else
  187. wError = MCIERR_OUT_OF_MEMORY;
  188. if (wError) {
  189. pwd->wTaskError = wError;
  190. return FALSE;
  191. }
  192. return TRUE;
  193. }
  194. /************************************************************************/
  195. /*
  196. @doc INTERNAL MCIWAVE
  197. @func DWORD | mwSplitCurrentDataNode |
  198. Splits the current node at the current position, creating a new node
  199. to contain the rest of the data, and possibly creating a second node
  200. to hold data not aligned on a block boundary, in the case of temporary
  201. data. The new node returned will have free temporary data space
  202. attached that is at least <p>wMinDataLength<d> bytes in length.
  203. If the split point is at the start of the current node, then the new
  204. node is just inserted in front of the current node.
  205. If the split point is at the end of the data of the current node, then
  206. the new node is just inserted after the current node.
  207. Else the current node must actually be split. This means that a new
  208. block to point to the data after the split point is created. If the
  209. current node points to temporary data and the split point is not block
  210. aligned, then any extra data needs to be copied over to the new node
  211. that is being inserted. This is because all starting points for
  212. temporary data are block aligned. If this is not temporary data,
  213. then the starting and ending points can just be adjusted to the exact
  214. split point, instead of having to be block aligned.
  215. @parm <t>PWAVEDESC<d> | pwd |
  216. Pointer to the wave device descriptor.
  217. @parm DWORD | dMinDataLength |
  218. Indicates the minimum size of temporary data space that is to be
  219. available to the new data node returned.
  220. @rdesc Returns the new node after the split, which is linked to the point
  221. after the current position in the current node. This node becomes the
  222. current node. Returns -1 if no memory was available, or a file error
  223. occurred, in which case the task error code is set.
  224. */
  225. PRIVATE DWORD PASCAL NEAR mwSplitCurrentDataNode(
  226. PWAVEDESC pwd,
  227. DWORD dMinDataLength)
  228. {
  229. LPWAVEDATANODE lpwdn;
  230. LPWAVEDATANODE lpwdnNew;
  231. DWORD dNewDataNode;
  232. DWORD dSplitAtData;
  233. BOOL fTempData;
  234. dSplitAtData = pwd->dCur - pwd->dVirtualWaveDataStart;
  235. lpwdn = LPWDN(pwd, pwd->dWaveDataCurrentNode);
  236. fTempData = ISTEMPDATA(lpwdn);
  237. if (fTempData)
  238. dMinDataLength += pwd->dAudioBufferLen;
  239. dNewDataNode = mwFindAnyFreeDataNode(pwd, dMinDataLength);
  240. if (dNewDataNode == -1)
  241. return (DWORD)-1;
  242. lpwdnNew = LPWDN(pwd, dNewDataNode);
  243. if (!dSplitAtData) {
  244. if (pwd->dWaveDataCurrentNode == pwd->dWaveDataStartNode)
  245. pwd->dWaveDataStartNode = dNewDataNode;
  246. else {
  247. LPWAVEDATANODE lpwdnCur;
  248. for (lpwdnCur = LPWDN(pwd, pwd->dWaveDataStartNode); lpwdnCur->dNextWaveDataNode != pwd->dWaveDataCurrentNode; lpwdnCur = LPWDN(pwd, lpwdnCur->dNextWaveDataNode))
  249. ;
  250. lpwdnCur->dNextWaveDataNode = dNewDataNode;
  251. }
  252. lpwdnNew->dNextWaveDataNode = pwd->dWaveDataCurrentNode;
  253. } else if (dSplitAtData == lpwdn->dDataLength) {
  254. lpwdnNew->dNextWaveDataNode = lpwdn->dNextWaveDataNode;
  255. lpwdn->dNextWaveDataNode = dNewDataNode;
  256. pwd->dVirtualWaveDataStart += lpwdn->dDataLength;
  257. } else {
  258. DWORD dEndBlockNode;
  259. LPWAVEDATANODE lpwdnEnd;
  260. DWORD dSplitPoint;
  261. if ((dEndBlockNode = mwFindAnyFreeBlockNode(pwd)) == -1) {
  262. RELEASEBLOCKNODE(lpwdnNew);
  263. return (DWORD)-1;
  264. }
  265. lpwdnEnd = LPWDN(pwd, dEndBlockNode);
  266. if (fTempData) {
  267. dSplitPoint = ROUNDDATA(pwd, dSplitAtData);
  268. if (dSplitPoint != dSplitAtData) {
  269. if (!CopyBlockData(pwd, lpwdn, lpwdnNew, dSplitAtData, 0, dSplitPoint - dSplitAtData)) {
  270. RELEASEBLOCKNODE(lpwdnNew);
  271. return (DWORD)-1;
  272. }
  273. lpwdnNew->dDataLength = dSplitPoint - dSplitAtData;
  274. }
  275. } else
  276. dSplitPoint = dSplitAtData;
  277. lpwdnEnd->dNextWaveDataNode = lpwdn->dNextWaveDataNode;
  278. lpwdnEnd->dDataStart = lpwdn->dDataStart + dSplitPoint;
  279. lpwdnEnd->dDataLength = lpwdn->dDataLength - dSplitPoint;
  280. lpwdnEnd->dTotalLength = lpwdn->dTotalLength - dSplitPoint;
  281. lpwdnNew->dNextWaveDataNode = dEndBlockNode;
  282. lpwdn->dDataLength = dSplitAtData;
  283. lpwdn->dTotalLength = dSplitPoint;
  284. lpwdn->dNextWaveDataNode = dNewDataNode;
  285. pwd->dVirtualWaveDataStart += lpwdn->dDataLength;
  286. }
  287. pwd->dWaveDataCurrentNode = dNewDataNode;
  288. return dNewDataNode;
  289. }
  290. /************************************************************************/
  291. /*
  292. @doc INTERNAL MCIWAVE
  293. @func DWORD | GatherAdjacentFreeDataNodes |
  294. This function is used to attempt to consolidate adjacent temporary
  295. data pointed to by free nodes so that a write can place data into
  296. a single node. This is done by repeatedly requesting any free data
  297. node whose data points to the end of the node's data passed.
  298. @parm <t>PWAVEDESC<d> | pwd |
  299. Pointer to the wave device descriptor.
  300. @parm <t>LPWAVEDATANODE<d> | lpwdn |
  301. Points to the node which is to collect adjacent temporary data.
  302. @parm DWORD | dStartPoint |
  303. Indicates the starting point to use when calculating the amount of
  304. data retrieved. This is just subtracted from the total length of
  305. the data attached to the node.
  306. @parm DWORD | dBufferLength |
  307. Indicates the amount of data to retrieve.
  308. @rdesc Returns the amount of data actually retrieved, adjusted by
  309. <d>dStartPoint<d>.
  310. */
  311. PRIVATE DWORD PASCAL NEAR GatherAdjacentFreeDataNodes(
  312. PWAVEDESC pwd,
  313. LPWAVEDATANODE lpwdn,
  314. DWORD dStartPoint,
  315. DWORD dBufferLength)
  316. {
  317. for (; lpwdn->dTotalLength - dStartPoint < dBufferLength;) {
  318. DWORD dFreeDataNode;
  319. LPWAVEDATANODE lpwdnFree;
  320. dFreeDataNode = mwFindThisFreeDataNode(pwd, UNMASKDATASTART(lpwdn) + lpwdn->dTotalLength);
  321. if (dFreeDataNode == -1)
  322. break;
  323. lpwdnFree = LPWDN(pwd, dFreeDataNode);
  324. lpwdn->dTotalLength += lpwdnFree->dTotalLength;
  325. lpwdnFree->dTotalLength = 0;
  326. }
  327. return min(dBufferLength, lpwdn->dTotalLength - dStartPoint);
  328. }
  329. /************************************************************************/
  330. /*
  331. @doc INTERNAL MCIWAVE
  332. @func <t>LPWAVEDATANODE<d> | NextDataNode |
  333. Locates a free data node with the specified amount of data, and inserts
  334. it after the current node, setting the current node to be this new
  335. node.
  336. @parm <t>PWAVEDESC<d> | pwd |
  337. Pointer to the wave device descriptor.
  338. @parm DWORD | dBufferLength |
  339. Indicates the minimum amount of data that is to be available to the
  340. new node inserted.
  341. @rdesc Returns the newly inserted node, else NULL on error, in which case the
  342. task error code is set.
  343. */
  344. PRIVATE LPWAVEDATANODE PASCAL NEAR NextDataNode(
  345. PWAVEDESC pwd,
  346. DWORD dBufferLength)
  347. {
  348. DWORD dWaveDataNew;
  349. LPWAVEDATANODE lpwdn;
  350. LPWAVEDATANODE lpwdnNew;
  351. if ((dWaveDataNew = mwFindAnyFreeDataNode(pwd, dBufferLength)) == -1)
  352. return NULL;
  353. lpwdn = LPWDN(pwd, pwd->dWaveDataCurrentNode);
  354. lpwdnNew = LPWDN(pwd, dWaveDataNew);
  355. lpwdnNew->dNextWaveDataNode = lpwdn->dNextWaveDataNode;
  356. lpwdn->dNextWaveDataNode = dWaveDataNew;
  357. pwd->dWaveDataCurrentNode = dWaveDataNew;
  358. pwd->dVirtualWaveDataStart += lpwdn->dDataLength;
  359. return lpwdnNew;
  360. }
  361. /************************************************************************/
  362. /*
  363. @doc INTERNAL MCIWAVE
  364. @func BOOL | AdjustLastTempData |
  365. This function makes two passes through the nodes that are affected by
  366. an overwrite record. These are nodes that are either no longer needed,
  367. or whose starting point needs to be adjusted. The two passes allow
  368. any data to be successfully copied before removing any unneeded nodes.
  369. This creates a more graceful exit to any failure.
  370. The first pass locates the last node affected. If that node points to
  371. temporary data, and the end of the overwrite does not fall on a block
  372. aligned boundary, then any extra data must be copied to a block aligned
  373. boundary. This means that a new node might need to be created if the
  374. amount of data to be copied is greater than one block's worth. If the
  375. end of overwrite happens to fall on a block boundary, then no copying
  376. need be done. In either case the data start point is adjusted to
  377. compensate for the data logically overwritten in this node, and the
  378. total overwrite length is adjusted so that this node is not checked on
  379. the second pass.
  380. The second pass just frees nodes that become empty, and removes them
  381. from the linked list of in-use nodes. When the last node affected is
  382. encountered, either it will point to temporary data, in which case be
  383. already adjusted, or point to original data, which must be adjusted.
  384. @parm <t>PWAVEDESC<d> | pwd |
  385. Pointer to the wave device descriptor.
  386. @parm <t>LPWAVEDATANODE<d> | lpwdn |
  387. Points to the node which is being adjusted for. It contains the new
  388. data.
  389. @parm DWORD | dStartPoint |
  390. Contains the starting point at which data was overwritten.
  391. @parm DWORD | dWriteSize |
  392. Contains the amount of data overwritten.
  393. @rdesc Returns TRUE if the nothing needed to be adjusted, or the last node
  394. in the overwrite pointed to temporary data, and it was moved correctly,
  395. else FALSE if no memory was available, or a file error occurred. In
  396. that case the task error code is set.
  397. */
  398. PRIVATE BOOL PASCAL NEAR AdjustLastTempData(
  399. PWAVEDESC pwd,
  400. LPWAVEDATANODE lpwdn,
  401. DWORD dStartPoint,
  402. DWORD dWriteSize)
  403. {
  404. LPWAVEDATANODE lpwdnCur;
  405. DWORD dLength;
  406. if ((lpwdn->dDataLength - dStartPoint >= dWriteSize) || (lpwdn->dNextWaveDataNode == ENDOFNODES))
  407. return TRUE;
  408. dWriteSize -= (lpwdn->dDataLength - dStartPoint);
  409. for (dLength = dWriteSize, lpwdnCur = lpwdn;;) {
  410. LPWAVEDATANODE lpwdnNext;
  411. lpwdnNext = LPWDN(pwd, lpwdnCur->dNextWaveDataNode);
  412. if (lpwdnNext->dDataLength >= dLength) {
  413. DWORD dNewBlockNode;
  414. DWORD dMoveData;
  415. if (!ISTEMPDATA(lpwdnNext) || (lpwdnNext->dDataLength == dLength))
  416. break;
  417. if (lpwdnNext->dDataLength - dLength > ROUNDDATA(pwd, 1)) {
  418. if ((dNewBlockNode = mwFindAnyFreeBlockNode(pwd)) == -1)
  419. return FALSE;
  420. } else
  421. dNewBlockNode = (DWORD)-1;
  422. dMoveData = min(ROUNDDATA(pwd, dLength), lpwdnNext->dDataLength) - dLength;
  423. if (dMoveData && !CopyBlockData(pwd, lpwdnNext, lpwdnNext, dLength, 0, dMoveData))
  424. return FALSE;
  425. if (dNewBlockNode != -1) {
  426. lpwdnCur = LPWDN(pwd, dNewBlockNode);
  427. lpwdnCur->dDataStart = lpwdnNext->dDataStart + dLength + dMoveData;
  428. lpwdnCur->dDataLength = lpwdnNext->dDataLength - (dLength + dMoveData);
  429. lpwdnCur->dTotalLength = lpwdnNext->dTotalLength - (dLength + dMoveData);
  430. lpwdnCur->dNextWaveDataNode = lpwdnNext->dNextWaveDataNode;
  431. lpwdnNext->dNextWaveDataNode = dNewBlockNode;
  432. lpwdnNext->dTotalLength = dLength + dMoveData;
  433. }
  434. lpwdnNext->dDataLength = dMoveData;
  435. dWriteSize -= dLength;
  436. break;
  437. } else if ((!ISTEMPDATA(lpwdnNext)) && (lpwdnNext->dNextWaveDataNode == ENDOFNODES))
  438. break;
  439. dLength -= lpwdnNext->dDataLength;
  440. lpwdnCur = lpwdnNext;
  441. }
  442. for (;;) {
  443. LPWAVEDATANODE lpwdnNext;
  444. lpwdnNext = LPWDN(pwd, lpwdn->dNextWaveDataNode);
  445. if (lpwdnNext->dDataLength > dWriteSize) {
  446. if (dWriteSize) {
  447. lpwdnNext->dDataStart += dWriteSize;
  448. lpwdnNext->dDataLength -= dWriteSize;
  449. lpwdnNext->dTotalLength -= dWriteSize;
  450. }
  451. return TRUE;
  452. }
  453. dWriteSize -= lpwdnNext->dDataLength;
  454. lpwdn->dNextWaveDataNode = lpwdnNext->dNextWaveDataNode;
  455. if (!ISTEMPDATA(lpwdnNext))
  456. lpwdnNext->dTotalLength = 0;
  457. RELEASEBLOCKNODE(lpwdnNext);
  458. if (lpwdn->dNextWaveDataNode == ENDOFNODES)
  459. return TRUE;
  460. }
  461. }
  462. /************************************************************************/
  463. /*
  464. @doc INTERNAL MCIWAVE
  465. @func BOOL | mwOverWrite |
  466. This function overwrites data in the wave file from the specified wave
  467. buffer. The position is taken from the <e>WAVEDESC.dCur<d> pointer,
  468. which is updated with the number of bytes actually overwritten.
  469. @parm <t>PWAVEDESC<d> | pwd |
  470. Pointer to the wave device descriptor.
  471. @parm LPBYTE | lpbBuffer |
  472. Points to a buffer to containing the data written.
  473. @parm DWORD | dBufferLength |
  474. Indicates the byte length of the buffer.
  475. @rdesc Returns TRUE if overwrite succeeded, else FALSE on an error.
  476. */
  477. PRIVATE BOOL PASCAL NEAR mwOverWrite(
  478. PWAVEDESC pwd,
  479. LPBYTE lpbBuffer,
  480. DWORD dBufferLength)
  481. {
  482. LPWAVEDATANODE lpwdn;
  483. lpwdn = LPWDN(pwd, pwd->dWaveDataCurrentNode);
  484. for (; dBufferLength;)
  485. if (ISTEMPDATA(lpwdn)) {
  486. DWORD dStartPoint;
  487. DWORD dRemainingSpace;
  488. DWORD dMaxWrite;
  489. dStartPoint = pwd->dCur - pwd->dVirtualWaveDataStart;
  490. dRemainingSpace = min(dBufferLength, lpwdn->dTotalLength - dStartPoint);
  491. if (dRemainingSpace == dBufferLength)
  492. dMaxWrite = dBufferLength;
  493. else if (UNMASKDATASTART(lpwdn) + lpwdn->dTotalLength == pwd->dWaveTempDataLength) {
  494. dMaxWrite = dBufferLength;
  495. lpwdn->dTotalLength += ROUNDDATA(pwd, dBufferLength - dRemainingSpace);
  496. pwd->dWaveTempDataLength += ROUNDDATA(pwd, dBufferLength - dRemainingSpace);
  497. } else
  498. dMaxWrite = GatherAdjacentFreeDataNodes(pwd, lpwdn, dStartPoint, dBufferLength);
  499. if (dMaxWrite) {
  500. DWORD dWriteSize;
  501. if (!MySeekFile(pwd->hTempBuffers, UNMASKDATASTART(lpwdn) + dStartPoint)) {
  502. pwd->wTaskError = MCIERR_FILE_WRITE;
  503. break;
  504. }
  505. if (MyWriteFile(pwd->hTempBuffers, lpbBuffer, dMaxWrite, &dWriteSize)) {
  506. if (!AdjustLastTempData(pwd, lpwdn, dStartPoint, dWriteSize))
  507. break;
  508. if (lpwdn->dDataLength < dStartPoint + dWriteSize)
  509. lpwdn->dDataLength = dStartPoint + dWriteSize;
  510. lpbBuffer += dWriteSize;
  511. dBufferLength -= dWriteSize;
  512. pwd->dCur += dWriteSize;
  513. if (pwd->dVirtualWaveDataStart + lpwdn->dDataLength > pwd->dSize)
  514. pwd->dSize = pwd->dVirtualWaveDataStart + lpwdn->dDataLength;
  515. }
  516. if (dWriteSize != dMaxWrite) {
  517. pwd->wTaskError = MCIERR_FILE_WRITE;
  518. break;
  519. }
  520. }
  521. if (dBufferLength && !(lpwdn = NextDataNode(pwd, dBufferLength)))
  522. break;
  523. } else {
  524. DWORD dWaveDataNew;
  525. if ((dWaveDataNew = mwSplitCurrentDataNode(pwd, dBufferLength)) != -1)
  526. lpwdn = LPWDN(pwd, dWaveDataNew);
  527. else
  528. break;
  529. }
  530. return !dBufferLength;
  531. }
  532. /************************************************************************/
  533. /*
  534. @doc INTERNAL MCIWAVE
  535. @func BOOL | mwInsert |
  536. This function inserts data to the wave file from the specified wave
  537. buffer. The position is taken from the <e>WAVEDESC.dCur<d> pointer,
  538. which is updated with the number of bytes actually written.
  539. @parm <t>PWAVEDESC<d> | pwd |
  540. Pointer to the wave device descriptor.
  541. @parm LPBYTE | lpbBuffer |
  542. Points to a buffer to containing the data written.
  543. @parm DWORD | dBufferLength |
  544. Indicates the byte length of the buffer.
  545. @rdesc Returns TRUE if insert succeeded, else FALSE on an error.
  546. */
  547. PRIVATE BOOL PASCAL NEAR mwInsert(
  548. PWAVEDESC pwd,
  549. LPBYTE lpbBuffer,
  550. DWORD dBufferLength)
  551. {
  552. LPWAVEDATANODE lpwdn;
  553. lpwdn = LPWDN(pwd, pwd->dWaveDataCurrentNode);
  554. for (; dBufferLength;)
  555. if (ISTEMPDATA(lpwdn) && (pwd->dCur == pwd->dVirtualWaveDataStart + lpwdn->dDataLength)) {
  556. DWORD dStartPoint;
  557. DWORD dRemainingSpace;
  558. DWORD dMaxInsert;
  559. dStartPoint = pwd->dCur - pwd->dVirtualWaveDataStart;
  560. dRemainingSpace = min(dBufferLength, lpwdn->dTotalLength - lpwdn->dDataLength);
  561. if (dRemainingSpace == dBufferLength)
  562. dMaxInsert = dBufferLength;
  563. else if (UNMASKDATASTART(lpwdn) + lpwdn->dTotalLength == pwd->dWaveTempDataLength) {
  564. dMaxInsert = dBufferLength;
  565. lpwdn->dTotalLength += ROUNDDATA(pwd, dBufferLength - dRemainingSpace);
  566. pwd->dWaveTempDataLength += ROUNDDATA(pwd, dBufferLength - dRemainingSpace);
  567. } else
  568. dMaxInsert = GatherAdjacentFreeDataNodes(pwd, lpwdn, dStartPoint, dBufferLength);
  569. if (dMaxInsert) {
  570. DWORD dWriteSize;
  571. if (!MySeekFile(pwd->hTempBuffers, UNMASKDATASTART(lpwdn) + dStartPoint)) {
  572. pwd->wTaskError = MCIERR_FILE_WRITE;
  573. break;
  574. }
  575. if (MyWriteFile(pwd->hTempBuffers, lpbBuffer, dMaxInsert, &dWriteSize)) {
  576. lpwdn->dDataLength += dWriteSize;
  577. lpbBuffer += dWriteSize;
  578. dBufferLength -= dWriteSize;
  579. pwd->dCur += dWriteSize;
  580. pwd->dSize += dWriteSize;
  581. }
  582. if (dWriteSize != dMaxInsert) {
  583. pwd->wTaskError = MCIERR_FILE_WRITE;
  584. break;
  585. }
  586. }
  587. if (dBufferLength && !(lpwdn = NextDataNode(pwd, dBufferLength)))
  588. break;
  589. } else {
  590. DWORD dWaveDataNew;
  591. if ((dWaveDataNew = mwSplitCurrentDataNode(pwd, dBufferLength)) != -1)
  592. lpwdn = LPWDN(pwd, dWaveDataNew);
  593. else
  594. break;
  595. }
  596. return !dBufferLength;
  597. }
  598. /************************************************************************/
  599. /*
  600. @doc INTERNAL MCIWAVE
  601. @func DWORD | mwGetLevel |
  602. This function finds the highest level in the specified wave sample.
  603. Note that the function assumes that in some cases the sample size
  604. is evenly divisable by 4.
  605. @parm <t>PWAVEDESC<d> | pwd |
  606. Pointer to the wave device descriptor.
  607. @parm LPBYTE | lpbBuffer |
  608. Points to a buffer containing the sample whose highest level is to be
  609. returned.
  610. @parm int | cbBufferLength |
  611. Indicates the byte length of the sample buffer.
  612. @rdesc Returns the highest level encountered in the sample for PCM data only.
  613. If the device has been opened with one channel, the level is contained
  614. in the low-order word. Else if the device has been opened with two
  615. channels, one channel is in the low-order word, and the other is in the
  616. high-order word.
  617. */
  618. PRIVATE DWORD PASCAL NEAR mwGetLevel(
  619. PWAVEDESC pwd,
  620. LPBYTE lpbBuffer,
  621. register int cbBufferLength)
  622. {
  623. if (pwd->pwavefmt->wFormatTag != WAVE_FORMAT_PCM)
  624. return 0;
  625. else if (pwd->pwavefmt->nChannels == 1) {
  626. int iMonoLevel;
  627. iMonoLevel = 0;
  628. if (((NPPCMWAVEFORMAT)(pwd->pwavefmt))->wBitsPerSample == 8)
  629. for (; cbBufferLength--; lpbBuffer++)
  630. iMonoLevel = max(*lpbBuffer > 128 ? *lpbBuffer - 128 : 128 - *lpbBuffer, iMonoLevel);
  631. else if (((NPPCMWAVEFORMAT)(pwd->pwavefmt))->wBitsPerSample == 16)
  632. for (; cbBufferLength; lpbBuffer += sizeof(SHORT)) {
  633. iMonoLevel = max(abs(*(PSHORT)lpbBuffer), iMonoLevel);
  634. cbBufferLength -= sizeof(SHORT);
  635. }
  636. else
  637. return 0;
  638. return (DWORD)iMonoLevel;
  639. } else if (pwd->pwavefmt->nChannels == 2) {
  640. int iLeftLevel;
  641. int iRightLevel;
  642. iLeftLevel = 0;
  643. iRightLevel = 0;
  644. if (((NPPCMWAVEFORMAT)(pwd->pwavefmt))->wBitsPerSample == 8)
  645. for (; cbBufferLength;) {
  646. iLeftLevel = max(*lpbBuffer > 128 ? *lpbBuffer - 128 : 128 - *lpbBuffer, iLeftLevel);
  647. lpbBuffer++;
  648. iRightLevel = max(*lpbBuffer > 128 ? *lpbBuffer - 128 : 128 - *lpbBuffer, iRightLevel);
  649. lpbBuffer++;
  650. cbBufferLength -= 2;
  651. }
  652. else if (((NPPCMWAVEFORMAT)(pwd->pwavefmt))->wBitsPerSample == 16)
  653. for (; cbBufferLength;) {
  654. iLeftLevel = max(abs(*(PSHORT)lpbBuffer), iLeftLevel);
  655. lpbBuffer += sizeof(SHORT);
  656. iRightLevel = max(abs(*(PSHORT)lpbBuffer), iRightLevel);
  657. lpbBuffer += sizeof(SHORT);
  658. cbBufferLength -= 2 * sizeof(SHORT);
  659. }
  660. else
  661. return 0;
  662. return MAKELONG(iLeftLevel, iRightLevel);
  663. }
  664. return 0;
  665. }
  666. /************************************************************************/
  667. /*
  668. @doc INTERNAL MCIWAVE
  669. @func BOOL | CheckNewCommand |
  670. This function is called when a New command flag is found during the
  671. record loop. It determines if the new commands affect current
  672. recording enough that it must be terminated. This can happen if a
  673. Stop command is received.
  674. Any other record change does not need to stop current recording, as
  675. they should just release all the buffers from the wave device before
  676. setting the command.
  677. @parm <t>PWAVEDESC<d> | pwd |
  678. Pointer to the wave device descriptor.
  679. @rdesc Returns TRUE if the new commands do not affect recording and it should
  680. continue, else FALSE if the new commands affect the recording, and it
  681. should be aborted.
  682. */
  683. REALLYPRIVATE BOOL PASCAL NEAR CheckNewCommand(
  684. PWAVEDESC pwd)
  685. {
  686. if (ISMODE(pwd, COMMAND_STOP))
  687. return FALSE;
  688. if (ISMODE(pwd, COMMAND_INSERT))
  689. ADDMODE(pwd, MODE_INSERT);
  690. else
  691. ADDMODE(pwd, MODE_OVERWRITE);
  692. REMOVEMODE(pwd, COMMAND_NEW);
  693. return TRUE;
  694. }
  695. /************************************************************************/
  696. /*
  697. @doc INTERNAL MCIWAVE
  698. @func <t>HMMIO<d> | CreateSaveFile |
  699. This function creates the file to which the current data is to be
  700. saved to in RIFF format. This is either a temporary file created on
  701. the same logical disk as the original file (so that this file can
  702. replace the original file), else a new file.
  703. The RIFF header and wave header chunks are written to the new file,
  704. and the file position is at the start of the data to be copied. Note
  705. that all the RIFF chunk headers will contain correct lengths, so there
  706. is no need to ascend out of the data chunk when complete.
  707. @parm <t>PWAVEDESC<d> | pwd |
  708. Pointer to the wave device descriptor.
  709. @parm SSZ | sszTempSaveFile |
  710. Points to a buffer to contain the name of the temporary file created,
  711. if any. This is zero length if a new file is to be created instead of
  712. a temporary file that would replace the original file.
  713. @rdesc Returns the handle to the save file, else NULL if a create error or
  714. write error occurred.
  715. @comm Note that this needs to be fixed so that non-DOS IO systems can save
  716. the file to the original name by creating a temporary file name through
  717. MMIO.
  718. */
  719. PRIVATE HMMIO PASCAL NEAR CreateSaveFile(
  720. PWAVEDESC pwd,
  721. LPWSTR sszTempSaveFile)
  722. {
  723. MMIOINFO mmioInfo;
  724. HMMIO hmmio;
  725. LPWSTR lszFile;
  726. InitMMIOOpen(pwd, &mmioInfo);
  727. if (pwd->szSaveFile) {
  728. *sszTempSaveFile = (char)0;
  729. lszFile = pwd->szSaveFile;
  730. } else {
  731. lstrcpy(sszTempSaveFile, pwd->aszFile);
  732. if (!mmioOpen(sszTempSaveFile, &mmioInfo, MMIO_GETTEMP)) {
  733. pwd->wTaskError = MCIERR_FILE_WRITE;
  734. return NULL;
  735. }
  736. lszFile = sszTempSaveFile;
  737. }
  738. if (0 != (hmmio = mmioOpen(lszFile, &mmioInfo, MMIO_CREATE | MMIO_READWRITE | MMIO_DENYWRITE))) {
  739. MMCKINFO mmck;
  740. mmck.cksize = sizeof(FOURCC) + sizeof(FOURCC) + sizeof(DWORD) + pwd->wFormatSize + sizeof(FOURCC) + sizeof(DWORD) + pwd->dSize;
  741. if (pwd->wFormatSize & 1)
  742. mmck.cksize++;
  743. mmck.fccType = mmioWAVE;
  744. if (!mmioCreateChunk(hmmio, &mmck, MMIO_CREATERIFF)) {
  745. mmck.cksize = pwd->wFormatSize;
  746. mmck.ckid = mmioFMT;
  747. if (!mmioCreateChunk(hmmio, &mmck, 0) && (mmioWrite(hmmio, (LPSTR)pwd->pwavefmt, (LONG)pwd->wFormatSize) == (LONG)pwd->wFormatSize) && !mmioAscend(hmmio, &mmck, 0)) {
  748. mmck.cksize = pwd->dSize;
  749. mmck.ckid = mmioDATA;
  750. if (!mmioCreateChunk(hmmio, &mmck, 0))
  751. return hmmio;
  752. }
  753. }
  754. pwd->wTaskError = MCIERR_FILE_WRITE;
  755. mmioClose(hmmio, 0);
  756. } else
  757. pwd->wTaskError = MCIERR_FILE_NOT_SAVED;
  758. return NULL;
  759. }
  760. /************************************************************************/
  761. /*
  762. @doc INTERNAL MCIWAVE
  763. @func VOID | mwSaveData |
  764. This function is used by the background task to save the data to a
  765. specified file. This has the effect of making all the temporary data
  766. now original data, and removing any temporary data file.
  767. @parm <t>PWAVEDESC<d> | pwd |
  768. Pointer to the wave device descriptor.
  769. @rdesc Nothing.
  770. */
  771. PUBLIC VOID PASCAL FAR mwSaveData(
  772. PWAVEDESC pwd)
  773. {
  774. LPBYTE lpbBuffer = NULL;
  775. HANDLE hMem;
  776. DWORD AllocSize = max(min(pwd->dAudioBufferLen, pwd->dSize),1);
  777. // If there is no wave data, we still allocate 1 byte in order to save a NULL
  778. // file. Otherwise we have no choice but to return an error saying "Out of memory"
  779. hMem = GlobalAlloc(GMEM_MOVEABLE, AllocSize);
  780. if (hMem) {
  781. lpbBuffer = GlobalLock(hMem);
  782. dprintf3(("mwSaveData allocated %d bytes at %8x, handle %8x",
  783. AllocSize, lpbBuffer, hMem));
  784. dprintf3(("pwd->AudioBufferLen = %d, pwd->dSize = %d",
  785. pwd->dAudioBufferLen, pwd->dSize));
  786. }
  787. if (lpbBuffer) {
  788. WCHAR aszTempSaveFile[_MAX_PATH];
  789. HMMIO hmmioSave;
  790. if (0 != (hmmioSave = CreateSaveFile(pwd, (SSZ)aszTempSaveFile))) {
  791. LPWAVEDATANODE lpwdn;
  792. lpwdn = LPWDN(pwd, pwd->dWaveDataStartNode);
  793. for (;;) {
  794. DWORD dDataLength;
  795. BOOL fTempData;
  796. fTempData = ISTEMPDATA(lpwdn);
  797. if (fTempData)
  798. MySeekFile(pwd->hTempBuffers, UNMASKDATASTART(lpwdn));
  799. else
  800. mmioSeek(pwd->hmmio, pwd->dRiffData + lpwdn->dDataStart, SEEK_SET);
  801. for (dDataLength = lpwdn->dDataLength; dDataLength;) {
  802. DWORD dReadSize;
  803. dReadSize = min(pwd->dAudioBufferLen, dDataLength);
  804. if (dReadSize >= AllocSize) {
  805. dprintf(("READING TOO MUCH DATA!!"));
  806. }
  807. if (fTempData) {
  808. if (!MyReadFile(pwd->hTempBuffers, lpbBuffer, dReadSize, NULL)) {
  809. pwd->wTaskError = MCIERR_FILE_READ;
  810. break;
  811. }
  812. } else if ((DWORD)mmioRead(pwd->hmmio, lpbBuffer, (LONG)dReadSize) != dReadSize) {
  813. pwd->wTaskError = MCIERR_FILE_READ;
  814. break;
  815. }
  816. if ((DWORD)mmioWrite(hmmioSave, lpbBuffer, (LONG)dReadSize) != dReadSize) {
  817. pwd->wTaskError = MCIERR_FILE_WRITE;
  818. break;
  819. }
  820. dDataLength -= dReadSize;
  821. }
  822. if (pwd->wTaskError)
  823. break;
  824. if (lpwdn->dNextWaveDataNode == ENDOFNODES)
  825. break;
  826. lpwdn = LPWDN(pwd, lpwdn->dNextWaveDataNode);
  827. }
  828. mmioClose(hmmioSave, 0);
  829. if (!pwd->wTaskError) {
  830. MMIOINFO mmioInfo;
  831. MMCKINFO mmckRiff;
  832. MMCKINFO mmck;
  833. if (pwd->hmmio)
  834. mmioClose(pwd->hmmio, 0);
  835. InitMMIOOpen(pwd, &mmioInfo);
  836. if (pwd->szSaveFile)
  837. lstrcpy(pwd->aszFile, pwd->szSaveFile);
  838. else {
  839. if (!mmioOpen(pwd->aszFile, &mmioInfo, MMIO_DELETE))
  840. pwd->wTaskError = MCIERR_FILE_WRITE;
  841. if (!pwd->wTaskError)
  842. if (mmioRename(aszTempSaveFile, pwd->aszFile, &mmioInfo, 0)) {
  843. lstrcpy(pwd->aszFile, aszTempSaveFile);
  844. *aszTempSaveFile = (char)0;
  845. }
  846. }
  847. pwd->hmmio = mmioOpen(pwd->aszFile, &mmioInfo, MMIO_READ | MMIO_DENYWRITE);
  848. if (!pwd->wTaskError) {
  849. LPWAVEDATANODE lpwdn;
  850. mmckRiff.fccType = mmioWAVE;
  851. mmioDescend(pwd->hmmio, &mmckRiff, NULL, MMIO_FINDRIFF);
  852. mmck.ckid = mmioDATA;
  853. mmioDescend(pwd->hmmio, &mmck, &mmckRiff, MMIO_FINDCHUNK);
  854. pwd->dRiffData = mmck.dwDataOffset;
  855. if (pwd->hTempBuffers != INVALID_HANDLE_VALUE) {
  856. CloseHandle(pwd->hTempBuffers);
  857. pwd->dWaveTempDataLength = 0;
  858. DeleteFile( pwd->aszTempFile );
  859. pwd->hTempBuffers = INVALID_HANDLE_VALUE;
  860. }
  861. if (pwd->lpWaveDataNode) {
  862. GlobalFreePtr(pwd->lpWaveDataNode);
  863. pwd->lpWaveDataNode = NULL;
  864. pwd->dWaveDataNodes = 0;
  865. }
  866. pwd->dVirtualWaveDataStart = 0;
  867. pwd->dWaveDataCurrentNode = 0;
  868. pwd->dWaveDataStartNode = 0;
  869. mwAllocMoreBlockNodes(pwd);
  870. lpwdn = LPWDN(pwd, 0);
  871. lpwdn->dNextWaveDataNode = (DWORD)ENDOFNODES;
  872. lpwdn->dDataLength = pwd->dSize;
  873. lpwdn->dTotalLength = pwd->dSize;
  874. if (!pwd->szSaveFile && !*aszTempSaveFile)
  875. pwd->wTaskError = MCIERR_FILE_WRITE;
  876. }
  877. }
  878. }
  879. GlobalUnlock(hMem);
  880. } else {
  881. pwd->wTaskError = MCIERR_OUT_OF_MEMORY;
  882. }
  883. if (hMem) {
  884. GlobalFree(hMem);
  885. }
  886. }
  887. /************************************************************************/
  888. /*
  889. @doc INTERNAL MCIWAVE
  890. @func VOID | mwDeleteData |
  891. This function is used by the background task to delete data.
  892. @parm <t>PWAVEDESC<d> | pwd |
  893. Pointer to the wave device descriptor.
  894. @rdesc Nothing.
  895. */
  896. PUBLIC VOID PASCAL FAR mwDeleteData(
  897. PWAVEDESC pwd)
  898. {
  899. DWORD dTotalToDelete;
  900. LPWAVEDATANODE lpwdn;
  901. LPWAVEDATANODE lpwdnCur;
  902. DWORD dVirtualWaveDataStart;
  903. lpwdn = LPWDN(pwd, pwd->dWaveDataCurrentNode);
  904. dTotalToDelete = pwd->dTo - pwd->dFrom;
  905. if (dTotalToDelete == pwd->dSize) {
  906. // The whole wave chunk is to be deleted - nice and simple
  907. DWORD dNewDataNode;
  908. if ((dNewDataNode = mwFindAnyFreeDataNode(pwd, 1)) == -1) {
  909. dprintf2(("mwDeleteData - no free data node"));
  910. return;
  911. }
  912. RELEASEBLOCKNODE(LPWDN(pwd, dNewDataNode));
  913. }
  914. dprintf3(("mwDeleteData - size to delete = %d", dTotalToDelete));
  915. for (dVirtualWaveDataStart = pwd->dVirtualWaveDataStart; dTotalToDelete;) {
  916. DWORD dDeleteLength;
  917. dDeleteLength = min(dTotalToDelete, lpwdn->dDataLength - (pwd->dFrom - dVirtualWaveDataStart));
  918. dprintf4(("mwDelete dTotalToDelete = %d, dDeleteLength = %d", dTotalToDelete, dDeleteLength));
  919. if (!dDeleteLength) {
  920. // Nothing to be deleted from this block
  921. dprintf3(("mwDelete skipping to next block"));
  922. dVirtualWaveDataStart += lpwdn->dDataLength;
  923. lpwdn = LPWDN(pwd, lpwdn->dNextWaveDataNode);
  924. continue; // iterate around the for loop
  925. }
  926. // Note: the block above is new to NT. Windows 3.1 as shipped fails.
  927. // The problem can be seen with a wave file > 3 seconds long and
  928. // the following two commands:
  929. // delete wave from 1000 to 2000
  930. // delete wave from 1000 to 2000
  931. // Because of the fragmentation the second delete fails. It decided
  932. // that NO data can be deleted from the first block, but never
  933. // stepped on to the next block.
  934. if (ISTEMPDATA(lpwdn)) {
  935. dprintf3(("mwDeleteData - temporary data"));
  936. if (dVirtualWaveDataStart + lpwdn->dDataLength <= pwd->dFrom + dTotalToDelete)
  937. lpwdn->dDataLength -= dDeleteLength; // Delete data in this block
  938. else {
  939. DWORD dNewBlockNode;
  940. DWORD dDeleteStart;
  941. DWORD dEndSplitPoint;
  942. DWORD dMoveData;
  943. dDeleteStart = pwd->dFrom - dVirtualWaveDataStart;
  944. dEndSplitPoint = min(ROUNDDATA(pwd, dDeleteStart + dDeleteLength), lpwdn->dDataLength);
  945. if (dEndSplitPoint < lpwdn->dDataLength) {
  946. if ((dNewBlockNode = mwFindAnyFreeBlockNode(pwd)) == -1)
  947. break;
  948. } else
  949. dNewBlockNode = (DWORD)-1;
  950. dMoveData = dEndSplitPoint - (dDeleteStart + dDeleteLength);
  951. if (dMoveData && !CopyBlockData(pwd, lpwdn, lpwdn, dDeleteStart + dDeleteLength, dDeleteStart, dMoveData))
  952. break;
  953. if (dNewBlockNode != -1) {
  954. lpwdnCur = LPWDN(pwd, dNewBlockNode);
  955. lpwdnCur->dDataStart = lpwdn->dDataStart + dEndSplitPoint;
  956. lpwdnCur->dDataLength = lpwdn->dDataLength - dEndSplitPoint;
  957. lpwdnCur->dTotalLength = lpwdn->dTotalLength - dEndSplitPoint;
  958. lpwdnCur->dNextWaveDataNode = lpwdn->dNextWaveDataNode;
  959. lpwdn->dNextWaveDataNode = dNewBlockNode;
  960. lpwdn->dTotalLength = dEndSplitPoint;
  961. }
  962. lpwdn->dDataLength = dDeleteStart + dMoveData;
  963. }
  964. } else if (dVirtualWaveDataStart == pwd->dFrom) {
  965. // FROM point is the same as the virtual start point, hence we are
  966. // deleting from the beginning of this wave data block. We can
  967. // simply adjust the total length and start point.
  968. dprintf4(("mwDeleteData - From == Start, deleting from start of block"));
  969. lpwdn->dDataStart += dDeleteLength;
  970. lpwdn->dDataLength -= dDeleteLength;
  971. lpwdn->dTotalLength = lpwdn->dDataLength;
  972. } else if (dVirtualWaveDataStart + lpwdn->dDataLength <= pwd->dFrom + dTotalToDelete) {
  973. // FROM point plus amount to delete takes us to the end of the wave
  974. // data - meaning that the data block can be truncated. We can
  975. // simply adjust the total length.
  976. dprintf4(("mwDeleteData - delete to end of block"));
  977. lpwdn->dDataLength -= dDeleteLength;
  978. lpwdn->dTotalLength = lpwdn->dDataLength;
  979. } else {
  980. // We have to delete a chunk out of the middle.
  981. DWORD dNewBlockNode;
  982. DWORD dDeleteStart;
  983. // The existing single block will now be covered by two blocks
  984. // Find a new node, then set the current node start->deletefrom
  985. // and the new node deletefrom+deletelength for the remaining
  986. // length of this node. It all hinges on finding a free node...
  987. if ((dNewBlockNode = mwFindAnyFreeBlockNode(pwd)) == -1) {
  988. dprintf2(("mwDeleteData - cannot find free node"));
  989. break;
  990. }
  991. dDeleteStart = pwd->dFrom - dVirtualWaveDataStart;
  992. lpwdnCur = LPWDN(pwd, dNewBlockNode);
  993. lpwdnCur->dDataStart = dVirtualWaveDataStart + dDeleteStart + dDeleteLength;
  994. lpwdnCur->dDataLength = lpwdn->dDataLength - (dDeleteStart + dDeleteLength);
  995. lpwdnCur->dTotalLength = lpwdnCur->dDataLength;
  996. lpwdnCur->dNextWaveDataNode = lpwdn->dNextWaveDataNode;
  997. lpwdn->dDataLength = dDeleteStart;
  998. lpwdn->dTotalLength = dDeleteStart;
  999. lpwdn->dNextWaveDataNode = dNewBlockNode;
  1000. }
  1001. dTotalToDelete -= dDeleteLength;
  1002. if (!lpwdn->dDataLength && dTotalToDelete) {
  1003. dVirtualWaveDataStart += lpwdn->dDataLength;
  1004. lpwdn = LPWDN(pwd, lpwdn->dNextWaveDataNode);
  1005. dprintf4(("mwDeleteData - more to delete, iterating"));
  1006. }
  1007. }
  1008. pwd->dSize -= ((pwd->dTo - pwd->dFrom) + dTotalToDelete);
  1009. for (lpwdn = NULL, lpwdnCur = LPWDN(pwd, pwd->dWaveDataStartNode);;) {
  1010. if (!lpwdnCur->dDataLength) {
  1011. if (lpwdn) {
  1012. if (pwd->dWaveDataCurrentNode == lpwdn->dNextWaveDataNode)
  1013. pwd->dWaveDataCurrentNode = lpwdnCur->dNextWaveDataNode;
  1014. lpwdn->dNextWaveDataNode = lpwdnCur->dNextWaveDataNode;
  1015. } else {
  1016. if (pwd->dWaveDataCurrentNode == pwd->dWaveDataStartNode)
  1017. pwd->dWaveDataCurrentNode = lpwdnCur->dNextWaveDataNode;
  1018. pwd->dWaveDataStartNode = lpwdnCur->dNextWaveDataNode;
  1019. }
  1020. RELEASEBLOCKNODE(lpwdnCur);
  1021. }
  1022. if (lpwdnCur->dNextWaveDataNode == ENDOFNODES){
  1023. break;
  1024. }
  1025. lpwdn = lpwdnCur;
  1026. lpwdnCur = LPWDN(pwd, lpwdn->dNextWaveDataNode);
  1027. }
  1028. if (!pwd->dSize) {
  1029. pwd->dWaveDataStartNode = mwFindAnyFreeDataNode(pwd, 1);
  1030. pwd->dWaveDataCurrentNode = pwd->dWaveDataStartNode;
  1031. lpwdn = LPWDN(pwd, pwd->dWaveDataStartNode);
  1032. lpwdn->dNextWaveDataNode = (DWORD)ENDOFNODES;
  1033. } else if (pwd->dWaveDataCurrentNode == ENDOFNODES) {
  1034. pwd->dVirtualWaveDataStart = 0;
  1035. pwd->dWaveDataCurrentNode = pwd->dWaveDataStartNode;
  1036. for (lpwdn = LPWDN(pwd, pwd->dWaveDataStartNode); pwd->dFrom > pwd->dVirtualWaveDataStart + lpwdn->dDataLength;) {
  1037. pwd->dVirtualWaveDataStart += lpwdn->dDataLength;
  1038. pwd->dWaveDataCurrentNode = lpwdn->dNextWaveDataNode;
  1039. lpwdn = LPWDN(pwd, pwd->dWaveDataCurrentNode);
  1040. }
  1041. }
  1042. }
  1043. /************************************************************************/
  1044. /*
  1045. @doc INTERNAL MCIWAVE
  1046. @func UINT | RecordFile |
  1047. This function is used to Cue or Record wave device input. For normal
  1048. recording mode the function basically queues buffers on the wave
  1049. device, and writes them to a file as they are filled, blocking for
  1050. each buffer. It also makes sure to call <f>mmYield<d> while both
  1051. writing out new buffers, and waiting for buffers to be filled. This
  1052. means that it will try to add all the buffers possible to the input
  1053. wave device, and then write them as fast as possible.
  1054. For Cue mode, the function also tries to add buffers to the wave
  1055. input device, but nothing is ever written out, and only the highest
  1056. level is calculated.
  1057. Within the record loop, the function first checks to see if there
  1058. is a Cue mode buffer waiting, and if so, waits for it. This allows
  1059. only one buffer to be added to the device when in Cue mode. The
  1060. current level is calculated with the contents of the buffer.
  1061. If the function is not in Cue mode, or there is not currently a
  1062. queued buffer, the function tries to add a new buffer to the input
  1063. wave device. This cannot occur if a new command is pending, or there
  1064. are no buffers available. This means that in normal recording mode,
  1065. there will possibly be extra data recorded that does not need to be.
  1066. If an error occurs adding the buffer to the wave device, the record
  1067. function is aborted with an error, else the current outstanding buffer
  1068. count is incremented, and a pointer to the next available recording
  1069. buffer is fetched.
  1070. If no new buffers can be added, the existing buffers are written to
  1071. the file. This section cannot be entered in Cue mode, as it is
  1072. dealt with in the first condition. The task is blocked pending a
  1073. signal from the wave device that a buffer has been filled. It then
  1074. checks to see if any more data needs to be recorded before attempting
  1075. to write that data. Note that all filled buffers are dealt with one
  1076. after the other without yielding or otherwise adding new record
  1077. buffers. If the input capability is much faster than the machine,
  1078. this means that instead of getting a lot of disconnect samples, large
  1079. gaps will be produced. This loop is broken out of when either all the
  1080. buffers that were added are written, or no more buffers are currently
  1081. ready (checks the WHDR_DONE flag).
  1082. If no buffers need to be written, the loop checks for the new command
  1083. flag, which can possibly interrupt or change the current recording.
  1084. The only thing that can really make a difference is a stop command,
  1085. and as this case is handled after all buffers are written, the loop
  1086. can immediately exit.
  1087. The final default condition occurs when all the data has been recorded,
  1088. all the buffers have been released, and no new command was encountered.
  1089. In this case, recording is done, and the record loop is exited.
  1090. @parm <t>PWAVEDESC<d> | pwd |
  1091. Pointer to the wave device descriptor.
  1092. @rdesc Returns the number of outstanding buffers added to the wave device.
  1093. This can be used when removing task signal from the message queue.
  1094. In cases of error, the <e>WAVEDESC.wTaskError<d> flag is set. This
  1095. specific error is not currently returned, as the calling task may not
  1096. have waited for the command to complete. But it is at least used for
  1097. notification in order to determine if Failure status should be sent.
  1098. @xref PlayFile.
  1099. */
  1100. PUBLIC UINT PASCAL FAR RecordFile(
  1101. register PWAVEDESC pwd)
  1102. {
  1103. LPWAVEHDR *lplpWaveHdrRecord;
  1104. LPWAVEHDR *lplpWaveHdrWrite;
  1105. UINT wMode;
  1106. register UINT wBuffersOutstanding;
  1107. if (0 != (pwd->wTaskError = waveInStart(pwd->hWaveIn)))
  1108. return 0;
  1109. for (wBuffersOutstanding = 0, lplpWaveHdrRecord = lplpWaveHdrWrite = pwd->rglpWaveHdr;;) {
  1110. if (ISMODE(pwd, COMMAND_CUE) && wBuffersOutstanding) {
  1111. if (TaskBlock() == WM_USER) {
  1112. wBuffersOutstanding--;
  1113. }
  1114. if (!ISMODE(pwd, COMMAND_NEW)) {
  1115. pwd->dLevel = mwGetLevel(pwd, (*lplpWaveHdrWrite)->lpData, (int)(*lplpWaveHdrWrite)->dwBytesRecorded);
  1116. ADDMODE(pwd, MODE_CUED);
  1117. }
  1118. lplpWaveHdrWrite = NextWaveHdr(pwd, lplpWaveHdrWrite);
  1119. } else if (!ISMODE(pwd, COMMAND_NEW) && (wBuffersOutstanding < pwd->wAudioBuffers)) {
  1120. (*lplpWaveHdrRecord)->dwBufferLength = (pwd->wMode & COMMAND_CUE) ? NUM_LEVEL_SAMPLES : min(pwd->dAudioBufferLen, pwd->dTo - pwd->dCur);
  1121. (*lplpWaveHdrRecord)->dwFlags &= ~(WHDR_DONE | WHDR_BEGINLOOP | WHDR_ENDLOOP);
  1122. if (0 != (pwd->wTaskError = waveInAddBuffer(pwd->hWaveIn, *lplpWaveHdrRecord, sizeof(WAVEHDR))))
  1123. break;
  1124. wBuffersOutstanding++;
  1125. lplpWaveHdrRecord = NextWaveHdr(pwd, lplpWaveHdrRecord);
  1126. } else if (wBuffersOutstanding) {
  1127. BOOL fExitRecording;
  1128. for (fExitRecording = FALSE; wBuffersOutstanding && !fExitRecording;) {
  1129. if (TaskBlock() == WM_USER) {
  1130. wBuffersOutstanding--;
  1131. }
  1132. if (pwd->dTo == pwd->dCur) {
  1133. fExitRecording = TRUE;
  1134. break;
  1135. }
  1136. if (!(pwd->wMode & COMMAND_CUE))
  1137. if (pwd->wMode & MODE_INSERT) {
  1138. if (!mwInsert(pwd, (LPBYTE)(*lplpWaveHdrWrite)->lpData, min((*lplpWaveHdrWrite)->dwBytesRecorded, pwd->dTo - pwd->dCur)))
  1139. fExitRecording = TRUE;
  1140. } else if (!mwOverWrite(pwd, (LPBYTE)(*lplpWaveHdrWrite)->lpData, min((*lplpWaveHdrWrite)->dwBytesRecorded, pwd->dTo - pwd->dCur)))
  1141. fExitRecording = TRUE;
  1142. lplpWaveHdrWrite = NextWaveHdr(pwd, lplpWaveHdrWrite);
  1143. if (!((*lplpWaveHdrWrite)->dwFlags & WHDR_DONE))
  1144. break;
  1145. }
  1146. if (fExitRecording)
  1147. break;
  1148. } else if (!ISMODE(pwd, COMMAND_NEW) || !CheckNewCommand(pwd))
  1149. break;
  1150. else
  1151. wMode = GETMODE(pwd);
  1152. mmYield(pwd);
  1153. }
  1154. REMOVEMODE(pwd, MODE_INSERT | MODE_OVERWRITE);
  1155. return wBuffersOutstanding;
  1156. }
  1157. /************************************************************************/