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
.DryMix
= SelectHrtfMixer(Resampler
);
177 ALSource
->Params
.DryMix
= SelectDirectMixer(Resampler
);
178 ALSource
->Params
.WetMix
= SelectSendMixer(Resampler
);
180 /* Calculate gains */
181 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
182 DryGain
*= ALSource
->DirectGain
;
183 DryGainHF
= ALSource
->DirectGainHF
;
184 for(i
= 0;i
< NumSends
;i
++)
186 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
187 WetGain
[i
] *= ALSource
->Send
[i
].Gain
;
188 WetGainHF
[i
] = ALSource
->Send
[i
].GainHF
;
191 SrcMatrix
= ALSource
->Params
.Direct
.Gains
;
192 for(i
= 0;i
< MAXCHANNELS
;i
++)
194 for(c
= 0;c
< MAXCHANNELS
;c
++)
195 SrcMatrix
[i
][c
] = 0.0f
;
204 if(!DirectChannels
&& (Device
->Flags
&DEVICE_DUPLICATE_STEREO
))
206 DryGain
*= aluSqrt(2.0f
/4.0f
);
209 pos
= aluCart2LUTpos(aluCos(RearMap
[c
].angle
),
210 aluSin(RearMap
[c
].angle
));
211 ChannelGain
= Device
->PanningLUT
[pos
];
213 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
215 enum Channel chan
= Device
->Speaker2Chan
[i
];
216 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
251 if(DirectChannels
!= AL_FALSE
)
253 for(c
= 0;c
< num_channels
;c
++)
255 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
257 enum Channel chan
= Device
->Speaker2Chan
[i
];
258 if(chan
== chans
[c
].channel
)
260 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
;
266 else if(Device
->Hrtf
)
268 for(c
= 0;c
< num_channels
;c
++)
270 if(chans
[c
].channel
== LFE
)
273 ALSource
->Params
.Hrtf
.Delay
[c
][0] = 0;
274 ALSource
->Params
.Hrtf
.Delay
[c
][1] = 0;
275 for(i
= 0;i
< HRIR_LENGTH
;i
++)
277 ALSource
->Params
.Hrtf
.Coeffs
[c
][i
][0] = 0.0f
;
278 ALSource
->Params
.Hrtf
.Coeffs
[c
][i
][1] = 0.0f
;
283 /* Get the static HRIR coefficients and delays for this
285 GetLerpedHrtfCoeffs(Device
->Hrtf
,
286 0.0f
, chans
[c
].angle
,
287 DryGain
*ListenerGain
,
288 ALSource
->Params
.Hrtf
.Coeffs
[c
],
289 ALSource
->Params
.Hrtf
.Delay
[c
]);
292 ALSource
->Hrtf
.Counter
= 0;
296 for(c
= 0;c
< num_channels
;c
++)
298 /* Special-case LFE */
299 if(chans
[c
].channel
== LFE
)
301 SrcMatrix
[c
][LFE
] += DryGain
* ListenerGain
;
304 pos
= aluCart2LUTpos(aluCos(chans
[c
].angle
), aluSin(chans
[c
].angle
));
305 ChannelGain
= Device
->PanningLUT
[pos
];
307 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
309 enum Channel chan
= Device
->Speaker2Chan
[i
];
310 SrcMatrix
[c
][chan
] += DryGain
* ListenerGain
*
315 for(i
= 0;i
< NumSends
;i
++)
317 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
320 Slot
= Device
->DefaultSlot
;
321 if(Slot
&& Slot
->effect
.type
== AL_EFFECT_NULL
)
323 ALSource
->Params
.Slot
[i
] = Slot
;
324 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
] * ListenerGain
;
327 /* Update filter coefficients. Calculations based on the I3DL2
329 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
331 /* We use two chained one-pole filters, so we need to take the
332 * square root of the squared gain, which is the same as the base
334 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
335 for(i
= 0;i
< NumSends
;i
++)
337 /* We use a one-pole filter, so we need to take the squared gain */
338 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
339 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
343 ALvoid
CalcSourceParams(ALsource
*ALSource
, const ALCcontext
*ALContext
)
345 const ALCdevice
*Device
= ALContext
->Device
;
346 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
347 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
348 ALfloat Velocity
[3],ListenerVel
[3];
349 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
350 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
351 ALfloat DopplerFactor
, SpeedOfSound
;
352 ALfloat AirAbsorptionFactor
;
353 ALfloat RoomAirAbsorption
[MAX_SENDS
];
354 ALbufferlistitem
*BufferListItem
;
356 ALfloat RoomAttenuation
[MAX_SENDS
];
357 ALfloat MetersPerUnit
;
358 ALfloat RoomRolloffBase
;
359 ALfloat RoomRolloff
[MAX_SENDS
];
360 ALfloat DecayDistance
[MAX_SENDS
];
363 ALboolean DryGainHFAuto
;
364 ALfloat WetGain
[MAX_SENDS
];
365 ALfloat WetGainHF
[MAX_SENDS
];
366 ALboolean WetGainAuto
;
367 ALboolean WetGainHFAuto
;
368 enum Resampler Resampler
;
369 ALfloat Matrix
[4][4];
377 for(i
= 0;i
< MAX_SENDS
;i
++)
380 /* Get context/device properties */
381 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
382 SpeedOfSound
= ALContext
->SpeedOfSound
* ALContext
->DopplerVelocity
;
383 NumSends
= Device
->NumAuxSends
;
384 Frequency
= Device
->Frequency
;
386 /* Get listener properties */
387 ListenerGain
= ALContext
->Listener
.Gain
;
388 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
389 ListenerVel
[0] = ALContext
->Listener
.Velocity
[0];
390 ListenerVel
[1] = ALContext
->Listener
.Velocity
[1];
391 ListenerVel
[2] = ALContext
->Listener
.Velocity
[2];
395 Matrix
[i
][j
] = ALContext
->Listener
.Matrix
[i
][j
];
398 /* Get source properties */
399 SourceVolume
= ALSource
->Gain
;
400 MinVolume
= ALSource
->MinGain
;
401 MaxVolume
= ALSource
->MaxGain
;
402 Pitch
= ALSource
->Pitch
;
403 Resampler
= ALSource
->Resampler
;
404 Position
[0] = ALSource
->Position
[0];
405 Position
[1] = ALSource
->Position
[1];
406 Position
[2] = ALSource
->Position
[2];
407 Direction
[0] = ALSource
->Orientation
[0];
408 Direction
[1] = ALSource
->Orientation
[1];
409 Direction
[2] = ALSource
->Orientation
[2];
410 Velocity
[0] = ALSource
->Velocity
[0];
411 Velocity
[1] = ALSource
->Velocity
[1];
412 Velocity
[2] = ALSource
->Velocity
[2];
413 MinDist
= ALSource
->RefDistance
;
414 MaxDist
= ALSource
->MaxDistance
;
415 Rolloff
= ALSource
->RollOffFactor
;
416 InnerAngle
= ALSource
->InnerAngle
* ConeScale
;
417 OuterAngle
= ALSource
->OuterAngle
* ConeScale
;
418 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
419 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
420 WetGainAuto
= ALSource
->WetGainAuto
;
421 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
422 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
423 for(i
= 0;i
< NumSends
;i
++)
425 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
428 Slot
= Device
->DefaultSlot
;
429 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
432 RoomRolloff
[i
] = 0.0f
;
433 DecayDistance
[i
] = 0.0f
;
434 RoomAirAbsorption
[i
] = 1.0f
;
436 else if(Slot
->AuxSendAuto
)
438 RoomRolloff
[i
] = RoomRolloffBase
;
439 if(IsReverbEffect(Slot
->effect
.type
))
441 RoomRolloff
[i
] += Slot
->effect
.Reverb
.RoomRolloffFactor
;
442 DecayDistance
[i
] = Slot
->effect
.Reverb
.DecayTime
*
443 SPEEDOFSOUNDMETRESPERSEC
;
444 RoomAirAbsorption
[i
] = Slot
->effect
.Reverb
.AirAbsorptionGainHF
;
448 DecayDistance
[i
] = 0.0f
;
449 RoomAirAbsorption
[i
] = 1.0f
;
454 /* If the slot's auxiliary send auto is off, the data sent to the
455 * effect slot is the same as the dry path, sans filter effects */
456 RoomRolloff
[i
] = Rolloff
;
457 DecayDistance
[i
] = 0.0f
;
458 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
461 ALSource
->Params
.Slot
[i
] = Slot
;
464 /* Transform source to listener space (convert to head relative) */
465 if(ALSource
->HeadRelative
== AL_FALSE
)
467 /* Translate position */
468 Position
[0] -= ALContext
->Listener
.Position
[0];
469 Position
[1] -= ALContext
->Listener
.Position
[1];
470 Position
[2] -= ALContext
->Listener
.Position
[2];
472 /* Transform source vectors */
473 aluMatrixVector(Position
, 1.0f
, Matrix
);
474 aluMatrixVector(Direction
, 0.0f
, Matrix
);
475 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
476 /* Transform listener velocity */
477 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
481 /* Transform listener velocity from world space to listener space */
482 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
483 /* Offset the source velocity to be relative of the listener velocity */
484 Velocity
[0] += ListenerVel
[0];
485 Velocity
[1] += ListenerVel
[1];
486 Velocity
[2] += ListenerVel
[2];
489 SourceToListener
[0] = -Position
[0];
490 SourceToListener
[1] = -Position
[1];
491 SourceToListener
[2] = -Position
[2];
492 aluNormalize(SourceToListener
);
493 aluNormalize(Direction
);
495 /* Calculate distance attenuation */
496 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
497 ClampedDist
= Distance
;
500 for(i
= 0;i
< NumSends
;i
++)
501 RoomAttenuation
[i
] = 1.0f
;
502 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
503 ALContext
->DistanceModel
)
505 case InverseDistanceClamped
:
506 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
507 if(MaxDist
< MinDist
)
510 case InverseDistance
:
513 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
514 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
515 for(i
= 0;i
< NumSends
;i
++)
517 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
518 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
523 case LinearDistanceClamped
:
524 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
525 if(MaxDist
< MinDist
)
529 if(MaxDist
!= MinDist
)
531 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
532 Attenuation
= maxf(Attenuation
, 0.0f
);
533 for(i
= 0;i
< NumSends
;i
++)
535 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
536 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
541 case ExponentDistanceClamped
:
542 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
543 if(MaxDist
< MinDist
)
546 case ExponentDistance
:
547 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
549 Attenuation
= aluPow(ClampedDist
/MinDist
, -Rolloff
);
550 for(i
= 0;i
< NumSends
;i
++)
551 RoomAttenuation
[i
] = aluPow(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
555 case DisableDistance
:
556 ClampedDist
= MinDist
;
560 /* Source Gain + Attenuation */
561 DryGain
= SourceVolume
* Attenuation
;
562 for(i
= 0;i
< NumSends
;i
++)
563 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
565 /* Distance-based air absorption */
566 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
568 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
569 DryGainHF
*= aluPow(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
570 for(i
= 0;i
< NumSends
;i
++)
571 WetGainHF
[i
] *= aluPow(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
576 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
578 /* Apply a decay-time transformation to the wet path, based on the
579 * attenuation of the dry path.
581 * Using the apparent distance, based on the distance attenuation, the
582 * initial decay of the reverb effect is calculated and applied to the
585 for(i
= 0;i
< NumSends
;i
++)
587 if(DecayDistance
[i
] > 0.0f
)
588 WetGain
[i
] *= aluPow(0.001f
/*-60dB*/, ApparentDist
/DecayDistance
[i
]);
592 /* Calculate directional soundcones */
593 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * (180.0f
/F_PI
);
594 if(Angle
> InnerAngle
&& Angle
<= OuterAngle
)
596 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
597 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
598 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
600 else if(Angle
> OuterAngle
)
602 ConeVolume
= ALSource
->OuterGain
;
603 ConeHF
= ALSource
->OuterGainHF
;
611 DryGain
*= ConeVolume
;
614 for(i
= 0;i
< NumSends
;i
++)
615 WetGain
[i
] *= ConeVolume
;
621 for(i
= 0;i
< NumSends
;i
++)
622 WetGainHF
[i
] *= ConeHF
;
625 /* Clamp to Min/Max Gain */
626 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
627 for(i
= 0;i
< NumSends
;i
++)
628 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
630 /* Apply gain and frequency filters */
631 DryGain
*= ALSource
->DirectGain
* ListenerGain
;
632 DryGainHF
*= ALSource
->DirectGainHF
;
633 for(i
= 0;i
< NumSends
;i
++)
635 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
636 WetGainHF
[i
] *= ALSource
->Send
[i
].GainHF
;
639 /* Calculate velocity-based doppler effect */
640 if(DopplerFactor
> 0.0f
)
644 if(SpeedOfSound
< 1.0f
)
646 DopplerFactor
*= 1.0f
/SpeedOfSound
;
650 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
651 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
653 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
654 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
657 BufferListItem
= ALSource
->queue
;
658 while(BufferListItem
!= NULL
)
661 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
663 /* Calculate fixed-point stepping value, based on the pitch, buffer
664 * frequency, and output frequency. */
665 ALsizei maxstep
= STACK_DATA_SIZE
/sizeof(ALfloat
) /
666 ALSource
->NumChannels
;
667 maxstep
-= ResamplerPadding
[Resampler
] +
668 ResamplerPrePadding
[Resampler
] + 1;
669 maxstep
= mini(maxstep
, INT_MAX
>>FRACTIONBITS
);
671 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
672 if(Pitch
> (ALfloat
)maxstep
)
673 ALSource
->Params
.Step
= maxstep
<<FRACTIONBITS
;
676 ALSource
->Params
.Step
= fastf2i(Pitch
*FRACTIONONE
);
677 if(ALSource
->Params
.Step
== 0)
678 ALSource
->Params
.Step
= 1;
680 if(ALSource
->Params
.Step
== FRACTIONONE
)
681 Resampler
= PointResampler
;
685 BufferListItem
= BufferListItem
->next
;
688 ALSource
->Params
.DryMix
= SelectHrtfMixer(Resampler
);
690 ALSource
->Params
.DryMix
= SelectDirectMixer(Resampler
);
691 ALSource
->Params
.WetMix
= SelectSendMixer(Resampler
);
695 /* Use a binaural HRTF algorithm for stereo headphone playback */
696 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
700 ALfloat invlen
= 1.0f
/Distance
;
701 Position
[0] *= invlen
;
702 Position
[1] *= invlen
;
703 Position
[2] *= invlen
;
705 /* Calculate elevation and azimuth only when the source is not at
706 * the listener. This prevents +0 and -0 Z from producing
707 * inconsistent panning. */
708 ev
= aluAsin(Position
[1]);
709 az
= aluAtan2(Position
[0], -Position
[2]*ZScale
);
712 /* Check to see if the HRIR is already moving. */
713 if(ALSource
->Hrtf
.Moving
)
715 /* Calculate the normalized HRTF transition factor (delta). */
716 delta
= CalcHrtfDelta(ALSource
->Params
.Hrtf
.Gain
, DryGain
,
717 ALSource
->Params
.Hrtf
.Dir
, Position
);
718 /* If the delta is large enough, get the moving HRIR target
719 * coefficients, target delays, steppping values, and counter. */
722 ALSource
->Hrtf
.Counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
723 ev
, az
, DryGain
, delta
,
724 ALSource
->Hrtf
.Counter
,
725 ALSource
->Params
.Hrtf
.Coeffs
[0],
726 ALSource
->Params
.Hrtf
.Delay
[0],
727 ALSource
->Params
.Hrtf
.CoeffStep
,
728 ALSource
->Params
.Hrtf
.DelayStep
);
729 ALSource
->Params
.Hrtf
.Gain
= DryGain
;
730 ALSource
->Params
.Hrtf
.Dir
[0] = Position
[0];
731 ALSource
->Params
.Hrtf
.Dir
[1] = Position
[1];
732 ALSource
->Params
.Hrtf
.Dir
[2] = Position
[2];
737 /* Get the initial (static) HRIR coefficients and delays. */
738 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
739 ALSource
->Params
.Hrtf
.Coeffs
[0],
740 ALSource
->Params
.Hrtf
.Delay
[0]);
741 ALSource
->Hrtf
.Counter
= 0;
742 ALSource
->Params
.Hrtf
.Gain
= DryGain
;
743 ALSource
->Params
.Hrtf
.Dir
[0] = Position
[0];
744 ALSource
->Params
.Hrtf
.Dir
[1] = Position
[1];
745 ALSource
->Params
.Hrtf
.Dir
[2] = Position
[2];
750 /* Use a lookup table for panning multi-speaker playback. */
751 ALfloat DirGain
, AmbientGain
;
752 const ALfloat
*ChannelGain
;
756 /* Normalize the length based on the source's min distance. Sources
757 * closer than this will not pan as much. */
758 length
= maxf(Distance
, MinDist
);
761 ALfloat invlen
= 1.0f
/length
;
762 Position
[0] *= invlen
;
763 Position
[1] *= invlen
;
764 Position
[2] *= invlen
;
767 pos
= aluCart2LUTpos(-Position
[2]*ZScale
, Position
[0]);
768 ChannelGain
= Device
->PanningLUT
[pos
];
770 /* Adjustment for partial panning. Not the greatest, but simple
772 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
773 AmbientGain
= aluSqrt(1.0f
/Device
->NumChan
);
774 for(i
= 0;i
< MAXCHANNELS
;i
++)
776 for(j
= 0;j
< MAXCHANNELS
;j
++)
777 ALSource
->Params
.Direct
.Gains
[i
][j
] = 0.0f
;
779 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
781 enum Channel chan
= Device
->Speaker2Chan
[i
];
782 ALfloat gain
= lerp(AmbientGain
, ChannelGain
[chan
], DirGain
);
783 ALSource
->Params
.Direct
.Gains
[0][chan
] = DryGain
* gain
;
786 for(i
= 0;i
< NumSends
;i
++)
787 ALSource
->Params
.Send
[i
].Gain
= WetGain
[i
];
789 /* Update filter coefficients. */
790 cw
= aluCos(F_PI
*2.0f
* LOWPASSFREQREF
/ Frequency
);
792 ALSource
->Params
.Direct
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
793 for(i
= 0;i
< NumSends
;i
++)
795 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
796 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
801 static __inline ALfloat
aluF2F(ALfloat val
)
803 static __inline ALint
aluF2I(ALfloat val
)
805 if(val
> 1.0f
) return 2147483647;
806 if(val
< -1.0f
) return -2147483647-1;
807 return fastf2i((ALfloat
)(val
*2147483647.0));
809 static __inline ALuint
aluF2UI(ALfloat val
)
810 { return aluF2I(val
)+2147483648u; }
811 static __inline ALshort
aluF2S(ALfloat val
)
812 { return aluF2I(val
)>>16; }
813 static __inline ALushort
aluF2US(ALfloat val
)
814 { return aluF2S(val
)+32768; }
815 static __inline ALbyte
aluF2B(ALfloat val
)
816 { return aluF2I(val
)>>24; }
817 static __inline ALubyte
aluF2UB(ALfloat val
)
818 { return aluF2B(val
)+128; }
820 #define DECL_TEMPLATE(T, N, func) \
821 static void Write_##T##_##N(ALCdevice *device, T *RESTRICT buffer, \
822 ALuint SamplesToDo) \
824 ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
825 const enum Channel *ChanMap = device->DevChannels; \
828 for(j = 0;j < N;j++) \
830 T *RESTRICT out = buffer + j; \
831 enum Channel chan = ChanMap[j]; \
833 for(i = 0;i < SamplesToDo;i++) \
834 out[i*N] = func(DryBuffer[i][chan]); \
838 DECL_TEMPLATE(ALfloat
, 1, aluF2F
)
839 DECL_TEMPLATE(ALfloat
, 2, aluF2F
)
840 DECL_TEMPLATE(ALfloat
, 4, aluF2F
)
841 DECL_TEMPLATE(ALfloat
, 6, aluF2F
)
842 DECL_TEMPLATE(ALfloat
, 7, aluF2F
)
843 DECL_TEMPLATE(ALfloat
, 8, aluF2F
)
845 DECL_TEMPLATE(ALuint
, 1, aluF2UI
)
846 DECL_TEMPLATE(ALuint
, 2, aluF2UI
)
847 DECL_TEMPLATE(ALuint
, 4, aluF2UI
)
848 DECL_TEMPLATE(ALuint
, 6, aluF2UI
)
849 DECL_TEMPLATE(ALuint
, 7, aluF2UI
)
850 DECL_TEMPLATE(ALuint
, 8, aluF2UI
)
852 DECL_TEMPLATE(ALint
, 1, aluF2I
)
853 DECL_TEMPLATE(ALint
, 2, aluF2I
)
854 DECL_TEMPLATE(ALint
, 4, aluF2I
)
855 DECL_TEMPLATE(ALint
, 6, aluF2I
)
856 DECL_TEMPLATE(ALint
, 7, aluF2I
)
857 DECL_TEMPLATE(ALint
, 8, aluF2I
)
859 DECL_TEMPLATE(ALushort
, 1, aluF2US
)
860 DECL_TEMPLATE(ALushort
, 2, aluF2US
)
861 DECL_TEMPLATE(ALushort
, 4, aluF2US
)
862 DECL_TEMPLATE(ALushort
, 6, aluF2US
)
863 DECL_TEMPLATE(ALushort
, 7, aluF2US
)
864 DECL_TEMPLATE(ALushort
, 8, aluF2US
)
866 DECL_TEMPLATE(ALshort
, 1, aluF2S
)
867 DECL_TEMPLATE(ALshort
, 2, aluF2S
)
868 DECL_TEMPLATE(ALshort
, 4, aluF2S
)
869 DECL_TEMPLATE(ALshort
, 6, aluF2S
)
870 DECL_TEMPLATE(ALshort
, 7, aluF2S
)
871 DECL_TEMPLATE(ALshort
, 8, aluF2S
)
873 DECL_TEMPLATE(ALubyte
, 1, aluF2UB
)
874 DECL_TEMPLATE(ALubyte
, 2, aluF2UB
)
875 DECL_TEMPLATE(ALubyte
, 4, aluF2UB
)
876 DECL_TEMPLATE(ALubyte
, 6, aluF2UB
)
877 DECL_TEMPLATE(ALubyte
, 7, aluF2UB
)
878 DECL_TEMPLATE(ALubyte
, 8, aluF2UB
)
880 DECL_TEMPLATE(ALbyte
, 1, aluF2B
)
881 DECL_TEMPLATE(ALbyte
, 2, aluF2B
)
882 DECL_TEMPLATE(ALbyte
, 4, aluF2B
)
883 DECL_TEMPLATE(ALbyte
, 6, aluF2B
)
884 DECL_TEMPLATE(ALbyte
, 7, aluF2B
)
885 DECL_TEMPLATE(ALbyte
, 8, aluF2B
)
889 #define DECL_TEMPLATE(T) \
890 static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
892 switch(device->FmtChans) \
895 Write_##T##_1(device, buffer, SamplesToDo); \
898 Write_##T##_2(device, buffer, SamplesToDo); \
901 Write_##T##_4(device, buffer, SamplesToDo); \
904 case DevFmtX51Side: \
905 Write_##T##_6(device, buffer, SamplesToDo); \
908 Write_##T##_7(device, buffer, SamplesToDo); \
911 Write_##T##_8(device, buffer, SamplesToDo); \
916 DECL_TEMPLATE(ALfloat
)
917 DECL_TEMPLATE(ALuint
)
919 DECL_TEMPLATE(ALushort
)
920 DECL_TEMPLATE(ALshort
)
921 DECL_TEMPLATE(ALubyte
)
922 DECL_TEMPLATE(ALbyte
)
926 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
929 ALeffectslot
**slot
, **slot_end
;
930 ALsource
**src
, **src_end
;
935 fpuState
= SetMixerFPUMode();
939 SamplesToDo
= minu(size
, BUFFERSIZE
);
940 memset(device
->DryBuffer
, 0, SamplesToDo
*MAXCHANNELS
*sizeof(ALfloat
));
943 ctx
= device
->ContextList
;
946 ALenum DeferUpdates
= ctx
->DeferUpdates
;
947 ALenum UpdateSources
= AL_FALSE
;
950 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
952 /* source processing */
953 src
= ctx
->ActiveSources
;
954 src_end
= src
+ ctx
->ActiveSourceCount
;
955 while(src
!= src_end
)
957 if((*src
)->state
!= AL_PLAYING
)
959 --(ctx
->ActiveSourceCount
);
964 if(!DeferUpdates
&& (ExchangeInt(&(*src
)->NeedsUpdate
, AL_FALSE
) ||
966 ALsource_Update(*src
, ctx
);
968 MixSource(*src
, device
, SamplesToDo
);
972 /* effect slot processing */
973 slot
= ctx
->ActiveEffectSlots
;
974 slot_end
= slot
+ ctx
->ActiveEffectSlotCount
;
975 while(slot
!= slot_end
)
977 for(c
= 0;c
< SamplesToDo
;c
++)
979 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
980 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
982 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
983 (*slot
)->PendingClicks
[0] = 0.0f
;
985 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
986 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
988 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
989 (*slot
)->WetBuffer
, device
->DryBuffer
);
991 for(i
= 0;i
< SamplesToDo
;i
++)
992 (*slot
)->WetBuffer
[i
] = 0.0f
;
1000 slot
= &device
->DefaultSlot
;
1003 for(c
= 0;c
< SamplesToDo
;c
++)
1005 (*slot
)->WetBuffer
[c
] += (*slot
)->ClickRemoval
[0];
1006 (*slot
)->ClickRemoval
[0] -= (*slot
)->ClickRemoval
[0] * (1.0f
/256.0f
);
1008 (*slot
)->ClickRemoval
[0] += (*slot
)->PendingClicks
[0];
1009 (*slot
)->PendingClicks
[0] = 0.0f
;
1011 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1012 ALeffectState_Update((*slot
)->EffectState
, device
, *slot
);
1014 ALeffectState_Process((*slot
)->EffectState
, SamplesToDo
,
1015 (*slot
)->WetBuffer
, device
->DryBuffer
);
1017 for(i
= 0;i
< SamplesToDo
;i
++)
1018 (*slot
)->WetBuffer
[i
] = 0.0f
;
1020 UnlockDevice(device
);
1022 /* Click-removal. Could do better; this only really handles immediate
1023 * changes between updates where a predictive sample could be
1024 * generated. Delays caused by effects and HRTF aren't caught. */
1025 if(device
->FmtChans
== DevFmtMono
)
1027 for(i
= 0;i
< SamplesToDo
;i
++)
1029 device
->DryBuffer
[i
][FRONT_CENTER
] += device
->ClickRemoval
[FRONT_CENTER
];
1030 device
->ClickRemoval
[FRONT_CENTER
] -= device
->ClickRemoval
[FRONT_CENTER
] * (1.0f
/256.0f
);
1032 device
->ClickRemoval
[FRONT_CENTER
] += device
->PendingClicks
[FRONT_CENTER
];
1033 device
->PendingClicks
[FRONT_CENTER
] = 0.0f
;
1035 else if(device
->FmtChans
== DevFmtStereo
)
1037 /* Assumes the first two channels are FRONT_LEFT and FRONT_RIGHT */
1038 for(i
= 0;i
< SamplesToDo
;i
++)
1040 for(c
= 0;c
< 2;c
++)
1042 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1043 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1046 for(c
= 0;c
< 2;c
++)
1048 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1049 device
->PendingClicks
[c
] = 0.0f
;
1053 for(i
= 0;i
< SamplesToDo
;i
++)
1054 bs2b_cross_feed(device
->Bs2b
, &device
->DryBuffer
[i
][0]);
1059 for(i
= 0;i
< SamplesToDo
;i
++)
1061 for(c
= 0;c
< MAXCHANNELS
;c
++)
1063 device
->DryBuffer
[i
][c
] += device
->ClickRemoval
[c
];
1064 device
->ClickRemoval
[c
] -= device
->ClickRemoval
[c
] * (1.0f
/256.0f
);
1067 for(c
= 0;c
< MAXCHANNELS
;c
++)
1069 device
->ClickRemoval
[c
] += device
->PendingClicks
[c
];
1070 device
->PendingClicks
[c
] = 0.0f
;
1076 switch(device
->FmtType
)
1079 Write_ALbyte(device
, buffer
, SamplesToDo
);
1082 Write_ALubyte(device
, buffer
, SamplesToDo
);
1085 Write_ALshort(device
, buffer
, SamplesToDo
);
1088 Write_ALushort(device
, buffer
, SamplesToDo
);
1091 Write_ALint(device
, buffer
, SamplesToDo
);
1094 Write_ALuint(device
, buffer
, SamplesToDo
);
1097 Write_ALfloat(device
, buffer
, SamplesToDo
);
1102 size
-= SamplesToDo
;
1105 RestoreFPUMode(fpuState
);
1109 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1111 ALCcontext
*Context
;
1114 device
->Connected
= ALC_FALSE
;
1116 Context
= device
->ContextList
;
1119 ALsource
**src
, **src_end
;
1121 src
= Context
->ActiveSources
;
1122 src_end
= src
+ Context
->ActiveSourceCount
;
1123 while(src
!= src_end
)
1125 if((*src
)->state
== AL_PLAYING
)
1127 (*src
)->state
= AL_STOPPED
;
1128 (*src
)->BuffersPlayed
= (*src
)->BuffersInQueue
;
1129 (*src
)->position
= 0;
1130 (*src
)->position_fraction
= 0;
1134 Context
->ActiveSourceCount
= 0;
1136 Context
= Context
->next
;
1138 UnlockDevice(device
);