Don't limit IMA4 decoding to 2 channels
[openal-soft.git] / Alc / dsound.c
blobce7dd80d9bad7fd33d2f025b51f925bf5d97eaa4
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>
31 #ifndef _WAVEFORMATEXTENSIBLE_
32 #include <ks.h>
33 #include <ksmedia.h>
34 #endif
36 #include "alMain.h"
37 #include "AL/al.h"
38 #include "AL/alc.h"
40 #ifndef DSSPEAKER_5POINT1
41 #define DSSPEAKER_5POINT1 6
42 #endif
43 #ifndef DSSPEAKER_7POINT1
44 #define DSSPEAKER_7POINT1 7
45 #endif
47 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
48 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
50 static void *ds_handle;
51 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter);
52 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA pDSEnumCallback, LPVOID pContext);
55 typedef struct {
56 // DirectSound Playback Device
57 LPDIRECTSOUND lpDS;
58 LPDIRECTSOUNDBUFFER DSpbuffer;
59 LPDIRECTSOUNDBUFFER DSsbuffer;
61 volatile int killNow;
62 ALvoid *thread;
63 } DSoundData;
66 typedef struct {
67 ALCchar *name;
68 GUID guid;
69 } DevMap;
71 static const ALCchar dsDevice[] = "DirectSound Default";
72 static DevMap *DeviceList;
73 static ALuint NumDevices;
76 void *DSoundLoad(void)
78 if(!ds_handle)
80 #ifdef _WIN32
81 ds_handle = LoadLibraryA("dsound.dll");
82 if(ds_handle == NULL)
84 AL_PRINT("Failed to load dsound.dll\n");
85 return NULL;
88 #define LOAD_FUNC(f) do { \
89 p##f = (void*)GetProcAddress((HMODULE)ds_handle, #f); \
90 if(p##f == NULL) \
91 { \
92 FreeLibrary(ds_handle); \
93 ds_handle = NULL; \
94 AL_PRINT("Could not load %s from dsound.dll\n", #f); \
95 return NULL; \
96 } \
97 } while(0)
98 #else
99 ds_handle = (void*)0xDEADBEEF;
100 #define LOAD_FUNC(f) p##f = f
101 #endif
103 LOAD_FUNC(DirectSoundCreate);
104 LOAD_FUNC(DirectSoundEnumerateA);
105 #undef LOAD_FUNC
107 return ds_handle;
111 static BOOL CALLBACK DSoundEnumDevices(LPGUID guid, LPCSTR desc, LPCSTR drvname, LPVOID data)
113 char str[1024];
114 void *temp;
115 int count;
116 ALuint i;
118 (void)data;
119 (void)drvname;
121 if(NumDevices == 0)
123 temp = realloc(DeviceList, sizeof(DevMap) * (NumDevices+1));
124 if(temp)
126 DeviceList = temp;
127 DeviceList[NumDevices].name = strdup(dsDevice);
128 DeviceList[NumDevices].guid = GUID_NULL;
129 NumDevices++;
133 if(!guid)
134 return TRUE;
136 count = 0;
137 do {
138 if(count == 0)
139 snprintf(str, sizeof(str), "%s via DirectSound", desc);
140 else
141 snprintf(str, sizeof(str), "%s #%d via DirectSound", desc, count+1);
142 count++;
144 for(i = 0;i < NumDevices;i++)
146 if(strcmp(str, DeviceList[i].name) == 0)
147 break;
149 } while(i != NumDevices);
151 temp = realloc(DeviceList, sizeof(DevMap) * (NumDevices+1));
152 if(temp)
154 DeviceList = temp;
155 DeviceList[NumDevices].name = strdup(str);
156 DeviceList[NumDevices].guid = *guid;
157 NumDevices++;
160 return TRUE;
164 static ALuint DSoundProc(ALvoid *ptr)
166 ALCdevice *pDevice = (ALCdevice*)ptr;
167 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
168 DSBCAPS DSBCaps;
169 DWORD LastCursor = 0;
170 DWORD PlayCursor;
171 VOID *WritePtr1, *WritePtr2;
172 DWORD WriteCnt1, WriteCnt2;
173 BOOL Playing = FALSE;
174 DWORD FrameSize;
175 DWORD FragSize;
176 DWORD avail;
177 HRESULT err;
179 SetRTPriority();
181 memset(&DSBCaps, 0, sizeof(DSBCaps));
182 DSBCaps.dwSize = sizeof(DSBCaps);
183 err = IDirectSoundBuffer_GetCaps(pData->DSsbuffer, &DSBCaps);
184 if(FAILED(err))
186 AL_PRINT("Failed to get buffer caps: 0x%lx\n", err);
187 aluHandleDisconnect(pDevice);
188 return 1;
191 FrameSize = aluFrameSizeFromFormat(pDevice->Format);
192 FragSize = pDevice->UpdateSize * FrameSize;
194 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &LastCursor, NULL);
195 while(!pData->killNow)
197 // Get current play and write cursors
198 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
199 avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes;
201 if(avail < FragSize)
203 if(!Playing)
205 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
206 if(FAILED(err))
208 AL_PRINT("Failed to play buffer: 0x%lx\n", err);
209 aluHandleDisconnect(pDevice);
210 return 1;
213 Sleep(1);
214 continue;
216 avail -= avail%FragSize;
218 // Lock output buffer
219 WriteCnt1 = 0;
220 WriteCnt2 = 0;
221 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
223 // If the buffer is lost, restore it and lock
224 if(err == DSERR_BUFFERLOST)
226 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
227 if(SUCCEEDED(err))
229 Playing = FALSE;
230 LastCursor = 0;
231 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, 0, DSBCaps.dwBufferBytes, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
235 // Successfully locked the output buffer
236 if(SUCCEEDED(err))
238 // If we have an active context, mix data directly into output buffer otherwise fill with silence
239 aluMixData(pDevice, WritePtr1, WriteCnt1/FrameSize);
240 aluMixData(pDevice, WritePtr2, WriteCnt2/FrameSize);
242 // Unlock output buffer only when successfully locked
243 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
245 else
247 AL_PRINT("Buffer lock error: %#lx\n", err);
248 aluHandleDisconnect(pDevice);
249 return 1;
252 // Update old write cursor location
253 LastCursor += WriteCnt1+WriteCnt2;
254 LastCursor %= DSBCaps.dwBufferBytes;
257 return 0;
260 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
262 DSoundData *pData = NULL;
263 LPGUID guid = NULL;
264 HRESULT hr;
266 if(!DSoundLoad())
267 return ALC_FALSE;
269 if(!deviceName)
270 deviceName = dsDevice;
271 else if(strcmp(deviceName, dsDevice) != 0)
273 ALuint i;
275 if(!DeviceList)
277 hr = pDirectSoundEnumerateA(DSoundEnumDevices, NULL);
278 if(FAILED(hr))
279 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);
282 for(i = 0;i < NumDevices;i++)
284 if(strcmp(deviceName, DeviceList[i].name) == 0)
286 if(i > 0)
287 guid = &DeviceList[i].guid;
288 break;
291 if(i == NumDevices)
292 return ALC_FALSE;
295 //Initialise requested device
296 pData = calloc(1, sizeof(DSoundData));
297 if(!pData)
299 alcSetError(device, ALC_OUT_OF_MEMORY);
300 return ALC_FALSE;
303 //DirectSound Init code
304 hr = pDirectSoundCreate(guid, &pData->lpDS, NULL);
305 if(SUCCEEDED(hr))
306 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
307 if(FAILED(hr))
309 if(pData->lpDS)
310 IDirectSound_Release(pData->lpDS);
311 free(pData);
312 AL_PRINT("Device init failed: 0x%08lx\n", hr);
313 return ALC_FALSE;
316 device->szDeviceName = strdup(deviceName);
317 device->ExtraData = pData;
318 return ALC_TRUE;
321 static void DSoundClosePlayback(ALCdevice *device)
323 DSoundData *pData = device->ExtraData;
325 IDirectSound_Release(pData->lpDS);
326 free(pData);
327 device->ExtraData = NULL;
330 static ALCboolean DSoundResetPlayback(ALCdevice *device)
332 DSoundData *pData = (DSoundData*)device->ExtraData;
333 DSBUFFERDESC DSBDescription;
334 WAVEFORMATEXTENSIBLE OutputType;
335 DWORD frameSize = 0;
336 ALenum format = 0;
337 DWORD speakers;
338 HRESULT hr;
340 memset(&OutputType, 0, sizeof(OutputType));
342 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
343 if(SUCCEEDED(hr) && ConfigValueExists(NULL, "format"))
345 if(aluChannelsFromFormat(device->Format) == 1)
346 speakers = DSSPEAKER_COMBINED(DSSPEAKER_MONO, 0);
347 else if(aluChannelsFromFormat(device->Format) == 2)
348 speakers = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, 0);
349 else if(aluChannelsFromFormat(device->Format) == 4)
350 speakers = DSSPEAKER_COMBINED(DSSPEAKER_QUAD, 0);
351 else if(aluChannelsFromFormat(device->Format) == 6)
352 speakers = DSSPEAKER_COMBINED(DSSPEAKER_5POINT1, 0);
353 else if(aluChannelsFromFormat(device->Format) == 8)
354 speakers = DSSPEAKER_COMBINED(DSSPEAKER_7POINT1, 0);
355 else
357 AL_PRINT("Unknown format: 0x%x\n", device->Format);
358 return ALC_FALSE;
361 if(SUCCEEDED(hr))
363 speakers = DSSPEAKER_CONFIG(speakers);
364 if(speakers == DSSPEAKER_MONO)
366 if(aluBytesFromFormat(device->Format) == 1)
367 format = AL_FORMAT_MONO8;
368 else if(aluBytesFromFormat(device->Format) == 2)
369 format = AL_FORMAT_MONO16;
370 else if(aluBytesFromFormat(device->Format) == 4)
371 format = AL_FORMAT_MONO_FLOAT32;
372 OutputType.dwChannelMask = SPEAKER_FRONT_CENTER;
374 else if(speakers == DSSPEAKER_STEREO)
376 if(aluBytesFromFormat(device->Format) == 1)
377 format = AL_FORMAT_STEREO8;
378 else if(aluBytesFromFormat(device->Format) == 2)
379 format = AL_FORMAT_STEREO16;
380 else if(aluBytesFromFormat(device->Format) == 4)
381 format = AL_FORMAT_STEREO_FLOAT32;
382 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
383 SPEAKER_FRONT_RIGHT;
385 else if(speakers == DSSPEAKER_QUAD)
387 if(aluBytesFromFormat(device->Format) == 1)
388 format = AL_FORMAT_QUAD8;
389 else if(aluBytesFromFormat(device->Format) == 2)
390 format = AL_FORMAT_QUAD16;
391 else if(aluBytesFromFormat(device->Format) == 4)
392 format = AL_FORMAT_QUAD32;
393 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
394 SPEAKER_FRONT_RIGHT |
395 SPEAKER_BACK_LEFT |
396 SPEAKER_BACK_RIGHT;
398 else if(speakers == DSSPEAKER_5POINT1)
400 if(aluBytesFromFormat(device->Format) == 1)
401 format = AL_FORMAT_51CHN8;
402 else if(aluBytesFromFormat(device->Format) == 2)
403 format = AL_FORMAT_51CHN16;
404 else if(aluBytesFromFormat(device->Format) == 4)
405 format = AL_FORMAT_51CHN32;
406 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
407 SPEAKER_FRONT_RIGHT |
408 SPEAKER_FRONT_CENTER |
409 SPEAKER_LOW_FREQUENCY |
410 SPEAKER_BACK_LEFT |
411 SPEAKER_BACK_RIGHT;
413 else if(speakers == DSSPEAKER_7POINT1)
415 if(aluBytesFromFormat(device->Format) == 1)
416 format = AL_FORMAT_71CHN8;
417 else if(aluBytesFromFormat(device->Format) == 2)
418 format = AL_FORMAT_71CHN16;
419 else if(aluBytesFromFormat(device->Format) == 4)
420 format = AL_FORMAT_71CHN32;
421 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
422 SPEAKER_FRONT_RIGHT |
423 SPEAKER_FRONT_CENTER |
424 SPEAKER_LOW_FREQUENCY |
425 SPEAKER_BACK_LEFT |
426 SPEAKER_BACK_RIGHT |
427 SPEAKER_SIDE_LEFT |
428 SPEAKER_SIDE_RIGHT;
430 else
431 format = device->Format;
432 frameSize = aluFrameSizeFromFormat(format);
434 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
435 OutputType.Format.nChannels = aluChannelsFromFormat(format);
436 OutputType.Format.wBitsPerSample = aluBytesFromFormat(format) * 8;
437 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
438 OutputType.Format.nSamplesPerSec = device->Frequency;
439 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
440 OutputType.Format.cbSize = 0;
443 if(OutputType.Format.nChannels > 2 || OutputType.Format.wBitsPerSample > 16)
445 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
446 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
447 OutputType.Format.cbSize = 22;
448 if(OutputType.Format.wBitsPerSample == 32)
449 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
450 else
451 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
453 else
455 if(SUCCEEDED(hr))
457 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
458 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
459 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
460 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
462 if(SUCCEEDED(hr))
463 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
466 if(SUCCEEDED(hr))
468 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
469 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
470 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
471 DSBDescription.dwBufferBytes=device->UpdateSize * device->NumUpdates * frameSize;
472 DSBDescription.lpwfxFormat=&OutputType.Format;
473 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
476 if(SUCCEEDED(hr))
478 device->Format = format;
479 SetDefaultWFXChannelOrder(device);
480 pData->thread = StartThread(DSoundProc, device);
481 if(!pData->thread)
482 hr = E_FAIL;
485 if(FAILED(hr))
487 if (pData->DSsbuffer)
488 IDirectSoundBuffer_Release(pData->DSsbuffer);
489 pData->DSsbuffer = NULL;
490 if (pData->DSpbuffer)
491 IDirectSoundBuffer_Release(pData->DSpbuffer);
492 pData->DSpbuffer = NULL;
493 return ALC_FALSE;
496 return ALC_TRUE;
499 static void DSoundStopPlayback(ALCdevice *device)
501 DSoundData *pData = device->ExtraData;
503 if(!pData->thread)
504 return;
506 pData->killNow = 1;
507 StopThread(pData->thread);
508 pData->thread = NULL;
510 pData->killNow = 0;
512 IDirectSoundBuffer_Release(pData->DSsbuffer);
513 pData->DSsbuffer = NULL;
514 if (pData->DSpbuffer)
515 IDirectSoundBuffer_Release(pData->DSpbuffer);
516 pData->DSpbuffer = NULL;
520 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName)
522 (void)pDevice;
523 (void)deviceName;
524 return ALC_FALSE;
527 static void DSoundCloseCapture(ALCdevice *pDevice)
529 (void)pDevice;
532 static void DSoundStartCapture(ALCdevice *pDevice)
534 (void)pDevice;
537 static void DSoundStopCapture(ALCdevice *pDevice)
539 (void)pDevice;
542 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
544 (void)pDevice;
545 (void)pBuffer;
546 (void)lSamples;
549 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
551 (void)pDevice;
552 return 0;
556 BackendFuncs DSoundFuncs = {
557 DSoundOpenPlayback,
558 DSoundClosePlayback,
559 DSoundResetPlayback,
560 DSoundStopPlayback,
561 DSoundOpenCapture,
562 DSoundCloseCapture,
563 DSoundStartCapture,
564 DSoundStopCapture,
565 DSoundCaptureSamples,
566 DSoundAvailableSamples
570 void alcDSoundInit(BackendFuncs *FuncList)
572 *FuncList = DSoundFuncs;
575 void alcDSoundDeinit(void)
577 ALuint i;
579 for(i = 0;i < NumDevices;++i)
580 free(DeviceList[i].name);
581 free(DeviceList);
582 DeviceList = NULL;
583 NumDevices = 0;
585 if(ds_handle)
587 #ifdef _WIN32
588 FreeLibrary(ds_handle);
589 #endif
590 ds_handle = NULL;
594 void alcDSoundProbe(int type)
596 if(!DSoundLoad()) return;
598 if(type == DEVICE_PROBE)
599 AppendDeviceList(dsDevice);
600 else if(type == ALL_DEVICE_PROBE)
602 HRESULT hr;
603 ALuint i;
605 for(i = 0;i < NumDevices;++i)
606 free(DeviceList[i].name);
607 free(DeviceList);
608 DeviceList = NULL;
609 NumDevices = 0;
611 hr = pDirectSoundEnumerateA(DSoundEnumDevices, NULL);
612 if(FAILED(hr))
613 AL_PRINT("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr);
614 else
616 for(i = 0;i < NumDevices;i++)
617 AppendAllDeviceList(DeviceList[i].name);