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
40 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
49 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
50 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
52 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
54 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
55 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
56 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
57 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
60 #include <devpropdef.h>
62 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
69 ALuint CPUCapFlags
= 0;
72 void FillCPUCaps(ALuint capfilter
)
76 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
77 /* FIXME: We really should get this for all available CPUs in case different
78 * CPUs have different caps (is that possible on one machine?). */
82 char str
[sizeof(unsigned int[4])];
85 if(!__get_cpuid(0, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
86 ERR("Failed to get CPUID\n");
89 unsigned int maxfunc
= cpuinf
[0].regs
[0];
90 unsigned int maxextfunc
= 0;
92 if(__get_cpuid(0x80000000, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
93 maxextfunc
= cpuinf
[0].regs
[0];
94 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
96 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
97 if(maxextfunc
>= 0x80000004 &&
98 __get_cpuid(0x80000002, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]) &&
99 __get_cpuid(0x80000003, &cpuinf
[1].regs
[0], &cpuinf
[1].regs
[1], &cpuinf
[1].regs
[2], &cpuinf
[1].regs
[3]) &&
100 __get_cpuid(0x80000004, &cpuinf
[2].regs
[0], &cpuinf
[2].regs
[1], &cpuinf
[2].regs
[2], &cpuinf
[2].regs
[3]))
101 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
104 __get_cpuid(1, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
107 if((cpuinf
[0].regs
[3]&bit_SSE
))
115 /* Assume Neon support if compiled with it */
116 caps
|= CPU_CAP_NEON
;
119 TRACE("Got caps:%s%s%s\n", ((caps
&CPU_CAP_SSE
)?((capfilter
&CPU_CAP_SSE
)?" SSE":" (SSE)"):""),
120 ((caps
&CPU_CAP_NEON
)?((capfilter
&CPU_CAP_NEON
)?" Neon":" (Neon)"):""),
121 ((!caps
)?" -none-":""));
122 CPUCapFlags
= caps
& capfilter
;
126 void *al_malloc(size_t alignment
, size_t size
)
128 #if defined(HAVE_ALIGNED_ALLOC)
129 size
= (size
+(alignment
-1))&~(alignment
-1);
130 return aligned_alloc(alignment
, size
);
131 #elif defined(HAVE_POSIX_MEMALIGN)
133 if(posix_memalign(&ret
, alignment
, size
) == 0)
136 #elif defined(HAVE__ALIGNED_MALLOC)
137 return _aligned_malloc(size
, alignment
);
139 char *ret
= malloc(size
+alignment
);
143 while(((ALintptrEXT
)ret
&(alignment
-1)) != 0)
150 void *al_calloc(size_t alignment
, size_t size
)
152 void *ret
= al_malloc(alignment
, size
);
153 if(ret
) memset(ret
, 0, size
);
157 void al_free(void *ptr
)
159 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
161 #elif defined(HAVE__ALIGNED_MALLOC)
169 } while(*finder
== 0x55);
176 #if (defined(HAVE___CONTROL87_2) || defined(HAVE__CONTROLFP)) && (defined(__x86_64__) || defined(_M_X64))
177 /* Win64 doesn't allow us to set the precision control. */
182 void SetMixerFPUMode(FPUCtl
*ctl
)
184 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
185 unsigned short fpuState
;
186 __asm__
__volatile__("fnstcw %0" : "=m" (*&fpuState
));
187 ctl
->state
= fpuState
;
188 fpuState
&= ~0x300; /* clear precision to single */
189 fpuState
|= 0xC00; /* set round-to-zero */
190 __asm__
__volatile__("fldcw %0" : : "m" (*&fpuState
));
192 if((CPUCapFlags
&CPU_CAP_SSE
))
195 __asm__
__volatile__("stmxcsr %0" : "=m" (*&sseState
));
196 ctl
->sse_state
= sseState
;
197 sseState
&= ~0x300; /* clear precision to single */
198 sseState
|= 0xC00; /* set round-to-zero */
199 sseState
|= 0x8000; /* set flush-to-zero */
200 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
203 #elif defined(HAVE___CONTROL87_2)
205 __control87_2(0, 0, &ctl
->state
, &ctl
->sse_state
);
206 __control87_2(_RC_CHOP
|_PC_24
, _MCW_RC
|_MCW_PC
, &mode
, NULL
);
208 if((CPUCapFlags
&CPU_CAP_SSE
))
209 __control87_2(_RC_CHOP
|_PC_24
|_DN_FLUSH
, _MCW_RC
|_MCW_PC
|_MCW_DN
, NULL
, &mode
);
211 #elif defined(HAVE__CONTROLFP)
212 ctl
->state
= _controlfp(0, 0);
213 (void)_controlfp(_RC_CHOP
|_PC_24
, _MCW_RC
|_MCW_PC
);
214 #elif defined(HAVE_FESETROUND)
215 ctl
->state
= fegetround();
217 fesetround(FE_TOWARDZERO
);
222 void RestoreFPUMode(const FPUCtl
*ctl
)
224 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
225 unsigned short fpuState
= ctl
->state
;
226 __asm__
__volatile__("fldcw %0" : : "m" (*&fpuState
));
228 if((CPUCapFlags
&CPU_CAP_SSE
))
229 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&ctl
->sse_state
));
231 #elif defined(HAVE___CONTROL87_2)
233 __control87_2(ctl
->state
, _MCW_RC
|_MCW_PC
, &mode
, NULL
);
235 if((CPUCapFlags
&CPU_CAP_SSE
))
236 __control87_2(ctl
->sse_state
, _MCW_RC
|_MCW_PC
|_MCW_DN
, NULL
, &mode
);
238 #elif defined(HAVE__CONTROLFP)
239 _controlfp(ctl
->state
, _MCW_RC
|_MCW_PC
);
240 #elif defined(HAVE_FESETROUND)
241 fesetround(ctl
->state
);
247 void pthread_once(pthread_once_t
*once
, void (*callback
)(void))
250 while((ret
=InterlockedExchange(once
, 1)) == 1)
254 InterlockedExchange(once
, 2);
258 int pthread_key_create(pthread_key_t
*key
, void (*callback
)(void*))
262 InsertUIntMapEntry(&TlsDestructor
, *key
, callback
);
266 int pthread_key_delete(pthread_key_t key
)
268 InsertUIntMapEntry(&TlsDestructor
, key
, NULL
);
273 void *pthread_getspecific(pthread_key_t key
)
274 { return TlsGetValue(key
); }
276 int pthread_setspecific(pthread_key_t key
, void *val
)
278 TlsSetValue(key
, val
);
283 void *LoadLib(const char *name
)
284 { return LoadLibraryA(name
); }
285 void CloseLib(void *handle
)
286 { FreeLibrary((HANDLE
)handle
); }
287 void *GetSymbol(void *handle
, const char *name
)
291 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
293 ERR("Failed to load %s\n", name
);
297 WCHAR
*strdupW(const WCHAR
*str
)
307 ret
= calloc(sizeof(WCHAR
), len
+1);
309 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
316 #ifdef HAVE_PTHREAD_NP_H
317 #include <pthread_np.h>
321 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
323 pthread_mutexattr_t attrib
;
326 ret
= pthread_mutexattr_init(&attrib
);
329 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
330 #ifdef HAVE_PTHREAD_NP_H
332 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
335 ret
= pthread_mutex_init(cs
, &attrib
);
338 pthread_mutexattr_destroy(&attrib
);
340 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
343 ret
= pthread_mutex_destroy(cs
);
346 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
349 ret
= pthread_mutex_lock(cs
);
352 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
355 ret
= pthread_mutex_unlock(cs
);
359 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
360 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
361 * Additionally, Win32 is supposed to measure the time since Windows started,
362 * as opposed to the actual time. */
363 ALuint
timeGetTime(void)
365 #if _POSIX_TIMERS > 0
369 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
370 #if _POSIX_MONOTONIC_CLOCK == 0
371 static int hasmono
= 0;
372 if(hasmono
> 0 || (hasmono
== 0 &&
373 (hasmono
=sysconf(_SC_MONOTONIC_CLOCK
)) > 0))
375 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
378 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
381 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
386 ret
= gettimeofday(&tv
, NULL
);
389 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
395 struct timespec tv
, rem
;
396 tv
.tv_nsec
= (t
*1000000)%1000000000;
399 while(nanosleep(&tv
, &rem
) == -1 && errno
== EINTR
)
405 void *LoadLib(const char *name
)
411 handle
= dlopen(name
, RTLD_NOW
);
412 if((err
=dlerror()) != NULL
)
416 void CloseLib(void *handle
)
418 void *GetSymbol(void *handle
, const char *name
)
424 sym
= dlsym(handle
, name
);
425 if((err
=dlerror()) != NULL
)
427 WARN("Failed to load %s: %s\n", name
, err
);
437 void al_print(const char *func
, const char *fmt
, ...)
442 i
= snprintf(str
, sizeof(str
), "AL lib: %s: ", func
);
443 if(i
< (int)sizeof(str
) && i
> 0)
447 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
450 str
[sizeof(str
)-1] = 0;
452 fprintf(LogFile
, "%s", str
);
457 void SetRTPriority(void)
459 ALboolean failed
= AL_FALSE
;
463 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
464 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
467 struct sched_param param
;
468 /* Use the minimum real-time priority possible for now (on Linux this
469 * should be 1 for SCHED_RR) */
470 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
471 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
474 /* Real-time priority not available */
475 failed
= (RTPrioLevel
>0);
478 ERR("Failed to set priority level for thread\n");
482 static void Lock(volatile ALenum
*l
)
484 while(ExchangeInt(l
, AL_TRUE
) == AL_TRUE
)
488 static void Unlock(volatile ALenum
*l
)
490 ExchangeInt(l
, AL_FALSE
);
493 void RWLockInit(RWLock
*lock
)
495 lock
->read_count
= 0;
496 lock
->write_count
= 0;
497 lock
->read_lock
= AL_FALSE
;
498 lock
->read_entry_lock
= AL_FALSE
;
499 lock
->write_lock
= AL_FALSE
;
502 void ReadLock(RWLock
*lock
)
504 Lock(&lock
->read_entry_lock
);
505 Lock(&lock
->read_lock
);
506 if(IncrementRef(&lock
->read_count
) == 1)
507 Lock(&lock
->write_lock
);
508 Unlock(&lock
->read_lock
);
509 Unlock(&lock
->read_entry_lock
);
512 void ReadUnlock(RWLock
*lock
)
514 if(DecrementRef(&lock
->read_count
) == 0)
515 Unlock(&lock
->write_lock
);
518 void WriteLock(RWLock
*lock
)
520 if(IncrementRef(&lock
->write_count
) == 1)
521 Lock(&lock
->read_lock
);
522 Lock(&lock
->write_lock
);
525 void WriteUnlock(RWLock
*lock
)
527 Unlock(&lock
->write_lock
);
528 if(DecrementRef(&lock
->write_count
) == 0)
529 Unlock(&lock
->read_lock
);
533 void InitUIntMap(UIntMap
*map
, ALsizei limit
)
539 RWLockInit(&map
->lock
);
542 void ResetUIntMap(UIntMap
*map
)
544 WriteLock(&map
->lock
);
549 WriteUnlock(&map
->lock
);
552 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
556 WriteLock(&map
->lock
);
560 ALsizei high
= map
->size
- 1;
563 ALsizei mid
= low
+ (high
-low
)/2;
564 if(map
->array
[mid
].key
< key
)
569 if(map
->array
[low
].key
< key
)
574 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
576 if(map
->size
== map
->limit
)
578 WriteUnlock(&map
->lock
);
579 return AL_OUT_OF_MEMORY
;
582 if(map
->size
== map
->maxsize
)
587 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
588 if(newsize
>= map
->maxsize
)
589 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
592 WriteUnlock(&map
->lock
);
593 return AL_OUT_OF_MEMORY
;
596 map
->maxsize
= newsize
;
600 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
601 (map
->size
-pos
)*sizeof(map
->array
[0]));
604 map
->array
[pos
].key
= key
;
605 map
->array
[pos
].value
= value
;
606 WriteUnlock(&map
->lock
);
611 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
614 WriteLock(&map
->lock
);
618 ALsizei high
= map
->size
- 1;
621 ALsizei mid
= low
+ (high
-low
)/2;
622 if(map
->array
[mid
].key
< key
)
627 if(map
->array
[low
].key
== key
)
629 ptr
= map
->array
[low
].value
;
630 if(low
< map
->size
-1)
631 memmove(&map
->array
[low
], &map
->array
[low
+1],
632 (map
->size
-1-low
)*sizeof(map
->array
[0]));
636 WriteUnlock(&map
->lock
);
640 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
643 ReadLock(&map
->lock
);
647 ALsizei high
= map
->size
- 1;
650 ALsizei mid
= low
+ (high
-low
)/2;
651 if(map
->array
[mid
].key
< key
)
656 if(map
->array
[low
].key
== key
)
657 ptr
= map
->array
[low
].value
;
659 ReadUnlock(&map
->lock
);