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
;
65 BufferSize
= pDevice
->UpdateFreq
*pDevice
->FrameSize
;
67 while(!pData
->killNow
)
69 // Get current play and write cursors
70 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
71 avail
= (PlayCursor
-LastCursor
+BufferSize
) % BufferSize
;
82 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
84 // If the buffer is lost, restore it, play and lock
85 if(err
== DSERR_BUFFERLOST
)
87 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
89 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
91 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
94 // Successfully locked the output buffer
97 // If we have an active context, mix data directly into output buffer otherwise fill with silence
99 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
100 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
101 ProcessContext(NULL
);
103 // Unlock output buffer only when successfully locked
104 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
107 AL_PRINT("Buffer lock error: %#lx\n", err
);
109 // Update old write cursor location
110 LastCursor
+= WriteCnt1
+WriteCnt2
;
111 LastCursor
%= BufferSize
;
117 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
119 DSBUFFERDESC DSBDescription
;
120 DSoundData
*pData
= NULL
;
121 WAVEFORMATEX OutputType
;
128 for(i
= 0;DeviceList
[i
];i
++)
130 if(strcmp(deviceName
, DeviceList
[i
]) == 0)
132 device
->szDeviceName
= DeviceList
[i
];
140 device
->szDeviceName
= DeviceList
[0];
142 memset(&OutputType
, 0, sizeof(WAVEFORMATEX
));
144 //Initialise requested device
146 pData
= calloc(1, sizeof(DSoundData
));
149 SetALCError(ALC_OUT_OF_MEMORY
);
156 //DirectSound Init code
157 hr
= CoCreateInstance(&CLSID_DirectSound
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectSound
, (LPVOID
*)&pData
->lpDS
);
159 hr
= IDirectSound_Initialize(pData
->lpDS
, NULL
);
161 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
164 hr
= IDirectSound_GetSpeakerConfig(pData
->lpDS
, &speakers
);
167 speakers
= DSSPEAKER_CONFIG(speakers
);
168 if(speakers
== DSSPEAKER_MONO
)
170 if(aluBytesFromFormat(device
->Format
) == 1)
171 device
->Format
= AL_FORMAT_MONO8
;
173 device
->Format
= AL_FORMAT_MONO16
;
175 else if(speakers
== DSSPEAKER_STEREO
)
177 if(aluBytesFromFormat(device
->Format
) == 1)
178 device
->Format
= AL_FORMAT_STEREO8
;
180 device
->Format
= AL_FORMAT_STEREO16
;
182 else if(speakers
== DSSPEAKER_QUAD
)
184 if(aluBytesFromFormat(device
->Format
) == 1)
185 device
->Format
= AL_FORMAT_QUAD8
;
187 device
->Format
= AL_FORMAT_QUAD16
;
189 else if(speakers
== DSSPEAKER_5POINT1
)
191 if(aluBytesFromFormat(device
->Format
) == 1)
192 device
->Format
= AL_FORMAT_51CHN8
;
194 device
->Format
= AL_FORMAT_51CHN16
;
196 else if(speakers
== DSSPEAKER_7POINT1
)
198 if(aluBytesFromFormat(device
->Format
) == 1)
199 device
->Format
= AL_FORMAT_71CHN8
;
201 device
->Format
= AL_FORMAT_71CHN16
;
203 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
204 aluChannelsFromFormat(device
->Format
);
206 OutputType
.wFormatTag
= WAVE_FORMAT_PCM
;
207 OutputType
.nChannels
= aluChannelsFromFormat(device
->Format
);
208 OutputType
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
209 OutputType
.nBlockAlign
= OutputType
.nChannels
*OutputType
.wBitsPerSample
/8;
210 OutputType
.nSamplesPerSec
= device
->Frequency
;
211 OutputType
.nAvgBytesPerSec
= OutputType
.nSamplesPerSec
*OutputType
.nBlockAlign
;
212 OutputType
.cbSize
= 0;
217 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
218 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
219 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
220 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
223 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
);
227 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
228 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
229 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
230 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* device
->FrameSize
;
231 DSBDescription
.lpwfxFormat
=&OutputType
;
232 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
236 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
238 device
->ExtraData
= pData
;
239 pData
->thread
= StartThread(DSoundProc
, device
);
245 if (pData
->DSsbuffer
)
246 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
247 if (pData
->DSpbuffer
)
248 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
250 IDirectSound_Release(pData
->lpDS
);
259 static void DSoundClosePlayback(ALCdevice
*device
)
261 DSoundData
*pData
= device
->ExtraData
;
264 StopThread(pData
->thread
);
266 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
267 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
268 IDirectSound_Release(pData
->lpDS
);
274 device
->ExtraData
= NULL
;
278 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
288 static void DSoundCloseCapture(ALCdevice
*pDevice
)
293 static void DSoundStartCapture(ALCdevice
*pDevice
)
298 static void DSoundStopCapture(ALCdevice
*pDevice
)
303 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
310 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
317 BackendFuncs DSoundFuncs
= {
324 DSoundCaptureSamples
,
325 DSoundAvailableSamples
328 void alcDSoundInit(BackendFuncs
*FuncList
)
330 *FuncList
= DSoundFuncs
;
332 DeviceList
[0] = AppendDeviceList("DirectSound Software");
333 AppendAllDeviceList(DeviceList
[0]);