Fix comment regarding the 3rd echo tap
[openal-soft.git] / Alc / winmm.c
blob0261e3736e16100e146d52e4881d3e7d96a090f5
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, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
180 WAVEFORMATEX wfexCaptureFormat;
181 WinMMData *pData = NULL;
182 ALint lDeviceID = 0;
183 ALint lBufferSize;
184 ALint i;
186 (void)format;
188 // Find the Device ID matching the deviceName if valid
189 if (deviceName)
191 for(i = 0;CaptureDeviceList[i];i++)
193 if (!strcmp(deviceName, CaptureDeviceList[i]))
195 lDeviceID = i;
196 break;
199 if(!CaptureDeviceList[i])
200 return ALC_FALSE;
202 pDevice->szDeviceName = CaptureDeviceList[lDeviceID];
204 pData = calloc(1, sizeof(*pData));
205 if(!pData)
207 SetALCError(ALC_OUT_OF_MEMORY);
208 return ALC_FALSE;
211 memset(&wfexCaptureFormat, 0, sizeof(WAVEFORMATEX));
212 wfexCaptureFormat.wFormatTag = WAVE_FORMAT_PCM;
213 wfexCaptureFormat.nChannels = aluChannelsFromFormat(pDevice->Format);
214 wfexCaptureFormat.wBitsPerSample = aluBytesFromFormat(pDevice->Format) * 8;
215 wfexCaptureFormat.nBlockAlign = wfexCaptureFormat.wBitsPerSample *
216 wfexCaptureFormat.nChannels / 8;
217 wfexCaptureFormat.nSamplesPerSec = frequency;
218 wfexCaptureFormat.nAvgBytesPerSec = wfexCaptureFormat.nSamplesPerSec *
219 wfexCaptureFormat.nBlockAlign;
220 wfexCaptureFormat.cbSize = 0;
222 if (waveInOpen(&pData->hWaveInHandle, lDeviceID, &wfexCaptureFormat, (DWORD_PTR)&WaveInProc, (DWORD_PTR)pDevice, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
223 goto failure;
225 pData->hWaveInHdrEvent = CreateEvent(NULL, AL_TRUE, AL_FALSE, "WaveInAllHeadersReturned");
226 if (pData->hWaveInHdrEvent == NULL)
227 goto failure;
229 pData->hWaveInThreadEvent = CreateEvent(NULL, AL_TRUE, AL_FALSE, "WaveInThreadDestroyed");
230 if (pData->hWaveInThreadEvent == NULL)
231 goto failure;
233 // Allocate circular memory buffer for the captured audio
234 pData->ulCapturedDataSize = SampleSize * wfexCaptureFormat.nBlockAlign;
236 // Make sure circular buffer is at least 100ms in size (and an exact multiple of
237 // the block alignment
238 if (pData->ulCapturedDataSize < (wfexCaptureFormat.nAvgBytesPerSec / 10))
240 pData->ulCapturedDataSize = wfexCaptureFormat.nAvgBytesPerSec / 10;
241 pData->ulCapturedDataSize -= (pData->ulCapturedDataSize % wfexCaptureFormat.nBlockAlign);
244 pData->pCapturedSampleData = (ALCchar*)malloc(pData->ulCapturedDataSize);
245 pData->lWaveInBuffersCommitted=0;
247 // Create 4 Buffers of 50ms each
248 lBufferSize = wfexCaptureFormat.nAvgBytesPerSec / 20;
249 lBufferSize -= (lBufferSize % wfexCaptureFormat.nBlockAlign);
251 for (i=0;i<4;i++)
253 memset(&pData->WaveInBuffer[i], 0, sizeof(WAVEHDR));
254 pData->WaveInBuffer[i].dwBufferLength = lBufferSize;
255 pData->WaveInBuffer[i].lpData = calloc(1,pData->WaveInBuffer[i].dwBufferLength);
256 pData->WaveInBuffer[i].dwFlags = 0;
257 pData->WaveInBuffer[i].dwLoops = 0;
258 waveInPrepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
259 waveInAddBuffer(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
260 pData->lWaveInBuffersCommitted++;
263 pData->ulReadCapturedDataPos = 0;
264 pData->ulWriteCapturedDataPos = 0;
266 pDevice->ExtraData = pData;
268 pData->hWaveInThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CaptureThreadProc, (LPVOID)pDevice, 0, &pData->ulWaveInThreadID);
269 if (pData->hWaveInThread == NULL)
270 goto failure;
272 return ALC_TRUE;
274 failure:
275 for (i=0;i<4;i++)
277 if(pData->WaveInBuffer[i].lpData)
279 waveInUnprepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
280 free(pData->WaveInBuffer[i].lpData);
284 free(pData->pCapturedSampleData);
285 if(pData->hWaveInHandle)
286 waveInClose(pData->hWaveInHandle);
287 if(pData->hWaveInThread)
288 CloseHandle(pData->hWaveInThread);
289 if (pData->hWaveInHdrEvent)
290 CloseHandle(pData->hWaveInHdrEvent);
291 if (pData->hWaveInThreadEvent)
292 CloseHandle(pData->hWaveInThreadEvent);
294 free(pData);
295 return ALC_FALSE;
298 static void WinMMCloseCapture(ALCdevice *pDevice)
300 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
301 int i;
303 // Call waveOutReset to shutdown wave device
304 pData->bWaveInShutdown = AL_TRUE;
305 waveInReset(pData->hWaveInHandle);
307 // Wait for signal that all Wave Buffers have returned
308 WaitForSingleObjectEx(pData->hWaveInHdrEvent, 5000, FALSE);
310 // Wait for signal that Wave Thread has been destroyed
311 WaitForSingleObjectEx(pData->hWaveInThreadEvent, 5000, FALSE);
313 // Release the wave buffers
314 for (i=0;i<4;i++)
316 waveInUnprepareHeader(pData->hWaveInHandle, &pData->WaveInBuffer[i], sizeof(WAVEHDR));
317 free(pData->WaveInBuffer[i].lpData);
320 // Free Audio Buffer data
321 free(pData->pCapturedSampleData);
322 pData->pCapturedSampleData = NULL;
324 // Close the Wave device
325 waveInClose(pData->hWaveInHandle);
326 pData->hWaveInHandle = 0;
328 CloseHandle(pData->hWaveInThread);
329 pData->hWaveInThread = 0;
331 if (pData->hWaveInHdrEvent)
333 CloseHandle(pData->hWaveInHdrEvent);
334 pData->hWaveInHdrEvent = 0;
337 if (pData->hWaveInThreadEvent)
339 CloseHandle(pData->hWaveInThreadEvent);
340 pData->hWaveInThreadEvent = 0;
343 free(pData);
344 pDevice->ExtraData = NULL;
347 static void WinMMStartCapture(ALCdevice *pDevice)
349 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
350 waveInStart(pData->hWaveInHandle);
353 static void WinMMStopCapture(ALCdevice *pDevice)
355 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
356 waveInStop(pData->hWaveInHandle);
359 static void WinMMCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
361 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
362 ALuint ulSamples = (unsigned long)lSamples;
363 ALuint ulBytes, ulBytesToCopy;
364 ALuint ulCapturedSamples;
365 ALuint ulReadOffset;
366 ALuint frameSize = aluBytesFromFormat(pDevice->Format) *
367 aluChannelsFromFormat(pDevice->Format);
369 // Check that we have the requested numbers of Samples
370 ulCapturedSamples = (pData->ulWriteCapturedDataPos -
371 pData->ulReadCapturedDataPos) /
372 frameSize;
373 if(ulSamples > ulCapturedSamples)
375 SetALCError(ALC_INVALID_VALUE);
376 return;
379 ulBytes = ulSamples * frameSize;
381 // Get Read Offset
382 ulReadOffset = (pData->ulReadCapturedDataPos % pData->ulCapturedDataSize);
384 // Check for wrap-around condition
385 if ((ulReadOffset + ulBytes) > pData->ulCapturedDataSize)
387 // Copy data from last Read position to end of data
388 ulBytesToCopy = pData->ulCapturedDataSize - ulReadOffset;
389 memcpy(pBuffer, pData->pCapturedSampleData + ulReadOffset, ulBytesToCopy);
391 // Copy rest of the data from the start of the captured data
392 memcpy(((char *)pBuffer) + ulBytesToCopy, pData->pCapturedSampleData, ulBytes - ulBytesToCopy);
394 else
396 // Copy data from the read position in the captured data
397 memcpy(pBuffer, pData->pCapturedSampleData + ulReadOffset, ulBytes);
400 // Update Read Position
401 pData->ulReadCapturedDataPos += ulBytes;
404 static ALCuint WinMMAvailableSamples(ALCdevice *pDevice)
406 WinMMData *pData = (WinMMData*)pDevice->ExtraData;
407 ALCuint lCapturedBytes = (pData->ulWriteCapturedDataPos - pData->ulReadCapturedDataPos);
408 return lCapturedBytes / (aluBytesFromFormat(pDevice->Format) *
409 aluChannelsFromFormat(pDevice->Format));
413 BackendFuncs WinMMFuncs = {
414 WinMMOpenPlayback,
415 WinMMClosePlayback,
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);