Remove static variable from DSound callback
[openal-soft.git] / Alc / dsound.c
blobc65637c877cd713f909c2f247b957939d5293f67
1 /**
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
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <memory.h>
25 #include <windows.h>
26 #include <mmsystem.h>
27 #include <dsound.h>
29 #include "alMain.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
34 typedef struct {
35 // DirectSound Playback Device
36 LPDIRECTSOUND lpDS;
37 LPDIRECTSOUNDBUFFER DSpbuffer;
38 LPDIRECTSOUNDBUFFER DSsbuffer;
39 MMRESULT ulDSTimerID;
40 DWORD OldWriteCursor;
41 } DSoundData;
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;
54 DWORD BytesPlayed;
55 DWORD BufSize;
56 HRESULT DSRes;
58 (void)uID;
59 (void)uReserved;
60 (void)dwReserved1;
61 (void)dwReserved2;
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
85 if (DSRes==DS_OK)
87 // If we have an active context, mix data directly into output buffer otherwise fill with silence
88 SuspendContext(NULL);
89 if (WritePtr1)
90 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
91 if (WritePtr2)
92 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
93 ProcessContext(NULL);
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;
109 HRESULT hr;
111 if(deviceName)
113 int i;
114 for(i = 0;DeviceList[i];i++)
116 if(strcmp(deviceName, DeviceList[i]) == 0)
117 break;
119 if(!DeviceList[i])
120 return ALC_FALSE;
123 //Platform specific
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));
136 if(!pData)
138 SetALCError(ALC_OUT_OF_MEMORY);
139 return ALC_FALSE;
142 //Init COM
143 CoInitialize(NULL);
145 //DirectSound Init code
146 hr = CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectSound, (LPVOID*)&pData->lpDS);
147 if(SUCCEEDED(hr))
148 hr = IDirectSound_Initialize(pData->lpDS, NULL);
149 if(SUCCEEDED(hr))
150 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
152 if(SUCCEEDED(hr))
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);
159 if(SUCCEEDED(hr))
160 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType);
162 if(SUCCEEDED(hr))
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);
172 if(SUCCEEDED(hr))
173 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
175 if(FAILED(hr))
177 if (pData->DSsbuffer)
178 IDirectSoundBuffer_Release(pData->DSsbuffer);
179 if (pData->DSpbuffer)
180 IDirectSoundBuffer_Release(pData->DSpbuffer);
181 if (pData->lpDS)
182 IDirectSound_Release(pData->lpDS);
184 free(pData);
185 return ALC_FALSE;
188 pData->ulDSTimerID = timeSetEvent(25, 0, (LPTIMECALLBACK)DirectSoundProc, (DWORD)device, (UINT)TIME_CALLBACK_FUNCTION|TIME_PERIODIC);
189 device->MaxNoOfSources = 256;
191 if(deviceName)
192 strcpy(device->szDeviceName, deviceName);
193 else
194 strcpy(device->szDeviceName, DeviceList[0]);
196 device->ExtraData = pData;
197 return ALC_TRUE;
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
209 Sleep(100);
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);
219 //Deinit COM
220 CoUninitialize();
222 ProcessContext(NULL);
224 free(pData);
225 device->ExtraData = NULL;
229 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
231 (void)pDevice;
232 (void)deviceName;
233 (void)frequency;
234 (void)format;
235 (void)SampleSize;
236 return ALC_FALSE;
239 static void DSoundCloseCapture(ALCdevice *pDevice)
241 (void)pDevice;
244 static void DSoundStartCapture(ALCdevice *pDevice)
246 (void)pDevice;
249 static void DSoundStopCapture(ALCdevice *pDevice)
251 (void)pDevice;
254 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
256 (void)pDevice;
257 (void)pBuffer;
258 (void)lSamples;
261 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
263 (void)pDevice;
264 return 0;
268 BackendFuncs DSoundFuncs = {
269 DSoundOpenPlayback,
270 DSoundClosePlayback,
271 DSoundOpenCapture,
272 DSoundCloseCapture,
273 DSoundStartCapture,
274 DSoundStopCapture,
275 DSoundCaptureSamples,
276 DSoundAvailableSamples
279 void alcDSoundInit(BackendFuncs *FuncList)
281 *FuncList = DSoundFuncs;
283 DeviceList[0] = AppendDeviceList("DirectSound Software");
284 AppendAllDeviceList(DeviceList[0]);