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
= FrameSizeFromDevFmt(pDevice
->FmtChans
, pDevice
->FmtType
);
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
);
217 avail
-= avail
%FragSize
;
219 // Lock output buffer
222 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
224 // If the buffer is lost, restore it and lock
225 if(err
== DSERR_BUFFERLOST
)
227 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
232 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, 0, DSBCaps
.dwBufferBytes
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
236 // Successfully locked the output buffer
239 // If we have an active context, mix data directly into output buffer otherwise fill with silence
240 aluMixData(pDevice
, WritePtr1
, WriteCnt1
/FrameSize
);
241 aluMixData(pDevice
, WritePtr2
, WriteCnt2
/FrameSize
);
243 // Unlock output buffer only when successfully locked
244 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
248 AL_PRINT("Buffer lock error: %#lx\n", err
);
249 aluHandleDisconnect(pDevice
);
253 // Update old write cursor location
254 LastCursor
+= WriteCnt1
+WriteCnt2
;
255 LastCursor
%= DSBCaps
.dwBufferBytes
;
261 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
263 DSoundData
*pData
= NULL
;
271 deviceName
= dsDevice
;
272 else if(strcmp(deviceName
, dsDevice
) != 0)
278 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, NULL
);
280 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
283 for(i
= 0;i
< NumDevices
;i
++)
285 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
288 guid
= &DeviceList
[i
].guid
;
296 //Initialise requested device
297 pData
= calloc(1, sizeof(DSoundData
));
300 alcSetError(device
, ALC_OUT_OF_MEMORY
);
304 //DirectSound Init code
305 hr
= pDirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
307 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
311 IDirectSound_Release(pData
->lpDS
);
313 AL_PRINT("Device init failed: 0x%08lx\n", hr
);
317 device
->szDeviceName
= strdup(deviceName
);
318 device
->ExtraData
= pData
;
322 static void DSoundClosePlayback(ALCdevice
*device
)
324 DSoundData
*pData
= device
->ExtraData
;
326 IDirectSound_Release(pData
->lpDS
);
328 device
->ExtraData
= NULL
;
331 static ALCboolean
DSoundResetPlayback(ALCdevice
*device
)
333 DSoundData
*pData
= (DSoundData
*)device
->ExtraData
;
334 DSBUFFERDESC DSBDescription
;
335 WAVEFORMATEXTENSIBLE OutputType
;
339 memset(&OutputType
, 0, sizeof(OutputType
));
341 switch(device
->FmtType
)
344 device
->FmtType
= DevFmtUByte
;
347 device
->FmtType
= DevFmtShort
;
355 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
356 if(SUCCEEDED(hr
) && ConfigValueExists(NULL
, "format"))
358 switch(device
->FmtChans
)
361 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_MONO
, 0);
364 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, 0);
367 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_QUAD
, 0);
370 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, 0);
376 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_7POINT1
, 0);
382 speakers
= DSSPEAKER_CONFIG(speakers
);
383 if(speakers
== DSSPEAKER_MONO
)
385 device
->FmtChans
= DevFmtMono
;
386 OutputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
388 else if(speakers
== DSSPEAKER_STEREO
|| speakers
== DSSPEAKER_HEADPHONE
)
390 device
->FmtChans
= DevFmtStereo
;
391 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
394 else if(speakers
== DSSPEAKER_QUAD
)
396 device
->FmtChans
= DevFmtQuad
;
397 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
398 SPEAKER_FRONT_RIGHT
|
402 else if(speakers
== DSSPEAKER_5POINT1
)
404 device
->FmtChans
= DevFmtX51
;
405 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
406 SPEAKER_FRONT_RIGHT
|
407 SPEAKER_FRONT_CENTER
|
408 SPEAKER_LOW_FREQUENCY
|
412 else if(speakers
== DSSPEAKER_7POINT1
)
414 device
->FmtChans
= DevFmtX71
;
415 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
416 SPEAKER_FRONT_RIGHT
|
417 SPEAKER_FRONT_CENTER
|
418 SPEAKER_LOW_FREQUENCY
|
425 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
426 OutputType
.Format
.nChannels
= ChannelsFromDevFmt(device
->FmtChans
);
427 OutputType
.Format
.wBitsPerSample
= BytesFromDevFmt(device
->FmtType
) * 8;
428 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
429 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
430 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
431 OutputType
.Format
.cbSize
= 0;
434 if(OutputType
.Format
.nChannels
> 2 || OutputType
.Format
.wBitsPerSample
> 16)
436 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
437 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
438 OutputType
.Format
.cbSize
= 22;
439 if(OutputType
.Format
.wBitsPerSample
== 32)
440 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
442 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
448 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
449 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
450 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
451 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
454 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
459 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
460 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
461 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
462 DSBDescription
.dwBufferBytes
=device
->UpdateSize
* device
->NumUpdates
*
463 OutputType
.Format
.nBlockAlign
;
464 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
465 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
470 SetDefaultWFXChannelOrder(device
);
471 pData
->thread
= StartThread(DSoundProc
, device
);
478 if (pData
->DSsbuffer
)
479 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
480 pData
->DSsbuffer
= NULL
;
481 if (pData
->DSpbuffer
)
482 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
483 pData
->DSpbuffer
= NULL
;
490 static void DSoundStopPlayback(ALCdevice
*device
)
492 DSoundData
*pData
= device
->ExtraData
;
498 StopThread(pData
->thread
);
499 pData
->thread
= NULL
;
503 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
504 pData
->DSsbuffer
= NULL
;
505 if (pData
->DSpbuffer
)
506 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
507 pData
->DSpbuffer
= NULL
;
511 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
518 static void DSoundCloseCapture(ALCdevice
*pDevice
)
523 static void DSoundStartCapture(ALCdevice
*pDevice
)
528 static void DSoundStopCapture(ALCdevice
*pDevice
)
533 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
540 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
547 BackendFuncs DSoundFuncs
= {
556 DSoundCaptureSamples
,
557 DSoundAvailableSamples
561 void alcDSoundInit(BackendFuncs
*FuncList
)
563 *FuncList
= DSoundFuncs
;
566 void alcDSoundDeinit(void)
570 for(i
= 0;i
< NumDevices
;++i
)
571 free(DeviceList
[i
].name
);
579 FreeLibrary(ds_handle
);
585 void alcDSoundProbe(int type
)
587 if(!DSoundLoad()) return;
589 if(type
== DEVICE_PROBE
)
590 AppendDeviceList(dsDevice
);
591 else if(type
== ALL_DEVICE_PROBE
)
596 for(i
= 0;i
< NumDevices
;++i
)
597 free(DeviceList
[i
].name
);
602 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, NULL
);
604 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
607 for(i
= 0;i
< NumDevices
;i
++)
608 AppendAllDeviceList(DeviceList
[i
].name
);