rlm@0
|
1 /**
|
rlm@0
|
2 * OpenAL cross platform audio library
|
rlm@0
|
3 * Copyright (C) 2011 by authors.
|
rlm@0
|
4 * This library is free software; you can redistribute it and/or
|
rlm@0
|
5 * modify it under the terms of the GNU Library General Public
|
rlm@0
|
6 * License as published by the Free Software Foundation; either
|
rlm@0
|
7 * version 2 of the License, or (at your option) any later version.
|
rlm@0
|
8 *
|
rlm@0
|
9 * This library is distributed in the hope that it will be useful,
|
rlm@0
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
rlm@0
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
rlm@0
|
12 * Library General Public License for more details.
|
rlm@0
|
13 *
|
rlm@0
|
14 * You should have received a copy of the GNU Library General Public
|
rlm@0
|
15 * License along with this library; if not, write to the
|
rlm@0
|
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
rlm@0
|
17 * Boston, MA 02111-1307, USA.
|
rlm@0
|
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
|
rlm@0
|
19 */
|
rlm@0
|
20
|
rlm@0
|
21 #include "config.h"
|
rlm@0
|
22
|
rlm@0
|
23 #include <stdlib.h>
|
rlm@0
|
24 #ifdef HAVE_DLFCN_H
|
rlm@0
|
25 #include <dlfcn.h>
|
rlm@0
|
26 #endif
|
rlm@0
|
27
|
rlm@0
|
28 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
|
rlm@0
|
29 #define INITGUID
|
rlm@0
|
30 #include <windows.h>
|
rlm@0
|
31 #ifdef HAVE_GUIDDEF_H
|
rlm@0
|
32 #include <guiddef.h>
|
rlm@0
|
33 #else
|
rlm@0
|
34 #include <initguid.h>
|
rlm@0
|
35 #endif
|
rlm@0
|
36
|
rlm@0
|
37 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
|
rlm@0
|
38 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
|
rlm@0
|
39
|
rlm@0
|
40 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
|
rlm@0
|
41
|
rlm@0
|
42 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
|
rlm@0
|
43 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
|
rlm@0
|
44 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
|
rlm@0
|
45 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
|
rlm@0
|
46
|
rlm@0
|
47 #endif
|
rlm@0
|
48
|
rlm@0
|
49 #include "alMain.h"
|
rlm@0
|
50
|
rlm@0
|
51 #ifdef _WIN32
|
rlm@0
|
52 void pthread_once(pthread_once_t *once, void (*callback)(void))
|
rlm@0
|
53 {
|
rlm@0
|
54 LONG ret;
|
rlm@0
|
55 while((ret=InterlockedExchange(once, 1)) == 1)
|
rlm@0
|
56 Sleep(0);
|
rlm@0
|
57 if(ret == 0)
|
rlm@0
|
58 callback();
|
rlm@0
|
59 InterlockedExchange(once, 2);
|
rlm@0
|
60 }
|
rlm@0
|
61
|
rlm@0
|
62 void *LoadLib(const char *name)
|
rlm@0
|
63 { return LoadLibraryA(name); }
|
rlm@0
|
64 void CloseLib(void *handle)
|
rlm@0
|
65 { FreeLibrary((HANDLE)handle); }
|
rlm@0
|
66 void *GetSymbol(void *handle, const char *name)
|
rlm@0
|
67 {
|
rlm@0
|
68 void *ret;
|
rlm@0
|
69
|
rlm@0
|
70 ret = (void*)GetProcAddress((HANDLE)handle, name);
|
rlm@0
|
71 if(ret == NULL)
|
rlm@0
|
72 ERR("Failed to load %s\n", name);
|
rlm@0
|
73 return ret;
|
rlm@0
|
74 }
|
rlm@0
|
75
|
rlm@0
|
76 #else
|
rlm@0
|
77
|
rlm@0
|
78 void InitializeCriticalSection(CRITICAL_SECTION *cs)
|
rlm@0
|
79 {
|
rlm@0
|
80 pthread_mutexattr_t attrib;
|
rlm@0
|
81 int ret;
|
rlm@0
|
82
|
rlm@0
|
83 ret = pthread_mutexattr_init(&attrib);
|
rlm@0
|
84 assert(ret == 0);
|
rlm@0
|
85
|
rlm@0
|
86 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
|
rlm@0
|
87 #ifdef HAVE_PTHREAD_NP_H
|
rlm@0
|
88 if(ret != 0)
|
rlm@0
|
89 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
|
rlm@0
|
90 #endif
|
rlm@0
|
91 assert(ret == 0);
|
rlm@0
|
92 ret = pthread_mutex_init(cs, &attrib);
|
rlm@0
|
93 assert(ret == 0);
|
rlm@0
|
94
|
rlm@0
|
95 pthread_mutexattr_destroy(&attrib);
|
rlm@0
|
96 }
|
rlm@0
|
97 void DeleteCriticalSection(CRITICAL_SECTION *cs)
|
rlm@0
|
98 {
|
rlm@0
|
99 int ret;
|
rlm@0
|
100 ret = pthread_mutex_destroy(cs);
|
rlm@0
|
101 assert(ret == 0);
|
rlm@0
|
102 }
|
rlm@0
|
103 void EnterCriticalSection(CRITICAL_SECTION *cs)
|
rlm@0
|
104 {
|
rlm@0
|
105 int ret;
|
rlm@0
|
106 ret = pthread_mutex_lock(cs);
|
rlm@0
|
107 assert(ret == 0);
|
rlm@0
|
108 }
|
rlm@0
|
109 void LeaveCriticalSection(CRITICAL_SECTION *cs)
|
rlm@0
|
110 {
|
rlm@0
|
111 int ret;
|
rlm@0
|
112 ret = pthread_mutex_unlock(cs);
|
rlm@0
|
113 assert(ret == 0);
|
rlm@0
|
114 }
|
rlm@0
|
115
|
rlm@0
|
116 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
|
rlm@0
|
117 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
|
rlm@0
|
118 * Additionally, Win32 is supposed to measure the time since Windows started,
|
rlm@0
|
119 * as opposed to the actual time. */
|
rlm@0
|
120 ALuint timeGetTime(void)
|
rlm@0
|
121 {
|
rlm@0
|
122 #if _POSIX_TIMERS > 0
|
rlm@0
|
123 struct timespec ts;
|
rlm@0
|
124 int ret = -1;
|
rlm@0
|
125
|
rlm@0
|
126 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
|
rlm@0
|
127 #if _POSIX_MONOTONIC_CLOCK == 0
|
rlm@0
|
128 static int hasmono = 0;
|
rlm@0
|
129 if(hasmono > 0 || (hasmono == 0 &&
|
rlm@0
|
130 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
|
rlm@0
|
131 #endif
|
rlm@0
|
132 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
rlm@0
|
133 #endif
|
rlm@0
|
134 if(ret != 0)
|
rlm@0
|
135 ret = clock_gettime(CLOCK_REALTIME, &ts);
|
rlm@0
|
136 assert(ret == 0);
|
rlm@0
|
137
|
rlm@0
|
138 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
|
rlm@0
|
139 #else
|
rlm@0
|
140 struct timeval tv;
|
rlm@0
|
141 int ret;
|
rlm@0
|
142
|
rlm@0
|
143 ret = gettimeofday(&tv, NULL);
|
rlm@0
|
144 assert(ret == 0);
|
rlm@0
|
145
|
rlm@0
|
146 return tv.tv_usec/1000 + tv.tv_sec*1000;
|
rlm@0
|
147 #endif
|
rlm@0
|
148 }
|
rlm@0
|
149
|
rlm@0
|
150 #ifdef HAVE_DLFCN_H
|
rlm@0
|
151
|
rlm@0
|
152 void *LoadLib(const char *name)
|
rlm@0
|
153 {
|
rlm@0
|
154 const char *err;
|
rlm@0
|
155 void *handle;
|
rlm@0
|
156
|
rlm@0
|
157 dlerror();
|
rlm@0
|
158 handle = dlopen(name, RTLD_NOW);
|
rlm@0
|
159 if((err=dlerror()) != NULL)
|
rlm@0
|
160 handle = NULL;
|
rlm@0
|
161 return handle;
|
rlm@0
|
162 }
|
rlm@0
|
163 void CloseLib(void *handle)
|
rlm@0
|
164 { dlclose(handle); }
|
rlm@0
|
165 void *GetSymbol(void *handle, const char *name)
|
rlm@0
|
166 {
|
rlm@0
|
167 const char *err;
|
rlm@0
|
168 void *sym;
|
rlm@0
|
169
|
rlm@0
|
170 dlerror();
|
rlm@0
|
171 sym = dlsym(handle, name);
|
rlm@0
|
172 if((err=dlerror()) != NULL)
|
rlm@0
|
173 {
|
rlm@0
|
174 ERR("Failed to load %s: %s\n", name, err);
|
rlm@0
|
175 sym = NULL;
|
rlm@0
|
176 }
|
rlm@0
|
177 return sym;
|
rlm@0
|
178 }
|
rlm@0
|
179
|
rlm@0
|
180 #endif
|
rlm@0
|
181 #endif
|
rlm@0
|
182
|
rlm@0
|
183
|
rlm@0
|
184 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
|
rlm@0
|
185 {
|
rlm@0
|
186 const char *fn;
|
rlm@0
|
187 char str[256];
|
rlm@0
|
188 int i;
|
rlm@0
|
189
|
rlm@0
|
190 fn = strrchr(fname, '/');
|
rlm@0
|
191 if(!fn) fn = strrchr(fname, '\\');
|
rlm@0
|
192 if(!fn) fn = fname;
|
rlm@0
|
193 else fn += 1;
|
rlm@0
|
194
|
rlm@0
|
195 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
|
rlm@0
|
196 if(i < (int)sizeof(str) && i > 0)
|
rlm@0
|
197 {
|
rlm@0
|
198 va_list ap;
|
rlm@0
|
199 va_start(ap, fmt);
|
rlm@0
|
200 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
|
rlm@0
|
201 va_end(ap);
|
rlm@0
|
202 }
|
rlm@0
|
203 str[sizeof(str)-1] = 0;
|
rlm@0
|
204
|
rlm@0
|
205 fprintf(LogFile, "%s", str);
|
rlm@0
|
206 fflush(LogFile);
|
rlm@0
|
207 }
|
rlm@0
|
208
|
rlm@0
|
209
|
rlm@0
|
210 void SetRTPriority(void)
|
rlm@0
|
211 {
|
rlm@0
|
212 ALboolean failed;
|
rlm@0
|
213
|
rlm@0
|
214 #ifdef _WIN32
|
rlm@0
|
215 if(RTPrioLevel > 0)
|
rlm@0
|
216 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
rlm@0
|
217 else
|
rlm@0
|
218 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
|
rlm@0
|
219 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
|
rlm@0
|
220 struct sched_param param;
|
rlm@0
|
221
|
rlm@0
|
222 if(RTPrioLevel > 0)
|
rlm@0
|
223 {
|
rlm@0
|
224 /* Use the minimum real-time priority possible for now (on Linux this
|
rlm@0
|
225 * should be 1 for SCHED_RR) */
|
rlm@0
|
226 param.sched_priority = sched_get_priority_min(SCHED_RR);
|
rlm@0
|
227 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
|
rlm@0
|
228 }
|
rlm@0
|
229 else
|
rlm@0
|
230 {
|
rlm@0
|
231 param.sched_priority = 0;
|
rlm@0
|
232 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, ¶m);
|
rlm@0
|
233 }
|
rlm@0
|
234 #else
|
rlm@0
|
235 /* Real-time priority not available */
|
rlm@0
|
236 failed = (RTPrioLevel>0);
|
rlm@0
|
237 #endif
|
rlm@0
|
238 if(failed)
|
rlm@0
|
239 ERR("Failed to set priority level for thread\n");
|
rlm@0
|
240 }
|
rlm@0
|
241
|
rlm@0
|
242
|
rlm@0
|
243 void InitUIntMap(UIntMap *map)
|
rlm@0
|
244 {
|
rlm@0
|
245 map->array = NULL;
|
rlm@0
|
246 map->size = 0;
|
rlm@0
|
247 map->maxsize = 0;
|
rlm@0
|
248 }
|
rlm@0
|
249
|
rlm@0
|
250 void ResetUIntMap(UIntMap *map)
|
rlm@0
|
251 {
|
rlm@0
|
252 free(map->array);
|
rlm@0
|
253 map->array = NULL;
|
rlm@0
|
254 map->size = 0;
|
rlm@0
|
255 map->maxsize = 0;
|
rlm@0
|
256 }
|
rlm@0
|
257
|
rlm@0
|
258 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
|
rlm@0
|
259 {
|
rlm@0
|
260 ALsizei pos = 0;
|
rlm@0
|
261
|
rlm@0
|
262 if(map->size > 0)
|
rlm@0
|
263 {
|
rlm@0
|
264 ALsizei low = 0;
|
rlm@0
|
265 ALsizei high = map->size - 1;
|
rlm@0
|
266 while(low < high)
|
rlm@0
|
267 {
|
rlm@0
|
268 ALsizei mid = low + (high-low)/2;
|
rlm@0
|
269 if(map->array[mid].key < key)
|
rlm@0
|
270 low = mid + 1;
|
rlm@0
|
271 else
|
rlm@0
|
272 high = mid;
|
rlm@0
|
273 }
|
rlm@0
|
274 if(map->array[low].key < key)
|
rlm@0
|
275 low++;
|
rlm@0
|
276 pos = low;
|
rlm@0
|
277 }
|
rlm@0
|
278
|
rlm@0
|
279 if(pos == map->size || map->array[pos].key != key)
|
rlm@0
|
280 {
|
rlm@0
|
281 if(map->size == map->maxsize)
|
rlm@0
|
282 {
|
rlm@0
|
283 ALvoid *temp;
|
rlm@0
|
284 ALsizei newsize;
|
rlm@0
|
285
|
rlm@0
|
286 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
|
rlm@0
|
287 if(newsize < map->maxsize)
|
rlm@0
|
288 return AL_OUT_OF_MEMORY;
|
rlm@0
|
289
|
rlm@0
|
290 temp = realloc(map->array, newsize*sizeof(map->array[0]));
|
rlm@0
|
291 if(!temp) return AL_OUT_OF_MEMORY;
|
rlm@0
|
292 map->array = temp;
|
rlm@0
|
293 map->maxsize = newsize;
|
rlm@0
|
294 }
|
rlm@0
|
295
|
rlm@0
|
296 map->size++;
|
rlm@0
|
297 if(pos < map->size-1)
|
rlm@0
|
298 memmove(&map->array[pos+1], &map->array[pos],
|
rlm@0
|
299 (map->size-1-pos)*sizeof(map->array[0]));
|
rlm@0
|
300 }
|
rlm@0
|
301 map->array[pos].key = key;
|
rlm@0
|
302 map->array[pos].value = value;
|
rlm@0
|
303
|
rlm@0
|
304 return AL_NO_ERROR;
|
rlm@0
|
305 }
|
rlm@0
|
306
|
rlm@0
|
307 void RemoveUIntMapKey(UIntMap *map, ALuint key)
|
rlm@0
|
308 {
|
rlm@0
|
309 if(map->size > 0)
|
rlm@0
|
310 {
|
rlm@0
|
311 ALsizei low = 0;
|
rlm@0
|
312 ALsizei high = map->size - 1;
|
rlm@0
|
313 while(low < high)
|
rlm@0
|
314 {
|
rlm@0
|
315 ALsizei mid = low + (high-low)/2;
|
rlm@0
|
316 if(map->array[mid].key < key)
|
rlm@0
|
317 low = mid + 1;
|
rlm@0
|
318 else
|
rlm@0
|
319 high = mid;
|
rlm@0
|
320 }
|
rlm@0
|
321 if(map->array[low].key == key)
|
rlm@0
|
322 {
|
rlm@0
|
323 if(low < map->size-1)
|
rlm@0
|
324 memmove(&map->array[low], &map->array[low+1],
|
rlm@0
|
325 (map->size-1-low)*sizeof(map->array[0]));
|
rlm@0
|
326 map->size--;
|
rlm@0
|
327 }
|
rlm@0
|
328 }
|
rlm@0
|
329 }
|
rlm@0
|
330
|
rlm@0
|
331 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
|
rlm@0
|
332 {
|
rlm@0
|
333 if(map->size > 0)
|
rlm@0
|
334 {
|
rlm@0
|
335 ALsizei low = 0;
|
rlm@0
|
336 ALsizei high = map->size - 1;
|
rlm@0
|
337 while(low < high)
|
rlm@0
|
338 {
|
rlm@0
|
339 ALsizei mid = low + (high-low)/2;
|
rlm@0
|
340 if(map->array[mid].key < key)
|
rlm@0
|
341 low = mid + 1;
|
rlm@0
|
342 else
|
rlm@0
|
343 high = mid;
|
rlm@0
|
344 }
|
rlm@0
|
345 if(map->array[low].key == key)
|
rlm@0
|
346 return map->array[low].value;
|
rlm@0
|
347 }
|
rlm@0
|
348 return NULL;
|
rlm@0
|
349 }
|