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
35 #ifndef DSSPEAKER_7POINT1
36 #define DSSPEAKER_7POINT1 7
40 // DirectSound Playback Device
42 LPDIRECTSOUNDBUFFER DSpbuffer
;
43 LPDIRECTSOUNDBUFFER DSsbuffer
;
50 static ALCchar
*DeviceList
[16];
53 static ALuint
DSoundProc(ALvoid
*ptr
)
55 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
56 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
59 VOID
*WritePtr1
, *WritePtr2
;
60 DWORD WriteCnt1
, WriteCnt2
;
64 while(!pData
->killNow
)
66 // Get current play and write cursors
67 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
68 avail
= (PlayCursor
-LastCursor
) % (pDevice
->UpdateFreq
*pDevice
->FrameSize
);
79 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
81 // If the buffer is lost, restore it, play and lock
82 if(err
== DSERR_BUFFERLOST
)
84 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
86 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
88 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
91 // Successfully locked the output buffer
94 // If we have an active context, mix data directly into output buffer otherwise fill with silence
96 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
97 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
100 // Unlock output buffer only when successfully locked
101 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
104 AL_PRINT("Buffer lock error: %#lx\n", err
);
106 // Update old write cursor location
107 LastCursor
+= WriteCnt1
+WriteCnt2
;
108 LastCursor
%= pDevice
->UpdateFreq
*pDevice
->FrameSize
;
114 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
116 DSBUFFERDESC DSBDescription
;
117 DSoundData
*pData
= NULL
;
118 WAVEFORMATEX OutputType
;
125 for(i
= 0;DeviceList
[i
];i
++)
127 if(strcmp(deviceName
, DeviceList
[i
]) == 0)
129 device
->szDeviceName
= DeviceList
[i
];
137 device
->szDeviceName
= DeviceList
[0];
139 memset(&OutputType
, 0, sizeof(WAVEFORMATEX
));
141 //Initialise requested device
143 pData
= calloc(1, sizeof(DSoundData
));
146 SetALCError(ALC_OUT_OF_MEMORY
);
153 //DirectSound Init code
154 hr
= CoCreateInstance(&CLSID_DirectSound
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectSound
, (LPVOID
*)&pData
->lpDS
);
156 hr
= IDirectSound_Initialize(pData
->lpDS
, NULL
);
158 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
161 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
164 speakers
= DSSPEAKER_CONFIG(speakers
);
165 if(speakers
== DSSPEAKER_MONO
)
167 if(aluBytesFromFormat(device
->Format
) == 8)
168 device
->Format
= AL_FORMAT_MONO8
;
170 device
->Format
= AL_FORMAT_MONO16
;
172 else if(speakers
== DSSPEAKER_STEREO
)
174 if(aluBytesFromFormat(device
->Format
) == 8)
175 device
->Format
= AL_FORMAT_STEREO8
;
177 device
->Format
= AL_FORMAT_STEREO16
;
179 else if(speakers
== DSSPEAKER_QUAD
)
181 if(aluBytesFromFormat(device
->Format
) == 8)
182 device
->Format
= AL_FORMAT_QUAD8
;
184 device
->Format
= AL_FORMAT_QUAD16
;
186 else if(speakers
== DSSPEAKER_5POINT1
)
188 if(aluBytesFromFormat(device
->Format
) == 8)
189 device
->Format
= AL_FORMAT_51CHN8
;
191 device
->Format
= AL_FORMAT_51CHN16
;
193 else if(speakers
== DSSPEAKER_7POINT1
)
195 if(aluBytesFromFormat(device
->Format
) == 8)
196 device
->Format
= AL_FORMAT_71CHN8
;
198 device
->Format
= AL_FORMAT_71CHN16
;
201 OutputType
.wFormatTag
= WAVE_FORMAT_PCM
;
202 OutputType
.nChannels
= aluChannelsFromFormat(device
->Format
);
203 OutputType
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
204 OutputType
.nBlockAlign
= OutputType
.nChannels
*OutputType
.wBitsPerSample
/8;
205 OutputType
.nSamplesPerSec
= device
->Frequency
;
206 OutputType
.nAvgBytesPerSec
= OutputType
.nSamplesPerSec
*OutputType
.nBlockAlign
;
207 OutputType
.cbSize
= 0;
212 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
213 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
214 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
215 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
218 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
);
222 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
223 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
224 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
225 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* device
->FrameSize
;
226 DSBDescription
.lpwfxFormat
=&OutputType
;
227 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
231 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
233 device
->ExtraData
= pData
;
234 pData
->thread
= StartThread(DSoundProc
, device
);
240 if (pData
->DSsbuffer
)
241 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
242 if (pData
->DSpbuffer
)
243 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
245 IDirectSound_Release(pData
->lpDS
);
254 static void DSoundClosePlayback(ALCdevice
*device
)
256 DSoundData
*pData
= device
->ExtraData
;
259 StopThread(pData
->thread
);
261 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
262 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
263 IDirectSound_Release(pData
->lpDS
);
269 device
->ExtraData
= NULL
;
273 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
283 static void DSoundCloseCapture(ALCdevice
*pDevice
)
288 static void DSoundStartCapture(ALCdevice
*pDevice
)
293 static void DSoundStopCapture(ALCdevice
*pDevice
)
298 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
305 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
312 BackendFuncs DSoundFuncs
= {
319 DSoundCaptureSamples
,
320 DSoundAvailableSamples
323 void alcDSoundInit(BackendFuncs
*FuncList
)
325 *FuncList
= DSoundFuncs
;
327 DeviceList
[0] = AppendDeviceList("DirectSound Software");
328 AppendAllDeviceList(DeviceList
[0]);