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 // DirectSound Playback Device
39 LPDIRECTSOUNDBUFFER DSpbuffer
;
40 LPDIRECTSOUNDBUFFER DSsbuffer
;
47 static ALCchar
*DeviceList
[16];
50 static ALuint
DSoundProc(ALvoid
*ptr
)
52 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
53 DSoundData
*pData
= (DSoundData
*)pDevice
->ExtraData
;
61 while(!pData
->killNow
)
63 // Get current play and write cursors
64 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
, &PlayCursor
, NULL
);
65 avail
= (PlayCursor
-LastCursor
) % (pDevice
->UpdateFreq
*pDevice
->FrameSize
);
75 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, &WritePtr
, &WriteCnt
, NULL
, NULL
, 0);
77 // If the buffer is lost, restore it, play and lock
78 if(err
== DSERR_BUFFERLOST
)
80 err
= IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
82 err
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
84 err
= IDirectSoundBuffer_Lock(pData
->DSsbuffer
, LastCursor
, avail
, (LPVOID
*)&WritePtr
, &WriteCnt
, NULL
, NULL
, 0);
87 // Successfully locked the output buffer
90 // If we have an active context, mix data directly into output buffer otherwise fill with silence
92 aluMixData(pDevice
->Context
, WritePtr
, WriteCnt
, pDevice
->Format
);
95 // Unlock output buffer only when successfully locked
96 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
, WritePtr
, WriteCnt
, NULL
, 0);
99 AL_PRINT("Buffer lock error: %#lx\n", err
);
101 // Update old write cursor location
102 LastCursor
+= WriteCnt
;
103 LastCursor
%= pDevice
->UpdateFreq
*pDevice
->FrameSize
;
109 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
111 DSBUFFERDESC DSBDescription
;
112 DSoundData
*pData
= NULL
;
113 WAVEFORMATEX OutputType
;
119 for(i
= 0;DeviceList
[i
];i
++)
121 if(strcmp(deviceName
, DeviceList
[i
]) == 0)
123 device
->szDeviceName
= DeviceList
[i
];
131 device
->szDeviceName
= DeviceList
[0];
134 memset(&OutputType
, 0, sizeof(WAVEFORMATEX
));
135 OutputType
.wFormatTag
= WAVE_FORMAT_PCM
;
136 OutputType
.nChannels
= device
->Channels
;
137 OutputType
.wBitsPerSample
= aluBytesFromFormat(device
->Format
) * 8;
138 OutputType
.nBlockAlign
= OutputType
.nChannels
*OutputType
.wBitsPerSample
/8;
139 OutputType
.nSamplesPerSec
= device
->Frequency
;
140 OutputType
.nAvgBytesPerSec
= OutputType
.nSamplesPerSec
*OutputType
.nBlockAlign
;
141 OutputType
.cbSize
= 0;
143 //Initialise requested device
145 pData
= calloc(1, sizeof(DSoundData
));
148 SetALCError(ALC_OUT_OF_MEMORY
);
155 //DirectSound Init code
156 hr
= CoCreateInstance(&CLSID_DirectSound
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectSound
, (LPVOID
*)&pData
->lpDS
);
158 hr
= IDirectSound_Initialize(pData
->lpDS
, NULL
);
160 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
164 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
165 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
166 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
167 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
170 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
);
174 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
175 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
176 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
177 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* device
->FrameSize
;
178 DSBDescription
.lpwfxFormat
=&OutputType
;
179 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
183 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
185 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]);