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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
32 #include "alListener.h"
33 #include "alAuxEffectSlot.h"
37 #include "static_assert.h"
39 #include "midi/base.h"
42 static_assert((INT_MAX
>>FRACTIONBITS
)/MAX_PITCH
> BUFFERSIZE
,
43 "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
51 ALfloat ConeScale
= 1.0f
;
53 /* Localized Z scalar for mono sources */
54 ALfloat ZScale
= 1.0f
;
56 extern inline ALfloat
minf(ALfloat a
, ALfloat b
);
57 extern inline ALfloat
maxf(ALfloat a
, ALfloat b
);
58 extern inline ALfloat
clampf(ALfloat val
, ALfloat min
, ALfloat max
);
60 extern inline ALdouble
mind(ALdouble a
, ALdouble b
);
61 extern inline ALdouble
maxd(ALdouble a
, ALdouble b
);
62 extern inline ALdouble
clampd(ALdouble val
, ALdouble min
, ALdouble max
);
64 extern inline ALuint
minu(ALuint a
, ALuint b
);
65 extern inline ALuint
maxu(ALuint a
, ALuint b
);
66 extern inline ALuint
clampu(ALuint val
, ALuint min
, ALuint max
);
68 extern inline ALint
mini(ALint a
, ALint b
);
69 extern inline ALint
maxi(ALint a
, ALint b
);
70 extern inline ALint
clampi(ALint val
, ALint min
, ALint max
);
72 extern inline ALint64
mini64(ALint64 a
, ALint64 b
);
73 extern inline ALint64
maxi64(ALint64 a
, ALint64 b
);
74 extern inline ALint64
clampi64(ALint64 val
, ALint64 min
, ALint64 max
);
76 extern inline ALuint64
minu64(ALuint64 a
, ALuint64 b
);
77 extern inline ALuint64
maxu64(ALuint64 a
, ALuint64 b
);
78 extern inline ALuint64
clampu64(ALuint64 val
, ALuint64 min
, ALuint64 max
);
80 extern inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALfloat mu
);
81 extern inline ALfloat
cubic(ALfloat val0
, ALfloat val1
, ALfloat val2
, ALfloat val3
, ALfloat mu
);
84 static inline void aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
86 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
87 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
88 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
91 static inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
93 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
94 inVector1
[2]*inVector2
[2];
97 static inline void aluNormalize(ALfloat
*inVector
)
99 ALfloat lengthsqr
= aluDotproduct(inVector
, inVector
);
102 ALfloat inv_length
= 1.0f
/sqrtf(lengthsqr
);
103 inVector
[0] *= inv_length
;
104 inVector
[1] *= inv_length
;
105 inVector
[2] *= inv_length
;
109 static inline ALvoid
aluMatrixVector(ALfloat
*vector
, ALfloat w
, ALfloat (*restrict matrix
)[4])
112 vector
[0], vector
[1], vector
[2], w
115 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
116 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
117 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
121 static ALvoid
CalcListenerParams(ALlistener
*Listener
)
123 ALfloat N
[3], V
[3], U
[3], P
[3];
126 N
[0] = Listener
->Forward
[0];
127 N
[1] = Listener
->Forward
[1];
128 N
[2] = Listener
->Forward
[2];
130 V
[0] = Listener
->Up
[0];
131 V
[1] = Listener
->Up
[1];
132 V
[2] = Listener
->Up
[2];
134 /* Build and normalize right-vector */
135 aluCrossproduct(N
, V
, U
);
138 Listener
->Params
.Matrix
[0][0] = U
[0];
139 Listener
->Params
.Matrix
[0][1] = V
[0];
140 Listener
->Params
.Matrix
[0][2] = -N
[0];
141 Listener
->Params
.Matrix
[0][3] = 0.0f
;
142 Listener
->Params
.Matrix
[1][0] = U
[1];
143 Listener
->Params
.Matrix
[1][1] = V
[1];
144 Listener
->Params
.Matrix
[1][2] = -N
[1];
145 Listener
->Params
.Matrix
[1][3] = 0.0f
;
146 Listener
->Params
.Matrix
[2][0] = U
[2];
147 Listener
->Params
.Matrix
[2][1] = V
[2];
148 Listener
->Params
.Matrix
[2][2] = -N
[2];
149 Listener
->Params
.Matrix
[2][3] = 0.0f
;
150 Listener
->Params
.Matrix
[3][0] = 0.0f
;
151 Listener
->Params
.Matrix
[3][1] = 0.0f
;
152 Listener
->Params
.Matrix
[3][2] = 0.0f
;
153 Listener
->Params
.Matrix
[3][3] = 1.0f
;
155 P
[0] = Listener
->Position
[0];
156 P
[1] = Listener
->Position
[1];
157 P
[2] = Listener
->Position
[2];
158 aluMatrixVector(P
, 1.0f
, Listener
->Params
.Matrix
);
159 Listener
->Params
.Matrix
[3][0] = -P
[0];
160 Listener
->Params
.Matrix
[3][1] = -P
[1];
161 Listener
->Params
.Matrix
[3][2] = -P
[2];
163 Listener
->Params
.Velocity
[0] = Listener
->Velocity
[0];
164 Listener
->Params
.Velocity
[1] = Listener
->Velocity
[1];
165 Listener
->Params
.Velocity
[2] = Listener
->Velocity
[2];
166 aluMatrixVector(Listener
->Params
.Velocity
, 0.0f
, Listener
->Params
.Matrix
);
169 ALvoid
CalcNonAttnSourceParams(ALvoice
*voice
, const ALsource
*ALSource
, const ALCcontext
*ALContext
)
171 static const struct ChanMap MonoMap
[1] = { { FrontCenter
, 0.0f
} };
172 static const struct ChanMap StereoMap
[2] = {
173 { FrontLeft
, DEG2RAD(-30.0f
) },
174 { FrontRight
, DEG2RAD( 30.0f
) }
176 static const struct ChanMap StereoWideMap
[2] = {
177 { FrontLeft
, DEG2RAD(-90.0f
) },
178 { FrontRight
, DEG2RAD( 90.0f
) }
180 static const struct ChanMap RearMap
[2] = {
181 { BackLeft
, DEG2RAD(-150.0f
) },
182 { BackRight
, DEG2RAD( 150.0f
) }
184 static const struct ChanMap QuadMap
[4] = {
185 { FrontLeft
, DEG2RAD( -45.0f
) },
186 { FrontRight
, DEG2RAD( 45.0f
) },
187 { BackLeft
, DEG2RAD(-135.0f
) },
188 { BackRight
, DEG2RAD( 135.0f
) }
190 static const struct ChanMap X51Map
[6] = {
191 { FrontLeft
, DEG2RAD( -30.0f
) },
192 { FrontRight
, DEG2RAD( 30.0f
) },
193 { FrontCenter
, DEG2RAD( 0.0f
) },
195 { BackLeft
, DEG2RAD(-110.0f
) },
196 { BackRight
, DEG2RAD( 110.0f
) }
198 static const struct ChanMap X61Map
[7] = {
199 { FrontLeft
, DEG2RAD(-30.0f
) },
200 { FrontRight
, DEG2RAD( 30.0f
) },
201 { FrontCenter
, DEG2RAD( 0.0f
) },
203 { BackCenter
, DEG2RAD(180.0f
) },
204 { SideLeft
, DEG2RAD(-90.0f
) },
205 { SideRight
, DEG2RAD( 90.0f
) }
207 static const struct ChanMap X71Map
[8] = {
208 { FrontLeft
, DEG2RAD( -30.0f
) },
209 { FrontRight
, DEG2RAD( 30.0f
) },
210 { FrontCenter
, DEG2RAD( 0.0f
) },
212 { BackLeft
, DEG2RAD(-150.0f
) },
213 { BackRight
, DEG2RAD( 150.0f
) },
214 { SideLeft
, DEG2RAD( -90.0f
) },
215 { SideRight
, DEG2RAD( 90.0f
) }
218 ALCdevice
*Device
= ALContext
->Device
;
219 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
220 ALbufferlistitem
*BufferListItem
;
221 enum FmtChannels Channels
;
222 ALfloat DryGain
, DryGainHF
, DryGainLF
;
223 ALfloat WetGain
[MAX_SENDS
];
224 ALfloat WetGainHF
[MAX_SENDS
];
225 ALfloat WetGainLF
[MAX_SENDS
];
226 ALint NumSends
, Frequency
;
227 const struct ChanMap
*chans
= NULL
;
228 ALint num_channels
= 0;
229 ALboolean DirectChannels
;
230 ALfloat hwidth
= 0.0f
;
234 /* Get device properties */
235 NumSends
= Device
->NumAuxSends
;
236 Frequency
= Device
->Frequency
;
238 /* Get listener properties */
239 ListenerGain
= ALContext
->Listener
->Gain
;
241 /* Get source properties */
242 SourceVolume
= ALSource
->Gain
;
243 MinVolume
= ALSource
->MinGain
;
244 MaxVolume
= ALSource
->MaxGain
;
245 Pitch
= ALSource
->Pitch
;
246 DirectChannels
= ALSource
->DirectChannels
;
248 voice
->Direct
.OutBuffer
= Device
->DryBuffer
;
249 for(i
= 0;i
< NumSends
;i
++)
251 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
253 Slot
= Device
->DefaultSlot
;
254 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
255 voice
->Send
[i
].OutBuffer
= NULL
;
257 voice
->Send
[i
].OutBuffer
= Slot
->WetBuffer
;
260 /* Calculate the stepping value */
262 BufferListItem
= ATOMIC_LOAD(&ALSource
->queue
);
263 while(BufferListItem
!= NULL
)
266 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
268 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
269 if(Pitch
> (ALfloat
)MAX_PITCH
)
270 voice
->Step
= MAX_PITCH
<<FRACTIONBITS
;
273 voice
->Step
= fastf2i(Pitch
*FRACTIONONE
);
278 Channels
= ALBuffer
->FmtChannels
;
281 BufferListItem
= BufferListItem
->next
;
284 /* Calculate gains */
285 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
286 DryGain
*= ALSource
->Direct
.Gain
* ListenerGain
;
287 DryGainHF
= ALSource
->Direct
.GainHF
;
288 DryGainLF
= ALSource
->Direct
.GainLF
;
289 for(i
= 0;i
< NumSends
;i
++)
291 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
292 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
293 WetGainHF
[i
] = ALSource
->Send
[i
].GainHF
;
294 WetGainLF
[i
] = ALSource
->Send
[i
].GainLF
;
305 if(!(Device
->Flags
&DEVICE_WIDE_STEREO
))
307 /* HACK: Place the stereo channels at +/-90 degrees when using non-
308 * HRTF stereo output. This helps reduce the "monoization" caused
309 * by them panning towards the center. */
310 if(Device
->FmtChans
== DevFmtStereo
&& !Device
->Hrtf
)
311 chans
= StereoWideMap
;
317 chans
= StereoWideMap
;
318 hwidth
= DEG2RAD(60.0f
);
349 if(DirectChannels
!= AL_FALSE
)
351 for(c
= 0;c
< num_channels
;c
++)
353 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[c
];
354 for(j
= 0;j
< MaxChannels
;j
++)
355 gains
[j
].Target
= 0.0f
;
358 for(c
= 0;c
< num_channels
;c
++)
360 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[c
];
361 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
363 enum Channel chan
= Device
->Speaker2Chan
[i
];
364 if(chan
== chans
[c
].channel
)
366 gains
[chan
].Target
= DryGain
;
372 if(!voice
->Direct
.Moving
)
374 for(i
= 0;i
< num_channels
;i
++)
376 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[i
];
377 for(j
= 0;j
< MaxChannels
;j
++)
379 gains
[j
].Current
= gains
[j
].Target
;
380 gains
[j
].Step
= 1.0f
;
383 voice
->Direct
.Counter
= 0;
384 voice
->Direct
.Moving
= AL_TRUE
;
388 for(i
= 0;i
< num_channels
;i
++)
390 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[i
];
391 for(j
= 0;j
< MaxChannels
;j
++)
393 ALfloat cur
= maxf(gains
[j
].Current
, FLT_EPSILON
);
394 ALfloat trg
= maxf(gains
[j
].Target
, FLT_EPSILON
);
395 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
396 gains
[j
].Step
= powf(trg
/cur
, 1.0f
/64.0f
);
398 gains
[j
].Step
= 1.0f
;
399 gains
[j
].Current
= cur
;
402 voice
->Direct
.Counter
= 64;
405 voice
->IsHrtf
= AL_FALSE
;
407 else if(Device
->Hrtf
)
409 for(c
= 0;c
< num_channels
;c
++)
411 if(chans
[c
].channel
== LFE
)
414 voice
->Direct
.Mix
.Hrtf
.Params
[c
].Delay
[0] = 0;
415 voice
->Direct
.Mix
.Hrtf
.Params
[c
].Delay
[1] = 0;
416 for(i
= 0;i
< HRIR_LENGTH
;i
++)
418 voice
->Direct
.Mix
.Hrtf
.Params
[c
].Coeffs
[i
][0] = 0.0f
;
419 voice
->Direct
.Mix
.Hrtf
.Params
[c
].Coeffs
[i
][1] = 0.0f
;
424 /* Get the static HRIR coefficients and delays for this
426 GetLerpedHrtfCoeffs(Device
->Hrtf
,
427 0.0f
, chans
[c
].angle
, 1.0f
, DryGain
,
428 voice
->Direct
.Mix
.Hrtf
.Params
[c
].Coeffs
,
429 voice
->Direct
.Mix
.Hrtf
.Params
[c
].Delay
);
432 voice
->Direct
.Counter
= 0;
433 voice
->Direct
.Moving
= AL_TRUE
;
434 voice
->Direct
.Mix
.Hrtf
.IrSize
= GetHrtfIrSize(Device
->Hrtf
);
436 voice
->IsHrtf
= AL_TRUE
;
440 for(i
= 0;i
< num_channels
;i
++)
442 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[i
];
443 for(j
= 0;j
< MaxChannels
;j
++)
444 gains
[j
].Target
= 0.0f
;
447 DryGain
*= lerp(1.0f
, 1.0f
/sqrtf((float)Device
->NumChan
), hwidth
/F_PI
);
448 for(c
= 0;c
< num_channels
;c
++)
450 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[c
];
451 ALfloat Target
[MaxChannels
];
453 /* Special-case LFE */
454 if(chans
[c
].channel
== LFE
)
456 gains
[chans
[c
].channel
].Target
= DryGain
;
459 ComputeAngleGains(Device
, chans
[c
].angle
, hwidth
, DryGain
, Target
);
460 for(i
= 0;i
< MaxChannels
;i
++)
461 gains
[i
].Target
= Target
[i
];
464 if(!voice
->Direct
.Moving
)
466 for(i
= 0;i
< num_channels
;i
++)
468 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[i
];
469 for(j
= 0;j
< MaxChannels
;j
++)
471 gains
[j
].Current
= gains
[j
].Target
;
472 gains
[j
].Step
= 1.0f
;
475 voice
->Direct
.Counter
= 0;
476 voice
->Direct
.Moving
= AL_TRUE
;
480 for(i
= 0;i
< num_channels
;i
++)
482 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[i
];
483 for(j
= 0;j
< MaxChannels
;j
++)
485 ALfloat trg
= maxf(gains
[j
].Target
, FLT_EPSILON
);
486 ALfloat cur
= maxf(gains
[j
].Current
, FLT_EPSILON
);
487 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
488 gains
[j
].Step
= powf(trg
/cur
, 1.0f
/64.0f
);
490 gains
[j
].Step
= 1.0f
;
491 gains
[j
].Current
= cur
;
494 voice
->Direct
.Counter
= 64;
497 voice
->IsHrtf
= AL_FALSE
;
499 for(i
= 0;i
< NumSends
;i
++)
501 voice
->Send
[i
].Gain
.Target
= WetGain
[i
];
502 if(!voice
->Send
[i
].Moving
)
504 voice
->Send
[i
].Gain
.Current
= voice
->Send
[i
].Gain
.Target
;
505 voice
->Send
[i
].Gain
.Step
= 1.0f
;
506 voice
->Send
[i
].Counter
= 0;
507 voice
->Send
[i
].Moving
= AL_TRUE
;
511 ALfloat cur
= maxf(voice
->Send
[i
].Gain
.Current
, FLT_EPSILON
);
512 ALfloat trg
= maxf(voice
->Send
[i
].Gain
.Target
, FLT_EPSILON
);
513 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
514 voice
->Send
[i
].Gain
.Step
= powf(trg
/cur
, 1.0f
/64.0f
);
516 voice
->Send
[i
].Gain
.Step
= 1.0f
;
517 voice
->Send
[i
].Gain
.Current
= cur
;
518 voice
->Send
[i
].Counter
= 64;
523 ALfloat gainhf
= maxf(0.01f
, DryGainHF
);
524 ALfloat gainlf
= maxf(0.01f
, DryGainLF
);
525 ALfloat hfscale
= ALSource
->Direct
.HFReference
/ Frequency
;
526 ALfloat lfscale
= ALSource
->Direct
.LFReference
/ Frequency
;
527 for(c
= 0;c
< num_channels
;c
++)
529 voice
->Direct
.Filters
[c
].ActiveType
= AF_None
;
530 if(gainhf
!= 1.0f
) voice
->Direct
.Filters
[c
].ActiveType
|= AF_LowPass
;
531 if(gainlf
!= 1.0f
) voice
->Direct
.Filters
[c
].ActiveType
|= AF_HighPass
;
532 ALfilterState_setParams(
533 &voice
->Direct
.Filters
[c
].LowPass
, ALfilterType_HighShelf
, gainhf
,
536 ALfilterState_setParams(
537 &voice
->Direct
.Filters
[c
].HighPass
, ALfilterType_LowShelf
, gainlf
,
542 for(i
= 0;i
< NumSends
;i
++)
544 ALfloat gainhf
= maxf(0.01f
, WetGainHF
[i
]);
545 ALfloat gainlf
= maxf(0.01f
, WetGainLF
[i
]);
546 ALfloat hfscale
= ALSource
->Send
[i
].HFReference
/ Frequency
;
547 ALfloat lfscale
= ALSource
->Send
[i
].LFReference
/ Frequency
;
548 for(c
= 0;c
< num_channels
;c
++)
550 voice
->Send
[i
].Filters
[c
].ActiveType
= AF_None
;
551 if(gainhf
!= 1.0f
) voice
->Send
[i
].Filters
[c
].ActiveType
|= AF_LowPass
;
552 if(gainlf
!= 1.0f
) voice
->Send
[i
].Filters
[c
].ActiveType
|= AF_HighPass
;
553 ALfilterState_setParams(
554 &voice
->Send
[i
].Filters
[c
].LowPass
, ALfilterType_HighShelf
, gainhf
,
557 ALfilterState_setParams(
558 &voice
->Send
[i
].Filters
[c
].HighPass
, ALfilterType_LowShelf
, gainlf
,
565 ALvoid
CalcSourceParams(ALvoice
*voice
, const ALsource
*ALSource
, const ALCcontext
*ALContext
)
567 ALCdevice
*Device
= ALContext
->Device
;
568 ALfloat Velocity
[3],Direction
[3],Position
[3],SourceToListener
[3];
569 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
570 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
571 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
572 ALfloat DopplerFactor
, SpeedOfSound
;
573 ALfloat AirAbsorptionFactor
;
574 ALfloat RoomAirAbsorption
[MAX_SENDS
];
575 ALbufferlistitem
*BufferListItem
;
577 ALfloat RoomAttenuation
[MAX_SENDS
];
578 ALfloat MetersPerUnit
;
579 ALfloat RoomRolloffBase
;
580 ALfloat RoomRolloff
[MAX_SENDS
];
581 ALfloat DecayDistance
[MAX_SENDS
];
585 ALboolean DryGainHFAuto
;
586 ALfloat WetGain
[MAX_SENDS
];
587 ALfloat WetGainHF
[MAX_SENDS
];
588 ALfloat WetGainLF
[MAX_SENDS
];
589 ALboolean WetGainAuto
;
590 ALboolean WetGainHFAuto
;
598 for(i
= 0;i
< MAX_SENDS
;i
++)
604 /* Get context/device properties */
605 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
606 SpeedOfSound
= ALContext
->SpeedOfSound
* ALContext
->DopplerVelocity
;
607 NumSends
= Device
->NumAuxSends
;
608 Frequency
= Device
->Frequency
;
610 /* Get listener properties */
611 ListenerGain
= ALContext
->Listener
->Gain
;
612 MetersPerUnit
= ALContext
->Listener
->MetersPerUnit
;
614 /* Get source properties */
615 SourceVolume
= ALSource
->Gain
;
616 MinVolume
= ALSource
->MinGain
;
617 MaxVolume
= ALSource
->MaxGain
;
618 Pitch
= ALSource
->Pitch
;
619 Position
[0] = ALSource
->Position
[0];
620 Position
[1] = ALSource
->Position
[1];
621 Position
[2] = ALSource
->Position
[2];
622 Direction
[0] = ALSource
->Orientation
[0];
623 Direction
[1] = ALSource
->Orientation
[1];
624 Direction
[2] = ALSource
->Orientation
[2];
625 Velocity
[0] = ALSource
->Velocity
[0];
626 Velocity
[1] = ALSource
->Velocity
[1];
627 Velocity
[2] = ALSource
->Velocity
[2];
628 MinDist
= ALSource
->RefDistance
;
629 MaxDist
= ALSource
->MaxDistance
;
630 Rolloff
= ALSource
->RollOffFactor
;
631 InnerAngle
= ALSource
->InnerAngle
;
632 OuterAngle
= ALSource
->OuterAngle
;
633 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
634 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
635 WetGainAuto
= ALSource
->WetGainAuto
;
636 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
637 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
639 voice
->Direct
.OutBuffer
= Device
->DryBuffer
;
640 for(i
= 0;i
< NumSends
;i
++)
642 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
645 Slot
= Device
->DefaultSlot
;
646 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
649 RoomRolloff
[i
] = 0.0f
;
650 DecayDistance
[i
] = 0.0f
;
651 RoomAirAbsorption
[i
] = 1.0f
;
653 else if(Slot
->AuxSendAuto
)
655 RoomRolloff
[i
] = RoomRolloffBase
;
656 if(IsReverbEffect(Slot
->EffectType
))
658 RoomRolloff
[i
] += Slot
->EffectProps
.Reverb
.RoomRolloffFactor
;
659 DecayDistance
[i
] = Slot
->EffectProps
.Reverb
.DecayTime
*
660 SPEEDOFSOUNDMETRESPERSEC
;
661 RoomAirAbsorption
[i
] = Slot
->EffectProps
.Reverb
.AirAbsorptionGainHF
;
665 DecayDistance
[i
] = 0.0f
;
666 RoomAirAbsorption
[i
] = 1.0f
;
671 /* If the slot's auxiliary send auto is off, the data sent to the
672 * effect slot is the same as the dry path, sans filter effects */
673 RoomRolloff
[i
] = Rolloff
;
674 DecayDistance
[i
] = 0.0f
;
675 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
678 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
679 voice
->Send
[i
].OutBuffer
= NULL
;
681 voice
->Send
[i
].OutBuffer
= Slot
->WetBuffer
;
684 /* Transform source to listener space (convert to head relative) */
685 if(ALSource
->HeadRelative
== AL_FALSE
)
687 ALfloat (*restrict Matrix
)[4] = ALContext
->Listener
->Params
.Matrix
;
688 /* Transform source vectors */
689 aluMatrixVector(Position
, 1.0f
, Matrix
);
690 aluMatrixVector(Direction
, 0.0f
, Matrix
);
691 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
695 const ALfloat
*ListenerVel
= ALContext
->Listener
->Params
.Velocity
;
696 /* Offset the source velocity to be relative of the listener velocity */
697 Velocity
[0] += ListenerVel
[0];
698 Velocity
[1] += ListenerVel
[1];
699 Velocity
[2] += ListenerVel
[2];
702 SourceToListener
[0] = -Position
[0];
703 SourceToListener
[1] = -Position
[1];
704 SourceToListener
[2] = -Position
[2];
705 aluNormalize(SourceToListener
);
706 aluNormalize(Direction
);
708 /* Calculate distance attenuation */
709 Distance
= sqrtf(aluDotproduct(Position
, Position
));
710 ClampedDist
= Distance
;
713 for(i
= 0;i
< NumSends
;i
++)
714 RoomAttenuation
[i
] = 1.0f
;
715 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
716 ALContext
->DistanceModel
)
718 case InverseDistanceClamped
:
719 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
720 if(MaxDist
< MinDist
)
723 case InverseDistance
:
726 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
727 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
728 for(i
= 0;i
< NumSends
;i
++)
730 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
731 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
736 case LinearDistanceClamped
:
737 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
738 if(MaxDist
< MinDist
)
742 if(MaxDist
!= MinDist
)
744 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
745 Attenuation
= maxf(Attenuation
, 0.0f
);
746 for(i
= 0;i
< NumSends
;i
++)
748 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
749 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
754 case ExponentDistanceClamped
:
755 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
756 if(MaxDist
< MinDist
)
759 case ExponentDistance
:
760 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
762 Attenuation
= powf(ClampedDist
/MinDist
, -Rolloff
);
763 for(i
= 0;i
< NumSends
;i
++)
764 RoomAttenuation
[i
] = powf(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
768 case DisableDistance
:
769 ClampedDist
= MinDist
;
773 /* Source Gain + Attenuation */
774 DryGain
= SourceVolume
* Attenuation
;
775 for(i
= 0;i
< NumSends
;i
++)
776 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
778 /* Distance-based air absorption */
779 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
781 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
782 DryGainHF
*= powf(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
783 for(i
= 0;i
< NumSends
;i
++)
784 WetGainHF
[i
] *= powf(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
789 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
791 /* Apply a decay-time transformation to the wet path, based on the
792 * attenuation of the dry path.
794 * Using the apparent distance, based on the distance attenuation, the
795 * initial decay of the reverb effect is calculated and applied to the
798 for(i
= 0;i
< NumSends
;i
++)
800 if(DecayDistance
[i
] > 0.0f
)
801 WetGain
[i
] *= powf(0.001f
/*-60dB*/, ApparentDist
/DecayDistance
[i
]);
805 /* Calculate directional soundcones */
806 Angle
= RAD2DEG(acosf(aluDotproduct(Direction
,SourceToListener
)) * ConeScale
) * 2.0f
;
807 if(Angle
> InnerAngle
&& Angle
<= OuterAngle
)
809 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
810 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
811 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
813 else if(Angle
> OuterAngle
)
815 ConeVolume
= ALSource
->OuterGain
;
816 ConeHF
= ALSource
->OuterGainHF
;
824 DryGain
*= ConeVolume
;
827 for(i
= 0;i
< NumSends
;i
++)
828 WetGain
[i
] *= ConeVolume
;
834 for(i
= 0;i
< NumSends
;i
++)
835 WetGainHF
[i
] *= ConeHF
;
838 /* Clamp to Min/Max Gain */
839 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
840 for(i
= 0;i
< NumSends
;i
++)
841 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
843 /* Apply gain and frequency filters */
844 DryGain
*= ALSource
->Direct
.Gain
* ListenerGain
;
845 DryGainHF
*= ALSource
->Direct
.GainHF
;
846 DryGainLF
*= ALSource
->Direct
.GainLF
;
847 for(i
= 0;i
< NumSends
;i
++)
849 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
850 WetGainHF
[i
] *= ALSource
->Send
[i
].GainHF
;
851 WetGainLF
[i
] *= ALSource
->Send
[i
].GainLF
;
854 /* Calculate velocity-based doppler effect */
855 if(DopplerFactor
> 0.0f
)
857 const ALfloat
*ListenerVel
= ALContext
->Listener
->Params
.Velocity
;
860 if(SpeedOfSound
< 1.0f
)
862 DopplerFactor
*= 1.0f
/SpeedOfSound
;
866 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
867 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
869 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
870 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
873 BufferListItem
= ATOMIC_LOAD(&ALSource
->queue
);
874 while(BufferListItem
!= NULL
)
877 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
879 /* Calculate fixed-point stepping value, based on the pitch, buffer
880 * frequency, and output frequency. */
881 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
882 if(Pitch
> (ALfloat
)MAX_PITCH
)
883 voice
->Step
= MAX_PITCH
<<FRACTIONBITS
;
886 voice
->Step
= fastf2i(Pitch
*FRACTIONONE
);
893 BufferListItem
= BufferListItem
->next
;
898 /* Use a binaural HRTF algorithm for stereo headphone playback */
899 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
900 ALfloat radius
= ALSource
->Radius
;
901 ALfloat dirfact
= 1.0f
;
903 if(Distance
> FLT_EPSILON
)
905 ALfloat invlen
= 1.0f
/Distance
;
906 Position
[0] *= invlen
;
907 Position
[1] *= invlen
;
908 Position
[2] *= invlen
;
910 /* Calculate elevation and azimuth only when the source is not at
911 * the listener. This prevents +0 and -0 Z from producing
912 * inconsistent panning. Also, clamp Y in case FP precision errors
913 * cause it to land outside of -1..+1. */
914 ev
= asinf(clampf(Position
[1], -1.0f
, 1.0f
));
915 az
= atan2f(Position
[0], -Position
[2]*ZScale
);
917 if(radius
> Distance
)
918 dirfact
*= Distance
/ radius
;
920 /* Check to see if the HRIR is already moving. */
921 if(voice
->Direct
.Moving
)
923 /* Calculate the normalized HRTF transition factor (delta). */
924 delta
= CalcHrtfDelta(voice
->Direct
.Mix
.Hrtf
.Gain
, DryGain
,
925 voice
->Direct
.Mix
.Hrtf
.Dir
, Position
);
926 /* If the delta is large enough, get the moving HRIR target
927 * coefficients, target delays, steppping values, and counter. */
930 ALuint counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
931 ev
, az
, dirfact
, DryGain
, delta
, voice
->Direct
.Counter
,
932 voice
->Direct
.Mix
.Hrtf
.Params
[0].Coeffs
, voice
->Direct
.Mix
.Hrtf
.Params
[0].Delay
,
933 voice
->Direct
.Mix
.Hrtf
.Params
[0].CoeffStep
, voice
->Direct
.Mix
.Hrtf
.Params
[0].DelayStep
935 voice
->Direct
.Counter
= counter
;
936 voice
->Direct
.Mix
.Hrtf
.Gain
= DryGain
;
937 voice
->Direct
.Mix
.Hrtf
.Dir
[0] = Position
[0];
938 voice
->Direct
.Mix
.Hrtf
.Dir
[1] = Position
[1];
939 voice
->Direct
.Mix
.Hrtf
.Dir
[2] = Position
[2];
944 /* Get the initial (static) HRIR coefficients and delays. */
945 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, dirfact
, DryGain
,
946 voice
->Direct
.Mix
.Hrtf
.Params
[0].Coeffs
,
947 voice
->Direct
.Mix
.Hrtf
.Params
[0].Delay
);
948 voice
->Direct
.Counter
= 0;
949 voice
->Direct
.Moving
= AL_TRUE
;
950 voice
->Direct
.Mix
.Hrtf
.Gain
= DryGain
;
951 voice
->Direct
.Mix
.Hrtf
.Dir
[0] = Position
[0];
952 voice
->Direct
.Mix
.Hrtf
.Dir
[1] = Position
[1];
953 voice
->Direct
.Mix
.Hrtf
.Dir
[2] = Position
[2];
955 voice
->Direct
.Mix
.Hrtf
.IrSize
= GetHrtfIrSize(Device
->Hrtf
);
957 voice
->IsHrtf
= AL_TRUE
;
961 MixGains
*gains
= voice
->Direct
.Mix
.Gains
[0];
962 ALfloat DirGain
= 0.0f
;
965 for(j
= 0;j
< MaxChannels
;j
++)
966 gains
[j
].Target
= 0.0f
;
968 /* Normalize the length, and compute panned gains. */
969 if(Distance
> FLT_EPSILON
)
971 ALfloat radius
= ALSource
->Radius
;
972 ALfloat Target
[MaxChannels
];
973 ALfloat invlen
= 1.0f
/maxf(Distance
, radius
);
974 Position
[0] *= invlen
;
975 Position
[1] *= invlen
;
976 Position
[2] *= invlen
;
978 DirGain
= sqrtf(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
979 ComputeAngleGains(Device
, atan2f(Position
[0], -Position
[2]*ZScale
), 0.0f
,
980 DryGain
*DirGain
, Target
);
981 for(j
= 0;j
< MaxChannels
;j
++)
982 gains
[j
].Target
= Target
[j
];
985 /* Adjustment for vertical offsets. Not the greatest, but simple
987 AmbientGain
= DryGain
* sqrtf(1.0f
/Device
->NumChan
) * (1.0f
-DirGain
);
988 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
990 enum Channel chan
= Device
->Speaker2Chan
[i
];
991 gains
[chan
].Target
= maxf(gains
[chan
].Target
, AmbientGain
);
994 if(!voice
->Direct
.Moving
)
996 for(j
= 0;j
< MaxChannels
;j
++)
998 gains
[j
].Current
= gains
[j
].Target
;
999 gains
[j
].Step
= 1.0f
;
1001 voice
->Direct
.Counter
= 0;
1002 voice
->Direct
.Moving
= AL_TRUE
;
1006 for(j
= 0;j
< MaxChannels
;j
++)
1008 ALfloat cur
= maxf(gains
[j
].Current
, FLT_EPSILON
);
1009 ALfloat trg
= maxf(gains
[j
].Target
, FLT_EPSILON
);
1010 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
1011 gains
[j
].Step
= powf(trg
/cur
, 1.0f
/64.0f
);
1013 gains
[j
].Step
= 1.0f
;
1014 gains
[j
].Current
= cur
;
1016 voice
->Direct
.Counter
= 64;
1019 voice
->IsHrtf
= AL_FALSE
;
1021 for(i
= 0;i
< NumSends
;i
++)
1023 voice
->Send
[i
].Gain
.Target
= WetGain
[i
];
1024 if(!voice
->Send
[i
].Moving
)
1026 voice
->Send
[i
].Gain
.Current
= voice
->Send
[i
].Gain
.Target
;
1027 voice
->Send
[i
].Gain
.Step
= 1.0f
;
1028 voice
->Send
[i
].Counter
= 0;
1029 voice
->Send
[i
].Moving
= AL_TRUE
;
1033 ALfloat cur
= maxf(voice
->Send
[i
].Gain
.Current
, FLT_EPSILON
);
1034 ALfloat trg
= maxf(voice
->Send
[i
].Gain
.Target
, FLT_EPSILON
);
1035 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
1036 voice
->Send
[i
].Gain
.Step
= powf(trg
/cur
, 1.0f
/64.0f
);
1038 voice
->Send
[i
].Gain
.Step
= 1.0f
;
1039 voice
->Send
[i
].Gain
.Current
= cur
;
1040 voice
->Send
[i
].Counter
= 64;
1045 ALfloat gainhf
= maxf(0.01f
, DryGainHF
);
1046 ALfloat gainlf
= maxf(0.01f
, DryGainLF
);
1047 ALfloat hfscale
= ALSource
->Direct
.HFReference
/ Frequency
;
1048 ALfloat lfscale
= ALSource
->Direct
.LFReference
/ Frequency
;
1049 voice
->Direct
.Filters
[0].ActiveType
= AF_None
;
1050 if(gainhf
!= 1.0f
) voice
->Direct
.Filters
[0].ActiveType
|= AF_LowPass
;
1051 if(gainlf
!= 1.0f
) voice
->Direct
.Filters
[0].ActiveType
|= AF_HighPass
;
1052 ALfilterState_setParams(
1053 &voice
->Direct
.Filters
[0].LowPass
, ALfilterType_HighShelf
, gainhf
,
1056 ALfilterState_setParams(
1057 &voice
->Direct
.Filters
[0].HighPass
, ALfilterType_LowShelf
, gainlf
,
1061 for(i
= 0;i
< NumSends
;i
++)
1063 ALfloat gainhf
= maxf(0.01f
, WetGainHF
[i
]);
1064 ALfloat gainlf
= maxf(0.01f
, WetGainLF
[i
]);
1065 ALfloat hfscale
= ALSource
->Send
[i
].HFReference
/ Frequency
;
1066 ALfloat lfscale
= ALSource
->Send
[i
].LFReference
/ Frequency
;
1067 voice
->Send
[i
].Filters
[0].ActiveType
= AF_None
;
1068 if(gainhf
!= 1.0f
) voice
->Send
[i
].Filters
[0].ActiveType
|= AF_LowPass
;
1069 if(gainlf
!= 1.0f
) voice
->Send
[i
].Filters
[0].ActiveType
|= AF_HighPass
;
1070 ALfilterState_setParams(
1071 &voice
->Send
[i
].Filters
[0].LowPass
, ALfilterType_HighShelf
, gainhf
,
1074 ALfilterState_setParams(
1075 &voice
->Send
[i
].Filters
[0].HighPass
, ALfilterType_LowShelf
, gainlf
,
1082 static inline ALint
aluF2I25(ALfloat val
)
1084 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1085 if(fabsf(val
) > 1.0f
)
1086 val
= (ALfloat
)((0.0f
< val
) - (val
< 0.0f
));
1087 /* Convert to a signed integer, between -16777215 and +16777215. */
1088 return fastf2i(val
*16777215.0f
);
1091 static inline ALfloat
aluF2F(ALfloat val
)
1093 static inline ALint
aluF2I(ALfloat val
)
1094 { return aluF2I25(val
)<<7; }
1095 static inline ALuint
aluF2UI(ALfloat val
)
1096 { return aluF2I(val
)+2147483648u; }
1097 static inline ALshort
aluF2S(ALfloat val
)
1098 { return aluF2I25(val
)>>9; }
1099 static inline ALushort
aluF2US(ALfloat val
)
1100 { return aluF2S(val
)+32768; }
1101 static inline ALbyte
aluF2B(ALfloat val
)
1102 { return aluF2I25(val
)>>17; }
1103 static inline ALubyte
aluF2UB(ALfloat val
)
1104 { return aluF2B(val
)+128; }
1106 #define DECL_TEMPLATE(T, func) \
1107 static void Write_##T(ALCdevice *device, ALvoid **buffer, ALuint SamplesToDo) \
1109 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
1110 const ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
1111 const ALuint *offsets = device->ChannelOffsets; \
1114 for(j = 0;j < MaxChannels;j++) \
1118 if(offsets[j] == INVALID_OFFSET) \
1121 out = (T*)(*buffer) + offsets[j]; \
1122 for(i = 0;i < SamplesToDo;i++) \
1123 out[i*numchans] = func(DryBuffer[j][i]); \
1125 *buffer = (char*)(*buffer) + SamplesToDo*numchans*sizeof(T); \
1128 DECL_TEMPLATE(ALfloat
, aluF2F
)
1129 DECL_TEMPLATE(ALuint
, aluF2UI
)
1130 DECL_TEMPLATE(ALint
, aluF2I
)
1131 DECL_TEMPLATE(ALushort
, aluF2US
)
1132 DECL_TEMPLATE(ALshort
, aluF2S
)
1133 DECL_TEMPLATE(ALubyte
, aluF2UB
)
1134 DECL_TEMPLATE(ALbyte
, aluF2B
)
1136 #undef DECL_TEMPLATE
1139 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1142 ALeffectslot
**slot
, **slot_end
;
1143 ALvoice
*voice
, *voice_end
;
1148 SetMixerFPUMode(&oldMode
);
1152 IncrementRef(&device
->MixCount
);
1154 SamplesToDo
= minu(size
, BUFFERSIZE
);
1155 for(c
= 0;c
< MaxChannels
;c
++)
1156 memset(device
->DryBuffer
[c
], 0, SamplesToDo
*sizeof(ALfloat
));
1158 ALCdevice_Lock(device
);
1159 V(device
->Synth
,process
)(SamplesToDo
, device
->DryBuffer
);
1161 ctx
= ATOMIC_LOAD(&device
->ContextList
);
1164 ALenum DeferUpdates
= ctx
->DeferUpdates
;
1165 ALenum UpdateSources
= AL_FALSE
;
1168 UpdateSources
= ATOMIC_EXCHANGE(ALenum
, &ctx
->UpdateSources
, AL_FALSE
);
1171 CalcListenerParams(ctx
->Listener
);
1173 /* source processing */
1174 voice
= ctx
->Voices
;
1175 voice_end
= voice
+ ctx
->VoiceCount
;
1176 while(voice
!= voice_end
)
1178 ALsource
*source
= voice
->Source
;
1179 if(!source
) goto next
;
1181 if(source
->state
!= AL_PLAYING
&& source
->state
!= AL_PAUSED
)
1183 voice
->Source
= NULL
;
1187 if(!DeferUpdates
&& (ATOMIC_EXCHANGE(ALenum
, &source
->NeedsUpdate
, AL_FALSE
) ||
1189 voice
->Update(voice
, source
, ctx
);
1191 if(source
->state
!= AL_PAUSED
)
1192 MixSource(voice
, source
, device
, SamplesToDo
);
1197 /* effect slot processing */
1198 slot
= VECTOR_ITER_BEGIN(ctx
->ActiveAuxSlots
);
1199 slot_end
= VECTOR_ITER_END(ctx
->ActiveAuxSlots
);
1200 while(slot
!= slot_end
)
1202 if(!DeferUpdates
&& ATOMIC_EXCHANGE(ALenum
, &(*slot
)->NeedsUpdate
, AL_FALSE
))
1203 V((*slot
)->EffectState
,update
)(device
, *slot
);
1205 V((*slot
)->EffectState
,process
)(SamplesToDo
, (*slot
)->WetBuffer
[0],
1208 for(i
= 0;i
< SamplesToDo
;i
++)
1209 (*slot
)->WetBuffer
[0][i
] = 0.0f
;
1217 slot
= &device
->DefaultSlot
;
1220 if(ATOMIC_EXCHANGE(ALenum
, &(*slot
)->NeedsUpdate
, AL_FALSE
))
1221 V((*slot
)->EffectState
,update
)(device
, *slot
);
1223 V((*slot
)->EffectState
,process
)(SamplesToDo
, (*slot
)->WetBuffer
[0],
1226 for(i
= 0;i
< SamplesToDo
;i
++)
1227 (*slot
)->WetBuffer
[0][i
] = 0.0f
;
1230 /* Increment the clock time. Every second's worth of samples is
1231 * converted and added to clock base so that large sample counts don't
1232 * overflow during conversion. This also guarantees an exact, stable
1234 device
->SamplesDone
+= SamplesToDo
;
1235 device
->ClockBase
+= (device
->SamplesDone
/device
->Frequency
) * DEVICE_CLOCK_RES
;
1236 device
->SamplesDone
%= device
->Frequency
;
1237 ALCdevice_Unlock(device
);
1241 /* Apply binaural/crossfeed filter */
1242 for(i
= 0;i
< SamplesToDo
;i
++)
1245 samples
[0] = device
->DryBuffer
[FrontLeft
][i
];
1246 samples
[1] = device
->DryBuffer
[FrontRight
][i
];
1247 bs2b_cross_feed(device
->Bs2b
, samples
);
1248 device
->DryBuffer
[FrontLeft
][i
] = samples
[0];
1249 device
->DryBuffer
[FrontRight
][i
] = samples
[1];
1255 switch(device
->FmtType
)
1258 Write_ALbyte(device
, &buffer
, SamplesToDo
);
1261 Write_ALubyte(device
, &buffer
, SamplesToDo
);
1264 Write_ALshort(device
, &buffer
, SamplesToDo
);
1267 Write_ALushort(device
, &buffer
, SamplesToDo
);
1270 Write_ALint(device
, &buffer
, SamplesToDo
);
1273 Write_ALuint(device
, &buffer
, SamplesToDo
);
1276 Write_ALfloat(device
, &buffer
, SamplesToDo
);
1281 size
-= SamplesToDo
;
1282 IncrementRef(&device
->MixCount
);
1285 RestoreFPUMode(&oldMode
);
1289 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1291 ALCcontext
*Context
;
1293 device
->Connected
= ALC_FALSE
;
1295 Context
= ATOMIC_LOAD(&device
->ContextList
);
1298 ALvoice
*voice
, *voice_end
;
1300 voice
= Context
->Voices
;
1301 voice_end
= voice
+ Context
->VoiceCount
;
1302 while(voice
!= voice_end
)
1304 ALsource
*source
= voice
->Source
;
1305 voice
->Source
= NULL
;
1307 if(source
&& source
->state
== AL_PLAYING
)
1309 source
->state
= AL_STOPPED
;
1310 ATOMIC_STORE(&source
->current_buffer
, NULL
);
1311 source
->position
= 0;
1312 source
->position_fraction
= 0;
1317 Context
->VoiceCount
= 0;
1319 Context
= Context
->next
;