Windows NT 4.0 source code leak
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.

1350 lines
51 KiB

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