16 #include "alAuxEffectSlot.h"
20 #include "nfcfilter.h"
21 #include "math_defs.h"
24 #define MAX_PITCH (255)
26 /* Maximum number of buffer samples before the current pos needed for resampling. */
27 #define MAX_PRE_SAMPLES 12
29 /* Maximum number of buffer samples after the current pos needed for resampling. */
30 #define MAX_POST_SAMPLES 12
38 struct ALbufferlistitem
;
43 #define DITHER_RNG_SEED 22222
47 SpatializeOff
= AL_FALSE
,
48 SpatializeOn
= AL_TRUE
,
49 SpatializeAuto
= AL_AUTO_SOFT
58 ResamplerMax
= BSincResampler
60 extern enum Resampler ResamplerDefault
;
62 /* The number of distinct scale and phase intervals within the filter table. */
63 #define BSINC_SCALE_BITS 4
64 #define BSINC_SCALE_COUNT (1<<BSINC_SCALE_BITS)
65 #define BSINC_PHASE_BITS 4
66 #define BSINC_PHASE_COUNT (1<<BSINC_PHASE_BITS)
68 /* Interpolator state. Kind of a misnomer since the interpolator itself is
69 * stateless. This just keeps it from having to recompute scale-related
70 * mappings for every sample.
72 typedef struct BsincState
{
73 ALfloat sf
; /* Scale interpolation factor. */
74 ALuint m
; /* Coefficient count. */
75 ALint l
; /* Left coefficient offset. */
77 const ALfloat
*filter
; /* Filter coefficients. */
78 const ALfloat
*scDelta
; /* Scale deltas. */
79 const ALfloat
*phDelta
; /* Phase deltas. */
80 const ALfloat
*spDelta
; /* Scale-phase deltas. */
81 } coeffs
[BSINC_PHASE_COUNT
];
84 typedef union InterpState
{
88 ALboolean
BsincPrepare(const ALuint increment
, BsincState
*state
);
90 typedef const ALfloat
* (*ResamplerFunc
)(const InterpState
*state
,
91 const ALfloat
*restrict src
, ALsizei frac
, ALint increment
,
92 ALfloat
*restrict dst
, ALsizei dstlen
96 typedef union aluVector
{
97 alignas(16) ALfloat v
[4];
100 inline void aluVectorSet(aluVector
*vector
, ALfloat x
, ALfloat y
, ALfloat z
, ALfloat w
)
109 typedef union aluMatrixf
{
110 alignas(16) ALfloat m
[4][4];
112 extern const aluMatrixf IdentityMatrixf
;
114 inline void aluMatrixfSetRow(aluMatrixf
*matrix
, ALuint row
,
115 ALfloat m0
, ALfloat m1
, ALfloat m2
, ALfloat m3
)
117 matrix
->m
[row
][0] = m0
;
118 matrix
->m
[row
][1] = m1
;
119 matrix
->m
[row
][2] = m2
;
120 matrix
->m
[row
][3] = m3
;
123 inline void aluMatrixfSet(aluMatrixf
*matrix
, ALfloat m00
, ALfloat m01
, ALfloat m02
, ALfloat m03
,
124 ALfloat m10
, ALfloat m11
, ALfloat m12
, ALfloat m13
,
125 ALfloat m20
, ALfloat m21
, ALfloat m22
, ALfloat m23
,
126 ALfloat m30
, ALfloat m31
, ALfloat m32
, ALfloat m33
)
128 aluMatrixfSetRow(matrix
, 0, m00
, m01
, m02
, m03
);
129 aluMatrixfSetRow(matrix
, 1, m10
, m11
, m12
, m13
);
130 aluMatrixfSetRow(matrix
, 2, m20
, m21
, m22
, m23
);
131 aluMatrixfSetRow(matrix
, 3, m30
, m31
, m32
, m33
);
139 AF_BandPass
= AF_LowPass
| AF_HighPass
143 typedef struct MixHrtfParams
{
144 const ALfloat (*Coeffs
)[2];
151 typedef struct DirectParams
{
152 ALfilterState LowPass
;
153 ALfilterState HighPass
;
155 NfcFilter NFCtrlFilter
[MAX_AMBI_ORDER
];
164 ALfloat Current
[MAX_OUTPUT_CHANNELS
];
165 ALfloat Target
[MAX_OUTPUT_CHANNELS
];
169 typedef struct SendParams
{
170 ALfilterState LowPass
;
171 ALfilterState HighPass
;
174 ALfloat Current
[MAX_OUTPUT_CHANNELS
];
175 ALfloat Target
[MAX_OUTPUT_CHANNELS
];
180 struct ALvoiceProps
{
181 ATOMIC(struct ALvoiceProps
*) next
;
192 ALfloat RolloffFactor
;
195 ALfloat Direction
[3];
196 ALfloat Orientation
[2][3];
197 ALboolean HeadRelative
;
198 enum DistanceModel DistanceModel
;
199 enum Resampler Resampler
;
200 ALboolean DirectChannels
;
201 enum SpatializeMode SpatializeMode
;
203 ALboolean DryGainHFAuto
;
204 ALboolean WetGainAuto
;
205 ALboolean WetGainHFAuto
;
208 ALfloat AirAbsorptionFactor
;
209 ALfloat RoomRolloffFactor
;
210 ALfloat DopplerFactor
;
212 ALfloat StereoPan
[2];
216 /** Direct filter and auxiliary send info. */
225 struct ALeffectslot
*Slot
;
234 /* If not 'fading', gain targets are used directly without fading. */
235 #define VOICE_IS_FADING (1<<0)
236 #define VOICE_HAS_HRTF (1<<1)
237 #define VOICE_HAS_NFC (1<<2)
239 typedef struct ALvoice
{
240 struct ALvoiceProps
*Props
;
242 ATOMIC(struct ALvoiceProps
*) Update
;
243 ATOMIC(struct ALvoiceProps
*) FreeList
;
245 ATOMIC(struct ALsource
*) Source
;
246 ATOMIC(bool) Playing
;
249 * Source offset in samples, relative to the currently playing buffer, NOT
250 * the whole queue, and the fractional (fixed-point) offset to the next
253 ATOMIC(ALuint
) position
;
254 ATOMIC(ALsizei
) position_fraction
;
256 /* Current buffer queue item being played. */
257 ATOMIC(struct ALbufferlistitem
*) current_buffer
;
259 /* Buffer queue item to loop to at end of queue (will be NULL for non-
262 ATOMIC(struct ALbufferlistitem
*) loop_buffer
;
265 * Number of channels and bytes-per-sample for the attached source's
271 /** Current target parameters used for mixing. */
274 ResamplerFunc Resampler
;
278 ALuint Offset
; /* Number of output samples mixed since starting. */
280 alignas(16) ALfloat PrevSamples
[MAX_INPUT_CHANNELS
][MAX_PRE_SAMPLES
];
282 InterpState ResampleState
;
285 enum ActiveFilters FilterType
;
286 DirectParams Params
[MAX_INPUT_CHANNELS
];
288 ALfloat (*Buffer
)[BUFFERSIZE
];
290 ALsizei ChannelsPerOrder
[MAX_AMBI_ORDER
+1];
294 enum ActiveFilters FilterType
;
295 SendParams Params
[MAX_INPUT_CHANNELS
];
297 ALfloat (*Buffer
)[BUFFERSIZE
];
302 void DeinitVoice(ALvoice
*voice
);
305 typedef void (*MixerFunc
)(const ALfloat
*data
, ALsizei OutChans
,
306 ALfloat (*restrict OutBuffer
)[BUFFERSIZE
], ALfloat
*CurrentGains
,
307 const ALfloat
*TargetGains
, ALsizei Counter
, ALsizei OutPos
,
309 typedef void (*RowMixerFunc
)(ALfloat
*OutBuffer
, const ALfloat
*gains
,
310 const ALfloat (*restrict data
)[BUFFERSIZE
], ALsizei InChans
,
311 ALsizei InPos
, ALsizei BufferSize
);
312 typedef void (*HrtfMixerFunc
)(ALfloat
*restrict LeftOut
, ALfloat
*restrict RightOut
,
313 const ALfloat
*data
, ALsizei Offset
, ALsizei OutPos
,
314 const ALsizei IrSize
, MixHrtfParams
*hrtfparams
,
315 HrtfState
*hrtfstate
, ALsizei BufferSize
);
316 typedef void (*HrtfMixerBlendFunc
)(ALfloat
*restrict LeftOut
, ALfloat
*restrict RightOut
,
317 const ALfloat
*data
, ALsizei Offset
, ALsizei OutPos
,
318 const ALsizei IrSize
, const HrtfParams
*oldparams
,
319 MixHrtfParams
*newparams
, HrtfState
*hrtfstate
,
321 typedef void (*HrtfDirectMixerFunc
)(ALfloat
*restrict LeftOut
, ALfloat
*restrict RightOut
,
322 const ALfloat
*data
, ALsizei Offset
, const ALsizei IrSize
,
323 const ALfloat (*restrict Coeffs
)[2],
324 ALfloat (*restrict Values
)[2], ALsizei BufferSize
);
327 #define GAIN_MIX_MAX (16.0f) /* +24dB */
329 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
331 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
332 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
334 /* Target gain for the reverb decay feedback reaching the decay time. */
335 #define REVERB_DECAY_GAIN (0.001f) /* -60 dB */
337 #define FRACTIONBITS (12)
338 #define FRACTIONONE (1<<FRACTIONBITS)
339 #define FRACTIONMASK (FRACTIONONE-1)
342 inline ALfloat
minf(ALfloat a
, ALfloat b
)
343 { return ((a
> b
) ? b
: a
); }
344 inline ALfloat
maxf(ALfloat a
, ALfloat b
)
345 { return ((a
> b
) ? a
: b
); }
346 inline ALfloat
clampf(ALfloat val
, ALfloat min
, ALfloat max
)
347 { return minf(max
, maxf(min
, val
)); }
349 inline ALdouble
mind(ALdouble a
, ALdouble b
)
350 { return ((a
> b
) ? b
: a
); }
351 inline ALdouble
maxd(ALdouble a
, ALdouble b
)
352 { return ((a
> b
) ? a
: b
); }
353 inline ALdouble
clampd(ALdouble val
, ALdouble min
, ALdouble max
)
354 { return mind(max
, maxd(min
, val
)); }
356 inline ALuint
minu(ALuint a
, ALuint b
)
357 { return ((a
> b
) ? b
: a
); }
358 inline ALuint
maxu(ALuint a
, ALuint b
)
359 { return ((a
> b
) ? a
: b
); }
360 inline ALuint
clampu(ALuint val
, ALuint min
, ALuint max
)
361 { return minu(max
, maxu(min
, val
)); }
363 inline ALint
mini(ALint a
, ALint b
)
364 { return ((a
> b
) ? b
: a
); }
365 inline ALint
maxi(ALint a
, ALint b
)
366 { return ((a
> b
) ? a
: b
); }
367 inline ALint
clampi(ALint val
, ALint min
, ALint max
)
368 { return mini(max
, maxi(min
, val
)); }
370 inline ALint64
mini64(ALint64 a
, ALint64 b
)
371 { return ((a
> b
) ? b
: a
); }
372 inline ALint64
maxi64(ALint64 a
, ALint64 b
)
373 { return ((a
> b
) ? a
: b
); }
374 inline ALint64
clampi64(ALint64 val
, ALint64 min
, ALint64 max
)
375 { return mini64(max
, maxi64(min
, val
)); }
377 inline ALuint64
minu64(ALuint64 a
, ALuint64 b
)
378 { return ((a
> b
) ? b
: a
); }
379 inline ALuint64
maxu64(ALuint64 a
, ALuint64 b
)
380 { return ((a
> b
) ? a
: b
); }
381 inline ALuint64
clampu64(ALuint64 val
, ALuint64 min
, ALuint64 max
)
382 { return minu64(max
, maxu64(min
, val
)); }
385 extern alignas(16) const ALfloat bsincTab
[18840];
386 extern alignas(16) const ALfloat sinc4Tab
[FRACTIONONE
][4];
389 inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALfloat mu
)
391 return val1
+ (val2
-val1
)*mu
;
393 inline ALfloat
resample_fir4(ALfloat val0
, ALfloat val1
, ALfloat val2
, ALfloat val3
, ALsizei frac
)
395 return sinc4Tab
[frac
][0]*val0
+ sinc4Tab
[frac
][1]*val1
+
396 sinc4Tab
[frac
][2]*val2
+ sinc4Tab
[frac
][3]*val3
;
400 enum HrtfRequestMode
{
406 void aluInitMixer(void);
408 MixerFunc
SelectMixer(void);
409 RowMixerFunc
SelectRowMixer(void);
410 ResamplerFunc
SelectResampler(enum Resampler resampler
);
414 * Set up the appropriate panning method and mixing method given the device
417 void aluInitRenderer(ALCdevice
*device
, ALint hrtf_id
, enum HrtfRequestMode hrtf_appreq
, enum HrtfRequestMode hrtf_userreq
);
419 void aluInitEffectPanning(struct ALeffectslot
*slot
);
422 * CalcDirectionCoeffs
424 * Calculates ambisonic coefficients based on a direction vector. The vector
425 * must be normalized (unit length), and the spread is the angular width of the
428 void CalcDirectionCoeffs(const ALfloat dir
[3], ALfloat spread
, ALfloat coeffs
[MAX_AMBI_COEFFS
]);
433 * Calculates ambisonic coefficients based on azimuth and elevation. The
434 * azimuth and elevation parameters are in radians, going right and up
437 inline void CalcAngleCoeffs(ALfloat azimuth
, ALfloat elevation
, ALfloat spread
, ALfloat coeffs
[MAX_AMBI_COEFFS
])
440 sinf(azimuth
) * cosf(elevation
),
442 -cosf(azimuth
) * cosf(elevation
)
444 CalcDirectionCoeffs(dir
, spread
, coeffs
);
448 * CalcAnglePairwiseCoeffs
450 * Calculates ambisonic coefficients based on azimuth and elevation. The
451 * azimuth and elevation parameters are in radians, going right and up
452 * respectively. This pairwise variant warps the result such that +30 azimuth
453 * is full right, and -30 azimuth is full left.
455 void CalcAnglePairwiseCoeffs(ALfloat azimuth
, ALfloat elevation
, ALfloat spread
, ALfloat coeffs
[MAX_AMBI_COEFFS
]);
458 * ComputeAmbientGains
460 * Computes channel gains for ambient, omni-directional sounds.
462 #define ComputeAmbientGains(b, g, o) do { \
463 if((b).CoeffCount > 0) \
464 ComputeAmbientGainsMC((b).Ambi.Coeffs, (b).NumChannels, g, o); \
466 ComputeAmbientGainsBF((b).Ambi.Map, (b).NumChannels, g, o); \
468 void ComputeAmbientGainsMC(const ChannelConfig
*chancoeffs
, ALsizei numchans
, ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
469 void ComputeAmbientGainsBF(const BFChannelConfig
*chanmap
, ALsizei numchans
, ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
472 * ComputePanningGains
474 * Computes panning gains using the given channel decoder coefficients and the
475 * pre-calculated direction or angle coefficients.
477 #define ComputePanningGains(b, c, g, o) do { \
478 if((b).CoeffCount > 0) \
479 ComputePanningGainsMC((b).Ambi.Coeffs, (b).NumChannels, (b).CoeffCount, c, g, o);\
481 ComputePanningGainsBF((b).Ambi.Map, (b).NumChannels, c, g, o); \
483 void ComputePanningGainsMC(const ChannelConfig
*chancoeffs
, ALsizei numchans
, ALsizei numcoeffs
, const ALfloat coeffs
[MAX_AMBI_COEFFS
], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
484 void ComputePanningGainsBF(const BFChannelConfig
*chanmap
, ALsizei numchans
, const ALfloat coeffs
[MAX_AMBI_COEFFS
], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
487 * ComputeFirstOrderGains
489 * Sets channel gains for a first-order ambisonics input channel. The matrix is
490 * a 1x4 'slice' of a transform matrix for the input channel, used to scale and
491 * orient the sound samples.
493 #define ComputeFirstOrderGains(b, m, g, o) do { \
494 if((b).CoeffCount > 0) \
495 ComputeFirstOrderGainsMC((b).Ambi.Coeffs, (b).NumChannels, m, g, o); \
497 ComputeFirstOrderGainsBF((b).Ambi.Map, (b).NumChannels, m, g, o); \
499 void ComputeFirstOrderGainsMC(const ChannelConfig
*chancoeffs
, ALsizei numchans
, const ALfloat mtx
[4], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
500 void ComputeFirstOrderGainsBF(const BFChannelConfig
*chanmap
, ALsizei numchans
, const ALfloat mtx
[4], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
503 ALboolean
MixSource(struct ALvoice
*voice
, struct ALsource
*Source
, ALCdevice
*Device
, ALsizei SamplesToDo
);
505 void aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
);
506 /* Caller must lock the device. */
507 void aluHandleDisconnect(ALCdevice
*device
);
509 extern ALfloat ConeScale
;
510 extern ALfloat ZScale
;