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
= 1.0f
;
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] = { { FrontCenter
, 0.0f
} };
67 static const struct ChanMap StereoMap
[2] = {
68 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
69 { FrontRight
, 30.0f
* F_PI
/180.0f
}
71 static const struct ChanMap StereoWideMap
[2] = {
72 { FrontLeft
, -90.0f
* F_PI
/180.0f
},
73 { FrontRight
, 90.0f
* F_PI
/180.0f
}
75 static const struct ChanMap RearMap
[2] = {
76 { BackLeft
, -150.0f
* F_PI
/180.0f
},
77 { BackRight
, 150.0f
* F_PI
/180.0f
}
79 static const struct ChanMap QuadMap
[4] = {
80 { FrontLeft
, -45.0f
* F_PI
/180.0f
},
81 { FrontRight
, 45.0f
* F_PI
/180.0f
},
82 { BackLeft
, -135.0f
* F_PI
/180.0f
},
83 { BackRight
, 135.0f
* F_PI
/180.0f
}
85 static const struct ChanMap X51Map
[6] = {
86 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
87 { FrontRight
, 30.0f
* F_PI
/180.0f
},
88 { FrontCenter
, 0.0f
* F_PI
/180.0f
},
90 { BackLeft
, -110.0f
* F_PI
/180.0f
},
91 { BackRight
, 110.0f
* F_PI
/180.0f
}
93 static const struct ChanMap X61Map
[7] = {
94 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
95 { FrontRight
, 30.0f
* F_PI
/180.0f
},
96 { FrontCenter
, 0.0f
* F_PI
/180.0f
},
98 { BackCenter
, 180.0f
* F_PI
/180.0f
},
99 { SideLeft
, -90.0f
* F_PI
/180.0f
},
100 { SideRight
, 90.0f
* F_PI
/180.0f
}
102 static const struct ChanMap X71Map
[8] = {
103 { FrontLeft
, -30.0f
* F_PI
/180.0f
},
104 { FrontRight
, 30.0f
* F_PI
/180.0f
},
105 { FrontCenter
, 0.0f
* F_PI
/180.0f
},
107 { BackLeft
, -150.0f
* F_PI
/180.0f
},
108 { BackRight
, 150.0f
* F_PI
/180.0f
},
109 { SideLeft
, -90.0f
* F_PI
/180.0f
},
110 { SideRight
, 90.0f
* F_PI
/180.0f
}
113 ALCdevice
*Device
= ALContext
->Device
;
114 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
115 ALbufferlistitem
*BufferListItem
;
116 enum FmtChannels Channels
;
117 ALfloat (*SrcMatrix
)[MaxChannels
];
118 ALfloat DryGain
, DryGainHF
;
119 ALfloat WetGain
[MAX_SENDS
];
120 ALfloat WetGainHF
[MAX_SENDS
];
121 ALint NumSends
, Frequency
;
122 const struct ChanMap
*chans
= NULL
;
123 enum Resampler Resampler
;
124 ALint num_channels
= 0;
125 ALboolean DirectChannels
;
126 ALfloat hwidth
= 0.0f
;
131 /* Get device properties */
132 NumSends
= Device
->NumAuxSends
;
133 Frequency
= Device
->Frequency
;
135 /* Get listener properties */
136 ListenerGain
= ALContext
->Listener
.Gain
;
138 /* Get source properties */
139 SourceVolume
= ALSource
->Gain
;
140 MinVolume
= ALSource
->MinGain
;
141 MaxVolume
= ALSource
->MaxGain
;
142 Pitch
= ALSource
->Pitch
;
143 Resampler
= ALSource
->Resampler
;
144 DirectChannels
= ALSource
->DirectChannels
;
146 /* Calculate the stepping value */
148 BufferListItem
= ALSource
->queue
;
149 while(BufferListItem
!= NULL
)
152 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
154 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
155 ALSource
->NumChannels
;
156 maxstep
-= ResamplerPadding
[Resampler
] +
157 ResamplerPrePadding
[Resampler
] + 1;
158 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
160 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
161 if(Pitch
> (ALfloat
)maxstep
)
162 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
165 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
166 if(ALSource
->Params
.Step
== 0)
167 ALSource
->Params
.Step
= 1;
170 Channels
= ALBuffer
->FmtChannels
;
173 BufferListItem
= BufferListItem
->next
;
175 if(!DirectChannels
&& Device
->Hrtf
)
176 ALSource
->Params
.DryMix
= SelectHrtfMixer();
178 ALSource
->Params
.DryMix
= SelectDirectMixer();
179 ALSource
->Params
.WetMix
= SelectSendMixer();
181 /* Calculate gains */
182 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
183 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
184 DryGainHF
= ALSource
->DirectGainHF
;
185 for(i
= 0;i
< NumSends
;i
++)
187 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
188 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
189 WetGainHF
[i
] = ALSource
->Send
[i
].GainHF
;
192 SrcMatrix
= ALSource
->Params
.Direct
.Gains
;
193 for(i
= 0;i
< MaxChannels
;i
++)
195 for(c
= 0;c
< MaxChannels
;c
++)
196 SrcMatrix
[i
][c
] = 0.0f
;
206 if(!(Device
->Flags
&DEVICE_WIDE_STEREO
))
210 chans
= StereoWideMap
;
211 hwidth
= 60.0f
* F_PI
/180.0f
;
242 if(DirectChannels
!= AL_FALSE
)
244 for(c
= 0;c
< num_channels
;c
++)
246 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
248 enum Channel chan
= Device
->Speaker2Chan
[i
];
249 if(chan
== chans
[c
].channel
)
251 SrcMatrix
[c
][chan
] += DryGain
;
257 else if(Device
->Hrtf
)
259 for(c
= 0;c
< num_channels
;c
++)
261 if(chans
[c
].channel
== LFE
)
264 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
][0] = 0;
265 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
][1] = 0;
266 for(i
= 0;i
< HRIR_LENGTH
;i
++)
268 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
][i
][0] = 0.0f
;
269 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
][i
][1] = 0.0f
;
274 /* Get the static HRIR coefficients and delays for this
276 GetLerpedHrtfCoeffs(Device
->Hrtf
,
277 0.0f
, chans
[c
].angle
, DryGain
,
278 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[c
],
279 ALSource
->Params
.Direct
.Hrtf
.Delay
[c
]);
282 ALSource
->Hrtf
.Counter
= 0;
286 DryGain
*= lerp(1.0f
, 1.0f
/sqrtf(Device
->NumChan
), hwidth
/(F_PI
*2.0f
));
287 for(c
= 0;c
< num_channels
;c
++)
289 /* Special-case LFE */
290 if(chans
[c
].channel
== LFE
)
292 SrcMatrix
[c
][chans
[c
].channel
] = DryGain
;
295 ComputeAngleGains(Device
, chans
[c
].angle
, hwidth
, DryGain
,
299 for(i
= 0;i
< NumSends
;i
++)
301 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
304 Slot
= Device
->DefaultSlot
;
305 if(Slot
&& Slot
->effect
.type
== AL_EFFECT_NULL
)
307 ALSource
->Params
.Slot
[i
] = Slot
;
308 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
311 /* Update filter coefficients. Calculations based on the I3DL2
313 cw
= cosf(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
315 /* We use two chained one-pole filters, so we need to take the
316 * square root of the squared gain, which is the same as the base
318 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
319 for(i
= 0;i
< NumSends
;i
++)
321 ALfloat a
= lpCoeffCalc(WetGainHF
[i
], cw
);
322 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
326 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
328 const ALCdevice
*Device
= ALContext
->Device
;
329 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
330 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
331 ALfloat Velocity
[3],ListenerVel
[3];
332 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
333 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
334 ALfloat DopplerFactor
, SpeedOfSound
;
335 ALfloat AirAbsorptionFactor
;
336 ALfloat RoomAirAbsorption
[MAX_SENDS
];
337 ALbufferlistitem
*BufferListItem
;
339 ALfloat RoomAttenuation
[MAX_SENDS
];
340 ALfloat MetersPerUnit
;
341 ALfloat RoomRolloffBase
;
342 ALfloat RoomRolloff
[MAX_SENDS
];
343 ALfloat DecayDistance
[MAX_SENDS
];
346 ALboolean DryGainHFAuto
;
347 ALfloat WetGain
[MAX_SENDS
];
348 ALfloat WetGainHF
[MAX_SENDS
];
349 ALboolean WetGainAuto
;
350 ALboolean WetGainHFAuto
;
351 enum Resampler Resampler
;
352 ALfloat Matrix
[4][4];
360 for(i
= 0;i
< MAX_SENDS
;i
++)
363 /* Get context/device properties */
364 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
365 SpeedOfSound
= ALContext
->SpeedOfSound
* ALContext
->DopplerVelocity
;
366 NumSends
= Device
->NumAuxSends
;
367 Frequency
= Device
->Frequency
;
369 /* Get listener properties */
370 ListenerGain
= ALContext
->Listener
.Gain
;
371 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
372 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
373 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
374 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
378 Matrix
[i
][j
] = ALContext
->Listener
.Matrix
[i
][j
];
381 /* Get source properties */
382 SourceVolume
= ALSource
->Gain
;
383 MinVolume
= ALSource
->MinGain
;
384 MaxVolume
= ALSource
->MaxGain
;
385 Pitch
= ALSource
->Pitch
;
386 Resampler
= ALSource
->Resampler
;
387 Position
[0] = ALSource
->Position
[0];
388 Position
[1] = ALSource
->Position
[1];
389 Position
[2] = ALSource
->Position
[2];
390 Direction
[0] = ALSource
->Orientation
[0];
391 Direction
[1] = ALSource
->Orientation
[1];
392 Direction
[2] = ALSource
->Orientation
[2];
393 Velocity
[0] = ALSource
->Velocity
[0];
394 Velocity
[1] = ALSource
->Velocity
[1];
395 Velocity
[2] = ALSource
->Velocity
[2];
396 MinDist
= ALSource
->RefDistance
;
397 MaxDist
= ALSource
->MaxDistance
;
398 Rolloff
= ALSource
->RollOffFactor
;
399 InnerAngle
= ALSource
->InnerAngle
;
400 OuterAngle
= ALSource
->OuterAngle
;
401 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
402 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
403 WetGainAuto
= ALSource
->WetGainAuto
;
404 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
405 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
406 for(i
= 0;i
< NumSends
;i
++)
408 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
411 Slot
= Device
->DefaultSlot
;
412 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
415 RoomRolloff
[i
] = 0.0f
;
416 DecayDistance
[i
] = 0.0f
;
417 RoomAirAbsorption
[i
] = 1.0f
;
419 else if(Slot
->AuxSendAuto
)
421 RoomRolloff
[i
] = RoomRolloffBase
;
422 if(IsReverbEffect(Slot
->effect
.type
))
424 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
425 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
426 SPEEDOFSOUNDMETRESPERSEC
;
427 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
431 DecayDistance
[i
] = 0.0f
;
432 RoomAirAbsorption
[i
] = 1.0f
;
437 /* If the slot's auxiliary send auto is off, the data sent to the
438 * effect slot is the same as the dry path, sans filter effects */
439 RoomRolloff
[i
] = Rolloff
;
440 DecayDistance
[i
] = 0.0f
;
441 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
444 ALSource
->Params
.Slot
[i
] = Slot
;
447 /* Transform source to listener space (convert to head relative) */
448 if(ALSource
->HeadRelative
== AL_FALSE
)
450 /* Translate position */
451 Position
[0] -= ALContext
->Listener
.Position
[0];
452 Position
[1] -= ALContext
->Listener
.Position
[1];
453 Position
[2] -= ALContext
->Listener
.Position
[2];
455 /* Transform source vectors */
456 aluMatrixVector(Position
, 1.0f
, Matrix
);
457 aluMatrixVector(Direction
, 0.0f
, Matrix
);
458 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
459 /* Transform listener velocity */
460 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
464 /* Transform listener velocity from world space to listener space */
465 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
466 /* Offset the source velocity to be relative of the listener velocity */
467 Velocity
[0] += ListenerVel
[0];
468 Velocity
[1] += ListenerVel
[1];
469 Velocity
[2] += ListenerVel
[2];
472 SourceToListener
[0] = -Position
[0];
473 SourceToListener
[1] = -Position
[1];
474 SourceToListener
[2] = -Position
[2];
475 aluNormalize(SourceToListener
);
476 aluNormalize(Direction
);
478 /* Calculate distance attenuation */
479 Distance
= sqrtf(aluDotproduct(Position
, Position
));
480 ClampedDist
= Distance
;
483 for(i
= 0;i
< NumSends
;i
++)
484 RoomAttenuation
[i
] = 1.0f
;
485 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
486 ALContext
->DistanceModel
)
488 case InverseDistanceClamped
:
489 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
490 if(MaxDist
< MinDist
)
493 case InverseDistance
:
496 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
497 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
498 for(i
= 0;i
< NumSends
;i
++)
500 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
501 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
506 case LinearDistanceClamped
:
507 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
508 if(MaxDist
< MinDist
)
512 if(MaxDist
!= MinDist
)
514 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
515 Attenuation
= maxf(Attenuation
, 0.0f
);
516 for(i
= 0;i
< NumSends
;i
++)
518 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
519 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
524 case ExponentDistanceClamped
:
525 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
526 if(MaxDist
< MinDist
)
529 case ExponentDistance
:
530 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
532 Attenuation
= powf(ClampedDist
/MinDist
, -Rolloff
);
533 for(i
= 0;i
< NumSends
;i
++)
534 RoomAttenuation
[i
] = powf(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
538 case DisableDistance
:
539 ClampedDist
= MinDist
;
543 /* Source Gain + Attenuation */
544 DryGain
= SourceVolume
* Attenuation
;
545 for(i
= 0;i
< NumSends
;i
++)
546 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
548 /* Distance-based air absorption */
549 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
551 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
552 DryGainHF
*= powf(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
553 for(i
= 0;i
< NumSends
;i
++)
554 WetGainHF
[i
] *= powf(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
559 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
561 /* Apply a decay-time transformation to the wet path, based on the
562 * attenuation of the dry path.
564 * Using the apparent distance, based on the distance attenuation, the
565 * initial decay of the reverb effect is calculated and applied to the
568 for(i
= 0;i
< NumSends
;i
++)
570 if(DecayDistance
[i
] > 0.0f
)
571 WetGain
[i
] *= powf(0.001f
/*-60dB*/, ApparentDist
/DecayDistance
[i
]);
575 /* Calculate directional soundcones */
576 Angle
= acosf(aluDotproduct(Direction
,SourceToListener
)) * ConeScale
* (360.0f
/F_PI
);
577 if(Angle
> InnerAngle
&& Angle
<= OuterAngle
)
579 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
580 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
581 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
583 else if(Angle
> OuterAngle
)
585 ConeVolume
= ALSource
->OuterGain
;
586 ConeHF
= ALSource
->OuterGainHF
;
594 DryGain
*= ConeVolume
;
597 for(i
= 0;i
< NumSends
;i
++)
598 WetGain
[i
] *= ConeVolume
;
604 for(i
= 0;i
< NumSends
;i
++)
605 WetGainHF
[i
] *= ConeHF
;
608 /* Clamp to Min/Max Gain */
609 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
610 for(i
= 0;i
< NumSends
;i
++)
611 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
613 /* Apply gain and frequency filters */
614 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
615 DryGainHF
*= ALSource
->DirectGainHF
;
616 for(i
= 0;i
< NumSends
;i
++)
618 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
619 WetGainHF
[i
] *= ALSource
->Send
[i
].GainHF
;
622 /* Calculate velocity-based doppler effect */
623 if(DopplerFactor
> 0.0f
)
627 if(SpeedOfSound
< 1.0f
)
629 DopplerFactor
*= 1.0f
/SpeedOfSound
;
633 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
634 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
636 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
637 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
640 BufferListItem
= ALSource
->queue
;
641 while(BufferListItem
!= NULL
)
644 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
646 /* Calculate fixed-point stepping value, based on the pitch, buffer
647 * frequency, and output frequency. */
648 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
649 ALSource
->NumChannels
;
650 maxstep
-= ResamplerPadding
[Resampler
] +
651 ResamplerPrePadding
[Resampler
] + 1;
652 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
654 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
655 if(Pitch
> (ALfloat
)maxstep
)
656 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
659 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
660 if(ALSource
->Params
.Step
== 0)
661 ALSource
->Params
.Step
= 1;
666 BufferListItem
= BufferListItem
->next
;
669 ALSource
->Params
.DryMix
= SelectHrtfMixer();
671 ALSource
->Params
.DryMix
= SelectDirectMixer();
672 ALSource
->Params
.WetMix
= SelectSendMixer();
676 /* Use a binaural HRTF algorithm for stereo headphone playback */
677 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
681 ALfloat invlen
= 1.0f
/Distance
;
682 Position
[0] *= invlen
;
683 Position
[1] *= invlen
;
684 Position
[2] *= invlen
;
686 /* Calculate elevation and azimuth only when the source is not at
687 * the listener. This prevents +0 and -0 Z from producing
688 * inconsistent panning. Also, clamp Y in case FP precision errors
689 * cause it to land outside of -1..+1. */
690 ev
= asinf(clampf(Position
[1], -1.0f
, 1.0f
));
691 az
= atan2f(Position
[0], -Position
[2]*ZScale
);
694 /* Check to see if the HRIR is already moving. */
695 if(ALSource
->Hrtf
.Moving
)
697 /* Calculate the normalized HRTF transition factor (delta). */
698 delta
= CalcHrtfDelta(ALSource
->Params
.Direct
.Hrtf
.Gain
, DryGain
,
699 ALSource
->Params
.Direct
.Hrtf
.Dir
, Position
);
700 /* If the delta is large enough, get the moving HRIR target
701 * coefficients, target delays, steppping values, and counter. */
704 ALSource
->Hrtf
.Counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
705 ev
, az
, DryGain
, delta
,
706 ALSource
->Hrtf
.Counter
,
707 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
708 ALSource
->Params
.Direct
.Hrtf
.Delay
[0],
709 ALSource
->Params
.Direct
.Hrtf
.CoeffStep
,
710 ALSource
->Params
.Direct
.Hrtf
.DelayStep
);
711 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
712 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
713 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
714 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
719 /* Get the initial (static) HRIR coefficients and delays. */
720 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
721 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
722 ALSource
->Params
.Direct
.Hrtf
.Delay
[0]);
723 ALSource
->Hrtf
.Counter
= 0;
724 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
725 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
726 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
727 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
732 ALfloat (*Matrix
)[MaxChannels
] = ALSource
->Params
.Direct
.Gains
;
733 ALfloat DirGain
= 0.0f
;
736 for(i
= 0;i
< MaxChannels
;i
++)
738 for(j
= 0;j
< MaxChannels
;j
++)
742 /* Normalize the length, and compute panned gains. */
745 ALfloat invlen
= 1.0f
/Distance
;
746 Position
[0] *= invlen
;
747 Position
[1] *= invlen
;
748 Position
[2] *= invlen
;
750 DirGain
= sqrtf(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
751 ComputeAngleGains(Device
, atan2f(Position
[0], -Position
[2]*ZScale
), 0.0f
,
752 DryGain
*DirGain
, Matrix
[0]);
755 /* Adjustment for vertical offsets. Not the greatest, but simple
757 AmbientGain
= DryGain
* sqrtf(1.0f
/Device
->NumChan
) * (1.0f
-DirGain
);
758 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
760 enum Channel chan
= Device
->Speaker2Chan
[i
];
761 Matrix
[0][chan
] = maxf(Matrix
[0][chan
], AmbientGain
);
764 for(i
= 0;i
< NumSends
;i
++)
765 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
767 /* Update filter coefficients. */
768 cw
= cosf(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
770 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
771 for(i
= 0;i
< NumSends
;i
++)
773 ALfloat a
= lpCoeffCalc(WetGainHF
[i
], cw
);
774 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
779 static __inline ALfloat
aluF2F(ALfloat val
)
781 static __inline ALint
aluF2I(ALfloat val
)
783 if(val
> 1.0f
) return 2147483647;
784 if(val
< -1.0f
) return -2147483647-1;
785 return fastf2i((ALfloat
)(val
*2147483647.0));
787 static __inline ALuint
aluF2UI(ALfloat val
)
788 { return aluF2I(val
)+2147483648u; }
789 static __inline ALshort
aluF2S(ALfloat val
)
790 { return aluF2I(val
)>>16; }
791 static __inline ALushort
aluF2US(ALfloat val
)
792 { return aluF2S(val
)+32768; }
793 static __inline ALbyte
aluF2B(ALfloat val
)
794 { return aluF2I(val
)>>24; }
795 static __inline ALubyte
aluF2UB(ALfloat val
)
796 { return aluF2B(val
)+128; }
798 #define DECL_TEMPLATE(T, N, func) \
799 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
800 ALuint SamplesToDo) \
802 ALfloat (*RESTRICT DryBuffer)[MaxChannels] = device->DryBuffer; \
803 const enum Channel *ChanMap = device->DevChannels; \
806 for(j = 0;j < N;j++) \
808 T *RESTRICT out = buffer + j; \
809 enum Channel chan = ChanMap[j]; \
811 for(i = 0;i < SamplesToDo;i++) \
812 out[i*N] = func(DryBuffer[i][chan]); \
816 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
817 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
818 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
819 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
820 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
821 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
823 DECL_TEMPLATE(ALuint
, 1, aluF2UI
)
824 DECL_TEMPLATE(ALuint
, 2, aluF2UI
)
825 DECL_TEMPLATE(ALuint
, 4, aluF2UI
)
826 DECL_TEMPLATE(ALuint
, 6, aluF2UI
)
827 DECL_TEMPLATE(ALuint
, 7, aluF2UI
)
828 DECL_TEMPLATE(ALuint
, 8, aluF2UI
)
830 DECL_TEMPLATE(ALint
, 1, aluF2I
)
831 DECL_TEMPLATE(ALint
, 2, aluF2I
)
832 DECL_TEMPLATE(ALint
, 4, aluF2I
)
833 DECL_TEMPLATE(ALint
, 6, aluF2I
)
834 DECL_TEMPLATE(ALint
, 7, aluF2I
)
835 DECL_TEMPLATE(ALint
, 8, aluF2I
)
837 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
838 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
839 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
840 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
841 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
842 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
844 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
845 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
846 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
847 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
848 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
849 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
851 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
852 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
853 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
854 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
855 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
856 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
858 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
859 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
860 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
861 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
862 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
863 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
867 #define DECL_TEMPLATE(T) \
868 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
870 switch(device->FmtChans) \
873 Write_##T##_1(device, buffer, SamplesToDo); \
876 Write_##T##_2(device, buffer, SamplesToDo); \
879 Write_##T##_4(device, buffer, SamplesToDo); \
882 case DevFmtX51Side: \
883 Write_##T##_6(device, buffer, SamplesToDo); \
886 Write_##T##_7(device, buffer, SamplesToDo); \
889 Write_##T##_8(device, buffer, SamplesToDo); \
894 DECL_TEMPLATE(ALfloat
)
895 DECL_TEMPLATE(ALuint
)
897 DECL_TEMPLATE(ALushort
)
898 DECL_TEMPLATE(ALshort
)
899 DECL_TEMPLATE(ALubyte
)
900 DECL_TEMPLATE(ALbyte
)
904 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
907 ALeffectslot
**slot
, **slot_end
;
908 ALsource
**src
, **src_end
;
913 fpuState
= SetMixerFPUMode();
917 SamplesToDo
= minu(size
, BUFFERSIZE
);
918 memset(device
->DryBuffer
, 0, SamplesToDo
*MaxChannels
*sizeof(ALfloat
));
920 ALCdevice_Lock(device
);
921 ctx
= device
->ContextList
;
924 ALenum DeferUpdates
= ctx
->DeferUpdates
;
925 ALenum UpdateSources
= AL_FALSE
;
928 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
930 /* source processing */
931 src
= ctx
->ActiveSources
;
932 src_end
= src
+ ctx
->ActiveSourceCount
;
933 while(src
!= src_end
)
935 if((*src
)->state
!= AL_PLAYING
)
937 --(ctx
->ActiveSourceCount
);
942 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
944 ALsource_Update(*src
, ctx
);
946 MixSource(*src
, device
, SamplesToDo
);
950 /* effect slot processing */
951 slot
= ctx
->ActiveEffectSlots
;
952 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
953 while(slot
!= slot_end
)
955 for(c
= 0;c
< SamplesToDo
;c
++)
957 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
958 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
960 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
961 (*slot
)->PendingClicks
[0] = 0.0f
;
963 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
964 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
966 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
967 (*slot
)->WetBuffer
, device
->DryBuffer
);
969 for(i
= 0;i
< SamplesToDo
;i
++)
970 (*slot
)->WetBuffer
[i
] = 0.0f
;
978 slot
= &device
->DefaultSlot
;
981 for(c
= 0;c
< SamplesToDo
;c
++)
983 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
984 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
986 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
987 (*slot
)->PendingClicks
[0] = 0.0f
;
989 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
990 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
992 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
993 (*slot
)->WetBuffer
, device
->DryBuffer
);
995 for(i
= 0;i
< SamplesToDo
;i
++)
996 (*slot
)->WetBuffer
[i
] = 0.0f
;
998 ALCdevice_Unlock(device
);
1000 /* Click-removal. Could do better; this only really handles immediate
1001 * changes between updates where a predictive sample could be
1002 * generated. Delays caused by effects and HRTF aren't caught. */
1003 if(device
->FmtChans
== DevFmtMono
)
1005 for(i
= 0;i
< SamplesToDo
;i
++)
1007 device
->DryBuffer
[i
][FrontCenter
] += device
->ClickRemoval
[FrontCenter
];
1008 device
->ClickRemoval
[FrontCenter
] -= device
->ClickRemoval
[FrontCenter
] * (1.0f
/256.0f
);
1010 device
->ClickRemoval
[FrontCenter
] += device
->PendingClicks
[FrontCenter
];
1011 device
->PendingClicks
[FrontCenter
] = 0.0f
;
1013 else if(device
->FmtChans
== DevFmtStereo
)
1015 /* Assumes the first two channels are FrontLeft and FrontRight */
1016 for(i
= 0;i
< SamplesToDo
;i
++)
1018 for(c
= 0;c
< 2;c
++)
1020 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1021 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1024 for(c
= 0;c
< 2;c
++)
1026 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1027 device
->PendingClicks
[c
] = 0.0f
;
1031 for(i
= 0;i
< SamplesToDo
;i
++)
1032 bs2b_cross_feed(device
->Bs2b
, &device
->DryBuffer
[i
][0]);
1037 for(i
= 0;i
< SamplesToDo
;i
++)
1039 for(c
= 0;c
< MaxChannels
;c
++)
1041 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1042 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1045 for(c
= 0;c
< MaxChannels
;c
++)
1047 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1048 device
->PendingClicks
[c
] = 0.0f
;
1054 switch(device
->FmtType
)
1057 Write_ALbyte(device
, buffer
, SamplesToDo
);
1060 Write_ALubyte(device
, buffer
, SamplesToDo
);
1063 Write_ALshort(device
, buffer
, SamplesToDo
);
1066 Write_ALushort(device
, buffer
, SamplesToDo
);
1069 Write_ALint(device
, buffer
, SamplesToDo
);
1072 Write_ALuint(device
, buffer
, SamplesToDo
);
1075 Write_ALfloat(device
, buffer
, SamplesToDo
);
1080 size
-= SamplesToDo
;
1083 RestoreFPUMode(fpuState
);
1087 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1089 ALCcontext
*Context
;
1091 ALCdevice_Lock(device
);
1092 device
->Connected
= ALC_FALSE
;
1094 Context
= device
->ContextList
;
1097 ALsource
**src
, **src_end
;
1099 src
= Context
->ActiveSources
;
1100 src_end
= src
+ Context
->ActiveSourceCount
;
1101 while(src
!= src_end
)
1103 if((*src
)->state
== AL_PLAYING
)
1105 (*src
)->state
= AL_STOPPED
;
1106 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1107 (*src
)->position
= 0;
1108 (*src
)->position_fraction
= 0;
1112 Context
->ActiveSourceCount
= 0;
1114 Context
= Context
->next
;
1116 ALCdevice_Unlock(device
);