winmm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <memory.h>
  24. #include <windows.h>
  25. #include <mmsystem.h>
  26. #include "alMain.h"
  27. #include "alu.h"
  28. #include "threads.h"
  29. #ifndef WAVE_FORMAT_IEEE_FLOAT
  30. #define WAVE_FORMAT_IEEE_FLOAT 0x0003
  31. #endif
  32. typedef struct {
  33. // MMSYSTEM Device
  34. volatile ALboolean killNow;
  35. HANDLE WaveThreadEvent;
  36. HANDLE WaveThread;
  37. DWORD WaveThreadID;
  38. volatile LONG WaveBuffersCommitted;
  39. WAVEHDR WaveBuffer[4];
  40. union {
  41. HWAVEIN In;
  42. HWAVEOUT Out;
  43. } WaveHandle;
  44. WAVEFORMATEX Format;
  45. RingBuffer *Ring;
  46. } WinMMData;
  47. static ALCchar **PlaybackDeviceList;
  48. static ALuint NumPlaybackDevices;
  49. static ALCchar **CaptureDeviceList;
  50. static ALuint NumCaptureDevices;
  51. static void ProbePlaybackDevices(void)
  52. {
  53. ALuint i;
  54. for(i = 0;i < NumPlaybackDevices;i++)
  55. free(PlaybackDeviceList[i]);
  56. NumPlaybackDevices = waveOutGetNumDevs();
  57. PlaybackDeviceList = realloc(PlaybackDeviceList, sizeof(ALCchar*) * NumPlaybackDevices);
  58. for(i = 0;i < NumPlaybackDevices;i++)
  59. {
  60. WAVEOUTCAPS WaveCaps;
  61. PlaybackDeviceList[i] = NULL;
  62. if(waveOutGetDevCaps(i, &WaveCaps, sizeof(WaveCaps)) == MMSYSERR_NOERROR)
  63. {
  64. char name[1024];
  65. ALuint count, j;
  66. count = 0;
  67. do {
  68. if(count == 0)
  69. snprintf(name, sizeof(name), "%s", WaveCaps.szPname);
  70. else
  71. snprintf(name, sizeof(name), "%s #%d", WaveCaps.szPname, count+1);
  72. count++;
  73. for(j = 0;j < i;j++)
  74. {
  75. if(strcmp(name, PlaybackDeviceList[j]) == 0)
  76. break;
  77. }
  78. } while(j != i);
  79. PlaybackDeviceList[i] = strdup(name);
  80. }
  81. }
  82. }
  83. static void ProbeCaptureDevices(void)
  84. {
  85. ALuint i;
  86. for(i = 0;i < NumCaptureDevices;i++)
  87. free(CaptureDeviceList[i]);
  88. NumCaptureDevices = waveInGetNumDevs();
  89. CaptureDeviceList = realloc(CaptureDeviceList, sizeof(ALCchar*) * NumCaptureDevices);
  90. for(i = 0;i < NumCaptureDevices;i++)
  91. {
  92. WAVEINCAPS WaveInCaps;
  93. CaptureDeviceList[i] = NULL;
  94. if(waveInGetDevCaps(i, &WaveInCaps, sizeof(WAVEINCAPS)) == MMSYSERR_NOERROR)
  95. {
  96. char name[1024];
  97. ALuint count, j;
  98. count = 0;
  99. do {
  100. if(count == 0)
  101. snprintf(name, sizeof(name), "%s", WaveInCaps.szPname);
  102. else
  103. snprintf(name, sizeof(name), "%s #%d", WaveInCaps.szPname, count+1);
  104. count++;
  105. for(j = 0;j < i;j++)
  106. {
  107. if(strcmp(name, CaptureDeviceList[j]) == 0)
  108. break;
  109. }
  110. } while(j != i);
  111. CaptureDeviceList[i] = strdup(name);
  112. }
  113. }
  114. }
  115. /*
  116. WaveOutProc
  117. Posts a message to 'PlaybackThreadProc' everytime a WaveOut Buffer is completed and
  118. returns to the application (for more data)
  119. */
  120. static void CALLBACK WaveOutProc(HWAVEOUT UNUSED(device), UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR UNUSED(param2))
  121. {
  122. ALCdevice *Device = (ALCdevice*)instance;
  123. WinMMData *data = Device->ExtraData;
  124. if(msg != WOM_DONE)
  125. return;
  126. InterlockedDecrement(&data->WaveBuffersCommitted);
  127. PostThreadMessage(data->WaveThreadID, msg, 0, param1);
  128. }
  129. /*
  130. PlaybackThreadProc
  131. Used by "MMSYSTEM" Device. Called when a WaveOut buffer has used up its
  132. audio data.
  133. */
  134. FORCE_ALIGN static DWORD WINAPI PlaybackThreadProc(LPVOID param)
  135. {
  136. ALCdevice *Device = (ALCdevice*)param;
  137. WinMMData *data = Device->ExtraData;
  138. LPWAVEHDR WaveHdr;
  139. ALuint FrameSize;
  140. MSG msg;
  141. FrameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
  142. SetRTPriority();
  143. SetThreadName(MIXER_THREAD_NAME);
  144. while(GetMessage(&msg, NULL, 0, 0))
  145. {
  146. if(msg.message != WOM_DONE)
  147. continue;
  148. if(data->killNow)
  149. {
  150. if(data->WaveBuffersCommitted == 0)
  151. break;
  152. continue;
  153. }
  154. WaveHdr = ((LPWAVEHDR)msg.lParam);
  155. aluMixData(Device, WaveHdr->lpData, WaveHdr->dwBufferLength/FrameSize);
  156. // Send buffer back to play more data
  157. waveOutWrite(data->WaveHandle.Out, WaveHdr, sizeof(WAVEHDR));
  158. InterlockedIncrement(&data->WaveBuffersCommitted);
  159. }
  160. // Signal Wave Thread completed event
  161. if(data->WaveThreadEvent)
  162. SetEvent(data->WaveThreadEvent);
  163. ExitThread(0);
  164. return 0;
  165. }
  166. /*
  167. WaveInProc
  168. Posts a message to 'CaptureThreadProc' everytime a WaveIn Buffer is completed and
  169. returns to the application (with more data)
  170. */
  171. static void CALLBACK WaveInProc(HWAVEIN UNUSED(device), UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR UNUSED(param2))
  172. {
  173. ALCdevice *Device = (ALCdevice*)instance;
  174. WinMMData *data = Device->ExtraData;
  175. if(msg != WIM_DATA)
  176. return;
  177. InterlockedDecrement(&data->WaveBuffersCommitted);
  178. PostThreadMessage(data->WaveThreadID, msg, 0, param1);
  179. }
  180. /*
  181. CaptureThreadProc
  182. Used by "MMSYSTEM" Device. Called when a WaveIn buffer had been filled with new
  183. audio data.
  184. */
  185. static DWORD WINAPI CaptureThreadProc(LPVOID param)
  186. {
  187. ALCdevice *Device = (ALCdevice*)param;
  188. WinMMData *data = Device->ExtraData;
  189. LPWAVEHDR WaveHdr;
  190. ALuint FrameSize;
  191. MSG msg;
  192. FrameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
  193. SetThreadName("alsoft-record");
  194. while(GetMessage(&msg, NULL, 0, 0))
  195. {
  196. if(msg.message != WIM_DATA)
  197. continue;
  198. /* Don't wait for other buffers to finish before quitting. We're
  199. * closing so we don't need them. */
  200. if(data->killNow)
  201. break;
  202. WaveHdr = ((LPWAVEHDR)msg.lParam);
  203. WriteRingBuffer(data->Ring, (ALubyte*)WaveHdr->lpData, WaveHdr->dwBytesRecorded/FrameSize);
  204. // Send buffer back to capture more data
  205. waveInAddBuffer(data->WaveHandle.In, WaveHdr, sizeof(WAVEHDR));
  206. InterlockedIncrement(&data->WaveBuffersCommitted);
  207. }
  208. // Signal Wave Thread completed event
  209. if(data->WaveThreadEvent)
  210. SetEvent(data->WaveThreadEvent);
  211. ExitThread(0);
  212. return 0;
  213. }
  214. static ALCenum WinMMOpenPlayback(ALCdevice *Device, const ALCchar *deviceName)
  215. {
  216. WinMMData *data = NULL;
  217. UINT DeviceID = 0;
  218. MMRESULT res;
  219. ALuint i = 0;
  220. if(!PlaybackDeviceList)
  221. ProbePlaybackDevices();
  222. // Find the Device ID matching the deviceName if valid
  223. for(i = 0;i < NumPlaybackDevices;i++)
  224. {
  225. if(PlaybackDeviceList[i] &&
  226. (!deviceName || strcmp(deviceName, PlaybackDeviceList[i]) == 0))
  227. {
  228. DeviceID = i;
  229. break;
  230. }
  231. }
  232. if(i == NumPlaybackDevices)
  233. return ALC_INVALID_VALUE;
  234. data = calloc(1, sizeof(*data));
  235. if(!data)
  236. return ALC_OUT_OF_MEMORY;
  237. Device->ExtraData = data;
  238. retry_open:
  239. memset(&data->Format, 0, sizeof(WAVEFORMATEX));
  240. if(Device->FmtType == DevFmtFloat)
  241. {
  242. data->Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
  243. data->Format.wBitsPerSample = 32;
  244. }
  245. else
  246. {
  247. data->Format.wFormatTag = WAVE_FORMAT_PCM;
  248. if(Device->FmtType == DevFmtUByte || Device->FmtType == DevFmtByte)
  249. data->Format.wBitsPerSample = 8;
  250. else
  251. data->Format.wBitsPerSample = 16;
  252. }
  253. data->Format.nChannels = ((Device->FmtChans == DevFmtMono) ? 1 : 2);
  254. data->Format.nBlockAlign = data->Format.wBitsPerSample *
  255. data->Format.nChannels / 8;
  256. data->Format.nSamplesPerSec = Device->Frequency;
  257. data->Format.nAvgBytesPerSec = data->Format.nSamplesPerSec *
  258. data->Format.nBlockAlign;
  259. data->Format.cbSize = 0;
  260. if((res=waveOutOpen(&data->WaveHandle.Out, DeviceID, &data->Format, (DWORD_PTR)&WaveOutProc, (DWORD_PTR)Device, CALLBACK_FUNCTION)) != MMSYSERR_NOERROR)
  261. {
  262. if(Device->FmtType == DevFmtFloat)
  263. {
  264. Device->FmtType = DevFmtShort;
  265. goto retry_open;
  266. }
  267. ERR("waveOutOpen failed: %u\n", res);
  268. goto failure;
  269. }
  270. data->WaveThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  271. if(data->WaveThreadEvent == NULL)
  272. {
  273. ERR("CreateEvent failed: %lu\n", GetLastError());
  274. goto failure;
  275. }
  276. Device->DeviceName = strdup(PlaybackDeviceList[DeviceID]);
  277. return ALC_NO_ERROR;
  278. failure:
  279. if(data->WaveThreadEvent)
  280. CloseHandle(data->WaveThreadEvent);
  281. if(data->WaveHandle.Out)
  282. waveOutClose(data->WaveHandle.Out);
  283. free(data);
  284. Device->ExtraData = NULL;
  285. return ALC_INVALID_VALUE;
  286. }
  287. static void WinMMClosePlayback(ALCdevice *device)
  288. {
  289. WinMMData *data = (WinMMData*)device->ExtraData;
  290. // Close the Wave device
  291. CloseHandle(data->WaveThreadEvent);
  292. data->WaveThreadEvent = 0;
  293. waveOutClose(data->WaveHandle.Out);
  294. data->WaveHandle.Out = 0;
  295. free(data);
  296. device->ExtraData = NULL;
  297. }
  298. static ALCboolean WinMMResetPlayback(ALCdevice *device)
  299. {
  300. WinMMData *data = (WinMMData*)device->ExtraData;
  301. device->UpdateSize = (ALuint)((ALuint64)device->UpdateSize *
  302. data->Format.nSamplesPerSec /
  303. device->Frequency);
  304. device->UpdateSize = (device->UpdateSize*device->NumUpdates + 3) / 4;
  305. device->NumUpdates = 4;
  306. device->Frequency = data->Format.nSamplesPerSec;
  307. if(data->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
  308. {
  309. if(data->Format.wBitsPerSample == 32)
  310. device->FmtType = DevFmtFloat;
  311. else
  312. {
  313. ERR("Unhandled IEEE float sample depth: %d\n", data->Format.wBitsPerSample);
  314. return ALC_FALSE;
  315. }
  316. }
  317. else if(data->Format.wFormatTag == WAVE_FORMAT_PCM)
  318. {
  319. if(data->Format.wBitsPerSample == 16)
  320. device->FmtType = DevFmtShort;
  321. else if(data->Format.wBitsPerSample == 8)
  322. device->FmtType = DevFmtUByte;
  323. else
  324. {
  325. ERR("Unhandled PCM sample depth: %d\n", data->Format.wBitsPerSample);
  326. return ALC_FALSE;
  327. }
  328. }
  329. else
  330. {
  331. ERR("Unhandled format tag: 0x%04x\n", data->Format.wFormatTag);
  332. return ALC_FALSE;
  333. }
  334. if(data->Format.nChannels == 2)
  335. device->FmtChans = DevFmtStereo;
  336. else if(data->Format.nChannels == 1)
  337. device->FmtChans = DevFmtMono;
  338. else
  339. {
  340. ERR("Unhandled channel count: %d\n", data->Format.nChannels);
  341. return ALC_FALSE;
  342. }
  343. SetDefaultWFXChannelOrder(device);
  344. return ALC_TRUE;
  345. }
  346. static ALCboolean WinMMStartPlayback(ALCdevice *device)
  347. {
  348. WinMMData *data = (WinMMData*)device->ExtraData;
  349. ALbyte *BufferData;
  350. ALint BufferSize;
  351. ALuint i;
  352. data->WaveThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlaybackThreadProc, (LPVOID)device, 0, &data->WaveThreadID);
  353. if(data->WaveThread == NULL)
  354. return ALC_FALSE;
  355. data->WaveBuffersCommitted = 0;
  356. // Create 4 Buffers
  357. BufferSize = device->UpdateSize*device->NumUpdates / 4;
  358. BufferSize *= FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  359. BufferData = calloc(4, BufferSize);
  360. for(i = 0;i < 4;i++)
  361. {
  362. memset(&data->WaveBuffer[i], 0, sizeof(WAVEHDR));
  363. data->WaveBuffer[i].dwBufferLength = BufferSize;
  364. data->WaveBuffer[i].lpData = ((i==0) ? (LPSTR)BufferData :
  365. (data->WaveBuffer[i-1].lpData +
  366. data->WaveBuffer[i-1].dwBufferLength));
  367. waveOutPrepareHeader(data->WaveHandle.Out, &data->WaveBuffer[i], sizeof(WAVEHDR));
  368. waveOutWrite(data->WaveHandle.Out, &data->WaveBuffer[i], sizeof(WAVEHDR));
  369. InterlockedIncrement(&data->WaveBuffersCommitted);
  370. }
  371. return ALC_TRUE;
  372. }
  373. static void WinMMStopPlayback(ALCdevice *device)
  374. {
  375. WinMMData *data = (WinMMData*)device->ExtraData;
  376. void *buffer = NULL;
  377. int i;
  378. if(data->WaveThread == NULL)
  379. return;
  380. // Set flag to stop processing headers
  381. data->killNow = AL_TRUE;
  382. // Wait for signal that Wave Thread has been destroyed
  383. WaitForSingleObjectEx(data->WaveThreadEvent, 5000, FALSE);
  384. CloseHandle(data->WaveThread);
  385. data->WaveThread = 0;
  386. data->killNow = AL_FALSE;
  387. // Release the wave buffers
  388. for(i = 0;i < 4;i++)
  389. {
  390. waveOutUnprepareHeader(data->WaveHandle.Out, &data->WaveBuffer[i], sizeof(WAVEHDR));
  391. if(i == 0) buffer = data->WaveBuffer[i].lpData;
  392. data->WaveBuffer[i].lpData = NULL;
  393. }
  394. free(buffer);
  395. }
  396. static ALCenum WinMMOpenCapture(ALCdevice *Device, const ALCchar *deviceName)
  397. {
  398. ALbyte *BufferData = NULL;
  399. DWORD CapturedDataSize;
  400. WinMMData *data = NULL;
  401. UINT DeviceID = 0;
  402. ALint BufferSize;
  403. MMRESULT res;
  404. ALuint i;
  405. if(!CaptureDeviceList)
  406. ProbeCaptureDevices();
  407. // Find the Device ID matching the deviceName if valid
  408. for(i = 0;i < NumCaptureDevices;i++)
  409. {
  410. if(CaptureDeviceList[i] &&
  411. (!deviceName || strcmp(deviceName, CaptureDeviceList[i]) == 0))
  412. {
  413. DeviceID = i;
  414. break;
  415. }
  416. }
  417. if(i == NumCaptureDevices)
  418. return ALC_INVALID_VALUE;
  419. switch(Device->FmtChans)
  420. {
  421. case DevFmtMono:
  422. case DevFmtStereo:
  423. break;
  424. case DevFmtQuad:
  425. case DevFmtX51:
  426. case DevFmtX51Side:
  427. case DevFmtX61:
  428. case DevFmtX71:
  429. return ALC_INVALID_ENUM;
  430. }
  431. switch(Device->FmtType)
  432. {
  433. case DevFmtUByte:
  434. case DevFmtShort:
  435. case DevFmtInt:
  436. case DevFmtFloat:
  437. break;
  438. case DevFmtByte:
  439. case DevFmtUShort:
  440. case DevFmtUInt:
  441. return ALC_INVALID_ENUM;
  442. }
  443. data = calloc(1, sizeof(*data));
  444. if(!data)
  445. return ALC_OUT_OF_MEMORY;
  446. Device->ExtraData = data;
  447. memset(&data->Format, 0, sizeof(WAVEFORMATEX));
  448. data->Format.wFormatTag = ((Device->FmtType == DevFmtFloat) ?
  449. WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM);
  450. data->Format.nChannels = ChannelsFromDevFmt(Device->FmtChans);
  451. data->Format.wBitsPerSample = BytesFromDevFmt(Device->FmtType) * 8;
  452. data->Format.nBlockAlign = data->Format.wBitsPerSample *
  453. data->Format.nChannels / 8;
  454. data->Format.nSamplesPerSec = Device->Frequency;
  455. data->Format.nAvgBytesPerSec = data->Format.nSamplesPerSec *
  456. data->Format.nBlockAlign;
  457. data->Format.cbSize = 0;
  458. if((res=waveInOpen(&data->WaveHandle.In, DeviceID, &data->Format, (DWORD_PTR)&WaveInProc, (DWORD_PTR)Device, CALLBACK_FUNCTION)) != MMSYSERR_NOERROR)
  459. {
  460. ERR("waveInOpen failed: %u\n", res);
  461. goto failure;
  462. }
  463. data->WaveThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  464. if(data->WaveThreadEvent == NULL)
  465. {
  466. ERR("CreateEvent failed: %lu\n", GetLastError());
  467. goto failure;
  468. }
  469. // Allocate circular memory buffer for the captured audio
  470. CapturedDataSize = Device->UpdateSize*Device->NumUpdates;
  471. // Make sure circular buffer is at least 100ms in size
  472. if(CapturedDataSize < (data->Format.nSamplesPerSec / 10))
  473. CapturedDataSize = data->Format.nSamplesPerSec / 10;
  474. data->Ring = CreateRingBuffer(data->Format.nBlockAlign, CapturedDataSize);
  475. if(!data->Ring)
  476. goto failure;
  477. data->WaveBuffersCommitted = 0;
  478. // Create 4 Buffers of 50ms each
  479. BufferSize = data->Format.nAvgBytesPerSec / 20;
  480. BufferSize -= (BufferSize % data->Format.nBlockAlign);
  481. BufferData = calloc(4, BufferSize);
  482. if(!BufferData)
  483. goto failure;
  484. for(i = 0;i < 4;i++)
  485. {
  486. memset(&data->WaveBuffer[i], 0, sizeof(WAVEHDR));
  487. data->WaveBuffer[i].dwBufferLength = BufferSize;
  488. data->WaveBuffer[i].lpData = ((i==0) ? (LPSTR)BufferData :
  489. (data->WaveBuffer[i-1].lpData +
  490. data->WaveBuffer[i-1].dwBufferLength));
  491. data->WaveBuffer[i].dwFlags = 0;
  492. data->WaveBuffer[i].dwLoops = 0;
  493. waveInPrepareHeader(data->WaveHandle.In, &data->WaveBuffer[i], sizeof(WAVEHDR));
  494. waveInAddBuffer(data->WaveHandle.In, &data->WaveBuffer[i], sizeof(WAVEHDR));
  495. InterlockedIncrement(&data->WaveBuffersCommitted);
  496. }
  497. data->WaveThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CaptureThreadProc, (LPVOID)Device, 0, &data->WaveThreadID);
  498. if (data->WaveThread == NULL)
  499. goto failure;
  500. Device->DeviceName = strdup(CaptureDeviceList[DeviceID]);
  501. return ALC_NO_ERROR;
  502. failure:
  503. if(data->WaveThread)
  504. CloseHandle(data->WaveThread);
  505. if(BufferData)
  506. {
  507. for(i = 0;i < 4;i++)
  508. waveInUnprepareHeader(data->WaveHandle.In, &data->WaveBuffer[i], sizeof(WAVEHDR));
  509. free(BufferData);
  510. }
  511. if(data->Ring)
  512. DestroyRingBuffer(data->Ring);
  513. if(data->WaveThreadEvent)
  514. CloseHandle(data->WaveThreadEvent);
  515. if(data->WaveHandle.In)
  516. waveInClose(data->WaveHandle.In);
  517. free(data);
  518. Device->ExtraData = NULL;
  519. return ALC_INVALID_VALUE;
  520. }
  521. static void WinMMCloseCapture(ALCdevice *Device)
  522. {
  523. WinMMData *data = (WinMMData*)Device->ExtraData;
  524. void *buffer = NULL;
  525. int i;
  526. /* Tell the processing thread to quit and wait for it to do so. */
  527. data->killNow = AL_TRUE;
  528. PostThreadMessage(data->WaveThreadID, WM_QUIT, 0, 0);
  529. WaitForSingleObjectEx(data->WaveThreadEvent, 5000, FALSE);
  530. /* Make sure capture is stopped and all pending buffers are flushed. */
  531. waveInReset(data->WaveHandle.In);
  532. CloseHandle(data->WaveThread);
  533. data->WaveThread = 0;
  534. // Release the wave buffers
  535. for(i = 0;i < 4;i++)
  536. {
  537. waveInUnprepareHeader(data->WaveHandle.In, &data->WaveBuffer[i], sizeof(WAVEHDR));
  538. if(i == 0) buffer = data->WaveBuffer[i].lpData;
  539. data->WaveBuffer[i].lpData = NULL;
  540. }
  541. free(buffer);
  542. DestroyRingBuffer(data->Ring);
  543. data->Ring = NULL;
  544. // Close the Wave device
  545. CloseHandle(data->WaveThreadEvent);
  546. data->WaveThreadEvent = 0;
  547. waveInClose(data->WaveHandle.In);
  548. data->WaveHandle.In = 0;
  549. free(data);
  550. Device->ExtraData = NULL;
  551. }
  552. static void WinMMStartCapture(ALCdevice *Device)
  553. {
  554. WinMMData *data = (WinMMData*)Device->ExtraData;
  555. waveInStart(data->WaveHandle.In);
  556. }
  557. static void WinMMStopCapture(ALCdevice *Device)
  558. {
  559. WinMMData *data = (WinMMData*)Device->ExtraData;
  560. waveInStop(data->WaveHandle.In);
  561. }
  562. static ALCenum WinMMCaptureSamples(ALCdevice *Device, ALCvoid *Buffer, ALCuint Samples)
  563. {
  564. WinMMData *data = (WinMMData*)Device->ExtraData;
  565. ReadRingBuffer(data->Ring, Buffer, Samples);
  566. return ALC_NO_ERROR;
  567. }
  568. static ALCuint WinMMAvailableSamples(ALCdevice *Device)
  569. {
  570. WinMMData *data = (WinMMData*)Device->ExtraData;
  571. return RingBufferSize(data->Ring);
  572. }
  573. static const BackendFuncs WinMMFuncs = {
  574. WinMMOpenPlayback,
  575. WinMMClosePlayback,
  576. WinMMResetPlayback,
  577. WinMMStartPlayback,
  578. WinMMStopPlayback,
  579. WinMMOpenCapture,
  580. WinMMCloseCapture,
  581. WinMMStartCapture,
  582. WinMMStopCapture,
  583. WinMMCaptureSamples,
  584. WinMMAvailableSamples,
  585. ALCdevice_GetLatencyDefault
  586. };
  587. ALCboolean alcWinMMInit(BackendFuncs *FuncList)
  588. {
  589. *FuncList = WinMMFuncs;
  590. return ALC_TRUE;
  591. }
  592. void alcWinMMDeinit()
  593. {
  594. ALuint i;
  595. for(i = 0;i < NumPlaybackDevices;i++)
  596. free(PlaybackDeviceList[i]);
  597. free(PlaybackDeviceList);
  598. PlaybackDeviceList = NULL;
  599. NumPlaybackDevices = 0;
  600. for(i = 0;i < NumCaptureDevices;i++)
  601. free(CaptureDeviceList[i]);
  602. free(CaptureDeviceList);
  603. CaptureDeviceList = NULL;
  604. NumCaptureDevices = 0;
  605. }
  606. void alcWinMMProbe(enum DevProbe type)
  607. {
  608. ALuint i;
  609. switch(type)
  610. {
  611. case ALL_DEVICE_PROBE:
  612. ProbePlaybackDevices();
  613. for(i = 0;i < NumPlaybackDevices;i++)
  614. {
  615. if(PlaybackDeviceList[i])
  616. AppendAllDevicesList(PlaybackDeviceList[i]);
  617. }
  618. break;
  619. case CAPTURE_DEVICE_PROBE:
  620. ProbeCaptureDevices();
  621. for(i = 0;i < NumCaptureDevices;i++)
  622. {
  623. if(CaptureDeviceList[i])
  624. AppendCaptureDeviceList(CaptureDeviceList[i]);
  625. }
  626. break;
  627. }
  628. }