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"
41 ALfloat ConeScale
= 0.5f
;
43 /* Localized Z scalar for mono sources */
44 ALfloat ZScale
= 1.0f
;
47 static __inline ALvoid
aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
49 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
50 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
51 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
54 static __inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
56 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
57 inVector1
[2]*inVector2
[2];
60 static __inline ALvoid
aluNormalize(ALfloat
*inVector
)
62 ALfloat length
, inverse_length
;
64 length
= aluSqrt(aluDotproduct(inVector
, inVector
));
67 inverse_length
= 1.0f
/length
;
68 inVector
[0] *= inverse_length
;
69 inVector
[1] *= inverse_length
;
70 inVector
[2] *= inverse_length
;
74 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
77 vector
[0], vector
[1], vector
[2], w
80 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
81 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
82 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
86 ALvoid
CalcNonAttnSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
88 static const ALfloat angles_Mono
[1] = { 0.0f
};
89 static const ALfloat angles_Stereo
[2] = { -30.0f
, 30.0f
};
90 static const ALfloat angles_Rear
[2] = { -150.0f
, 150.0f
};
91 static const ALfloat angles_Quad
[4] = { -45.0f
, 45.0f
, -135.0f
, 135.0f
};
92 static const ALfloat angles_X51
[6] = { -30.0f
, 30.0f
, 0.0f
, 0.0f
,
94 static const ALfloat angles_X61
[7] = { -30.0f
, 30.0f
, 0.0f
, 0.0f
,
95 180.0f
, -90.0f
, 90.0f
};
96 static const ALfloat angles_X71
[8] = { -30.0f
, 30.0f
, 0.0f
, 0.0f
,
97 -110.0f
, 110.0f
, -90.0f
, 90.0f
};
99 static const enum Channel chans_Mono
[1] = { FRONT_CENTER
};
100 static const enum Channel chans_Stereo
[2] = { FRONT_LEFT
, FRONT_RIGHT
};
101 static const enum Channel chans_Rear
[2] = { BACK_LEFT
, BACK_RIGHT
};
102 static const enum Channel chans_Quad
[4] = { FRONT_LEFT
, FRONT_RIGHT
,
103 BACK_LEFT
, BACK_RIGHT
};
104 static const enum Channel chans_X51
[6] = { FRONT_LEFT
, FRONT_RIGHT
,
106 BACK_LEFT
, BACK_RIGHT
};
107 static const enum Channel chans_X61
[7] = { FRONT_LEFT
, FRONT_RIGHT
,
108 FRONT_CENTER
, LFE
, BACK_CENTER
,
109 SIDE_LEFT
, SIDE_RIGHT
};
110 static const enum Channel chans_X71
[8] = { FRONT_LEFT
, FRONT_RIGHT
,
112 BACK_LEFT
, BACK_RIGHT
,
113 SIDE_LEFT
, SIDE_RIGHT
};
115 ALCdevice
*Device
= ALContext
->Device
;
116 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
117 ALbufferlistitem
*BufferListItem
;
118 enum DevFmtChannels DevChans
;
119 enum FmtChannels Channels
;
120 ALfloat (*SrcMatrix
)[MAXCHANNELS
];
121 ALfloat DryGain
, DryGainHF
;
122 ALfloat WetGain
[MAX_SENDS
];
123 ALfloat WetGainHF
[MAX_SENDS
];
124 ALint NumSends
, Frequency
;
125 const ALfloat
*SpeakerGain
;
126 const ALfloat
*angles
= NULL
;
127 const enum Channel
*chans
= NULL
;
128 enum Resampler Resampler
;
129 ALint num_channels
= 0;
130 ALboolean VirtualChannels
;
136 /* Get device properties */
137 DevChans
= Device
->FmtChans
;
138 NumSends
= Device
->NumAuxSends
;
139 Frequency
= Device
->Frequency
;
141 /* Get listener properties */
142 ListenerGain
= ALContext
->Listener
.Gain
;
144 /* Get source properties */
145 SourceVolume
= ALSource
->flGain
;
146 MinVolume
= ALSource
->flMinGain
;
147 MaxVolume
= ALSource
->flMaxGain
;
148 Pitch
= ALSource
->flPitch
;
149 Resampler
= ALSource
->Resampler
;
150 VirtualChannels
= ALSource
->VirtualChannels
;
152 /* Calculate the stepping value */
154 BufferListItem
= ALSource
->queue
;
155 while(BufferListItem
!= NULL
)
158 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
160 ALint maxstep
= STACK_DATA_SIZE
/ ALSource
->NumChannels
/
161 ALSource
->SampleSize
;
162 maxstep
-= ResamplerPadding
[Resampler
] +
163 ResamplerPrePadding
[Resampler
] + 1;
164 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
166 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
167 if(Pitch
> (ALfloat
)maxstep
)
168 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
171 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
172 if(ALSource
->Params
.Step
== 0)
173 ALSource
->Params
.Step
= 1;
176 Channels
= ALBuffer
->FmtChannels
;
179 BufferListItem
= BufferListItem
->next
;
181 if(VirtualChannels
&& Device
->Hrtf
)
182 ALSource
->Params
.DoMix
= SelectHrtfMixer((ALSource
->Params
.Step
==FRACTIONONE
) ?
183 POINT_RESAMPLER
: Resampler
);
185 ALSource
->Params
.DoMix
= SelectMixer((ALSource
->Params
.Step
==FRACTIONONE
) ?
186 POINT_RESAMPLER
: Resampler
);
188 /* Calculate gains */
189 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
190 DryGain
*= ALSource
->DirectGain
;
191 DryGainHF
= ALSource
->DirectGainHF
;
192 for(i
= 0;i
< NumSends
;i
++)
194 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
195 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
;
196 WetGainHF
[i
] = ALSource
->Send
[i
].WetGainHF
;
199 SrcMatrix
= ALSource
->Params
.DryGains
;
200 for(i
= 0;i
< MAXCHANNELS
;i
++)
202 for(c
= 0;c
< MAXCHANNELS
;c
++)
203 SrcMatrix
[i
][c
] = 0.0f
;
208 angles
= angles_Mono
;
213 if(VirtualChannels
&& (Device
->Flags
&DEVICE_DUPLICATE_STEREO
))
215 DryGain
*= aluSqrt(2.0f
/4.0f
);
218 pos
= aluCart2LUTpos(aluCos(F_PI
/180.0f
* angles_Rear
[c
]),
219 aluSin(F_PI
/180.0f
* angles_Rear
[c
]));
220 SpeakerGain
= Device
->PanningLUT
[pos
];
222 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
224 enum Channel chan
= Device
->Speaker2Chan
[i
];
225 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
230 angles
= angles_Stereo
;
231 chans
= chans_Stereo
;
236 angles
= angles_Rear
;
242 angles
= angles_Quad
;
266 if(VirtualChannels
== AL_FALSE
)
268 for(c
= 0;c
< num_channels
;c
++)
269 SrcMatrix
[c
][chans
[c
]] += DryGain
* ListenerGain
;
271 else if(Device
->Hrtf
)
273 for(c
= 0;c
< num_channels
;c
++)
278 ALSource
->Params
.HrtfDelay
[c
][0] = 0;
279 ALSource
->Params
.HrtfDelay
[c
][1] = 0;
280 for(i
= 0;i
< HRIR_LENGTH
;i
++)
282 ALSource
->Params
.HrtfCoeffs
[c
][i
][0] = 0.0f
;
283 ALSource
->Params
.HrtfCoeffs
[c
][i
][1] = 0.0f
;
288 /* Get the static HRIR coefficients and delays for this
290 GetLerpedHrtfCoeffs(Device
->Hrtf
,
291 0.0f
, F_PI
/180.0f
* angles
[c
],
292 DryGain
*ListenerGain
,
293 ALSource
->Params
.HrtfCoeffs
[c
],
294 ALSource
->Params
.HrtfDelay
[c
]);
296 ALSource
->HrtfCounter
= 0;
301 for(c
= 0;c
< num_channels
;c
++)
303 if(chans
[c
] == LFE
) /* Special-case LFE */
305 SrcMatrix
[c
][LFE
] += DryGain
* ListenerGain
;
308 pos
= aluCart2LUTpos(aluCos(F_PI
/180.0f
* angles
[c
]),
309 aluSin(F_PI
/180.0f
* angles
[c
]));
310 SpeakerGain
= Device
->PanningLUT
[pos
];
312 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
314 enum Channel chan
= Device
->Speaker2Chan
[i
];
315 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
320 for(i
= 0;i
< NumSends
;i
++)
322 ALSource
->Params
.Send
[i
].Slot
= ALSource
->Send
[i
].Slot
;
323 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
] * ListenerGain
;
326 /* Update filter coefficients. Calculations based on the I3DL2
328 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
330 /* We use two chained one-pole filters, so we need to take the
331 * square root of the squared gain, which is the same as the base
333 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
334 for(i
= 0;i
< NumSends
;i
++)
336 /* We use a one-pole filter, so we need to take the squared gain */
337 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
338 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
342 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
344 const ALCdevice
*Device
= ALContext
->Device
;
345 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
346 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
347 ALfloat Velocity
[3],ListenerVel
[3];
348 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
349 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
350 ALfloat DopplerFactor
, DopplerVelocity
, SpeedOfSound
;
351 ALfloat AirAbsorptionFactor
;
352 ALfloat RoomAirAbsorption
[MAX_SENDS
];
353 ALbufferlistitem
*BufferListItem
;
354 ALfloat Attenuation
, EffectiveDist
;
355 ALfloat RoomAttenuation
[MAX_SENDS
];
356 ALfloat MetersPerUnit
;
357 ALfloat RoomRolloffBase
;
358 ALfloat RoomRolloff
[MAX_SENDS
];
359 ALfloat DecayDistance
[MAX_SENDS
];
362 ALboolean DryGainHFAuto
;
363 ALfloat WetGain
[MAX_SENDS
];
364 ALfloat WetGainHF
[MAX_SENDS
];
365 ALboolean WetGainAuto
;
366 ALboolean WetGainHFAuto
;
367 enum Resampler Resampler
;
375 for(i
= 0;i
< MAX_SENDS
;i
++)
378 //Get context properties
379 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
380 DopplerVelocity
= ALContext
->DopplerVelocity
;
381 SpeedOfSound
= ALContext
->flSpeedOfSound
;
382 NumSends
= Device
->NumAuxSends
;
383 Frequency
= Device
->Frequency
;
385 //Get listener properties
386 ListenerGain
= ALContext
->Listener
.Gain
;
387 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
388 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
389 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
390 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
392 //Get source properties
393 SourceVolume
= ALSource
->flGain
;
394 MinVolume
= ALSource
->flMinGain
;
395 MaxVolume
= ALSource
->flMaxGain
;
396 Pitch
= ALSource
->flPitch
;
397 Resampler
= ALSource
->Resampler
;
398 Position
[0] = ALSource
->vPosition
[0];
399 Position
[1] = ALSource
->vPosition
[1];
400 Position
[2] = ALSource
->vPosition
[2];
401 Direction
[0] = ALSource
->vOrientation
[0];
402 Direction
[1] = ALSource
->vOrientation
[1];
403 Direction
[2] = ALSource
->vOrientation
[2];
404 Velocity
[0] = ALSource
->vVelocity
[0];
405 Velocity
[1] = ALSource
->vVelocity
[1];
406 Velocity
[2] = ALSource
->vVelocity
[2];
407 MinDist
= ALSource
->flRefDistance
;
408 MaxDist
= ALSource
->flMaxDistance
;
409 Rolloff
= ALSource
->flRollOffFactor
;
410 InnerAngle
= ALSource
->flInnerAngle
* ConeScale
;
411 OuterAngle
= ALSource
->flOuterAngle
* ConeScale
;
412 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
413 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
414 WetGainAuto
= ALSource
->WetGainAuto
;
415 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
416 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
417 for(i
= 0;i
< NumSends
;i
++)
419 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
421 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
423 RoomRolloff
[i
] = 0.0f
;
424 DecayDistance
[i
] = 0.0f
;
425 RoomAirAbsorption
[i
] = 1.0f
;
427 else if(Slot
->AuxSendAuto
)
429 RoomRolloff
[i
] = RoomRolloffBase
;
430 if(IsReverbEffect(Slot
->effect
.type
))
432 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
433 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
434 SPEEDOFSOUNDMETRESPERSEC
;
435 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
439 DecayDistance
[i
] = 0.0f
;
440 RoomAirAbsorption
[i
] = 1.0f
;
445 /* If the slot's auxiliary send auto is off, the data sent to the
446 * effect slot is the same as the dry path, sans filter effects */
447 RoomRolloff
[i
] = Rolloff
;
448 DecayDistance
[i
] = 0.0f
;
449 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
452 ALSource
->Params
.Send
[i
].Slot
= Slot
;
455 //1. Translate Listener to origin (convert to head relative)
456 if(ALSource
->bHeadRelative
== AL_FALSE
)
458 ALfloat U
[3],V
[3],N
[3];
459 ALfloat Matrix
[4][4];
461 // Build transform matrix
462 N
[0] = ALContext
->Listener
.Forward
[0]; // At-vector
463 N
[1] = ALContext
->Listener
.Forward
[1];
464 N
[2] = ALContext
->Listener
.Forward
[2];
465 aluNormalize(N
); // Normalized At-vector
466 V
[0] = ALContext
->Listener
.Up
[0]; // Up-vector
467 V
[1] = ALContext
->Listener
.Up
[1];
468 V
[2] = ALContext
->Listener
.Up
[2];
469 aluNormalize(V
); // Normalized Up-vector
470 aluCrossproduct(N
, V
, U
); // Right-vector
471 aluNormalize(U
); // Normalized Right-vector
472 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0]; Matrix
[0][3] = 0.0f
;
473 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1]; Matrix
[1][3] = 0.0f
;
474 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2]; Matrix
[2][3] = 0.0f
;
475 Matrix
[3][0] = 0.0f
; Matrix
[3][1] = 0.0f
; Matrix
[3][2] = 0.0f
; Matrix
[3][3] = 1.0f
;
477 // Translate position
478 Position
[0] -= ALContext
->Listener
.Position
[0];
479 Position
[1] -= ALContext
->Listener
.Position
[1];
480 Position
[2] -= ALContext
->Listener
.Position
[2];
482 // Transform source position and direction into listener space
483 aluMatrixVector(Position
, 1.0f
, Matrix
);
484 aluMatrixVector(Direction
, 0.0f
, Matrix
);
485 // Transform source and listener velocity into listener space
486 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
487 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
490 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
492 SourceToListener
[0] = -Position
[0];
493 SourceToListener
[1] = -Position
[1];
494 SourceToListener
[2] = -Position
[2];
495 aluNormalize(SourceToListener
);
496 aluNormalize(Direction
);
498 //2. Calculate distance attenuation
499 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
500 ClampedDist
= Distance
;
503 for(i
= 0;i
< NumSends
;i
++)
504 RoomAttenuation
[i
] = 1.0f
;
505 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
506 ALContext
->DistanceModel
)
508 case InverseDistanceClamped
:
509 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
510 if(MaxDist
< MinDist
)
513 case InverseDistance
:
516 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
517 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
518 for(i
= 0;i
< NumSends
;i
++)
520 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
521 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
526 case LinearDistanceClamped
:
527 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
528 if(MaxDist
< MinDist
)
532 if(MaxDist
!= MinDist
)
534 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
535 Attenuation
= maxf(Attenuation
, 0.0f
);
536 for(i
= 0;i
< NumSends
;i
++)
538 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
539 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
544 case ExponentDistanceClamped
:
545 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
546 if(MaxDist
< MinDist
)
549 case ExponentDistance
:
550 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
552 Attenuation
= aluPow(ClampedDist
/MinDist
, -Rolloff
);
553 for(i
= 0;i
< NumSends
;i
++)
554 RoomAttenuation
[i
] = aluPow(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
558 case DisableDistance
:
562 // Source Gain + Attenuation
563 DryGain
= SourceVolume
* Attenuation
;
564 for(i
= 0;i
< NumSends
;i
++)
565 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
567 // Distance-based air absorption
568 EffectiveDist
= 0.0f
;
569 if(MinDist
> 0.0f
&& Attenuation
< 1.0f
)
570 EffectiveDist
= (MinDist
/Attenuation
- MinDist
)*MetersPerUnit
;
571 if(AirAbsorptionFactor
> 0.0f
&& EffectiveDist
> 0.0f
)
573 DryGainHF
*= aluPow(AIRABSORBGAINHF
, AirAbsorptionFactor
*EffectiveDist
);
574 for(i
= 0;i
< NumSends
;i
++)
575 WetGainHF
[i
] *= aluPow(RoomAirAbsorption
[i
],
576 AirAbsorptionFactor
*EffectiveDist
);
581 /* Apply a decay-time transformation to the wet path, based on the
582 * attenuation of the dry path.
584 * Using the approximate (effective) source to listener distance, the
585 * initial decay of the reverb effect is calculated and applied to the
588 for(i
= 0;i
< NumSends
;i
++)
590 if(DecayDistance
[i
] > 0.0f
)
591 WetGain
[i
] *= aluPow(0.001f
/* -60dB */,
592 EffectiveDist
/ DecayDistance
[i
]);
596 /* Calculate directional soundcones */
597 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * (180.0f
/F_PI
);
598 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
600 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
601 ConeVolume
= lerp(1.0f
, ALSource
->flOuterGain
, scale
);
602 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
604 else if(Angle
> OuterAngle
)
606 ConeVolume
= ALSource
->flOuterGain
;
607 ConeHF
= ALSource
->OuterGainHF
;
615 DryGain
*= ConeVolume
;
618 for(i
= 0;i
< NumSends
;i
++)
619 WetGain
[i
] *= ConeVolume
;
625 for(i
= 0;i
< NumSends
;i
++)
626 WetGainHF
[i
] *= ConeHF
;
629 // Clamp to Min/Max Gain
630 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
631 for(i
= 0;i
< NumSends
;i
++)
632 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
634 // Apply filter gains and filters
635 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
636 DryGainHF
*= ALSource
->DirectGainHF
;
637 for(i
= 0;i
< NumSends
;i
++)
639 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
* ListenerGain
;
640 WetGainHF
[i
] *= ALSource
->Send
[i
].WetGainHF
;
643 // Calculate Velocity
644 if(DopplerFactor
!= 0.0f
)
647 ALfloat MaxVelocity
= (SpeedOfSound
*DopplerVelocity
) /
650 VSS
= aluDotproduct(Velocity
, SourceToListener
);
651 if(VSS
>= MaxVelocity
)
652 VSS
= (MaxVelocity
- 1.0f
);
653 else if(VSS
<= -MaxVelocity
)
654 VSS
= -MaxVelocity
+ 1.0f
;
656 VLS
= aluDotproduct(ListenerVel
, SourceToListener
);
657 if(VLS
>= MaxVelocity
)
658 VLS
= (MaxVelocity
- 1.0f
);
659 else if(VLS
<= -MaxVelocity
)
660 VLS
= -MaxVelocity
+ 1.0f
;
662 Pitch
*= ((SpeedOfSound
*DopplerVelocity
) - (DopplerFactor
*VLS
)) /
663 ((SpeedOfSound
*DopplerVelocity
) - (DopplerFactor
*VSS
));
666 BufferListItem
= ALSource
->queue
;
667 while(BufferListItem
!= NULL
)
670 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
672 ALint maxstep
= STACK_DATA_SIZE
/ ALSource
->NumChannels
/
673 ALSource
->SampleSize
;
674 maxstep
-= ResamplerPadding
[Resampler
] +
675 ResamplerPrePadding
[Resampler
] + 1;
676 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
678 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
679 if(Pitch
> (ALfloat
)maxstep
)
680 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
683 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
684 if(ALSource
->Params
.Step
== 0)
685 ALSource
->Params
.Step
= 1;
690 BufferListItem
= BufferListItem
->next
;
693 ALSource
->Params
.DoMix
= SelectHrtfMixer((ALSource
->Params
.Step
==FRACTIONONE
) ?
694 POINT_RESAMPLER
: Resampler
);
696 ALSource
->Params
.DoMix
= SelectMixer((ALSource
->Params
.Step
==FRACTIONONE
) ?
697 POINT_RESAMPLER
: Resampler
);
701 // Use a binaural HRTF algorithm for stereo headphone playback
702 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
706 ALfloat invlen
= 1.0f
/Distance
;
707 Position
[0] *= invlen
;
708 Position
[1] *= invlen
;
709 Position
[2] *= invlen
;
711 // Calculate elevation and azimuth only when the source is not at
712 // the listener. This prevents +0 and -0 Z from producing
713 // inconsistent panning.
714 ev
= aluAsin(Position
[1]);
715 az
= aluAtan2(Position
[0], -Position
[2]*ZScale
);
718 // Check to see if the HRIR is already moving.
719 if(ALSource
->HrtfMoving
)
721 // Calculate the normalized HRTF transition factor (delta).
722 delta
= CalcHrtfDelta(ALSource
->Params
.HrtfGain
, DryGain
,
723 ALSource
->Params
.HrtfDir
, Position
);
724 // If the delta is large enough, get the moving HRIR target
725 // coefficients, target delays, steppping values, and counter.
728 ALSource
->HrtfCounter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
729 ev
, az
, DryGain
, delta
,
730 ALSource
->HrtfCounter
,
731 ALSource
->Params
.HrtfCoeffs
[0],
732 ALSource
->Params
.HrtfDelay
[0],
733 ALSource
->Params
.HrtfCoeffStep
,
734 ALSource
->Params
.HrtfDelayStep
);
735 ALSource
->Params
.HrtfGain
= DryGain
;
736 ALSource
->Params
.HrtfDir
[0] = Position
[0];
737 ALSource
->Params
.HrtfDir
[1] = Position
[1];
738 ALSource
->Params
.HrtfDir
[2] = Position
[2];
743 // Get the initial (static) HRIR coefficients and delays.
744 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
745 ALSource
->Params
.HrtfCoeffs
[0],
746 ALSource
->Params
.HrtfDelay
[0]);
747 ALSource
->HrtfCounter
= 0;
748 ALSource
->Params
.HrtfGain
= DryGain
;
749 ALSource
->Params
.HrtfDir
[0] = Position
[0];
750 ALSource
->Params
.HrtfDir
[1] = Position
[1];
751 ALSource
->Params
.HrtfDir
[2] = Position
[2];
756 // Use energy-preserving panning algorithm for multi-speaker playback
757 ALfloat DirGain
, AmbientGain
;
758 const ALfloat
*SpeakerGain
;
762 length
= maxf(Distance
, MinDist
);
765 ALfloat invlen
= 1.0f
/length
;
766 Position
[0] *= invlen
;
767 Position
[1] *= invlen
;
768 Position
[2] *= invlen
;
771 pos
= aluCart2LUTpos(-Position
[2]*ZScale
, Position
[0]);
772 SpeakerGain
= Device
->PanningLUT
[pos
];
774 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
775 // elevation adjustment for directional gain. this sucks, but
776 // has low complexity
777 AmbientGain
= aluSqrt(1.0f
/Device
->NumChan
);
778 for(i
= 0;i
< MAXCHANNELS
;i
++)
781 for(i2
= 0;i2
< MAXCHANNELS
;i2
++)
782 ALSource
->Params
.DryGains
[i
][i2
] = 0.0f
;
784 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
786 enum Channel chan
= Device
->Speaker2Chan
[i
];
787 ALfloat gain
= lerp(AmbientGain
, SpeakerGain
[chan
], DirGain
);
788 ALSource
->Params
.DryGains
[0][chan
] = DryGain
* gain
;
791 for(i
= 0;i
< NumSends
;i
++)
792 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
];
794 /* Update filter coefficients. */
795 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
797 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
798 for(i
= 0;i
< NumSends
;i
++)
800 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
801 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
806 static __inline ALfloat
aluF2F(ALfloat val
)
808 static __inline ALshort
aluF2S(ALfloat val
)
810 if(val
> 1.0f
) return 32767;
811 if(val
< -1.0f
) return -32768;
812 return fastf2i(val
*32767.0f
);
814 static __inline ALushort
aluF2US(ALfloat val
)
815 { return aluF2S(val
)+32768; }
816 static __inline ALbyte
aluF2B(ALfloat val
)
817 { return aluF2S(val
)>>8; }
818 static __inline ALubyte
aluF2UB(ALfloat val
)
819 { return aluF2US(val
)>>8; }
821 #define DECL_TEMPLATE(T, N, func) \
822 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
823 ALuint SamplesToDo) \
825 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
826 const enum Channel *ChanMap = device->DevChannels; \
829 for(i = 0;i < SamplesToDo;i++) \
831 for(j = 0;j < N;j++) \
832 *(buffer++) = func(DryBuffer[i][ChanMap[j]]); \
836 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
837 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
838 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
839 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
840 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
842 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
843 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
844 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
845 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
846 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
848 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
849 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
850 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
851 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
852 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
854 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
855 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
856 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
857 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
858 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
860 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
861 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
862 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
863 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
864 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
868 #define DECL_TEMPLATE(T, N, func) \
869 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
870 ALuint SamplesToDo) \
872 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
873 const enum Channel *ChanMap = device->DevChannels; \
878 for(i = 0;i < SamplesToDo;i++) \
881 samples[0] = DryBuffer[i][ChanMap[0]]; \
882 samples[1] = DryBuffer[i][ChanMap[1]]; \
883 bs2b_cross_feed(device->Bs2b, samples); \
884 *(buffer++) = func(samples[0]); \
885 *(buffer++) = func(samples[1]); \
890 for(i = 0;i < SamplesToDo;i++) \
892 for(j = 0;j < N;j++) \
893 *(buffer++) = func(DryBuffer[i][ChanMap[j]]); \
898 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
899 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
900 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
901 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
902 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
906 #define DECL_TEMPLATE(T) \
907 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
909 switch(device->FmtChans) \
912 Write_##T##_1(device, buffer, SamplesToDo); \
915 Write_##T##_2(device, buffer, SamplesToDo); \
918 Write_##T##_4(device, buffer, SamplesToDo); \
921 case DevFmtX51Side: \
922 Write_##T##_6(device, buffer, SamplesToDo); \
925 Write_##T##_7(device, buffer, SamplesToDo); \
928 Write_##T##_8(device, buffer, SamplesToDo); \
933 DECL_TEMPLATE(ALfloat
)
934 DECL_TEMPLATE(ALushort
)
935 DECL_TEMPLATE(ALshort
)
936 DECL_TEMPLATE(ALubyte
)
937 DECL_TEMPLATE(ALbyte
)
941 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
944 ALeffectslot
**slot
, **slot_end
;
945 ALsource
**src
, **src_end
;
950 fpuState
= SetMixerFPUMode();
954 /* Setup variables */
955 SamplesToDo
= minu(size
, BUFFERSIZE
);
957 /* Clear mixing buffer */
958 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
961 ctx
= device
->ContextList
;
964 ALenum DeferUpdates
= ctx
->DeferUpdates
;
965 ALenum UpdateSources
= AL_FALSE
;
968 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
970 src
= ctx
->ActiveSources
;
971 src_end
= src
+ ctx
->ActiveSourceCount
;
972 while(src
!= src_end
)
974 if((*src
)->state
!= AL_PLAYING
)
976 --(ctx
->ActiveSourceCount
);
981 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
983 ALsource_Update(*src
, ctx
);
985 MixSource(*src
, device
, SamplesToDo
);
989 /* effect slot processing */
990 slot
= ctx
->ActiveEffectSlots
;
991 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
992 while(slot
!= slot_end
)
994 for(c
= 0;c
< SamplesToDo
;c
++)
996 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
997 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
999 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
1000 (*slot
)->PendingClicks
[0] = 0.0f
;
1002 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1003 ALeffectState_Update((*slot
)->EffectState
, ctx
, *slot
);
1005 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
1006 (*slot
)->WetBuffer
, device
->DryBuffer
);
1008 for(i
= 0;i
< SamplesToDo
;i
++)
1009 (*slot
)->WetBuffer
[i
] = 0.0f
;
1016 UnlockDevice(device
);
1018 //Post processing loop
1019 if(device
->FmtChans
== DevFmtMono
)
1021 for(i
= 0;i
< SamplesToDo
;i
++)
1023 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
1024 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] * (1.0f
/256.0f
);
1026 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1027 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1029 else if(device
->FmtChans
== DevFmtStereo
)
1031 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1032 for(i
= 0;i
< SamplesToDo
;i
++)
1034 for(c
= 0;c
< 2;c
++)
1036 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1037 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1040 for(c
= 0;c
< 2;c
++)
1042 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1043 device
->PendingClicks
[c
] = 0.0f
;
1048 for(i
= 0;i
< SamplesToDo
;i
++)
1050 for(c
= 0;c
< MAXCHANNELS
;c
++)
1052 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1053 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1056 for(c
= 0;c
< MAXCHANNELS
;c
++)
1058 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1059 device
->PendingClicks
[c
] = 0.0f
;
1065 switch(device
->FmtType
)
1068 Write_ALbyte(device
, buffer
, SamplesToDo
);
1071 Write_ALubyte(device
, buffer
, SamplesToDo
);
1074 Write_ALshort(device
, buffer
, SamplesToDo
);
1077 Write_ALushort(device
, buffer
, SamplesToDo
);
1080 Write_ALfloat(device
, buffer
, SamplesToDo
);
1085 size
-= SamplesToDo
;
1088 RestoreFPUMode(fpuState
);
1092 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1094 ALCcontext
*Context
;
1097 device
->Connected
= ALC_FALSE
;
1099 Context
= device
->ContextList
;
1102 ALsource
**src
, **src_end
;
1104 src
= Context
->ActiveSources
;
1105 src_end
= src
+ Context
->ActiveSourceCount
;
1106 while(src
!= src_end
)
1108 if((*src
)->state
== AL_PLAYING
)
1110 (*src
)->state
= AL_STOPPED
;
1111 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1112 (*src
)->position
= 0;
1113 (*src
)->position_fraction
= 0;
1117 Context
->ActiveSourceCount
= 0;
1119 Context
= Context
->next
;
1121 UnlockDevice(device
);