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
23 #define _WIN32_IE 0x501
25 #define _WIN32_IE 0x400
39 #ifndef AL_NO_UID_DEFS
40 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
49 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
50 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
52 DEFINE_GUID(IID_IDirectSoundNotify
, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
54 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
55 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
56 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
57 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
60 #include <devpropdef.h>
61 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
64 #endif /* AL_NO_UID_DEFS */
72 #ifdef HAVE_SYS_SYSCONF_H
73 #include <sys/sysconf.h>
96 extern inline ALuint
NextPowerOf2(ALuint value
);
97 extern inline ALint
fastf2i(ALfloat f
);
98 extern inline ALuint
fastf2u(ALfloat f
);
101 ALuint CPUCapFlags
= 0;
104 void FillCPUCaps(ALuint capfilter
)
108 /* FIXME: We really should get this for all available CPUs in case different
109 * CPUs have different caps (is that possible on one machine?). */
110 #if defined(HAVE_CPUID_H) && (defined(__i386__) || defined(__x86_64__) || \
111 defined(_M_IX86) || defined(_M_X64))
113 unsigned int regs
[4];
114 char str
[sizeof(unsigned int[4])];
117 if(!__get_cpuid(0, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
118 ERR("Failed to get CPUID\n");
121 unsigned int maxfunc
= cpuinf
[0].regs
[0];
122 unsigned int maxextfunc
= 0;
124 if(__get_cpuid(0x80000000, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
125 maxextfunc
= cpuinf
[0].regs
[0];
126 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
128 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
129 if(maxextfunc
>= 0x80000004 &&
130 __get_cpuid(0x80000002, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]) &&
131 __get_cpuid(0x80000003, &cpuinf
[1].regs
[0], &cpuinf
[1].regs
[1], &cpuinf
[1].regs
[2], &cpuinf
[1].regs
[3]) &&
132 __get_cpuid(0x80000004, &cpuinf
[2].regs
[0], &cpuinf
[2].regs
[1], &cpuinf
[2].regs
[2], &cpuinf
[2].regs
[3]))
133 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
136 __get_cpuid(1, &cpuinf
[0].regs
[0], &cpuinf
[0].regs
[1], &cpuinf
[0].regs
[2], &cpuinf
[0].regs
[3]))
138 if((cpuinf
[0].regs
[3]&(1<<25)))
141 if((cpuinf
[0].regs
[3]&(1<<26)))
143 caps
|= CPU_CAP_SSE2
;
144 if((cpuinf
[0].regs
[2]&(1<<19)))
145 caps
|= CPU_CAP_SSE4_1
;
150 #elif defined(HAVE_WINDOWS_H)
151 HMODULE k32
= GetModuleHandleA("kernel32.dll");
152 BOOL (WINAPI
*IsProcessorFeaturePresent
)(DWORD ProcessorFeature
);
153 IsProcessorFeaturePresent
= (BOOL(WINAPI
*)(DWORD
))GetProcAddress(k32
, "IsProcessorFeaturePresent");
154 if(!IsProcessorFeaturePresent
)
155 ERR("IsProcessorFeaturePresent not available; CPU caps not detected\n");
158 if(IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE
))
161 if(IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE
))
162 caps
|= CPU_CAP_SSE2
;
167 /* Assume Neon support if compiled with it */
168 caps
|= CPU_CAP_NEON
;
171 TRACE("Extensions:%s%s%s%s%s\n",
172 ((capfilter
&CPU_CAP_SSE
) ? ((caps
&CPU_CAP_SSE
) ? " +SSE" : " -SSE") : ""),
173 ((capfilter
&CPU_CAP_SSE2
) ? ((caps
&CPU_CAP_SSE2
) ? " +SSE2" : " -SSE2") : ""),
174 ((capfilter
&CPU_CAP_SSE4_1
) ? ((caps
&CPU_CAP_SSE4_1
) ? " +SSE4.1" : " -SSE4.1") : ""),
175 ((capfilter
&CPU_CAP_NEON
) ? ((caps
&CPU_CAP_NEON
) ? " +Neon" : " -Neon") : ""),
176 ((!capfilter
) ? " -none-" : "")
178 CPUCapFlags
= caps
& capfilter
;
182 void *al_malloc(size_t alignment
, size_t size
)
184 #if defined(HAVE_ALIGNED_ALLOC)
185 size
= (size
+(alignment
-1))&~(alignment
-1);
186 return aligned_alloc(alignment
, size
);
187 #elif defined(HAVE_POSIX_MEMALIGN)
189 if(posix_memalign(&ret
, alignment
, size
) == 0)
192 #elif defined(HAVE__ALIGNED_MALLOC)
193 return _aligned_malloc(size
, alignment
);
195 char *ret
= malloc(size
+alignment
);
199 while(((ALintptrEXT
)ret
&(alignment
-1)) != 0)
206 void *al_calloc(size_t alignment
, size_t size
)
208 void *ret
= al_malloc(alignment
, size
);
209 if(ret
) memset(ret
, 0, size
);
213 void al_free(void *ptr
)
215 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
217 #elif defined(HAVE__ALIGNED_MALLOC)
225 } while(*finder
== 0x55);
232 void SetMixerFPUMode(FPUCtl
*ctl
)
235 fegetenv(STATIC_CAST(fenv_t
, ctl
));
236 #if defined(__GNUC__) && defined(HAVE_SSE)
237 if((CPUCapFlags
&CPU_CAP_SSE
))
238 __asm__
__volatile__("stmxcsr %0" : "=m" (*&ctl
->sse_state
));
242 fesetround(FE_TOWARDZERO
);
244 #if defined(__GNUC__) && defined(HAVE_SSE)
245 if((CPUCapFlags
&CPU_CAP_SSE
))
247 int sseState
= ctl
->sse_state
;
248 sseState
|= 0x6000; /* set round-to-zero */
249 sseState
|= 0x8000; /* set flush-to-zero */
250 if((CPUCapFlags
&CPU_CAP_SSE2
))
251 sseState
|= 0x0040; /* set denormals-are-zero */
252 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
256 #elif defined(HAVE___CONTROL87_2)
259 __control87_2(0, 0, &ctl
->state
, NULL
);
260 __control87_2(_RC_CHOP
, _MCW_RC
, &mode
, NULL
);
262 if((CPUCapFlags
&CPU_CAP_SSE
))
264 __control87_2(0, 0, NULL
, &ctl
->sse_state
);
265 __control87_2(_RC_CHOP
|_DN_FLUSH
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
269 #elif defined(HAVE__CONTROLFP)
271 ctl
->state
= _controlfp(0, 0);
272 (void)_controlfp(_RC_CHOP
, _MCW_RC
);
276 void RestoreFPUMode(const FPUCtl
*ctl
)
279 fesetenv(STATIC_CAST(fenv_t
, ctl
));
280 #if defined(__GNUC__) && defined(HAVE_SSE)
281 if((CPUCapFlags
&CPU_CAP_SSE
))
282 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&ctl
->sse_state
));
285 #elif defined(HAVE___CONTROL87_2)
288 __control87_2(ctl
->state
, _MCW_RC
, &mode
, NULL
);
290 if((CPUCapFlags
&CPU_CAP_SSE
))
291 __control87_2(ctl
->sse_state
, _MCW_RC
|_MCW_DN
, NULL
, &mode
);
294 #elif defined(HAVE__CONTROLFP)
296 _controlfp(ctl
->state
, _MCW_RC
);
303 static WCHAR
*FromUTF8(const char *str
)
308 if((len
=MultiByteToWideChar(CP_UTF8
, 0, str
, -1, NULL
, 0)) > 0)
310 out
= calloc(sizeof(WCHAR
), len
);
311 MultiByteToWideChar(CP_UTF8
, 0, str
, -1, out
, len
);
317 void *LoadLib(const char *name
)
322 wname
= FromUTF8(name
);
324 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name
);
327 hdl
= LoadLibraryW(wname
);
332 void CloseLib(void *handle
)
333 { FreeLibrary((HANDLE
)handle
); }
334 void *GetSymbol(void *handle
, const char *name
)
338 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
340 ERR("Failed to load %s\n", name
);
344 WCHAR
*strdupW(const WCHAR
*str
)
354 ret
= calloc(sizeof(WCHAR
), len
+1);
356 memcpy(ret
, str
, sizeof(WCHAR
)*len
);
360 FILE *al_fopen(const char *fname
, const char *mode
)
362 WCHAR
*wname
=NULL
, *wmode
=NULL
;
365 wname
= FromUTF8(fname
);
366 wmode
= FromUTF8(mode
);
368 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname
);
370 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode
);
372 file
= _wfopen(wname
, wmode
);
384 void *LoadLib(const char *name
)
390 handle
= dlopen(name
, RTLD_NOW
);
391 if((err
=dlerror()) != NULL
)
395 void CloseLib(void *handle
)
397 void *GetSymbol(void *handle
, const char *name
)
403 sym
= dlsym(handle
, name
);
404 if((err
=dlerror()) != NULL
)
406 WARN("Failed to load %s: %s\n", name
, err
);
416 void al_print(const char *type
, const char *func
, const char *fmt
, ...)
421 fprintf(LogFile
, "AL lib: %s %s: ", type
, func
);
422 vfprintf(LogFile
, fmt
, ap
);
429 static inline int is_slash(int c
)
430 { return (c
== '\\' || c
== '/'); }
432 FILE *OpenDataFile(const char *fname
, const char *subdir
)
434 static const int ids
[2] = { CSIDL_APPDATA
, CSIDL_COMMON_APPDATA
};
435 WCHAR
*wname
=NULL
, *wsubdir
=NULL
;
438 /* If the path is absolute, open it directly. */
439 if(fname
[0] != '\0' && fname
[1] == ':' && is_slash(fname
[2]))
442 if((f
=al_fopen(fname
, "rb")) != NULL
)
444 TRACE("Opened %s\n", fname
);
447 WARN("Could not open %s\n", fname
);
451 wname
= FromUTF8(fname
);
452 wsubdir
= FromUTF8(subdir
);
454 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname
);
456 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir
);
457 else for(i
= 0;i
< 2;i
++)
459 WCHAR buffer
[PATH_MAX
];
463 if(SHGetSpecialFolderPathW(NULL
, buffer
, ids
[i
], FALSE
) == FALSE
)
466 len
= lstrlenW(buffer
);
467 if(len
> 0 && is_slash(buffer
[len
-1]))
468 buffer
[--len
] = '\0';
469 _snwprintf(buffer
+len
, PATH_MAX
-len
, L
"/%ls/%ls", wsubdir
, wname
);
470 len
= lstrlenW(buffer
);
474 if(buffer
[len
] == '/')
478 if((f
=_wfopen(buffer
, L
"rb")) != NULL
)
480 TRACE("Opened %ls\n", buffer
);
483 WARN("Could not open %ls\n", buffer
);
491 FILE *OpenDataFile(const char *fname
, const char *subdir
)
493 char buffer
[PATH_MAX
] = "";
494 const char *str
, *next
;
499 if((f
=al_fopen(fname
, "rb")) != NULL
)
501 TRACE("Opened %s\n", fname
);
504 WARN("Could not open %s\n", fname
);
508 if((str
=getenv("XDG_DATA_HOME")) != NULL
&& str
[0] != '\0')
509 snprintf(buffer
, sizeof(buffer
), "%s/%s/%s", str
, subdir
, fname
);
510 else if((str
=getenv("HOME")) != NULL
&& str
[0] != '\0')
511 snprintf(buffer
, sizeof(buffer
), "%s/.local/share/%s/%s", str
, subdir
, fname
);
514 if((f
=al_fopen(buffer
, "rb")) != NULL
)
516 TRACE("Opened %s\n", buffer
);
519 WARN("Could not open %s\n", buffer
);
522 if((str
=getenv("XDG_DATA_DIRS")) == NULL
|| str
[0] == '\0')
523 str
= "/usr/local/share/:/usr/share/";
526 while((str
=next
) != NULL
&& str
[0] != '\0')
529 next
= strchr(str
, ':');
539 if(len
> sizeof(buffer
)-1)
540 len
= sizeof(buffer
)-1;
541 strncpy(buffer
, str
, len
);
543 snprintf(buffer
+len
, sizeof(buffer
)-len
, "/%s/%s", subdir
, fname
);
545 if((f
=al_fopen(buffer
, "rb")) != NULL
)
547 TRACE("Opened %s\n", buffer
);
550 WARN("Could not open %s\n", buffer
);
558 void SetRTPriority(void)
560 ALboolean failed
= AL_FALSE
;
564 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
565 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
568 struct sched_param param
;
569 /* Use the minimum real-time priority possible for now (on Linux this
570 * should be 1 for SCHED_RR) */
571 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
572 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
575 /* Real-time priority not available */
576 failed
= (RTPrioLevel
>0);
579 ERR("Failed to set priority level for thread\n");
583 ALboolean
vector_reserve(void *ptr
, size_t base_size
, size_t obj_count
, size_t obj_size
, ALboolean exact
)
585 vector_
*vecptr
= ptr
;
586 if((size_t)(*vecptr
? (*vecptr
)->Capacity
: 0) < obj_count
)
588 ALsizei old_size
= (*vecptr
? (*vecptr
)->Size
: 0);
591 /* Limit vector sizes to the greatest power-of-two value that an
592 * ALsizei can hold. */
593 if(obj_count
> (INT_MAX
>>1)+1)
596 /* Use the next power-of-2 size if we don't need to allocate the exact
597 * amount. This is preferred when regularly increasing the vector since
598 * it means fewer reallocations. Though it means it also wastes some
600 if(exact
== AL_FALSE
)
601 obj_count
= NextPowerOf2((ALuint
)obj_count
);
603 /* Need to be explicit with the caller type's base size, because it
604 * could have extra padding before the start of the array (that is,
605 * sizeof(*vector_) may not equal base_size). */
606 temp
= realloc(*vecptr
, base_size
+ obj_size
*obj_count
);
607 if(temp
== NULL
) return AL_FALSE
;
610 (*vecptr
)->Capacity
= (ALsizei
)obj_count
;
611 (*vecptr
)->Size
= old_size
;
616 ALboolean
vector_resize(void *ptr
, size_t base_size
, size_t obj_count
, size_t obj_size
)
618 vector_
*vecptr
= ptr
;
619 if(*vecptr
|| obj_count
> 0)
621 if(!vector_reserve(vecptr
, base_size
, obj_count
, obj_size
, AL_TRUE
))
623 (*vecptr
)->Size
= (ALsizei
)obj_count
;
628 ALboolean
vector_insert(void *ptr
, size_t base_size
, size_t obj_size
, void *ins_pos
, const void *datstart
, const void *datend
)
630 vector_
*vecptr
= ptr
;
631 if(datstart
!= datend
)
633 ptrdiff_t ins_elem
= ((char*)ins_pos
- ((char*)(*vecptr
) + base_size
)) / obj_size
;
634 ptrdiff_t numins
= ((const char*)datend
- (const char*)datstart
) / obj_size
;
637 if(INT_MAX
-VECTOR_SIZE(*vecptr
) <= numins
||
638 !vector_reserve(vecptr
, base_size
, VECTOR_SIZE(*vecptr
)+numins
, obj_size
, AL_TRUE
))
641 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
642 if(ins_elem
< (*vecptr
)->Size
)
644 memmove((char*)(*vecptr
) + base_size
+ ((ins_elem
+numins
)*obj_size
),
645 (char*)(*vecptr
) + base_size
+ ((ins_elem
)*obj_size
),
646 ((*vecptr
)->Size
-ins_elem
)*obj_size
);
648 memcpy((char*)(*vecptr
) + base_size
+ (ins_elem
*obj_size
),
649 datstart
, numins
*obj_size
);
650 (*vecptr
)->Size
+= (ALsizei
)numins
;
656 extern inline void al_string_deinit(al_string
*str
);
657 extern inline ALsizei
al_string_length(const_al_string str
);
658 extern inline ALboolean
al_string_empty(const_al_string str
);
659 extern inline const al_string_char_type
*al_string_get_cstr(const_al_string str
);
661 void al_string_clear(al_string
*str
)
663 /* Reserve one more character than the total size of the string. This is to
664 * ensure we have space to add a null terminator in the string data so it
665 * can be used as a C-style string. */
666 VECTOR_RESERVE(*str
, 1);
667 VECTOR_RESIZE(*str
, 0);
668 *VECTOR_ITER_END(*str
) = 0;
671 static inline int al_string_compare(const al_string_char_type
*str1
, ALsizei str1len
,
672 const al_string_char_type
*str2
, ALsizei str2len
)
674 ALsizei complen
= mini(str1len
, str2len
);
675 int ret
= memcmp(str1
, str2
, complen
);
678 if(str1len
> str2len
) return 1;
679 if(str1len
< str2len
) return -1;
683 int al_string_cmp(const_al_string str1
, const_al_string str2
)
685 return al_string_compare(&VECTOR_FRONT(str1
), al_string_length(str1
),
686 &VECTOR_FRONT(str2
), al_string_length(str2
));
688 int al_string_cmp_cstr(const_al_string str1
, const al_string_char_type
*str2
)
690 return al_string_compare(&VECTOR_FRONT(str1
), al_string_length(str1
),
691 str2
, (ALsizei
)strlen(str2
));
694 void al_string_copy(al_string
*str
, const_al_string from
)
696 ALsizei len
= VECTOR_SIZE(from
);
697 VECTOR_RESERVE(*str
, len
+1);
698 VECTOR_RESIZE(*str
, 0);
699 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), VECTOR_ITER_BEGIN(from
), VECTOR_ITER_BEGIN(from
)+len
);
700 *VECTOR_ITER_END(*str
) = 0;
703 void al_string_copy_cstr(al_string
*str
, const al_string_char_type
*from
)
705 size_t len
= strlen(from
);
706 VECTOR_RESERVE(*str
, len
+1);
707 VECTOR_RESIZE(*str
, 0);
708 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), from
, from
+len
);
709 *VECTOR_ITER_END(*str
) = 0;
712 void al_string_append_char(al_string
*str
, const al_string_char_type c
)
714 VECTOR_RESERVE(*str
, al_string_length(*str
)+2);
715 VECTOR_PUSH_BACK(*str
, c
);
716 *VECTOR_ITER_END(*str
) = 0;
719 void al_string_append_cstr(al_string
*str
, const al_string_char_type
*from
)
721 size_t len
= strlen(from
);
724 VECTOR_RESERVE(*str
, al_string_length(*str
)+len
+1);
725 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), from
, from
+len
);
726 *VECTOR_ITER_END(*str
) = 0;
730 void al_string_append_range(al_string
*str
, const al_string_char_type
*from
, const al_string_char_type
*to
)
734 VECTOR_RESERVE(*str
, al_string_length(*str
)+(to
-from
)+1);
735 VECTOR_INSERT(*str
, VECTOR_ITER_END(*str
), from
, to
);
736 *VECTOR_ITER_END(*str
) = 0;
741 void al_string_copy_wcstr(al_string
*str
, const wchar_t *from
)
744 if((len
=WideCharToMultiByte(CP_UTF8
, 0, from
, -1, NULL
, 0, NULL
, NULL
)) > 0)
746 VECTOR_RESERVE(*str
, len
);
747 VECTOR_RESIZE(*str
, len
-1);
748 WideCharToMultiByte(CP_UTF8
, 0, from
, -1, &VECTOR_FRONT(*str
), len
, NULL
, NULL
);
749 *VECTOR_ITER_END(*str
) = 0;