Use "internal" visibility by default
[openal-soft.git] / Alc / dsound.c
blobac7782b9c94e0a141ea27d73aeae25ffc928b093
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 Default";
72 static DevMap *DeviceList;
73 static ALuint NumDevices;
76 void *DSoundLoad(void)
78 if(!ds_handle)
80 #ifdef _WIN32
81 ds_handle = LoadLibraryA("dsound.dll");
82 if(ds_handle == NULL)
84 AL_PRINT("Failed to load dsound.dll\n");
85 return NULL;
88 #define LOAD_FUNC(f) do { \
89 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
90 if(p##f == NULL) \
91 { \
92 FreeLibrary(ds_handle); \
93 ds_handle = NULL; \
94 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
95 return NULL; \
96 } \
97 } while(0)
98 #else
99 ds_handle = (void*)0xDEADBEEF;
100 #define LOAD_FUNC(f) p##f = f
101 #endif
103 LOAD_FUNC(DirectSoundCreate);
104 LOAD_FUNC(DirectSoundEnumerateA);
105 #undef LOAD_FUNC
107 return ds_handle;
111 static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCSTR desc, LPCSTR drvname, LPVOID data)
113 char str[1024];
114 void *temp;
115 int count;
116 ALuint i;
118 (void)data;
119 (void)drvname;
121 if(NumDevices == 0)
123 temp = realloc(DeviceList, sizeof(DevMap) * (NumDevices+1));
124 if(temp)
126 DeviceList = temp;
127 DeviceList[NumDevices].name = strdup(dsDevice);
128 DeviceList[NumDevices].guid = GUID_NULL;
129 NumDevices++;
133 if(!guid)
134 return TRUE;
136 count = 0;
137 do {
138 if(count == 0)
139 snprintf(str, sizeof(str), "%s via DirectSound", desc);
140 else
141 snprintf(str, sizeof(str), "%s #%d via DirectSound", desc, count+1);
142 count++;
144 for(i = 0;i < NumDevices;i++)
146 if(strcmp(str, DeviceList[i].name) == 0)
147 break;
149 } while(i != NumDevices);
151 temp = realloc(DeviceList, sizeof(DevMap) * (NumDevices+1));
152 if(temp)
154 DeviceList = temp;
155 DeviceList[NumDevices].name = strdup(str);
156 DeviceList[NumDevices].guid = *guid;
157 NumDevices++;
160 return TRUE;
164 static ALuint DSoundProc(ALvoid *ptr)
166 ALCdevice *pDevice = (ALCdevice*)ptr;
167 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
168 DSBCAPS DSBCaps;
169 DWORD LastCursor = 0;
170 DWORD PlayCursor;
171 VOID *WritePtr1, *WritePtr2;
172 DWORD WriteCnt1, WriteCnt2;
173 BOOL Playing = FALSE;
174 DWORD FrameSize;
175 DWORD FragSize;
176 DWORD avail;
177 HRESULT err;
179 SetRTPriority();
181 memset(&DSBCaps, 0, sizeof(DSBCaps));
182 DSBCaps.dwSize = sizeof(DSBCaps);
183 err = IDirectSoundBuffer_GetCaps(pData->DSsbuffer, &DSBCaps);
184 if(FAILED(err))
186 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err);
187 aluHandleDisconnect(pDevice);
188 return 1;
191 FrameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType);
192 FragSize = pDevice->UpdateSize * FrameSize;
194 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &LastCursor, NULL);
195 while(!pData->killNow)
197 // Get current play and write cursors
198 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
199 avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes;
201 if(avail < FragSize)
203 if(!Playing)
205 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
206 if(FAILED(err))
208 AL_PRINT("Failed to play buffer: 0x%lx\n", err);
209 aluHandleDisconnect(pDevice);
210 return 1;
212 Playing = TRUE;
214 Sleep(1);
215 continue;
217 avail -= avail%FragSize;
219 // Lock output buffer
220 WriteCnt1 = 0;
221 WriteCnt2 = 0;
222 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
224 // If the buffer is lost, restore it and lock
225 if(err == DSERR_BUFFERLOST)
227 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
228 if(SUCCEEDED(err))
230 Playing = FALSE;
231 LastCursor = 0;
232 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, 0, DSBCaps.dwBufferBytes, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
236 // Successfully locked the output buffer
237 if(SUCCEEDED(err))
239 // If we have an active context, mix data directly into output buffer otherwise fill with silence
240 aluMixData(pDevice, WritePtr1, WriteCnt1/FrameSize);
241 aluMixData(pDevice, WritePtr2, WriteCnt2/FrameSize);
243 // Unlock output buffer only when successfully locked
244 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
246 else
248 AL_PRINT("Buffer lock error: %#lx\n", err);
249 aluHandleDisconnect(pDevice);
250 return 1;
253 // Update old write cursor location
254 LastCursor += WriteCnt1+WriteCnt2;
255 LastCursor %= DSBCaps.dwBufferBytes;
258 return 0;
261 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
263 DSoundData *pData = NULL;
264 LPGUID guid = NULL;
265 HRESULT hr;
267 if(!DSoundLoad())
268 return ALC_FALSE;
270 if(!deviceName)
271 deviceName = dsDevice;
272 else if(strcmp(deviceName, dsDevice) != 0)
274 ALuint i;
276 if(!DeviceList)
278 hr = pDirectSoundEnumerateA(DSoundEnumDevices, NULL);
279 if(FAILED(hr))
280 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);
283 for(i = 0;i < NumDevices;i++)
285 if(strcmp(deviceName, DeviceList[i].name) == 0)
287 if(i > 0)
288 guid = &DeviceList[i].guid;
289 break;
292 if(i == NumDevices)
293 return ALC_FALSE;
296 //Initialise requested device
297 pData = calloc(1, sizeof(DSoundData));
298 if(!pData)
300 alcSetError(device, ALC_OUT_OF_MEMORY);
301 return ALC_FALSE;
304 //DirectSound Init code
305 hr = pDirectSoundCreate(guid, &pData->lpDS, NULL);
306 if(SUCCEEDED(hr))
307 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
308 if(FAILED(hr))
310 if(pData->lpDS)
311 IDirectSound_Release(pData->lpDS);
312 free(pData);
313 AL_PRINT("Device init failed: 0x%08lx\n", hr);
314 return ALC_FALSE;
317 device->szDeviceName = strdup(deviceName);
318 device->ExtraData = pData;
319 return ALC_TRUE;
322 static void DSoundClosePlayback(ALCdevice *device)
324 DSoundData *pData = device->ExtraData;
326 IDirectSound_Release(pData->lpDS);
327 free(pData);
328 device->ExtraData = NULL;
331 static ALCboolean DSoundResetPlayback(ALCdevice *device)
333 DSoundData *pData = (DSoundData*)device->ExtraData;
334 DSBUFFERDESC DSBDescription;
335 WAVEFORMATEXTENSIBLE OutputType;
336 DWORD speakers;
337 HRESULT hr;
339 memset(&OutputType, 0, sizeof(OutputType));
341 switch(device->FmtType)
343 case DevFmtByte:
344 device->FmtType = DevFmtUByte;
345 break;
346 case DevFmtUShort:
347 device->FmtType = DevFmtShort;
348 break;
349 case DevFmtUByte:
350 case DevFmtShort:
351 case DevFmtFloat:
352 break;
355 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
356 if(SUCCEEDED(hr) && ConfigValueExists(NULL, "format"))
358 switch(device->FmtChans)
360 case DevFmtMono:
361 speakers = DSSPEAKER_COMBINED(DSSPEAKER_MONO, 0);
362 break;
363 case DevFmtStereo:
364 speakers = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, 0);
365 break;
366 case DevFmtQuad:
367 speakers = DSSPEAKER_COMBINED(DSSPEAKER_QUAD, 0);
368 break;
369 case DevFmtX51:
370 speakers = DSSPEAKER_COMBINED(DSSPEAKER_5POINT1, 0);
371 break;
372 case DevFmtX61:
373 /* ??? */;
374 break;
375 case DevFmtX71:
376 speakers = DSSPEAKER_COMBINED(DSSPEAKER_7POINT1, 0);
377 break;
380 if(SUCCEEDED(hr))
382 speakers = DSSPEAKER_CONFIG(speakers);
383 if(speakers == DSSPEAKER_MONO)
385 device->FmtChans = DevFmtMono;
386 OutputType.dwChannelMask = SPEAKER_FRONT_CENTER;
388 else if(speakers == DSSPEAKER_STEREO || speakers == DSSPEAKER_HEADPHONE)
390 device->FmtChans = DevFmtStereo;
391 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
392 SPEAKER_FRONT_RIGHT;
394 else if(speakers == DSSPEAKER_QUAD)
396 device->FmtChans = DevFmtQuad;
397 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
398 SPEAKER_FRONT_RIGHT |
399 SPEAKER_BACK_LEFT |
400 SPEAKER_BACK_RIGHT;
402 else if(speakers == DSSPEAKER_5POINT1)
404 device->FmtChans = DevFmtX51;
405 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
406 SPEAKER_FRONT_RIGHT |
407 SPEAKER_FRONT_CENTER |
408 SPEAKER_LOW_FREQUENCY |
409 SPEAKER_BACK_LEFT |
410 SPEAKER_BACK_RIGHT;
412 else if(speakers == DSSPEAKER_7POINT1)
414 device->FmtChans = DevFmtX71;
415 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
416 SPEAKER_FRONT_RIGHT |
417 SPEAKER_FRONT_CENTER |
418 SPEAKER_LOW_FREQUENCY |
419 SPEAKER_BACK_LEFT |
420 SPEAKER_BACK_RIGHT |
421 SPEAKER_SIDE_LEFT |
422 SPEAKER_SIDE_RIGHT;
425 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
426 OutputType.Format.nChannels = ChannelsFromDevFmt(device->FmtChans);
427 OutputType.Format.wBitsPerSample = BytesFromDevFmt(device->FmtType) * 8;
428 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
429 OutputType.Format.nSamplesPerSec = device->Frequency;
430 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
431 OutputType.Format.cbSize = 0;
434 if(OutputType.Format.nChannels > 2 || OutputType.Format.wBitsPerSample > 16)
436 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
437 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
438 OutputType.Format.cbSize = 22;
439 if(OutputType.Format.wBitsPerSample == 32)
440 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
441 else
442 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
444 else
446 if(SUCCEEDED(hr))
448 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
449 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
450 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
451 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
453 if(SUCCEEDED(hr))
454 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
457 if(SUCCEEDED(hr))
459 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
460 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
461 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
462 DSBDescription.dwBufferBytes=device->UpdateSize * device->NumUpdates *
463 OutputType.Format.nBlockAlign;
464 DSBDescription.lpwfxFormat=&OutputType.Format;
465 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
468 if(SUCCEEDED(hr))
470 SetDefaultWFXChannelOrder(device);
471 pData->thread = StartThread(DSoundProc, device);
472 if(!pData->thread)
473 hr = E_FAIL;
476 if(FAILED(hr))
478 if (pData->DSsbuffer)
479 IDirectSoundBuffer_Release(pData->DSsbuffer);
480 pData->DSsbuffer = NULL;
481 if (pData->DSpbuffer)
482 IDirectSoundBuffer_Release(pData->DSpbuffer);
483 pData->DSpbuffer = NULL;
484 return ALC_FALSE;
487 return ALC_TRUE;
490 static void DSoundStopPlayback(ALCdevice *device)
492 DSoundData *pData = device->ExtraData;
494 if(!pData->thread)
495 return;
497 pData->killNow = 1;
498 StopThread(pData->thread);
499 pData->thread = NULL;
501 pData->killNow = 0;
503 IDirectSoundBuffer_Release(pData->DSsbuffer);
504 pData->DSsbuffer = NULL;
505 if (pData->DSpbuffer)
506 IDirectSoundBuffer_Release(pData->DSpbuffer);
507 pData->DSpbuffer = NULL;
511 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName)
513 (void)pDevice;
514 (void)deviceName;
515 return ALC_FALSE;
518 static void DSoundCloseCapture(ALCdevice *pDevice)
520 (void)pDevice;
523 static void DSoundStartCapture(ALCdevice *pDevice)
525 (void)pDevice;
528 static void DSoundStopCapture(ALCdevice *pDevice)
530 (void)pDevice;
533 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
535 (void)pDevice;
536 (void)pBuffer;
537 (void)lSamples;
540 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
542 (void)pDevice;
543 return 0;
547 BackendFuncs DSoundFuncs = {
548 DSoundOpenPlayback,
549 DSoundClosePlayback,
550 DSoundResetPlayback,
551 DSoundStopPlayback,
552 DSoundOpenCapture,
553 DSoundCloseCapture,
554 DSoundStartCapture,
555 DSoundStopCapture,
556 DSoundCaptureSamples,
557 DSoundAvailableSamples
561 void alcDSoundInit(BackendFuncs *FuncList)
563 *FuncList = DSoundFuncs;
566 void alcDSoundDeinit(void)
568 ALuint i;
570 for(i = 0;i < NumDevices;++i)
571 free(DeviceList[i].name);
572 free(DeviceList);
573 DeviceList = NULL;
574 NumDevices = 0;
576 if(ds_handle)
578 #ifdef _WIN32
579 FreeLibrary(ds_handle);
580 #endif
581 ds_handle = NULL;
585 void alcDSoundProbe(int type)
587 if(!DSoundLoad()) return;
589 if(type == DEVICE_PROBE)
590 AppendDeviceList(dsDevice);
591 else if(type == ALL_DEVICE_PROBE)
593 HRESULT hr;
594 ALuint i;
596 for(i = 0;i < NumDevices;++i)
597 free(DeviceList[i].name);
598 free(DeviceList);
599 DeviceList = NULL;
600 NumDevices = 0;
602 hr = pDirectSoundEnumerateA(DSoundEnumDevices, NULL);
603 if(FAILED(hr))
604 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);
605 else
607 for(i = 0;i < NumDevices;i++)
608 AppendAllDeviceList(DeviceList[i].name);