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
28 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
37 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
38 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
40 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
42 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
43 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
44 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
45 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
48 #include <devpropdef.h>
50 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
58 void pthread_once(pthread_once_t
*once
, void (*callback
)(void))
61 while((ret
=InterlockedExchange(once
, 1)) == 1)
65 InterlockedExchange(once
, 2);
69 int pthread_key_create(pthread_key_t
*key
, void (*callback
)(void*))
73 InsertUIntMapEntry(&TlsDestructor
, *key
, callback
);
77 int pthread_key_delete(pthread_key_t key
)
79 InsertUIntMapEntry(&TlsDestructor
, key
, NULL
);
84 void *pthread_getspecific(pthread_key_t key
)
85 { return TlsGetValue(key
); }
87 int pthread_setspecific(pthread_key_t key
, void *val
)
89 TlsSetValue(key
, val
);
94 void *LoadLib(const char *name
)
95 { return LoadLibraryA(name
); }
96 void CloseLib(void *handle
)
97 { FreeLibrary((HANDLE
)handle
); }
98 void *GetSymbol(void *handle
, const char *name
)
102 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
104 ERR("Failed to load %s\n", name
);
108 WCHAR
*strdupW(const WCHAR
*str
)
118 ret
= calloc(sizeof(WCHAR
), len
+1);
120 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
126 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
128 pthread_mutexattr_t attrib
;
131 ret
= pthread_mutexattr_init(&attrib
);
134 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
135 #ifdef HAVE_PTHREAD_NP_H
137 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
140 ret
= pthread_mutex_init(cs
, &attrib
);
143 pthread_mutexattr_destroy(&attrib
);
145 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
148 ret
= pthread_mutex_destroy(cs
);
151 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
154 ret
= pthread_mutex_lock(cs
);
157 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
160 ret
= pthread_mutex_unlock(cs
);
164 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
165 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
166 * Additionally, Win32 is supposed to measure the time since Windows started,
167 * as opposed to the actual time. */
168 ALuint
timeGetTime(void)
170 #if _POSIX_TIMERS > 0
174 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
175 #if _POSIX_MONOTONIC_CLOCK == 0
176 static int hasmono
= 0;
177 if(hasmono
> 0 || (hasmono
== 0 &&
178 (hasmono
=sysconf(_SC_MONOTONIC_CLOCK
)) > 0))
180 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
183 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
186 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
191 ret
= gettimeofday(&tv
, NULL
);
194 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
200 struct timespec tv
, rem
;
201 tv
.tv_nsec
= (t
*1000000)%1000000000;
204 while(nanosleep(&tv
, &rem
) == -1 && errno
== EINTR
)
210 void *LoadLib(const char *name
)
216 handle
= dlopen(name
, RTLD_NOW
);
217 if((err
=dlerror()) != NULL
)
221 void CloseLib(void *handle
)
223 void *GetSymbol(void *handle
, const char *name
)
229 sym
= dlsym(handle
, name
);
230 if((err
=dlerror()) != NULL
)
232 WARN("Failed to load %s: %s\n", name
, err
);
242 void al_print(const char *func
, const char *fmt
, ...)
247 i
= snprintf(str
, sizeof(str
), "AL lib: %s: ", func
);
248 if(i
< (int)sizeof(str
) && i
> 0)
252 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
255 str
[sizeof(str
)-1] = 0;
257 fprintf(LogFile
, "%s", str
);
262 void SetRTPriority(void)
264 ALboolean failed
= AL_FALSE
;
268 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
269 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
272 struct sched_param param
;
273 /* Use the minimum real-time priority possible for now (on Linux this
274 * should be 1 for SCHED_RR) */
275 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
276 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
279 /* Real-time priority not available */
280 failed
= (RTPrioLevel
>0);
283 ERR("Failed to set priority level for thread\n");
287 static void Lock(volatile ALenum
*l
)
289 while(ExchangeInt(l
, AL_TRUE
) == AL_TRUE
)
293 static void Unlock(volatile ALenum
*l
)
295 ExchangeInt(l
, AL_FALSE
);
298 void RWLockInit(RWLock
*lock
)
300 lock
->read_count
= 0;
301 lock
->write_count
= 0;
302 lock
->read_lock
= AL_FALSE
;
303 lock
->read_entry_lock
= AL_FALSE
;
304 lock
->write_lock
= AL_FALSE
;
307 void ReadLock(RWLock
*lock
)
309 Lock(&lock
->read_entry_lock
);
310 Lock(&lock
->read_lock
);
311 if(IncrementRef(&lock
->read_count
) == 1)
312 Lock(&lock
->write_lock
);
313 Unlock(&lock
->read_lock
);
314 Unlock(&lock
->read_entry_lock
);
317 void ReadUnlock(RWLock
*lock
)
319 if(DecrementRef(&lock
->read_count
) == 0)
320 Unlock(&lock
->write_lock
);
323 void WriteLock(RWLock
*lock
)
325 if(IncrementRef(&lock
->write_count
) == 1)
326 Lock(&lock
->read_lock
);
327 Lock(&lock
->write_lock
);
330 void WriteUnlock(RWLock
*lock
)
332 Unlock(&lock
->write_lock
);
333 if(DecrementRef(&lock
->write_count
) == 0)
334 Unlock(&lock
->read_lock
);
338 void InitUIntMap(UIntMap
*map
, ALsizei limit
)
344 RWLockInit(&map
->lock
);
347 void ResetUIntMap(UIntMap
*map
)
349 WriteLock(&map
->lock
);
354 WriteUnlock(&map
->lock
);
357 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
361 WriteLock(&map
->lock
);
365 ALsizei high
= map
->size
- 1;
368 ALsizei mid
= low
+ (high
-low
)/2;
369 if(map
->array
[mid
].key
< key
)
374 if(map
->array
[low
].key
< key
)
379 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
381 if(map
->size
== map
->limit
)
383 WriteUnlock(&map
->lock
);
384 return AL_OUT_OF_MEMORY
;
387 if(map
->size
== map
->maxsize
)
392 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
393 if(newsize
>= map
->maxsize
)
394 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
397 WriteUnlock(&map
->lock
);
398 return AL_OUT_OF_MEMORY
;
401 map
->maxsize
= newsize
;
405 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
406 (map
->size
-pos
)*sizeof(map
->array
[0]));
409 map
->array
[pos
].key
= key
;
410 map
->array
[pos
].value
= value
;
411 WriteUnlock(&map
->lock
);
416 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
419 WriteLock(&map
->lock
);
423 ALsizei high
= map
->size
- 1;
426 ALsizei mid
= low
+ (high
-low
)/2;
427 if(map
->array
[mid
].key
< key
)
432 if(map
->array
[low
].key
== key
)
434 ptr
= map
->array
[low
].value
;
435 if(low
< map
->size
-1)
436 memmove(&map
->array
[low
], &map
->array
[low
+1],
437 (map
->size
-1-low
)*sizeof(map
->array
[0]));
441 WriteUnlock(&map
->lock
);
445 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
448 ReadLock(&map
->lock
);
452 ALsizei high
= map
->size
- 1;
455 ALsizei mid
= low
+ (high
-low
)/2;
456 if(map
->array
[mid
].key
< key
)
461 if(map
->array
[low
].key
== key
)
462 ptr
= map
->array
[low
].value
;
464 ReadUnlock(&map
->lock
);