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
34 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
43 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
44 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
46 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
48 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
49 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
50 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
51 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
54 #include <devpropdef.h>
56 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
63 ALuint CPUCapFlags
= 0;
66 void FillCPUCaps(ALuint capfilter
)
70 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
71 /* FIXME: We really should get this for all available CPUs in case different
72 * CPUs have different caps (is that possible on one machine?). */
76 char str
[sizeof(unsigned int[4])];
79 if(!__get_cpuid(0, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
80 ERR("Failed to get CPUID\n");
83 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
84 if(__get_cpuid(0x80000002, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]) &&
85 __get_cpuid(0x80000003, &cpuinf
[1].regs
[0], &cpuinf
[1].regs
[1], &cpuinf
[1].regs
[2], &cpuinf
[1].regs
[3]) &&
86 __get_cpuid(0x80000004, &cpuinf
[2].regs
[0], &cpuinf
[2].regs
[1], &cpuinf
[2].regs
[2], &cpuinf
[2].regs
[3]))
87 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
89 if(!__get_cpuid(1, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
90 ERR("Failed to get CPU features\n");
94 if((cpuinf
[0].regs
[3]&bit_MMX
))
98 if((cpuinf
[0].regs
[3]&bit_SSE
))
105 #ifdef HAVE_ARM_NEON_H
106 /* Assume Neon support if compiled with it */
107 caps
|= CPU_CAP_NEON
;
110 TRACE("Got caps:%s%s%s%s\n", ((caps
&CPU_CAP_MMX
)?((capfilter
&CPU_CAP_MMX
)?" MMX":" (MMX)"):""),
111 ((caps
&CPU_CAP_SSE
)?((capfilter
&CPU_CAP_SSE
)?" SSE":" (SSE)"):""),
112 ((caps
&CPU_CAP_NEON
)?((capfilter
&CPU_CAP_NEON
)?" Neon":" (Neon)"):""),
113 ((!caps
)?" (none)":""));
114 CPUCapFlags
= caps
& capfilter
;
119 void pthread_once(pthread_once_t
*once
, void (*callback
)(void))
122 while((ret
=InterlockedExchange(once
, 1)) == 1)
126 InterlockedExchange(once
, 2);
130 int pthread_key_create(pthread_key_t
*key
, void (*callback
)(void*))
134 InsertUIntMapEntry(&TlsDestructor
, *key
, callback
);
138 int pthread_key_delete(pthread_key_t key
)
140 InsertUIntMapEntry(&TlsDestructor
, key
, NULL
);
145 void *pthread_getspecific(pthread_key_t key
)
146 { return TlsGetValue(key
); }
148 int pthread_setspecific(pthread_key_t key
, void *val
)
150 TlsSetValue(key
, val
);
155 void *LoadLib(const char *name
)
156 { return LoadLibraryA(name
); }
157 void CloseLib(void *handle
)
158 { FreeLibrary((HANDLE
)handle
); }
159 void *GetSymbol(void *handle
, const char *name
)
163 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
165 ERR("Failed to load %s\n", name
);
169 WCHAR
*strdupW(const WCHAR
*str
)
179 ret
= calloc(sizeof(WCHAR
), len
+1);
181 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
188 #ifdef HAVE_PTHREAD_NP_H
189 #include <pthread_np.h>
193 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
195 pthread_mutexattr_t attrib
;
198 ret
= pthread_mutexattr_init(&attrib
);
201 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
202 #ifdef HAVE_PTHREAD_NP_H
204 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
207 ret
= pthread_mutex_init(cs
, &attrib
);
210 pthread_mutexattr_destroy(&attrib
);
212 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
215 ret
= pthread_mutex_destroy(cs
);
218 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
221 ret
= pthread_mutex_lock(cs
);
224 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
227 ret
= pthread_mutex_unlock(cs
);
231 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
232 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
233 * Additionally, Win32 is supposed to measure the time since Windows started,
234 * as opposed to the actual time. */
235 ALuint
timeGetTime(void)
237 #if _POSIX_TIMERS > 0
241 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
242 #if _POSIX_MONOTONIC_CLOCK == 0
243 static int hasmono
= 0;
244 if(hasmono
> 0 || (hasmono
== 0 &&
245 (hasmono
=sysconf(_SC_MONOTONIC_CLOCK
)) > 0))
247 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
250 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
253 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
258 ret
= gettimeofday(&tv
, NULL
);
261 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
267 struct timespec tv
, rem
;
268 tv
.tv_nsec
= (t
*1000000)%1000000000;
271 while(nanosleep(&tv
, &rem
) == -1 && errno
== EINTR
)
277 void *LoadLib(const char *name
)
283 handle
= dlopen(name
, RTLD_NOW
);
284 if((err
=dlerror()) != NULL
)
288 void CloseLib(void *handle
)
290 void *GetSymbol(void *handle
, const char *name
)
296 sym
= dlsym(handle
, name
);
297 if((err
=dlerror()) != NULL
)
299 WARN("Failed to load %s: %s\n", name
, err
);
309 void al_print(const char *func
, const char *fmt
, ...)
314 i
= snprintf(str
, sizeof(str
), "AL lib: %s: ", func
);
315 if(i
< (int)sizeof(str
) && i
> 0)
319 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
322 str
[sizeof(str
)-1] = 0;
324 fprintf(LogFile
, "%s", str
);
329 void SetRTPriority(void)
331 ALboolean failed
= AL_FALSE
;
335 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
336 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
339 struct sched_param param
;
340 /* Use the minimum real-time priority possible for now (on Linux this
341 * should be 1 for SCHED_RR) */
342 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
343 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
346 /* Real-time priority not available */
347 failed
= (RTPrioLevel
>0);
350 ERR("Failed to set priority level for thread\n");
354 static void Lock(volatile ALenum
*l
)
356 while(ExchangeInt(l
, AL_TRUE
) == AL_TRUE
)
360 static void Unlock(volatile ALenum
*l
)
362 ExchangeInt(l
, AL_FALSE
);
365 void RWLockInit(RWLock
*lock
)
367 lock
->read_count
= 0;
368 lock
->write_count
= 0;
369 lock
->read_lock
= AL_FALSE
;
370 lock
->read_entry_lock
= AL_FALSE
;
371 lock
->write_lock
= AL_FALSE
;
374 void ReadLock(RWLock
*lock
)
376 Lock(&lock
->read_entry_lock
);
377 Lock(&lock
->read_lock
);
378 if(IncrementRef(&lock
->read_count
) == 1)
379 Lock(&lock
->write_lock
);
380 Unlock(&lock
->read_lock
);
381 Unlock(&lock
->read_entry_lock
);
384 void ReadUnlock(RWLock
*lock
)
386 if(DecrementRef(&lock
->read_count
) == 0)
387 Unlock(&lock
->write_lock
);
390 void WriteLock(RWLock
*lock
)
392 if(IncrementRef(&lock
->write_count
) == 1)
393 Lock(&lock
->read_lock
);
394 Lock(&lock
->write_lock
);
397 void WriteUnlock(RWLock
*lock
)
399 Unlock(&lock
->write_lock
);
400 if(DecrementRef(&lock
->write_count
) == 0)
401 Unlock(&lock
->read_lock
);
405 void InitUIntMap(UIntMap
*map
, ALsizei limit
)
411 RWLockInit(&map
->lock
);
414 void ResetUIntMap(UIntMap
*map
)
416 WriteLock(&map
->lock
);
421 WriteUnlock(&map
->lock
);
424 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
428 WriteLock(&map
->lock
);
432 ALsizei high
= map
->size
- 1;
435 ALsizei mid
= low
+ (high
-low
)/2;
436 if(map
->array
[mid
].key
< key
)
441 if(map
->array
[low
].key
< key
)
446 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
448 if(map
->size
== map
->limit
)
450 WriteUnlock(&map
->lock
);
451 return AL_OUT_OF_MEMORY
;
454 if(map
->size
== map
->maxsize
)
459 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
460 if(newsize
>= map
->maxsize
)
461 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
464 WriteUnlock(&map
->lock
);
465 return AL_OUT_OF_MEMORY
;
468 map
->maxsize
= newsize
;
472 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
473 (map
->size
-pos
)*sizeof(map
->array
[0]));
476 map
->array
[pos
].key
= key
;
477 map
->array
[pos
].value
= value
;
478 WriteUnlock(&map
->lock
);
483 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
486 WriteLock(&map
->lock
);
490 ALsizei high
= map
->size
- 1;
493 ALsizei mid
= low
+ (high
-low
)/2;
494 if(map
->array
[mid
].key
< key
)
499 if(map
->array
[low
].key
== key
)
501 ptr
= map
->array
[low
].value
;
502 if(low
< map
->size
-1)
503 memmove(&map
->array
[low
], &map
->array
[low
+1],
504 (map
->size
-1-low
)*sizeof(map
->array
[0]));
508 WriteUnlock(&map
->lock
);
512 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
515 ReadLock(&map
->lock
);
519 ALsizei high
= map
->size
- 1;
522 ALsizei mid
= low
+ (high
-low
)/2;
523 if(map
->array
[mid
].key
< key
)
528 if(map
->array
[low
].key
== key
)
529 ptr
= map
->array
[low
].value
;
531 ReadUnlock(&map
->lock
);