Source code of Windows XP (NT5)
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.

279 lines
6.3 KiB

  1. /****************************************************************************
  2. *
  3. * playfile.c
  4. *
  5. * Copyright (c) 1991 Microsoft Corporation. All Rights Reserved.
  6. *
  7. ***************************************************************************/
  8. #include <windows.h>
  9. #include <mmsystem.h>
  10. #include <stdio.h>
  11. #include "wincom.h"
  12. #include "sbtest.h"
  13. #define CHUNKSIZE 0x4000 // size of chunk we send to the driver
  14. /****************************************************************************
  15. *
  16. * public data
  17. *
  18. ***************************************************************************/
  19. HWAVEOUT hWaveOut = 0; // handle to open wave device
  20. int iWaveOut = 0; // count of blocks writen to the wave device
  21. #if 0
  22. /****************************************************************************
  23. *
  24. * internal function prototypes
  25. *
  26. ***************************************************************************/
  27. static void ShowResError(void);
  28. static void ShowResError(void)
  29. {
  30. DWORD dwErr;
  31. char buf[80];
  32. dwErr = medGetError();
  33. if (!dwErr)
  34. return;
  35. medGetErrorText(dwErr, buf, sizeof(buf));
  36. sprintf(ach, "\nMediaman error: %s", (LPSTR) buf);
  37. dbgOut;
  38. }
  39. #endif
  40. void PlayFile(LPSTR fname)
  41. {
  42. // MEDID medid;
  43. int hMed;
  44. // FOURCC ckid;
  45. DWORD dwFmtSize;
  46. char szFileName[80];
  47. UINT wResult;
  48. WAVEFORMAT *pFormat;
  49. DWORD dwDataSize;
  50. HANDLE hData;
  51. LPSTR lpData;
  52. LPWAVEHDR lpWaveHdr;
  53. PVOID pData;
  54. // Get a handle to our file
  55. hMed = _lopen(fname, OF_READ);
  56. #if 0
  57. lstrcpy(szFileName, fname);
  58. medid = medLocate( szFileName,
  59. medMEDTYPE('W','A','V','E'),
  60. MEDF_LOCATE,
  61. NULL);
  62. if (medid == 0) {
  63. sprintf(ach, "\nFailed to locate resource: %s", (LPSTR)szFileName);
  64. dbgOut;
  65. // ShowResError();
  66. return;
  67. }
  68. // open the resource and validate it
  69. hMed = medOpen(medid, MOP_READ, 64);
  70. #endif
  71. if (!hMed) {
  72. sprintf(ach, "\nFailed to open resource");
  73. // ShowResError();
  74. return;
  75. }
  76. hData = CreateFileMapping(hMed, NULL, PAGE_READONLY, 0, 0, NULL);
  77. pData = MapViewOfFile(hData, FILE_MAP_READ, 0, 0, 0);
  78. if (!pData) {
  79. sprintf(ach, "\nCould not map file");
  80. dbgOut;
  81. return;
  82. }
  83. // descend into the murky depths of the RIFF chunk
  84. #if 0
  85. ckid = medDescend(hMed);
  86. if (ckid != medFOURCC('R','I','F','F')) {
  87. sprintf(ach, "\nResource is not Ricky's Interchange File Format");
  88. dbgOut;
  89. // ShowResError();
  90. return;
  91. }
  92. // first four chars should be WAVE
  93. if ((medGetFOURCC(hMed) != mmioFOURCC('W','A','V','E'))
  94. #endif
  95. if (!IsRiffWaveFormat(pData)) {
  96. sprintf(ach, "\nRIFF file not a WAVE file");
  97. dbgOut;
  98. // ShowResError();
  99. return;
  100. }
  101. // get all excited and look for a fmt chunk
  102. #if 0
  103. if (!medFindChunk(hMed, medFOURCC('f','m','t',' '))) {
  104. sprintf(ach, "\nRIFF/WAVE file has no fmt chunk");
  105. dbgOut;
  106. // ShowResError();
  107. return;
  108. }
  109. // allocate some memory we can read the format chunk into
  110. dwFmtSize = medGetChunkSize(hMed);
  111. pFormat = (WAVEFORMAT*)LocalAlloc(LPTR, (UINT)dwFmtSize);
  112. #endif
  113. pFormat = FindRiffChunk(&dwFmtSize, pData, mmioFOURCC('f','m','t',' '));
  114. if (!pFormat) {
  115. sprintf(ach, "\nFailed to alloc memory for WAVEFORMAT");
  116. dbgOut;
  117. return;
  118. }
  119. // read the header
  120. #if 0
  121. if (medRead(hMed, (LPSTR) pFormat, dwFmtSize) != dwFmtSize) {
  122. sprintf(ach, "\nFailed to read header");
  123. dbgOut;
  124. ShowResError();
  125. return;
  126. }
  127. #endif
  128. // try to open the first wave device with this format
  129. // (we should enum them later)
  130. if (!hWaveOut) {
  131. if (waveOutOpen(&hWaveOut,
  132. 0,
  133. pFormat,
  134. (DWORD)hMainWnd,
  135. 0L,
  136. CALLBACK_WINDOW)) {
  137. sprintf(ach, "\nFailed to open wave device");
  138. dbgOut;
  139. return;
  140. }
  141. }
  142. // LocalFree((HANDLE)pFormat);
  143. // find the data chunk and get it's size
  144. #if 0
  145. if (!medAscend(hMed)) {
  146. sprintf(ach, "\nFailed to ascend from the depths");
  147. dbgOut;
  148. ShowResError();
  149. return;
  150. }
  151. if (!medFindChunk(hMed, medFOURCC('d','a','t','a'))) {
  152. sprintf(ach, "\nRIFF/WAVE file has no data chunk");
  153. dbgOut;
  154. ShowResError();
  155. return;
  156. }
  157. dwDataSize = medGetChunkSize(hMed);
  158. // read the data chunk
  159. hData = GlobalAlloc(GMEM_MOVEABLE, dwDataSize + sizeof(WAVEHDR));
  160. if (!hData) {
  161. sprintf(ach, "\nNot enough memory for data block");
  162. dbgOut;
  163. return;
  164. }
  165. // fill the buffer from the file
  166. lpData = GlobalLock(hData);
  167. #endif
  168. lpData = FindRiffChunk(&dwDataSize, pData, mmioFOURCC('d','a','t','a'));
  169. if (!lpData) {
  170. sprintf(ach, "\nFailed to lock data memory");
  171. dbgOut;
  172. CloseHandle(hData);
  173. // GlobalFree(hData);
  174. return;
  175. }
  176. #if 0
  177. if (medRead(hMed, lpData + sizeof(WAVEHDR), dwDataSize) != dwDataSize) {
  178. sprintf(ach, "\nFailed to read data block");
  179. dbgOut;
  180. GlobalUnlock(hData);
  181. GlobalFree(hData);
  182. return;
  183. }
  184. #endif
  185. // create a header to send to the device
  186. lpWaveHdr = LocalAlloc(LPTR, sizeof(WAVEHDR));
  187. lpWaveHdr->lpData = lpData;
  188. lpWaveHdr->dwBufferLength = dwDataSize;
  189. lpWaveHdr->dwUser = 0;
  190. lpWaveHdr->dwFlags = 0;
  191. lpWaveHdr->dwLoops = 0;
  192. // Add looping flags if required
  193. if (wLoops != IDM_LOOPOFF) {
  194. lpWaveHdr->dwFlags |= (WHDR_BEGINLOOP | WHDR_ENDLOOP);
  195. if (wLoops == IDM_LOOP2)
  196. lpWaveHdr->dwLoops = 2;
  197. else
  198. lpWaveHdr->dwLoops = 100;
  199. }
  200. // let the driver prepare the header and data block
  201. waveOutPrepareHeader(hWaveOut, lpWaveHdr, sizeof(WAVEHDR));
  202. // send it to the driver
  203. wResult = waveOutWrite(hWaveOut, lpWaveHdr, sizeof(WAVEHDR));
  204. if (wResult != 0) {
  205. sprintf(ach, "\nFailed to write block to device : code %d", wResult);
  206. dbgOut;
  207. waveOutUnprepareHeader(hWaveOut, lpWaveHdr, sizeof(WAVEHDR));
  208. // GlobalUnlock(hData);
  209. // GlobalFree(hData);
  210. return;
  211. }
  212. _lclose(hMed);
  213. iWaveOut++;
  214. }