Use M_PI since it is sure to be defined
[openal-soft.git] / Alc / dsound.c
blob899a0936dbb521ac2a6df5e16332e946c19c5b16
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 #define _WIN32_WINNT 0x0500
24 #define INITGUID
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <memory.h>
29 #include <dsound.h>
30 #include <mmreg.h>
32 #include "alMain.h"
33 #include "AL/al.h"
34 #include "AL/alc.h"
36 #ifndef DSSPEAKER_7POINT1
37 #define DSSPEAKER_7POINT1 7
38 #endif
40 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
42 // Since DSound doesn't report the fragment size, emulate it
43 static int num_frags;
45 typedef struct {
46 // DirectSound Playback Device
47 LPDIRECTSOUND lpDS;
48 LPDIRECTSOUNDBUFFER DSpbuffer;
49 LPDIRECTSOUNDBUFFER DSsbuffer;
51 volatile int killNow;
52 ALvoid *thread;
53 } DSoundData;
56 typedef struct {
57 ALCchar *name;
58 GUID guid;
59 } DevMap;
60 static DevMap DeviceList[16];
63 static ALuint DSoundProc(ALvoid *ptr)
65 ALCdevice *pDevice = (ALCdevice*)ptr;
66 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
67 DWORD LastCursor = 0;
68 DWORD PlayCursor;
69 VOID *WritePtr1, *WritePtr2;
70 DWORD WriteCnt1, WriteCnt2;
71 DWORD BufferSize;
72 DWORD avail;
73 HRESULT err;
75 BufferSize = pDevice->UpdateSize * num_frags *
76 aluBytesFromFormat(pDevice->Format) *
77 aluChannelsFromFormat(pDevice->Format);
79 while(!pData->killNow)
81 // Get current play and write cursors
82 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
83 avail = (PlayCursor-LastCursor+BufferSize) % BufferSize;
85 if(avail < BufferSize/num_frags)
87 Sleep(1);
88 continue;
91 // Lock output buffer
92 WriteCnt1 = 0;
93 WriteCnt2 = 0;
94 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
96 // If the buffer is lost, restore it, play and lock
97 if(err == DSERR_BUFFERLOST)
99 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
100 if(SUCCEEDED(err))
101 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
102 if(SUCCEEDED(err))
103 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
106 // Successfully locked the output buffer
107 if(SUCCEEDED(err))
109 // If we have an active context, mix data directly into output buffer otherwise fill with silence
110 SuspendContext(NULL);
111 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
112 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
113 ProcessContext(NULL);
115 // Unlock output buffer only when successfully locked
116 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
118 else
119 AL_PRINT("Buffer lock error: %#lx\n", err);
121 // Update old write cursor location
122 LastCursor += WriteCnt1+WriteCnt2;
123 LastCursor %= BufferSize;
126 return 0;
129 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
131 DSBUFFERDESC DSBDescription;
132 DSoundData *pData = NULL;
133 WAVEFORMATEXTENSIBLE OutputType;
134 DWORD frameSize = 0;
135 LPGUID guid = NULL;
136 DWORD speakers;
137 HRESULT hr;
139 if(deviceName)
141 int i;
142 for(i = 0;DeviceList[i].name;i++)
144 if(strcmp(deviceName, DeviceList[i].name) == 0)
146 device->szDeviceName = DeviceList[i].name;
147 if(i > 0)
148 guid = &DeviceList[i].guid;
149 break;
152 if(!DeviceList[i].name)
153 return ALC_FALSE;
155 else
156 device->szDeviceName = DeviceList[0].name;
158 memset(&OutputType, 0, sizeof(OutputType));
160 //Initialise requested device
162 pData = calloc(1, sizeof(DSoundData));
163 if(!pData)
165 SetALCError(ALC_OUT_OF_MEMORY);
166 return ALC_FALSE;
169 //DirectSound Init code
170 hr = DirectSoundCreate(guid, &pData->lpDS, NULL);
171 if(SUCCEEDED(hr))
172 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
174 if(SUCCEEDED(hr))
176 if(*(GetConfigValue(NULL, "format", "")) == 0)
177 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
178 else
180 if(device->Format == AL_FORMAT_MONO8 || device->Format == AL_FORMAT_MONO16)
181 speakers = DSSPEAKER_COMBINED(DSSPEAKER_MONO, 0);
182 else if(device->Format == AL_FORMAT_STEREO8 || device->Format == AL_FORMAT_STEREO16)
183 speakers = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, 0);
184 else if(device->Format == AL_FORMAT_QUAD8 || device->Format == AL_FORMAT_QUAD16)
185 speakers = DSSPEAKER_COMBINED(DSSPEAKER_QUAD, 0);
186 else if(device->Format == AL_FORMAT_51CHN8 || device->Format == AL_FORMAT_51CHN16)
187 speakers = DSSPEAKER_COMBINED(DSSPEAKER_5POINT1, 0);
188 else if(device->Format == AL_FORMAT_71CHN8 || device->Format == AL_FORMAT_71CHN16)
189 speakers = DSSPEAKER_COMBINED(DSSPEAKER_7POINT1, 0);
190 else
191 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
194 if(SUCCEEDED(hr))
196 speakers = DSSPEAKER_CONFIG(speakers);
197 if(speakers == DSSPEAKER_MONO)
199 if(aluBytesFromFormat(device->Format) == 1)
200 device->Format = AL_FORMAT_MONO8;
201 else
202 device->Format = AL_FORMAT_MONO16;
204 else if(speakers == DSSPEAKER_STEREO)
206 if(aluBytesFromFormat(device->Format) == 1)
207 device->Format = AL_FORMAT_STEREO8;
208 else
209 device->Format = AL_FORMAT_STEREO16;
211 else if(speakers == DSSPEAKER_QUAD)
213 if(aluBytesFromFormat(device->Format) == 1)
214 device->Format = AL_FORMAT_QUAD8;
215 else
216 device->Format = AL_FORMAT_QUAD16;
217 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
218 SPEAKER_FRONT_RIGHT |
219 SPEAKER_BACK_LEFT |
220 SPEAKER_BACK_RIGHT;
222 else if(speakers == DSSPEAKER_5POINT1)
224 if(aluBytesFromFormat(device->Format) == 1)
225 device->Format = AL_FORMAT_51CHN8;
226 else
227 device->Format = AL_FORMAT_51CHN16;
228 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
229 SPEAKER_FRONT_RIGHT |
230 SPEAKER_FRONT_CENTER |
231 SPEAKER_LOW_FREQUENCY |
232 SPEAKER_BACK_LEFT |
233 SPEAKER_BACK_RIGHT;
235 else if(speakers == DSSPEAKER_7POINT1)
237 if(aluBytesFromFormat(device->Format) == 1)
238 device->Format = AL_FORMAT_71CHN8;
239 else
240 device->Format = AL_FORMAT_71CHN16;
241 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
242 SPEAKER_FRONT_RIGHT |
243 SPEAKER_FRONT_CENTER |
244 SPEAKER_LOW_FREQUENCY |
245 SPEAKER_BACK_LEFT |
246 SPEAKER_BACK_RIGHT |
247 SPEAKER_SIDE_LEFT |
248 SPEAKER_SIDE_RIGHT;
250 frameSize = aluBytesFromFormat(device->Format) *
251 aluChannelsFromFormat(device->Format);
253 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
254 OutputType.Format.nChannels = aluChannelsFromFormat(device->Format);
255 OutputType.Format.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
256 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
257 OutputType.Format.nSamplesPerSec = device->Frequency;
258 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
259 OutputType.Format.cbSize = 0;
261 device->UpdateSize /= num_frags;
264 if(OutputType.Format.nChannels > 2)
266 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
267 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
268 OutputType.Format.cbSize = 22;
269 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
271 else
273 if(SUCCEEDED(hr))
275 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
276 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
277 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
278 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
280 if(SUCCEEDED(hr))
281 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
284 if(SUCCEEDED(hr))
286 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
287 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
288 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
289 DSBDescription.dwBufferBytes=device->UpdateSize * num_frags * frameSize;
290 DSBDescription.lpwfxFormat=&OutputType.Format;
291 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
294 if(SUCCEEDED(hr))
295 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
297 if(SUCCEEDED(hr))
299 device->ExtraData = pData;
300 pData->thread = StartThread(DSoundProc, device);
301 if(!pData->thread)
302 hr = E_FAIL;
305 if(FAILED(hr))
307 if (pData->DSsbuffer)
308 IDirectSoundBuffer_Release(pData->DSsbuffer);
309 if (pData->DSpbuffer)
310 IDirectSoundBuffer_Release(pData->DSpbuffer);
311 if (pData->lpDS)
312 IDirectSound_Release(pData->lpDS);
314 free(pData);
315 return ALC_FALSE;
318 return ALC_TRUE;
321 static void DSoundClosePlayback(ALCdevice *device)
323 DSoundData *pData = device->ExtraData;
325 pData->killNow = 1;
326 StopThread(pData->thread);
328 IDirectSoundBuffer_Release(pData->DSsbuffer);
329 if (pData->DSpbuffer)
330 IDirectSoundBuffer_Release(pData->DSpbuffer);
331 IDirectSound_Release(pData->lpDS);
333 free(pData);
334 device->ExtraData = NULL;
338 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
340 (void)pDevice;
341 (void)deviceName;
342 (void)frequency;
343 (void)format;
344 (void)SampleSize;
345 return ALC_FALSE;
348 static void DSoundCloseCapture(ALCdevice *pDevice)
350 (void)pDevice;
353 static void DSoundStartCapture(ALCdevice *pDevice)
355 (void)pDevice;
358 static void DSoundStopCapture(ALCdevice *pDevice)
360 (void)pDevice;
363 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
365 (void)pDevice;
366 (void)pBuffer;
367 (void)lSamples;
370 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
372 (void)pDevice;
373 return 0;
377 BackendFuncs DSoundFuncs = {
378 DSoundOpenPlayback,
379 DSoundClosePlayback,
380 DSoundOpenCapture,
381 DSoundCloseCapture,
382 DSoundStartCapture,
383 DSoundStopCapture,
384 DSoundCaptureSamples,
385 DSoundAvailableSamples
388 static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCSTR desc, LPCSTR drvname, LPVOID data)
390 size_t *iter = data;
391 (void)drvname;
393 if(guid)
395 char str[128];
396 snprintf(str, sizeof(str), "DirectSound Software on %s", desc);
397 DeviceList[*iter].name = AppendAllDeviceList(str);
398 DeviceList[*iter].guid = *guid;
399 (*iter)++;
401 else
402 DeviceList[0].name = AppendDeviceList("DirectSound Software");
404 return TRUE;
407 void alcDSoundInit(BackendFuncs *FuncList)
409 size_t iter = 1;
410 HRESULT hr;
412 *FuncList = DSoundFuncs;
414 num_frags = GetConfigValueInt("dsound", "periods", 4);
415 if(num_frags < 2) num_frags = 2;
417 hr = DirectSoundEnumerate(DSoundEnumDevices, &iter);
418 if(FAILED(hr))
419 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);