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_7POINT1
37 #define DSSPEAKER_7POINT1 7
40 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
42 static void *ds_handle
;
43 static HRESULT (WINAPI
*pDirectSoundCreate
)(LPCGUID pcGuidDevice
, LPDIRECTSOUND
*ppDS
, LPUNKNOWN pUnkOuter
);
44 static HRESULT (WINAPI
*pDirectSoundEnumerateA
)(LPDSENUMCALLBACKA pDSEnumCallback
, LPVOID pContext
);
46 // Since DSound doesn't report the fragment size, emulate it
50 // DirectSound Playback Device
52 LPDIRECTSOUNDBUFFER DSpbuffer
;
53 LPDIRECTSOUNDBUFFER DSsbuffer
;
64 static DevMap DeviceList
[16];
67 static ALuint
DSoundProc(ALvoid
*ptr
)
69 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
70 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
73 VOID
*WritePtr1
, *WritePtr2
;
74 DWORD WriteCnt1
, WriteCnt2
;
79 BufferSize
= pDevice
->UpdateSize
* num_frags
*
80 aluBytesFromFormat(pDevice
->Format
) *
81 aluChannelsFromFormat(pDevice
->Format
);
83 while(!pData
->killNow
)
85 // Get current play and write cursors
86 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
87 avail
= (PlayCursor
-LastCursor
+BufferSize
) % BufferSize
;
89 if(avail
< BufferSize
/num_frags
)
98 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
100 // If the buffer is lost, restore it, play and lock
101 if(err
== DSERR_BUFFERLOST
)
103 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
105 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
107 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
110 // Successfully locked the output buffer
113 // If we have an active context, mix data directly into output buffer otherwise fill with silence
114 SuspendContext(NULL
);
115 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
116 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
117 ProcessContext(NULL
);
119 // Unlock output buffer only when successfully locked
120 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
123 AL_PRINT("Buffer lock error: %#lx\n", err
);
125 // Update old write cursor location
126 LastCursor
+= WriteCnt1
+WriteCnt2
;
127 LastCursor
%= BufferSize
;
133 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
135 DSBUFFERDESC DSBDescription
;
136 DSoundData
*pData
= NULL
;
137 WAVEFORMATEXTENSIBLE OutputType
;
143 if(ds_handle
== NULL
)
149 for(i
= 0;DeviceList
[i
].name
;i
++)
151 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
153 device
->szDeviceName
= DeviceList
[i
].name
;
155 guid
= &DeviceList
[i
].guid
;
159 if(!DeviceList
[i
].name
)
163 device
->szDeviceName
= DeviceList
[0].name
;
165 memset(&OutputType
, 0, sizeof(OutputType
));
167 //Initialise requested device
169 pData
= calloc(1, sizeof(DSoundData
));
172 SetALCError(ALC_OUT_OF_MEMORY
);
176 //DirectSound Init code
177 hr
= pDirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
179 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
183 if(*(GetConfigValue(NULL
, "format", "")) == 0)
184 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
187 if(device
->Format
== AL_FORMAT_MONO8
|| device
->Format
== AL_FORMAT_MONO16
)
188 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_MONO
, 0);
189 else if(device
->Format
== AL_FORMAT_STEREO8
|| device
->Format
== AL_FORMAT_STEREO16
)
190 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, 0);
191 else if(device
->Format
== AL_FORMAT_QUAD8
|| device
->Format
== AL_FORMAT_QUAD16
)
192 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_QUAD
, 0);
193 else if(device
->Format
== AL_FORMAT_51CHN8
|| device
->Format
== AL_FORMAT_51CHN16
)
194 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, 0);
195 else if(device
->Format
== AL_FORMAT_71CHN8
|| device
->Format
== AL_FORMAT_71CHN16
)
196 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_7POINT1
, 0);
198 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
203 speakers
= DSSPEAKER_CONFIG(speakers
);
204 if(speakers
== DSSPEAKER_MONO
)
206 if(aluBytesFromFormat(device
->Format
) == 1)
207 device
->Format
= AL_FORMAT_MONO8
;
209 device
->Format
= AL_FORMAT_MONO16
;
211 else if(speakers
== DSSPEAKER_STEREO
)
213 if(aluBytesFromFormat(device
->Format
) == 1)
214 device
->Format
= AL_FORMAT_STEREO8
;
216 device
->Format
= AL_FORMAT_STEREO16
;
218 else if(speakers
== DSSPEAKER_QUAD
)
220 if(aluBytesFromFormat(device
->Format
) == 1)
221 device
->Format
= AL_FORMAT_QUAD8
;
223 device
->Format
= AL_FORMAT_QUAD16
;
224 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
225 SPEAKER_FRONT_RIGHT
|
229 else if(speakers
== DSSPEAKER_5POINT1
)
231 if(aluBytesFromFormat(device
->Format
) == 1)
232 device
->Format
= AL_FORMAT_51CHN8
;
234 device
->Format
= AL_FORMAT_51CHN16
;
235 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
236 SPEAKER_FRONT_RIGHT
|
237 SPEAKER_FRONT_CENTER
|
238 SPEAKER_LOW_FREQUENCY
|
242 else if(speakers
== DSSPEAKER_7POINT1
)
244 if(aluBytesFromFormat(device
->Format
) == 1)
245 device
->Format
= AL_FORMAT_71CHN8
;
247 device
->Format
= AL_FORMAT_71CHN16
;
248 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
249 SPEAKER_FRONT_RIGHT
|
250 SPEAKER_FRONT_CENTER
|
251 SPEAKER_LOW_FREQUENCY
|
257 frameSize
= aluBytesFromFormat(device
->Format
) *
258 aluChannelsFromFormat(device
->Format
);
260 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
261 OutputType
.Format
.nChannels
= aluChannelsFromFormat(device
->Format
);
262 OutputType
.Format
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
263 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
264 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
265 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
266 OutputType
.Format
.cbSize
= 0;
268 device
->UpdateSize
/= num_frags
;
271 if(OutputType
.Format
.nChannels
> 2)
273 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
274 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
275 OutputType
.Format
.cbSize
= 22;
276 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
282 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
283 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
284 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
285 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
288 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
293 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
294 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
295 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
296 DSBDescription
.dwBufferBytes
=device
->UpdateSize
* num_frags
* frameSize
;
297 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
298 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
302 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
306 device
->ExtraData
= pData
;
307 pData
->thread
= StartThread(DSoundProc
, device
);
314 if (pData
->DSsbuffer
)
315 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
316 if (pData
->DSpbuffer
)
317 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
319 IDirectSound_Release(pData
->lpDS
);
328 static void DSoundClosePlayback(ALCdevice
*device
)
330 DSoundData
*pData
= device
->ExtraData
;
333 StopThread(pData
->thread
);
335 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
336 if (pData
->DSpbuffer
)
337 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
338 IDirectSound_Release(pData
->lpDS
);
341 device
->ExtraData
= NULL
;
345 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
355 static void DSoundCloseCapture(ALCdevice
*pDevice
)
360 static void DSoundStartCapture(ALCdevice
*pDevice
)
365 static void DSoundStopCapture(ALCdevice
*pDevice
)
370 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
377 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
384 BackendFuncs DSoundFuncs
= {
391 DSoundCaptureSamples
,
392 DSoundAvailableSamples
395 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
403 snprintf(str
, sizeof(str
), "DirectSound Software on %s", desc
);
404 DeviceList
[*iter
].name
= AppendAllDeviceList(str
);
405 DeviceList
[*iter
].guid
= *guid
;
409 DeviceList
[0].name
= AppendDeviceList("DirectSound Software");
414 void alcDSoundInit(BackendFuncs
*FuncList
)
419 *FuncList
= DSoundFuncs
;
422 ds_handle
= LoadLibraryA("dsound.dll");
423 if(ds_handle
== NULL
)
425 AL_PRINT("Failed to load dsound.dll\n");
429 #define LOAD_FUNC(f) do { \
430 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
433 FreeLibrary(ds_handle); \
435 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
440 ds_handle
= (void*)0xDEADBEEF;
441 #define LOAD_FUNC(f) p##f = f
444 LOAD_FUNC(DirectSoundCreate
);
445 LOAD_FUNC(DirectSoundEnumerateA
);
448 num_frags
= GetConfigValueInt("dsound", "periods", 4);
449 if(num_frags
< 2) num_frags
= 2;
451 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, &iter
);
453 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);