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
37 #ifndef DSSPEAKER_7POINT1
38 #define DSSPEAKER_7POINT1 7
41 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
43 // Since DSound doesn't report the fragment size, just assume 4 fragments
47 // DirectSound Playback Device
49 LPDIRECTSOUNDBUFFER DSpbuffer
;
50 LPDIRECTSOUNDBUFFER DSsbuffer
;
61 static DevMap DeviceList
[16];
64 static ALuint
DSoundProc(ALvoid
*ptr
)
66 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
67 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
70 VOID
*WritePtr1
, *WritePtr2
;
71 DWORD WriteCnt1
, WriteCnt2
;
76 BufferSize
= pDevice
->UpdateFreq
*DS_FRAGS
*pDevice
->FrameSize
;
78 while(!pData
->killNow
)
80 // Get current play and write cursors
81 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
82 avail
= (PlayCursor
-LastCursor
+BufferSize
) % BufferSize
;
93 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
95 // If the buffer is lost, restore it, play and lock
96 if(err
== DSERR_BUFFERLOST
)
98 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
100 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
102 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
105 // Successfully locked the output buffer
108 // If we have an active context, mix data directly into output buffer otherwise fill with silence
109 SuspendContext(NULL
);
110 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
111 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
112 ProcessContext(NULL
);
114 // Unlock output buffer only when successfully locked
115 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
118 AL_PRINT("Buffer lock error: %#lx\n", err
);
120 // Update old write cursor location
121 LastCursor
+= WriteCnt1
+WriteCnt2
;
122 LastCursor
%= BufferSize
;
128 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
130 DSBUFFERDESC DSBDescription
;
131 DSoundData
*pData
= NULL
;
132 WAVEFORMATEXTENSIBLE OutputType
;
140 for(i
= 0;DeviceList
[i
].name
;i
++)
142 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
144 device
->szDeviceName
= DeviceList
[i
].name
;
146 guid
= &DeviceList
[i
].guid
;
150 if(!DeviceList
[i
].name
)
154 device
->szDeviceName
= DeviceList
[0].name
;
156 memset(&OutputType
, 0, sizeof(OutputType
));
158 //Initialise requested device
160 pData
= calloc(1, sizeof(DSoundData
));
163 SetALCError(ALC_OUT_OF_MEMORY
);
167 //DirectSound Init code
168 hr
= DirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
170 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
173 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
176 speakers
= DSSPEAKER_CONFIG(speakers
);
177 if(speakers
== DSSPEAKER_MONO
)
179 if(aluBytesFromFormat(device
->Format
) == 1)
180 device
->Format
= AL_FORMAT_MONO8
;
182 device
->Format
= AL_FORMAT_MONO16
;
184 else if(speakers
== DSSPEAKER_STEREO
)
186 if(aluBytesFromFormat(device
->Format
) == 1)
187 device
->Format
= AL_FORMAT_STEREO8
;
189 device
->Format
= AL_FORMAT_STEREO16
;
191 else if(speakers
== DSSPEAKER_QUAD
)
193 if(aluBytesFromFormat(device
->Format
) == 1)
194 device
->Format
= AL_FORMAT_QUAD8
;
196 device
->Format
= AL_FORMAT_QUAD16
;
197 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
198 SPEAKER_FRONT_RIGHT
|
202 else if(speakers
== DSSPEAKER_5POINT1
)
204 if(aluBytesFromFormat(device
->Format
) == 1)
205 device
->Format
= AL_FORMAT_51CHN8
;
207 device
->Format
= AL_FORMAT_51CHN16
;
208 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
209 SPEAKER_FRONT_RIGHT
|
210 SPEAKER_FRONT_CENTER
|
211 SPEAKER_LOW_FREQUENCY
|
215 else if(speakers
== DSSPEAKER_7POINT1
)
217 if(aluBytesFromFormat(device
->Format
) == 1)
218 device
->Format
= AL_FORMAT_71CHN8
;
220 device
->Format
= AL_FORMAT_71CHN16
;
221 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
222 SPEAKER_FRONT_RIGHT
|
223 SPEAKER_FRONT_CENTER
|
224 SPEAKER_LOW_FREQUENCY
|
230 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
231 aluChannelsFromFormat(device
->Format
);
233 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
234 OutputType
.Format
.nChannels
= aluChannelsFromFormat(device
->Format
);
235 OutputType
.Format
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
236 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
237 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
238 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
239 OutputType
.Format
.cbSize
= 0;
241 device
->UpdateFreq
/= DS_FRAGS
;
244 if(OutputType
.Format
.nChannels
> 2)
246 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
247 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
248 OutputType
.Format
.cbSize
= 22;
249 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
255 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
256 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
257 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
258 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
261 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
266 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
267 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
268 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
269 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* DS_FRAGS
* device
->FrameSize
;
270 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
271 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
275 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
277 device
->ExtraData
= pData
;
278 pData
->thread
= StartThread(DSoundProc
, device
);
284 if (pData
->DSsbuffer
)
285 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
286 if (pData
->DSpbuffer
)
287 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
289 IDirectSound_Release(pData
->lpDS
);
298 static void DSoundClosePlayback(ALCdevice
*device
)
300 DSoundData
*pData
= device
->ExtraData
;
303 StopThread(pData
->thread
);
305 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
306 if (pData
->DSpbuffer
)
307 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
308 IDirectSound_Release(pData
->lpDS
);
311 device
->ExtraData
= NULL
;
315 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
325 static void DSoundCloseCapture(ALCdevice
*pDevice
)
330 static void DSoundStartCapture(ALCdevice
*pDevice
)
335 static void DSoundStopCapture(ALCdevice
*pDevice
)
340 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
347 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
354 BackendFuncs DSoundFuncs
= {
361 DSoundCaptureSamples
,
362 DSoundAvailableSamples
365 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
374 snprintf(str
, sizeof(str
), "DirectSound Software on %s", desc
);
375 DeviceList
[i
].name
= AppendAllDeviceList(str
);
376 DeviceList
[i
].guid
= *guid
;
380 DeviceList
[0].name
= AppendDeviceList("DirectSound Software");
385 void alcDSoundInit(BackendFuncs
*FuncList
)
389 *FuncList
= DSoundFuncs
;
391 hr
= DirectSoundEnumerate(DSoundEnumDevices
, NULL
);
393 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);