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
;
44 static ALCchar
*DeviceList
[16];
46 static void CALLBACK
DirectSoundProc(UINT uID
,UINT uReserved
,DWORD_PTR dwUser
,DWORD_PTR dwReserved1
,DWORD_PTR dwReserved2
)
48 ALCdevice
*pDevice
= (ALCdevice
*)dwUser
;
49 DSoundData
*pData
= pDevice
->ExtraData
;
50 DWORD PlayCursor
,WriteCursor
;
51 BYTE
*WritePtr1
,*WritePtr2
;
52 DWORD WriteCnt1
,WriteCnt2
;
53 WAVEFORMATEX OutputType
;
63 BufSize
= pDevice
->UpdateFreq
* pDevice
->FrameSize
;
65 // Get current play and write cursors
66 IDirectSoundBuffer_GetCurrentPosition(pData
->DSsbuffer
,&PlayCursor
,&WriteCursor
);
67 if (!pData
->OldWriteCursor
) pData
->OldWriteCursor
=WriteCursor
-PlayCursor
;
69 // Get the output format and figure the number of bytes played (block aligned)
70 IDirectSoundBuffer_GetFormat(pData
->DSsbuffer
,&OutputType
,sizeof(WAVEFORMATEX
),NULL
);
71 BytesPlayed
=((((WriteCursor
<pData
->OldWriteCursor
)?(BufSize
+WriteCursor
-pData
->OldWriteCursor
):(WriteCursor
-pData
->OldWriteCursor
))/OutputType
.nBlockAlign
)*OutputType
.nBlockAlign
);
73 // Lock output buffer started at 40msec in front of the old write cursor (15msec in front of the actual write cursor)
74 DSRes
=IDirectSoundBuffer_Lock(pData
->DSsbuffer
,(pData
->OldWriteCursor
+(OutputType
.nSamplesPerSec
/25)*OutputType
.nBlockAlign
)%BufSize
,BytesPlayed
,(LPVOID
*)&WritePtr1
,&WriteCnt1
,(LPVOID
*)&WritePtr2
,&WriteCnt2
,0);
76 // If the buffer is lost, restore it, play and lock
77 if (DSRes
==DSERR_BUFFERLOST
)
79 IDirectSoundBuffer_Restore(pData
->DSsbuffer
);
80 IDirectSoundBuffer_Play(pData
->DSsbuffer
,0,0,DSBPLAY_LOOPING
);
81 DSRes
=IDirectSoundBuffer_Lock(pData
->DSsbuffer
,(pData
->OldWriteCursor
+(OutputType
.nSamplesPerSec
/25)*OutputType
.nBlockAlign
)%BufSize
,BytesPlayed
,(LPVOID
*)&WritePtr1
,&WriteCnt1
,(LPVOID
*)&WritePtr2
,&WriteCnt2
,0);
84 // Successfully locked the output buffer
87 // If we have an active context, mix data directly into output buffer otherwise fill with silence
90 aluMixData(pDevice
->Context
, WritePtr1
, WriteCnt1
, pDevice
->Format
);
92 aluMixData(pDevice
->Context
, WritePtr2
, WriteCnt2
, pDevice
->Format
);
95 // Unlock output buffer only when successfully locked
96 IDirectSoundBuffer_Unlock(pData
->DSsbuffer
,WritePtr1
,WriteCnt1
,WritePtr2
,WriteCnt2
);
99 // Update old write cursor location
100 pData
->OldWriteCursor
=((pData
->OldWriteCursor
+BytesPlayed
)%BufSize
);
104 static ALCboolean
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
106 DSBUFFERDESC DSBDescription
;
107 DSoundData
*pData
= NULL
;
108 WAVEFORMATEX OutputType
;
114 for(i
= 0;DeviceList
[i
];i
++)
116 if(strcmp(deviceName
, DeviceList
[i
]) == 0)
118 device
->szDeviceName
= DeviceList
[i
];
126 device
->szDeviceName
= DeviceList
[0];
129 memset(&OutputType
, 0, sizeof(WAVEFORMATEX
));
130 OutputType
.wFormatTag
= WAVE_FORMAT_PCM
;
131 OutputType
.nChannels
= device
->Channels
;
132 OutputType
.wBitsPerSample
= (((device
->Format
==AL_FORMAT_MONO16
)||(device
->Format
==AL_FORMAT_STEREO16
)||(device
->Format
==AL_FORMAT_QUAD16
))?16:8);
133 OutputType
.nBlockAlign
= OutputType
.nChannels
*OutputType
.wBitsPerSample
/8;
134 OutputType
.nSamplesPerSec
= device
->Frequency
;
135 OutputType
.nAvgBytesPerSec
= OutputType
.nSamplesPerSec
*OutputType
.nBlockAlign
;
136 OutputType
.cbSize
= 0;
138 //Initialise requested device
140 pData
= calloc(1, sizeof(DSoundData
));
143 SetALCError(ALC_OUT_OF_MEMORY
);
150 //DirectSound Init code
151 hr
= CoCreateInstance(&CLSID_DirectSound
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectSound
, (LPVOID
*)&pData
->lpDS
);
153 hr
= IDirectSound_Initialize(pData
->lpDS
, NULL
);
155 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
159 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
160 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
161 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
162 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
165 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
);
169 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
170 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
171 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
172 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* device
->FrameSize
;
173 DSBDescription
.lpwfxFormat
=&OutputType
;
174 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
178 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
182 if (pData
->DSsbuffer
)
183 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
184 if (pData
->DSpbuffer
)
185 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
187 IDirectSound_Release(pData
->lpDS
);
193 pData
->ulDSTimerID
= timeSetEvent(25, 0, (LPTIMECALLBACK
)DirectSoundProc
, (DWORD
)device
, (UINT
)TIME_CALLBACK_FUNCTION
|TIME_PERIODIC
);
194 device
->MaxNoOfSources
= 256;
196 device
->ExtraData
= pData
;
200 static void DSoundClosePlayback(ALCdevice
*device
)
202 DSoundData
*pData
= device
->ExtraData
;
204 // Stop and release the DS timer
205 if (pData
->ulDSTimerID
)
206 timeKillEvent(pData
->ulDSTimerID
);
208 // Wait ... just in case any timer events happen
211 SuspendContext(NULL
);
213 if (pData
->DSsbuffer
)
214 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
215 if (pData
->DSpbuffer
)
216 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
217 IDirectSound_Release(pData
->lpDS
);
222 ProcessContext(NULL
);
225 device
->ExtraData
= NULL
;
229 static ALCboolean
DSoundOpenCapture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
239 static void DSoundCloseCapture(ALCdevice
*pDevice
)
244 static void DSoundStartCapture(ALCdevice
*pDevice
)
249 static void DSoundStopCapture(ALCdevice
*pDevice
)
254 static void DSoundCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
261 static ALCuint
DSoundAvailableSamples(ALCdevice
*pDevice
)
268 BackendFuncs DSoundFuncs
= {
275 DSoundCaptureSamples
,
276 DSoundAvailableSamples
279 void alcDSoundInit(BackendFuncs
*FuncList
)
281 *FuncList
= DSoundFuncs
;
283 DeviceList
[0] = AppendDeviceList("DirectSound Software");
284 AppendAllDeviceList(DeviceList
[0]);