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
++)
254 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
256 enum Channel chan
= Device
->Speaker2Chan
[i
];
257 if(chan
== chans
[c
].channel
)
259 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
;
265 else if(Device
->Hrtf
)
267 for(c
= 0;c
< num_channels
;c
++)
269 if(chans
[c
].channel
== LFE
)
272 ALSource
->Params
.HrtfDelay
[c
][0] = 0;
273 ALSource
->Params
.HrtfDelay
[c
][1] = 0;
274 for(i
= 0;i
< HRIR_LENGTH
;i
++)
276 ALSource
->Params
.HrtfCoeffs
[c
][i
][0] = 0.0f
;
277 ALSource
->Params
.HrtfCoeffs
[c
][i
][1] = 0.0f
;
282 /* Get the static HRIR coefficients and delays for this
284 GetLerpedHrtfCoeffs(Device
->Hrtf
,
285 0.0f
, chans
[c
].angle
,
286 DryGain
*ListenerGain
,
287 ALSource
->Params
.HrtfCoeffs
[c
],
288 ALSource
->Params
.HrtfDelay
[c
]);
290 ALSource
->HrtfCounter
= 0;
295 for(c
= 0;c
< num_channels
;c
++)
297 if(chans
[c
].channel
== LFE
) /* Special-case LFE */
299 SrcMatrix
[c
][LFE
] += DryGain
* ListenerGain
;
302 pos
= aluCart2LUTpos(aluCos(chans
[c
].angle
), aluSin(chans
[c
].angle
));
303 ChannelGain
= Device
->PanningLUT
[pos
];
305 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
307 enum Channel chan
= Device
->Speaker2Chan
[i
];
308 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
313 for(i
= 0;i
< NumSends
;i
++)
315 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
318 Slot
= Device
->DefaultSlot
;
319 if(Slot
&& Slot
->effect
.type
== AL_EFFECT_NULL
)
321 ALSource
->Params
.Send
[i
].Slot
= Slot
;
322 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
] * ListenerGain
;
325 /* Update filter coefficients. Calculations based on the I3DL2
327 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
329 /* We use two chained one-pole filters, so we need to take the
330 * square root of the squared gain, which is the same as the base
332 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
333 for(i
= 0;i
< NumSends
;i
++)
335 /* We use a one-pole filter, so we need to take the squared gain */
336 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
337 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
341 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
343 const ALCdevice
*Device
= ALContext
->Device
;
344 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
345 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
346 ALfloat Velocity
[3],ListenerVel
[3];
347 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
348 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
349 ALfloat DopplerFactor
, SpeedOfSound
;
350 ALfloat AirAbsorptionFactor
;
351 ALfloat RoomAirAbsorption
[MAX_SENDS
];
352 ALbufferlistitem
*BufferListItem
;
353 ALfloat Attenuation
, EffectiveDist
;
354 ALfloat RoomAttenuation
[MAX_SENDS
];
355 ALfloat MetersPerUnit
;
356 ALfloat RoomRolloffBase
;
357 ALfloat RoomRolloff
[MAX_SENDS
];
358 ALfloat DecayDistance
[MAX_SENDS
];
361 ALboolean DryGainHFAuto
;
362 ALfloat WetGain
[MAX_SENDS
];
363 ALfloat WetGainHF
[MAX_SENDS
];
364 ALboolean WetGainAuto
;
365 ALboolean WetGainHFAuto
;
366 enum Resampler Resampler
;
367 ALfloat Matrix
[4][4];
375 for(i
= 0;i
< MAX_SENDS
;i
++)
378 //Get context properties
379 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
380 SpeedOfSound
= ALContext
->flSpeedOfSound
* ALContext
->DopplerVelocity
;
381 NumSends
= Device
->NumAuxSends
;
382 Frequency
= Device
->Frequency
;
384 //Get listener properties
385 ListenerGain
= ALContext
->Listener
.Gain
;
386 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
387 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
388 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
389 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
391 //Get source properties
392 SourceVolume
= ALSource
->flGain
;
393 MinVolume
= ALSource
->flMinGain
;
394 MaxVolume
= ALSource
->flMaxGain
;
395 Pitch
= ALSource
->flPitch
;
396 Resampler
= ALSource
->Resampler
;
397 Position
[0] = ALSource
->vPosition
[0];
398 Position
[1] = ALSource
->vPosition
[1];
399 Position
[2] = ALSource
->vPosition
[2];
400 Direction
[0] = ALSource
->vOrientation
[0];
401 Direction
[1] = ALSource
->vOrientation
[1];
402 Direction
[2] = ALSource
->vOrientation
[2];
403 Velocity
[0] = ALSource
->vVelocity
[0];
404 Velocity
[1] = ALSource
->vVelocity
[1];
405 Velocity
[2] = ALSource
->vVelocity
[2];
406 MinDist
= ALSource
->flRefDistance
;
407 MaxDist
= ALSource
->flMaxDistance
;
408 Rolloff
= ALSource
->flRollOffFactor
;
409 InnerAngle
= ALSource
->flInnerAngle
* ConeScale
;
410 OuterAngle
= ALSource
->flOuterAngle
* ConeScale
;
411 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
412 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
413 WetGainAuto
= ALSource
->WetGainAuto
;
414 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
415 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
416 for(i
= 0;i
< NumSends
;i
++)
418 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
421 Slot
= Device
->DefaultSlot
;
422 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
425 RoomRolloff
[i
] = 0.0f
;
426 DecayDistance
[i
] = 0.0f
;
427 RoomAirAbsorption
[i
] = 1.0f
;
429 else if(Slot
->AuxSendAuto
)
431 RoomRolloff
[i
] = RoomRolloffBase
;
432 if(IsReverbEffect(Slot
->effect
.type
))
434 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
435 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
436 SPEEDOFSOUNDMETRESPERSEC
;
437 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
441 DecayDistance
[i
] = 0.0f
;
442 RoomAirAbsorption
[i
] = 1.0f
;
447 /* If the slot's auxiliary send auto is off, the data sent to the
448 * effect slot is the same as the dry path, sans filter effects */
449 RoomRolloff
[i
] = Rolloff
;
450 DecayDistance
[i
] = 0.0f
;
451 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
454 ALSource
->Params
.Send
[i
].Slot
= Slot
;
460 Matrix
[i
][j
] = ALContext
->Listener
.Matrix
[i
][j
];
463 //1. Translate Listener to origin (convert to head relative)
464 if(ALSource
->bHeadRelative
== AL_FALSE
)
466 /* Translate position */
467 Position
[0] -= ALContext
->Listener
.Position
[0];
468 Position
[1] -= ALContext
->Listener
.Position
[1];
469 Position
[2] -= ALContext
->Listener
.Position
[2];
471 /* Transform source vectors into listener space */
472 aluMatrixVector(Position
, 1.0f
, Matrix
);
473 aluMatrixVector(Direction
, 0.0f
, Matrix
);
474 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
475 /* Transform listener velocity into listener space */
476 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
480 /* Transform listener velocity into listener space */
481 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
482 /* Offset the source velocity to be relative of the listener velocity */
483 Velocity
[0] += ListenerVel
[0];
484 Velocity
[1] += ListenerVel
[1];
485 Velocity
[2] += ListenerVel
[2];
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
);
577 /* Apply a decay-time transformation to the wet path, based on the
578 * attenuation of the dry path.
580 * Using the approximate (effective) source to listener distance, the
581 * initial decay of the reverb effect is calculated and applied to the
584 for(i
= 0;i
< NumSends
;i
++)
586 if(DecayDistance
[i
] > 0.0f
)
587 WetGain
[i
] *= aluPow(0.001f
/* -60dB */,
588 EffectiveDist
/ DecayDistance
[i
]);
592 /* Calculate directional soundcones */
593 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * (180.0f
/F_PI
);
594 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
596 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
597 ConeVolume
= lerp(1.0f
, ALSource
->flOuterGain
, scale
);
598 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
600 else if(Angle
> OuterAngle
)
602 ConeVolume
= ALSource
->flOuterGain
;
603 ConeHF
= ALSource
->OuterGainHF
;
611 DryGain
*= ConeVolume
;
614 for(i
= 0;i
< NumSends
;i
++)
615 WetGain
[i
] *= ConeVolume
;
621 for(i
= 0;i
< NumSends
;i
++)
622 WetGainHF
[i
] *= ConeHF
;
625 // Clamp to Min/Max Gain
626 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
627 for(i
= 0;i
< NumSends
;i
++)
628 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
630 // Apply filter gains and filters
631 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
632 DryGainHF
*= ALSource
->DirectGainHF
;
633 for(i
= 0;i
< NumSends
;i
++)
635 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
* ListenerGain
;
636 WetGainHF
[i
] *= ALSource
->Send
[i
].WetGainHF
;
639 // Calculate Velocity
640 if(DopplerFactor
> 0.0f
&& SpeedOfSound
> 0.5f
)
644 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
645 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
647 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
648 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
651 BufferListItem
= ALSource
->queue
;
652 while(BufferListItem
!= NULL
)
655 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
657 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
658 ALSource
->NumChannels
;
659 maxstep
-= ResamplerPadding
[Resampler
] +
660 ResamplerPrePadding
[Resampler
] + 1;
661 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
663 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
664 if(Pitch
> (ALfloat
)maxstep
)
665 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
668 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
669 if(ALSource
->Params
.Step
== 0)
670 ALSource
->Params
.Step
= 1;
672 if(ALSource
->Params
.Step
== FRACTIONONE
)
673 Resampler
= PointResampler
;
677 BufferListItem
= BufferListItem
->next
;
680 ALSource
->Params
.DoMix
= SelectHrtfMixer(Resampler
);
682 ALSource
->Params
.DoMix
= SelectMixer(Resampler
);
686 // Use a binaural HRTF algorithm for stereo headphone playback
687 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
691 ALfloat invlen
= 1.0f
/Distance
;
692 Position
[0] *= invlen
;
693 Position
[1] *= invlen
;
694 Position
[2] *= invlen
;
696 // Calculate elevation and azimuth only when the source is not at
697 // the listener. This prevents +0 and -0 Z from producing
698 // inconsistent panning.
699 ev
= aluAsin(Position
[1]);
700 az
= aluAtan2(Position
[0], -Position
[2]*ZScale
);
703 // Check to see if the HRIR is already moving.
704 if(ALSource
->HrtfMoving
)
706 // Calculate the normalized HRTF transition factor (delta).
707 delta
= CalcHrtfDelta(ALSource
->Params
.HrtfGain
, DryGain
,
708 ALSource
->Params
.HrtfDir
, Position
);
709 // If the delta is large enough, get the moving HRIR target
710 // coefficients, target delays, steppping values, and counter.
713 ALSource
->HrtfCounter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
714 ev
, az
, DryGain
, delta
,
715 ALSource
->HrtfCounter
,
716 ALSource
->Params
.HrtfCoeffs
[0],
717 ALSource
->Params
.HrtfDelay
[0],
718 ALSource
->Params
.HrtfCoeffStep
,
719 ALSource
->Params
.HrtfDelayStep
);
720 ALSource
->Params
.HrtfGain
= DryGain
;
721 ALSource
->Params
.HrtfDir
[0] = Position
[0];
722 ALSource
->Params
.HrtfDir
[1] = Position
[1];
723 ALSource
->Params
.HrtfDir
[2] = Position
[2];
728 // Get the initial (static) HRIR coefficients and delays.
729 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
730 ALSource
->Params
.HrtfCoeffs
[0],
731 ALSource
->Params
.HrtfDelay
[0]);
732 ALSource
->HrtfCounter
= 0;
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 // Use energy-preserving panning algorithm for multi-speaker playback
742 ALfloat DirGain
, AmbientGain
;
743 const ALfloat
*ChannelGain
;
747 length
= maxf(Distance
, MinDist
);
750 ALfloat invlen
= 1.0f
/length
;
751 Position
[0] *= invlen
;
752 Position
[1] *= invlen
;
753 Position
[2] *= invlen
;
756 pos
= aluCart2LUTpos(-Position
[2]*ZScale
, Position
[0]);
757 ChannelGain
= Device
->PanningLUT
[pos
];
759 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
760 // elevation adjustment for directional gain. this sucks, but
761 // has low complexity
762 AmbientGain
= aluSqrt(1.0f
/Device
->NumChan
);
763 for(i
= 0;i
< MAXCHANNELS
;i
++)
766 for(i2
= 0;i2
< MAXCHANNELS
;i2
++)
767 ALSource
->Params
.DryGains
[i
][i2
] = 0.0f
;
769 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
771 enum Channel chan
= Device
->Speaker2Chan
[i
];
772 ALfloat gain
= lerp(AmbientGain
, ChannelGain
[chan
], DirGain
);
773 ALSource
->Params
.DryGains
[0][chan
] = DryGain
* gain
;
776 for(i
= 0;i
< NumSends
;i
++)
777 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
];
779 /* Update filter coefficients. */
780 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
782 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
783 for(i
= 0;i
< NumSends
;i
++)
785 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
786 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
791 static __inline ALfloat
aluF2F(ALfloat val
)
793 static __inline ALint
aluF2I(ALfloat val
)
795 if(val
> 1.0f
) return 2147483647;
796 if(val
< -1.0f
) return -2147483647-1;
797 return fastf2i((ALfloat
)(val
*2147483647.0));
799 static __inline ALuint
aluF2UI(ALfloat val
)
800 { return aluF2I(val
)+2147483648u; }
801 static __inline ALshort
aluF2S(ALfloat val
)
802 { return aluF2I(val
)>>16; }
803 static __inline ALushort
aluF2US(ALfloat val
)
804 { return aluF2S(val
)+32768; }
805 static __inline ALbyte
aluF2B(ALfloat val
)
806 { return aluF2I(val
)>>24; }
807 static __inline ALubyte
aluF2UB(ALfloat val
)
808 { return aluF2B(val
)+128; }
810 #define DECL_TEMPLATE(T, N, func) \
811 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
812 ALuint SamplesToDo) \
814 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
815 const enum Channel *ChanMap = device->DevChannels; \
818 for(j = 0;j < N;j++) \
820 T *RESTRICT out = buffer + j; \
821 enum Channel chan = ChanMap[j]; \
823 for(i = 0;i < SamplesToDo;i++) \
824 out[i*N] = func(DryBuffer[i][chan]); \
828 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
829 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
830 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
831 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
832 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
833 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
835 DECL_TEMPLATE(ALuint
, 1, aluF2UI
)
836 DECL_TEMPLATE(ALuint
, 2, aluF2UI
)
837 DECL_TEMPLATE(ALuint
, 4, aluF2UI
)
838 DECL_TEMPLATE(ALuint
, 6, aluF2UI
)
839 DECL_TEMPLATE(ALuint
, 7, aluF2UI
)
840 DECL_TEMPLATE(ALuint
, 8, aluF2UI
)
842 DECL_TEMPLATE(ALint
, 1, aluF2I
)
843 DECL_TEMPLATE(ALint
, 2, aluF2I
)
844 DECL_TEMPLATE(ALint
, 4, aluF2I
)
845 DECL_TEMPLATE(ALint
, 6, aluF2I
)
846 DECL_TEMPLATE(ALint
, 7, aluF2I
)
847 DECL_TEMPLATE(ALint
, 8, aluF2I
)
849 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
850 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
851 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
852 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
853 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
854 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
856 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
857 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
858 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
859 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
860 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
861 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
863 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
864 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
865 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
866 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
867 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
868 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
870 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
871 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
872 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
873 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
874 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
875 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
879 #define DECL_TEMPLATE(T) \
880 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
882 switch(device->FmtChans) \
885 Write_##T##_1(device, buffer, SamplesToDo); \
888 Write_##T##_2(device, buffer, SamplesToDo); \
891 Write_##T##_4(device, buffer, SamplesToDo); \
894 case DevFmtX51Side: \
895 Write_##T##_6(device, buffer, SamplesToDo); \
898 Write_##T##_7(device, buffer, SamplesToDo); \
901 Write_##T##_8(device, buffer, SamplesToDo); \
906 DECL_TEMPLATE(ALfloat
)
907 DECL_TEMPLATE(ALuint
)
909 DECL_TEMPLATE(ALushort
)
910 DECL_TEMPLATE(ALshort
)
911 DECL_TEMPLATE(ALubyte
)
912 DECL_TEMPLATE(ALbyte
)
916 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
919 ALeffectslot
**slot
, **slot_end
;
920 ALsource
**src
, **src_end
;
925 fpuState
= SetMixerFPUMode();
929 /* Setup variables */
930 SamplesToDo
= minu(size
, BUFFERSIZE
);
932 /* Clear mixing buffer */
933 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
936 ctx
= device
->ContextList
;
939 ALenum DeferUpdates
= ctx
->DeferUpdates
;
940 ALenum UpdateSources
= AL_FALSE
;
943 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
945 src
= ctx
->ActiveSources
;
946 src_end
= src
+ ctx
->ActiveSourceCount
;
947 while(src
!= src_end
)
949 if((*src
)->state
!= AL_PLAYING
)
951 --(ctx
->ActiveSourceCount
);
956 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
958 ALsource_Update(*src
, ctx
);
960 MixSource(*src
, device
, SamplesToDo
);
964 /* effect slot processing */
965 slot
= ctx
->ActiveEffectSlots
;
966 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
967 while(slot
!= slot_end
)
969 for(c
= 0;c
< SamplesToDo
;c
++)
971 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
972 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
974 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
975 (*slot
)->PendingClicks
[0] = 0.0f
;
977 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
978 ALeffectState_Update((*slot
)->EffectState
, ctx
, *slot
);
980 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
981 (*slot
)->WetBuffer
, device
->DryBuffer
);
983 for(i
= 0;i
< SamplesToDo
;i
++)
984 (*slot
)->WetBuffer
[i
] = 0.0f
;
992 slot
= &device
->DefaultSlot
;
995 for(c
= 0;c
< SamplesToDo
;c
++)
997 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
998 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
1000 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
1001 (*slot
)->PendingClicks
[0] = 0.0f
;
1003 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1004 ALeffectState_Update((*slot
)->EffectState
, ctx
, *slot
);
1006 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
1007 (*slot
)->WetBuffer
, device
->DryBuffer
);
1009 for(i
= 0;i
< SamplesToDo
;i
++)
1010 (*slot
)->WetBuffer
[i
] = 0.0f
;
1012 UnlockDevice(device
);
1014 //Post processing loop
1015 if(device
->FmtChans
== DevFmtMono
)
1017 for(i
= 0;i
< SamplesToDo
;i
++)
1019 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
1020 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] * (1.0f
/256.0f
);
1022 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1023 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1025 else if(device
->FmtChans
== DevFmtStereo
)
1027 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1028 for(i
= 0;i
< SamplesToDo
;i
++)
1030 for(c
= 0;c
< 2;c
++)
1032 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1033 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1036 for(c
= 0;c
< 2;c
++)
1038 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1039 device
->PendingClicks
[c
] = 0.0f
;
1043 for(i
= 0;i
< SamplesToDo
;i
++)
1044 bs2b_cross_feed(device
->Bs2b
, &device
->DryBuffer
[i
][0]);
1049 for(i
= 0;i
< SamplesToDo
;i
++)
1051 for(c
= 0;c
< MAXCHANNELS
;c
++)
1053 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1054 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1057 for(c
= 0;c
< MAXCHANNELS
;c
++)
1059 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1060 device
->PendingClicks
[c
] = 0.0f
;
1066 switch(device
->FmtType
)
1069 Write_ALbyte(device
, buffer
, SamplesToDo
);
1072 Write_ALubyte(device
, buffer
, SamplesToDo
);
1075 Write_ALshort(device
, buffer
, SamplesToDo
);
1078 Write_ALushort(device
, buffer
, SamplesToDo
);
1081 Write_ALint(device
, buffer
, SamplesToDo
);
1084 Write_ALuint(device
, buffer
, SamplesToDo
);
1087 Write_ALfloat(device
, buffer
, SamplesToDo
);
1092 size
-= SamplesToDo
;
1095 RestoreFPUMode(fpuState
);
1099 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1101 ALCcontext
*Context
;
1104 device
->Connected
= ALC_FALSE
;
1106 Context
= device
->ContextList
;
1109 ALsource
**src
, **src_end
;
1111 src
= Context
->ActiveSources
;
1112 src_end
= src
+ Context
->ActiveSourceCount
;
1113 while(src
!= src_end
)
1115 if((*src
)->state
== AL_PLAYING
)
1117 (*src
)->state
= AL_STOPPED
;
1118 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1119 (*src
)->position
= 0;
1120 (*src
)->position_fraction
= 0;
1124 Context
->ActiveSourceCount
= 0;
1126 Context
= Context
->next
;
1128 UnlockDevice(device
);