Simplifiy verification loops
[openal-soft.git] / Alc / dsound.c
blob7894585d92de2f1490ac4e0985c4ecc185e57a89
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 #define INITGUID
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <memory.h>
29 #include <dsound.h>
30 #include <mmreg.h>
31 #ifndef _WAVEFORMATEXTENSIBLE_
32 #include <ks.h>
33 #include <ksmedia.h>
34 #endif
36 #include "alMain.h"
37 #include "AL/al.h"
38 #include "AL/alc.h"
40 #ifndef DSSPEAKER_5POINT1
41 #define DSSPEAKER_5POINT1 6
42 #endif
43 #ifndef DSSPEAKER_7POINT1
44 #define DSSPEAKER_7POINT1 7
45 #endif
47 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
48 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
50 static void *ds_handle;
51 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter);
52 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA pDSEnumCallback, LPVOID pContext);
55 typedef struct {
56 // DirectSound Playback Device
57 LPDIRECTSOUND lpDS;
58 LPDIRECTSOUNDBUFFER DSpbuffer;
59 LPDIRECTSOUNDBUFFER DSsbuffer;
61 volatile int killNow;
62 ALvoid *thread;
63 } DSoundData;
66 typedef struct {
67 ALCchar *name;
68 GUID guid;
69 } DevMap;
71 static const ALCchar dsDevice[] = "DirectSound Software";
72 static DevMap *DeviceList;
73 static ALuint NumDevices;
74 static volatile ALuint load_count;
77 void *DSoundLoad(void)
79 if(load_count == 0)
81 #ifdef _WIN32
82 ds_handle = LoadLibraryA("dsound.dll");
83 if(ds_handle == NULL)
85 AL_PRINT("Failed to load dsound.dll\n");
86 return NULL;
89 #define LOAD_FUNC(f) do { \
90 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
91 if(p##f == NULL) \
92 { \
93 FreeLibrary(ds_handle); \
94 ds_handle = NULL; \
95 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
96 return NULL; \
97 } \
98 } while(0)
99 #else
100 ds_handle = (void*)0xDEADBEEF;
101 #define LOAD_FUNC(f) p##f = f
102 #endif
104 LOAD_FUNC(DirectSoundCreate);
105 LOAD_FUNC(DirectSoundEnumerateA);
106 #undef LOAD_FUNC
108 ++load_count;
110 return ds_handle;
113 void DSoundUnload(void)
115 if(load_count == 0 || --load_count > 0)
116 return;
118 #ifdef _WIN32
119 FreeLibrary(ds_handle);
120 #endif
121 ds_handle = NULL;
125 static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCSTR desc, LPCSTR drvname, LPVOID data)
127 (void)data;
128 (void)drvname;
130 if(guid)
132 char str[128];
133 void *temp;
135 temp = realloc(DeviceList, sizeof(DevMap) * (NumDevices+1));
136 if(temp)
138 DeviceList = temp;
140 snprintf(str, sizeof(str), "DirectSound Software on %s", desc);
142 DeviceList[NumDevices].name = strdup(str);
143 DeviceList[NumDevices].guid = *guid;
144 NumDevices++;
148 return TRUE;
152 static ALuint DSoundProc(ALvoid *ptr)
154 ALCdevice *pDevice = (ALCdevice*)ptr;
155 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
156 DSBCAPS DSBCaps;
157 DWORD LastCursor = 0;
158 DWORD PlayCursor;
159 VOID *WritePtr1, *WritePtr2;
160 DWORD WriteCnt1, WriteCnt2;
161 DWORD FrameSize;
162 DWORD FragSize;
163 DWORD avail;
164 HRESULT err;
166 EnableRTPrio(RTPrioLevel);
168 memset(&DSBCaps, 0, sizeof(DSBCaps));
169 DSBCaps.dwSize = sizeof(DSBCaps);
170 err = IDirectSoundBuffer_GetCaps(pData->DSsbuffer, &DSBCaps);
171 if(FAILED(err))
173 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err);
174 aluHandleDisconnect(pDevice);
175 return 1;
178 FrameSize = aluChannelsFromFormat(pDevice->Format) *
179 aluBytesFromFormat(pDevice->Format);
180 FragSize = pDevice->UpdateSize * FrameSize;
182 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &LastCursor, NULL);
183 while(!pData->killNow)
185 // Get current play and write cursors
186 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
187 avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes;
189 if(avail < FragSize)
191 Sleep(1);
192 continue;
194 avail -= avail%FragSize;
196 // Lock output buffer
197 WriteCnt1 = 0;
198 WriteCnt2 = 0;
199 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
201 // If the buffer is lost, restore it, play and lock
202 if(err == DSERR_BUFFERLOST)
204 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
205 if(SUCCEEDED(err))
206 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
207 if(SUCCEEDED(err))
208 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
211 // Successfully locked the output buffer
212 if(SUCCEEDED(err))
214 // If we have an active context, mix data directly into output buffer otherwise fill with silence
215 aluMixData(pDevice, WritePtr1, WriteCnt1/FrameSize);
216 aluMixData(pDevice, WritePtr2, WriteCnt2/FrameSize);
218 // Unlock output buffer only when successfully locked
219 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
221 else
222 AL_PRINT("Buffer lock error: %#lx\n", err);
224 // Update old write cursor location
225 LastCursor += WriteCnt1+WriteCnt2;
226 LastCursor %= DSBCaps.dwBufferBytes;
229 return 0;
232 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
234 DSoundData *pData = NULL;
235 LPGUID guid = NULL;
236 HRESULT hr;
238 if(!DSoundLoad())
239 return ALC_FALSE;
241 if(!deviceName)
242 deviceName = dsDevice;
243 else if(strcmp(deviceName, dsDevice) != 0)
245 ALuint i;
247 if(!DeviceList)
249 hr = pDirectSoundEnumerateA(DSoundEnumDevices, NULL);
250 if(FAILED(hr))
251 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);
254 for(i = 0;i < NumDevices;i++)
256 if(strcmp(deviceName, DeviceList[i].name) == 0)
258 guid = &DeviceList[i].guid;
259 break;
262 if(i == NumDevices)
264 DSoundUnload();
265 return ALC_FALSE;
269 //Initialise requested device
270 pData = calloc(1, sizeof(DSoundData));
271 if(!pData)
273 alcSetError(device, ALC_OUT_OF_MEMORY);
274 DSoundUnload();
275 return ALC_FALSE;
278 //DirectSound Init code
279 hr = pDirectSoundCreate(guid, &pData->lpDS, NULL);
280 if(SUCCEEDED(hr))
281 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
282 if(FAILED(hr))
284 if(pData->lpDS)
285 IDirectSound_Release(pData->lpDS);
286 free(pData);
287 DSoundUnload();
288 return ALC_FALSE;
291 device->szDeviceName = strdup(deviceName);
292 device->ExtraData = pData;
293 return ALC_TRUE;
296 static void DSoundClosePlayback(ALCdevice *device)
298 DSoundData *pData = device->ExtraData;
300 IDirectSound_Release(pData->lpDS);
301 free(pData);
302 device->ExtraData = NULL;
304 DSoundUnload();
307 static ALCboolean DSoundResetPlayback(ALCdevice *device)
309 DSoundData *pData = (DSoundData*)device->ExtraData;
310 DSBUFFERDESC DSBDescription;
311 WAVEFORMATEXTENSIBLE OutputType;
312 DWORD frameSize = 0;
313 ALenum format = 0;
314 DWORD speakers;
315 HRESULT hr;
317 memset(&OutputType, 0, sizeof(OutputType));
319 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
320 if(SUCCEEDED(hr) && ConfigValueExists(NULL, "format"))
322 if(aluChannelsFromFormat(device->Format) == 1)
323 speakers = DSSPEAKER_COMBINED(DSSPEAKER_MONO, 0);
324 else if(aluChannelsFromFormat(device->Format) == 2)
325 speakers = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, 0);
326 else if(aluChannelsFromFormat(device->Format) == 4)
327 speakers = DSSPEAKER_COMBINED(DSSPEAKER_QUAD, 0);
328 else if(aluChannelsFromFormat(device->Format) == 6)
329 speakers = DSSPEAKER_COMBINED(DSSPEAKER_5POINT1, 0);
330 else if(aluChannelsFromFormat(device->Format) == 8)
331 speakers = DSSPEAKER_COMBINED(DSSPEAKER_7POINT1, 0);
332 else
334 AL_PRINT("Unknown format: 0x%x\n", device->Format);
335 return ALC_FALSE;
338 if(SUCCEEDED(hr))
340 speakers = DSSPEAKER_CONFIG(speakers);
341 if(speakers == DSSPEAKER_MONO)
343 if(aluBytesFromFormat(device->Format) == 1)
344 format = AL_FORMAT_MONO8;
345 else if(aluBytesFromFormat(device->Format) == 2)
346 format = AL_FORMAT_MONO16;
347 else if(aluBytesFromFormat(device->Format) == 4)
348 format = AL_FORMAT_MONO_FLOAT32;
350 else if(speakers == DSSPEAKER_STEREO)
352 if(aluBytesFromFormat(device->Format) == 1)
353 format = AL_FORMAT_STEREO8;
354 else if(aluBytesFromFormat(device->Format) == 2)
355 format = AL_FORMAT_STEREO16;
356 else if(aluBytesFromFormat(device->Format) == 4)
357 format = AL_FORMAT_STEREO_FLOAT32;
359 else if(speakers == DSSPEAKER_QUAD)
361 if(aluBytesFromFormat(device->Format) == 1)
362 format = AL_FORMAT_QUAD8;
363 else if(aluBytesFromFormat(device->Format) == 2)
364 format = AL_FORMAT_QUAD16;
365 else if(aluBytesFromFormat(device->Format) == 4)
366 format = AL_FORMAT_QUAD32;
367 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
368 SPEAKER_FRONT_RIGHT |
369 SPEAKER_BACK_LEFT |
370 SPEAKER_BACK_RIGHT;
372 else if(speakers == DSSPEAKER_5POINT1)
374 if(aluBytesFromFormat(device->Format) == 1)
375 format = AL_FORMAT_51CHN8;
376 else if(aluBytesFromFormat(device->Format) == 2)
377 format = AL_FORMAT_51CHN16;
378 else if(aluBytesFromFormat(device->Format) == 4)
379 format = AL_FORMAT_51CHN32;
380 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
381 SPEAKER_FRONT_RIGHT |
382 SPEAKER_FRONT_CENTER |
383 SPEAKER_LOW_FREQUENCY |
384 SPEAKER_BACK_LEFT |
385 SPEAKER_BACK_RIGHT;
387 else if(speakers == DSSPEAKER_7POINT1)
389 if(aluBytesFromFormat(device->Format) == 1)
390 format = AL_FORMAT_71CHN8;
391 else if(aluBytesFromFormat(device->Format) == 2)
392 format = AL_FORMAT_71CHN16;
393 else if(aluBytesFromFormat(device->Format) == 4)
394 format = AL_FORMAT_71CHN32;
395 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
396 SPEAKER_FRONT_RIGHT |
397 SPEAKER_FRONT_CENTER |
398 SPEAKER_LOW_FREQUENCY |
399 SPEAKER_BACK_LEFT |
400 SPEAKER_BACK_RIGHT |
401 SPEAKER_SIDE_LEFT |
402 SPEAKER_SIDE_RIGHT;
404 else
405 format = device->Format;
406 frameSize = aluBytesFromFormat(format) * aluChannelsFromFormat(format);
408 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
409 OutputType.Format.nChannels = aluChannelsFromFormat(format);
410 OutputType.Format.wBitsPerSample = aluBytesFromFormat(format) * 8;
411 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
412 OutputType.Format.nSamplesPerSec = device->Frequency;
413 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
414 OutputType.Format.cbSize = 0;
417 if(OutputType.Format.nChannels > 2 || OutputType.Format.wBitsPerSample > 16)
419 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
420 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
421 OutputType.Format.cbSize = 22;
422 if(OutputType.Format.wBitsPerSample == 32)
423 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
424 else
425 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
427 else
429 if(SUCCEEDED(hr))
431 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
432 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
433 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
434 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
436 if(SUCCEEDED(hr))
437 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
440 if(SUCCEEDED(hr))
442 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
443 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
444 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
445 DSBDescription.dwBufferBytes=device->UpdateSize * device->NumUpdates * frameSize;
446 DSBDescription.lpwfxFormat=&OutputType.Format;
447 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
450 if(SUCCEEDED(hr))
451 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
453 if(SUCCEEDED(hr))
455 device->Format = format;
456 SetDefaultWFXChannelOrder(device);
457 pData->thread = StartThread(DSoundProc, device);
458 if(!pData->thread)
459 hr = E_FAIL;
462 if(FAILED(hr))
464 if (pData->DSsbuffer)
465 IDirectSoundBuffer_Release(pData->DSsbuffer);
466 pData->DSsbuffer = NULL;
467 if (pData->DSpbuffer)
468 IDirectSoundBuffer_Release(pData->DSpbuffer);
469 pData->DSpbuffer = NULL;
470 return ALC_FALSE;
473 return ALC_TRUE;
476 static void DSoundStopPlayback(ALCdevice *device)
478 DSoundData *pData = device->ExtraData;
480 if(!pData->thread)
481 return;
483 pData->killNow = 1;
484 StopThread(pData->thread);
485 pData->thread = NULL;
487 pData->killNow = 0;
489 IDirectSoundBuffer_Release(pData->DSsbuffer);
490 pData->DSsbuffer = NULL;
491 if (pData->DSpbuffer)
492 IDirectSoundBuffer_Release(pData->DSpbuffer);
493 pData->DSpbuffer = NULL;
497 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName)
499 (void)pDevice;
500 (void)deviceName;
501 return ALC_FALSE;
504 static void DSoundCloseCapture(ALCdevice *pDevice)
506 (void)pDevice;
509 static void DSoundStartCapture(ALCdevice *pDevice)
511 (void)pDevice;
514 static void DSoundStopCapture(ALCdevice *pDevice)
516 (void)pDevice;
519 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
521 (void)pDevice;
522 (void)pBuffer;
523 (void)lSamples;
526 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
528 (void)pDevice;
529 return 0;
533 BackendFuncs DSoundFuncs = {
534 DSoundOpenPlayback,
535 DSoundClosePlayback,
536 DSoundResetPlayback,
537 DSoundStopPlayback,
538 DSoundOpenCapture,
539 DSoundCloseCapture,
540 DSoundStartCapture,
541 DSoundStopCapture,
542 DSoundCaptureSamples,
543 DSoundAvailableSamples
547 void alcDSoundInit(BackendFuncs *FuncList)
549 *FuncList = DSoundFuncs;
552 void alcDSoundDeinit(void)
554 ALuint i;
556 for(i = 0;i < NumDevices;++i)
557 free(DeviceList[i].name);
558 free(DeviceList);
559 DeviceList = NULL;
560 NumDevices = 0;
563 void alcDSoundProbe(int type)
565 if(!DSoundLoad()) return;
567 if(type == DEVICE_PROBE)
568 AppendDeviceList(dsDevice);
569 else if(type == ALL_DEVICE_PROBE)
571 HRESULT hr;
572 ALuint i;
574 for(i = 0;i < NumDevices;++i)
575 free(DeviceList[i].name);
576 free(DeviceList);
577 DeviceList = NULL;
578 NumDevices = 0;
580 hr = pDirectSoundEnumerateA(DSoundEnumDevices, NULL);
581 if(FAILED(hr))
582 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);
583 else
585 for(i = 0;i < NumDevices;i++)
586 AppendAllDeviceList(DeviceList[i].name);
590 DSoundUnload();