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. */
681 ev
= aluAsin(Position
[1]);
682 az
= aluAtan2(Position
[0], -Position
[2]*ZScale
);
685 /* Check to see if the HRIR is already moving. */
686 if(ALSource
->Hrtf
.Moving
)
688 /* Calculate the normalized HRTF transition factor (delta). */
689 delta
= CalcHrtfDelta(ALSource
->Params
.Direct
.Hrtf
.Gain
, DryGain
,
690 ALSource
->Params
.Direct
.Hrtf
.Dir
, Position
);
691 /* If the delta is large enough, get the moving HRIR target
692 * coefficients, target delays, steppping values, and counter. */
695 ALSource
->Hrtf
.Counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
696 ev
, az
, DryGain
, delta
,
697 ALSource
->Hrtf
.Counter
,
698 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
699 ALSource
->Params
.Direct
.Hrtf
.Delay
[0],
700 ALSource
->Params
.Direct
.Hrtf
.CoeffStep
,
701 ALSource
->Params
.Direct
.Hrtf
.DelayStep
);
702 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
703 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
704 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
705 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
710 /* Get the initial (static) HRIR coefficients and delays. */
711 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
712 ALSource
->Params
.Direct
.Hrtf
.Coeffs
[0],
713 ALSource
->Params
.Direct
.Hrtf
.Delay
[0]);
714 ALSource
->Hrtf
.Counter
= 0;
715 ALSource
->Params
.Direct
.Hrtf
.Gain
= DryGain
;
716 ALSource
->Params
.Direct
.Hrtf
.Dir
[0] = Position
[0];
717 ALSource
->Params
.Direct
.Hrtf
.Dir
[1] = Position
[1];
718 ALSource
->Params
.Direct
.Hrtf
.Dir
[2] = Position
[2];
723 ALfloat (*Matrix
)[MAXCHANNELS
] = ALSource
->Params
.Direct
.Gains
;
724 ALfloat DirGain
= 0.0f
;
727 for(i
= 0;i
< MAXCHANNELS
;i
++)
729 for(j
= 0;j
< MAXCHANNELS
;j
++)
733 /* Normalize the length, and compute panned gains. */
736 ALfloat invlen
= 1.0f
/Distance
;
737 Position
[0] *= invlen
;
738 Position
[1] *= invlen
;
739 Position
[2] *= invlen
;
741 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
742 ComputeAngleGains(Device
, aluAtan2(Position
[0], -Position
[2]*ZScale
), 0.0f
,
743 DryGain
*DirGain
, Matrix
[0]);
746 /* Adjustment for vertical offsets. Not the greatest, but simple
748 AmbientGain
= DryGain
* aluSqrt(1.0f
/Device
->NumChan
) * (1.0f
-DirGain
);
749 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
751 enum Channel chan
= Device
->Speaker2Chan
[i
];
752 Matrix
[0][chan
] = maxf(Matrix
[0][chan
], AmbientGain
);
755 for(i
= 0;i
< NumSends
;i
++)
756 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
758 /* Update filter coefficients. */
759 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
761 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
762 for(i
= 0;i
< NumSends
;i
++)
764 ALfloat a
= lpCoeffCalc(WetGainHF
[i
], cw
);
765 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
770 static __inline ALfloat
aluF2F(ALfloat val
)
772 static __inline ALint
aluF2I(ALfloat val
)
774 if(val
> 1.0f
) return 2147483647;
775 if(val
< -1.0f
) return -2147483647-1;
776 return fastf2i((ALfloat
)(val
*2147483647.0));
778 static __inline ALuint
aluF2UI(ALfloat val
)
779 { return aluF2I(val
)+2147483648u; }
780 static __inline ALshort
aluF2S(ALfloat val
)
781 { return aluF2I(val
)>>16; }
782 static __inline ALushort
aluF2US(ALfloat val
)
783 { return aluF2S(val
)+32768; }
784 static __inline ALbyte
aluF2B(ALfloat val
)
785 { return aluF2I(val
)>>24; }
786 static __inline ALubyte
aluF2UB(ALfloat val
)
787 { return aluF2B(val
)+128; }
789 #define DECL_TEMPLATE(T, N, func) \
790 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
791 ALuint SamplesToDo) \
793 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
794 const enum Channel *ChanMap = device->DevChannels; \
797 for(j = 0;j < N;j++) \
799 T *RESTRICT out = buffer + j; \
800 enum Channel chan = ChanMap[j]; \
802 for(i = 0;i < SamplesToDo;i++) \
803 out[i*N] = func(DryBuffer[i][chan]); \
807 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
808 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
809 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
810 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
811 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
812 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
814 DECL_TEMPLATE(ALuint
, 1, aluF2UI
)
815 DECL_TEMPLATE(ALuint
, 2, aluF2UI
)
816 DECL_TEMPLATE(ALuint
, 4, aluF2UI
)
817 DECL_TEMPLATE(ALuint
, 6, aluF2UI
)
818 DECL_TEMPLATE(ALuint
, 7, aluF2UI
)
819 DECL_TEMPLATE(ALuint
, 8, aluF2UI
)
821 DECL_TEMPLATE(ALint
, 1, aluF2I
)
822 DECL_TEMPLATE(ALint
, 2, aluF2I
)
823 DECL_TEMPLATE(ALint
, 4, aluF2I
)
824 DECL_TEMPLATE(ALint
, 6, aluF2I
)
825 DECL_TEMPLATE(ALint
, 7, aluF2I
)
826 DECL_TEMPLATE(ALint
, 8, aluF2I
)
828 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
829 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
830 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
831 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
832 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
833 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
835 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
836 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
837 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
838 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
839 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
840 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
842 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
843 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
844 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
845 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
846 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
847 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
849 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
850 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
851 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
852 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
853 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
854 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
858 #define DECL_TEMPLATE(T) \
859 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
861 switch(device->FmtChans) \
864 Write_##T##_1(device, buffer, SamplesToDo); \
867 Write_##T##_2(device, buffer, SamplesToDo); \
870 Write_##T##_4(device, buffer, SamplesToDo); \
873 case DevFmtX51Side: \
874 Write_##T##_6(device, buffer, SamplesToDo); \
877 Write_##T##_7(device, buffer, SamplesToDo); \
880 Write_##T##_8(device, buffer, SamplesToDo); \
885 DECL_TEMPLATE(ALfloat
)
886 DECL_TEMPLATE(ALuint
)
888 DECL_TEMPLATE(ALushort
)
889 DECL_TEMPLATE(ALshort
)
890 DECL_TEMPLATE(ALubyte
)
891 DECL_TEMPLATE(ALbyte
)
895 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
898 ALeffectslot
**slot
, **slot_end
;
899 ALsource
**src
, **src_end
;
904 fpuState
= SetMixerFPUMode();
908 SamplesToDo
= minu(size
, BUFFERSIZE
);
909 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
912 ctx
= device
->ContextList
;
915 ALenum DeferUpdates
= ctx
->DeferUpdates
;
916 ALenum UpdateSources
= AL_FALSE
;
919 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
921 /* source processing */
922 src
= ctx
->ActiveSources
;
923 src_end
= src
+ ctx
->ActiveSourceCount
;
924 while(src
!= src_end
)
926 if((*src
)->state
!= AL_PLAYING
)
928 --(ctx
->ActiveSourceCount
);
933 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
935 ALsource_Update(*src
, ctx
);
937 MixSource(*src
, device
, SamplesToDo
);
941 /* effect slot processing */
942 slot
= ctx
->ActiveEffectSlots
;
943 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
944 while(slot
!= slot_end
)
946 for(c
= 0;c
< SamplesToDo
;c
++)
948 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
949 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
951 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
952 (*slot
)->PendingClicks
[0] = 0.0f
;
954 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
955 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
957 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
958 (*slot
)->WetBuffer
, device
->DryBuffer
);
960 for(i
= 0;i
< SamplesToDo
;i
++)
961 (*slot
)->WetBuffer
[i
] = 0.0f
;
969 slot
= &device
->DefaultSlot
;
972 for(c
= 0;c
< SamplesToDo
;c
++)
974 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
975 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
977 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
978 (*slot
)->PendingClicks
[0] = 0.0f
;
980 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
981 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
983 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
984 (*slot
)->WetBuffer
, device
->DryBuffer
);
986 for(i
= 0;i
< SamplesToDo
;i
++)
987 (*slot
)->WetBuffer
[i
] = 0.0f
;
989 UnlockDevice(device
);
991 /* Click-removal. Could do better; this only really handles immediate
992 * changes between updates where a predictive sample could be
993 * generated. Delays caused by effects and HRTF aren't caught. */
994 if(device
->FmtChans
== DevFmtMono
)
996 for(i
= 0;i
< SamplesToDo
;i
++)
998 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
999 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] * (1.0f
/256.0f
);
1001 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1002 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1004 else if(device
->FmtChans
== DevFmtStereo
)
1006 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1007 for(i
= 0;i
< SamplesToDo
;i
++)
1009 for(c
= 0;c
< 2;c
++)
1011 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1012 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1015 for(c
= 0;c
< 2;c
++)
1017 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1018 device
->PendingClicks
[c
] = 0.0f
;
1022 for(i
= 0;i
< SamplesToDo
;i
++)
1023 bs2b_cross_feed(device
->Bs2b
, &device
->DryBuffer
[i
][0]);
1028 for(i
= 0;i
< SamplesToDo
;i
++)
1030 for(c
= 0;c
< MAXCHANNELS
;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
< MAXCHANNELS
;c
++)
1038 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1039 device
->PendingClicks
[c
] = 0.0f
;
1045 switch(device
->FmtType
)
1048 Write_ALbyte(device
, buffer
, SamplesToDo
);
1051 Write_ALubyte(device
, buffer
, SamplesToDo
);
1054 Write_ALshort(device
, buffer
, SamplesToDo
);
1057 Write_ALushort(device
, buffer
, SamplesToDo
);
1060 Write_ALint(device
, buffer
, SamplesToDo
);
1063 Write_ALuint(device
, buffer
, SamplesToDo
);
1066 Write_ALfloat(device
, buffer
, SamplesToDo
);
1071 size
-= SamplesToDo
;
1074 RestoreFPUMode(fpuState
);
1078 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1080 ALCcontext
*Context
;
1083 device
->Connected
= ALC_FALSE
;
1085 Context
= device
->ContextList
;
1088 ALsource
**src
, **src_end
;
1090 src
= Context
->ActiveSources
;
1091 src_end
= src
+ Context
->ActiveSourceCount
;
1092 while(src
!= src_end
)
1094 if((*src
)->state
== AL_PLAYING
)
1096 (*src
)->state
= AL_STOPPED
;
1097 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1098 (*src
)->position
= 0;
1099 (*src
)->position_fraction
= 0;
1103 Context
->ActiveSourceCount
= 0;
1105 Context
= Context
->next
;
1107 UnlockDevice(device
);