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"
46 ALfloat ConeScale
= 0.5f
;
48 /* Localized Z scalar for mono sources */
49 ALfloat ZScale
= 1.0f
;
52 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
55 vector
[0], vector
[1], vector
[2], w
58 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
59 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
60 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
64 ALvoid
CalcNonAttnSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
66 static const struct ChanMap MonoMap
[1] = { { FRONT_CENTER
, 0.0f
} };
67 static const struct ChanMap StereoMap
[2] = {
68 { FRONT_LEFT
, -30.0f
* F_PI
/180.0f
},
69 { FRONT_RIGHT
, 30.0f
* F_PI
/180.0f
}
71 static const struct ChanMap RearMap
[2] = {
72 { BACK_LEFT
, -150.0f
* F_PI
/180.0f
},
73 { BACK_RIGHT
, 150.0f
* F_PI
/180.0f
}
75 static const struct ChanMap QuadMap
[4] = {
76 { FRONT_LEFT
, -45.0f
* F_PI
/180.0f
},
77 { FRONT_RIGHT
, 45.0f
* F_PI
/180.0f
},
78 { BACK_LEFT
, -135.0f
* F_PI
/180.0f
},
79 { BACK_RIGHT
, 135.0f
* F_PI
/180.0f
}
81 static const struct ChanMap X51Map
[6] = {
82 { FRONT_LEFT
, -30.0f
* F_PI
/180.0f
},
83 { FRONT_RIGHT
, 30.0f
* F_PI
/180.0f
},
84 { FRONT_CENTER
, 0.0f
* F_PI
/180.0f
},
86 { BACK_LEFT
, -110.0f
* F_PI
/180.0f
},
87 { BACK_RIGHT
, 110.0f
* F_PI
/180.0f
}
89 static const struct ChanMap X61Map
[7] = {
90 { FRONT_LEFT
, -30.0f
* F_PI
/180.0f
},
91 { FRONT_RIGHT
, 30.0f
* F_PI
/180.0f
},
92 { FRONT_CENTER
, 0.0f
* F_PI
/180.0f
},
94 { BACK_CENTER
, 180.0f
* F_PI
/180.0f
},
95 { SIDE_LEFT
, -90.0f
* F_PI
/180.0f
},
96 { SIDE_RIGHT
, 90.0f
* F_PI
/180.0f
}
98 static const struct ChanMap X71Map
[8] = {
99 { FRONT_LEFT
, -30.0f
* F_PI
/180.0f
},
100 { FRONT_RIGHT
, 30.0f
* F_PI
/180.0f
},
101 { FRONT_CENTER
, 0.0f
* F_PI
/180.0f
},
103 { BACK_LEFT
, -150.0f
* F_PI
/180.0f
},
104 { BACK_RIGHT
, 150.0f
* F_PI
/180.0f
},
105 { SIDE_LEFT
, -90.0f
* F_PI
/180.0f
},
106 { SIDE_RIGHT
, 90.0f
* F_PI
/180.0f
}
109 ALCdevice
*Device
= ALContext
->Device
;
110 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
111 ALbufferlistitem
*BufferListItem
;
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
*ChannelGain
;
119 const struct ChanMap
*chans
= NULL
;
120 enum Resampler Resampler
;
121 ALint num_channels
= 0;
122 ALboolean DirectChannels
;
128 /* Get device properties */
129 NumSends
= Device
->NumAuxSends
;
130 Frequency
= Device
->Frequency
;
132 /* Get listener properties */
133 ListenerGain
= ALContext
->Listener
.Gain
;
135 /* Get source properties */
136 SourceVolume
= ALSource
->flGain
;
137 MinVolume
= ALSource
->flMinGain
;
138 MaxVolume
= ALSource
->flMaxGain
;
139 Pitch
= ALSource
->flPitch
;
140 Resampler
= ALSource
->Resampler
;
141 DirectChannels
= ALSource
->DirectChannels
;
143 /* Calculate the stepping value */
145 BufferListItem
= ALSource
->queue
;
146 while(BufferListItem
!= NULL
)
149 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
151 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
152 ALSource
->NumChannels
;
153 maxstep
-= ResamplerPadding
[Resampler
] +
154 ResamplerPrePadding
[Resampler
] + 1;
155 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
157 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
158 if(Pitch
> (ALfloat
)maxstep
)
159 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
162 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
163 if(ALSource
->Params
.Step
== 0)
164 ALSource
->Params
.Step
= 1;
166 if(ALSource
->Params
.Step
== FRACTIONONE
)
167 Resampler
= PointResampler
;
169 Channels
= ALBuffer
->FmtChannels
;
172 BufferListItem
= BufferListItem
->next
;
174 if(!DirectChannels
&& Device
->Hrtf
)
175 ALSource
->Params
.DoMix
= SelectHrtfMixer(Resampler
);
177 ALSource
->Params
.DoMix
= SelectMixer(Resampler
);
179 /* Calculate gains */
180 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
181 DryGain
*= ALSource
->DirectGain
;
182 DryGainHF
= ALSource
->DirectGainHF
;
183 for(i
= 0;i
< NumSends
;i
++)
185 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
186 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
;
187 WetGainHF
[i
] = ALSource
->Send
[i
].WetGainHF
;
190 SrcMatrix
= ALSource
->Params
.DryGains
;
191 for(i
= 0;i
< MAXCHANNELS
;i
++)
193 for(c
= 0;c
< MAXCHANNELS
;c
++)
194 SrcMatrix
[i
][c
] = 0.0f
;
203 if(!DirectChannels
&& (Device
->Flags
&DEVICE_DUPLICATE_STEREO
))
205 DryGain
*= aluSqrt(2.0f
/4.0f
);
208 pos
= aluCart2LUTpos(aluCos(RearMap
[c
].angle
),
209 aluSin(RearMap
[c
].angle
));
210 ChannelGain
= Device
->PanningLUT
[pos
];
212 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
214 enum Channel chan
= Device
->Speaker2Chan
[i
];
215 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
250 if(DirectChannels
!= AL_FALSE
)
252 for(c
= 0;c
< num_channels
;c
++)
253 SrcMatrix
[c
][chans
[c
].channel
] += DryGain
* ListenerGain
;
255 else if(Device
->Hrtf
)
257 for(c
= 0;c
< num_channels
;c
++)
259 if(chans
[c
].channel
== LFE
)
262 ALSource
->Params
.HrtfDelay
[c
][0] = 0;
263 ALSource
->Params
.HrtfDelay
[c
][1] = 0;
264 for(i
= 0;i
< HRIR_LENGTH
;i
++)
266 ALSource
->Params
.HrtfCoeffs
[c
][i
][0] = 0.0f
;
267 ALSource
->Params
.HrtfCoeffs
[c
][i
][1] = 0.0f
;
272 /* Get the static HRIR coefficients and delays for this
274 GetLerpedHrtfCoeffs(Device
->Hrtf
,
275 0.0f
, F_PI
/180.0f
* chans
[c
].angle
,
276 DryGain
*ListenerGain
,
277 ALSource
->Params
.HrtfCoeffs
[c
],
278 ALSource
->Params
.HrtfDelay
[c
]);
280 ALSource
->HrtfCounter
= 0;
285 for(c
= 0;c
< num_channels
;c
++)
287 if(chans
[c
].channel
== LFE
) /* Special-case LFE */
289 SrcMatrix
[c
][LFE
] += DryGain
* ListenerGain
;
292 pos
= aluCart2LUTpos(aluCos(chans
[c
].angle
), aluSin(chans
[c
].angle
));
293 ChannelGain
= Device
->PanningLUT
[pos
];
295 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
297 enum Channel chan
= Device
->Speaker2Chan
[i
];
298 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
303 for(i
= 0;i
< NumSends
;i
++)
305 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
308 Slot
= Device
->DefaultSlot
;
309 if(Slot
&& Slot
->effect
.type
== AL_EFFECT_NULL
)
311 ALSource
->Params
.Send
[i
].Slot
= Slot
;
312 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
] * ListenerGain
;
315 /* Update filter coefficients. Calculations based on the I3DL2
317 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
319 /* We use two chained one-pole filters, so we need to take the
320 * square root of the squared gain, which is the same as the base
322 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
323 for(i
= 0;i
< NumSends
;i
++)
325 /* We use a one-pole filter, so we need to take the squared gain */
326 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
327 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
331 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
333 const ALCdevice
*Device
= ALContext
->Device
;
334 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
335 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
336 ALfloat Velocity
[3],ListenerVel
[3];
337 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
338 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
339 ALfloat DopplerFactor
, SpeedOfSound
;
340 ALfloat AirAbsorptionFactor
;
341 ALfloat RoomAirAbsorption
[MAX_SENDS
];
342 ALbufferlistitem
*BufferListItem
;
343 ALfloat Attenuation
, EffectiveDist
;
344 ALfloat RoomAttenuation
[MAX_SENDS
];
345 ALfloat MetersPerUnit
;
346 ALfloat RoomRolloffBase
;
347 ALfloat RoomRolloff
[MAX_SENDS
];
348 ALfloat DecayDistance
[MAX_SENDS
];
351 ALboolean DryGainHFAuto
;
352 ALfloat WetGain
[MAX_SENDS
];
353 ALfloat WetGainHF
[MAX_SENDS
];
354 ALboolean WetGainAuto
;
355 ALboolean WetGainHFAuto
;
356 enum Resampler Resampler
;
364 for(i
= 0;i
< MAX_SENDS
;i
++)
367 //Get context properties
368 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
369 SpeedOfSound
= ALContext
->flSpeedOfSound
* ALContext
->DopplerVelocity
;
370 NumSends
= Device
->NumAuxSends
;
371 Frequency
= Device
->Frequency
;
373 //Get listener properties
374 ListenerGain
= ALContext
->Listener
.Gain
;
375 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
376 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
377 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
378 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
380 //Get source properties
381 SourceVolume
= ALSource
->flGain
;
382 MinVolume
= ALSource
->flMinGain
;
383 MaxVolume
= ALSource
->flMaxGain
;
384 Pitch
= ALSource
->flPitch
;
385 Resampler
= ALSource
->Resampler
;
386 Position
[0] = ALSource
->vPosition
[0];
387 Position
[1] = ALSource
->vPosition
[1];
388 Position
[2] = ALSource
->vPosition
[2];
389 Direction
[0] = ALSource
->vOrientation
[0];
390 Direction
[1] = ALSource
->vOrientation
[1];
391 Direction
[2] = ALSource
->vOrientation
[2];
392 Velocity
[0] = ALSource
->vVelocity
[0];
393 Velocity
[1] = ALSource
->vVelocity
[1];
394 Velocity
[2] = ALSource
->vVelocity
[2];
395 MinDist
= ALSource
->flRefDistance
;
396 MaxDist
= ALSource
->flMaxDistance
;
397 Rolloff
= ALSource
->flRollOffFactor
;
398 InnerAngle
= ALSource
->flInnerAngle
* ConeScale
;
399 OuterAngle
= ALSource
->flOuterAngle
* ConeScale
;
400 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
401 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
402 WetGainAuto
= ALSource
->WetGainAuto
;
403 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
404 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
405 for(i
= 0;i
< NumSends
;i
++)
407 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
410 Slot
= Device
->DefaultSlot
;
411 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
414 RoomRolloff
[i
] = 0.0f
;
415 DecayDistance
[i
] = 0.0f
;
416 RoomAirAbsorption
[i
] = 1.0f
;
418 else if(Slot
->AuxSendAuto
)
420 RoomRolloff
[i
] = RoomRolloffBase
;
421 if(IsReverbEffect(Slot
->effect
.type
))
423 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
424 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
425 SPEEDOFSOUNDMETRESPERSEC
;
426 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
430 DecayDistance
[i
] = 0.0f
;
431 RoomAirAbsorption
[i
] = 1.0f
;
436 /* If the slot's auxiliary send auto is off, the data sent to the
437 * effect slot is the same as the dry path, sans filter effects */
438 RoomRolloff
[i
] = Rolloff
;
439 DecayDistance
[i
] = 0.0f
;
440 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
443 ALSource
->Params
.Send
[i
].Slot
= Slot
;
446 //1. Translate Listener to origin (convert to head relative)
447 if(ALSource
->bHeadRelative
== AL_FALSE
)
449 ALfloat Matrix
[4][4];
453 for(i2
= 0;i2
< 4;i2
++)
454 Matrix
[i
][i2
] = ALContext
->Listener
.Matrix
[i
][i2
];
457 /* Translate position */
458 Position
[0] -= ALContext
->Listener
.Position
[0];
459 Position
[1] -= ALContext
->Listener
.Position
[1];
460 Position
[2] -= ALContext
->Listener
.Position
[2];
462 /* Transform source vectors into listener space */
463 aluMatrixVector(Position
, 1.0f
, Matrix
);
464 aluMatrixVector(Direction
, 0.0f
, Matrix
);
465 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
469 ListenerVel
[0] = 0.0f
;
470 ListenerVel
[1] = 0.0f
;
471 ListenerVel
[2] = 0.0f
;
474 SourceToListener
[0] = -Position
[0];
475 SourceToListener
[1] = -Position
[1];
476 SourceToListener
[2] = -Position
[2];
477 aluNormalize(SourceToListener
);
478 aluNormalize(Direction
);
480 //2. Calculate distance attenuation
481 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
482 ClampedDist
= Distance
;
485 for(i
= 0;i
< NumSends
;i
++)
486 RoomAttenuation
[i
] = 1.0f
;
487 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
488 ALContext
->DistanceModel
)
490 case InverseDistanceClamped
:
491 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
492 if(MaxDist
< MinDist
)
495 case InverseDistance
:
498 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
499 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
500 for(i
= 0;i
< NumSends
;i
++)
502 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
503 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
508 case LinearDistanceClamped
:
509 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
510 if(MaxDist
< MinDist
)
514 if(MaxDist
!= MinDist
)
516 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
517 Attenuation
= maxf(Attenuation
, 0.0f
);
518 for(i
= 0;i
< NumSends
;i
++)
520 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
521 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
526 case ExponentDistanceClamped
:
527 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
528 if(MaxDist
< MinDist
)
531 case ExponentDistance
:
532 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
534 Attenuation
= aluPow(ClampedDist
/MinDist
, -Rolloff
);
535 for(i
= 0;i
< NumSends
;i
++)
536 RoomAttenuation
[i
] = aluPow(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
540 case DisableDistance
:
544 // Source Gain + Attenuation
545 DryGain
= SourceVolume
* Attenuation
;
546 for(i
= 0;i
< NumSends
;i
++)
547 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
549 // Distance-based air absorption
550 EffectiveDist
= 0.0f
;
551 if(MinDist
> 0.0f
&& Attenuation
< 1.0f
)
552 EffectiveDist
= (MinDist
/Attenuation
- MinDist
)*MetersPerUnit
;
553 if(AirAbsorptionFactor
> 0.0f
&& EffectiveDist
> 0.0f
)
555 DryGainHF
*= aluPow(AIRABSORBGAINHF
, AirAbsorptionFactor
*EffectiveDist
);
556 for(i
= 0;i
< NumSends
;i
++)
557 WetGainHF
[i
] *= aluPow(RoomAirAbsorption
[i
],
558 AirAbsorptionFactor
*EffectiveDist
);
563 /* Apply a decay-time transformation to the wet path, based on the
564 * attenuation of the dry path.
566 * Using the approximate (effective) source to listener distance, the
567 * initial decay of the reverb effect is calculated and applied to the
570 for(i
= 0;i
< NumSends
;i
++)
572 if(DecayDistance
[i
] > 0.0f
)
573 WetGain
[i
] *= aluPow(0.001f
/* -60dB */,
574 EffectiveDist
/ DecayDistance
[i
]);
578 /* Calculate directional soundcones */
579 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * (180.0f
/F_PI
);
580 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
582 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
583 ConeVolume
= lerp(1.0f
, ALSource
->flOuterGain
, scale
);
584 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
586 else if(Angle
> OuterAngle
)
588 ConeVolume
= ALSource
->flOuterGain
;
589 ConeHF
= ALSource
->OuterGainHF
;
597 DryGain
*= ConeVolume
;
600 for(i
= 0;i
< NumSends
;i
++)
601 WetGain
[i
] *= ConeVolume
;
607 for(i
= 0;i
< NumSends
;i
++)
608 WetGainHF
[i
] *= ConeHF
;
611 // Clamp to Min/Max Gain
612 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
613 for(i
= 0;i
< NumSends
;i
++)
614 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
616 // Apply filter gains and filters
617 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
618 DryGainHF
*= ALSource
->DirectGainHF
;
619 for(i
= 0;i
< NumSends
;i
++)
621 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
* ListenerGain
;
622 WetGainHF
[i
] *= ALSource
->Send
[i
].WetGainHF
;
625 // Calculate Velocity
626 if(DopplerFactor
> 0.0f
)
630 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
631 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
633 Pitch
*= maxf(SpeedOfSound
-VLS
, 1.0f
) / maxf(SpeedOfSound
-VSS
, 1.0f
);
636 BufferListItem
= ALSource
->queue
;
637 while(BufferListItem
!= NULL
)
640 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
642 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
643 ALSource
->NumChannels
;
644 maxstep
-= ResamplerPadding
[Resampler
] +
645 ResamplerPrePadding
[Resampler
] + 1;
646 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
648 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
649 if(Pitch
> (ALfloat
)maxstep
)
650 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
653 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
654 if(ALSource
->Params
.Step
== 0)
655 ALSource
->Params
.Step
= 1;
657 if(ALSource
->Params
.Step
== FRACTIONONE
)
658 Resampler
= PointResampler
;
662 BufferListItem
= BufferListItem
->next
;
665 ALSource
->Params
.DoMix
= SelectHrtfMixer(Resampler
);
667 ALSource
->Params
.DoMix
= SelectMixer(Resampler
);
671 // Use a binaural HRTF algorithm for stereo headphone playback
672 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
676 ALfloat invlen
= 1.0f
/Distance
;
677 Position
[0] *= invlen
;
678 Position
[1] *= invlen
;
679 Position
[2] *= invlen
;
681 // Calculate elevation and azimuth only when the source is not at
682 // the listener. This prevents +0 and -0 Z from producing
683 // inconsistent panning.
684 ev
= aluAsin(Position
[1]);
685 az
= aluAtan2(Position
[0], -Position
[2]*ZScale
);
688 // Check to see if the HRIR is already moving.
689 if(ALSource
->HrtfMoving
)
691 // Calculate the normalized HRTF transition factor (delta).
692 delta
= CalcHrtfDelta(ALSource
->Params
.HrtfGain
, DryGain
,
693 ALSource
->Params
.HrtfDir
, Position
);
694 // If the delta is large enough, get the moving HRIR target
695 // coefficients, target delays, steppping values, and counter.
698 ALSource
->HrtfCounter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
699 ev
, az
, DryGain
, delta
,
700 ALSource
->HrtfCounter
,
701 ALSource
->Params
.HrtfCoeffs
[0],
702 ALSource
->Params
.HrtfDelay
[0],
703 ALSource
->Params
.HrtfCoeffStep
,
704 ALSource
->Params
.HrtfDelayStep
);
705 ALSource
->Params
.HrtfGain
= DryGain
;
706 ALSource
->Params
.HrtfDir
[0] = Position
[0];
707 ALSource
->Params
.HrtfDir
[1] = Position
[1];
708 ALSource
->Params
.HrtfDir
[2] = Position
[2];
713 // Get the initial (static) HRIR coefficients and delays.
714 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
715 ALSource
->Params
.HrtfCoeffs
[0],
716 ALSource
->Params
.HrtfDelay
[0]);
717 ALSource
->HrtfCounter
= 0;
718 ALSource
->Params
.HrtfGain
= DryGain
;
719 ALSource
->Params
.HrtfDir
[0] = Position
[0];
720 ALSource
->Params
.HrtfDir
[1] = Position
[1];
721 ALSource
->Params
.HrtfDir
[2] = Position
[2];
726 // Use energy-preserving panning algorithm for multi-speaker playback
727 ALfloat DirGain
, AmbientGain
;
728 const ALfloat
*ChannelGain
;
732 length
= maxf(Distance
, MinDist
);
735 ALfloat invlen
= 1.0f
/length
;
736 Position
[0] *= invlen
;
737 Position
[1] *= invlen
;
738 Position
[2] *= invlen
;
741 pos
= aluCart2LUTpos(-Position
[2]*ZScale
, Position
[0]);
742 ChannelGain
= Device
->PanningLUT
[pos
];
744 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
745 // elevation adjustment for directional gain. this sucks, but
746 // has low complexity
747 AmbientGain
= aluSqrt(1.0f
/Device
->NumChan
);
748 for(i
= 0;i
< MAXCHANNELS
;i
++)
751 for(i2
= 0;i2
< MAXCHANNELS
;i2
++)
752 ALSource
->Params
.DryGains
[i
][i2
] = 0.0f
;
754 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
756 enum Channel chan
= Device
->Speaker2Chan
[i
];
757 ALfloat gain
= lerp(AmbientGain
, ChannelGain
[chan
], DirGain
);
758 ALSource
->Params
.DryGains
[0][chan
] = DryGain
* gain
;
761 for(i
= 0;i
< NumSends
;i
++)
762 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
];
764 /* Update filter coefficients. */
765 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
767 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
768 for(i
= 0;i
< NumSends
;i
++)
770 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
771 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
776 static __inline ALfloat
aluF2F(ALfloat val
)
778 static __inline ALint
aluF2I(ALfloat val
)
780 if(val
> 1.0f
) return 2147483647;
781 if(val
< -1.0f
) return -2147483647-1;
782 return fastf2i((ALfloat
)(val
*2147483647.0));
784 static __inline ALuint
aluF2UI(ALfloat val
)
785 { return aluF2I(val
)+2147483648u; }
786 static __inline ALshort
aluF2S(ALfloat val
)
787 { return aluF2I(val
)>>16; }
788 static __inline ALushort
aluF2US(ALfloat val
)
789 { return aluF2S(val
)+32768; }
790 static __inline ALbyte
aluF2B(ALfloat val
)
791 { return aluF2I(val
)>>24; }
792 static __inline ALubyte
aluF2UB(ALfloat val
)
793 { return aluF2B(val
)+128; }
795 #define DECL_TEMPLATE(T, N, func) \
796 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
797 ALuint SamplesToDo) \
799 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
800 const enum Channel *ChanMap = device->DevChannels; \
803 for(j = 0;j < N;j++) \
805 T *RESTRICT out = buffer + j; \
806 enum Channel chan = ChanMap[j]; \
808 for(i = 0;i < SamplesToDo;i++) \
809 out[i*N] = func(DryBuffer[i][chan]); \
813 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
814 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
815 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
816 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
817 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
818 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
820 DECL_TEMPLATE(ALuint
, 1, aluF2UI
)
821 DECL_TEMPLATE(ALuint
, 2, aluF2UI
)
822 DECL_TEMPLATE(ALuint
, 4, aluF2UI
)
823 DECL_TEMPLATE(ALuint
, 6, aluF2UI
)
824 DECL_TEMPLATE(ALuint
, 7, aluF2UI
)
825 DECL_TEMPLATE(ALuint
, 8, aluF2UI
)
827 DECL_TEMPLATE(ALint
, 1, aluF2I
)
828 DECL_TEMPLATE(ALint
, 2, aluF2I
)
829 DECL_TEMPLATE(ALint
, 4, aluF2I
)
830 DECL_TEMPLATE(ALint
, 6, aluF2I
)
831 DECL_TEMPLATE(ALint
, 7, aluF2I
)
832 DECL_TEMPLATE(ALint
, 8, aluF2I
)
834 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
835 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
836 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
837 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
838 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
839 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
841 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
842 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
843 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
844 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
845 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
846 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
848 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
849 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
850 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
851 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
852 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
853 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
855 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
856 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
857 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
858 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
859 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
860 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
864 #define DECL_TEMPLATE(T) \
865 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
867 switch(device->FmtChans) \
870 Write_##T##_1(device, buffer, SamplesToDo); \
873 Write_##T##_2(device, buffer, SamplesToDo); \
876 Write_##T##_4(device, buffer, SamplesToDo); \
879 case DevFmtX51Side: \
880 Write_##T##_6(device, buffer, SamplesToDo); \
883 Write_##T##_7(device, buffer, SamplesToDo); \
886 Write_##T##_8(device, buffer, SamplesToDo); \
891 DECL_TEMPLATE(ALfloat
)
892 DECL_TEMPLATE(ALuint
)
894 DECL_TEMPLATE(ALushort
)
895 DECL_TEMPLATE(ALshort
)
896 DECL_TEMPLATE(ALubyte
)
897 DECL_TEMPLATE(ALbyte
)
901 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
904 ALeffectslot
**slot
, **slot_end
;
905 ALsource
**src
, **src_end
;
910 fpuState
= SetMixerFPUMode();
914 /* Setup variables */
915 SamplesToDo
= minu(size
, BUFFERSIZE
);
917 /* Clear mixing buffer */
918 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
921 ctx
= device
->ContextList
;
924 ALenum DeferUpdates
= ctx
->DeferUpdates
;
925 ALenum UpdateSources
= AL_FALSE
;
928 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
930 src
= ctx
->ActiveSources
;
931 src_end
= src
+ ctx
->ActiveSourceCount
;
932 while(src
!= src_end
)
934 if((*src
)->state
!= AL_PLAYING
)
936 --(ctx
->ActiveSourceCount
);
941 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
943 ALsource_Update(*src
, ctx
);
945 MixSource(*src
, device
, SamplesToDo
);
949 /* effect slot processing */
950 slot
= ctx
->ActiveEffectSlots
;
951 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
952 while(slot
!= slot_end
)
954 for(c
= 0;c
< SamplesToDo
;c
++)
956 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
957 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
959 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
960 (*slot
)->PendingClicks
[0] = 0.0f
;
962 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
963 ALeffectState_Update((*slot
)->EffectState
, ctx
, *slot
);
965 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
966 (*slot
)->WetBuffer
, device
->DryBuffer
);
968 for(i
= 0;i
< SamplesToDo
;i
++)
969 (*slot
)->WetBuffer
[i
] = 0.0f
;
977 slot
= &device
->DefaultSlot
;
980 for(c
= 0;c
< SamplesToDo
;c
++)
982 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
983 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
985 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
986 (*slot
)->PendingClicks
[0] = 0.0f
;
988 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
989 ALeffectState_Update((*slot
)->EffectState
, ctx
, *slot
);
991 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
992 (*slot
)->WetBuffer
, device
->DryBuffer
);
994 for(i
= 0;i
< SamplesToDo
;i
++)
995 (*slot
)->WetBuffer
[i
] = 0.0f
;
997 UnlockDevice(device
);
999 //Post processing loop
1000 if(device
->FmtChans
== DevFmtMono
)
1002 for(i
= 0;i
< SamplesToDo
;i
++)
1004 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
1005 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] * (1.0f
/256.0f
);
1007 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1008 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1010 else if(device
->FmtChans
== DevFmtStereo
)
1012 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1013 for(i
= 0;i
< SamplesToDo
;i
++)
1015 for(c
= 0;c
< 2;c
++)
1017 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1018 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1021 for(c
= 0;c
< 2;c
++)
1023 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1024 device
->PendingClicks
[c
] = 0.0f
;
1028 for(i
= 0;i
< SamplesToDo
;i
++)
1029 bs2b_cross_feed(device
->Bs2b
, &device
->DryBuffer
[i
][0]);
1034 for(i
= 0;i
< SamplesToDo
;i
++)
1036 for(c
= 0;c
< MAXCHANNELS
;c
++)
1038 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1039 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1042 for(c
= 0;c
< MAXCHANNELS
;c
++)
1044 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1045 device
->PendingClicks
[c
] = 0.0f
;
1051 switch(device
->FmtType
)
1054 Write_ALbyte(device
, buffer
, SamplesToDo
);
1057 Write_ALubyte(device
, buffer
, SamplesToDo
);
1060 Write_ALshort(device
, buffer
, SamplesToDo
);
1063 Write_ALushort(device
, buffer
, SamplesToDo
);
1066 Write_ALint(device
, buffer
, SamplesToDo
);
1069 Write_ALuint(device
, buffer
, SamplesToDo
);
1072 Write_ALfloat(device
, buffer
, SamplesToDo
);
1077 size
-= SamplesToDo
;
1080 RestoreFPUMode(fpuState
);
1084 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1086 ALCcontext
*Context
;
1089 device
->Connected
= ALC_FALSE
;
1091 Context
= device
->ContextList
;
1094 ALsource
**src
, **src_end
;
1096 src
= Context
->ActiveSources
;
1097 src_end
= src
+ Context
->ActiveSourceCount
;
1098 while(src
!= src_end
)
1100 if((*src
)->state
== AL_PLAYING
)
1102 (*src
)->state
= AL_STOPPED
;
1103 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1104 (*src
)->position
= 0;
1105 (*src
)->position_fraction
= 0;
1109 Context
->ActiveSourceCount
= 0;
1111 Context
= Context
->next
;
1113 UnlockDevice(device
);