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
29 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
38 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
39 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
41 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
43 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
44 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
45 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
46 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
49 #include <devpropdef.h>
50 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
68 ALuint CPUCapFlags
= 0;
71 void FillCPUCaps(ALuint capfilter
)
75 /* FIXME: We really should get this for all available CPUs in case different
76 * CPUs have different caps (is that possible on one machine?). */
77 #if defined(HAVE_CPUID_H) && (defined(__i386__) || defined(__x86_64__) || \
78 defined(_M_IX86) || defined(_M_X64))
81 char str
[sizeof(unsigned int[4])];
84 if(!__get_cpuid(0, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
85 ERR("Failed to get CPUID\n");
88 unsigned int maxfunc
= cpuinf
[0].regs
[0];
89 unsigned int maxextfunc
= 0;
91 if(__get_cpuid(0x80000000, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
92 maxextfunc
= cpuinf
[0].regs
[0];
93 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
95 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
96 if(maxextfunc
>= 0x80000004 &&
97 __get_cpuid(0x80000002, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]) &&
98 __get_cpuid(0x80000003, &cpuinf
[1].regs
[0], &cpuinf
[1].regs
[1], &cpuinf
[1].regs
[2], &cpuinf
[1].regs
[3]) &&
99 __get_cpuid(0x80000004, &cpuinf
[2].regs
[0], &cpuinf
[2].regs
[1], &cpuinf
[2].regs
[2], &cpuinf
[2].regs
[3]))
100 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
103 __get_cpuid(1, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
106 if((cpuinf
[0].regs
[3]&bit_SSE
))
111 #elif defined(HAVE_WINDOWS_H)
112 HMODULE k32
= GetModuleHandleA("kernel32.dll");
113 BOOL (WINAPI
*IsProcessorFeaturePresent
)(DWORD ProcessorFeature
);
114 IsProcessorFeaturePresent
= (BOOL(WINAPI
*)(DWORD
))GetProcAddress(k32
, "IsProcessorFeaturePresent");
115 if(!IsProcessorFeaturePresent
)
116 ERR("IsProcessorFeaturePresent not available; CPU caps not detected\n");
119 if(IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE
))
124 /* Assume Neon support if compiled with it */
125 caps
|= CPU_CAP_NEON
;
128 TRACE("Got caps:%s%s%s\n", ((caps
&CPU_CAP_SSE
)?((capfilter
&CPU_CAP_SSE
)?" SSE":" (SSE)"):""),
129 ((caps
&CPU_CAP_NEON
)?((capfilter
&CPU_CAP_NEON
)?" Neon":" (Neon)"):""),
130 ((!caps
)?" -none-":""));
131 CPUCapFlags
= caps
& capfilter
;
135 void *al_malloc(size_t alignment
, size_t size
)
137 #if defined(HAVE_ALIGNED_ALLOC)
138 size
= (size
+(alignment
-1))&~(alignment
-1);
139 return aligned_alloc(alignment
, size
);
140 #elif defined(HAVE_POSIX_MEMALIGN)
142 if(posix_memalign(&ret
, alignment
, size
) == 0)
145 #elif defined(HAVE__ALIGNED_MALLOC)
146 return _aligned_malloc(size
, alignment
);
148 char *ret
= malloc(size
+alignment
);
152 while(((ALintptrEXT
)ret
&(alignment
-1)) != 0)
159 void *al_calloc(size_t alignment
, size_t size
)
161 void *ret
= al_malloc(alignment
, size
);
162 if(ret
) memset(ret
, 0, size
);
166 void al_free(void *ptr
)
168 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
170 #elif defined(HAVE__ALIGNED_MALLOC)
178 } while(*finder
== 0x55);
185 #if (defined(HAVE___CONTROL87_2) || defined(HAVE__CONTROLFP)) && (defined(__x86_64__) || defined(_M_X64))
186 /* Win64 doesn't allow us to set the precision control. */
191 void SetMixerFPUMode(FPUCtl
*ctl
)
193 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
194 unsigned short fpuState
;
195 __asm__
__volatile__("fnstcw %0" : "=m" (*&fpuState
));
196 ctl
->state
= fpuState
;
197 fpuState
&= ~0x300; /* clear precision to single */
198 fpuState
|= 0xC00; /* set round-to-zero */
199 __asm__
__volatile__("fldcw %0" : : "m" (*&fpuState
));
201 if((CPUCapFlags
&CPU_CAP_SSE
))
204 __asm__
__volatile__("stmxcsr %0" : "=m" (*&sseState
));
205 ctl
->sse_state
= sseState
;
206 sseState
|= 0x0C00; /* set round-to-zero */
207 sseState
|= 0x8000; /* set flush-to-zero */
208 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
211 #elif defined(HAVE___CONTROL87_2)
213 __control87_2(0, 0, &ctl
->state
, NULL
);
214 __control87_2(_RC_CHOP
|_PC_24
, _MCW_RC
|_MCW_PC
, &mode
, NULL
);
216 if((CPUCapFlags
&CPU_CAP_SSE
))
218 __control87_2(0, 0, NULL
, &ctl
->sse_state
);
219 __control87_2(_RC_CHOP
|_DN_FLUSH
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
222 #elif defined(HAVE__CONTROLFP)
223 ctl
->state
= _controlfp(0, 0);
224 (void)_controlfp(_RC_CHOP
|_PC_24
, _MCW_RC
|_MCW_PC
);
225 #elif defined(HAVE_FESETROUND)
226 ctl
->state
= fegetround();
228 fesetround(FE_TOWARDZERO
);
233 void RestoreFPUMode(const FPUCtl
*ctl
)
235 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
236 unsigned short fpuState
= ctl
->state
;
237 __asm__
__volatile__("fldcw %0" : : "m" (*&fpuState
));
239 if((CPUCapFlags
&CPU_CAP_SSE
))
240 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&ctl
->sse_state
));
242 #elif defined(HAVE___CONTROL87_2)
244 __control87_2(ctl
->state
, _MCW_RC
|_MCW_PC
, &mode
, NULL
);
246 if((CPUCapFlags
&CPU_CAP_SSE
))
247 __control87_2(ctl
->sse_state
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
249 #elif defined(HAVE__CONTROLFP)
250 _controlfp(ctl
->state
, _MCW_RC
|_MCW_PC
);
251 #elif defined(HAVE_FESETROUND)
252 fesetround(ctl
->state
);
258 void pthread_once(pthread_once_t
*once
, void (*callback
)(void))
261 while((ret
=InterlockedExchange(once
, 1)) == 1)
265 InterlockedExchange(once
, 2);
269 int pthread_key_create(pthread_key_t
*key
, void (*callback
)(void*))
273 InsertUIntMapEntry(&TlsDestructor
, *key
, callback
);
277 int pthread_key_delete(pthread_key_t key
)
279 InsertUIntMapEntry(&TlsDestructor
, key
, NULL
);
284 void *pthread_getspecific(pthread_key_t key
)
285 { return TlsGetValue(key
); }
287 int pthread_setspecific(pthread_key_t key
, void *val
)
289 TlsSetValue(key
, val
);
294 void *LoadLib(const char *name
)
295 { return LoadLibraryA(name
); }
296 void CloseLib(void *handle
)
297 { FreeLibrary((HANDLE
)handle
); }
298 void *GetSymbol(void *handle
, const char *name
)
302 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
304 ERR("Failed to load %s\n", name
);
308 WCHAR
*strdupW(const WCHAR
*str
)
318 ret
= calloc(sizeof(WCHAR
), len
+1);
320 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
327 #ifdef HAVE_PTHREAD_NP_H
328 #include <pthread_np.h>
332 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
334 pthread_mutexattr_t attrib
;
337 ret
= pthread_mutexattr_init(&attrib
);
340 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
341 #ifdef HAVE_PTHREAD_NP_H
343 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
346 ret
= pthread_mutex_init(cs
, &attrib
);
349 pthread_mutexattr_destroy(&attrib
);
351 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
354 ret
= pthread_mutex_destroy(cs
);
357 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
360 ret
= pthread_mutex_lock(cs
);
363 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
366 ret
= pthread_mutex_unlock(cs
);
370 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
371 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
372 * Additionally, Win32 is supposed to measure the time since Windows started,
373 * as opposed to the actual time. */
374 ALuint
timeGetTime(void)
376 #if _POSIX_TIMERS > 0
380 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
381 #if _POSIX_MONOTONIC_CLOCK == 0
382 static int hasmono
= 0;
383 if(hasmono
> 0 || (hasmono
== 0 &&
384 (hasmono
=sysconf(_SC_MONOTONIC_CLOCK
)) > 0))
386 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
389 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
392 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
397 ret
= gettimeofday(&tv
, NULL
);
400 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
406 struct timespec tv
, rem
;
407 tv
.tv_nsec
= (t
*1000000)%1000000000;
410 while(nanosleep(&tv
, &rem
) == -1 && errno
== EINTR
)
416 void *LoadLib(const char *name
)
422 handle
= dlopen(name
, RTLD_NOW
);
423 if((err
=dlerror()) != NULL
)
427 void CloseLib(void *handle
)
429 void *GetSymbol(void *handle
, const char *name
)
435 sym
= dlsym(handle
, name
);
436 if((err
=dlerror()) != NULL
)
438 WARN("Failed to load %s: %s\n", name
, err
);
448 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
453 i
= snprintf(str
, sizeof(str
), "AL lib: %s %s: ", type
, func
);
454 if(i
> 0 && (unsigned int)i
< sizeof(str
))
458 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
461 str
[sizeof(str
)-1] = 0;
463 fprintf(LogFile
, "%s", str
);
468 void SetRTPriority(void)
470 ALboolean failed
= AL_FALSE
;
474 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
475 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
478 struct sched_param param
;
479 /* Use the minimum real-time priority possible for now (on Linux this
480 * should be 1 for SCHED_RR) */
481 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
482 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
485 /* Real-time priority not available */
486 failed
= (RTPrioLevel
>0);
489 ERR("Failed to set priority level for thread\n");
493 static void Lock(volatile ALenum
*l
)
495 while(ExchangeInt(l
, AL_TRUE
) == AL_TRUE
)
499 static void Unlock(volatile ALenum
*l
)
501 ExchangeInt(l
, AL_FALSE
);
504 void RWLockInit(RWLock
*lock
)
506 lock
->read_count
= 0;
507 lock
->write_count
= 0;
508 lock
->read_lock
= AL_FALSE
;
509 lock
->read_entry_lock
= AL_FALSE
;
510 lock
->write_lock
= AL_FALSE
;
513 void ReadLock(RWLock
*lock
)
515 Lock(&lock
->read_entry_lock
);
516 Lock(&lock
->read_lock
);
517 if(IncrementRef(&lock
->read_count
) == 1)
518 Lock(&lock
->write_lock
);
519 Unlock(&lock
->read_lock
);
520 Unlock(&lock
->read_entry_lock
);
523 void ReadUnlock(RWLock
*lock
)
525 if(DecrementRef(&lock
->read_count
) == 0)
526 Unlock(&lock
->write_lock
);
529 void WriteLock(RWLock
*lock
)
531 if(IncrementRef(&lock
->write_count
) == 1)
532 Lock(&lock
->read_lock
);
533 Lock(&lock
->write_lock
);
536 void WriteUnlock(RWLock
*lock
)
538 Unlock(&lock
->write_lock
);
539 if(DecrementRef(&lock
->write_count
) == 0)
540 Unlock(&lock
->read_lock
);
544 void InitUIntMap(UIntMap
*map
, ALsizei limit
)
550 RWLockInit(&map
->lock
);
553 void ResetUIntMap(UIntMap
*map
)
555 WriteLock(&map
->lock
);
560 WriteUnlock(&map
->lock
);
563 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
567 WriteLock(&map
->lock
);
571 ALsizei high
= map
->size
- 1;
574 ALsizei mid
= low
+ (high
-low
)/2;
575 if(map
->array
[mid
].key
< key
)
580 if(map
->array
[low
].key
< key
)
585 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
587 if(map
->size
== map
->limit
)
589 WriteUnlock(&map
->lock
);
590 return AL_OUT_OF_MEMORY
;
593 if(map
->size
== map
->maxsize
)
598 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
599 if(newsize
>= map
->maxsize
)
600 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
603 WriteUnlock(&map
->lock
);
604 return AL_OUT_OF_MEMORY
;
607 map
->maxsize
= newsize
;
611 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
612 (map
->size
-pos
)*sizeof(map
->array
[0]));
615 map
->array
[pos
].key
= key
;
616 map
->array
[pos
].value
= value
;
617 WriteUnlock(&map
->lock
);
622 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
625 WriteLock(&map
->lock
);
629 ALsizei high
= map
->size
- 1;
632 ALsizei mid
= low
+ (high
-low
)/2;
633 if(map
->array
[mid
].key
< key
)
638 if(map
->array
[low
].key
== key
)
640 ptr
= map
->array
[low
].value
;
641 if(low
< map
->size
-1)
642 memmove(&map
->array
[low
], &map
->array
[low
+1],
643 (map
->size
-1-low
)*sizeof(map
->array
[0]));
647 WriteUnlock(&map
->lock
);
651 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
654 ReadLock(&map
->lock
);
658 ALsizei high
= map
->size
- 1;
661 ALsizei mid
= low
+ (high
-low
)/2;
662 if(map
->array
[mid
].key
< key
)
667 if(map
->array
[low
].key
== key
)
668 ptr
= map
->array
[low
].value
;
670 ReadUnlock(&map
->lock
);