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
23 #define _WIN32_WINNT 0x0500
37 // MMSYSTEM Capture Device
38 ALboolean bWaveInShutdown
;
39 HANDLE hWaveInHdrEvent
;
40 HANDLE hWaveInThreadEvent
;
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
;
53 static ALCchar
**CaptureDeviceList
;
54 static ALuint NumCaptureDevices
;
56 static void ProbeDevices(void)
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
)
73 snprintf(name
, sizeof(name
), "WaveIn on %s", WaveInCaps
.szPname
);
74 CaptureDeviceList
[i
] = strdup(name
);
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
;
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
);
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);
121 Used by "MMSYSTEM" Device. Called when a WaveIn buffer had been filled with new
124 DWORD WINAPI
CaptureThreadProc(LPVOID lpParameter
)
126 ALCdevice
*pDevice
= (ALCdevice
*)lpParameter
;
127 WinMMData
*pData
= pDevice
->ExtraData
;
128 ALuint ulOffset
, ulMaxSize
, ulSection
;
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
);
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
);
189 static ALCboolean
WinMMOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
196 static void WinMMClosePlayback(ALCdevice
*device
)
202 static ALCboolean
WinMMOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
204 WAVEFORMATEX wfexCaptureFormat
;
205 WinMMData
*pData
= NULL
;
210 // Find the Device ID matching the deviceName if valid
213 for(i
= 0;i
< NumCaptureDevices
;i
++)
215 if(CaptureDeviceList
[i
] &&
216 strcmp(deviceName
, CaptureDeviceList
[i
]) == 0)
225 for(i
= 0;i
< NumCaptureDevices
;i
++)
227 if(CaptureDeviceList
[i
])
234 if(i
== NumCaptureDevices
)
237 pData
= calloc(1, sizeof(*pData
));
240 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
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
)
258 pData
->hWaveInHdrEvent
= CreateEvent(NULL
, AL_TRUE
, AL_FALSE
, "WaveInAllHeadersReturned");
259 if (pData
->hWaveInHdrEvent
== NULL
)
262 pData
->hWaveInThreadEvent
= CreateEvent(NULL
, AL_TRUE
, AL_FALSE
, "WaveInThreadDestroyed");
263 if (pData
->hWaveInThreadEvent
== NULL
)
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
);
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
)
306 pDevice
->szDeviceName
= strdup(CaptureDeviceList
[lDeviceID
]);
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
);
330 pDevice
->ExtraData
= NULL
;
334 static void WinMMCloseCapture(ALCdevice
*pDevice
)
336 WinMMData
*pData
= (WinMMData
*)pDevice
->ExtraData
;
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
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;
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
;
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
) /
409 if(ulSamples
> ulCapturedSamples
)
411 alcSetError(pDevice
, ALC_INVALID_VALUE
);
415 ulBytes
= ulSamples
* frameSize
;
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
);
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
= {
459 WinMMAvailableSamples
462 void alcWinMMInit(BackendFuncs
*FuncList
)
464 *FuncList
= WinMMFuncs
;
467 void alcWinMMDeinit()
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
)
483 if(type
!= CAPTURE_DEVICE_PROBE
)
487 for(i
= 0;i
< NumCaptureDevices
;i
++)
489 if(CaptureDeviceList
[i
])
490 AppendCaptureDeviceList(CaptureDeviceList
[i
]);