2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 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
32 #include "alListener.h"
33 #include "alAuxEffectSlot.h"
38 #include "mixer_defs.h"
40 #include "midi/base.h"
49 ALfloat ConeScale
= 1.0f
;
51 /* Localized Z scalar for mono sources */
52 ALfloat ZScale
= 1.0f
;
54 extern inline ALfloat
minf(ALfloat a
, ALfloat b
);
55 extern inline ALfloat
maxf(ALfloat a
, ALfloat b
);
56 extern inline ALfloat
clampf(ALfloat val
, ALfloat min
, ALfloat max
);
58 extern inline ALdouble
mind(ALdouble a
, ALdouble b
);
59 extern inline ALdouble
maxd(ALdouble a
, ALdouble b
);
60 extern inline ALdouble
clampd(ALdouble val
, ALdouble min
, ALdouble max
);
62 extern inline ALuint
minu(ALuint a
, ALuint b
);
63 extern inline ALuint
maxu(ALuint a
, ALuint b
);
64 extern inline ALuint
clampu(ALuint val
, ALuint min
, ALuint max
);
66 extern inline ALint
mini(ALint a
, ALint b
);
67 extern inline ALint
maxi(ALint a
, ALint b
);
68 extern inline ALint
clampi(ALint val
, ALint min
, ALint max
);
70 extern inline ALint64
mini64(ALint64 a
, ALint64 b
);
71 extern inline ALint64
maxi64(ALint64 a
, ALint64 b
);
72 extern inline ALint64
clampi64(ALint64 val
, ALint64 min
, ALint64 max
);
74 extern inline ALuint64
minu64(ALuint64 a
, ALuint64 b
);
75 extern inline ALuint64
maxu64(ALuint64 a
, ALuint64 b
);
76 extern inline ALuint64
clampu64(ALuint64 val
, ALuint64 min
, ALuint64 max
);
78 extern inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALfloat mu
);
79 extern inline ALfloat
cubic(ALfloat val0
, ALfloat val1
, ALfloat val2
, ALfloat val3
, ALfloat mu
);
81 static ResamplerFunc
SelectResampler(enum Resampler Resampler
, ALuint increment
)
83 if(increment
== FRACTIONONE
)
84 return Resample_copy32_C
;
88 return Resample_point32_C
;
90 return Resample_lerp32_C
;
92 return Resample_cubic32_C
;
94 /* Shouldn't happen */
98 return Resample_point32_C
;
102 static DryMixerFunc
SelectHrtfMixer(void)
105 if((CPUCapFlags
&CPU_CAP_SSE
))
106 return MixDirect_Hrtf_SSE
;
109 if((CPUCapFlags
&CPU_CAP_NEON
))
110 return MixDirect_Hrtf_Neon
;
113 return MixDirect_Hrtf_C
;
116 static DryMixerFunc
SelectDirectMixer(void)
119 if((CPUCapFlags
&CPU_CAP_SSE
))
120 return MixDirect_SSE
;
123 if((CPUCapFlags
&CPU_CAP_NEON
))
124 return MixDirect_Neon
;
130 static WetMixerFunc
SelectSendMixer(void)
133 if((CPUCapFlags
&CPU_CAP_SSE
))
137 if((CPUCapFlags
&CPU_CAP_NEON
))
145 static inline void aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
147 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
148 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
149 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
152 static inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
154 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
155 inVector1
[2]*inVector2
[2];
158 static inline void aluNormalize(ALfloat
*inVector
)
160 ALfloat lengthsqr
= aluDotproduct(inVector
, inVector
);
163 ALfloat inv_length
= 1.0f
/sqrtf(lengthsqr
);
164 inVector
[0] *= inv_length
;
165 inVector
[1] *= inv_length
;
166 inVector
[2] *= inv_length
;
170 static inline ALvoid
aluMatrixVector(ALfloat
*vector
, ALfloat w
, ALfloat (*restrict matrix
)[4])
173 vector
[0], vector
[1], vector
[2], w
176 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
177 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
178 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
182 static ALvoid
CalcListenerParams(ALlistener
*Listener
)
184 ALfloat N
[3], V
[3], U
[3], P
[3];
187 N
[0] = Listener
->Forward
[0];
188 N
[1] = Listener
->Forward
[1];
189 N
[2] = Listener
->Forward
[2];
191 V
[0] = Listener
->Up
[0];
192 V
[1] = Listener
->Up
[1];
193 V
[2] = Listener
->Up
[2];
195 /* Build and normalize right-vector */
196 aluCrossproduct(N
, V
, U
);
199 Listener
->Params
.Matrix
[0][0] = U
[0];
200 Listener
->Params
.Matrix
[0][1] = V
[0];
201 Listener
->Params
.Matrix
[0][2] = -N
[0];
202 Listener
->Params
.Matrix
[0][3] = 0.0f
;
203 Listener
->Params
.Matrix
[1][0] = U
[1];
204 Listener
->Params
.Matrix
[1][1] = V
[1];
205 Listener
->Params
.Matrix
[1][2] = -N
[1];
206 Listener
->Params
.Matrix
[1][3] = 0.0f
;
207 Listener
->Params
.Matrix
[2][0] = U
[2];
208 Listener
->Params
.Matrix
[2][1] = V
[2];
209 Listener
->Params
.Matrix
[2][2] = -N
[2];
210 Listener
->Params
.Matrix
[2][3] = 0.0f
;
211 Listener
->Params
.Matrix
[3][0] = 0.0f
;
212 Listener
->Params
.Matrix
[3][1] = 0.0f
;
213 Listener
->Params
.Matrix
[3][2] = 0.0f
;
214 Listener
->Params
.Matrix
[3][3] = 1.0f
;
216 P
[0] = Listener
->Position
[0];
217 P
[1] = Listener
->Position
[1];
218 P
[2] = Listener
->Position
[2];
219 aluMatrixVector(P
, 1.0f
, Listener
->Params
.Matrix
);
220 Listener
->Params
.Matrix
[3][0] = -P
[0];
221 Listener
->Params
.Matrix
[3][1] = -P
[1];
222 Listener
->Params
.Matrix
[3][2] = -P
[2];
224 Listener
->Params
.Velocity
[0] = Listener
->Velocity
[0];
225 Listener
->Params
.Velocity
[1] = Listener
->Velocity
[1];
226 Listener
->Params
.Velocity
[2] = Listener
->Velocity
[2];
227 aluMatrixVector(Listener
->Params
.Velocity
, 0.0f
, Listener
->Params
.Matrix
);
230 ALvoid
CalcNonAttnSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
232 static const struct ChanMap MonoMap
[1] = { { FrontCenter
, 0.0f
} };
233 static const struct ChanMap StereoMap
[2] = {
234 { FrontLeft
, DEG2RAD(-30.0f
) },
235 { FrontRight
, DEG2RAD( 30.0f
) }
237 static const struct ChanMap StereoWideMap
[2] = {
238 { FrontLeft
, DEG2RAD(-90.0f
) },
239 { FrontRight
, DEG2RAD( 90.0f
) }
241 static const struct ChanMap RearMap
[2] = {
242 { BackLeft
, DEG2RAD(-150.0f
) },
243 { BackRight
, DEG2RAD( 150.0f
) }
245 static const struct ChanMap QuadMap
[4] = {
246 { FrontLeft
, DEG2RAD( -45.0f
) },
247 { FrontRight
, DEG2RAD( 45.0f
) },
248 { BackLeft
, DEG2RAD(-135.0f
) },
249 { BackRight
, DEG2RAD( 135.0f
) }
251 static const struct ChanMap X51Map
[6] = {
252 { FrontLeft
, DEG2RAD( -30.0f
) },
253 { FrontRight
, DEG2RAD( 30.0f
) },
254 { FrontCenter
, DEG2RAD( 0.0f
) },
256 { BackLeft
, DEG2RAD(-110.0f
) },
257 { BackRight
, DEG2RAD( 110.0f
) }
259 static const struct ChanMap X61Map
[7] = {
260 { FrontLeft
, DEG2RAD(-30.0f
) },
261 { FrontRight
, DEG2RAD( 30.0f
) },
262 { FrontCenter
, DEG2RAD( 0.0f
) },
264 { BackCenter
, DEG2RAD(180.0f
) },
265 { SideLeft
, DEG2RAD(-90.0f
) },
266 { SideRight
, DEG2RAD( 90.0f
) }
268 static const struct ChanMap X71Map
[8] = {
269 { FrontLeft
, DEG2RAD( -30.0f
) },
270 { FrontRight
, DEG2RAD( 30.0f
) },
271 { FrontCenter
, DEG2RAD( 0.0f
) },
273 { BackLeft
, DEG2RAD(-150.0f
) },
274 { BackRight
, DEG2RAD( 150.0f
) },
275 { SideLeft
, DEG2RAD( -90.0f
) },
276 { SideRight
, DEG2RAD( 90.0f
) }
279 ALCdevice
*Device
= ALContext
->Device
;
280 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
281 ALbufferlistitem
*BufferListItem
;
282 enum FmtChannels Channels
;
283 ALfloat (*SrcMatrix
)[MaxChannels
];
284 ALfloat DryGain
, DryGainHF
;
285 ALfloat WetGain
[MAX_SENDS
];
286 ALfloat WetGainHF
[MAX_SENDS
];
287 ALint NumSends
, Frequency
;
288 const struct ChanMap
*chans
= NULL
;
289 enum Resampler Resampler
;
290 ALint num_channels
= 0;
291 ALboolean DirectChannels
;
292 ALfloat hwidth
= 0.0f
;
296 /* Get device properties */
297 NumSends
= Device
->NumAuxSends
;
298 Frequency
= Device
->Frequency
;
300 /* Get listener properties */
301 ListenerGain
= ALContext
->Listener
->Gain
;
303 /* Get source properties */
304 SourceVolume
= ALSource
->Gain
;
305 MinVolume
= ALSource
->MinGain
;
306 MaxVolume
= ALSource
->MaxGain
;
307 Pitch
= ALSource
->Pitch
;
308 Resampler
= ALSource
->Resampler
;
309 DirectChannels
= ALSource
->DirectChannels
;
311 /* Calculate the stepping value */
313 BufferListItem
= ALSource
->queue
;
314 while(BufferListItem
!= NULL
)
317 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
319 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
321 ALSource
->Params
.Step
= 10<<FRACTIONBITS
;
324 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
325 if(ALSource
->Params
.Step
== 0)
326 ALSource
->Params
.Step
= 1;
328 ALSource
->Params
.Resample
= SelectResampler(Resampler
, ALSource
->Params
.Step
);
330 Channels
= ALBuffer
->FmtChannels
;
333 BufferListItem
= BufferListItem
->next
;
335 if(!DirectChannels
&& Device
->Hrtf
)
336 ALSource
->Params
.DryMix
= SelectHrtfMixer();
338 ALSource
->Params
.DryMix
= SelectDirectMixer();
339 ALSource
->Params
.WetMix
= SelectSendMixer();
341 /* Calculate gains */
342 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
343 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
344 DryGainHF
= ALSource
->DirectGainHF
;
345 for(i
= 0;i
< NumSends
;i
++)
347 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
348 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
349 WetGainHF
[i
] = ALSource
->Send
[i
].GainHF
;
352 SrcMatrix
= ALSource
->Params
.Direct
.Gains
;
353 for(i
= 0;i
< MAX_INPUT_CHANNELS
;i
++)
355 for(c
= 0;c
< MaxChannels
;c
++)
356 SrcMatrix
[i
][c
] = 0.0f
;
366 if(!(Device
->Flags
&DEVICE_WIDE_STEREO
))
368 /* HACK: Place the stereo channels at +/-90 degrees when using non-
369 * HRTF stereo output. This helps reduce the "monoization" caused
370 * by them panning towards the center. */
371 if(Device
->FmtChans
== DevFmtStereo
&& !Device
->Hrtf
)
372 chans
= StereoWideMap
;
378 chans
= StereoWideMap
;
379 hwidth
= DEG2RAD(60.0f
);
410 if(DirectChannels
!= AL_FALSE
)
412 for(c
= 0;c
< num_channels
;c
++)
414 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
416 enum Channel chan
= Device
->Speaker2Chan
[i
];
417 if(chan
== chans
[c
].channel
)
419 SrcMatrix
[c
][chan
] = DryGain
;
425 else if(Device
->Hrtf
)
427 for(c
= 0;c
< num_channels
;c
++)
429 if(chans
[c
].channel
== LFE
)
432 ALSource
->Params
.Direct
.Hrtf
.Params
.Delay
[c
][0] = 0;
433 ALSource
->Params
.Direct
.Hrtf
.Params
.Delay
[c
][1] = 0;
434 for(i
= 0;i
< HRIR_LENGTH
;i
++)
436 ALSource
->Params
.Direct
.Hrtf
.Params
.Coeffs
[c
][i
][0] = 0.0f
;
437 ALSource
->Params
.Direct
.Hrtf
.Params
.Coeffs
[c
][i
][1] = 0.0f
;
442 /* Get the static HRIR coefficients and delays for this
444 GetLerpedHrtfCoeffs(Device
->Hrtf
,
445 0.0f
, chans
[c
].angle
, DryGain
,
446 ALSource
->Params
.Direct
.Hrtf
.Params
.Coeffs
[c
],
447 ALSource
->Params
.Direct
.Hrtf
.Params
.Delay
[c
]);
450 ALSource
->Hrtf
.Counter
= 0;
451 ALSource
->Params
.Direct
.Hrtf
.Params
.IrSize
= GetHrtfIrSize(Device
->Hrtf
);
453 ALSource
->Params
.Direct
.Hrtf
.State
= &ALSource
->Hrtf
;
457 DryGain
*= lerp(1.0f
, 1.0f
/sqrtf((float)Device
->NumChan
), hwidth
/F_PI
);
458 for(c
= 0;c
< num_channels
;c
++)
460 /* Special-case LFE */
461 if(chans
[c
].channel
== LFE
)
463 SrcMatrix
[c
][chans
[c
].channel
] = DryGain
;
466 ComputeAngleGains(Device
, chans
[c
].angle
, hwidth
, DryGain
,
471 ALSource
->Params
.Direct
.OutBuffer
= Device
->DryBuffer
;
472 ALSource
->Params
.Direct
.ClickRemoval
= Device
->ClickRemoval
;
473 ALSource
->Params
.Direct
.PendingClicks
= Device
->PendingClicks
;
474 for(i
= 0;i
< NumSends
;i
++)
476 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
478 Slot
= Device
->DefaultSlot
;
479 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
481 ALSource
->Params
.Send
[i
].OutBuffer
= NULL
;
482 ALSource
->Params
.Send
[i
].ClickRemoval
= NULL
;
483 ALSource
->Params
.Send
[i
].PendingClicks
= NULL
;
487 ALSource
->Params
.Send
[i
].OutBuffer
= Slot
->WetBuffer
;
488 ALSource
->Params
.Send
[i
].ClickRemoval
= Slot
->ClickRemoval
;
489 ALSource
->Params
.Send
[i
].PendingClicks
= Slot
->PendingClicks
;
491 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
495 ALfloat gain
= maxf(0.01f
, DryGainHF
);
496 for(c
= 0;c
< num_channels
;c
++)
497 ALfilterState_setParams(&ALSource
->Params
.Direct
.LpFilter
[c
],
498 ALfilterType_HighShelf
, gain
,
499 (ALfloat
)LOWPASSFREQREF
/Frequency
, 0.0f
);
501 for(i
= 0;i
< NumSends
;i
++)
503 ALfloat gain
= maxf(0.01f
, WetGainHF
[i
]);
504 for(c
= 0;c
< num_channels
;c
++)
505 ALfilterState_setParams(&ALSource
->Params
.Send
[i
].LpFilter
[c
],
506 ALfilterType_HighShelf
, gain
,
507 (ALfloat
)LOWPASSFREQREF
/Frequency
, 0.0f
);
511 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
513 ALCdevice
*Device
= ALContext
->Device
;
514 ALfloat Velocity
[3],Direction
[3],Position
[3],SourceToListener
[3];
515 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
516 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
517 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
518 ALfloat DopplerFactor
, SpeedOfSound
;
519 ALfloat AirAbsorptionFactor
;
520 ALfloat RoomAirAbsorption
[MAX_SENDS
];
521 ALbufferlistitem
*BufferListItem
;
523 ALfloat RoomAttenuation
[MAX_SENDS
];
524 ALfloat MetersPerUnit
;
525 ALfloat RoomRolloffBase
;
526 ALfloat RoomRolloff
[MAX_SENDS
];
527 ALfloat DecayDistance
[MAX_SENDS
];
530 ALboolean DryGainHFAuto
;
531 ALfloat WetGain
[MAX_SENDS
];
532 ALfloat WetGainHF
[MAX_SENDS
];
533 ALboolean WetGainAuto
;
534 ALboolean WetGainHFAuto
;
535 enum Resampler Resampler
;
542 for(i
= 0;i
< MAX_SENDS
;i
++)
545 /* Get context/device properties */
546 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
547 SpeedOfSound
= ALContext
->SpeedOfSound
* ALContext
->DopplerVelocity
;
548 NumSends
= Device
->NumAuxSends
;
549 Frequency
= Device
->Frequency
;
551 /* Get listener properties */
552 ListenerGain
= ALContext
->Listener
->Gain
;
553 MetersPerUnit
= ALContext
->Listener
->MetersPerUnit
;
555 /* Get source properties */
556 SourceVolume
= ALSource
->Gain
;
557 MinVolume
= ALSource
->MinGain
;
558 MaxVolume
= ALSource
->MaxGain
;
559 Pitch
= ALSource
->Pitch
;
560 Resampler
= ALSource
->Resampler
;
561 Position
[0] = ALSource
->Position
[0];
562 Position
[1] = ALSource
->Position
[1];
563 Position
[2] = ALSource
->Position
[2];
564 Direction
[0] = ALSource
->Orientation
[0];
565 Direction
[1] = ALSource
->Orientation
[1];
566 Direction
[2] = ALSource
->Orientation
[2];
567 Velocity
[0] = ALSource
->Velocity
[0];
568 Velocity
[1] = ALSource
->Velocity
[1];
569 Velocity
[2] = ALSource
->Velocity
[2];
570 MinDist
= ALSource
->RefDistance
;
571 MaxDist
= ALSource
->MaxDistance
;
572 Rolloff
= ALSource
->RollOffFactor
;
573 InnerAngle
= ALSource
->InnerAngle
;
574 OuterAngle
= ALSource
->OuterAngle
;
575 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
576 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
577 WetGainAuto
= ALSource
->WetGainAuto
;
578 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
579 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
581 ALSource
->Params
.Direct
.OutBuffer
= Device
->DryBuffer
;
582 ALSource
->Params
.Direct
.ClickRemoval
= Device
->ClickRemoval
;
583 ALSource
->Params
.Direct
.PendingClicks
= Device
->PendingClicks
;
584 for(i
= 0;i
< NumSends
;i
++)
586 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
589 Slot
= Device
->DefaultSlot
;
590 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
593 RoomRolloff
[i
] = 0.0f
;
594 DecayDistance
[i
] = 0.0f
;
595 RoomAirAbsorption
[i
] = 1.0f
;
597 else if(Slot
->AuxSendAuto
)
599 RoomRolloff
[i
] = RoomRolloffBase
;
600 if(IsReverbEffect(Slot
->EffectType
))
602 RoomRolloff
[i
] += Slot
->EffectProps
.Reverb
.RoomRolloffFactor
;
603 DecayDistance
[i
] = Slot
->EffectProps
.Reverb
.DecayTime
*
604 SPEEDOFSOUNDMETRESPERSEC
;
605 RoomAirAbsorption
[i
] = Slot
->EffectProps
.Reverb
.AirAbsorptionGainHF
;
609 DecayDistance
[i
] = 0.0f
;
610 RoomAirAbsorption
[i
] = 1.0f
;
615 /* If the slot's auxiliary send auto is off, the data sent to the
616 * effect slot is the same as the dry path, sans filter effects */
617 RoomRolloff
[i
] = Rolloff
;
618 DecayDistance
[i
] = 0.0f
;
619 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
622 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
624 ALSource
->Params
.Send
[i
].OutBuffer
= NULL
;
625 ALSource
->Params
.Send
[i
].ClickRemoval
= NULL
;
626 ALSource
->Params
.Send
[i
].PendingClicks
= NULL
;
630 ALSource
->Params
.Send
[i
].OutBuffer
= Slot
->WetBuffer
;
631 ALSource
->Params
.Send
[i
].ClickRemoval
= Slot
->ClickRemoval
;
632 ALSource
->Params
.Send
[i
].PendingClicks
= Slot
->PendingClicks
;
636 /* Transform source to listener space (convert to head relative) */
637 if(ALSource
->HeadRelative
== AL_FALSE
)
639 ALfloat (*restrict Matrix
)[4] = ALContext
->Listener
->Params
.Matrix
;
640 /* Transform source vectors */
641 aluMatrixVector(Position
, 1.0f
, Matrix
);
642 aluMatrixVector(Direction
, 0.0f
, Matrix
);
643 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
647 const ALfloat
*ListenerVel
= ALContext
->Listener
->Params
.Velocity
;
648 /* Offset the source velocity to be relative of the listener velocity */
649 Velocity
[0] += ListenerVel
[0];
650 Velocity
[1] += ListenerVel
[1];
651 Velocity
[2] += ListenerVel
[2];
654 SourceToListener
[0] = -Position
[0];
655 SourceToListener
[1] = -Position
[1];
656 SourceToListener
[2] = -Position
[2];
657 aluNormalize(SourceToListener
);
658 aluNormalize(Direction
);
660 /* Calculate distance attenuation */
661 Distance
= sqrtf(aluDotproduct(Position
, Position
));
662 ClampedDist
= Distance
;
665 for(i
= 0;i
< NumSends
;i
++)
666 RoomAttenuation
[i
] = 1.0f
;
667 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
668 ALContext
->DistanceModel
)
670 case InverseDistanceClamped
:
671 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
672 if(MaxDist
< MinDist
)
675 case InverseDistance
:
678 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
679 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
680 for(i
= 0;i
< NumSends
;i
++)
682 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
683 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
688 case LinearDistanceClamped
:
689 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
690 if(MaxDist
< MinDist
)
694 if(MaxDist
!= MinDist
)
696 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
697 Attenuation
= maxf(Attenuation
, 0.0f
);
698 for(i
= 0;i
< NumSends
;i
++)
700 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
701 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
706 case ExponentDistanceClamped
:
707 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
708 if(MaxDist
< MinDist
)
711 case ExponentDistance
:
712 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
714 Attenuation
= powf(ClampedDist
/MinDist
, -Rolloff
);
715 for(i
= 0;i
< NumSends
;i
++)
716 RoomAttenuation
[i
] = powf(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
720 case DisableDistance
:
721 ClampedDist
= MinDist
;
725 /* Source Gain + Attenuation */
726 DryGain
= SourceVolume
* Attenuation
;
727 for(i
= 0;i
< NumSends
;i
++)
728 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
730 /* Distance-based air absorption */
731 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
733 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
734 DryGainHF
*= powf(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
735 for(i
= 0;i
< NumSends
;i
++)
736 WetGainHF
[i
] *= powf(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
741 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
743 /* Apply a decay-time transformation to the wet path, based on the
744 * attenuation of the dry path.
746 * Using the apparent distance, based on the distance attenuation, the
747 * initial decay of the reverb effect is calculated and applied to the
750 for(i
= 0;i
< NumSends
;i
++)
752 if(DecayDistance
[i
] > 0.0f
)
753 WetGain
[i
] *= powf(0.001f
/*-60dB*/, ApparentDist
/DecayDistance
[i
]);
757 /* Calculate directional soundcones */
758 Angle
= RAD2DEG(acosf(aluDotproduct(Direction
,SourceToListener
)) * ConeScale
) * 2.0f
;
759 if(Angle
> InnerAngle
&& Angle
<= OuterAngle
)
761 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
762 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
763 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
765 else if(Angle
> OuterAngle
)
767 ConeVolume
= ALSource
->OuterGain
;
768 ConeHF
= ALSource
->OuterGainHF
;
776 DryGain
*= ConeVolume
;
779 for(i
= 0;i
< NumSends
;i
++)
780 WetGain
[i
] *= ConeVolume
;
786 for(i
= 0;i
< NumSends
;i
++)
787 WetGainHF
[i
] *= ConeHF
;
790 /* Clamp to Min/Max Gain */
791 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
792 for(i
= 0;i
< NumSends
;i
++)
793 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
795 /* Apply gain and frequency filters */
796 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
797 DryGainHF
*= ALSource
->DirectGainHF
;
798 for(i
= 0;i
< NumSends
;i
++)
800 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
801 WetGainHF
[i
] *= ALSource
->Send
[i
].GainHF
;
804 /* Calculate velocity-based doppler effect */
805 if(DopplerFactor
> 0.0f
)
807 const ALfloat
*ListenerVel
= ALContext
->Listener
->Params
.Velocity
;
810 if(SpeedOfSound
< 1.0f
)
812 DopplerFactor
*= 1.0f
/SpeedOfSound
;
816 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
817 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
819 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
820 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
823 BufferListItem
= ALSource
->queue
;
824 while(BufferListItem
!= NULL
)
827 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
829 /* Calculate fixed-point stepping value, based on the pitch, buffer
830 * frequency, and output frequency. */
831 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
833 ALSource
->Params
.Step
= 10<<FRACTIONBITS
;
836 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
837 if(ALSource
->Params
.Step
== 0)
838 ALSource
->Params
.Step
= 1;
840 ALSource
->Params
.Resample
= SelectResampler(Resampler
, ALSource
->Params
.Step
);
844 BufferListItem
= BufferListItem
->next
;
847 ALSource
->Params
.DryMix
= SelectHrtfMixer();
849 ALSource
->Params
.DryMix
= SelectDirectMixer();
850 ALSource
->Params
.WetMix
= SelectSendMixer();
854 /* Use a binaural HRTF algorithm for stereo headphone playback */
855 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
857 if(Distance
> FLT_EPSILON
)
859 ALfloat invlen
= 1.0f
/Distance
;
860 Position
[0] *= invlen
;
861 Position
[1] *= invlen
;
862 Position
[2] *= invlen
;
864 /* Calculate elevation and azimuth only when the source is not at
865 * the listener. This prevents +0 and -0 Z from producing
866 * inconsistent panning. Also, clamp Y in case FP precision errors
867 * cause it to land outside of -1..+1. */
868 ev
= asinf(clampf(Position
[1], -1.0f
, 1.0f
));
869 az
= atan2f(Position
[0], -Position
[2]*ZScale
);
872 /* Check to see if the HRIR is already moving. */
873 if(ALSource
->Hrtf
.Moving
)
875 /* Calculate the normalized HRTF transition factor (delta). */
876 delta
= CalcHrtfDelta(ALSource
->Params
.Direct
.Hrtf
.Params
.Gain
, DryGain
,
877 ALSource
->Params
.Direct
.Hrtf
.Params
.Dir
, Position
);
878 /* If the delta is large enough, get the moving HRIR target
879 * coefficients, target delays, steppping values, and counter. */
882 ALSource
->Hrtf
.Counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
883 ev
, az
, DryGain
, delta
,
884 ALSource
->Hrtf
.Counter
,
885 ALSource
->Params
.Direct
.Hrtf
.Params
.Coeffs
[0],
886 ALSource
->Params
.Direct
.Hrtf
.Params
.Delay
[0],
887 ALSource
->Params
.Direct
.Hrtf
.Params
.CoeffStep
,
888 ALSource
->Params
.Direct
.Hrtf
.Params
.DelayStep
);
889 ALSource
->Params
.Direct
.Hrtf
.Params
.Gain
= DryGain
;
890 ALSource
->Params
.Direct
.Hrtf
.Params
.Dir
[0] = Position
[0];
891 ALSource
->Params
.Direct
.Hrtf
.Params
.Dir
[1] = Position
[1];
892 ALSource
->Params
.Direct
.Hrtf
.Params
.Dir
[2] = Position
[2];
897 /* Get the initial (static) HRIR coefficients and delays. */
898 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
899 ALSource
->Params
.Direct
.Hrtf
.Params
.Coeffs
[0],
900 ALSource
->Params
.Direct
.Hrtf
.Params
.Delay
[0]);
901 ALSource
->Hrtf
.Counter
= 0;
902 ALSource
->Hrtf
.Moving
= AL_TRUE
;
903 ALSource
->Params
.Direct
.Hrtf
.Params
.Gain
= DryGain
;
904 ALSource
->Params
.Direct
.Hrtf
.Params
.Dir
[0] = Position
[0];
905 ALSource
->Params
.Direct
.Hrtf
.Params
.Dir
[1] = Position
[1];
906 ALSource
->Params
.Direct
.Hrtf
.Params
.Dir
[2] = Position
[2];
908 ALSource
->Params
.Direct
.Hrtf
.Params
.IrSize
= GetHrtfIrSize(Device
->Hrtf
);
910 ALSource
->Params
.Direct
.Hrtf
.State
= &ALSource
->Hrtf
;
914 ALfloat (*Matrix
)[MaxChannels
] = ALSource
->Params
.Direct
.Gains
;
915 ALfloat DirGain
= 0.0f
;
918 for(i
= 0;i
< MAX_INPUT_CHANNELS
;i
++)
920 for(j
= 0;j
< MaxChannels
;j
++)
924 /* Normalize the length, and compute panned gains. */
925 if(Distance
> FLT_EPSILON
)
927 ALfloat invlen
= 1.0f
/Distance
;
928 Position
[0] *= invlen
;
929 Position
[1] *= invlen
;
930 Position
[2] *= invlen
;
932 DirGain
= sqrtf(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
933 ComputeAngleGains(Device
, atan2f(Position
[0], -Position
[2]*ZScale
), 0.0f
,
934 DryGain
*DirGain
, Matrix
[0]);
937 /* Adjustment for vertical offsets. Not the greatest, but simple
939 AmbientGain
= DryGain
* sqrtf(1.0f
/Device
->NumChan
) * (1.0f
-DirGain
);
940 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
942 enum Channel chan
= Device
->Speaker2Chan
[i
];
943 Matrix
[0][chan
] = maxf(Matrix
[0][chan
], AmbientGain
);
946 for(i
= 0;i
< NumSends
;i
++)
947 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
951 ALfloat gain
= maxf(0.01f
, DryGainHF
);
952 ALfilterState_setParams(&ALSource
->Params
.Direct
.LpFilter
[0],
953 ALfilterType_HighShelf
, gain
,
954 (ALfloat
)LOWPASSFREQREF
/Frequency
, 0.0f
);
956 for(i
= 0;i
< NumSends
;i
++)
958 ALfloat gain
= maxf(0.01f
, WetGainHF
[i
]);
959 ALfilterState_setParams(&ALSource
->Params
.Send
[i
].LpFilter
[0],
960 ALfilterType_HighShelf
, gain
,
961 (ALfloat
)LOWPASSFREQREF
/Frequency
, 0.0f
);
966 static inline ALint
aluF2I25(ALfloat val
)
968 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
969 if(fabsf(val
) > 1.0f
)
970 val
= (ALfloat
)((0.0f
< val
) - (val
< 0.0f
));
971 /* Convert to a signed integer, between -16777215 and +16777215. */
972 return fastf2i(val
*16777215.0f
);
975 static inline ALfloat
aluF2F(ALfloat val
)
977 static inline ALint
aluF2I(ALfloat val
)
978 { return aluF2I25(val
)<<7; }
979 static inline ALuint
aluF2UI(ALfloat val
)
980 { return aluF2I(val
)+2147483648u; }
981 static inline ALshort
aluF2S(ALfloat val
)
982 { return aluF2I25(val
)>>9; }
983 static inline ALushort
aluF2US(ALfloat val
)
984 { return aluF2S(val
)+32768; }
985 static inline ALbyte
aluF2B(ALfloat val
)
986 { return aluF2I25(val
)>>17; }
987 static inline ALubyte
aluF2UB(ALfloat val
)
988 { return aluF2B(val
)+128; }
990 #define DECL_TEMPLATE(T, func) \
991 static int Write_##T(ALCdevice *device, T *restrict buffer, \
992 ALuint SamplesToDo) \
994 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
995 ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
996 const ALuint *offsets = device->ChannelOffsets; \
999 for(j = 0;j < MaxChannels;j++) \
1003 if(offsets[j] == INVALID_OFFSET) \
1006 out = buffer + offsets[j]; \
1007 for(i = 0;i < SamplesToDo;i++) \
1008 out[i*numchans] = func(DryBuffer[j][i]); \
1010 return SamplesToDo*numchans*sizeof(T); \
1013 DECL_TEMPLATE(ALfloat
, aluF2F
)
1014 DECL_TEMPLATE(ALuint
, aluF2UI
)
1015 DECL_TEMPLATE(ALint
, aluF2I
)
1016 DECL_TEMPLATE(ALushort
, aluF2US
)
1017 DECL_TEMPLATE(ALshort
, aluF2S
)
1018 DECL_TEMPLATE(ALubyte
, aluF2UB
)
1019 DECL_TEMPLATE(ALbyte
, aluF2B
)
1021 #undef DECL_TEMPLATE
1024 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1027 ALeffectslot
**slot
, **slot_end
;
1028 ALsource
**src
, **src_end
;
1033 SetMixerFPUMode(&oldMode
);
1037 SamplesToDo
= minu(size
, BUFFERSIZE
);
1038 for(c
= 0;c
< MaxChannels
;c
++)
1039 memset(device
->DryBuffer
[c
], 0, SamplesToDo
*sizeof(ALfloat
));
1041 ALCdevice_Lock(device
);
1042 V(device
->Synth
,process
)(SamplesToDo
, device
->DryBuffer
);
1044 ctx
= device
->ContextList
;
1047 ALenum DeferUpdates
= ctx
->DeferUpdates
;
1048 ALenum UpdateSources
= AL_FALSE
;
1051 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
1054 CalcListenerParams(ctx
->Listener
);
1056 /* source processing */
1057 src
= ctx
->ActiveSources
;
1058 src_end
= src
+ ctx
->ActiveSourceCount
;
1059 while(src
!= src_end
)
1061 if((*src
)->state
!= AL_PLAYING
)
1063 --(ctx
->ActiveSourceCount
);
1064 *src
= *(--src_end
);
1068 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
1070 ALsource_Update(*src
, ctx
);
1072 MixSource(*src
, device
, SamplesToDo
);
1076 /* effect slot processing */
1077 slot
= ctx
->ActiveEffectSlots
;
1078 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
1079 while(slot
!= slot_end
)
1081 ALfloat offset
= (*slot
)->ClickRemoval
[0];
1082 if(offset
< (1.0f
/32768.0f
))
1084 else for(i
= 0;i
< SamplesToDo
;i
++)
1086 (*slot
)->WetBuffer
[0][i
] += offset
;
1087 offset
-= offset
* (1.0f
/256.0f
);
1089 (*slot
)->ClickRemoval
[0] = offset
+ (*slot
)->PendingClicks
[0];
1090 (*slot
)->PendingClicks
[0] = 0.0f
;
1092 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1093 V((*slot
)->EffectState
,update
)(device
, *slot
);
1095 V((*slot
)->EffectState
,process
)(SamplesToDo
, (*slot
)->WetBuffer
[0],
1098 for(i
= 0;i
< SamplesToDo
;i
++)
1099 (*slot
)->WetBuffer
[0][i
] = 0.0f
;
1107 slot
= &device
->DefaultSlot
;
1110 ALfloat offset
= (*slot
)->ClickRemoval
[0];
1111 if(offset
< (1.0f
/32768.0f
))
1113 else for(i
= 0;i
< SamplesToDo
;i
++)
1115 (*slot
)->WetBuffer
[0][i
] += offset
;
1116 offset
-= offset
* (1.0f
/256.0f
);
1118 (*slot
)->ClickRemoval
[0] = offset
+ (*slot
)->PendingClicks
[0];
1119 (*slot
)->PendingClicks
[0] = 0.0f
;
1121 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1122 V((*slot
)->EffectState
,update
)(device
, *slot
);
1124 V((*slot
)->EffectState
,process
)(SamplesToDo
, (*slot
)->WetBuffer
[0],
1127 for(i
= 0;i
< SamplesToDo
;i
++)
1128 (*slot
)->WetBuffer
[0][i
] = 0.0f
;
1131 /* Increment the clock time. Every second's worth of samples is
1132 * converted and added to clock base so that large sample counts don't
1133 * overflow during conversion. This also guarantees an exact, stable
1135 device
->SamplesDone
+= SamplesToDo
;
1136 device
->ClockBase
+= (device
->SamplesDone
/device
->Frequency
) * DEVICE_CLOCK_RES
;
1137 device
->SamplesDone
%= device
->Frequency
;
1138 ALCdevice_Unlock(device
);
1140 /* Click-removal. Could do better; this only really handles immediate
1141 * changes between updates where a predictive sample could be
1142 * generated. Delays caused by effects and HRTF aren't caught. */
1143 if(device
->FmtChans
== DevFmtStereo
)
1145 /* Assumes the first two channels are FrontLeft and FrontRight */
1146 for(c
= 0;c
< 2;c
++)
1148 ALfloat offset
= device
->ClickRemoval
[c
];
1149 if(offset
< (1.0f
/32768.0f
))
1151 else for(i
= 0;i
< SamplesToDo
;i
++)
1153 device
->DryBuffer
[c
][i
] += offset
;
1154 offset
-= offset
* (1.0f
/256.0f
);
1156 device
->ClickRemoval
[c
] = offset
+ device
->PendingClicks
[c
];
1157 device
->PendingClicks
[c
] = 0.0f
;
1162 for(i
= 0;i
< SamplesToDo
;i
++)
1164 samples
[0] = device
->DryBuffer
[FrontLeft
][i
];
1165 samples
[1] = device
->DryBuffer
[FrontRight
][i
];
1166 bs2b_cross_feed(device
->Bs2b
, samples
);
1167 device
->DryBuffer
[FrontLeft
][i
] = samples
[0];
1168 device
->DryBuffer
[FrontRight
][i
] = samples
[1];
1174 for(c
= 0;c
< MaxChannels
;c
++)
1176 ALfloat offset
= device
->ClickRemoval
[c
];
1177 if(offset
< (1.0f
/32768.0f
))
1179 else for(i
= 0;i
< SamplesToDo
;i
++)
1181 device
->DryBuffer
[c
][i
] += offset
;
1182 offset
-= offset
* (1.0f
/256.0f
);
1184 device
->ClickRemoval
[c
] = offset
+ device
->PendingClicks
[c
];
1185 device
->PendingClicks
[c
] = 0.0f
;
1192 switch(device
->FmtType
)
1195 bytes
= Write_ALbyte(device
, buffer
, SamplesToDo
);
1198 bytes
= Write_ALubyte(device
, buffer
, SamplesToDo
);
1201 bytes
= Write_ALshort(device
, buffer
, SamplesToDo
);
1204 bytes
= Write_ALushort(device
, buffer
, SamplesToDo
);
1207 bytes
= Write_ALint(device
, buffer
, SamplesToDo
);
1210 bytes
= Write_ALuint(device
, buffer
, SamplesToDo
);
1213 bytes
= Write_ALfloat(device
, buffer
, SamplesToDo
);
1217 buffer
= (ALubyte
*)buffer
+ bytes
;
1220 size
-= SamplesToDo
;
1223 RestoreFPUMode(&oldMode
);
1227 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1229 ALCcontext
*Context
;
1231 device
->Connected
= ALC_FALSE
;
1233 Context
= device
->ContextList
;
1236 ALsource
**src
, **src_end
;
1238 src
= Context
->ActiveSources
;
1239 src_end
= src
+ Context
->ActiveSourceCount
;
1240 while(src
!= src_end
)
1242 if((*src
)->state
== AL_PLAYING
)
1244 (*src
)->state
= AL_STOPPED
;
1245 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1246 (*src
)->position
= 0;
1247 (*src
)->position_fraction
= 0;
1251 Context
->ActiveSourceCount
= 0;
1253 Context
= Context
->next
;