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
23 #define _WIN32_WINNT 0x0500
31 #ifndef _WAVEFORMATEXTENSIBLE_
40 #ifndef DSSPEAKER_5POINT1
41 #define DSSPEAKER_5POINT1 6
43 #ifndef DSSPEAKER_7POINT1
44 #define DSSPEAKER_7POINT1 7
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
);
56 // DirectSound Playback Device
58 LPDIRECTSOUNDBUFFER DSpbuffer
;
59 LPDIRECTSOUNDBUFFER DSsbuffer
;
71 static const ALCchar dsDevice
[] = "DirectSound Default";
72 static DevMap
*DeviceList
;
73 static ALuint NumDevices
;
76 void *DSoundLoad(void)
81 ds_handle
= LoadLibraryA("dsound.dll");
84 AL_PRINT("Failed to load dsound.dll\n");
88 #define LOAD_FUNC(f) do { \
89 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
92 FreeLibrary(ds_handle); \
94 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
99 ds_handle
= (void*)0xDEADBEEF;
100 #define LOAD_FUNC(f) p##f = f
103 LOAD_FUNC(DirectSoundCreate
);
104 LOAD_FUNC(DirectSoundEnumerateA
);
111 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
123 temp
= realloc(DeviceList
, sizeof(DevMap
) * (NumDevices
+1));
127 DeviceList
[NumDevices
].name
= strdup(dsDevice
);
128 DeviceList
[NumDevices
].guid
= GUID_NULL
;
139 snprintf(str
, sizeof(str
), "%s via DirectSound", desc
);
141 snprintf(str
, sizeof(str
), "%s #%d via DirectSound", desc
, count
+1);
144 for(i
= 0;i
< NumDevices
;i
++)
146 if(strcmp(str
, DeviceList
[i
].name
) == 0)
149 } while(i
!= NumDevices
);
151 temp
= realloc(DeviceList
, sizeof(DevMap
) * (NumDevices
+1));
155 DeviceList
[NumDevices
].name
= strdup(str
);
156 DeviceList
[NumDevices
].guid
= *guid
;
164 static ALuint
DSoundProc(ALvoid
*ptr
)
166 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
167 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
169 DWORD LastCursor
= 0;
171 VOID
*WritePtr1
, *WritePtr2
;
172 DWORD WriteCnt1
, WriteCnt2
;
173 BOOL Playing
= FALSE
;
181 memset(&DSBCaps
, 0, sizeof(DSBCaps
));
182 DSBCaps
.dwSize
= sizeof(DSBCaps
);
183 err
= IDirectSoundBuffer_GetCaps(pData
->DSsbuffer
, &DSBCaps
);
186 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err
);
187 aluHandleDisconnect(pDevice
);
191 FrameSize
= aluFrameSizeFromFormat(pDevice
->Format
);
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
;
205 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
208 AL_PRINT("Failed to play buffer: 0x%lx\n", err
);
209 aluHandleDisconnect(pDevice
);
216 avail
-= avail
%FragSize
;
218 // Lock output buffer
221 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
223 // If the buffer is lost, restore it and lock
224 if(err
== DSERR_BUFFERLOST
)
226 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
231 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, 0, DSBCaps
.dwBufferBytes
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
235 // Successfully locked the output buffer
238 // If we have an active context, mix data directly into output buffer otherwise fill with silence
239 aluMixData(pDevice
, WritePtr1
, WriteCnt1
/FrameSize
);
240 aluMixData(pDevice
, WritePtr2
, WriteCnt2
/FrameSize
);
242 // Unlock output buffer only when successfully locked
243 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
247 AL_PRINT("Buffer lock error: %#lx\n", err
);
248 aluHandleDisconnect(pDevice
);
252 // Update old write cursor location
253 LastCursor
+= WriteCnt1
+WriteCnt2
;
254 LastCursor
%= DSBCaps
.dwBufferBytes
;
260 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
262 DSoundData
*pData
= NULL
;
270 deviceName
= dsDevice
;
271 else if(strcmp(deviceName
, dsDevice
) != 0)
277 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, NULL
);
279 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
282 for(i
= 0;i
< NumDevices
;i
++)
284 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
287 guid
= &DeviceList
[i
].guid
;
295 //Initialise requested device
296 pData
= calloc(1, sizeof(DSoundData
));
299 alcSetError(device
, ALC_OUT_OF_MEMORY
);
303 //DirectSound Init code
304 hr
= pDirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
306 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
310 IDirectSound_Release(pData
->lpDS
);
312 AL_PRINT("Device init failed: 0x%08lx\n", hr
);
316 device
->szDeviceName
= strdup(deviceName
);
317 device
->ExtraData
= pData
;
321 static void DSoundClosePlayback(ALCdevice
*device
)
323 DSoundData
*pData
= device
->ExtraData
;
325 IDirectSound_Release(pData
->lpDS
);
327 device
->ExtraData
= NULL
;
330 static ALCboolean
DSoundResetPlayback(ALCdevice
*device
)
332 DSoundData
*pData
= (DSoundData
*)device
->ExtraData
;
333 DSBUFFERDESC DSBDescription
;
334 WAVEFORMATEXTENSIBLE OutputType
;
340 memset(&OutputType
, 0, sizeof(OutputType
));
342 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
343 if(SUCCEEDED(hr
) && ConfigValueExists(NULL
, "format"))
345 if(aluChannelsFromFormat(device
->Format
) == 1)
346 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_MONO
, 0);
347 else if(aluChannelsFromFormat(device
->Format
) == 2)
348 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, 0);
349 else if(aluChannelsFromFormat(device
->Format
) == 4)
350 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_QUAD
, 0);
351 else if(aluChannelsFromFormat(device
->Format
) == 6)
352 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, 0);
353 else if(aluChannelsFromFormat(device
->Format
) == 8)
354 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_7POINT1
, 0);
357 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
363 speakers
= DSSPEAKER_CONFIG(speakers
);
364 if(speakers
== DSSPEAKER_MONO
)
366 if(aluBytesFromFormat(device
->Format
) == 1)
367 format
= AL_FORMAT_MONO8
;
368 else if(aluBytesFromFormat(device
->Format
) == 2)
369 format
= AL_FORMAT_MONO16
;
370 else if(aluBytesFromFormat(device
->Format
) == 4)
371 format
= AL_FORMAT_MONO_FLOAT32
;
372 OutputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
374 else if(speakers
== DSSPEAKER_STEREO
)
376 if(aluBytesFromFormat(device
->Format
) == 1)
377 format
= AL_FORMAT_STEREO8
;
378 else if(aluBytesFromFormat(device
->Format
) == 2)
379 format
= AL_FORMAT_STEREO16
;
380 else if(aluBytesFromFormat(device
->Format
) == 4)
381 format
= AL_FORMAT_STEREO_FLOAT32
;
382 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
385 else if(speakers
== DSSPEAKER_QUAD
)
387 if(aluBytesFromFormat(device
->Format
) == 1)
388 format
= AL_FORMAT_QUAD8
;
389 else if(aluBytesFromFormat(device
->Format
) == 2)
390 format
= AL_FORMAT_QUAD16
;
391 else if(aluBytesFromFormat(device
->Format
) == 4)
392 format
= AL_FORMAT_QUAD32
;
393 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
394 SPEAKER_FRONT_RIGHT
|
398 else if(speakers
== DSSPEAKER_5POINT1
)
400 if(aluBytesFromFormat(device
->Format
) == 1)
401 format
= AL_FORMAT_51CHN8
;
402 else if(aluBytesFromFormat(device
->Format
) == 2)
403 format
= AL_FORMAT_51CHN16
;
404 else if(aluBytesFromFormat(device
->Format
) == 4)
405 format
= AL_FORMAT_51CHN32
;
406 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
407 SPEAKER_FRONT_RIGHT
|
408 SPEAKER_FRONT_CENTER
|
409 SPEAKER_LOW_FREQUENCY
|
413 else if(speakers
== DSSPEAKER_7POINT1
)
415 if(aluBytesFromFormat(device
->Format
) == 1)
416 format
= AL_FORMAT_71CHN8
;
417 else if(aluBytesFromFormat(device
->Format
) == 2)
418 format
= AL_FORMAT_71CHN16
;
419 else if(aluBytesFromFormat(device
->Format
) == 4)
420 format
= AL_FORMAT_71CHN32
;
421 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
422 SPEAKER_FRONT_RIGHT
|
423 SPEAKER_FRONT_CENTER
|
424 SPEAKER_LOW_FREQUENCY
|
431 format
= device
->Format
;
432 frameSize
= aluFrameSizeFromFormat(format
);
434 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
435 OutputType
.Format
.nChannels
= aluChannelsFromFormat(format
);
436 OutputType
.Format
.wBitsPerSample
= aluBytesFromFormat(format
) * 8;
437 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
438 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
439 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
440 OutputType
.Format
.cbSize
= 0;
443 if(OutputType
.Format
.nChannels
> 2 || OutputType
.Format
.wBitsPerSample
> 16)
445 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
446 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
447 OutputType
.Format
.cbSize
= 22;
448 if(OutputType
.Format
.wBitsPerSample
== 32)
449 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
451 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
457 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
458 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
459 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
460 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
463 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
468 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
469 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
470 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
471 DSBDescription
.dwBufferBytes
=device
->UpdateSize
* device
->NumUpdates
* frameSize
;
472 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
473 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
478 device
->Format
= format
;
479 SetDefaultWFXChannelOrder(device
);
480 pData
->thread
= StartThread(DSoundProc
, device
);
487 if (pData
->DSsbuffer
)
488 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
489 pData
->DSsbuffer
= NULL
;
490 if (pData
->DSpbuffer
)
491 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
492 pData
->DSpbuffer
= NULL
;
499 static void DSoundStopPlayback(ALCdevice
*device
)
501 DSoundData
*pData
= device
->ExtraData
;
507 StopThread(pData
->thread
);
508 pData
->thread
= NULL
;
512 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
513 pData
->DSsbuffer
= NULL
;
514 if (pData
->DSpbuffer
)
515 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
516 pData
->DSpbuffer
= NULL
;
520 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
527 static void DSoundCloseCapture(ALCdevice
*pDevice
)
532 static void DSoundStartCapture(ALCdevice
*pDevice
)
537 static void DSoundStopCapture(ALCdevice
*pDevice
)
542 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
549 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
556 BackendFuncs DSoundFuncs
= {
565 DSoundCaptureSamples
,
566 DSoundAvailableSamples
570 void alcDSoundInit(BackendFuncs
*FuncList
)
572 *FuncList
= DSoundFuncs
;
575 void alcDSoundDeinit(void)
579 for(i
= 0;i
< NumDevices
;++i
)
580 free(DeviceList
[i
].name
);
588 FreeLibrary(ds_handle
);
594 void alcDSoundProbe(int type
)
596 if(!DSoundLoad()) return;
598 if(type
== DEVICE_PROBE
)
599 AppendDeviceList(dsDevice
);
600 else if(type
== ALL_DEVICE_PROBE
)
605 for(i
= 0;i
< NumDevices
;++i
)
606 free(DeviceList
[i
].name
);
611 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, NULL
);
613 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
616 for(i
= 0;i
< NumDevices
;i
++)
617 AppendAllDeviceList(DeviceList
[i
].name
);