Don't update the devicevalues before returning success
[openal-soft.git] / Alc / dsound.c
blob11cce1500dc91607565e90b17e92d8caf3cdadcb
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 DSBCAPS DSBCaps;
75 DWORD LastCursor = 0;
76 DWORD PlayCursor;
77 VOID *WritePtr1, *WritePtr2;
78 DWORD WriteCnt1, WriteCnt2;
79 DWORD FragSize;
80 DWORD avail;
81 HRESULT err;
83 memset(&DSBCaps, 0, sizeof(DSBCaps));
84 DSBCaps.dwSize = sizeof(DSBCaps);
85 err = IDirectSoundBuffer_GetCaps(pData->DSsbuffer, &DSBCaps);
86 if(FAILED(err))
88 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err);
89 return 1;
91 FragSize = DSBCaps.dwBufferBytes / num_frags;
93 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &LastCursor, NULL);
94 while(!pData->killNow)
96 // Get current play and write cursors
97 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
98 avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes;
100 if(avail < FragSize)
102 Sleep(1);
103 continue;
105 avail -= avail%FragSize;
107 // Lock output buffer
108 WriteCnt1 = 0;
109 WriteCnt2 = 0;
110 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
112 // If the buffer is lost, restore it, play and lock
113 if(err == DSERR_BUFFERLOST)
115 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
116 if(SUCCEEDED(err))
117 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
118 if(SUCCEEDED(err))
119 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
122 // Successfully locked the output buffer
123 if(SUCCEEDED(err))
125 // If we have an active context, mix data directly into output buffer otherwise fill with silence
126 SuspendContext(NULL);
127 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
128 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
129 ProcessContext(NULL);
131 // Unlock output buffer only when successfully locked
132 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
134 else
135 AL_PRINT("Buffer lock error: %#lx\n", err);
137 // Update old write cursor location
138 LastCursor += WriteCnt1+WriteCnt2;
139 LastCursor %= DSBCaps.dwBufferBytes;
142 return 0;
145 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
147 DSBUFFERDESC DSBDescription;
148 DSoundData *pData = NULL;
149 WAVEFORMATEXTENSIBLE OutputType;
150 DWORD frameSize = 0;
151 LPGUID guid = NULL;
152 ALenum format = 0;
153 DWORD speakers;
154 HRESULT hr;
156 if(ds_handle == NULL)
157 return ALC_FALSE;
159 if(deviceName)
161 int i;
162 for(i = 0;DeviceList[i].name;i++)
164 if(strcmp(deviceName, DeviceList[i].name) == 0)
166 device->szDeviceName = DeviceList[i].name;
167 if(i > 0)
168 guid = &DeviceList[i].guid;
169 break;
172 if(!DeviceList[i].name)
173 return ALC_FALSE;
175 else
176 device->szDeviceName = DeviceList[0].name;
178 memset(&OutputType, 0, sizeof(OutputType));
180 //Initialise requested device
182 pData = calloc(1, sizeof(DSoundData));
183 if(!pData)
185 SetALCError(ALC_OUT_OF_MEMORY);
186 return ALC_FALSE;
189 //DirectSound Init code
190 hr = pDirectSoundCreate(guid, &pData->lpDS, NULL);
191 if(SUCCEEDED(hr))
192 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
194 if(SUCCEEDED(hr))
196 if(*(GetConfigValue(NULL, "format", "")) == 0)
197 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
198 else
200 if(device->Format == AL_FORMAT_MONO8 || device->Format == AL_FORMAT_MONO16)
201 speakers = DSSPEAKER_COMBINED(DSSPEAKER_MONO, 0);
202 else if(device->Format == AL_FORMAT_STEREO8 || device->Format == AL_FORMAT_STEREO16)
203 speakers = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, 0);
204 else if(device->Format == AL_FORMAT_QUAD8 || device->Format == AL_FORMAT_QUAD16)
205 speakers = DSSPEAKER_COMBINED(DSSPEAKER_QUAD, 0);
206 else if(device->Format == AL_FORMAT_51CHN8 || device->Format == AL_FORMAT_51CHN16)
207 speakers = DSSPEAKER_COMBINED(DSSPEAKER_5POINT1, 0);
208 else if(device->Format == AL_FORMAT_71CHN8 || device->Format == AL_FORMAT_71CHN16)
209 speakers = DSSPEAKER_COMBINED(DSSPEAKER_7POINT1, 0);
210 else
211 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
214 if(SUCCEEDED(hr))
216 speakers = DSSPEAKER_CONFIG(speakers);
217 if(speakers == DSSPEAKER_MONO)
219 if(aluBytesFromFormat(device->Format) == 1)
220 format = AL_FORMAT_MONO8;
221 else
222 format = AL_FORMAT_MONO16;
224 else if(speakers == DSSPEAKER_STEREO)
226 if(aluBytesFromFormat(device->Format) == 1)
227 format = AL_FORMAT_STEREO8;
228 else
229 format = AL_FORMAT_STEREO16;
231 else if(speakers == DSSPEAKER_QUAD)
233 if(aluBytesFromFormat(device->Format) == 1)
234 format = AL_FORMAT_QUAD8;
235 else
236 format = AL_FORMAT_QUAD16;
237 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
238 SPEAKER_FRONT_RIGHT |
239 SPEAKER_BACK_LEFT |
240 SPEAKER_BACK_RIGHT;
242 else if(speakers == DSSPEAKER_5POINT1)
244 if(aluBytesFromFormat(device->Format) == 1)
245 format = AL_FORMAT_51CHN8;
246 else
247 format = AL_FORMAT_51CHN16;
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;
255 else if(speakers == DSSPEAKER_7POINT1)
257 if(aluBytesFromFormat(device->Format) == 1)
258 format = AL_FORMAT_71CHN8;
259 else
260 format = AL_FORMAT_71CHN16;
261 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
262 SPEAKER_FRONT_RIGHT |
263 SPEAKER_FRONT_CENTER |
264 SPEAKER_LOW_FREQUENCY |
265 SPEAKER_BACK_LEFT |
266 SPEAKER_BACK_RIGHT |
267 SPEAKER_SIDE_LEFT |
268 SPEAKER_SIDE_RIGHT;
270 else
271 format = device->Format;
272 frameSize = aluBytesFromFormat(format) * aluChannelsFromFormat(format);
274 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
275 OutputType.Format.nChannels = aluChannelsFromFormat(format);
276 OutputType.Format.wBitsPerSample = aluBytesFromFormat(format) * 8;
277 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
278 OutputType.Format.nSamplesPerSec = device->Frequency;
279 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
280 OutputType.Format.cbSize = 0;
283 if(OutputType.Format.nChannels > 2)
285 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
286 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
287 OutputType.Format.cbSize = 22;
288 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
290 else
292 if(SUCCEEDED(hr))
294 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
295 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
296 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
297 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
299 if(SUCCEEDED(hr))
300 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
303 if(SUCCEEDED(hr))
305 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
306 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
307 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
308 DSBDescription.dwBufferBytes=(device->UpdateSize/num_frags) * num_frags * frameSize;
309 DSBDescription.lpwfxFormat=&OutputType.Format;
310 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
313 if(SUCCEEDED(hr))
314 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
316 if(SUCCEEDED(hr))
318 device->ExtraData = pData;
319 pData->thread = StartThread(DSoundProc, device);
320 if(!pData->thread)
321 hr = E_FAIL;
324 if(FAILED(hr))
326 if (pData->DSsbuffer)
327 IDirectSoundBuffer_Release(pData->DSsbuffer);
328 if (pData->DSpbuffer)
329 IDirectSoundBuffer_Release(pData->DSpbuffer);
330 if (pData->lpDS)
331 IDirectSound_Release(pData->lpDS);
333 free(pData);
334 return ALC_FALSE;
337 device->Format = format;
338 device->UpdateSize /= num_frags;
340 return ALC_TRUE;
343 static void DSoundClosePlayback(ALCdevice *device)
345 DSoundData *pData = device->ExtraData;
347 pData->killNow = 1;
348 StopThread(pData->thread);
350 IDirectSoundBuffer_Release(pData->DSsbuffer);
351 if (pData->DSpbuffer)
352 IDirectSoundBuffer_Release(pData->DSpbuffer);
353 IDirectSound_Release(pData->lpDS);
355 free(pData);
356 device->ExtraData = NULL;
360 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
362 (void)pDevice;
363 (void)deviceName;
364 (void)frequency;
365 (void)format;
366 (void)SampleSize;
367 return ALC_FALSE;
370 static void DSoundCloseCapture(ALCdevice *pDevice)
372 (void)pDevice;
375 static void DSoundStartCapture(ALCdevice *pDevice)
377 (void)pDevice;
380 static void DSoundStopCapture(ALCdevice *pDevice)
382 (void)pDevice;
385 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
387 (void)pDevice;
388 (void)pBuffer;
389 (void)lSamples;
392 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
394 (void)pDevice;
395 return 0;
399 BackendFuncs DSoundFuncs = {
400 DSoundOpenPlayback,
401 DSoundClosePlayback,
402 DSoundOpenCapture,
403 DSoundCloseCapture,
404 DSoundStartCapture,
405 DSoundStopCapture,
406 DSoundCaptureSamples,
407 DSoundAvailableSamples
410 static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCSTR desc, LPCSTR drvname, LPVOID data)
412 size_t *iter = data;
413 (void)drvname;
415 if(guid)
417 char str[128];
418 snprintf(str, sizeof(str), "DirectSound Software on %s", desc);
419 DeviceList[*iter].name = AppendAllDeviceList(str);
420 DeviceList[*iter].guid = *guid;
421 (*iter)++;
423 else
424 DeviceList[0].name = AppendDeviceList("DirectSound Software");
426 return TRUE;
429 void alcDSoundInit(BackendFuncs *FuncList)
431 size_t iter = 1;
432 HRESULT hr;
434 *FuncList = DSoundFuncs;
436 #ifdef _WIN32
437 ds_handle = LoadLibraryA("dsound.dll");
438 if(ds_handle == NULL)
440 AL_PRINT("Failed to load dsound.dll\n");
441 return;
444 #define LOAD_FUNC(f) do { \
445 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
446 if(p##f == NULL) \
448 FreeLibrary(ds_handle); \
449 ds_handle = NULL; \
450 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
451 return; \
453 } while(0)
454 #else
455 ds_handle = (void*)0xDEADBEEF;
456 #define LOAD_FUNC(f) p##f = f
457 #endif
459 LOAD_FUNC(DirectSoundCreate);
460 LOAD_FUNC(DirectSoundEnumerateA);
461 #undef LOAD_FUNC
463 num_frags = GetConfigValueInt("dsound", "periods", 4);
464 if(num_frags < 2) num_frags = 2;
466 hr = pDirectSoundEnumerateA(DSoundEnumDevices, &iter);
467 if(FAILED(hr))
468 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);