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
->Gain
;
137 MinVolume
= ALSource
->MinGain
;
138 MaxVolume
= ALSource
->MaxGain
;
139 Pitch
= ALSource
->Pitch
;
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
;
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
->SpeedOfSound
* 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
->Gain
;
393 MinVolume
= ALSource
->MinGain
;
394 MaxVolume
= ALSource
->MaxGain
;
395 Pitch
= ALSource
->Pitch
;
396 Resampler
= ALSource
->Resampler
;
397 Position
[0] = ALSource
->Position
[0];
398 Position
[1] = ALSource
->Position
[1];
399 Position
[2] = ALSource
->Position
[2];
400 Direction
[0] = ALSource
->Orientation
[0];
401 Direction
[1] = ALSource
->Orientation
[1];
402 Direction
[2] = ALSource
->Orientation
[2];
403 Velocity
[0] = ALSource
->Velocity
[0];
404 Velocity
[1] = ALSource
->Velocity
[1];
405 Velocity
[2] = ALSource
->Velocity
[2];
406 MinDist
= ALSource
->RefDistance
;
407 MaxDist
= ALSource
->MaxDistance
;
408 Rolloff
= ALSource
->RollOffFactor
;
409 InnerAngle
= ALSource
->InnerAngle
* ConeScale
;
410 OuterAngle
= ALSource
->OuterAngle
* 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
->HeadRelative
== 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
:
555 ClampedDist
= MinDist
;
559 // Source Gain + Attenuation
560 DryGain
= SourceVolume
* Attenuation
;
561 for(i
= 0;i
< NumSends
;i
++)
562 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
564 // Distance-based air absorption
565 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
567 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
568 DryGainHF
*= aluPow(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
569 for(i
= 0;i
< NumSends
;i
++)
570 WetGainHF
[i
] *= aluPow(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
575 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
577 /* Apply a decay-time transformation to the wet path, based on the
578 * attenuation of the dry path.
580 * Using the apparent distance, based on the distance attenuation, 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*/, ApparentDist
/DecayDistance
[i
]);
591 /* Calculate directional soundcones */
592 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * (180.0f
/F_PI
);
593 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
595 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
596 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
597 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
599 else if(Angle
> OuterAngle
)
601 ConeVolume
= ALSource
->OuterGain
;
602 ConeHF
= ALSource
->OuterGainHF
;
610 DryGain
*= ConeVolume
;
613 for(i
= 0;i
< NumSends
;i
++)
614 WetGain
[i
] *= ConeVolume
;
620 for(i
= 0;i
< NumSends
;i
++)
621 WetGainHF
[i
] *= ConeHF
;
624 // Clamp to Min/Max Gain
625 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
626 for(i
= 0;i
< NumSends
;i
++)
627 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
629 // Apply filter gains and filters
630 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
631 DryGainHF
*= ALSource
->DirectGainHF
;
632 for(i
= 0;i
< NumSends
;i
++)
634 WetGain
[i
] *= ALSource
->Send
[i
].WetGain
* ListenerGain
;
635 WetGainHF
[i
] *= ALSource
->Send
[i
].WetGainHF
;
638 // Calculate Velocity
639 if(DopplerFactor
> 0.0f
)
643 if(SpeedOfSound
< 1.0f
)
645 DopplerFactor
*= 1.0f
/SpeedOfSound
;
649 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
650 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
652 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
653 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
656 BufferListItem
= ALSource
->queue
;
657 while(BufferListItem
!= NULL
)
660 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
662 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
663 ALSource
->NumChannels
;
664 maxstep
-= ResamplerPadding
[Resampler
] +
665 ResamplerPrePadding
[Resampler
] + 1;
666 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
668 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
669 if(Pitch
> (ALfloat
)maxstep
)
670 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
673 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
674 if(ALSource
->Params
.Step
== 0)
675 ALSource
->Params
.Step
= 1;
677 if(ALSource
->Params
.Step
== FRACTIONONE
)
678 Resampler
= PointResampler
;
682 BufferListItem
= BufferListItem
->next
;
685 ALSource
->Params
.DoMix
= SelectHrtfMixer(Resampler
);
687 ALSource
->Params
.DoMix
= SelectMixer(Resampler
);
691 // Use a binaural HRTF algorithm for stereo headphone playback
692 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
696 ALfloat invlen
= 1.0f
/Distance
;
697 Position
[0] *= invlen
;
698 Position
[1] *= invlen
;
699 Position
[2] *= invlen
;
701 // Calculate elevation and azimuth only when the source is not at
702 // the listener. This prevents +0 and -0 Z from producing
703 // inconsistent panning.
704 ev
= aluAsin(Position
[1]);
705 az
= aluAtan2(Position
[0], -Position
[2]*ZScale
);
708 // Check to see if the HRIR is already moving.
709 if(ALSource
->HrtfMoving
)
711 // Calculate the normalized HRTF transition factor (delta).
712 delta
= CalcHrtfDelta(ALSource
->Params
.HrtfGain
, DryGain
,
713 ALSource
->Params
.HrtfDir
, Position
);
714 // If the delta is large enough, get the moving HRIR target
715 // coefficients, target delays, steppping values, and counter.
718 ALSource
->HrtfCounter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
719 ev
, az
, DryGain
, delta
,
720 ALSource
->HrtfCounter
,
721 ALSource
->Params
.HrtfCoeffs
[0],
722 ALSource
->Params
.HrtfDelay
[0],
723 ALSource
->Params
.HrtfCoeffStep
,
724 ALSource
->Params
.HrtfDelayStep
);
725 ALSource
->Params
.HrtfGain
= DryGain
;
726 ALSource
->Params
.HrtfDir
[0] = Position
[0];
727 ALSource
->Params
.HrtfDir
[1] = Position
[1];
728 ALSource
->Params
.HrtfDir
[2] = Position
[2];
733 // Get the initial (static) HRIR coefficients and delays.
734 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
735 ALSource
->Params
.HrtfCoeffs
[0],
736 ALSource
->Params
.HrtfDelay
[0]);
737 ALSource
->HrtfCounter
= 0;
738 ALSource
->Params
.HrtfGain
= DryGain
;
739 ALSource
->Params
.HrtfDir
[0] = Position
[0];
740 ALSource
->Params
.HrtfDir
[1] = Position
[1];
741 ALSource
->Params
.HrtfDir
[2] = Position
[2];
746 // Use energy-preserving panning algorithm for multi-speaker playback
747 ALfloat DirGain
, AmbientGain
;
748 const ALfloat
*ChannelGain
;
752 length
= maxf(Distance
, MinDist
);
755 ALfloat invlen
= 1.0f
/length
;
756 Position
[0] *= invlen
;
757 Position
[1] *= invlen
;
758 Position
[2] *= invlen
;
761 pos
= aluCart2LUTpos(-Position
[2]*ZScale
, Position
[0]);
762 ChannelGain
= Device
->PanningLUT
[pos
];
764 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
765 // elevation adjustment for directional gain. this sucks, but
766 // has low complexity
767 AmbientGain
= aluSqrt(1.0f
/Device
->NumChan
);
768 for(i
= 0;i
< MAXCHANNELS
;i
++)
771 for(i2
= 0;i2
< MAXCHANNELS
;i2
++)
772 ALSource
->Params
.DryGains
[i
][i2
] = 0.0f
;
774 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
776 enum Channel chan
= Device
->Speaker2Chan
[i
];
777 ALfloat gain
= lerp(AmbientGain
, ChannelGain
[chan
], DirGain
);
778 ALSource
->Params
.DryGains
[0][chan
] = DryGain
* gain
;
781 for(i
= 0;i
< NumSends
;i
++)
782 ALSource
->Params
.Send
[i
].WetGain
= WetGain
[i
];
784 /* Update filter coefficients. */
785 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
787 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
788 for(i
= 0;i
< NumSends
;i
++)
790 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
791 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
796 static __inline ALfloat
aluF2F(ALfloat val
)
798 static __inline ALint
aluF2I(ALfloat val
)
800 if(val
> 1.0f
) return 2147483647;
801 if(val
< -1.0f
) return -2147483647-1;
802 return fastf2i((ALfloat
)(val
*2147483647.0));
804 static __inline ALuint
aluF2UI(ALfloat val
)
805 { return aluF2I(val
)+2147483648u; }
806 static __inline ALshort
aluF2S(ALfloat val
)
807 { return aluF2I(val
)>>16; }
808 static __inline ALushort
aluF2US(ALfloat val
)
809 { return aluF2S(val
)+32768; }
810 static __inline ALbyte
aluF2B(ALfloat val
)
811 { return aluF2I(val
)>>24; }
812 static __inline ALubyte
aluF2UB(ALfloat val
)
813 { return aluF2B(val
)+128; }
815 #define DECL_TEMPLATE(T, N, func) \
816 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
817 ALuint SamplesToDo) \
819 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
820 const enum Channel *ChanMap = device->DevChannels; \
823 for(j = 0;j < N;j++) \
825 T *RESTRICT out = buffer + j; \
826 enum Channel chan = ChanMap[j]; \
828 for(i = 0;i < SamplesToDo;i++) \
829 out[i*N] = func(DryBuffer[i][chan]); \
833 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
834 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
835 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
836 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
837 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
838 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
840 DECL_TEMPLATE(ALuint
, 1, aluF2UI
)
841 DECL_TEMPLATE(ALuint
, 2, aluF2UI
)
842 DECL_TEMPLATE(ALuint
, 4, aluF2UI
)
843 DECL_TEMPLATE(ALuint
, 6, aluF2UI
)
844 DECL_TEMPLATE(ALuint
, 7, aluF2UI
)
845 DECL_TEMPLATE(ALuint
, 8, aluF2UI
)
847 DECL_TEMPLATE(ALint
, 1, aluF2I
)
848 DECL_TEMPLATE(ALint
, 2, aluF2I
)
849 DECL_TEMPLATE(ALint
, 4, aluF2I
)
850 DECL_TEMPLATE(ALint
, 6, aluF2I
)
851 DECL_TEMPLATE(ALint
, 7, aluF2I
)
852 DECL_TEMPLATE(ALint
, 8, aluF2I
)
854 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
855 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
856 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
857 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
858 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
859 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
861 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
862 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
863 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
864 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
865 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
866 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
868 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
869 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
870 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
871 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
872 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
873 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
875 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
876 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
877 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
878 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
879 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
880 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
884 #define DECL_TEMPLATE(T) \
885 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
887 switch(device->FmtChans) \
890 Write_##T##_1(device, buffer, SamplesToDo); \
893 Write_##T##_2(device, buffer, SamplesToDo); \
896 Write_##T##_4(device, buffer, SamplesToDo); \
899 case DevFmtX51Side: \
900 Write_##T##_6(device, buffer, SamplesToDo); \
903 Write_##T##_7(device, buffer, SamplesToDo); \
906 Write_##T##_8(device, buffer, SamplesToDo); \
911 DECL_TEMPLATE(ALfloat
)
912 DECL_TEMPLATE(ALuint
)
914 DECL_TEMPLATE(ALushort
)
915 DECL_TEMPLATE(ALshort
)
916 DECL_TEMPLATE(ALubyte
)
917 DECL_TEMPLATE(ALbyte
)
921 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
924 ALeffectslot
**slot
, **slot_end
;
925 ALsource
**src
, **src_end
;
930 fpuState
= SetMixerFPUMode();
934 /* Setup variables */
935 SamplesToDo
= minu(size
, BUFFERSIZE
);
937 /* Clear mixing buffer */
938 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
941 ctx
= device
->ContextList
;
944 ALenum DeferUpdates
= ctx
->DeferUpdates
;
945 ALenum UpdateSources
= AL_FALSE
;
948 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
950 src
= ctx
->ActiveSources
;
951 src_end
= src
+ ctx
->ActiveSourceCount
;
952 while(src
!= src_end
)
954 if((*src
)->state
!= AL_PLAYING
)
956 --(ctx
->ActiveSourceCount
);
961 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
963 ALsource_Update(*src
, ctx
);
965 MixSource(*src
, device
, SamplesToDo
);
969 /* effect slot processing */
970 slot
= ctx
->ActiveEffectSlots
;
971 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
972 while(slot
!= slot_end
)
974 for(c
= 0;c
< SamplesToDo
;c
++)
976 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
977 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
979 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
980 (*slot
)->PendingClicks
[0] = 0.0f
;
982 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
983 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
985 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
986 (*slot
)->WetBuffer
, device
->DryBuffer
);
988 for(i
= 0;i
< SamplesToDo
;i
++)
989 (*slot
)->WetBuffer
[i
] = 0.0f
;
997 slot
= &device
->DefaultSlot
;
1000 for(c
= 0;c
< SamplesToDo
;c
++)
1002 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
1003 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
1005 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
1006 (*slot
)->PendingClicks
[0] = 0.0f
;
1008 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1009 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
1011 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
1012 (*slot
)->WetBuffer
, device
->DryBuffer
);
1014 for(i
= 0;i
< SamplesToDo
;i
++)
1015 (*slot
)->WetBuffer
[i
] = 0.0f
;
1017 UnlockDevice(device
);
1019 //Post processing loop
1020 if(device
->FmtChans
== DevFmtMono
)
1022 for(i
= 0;i
< SamplesToDo
;i
++)
1024 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
1025 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] * (1.0f
/256.0f
);
1027 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1028 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1030 else if(device
->FmtChans
== DevFmtStereo
)
1032 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1033 for(i
= 0;i
< SamplesToDo
;i
++)
1035 for(c
= 0;c
< 2;c
++)
1037 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1038 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1041 for(c
= 0;c
< 2;c
++)
1043 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1044 device
->PendingClicks
[c
] = 0.0f
;
1048 for(i
= 0;i
< SamplesToDo
;i
++)
1049 bs2b_cross_feed(device
->Bs2b
, &device
->DryBuffer
[i
][0]);
1054 for(i
= 0;i
< SamplesToDo
;i
++)
1056 for(c
= 0;c
< MAXCHANNELS
;c
++)
1058 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1059 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1062 for(c
= 0;c
< MAXCHANNELS
;c
++)
1064 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1065 device
->PendingClicks
[c
] = 0.0f
;
1071 switch(device
->FmtType
)
1074 Write_ALbyte(device
, buffer
, SamplesToDo
);
1077 Write_ALubyte(device
, buffer
, SamplesToDo
);
1080 Write_ALshort(device
, buffer
, SamplesToDo
);
1083 Write_ALushort(device
, buffer
, SamplesToDo
);
1086 Write_ALint(device
, buffer
, SamplesToDo
);
1089 Write_ALuint(device
, buffer
, SamplesToDo
);
1092 Write_ALfloat(device
, buffer
, SamplesToDo
);
1097 size
-= SamplesToDo
;
1100 RestoreFPUMode(fpuState
);
1104 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1106 ALCcontext
*Context
;
1109 device
->Connected
= ALC_FALSE
;
1111 Context
= device
->ContextList
;
1114 ALsource
**src
, **src_end
;
1116 src
= Context
->ActiveSources
;
1117 src_end
= src
+ Context
->ActiveSourceCount
;
1118 while(src
!= src_end
)
1120 if((*src
)->state
== AL_PLAYING
)
1122 (*src
)->state
= AL_STOPPED
;
1123 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1124 (*src
)->position
= 0;
1125 (*src
)->position_fraction
= 0;
1129 Context
->ActiveSourceCount
= 0;
1131 Context
= Context
->next
;
1133 UnlockDevice(device
);