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"
39 #include "mixer_defs.h"
48 ALfloat ConeScale
= 1.0f
;
50 /* Localized Z scalar for mono sources */
51 ALfloat ZScale
= 1.0f
;
54 static ResamplerFunc
SelectResampler(enum Resampler Resampler
, ALuint increment
)
56 if(increment
== FRACTIONONE
)
57 return Resample_point32_C
;
61 return Resample_point32_C
;
64 if((CPUCapFlags
&CPU_CAP_SSE
))
65 return Resample_lerp32_SSE
;
67 return Resample_lerp32_C
;
70 if((CPUCapFlags
&CPU_CAP_SSE
))
71 return Resample_cubic32_SSE
;
73 return Resample_cubic32_C
;
75 /* Shouldn't happen */
79 return Resample_point32_C
;
83 static DryMixerFunc
SelectDirectMixer(void)
86 if((CPUCapFlags
&CPU_CAP_SSE
))
90 if((CPUCapFlags
&CPU_CAP_NEON
))
91 return MixDirect_Neon
;
97 static DryMixerFunc
SelectHrtfMixer(void)
100 if((CPUCapFlags
&CPU_CAP_SSE
))
101 return MixDirect_Hrtf_SSE
;
104 if((CPUCapFlags
&CPU_CAP_NEON
))
105 return MixDirect_Hrtf_Neon
;
108 return MixDirect_Hrtf_C
;
111 static WetMixerFunc
SelectSendMixer(void)
114 if((CPUCapFlags
&CPU_CAP_SSE
))
118 if((CPUCapFlags
&CPU_CAP_NEON
))
126 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
129 vector
[0], vector
[1], vector
[2], w
132 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
133 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
134 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
138 ALvoid
CalcNonAttnSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
140 static const struct ChanMap MonoMap
[1] = { { FrontCenter
, 0.0f
} };
141 static const struct ChanMap StereoMap
[2] = {
142 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
143 { FrontRight
, 30.0f
* F_PI
/180.0f
}
145 static const struct ChanMap StereoWideMap
[2] = {
146 { FrontLeft
, -90.0f
* F_PI
/180.0f
},
147 { FrontRight
, 90.0f
* F_PI
/180.0f
}
149 static const struct ChanMap RearMap
[2] = {
150 { BackLeft
, -150.0f
* F_PI
/180.0f
},
151 { BackRight
, 150.0f
* F_PI
/180.0f
}
153 static const struct ChanMap QuadMap
[4] = {
154 { FrontLeft
, -45.0f
* F_PI
/180.0f
},
155 { FrontRight
, 45.0f
* F_PI
/180.0f
},
156 { BackLeft
, -135.0f
* F_PI
/180.0f
},
157 { BackRight
, 135.0f
* F_PI
/180.0f
}
159 static const struct ChanMap X51Map
[6] = {
160 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
161 { FrontRight
, 30.0f
* F_PI
/180.0f
},
162 { FrontCenter
, 0.0f
* F_PI
/180.0f
},
164 { BackLeft
, -110.0f
* F_PI
/180.0f
},
165 { BackRight
, 110.0f
* F_PI
/180.0f
}
167 static const struct ChanMap X61Map
[7] = {
168 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
169 { FrontRight
, 30.0f
* F_PI
/180.0f
},
170 { FrontCenter
, 0.0f
* F_PI
/180.0f
},
172 { BackCenter
, 180.0f
* F_PI
/180.0f
},
173 { SideLeft
, -90.0f
* F_PI
/180.0f
},
174 { SideRight
, 90.0f
* F_PI
/180.0f
}
176 static const struct ChanMap X71Map
[8] = {
177 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
178 { FrontRight
, 30.0f
* F_PI
/180.0f
},
179 { FrontCenter
, 0.0f
* F_PI
/180.0f
},
181 { BackLeft
, -150.0f
* F_PI
/180.0f
},
182 { BackRight
, 150.0f
* F_PI
/180.0f
},
183 { SideLeft
, -90.0f
* F_PI
/180.0f
},
184 { SideRight
, 90.0f
* F_PI
/180.0f
}
187 ALCdevice
*Device
= ALContext
->Device
;
188 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
189 ALbufferlistitem
*BufferListItem
;
190 enum FmtChannels Channels
;
191 ALfloat (*SrcMatrix
)[MaxChannels
];
192 ALfloat DryGain
, DryGainHF
;
193 ALfloat WetGain
[MAX_SENDS
];
194 ALfloat WetGainHF
[MAX_SENDS
];
195 ALint NumSends
, Frequency
;
196 const struct ChanMap
*chans
= NULL
;
197 enum Resampler Resampler
;
198 ALint num_channels
= 0;
199 ALboolean DirectChannels
;
200 ALfloat hwidth
= 0.0f
;
205 /* Get device properties */
206 NumSends
= Device
->NumAuxSends
;
207 Frequency
= Device
->Frequency
;
209 /* Get listener properties */
210 ListenerGain
= ALContext
->Listener
.Gain
;
212 /* Get source properties */
213 SourceVolume
= ALSource
->Gain
;
214 MinVolume
= ALSource
->MinGain
;
215 MaxVolume
= ALSource
->MaxGain
;
216 Pitch
= ALSource
->Pitch
;
217 Resampler
= ALSource
->Resampler
;
218 DirectChannels
= ALSource
->DirectChannels
;
220 /* Calculate the stepping value */
222 BufferListItem
= ALSource
->queue
;
223 while(BufferListItem
!= NULL
)
226 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
228 ALsizei maxstep
= BUFFERSIZE
/ ALSource
->NumChannels
;
229 maxstep
-= ResamplerPadding
[Resampler
] +
230 ResamplerPrePadding
[Resampler
] + 1;
231 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
233 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
234 if(Pitch
> (ALfloat
)maxstep
)
235 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
238 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
239 if(ALSource
->Params
.Step
== 0)
240 ALSource
->Params
.Step
= 1;
242 ALSource
->Params
.Resample
= SelectResampler(Resampler
, ALSource
->Params
.Step
);
244 Channels
= ALBuffer
->FmtChannels
;
247 BufferListItem
= BufferListItem
->next
;
249 if(!DirectChannels
&& Device
->Hrtf
)
250 ALSource
->Params
.DryMix
= SelectHrtfMixer();
252 ALSource
->Params
.DryMix
= SelectDirectMixer();
253 ALSource
->Params
.WetMix
= SelectSendMixer();
255 /* Calculate gains */
256 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
257 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
258 DryGainHF
= ALSource
->DirectGainHF
;
259 for(i
= 0;i
< NumSends
;i
++)
261 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
262 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
263 WetGainHF
[i
] = ALSource
->Send
[i
].GainHF
;
266 SrcMatrix
= ALSource
->Params
.Direct
.Gains
;
267 for(i
= 0;i
< MaxChannels
;i
++)
269 for(c
= 0;c
< MaxChannels
;c
++)
270 SrcMatrix
[i
][c
] = 0.0f
;
280 if(!(Device
->Flags
&DEVICE_WIDE_STEREO
))
284 chans
= StereoWideMap
;
285 hwidth
= 60.0f
* F_PI
/180.0f
;
316 if(DirectChannels
!= AL_FALSE
)
318 for(c
= 0;c
< num_channels
;c
++)
320 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
322 enum Channel chan
= Device
->Speaker2Chan
[i
];
323 if(chan
== chans
[c
].channel
)
325 SrcMatrix
[c
][chan
] += DryGain
;
331 else if(Device
->Hrtf
)
333 for(c
= 0;c
< num_channels
;c
++)
335 if(chans
[c
].channel
== LFE
)
338 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
][0] = 0;
339 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
][1] = 0;
340 for(i
= 0;i
< HRIR_LENGTH
;i
++)
342 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
][i
][0] = 0.0f
;
343 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
][i
][1] = 0.0f
;
348 /* Get the static HRIR coefficients and delays for this
350 GetLerpedHrtfCoeffs(Device
->Hrtf
,
351 0.0f
, chans
[c
].angle
, DryGain
,
352 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
],
353 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
]);
356 ALSource
->Hrtf
.Counter
= 0;
360 DryGain
*= lerp(1.0f
, 1.0f
/sqrtf(Device
->NumChan
), hwidth
/(F_PI
*2.0f
));
361 for(c
= 0;c
< num_channels
;c
++)
363 /* Special-case LFE */
364 if(chans
[c
].channel
== LFE
)
366 SrcMatrix
[c
][chans
[c
].channel
] = DryGain
;
369 ComputeAngleGains(Device
, chans
[c
].angle
, hwidth
, DryGain
,
373 for(i
= 0;i
< NumSends
;i
++)
375 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
378 Slot
= Device
->DefaultSlot
;
379 if(Slot
&& Slot
->effect
.type
== AL_EFFECT_NULL
)
381 ALSource
->Params
.Send
[i
].Slot
= Slot
;
382 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
385 /* Update filter coefficients. Calculations based on the I3DL2
387 cw
= cosf(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
389 /* We use two chained one-pole filters, so we need to take the
390 * square root of the squared gain, which is the same as the base
392 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
393 for(i
= 0;i
< NumSends
;i
++)
395 ALfloat a
= lpCoeffCalc(WetGainHF
[i
], cw
);
396 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
400 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
402 const ALCdevice
*Device
= ALContext
->Device
;
403 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
404 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
405 ALfloat Velocity
[3],ListenerVel
[3];
406 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
407 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
408 ALfloat DopplerFactor
, SpeedOfSound
;
409 ALfloat AirAbsorptionFactor
;
410 ALfloat RoomAirAbsorption
[MAX_SENDS
];
411 ALbufferlistitem
*BufferListItem
;
413 ALfloat RoomAttenuation
[MAX_SENDS
];
414 ALfloat MetersPerUnit
;
415 ALfloat RoomRolloffBase
;
416 ALfloat RoomRolloff
[MAX_SENDS
];
417 ALfloat DecayDistance
[MAX_SENDS
];
420 ALboolean DryGainHFAuto
;
421 ALfloat WetGain
[MAX_SENDS
];
422 ALfloat WetGainHF
[MAX_SENDS
];
423 ALboolean WetGainAuto
;
424 ALboolean WetGainHFAuto
;
425 enum Resampler Resampler
;
426 ALfloat Matrix
[4][4];
434 for(i
= 0;i
< MAX_SENDS
;i
++)
437 /* Get context/device properties */
438 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
439 SpeedOfSound
= ALContext
->SpeedOfSound
* ALContext
->DopplerVelocity
;
440 NumSends
= Device
->NumAuxSends
;
441 Frequency
= Device
->Frequency
;
443 /* Get listener properties */
444 ListenerGain
= ALContext
->Listener
.Gain
;
445 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
446 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
447 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
448 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
452 Matrix
[i
][j
] = ALContext
->Listener
.Matrix
[i
][j
];
455 /* Get source properties */
456 SourceVolume
= ALSource
->Gain
;
457 MinVolume
= ALSource
->MinGain
;
458 MaxVolume
= ALSource
->MaxGain
;
459 Pitch
= ALSource
->Pitch
;
460 Resampler
= ALSource
->Resampler
;
461 Position
[0] = ALSource
->Position
[0];
462 Position
[1] = ALSource
->Position
[1];
463 Position
[2] = ALSource
->Position
[2];
464 Direction
[0] = ALSource
->Orientation
[0];
465 Direction
[1] = ALSource
->Orientation
[1];
466 Direction
[2] = ALSource
->Orientation
[2];
467 Velocity
[0] = ALSource
->Velocity
[0];
468 Velocity
[1] = ALSource
->Velocity
[1];
469 Velocity
[2] = ALSource
->Velocity
[2];
470 MinDist
= ALSource
->RefDistance
;
471 MaxDist
= ALSource
->MaxDistance
;
472 Rolloff
= ALSource
->RollOffFactor
;
473 InnerAngle
= ALSource
->InnerAngle
;
474 OuterAngle
= ALSource
->OuterAngle
;
475 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
476 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
477 WetGainAuto
= ALSource
->WetGainAuto
;
478 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
479 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
480 for(i
= 0;i
< NumSends
;i
++)
482 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
485 Slot
= Device
->DefaultSlot
;
486 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
489 RoomRolloff
[i
] = 0.0f
;
490 DecayDistance
[i
] = 0.0f
;
491 RoomAirAbsorption
[i
] = 1.0f
;
493 else if(Slot
->AuxSendAuto
)
495 RoomRolloff
[i
] = RoomRolloffBase
;
496 if(IsReverbEffect(Slot
->effect
.type
))
498 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
499 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
500 SPEEDOFSOUNDMETRESPERSEC
;
501 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
505 DecayDistance
[i
] = 0.0f
;
506 RoomAirAbsorption
[i
] = 1.0f
;
511 /* If the slot's auxiliary send auto is off, the data sent to the
512 * effect slot is the same as the dry path, sans filter effects */
513 RoomRolloff
[i
] = Rolloff
;
514 DecayDistance
[i
] = 0.0f
;
515 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
518 ALSource
->Params
.Send
[i
].Slot
= Slot
;
521 /* Transform source to listener space (convert to head relative) */
522 if(ALSource
->HeadRelative
== AL_FALSE
)
524 /* Translate position */
525 Position
[0] -= ALContext
->Listener
.Position
[0];
526 Position
[1] -= ALContext
->Listener
.Position
[1];
527 Position
[2] -= ALContext
->Listener
.Position
[2];
529 /* Transform source vectors */
530 aluMatrixVector(Position
, 1.0f
, Matrix
);
531 aluMatrixVector(Direction
, 0.0f
, Matrix
);
532 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
533 /* Transform listener velocity */
534 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
538 /* Transform listener velocity from world space to listener space */
539 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
540 /* Offset the source velocity to be relative of the listener velocity */
541 Velocity
[0] += ListenerVel
[0];
542 Velocity
[1] += ListenerVel
[1];
543 Velocity
[2] += ListenerVel
[2];
546 SourceToListener
[0] = -Position
[0];
547 SourceToListener
[1] = -Position
[1];
548 SourceToListener
[2] = -Position
[2];
549 aluNormalize(SourceToListener
);
550 aluNormalize(Direction
);
552 /* Calculate distance attenuation */
553 Distance
= sqrtf(aluDotproduct(Position
, Position
));
554 ClampedDist
= Distance
;
557 for(i
= 0;i
< NumSends
;i
++)
558 RoomAttenuation
[i
] = 1.0f
;
559 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
560 ALContext
->DistanceModel
)
562 case InverseDistanceClamped
:
563 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
564 if(MaxDist
< MinDist
)
567 case InverseDistance
:
570 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
571 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
572 for(i
= 0;i
< NumSends
;i
++)
574 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
575 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
580 case LinearDistanceClamped
:
581 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
582 if(MaxDist
< MinDist
)
586 if(MaxDist
!= MinDist
)
588 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
589 Attenuation
= maxf(Attenuation
, 0.0f
);
590 for(i
= 0;i
< NumSends
;i
++)
592 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
593 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
598 case ExponentDistanceClamped
:
599 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
600 if(MaxDist
< MinDist
)
603 case ExponentDistance
:
604 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
606 Attenuation
= powf(ClampedDist
/MinDist
, -Rolloff
);
607 for(i
= 0;i
< NumSends
;i
++)
608 RoomAttenuation
[i
] = powf(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
612 case DisableDistance
:
613 ClampedDist
= MinDist
;
617 /* Source Gain + Attenuation */
618 DryGain
= SourceVolume
* Attenuation
;
619 for(i
= 0;i
< NumSends
;i
++)
620 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
622 /* Distance-based air absorption */
623 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
625 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
626 DryGainHF
*= powf(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
627 for(i
= 0;i
< NumSends
;i
++)
628 WetGainHF
[i
] *= powf(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
633 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
635 /* Apply a decay-time transformation to the wet path, based on the
636 * attenuation of the dry path.
638 * Using the apparent distance, based on the distance attenuation, the
639 * initial decay of the reverb effect is calculated and applied to the
642 for(i
= 0;i
< NumSends
;i
++)
644 if(DecayDistance
[i
] > 0.0f
)
645 WetGain
[i
] *= powf(0.001f
/*-60dB*/, ApparentDist
/DecayDistance
[i
]);
649 /* Calculate directional soundcones */
650 Angle
= acosf(aluDotproduct(Direction
,SourceToListener
)) * ConeScale
* (360.0f
/F_PI
);
651 if(Angle
> InnerAngle
&& Angle
<= OuterAngle
)
653 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
654 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
655 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
657 else if(Angle
> OuterAngle
)
659 ConeVolume
= ALSource
->OuterGain
;
660 ConeHF
= ALSource
->OuterGainHF
;
668 DryGain
*= ConeVolume
;
671 for(i
= 0;i
< NumSends
;i
++)
672 WetGain
[i
] *= ConeVolume
;
678 for(i
= 0;i
< NumSends
;i
++)
679 WetGainHF
[i
] *= ConeHF
;
682 /* Clamp to Min/Max Gain */
683 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
684 for(i
= 0;i
< NumSends
;i
++)
685 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
687 /* Apply gain and frequency filters */
688 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
689 DryGainHF
*= ALSource
->DirectGainHF
;
690 for(i
= 0;i
< NumSends
;i
++)
692 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
693 WetGainHF
[i
] *= ALSource
->Send
[i
].GainHF
;
696 /* Calculate velocity-based doppler effect */
697 if(DopplerFactor
> 0.0f
)
701 if(SpeedOfSound
< 1.0f
)
703 DopplerFactor
*= 1.0f
/SpeedOfSound
;
707 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
708 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
710 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
711 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
714 BufferListItem
= ALSource
->queue
;
715 while(BufferListItem
!= NULL
)
718 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
720 /* Calculate fixed-point stepping value, based on the pitch, buffer
721 * frequency, and output frequency. */
722 ALsizei maxstep
= BUFFERSIZE
/ ALSource
->NumChannels
;
723 maxstep
-= ResamplerPadding
[Resampler
] +
724 ResamplerPrePadding
[Resampler
] + 1;
725 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
727 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
728 if(Pitch
> (ALfloat
)maxstep
)
729 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
732 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
733 if(ALSource
->Params
.Step
== 0)
734 ALSource
->Params
.Step
= 1;
736 ALSource
->Params
.Resample
= SelectResampler(Resampler
, ALSource
->Params
.Step
);
740 BufferListItem
= BufferListItem
->next
;
743 ALSource
->Params
.DryMix
= SelectHrtfMixer();
745 ALSource
->Params
.DryMix
= SelectDirectMixer();
746 ALSource
->Params
.WetMix
= SelectSendMixer();
750 /* Use a binaural HRTF algorithm for stereo headphone playback */
751 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
755 ALfloat invlen
= 1.0f
/Distance
;
756 Position
[0] *= invlen
;
757 Position
[1] *= invlen
;
758 Position
[2] *= invlen
;
760 /* Calculate elevation and azimuth only when the source is not at
761 * the listener. This prevents +0 and -0 Z from producing
762 * inconsistent panning. Also, clamp Y in case FP precision errors
763 * cause it to land outside of -1..+1. */
764 ev
= asinf(clampf(Position
[1], -1.0f
, 1.0f
));
765 az
= atan2f(Position
[0], -Position
[2]*ZScale
);
768 /* Check to see if the HRIR is already moving. */
769 if(ALSource
->Hrtf
.Moving
)
771 /* Calculate the normalized HRTF transition factor (delta). */
772 delta
= CalcHrtfDelta(ALSource
->Params
.Direct
.Hrtf
.Gain
, DryGain
,
773 ALSource
->Params
.Direct
.Hrtf
.Dir
, Position
);
774 /* If the delta is large enough, get the moving HRIR target
775 * coefficients, target delays, steppping values, and counter. */
778 ALSource
->Hrtf
.Counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
779 ev
, az
, DryGain
, delta
,
780 ALSource
->Hrtf
.Counter
,
781 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
782 ALSource
->Params
.Direct
.Hrtf
.Delay
[0],
783 ALSource
->Params
.Direct
.Hrtf
.CoeffStep
,
784 ALSource
->Params
.Direct
.Hrtf
.DelayStep
);
785 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
786 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
787 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
788 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
793 /* Get the initial (static) HRIR coefficients and delays. */
794 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
795 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
796 ALSource
->Params
.Direct
.Hrtf
.Delay
[0]);
797 ALSource
->Hrtf
.Counter
= 0;
798 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
799 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
800 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
801 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
806 ALfloat (*Matrix
)[MaxChannels
] = ALSource
->Params
.Direct
.Gains
;
807 ALfloat DirGain
= 0.0f
;
810 for(i
= 0;i
< MaxChannels
;i
++)
812 for(j
= 0;j
< MaxChannels
;j
++)
816 /* Normalize the length, and compute panned gains. */
819 ALfloat invlen
= 1.0f
/Distance
;
820 Position
[0] *= invlen
;
821 Position
[1] *= invlen
;
822 Position
[2] *= invlen
;
824 DirGain
= sqrtf(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
825 ComputeAngleGains(Device
, atan2f(Position
[0], -Position
[2]*ZScale
), 0.0f
,
826 DryGain
*DirGain
, Matrix
[0]);
829 /* Adjustment for vertical offsets. Not the greatest, but simple
831 AmbientGain
= DryGain
* sqrtf(1.0f
/Device
->NumChan
) * (1.0f
-DirGain
);
832 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
834 enum Channel chan
= Device
->Speaker2Chan
[i
];
835 Matrix
[0][chan
] = maxf(Matrix
[0][chan
], AmbientGain
);
838 for(i
= 0;i
< NumSends
;i
++)
839 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
841 /* Update filter coefficients. */
842 cw
= cosf(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
844 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
845 for(i
= 0;i
< NumSends
;i
++)
847 ALfloat a
= lpCoeffCalc(WetGainHF
[i
], cw
);
848 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
853 static __inline ALfloat
aluF2F(ALfloat val
)
855 static __inline ALint
aluF2I(ALfloat val
)
857 if(val
> 1.0f
) return 2147483647;
858 if(val
< -1.0f
) return -2147483647-1;
859 return fastf2i((ALfloat
)(val
*2147483647.0));
861 static __inline ALuint
aluF2UI(ALfloat val
)
862 { return aluF2I(val
)+2147483648u; }
863 static __inline ALshort
aluF2S(ALfloat val
)
864 { return aluF2I(val
)>>16; }
865 static __inline ALushort
aluF2US(ALfloat val
)
866 { return aluF2S(val
)+32768; }
867 static __inline ALbyte
aluF2B(ALfloat val
)
868 { return aluF2I(val
)>>24; }
869 static __inline ALubyte
aluF2UB(ALfloat val
)
870 { return aluF2B(val
)+128; }
872 #define DECL_TEMPLATE(T, func) \
873 static void Write_##T(ALCdevice *device, T *RESTRICT buffer, \
874 ALuint SamplesToDo) \
876 ALfloat (*RESTRICT DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
877 ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
878 const enum Channel *ChanMap = device->DevChannels; \
881 for(j = 0;j < numchans;j++) \
883 T *RESTRICT out = buffer + j; \
884 enum Channel chan = ChanMap[j]; \
886 for(i = 0;i < SamplesToDo;i++) \
887 out[i*numchans] = func(DryBuffer[chan][i]); \
891 DECL_TEMPLATE(ALfloat
, aluF2F
)
892 DECL_TEMPLATE(ALuint
, aluF2UI
)
893 DECL_TEMPLATE(ALint
, aluF2I
)
894 DECL_TEMPLATE(ALushort
, aluF2US
)
895 DECL_TEMPLATE(ALshort
, aluF2S
)
896 DECL_TEMPLATE(ALubyte
, aluF2UB
)
897 DECL_TEMPLATE(ALbyte
, aluF2B
)
902 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
905 ALeffectslot
**slot
, **slot_end
;
906 ALsource
**src
, **src_end
;
911 SetMixerFPUMode(&oldMode
);
915 SamplesToDo
= minu(size
, BUFFERSIZE
);
916 for(c
= 0;c
< MaxChannels
;c
++)
917 memset(device
->DryBuffer
[c
], 0, SamplesToDo
*sizeof(ALfloat
));
919 ALCdevice_Lock(device
);
920 ctx
= device
->ContextList
;
923 ALenum DeferUpdates
= ctx
->DeferUpdates
;
924 ALenum UpdateSources
= AL_FALSE
;
927 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
929 /* source processing */
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
, device
, *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
, device
, *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 ALCdevice_Unlock(device
);
999 /* Click-removal. Could do better; this only really handles immediate
1000 * changes between updates where a predictive sample could be
1001 * generated. Delays caused by effects and HRTF aren't caught. */
1002 if(device
->FmtChans
== DevFmtMono
)
1004 for(i
= 0;i
< SamplesToDo
;i
++)
1006 device
->DryBuffer
[FrontCenter
][i
] += device
->ClickRemoval
[FrontCenter
];
1007 device
->ClickRemoval
[FrontCenter
] -= device
->ClickRemoval
[FrontCenter
] * (1.0f
/256.0f
);
1009 device
->ClickRemoval
[FrontCenter
] += device
->PendingClicks
[FrontCenter
];
1010 device
->PendingClicks
[FrontCenter
] = 0.0f
;
1012 else if(device
->FmtChans
== DevFmtStereo
)
1014 /* Assumes the first two channels are FrontLeft and FrontRight */
1015 for(c
= 0;c
< 2;c
++)
1017 ALfloat offset
= device
->ClickRemoval
[c
];
1018 for(i
= 0;i
< SamplesToDo
;i
++)
1020 device
->DryBuffer
[c
][i
] += offset
;
1021 offset
-= offset
* (1.0f
/256.0f
);
1023 device
->ClickRemoval
[c
] = offset
+ device
->PendingClicks
[c
];
1024 device
->PendingClicks
[c
] = 0.0f
;
1029 for(i
= 0;i
< SamplesToDo
;i
++)
1031 samples
[0] = device
->DryBuffer
[FrontLeft
][i
];
1032 samples
[1] = device
->DryBuffer
[FrontRight
][i
];
1033 bs2b_cross_feed(device
->Bs2b
, samples
);
1034 device
->DryBuffer
[FrontLeft
][i
] = samples
[0];
1035 device
->DryBuffer
[FrontRight
][i
] = samples
[1];
1041 for(c
= 0;c
< MaxChannels
;c
++)
1043 ALfloat offset
= device
->ClickRemoval
[c
];
1044 for(i
= 0;i
< SamplesToDo
;i
++)
1046 device
->DryBuffer
[c
][i
] += offset
;
1047 offset
-= offset
* (1.0f
/256.0f
);
1049 device
->ClickRemoval
[c
] = offset
+ device
->PendingClicks
[c
];
1050 device
->PendingClicks
[c
] = 0.0f
;
1056 switch(device
->FmtType
)
1059 Write_ALbyte(device
, buffer
, SamplesToDo
);
1062 Write_ALubyte(device
, buffer
, SamplesToDo
);
1065 Write_ALshort(device
, buffer
, SamplesToDo
);
1068 Write_ALushort(device
, buffer
, SamplesToDo
);
1071 Write_ALint(device
, buffer
, SamplesToDo
);
1074 Write_ALuint(device
, buffer
, SamplesToDo
);
1077 Write_ALfloat(device
, buffer
, SamplesToDo
);
1082 size
-= SamplesToDo
;
1085 RestoreFPUMode(&oldMode
);
1089 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1091 ALCcontext
*Context
;
1093 ALCdevice_Lock(device
);
1094 device
->Connected
= ALC_FALSE
;
1096 Context
= device
->ContextList
;
1099 ALsource
**src
, **src_end
;
1101 src
= Context
->ActiveSources
;
1102 src_end
= src
+ Context
->ActiveSourceCount
;
1103 while(src
!= src_end
)
1105 if((*src
)->state
== AL_PLAYING
)
1107 (*src
)->state
= AL_STOPPED
;
1108 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1109 (*src
)->position
= 0;
1110 (*src
)->position_fraction
= 0;
1114 Context
->ActiveSourceCount
= 0;
1116 Context
= Context
->next
;
1118 ALCdevice_Unlock(device
);