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 // Since DSound doesn't report the fragment size, just assume 4 fragments
46 // DirectSound Playback Device
48 LPDIRECTSOUNDBUFFER DSpbuffer
;
49 LPDIRECTSOUNDBUFFER DSsbuffer
;
60 static DevMap DeviceList
[16];
63 static ALuint
DSoundProc(ALvoid
*ptr
)
65 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
66 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
69 VOID
*WritePtr1
, *WritePtr2
;
70 DWORD WriteCnt1
, WriteCnt2
;
75 BufferSize
= pDevice
->UpdateSize
* DS_FRAGS
*
76 aluBytesFromFormat(pDevice
->Format
) *
77 aluChannelsFromFormat(pDevice
->Format
);
79 while(!pData
->killNow
)
81 // Get current play and write cursors
82 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
83 avail
= (PlayCursor
-LastCursor
+BufferSize
) % BufferSize
;
94 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
96 // If the buffer is lost, restore it, play and lock
97 if(err
== DSERR_BUFFERLOST
)
99 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
101 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
103 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
106 // Successfully locked the output buffer
109 // If we have an active context, mix data directly into output buffer otherwise fill with silence
110 SuspendContext(NULL
);
111 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
112 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
113 ProcessContext(NULL
);
115 // Unlock output buffer only when successfully locked
116 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
119 AL_PRINT("Buffer lock error: %#lx\n", err
);
121 // Update old write cursor location
122 LastCursor
+= WriteCnt1
+WriteCnt2
;
123 LastCursor
%= BufferSize
;
129 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
131 DSBUFFERDESC DSBDescription
;
132 DSoundData
*pData
= NULL
;
133 WAVEFORMATEXTENSIBLE OutputType
;
142 for(i
= 0;DeviceList
[i
].name
;i
++)
144 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
146 device
->szDeviceName
= DeviceList
[i
].name
;
148 guid
= &DeviceList
[i
].guid
;
152 if(!DeviceList
[i
].name
)
156 device
->szDeviceName
= DeviceList
[0].name
;
158 memset(&OutputType
, 0, sizeof(OutputType
));
160 //Initialise requested device
162 pData
= calloc(1, sizeof(DSoundData
));
165 SetALCError(ALC_OUT_OF_MEMORY
);
169 //DirectSound Init code
170 hr
= DirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
172 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
176 if(*(GetConfigValue(NULL
, "format", "")) == 0)
177 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
180 if(device
->Format
== AL_FORMAT_MONO8
|| device
->Format
== AL_FORMAT_MONO16
)
181 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_MONO
, 0);
182 else if(device
->Format
== AL_FORMAT_STEREO8
|| device
->Format
== AL_FORMAT_STEREO16
)
183 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, 0);
184 else if(device
->Format
== AL_FORMAT_QUAD8
|| device
->Format
== AL_FORMAT_QUAD16
)
185 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_QUAD
, 0);
186 else if(device
->Format
== AL_FORMAT_51CHN8
|| device
->Format
== AL_FORMAT_51CHN16
)
187 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, 0);
188 else if(device
->Format
== AL_FORMAT_71CHN8
|| device
->Format
== AL_FORMAT_71CHN16
)
189 speakers
= DSSPEAKER_COMBINED(DSSPEAKER_7POINT1
, 0);
191 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
196 speakers
= DSSPEAKER_CONFIG(speakers
);
197 if(speakers
== DSSPEAKER_MONO
)
199 if(aluBytesFromFormat(device
->Format
) == 1)
200 device
->Format
= AL_FORMAT_MONO8
;
202 device
->Format
= AL_FORMAT_MONO16
;
204 else if(speakers
== DSSPEAKER_STEREO
)
206 if(aluBytesFromFormat(device
->Format
) == 1)
207 device
->Format
= AL_FORMAT_STEREO8
;
209 device
->Format
= AL_FORMAT_STEREO16
;
211 else if(speakers
== DSSPEAKER_QUAD
)
213 if(aluBytesFromFormat(device
->Format
) == 1)
214 device
->Format
= AL_FORMAT_QUAD8
;
216 device
->Format
= AL_FORMAT_QUAD16
;
217 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
218 SPEAKER_FRONT_RIGHT
|
222 else if(speakers
== DSSPEAKER_5POINT1
)
224 if(aluBytesFromFormat(device
->Format
) == 1)
225 device
->Format
= AL_FORMAT_51CHN8
;
227 device
->Format
= AL_FORMAT_51CHN16
;
228 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
229 SPEAKER_FRONT_RIGHT
|
230 SPEAKER_FRONT_CENTER
|
231 SPEAKER_LOW_FREQUENCY
|
235 else if(speakers
== DSSPEAKER_7POINT1
)
237 if(aluBytesFromFormat(device
->Format
) == 1)
238 device
->Format
= AL_FORMAT_71CHN8
;
240 device
->Format
= AL_FORMAT_71CHN16
;
241 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
242 SPEAKER_FRONT_RIGHT
|
243 SPEAKER_FRONT_CENTER
|
244 SPEAKER_LOW_FREQUENCY
|
250 frameSize
= aluBytesFromFormat(device
->Format
) *
251 aluChannelsFromFormat(device
->Format
);
253 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
254 OutputType
.Format
.nChannels
= aluChannelsFromFormat(device
->Format
);
255 OutputType
.Format
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
256 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
257 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
258 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
259 OutputType
.Format
.cbSize
= 0;
261 device
->UpdateSize
/= DS_FRAGS
;
264 if(OutputType
.Format
.nChannels
> 2)
266 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
267 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
268 OutputType
.Format
.cbSize
= 22;
269 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
275 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
276 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
277 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
278 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
281 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
286 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
287 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
288 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
289 DSBDescription
.dwBufferBytes
=device
->UpdateSize
* DS_FRAGS
* frameSize
;
290 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
291 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
295 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
299 device
->ExtraData
= pData
;
300 pData
->thread
= StartThread(DSoundProc
, device
);
307 if (pData
->DSsbuffer
)
308 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
309 if (pData
->DSpbuffer
)
310 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
312 IDirectSound_Release(pData
->lpDS
);
321 static void DSoundClosePlayback(ALCdevice
*device
)
323 DSoundData
*pData
= device
->ExtraData
;
326 StopThread(pData
->thread
);
328 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
329 if (pData
->DSpbuffer
)
330 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
331 IDirectSound_Release(pData
->lpDS
);
334 device
->ExtraData
= NULL
;
338 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
348 static void DSoundCloseCapture(ALCdevice
*pDevice
)
353 static void DSoundStartCapture(ALCdevice
*pDevice
)
358 static void DSoundStopCapture(ALCdevice
*pDevice
)
363 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
370 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
377 BackendFuncs DSoundFuncs
= {
384 DSoundCaptureSamples
,
385 DSoundAvailableSamples
388 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
396 snprintf(str
, sizeof(str
), "DirectSound Software on %s", desc
);
397 DeviceList
[*iter
].name
= AppendAllDeviceList(str
);
398 DeviceList
[*iter
].guid
= *guid
;
402 DeviceList
[0].name
= AppendDeviceList("DirectSound Software");
407 void alcDSoundInit(BackendFuncs
*FuncList
)
412 *FuncList
= DSoundFuncs
;
414 hr
= DirectSoundEnumerate(DSoundEnumDevices
, &iter
);
416 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);