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
);
54 // Since DSound doesn't report the fragment size, emulate it
58 // DirectSound Playback Device
60 LPDIRECTSOUNDBUFFER DSpbuffer
;
61 LPDIRECTSOUNDBUFFER DSsbuffer
;
72 static DevMap DeviceList
[16];
75 static ALuint
DSoundProc(ALvoid
*ptr
)
77 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
78 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
82 VOID
*WritePtr1
, *WritePtr2
;
83 DWORD WriteCnt1
, WriteCnt2
;
88 memset(&DSBCaps
, 0, sizeof(DSBCaps
));
89 DSBCaps
.dwSize
= sizeof(DSBCaps
);
90 err
= IDirectSoundBuffer_GetCaps(pData
->DSsbuffer
, &DSBCaps
);
93 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err
);
96 FragSize
= DSBCaps
.dwBufferBytes
/ num_frags
;
98 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &LastCursor
, NULL
);
99 while(!pData
->killNow
)
101 // Get current play and write cursors
102 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
103 avail
= (PlayCursor
-LastCursor
+DSBCaps
.dwBufferBytes
) % DSBCaps
.dwBufferBytes
;
110 avail
-= avail
%FragSize
;
112 // Lock output buffer
115 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
117 // If the buffer is lost, restore it, play and lock
118 if(err
== DSERR_BUFFERLOST
)
120 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
122 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
124 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
127 // Successfully locked the output buffer
130 // If we have an active context, mix data directly into output buffer otherwise fill with silence
131 SuspendContext(NULL
);
132 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
133 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
134 ProcessContext(NULL
);
136 // Unlock output buffer only when successfully locked
137 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
140 AL_PRINT("Buffer lock error: %#lx\n", err
);
142 // Update old write cursor location
143 LastCursor
+= WriteCnt1
+WriteCnt2
;
144 LastCursor
%= DSBCaps
.dwBufferBytes
;
150 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
152 DSoundData
*pData
= NULL
;
156 if(ds_handle
== NULL
)
162 for(i
= 0;DeviceList
[i
].name
;i
++)
164 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
166 device
->szDeviceName
= DeviceList
[i
].name
;
168 guid
= &DeviceList
[i
].guid
;
172 if(!DeviceList
[i
].name
)
176 device
->szDeviceName
= DeviceList
[0].name
;
178 //Initialise requested device
180 pData
= calloc(1, sizeof(DSoundData
));
183 SetALCError(ALC_OUT_OF_MEMORY
);
187 //DirectSound Init code
188 hr
= pDirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
190 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
194 IDirectSound_Release(pData
->lpDS
);
199 device
->ExtraData
= pData
;
203 static void DSoundClosePlayback(ALCdevice
*device
)
205 DSoundData
*pData
= device
->ExtraData
;
207 IDirectSound_Release(pData
->lpDS
);
209 device
->ExtraData
= NULL
;
212 static ALCboolean
DSoundStartContext(ALCdevice
*device
, ALCcontext
*context
)
214 DSoundData
*pData
= (DSoundData
*)device
->ExtraData
;
215 DSBUFFERDESC DSBDescription
;
216 WAVEFORMATEXTENSIBLE OutputType
;
223 memset(&OutputType
, 0, sizeof(OutputType
));
225 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
226 if(SUCCEEDED(hr
) && *(GetConfigValue(NULL
, "format", "")) != 0)
228 if(device
->Format
== AL_FORMAT_MONO8
|| device
->Format
== AL_FORMAT_MONO16
)
229 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_MONO
, 0);
230 else if(device
->Format
== AL_FORMAT_STEREO8
|| device
->Format
== AL_FORMAT_STEREO16
)
231 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, 0);
232 else if(device
->Format
== AL_FORMAT_QUAD8
|| device
->Format
== AL_FORMAT_QUAD16
)
233 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_QUAD
, 0);
234 else if(device
->Format
== AL_FORMAT_51CHN8
|| device
->Format
== AL_FORMAT_51CHN16
)
235 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, 0);
236 else if(device
->Format
== AL_FORMAT_71CHN8
|| device
->Format
== AL_FORMAT_71CHN16
)
237 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_7POINT1
, 0);
241 speakers
= DSSPEAKER_CONFIG(speakers
);
242 if(speakers
== DSSPEAKER_MONO
)
244 if(aluBytesFromFormat(device
->Format
) == 1)
245 format
= AL_FORMAT_MONO8
;
246 else if(aluBytesFromFormat(device
->Format
) == 2)
247 format
= AL_FORMAT_MONO16
;
248 else if(aluBytesFromFormat(device
->Format
) == 4)
249 format
= AL_FORMAT_MONO_FLOAT32
;
251 else if(speakers
== DSSPEAKER_STEREO
)
253 if(aluBytesFromFormat(device
->Format
) == 1)
254 format
= AL_FORMAT_STEREO8
;
255 else if(aluBytesFromFormat(device
->Format
) == 2)
256 format
= AL_FORMAT_STEREO16
;
257 else if(aluBytesFromFormat(device
->Format
) == 4)
258 format
= AL_FORMAT_STEREO_FLOAT32
;
260 else if(speakers
== DSSPEAKER_QUAD
)
262 if(aluBytesFromFormat(device
->Format
) == 1)
263 format
= AL_FORMAT_QUAD8
;
264 else if(aluBytesFromFormat(device
->Format
) == 2)
265 format
= AL_FORMAT_QUAD16
;
266 else if(aluBytesFromFormat(device
->Format
) == 4)
267 format
= AL_FORMAT_QUAD32
;
268 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
269 SPEAKER_FRONT_RIGHT
|
273 else if(speakers
== DSSPEAKER_5POINT1
)
275 if(aluBytesFromFormat(device
->Format
) == 1)
276 format
= AL_FORMAT_51CHN8
;
277 else if(aluBytesFromFormat(device
->Format
) == 2)
278 format
= AL_FORMAT_51CHN16
;
279 else if(aluBytesFromFormat(device
->Format
) == 4)
280 format
= AL_FORMAT_51CHN32
;
281 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
282 SPEAKER_FRONT_RIGHT
|
283 SPEAKER_FRONT_CENTER
|
284 SPEAKER_LOW_FREQUENCY
|
288 else if(speakers
== DSSPEAKER_7POINT1
)
290 if(aluBytesFromFormat(device
->Format
) == 1)
291 format
= AL_FORMAT_71CHN8
;
292 else if(aluBytesFromFormat(device
->Format
) == 2)
293 format
= AL_FORMAT_71CHN16
;
294 else if(aluBytesFromFormat(device
->Format
) == 4)
295 format
= AL_FORMAT_71CHN32
;
296 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
297 SPEAKER_FRONT_RIGHT
|
298 SPEAKER_FRONT_CENTER
|
299 SPEAKER_LOW_FREQUENCY
|
306 format
= device
->Format
;
307 frameSize
= aluBytesFromFormat(format
) * aluChannelsFromFormat(format
);
309 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
310 OutputType
.Format
.nChannels
= aluChannelsFromFormat(format
);
311 OutputType
.Format
.wBitsPerSample
= aluBytesFromFormat(format
) * 8;
312 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
313 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
314 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
315 OutputType
.Format
.cbSize
= 0;
318 if(OutputType
.Format
.nChannels
> 2 || OutputType
.Format
.wBitsPerSample
> 16)
320 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
321 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
322 OutputType
.Format
.cbSize
= 22;
323 if(OutputType
.Format
.wBitsPerSample
== 32)
324 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
326 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
332 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
333 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
334 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
335 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
338 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
343 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
344 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
345 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
346 DSBDescription
.dwBufferBytes
=(device
->BufferSize
/num_frags
) * num_frags
* frameSize
;
347 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
348 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
352 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
356 device
->ExtraData
= pData
;
357 pData
->thread
= StartThread(DSoundProc
, device
);
364 if (pData
->DSsbuffer
)
365 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
366 pData
->DSsbuffer
= NULL
;
367 if (pData
->DSpbuffer
)
368 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
369 pData
->DSpbuffer
= NULL
;
373 device
->Format
= format
;
374 device
->UpdateSize
= device
->BufferSize
/num_frags
;
379 static void DSoundStopContext(ALCdevice
*device
, ALCcontext
*context
)
381 DSoundData
*pData
= device
->ExtraData
;
388 StopThread(pData
->thread
);
389 pData
->thread
= NULL
;
391 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
392 pData
->DSsbuffer
= NULL
;
393 if (pData
->DSpbuffer
)
394 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
395 pData
->DSpbuffer
= NULL
;
399 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
)
406 static void DSoundCloseCapture(ALCdevice
*pDevice
)
411 static void DSoundStartCapture(ALCdevice
*pDevice
)
416 static void DSoundStopCapture(ALCdevice
*pDevice
)
421 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
428 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
435 BackendFuncs DSoundFuncs
= {
444 DSoundCaptureSamples
,
445 DSoundAvailableSamples
448 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
456 snprintf(str
, sizeof(str
), "DirectSound Software on %s", desc
);
457 DeviceList
[*iter
].name
= AppendAllDeviceList(str
);
458 DeviceList
[*iter
].guid
= *guid
;
462 DeviceList
[0].name
= AppendDeviceList("DirectSound Software");
467 void alcDSoundInit(BackendFuncs
*FuncList
)
472 *FuncList
= DSoundFuncs
;
475 ds_handle
= LoadLibraryA("dsound.dll");
476 if(ds_handle
== NULL
)
478 AL_PRINT("Failed to load dsound.dll\n");
482 #define LOAD_FUNC(f) do { \
483 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
486 FreeLibrary(ds_handle); \
488 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
493 ds_handle
= (void*)0xDEADBEEF;
494 #define LOAD_FUNC(f) p##f = f
497 LOAD_FUNC(DirectSoundCreate
);
498 LOAD_FUNC(DirectSoundEnumerateA
);
501 num_frags
= GetConfigValueInt("dsound", "periods", 4);
502 if(num_frags
< 2) num_frags
= 2;
504 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, &iter
);
506 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
509 void alcDSoundDeinit(void)