21 #include "polymorphism.h"
22 #include "static_assert.h"
31 #ifndef ALC_SOFT_loopback2
32 #define ALC_SOFT_loopback2 1
33 #define ALC_AMBISONIC_LAYOUT_SOFT 0xfff0
34 #define ALC_AMBISONIC_SCALING_SOFT 0xfff1
35 #define ALC_AMBISONIC_ORDER_SOFT 0xfff2
36 #define ALC_MAX_AMBISONIC_ORDER_SOFT 0xfff3
38 #define ALC_BFORMAT3D_SOFT 0x1508
40 /* Ambisonic layouts */
41 #define ALC_ACN_SOFT 0xfff4
42 #define ALC_FUMA_SOFT 0xfff5
44 /* Ambisonic scalings (normalization) */
45 /*#define ALC_FUMA_SOFT*/
46 #define ALC_SN3D_SOFT 0xfff6
47 #define ALC_N3D_SOFT 0xfff7
50 #ifndef ALC_SOFT_device_clock
51 #define ALC_SOFT_device_clock 1
52 typedef int64_t ALCint64SOFT
;
53 typedef uint64_t ALCuint64SOFT
;
54 #define ALC_DEVICE_CLOCK_SOFT 0x1600
55 #define ALC_DEVICE_LATENCY_SOFT 0x1601
56 #define ALC_DEVICE_CLOCK_LATENCY_SOFT 0x1602
57 #define AL_SAMPLE_OFFSET_CLOCK_SOFT 0x1202
58 #define AL_SEC_OFFSET_CLOCK_SOFT 0x1203
59 typedef void (ALC_APIENTRY
*LPALCGETINTEGER64VSOFT
)(ALCdevice
*device
, ALCenum pname
, ALsizei size
, ALCint64SOFT
*values
);
60 #ifdef AL_ALEXT_PROTOTYPES
61 ALC_API
void ALC_APIENTRY
alcGetInteger64vSOFT(ALCdevice
*device
, ALCenum pname
, ALsizei size
, ALCint64SOFT
*values
);
65 #ifndef AL_SOFT_buffer_samples2
66 #define AL_SOFT_buffer_samples2 1
67 /* Channel configurations */
68 #define AL_MONO_SOFT 0x1500
69 #define AL_STEREO_SOFT 0x1501
70 #define AL_REAR_SOFT 0x1502
71 #define AL_QUAD_SOFT 0x1503
72 #define AL_5POINT1_SOFT 0x1504
73 #define AL_6POINT1_SOFT 0x1505
74 #define AL_7POINT1_SOFT 0x1506
75 #define AL_BFORMAT2D_SOFT 0x1507
76 #define AL_BFORMAT3D_SOFT 0x1508
79 #define AL_BYTE_SOFT 0x1400
80 #define AL_UNSIGNED_BYTE_SOFT 0x1401
81 #define AL_SHORT_SOFT 0x1402
82 #define AL_UNSIGNED_SHORT_SOFT 0x1403
83 #define AL_INT_SOFT 0x1404
84 #define AL_UNSIGNED_INT_SOFT 0x1405
85 #define AL_FLOAT_SOFT 0x1406
86 #define AL_DOUBLE_SOFT 0x1407
87 #define AL_BYTE3_SOFT 0x1408
88 #define AL_UNSIGNED_BYTE3_SOFT 0x1409
89 #define AL_MULAW_SOFT 0x140A
92 #define AL_MONO8_SOFT 0x1100
93 #define AL_MONO16_SOFT 0x1101
94 #define AL_MONO32F_SOFT 0x10010
95 #define AL_STEREO8_SOFT 0x1102
96 #define AL_STEREO16_SOFT 0x1103
97 #define AL_STEREO32F_SOFT 0x10011
98 #define AL_QUAD8_SOFT 0x1204
99 #define AL_QUAD16_SOFT 0x1205
100 #define AL_QUAD32F_SOFT 0x1206
101 #define AL_REAR8_SOFT 0x1207
102 #define AL_REAR16_SOFT 0x1208
103 #define AL_REAR32F_SOFT 0x1209
104 #define AL_5POINT1_8_SOFT 0x120A
105 #define AL_5POINT1_16_SOFT 0x120B
106 #define AL_5POINT1_32F_SOFT 0x120C
107 #define AL_6POINT1_8_SOFT 0x120D
108 #define AL_6POINT1_16_SOFT 0x120E
109 #define AL_6POINT1_32F_SOFT 0x120F
110 #define AL_7POINT1_8_SOFT 0x1210
111 #define AL_7POINT1_16_SOFT 0x1211
112 #define AL_7POINT1_32F_SOFT 0x1212
113 #define AL_BFORMAT2D_8_SOFT 0x20021
114 #define AL_BFORMAT2D_16_SOFT 0x20022
115 #define AL_BFORMAT2D_32F_SOFT 0x20023
116 #define AL_BFORMAT3D_8_SOFT 0x20031
117 #define AL_BFORMAT3D_16_SOFT 0x20032
118 #define AL_BFORMAT3D_32F_SOFT 0x20033
120 /* Buffer attributes */
121 #define AL_INTERNAL_FORMAT_SOFT 0x2008
122 #define AL_BYTE_LENGTH_SOFT 0x2009
123 #define AL_SAMPLE_LENGTH_SOFT 0x200A
124 #define AL_SEC_LENGTH_SOFT 0x200B
127 typedef void (AL_APIENTRY
*LPALBUFFERSAMPLESSOFT
)(ALuint
,ALuint
,ALenum
,ALsizei
,ALenum
,ALenum
,const ALvoid
*);
128 typedef void (AL_APIENTRY
*LPALGETBUFFERSAMPLESSOFT
)(ALuint
,ALsizei
,ALsizei
,ALenum
,ALenum
,ALvoid
*);
129 typedef ALboolean (AL_APIENTRY
*LPALISBUFFERFORMATSUPPORTEDSOFT
)(ALenum
);
130 #ifdef AL_ALEXT_PROTOTYPES
131 AL_API
void AL_APIENTRY
alBufferSamplesSOFT(ALuint buffer
, ALuint samplerate
, ALenum internalformat
, ALsizei samples
, ALenum channels
, ALenum type
, const ALvoid
*data
);
132 AL_API
void AL_APIENTRY
alGetBufferSamplesSOFT(ALuint buffer
, ALsizei offset
, ALsizei samples
, ALenum channels
, ALenum type
, ALvoid
*data
);
133 AL_API ALboolean AL_APIENTRY
alIsBufferFormatSupportedSOFT(ALenum format
);
140 #define SZFMT "%I64u"
141 #elif defined(_WIN32)
149 #define LIKELY(x) __builtin_expect(!!(x), !0)
150 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
152 #define LIKELY(x) (!!(x))
153 #define UNLIKELY(x) (!!(x))
156 typedef ALint64SOFT ALint64
;
157 typedef ALuint64SOFT ALuint64
;
160 #if defined(_MSC_VER)
161 #define U64(x) ((ALuint64)(x##ui64))
162 #elif SIZEOF_LONG == 8
163 #define U64(x) ((ALuint64)(x##ul))
164 #elif SIZEOF_LONG_LONG == 8
165 #define U64(x) ((ALuint64)(x##ull))
170 #define UINT64_MAX U64(18446744073709551615)
174 #if defined(__cplusplus)
176 #elif defined(__GNUC__)
177 #define UNUSED(x) UNUSED_##x __attribute__((unused))
178 #elif defined(__LCLINT__)
179 #define UNUSED(x) /*@unused@*/ x
185 /* Calculates the size of a struct with N elements of a flexible array member.
186 * GCC and Clang allow offsetof(Type, fam[N]) for this, but MSVC seems to have
187 * trouble, so a bit more verbose workaround is needed.
189 #define FAM_SIZE(T, M, N) (offsetof(T, M) + sizeof(((T*)NULL)->M[0])*(N))
192 #define DECL_VLA(T, _name, _size) T _name[(_size)]
194 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
200 ALubyte b
[sizeof(ALuint
)];
201 } EndianTest
= { 1 };
202 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
204 #define COUNTOF(x) (sizeof(x) / sizeof(0[x]))
213 struct DirectHrtfState
;
214 struct FrontStablizer
;
217 struct ALcontextProps
;
218 struct ALlistenerProps
;
220 struct ALeffectslotProps
;
223 #define DEFAULT_OUTPUT_RATE (44100)
224 #define MIN_OUTPUT_RATE (8000)
227 /* Find the next power-of-2 for non-power-of-2 numbers. */
228 inline ALuint
NextPowerOf2(ALuint value
)
242 /** Round up a value to the next multiple. */
243 inline size_t RoundUp(size_t value
, size_t r
)
246 return value
- (value
%r
);
249 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
251 inline ALint
fastf2i(ALfloat f
)
255 #elif defined(_MSC_VER) && defined(_M_IX86)
273 InverseDistanceClamped
= AL_INVERSE_DISTANCE_CLAMPED
,
274 LinearDistanceClamped
= AL_LINEAR_DISTANCE_CLAMPED
,
275 ExponentDistanceClamped
= AL_EXPONENT_DISTANCE_CLAMPED
,
276 InverseDistance
= AL_INVERSE_DISTANCE
,
277 LinearDistance
= AL_LINEAR_DISTANCE
,
278 ExponentDistance
= AL_EXPONENT_DISTANCE
,
279 DisableDistance
= AL_NONE
,
281 DefaultDistanceModel
= InverseDistanceClamped
327 DevFmtByte
= ALC_BYTE_SOFT
,
328 DevFmtUByte
= ALC_UNSIGNED_BYTE_SOFT
,
329 DevFmtShort
= ALC_SHORT_SOFT
,
330 DevFmtUShort
= ALC_UNSIGNED_SHORT_SOFT
,
331 DevFmtInt
= ALC_INT_SOFT
,
332 DevFmtUInt
= ALC_UNSIGNED_INT_SOFT
,
333 DevFmtFloat
= ALC_FLOAT_SOFT
,
335 DevFmtTypeDefault
= DevFmtFloat
337 enum DevFmtChannels
{
338 DevFmtMono
= ALC_MONO_SOFT
,
339 DevFmtStereo
= ALC_STEREO_SOFT
,
340 DevFmtQuad
= ALC_QUAD_SOFT
,
341 DevFmtX51
= ALC_5POINT1_SOFT
,
342 DevFmtX61
= ALC_6POINT1_SOFT
,
343 DevFmtX71
= ALC_7POINT1_SOFT
,
344 DevFmtAmbi3D
= ALC_BFORMAT3D_SOFT
,
346 /* Similar to 5.1, except using rear channels instead of sides */
347 DevFmtX51Rear
= 0x80000000,
349 DevFmtChannelsDefault
= DevFmtStereo
351 #define MAX_OUTPUT_CHANNELS (16)
353 ALsizei
BytesFromDevFmt(enum DevFmtType type
);
354 ALsizei
ChannelsFromDevFmt(enum DevFmtChannels chans
, ALsizei ambiorder
);
355 inline ALsizei
FrameSizeFromDevFmt(enum DevFmtChannels chans
, enum DevFmtType type
, ALsizei ambiorder
)
357 return ChannelsFromDevFmt(chans
, ambiorder
) * BytesFromDevFmt(type
);
361 AmbiLayout_FuMa
= ALC_FUMA_SOFT
, /* FuMa channel order */
362 AmbiLayout_ACN
= ALC_ACN_SOFT
, /* ACN channel order */
364 AmbiLayout_Default
= AmbiLayout_ACN
368 AmbiNorm_FuMa
= ALC_FUMA_SOFT
, /* FuMa normalization */
369 AmbiNorm_SN3D
= ALC_SN3D_SOFT
, /* SN3D normalization */
370 AmbiNorm_N3D
= ALC_N3D_SOFT
, /* N3D normalization */
372 AmbiNorm_Default
= AmbiNorm_SN3D
390 /* The maximum number of Ambisonics coefficients. For a given order (o), the
391 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
392 * second-order has 9, third-order has 16, and fourth-order has 25.
394 #define MAX_AMBI_ORDER 3
395 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
397 /* A bitmask of ambisonic channels with height information. If none of these
398 * channels are used/needed, there's no height (e.g. with most surround sound
399 * speaker setups). This only specifies up to 4th order, which is the highest
400 * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
401 * order). This is ACN ordering, with bit 0 being ACN 0, etc.
403 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
405 /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
406 * representation. This is 2 per each order above zero-order, plus 1 for zero-
407 * order. Or simply, o*2 + 1.
409 #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
412 typedef ALfloat ChannelConfig
[MAX_AMBI_COEFFS
];
413 typedef struct BFChannelConfig
{
418 typedef union AmbiConfig
{
419 /* Ambisonic coefficients for mixing to the dry buffer. */
420 ChannelConfig Coeffs
[MAX_OUTPUT_CHANNELS
];
421 /* Coefficient channel mapping for mixing to the dry buffer. */
422 BFChannelConfig Map
[MAX_OUTPUT_CHANNELS
];
426 typedef struct EnumeratedHrtf
{
429 struct HrtfEntry
*hrtf
;
431 TYPEDEF_VECTOR(EnumeratedHrtf
, vector_EnumeratedHrtf
)
434 /* Maximum delay in samples for speaker distance compensation. */
435 #define MAX_DELAY_LENGTH 1024
437 typedef struct DistanceComp
{
439 ALsizei Length
; /* Valid range is [0...MAX_DELAY_LENGTH). */
443 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
444 * more memory, while smaller values may need more iterations. The value needs
445 * to be a sensible size, however, as it constrains the max stepping value used
446 * for mixing, as well as the maximum number of samples per mixing iteration.
448 #define BUFFERSIZE 2048
450 typedef struct DryMixParams
{
452 /* Number of coefficients in each Ambi.Coeffs to mix together (4 for first-
453 * order, 9 for second-order, etc). If the count is 0, Ambi.Map is used
454 * instead to map each output to a coefficient index.
458 ALfloat (*Buffer
)[BUFFERSIZE
];
460 ALsizei NumChannelsPerOrder
[MAX_AMBI_ORDER
+1];
463 typedef struct BFMixParams
{
465 /* Will only be 4 or 0. */
468 ALfloat (*Buffer
)[BUFFERSIZE
];
472 typedef struct RealMixParams
{
473 enum Channel ChannelName
[MAX_OUTPUT_CHANNELS
];
475 ALfloat (*Buffer
)[BUFFERSIZE
];
479 struct ALCdevice_struct
483 ALCboolean Connected
;
484 enum DeviceType Type
;
489 enum DevFmtChannels FmtChans
;
490 enum DevFmtType FmtType
;
491 ALboolean IsHeadphones
;
493 /* For DevFmtAmbi* output only, specifies the channel order and
496 enum AmbiLayout AmbiLayout
;
497 enum AmbiNorm AmbiScale
;
499 al_string DeviceName
;
501 ATOMIC(ALCenum
) LastError
;
503 // Maximum number of sources that can be created
505 // Maximum number of slots that can be created
506 ALuint AuxiliaryEffectSlotMax
;
508 ALCuint NumMonoSources
;
509 ALCuint NumStereoSources
;
512 // Map of Buffers for this device
515 // Map of Effects for this device
518 // Map of Filters for this device
521 /* HRTF state and info */
522 struct DirectHrtfState
*Hrtf
;
524 struct Hrtf
*HrtfHandle
;
525 vector_EnumeratedHrtf HrtfList
;
528 /* UHJ encoder state */
529 struct Uhj2Encoder
*Uhj_Encoder
;
531 /* High quality Ambisonic decoder */
532 struct BFormatDec
*AmbiDecoder
;
534 /* Stereo-to-binaural filter */
537 /* First-order ambisonic upsampler for higher-order output */
538 struct AmbiUpsampler
*AmbiUp
;
540 /* Rendering mode. */
541 enum RenderMode Render_Mode
;
549 /* Temp storage used for mixer processing. */
550 alignas(16) ALfloat TempBuffer
[4][BUFFERSIZE
];
552 /* The "dry" path corresponds to the main output. */
555 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
558 /* "Real" output, which will be written to the device buffer. May alias the
561 RealMixParams RealOut
;
563 struct FrontStablizer
*Stablizer
;
565 struct Compressor
*Limiter
;
567 /* The average speaker distance as determined by the ambdec configuration
568 * (or alternatively, by the NFC-HOA reference delay). Only used for NFC.
570 ALfloat AvgSpeakerDist
;
572 /* Delay buffers used to compensate for speaker distances. */
573 DistanceComp ChannelDelay
[MAX_OUTPUT_CHANNELS
];
575 /* Dithering control. */
579 /* Running count of the mixer invocations, in 31.1 fixed point. This
580 * actually increments *twice* when mixing, first at the start and then at
581 * the end, so the bottom bit indicates if the device is currently mixing
582 * and the upper bits indicates how many mixes have been done.
586 // Contexts created on this device
587 ATOMIC(ALCcontext
*) ContextList
;
590 struct ALCbackend
*Backend
;
592 ALCdevice
*volatile next
;
595 // Frequency was requested by the app or config file
596 #define DEVICE_FREQUENCY_REQUEST (1u<<1)
597 // Channel configuration was requested by the config file
598 #define DEVICE_CHANNELS_REQUEST (1u<<2)
599 // Sample type was requested by the config file
600 #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
602 // Specifies if the DSP is paused at user request
603 #define DEVICE_PAUSED (1u<<30)
605 // Specifies if the device is currently running
606 #define DEVICE_RUNNING (1u<<31)
609 /* Nanosecond resolution for the device clock time. */
610 #define DEVICE_CLOCK_RES U64(1000000000)
613 /* Must be less than 15 characters (16 including terminating null) for
614 * compatibility with pthread_setname_np limitations. */
615 #define MIXER_THREAD_NAME "alsoft-mixer"
617 #define RECORD_THREAD_NAME "alsoft-record"
620 struct ALCcontext_struct
{
623 struct ALlistener
*Listener
;
626 UIntMap EffectSlotMap
;
628 ATOMIC(ALenum
) LastError
;
630 enum DistanceModel DistanceModel
;
631 ALboolean SourceDistanceModel
;
633 ALfloat DopplerFactor
;
634 ALfloat DopplerVelocity
;
635 ALfloat SpeedOfSound
;
636 ALfloat MetersPerUnit
;
638 ATOMIC_FLAG PropsClean
;
639 ATOMIC(ALenum
) DeferUpdates
;
643 /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
644 * indicates if updates are currently happening).
646 RefCount UpdateCount
;
647 ATOMIC(ALenum
) HoldUpdates
;
651 ATOMIC(struct ALcontextProps
*) Update
;
653 /* Linked lists of unused property containers, free to use for future
656 ATOMIC(struct ALcontextProps
*) FreeContextProps
;
657 ATOMIC(struct ALlistenerProps
*) FreeListenerProps
;
658 ATOMIC(struct ALvoiceProps
*) FreeVoiceProps
;
659 ATOMIC(struct ALeffectslotProps
*) FreeEffectslotProps
;
661 struct ALvoice
**Voices
;
665 ATOMIC(struct ALeffectslotArray
*) ActiveAuxSlots
;
667 /* Default effect slot */
668 struct ALeffectslot
*DefaultSlot
;
671 const ALCchar
*ExtensionList
;
673 ALCcontext
*volatile next
;
675 /* Memory space used by the listener (and possibly default effect slot) */
676 alignas(16) ALCbyte _listener_mem
[];
679 ALCcontext
*GetContextRef(void);
681 void ALCcontext_DecRef(ALCcontext
*context
);
683 void ALCcontext_DeferUpdates(ALCcontext
*context
);
684 void ALCcontext_ProcessUpdates(ALCcontext
*context
);
686 void AllocateVoices(ALCcontext
*context
, ALsizei num_voices
, ALsizei old_sends
);
688 void AppendAllDevicesList(const ALCchar
*name
);
689 void AppendCaptureDeviceList(const ALCchar
*name
);
692 extern ALint RTPrioLevel
;
693 void SetRTPriority(void);
695 void SetDefaultChannelOrder(ALCdevice
*device
);
696 void SetDefaultWFXChannelOrder(ALCdevice
*device
);
698 const ALCchar
*DevFmtTypeString(enum DevFmtType type
);
699 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
);
701 inline ALint
GetChannelIndex(const enum Channel names
[MAX_OUTPUT_CHANNELS
], enum Channel chan
)
704 for(i
= 0;i
< MAX_OUTPUT_CHANNELS
;i
++)
712 * GetChannelIdxByName
714 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
717 inline ALint
GetChannelIdxByName(const RealMixParams
*real
, enum Channel chan
)
718 { return GetChannelIndex(real
->ChannelName
, chan
); }
721 vector_al_string
SearchDataFiles(const char *match
, const char *subdir
);