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);
44 // DirectSound Playback Device
46 LPDIRECTSOUNDBUFFER DSpbuffer
;
47 LPDIRECTSOUNDBUFFER DSsbuffer
;
58 static DevMap DeviceList
[16];
61 static ALuint
DSoundProc(ALvoid
*ptr
)
63 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
64 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
67 VOID
*WritePtr1
, *WritePtr2
;
68 DWORD WriteCnt1
, WriteCnt2
;
73 BufferSize
= pDevice
->UpdateFreq
*pDevice
->FrameSize
;
75 while(!pData
->killNow
)
77 // Get current play and write cursors
78 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
79 avail
= (PlayCursor
-LastCursor
+BufferSize
) % BufferSize
;
90 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
92 // If the buffer is lost, restore it, play and lock
93 if(err
== DSERR_BUFFERLOST
)
95 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
97 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
99 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
102 // Successfully locked the output buffer
105 // If we have an active context, mix data directly into output buffer otherwise fill with silence
106 SuspendContext(NULL
);
107 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
108 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
109 ProcessContext(NULL
);
111 // Unlock output buffer only when successfully locked
112 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
115 AL_PRINT("Buffer lock error: %#lx\n", err
);
117 // Update old write cursor location
118 LastCursor
+= WriteCnt1
+WriteCnt2
;
119 LastCursor
%= BufferSize
;
125 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
127 DSBUFFERDESC DSBDescription
;
128 DSoundData
*pData
= NULL
;
129 WAVEFORMATEXTENSIBLE OutputType
;
137 for(i
= 0;DeviceList
[i
].name
;i
++)
139 if(strcmp(deviceName
, DeviceList
[i
].name
) == 0)
141 device
->szDeviceName
= DeviceList
[i
].name
;
143 guid
= &DeviceList
[i
].guid
;
147 if(!DeviceList
[i
].name
)
151 device
->szDeviceName
= DeviceList
[0].name
;
153 memset(&OutputType
, 0, sizeof(OutputType
));
155 //Initialise requested device
157 pData
= calloc(1, sizeof(DSoundData
));
160 SetALCError(ALC_OUT_OF_MEMORY
);
164 //DirectSound Init code
165 hr
= DirectSoundCreate(guid
, &pData
->lpDS
, NULL
);
167 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
170 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
173 speakers
= DSSPEAKER_CONFIG(speakers
);
174 if(speakers
== DSSPEAKER_MONO
)
176 if(aluBytesFromFormat(device
->Format
) == 1)
177 device
->Format
= AL_FORMAT_MONO8
;
179 device
->Format
= AL_FORMAT_MONO16
;
181 else if(speakers
== DSSPEAKER_STEREO
)
183 if(aluBytesFromFormat(device
->Format
) == 1)
184 device
->Format
= AL_FORMAT_STEREO8
;
186 device
->Format
= AL_FORMAT_STEREO16
;
188 else if(speakers
== DSSPEAKER_QUAD
)
190 if(aluBytesFromFormat(device
->Format
) == 1)
191 device
->Format
= AL_FORMAT_QUAD8
;
193 device
->Format
= AL_FORMAT_QUAD16
;
194 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
195 SPEAKER_FRONT_RIGHT
|
199 else if(speakers
== DSSPEAKER_5POINT1
)
201 if(aluBytesFromFormat(device
->Format
) == 1)
202 device
->Format
= AL_FORMAT_51CHN8
;
204 device
->Format
= AL_FORMAT_51CHN16
;
205 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
206 SPEAKER_FRONT_RIGHT
|
207 SPEAKER_FRONT_CENTER
|
208 SPEAKER_LOW_FREQUENCY
|
212 else if(speakers
== DSSPEAKER_7POINT1
)
214 if(aluBytesFromFormat(device
->Format
) == 1)
215 device
->Format
= AL_FORMAT_71CHN8
;
217 device
->Format
= AL_FORMAT_71CHN16
;
218 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
219 SPEAKER_FRONT_RIGHT
|
220 SPEAKER_FRONT_CENTER
|
221 SPEAKER_LOW_FREQUENCY
|
227 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
228 aluChannelsFromFormat(device
->Format
);
230 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
231 OutputType
.Format
.nChannels
= aluChannelsFromFormat(device
->Format
);
232 OutputType
.Format
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
233 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
234 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
235 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
236 OutputType
.Format
.cbSize
= 0;
239 if(OutputType
.Format
.nChannels
> 2)
241 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
242 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
243 OutputType
.Format
.cbSize
= 22;
244 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
250 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
251 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
252 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
253 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
256 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
.Format
);
261 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
262 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
263 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
264 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* device
->FrameSize
;
265 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
266 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
270 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
272 device
->ExtraData
= pData
;
273 pData
->thread
= StartThread(DSoundProc
, device
);
279 if (pData
->DSsbuffer
)
280 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
281 if (pData
->DSpbuffer
)
282 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
284 IDirectSound_Release(pData
->lpDS
);
293 static void DSoundClosePlayback(ALCdevice
*device
)
295 DSoundData
*pData
= device
->ExtraData
;
298 StopThread(pData
->thread
);
300 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
301 if (pData
->DSpbuffer
)
302 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
303 IDirectSound_Release(pData
->lpDS
);
306 device
->ExtraData
= NULL
;
310 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
320 static void DSoundCloseCapture(ALCdevice
*pDevice
)
325 static void DSoundStartCapture(ALCdevice
*pDevice
)
330 static void DSoundStopCapture(ALCdevice
*pDevice
)
335 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
342 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
349 BackendFuncs DSoundFuncs
= {
356 DSoundCaptureSamples
,
357 DSoundAvailableSamples
360 static BOOL CALLBACK
DSoundEnumDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
369 snprintf(str
, sizeof(str
), "DirectSound Software on %s", desc
);
370 DeviceList
[i
].name
= AppendAllDeviceList(str
);
371 DeviceList
[i
].guid
= *guid
;
375 DeviceList
[0].name
= AppendDeviceList("DirectSound Software");
380 void alcDSoundInit(BackendFuncs
*FuncList
)
384 *FuncList
= DSoundFuncs
;
386 hr
= DirectSoundEnumerate(DSoundEnumDevices
, NULL
);
388 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);