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
36 #ifndef DSSPEAKER_5POINT1
37 #define DSSPEAKER_5POINT1 6
39 #ifndef DSSPEAKER_7POINT1
40 #define DSSPEAKER_7POINT1 7
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
53 // DirectSound Playback Device
55 LPDIRECTSOUNDBUFFER DSpbuffer
;
56 LPDIRECTSOUNDBUFFER DSsbuffer
;
67 static DevMap DeviceList
[16];
70 static ALuint
DSoundProc(ALvoid
*ptr
)
72 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
73 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
77 VOID
*WritePtr1
, *WritePtr2
;
78 DWORD WriteCnt1
, WriteCnt2
;
83 memset(&DSBCaps
, 0, sizeof(DSBCaps
));
84 DSBCaps
.dwSize
= sizeof(DSBCaps
);
85 err
= IDirectSoundBuffer_GetCaps(pData
->DSsbuffer
, &DSBCaps
);
88 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err
);
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
;
105 avail
-= avail
%FragSize
;
107 // Lock output buffer
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
);
117 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
119 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
122 // Successfully locked the output buffer
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
);
135 AL_PRINT("Buffer lock error: %#lx\n", err
);
137 // Update old write cursor location
138 LastCursor
+= WriteCnt1
+WriteCnt2
;
139 LastCursor
%= DSBCaps
.dwBufferBytes
;
145 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
147 DSBUFFERDESC DSBDescription
;
148 DSoundData
*pData
= NULL
;
149 WAVEFORMATEXTENSIBLE OutputType
;
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 memset(&OutputType
, 0, sizeof(OutputType
));
180 //Initialise requested device
182 pData
= calloc(1, sizeof(DSoundData
));
185 SetALCError(ALC_OUT_OF_MEMORY
);
189 //DirectSound Init code
190 hr
= pDirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
192 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
196 if(*(GetConfigValue(NULL
, "format", "")) == 0)
197 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
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);
211 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
216 speakers
= DSSPEAKER_CONFIG(speakers
);
217 if(speakers
== DSSPEAKER_MONO
)
219 if(aluBytesFromFormat(device
->Format
) == 1)
220 format
= AL_FORMAT_MONO8
;
222 format
= AL_FORMAT_MONO16
;
224 else if(speakers
== DSSPEAKER_STEREO
)
226 if(aluBytesFromFormat(device
->Format
) == 1)
227 format
= AL_FORMAT_STEREO8
;
229 format
= AL_FORMAT_STEREO16
;
231 else if(speakers
== DSSPEAKER_QUAD
)
233 if(aluBytesFromFormat(device
->Format
) == 1)
234 format
= AL_FORMAT_QUAD8
;
236 format
= AL_FORMAT_QUAD16
;
237 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
238 SPEAKER_FRONT_RIGHT
|
242 else if(speakers
== DSSPEAKER_5POINT1
)
244 if(aluBytesFromFormat(device
->Format
) == 1)
245 format
= AL_FORMAT_51CHN8
;
247 format
= AL_FORMAT_51CHN16
;
248 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
249 SPEAKER_FRONT_RIGHT
|
250 SPEAKER_FRONT_CENTER
|
251 SPEAKER_LOW_FREQUENCY
|
255 else if(speakers
== DSSPEAKER_7POINT1
)
257 if(aluBytesFromFormat(device
->Format
) == 1)
258 format
= AL_FORMAT_71CHN8
;
260 format
= AL_FORMAT_71CHN16
;
261 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
262 SPEAKER_FRONT_RIGHT
|
263 SPEAKER_FRONT_CENTER
|
264 SPEAKER_LOW_FREQUENCY
|
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
;
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
);
300 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
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
);
314 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
318 device
->ExtraData
= pData
;
319 pData
->thread
= StartThread(DSoundProc
, device
);
326 if (pData
->DSsbuffer
)
327 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
328 if (pData
->DSpbuffer
)
329 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
331 IDirectSound_Release(pData
->lpDS
);
337 device
->Format
= format
;
338 device
->UpdateSize
/= num_frags
;
343 static void DSoundClosePlayback(ALCdevice
*device
)
345 DSoundData
*pData
= device
->ExtraData
;
348 StopThread(pData
->thread
);
350 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
351 if (pData
->DSpbuffer
)
352 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
353 IDirectSound_Release(pData
->lpDS
);
356 device
->ExtraData
= NULL
;
360 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
370 static void DSoundCloseCapture(ALCdevice
*pDevice
)
375 static void DSoundStartCapture(ALCdevice
*pDevice
)
380 static void DSoundStopCapture(ALCdevice
*pDevice
)
385 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
392 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
399 BackendFuncs DSoundFuncs
= {
406 DSoundCaptureSamples
,
407 DSoundAvailableSamples
410 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
418 snprintf(str
, sizeof(str
), "DirectSound Software on %s", desc
);
419 DeviceList
[*iter
].name
= AppendAllDeviceList(str
);
420 DeviceList
[*iter
].guid
= *guid
;
424 DeviceList
[0].name
= AppendDeviceList("DirectSound Software");
429 void alcDSoundInit(BackendFuncs
*FuncList
)
434 *FuncList
= DSoundFuncs
;
437 ds_handle
= LoadLibraryA("dsound.dll");
438 if(ds_handle
== NULL
)
440 AL_PRINT("Failed to load dsound.dll\n");
444 #define LOAD_FUNC(f) do { \
445 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
448 FreeLibrary(ds_handle); \
450 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
455 ds_handle
= (void*)0xDEADBEEF;
456 #define LOAD_FUNC(f) p##f = f
459 LOAD_FUNC(DirectSoundCreate
);
460 LOAD_FUNC(DirectSoundEnumerateA
);
463 num_frags
= GetConfigValueInt("dsound", "periods", 4);
464 if(num_frags
< 2) num_frags
= 2;
466 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, &iter
);
468 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);