2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 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
23 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
32 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
33 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
35 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
37 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
38 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
39 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
40 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
43 #include <devpropdef.h>
45 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
72 ALuint CPUCapFlags
= 0;
75 void FillCPUCaps(ALuint capfilter
)
79 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
80 /* FIXME: We really should get this for all available CPUs in case different
81 * CPUs have different caps (is that possible on one machine?). */
85 char str
[sizeof(unsigned int[4])];
88 if(!__get_cpuid(0, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
89 ERR("Failed to get CPUID\n");
92 unsigned int maxfunc
= cpuinf
[0].regs
[0];
93 unsigned int maxextfunc
= 0;
95 if(__get_cpuid(0x80000000, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
96 maxextfunc
= cpuinf
[0].regs
[0];
97 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
99 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
100 if(maxextfunc
>= 0x80000004 &&
101 __get_cpuid(0x80000002, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]) &&
102 __get_cpuid(0x80000003, &cpuinf
[1].regs
[0], &cpuinf
[1].regs
[1], &cpuinf
[1].regs
[2], &cpuinf
[1].regs
[3]) &&
103 __get_cpuid(0x80000004, &cpuinf
[2].regs
[0], &cpuinf
[2].regs
[1], &cpuinf
[2].regs
[2], &cpuinf
[2].regs
[3]))
104 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
107 __get_cpuid(1, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
110 if((cpuinf
[0].regs
[3]&bit_SSE
))
115 #elif defined(HAVE___CPUID)
118 char str
[sizeof(int[4])];
120 unsigned int maxfunc
= 0;
121 unsigned int maxextfunc
= 0;
123 (__cpuid
)(cpuinf
[0].regs
, 0);
124 maxfunc
= cpuinf
[0].regs
[0];
126 (__cpuid
)(cpuinf
[0].regs
, 0x80000000);
127 maxextfunc
= cpuinf
[0].regs
[0];
128 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
130 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
131 if(maxextfunc
>= 0x80000004)
133 (__cpuid
)(cpuinf
[0].regs
, 0x80000002);
134 (__cpuid
)(cpuinf
[1].regs
, 0x80000003);
135 (__cpuid
)(cpuinf
[2].regs
, 0x80000004);
136 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
141 (__cpuid
)(cpuinf
[0].regs
, 1);
142 if((cpuinf
[0].regs
[3]&(1<<25)))
148 /* Assume Neon support if compiled with it */
149 caps
|= CPU_CAP_NEON
;
152 TRACE("Got caps:%s%s%s\n", ((caps
&CPU_CAP_SSE
)?((capfilter
&CPU_CAP_SSE
)?" SSE":" (SSE)"):""),
153 ((caps
&CPU_CAP_NEON
)?((capfilter
&CPU_CAP_NEON
)?" Neon":" (Neon)"):""),
154 ((!caps
)?" -none-":""));
155 CPUCapFlags
= caps
& capfilter
;
159 void *al_malloc(size_t alignment
, size_t size
)
161 #if defined(HAVE_ALIGNED_ALLOC)
162 size
= (size
+(alignment
-1))&~(alignment
-1);
163 return aligned_alloc(alignment
, size
);
164 #elif defined(HAVE_POSIX_MEMALIGN)
166 if(posix_memalign(&ret
, alignment
, size
) == 0)
169 #elif defined(HAVE__ALIGNED_MALLOC)
170 return _aligned_malloc(size
, alignment
);
172 char *ret
= malloc(size
+alignment
);
176 while(((ALintptrEXT
)ret
&(alignment
-1)) != 0)
183 void *al_calloc(size_t alignment
, size_t size
)
185 void *ret
= al_malloc(alignment
, size
);
186 if(ret
) memset(ret
, 0, size
);
190 void al_free(void *ptr
)
192 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
194 #elif defined(HAVE__ALIGNED_MALLOC)
202 } while(*finder
== 0x55);
209 #if (defined(HAVE___CONTROL87_2) || defined(HAVE__CONTROLFP)) && (defined(__x86_64__) || defined(_M_X64))
210 /* Win64 doesn't allow us to set the precision control. */
215 void SetMixerFPUMode(FPUCtl
*ctl
)
217 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
218 unsigned short fpuState
;
219 __asm__
__volatile__("fnstcw %0" : "=m" (*&fpuState
));
220 ctl
->state
= fpuState
;
221 fpuState
&= ~0x300; /* clear precision to single */
222 fpuState
|= 0xC00; /* set round-to-zero */
223 __asm__
__volatile__("fldcw %0" : : "m" (*&fpuState
));
225 if((CPUCapFlags
&CPU_CAP_SSE
))
228 __asm__
__volatile__("stmxcsr %0" : "=m" (*&sseState
));
229 ctl
->sse_state
= sseState
;
230 sseState
|= 0x0C00; /* set round-to-zero */
231 sseState
|= 0x8000; /* set flush-to-zero */
232 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
235 #elif defined(HAVE___CONTROL87_2)
237 __control87_2(0, 0, &ctl
->state
, &ctl
->sse_state
);
238 __control87_2(_RC_CHOP
|_PC_24
, _MCW_RC
|_MCW_PC
, &mode
, NULL
);
240 if((CPUCapFlags
&CPU_CAP_SSE
))
241 __control87_2(_RC_CHOP
|_DN_FLUSH
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
243 #elif defined(HAVE__CONTROLFP)
244 ctl
->state
= _controlfp(0, 0);
245 (void)_controlfp(_RC_CHOP
|_PC_24
, _MCW_RC
|_MCW_PC
);
246 #elif defined(HAVE_FESETROUND)
247 ctl
->state
= fegetround();
249 fesetround(FE_TOWARDZERO
);
254 void RestoreFPUMode(const FPUCtl
*ctl
)
256 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
257 unsigned short fpuState
= ctl
->state
;
258 __asm__
__volatile__("fldcw %0" : : "m" (*&fpuState
));
260 if((CPUCapFlags
&CPU_CAP_SSE
))
261 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&ctl
->sse_state
));
263 #elif defined(HAVE___CONTROL87_2)
265 __control87_2(ctl
->state
, _MCW_RC
|_MCW_PC
, &mode
, NULL
);
267 if((CPUCapFlags
&CPU_CAP_SSE
))
268 __control87_2(ctl
->sse_state
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
270 #elif defined(HAVE__CONTROLFP)
271 _controlfp(ctl
->state
, _MCW_RC
|_MCW_PC
);
272 #elif defined(HAVE_FESETROUND)
273 fesetround(ctl
->state
);
279 void pthread_once(pthread_once_t
*once
, void (*callback
)(void))
282 while((ret
=InterlockedExchange(once
, 1)) == 1)
286 InterlockedExchange(once
, 2);
290 int pthread_key_create(pthread_key_t
*key
, void (*callback
)(void*))
294 InsertUIntMapEntry(&TlsDestructor
, *key
, callback
);
298 int pthread_key_delete(pthread_key_t key
)
300 InsertUIntMapEntry(&TlsDestructor
, key
, NULL
);
305 void *pthread_getspecific(pthread_key_t key
)
306 { return TlsGetValue(key
); }
308 int pthread_setspecific(pthread_key_t key
, void *val
)
310 TlsSetValue(key
, val
);
315 void *LoadLib(const char *name
)
316 { return LoadLibraryA(name
); }
317 void CloseLib(void *handle
)
318 { FreeLibrary((HANDLE
)handle
); }
319 void *GetSymbol(void *handle
, const char *name
)
323 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
325 ERR("Failed to load %s\n", name
);
329 WCHAR
*strdupW(const WCHAR
*str
)
339 ret
= calloc(sizeof(WCHAR
), len
+1);
341 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
348 #ifdef HAVE_PTHREAD_NP_H
349 #include <pthread_np.h>
353 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
355 pthread_mutexattr_t attrib
;
358 ret
= pthread_mutexattr_init(&attrib
);
361 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
362 #ifdef HAVE_PTHREAD_NP_H
364 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
367 ret
= pthread_mutex_init(cs
, &attrib
);
370 pthread_mutexattr_destroy(&attrib
);
372 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
375 ret
= pthread_mutex_destroy(cs
);
378 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
381 ret
= pthread_mutex_lock(cs
);
384 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
387 ret
= pthread_mutex_unlock(cs
);
391 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
392 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
393 * Additionally, Win32 is supposed to measure the time since Windows started,
394 * as opposed to the actual time. */
395 ALuint
timeGetTime(void)
397 #if _POSIX_TIMERS > 0
401 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
402 #if _POSIX_MONOTONIC_CLOCK == 0
403 static int hasmono
= 0;
404 if(hasmono
> 0 || (hasmono
== 0 &&
405 (hasmono
=sysconf(_SC_MONOTONIC_CLOCK
)) > 0))
407 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
410 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
413 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
418 ret
= gettimeofday(&tv
, NULL
);
421 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
427 struct timespec tv
, rem
;
428 tv
.tv_nsec
= (t
*1000000)%1000000000;
431 while(nanosleep(&tv
, &rem
) == -1 && errno
== EINTR
)
437 void *LoadLib(const char *name
)
443 handle
= dlopen(name
, RTLD_NOW
);
444 if((err
=dlerror()) != NULL
)
448 void CloseLib(void *handle
)
450 void *GetSymbol(void *handle
, const char *name
)
456 sym
= dlsym(handle
, name
);
457 if((err
=dlerror()) != NULL
)
459 WARN("Failed to load %s: %s\n", name
, err
);
469 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
474 i
= snprintf(str
, sizeof(str
), "AL lib: %s %s: ", type
, func
);
475 if(i
> 0 && (unsigned int)i
< sizeof(str
))
479 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
482 str
[sizeof(str
)-1] = 0;
484 fprintf(LogFile
, "%s", str
);
489 void SetRTPriority(void)
491 ALboolean failed
= AL_FALSE
;
495 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
496 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
499 struct sched_param param
;
500 /* Use the minimum real-time priority possible for now (on Linux this
501 * should be 1 for SCHED_RR) */
502 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
503 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
506 /* Real-time priority not available */
507 failed
= (RTPrioLevel
>0);
510 ERR("Failed to set priority level for thread\n");
514 static void Lock(volatile ALenum
*l
)
516 while(ExchangeInt(l
, AL_TRUE
) == AL_TRUE
)
520 static void Unlock(volatile ALenum
*l
)
522 ExchangeInt(l
, AL_FALSE
);
525 void RWLockInit(RWLock
*lock
)
527 lock
->read_count
= 0;
528 lock
->write_count
= 0;
529 lock
->read_lock
= AL_FALSE
;
530 lock
->read_entry_lock
= AL_FALSE
;
531 lock
->write_lock
= AL_FALSE
;
534 void ReadLock(RWLock
*lock
)
536 Lock(&lock
->read_entry_lock
);
537 Lock(&lock
->read_lock
);
538 if(IncrementRef(&lock
->read_count
) == 1)
539 Lock(&lock
->write_lock
);
540 Unlock(&lock
->read_lock
);
541 Unlock(&lock
->read_entry_lock
);
544 void ReadUnlock(RWLock
*lock
)
546 if(DecrementRef(&lock
->read_count
) == 0)
547 Unlock(&lock
->write_lock
);
550 void WriteLock(RWLock
*lock
)
552 if(IncrementRef(&lock
->write_count
) == 1)
553 Lock(&lock
->read_lock
);
554 Lock(&lock
->write_lock
);
557 void WriteUnlock(RWLock
*lock
)
559 Unlock(&lock
->write_lock
);
560 if(DecrementRef(&lock
->write_count
) == 0)
561 Unlock(&lock
->read_lock
);
565 void InitUIntMap(UIntMap
*map
, ALsizei limit
)
571 RWLockInit(&map
->lock
);
574 void ResetUIntMap(UIntMap
*map
)
576 WriteLock(&map
->lock
);
581 WriteUnlock(&map
->lock
);
584 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
588 WriteLock(&map
->lock
);
592 ALsizei high
= map
->size
- 1;
595 ALsizei mid
= low
+ (high
-low
)/2;
596 if(map
->array
[mid
].key
< key
)
601 if(map
->array
[low
].key
< key
)
606 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
608 if(map
->size
== map
->limit
)
610 WriteUnlock(&map
->lock
);
611 return AL_OUT_OF_MEMORY
;
614 if(map
->size
== map
->maxsize
)
619 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
620 if(newsize
>= map
->maxsize
)
621 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
624 WriteUnlock(&map
->lock
);
625 return AL_OUT_OF_MEMORY
;
628 map
->maxsize
= newsize
;
632 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
633 (map
->size
-pos
)*sizeof(map
->array
[0]));
636 map
->array
[pos
].key
= key
;
637 map
->array
[pos
].value
= value
;
638 WriteUnlock(&map
->lock
);
643 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
646 WriteLock(&map
->lock
);
650 ALsizei high
= map
->size
- 1;
653 ALsizei mid
= low
+ (high
-low
)/2;
654 if(map
->array
[mid
].key
< key
)
659 if(map
->array
[low
].key
== key
)
661 ptr
= map
->array
[low
].value
;
662 if(low
< map
->size
-1)
663 memmove(&map
->array
[low
], &map
->array
[low
+1],
664 (map
->size
-1-low
)*sizeof(map
->array
[0]));
668 WriteUnlock(&map
->lock
);
672 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
675 ReadLock(&map
->lock
);
679 ALsizei high
= map
->size
- 1;
682 ALsizei mid
= low
+ (high
-low
)/2;
683 if(map
->array
[mid
].key
< key
)
688 if(map
->array
[low
].key
== key
)
689 ptr
= map
->array
[low
].value
;
691 ReadUnlock(&map
->lock
);