Fix availibility amount calculation
[openal-soft.git] / Alc / dsound.c
blob4124896119cd19526add691ffc7a3fe7b2ebfb26
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 "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
27 #include <windows.h>
28 #include <mmsystem.h>
29 #include <dsound.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
35 #ifndef DSSPEAKER_7POINT1
36 #define DSSPEAKER_7POINT1 7
37 #endif
39 typedef struct {
40 // DirectSound Playback Device
41 LPDIRECTSOUND lpDS;
42 LPDIRECTSOUNDBUFFER DSpbuffer;
43 LPDIRECTSOUNDBUFFER DSsbuffer;
45 int killNow;
46 ALvoid *thread;
47 } DSoundData;
50 static ALCchar *DeviceList[16];
53 static ALuint DSoundProc(ALvoid *ptr)
55 ALCdevice *pDevice = (ALCdevice*)ptr;
56 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
57 DWORD LastCursor = 0;
58 DWORD PlayCursor;
59 VOID *WritePtr1, *WritePtr2;
60 DWORD WriteCnt1, WriteCnt2;
61 DWORD BufferSize;
62 DWORD avail;
63 HRESULT err;
65 BufferSize = pDevice->UpdateFreq*pDevice->FrameSize;
67 while(!pData->killNow)
69 // Get current play and write cursors
70 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
71 avail = (PlayCursor-LastCursor+BufferSize) % BufferSize;
73 if(avail == 0)
75 Sleep(1);
76 continue;
79 // Lock output buffer
80 WriteCnt1 = 0;
81 WriteCnt2 = 0;
82 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
84 // If the buffer is lost, restore it, play and lock
85 if(err == DSERR_BUFFERLOST)
87 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
88 if(SUCCEEDED(err))
89 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
90 if(SUCCEEDED(err))
91 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
94 // Successfully locked the output buffer
95 if(SUCCEEDED(err))
97 // If we have an active context, mix data directly into output buffer otherwise fill with silence
98 SuspendContext(NULL);
99 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
100 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
101 ProcessContext(NULL);
103 // Unlock output buffer only when successfully locked
104 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
106 else
107 AL_PRINT("Buffer lock error: %#lx\n", err);
109 // Update old write cursor location
110 LastCursor += WriteCnt1+WriteCnt2;
111 LastCursor %= BufferSize;
114 return 0;
117 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
119 DSBUFFERDESC DSBDescription;
120 DSoundData *pData = NULL;
121 WAVEFORMATEX OutputType;
122 DWORD speakers;
123 HRESULT hr;
125 if(deviceName)
127 int i;
128 for(i = 0;DeviceList[i];i++)
130 if(strcmp(deviceName, DeviceList[i]) == 0)
132 device->szDeviceName = DeviceList[i];
133 break;
136 if(!DeviceList[i])
137 return ALC_FALSE;
139 else
140 device->szDeviceName = DeviceList[0];
142 memset(&OutputType, 0, sizeof(WAVEFORMATEX));
144 //Initialise requested device
146 pData = calloc(1, sizeof(DSoundData));
147 if(!pData)
149 SetALCError(ALC_OUT_OF_MEMORY);
150 return ALC_FALSE;
153 //Init COM
154 CoInitialize(NULL);
156 //DirectSound Init code
157 hr = CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectSound, (LPVOID*)&pData->lpDS);
158 if(SUCCEEDED(hr))
159 hr = IDirectSound_Initialize(pData->lpDS, NULL);
160 if(SUCCEEDED(hr))
161 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
163 if(SUCCEEDED(hr))
164 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
165 if(SUCCEEDED(hr))
167 speakers = DSSPEAKER_CONFIG(speakers);
168 if(speakers == DSSPEAKER_MONO)
170 if(aluBytesFromFormat(device->Format) == 1)
171 device->Format = AL_FORMAT_MONO8;
172 else
173 device->Format = AL_FORMAT_MONO16;
175 else if(speakers == DSSPEAKER_STEREO)
177 if(aluBytesFromFormat(device->Format) == 1)
178 device->Format = AL_FORMAT_STEREO8;
179 else
180 device->Format = AL_FORMAT_STEREO16;
182 else if(speakers == DSSPEAKER_QUAD)
184 if(aluBytesFromFormat(device->Format) == 1)
185 device->Format = AL_FORMAT_QUAD8;
186 else
187 device->Format = AL_FORMAT_QUAD16;
189 else if(speakers == DSSPEAKER_5POINT1)
191 if(aluBytesFromFormat(device->Format) == 1)
192 device->Format = AL_FORMAT_51CHN8;
193 else
194 device->Format = AL_FORMAT_51CHN16;
196 else if(speakers == DSSPEAKER_7POINT1)
198 if(aluBytesFromFormat(device->Format) == 1)
199 device->Format = AL_FORMAT_71CHN8;
200 else
201 device->Format = AL_FORMAT_71CHN16;
203 device->FrameSize = aluBytesFromFormat(device->Format) *
204 aluChannelsFromFormat(device->Format);
206 OutputType.wFormatTag = WAVE_FORMAT_PCM;
207 OutputType.nChannels = aluChannelsFromFormat(device->Format);
208 OutputType.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
209 OutputType.nBlockAlign = OutputType.nChannels*OutputType.wBitsPerSample/8;
210 OutputType.nSamplesPerSec = device->Frequency;
211 OutputType.nAvgBytesPerSec = OutputType.nSamplesPerSec*OutputType.nBlockAlign;
212 OutputType.cbSize = 0;
215 if(SUCCEEDED(hr))
217 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
218 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
219 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
220 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
222 if(SUCCEEDED(hr))
223 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType);
225 if(SUCCEEDED(hr))
227 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
228 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
229 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
230 DSBDescription.dwBufferBytes=device->UpdateFreq * device->FrameSize;
231 DSBDescription.lpwfxFormat=&OutputType;
232 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
235 if(SUCCEEDED(hr))
236 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
238 device->ExtraData = pData;
239 pData->thread = StartThread(DSoundProc, device);
240 if(!pData->thread)
241 hr = E_FAIL;
243 if(FAILED(hr))
245 if (pData->DSsbuffer)
246 IDirectSoundBuffer_Release(pData->DSsbuffer);
247 if (pData->DSpbuffer)
248 IDirectSoundBuffer_Release(pData->DSpbuffer);
249 if (pData->lpDS)
250 IDirectSound_Release(pData->lpDS);
252 free(pData);
253 return ALC_FALSE;
256 return ALC_TRUE;
259 static void DSoundClosePlayback(ALCdevice *device)
261 DSoundData *pData = device->ExtraData;
263 pData->killNow = 1;
264 StopThread(pData->thread);
266 IDirectSoundBuffer_Release(pData->DSsbuffer);
267 IDirectSoundBuffer_Release(pData->DSpbuffer);
268 IDirectSound_Release(pData->lpDS);
270 //Deinit COM
271 CoUninitialize();
273 free(pData);
274 device->ExtraData = NULL;
278 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
280 (void)pDevice;
281 (void)deviceName;
282 (void)frequency;
283 (void)format;
284 (void)SampleSize;
285 return ALC_FALSE;
288 static void DSoundCloseCapture(ALCdevice *pDevice)
290 (void)pDevice;
293 static void DSoundStartCapture(ALCdevice *pDevice)
295 (void)pDevice;
298 static void DSoundStopCapture(ALCdevice *pDevice)
300 (void)pDevice;
303 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
305 (void)pDevice;
306 (void)pBuffer;
307 (void)lSamples;
310 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
312 (void)pDevice;
313 return 0;
317 BackendFuncs DSoundFuncs = {
318 DSoundOpenPlayback,
319 DSoundClosePlayback,
320 DSoundOpenCapture,
321 DSoundCloseCapture,
322 DSoundStartCapture,
323 DSoundStopCapture,
324 DSoundCaptureSamples,
325 DSoundAvailableSamples
328 void alcDSoundInit(BackendFuncs *FuncList)
330 *FuncList = DSoundFuncs;
332 DeviceList[0] = AppendDeviceList("DirectSound Software");
333 AppendAllDeviceList(DeviceList[0]);