Remove duplication of setting the max source count
[openal-soft.git] / Alc / dsound.c
blobd513fb730b59d8ca841d3f9872de0727a48e591b
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"
36 typedef struct {
37 // DirectSound Playback Device
38 LPDIRECTSOUND lpDS;
39 LPDIRECTSOUNDBUFFER DSpbuffer;
40 LPDIRECTSOUNDBUFFER DSsbuffer;
42 int killNow;
43 ALvoid *thread;
44 } DSoundData;
47 static ALCchar *DeviceList[16];
50 static ALuint DSoundProc(ALvoid *ptr)
52 ALCdevice *pDevice = (ALCdevice*)ptr;
53 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
54 DWORD LastCursor = 0;
55 DWORD PlayCursor;
56 VOID *WritePtr;
57 DWORD WriteCnt;
58 DWORD avail;
59 HRESULT err;
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);
67 if(avail == 0)
69 Sleep(1);
70 continue;
73 // Lock output buffer
74 WriteCnt = 0;
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);
81 if(SUCCEEDED(err))
82 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
83 if(SUCCEEDED(err))
84 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, (LPVOID*)&WritePtr, &WriteCnt, NULL, NULL, 0);
87 // Successfully locked the output buffer
88 if(SUCCEEDED(err))
90 // If we have an active context, mix data directly into output buffer otherwise fill with silence
91 SuspendContext(NULL);
92 aluMixData(pDevice->Context, WritePtr, WriteCnt, pDevice->Format);
93 ProcessContext(NULL);
95 // Unlock output buffer only when successfully locked
96 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr, WriteCnt, NULL, 0);
98 else
99 AL_PRINT("Buffer lock error: %#lx\n", err);
101 // Update old write cursor location
102 LastCursor += WriteCnt;
103 LastCursor %= pDevice->UpdateFreq*pDevice->FrameSize;
106 return 0;
109 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
111 DSBUFFERDESC DSBDescription;
112 DSoundData *pData = NULL;
113 WAVEFORMATEX OutputType;
114 HRESULT hr;
116 if(deviceName)
118 int i;
119 for(i = 0;DeviceList[i];i++)
121 if(strcmp(deviceName, DeviceList[i]) == 0)
123 device->szDeviceName = DeviceList[i];
124 break;
127 if(!DeviceList[i])
128 return ALC_FALSE;
130 else
131 device->szDeviceName = DeviceList[0];
133 //Platform specific
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));
146 if(!pData)
148 SetALCError(ALC_OUT_OF_MEMORY);
149 return ALC_FALSE;
152 //Init COM
153 CoInitialize(NULL);
155 //DirectSound Init code
156 hr = CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectSound, (LPVOID*)&pData->lpDS);
157 if(SUCCEEDED(hr))
158 hr = IDirectSound_Initialize(pData->lpDS, NULL);
159 if(SUCCEEDED(hr))
160 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
162 if(SUCCEEDED(hr))
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);
169 if(SUCCEEDED(hr))
170 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType);
172 if(SUCCEEDED(hr))
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);
182 if(SUCCEEDED(hr))
183 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
185 device->ExtraData = pData;
186 pData->thread = StartThread(DSoundProc, device);
187 if(!pData->thread)
188 hr = E_FAIL;
190 if(FAILED(hr))
192 if (pData->DSsbuffer)
193 IDirectSoundBuffer_Release(pData->DSsbuffer);
194 if (pData->DSpbuffer)
195 IDirectSoundBuffer_Release(pData->DSpbuffer);
196 if (pData->lpDS)
197 IDirectSound_Release(pData->lpDS);
199 free(pData);
200 return ALC_FALSE;
203 return ALC_TRUE;
206 static void DSoundClosePlayback(ALCdevice *device)
208 DSoundData *pData = device->ExtraData;
210 pData->killNow = 1;
211 StopThread(pData->thread);
213 IDirectSoundBuffer_Release(pData->DSsbuffer);
214 IDirectSoundBuffer_Release(pData->DSpbuffer);
215 IDirectSound_Release(pData->lpDS);
217 //Deinit COM
218 CoUninitialize();
220 free(pData);
221 device->ExtraData = NULL;
225 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
227 (void)pDevice;
228 (void)deviceName;
229 (void)frequency;
230 (void)format;
231 (void)SampleSize;
232 return ALC_FALSE;
235 static void DSoundCloseCapture(ALCdevice *pDevice)
237 (void)pDevice;
240 static void DSoundStartCapture(ALCdevice *pDevice)
242 (void)pDevice;
245 static void DSoundStopCapture(ALCdevice *pDevice)
247 (void)pDevice;
250 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
252 (void)pDevice;
253 (void)pBuffer;
254 (void)lSamples;
257 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
259 (void)pDevice;
260 return 0;
264 BackendFuncs DSoundFuncs = {
265 DSoundOpenPlayback,
266 DSoundClosePlayback,
267 DSoundOpenCapture,
268 DSoundCloseCapture,
269 DSoundStartCapture,
270 DSoundStopCapture,
271 DSoundCaptureSamples,
272 DSoundAvailableSamples
275 void alcDSoundInit(BackendFuncs *FuncList)
277 *FuncList = DSoundFuncs;
279 DeviceList[0] = AppendDeviceList("DirectSound Software");
280 AppendAllDeviceList(DeviceList[0]);