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
34 #include "alListener.h"
35 #include "alAuxEffectSlot.h"
40 static __inline ALvoid
aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
42 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
43 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
44 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
47 static __inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
49 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
50 inVector1
[2]*inVector2
[2];
53 static __inline ALvoid
aluNormalize(ALfloat
*inVector
)
55 ALfloat length
, inverse_length
;
57 length
= aluSqrt(aluDotproduct(inVector
, inVector
));
60 inverse_length
= 1.0f
/length
;
61 inVector
[0] *= inverse_length
;
62 inVector
[1] *= inverse_length
;
63 inVector
[2] *= inverse_length
;
67 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
70 vector
[0], vector
[1], vector
[2], w
73 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
74 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
75 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
79 ALvoid
CalcNonAttnSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
81 static const ALfloat angles_Mono
[1] = { 0.0f
};
82 static const ALfloat angles_Stereo
[2] = { -30.0f
, 30.0f
};
83 static const ALfloat angles_Rear
[2] = { -150.0f
, 150.0f
};
84 static const ALfloat angles_Quad
[4] = { -45.0f
, 45.0f
, -135.0f
, 135.0f
};
85 static const ALfloat angles_X51
[6] = { -30.0f
, 30.0f
, 0.0f
, 0.0f
,
87 static const ALfloat angles_X61
[7] = { -30.0f
, 30.0f
, 0.0f
, 0.0f
,
88 180.0f
, -90.0f
, 90.0f
};
89 static const ALfloat angles_X71
[8] = { -30.0f
, 30.0f
, 0.0f
, 0.0f
,
90 -110.0f
, 110.0f
, -90.0f
, 90.0f
};
92 static const enum Channel chans_Mono
[1] = { FRONT_CENTER
};
93 static const enum Channel chans_Stereo
[2] = { FRONT_LEFT
, FRONT_RIGHT
};
94 static const enum Channel chans_Rear
[2] = { BACK_LEFT
, BACK_RIGHT
};
95 static const enum Channel chans_Quad
[4] = { FRONT_LEFT
, FRONT_RIGHT
,
96 BACK_LEFT
, BACK_RIGHT
};
97 static const enum Channel chans_X51
[6] = { FRONT_LEFT
, FRONT_RIGHT
,
99 BACK_LEFT
, BACK_RIGHT
};
100 static const enum Channel chans_X61
[7] = { FRONT_LEFT
, FRONT_RIGHT
,
101 FRONT_CENTER
, LFE
, BACK_CENTER
,
102 SIDE_LEFT
, SIDE_RIGHT
};
103 static const enum Channel chans_X71
[8] = { FRONT_LEFT
, FRONT_RIGHT
,
105 BACK_LEFT
, BACK_RIGHT
,
106 SIDE_LEFT
, SIDE_RIGHT
};
108 ALCdevice
*Device
= ALContext
->Device
;
109 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
110 ALbufferlistitem
*BufferListItem
;
111 enum DevFmtChannels DevChans
;
112 enum FmtChannels Channels
;
113 ALfloat (*SrcMatrix
)[MAXCHANNELS
];
114 ALfloat DryGain
, DryGainHF
;
115 ALfloat WetGain
[MAX_SENDS
];
116 ALfloat WetGainHF
[MAX_SENDS
];
117 ALint NumSends
, Frequency
;
118 const ALfloat
*SpeakerGain
;
119 const ALfloat
*angles
= NULL
;
120 const enum Channel
*chans
= NULL
;
121 enum Resampler Resampler
;
122 ALint num_channels
= 0;
123 ALboolean VirtualChannels
;
129 /* Get device properties */
130 DevChans
= ALContext
->Device
->FmtChans
;
131 NumSends
= ALContext
->Device
->NumAuxSends
;
132 Frequency
= ALContext
->Device
->Frequency
;
134 /* Get listener properties */
135 ListenerGain
= ALContext
->Listener
.Gain
;
137 /* Get source properties */
138 SourceVolume
= ALSource
->flGain
;
139 MinVolume
= ALSource
->flMinGain
;
140 MaxVolume
= ALSource
->flMaxGain
;
141 Pitch
= ALSource
->flPitch
;
142 Resampler
= ALSource
->Resampler
;
143 VirtualChannels
= ALSource
->VirtualChannels
;
145 /* Calculate the stepping value */
147 BufferListItem
= ALSource
->queue
;
148 while(BufferListItem
!= NULL
)
151 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
153 ALint maxstep
= STACK_DATA_SIZE
/ ALSource
->NumChannels
/
154 ALSource
->SampleSize
;
155 maxstep
-= ResamplerPadding
[Resampler
] +
156 ResamplerPrePadding
[Resampler
] + 1;
157 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
159 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
160 if(Pitch
> (ALfloat
)maxstep
)
161 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
164 ALSource
->Params
.Step
= (ALint
)(Pitch
*FRACTIONONE
);
165 if(ALSource
->Params
.Step
== 0)
166 ALSource
->Params
.Step
= 1;
169 Channels
= ALBuffer
->FmtChannels
;
171 if(ALSource
->VirtualChannels
&& (Device
->Flags
&DEVICE_USE_HRTF
))
172 ALSource
->Params
.DoMix
= SelectHrtfMixer(ALBuffer
,
173 (ALSource
->Params
.Step
==FRACTIONONE
) ? POINT_RESAMPLER
:
176 ALSource
->Params
.DoMix
= SelectMixer(ALBuffer
,
177 (ALSource
->Params
.Step
==FRACTIONONE
) ? POINT_RESAMPLER
:
181 BufferListItem
= BufferListItem
->next
;
184 /* Calculate gains */
185 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
186 DryGain
*= ALSource
->DirectGain
;
187 DryGainHF
= ALSource
->DirectGainHF
;
188 for(i
= 0;i
< NumSends
;i
++)
190 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
191 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
;
192 WetGainHF
[i
] = ALSource
->Send
[i
].WetGainHF
;
195 SrcMatrix
= ALSource
->Params
.DryGains
;
196 for(i
= 0;i
< MAXCHANNELS
;i
++)
198 for(c
= 0;c
< MAXCHANNELS
;c
++)
199 SrcMatrix
[i
][c
] = 0.0f
;
204 angles
= angles_Mono
;
209 if(VirtualChannels
&& (ALContext
->Device
->Flags
&DEVICE_DUPLICATE_STEREO
))
211 DryGain
*= aluSqrt(2.0f
/4.0f
);
214 pos
= aluCart2LUTpos(aluCos(F_PI
/180.0f
* angles_Rear
[c
]),
215 aluSin(F_PI
/180.0f
* angles_Rear
[c
]));
216 SpeakerGain
= Device
->PanningLUT
[pos
];
218 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
220 enum Channel chan
= Device
->Speaker2Chan
[i
];
221 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
226 angles
= angles_Stereo
;
227 chans
= chans_Stereo
;
232 angles
= angles_Rear
;
238 angles
= angles_Quad
;
262 if(VirtualChannels
== AL_FALSE
)
264 for(c
= 0;c
< num_channels
;c
++)
265 SrcMatrix
[c
][chans
[c
]] += DryGain
* ListenerGain
;
267 else if((Device
->Flags
&DEVICE_USE_HRTF
))
269 for(c
= 0;c
< num_channels
;c
++)
274 ALSource
->Params
.HrtfDelay
[c
][0] = 0;
275 ALSource
->Params
.HrtfDelay
[c
][1] = 0;
276 for(i
= 0;i
< HRIR_LENGTH
;i
++)
278 ALSource
->Params
.HrtfCoeffs
[c
][i
][0] = 0.0f
;
279 ALSource
->Params
.HrtfCoeffs
[c
][i
][1] = 0.0f
;
284 /* Get the static HRIR coefficients and delays for this
286 GetLerpedHrtfCoeffs(ALContext
->Device
->Hrtf
,
287 0.0f
, F_PI
/180.0f
* angles
[c
],
288 DryGain
*ListenerGain
,
289 ALSource
->Params
.HrtfCoeffs
[c
],
290 ALSource
->Params
.HrtfDelay
[c
]);
292 ALSource
->HrtfCounter
= 0;
297 for(c
= 0;c
< num_channels
;c
++)
299 if(chans
[c
] == LFE
) /* Special-case LFE */
301 SrcMatrix
[c
][LFE
] += DryGain
* ListenerGain
;
304 pos
= aluCart2LUTpos(aluCos(F_PI
/180.0f
* angles
[c
]),
305 aluSin(F_PI
/180.0f
* angles
[c
]));
306 SpeakerGain
= Device
->PanningLUT
[pos
];
308 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
310 enum Channel chan
= Device
->Speaker2Chan
[i
];
311 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
316 for(i
= 0;i
< NumSends
;i
++)
318 ALSource
->Params
.Send
[i
].Slot
= ALSource
->Send
[i
].Slot
;
319 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
] * ListenerGain
;
322 /* Update filter coefficients. Calculations based on the I3DL2
324 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQCUTOFF
/ Frequency
);
326 /* We use two chained one-pole filters, so we need to take the
327 * square root of the squared gain, which is the same as the base
329 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
330 for(i
= 0;i
< NumSends
;i
++)
332 /* We use a one-pole filter, so we need to take the squared gain */
333 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
334 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
338 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
340 const ALCdevice
*Device
= ALContext
->Device
;
341 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
342 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
343 ALfloat Velocity
[3],ListenerVel
[3];
344 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
345 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
346 ALfloat DopplerFactor
, DopplerVelocity
, SpeedOfSound
;
347 ALfloat AirAbsorptionFactor
;
348 ALfloat RoomAirAbsorption
[MAX_SENDS
];
349 ALbufferlistitem
*BufferListItem
;
350 ALfloat Attenuation
, EffectiveDist
;
351 ALfloat RoomAttenuation
[MAX_SENDS
];
352 ALfloat MetersPerUnit
;
353 ALfloat RoomRolloffBase
;
354 ALfloat RoomRolloff
[MAX_SENDS
];
355 ALfloat DecayDistance
[MAX_SENDS
];
358 ALboolean DryGainHFAuto
;
359 ALfloat WetGain
[MAX_SENDS
];
360 ALfloat WetGainHF
[MAX_SENDS
];
361 ALboolean WetGainAuto
;
362 ALboolean WetGainHFAuto
;
363 enum Resampler Resampler
;
371 for(i
= 0;i
< MAX_SENDS
;i
++)
374 //Get context properties
375 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
376 DopplerVelocity
= ALContext
->DopplerVelocity
;
377 SpeedOfSound
= ALContext
->flSpeedOfSound
;
378 NumSends
= Device
->NumAuxSends
;
379 Frequency
= Device
->Frequency
;
381 //Get listener properties
382 ListenerGain
= ALContext
->Listener
.Gain
;
383 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
384 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
385 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
386 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
388 //Get source properties
389 SourceVolume
= ALSource
->flGain
;
390 MinVolume
= ALSource
->flMinGain
;
391 MaxVolume
= ALSource
->flMaxGain
;
392 Pitch
= ALSource
->flPitch
;
393 Resampler
= ALSource
->Resampler
;
394 Position
[0] = ALSource
->vPosition
[0];
395 Position
[1] = ALSource
->vPosition
[1];
396 Position
[2] = ALSource
->vPosition
[2];
397 Direction
[0] = ALSource
->vOrientation
[0];
398 Direction
[1] = ALSource
->vOrientation
[1];
399 Direction
[2] = ALSource
->vOrientation
[2];
400 Velocity
[0] = ALSource
->vVelocity
[0];
401 Velocity
[1] = ALSource
->vVelocity
[1];
402 Velocity
[2] = ALSource
->vVelocity
[2];
403 MinDist
= ALSource
->flRefDistance
;
404 MaxDist
= ALSource
->flMaxDistance
;
405 Rolloff
= ALSource
->flRollOffFactor
;
406 InnerAngle
= ALSource
->flInnerAngle
* ConeScale
;
407 OuterAngle
= ALSource
->flOuterAngle
* ConeScale
;
408 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
409 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
410 WetGainAuto
= ALSource
->WetGainAuto
;
411 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
412 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
413 for(i
= 0;i
< NumSends
;i
++)
415 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
417 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
419 RoomRolloff
[i
] = 0.0f
;
420 DecayDistance
[i
] = 0.0f
;
421 RoomAirAbsorption
[i
] = 1.0f
;
423 else if(Slot
->AuxSendAuto
)
425 RoomRolloff
[i
] = RoomRolloffBase
;
426 if(IsReverbEffect(Slot
->effect
.type
))
428 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
429 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
430 SPEEDOFSOUNDMETRESPERSEC
;
431 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
435 DecayDistance
[i
] = 0.0f
;
436 RoomAirAbsorption
[i
] = 1.0f
;
441 /* If the slot's auxiliary send auto is off, the data sent to the
442 * effect slot is the same as the dry path, sans filter effects */
443 RoomRolloff
[i
] = Rolloff
;
444 DecayDistance
[i
] = 0.0f
;
445 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
448 ALSource
->Params
.Send
[i
].Slot
= Slot
;
451 //1. Translate Listener to origin (convert to head relative)
452 if(ALSource
->bHeadRelative
== AL_FALSE
)
454 ALfloat U
[3],V
[3],N
[3];
455 ALfloat Matrix
[4][4];
457 // Build transform matrix
458 N
[0] = ALContext
->Listener
.Forward
[0]; // At-vector
459 N
[1] = ALContext
->Listener
.Forward
[1];
460 N
[2] = ALContext
->Listener
.Forward
[2];
461 aluNormalize(N
); // Normalized At-vector
462 V
[0] = ALContext
->Listener
.Up
[0]; // Up-vector
463 V
[1] = ALContext
->Listener
.Up
[1];
464 V
[2] = ALContext
->Listener
.Up
[2];
465 aluNormalize(V
); // Normalized Up-vector
466 aluCrossproduct(N
, V
, U
); // Right-vector
467 aluNormalize(U
); // Normalized Right-vector
468 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0]; Matrix
[0][3] = 0.0f
;
469 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1]; Matrix
[1][3] = 0.0f
;
470 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2]; Matrix
[2][3] = 0.0f
;
471 Matrix
[3][0] = 0.0f
; Matrix
[3][1] = 0.0f
; Matrix
[3][2] = 0.0f
; Matrix
[3][3] = 1.0f
;
473 // Translate position
474 Position
[0] -= ALContext
->Listener
.Position
[0];
475 Position
[1] -= ALContext
->Listener
.Position
[1];
476 Position
[2] -= ALContext
->Listener
.Position
[2];
478 // Transform source position and direction into listener space
479 aluMatrixVector(Position
, 1.0f
, Matrix
);
480 aluMatrixVector(Direction
, 0.0f
, Matrix
);
481 // Transform source and listener velocity into listener space
482 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
483 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
486 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
488 SourceToListener
[0] = -Position
[0];
489 SourceToListener
[1] = -Position
[1];
490 SourceToListener
[2] = -Position
[2];
491 aluNormalize(SourceToListener
);
492 aluNormalize(Direction
);
494 //2. Calculate distance attenuation
495 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
496 ClampedDist
= Distance
;
499 for(i
= 0;i
< NumSends
;i
++)
500 RoomAttenuation
[i
] = 1.0f
;
501 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
502 ALContext
->DistanceModel
)
504 case InverseDistanceClamped
:
505 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
506 if(MaxDist
< MinDist
)
509 case InverseDistance
:
512 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
513 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
514 for(i
= 0;i
< NumSends
;i
++)
516 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
517 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
522 case LinearDistanceClamped
:
523 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
524 if(MaxDist
< MinDist
)
528 if(MaxDist
!= MinDist
)
530 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
531 Attenuation
= maxf(Attenuation
, 0.0f
);
532 for(i
= 0;i
< NumSends
;i
++)
534 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
535 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
540 case ExponentDistanceClamped
:
541 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
542 if(MaxDist
< MinDist
)
545 case ExponentDistance
:
546 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
548 Attenuation
= aluPow(ClampedDist
/MinDist
, -Rolloff
);
549 for(i
= 0;i
< NumSends
;i
++)
550 RoomAttenuation
[i
] = aluPow(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
554 case DisableDistance
:
558 // Source Gain + Attenuation
559 DryGain
= SourceVolume
* Attenuation
;
560 for(i
= 0;i
< NumSends
;i
++)
561 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
563 // Distance-based air absorption
564 EffectiveDist
= 0.0f
;
565 if(MinDist
> 0.0f
&& Attenuation
< 1.0f
)
566 EffectiveDist
= (MinDist
/Attenuation
- MinDist
)*MetersPerUnit
;
567 if(AirAbsorptionFactor
> 0.0f
&& EffectiveDist
> 0.0f
)
569 DryGainHF
*= aluPow(AIRABSORBGAINHF
, AirAbsorptionFactor
*EffectiveDist
);
570 for(i
= 0;i
< NumSends
;i
++)
571 WetGainHF
[i
] *= aluPow(RoomAirAbsorption
[i
],
572 AirAbsorptionFactor
*EffectiveDist
);
575 //3. Apply directional soundcones
576 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * (180.0f
/F_PI
);
577 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
579 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
580 ConeVolume
= lerp(1.0, ALSource
->flOuterGain
, scale
);
581 ConeHF
= lerp(1.0, ALSource
->OuterGainHF
, scale
);
583 else if(Angle
> OuterAngle
)
585 ConeVolume
= ALSource
->flOuterGain
;
586 ConeHF
= ALSource
->OuterGainHF
;
594 DryGain
*= ConeVolume
;
597 for(i
= 0;i
< NumSends
;i
++)
598 WetGain
[i
] *= ConeVolume
;
604 for(i
= 0;i
< NumSends
;i
++)
605 WetGainHF
[i
] *= ConeHF
;
608 // Clamp to Min/Max Gain
609 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
610 for(i
= 0;i
< NumSends
;i
++)
611 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
613 // Apply filter gains and filters
614 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
615 DryGainHF
*= ALSource
->DirectGainHF
;
616 for(i
= 0;i
< NumSends
;i
++)
618 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
* ListenerGain
;
619 WetGainHF
[i
] *= ALSource
->Send
[i
].WetGainHF
;
624 /* Apply a decay-time transformation to the wet path, based on the
625 * attenuation of the dry path.
627 * Using the approximate (effective) source to listener distance, the
628 * initial decay of the reverb effect is calculated and applied to the
631 for(i
= 0;i
< NumSends
;i
++)
633 if(DecayDistance
[i
] > 0.0f
)
634 WetGain
[i
] *= aluPow(0.001f
/* -60dB */,
635 EffectiveDist
/ DecayDistance
[i
]);
639 // Calculate Velocity
640 if(DopplerFactor
!= 0.0f
)
643 ALfloat MaxVelocity
= (SpeedOfSound
*DopplerVelocity
) /
646 VSS
= aluDotproduct(Velocity
, SourceToListener
);
647 if(VSS
>= MaxVelocity
)
648 VSS
= (MaxVelocity
- 1.0f
);
649 else if(VSS
<= -MaxVelocity
)
650 VSS
= -MaxVelocity
+ 1.0f
;
652 VLS
= aluDotproduct(ListenerVel
, SourceToListener
);
653 if(VLS
>= MaxVelocity
)
654 VLS
= (MaxVelocity
- 1.0f
);
655 else if(VLS
<= -MaxVelocity
)
656 VLS
= -MaxVelocity
+ 1.0f
;
658 Pitch
*= ((SpeedOfSound
*DopplerVelocity
) - (DopplerFactor
*VLS
)) /
659 ((SpeedOfSound
*DopplerVelocity
) - (DopplerFactor
*VSS
));
662 BufferListItem
= ALSource
->queue
;
663 while(BufferListItem
!= NULL
)
666 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
668 ALint maxstep
= STACK_DATA_SIZE
/ ALSource
->NumChannels
/
669 ALSource
->SampleSize
;
670 maxstep
-= ResamplerPadding
[Resampler
] +
671 ResamplerPrePadding
[Resampler
] + 1;
672 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
674 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
675 if(Pitch
> (ALfloat
)maxstep
)
676 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
679 ALSource
->Params
.Step
= (ALint
)(Pitch
*FRACTIONONE
);
680 if(ALSource
->Params
.Step
== 0)
681 ALSource
->Params
.Step
= 1;
684 if((Device
->Flags
&DEVICE_USE_HRTF
))
685 ALSource
->Params
.DoMix
= SelectHrtfMixer(ALBuffer
,
686 (ALSource
->Params
.Step
==FRACTIONONE
) ? POINT_RESAMPLER
:
689 ALSource
->Params
.DoMix
= SelectMixer(ALBuffer
,
690 (ALSource
->Params
.Step
==FRACTIONONE
) ? POINT_RESAMPLER
:
694 BufferListItem
= BufferListItem
->next
;
697 if((Device
->Flags
&DEVICE_USE_HRTF
))
699 // Use a binaural HRTF algorithm for stereo headphone playback
700 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
704 ALfloat invlen
= 1.0f
/Distance
;
705 Position
[0] *= invlen
;
706 Position
[1] *= invlen
;
707 Position
[2] *= invlen
;
709 // Calculate elevation and azimuth only when the source is not at
710 // the listener. This prevents +0 and -0 Z from producing
711 // inconsistent panning.
712 ev
= asin(Position
[1]);
713 az
= atan2(Position
[0], -Position
[2]*ZScale
);
716 // Check to see if the HRIR is already moving.
717 if(ALSource
->HrtfMoving
)
719 // Calculate the normalized HRTF transition factor (delta).
720 delta
= CalcHrtfDelta(ALSource
->Params
.HrtfGain
, DryGain
,
721 ALSource
->Params
.HrtfDir
, Position
);
722 // If the delta is large enough, get the moving HRIR target
723 // coefficients, target delays, steppping values, and counter.
726 ALSource
->HrtfCounter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
727 ev
, az
, DryGain
, delta
,
728 ALSource
->HrtfCounter
,
729 ALSource
->Params
.HrtfCoeffs
[0],
730 ALSource
->Params
.HrtfDelay
[0],
731 ALSource
->Params
.HrtfCoeffStep
,
732 ALSource
->Params
.HrtfDelayStep
);
733 ALSource
->Params
.HrtfGain
= DryGain
;
734 ALSource
->Params
.HrtfDir
[0] = Position
[0];
735 ALSource
->Params
.HrtfDir
[1] = Position
[1];
736 ALSource
->Params
.HrtfDir
[2] = Position
[2];
741 // Get the initial (static) HRIR coefficients and delays.
742 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
743 ALSource
->Params
.HrtfCoeffs
[0],
744 ALSource
->Params
.HrtfDelay
[0]);
745 ALSource
->HrtfCounter
= 0;
746 ALSource
->Params
.HrtfGain
= DryGain
;
747 ALSource
->Params
.HrtfDir
[0] = Position
[0];
748 ALSource
->Params
.HrtfDir
[1] = Position
[1];
749 ALSource
->Params
.HrtfDir
[2] = Position
[2];
754 // Use energy-preserving panning algorithm for multi-speaker playback
755 ALfloat DirGain
, AmbientGain
;
756 const ALfloat
*SpeakerGain
;
760 length
= maxf(Distance
, MinDist
);
763 ALfloat invlen
= 1.0f
/length
;
764 Position
[0] *= invlen
;
765 Position
[1] *= invlen
;
766 Position
[2] *= invlen
;
769 pos
= aluCart2LUTpos(-Position
[2]*ZScale
, Position
[0]);
770 SpeakerGain
= Device
->PanningLUT
[pos
];
772 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
773 // elevation adjustment for directional gain. this sucks, but
774 // has low complexity
775 AmbientGain
= aluSqrt(1.0/Device
->NumChan
);
776 for(i
= 0;i
< MAXCHANNELS
;i
++)
779 for(i2
= 0;i2
< MAXCHANNELS
;i2
++)
780 ALSource
->Params
.DryGains
[i
][i2
] = 0.0f
;
782 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
784 enum Channel chan
= Device
->Speaker2Chan
[i
];
785 ALfloat gain
= lerp(AmbientGain
, SpeakerGain
[chan
], DirGain
);
786 ALSource
->Params
.DryGains
[0][chan
] = DryGain
* gain
;
789 for(i
= 0;i
< NumSends
;i
++)
790 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
];
792 /* Update filter coefficients. */
793 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQCUTOFF
/ Frequency
);
795 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
796 for(i
= 0;i
< NumSends
;i
++)
798 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
799 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
804 static __inline ALfloat
aluF2F(ALfloat val
)
806 static __inline ALshort
aluF2S(ALfloat val
)
808 if(val
> 1.0f
) return 32767;
809 if(val
< -1.0f
) return -32768;
810 return (ALint
)(val
*32767.0f
);
812 static __inline ALushort
aluF2US(ALfloat val
)
813 { return aluF2S(val
)+32768; }
814 static __inline ALbyte
aluF2B(ALfloat val
)
815 { return aluF2S(val
)>>8; }
816 static __inline ALubyte
aluF2UB(ALfloat val
)
817 { return aluF2US(val
)>>8; }
819 #define DECL_TEMPLATE(T, N, func) \
820 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
821 ALuint SamplesToDo) \
823 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
824 const enum Channel *ChanMap = device->DevChannels; \
827 for(i = 0;i < SamplesToDo;i++) \
829 for(j = 0;j < N;j++) \
830 *(buffer++) = func(DryBuffer[i][ChanMap[j]]); \
834 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
835 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
836 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
837 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
838 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
840 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
841 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
842 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
843 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
844 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
846 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
847 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
848 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
849 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
850 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
852 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
853 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
854 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
855 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
856 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
858 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
859 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
860 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
861 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
862 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
866 #define DECL_TEMPLATE(T, N, func) \
867 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
868 ALuint SamplesToDo) \
870 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
871 const enum Channel *ChanMap = device->DevChannels; \
876 for(i = 0;i < SamplesToDo;i++) \
879 samples[0] = DryBuffer[i][ChanMap[0]]; \
880 samples[1] = DryBuffer[i][ChanMap[1]]; \
881 bs2b_cross_feed(device->Bs2b, samples); \
882 *(buffer++) = func(samples[0]); \
883 *(buffer++) = func(samples[1]); \
888 for(i = 0;i < SamplesToDo;i++) \
890 for(j = 0;j < N;j++) \
891 *(buffer++) = func(DryBuffer[i][ChanMap[j]]); \
896 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
897 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
898 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
899 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
900 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
904 #define DECL_TEMPLATE(T) \
905 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
907 switch(device->FmtChans) \
910 Write_##T##_1(device, buffer, SamplesToDo); \
913 Write_##T##_2(device, buffer, SamplesToDo); \
916 Write_##T##_4(device, buffer, SamplesToDo); \
919 case DevFmtX51Side: \
920 Write_##T##_6(device, buffer, SamplesToDo); \
923 Write_##T##_7(device, buffer, SamplesToDo); \
926 Write_##T##_8(device, buffer, SamplesToDo); \
931 DECL_TEMPLATE(ALfloat
)
932 DECL_TEMPLATE(ALushort
)
933 DECL_TEMPLATE(ALshort
)
934 DECL_TEMPLATE(ALubyte
)
935 DECL_TEMPLATE(ALbyte
)
939 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
942 ALeffectslot
**slot
, **slot_end
;
943 ALsource
**src
, **src_end
;
948 #if defined(HAVE_FESETROUND)
949 fpuState
= fegetround();
950 fesetround(FE_TOWARDZERO
);
951 #elif defined(HAVE__CONTROLFP)
952 fpuState
= _controlfp(0, 0);
953 (void)_controlfp(_RC_CHOP
, _MCW_RC
);
960 /* Setup variables */
961 SamplesToDo
= minu(size
, BUFFERSIZE
);
963 /* Clear mixing buffer */
964 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
967 ctx
= device
->ContextList
;
970 ALenum DeferUpdates
= ctx
->DeferUpdates
;
971 ALenum UpdateSources
= AL_FALSE
;
974 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
976 src
= ctx
->ActiveSources
;
977 src_end
= src
+ ctx
->ActiveSourceCount
;
978 while(src
!= src_end
)
980 if((*src
)->state
!= AL_PLAYING
)
982 --(ctx
->ActiveSourceCount
);
987 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
989 ALsource_Update(*src
, ctx
);
991 MixSource(*src
, device
, SamplesToDo
);
995 /* effect slot processing */
996 slot
= ctx
->ActiveEffectSlots
;
997 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
998 while(slot
!= slot_end
)
1000 for(c
= 0;c
< SamplesToDo
;c
++)
1002 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
1003 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] / 256.0f
;
1005 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
1006 (*slot
)->PendingClicks
[0] = 0.0f
;
1008 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1009 ALeffectState_Update((*slot
)->EffectState
, ctx
, *slot
);
1011 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
1012 (*slot
)->WetBuffer
, device
->DryBuffer
);
1014 for(i
= 0;i
< SamplesToDo
;i
++)
1015 (*slot
)->WetBuffer
[i
] = 0.0f
;
1022 UnlockDevice(device
);
1024 //Post processing loop
1025 if(device
->FmtChans
== DevFmtMono
)
1027 for(i
= 0;i
< SamplesToDo
;i
++)
1029 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
1030 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] / 256.0f
;
1032 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1033 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1035 else if(device
->FmtChans
== DevFmtStereo
)
1037 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1038 for(i
= 0;i
< SamplesToDo
;i
++)
1040 for(c
= 0;c
< 2;c
++)
1042 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1043 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] / 256.0f
;
1046 for(c
= 0;c
< 2;c
++)
1048 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1049 device
->PendingClicks
[c
] = 0.0f
;
1054 for(i
= 0;i
< SamplesToDo
;i
++)
1056 for(c
= 0;c
< MAXCHANNELS
;c
++)
1058 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1059 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] / 256.0f
;
1062 for(c
= 0;c
< MAXCHANNELS
;c
++)
1064 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1065 device
->PendingClicks
[c
] = 0.0f
;
1071 switch(device
->FmtType
)
1074 Write_ALbyte(device
, buffer
, SamplesToDo
);
1077 Write_ALubyte(device
, buffer
, SamplesToDo
);
1080 Write_ALshort(device
, buffer
, SamplesToDo
);
1083 Write_ALushort(device
, buffer
, SamplesToDo
);
1086 Write_ALfloat(device
, buffer
, SamplesToDo
);
1091 size
-= SamplesToDo
;
1094 #if defined(HAVE_FESETROUND)
1095 fesetround(fpuState
);
1096 #elif defined(HAVE__CONTROLFP)
1097 _controlfp(fpuState
, _MCW_RC
);
1102 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1104 ALCcontext
*Context
;
1107 device
->Connected
= ALC_FALSE
;
1109 Context
= device
->ContextList
;
1112 ALsource
**src
, **src_end
;
1114 src
= Context
->ActiveSources
;
1115 src_end
= src
+ Context
->ActiveSourceCount
;
1116 while(src
!= src_end
)
1118 if((*src
)->state
== AL_PLAYING
)
1120 (*src
)->state
= AL_STOPPED
;
1121 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1122 (*src
)->position
= 0;
1123 (*src
)->position_fraction
= 0;
1127 Context
->ActiveSourceCount
= 0;
1129 Context
= Context
->next
;
1131 UnlockDevice(device
);