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 struct ChanMap
*chans
= NULL
;
119 enum Resampler Resampler
;
120 ALint num_channels
= 0;
121 ALboolean DirectChannels
;
126 /* Get device properties */
127 NumSends
= Device
->NumAuxSends
;
128 Frequency
= Device
->Frequency
;
130 /* Get listener properties */
131 ListenerGain
= ALContext
->Listener
.Gain
;
133 /* Get source properties */
134 SourceVolume
= ALSource
->Gain
;
135 MinVolume
= ALSource
->MinGain
;
136 MaxVolume
= ALSource
->MaxGain
;
137 Pitch
= ALSource
->Pitch
;
138 Resampler
= ALSource
->Resampler
;
139 DirectChannels
= ALSource
->DirectChannels
;
141 /* Calculate the stepping value */
143 BufferListItem
= ALSource
->queue
;
144 while(BufferListItem
!= NULL
)
147 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
149 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
150 ALSource
->NumChannels
;
151 maxstep
-= ResamplerPadding
[Resampler
] +
152 ResamplerPrePadding
[Resampler
] + 1;
153 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
155 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
156 if(Pitch
> (ALfloat
)maxstep
)
157 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
160 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
161 if(ALSource
->Params
.Step
== 0)
162 ALSource
->Params
.Step
= 1;
164 if(ALSource
->Params
.Step
== FRACTIONONE
)
165 Resampler
= PointResampler
;
167 Channels
= ALBuffer
->FmtChannels
;
170 BufferListItem
= BufferListItem
->next
;
172 if(!DirectChannels
&& Device
->Hrtf
)
173 ALSource
->Params
.DryMix
= SelectHrtfMixer(Resampler
);
175 ALSource
->Params
.DryMix
= SelectDirectMixer(Resampler
);
176 ALSource
->Params
.WetMix
= SelectSendMixer(Resampler
);
178 /* Calculate gains */
179 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
180 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
181 DryGainHF
= ALSource
->DirectGainHF
;
182 for(i
= 0;i
< NumSends
;i
++)
184 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
185 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
186 WetGainHF
[i
] = ALSource
->Send
[i
].GainHF
;
189 SrcMatrix
= ALSource
->Params
.Direct
.Gains
;
190 for(i
= 0;i
< MAXCHANNELS
;i
++)
192 for(c
= 0;c
< MAXCHANNELS
;c
++)
193 SrcMatrix
[i
][c
] = 0.0f
;
233 if(DirectChannels
!= AL_FALSE
)
235 for(c
= 0;c
< num_channels
;c
++)
237 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
239 enum Channel chan
= Device
->Speaker2Chan
[i
];
240 if(chan
== chans
[c
].channel
)
242 SrcMatrix
[c
][chan
] += DryGain
;
248 else if(Device
->Hrtf
)
250 for(c
= 0;c
< num_channels
;c
++)
252 if(chans
[c
].channel
== LFE
)
255 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
][0] = 0;
256 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
][1] = 0;
257 for(i
= 0;i
< HRIR_LENGTH
;i
++)
259 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
][i
][0] = 0.0f
;
260 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
][i
][1] = 0.0f
;
265 /* Get the static HRIR coefficients and delays for this
267 GetLerpedHrtfCoeffs(Device
->Hrtf
,
268 0.0f
, chans
[c
].angle
, DryGain
,
269 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
],
270 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
]);
273 ALSource
->Hrtf
.Counter
= 0;
277 for(c
= 0;c
< num_channels
;c
++)
279 /* Special-case LFE */
280 if(chans
[c
].channel
== LFE
)
282 SrcMatrix
[c
][chans
[c
].channel
] = DryGain
;
285 ComputeAngleGains(Device
, chans
[c
].angle
, 0.0f
, DryGain
,
289 for(i
= 0;i
< NumSends
;i
++)
291 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
294 Slot
= Device
->DefaultSlot
;
295 if(Slot
&& Slot
->effect
.type
== AL_EFFECT_NULL
)
297 ALSource
->Params
.Slot
[i
] = Slot
;
298 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
301 /* Update filter coefficients. Calculations based on the I3DL2
303 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
305 /* We use two chained one-pole filters, so we need to take the
306 * square root of the squared gain, which is the same as the base
308 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
309 for(i
= 0;i
< NumSends
;i
++)
311 ALfloat a
= lpCoeffCalc(WetGainHF
[i
], cw
);
312 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
316 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
318 const ALCdevice
*Device
= ALContext
->Device
;
319 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
320 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
321 ALfloat Velocity
[3],ListenerVel
[3];
322 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
323 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
324 ALfloat DopplerFactor
, SpeedOfSound
;
325 ALfloat AirAbsorptionFactor
;
326 ALfloat RoomAirAbsorption
[MAX_SENDS
];
327 ALbufferlistitem
*BufferListItem
;
329 ALfloat RoomAttenuation
[MAX_SENDS
];
330 ALfloat MetersPerUnit
;
331 ALfloat RoomRolloffBase
;
332 ALfloat RoomRolloff
[MAX_SENDS
];
333 ALfloat DecayDistance
[MAX_SENDS
];
336 ALboolean DryGainHFAuto
;
337 ALfloat WetGain
[MAX_SENDS
];
338 ALfloat WetGainHF
[MAX_SENDS
];
339 ALboolean WetGainAuto
;
340 ALboolean WetGainHFAuto
;
341 enum Resampler Resampler
;
342 ALfloat Matrix
[4][4];
350 for(i
= 0;i
< MAX_SENDS
;i
++)
353 /* Get context/device properties */
354 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
355 SpeedOfSound
= ALContext
->SpeedOfSound
* ALContext
->DopplerVelocity
;
356 NumSends
= Device
->NumAuxSends
;
357 Frequency
= Device
->Frequency
;
359 /* Get listener properties */
360 ListenerGain
= ALContext
->Listener
.Gain
;
361 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
362 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
363 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
364 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
368 Matrix
[i
][j
] = ALContext
->Listener
.Matrix
[i
][j
];
371 /* Get source properties */
372 SourceVolume
= ALSource
->Gain
;
373 MinVolume
= ALSource
->MinGain
;
374 MaxVolume
= ALSource
->MaxGain
;
375 Pitch
= ALSource
->Pitch
;
376 Resampler
= ALSource
->Resampler
;
377 Position
[0] = ALSource
->Position
[0];
378 Position
[1] = ALSource
->Position
[1];
379 Position
[2] = ALSource
->Position
[2];
380 Direction
[0] = ALSource
->Orientation
[0];
381 Direction
[1] = ALSource
->Orientation
[1];
382 Direction
[2] = ALSource
->Orientation
[2];
383 Velocity
[0] = ALSource
->Velocity
[0];
384 Velocity
[1] = ALSource
->Velocity
[1];
385 Velocity
[2] = ALSource
->Velocity
[2];
386 MinDist
= ALSource
->RefDistance
;
387 MaxDist
= ALSource
->MaxDistance
;
388 Rolloff
= ALSource
->RollOffFactor
;
389 InnerAngle
= ALSource
->InnerAngle
* ConeScale
;
390 OuterAngle
= ALSource
->OuterAngle
* ConeScale
;
391 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
392 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
393 WetGainAuto
= ALSource
->WetGainAuto
;
394 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
395 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
396 for(i
= 0;i
< NumSends
;i
++)
398 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
401 Slot
= Device
->DefaultSlot
;
402 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
405 RoomRolloff
[i
] = 0.0f
;
406 DecayDistance
[i
] = 0.0f
;
407 RoomAirAbsorption
[i
] = 1.0f
;
409 else if(Slot
->AuxSendAuto
)
411 RoomRolloff
[i
] = RoomRolloffBase
;
412 if(IsReverbEffect(Slot
->effect
.type
))
414 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
415 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
416 SPEEDOFSOUNDMETRESPERSEC
;
417 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
421 DecayDistance
[i
] = 0.0f
;
422 RoomAirAbsorption
[i
] = 1.0f
;
427 /* If the slot's auxiliary send auto is off, the data sent to the
428 * effect slot is the same as the dry path, sans filter effects */
429 RoomRolloff
[i
] = Rolloff
;
430 DecayDistance
[i
] = 0.0f
;
431 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
434 ALSource
->Params
.Slot
[i
] = Slot
;
437 /* Transform source to listener space (convert to head relative) */
438 if(ALSource
->HeadRelative
== AL_FALSE
)
440 /* Translate position */
441 Position
[0] -= ALContext
->Listener
.Position
[0];
442 Position
[1] -= ALContext
->Listener
.Position
[1];
443 Position
[2] -= ALContext
->Listener
.Position
[2];
445 /* Transform source vectors */
446 aluMatrixVector(Position
, 1.0f
, Matrix
);
447 aluMatrixVector(Direction
, 0.0f
, Matrix
);
448 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
449 /* Transform listener velocity */
450 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
454 /* Transform listener velocity from world space to listener space */
455 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
456 /* Offset the source velocity to be relative of the listener velocity */
457 Velocity
[0] += ListenerVel
[0];
458 Velocity
[1] += ListenerVel
[1];
459 Velocity
[2] += ListenerVel
[2];
462 SourceToListener
[0] = -Position
[0];
463 SourceToListener
[1] = -Position
[1];
464 SourceToListener
[2] = -Position
[2];
465 aluNormalize(SourceToListener
);
466 aluNormalize(Direction
);
468 /* Calculate distance attenuation */
469 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
470 ClampedDist
= Distance
;
473 for(i
= 0;i
< NumSends
;i
++)
474 RoomAttenuation
[i
] = 1.0f
;
475 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
476 ALContext
->DistanceModel
)
478 case InverseDistanceClamped
:
479 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
480 if(MaxDist
< MinDist
)
483 case InverseDistance
:
486 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
487 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
488 for(i
= 0;i
< NumSends
;i
++)
490 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
491 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
496 case LinearDistanceClamped
:
497 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
498 if(MaxDist
< MinDist
)
502 if(MaxDist
!= MinDist
)
504 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
505 Attenuation
= maxf(Attenuation
, 0.0f
);
506 for(i
= 0;i
< NumSends
;i
++)
508 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
509 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
514 case ExponentDistanceClamped
:
515 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
516 if(MaxDist
< MinDist
)
519 case ExponentDistance
:
520 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
522 Attenuation
= aluPow(ClampedDist
/MinDist
, -Rolloff
);
523 for(i
= 0;i
< NumSends
;i
++)
524 RoomAttenuation
[i
] = aluPow(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
528 case DisableDistance
:
529 ClampedDist
= MinDist
;
533 /* Source Gain + Attenuation */
534 DryGain
= SourceVolume
* Attenuation
;
535 for(i
= 0;i
< NumSends
;i
++)
536 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
538 /* Distance-based air absorption */
539 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
541 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
542 DryGainHF
*= aluPow(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
543 for(i
= 0;i
< NumSends
;i
++)
544 WetGainHF
[i
] *= aluPow(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
549 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
551 /* Apply a decay-time transformation to the wet path, based on the
552 * attenuation of the dry path.
554 * Using the apparent distance, based on the distance attenuation, the
555 * initial decay of the reverb effect is calculated and applied to the
558 for(i
= 0;i
< NumSends
;i
++)
560 if(DecayDistance
[i
] > 0.0f
)
561 WetGain
[i
] *= aluPow(0.001f
/*-60dB*/, ApparentDist
/DecayDistance
[i
]);
565 /* Calculate directional soundcones */
566 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * (180.0f
/F_PI
);
567 if(Angle
> InnerAngle
&& Angle
<= OuterAngle
)
569 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
570 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
571 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
573 else if(Angle
> OuterAngle
)
575 ConeVolume
= ALSource
->OuterGain
;
576 ConeHF
= ALSource
->OuterGainHF
;
584 DryGain
*= ConeVolume
;
587 for(i
= 0;i
< NumSends
;i
++)
588 WetGain
[i
] *= ConeVolume
;
594 for(i
= 0;i
< NumSends
;i
++)
595 WetGainHF
[i
] *= ConeHF
;
598 /* Clamp to Min/Max Gain */
599 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
600 for(i
= 0;i
< NumSends
;i
++)
601 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
603 /* Apply gain and frequency filters */
604 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
605 DryGainHF
*= ALSource
->DirectGainHF
;
606 for(i
= 0;i
< NumSends
;i
++)
608 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
609 WetGainHF
[i
] *= ALSource
->Send
[i
].GainHF
;
612 /* Calculate velocity-based doppler effect */
613 if(DopplerFactor
> 0.0f
)
617 if(SpeedOfSound
< 1.0f
)
619 DopplerFactor
*= 1.0f
/SpeedOfSound
;
623 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
624 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
626 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
627 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
630 BufferListItem
= ALSource
->queue
;
631 while(BufferListItem
!= NULL
)
634 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
636 /* Calculate fixed-point stepping value, based on the pitch, buffer
637 * frequency, and output frequency. */
638 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
639 ALSource
->NumChannels
;
640 maxstep
-= ResamplerPadding
[Resampler
] +
641 ResamplerPrePadding
[Resampler
] + 1;
642 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
644 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
645 if(Pitch
> (ALfloat
)maxstep
)
646 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
649 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
650 if(ALSource
->Params
.Step
== 0)
651 ALSource
->Params
.Step
= 1;
653 if(ALSource
->Params
.Step
== FRACTIONONE
)
654 Resampler
= PointResampler
;
658 BufferListItem
= BufferListItem
->next
;
661 ALSource
->Params
.DryMix
= SelectHrtfMixer(Resampler
);
663 ALSource
->Params
.DryMix
= SelectDirectMixer(Resampler
);
664 ALSource
->Params
.WetMix
= SelectSendMixer(Resampler
);
668 /* Use a binaural HRTF algorithm for stereo headphone playback */
669 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
673 ALfloat invlen
= 1.0f
/Distance
;
674 Position
[0] *= invlen
;
675 Position
[1] *= invlen
;
676 Position
[2] *= invlen
;
678 /* Calculate elevation and azimuth only when the source is not at
679 * the listener. This prevents +0 and -0 Z from producing
680 * inconsistent panning. Also, clamp Y in case FP precision errors
681 * cause it to land outside of -1..+1. */
682 ev
= aluAsin(clampf(Position
[1], -1.0f
, 1.0f
));
683 az
= aluAtan2(Position
[0], -Position
[2]*ZScale
);
686 /* Check to see if the HRIR is already moving. */
687 if(ALSource
->Hrtf
.Moving
)
689 /* Calculate the normalized HRTF transition factor (delta). */
690 delta
= CalcHrtfDelta(ALSource
->Params
.Direct
.Hrtf
.Gain
, DryGain
,
691 ALSource
->Params
.Direct
.Hrtf
.Dir
, Position
);
692 /* If the delta is large enough, get the moving HRIR target
693 * coefficients, target delays, steppping values, and counter. */
696 ALSource
->Hrtf
.Counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
697 ev
, az
, DryGain
, delta
,
698 ALSource
->Hrtf
.Counter
,
699 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
700 ALSource
->Params
.Direct
.Hrtf
.Delay
[0],
701 ALSource
->Params
.Direct
.Hrtf
.CoeffStep
,
702 ALSource
->Params
.Direct
.Hrtf
.DelayStep
);
703 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
704 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
705 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
706 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
711 /* Get the initial (static) HRIR coefficients and delays. */
712 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
713 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
714 ALSource
->Params
.Direct
.Hrtf
.Delay
[0]);
715 ALSource
->Hrtf
.Counter
= 0;
716 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
717 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
718 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
719 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
724 ALfloat (*Matrix
)[MAXCHANNELS
] = ALSource
->Params
.Direct
.Gains
;
725 ALfloat DirGain
= 0.0f
;
728 for(i
= 0;i
< MAXCHANNELS
;i
++)
730 for(j
= 0;j
< MAXCHANNELS
;j
++)
734 /* Normalize the length, and compute panned gains. */
737 ALfloat invlen
= 1.0f
/Distance
;
738 Position
[0] *= invlen
;
739 Position
[1] *= invlen
;
740 Position
[2] *= invlen
;
742 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
743 ComputeAngleGains(Device
, aluAtan2(Position
[0], -Position
[2]*ZScale
), 0.0f
,
744 DryGain
*DirGain
, Matrix
[0]);
747 /* Adjustment for vertical offsets. Not the greatest, but simple
749 AmbientGain
= DryGain
* aluSqrt(1.0f
/Device
->NumChan
) * (1.0f
-DirGain
);
750 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
752 enum Channel chan
= Device
->Speaker2Chan
[i
];
753 Matrix
[0][chan
] = maxf(Matrix
[0][chan
], AmbientGain
);
756 for(i
= 0;i
< NumSends
;i
++)
757 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
759 /* Update filter coefficients. */
760 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
762 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
763 for(i
= 0;i
< NumSends
;i
++)
765 ALfloat a
= lpCoeffCalc(WetGainHF
[i
], cw
);
766 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
771 static __inline ALfloat
aluF2F(ALfloat val
)
773 static __inline ALint
aluF2I(ALfloat val
)
775 if(val
> 1.0f
) return 2147483647;
776 if(val
< -1.0f
) return -2147483647-1;
777 return fastf2i((ALfloat
)(val
*2147483647.0));
779 static __inline ALuint
aluF2UI(ALfloat val
)
780 { return aluF2I(val
)+2147483648u; }
781 static __inline ALshort
aluF2S(ALfloat val
)
782 { return aluF2I(val
)>>16; }
783 static __inline ALushort
aluF2US(ALfloat val
)
784 { return aluF2S(val
)+32768; }
785 static __inline ALbyte
aluF2B(ALfloat val
)
786 { return aluF2I(val
)>>24; }
787 static __inline ALubyte
aluF2UB(ALfloat val
)
788 { return aluF2B(val
)+128; }
790 #define DECL_TEMPLATE(T, N, func) \
791 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
792 ALuint SamplesToDo) \
794 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
795 const enum Channel *ChanMap = device->DevChannels; \
798 for(j = 0;j < N;j++) \
800 T *RESTRICT out = buffer + j; \
801 enum Channel chan = ChanMap[j]; \
803 for(i = 0;i < SamplesToDo;i++) \
804 out[i*N] = func(DryBuffer[i][chan]); \
808 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
809 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
810 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
811 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
812 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
813 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
815 DECL_TEMPLATE(ALuint
, 1, aluF2UI
)
816 DECL_TEMPLATE(ALuint
, 2, aluF2UI
)
817 DECL_TEMPLATE(ALuint
, 4, aluF2UI
)
818 DECL_TEMPLATE(ALuint
, 6, aluF2UI
)
819 DECL_TEMPLATE(ALuint
, 7, aluF2UI
)
820 DECL_TEMPLATE(ALuint
, 8, aluF2UI
)
822 DECL_TEMPLATE(ALint
, 1, aluF2I
)
823 DECL_TEMPLATE(ALint
, 2, aluF2I
)
824 DECL_TEMPLATE(ALint
, 4, aluF2I
)
825 DECL_TEMPLATE(ALint
, 6, aluF2I
)
826 DECL_TEMPLATE(ALint
, 7, aluF2I
)
827 DECL_TEMPLATE(ALint
, 8, aluF2I
)
829 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
830 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
831 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
832 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
833 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
834 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
836 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
837 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
838 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
839 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
840 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
841 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
843 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
844 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
845 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
846 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
847 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
848 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
850 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
851 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
852 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
853 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
854 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
855 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
859 #define DECL_TEMPLATE(T) \
860 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
862 switch(device->FmtChans) \
865 Write_##T##_1(device, buffer, SamplesToDo); \
868 Write_##T##_2(device, buffer, SamplesToDo); \
871 Write_##T##_4(device, buffer, SamplesToDo); \
874 case DevFmtX51Side: \
875 Write_##T##_6(device, buffer, SamplesToDo); \
878 Write_##T##_7(device, buffer, SamplesToDo); \
881 Write_##T##_8(device, buffer, SamplesToDo); \
886 DECL_TEMPLATE(ALfloat
)
887 DECL_TEMPLATE(ALuint
)
889 DECL_TEMPLATE(ALushort
)
890 DECL_TEMPLATE(ALshort
)
891 DECL_TEMPLATE(ALubyte
)
892 DECL_TEMPLATE(ALbyte
)
896 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
899 ALeffectslot
**slot
, **slot_end
;
900 ALsource
**src
, **src_end
;
905 fpuState
= SetMixerFPUMode();
909 SamplesToDo
= minu(size
, BUFFERSIZE
);
910 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
913 ctx
= device
->ContextList
;
916 ALenum DeferUpdates
= ctx
->DeferUpdates
;
917 ALenum UpdateSources
= AL_FALSE
;
920 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
922 /* source processing */
923 src
= ctx
->ActiveSources
;
924 src_end
= src
+ ctx
->ActiveSourceCount
;
925 while(src
!= src_end
)
927 if((*src
)->state
!= AL_PLAYING
)
929 --(ctx
->ActiveSourceCount
);
934 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
936 ALsource_Update(*src
, ctx
);
938 MixSource(*src
, device
, SamplesToDo
);
942 /* effect slot processing */
943 slot
= ctx
->ActiveEffectSlots
;
944 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
945 while(slot
!= slot_end
)
947 for(c
= 0;c
< SamplesToDo
;c
++)
949 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
950 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
952 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
953 (*slot
)->PendingClicks
[0] = 0.0f
;
955 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
956 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
958 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
959 (*slot
)->WetBuffer
, device
->DryBuffer
);
961 for(i
= 0;i
< SamplesToDo
;i
++)
962 (*slot
)->WetBuffer
[i
] = 0.0f
;
970 slot
= &device
->DefaultSlot
;
973 for(c
= 0;c
< SamplesToDo
;c
++)
975 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
976 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
978 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
979 (*slot
)->PendingClicks
[0] = 0.0f
;
981 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
982 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
984 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
985 (*slot
)->WetBuffer
, device
->DryBuffer
);
987 for(i
= 0;i
< SamplesToDo
;i
++)
988 (*slot
)->WetBuffer
[i
] = 0.0f
;
990 UnlockDevice(device
);
992 /* Click-removal. Could do better; this only really handles immediate
993 * changes between updates where a predictive sample could be
994 * generated. Delays caused by effects and HRTF aren't caught. */
995 if(device
->FmtChans
== DevFmtMono
)
997 for(i
= 0;i
< SamplesToDo
;i
++)
999 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
1000 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] * (1.0f
/256.0f
);
1002 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1003 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1005 else if(device
->FmtChans
== DevFmtStereo
)
1007 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1008 for(i
= 0;i
< SamplesToDo
;i
++)
1010 for(c
= 0;c
< 2;c
++)
1012 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1013 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1016 for(c
= 0;c
< 2;c
++)
1018 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1019 device
->PendingClicks
[c
] = 0.0f
;
1023 for(i
= 0;i
< SamplesToDo
;i
++)
1024 bs2b_cross_feed(device
->Bs2b
, &device
->DryBuffer
[i
][0]);
1029 for(i
= 0;i
< SamplesToDo
;i
++)
1031 for(c
= 0;c
< MAXCHANNELS
;c
++)
1033 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1034 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1037 for(c
= 0;c
< MAXCHANNELS
;c
++)
1039 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1040 device
->PendingClicks
[c
] = 0.0f
;
1046 switch(device
->FmtType
)
1049 Write_ALbyte(device
, buffer
, SamplesToDo
);
1052 Write_ALubyte(device
, buffer
, SamplesToDo
);
1055 Write_ALshort(device
, buffer
, SamplesToDo
);
1058 Write_ALushort(device
, buffer
, SamplesToDo
);
1061 Write_ALint(device
, buffer
, SamplesToDo
);
1064 Write_ALuint(device
, buffer
, SamplesToDo
);
1067 Write_ALfloat(device
, buffer
, SamplesToDo
);
1072 size
-= SamplesToDo
;
1075 RestoreFPUMode(fpuState
);
1079 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1081 ALCcontext
*Context
;
1084 device
->Connected
= ALC_FALSE
;
1086 Context
= device
->ContextList
;
1089 ALsource
**src
, **src_end
;
1091 src
= Context
->ActiveSources
;
1092 src_end
= src
+ Context
->ActiveSourceCount
;
1093 while(src
!= src_end
)
1095 if((*src
)->state
== AL_PLAYING
)
1097 (*src
)->state
= AL_STOPPED
;
1098 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1099 (*src
)->position
= 0;
1100 (*src
)->position_fraction
= 0;
1104 Context
->ActiveSourceCount
= 0;
1106 Context
= Context
->next
;
1108 UnlockDevice(device
);