Add a method to deinitialize backends
[openal-soft.git] / Alc / winmm.c
blob9b16b9ebc12948036ca730e02615f460db142f26
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[16];
56 WaveInProc
58 Posts a message to 'CaptureThreadProc' everytime a WaveIn Buffer is completed and
59 returns to the application (with more data)
61 static void CALLBACK WaveInProc(HWAVEIN hDevice,UINT uMsg,DWORD_PTR dwInstance,DWORD_PTR dwParam1,DWORD_PTR dwParam2)
63 ALCdevice *pDevice = (ALCdevice *)dwInstance;
64 WinMMData *pData = pDevice->ExtraData;
66 (void)hDevice;
67 (void)dwParam2;
69 if ((uMsg==WIM_DATA))
71 // Decrement number of buffers in use
72 pData->lWaveInBuffersCommitted--;
74 if (pData->bWaveInShutdown == AL_FALSE)
76 // Notify Wave Processor Thread that a Wave Header has returned
77 PostThreadMessage(pData->ulWaveInThreadID,uMsg,0,dwParam1);
79 else
81 if (pData->lWaveInBuffersCommitted == 0)
83 // Signal Wave Buffers Returned event
84 if (pData->hWaveInHdrEvent)
85 SetEvent(pData->hWaveInHdrEvent);
87 // Post 'Quit' Message to WaveIn Processor Thread
88 PostThreadMessage(pData->ulWaveInThreadID,WM_QUIT,0,0);
95 CaptureThreadProc
97 Used by "MMSYSTEM" Device. Called when a WaveIn buffer had been filled with new
98 audio data.
100 DWORD WINAPI CaptureThreadProc(LPVOID lpParameter)
102 ALCdevice *pDevice = (ALCdevice*)lpParameter;
103 WinMMData *pData = pDevice->ExtraData;
104 ALuint ulOffset, ulMaxSize, ulSection;
105 LPWAVEHDR pWaveHdr;
106 MSG msg;
108 while (GetMessage(&msg, NULL, 0, 0))
110 if ((msg.message==WIM_DATA)&&(!pData->bWaveInShutdown))
112 SuspendContext(NULL);
114 pWaveHdr = ((LPWAVEHDR)msg.lParam);
116 // Calculate offset in local buffer to write data to
117 ulOffset = pData->ulWriteCapturedDataPos % pData->ulCapturedDataSize;
119 if ((ulOffset + pWaveHdr->dwBytesRecorded) > pData->ulCapturedDataSize)
121 ulSection = pData->ulCapturedDataSize - ulOffset;
122 memcpy(pData->pCapturedSampleData + ulOffset, pWaveHdr->lpData, ulSection);
123 memcpy(pData->pCapturedSampleData, pWaveHdr->lpData + ulSection, pWaveHdr->dwBytesRecorded - ulSection);
125 else
127 memcpy(pData->pCapturedSampleData + ulOffset, pWaveHdr->lpData, pWaveHdr->dwBytesRecorded);
130 pData->ulWriteCapturedDataPos += pWaveHdr->dwBytesRecorded;
132 if (pData->ulWriteCapturedDataPos > (pData->ulReadCapturedDataPos + pData->ulCapturedDataSize))
134 // Application has not read enough audio data from the capture buffer so data has been
135 // overwritten. Reset ReadPosition.
136 pData->ulReadCapturedDataPos = pData->ulWriteCapturedDataPos - pData->ulCapturedDataSize;
139 // To prevent an over-flow prevent the offset values from getting too large
140 ulMaxSize = pData->ulCapturedDataSize << 4;
141 if ((pData->ulReadCapturedDataPos > ulMaxSize) && (pData->ulWriteCapturedDataPos > ulMaxSize))
143 pData->ulReadCapturedDataPos -= ulMaxSize;
144 pData->ulWriteCapturedDataPos -= ulMaxSize;
147 // Send buffer back to capture more data
148 waveInAddBuffer(pData->hWaveInHandle,pWaveHdr,sizeof(WAVEHDR));
149 pData->lWaveInBuffersCommitted++;
151 ProcessContext(NULL);
155 // Signal Wave Thread completed event
156 if (pData->hWaveInThreadEvent)
157 SetEvent(pData->hWaveInThreadEvent);
159 ExitThread(0);
161 return 0;
165 static ALCboolean WinMMOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
167 (void)device;
168 (void)deviceName;
169 return ALC_FALSE;
172 static void WinMMClosePlayback(ALCdevice *device)
174 (void)device;
178 static ALCboolean WinMMOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName)
180 WAVEFORMATEX wfexCaptureFormat;
181 WinMMData *pData = NULL;
182 ALint lDeviceID = 0;
183 ALint lBufferSize;
184 ALint i;
186 // Find the Device ID matching the deviceName if valid
187 if (deviceName)
189 for(i = 0;CaptureDeviceList[i];i++)
191 if (!strcmp(deviceName, CaptureDeviceList[i]))
193 lDeviceID = i;
194 break;
197 if(!CaptureDeviceList[i])
198 return ALC_FALSE;
200 pDevice->szDeviceName = CaptureDeviceList[lDeviceID];
202 pData = calloc(1, sizeof(*pData));
203 if(!pData)
205 SetALCError(ALC_OUT_OF_MEMORY);
206 return ALC_FALSE;
209 memset(&wfexCaptureFormat, 0, sizeof(WAVEFORMATEX));
210 wfexCaptureFormat.wFormatTag = WAVE_FORMAT_PCM;
211 wfexCaptureFormat.nChannels = aluChannelsFromFormat(pDevice->Format);
212 wfexCaptureFormat.wBitsPerSample = aluBytesFromFormat(pDevice->Format) * 8;
213 wfexCaptureFormat.nBlockAlign = wfexCaptureFormat.wBitsPerSample *
214 wfexCaptureFormat.nChannels / 8;
215 wfexCaptureFormat.nSamplesPerSec = pDevice->Frequency;
216 wfexCaptureFormat.nAvgBytesPerSec = wfexCaptureFormat.nSamplesPerSec *
217 wfexCaptureFormat.nBlockAlign;
218 wfexCaptureFormat.cbSize = 0;
220 if (waveInOpen(&pData->hWaveInHandle, lDeviceID, &wfexCaptureFormat, (DWORD_PTR)&WaveInProc, (DWORD_PTR)pDevice, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
221 goto failure;
223 pData->hWaveInHdrEvent = CreateEvent(NULL, AL_TRUE, AL_FALSE, "WaveInAllHeadersReturned");
224 if (pData->hWaveInHdrEvent == NULL)
225 goto failure;
227 pData->hWaveInThreadEvent = CreateEvent(NULL, AL_TRUE, AL_FALSE, "WaveInThreadDestroyed");
228 if (pData->hWaveInThreadEvent == NULL)
229 goto failure;
231 // Allocate circular memory buffer for the captured audio
232 pData->ulCapturedDataSize = pDevice->BufferSize * wfexCaptureFormat.nBlockAlign;
234 // Make sure circular buffer is at least 100ms in size (and an exact multiple of
235 // the block alignment
236 if (pData->ulCapturedDataSize < (wfexCaptureFormat.nAvgBytesPerSec / 10))
238 pData->ulCapturedDataSize = wfexCaptureFormat.nAvgBytesPerSec / 10;
239 pData->ulCapturedDataSize -= (pData->ulCapturedDataSize % wfexCaptureFormat.nBlockAlign);
242 pData->pCapturedSampleData = (ALCchar*)malloc(pData->ulCapturedDataSize);
243 pData->lWaveInBuffersCommitted=0;
245 // Create 4 Buffers of 50ms each
246 lBufferSize = wfexCaptureFormat.nAvgBytesPerSec / 20;
247 lBufferSize -= (lBufferSize % wfexCaptureFormat.nBlockAlign);
249 for (i=0;i<4;i++)
251 memset(&pData->WaveInBuffer[i], 0, sizeof(WAVEHDR));
252 pData->WaveInBuffer[i].dwBufferLength = lBufferSize;
253 pData->WaveInBuffer[i].lpData = calloc(1,pData->WaveInBuffer[i].dwBufferLength);
254 pData->WaveInBuffer[i].dwFlags = 0;
255 pData->WaveInBuffer[i].dwLoops = 0;
256 waveInPrepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
257 waveInAddBuffer(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
258 pData->lWaveInBuffersCommitted++;
261 pData->ulReadCapturedDataPos = 0;
262 pData->ulWriteCapturedDataPos = 0;
264 pDevice->ExtraData = pData;
266 pData->hWaveInThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CaptureThreadProc, (LPVOID)pDevice, 0, &pData->ulWaveInThreadID);
267 if (pData->hWaveInThread == NULL)
268 goto failure;
270 return ALC_TRUE;
272 failure:
273 for (i=0;i<4;i++)
275 if(pData->WaveInBuffer[i].lpData)
277 waveInUnprepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
278 free(pData->WaveInBuffer[i].lpData);
282 free(pData->pCapturedSampleData);
283 if(pData->hWaveInHandle)
284 waveInClose(pData->hWaveInHandle);
285 if(pData->hWaveInThread)
286 CloseHandle(pData->hWaveInThread);
287 if (pData->hWaveInHdrEvent)
288 CloseHandle(pData->hWaveInHdrEvent);
289 if (pData->hWaveInThreadEvent)
290 CloseHandle(pData->hWaveInThreadEvent);
292 free(pData);
293 return ALC_FALSE;
296 static void WinMMCloseCapture(ALCdevice *pDevice)
298 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
299 int i;
301 // Call waveOutReset to shutdown wave device
302 pData->bWaveInShutdown = AL_TRUE;
303 waveInReset(pData->hWaveInHandle);
305 // Wait for signal that all Wave Buffers have returned
306 WaitForSingleObjectEx(pData->hWaveInHdrEvent, 5000, FALSE);
308 // Wait for signal that Wave Thread has been destroyed
309 WaitForSingleObjectEx(pData->hWaveInThreadEvent, 5000, FALSE);
311 // Release the wave buffers
312 for (i=0;i<4;i++)
314 waveInUnprepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
315 free(pData->WaveInBuffer[i].lpData);
318 // Free Audio Buffer data
319 free(pData->pCapturedSampleData);
320 pData->pCapturedSampleData = NULL;
322 // Close the Wave device
323 waveInClose(pData->hWaveInHandle);
324 pData->hWaveInHandle = 0;
326 CloseHandle(pData->hWaveInThread);
327 pData->hWaveInThread = 0;
329 if (pData->hWaveInHdrEvent)
331 CloseHandle(pData->hWaveInHdrEvent);
332 pData->hWaveInHdrEvent = 0;
335 if (pData->hWaveInThreadEvent)
337 CloseHandle(pData->hWaveInThreadEvent);
338 pData->hWaveInThreadEvent = 0;
341 free(pData);
342 pDevice->ExtraData = NULL;
345 static void WinMMStartCapture(ALCdevice *pDevice)
347 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
348 waveInStart(pData->hWaveInHandle);
351 static void WinMMStopCapture(ALCdevice *pDevice)
353 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
354 waveInStop(pData->hWaveInHandle);
357 static void WinMMCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
359 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
360 ALuint ulSamples = (unsigned long)lSamples;
361 ALuint ulBytes, ulBytesToCopy;
362 ALuint ulCapturedSamples;
363 ALuint ulReadOffset;
364 ALuint frameSize = aluBytesFromFormat(pDevice->Format) *
365 aluChannelsFromFormat(pDevice->Format);
367 // Check that we have the requested numbers of Samples
368 ulCapturedSamples = (pData->ulWriteCapturedDataPos -
369 pData->ulReadCapturedDataPos) /
370 frameSize;
371 if(ulSamples > ulCapturedSamples)
373 SetALCError(ALC_INVALID_VALUE);
374 return;
377 ulBytes = ulSamples * frameSize;
379 // Get Read Offset
380 ulReadOffset = (pData->ulReadCapturedDataPos % pData->ulCapturedDataSize);
382 // Check for wrap-around condition
383 if ((ulReadOffset + ulBytes) > pData->ulCapturedDataSize)
385 // Copy data from last Read position to end of data
386 ulBytesToCopy = pData->ulCapturedDataSize - ulReadOffset;
387 memcpy(pBuffer, pData->pCapturedSampleData + ulReadOffset, ulBytesToCopy);
389 // Copy rest of the data from the start of the captured data
390 memcpy(((char *)pBuffer) + ulBytesToCopy, pData->pCapturedSampleData, ulBytes - ulBytesToCopy);
392 else
394 // Copy data from the read position in the captured data
395 memcpy(pBuffer, pData->pCapturedSampleData + ulReadOffset, ulBytes);
398 // Update Read Position
399 pData->ulReadCapturedDataPos += ulBytes;
402 static ALCuint WinMMAvailableSamples(ALCdevice *pDevice)
404 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
405 ALCuint lCapturedBytes = (pData->ulWriteCapturedDataPos - pData->ulReadCapturedDataPos);
406 return lCapturedBytes / (aluBytesFromFormat(pDevice->Format) *
407 aluChannelsFromFormat(pDevice->Format));
411 BackendFuncs WinMMFuncs = {
412 WinMMOpenPlayback,
413 WinMMClosePlayback,
414 NULL,
415 NULL,
416 WinMMOpenCapture,
417 WinMMCloseCapture,
418 WinMMStartCapture,
419 WinMMStopCapture,
420 WinMMCaptureSamples,
421 WinMMAvailableSamples
424 void alcWinMMInit(BackendFuncs *FuncList)
426 ALint lNumDevs;
427 ALint lLoop;
429 *FuncList = WinMMFuncs;
431 lNumDevs = waveInGetNumDevs();
432 for (lLoop = 0; lLoop < lNumDevs; lLoop++)
434 WAVEINCAPS WaveInCaps;
435 if(waveInGetDevCaps(lLoop, &WaveInCaps, sizeof(WAVEINCAPS)) == MMSYSERR_NOERROR)
437 char name[128];
438 snprintf(name, sizeof(name), "WaveIn on %s", WaveInCaps.szPname);
439 CaptureDeviceList[lLoop] = AppendCaptureDeviceList(name);
444 void alcWinMMDeinit()