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 // DirectSound Playback Device
37 LPDIRECTSOUNDBUFFER DSpbuffer
;
38 LPDIRECTSOUNDBUFFER DSsbuffer
;
45 static ALCchar
*DeviceList
[16];
48 static ALuint
DSoundProc(ALvoid
*ptr
)
50 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
51 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
59 while(!pData
->killNow
)
61 // Get current play and write cursors
62 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
63 avail
= (PlayCursor
-LastCursor
) % (pDevice
->UpdateFreq
*pDevice
->FrameSize
);
73 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr
, &WriteCnt
, NULL
, NULL
, 0);
75 // If the buffer is lost, restore it, play and lock
76 if(err
== DSERR_BUFFERLOST
)
78 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
80 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
82 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, (LPVOID
*)&WritePtr
, &WriteCnt
, NULL
, NULL
, 0);
85 // Successfully locked the output buffer
88 // If we have an active context, mix data directly into output buffer otherwise fill with silence
90 aluMixData(pDevice
->Context
, WritePtr
, WriteCnt
, pDevice
->Format
);
93 // Unlock output buffer only when successfully locked
94 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr
, WriteCnt
, NULL
, 0);
97 AL_PRINT("Buffer lock error: %#lx\n", err
);
99 // Update old write cursor location
100 LastCursor
+= WriteCnt
;
101 LastCursor
%= pDevice
->UpdateFreq
*pDevice
->FrameSize
;
107 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
109 DSBUFFERDESC DSBDescription
;
110 DSoundData
*pData
= NULL
;
111 WAVEFORMATEX OutputType
;
117 for(i
= 0;DeviceList
[i
];i
++)
119 if(strcmp(deviceName
, DeviceList
[i
]) == 0)
121 device
->szDeviceName
= DeviceList
[i
];
129 device
->szDeviceName
= DeviceList
[0];
132 memset(&OutputType
, 0, sizeof(WAVEFORMATEX
));
133 OutputType
.wFormatTag
= WAVE_FORMAT_PCM
;
134 OutputType
.nChannels
= device
->Channels
;
135 OutputType
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
136 OutputType
.nBlockAlign
= OutputType
.nChannels
*OutputType
.wBitsPerSample
/8;
137 OutputType
.nSamplesPerSec
= device
->Frequency
;
138 OutputType
.nAvgBytesPerSec
= OutputType
.nSamplesPerSec
*OutputType
.nBlockAlign
;
139 OutputType
.cbSize
= 0;
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
);
162 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
163 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
164 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
165 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
168 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
);
172 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
173 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
174 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
175 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* device
->FrameSize
;
176 DSBDescription
.lpwfxFormat
=&OutputType
;
177 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
181 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
183 device
->MaxNoOfSources
= 256;
184 device
->ExtraData
= pData
;
186 pData
->thread
= StartThread(DSoundProc
, device
);
192 if (pData
->DSsbuffer
)
193 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
194 if (pData
->DSpbuffer
)
195 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
197 IDirectSound_Release(pData
->lpDS
);
206 static void DSoundClosePlayback(ALCdevice
*device
)
208 DSoundData
*pData
= device
->ExtraData
;
211 StopThread(pData
->thread
);
213 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
214 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
215 IDirectSound_Release(pData
->lpDS
);
221 device
->ExtraData
= NULL
;
225 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
235 static void DSoundCloseCapture(ALCdevice
*pDevice
)
240 static void DSoundStartCapture(ALCdevice
*pDevice
)
245 static void DSoundStopCapture(ALCdevice
*pDevice
)
250 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
257 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
264 BackendFuncs DSoundFuncs
= {
271 DSoundCaptureSamples
,
272 DSoundAvailableSamples
275 void alcDSoundInit(BackendFuncs
*FuncList
)
277 *FuncList
= DSoundFuncs
;
279 DeviceList
[0] = AppendDeviceList("DirectSound Software");
280 AppendAllDeviceList(DeviceList
[0]);