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)
124 memset(&OutputType
, 0, sizeof(WAVEFORMATEX
));
125 OutputType
.wFormatTag
= WAVE_FORMAT_PCM
;
126 OutputType
.nChannels
= device
->Channels
;
127 OutputType
.wBitsPerSample
= (((device
->Format
==AL_FORMAT_MONO16
)||(device
->Format
==AL_FORMAT_STEREO16
)||(device
->Format
==AL_FORMAT_QUAD16
))?16:8);
128 OutputType
.nBlockAlign
= OutputType
.nChannels
*OutputType
.wBitsPerSample
/8;
129 OutputType
.nSamplesPerSec
= device
->Frequency
;
130 OutputType
.nAvgBytesPerSec
= OutputType
.nSamplesPerSec
*OutputType
.nBlockAlign
;
131 OutputType
.cbSize
= 0;
133 //Initialise requested device
135 pData
= calloc(1, sizeof(DSoundData
));
138 SetALCError(ALC_OUT_OF_MEMORY
);
145 //DirectSound Init code
146 hr
= CoCreateInstance(&CLSID_DirectSound
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectSound
, (LPVOID
*)&pData
->lpDS
);
148 hr
= IDirectSound_Initialize(pData
->lpDS
, NULL
);
150 hr
= IDirectSound_SetCooperativeLevel(pData
->lpDS
, GetForegroundWindow(), DSSCL_PRIORITY
);
154 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
155 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
156 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
157 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSpbuffer
, NULL
);
160 hr
= IDirectSoundBuffer_SetFormat(pData
->DSpbuffer
,&OutputType
);
164 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
165 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
166 DSBDescription
.dwFlags
=DSBCAPS_GLOBALFOCUS
|DSBCAPS_GETCURRENTPOSITION2
;
167 DSBDescription
.dwBufferBytes
=device
->UpdateFreq
* device
->FrameSize
;
168 DSBDescription
.lpwfxFormat
=&OutputType
;
169 hr
= IDirectSound_CreateSoundBuffer(pData
->lpDS
, &DSBDescription
, &pData
->DSsbuffer
, NULL
);
173 hr
= IDirectSoundBuffer_Play(pData
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
177 if (pData
->DSsbuffer
)
178 IDirectSoundBuffer_Release(pData
->DSsbuffer
);
179 if (pData
->DSpbuffer
)
180 IDirectSoundBuffer_Release(pData
->DSpbuffer
);
182 IDirectSound_Release(pData
->lpDS
);
188 pData
->ulDSTimerID
= timeSetEvent(25, 0, (LPTIMECALLBACK
)DirectSoundProc
, (DWORD
)device
, (UINT
)TIME_CALLBACK_FUNCTION
|TIME_PERIODIC
);
189 device
->MaxNoOfSources
= 256;
192 strcpy(device
->szDeviceName
, deviceName
);
194 strcpy(device
->szDeviceName
, DeviceList
[0]);
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]);