Add an option for wide stereo sources
[openal-soft.git] / Alc / helpers.c
blob009e83d2e7eb6a26110f13197e75bb9efb7d02d2
1 /**
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <time.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #ifdef HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #endif
31 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
32 #define INITGUID
33 #include <windows.h>
34 #ifdef HAVE_GUIDDEF_H
35 #include <guiddef.h>
36 #else
37 #include <initguid.h>
38 #endif
40 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
41 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
43 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
45 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
46 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
47 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
48 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
50 #ifdef HAVE_MMDEVAPI
51 #include <devpropdef.h>
53 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
54 #endif
56 #endif
58 #include "alMain.h"
60 #ifdef _WIN32
61 void pthread_once(pthread_once_t *once, void (*callback)(void))
63 LONG ret;
64 while((ret=InterlockedExchange(once, 1)) == 1)
65 sched_yield();
66 if(ret == 0)
67 callback();
68 InterlockedExchange(once, 2);
72 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
74 *key = TlsAlloc();
75 if(callback)
76 InsertUIntMapEntry(&TlsDestructor, *key, callback);
77 return 0;
80 int pthread_key_delete(pthread_key_t key)
82 InsertUIntMapEntry(&TlsDestructor, key, NULL);
83 TlsFree(key);
84 return 0;
87 void *pthread_getspecific(pthread_key_t key)
88 { return TlsGetValue(key); }
90 int pthread_setspecific(pthread_key_t key, void *val)
92 TlsSetValue(key, val);
93 return 0;
97 void *LoadLib(const char *name)
98 { return LoadLibraryA(name); }
99 void CloseLib(void *handle)
100 { FreeLibrary((HANDLE)handle); }
101 void *GetSymbol(void *handle, const char *name)
103 void *ret;
105 ret = (void*)GetProcAddress((HANDLE)handle, name);
106 if(ret == NULL)
107 ERR("Failed to load %s\n", name);
108 return ret;
111 WCHAR *strdupW(const WCHAR *str)
113 const WCHAR *n;
114 WCHAR *ret;
115 size_t len;
117 n = str;
118 while(*n) n++;
119 len = n - str;
121 ret = calloc(sizeof(WCHAR), len+1);
122 if(ret != NULL)
123 memcpy(ret, str, sizeof(WCHAR)*len);
124 return ret;
127 #else
129 #include <pthread.h>
130 #ifdef HAVE_PTHREAD_NP_H
131 #include <pthread_np.h>
132 #endif
133 #include <sched.h>
135 void InitializeCriticalSection(CRITICAL_SECTION *cs)
137 pthread_mutexattr_t attrib;
138 int ret;
140 ret = pthread_mutexattr_init(&attrib);
141 assert(ret == 0);
143 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
144 #ifdef HAVE_PTHREAD_NP_H
145 if(ret != 0)
146 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
147 #endif
148 assert(ret == 0);
149 ret = pthread_mutex_init(cs, &attrib);
150 assert(ret == 0);
152 pthread_mutexattr_destroy(&attrib);
154 void DeleteCriticalSection(CRITICAL_SECTION *cs)
156 int ret;
157 ret = pthread_mutex_destroy(cs);
158 assert(ret == 0);
160 void EnterCriticalSection(CRITICAL_SECTION *cs)
162 int ret;
163 ret = pthread_mutex_lock(cs);
164 assert(ret == 0);
166 void LeaveCriticalSection(CRITICAL_SECTION *cs)
168 int ret;
169 ret = pthread_mutex_unlock(cs);
170 assert(ret == 0);
173 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
174 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
175 * Additionally, Win32 is supposed to measure the time since Windows started,
176 * as opposed to the actual time. */
177 ALuint timeGetTime(void)
179 #if _POSIX_TIMERS > 0
180 struct timespec ts;
181 int ret = -1;
183 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
184 #if _POSIX_MONOTONIC_CLOCK == 0
185 static int hasmono = 0;
186 if(hasmono > 0 || (hasmono == 0 &&
187 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
188 #endif
189 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
190 #endif
191 if(ret != 0)
192 ret = clock_gettime(CLOCK_REALTIME, &ts);
193 assert(ret == 0);
195 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
196 #else
197 struct timeval tv;
198 int ret;
200 ret = gettimeofday(&tv, NULL);
201 assert(ret == 0);
203 return tv.tv_usec/1000 + tv.tv_sec*1000;
204 #endif
207 void Sleep(ALuint t)
209 struct timespec tv, rem;
210 tv.tv_nsec = (t*1000000)%1000000000;
211 tv.tv_sec = t/1000;
213 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
214 tv = rem;
217 #ifdef HAVE_DLFCN_H
219 void *LoadLib(const char *name)
221 const char *err;
222 void *handle;
224 dlerror();
225 handle = dlopen(name, RTLD_NOW);
226 if((err=dlerror()) != NULL)
227 handle = NULL;
228 return handle;
230 void CloseLib(void *handle)
231 { dlclose(handle); }
232 void *GetSymbol(void *handle, const char *name)
234 const char *err;
235 void *sym;
237 dlerror();
238 sym = dlsym(handle, name);
239 if((err=dlerror()) != NULL)
241 WARN("Failed to load %s: %s\n", name, err);
242 sym = NULL;
244 return sym;
247 #endif
248 #endif
251 void al_print(const char *func, const char *fmt, ...)
253 char str[256];
254 int i;
256 i = snprintf(str, sizeof(str), "AL lib: %s: ", func);
257 if(i < (int)sizeof(str) && i > 0)
259 va_list ap;
260 va_start(ap, fmt);
261 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
262 va_end(ap);
264 str[sizeof(str)-1] = 0;
266 fprintf(LogFile, "%s", str);
267 fflush(LogFile);
271 void SetRTPriority(void)
273 ALboolean failed = AL_FALSE;
275 #ifdef _WIN32
276 if(RTPrioLevel > 0)
277 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
278 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
279 if(RTPrioLevel > 0)
281 struct sched_param param;
282 /* Use the minimum real-time priority possible for now (on Linux this
283 * should be 1 for SCHED_RR) */
284 param.sched_priority = sched_get_priority_min(SCHED_RR);
285 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
287 #else
288 /* Real-time priority not available */
289 failed = (RTPrioLevel>0);
290 #endif
291 if(failed)
292 ERR("Failed to set priority level for thread\n");
296 static void Lock(volatile ALenum *l)
298 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
299 sched_yield();
302 static void Unlock(volatile ALenum *l)
304 ExchangeInt(l, AL_FALSE);
307 void RWLockInit(RWLock *lock)
309 lock->read_count = 0;
310 lock->write_count = 0;
311 lock->read_lock = AL_FALSE;
312 lock->read_entry_lock = AL_FALSE;
313 lock->write_lock = AL_FALSE;
316 void ReadLock(RWLock *lock)
318 Lock(&lock->read_entry_lock);
319 Lock(&lock->read_lock);
320 if(IncrementRef(&lock->read_count) == 1)
321 Lock(&lock->write_lock);
322 Unlock(&lock->read_lock);
323 Unlock(&lock->read_entry_lock);
326 void ReadUnlock(RWLock *lock)
328 if(DecrementRef(&lock->read_count) == 0)
329 Unlock(&lock->write_lock);
332 void WriteLock(RWLock *lock)
334 if(IncrementRef(&lock->write_count) == 1)
335 Lock(&lock->read_lock);
336 Lock(&lock->write_lock);
339 void WriteUnlock(RWLock *lock)
341 Unlock(&lock->write_lock);
342 if(DecrementRef(&lock->write_count) == 0)
343 Unlock(&lock->read_lock);
347 void InitUIntMap(UIntMap *map, ALsizei limit)
349 map->array = NULL;
350 map->size = 0;
351 map->maxsize = 0;
352 map->limit = limit;
353 RWLockInit(&map->lock);
356 void ResetUIntMap(UIntMap *map)
358 WriteLock(&map->lock);
359 free(map->array);
360 map->array = NULL;
361 map->size = 0;
362 map->maxsize = 0;
363 WriteUnlock(&map->lock);
366 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
368 ALsizei pos = 0;
370 WriteLock(&map->lock);
371 if(map->size > 0)
373 ALsizei low = 0;
374 ALsizei high = map->size - 1;
375 while(low < high)
377 ALsizei mid = low + (high-low)/2;
378 if(map->array[mid].key < key)
379 low = mid + 1;
380 else
381 high = mid;
383 if(map->array[low].key < key)
384 low++;
385 pos = low;
388 if(pos == map->size || map->array[pos].key != key)
390 if(map->size == map->limit)
392 WriteUnlock(&map->lock);
393 return AL_OUT_OF_MEMORY;
396 if(map->size == map->maxsize)
398 ALvoid *temp = NULL;
399 ALsizei newsize;
401 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
402 if(newsize >= map->maxsize)
403 temp = realloc(map->array, newsize*sizeof(map->array[0]));
404 if(!temp)
406 WriteUnlock(&map->lock);
407 return AL_OUT_OF_MEMORY;
409 map->array = temp;
410 map->maxsize = newsize;
413 if(pos < map->size)
414 memmove(&map->array[pos+1], &map->array[pos],
415 (map->size-pos)*sizeof(map->array[0]));
416 map->size++;
418 map->array[pos].key = key;
419 map->array[pos].value = value;
420 WriteUnlock(&map->lock);
422 return AL_NO_ERROR;
425 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
427 ALvoid *ptr = NULL;
428 WriteLock(&map->lock);
429 if(map->size > 0)
431 ALsizei low = 0;
432 ALsizei high = map->size - 1;
433 while(low < high)
435 ALsizei mid = low + (high-low)/2;
436 if(map->array[mid].key < key)
437 low = mid + 1;
438 else
439 high = mid;
441 if(map->array[low].key == key)
443 ptr = map->array[low].value;
444 if(low < map->size-1)
445 memmove(&map->array[low], &map->array[low+1],
446 (map->size-1-low)*sizeof(map->array[0]));
447 map->size--;
450 WriteUnlock(&map->lock);
451 return ptr;
454 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
456 ALvoid *ptr = NULL;
457 ReadLock(&map->lock);
458 if(map->size > 0)
460 ALsizei low = 0;
461 ALsizei high = map->size - 1;
462 while(low < high)
464 ALsizei mid = low + (high-low)/2;
465 if(map->array[mid].key < key)
466 low = mid + 1;
467 else
468 high = mid;
470 if(map->array[low].key == key)
471 ptr = map->array[low].value;
473 ReadUnlock(&map->lock);
474 return ptr;