Dynamically load dsound when possible
[openal-soft.git] / Alc / dsound.c
blob6ce4c66e62e91e26fab5bc10a08de657a89d9d93
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>
32 #include "alMain.h"
33 #include "AL/al.h"
34 #include "AL/alc.h"
36 #ifndef DSSPEAKER_7POINT1
37 #define DSSPEAKER_7POINT1 7
38 #endif
40 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
42 static void *ds_handle;
43 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter);
44 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA pDSEnumCallback, LPVOID pContext);
46 // Since DSound doesn't report the fragment size, emulate it
47 static int num_frags;
49 typedef struct {
50 // DirectSound Playback Device
51 LPDIRECTSOUND lpDS;
52 LPDIRECTSOUNDBUFFER DSpbuffer;
53 LPDIRECTSOUNDBUFFER DSsbuffer;
55 volatile int killNow;
56 ALvoid *thread;
57 } DSoundData;
60 typedef struct {
61 ALCchar *name;
62 GUID guid;
63 } DevMap;
64 static DevMap DeviceList[16];
67 static ALuint DSoundProc(ALvoid *ptr)
69 ALCdevice *pDevice = (ALCdevice*)ptr;
70 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
71 DWORD LastCursor = 0;
72 DWORD PlayCursor;
73 VOID *WritePtr1, *WritePtr2;
74 DWORD WriteCnt1, WriteCnt2;
75 DWORD BufferSize;
76 DWORD avail;
77 HRESULT err;
79 BufferSize = pDevice->UpdateSize * num_frags *
80 aluBytesFromFormat(pDevice->Format) *
81 aluChannelsFromFormat(pDevice->Format);
83 while(!pData->killNow)
85 // Get current play and write cursors
86 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
87 avail = (PlayCursor-LastCursor+BufferSize) % BufferSize;
89 if(avail < BufferSize/num_frags)
91 Sleep(1);
92 continue;
95 // Lock output buffer
96 WriteCnt1 = 0;
97 WriteCnt2 = 0;
98 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
100 // If the buffer is lost, restore it, play and lock
101 if(err == DSERR_BUFFERLOST)
103 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
104 if(SUCCEEDED(err))
105 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
106 if(SUCCEEDED(err))
107 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
110 // Successfully locked the output buffer
111 if(SUCCEEDED(err))
113 // If we have an active context, mix data directly into output buffer otherwise fill with silence
114 SuspendContext(NULL);
115 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
116 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
117 ProcessContext(NULL);
119 // Unlock output buffer only when successfully locked
120 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
122 else
123 AL_PRINT("Buffer lock error: %#lx\n", err);
125 // Update old write cursor location
126 LastCursor += WriteCnt1+WriteCnt2;
127 LastCursor %= BufferSize;
130 return 0;
133 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
135 DSBUFFERDESC DSBDescription;
136 DSoundData *pData = NULL;
137 WAVEFORMATEXTENSIBLE OutputType;
138 DWORD frameSize = 0;
139 LPGUID guid = NULL;
140 DWORD speakers;
141 HRESULT hr;
143 if(ds_handle == NULL)
144 return ALC_FALSE;
146 if(deviceName)
148 int i;
149 for(i = 0;DeviceList[i].name;i++)
151 if(strcmp(deviceName, DeviceList[i].name) == 0)
153 device->szDeviceName = DeviceList[i].name;
154 if(i > 0)
155 guid = &DeviceList[i].guid;
156 break;
159 if(!DeviceList[i].name)
160 return ALC_FALSE;
162 else
163 device->szDeviceName = DeviceList[0].name;
165 memset(&OutputType, 0, sizeof(OutputType));
167 //Initialise requested device
169 pData = calloc(1, sizeof(DSoundData));
170 if(!pData)
172 SetALCError(ALC_OUT_OF_MEMORY);
173 return ALC_FALSE;
176 //DirectSound Init code
177 hr = pDirectSoundCreate(guid, &pData->lpDS, NULL);
178 if(SUCCEEDED(hr))
179 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
181 if(SUCCEEDED(hr))
183 if(*(GetConfigValue(NULL, "format", "")) == 0)
184 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
185 else
187 if(device->Format == AL_FORMAT_MONO8 || device->Format == AL_FORMAT_MONO16)
188 speakers = DSSPEAKER_COMBINED(DSSPEAKER_MONO, 0);
189 else if(device->Format == AL_FORMAT_STEREO8 || device->Format == AL_FORMAT_STEREO16)
190 speakers = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, 0);
191 else if(device->Format == AL_FORMAT_QUAD8 || device->Format == AL_FORMAT_QUAD16)
192 speakers = DSSPEAKER_COMBINED(DSSPEAKER_QUAD, 0);
193 else if(device->Format == AL_FORMAT_51CHN8 || device->Format == AL_FORMAT_51CHN16)
194 speakers = DSSPEAKER_COMBINED(DSSPEAKER_5POINT1, 0);
195 else if(device->Format == AL_FORMAT_71CHN8 || device->Format == AL_FORMAT_71CHN16)
196 speakers = DSSPEAKER_COMBINED(DSSPEAKER_7POINT1, 0);
197 else
198 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
201 if(SUCCEEDED(hr))
203 speakers = DSSPEAKER_CONFIG(speakers);
204 if(speakers == DSSPEAKER_MONO)
206 if(aluBytesFromFormat(device->Format) == 1)
207 device->Format = AL_FORMAT_MONO8;
208 else
209 device->Format = AL_FORMAT_MONO16;
211 else if(speakers == DSSPEAKER_STEREO)
213 if(aluBytesFromFormat(device->Format) == 1)
214 device->Format = AL_FORMAT_STEREO8;
215 else
216 device->Format = AL_FORMAT_STEREO16;
218 else if(speakers == DSSPEAKER_QUAD)
220 if(aluBytesFromFormat(device->Format) == 1)
221 device->Format = AL_FORMAT_QUAD8;
222 else
223 device->Format = AL_FORMAT_QUAD16;
224 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
225 SPEAKER_FRONT_RIGHT |
226 SPEAKER_BACK_LEFT |
227 SPEAKER_BACK_RIGHT;
229 else if(speakers == DSSPEAKER_5POINT1)
231 if(aluBytesFromFormat(device->Format) == 1)
232 device->Format = AL_FORMAT_51CHN8;
233 else
234 device->Format = AL_FORMAT_51CHN16;
235 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
236 SPEAKER_FRONT_RIGHT |
237 SPEAKER_FRONT_CENTER |
238 SPEAKER_LOW_FREQUENCY |
239 SPEAKER_BACK_LEFT |
240 SPEAKER_BACK_RIGHT;
242 else if(speakers == DSSPEAKER_7POINT1)
244 if(aluBytesFromFormat(device->Format) == 1)
245 device->Format = AL_FORMAT_71CHN8;
246 else
247 device->Format = AL_FORMAT_71CHN16;
248 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
249 SPEAKER_FRONT_RIGHT |
250 SPEAKER_FRONT_CENTER |
251 SPEAKER_LOW_FREQUENCY |
252 SPEAKER_BACK_LEFT |
253 SPEAKER_BACK_RIGHT |
254 SPEAKER_SIDE_LEFT |
255 SPEAKER_SIDE_RIGHT;
257 frameSize = aluBytesFromFormat(device->Format) *
258 aluChannelsFromFormat(device->Format);
260 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
261 OutputType.Format.nChannels = aluChannelsFromFormat(device->Format);
262 OutputType.Format.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
263 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
264 OutputType.Format.nSamplesPerSec = device->Frequency;
265 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
266 OutputType.Format.cbSize = 0;
268 device->UpdateSize /= num_frags;
271 if(OutputType.Format.nChannels > 2)
273 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
274 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
275 OutputType.Format.cbSize = 22;
276 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
278 else
280 if(SUCCEEDED(hr))
282 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
283 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
284 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
285 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
287 if(SUCCEEDED(hr))
288 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
291 if(SUCCEEDED(hr))
293 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
294 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
295 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
296 DSBDescription.dwBufferBytes=device->UpdateSize * num_frags * frameSize;
297 DSBDescription.lpwfxFormat=&OutputType.Format;
298 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
301 if(SUCCEEDED(hr))
302 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
304 if(SUCCEEDED(hr))
306 device->ExtraData = pData;
307 pData->thread = StartThread(DSoundProc, device);
308 if(!pData->thread)
309 hr = E_FAIL;
312 if(FAILED(hr))
314 if (pData->DSsbuffer)
315 IDirectSoundBuffer_Release(pData->DSsbuffer);
316 if (pData->DSpbuffer)
317 IDirectSoundBuffer_Release(pData->DSpbuffer);
318 if (pData->lpDS)
319 IDirectSound_Release(pData->lpDS);
321 free(pData);
322 return ALC_FALSE;
325 return ALC_TRUE;
328 static void DSoundClosePlayback(ALCdevice *device)
330 DSoundData *pData = device->ExtraData;
332 pData->killNow = 1;
333 StopThread(pData->thread);
335 IDirectSoundBuffer_Release(pData->DSsbuffer);
336 if (pData->DSpbuffer)
337 IDirectSoundBuffer_Release(pData->DSpbuffer);
338 IDirectSound_Release(pData->lpDS);
340 free(pData);
341 device->ExtraData = NULL;
345 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
347 (void)pDevice;
348 (void)deviceName;
349 (void)frequency;
350 (void)format;
351 (void)SampleSize;
352 return ALC_FALSE;
355 static void DSoundCloseCapture(ALCdevice *pDevice)
357 (void)pDevice;
360 static void DSoundStartCapture(ALCdevice *pDevice)
362 (void)pDevice;
365 static void DSoundStopCapture(ALCdevice *pDevice)
367 (void)pDevice;
370 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
372 (void)pDevice;
373 (void)pBuffer;
374 (void)lSamples;
377 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
379 (void)pDevice;
380 return 0;
384 BackendFuncs DSoundFuncs = {
385 DSoundOpenPlayback,
386 DSoundClosePlayback,
387 DSoundOpenCapture,
388 DSoundCloseCapture,
389 DSoundStartCapture,
390 DSoundStopCapture,
391 DSoundCaptureSamples,
392 DSoundAvailableSamples
395 static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCSTR desc, LPCSTR drvname, LPVOID data)
397 size_t *iter = data;
398 (void)drvname;
400 if(guid)
402 char str[128];
403 snprintf(str, sizeof(str), "DirectSound Software on %s", desc);
404 DeviceList[*iter].name = AppendAllDeviceList(str);
405 DeviceList[*iter].guid = *guid;
406 (*iter)++;
408 else
409 DeviceList[0].name = AppendDeviceList("DirectSound Software");
411 return TRUE;
414 void alcDSoundInit(BackendFuncs *FuncList)
416 size_t iter = 1;
417 HRESULT hr;
419 *FuncList = DSoundFuncs;
421 #ifdef _WIN32
422 ds_handle = LoadLibraryA("dsound.dll");
423 if(ds_handle == NULL)
425 AL_PRINT("Failed to load dsound.dll\n");
426 return;
429 #define LOAD_FUNC(f) do { \
430 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
431 if(p##f == NULL) \
433 FreeLibrary(ds_handle); \
434 ds_handle = NULL; \
435 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
436 return; \
438 } while(0)
439 #else
440 ds_handle = (void*)0xDEADBEEF;
441 #define LOAD_FUNC(f) p##f = f
442 #endif
444 LOAD_FUNC(DirectSoundCreate);
445 LOAD_FUNC(DirectSoundEnumerateA);
446 #undef LOAD_FUNC
448 num_frags = GetConfigValueInt("dsound", "periods", 4);
449 if(num_frags < 2) num_frags = 2;
451 hr = pDirectSoundEnumerateA(DSoundEnumDevices, &iter);
452 if(FAILED(hr))
453 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);