Probe physical devices separately from appending them to the device list
[openal-soft.git] / Alc / winmm.c
blob54ec2f30164a67d7f6efe339b3eff7a9b834dd22
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.
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.
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
21 #include "config.h"
23 #define _WIN32_WINNT 0x0500
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
28 #include <windows.h>
29 #include <mmsystem.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
36 typedef struct {
37 // MMSYSTEM Capture Device
38 ALboolean bWaveInShutdown;
39 HANDLE hWaveInHdrEvent;
40 HANDLE hWaveInThreadEvent;
41 HANDLE hWaveInThread;
42 DWORD ulWaveInThreadID;
43 ALint lWaveInBuffersCommitted;
44 HWAVEIN hWaveInHandle;
45 WAVEHDR WaveInBuffer[4];
46 ALCchar *pCapturedSampleData;
47 ALuint ulCapturedDataSize;
48 ALuint ulReadCapturedDataPos;
49 ALuint ulWriteCapturedDataPos;
50 } WinMMData;
53 static ALCchar **CaptureDeviceList;
54 static ALuint NumCaptureDevices;
56 static void ProbeDevices(void)
58 ALuint i;
60 for(i = 0;i < NumCaptureDevices;i++)
61 free(CaptureDeviceList[i]);
63 NumCaptureDevices = waveInGetNumDevs();
64 CaptureDeviceList = realloc(CaptureDeviceList, sizeof(ALCchar*) * NumCaptureDevices);
65 for(i = 0;i < NumCaptureDevices;i++)
67 WAVEINCAPS WaveInCaps;
69 CaptureDeviceList[i] = NULL;
70 if(waveInGetDevCaps(i, &WaveInCaps, sizeof(WAVEINCAPS)) == MMSYSERR_NOERROR)
72 char name[128];
73 snprintf(name, sizeof(name), "WaveIn on %s", WaveInCaps.szPname);
74 CaptureDeviceList[i] = strdup(name);
80 WaveInProc
82 Posts a message to 'CaptureThreadProc' everytime a WaveIn Buffer is completed and
83 returns to the application (with more data)
85 static void CALLBACK WaveInProc(HWAVEIN hDevice,UINT uMsg,DWORD_PTR dwInstance,DWORD_PTR dwParam1,DWORD_PTR dwParam2)
87 ALCdevice *pDevice = (ALCdevice *)dwInstance;
88 WinMMData *pData = pDevice->ExtraData;
90 (void)hDevice;
91 (void)dwParam2;
93 if ((uMsg==WIM_DATA))
95 // Decrement number of buffers in use
96 pData->lWaveInBuffersCommitted--;
98 if (pData->bWaveInShutdown == AL_FALSE)
100 // Notify Wave Processor Thread that a Wave Header has returned
101 PostThreadMessage(pData->ulWaveInThreadID,uMsg,0,dwParam1);
103 else
105 if (pData->lWaveInBuffersCommitted == 0)
107 // Signal Wave Buffers Returned event
108 if (pData->hWaveInHdrEvent)
109 SetEvent(pData->hWaveInHdrEvent);
111 // Post 'Quit' Message to WaveIn Processor Thread
112 PostThreadMessage(pData->ulWaveInThreadID,WM_QUIT,0,0);
119 CaptureThreadProc
121 Used by "MMSYSTEM" Device. Called when a WaveIn buffer had been filled with new
122 audio data.
124 DWORD WINAPI CaptureThreadProc(LPVOID lpParameter)
126 ALCdevice *pDevice = (ALCdevice*)lpParameter;
127 WinMMData *pData = pDevice->ExtraData;
128 ALuint ulOffset, ulMaxSize, ulSection;
129 LPWAVEHDR pWaveHdr;
130 MSG msg;
132 while (GetMessage(&msg, NULL, 0, 0))
134 if ((msg.message==WIM_DATA)&&(!pData->bWaveInShutdown))
136 SuspendContext(NULL);
138 pWaveHdr = ((LPWAVEHDR)msg.lParam);
140 // Calculate offset in local buffer to write data to
141 ulOffset = pData->ulWriteCapturedDataPos % pData->ulCapturedDataSize;
143 if ((ulOffset + pWaveHdr->dwBytesRecorded) > pData->ulCapturedDataSize)
145 ulSection = pData->ulCapturedDataSize - ulOffset;
146 memcpy(pData->pCapturedSampleData + ulOffset, pWaveHdr->lpData, ulSection);
147 memcpy(pData->pCapturedSampleData, pWaveHdr->lpData + ulSection, pWaveHdr->dwBytesRecorded - ulSection);
149 else
151 memcpy(pData->pCapturedSampleData + ulOffset, pWaveHdr->lpData, pWaveHdr->dwBytesRecorded);
154 pData->ulWriteCapturedDataPos += pWaveHdr->dwBytesRecorded;
156 if (pData->ulWriteCapturedDataPos > (pData->ulReadCapturedDataPos + pData->ulCapturedDataSize))
158 // Application has not read enough audio data from the capture buffer so data has been
159 // overwritten. Reset ReadPosition.
160 pData->ulReadCapturedDataPos = pData->ulWriteCapturedDataPos - pData->ulCapturedDataSize;
163 // To prevent an over-flow prevent the offset values from getting too large
164 ulMaxSize = pData->ulCapturedDataSize << 4;
165 if ((pData->ulReadCapturedDataPos > ulMaxSize) && (pData->ulWriteCapturedDataPos > ulMaxSize))
167 pData->ulReadCapturedDataPos -= ulMaxSize;
168 pData->ulWriteCapturedDataPos -= ulMaxSize;
171 // Send buffer back to capture more data
172 waveInAddBuffer(pData->hWaveInHandle,pWaveHdr,sizeof(WAVEHDR));
173 pData->lWaveInBuffersCommitted++;
175 ProcessContext(NULL);
179 // Signal Wave Thread completed event
180 if (pData->hWaveInThreadEvent)
181 SetEvent(pData->hWaveInThreadEvent);
183 ExitThread(0);
185 return 0;
189 static ALCboolean WinMMOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
191 (void)device;
192 (void)deviceName;
193 return ALC_FALSE;
196 static void WinMMClosePlayback(ALCdevice *device)
198 (void)device;
202 static ALCboolean WinMMOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName)
204 WAVEFORMATEX wfexCaptureFormat;
205 WinMMData *pData = NULL;
206 ALint lDeviceID = 0;
207 ALint lBufferSize;
208 ALuint i;
210 // Find the Device ID matching the deviceName if valid
211 if(deviceName)
213 for(i = 0;i < NumCaptureDevices;i++)
215 if(CaptureDeviceList[i] &&
216 strcmp(deviceName, CaptureDeviceList[i]) == 0)
218 lDeviceID = i;
219 break;
223 else
225 for(i = 0;i < NumCaptureDevices;i++)
227 if(CaptureDeviceList[i])
229 lDeviceID = i;
230 break;
234 if(i == NumCaptureDevices)
235 return ALC_FALSE;
237 pData = calloc(1, sizeof(*pData));
238 if(!pData)
240 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
241 return ALC_FALSE;
244 memset(&wfexCaptureFormat, 0, sizeof(WAVEFORMATEX));
245 wfexCaptureFormat.wFormatTag = WAVE_FORMAT_PCM;
246 wfexCaptureFormat.nChannels = aluChannelsFromFormat(pDevice->Format);
247 wfexCaptureFormat.wBitsPerSample = aluBytesFromFormat(pDevice->Format) * 8;
248 wfexCaptureFormat.nBlockAlign = wfexCaptureFormat.wBitsPerSample *
249 wfexCaptureFormat.nChannels / 8;
250 wfexCaptureFormat.nSamplesPerSec = pDevice->Frequency;
251 wfexCaptureFormat.nAvgBytesPerSec = wfexCaptureFormat.nSamplesPerSec *
252 wfexCaptureFormat.nBlockAlign;
253 wfexCaptureFormat.cbSize = 0;
255 if (waveInOpen(&pData->hWaveInHandle, lDeviceID, &wfexCaptureFormat, (DWORD_PTR)&WaveInProc, (DWORD_PTR)pDevice, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
256 goto failure;
258 pData->hWaveInHdrEvent = CreateEvent(NULL, AL_TRUE, AL_FALSE, "WaveInAllHeadersReturned");
259 if (pData->hWaveInHdrEvent == NULL)
260 goto failure;
262 pData->hWaveInThreadEvent = CreateEvent(NULL, AL_TRUE, AL_FALSE, "WaveInThreadDestroyed");
263 if (pData->hWaveInThreadEvent == NULL)
264 goto failure;
266 // Allocate circular memory buffer for the captured audio
267 pData->ulCapturedDataSize = pDevice->UpdateSize*pDevice->NumUpdates *
268 wfexCaptureFormat.nBlockAlign;
270 // Make sure circular buffer is at least 100ms in size (and an exact multiple of
271 // the block alignment
272 if (pData->ulCapturedDataSize < (wfexCaptureFormat.nAvgBytesPerSec / 10))
274 pData->ulCapturedDataSize = wfexCaptureFormat.nAvgBytesPerSec / 10;
275 pData->ulCapturedDataSize -= (pData->ulCapturedDataSize % wfexCaptureFormat.nBlockAlign);
278 pData->pCapturedSampleData = (ALCchar*)malloc(pData->ulCapturedDataSize);
279 pData->lWaveInBuffersCommitted=0;
281 // Create 4 Buffers of 50ms each
282 lBufferSize = wfexCaptureFormat.nAvgBytesPerSec / 20;
283 lBufferSize -= (lBufferSize % wfexCaptureFormat.nBlockAlign);
285 for (i=0;i<4;i++)
287 memset(&pData->WaveInBuffer[i], 0, sizeof(WAVEHDR));
288 pData->WaveInBuffer[i].dwBufferLength = lBufferSize;
289 pData->WaveInBuffer[i].lpData = calloc(1,pData->WaveInBuffer[i].dwBufferLength);
290 pData->WaveInBuffer[i].dwFlags = 0;
291 pData->WaveInBuffer[i].dwLoops = 0;
292 waveInPrepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
293 waveInAddBuffer(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
294 pData->lWaveInBuffersCommitted++;
297 pData->ulReadCapturedDataPos = 0;
298 pData->ulWriteCapturedDataPos = 0;
300 pDevice->ExtraData = pData;
302 pData->hWaveInThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CaptureThreadProc, (LPVOID)pDevice, 0, &pData->ulWaveInThreadID);
303 if (pData->hWaveInThread == NULL)
304 goto failure;
306 pDevice->szDeviceName = strdup(CaptureDeviceList[lDeviceID]);
307 return ALC_TRUE;
309 failure:
310 for (i=0;i<4;i++)
312 if(pData->WaveInBuffer[i].lpData)
314 waveInUnprepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
315 free(pData->WaveInBuffer[i].lpData);
319 free(pData->pCapturedSampleData);
320 if(pData->hWaveInHandle)
321 waveInClose(pData->hWaveInHandle);
322 if(pData->hWaveInThread)
323 CloseHandle(pData->hWaveInThread);
324 if (pData->hWaveInHdrEvent)
325 CloseHandle(pData->hWaveInHdrEvent);
326 if (pData->hWaveInThreadEvent)
327 CloseHandle(pData->hWaveInThreadEvent);
329 free(pData);
330 pDevice->ExtraData = NULL;
331 return ALC_FALSE;
334 static void WinMMCloseCapture(ALCdevice *pDevice)
336 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
337 int i;
339 // Call waveOutReset to shutdown wave device
340 pData->bWaveInShutdown = AL_TRUE;
341 waveInReset(pData->hWaveInHandle);
343 // Wait for signal that all Wave Buffers have returned
344 WaitForSingleObjectEx(pData->hWaveInHdrEvent, 5000, FALSE);
346 // Wait for signal that Wave Thread has been destroyed
347 WaitForSingleObjectEx(pData->hWaveInThreadEvent, 5000, FALSE);
349 // Release the wave buffers
350 for (i=0;i<4;i++)
352 waveInUnprepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
353 free(pData->WaveInBuffer[i].lpData);
356 // Free Audio Buffer data
357 free(pData->pCapturedSampleData);
358 pData->pCapturedSampleData = NULL;
360 // Close the Wave device
361 waveInClose(pData->hWaveInHandle);
362 pData->hWaveInHandle = 0;
364 CloseHandle(pData->hWaveInThread);
365 pData->hWaveInThread = 0;
367 if (pData->hWaveInHdrEvent)
369 CloseHandle(pData->hWaveInHdrEvent);
370 pData->hWaveInHdrEvent = 0;
373 if (pData->hWaveInThreadEvent)
375 CloseHandle(pData->hWaveInThreadEvent);
376 pData->hWaveInThreadEvent = 0;
379 free(pData);
380 pDevice->ExtraData = NULL;
383 static void WinMMStartCapture(ALCdevice *pDevice)
385 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
386 waveInStart(pData->hWaveInHandle);
389 static void WinMMStopCapture(ALCdevice *pDevice)
391 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
392 waveInStop(pData->hWaveInHandle);
395 static void WinMMCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
397 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
398 ALuint ulSamples = (unsigned long)lSamples;
399 ALuint ulBytes, ulBytesToCopy;
400 ALuint ulCapturedSamples;
401 ALuint ulReadOffset;
402 ALuint frameSize = aluBytesFromFormat(pDevice->Format) *
403 aluChannelsFromFormat(pDevice->Format);
405 // Check that we have the requested numbers of Samples
406 ulCapturedSamples = (pData->ulWriteCapturedDataPos -
407 pData->ulReadCapturedDataPos) /
408 frameSize;
409 if(ulSamples > ulCapturedSamples)
411 alcSetError(pDevice, ALC_INVALID_VALUE);
412 return;
415 ulBytes = ulSamples * frameSize;
417 // Get Read Offset
418 ulReadOffset = (pData->ulReadCapturedDataPos % pData->ulCapturedDataSize);
420 // Check for wrap-around condition
421 if ((ulReadOffset + ulBytes) > pData->ulCapturedDataSize)
423 // Copy data from last Read position to end of data
424 ulBytesToCopy = pData->ulCapturedDataSize - ulReadOffset;
425 memcpy(pBuffer, pData->pCapturedSampleData + ulReadOffset, ulBytesToCopy);
427 // Copy rest of the data from the start of the captured data
428 memcpy(((char *)pBuffer) + ulBytesToCopy, pData->pCapturedSampleData, ulBytes - ulBytesToCopy);
430 else
432 // Copy data from the read position in the captured data
433 memcpy(pBuffer, pData->pCapturedSampleData + ulReadOffset, ulBytes);
436 // Update Read Position
437 pData->ulReadCapturedDataPos += ulBytes;
440 static ALCuint WinMMAvailableSamples(ALCdevice *pDevice)
442 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
443 ALCuint lCapturedBytes = (pData->ulWriteCapturedDataPos - pData->ulReadCapturedDataPos);
444 return lCapturedBytes / (aluBytesFromFormat(pDevice->Format) *
445 aluChannelsFromFormat(pDevice->Format));
449 BackendFuncs WinMMFuncs = {
450 WinMMOpenPlayback,
451 WinMMClosePlayback,
452 NULL,
453 NULL,
454 WinMMOpenCapture,
455 WinMMCloseCapture,
456 WinMMStartCapture,
457 WinMMStopCapture,
458 WinMMCaptureSamples,
459 WinMMAvailableSamples
462 void alcWinMMInit(BackendFuncs *FuncList)
464 *FuncList = WinMMFuncs;
467 void alcWinMMDeinit()
469 ALuint lLoop;
471 for(lLoop = 0; lLoop < NumCaptureDevices; lLoop++)
472 free(CaptureDeviceList[lLoop]);
473 free(CaptureDeviceList);
474 CaptureDeviceList = NULL;
476 NumCaptureDevices = 0;
479 void alcWinMMProbe(int type)
481 ALuint i;
483 if(type != CAPTURE_DEVICE_PROBE)
484 return;
486 ProbeDevices();
487 for(i = 0;i < NumCaptureDevices;i++)
489 if(CaptureDeviceList[i])
490 AppendCaptureDeviceList(CaptureDeviceList[i]);