33 #include "static_assert.h"
42 #ifndef ALC_SOFT_device_clock
43 #define ALC_SOFT_device_clock 1
44 typedef int64_t ALCint64SOFT
;
45 typedef uint64_t ALCuint64SOFT
;
46 #define ALC_DEVICE_CLOCK_SOFT 0x1600
47 typedef void (ALC_APIENTRY
*LPALCGETINTEGER64VSOFT
)(ALCdevice
*device
, ALCenum pname
, ALsizei size
, ALCint64SOFT
*values
);
48 #ifdef AL_ALEXT_PROTOTYPES
49 ALC_API
void ALC_APIENTRY
alcGetInteger64vSOFT(ALCdevice
*device
, ALCenum pname
, ALsizei size
, ALCint64SOFT
*values
);
54 typedef ALint64SOFT ALint64
;
55 typedef ALuint64SOFT ALuint64
;
59 #define U64(x) ((ALuint64)(x##ui64))
60 #elif SIZEOF_LONG == 8
61 #define U64(x) ((ALuint64)(x##ul))
62 #elif SIZEOF_LONG_LONG == 8
63 #define U64(x) ((ALuint64)(x##ull))
68 #define UINT64_MAX U64(18446744073709551615)
72 #if defined(__cplusplus)
74 #elif defined(__GNUC__)
75 #define UNUSED(x) UNUSED_##x __attribute__((unused))
76 #elif defined(__LCLINT__)
77 #define UNUSED(x) /*@unused@*/ x
84 #define DECL_CONST __attribute__((const))
85 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
88 #define DECL_FORMAT(x, y, z)
91 #if defined(__GNUC__) && defined(__i386__)
92 /* force_align_arg_pointer is required for proper function arguments aligning
93 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
94 * thread functions will be properly aligned on the stack, even though GCC may
95 * generate code with the assumption that it is. */
96 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
102 #define DECL_VLA(T, _name, _size) T _name[(_size)]
104 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
109 #define PATH_MAX MAX_PATH
111 #define PATH_MAX 4096
118 ALubyte b
[sizeof(ALuint
)];
119 } EndianTest
= { 1 };
120 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
122 #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
125 #define DERIVE_FROM_TYPE(t) t t##_parent
126 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
128 #define STATIC_UPCAST(to, from, obj) __extension__({ \
129 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
130 "Invalid upcast object from type"); \
131 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
134 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
137 #define DECLARE_FORWARD(T1, T2, rettype, func) \
138 rettype T1##_##func(T1 *obj) \
139 { return T2##_##func(STATIC_CAST(T2, obj)); }
141 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
142 rettype T1##_##func(T1 *obj, argtype1 a) \
143 { return T2##_##func(STATIC_CAST(T2, obj), a); }
145 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
146 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
147 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
149 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
150 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
151 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
154 #define GET_VTABLE1(T1) (&(T1##_vtable))
155 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
157 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
158 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
160 #define DECLARE_THUNK(T1, T2, rettype, func) \
161 static rettype T1##_##T2##_##func(T2 *obj) \
162 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
164 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
165 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
166 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
168 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
169 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
170 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
172 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
173 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
174 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
176 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
177 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
178 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
180 #define DECLARE_DEFAULT_ALLOCATORS(T) \
181 static void* T##_New(size_t size) { return al_malloc(16, size); } \
182 static void T##_Delete(void *ptr) { al_free(ptr); }
184 /* Helper to extract an argument list for VCALL. Not used directly. */
185 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
187 /* Call a "virtual" method on an object, with arguments. */
188 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
189 /* Call a "virtual" method on an object, with no arguments. */
190 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
192 #define DELETE_OBJ(obj) do { \
195 V0((obj),Destruct)(); \
196 V0((obj),Delete)(); \
201 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
205 #define NEW_OBJ(_res, T) do { \
206 _res = T##_New(sizeof(T)); \
209 memset(_res, 0, sizeof(T)); \
210 T##_Construct(_res, EXTRACT_NEW_ARGS
220 #define DEFAULT_OUTPUT_RATE (44100)
221 #define MIN_OUTPUT_RATE (8000)
224 /* Find the next power-of-2 for non-power-of-2 numbers. */
225 inline ALuint
NextPowerOf2(ALuint value
)
239 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
241 inline ALint
fastf2i(ALfloat f
)
245 #elif defined(_MSC_VER) && defined(_M_IX86)
255 /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
257 inline ALuint
fastf2u(ALfloat f
)
258 { return fastf2i(f
); }
267 ALCenum (*OpenPlayback
)(ALCdevice
*, const ALCchar
*);
268 void (*ClosePlayback
)(ALCdevice
*);
269 ALCboolean (*ResetPlayback
)(ALCdevice
*);
270 ALCboolean (*StartPlayback
)(ALCdevice
*);
271 void (*StopPlayback
)(ALCdevice
*);
273 ALCenum (*OpenCapture
)(ALCdevice
*, const ALCchar
*);
274 void (*CloseCapture
)(ALCdevice
*);
275 void (*StartCapture
)(ALCdevice
*);
276 void (*StopCapture
)(ALCdevice
*);
277 ALCenum (*CaptureSamples
)(ALCdevice
*, void*, ALCuint
);
278 ALCuint (*AvailableSamples
)(ALCdevice
*);
281 ALCboolean
alc_sndio_init(BackendFuncs
*func_list
);
282 void alc_sndio_deinit(void);
283 void alc_sndio_probe(enum DevProbe type
);
284 ALCboolean
alc_ca_init(BackendFuncs
*func_list
);
285 void alc_ca_deinit(void);
286 void alc_ca_probe(enum DevProbe type
);
287 ALCboolean
alc_opensl_init(BackendFuncs
*func_list
);
288 void alc_opensl_deinit(void);
289 void alc_opensl_probe(enum DevProbe type
);
290 ALCboolean
alc_qsa_init(BackendFuncs
*func_list
);
291 void alc_qsa_deinit(void);
292 void alc_qsa_probe(enum DevProbe type
);
298 InverseDistanceClamped
= AL_INVERSE_DISTANCE_CLAMPED
,
299 LinearDistanceClamped
= AL_LINEAR_DISTANCE_CLAMPED
,
300 ExponentDistanceClamped
= AL_EXPONENT_DISTANCE_CLAMPED
,
301 InverseDistance
= AL_INVERSE_DISTANCE
,
302 LinearDistance
= AL_LINEAR_DISTANCE
,
303 ExponentDistance
= AL_EXPONENT_DISTANCE
,
304 DisableDistance
= AL_NONE
,
306 DefaultDistanceModel
= InverseDistanceClamped
340 DevFmtByte
= ALC_BYTE_SOFT
,
341 DevFmtUByte
= ALC_UNSIGNED_BYTE_SOFT
,
342 DevFmtShort
= ALC_SHORT_SOFT
,
343 DevFmtUShort
= ALC_UNSIGNED_SHORT_SOFT
,
344 DevFmtInt
= ALC_INT_SOFT
,
345 DevFmtUInt
= ALC_UNSIGNED_INT_SOFT
,
346 DevFmtFloat
= ALC_FLOAT_SOFT
,
348 DevFmtTypeDefault
= DevFmtFloat
350 enum DevFmtChannels
{
351 DevFmtMono
= ALC_MONO_SOFT
,
352 DevFmtStereo
= ALC_STEREO_SOFT
,
353 DevFmtQuad
= ALC_QUAD_SOFT
,
354 DevFmtX51
= ALC_5POINT1_SOFT
,
355 DevFmtX61
= ALC_6POINT1_SOFT
,
356 DevFmtX71
= ALC_7POINT1_SOFT
,
358 /* Similar to 5.1, except using rear channels instead of sides */
359 DevFmtX51Rear
= 0x80000000,
363 DevFmtChannelsDefault
= DevFmtStereo
365 #define MAX_OUTPUT_CHANNELS (8)
367 ALuint
BytesFromDevFmt(enum DevFmtType type
) DECL_CONST
;
368 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
) DECL_CONST
;
369 inline ALuint
FrameSizeFromDevFmt(enum DevFmtChannels chans
, enum DevFmtType type
)
371 return ChannelsFromDevFmt(chans
) * BytesFromDevFmt(type
);
375 extern const struct EffectList
{
397 /* The maximum number of Ambisonics coefficients. For a given order (o), the
398 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
399 * second-order has 9, and third-order has 16. */
400 #define MAX_AMBI_COEFFS 16
402 typedef ALfloat ChannelConfig
[MAX_AMBI_COEFFS
];
405 #define HRTF_HISTORY_BITS (6)
406 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
407 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
409 typedef struct HrtfState
{
410 alignas(16) ALfloat History
[HRTF_HISTORY_LENGTH
];
411 alignas(16) ALfloat Values
[HRIR_LENGTH
][2];
414 typedef struct HrtfParams
{
415 alignas(16) ALfloat Coeffs
[HRIR_LENGTH
][2];
420 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
421 * more memory, while smaller values may need more iterations. The value needs
422 * to be a sensible size, however, as it constrains the max stepping value used
423 * for mixing, as well as the maximum number of samples per mixing iteration.
425 #define BUFFERSIZE (2048u)
427 struct ALCdevice_struct
431 ALCboolean Connected
;
432 enum DeviceType Type
;
437 enum DevFmtChannels FmtChans
;
438 enum DevFmtType FmtType
;
439 ALboolean IsHeadphones
;
441 al_string DeviceName
;
443 ATOMIC(ALCenum
) LastError
;
445 // Maximum number of sources that can be created
446 ALuint MaxNoOfSources
;
447 // Maximum number of slots that can be created
448 ALuint AuxiliaryEffectSlotMax
;
450 ALCuint NumMonoSources
;
451 ALCuint NumStereoSources
;
454 // Map of Buffers for this device
457 // Map of Effects for this device
460 // Map of Filters for this device
463 /* HRTF filter tables */
464 vector_HrtfEntry Hrtf_List
;
466 const struct Hrtf
*Hrtf
;
468 HrtfState Hrtf_State
[MAX_OUTPUT_CHANNELS
];
469 HrtfParams Hrtf_Params
[MAX_OUTPUT_CHANNELS
];
472 /* UHJ encoder state */
473 struct Uhj2Encoder
*Uhj_Encoder
;
475 // Stereo-to-binaural filter
478 /* Rendering mode. */
479 enum RenderMode Render_Mode
;
487 /* Temp storage used for each source when mixing. */
488 alignas(16) ALfloat SourceData
[BUFFERSIZE
];
489 alignas(16) ALfloat ResampledData
[BUFFERSIZE
];
490 alignas(16) ALfloat FilteredData
[BUFFERSIZE
];
492 /* The "dry" path corresponds to the main output. */
494 /* Channel names for the dry buffer mix. */
495 enum Channel ChannelName
[MAX_OUTPUT_CHANNELS
];
496 /* Ambisonic coefficients for mixing to the dry buffer. */
497 ChannelConfig AmbiCoeffs
[MAX_OUTPUT_CHANNELS
];
498 /* Scale for first-order XYZ inputs using AmbiCoeffs. */
501 /* Dry buffer will be aliased by the virtual or real output. */
502 ALfloat (*Buffer
)[BUFFERSIZE
];
506 /* Virtual output, to be post-processed to the real output. */
508 ALfloat (*Buffer
)[BUFFERSIZE
];
511 /* "Real" output, which will be written to the device buffer. */
513 enum Channel ChannelName
[MAX_OUTPUT_CHANNELS
];
515 ALfloat (*Buffer
)[BUFFERSIZE
];
519 /* Running count of the mixer invocations, in 31.1 fixed point. This
520 * actually increments *twice* when mixing, first at the start and then at
521 * the end, so the bottom bit indicates if the device is currently mixing
522 * and the upper bits indicates how many mixes have been done.
526 /* Default effect slot */
527 struct ALeffectslot
*DefaultSlot
;
529 // Contexts created on this device
530 ATOMIC(ALCcontext
*) ContextList
;
532 struct ALCbackend
*Backend
;
534 void *ExtraData
; // For the backend's use
536 ALCdevice
*volatile next
;
538 /* Memory space used by the default slot (Playback devices only) */
539 alignas(16) ALCbyte _slot_mem
[];
542 // Frequency was requested by the app or config file
543 #define DEVICE_FREQUENCY_REQUEST (1<<1)
544 // Channel configuration was requested by the config file
545 #define DEVICE_CHANNELS_REQUEST (1<<2)
546 // Sample type was requested by the config file
547 #define DEVICE_SAMPLE_TYPE_REQUEST (1<<3)
549 // Specifies if the DSP is paused at user request
550 #define DEVICE_PAUSED (1<<30)
552 // Specifies if the device is currently running
553 #define DEVICE_RUNNING (1<<31)
556 /* Nanosecond resolution for the device clock time. */
557 #define DEVICE_CLOCK_RES U64(1000000000)
560 /* Must be less than 15 characters (16 including terminating null) for
561 * compatibility with pthread_setname_np limitations. */
562 #define MIXER_THREAD_NAME "alsoft-mixer"
564 #define RECORD_THREAD_NAME "alsoft-record"
567 struct ALCcontext_struct
571 struct ALlistener
*Listener
;
574 UIntMap EffectSlotMap
;
576 ATOMIC(ALenum
) LastError
;
578 ATOMIC(ALenum
) UpdateSources
;
580 volatile enum DistanceModel DistanceModel
;
581 volatile ALboolean SourceDistanceModel
;
583 volatile ALfloat DopplerFactor
;
584 volatile ALfloat DopplerVelocity
;
585 volatile ALfloat SpeedOfSound
;
586 volatile ALenum DeferUpdates
;
588 struct ALvoice
*Voices
;
592 VECTOR(struct ALeffectslot
*) ActiveAuxSlots
;
595 const ALCchar
*ExtensionList
;
597 ALCcontext
*volatile next
;
599 /* Memory space used by the listener */
600 alignas(16) ALCbyte _listener_mem
[];
603 ALCcontext
*GetContextRef(void);
605 void ALCcontext_IncRef(ALCcontext
*context
);
606 void ALCcontext_DecRef(ALCcontext
*context
);
608 void AppendAllDevicesList(const ALCchar
*name
);
609 void AppendCaptureDeviceList(const ALCchar
*name
);
611 void ALCdevice_Lock(ALCdevice
*device
);
612 void ALCdevice_Unlock(ALCdevice
*device
);
614 void ALCcontext_DeferUpdates(ALCcontext
*context
);
615 void ALCcontext_ProcessUpdates(ALCcontext
*context
);
617 inline void LockContext(ALCcontext
*context
)
618 { ALCdevice_Lock(context
->Device
); }
620 inline void UnlockContext(ALCcontext
*context
)
621 { ALCdevice_Unlock(context
->Device
); }
624 void *al_malloc(size_t alignment
, size_t size
);
625 void *al_calloc(size_t alignment
, size_t size
);
626 void al_free(void *ptr
);
631 DERIVE_FROM_TYPE(fenv_t
);
639 void SetMixerFPUMode(FPUCtl
*ctl
);
640 void RestoreFPUMode(const FPUCtl
*ctl
);
643 typedef struct RingBuffer RingBuffer
;
644 RingBuffer
*CreateRingBuffer(ALsizei frame_size
, ALsizei length
);
645 void DestroyRingBuffer(RingBuffer
*ring
);
646 ALsizei
RingBufferSize(RingBuffer
*ring
);
647 void WriteRingBuffer(RingBuffer
*ring
, const ALubyte
*data
, ALsizei len
);
648 void ReadRingBuffer(RingBuffer
*ring
, ALubyte
*data
, ALsizei len
);
650 typedef struct ll_ringbuffer ll_ringbuffer_t
;
651 typedef struct ll_ringbuffer_data
{
654 } ll_ringbuffer_data_t
;
655 ll_ringbuffer_t
*ll_ringbuffer_create(size_t sz
, size_t elem_sz
);
656 void ll_ringbuffer_free(ll_ringbuffer_t
*rb
);
657 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t
*rb
, ll_ringbuffer_data_t
*vec
);
658 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t
*rb
, ll_ringbuffer_data_t
*vec
);
659 size_t ll_ringbuffer_read(ll_ringbuffer_t
*rb
, char *dest
, size_t cnt
);
660 size_t ll_ringbuffer_peek(ll_ringbuffer_t
*rb
, char *dest
, size_t cnt
);
661 void ll_ringbuffer_read_advance(ll_ringbuffer_t
*rb
, size_t cnt
);
662 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t
*rb
);
663 int ll_ringbuffer_mlock(ll_ringbuffer_t
*rb
);
664 void ll_ringbuffer_reset(ll_ringbuffer_t
*rb
);
665 size_t ll_ringbuffer_write(ll_ringbuffer_t
*rb
, const char *src
, size_t cnt
);
666 void ll_ringbuffer_write_advance(ll_ringbuffer_t
*rb
, size_t cnt
);
667 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t
*rb
);
669 void ReadALConfig(void);
670 void FreeALConfig(void);
671 int ConfigValueExists(const char *devName
, const char *blockName
, const char *keyName
);
672 const char *GetConfigValue(const char *devName
, const char *blockName
, const char *keyName
, const char *def
);
673 int GetConfigValueBool(const char *devName
, const char *blockName
, const char *keyName
, int def
);
674 int ConfigValueStr(const char *devName
, const char *blockName
, const char *keyName
, const char **ret
);
675 int ConfigValueInt(const char *devName
, const char *blockName
, const char *keyName
, int *ret
);
676 int ConfigValueUInt(const char *devName
, const char *blockName
, const char *keyName
, unsigned int *ret
);
677 int ConfigValueFloat(const char *devName
, const char *blockName
, const char *keyName
, float *ret
);
678 int ConfigValueBool(const char *devName
, const char *blockName
, const char *keyName
, int *ret
);
680 void SetRTPriority(void);
682 void SetDefaultChannelOrder(ALCdevice
*device
);
683 void SetDefaultWFXChannelOrder(ALCdevice
*device
);
685 const ALCchar
*DevFmtTypeString(enum DevFmtType type
) DECL_CONST
;
686 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
) DECL_CONST
;
689 * GetChannelIdxByName
691 * Returns the dry buffer's channel index for the given channel name (e.g.
692 * FrontCenter), or -1 if it doesn't exist.
694 inline ALint
GetChannelIdxByName(const ALCdevice
*device
, enum Channel chan
)
697 for(i
= 0;i
< MAX_OUTPUT_CHANNELS
;i
++)
699 if(device
->Dry
.ChannelName
[i
] == chan
)
706 extern FILE *LogFile
;
708 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
709 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
711 void al_print(const char *type
, const char *func
, const char *fmt
, ...) DECL_FORMAT(printf
, 3,4);
712 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
722 extern enum LogLevel LogLevel
;
724 #define TRACEREF(...) do { \
725 if(LogLevel >= LogRef) \
726 AL_PRINT("(--)", __VA_ARGS__); \
729 #define TRACE(...) do { \
730 if(LogLevel >= LogTrace) \
731 AL_PRINT("(II)", __VA_ARGS__); \
734 #define WARN(...) do { \
735 if(LogLevel >= LogWarning) \
736 AL_PRINT("(WW)", __VA_ARGS__); \
739 #define ERR(...) do { \
740 if(LogLevel >= LogError) \
741 AL_PRINT("(EE)", __VA_ARGS__); \
745 extern ALint RTPrioLevel
;
748 extern ALuint CPUCapFlags
;
753 CPU_CAP_SSE4_1
= 1<<3,
757 void FillCPUCaps(ALuint capfilter
);
759 vector_al_string
SearchDataFiles(const char *match
, const char *subdir
);
761 /* Small hack to use a pointer-to-array type as a normal argument type.
762 * Shouldn't be used directly. */
763 typedef ALfloat ALfloatBUFFERSIZE
[BUFFERSIZE
];