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
32 #ifndef _WAVEFORMATEXTENSIBLE_
41 #ifndef DSSPEAKER_5POINT1
42 #define DSSPEAKER_5POINT1 6
44 #ifndef DSSPEAKER_7POINT1
45 #define DSSPEAKER_7POINT1 7
48 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
49 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
51 static void *ds_handle
;
52 static HRESULT (WINAPI
*pDirectSoundCreate
)(LPCGUID pcGuidDevice
, LPDIRECTSOUND
*ppDS
, LPUNKNOWN pUnkOuter
);
53 static HRESULT (WINAPI
*pDirectSoundEnumerateA
)(LPDSENUMCALLBACKA pDSEnumCallback
, LPVOID pContext
);
57 // DirectSound Playback Device
59 LPDIRECTSOUNDBUFFER DSpbuffer
;
60 LPDIRECTSOUNDBUFFER DSsbuffer
;
72 static const ALCchar dsDevice
[] = "DirectSound Default";
73 static DevMap
*DeviceList
;
74 static ALuint NumDevices
;
77 void *DSoundLoad(void)
82 ds_handle
= LoadLibraryA("dsound.dll");
85 AL_PRINT("Failed to load dsound.dll\n");
89 #define LOAD_FUNC(f) do { \
90 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
93 FreeLibrary(ds_handle); \
95 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
100 ds_handle
= (void*)0xDEADBEEF;
101 #define LOAD_FUNC(f) p##f = f
104 LOAD_FUNC(DirectSoundCreate
);
105 LOAD_FUNC(DirectSoundEnumerateA
);
112 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
124 temp
= realloc(DeviceList
, sizeof(DevMap
) * (NumDevices
+1));
128 DeviceList
[NumDevices
].name
= strdup(dsDevice
);
129 DeviceList
[NumDevices
].guid
= GUID_NULL
;
140 snprintf(str
, sizeof(str
), "%s via DirectSound", desc
);
142 snprintf(str
, sizeof(str
), "%s #%d via DirectSound", desc
, count
+1);
145 for(i
= 0;i
< NumDevices
;i
++)
147 if(strcmp(str
, DeviceList
[i
].name
) == 0)
150 } while(i
!= NumDevices
);
152 temp
= realloc(DeviceList
, sizeof(DevMap
) * (NumDevices
+1));
156 DeviceList
[NumDevices
].name
= strdup(str
);
157 DeviceList
[NumDevices
].guid
= *guid
;
165 static ALuint
DSoundProc(ALvoid
*ptr
)
167 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
168 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
170 DWORD LastCursor
= 0;
172 VOID
*WritePtr1
, *WritePtr2
;
173 DWORD WriteCnt1
, WriteCnt2
;
174 BOOL Playing
= FALSE
;
182 memset(&DSBCaps
, 0, sizeof(DSBCaps
));
183 DSBCaps
.dwSize
= sizeof(DSBCaps
);
184 err
= IDirectSoundBuffer_GetCaps(pData
->DSsbuffer
, &DSBCaps
);
187 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err
);
188 aluHandleDisconnect(pDevice
);
192 FrameSize
= FrameSizeFromDevFmt(pDevice
->FmtChans
, pDevice
->FmtType
);
193 FragSize
= pDevice
->UpdateSize
* FrameSize
;
195 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &LastCursor
, NULL
);
196 while(!pData
->killNow
)
198 // Get current play and write cursors
199 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
200 avail
= (PlayCursor
-LastCursor
+DSBCaps
.dwBufferBytes
) % DSBCaps
.dwBufferBytes
;
206 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
209 AL_PRINT("Failed to play buffer: 0x%lx\n", err
);
210 aluHandleDisconnect(pDevice
);
218 avail
-= avail
%FragSize
;
220 // Lock output buffer
223 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
225 // If the buffer is lost, restore it and lock
226 if(err
== DSERR_BUFFERLOST
)
228 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
233 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, 0, DSBCaps
.dwBufferBytes
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
237 // Successfully locked the output buffer
240 // If we have an active context, mix data directly into output buffer otherwise fill with silence
241 aluMixData(pDevice
, WritePtr1
, WriteCnt1
/FrameSize
);
242 aluMixData(pDevice
, WritePtr2
, WriteCnt2
/FrameSize
);
244 // Unlock output buffer only when successfully locked
245 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
249 AL_PRINT("Buffer lock error: %#lx\n", err
);
250 aluHandleDisconnect(pDevice
);
254 // Update old write cursor location
255 LastCursor
+= WriteCnt1
+WriteCnt2
;
256 LastCursor
%= DSBCaps
.dwBufferBytes
;
262 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
264 DSoundData
*pData
= NULL
;
272 deviceName
= dsDevice
;
273 else if(strcmp(deviceName
, dsDevice
) != 0)
279 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, NULL
);
281 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
284 for(i
= 0;i
< NumDevices
;i
++)
286 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
289 guid
= &DeviceList
[i
].guid
;
297 //Initialise requested device
298 pData
= calloc(1, sizeof(DSoundData
));
301 alcSetError(device
, ALC_OUT_OF_MEMORY
);
305 //DirectSound Init code
306 hr
= pDirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
308 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
312 IDirectSound_Release(pData
->lpDS
);
314 AL_PRINT("Device init failed: 0x%08lx\n", hr
);
318 device
->szDeviceName
= strdup(deviceName
);
319 device
->ExtraData
= pData
;
323 static void DSoundClosePlayback(ALCdevice
*device
)
325 DSoundData
*pData
= device
->ExtraData
;
327 IDirectSound_Release(pData
->lpDS
);
329 device
->ExtraData
= NULL
;
332 static ALCboolean
DSoundResetPlayback(ALCdevice
*device
)
334 DSoundData
*pData
= (DSoundData
*)device
->ExtraData
;
335 DSBUFFERDESC DSBDescription
;
336 WAVEFORMATEXTENSIBLE OutputType
;
340 memset(&OutputType
, 0, sizeof(OutputType
));
342 switch(device
->FmtType
)
345 device
->FmtType
= DevFmtUByte
;
348 device
->FmtType
= DevFmtShort
;
356 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
357 if(SUCCEEDED(hr
) && ConfigValueExists(NULL
, "format"))
359 switch(device
->FmtChans
)
362 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_MONO
, 0);
365 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, 0);
368 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_QUAD
, 0);
371 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, 0);
377 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_7POINT1
, 0);
383 speakers
= DSSPEAKER_CONFIG(speakers
);
384 if(speakers
== DSSPEAKER_MONO
)
386 device
->FmtChans
= DevFmtMono
;
387 OutputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
389 else if(speakers
== DSSPEAKER_STEREO
|| speakers
== DSSPEAKER_HEADPHONE
)
391 device
->FmtChans
= DevFmtStereo
;
392 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
395 else if(speakers
== DSSPEAKER_QUAD
)
397 device
->FmtChans
= DevFmtQuad
;
398 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
399 SPEAKER_FRONT_RIGHT
|
403 else if(speakers
== DSSPEAKER_5POINT1
)
405 device
->FmtChans
= DevFmtX51
;
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 device
->FmtChans
= DevFmtX71
;
416 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
417 SPEAKER_FRONT_RIGHT
|
418 SPEAKER_FRONT_CENTER
|
419 SPEAKER_LOW_FREQUENCY
|
426 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
427 OutputType
.Format
.nChannels
= ChannelsFromDevFmt(device
->FmtChans
);
428 OutputType
.Format
.wBitsPerSample
= BytesFromDevFmt(device
->FmtType
) * 8;
429 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
430 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
431 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
432 OutputType
.Format
.cbSize
= 0;
435 if(OutputType
.Format
.nChannels
> 2 || OutputType
.Format
.wBitsPerSample
> 16)
437 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
438 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
439 OutputType
.Format
.cbSize
= 22;
440 if(OutputType
.Format
.wBitsPerSample
== 32)
441 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
443 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
449 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
450 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
451 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
452 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
455 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
460 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
461 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
462 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
463 DSBDescription
.dwBufferBytes
=device
->UpdateSize
* device
->NumUpdates
*
464 OutputType
.Format
.nBlockAlign
;
465 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
466 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
471 SetDefaultWFXChannelOrder(device
);
472 pData
->thread
= StartThread(DSoundProc
, device
);
479 if (pData
->DSsbuffer
)
480 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
481 pData
->DSsbuffer
= NULL
;
482 if (pData
->DSpbuffer
)
483 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
484 pData
->DSpbuffer
= NULL
;
491 static void DSoundStopPlayback(ALCdevice
*device
)
493 DSoundData
*pData
= device
->ExtraData
;
499 StopThread(pData
->thread
);
500 pData
->thread
= NULL
;
504 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
505 pData
->DSsbuffer
= NULL
;
506 if (pData
->DSpbuffer
)
507 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
508 pData
->DSpbuffer
= NULL
;
512 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
519 static void DSoundCloseCapture(ALCdevice
*pDevice
)
524 static void DSoundStartCapture(ALCdevice
*pDevice
)
529 static void DSoundStopCapture(ALCdevice
*pDevice
)
534 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
541 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
548 BackendFuncs DSoundFuncs
= {
557 DSoundCaptureSamples
,
558 DSoundAvailableSamples
562 void alcDSoundInit(BackendFuncs
*FuncList
)
564 *FuncList
= DSoundFuncs
;
567 void alcDSoundDeinit(void)
571 for(i
= 0;i
< NumDevices
;++i
)
572 free(DeviceList
[i
].name
);
580 FreeLibrary(ds_handle
);
586 void alcDSoundProbe(int type
)
588 if(!DSoundLoad()) return;
590 if(type
== DEVICE_PROBE
)
591 AppendDeviceList(dsDevice
);
592 else if(type
== ALL_DEVICE_PROBE
)
597 for(i
= 0;i
< NumDevices
;++i
)
598 free(DeviceList
[i
].name
);
603 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, NULL
);
605 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
608 for(i
= 0;i
< NumDevices
;i
++)
609 AppendAllDeviceList(DeviceList
[i
].name
);