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
;
76 VOID
*WritePtr1
, *WritePtr2
;
77 DWORD WriteCnt1
, WriteCnt2
;
82 BufferSize
= pDevice
->UpdateSize
* num_frags
*
83 aluBytesFromFormat(pDevice
->Format
) *
84 aluChannelsFromFormat(pDevice
->Format
);
86 while(!pData
->killNow
)
88 // Get current play and write cursors
89 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
90 avail
= (PlayCursor
-LastCursor
+BufferSize
) % BufferSize
;
92 if(avail
< BufferSize
/num_frags
)
101 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
103 // If the buffer is lost, restore it, play and lock
104 if(err
== DSERR_BUFFERLOST
)
106 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
108 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
110 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
113 // Successfully locked the output buffer
116 // If we have an active context, mix data directly into output buffer otherwise fill with silence
117 SuspendContext(NULL
);
118 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
119 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
120 ProcessContext(NULL
);
122 // Unlock output buffer only when successfully locked
123 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
126 AL_PRINT("Buffer lock error: %#lx\n", err
);
128 // Update old write cursor location
129 LastCursor
+= WriteCnt1
+WriteCnt2
;
130 LastCursor
%= BufferSize
;
136 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
138 DSBUFFERDESC DSBDescription
;
139 DSoundData
*pData
= NULL
;
140 WAVEFORMATEXTENSIBLE OutputType
;
146 if(ds_handle
== NULL
)
152 for(i
= 0;DeviceList
[i
].name
;i
++)
154 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
156 device
->szDeviceName
= DeviceList
[i
].name
;
158 guid
= &DeviceList
[i
].guid
;
162 if(!DeviceList
[i
].name
)
166 device
->szDeviceName
= DeviceList
[0].name
;
168 memset(&OutputType
, 0, sizeof(OutputType
));
170 //Initialise requested device
172 pData
= calloc(1, sizeof(DSoundData
));
175 SetALCError(ALC_OUT_OF_MEMORY
);
179 //DirectSound Init code
180 hr
= pDirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
182 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
186 if(*(GetConfigValue(NULL
, "format", "")) == 0)
187 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
190 if(device
->Format
== AL_FORMAT_MONO8
|| device
->Format
== AL_FORMAT_MONO16
)
191 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_MONO
, 0);
192 else if(device
->Format
== AL_FORMAT_STEREO8
|| device
->Format
== AL_FORMAT_STEREO16
)
193 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, 0);
194 else if(device
->Format
== AL_FORMAT_QUAD8
|| device
->Format
== AL_FORMAT_QUAD16
)
195 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_QUAD
, 0);
196 else if(device
->Format
== AL_FORMAT_51CHN8
|| device
->Format
== AL_FORMAT_51CHN16
)
197 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, 0);
198 else if(device
->Format
== AL_FORMAT_71CHN8
|| device
->Format
== AL_FORMAT_71CHN16
)
199 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_7POINT1
, 0);
201 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
206 speakers
= DSSPEAKER_CONFIG(speakers
);
207 if(speakers
== DSSPEAKER_MONO
)
209 if(aluBytesFromFormat(device
->Format
) == 1)
210 device
->Format
= AL_FORMAT_MONO8
;
212 device
->Format
= AL_FORMAT_MONO16
;
214 else if(speakers
== DSSPEAKER_STEREO
)
216 if(aluBytesFromFormat(device
->Format
) == 1)
217 device
->Format
= AL_FORMAT_STEREO8
;
219 device
->Format
= AL_FORMAT_STEREO16
;
221 else if(speakers
== DSSPEAKER_QUAD
)
223 if(aluBytesFromFormat(device
->Format
) == 1)
224 device
->Format
= AL_FORMAT_QUAD8
;
226 device
->Format
= AL_FORMAT_QUAD16
;
227 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
228 SPEAKER_FRONT_RIGHT
|
232 else if(speakers
== DSSPEAKER_5POINT1
)
234 if(aluBytesFromFormat(device
->Format
) == 1)
235 device
->Format
= AL_FORMAT_51CHN8
;
237 device
->Format
= AL_FORMAT_51CHN16
;
238 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
239 SPEAKER_FRONT_RIGHT
|
240 SPEAKER_FRONT_CENTER
|
241 SPEAKER_LOW_FREQUENCY
|
245 else if(speakers
== DSSPEAKER_7POINT1
)
247 if(aluBytesFromFormat(device
->Format
) == 1)
248 device
->Format
= AL_FORMAT_71CHN8
;
250 device
->Format
= AL_FORMAT_71CHN16
;
251 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
252 SPEAKER_FRONT_RIGHT
|
253 SPEAKER_FRONT_CENTER
|
254 SPEAKER_LOW_FREQUENCY
|
260 frameSize
= aluBytesFromFormat(device
->Format
) *
261 aluChannelsFromFormat(device
->Format
);
263 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
264 OutputType
.Format
.nChannels
= aluChannelsFromFormat(device
->Format
);
265 OutputType
.Format
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
266 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
267 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
268 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
269 OutputType
.Format
.cbSize
= 0;
271 device
->UpdateSize
/= num_frags
;
274 if(OutputType
.Format
.nChannels
> 2)
276 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
277 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
278 OutputType
.Format
.cbSize
= 22;
279 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
285 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
286 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
287 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
288 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
291 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
296 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
297 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
298 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
299 DSBDescription
.dwBufferBytes
=device
->UpdateSize
* num_frags
* frameSize
;
300 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
301 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
305 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
309 device
->ExtraData
= pData
;
310 pData
->thread
= StartThread(DSoundProc
, device
);
317 if (pData
->DSsbuffer
)
318 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
319 if (pData
->DSpbuffer
)
320 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
322 IDirectSound_Release(pData
->lpDS
);
331 static void DSoundClosePlayback(ALCdevice
*device
)
333 DSoundData
*pData
= device
->ExtraData
;
336 StopThread(pData
->thread
);
338 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
339 if (pData
->DSpbuffer
)
340 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
341 IDirectSound_Release(pData
->lpDS
);
344 device
->ExtraData
= NULL
;
348 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
358 static void DSoundCloseCapture(ALCdevice
*pDevice
)
363 static void DSoundStartCapture(ALCdevice
*pDevice
)
368 static void DSoundStopCapture(ALCdevice
*pDevice
)
373 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
380 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
387 BackendFuncs DSoundFuncs
= {
394 DSoundCaptureSamples
,
395 DSoundAvailableSamples
398 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
406 snprintf(str
, sizeof(str
), "DirectSound Software on %s", desc
);
407 DeviceList
[*iter
].name
= AppendAllDeviceList(str
);
408 DeviceList
[*iter
].guid
= *guid
;
412 DeviceList
[0].name
= AppendDeviceList("DirectSound Software");
417 void alcDSoundInit(BackendFuncs
*FuncList
)
422 *FuncList
= DSoundFuncs
;
425 ds_handle
= LoadLibraryA("dsound.dll");
426 if(ds_handle
== NULL
)
428 AL_PRINT("Failed to load dsound.dll\n");
432 #define LOAD_FUNC(f) do { \
433 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
436 FreeLibrary(ds_handle); \
438 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
443 ds_handle
= (void*)0xDEADBEEF;
444 #define LOAD_FUNC(f) p##f = f
447 LOAD_FUNC(DirectSoundCreate
);
448 LOAD_FUNC(DirectSoundEnumerateA
);
451 num_frags
= GetConfigValueInt("dsound", "periods", 4);
452 if(num_frags
< 2) num_frags
= 2;
454 hr
= pDirectSoundEnumerateA(DSoundEnumDevices
, &iter
);
456 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);