Dynamically load PulseAudio
[openal-soft.git] / Alc / dsound.c
blob5506700829d533fb32ae86afaf5b8dc62bf5a4bf
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_5POINT1
37 #define DSSPEAKER_5POINT1 6
38 #endif
39 #ifndef DSSPEAKER_7POINT1
40 #define DSSPEAKER_7POINT1 7
41 #endif
43 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
45 static void *ds_handle;
46 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter);
47 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA pDSEnumCallback, LPVOID pContext);
49 // Since DSound doesn't report the fragment size, emulate it
50 static int num_frags;
52 typedef struct {
53 // DirectSound Playback Device
54 LPDIRECTSOUND lpDS;
55 LPDIRECTSOUNDBUFFER DSpbuffer;
56 LPDIRECTSOUNDBUFFER DSsbuffer;
58 volatile int killNow;
59 ALvoid *thread;
60 } DSoundData;
63 typedef struct {
64 ALCchar *name;
65 GUID guid;
66 } DevMap;
67 static DevMap DeviceList[16];
70 static ALuint DSoundProc(ALvoid *ptr)
72 ALCdevice *pDevice = (ALCdevice*)ptr;
73 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
74 DWORD LastCursor = 0;
75 DWORD PlayCursor;
76 VOID *WritePtr1, *WritePtr2;
77 DWORD WriteCnt1, WriteCnt2;
78 DWORD BufferSize;
79 DWORD avail;
80 HRESULT err;
82 BufferSize = pDevice->UpdateSize * num_frags *
83 aluBytesFromFormat(pDevice->Format) *
84 aluChannelsFromFormat(pDevice->Format);
86 while(!pData->killNow)
88 // Get current play and write cursors
89 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
90 avail = (PlayCursor-LastCursor+BufferSize) % BufferSize;
92 if(avail < BufferSize/num_frags)
94 Sleep(1);
95 continue;
98 // Lock output buffer
99 WriteCnt1 = 0;
100 WriteCnt2 = 0;
101 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
103 // If the buffer is lost, restore it, play and lock
104 if(err == DSERR_BUFFERLOST)
106 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
107 if(SUCCEEDED(err))
108 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
109 if(SUCCEEDED(err))
110 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
113 // Successfully locked the output buffer
114 if(SUCCEEDED(err))
116 // If we have an active context, mix data directly into output buffer otherwise fill with silence
117 SuspendContext(NULL);
118 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
119 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
120 ProcessContext(NULL);
122 // Unlock output buffer only when successfully locked
123 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
125 else
126 AL_PRINT("Buffer lock error: %#lx\n", err);
128 // Update old write cursor location
129 LastCursor += WriteCnt1+WriteCnt2;
130 LastCursor %= BufferSize;
133 return 0;
136 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
138 DSBUFFERDESC DSBDescription;
139 DSoundData *pData = NULL;
140 WAVEFORMATEXTENSIBLE OutputType;
141 DWORD frameSize = 0;
142 LPGUID guid = NULL;
143 DWORD speakers;
144 HRESULT hr;
146 if(ds_handle == NULL)
147 return ALC_FALSE;
149 if(deviceName)
151 int i;
152 for(i = 0;DeviceList[i].name;i++)
154 if(strcmp(deviceName, DeviceList[i].name) == 0)
156 device->szDeviceName = DeviceList[i].name;
157 if(i > 0)
158 guid = &DeviceList[i].guid;
159 break;
162 if(!DeviceList[i].name)
163 return ALC_FALSE;
165 else
166 device->szDeviceName = DeviceList[0].name;
168 memset(&OutputType, 0, sizeof(OutputType));
170 //Initialise requested device
172 pData = calloc(1, sizeof(DSoundData));
173 if(!pData)
175 SetALCError(ALC_OUT_OF_MEMORY);
176 return ALC_FALSE;
179 //DirectSound Init code
180 hr = pDirectSoundCreate(guid, &pData->lpDS, NULL);
181 if(SUCCEEDED(hr))
182 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
184 if(SUCCEEDED(hr))
186 if(*(GetConfigValue(NULL, "format", "")) == 0)
187 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
188 else
190 if(device->Format == AL_FORMAT_MONO8 || device->Format == AL_FORMAT_MONO16)
191 speakers = DSSPEAKER_COMBINED(DSSPEAKER_MONO, 0);
192 else if(device->Format == AL_FORMAT_STEREO8 || device->Format == AL_FORMAT_STEREO16)
193 speakers = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, 0);
194 else if(device->Format == AL_FORMAT_QUAD8 || device->Format == AL_FORMAT_QUAD16)
195 speakers = DSSPEAKER_COMBINED(DSSPEAKER_QUAD, 0);
196 else if(device->Format == AL_FORMAT_51CHN8 || device->Format == AL_FORMAT_51CHN16)
197 speakers = DSSPEAKER_COMBINED(DSSPEAKER_5POINT1, 0);
198 else if(device->Format == AL_FORMAT_71CHN8 || device->Format == AL_FORMAT_71CHN16)
199 speakers = DSSPEAKER_COMBINED(DSSPEAKER_7POINT1, 0);
200 else
201 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
204 if(SUCCEEDED(hr))
206 speakers = DSSPEAKER_CONFIG(speakers);
207 if(speakers == DSSPEAKER_MONO)
209 if(aluBytesFromFormat(device->Format) == 1)
210 device->Format = AL_FORMAT_MONO8;
211 else
212 device->Format = AL_FORMAT_MONO16;
214 else if(speakers == DSSPEAKER_STEREO)
216 if(aluBytesFromFormat(device->Format) == 1)
217 device->Format = AL_FORMAT_STEREO8;
218 else
219 device->Format = AL_FORMAT_STEREO16;
221 else if(speakers == DSSPEAKER_QUAD)
223 if(aluBytesFromFormat(device->Format) == 1)
224 device->Format = AL_FORMAT_QUAD8;
225 else
226 device->Format = AL_FORMAT_QUAD16;
227 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
228 SPEAKER_FRONT_RIGHT |
229 SPEAKER_BACK_LEFT |
230 SPEAKER_BACK_RIGHT;
232 else if(speakers == DSSPEAKER_5POINT1)
234 if(aluBytesFromFormat(device->Format) == 1)
235 device->Format = AL_FORMAT_51CHN8;
236 else
237 device->Format = AL_FORMAT_51CHN16;
238 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
239 SPEAKER_FRONT_RIGHT |
240 SPEAKER_FRONT_CENTER |
241 SPEAKER_LOW_FREQUENCY |
242 SPEAKER_BACK_LEFT |
243 SPEAKER_BACK_RIGHT;
245 else if(speakers == DSSPEAKER_7POINT1)
247 if(aluBytesFromFormat(device->Format) == 1)
248 device->Format = AL_FORMAT_71CHN8;
249 else
250 device->Format = AL_FORMAT_71CHN16;
251 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
252 SPEAKER_FRONT_RIGHT |
253 SPEAKER_FRONT_CENTER |
254 SPEAKER_LOW_FREQUENCY |
255 SPEAKER_BACK_LEFT |
256 SPEAKER_BACK_RIGHT |
257 SPEAKER_SIDE_LEFT |
258 SPEAKER_SIDE_RIGHT;
260 frameSize = aluBytesFromFormat(device->Format) *
261 aluChannelsFromFormat(device->Format);
263 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
264 OutputType.Format.nChannels = aluChannelsFromFormat(device->Format);
265 OutputType.Format.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
266 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
267 OutputType.Format.nSamplesPerSec = device->Frequency;
268 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
269 OutputType.Format.cbSize = 0;
271 device->UpdateSize /= num_frags;
274 if(OutputType.Format.nChannels > 2)
276 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
277 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
278 OutputType.Format.cbSize = 22;
279 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
281 else
283 if(SUCCEEDED(hr))
285 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
286 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
287 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
288 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
290 if(SUCCEEDED(hr))
291 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
294 if(SUCCEEDED(hr))
296 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
297 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
298 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
299 DSBDescription.dwBufferBytes=device->UpdateSize * num_frags * frameSize;
300 DSBDescription.lpwfxFormat=&OutputType.Format;
301 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
304 if(SUCCEEDED(hr))
305 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
307 if(SUCCEEDED(hr))
309 device->ExtraData = pData;
310 pData->thread = StartThread(DSoundProc, device);
311 if(!pData->thread)
312 hr = E_FAIL;
315 if(FAILED(hr))
317 if (pData->DSsbuffer)
318 IDirectSoundBuffer_Release(pData->DSsbuffer);
319 if (pData->DSpbuffer)
320 IDirectSoundBuffer_Release(pData->DSpbuffer);
321 if (pData->lpDS)
322 IDirectSound_Release(pData->lpDS);
324 free(pData);
325 return ALC_FALSE;
328 return ALC_TRUE;
331 static void DSoundClosePlayback(ALCdevice *device)
333 DSoundData *pData = device->ExtraData;
335 pData->killNow = 1;
336 StopThread(pData->thread);
338 IDirectSoundBuffer_Release(pData->DSsbuffer);
339 if (pData->DSpbuffer)
340 IDirectSoundBuffer_Release(pData->DSpbuffer);
341 IDirectSound_Release(pData->lpDS);
343 free(pData);
344 device->ExtraData = NULL;
348 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
350 (void)pDevice;
351 (void)deviceName;
352 (void)frequency;
353 (void)format;
354 (void)SampleSize;
355 return ALC_FALSE;
358 static void DSoundCloseCapture(ALCdevice *pDevice)
360 (void)pDevice;
363 static void DSoundStartCapture(ALCdevice *pDevice)
365 (void)pDevice;
368 static void DSoundStopCapture(ALCdevice *pDevice)
370 (void)pDevice;
373 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
375 (void)pDevice;
376 (void)pBuffer;
377 (void)lSamples;
380 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
382 (void)pDevice;
383 return 0;
387 BackendFuncs DSoundFuncs = {
388 DSoundOpenPlayback,
389 DSoundClosePlayback,
390 DSoundOpenCapture,
391 DSoundCloseCapture,
392 DSoundStartCapture,
393 DSoundStopCapture,
394 DSoundCaptureSamples,
395 DSoundAvailableSamples
398 static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCSTR desc, LPCSTR drvname, LPVOID data)
400 size_t *iter = data;
401 (void)drvname;
403 if(guid)
405 char str[128];
406 snprintf(str, sizeof(str), "DirectSound Software on %s", desc);
407 DeviceList[*iter].name = AppendAllDeviceList(str);
408 DeviceList[*iter].guid = *guid;
409 (*iter)++;
411 else
412 DeviceList[0].name = AppendDeviceList("DirectSound Software");
414 return TRUE;
417 void alcDSoundInit(BackendFuncs *FuncList)
419 size_t iter = 1;
420 HRESULT hr;
422 *FuncList = DSoundFuncs;
424 #ifdef _WIN32
425 ds_handle = LoadLibraryA("dsound.dll");
426 if(ds_handle == NULL)
428 AL_PRINT("Failed to load dsound.dll\n");
429 return;
432 #define LOAD_FUNC(f) do { \
433 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
434 if(p##f == NULL) \
436 FreeLibrary(ds_handle); \
437 ds_handle = NULL; \
438 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
439 return; \
441 } while(0)
442 #else
443 ds_handle = (void*)0xDEADBEEF;
444 #define LOAD_FUNC(f) p##f = f
445 #endif
447 LOAD_FUNC(DirectSoundCreate);
448 LOAD_FUNC(DirectSoundEnumerateA);
449 #undef LOAD_FUNC
451 num_frags = GetConfigValueInt("dsound", "periods", 4);
452 if(num_frags < 2) num_frags = 2;
454 hr = pDirectSoundEnumerateA(DSoundEnumDevices, &iter);
455 if(FAILED(hr))
456 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);