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
32 #include "alListener.h"
33 #include "alAuxEffectSlot.h"
37 #include "static_assert.h"
39 #include "mixer_defs.h"
41 #include "midi/base.h"
44 static_assert((INT_MAX
>>FRACTIONBITS
)/MAX_PITCH
> BUFFERSIZE
,
45 "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
53 ALfloat ConeScale
= 1.0f
;
55 /* Localized Z scalar for mono sources */
56 ALfloat ZScale
= 1.0f
;
58 extern inline ALfloat
minf(ALfloat a
, ALfloat b
);
59 extern inline ALfloat
maxf(ALfloat a
, ALfloat b
);
60 extern inline ALfloat
clampf(ALfloat val
, ALfloat min
, ALfloat max
);
62 extern inline ALdouble
mind(ALdouble a
, ALdouble b
);
63 extern inline ALdouble
maxd(ALdouble a
, ALdouble b
);
64 extern inline ALdouble
clampd(ALdouble val
, ALdouble min
, ALdouble max
);
66 extern inline ALuint
minu(ALuint a
, ALuint b
);
67 extern inline ALuint
maxu(ALuint a
, ALuint b
);
68 extern inline ALuint
clampu(ALuint val
, ALuint min
, ALuint max
);
70 extern inline ALint
mini(ALint a
, ALint b
);
71 extern inline ALint
maxi(ALint a
, ALint b
);
72 extern inline ALint
clampi(ALint val
, ALint min
, ALint max
);
74 extern inline ALint64
mini64(ALint64 a
, ALint64 b
);
75 extern inline ALint64
maxi64(ALint64 a
, ALint64 b
);
76 extern inline ALint64
clampi64(ALint64 val
, ALint64 min
, ALint64 max
);
78 extern inline ALuint64
minu64(ALuint64 a
, ALuint64 b
);
79 extern inline ALuint64
maxu64(ALuint64 a
, ALuint64 b
);
80 extern inline ALuint64
clampu64(ALuint64 val
, ALuint64 min
, ALuint64 max
);
82 extern inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALfloat mu
);
83 extern inline ALfloat
cubic(ALfloat val0
, ALfloat val1
, ALfloat val2
, ALfloat val3
, ALfloat mu
);
85 static ResamplerFunc
SelectResampler(enum Resampler Resampler
, ALuint increment
)
87 if(increment
== FRACTIONONE
)
88 return Resample_copy32_C
;
92 return Resample_point32_C
;
94 return Resample_lerp32_C
;
96 return Resample_cubic32_C
;
98 /* Shouldn't happen */
102 return Resample_point32_C
;
106 static HrtfMixerFunc
SelectHrtfMixer(void)
109 if((CPUCapFlags
&CPU_CAP_SSE
))
110 return MixDirect_Hrtf_SSE
;
113 if((CPUCapFlags
&CPU_CAP_NEON
))
114 return MixDirect_Hrtf_Neon
;
117 return MixDirect_Hrtf_C
;
120 static DryMixerFunc
SelectDirectMixer(void)
123 if((CPUCapFlags
&CPU_CAP_SSE
))
124 return MixDirect_SSE
;
127 if((CPUCapFlags
&CPU_CAP_NEON
))
128 return MixDirect_Neon
;
134 static WetMixerFunc
SelectSendMixer(void)
137 if((CPUCapFlags
&CPU_CAP_SSE
))
141 if((CPUCapFlags
&CPU_CAP_NEON
))
149 static inline void aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
151 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
152 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
153 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
156 static inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
158 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
159 inVector1
[2]*inVector2
[2];
162 static inline void aluNormalize(ALfloat
*inVector
)
164 ALfloat lengthsqr
= aluDotproduct(inVector
, inVector
);
167 ALfloat inv_length
= 1.0f
/sqrtf(lengthsqr
);
168 inVector
[0] *= inv_length
;
169 inVector
[1] *= inv_length
;
170 inVector
[2] *= inv_length
;
174 static inline ALvoid
aluMatrixVector(ALfloat
*vector
, ALfloat w
, ALfloat (*restrict matrix
)[4])
177 vector
[0], vector
[1], vector
[2], w
180 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
181 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
182 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
186 static ALvoid
CalcListenerParams(ALlistener
*Listener
)
188 ALfloat N
[3], V
[3], U
[3], P
[3];
191 N
[0] = Listener
->Forward
[0];
192 N
[1] = Listener
->Forward
[1];
193 N
[2] = Listener
->Forward
[2];
195 V
[0] = Listener
->Up
[0];
196 V
[1] = Listener
->Up
[1];
197 V
[2] = Listener
->Up
[2];
199 /* Build and normalize right-vector */
200 aluCrossproduct(N
, V
, U
);
203 Listener
->Params
.Matrix
[0][0] = U
[0];
204 Listener
->Params
.Matrix
[0][1] = V
[0];
205 Listener
->Params
.Matrix
[0][2] = -N
[0];
206 Listener
->Params
.Matrix
[0][3] = 0.0f
;
207 Listener
->Params
.Matrix
[1][0] = U
[1];
208 Listener
->Params
.Matrix
[1][1] = V
[1];
209 Listener
->Params
.Matrix
[1][2] = -N
[1];
210 Listener
->Params
.Matrix
[1][3] = 0.0f
;
211 Listener
->Params
.Matrix
[2][0] = U
[2];
212 Listener
->Params
.Matrix
[2][1] = V
[2];
213 Listener
->Params
.Matrix
[2][2] = -N
[2];
214 Listener
->Params
.Matrix
[2][3] = 0.0f
;
215 Listener
->Params
.Matrix
[3][0] = 0.0f
;
216 Listener
->Params
.Matrix
[3][1] = 0.0f
;
217 Listener
->Params
.Matrix
[3][2] = 0.0f
;
218 Listener
->Params
.Matrix
[3][3] = 1.0f
;
220 P
[0] = Listener
->Position
[0];
221 P
[1] = Listener
->Position
[1];
222 P
[2] = Listener
->Position
[2];
223 aluMatrixVector(P
, 1.0f
, Listener
->Params
.Matrix
);
224 Listener
->Params
.Matrix
[3][0] = -P
[0];
225 Listener
->Params
.Matrix
[3][1] = -P
[1];
226 Listener
->Params
.Matrix
[3][2] = -P
[2];
228 Listener
->Params
.Velocity
[0] = Listener
->Velocity
[0];
229 Listener
->Params
.Velocity
[1] = Listener
->Velocity
[1];
230 Listener
->Params
.Velocity
[2] = Listener
->Velocity
[2];
231 aluMatrixVector(Listener
->Params
.Velocity
, 0.0f
, Listener
->Params
.Matrix
);
234 ALvoid
CalcNonAttnSourceParams(ALactivesource
*src
, const ALCcontext
*ALContext
)
236 static const struct ChanMap MonoMap
[1] = { { FrontCenter
, 0.0f
} };
237 static const struct ChanMap StereoMap
[2] = {
238 { FrontLeft
, DEG2RAD(-30.0f
) },
239 { FrontRight
, DEG2RAD( 30.0f
) }
241 static const struct ChanMap StereoWideMap
[2] = {
242 { FrontLeft
, DEG2RAD(-90.0f
) },
243 { FrontRight
, DEG2RAD( 90.0f
) }
245 static const struct ChanMap RearMap
[2] = {
246 { BackLeft
, DEG2RAD(-150.0f
) },
247 { BackRight
, DEG2RAD( 150.0f
) }
249 static const struct ChanMap QuadMap
[4] = {
250 { FrontLeft
, DEG2RAD( -45.0f
) },
251 { FrontRight
, DEG2RAD( 45.0f
) },
252 { BackLeft
, DEG2RAD(-135.0f
) },
253 { BackRight
, DEG2RAD( 135.0f
) }
255 static const struct ChanMap X51Map
[6] = {
256 { FrontLeft
, DEG2RAD( -30.0f
) },
257 { FrontRight
, DEG2RAD( 30.0f
) },
258 { FrontCenter
, DEG2RAD( 0.0f
) },
260 { BackLeft
, DEG2RAD(-110.0f
) },
261 { BackRight
, DEG2RAD( 110.0f
) }
263 static const struct ChanMap X61Map
[7] = {
264 { FrontLeft
, DEG2RAD(-30.0f
) },
265 { FrontRight
, DEG2RAD( 30.0f
) },
266 { FrontCenter
, DEG2RAD( 0.0f
) },
268 { BackCenter
, DEG2RAD(180.0f
) },
269 { SideLeft
, DEG2RAD(-90.0f
) },
270 { SideRight
, DEG2RAD( 90.0f
) }
272 static const struct ChanMap X71Map
[8] = {
273 { FrontLeft
, DEG2RAD( -30.0f
) },
274 { FrontRight
, DEG2RAD( 30.0f
) },
275 { FrontCenter
, DEG2RAD( 0.0f
) },
277 { BackLeft
, DEG2RAD(-150.0f
) },
278 { BackRight
, DEG2RAD( 150.0f
) },
279 { SideLeft
, DEG2RAD( -90.0f
) },
280 { SideRight
, DEG2RAD( 90.0f
) }
283 ALCdevice
*Device
= ALContext
->Device
;
284 const ALsource
*ALSource
= src
->Source
;
285 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
286 ALbufferlistitem
*BufferListItem
;
287 enum FmtChannels Channels
;
288 ALfloat DryGain
, DryGainHF
, DryGainLF
;
289 ALfloat WetGain
[MAX_SENDS
];
290 ALfloat WetGainHF
[MAX_SENDS
];
291 ALfloat WetGainLF
[MAX_SENDS
];
292 ALint NumSends
, Frequency
;
293 const struct ChanMap
*chans
= NULL
;
294 enum Resampler Resampler
;
295 ALint num_channels
= 0;
296 ALboolean DirectChannels
;
297 ALfloat hwidth
= 0.0f
;
301 /* Get device properties */
302 NumSends
= Device
->NumAuxSends
;
303 Frequency
= Device
->Frequency
;
305 /* Get listener properties */
306 ListenerGain
= ALContext
->Listener
->Gain
;
308 /* Get source properties */
309 SourceVolume
= ALSource
->Gain
;
310 MinVolume
= ALSource
->MinGain
;
311 MaxVolume
= ALSource
->MaxGain
;
312 Pitch
= ALSource
->Pitch
;
313 Resampler
= ALSource
->Resampler
;
314 DirectChannels
= ALSource
->DirectChannels
;
316 src
->Direct
.OutBuffer
= Device
->DryBuffer
;
317 for(i
= 0;i
< NumSends
;i
++)
319 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
321 Slot
= Device
->DefaultSlot
;
322 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
323 src
->Send
[i
].OutBuffer
= NULL
;
325 src
->Send
[i
].OutBuffer
= Slot
->WetBuffer
;
328 /* Calculate the stepping value */
330 BufferListItem
= ALSource
->queue
;
331 while(BufferListItem
!= NULL
)
334 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
336 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
337 if(Pitch
> (ALfloat
)MAX_PITCH
)
338 src
->Step
= MAX_PITCH
<<FRACTIONBITS
;
341 src
->Step
= fastf2i(Pitch
*FRACTIONONE
);
345 src
->Resample
= SelectResampler(Resampler
, src
->Step
);
347 Channels
= ALBuffer
->FmtChannels
;
350 BufferListItem
= BufferListItem
->next
;
353 /* Calculate gains */
354 DryGain
= clampf(SourceVolume
, MinVolume
, MaxVolume
);
355 DryGain
*= ALSource
->Direct
.Gain
* ListenerGain
;
356 DryGainHF
= ALSource
->Direct
.GainHF
;
357 DryGainLF
= ALSource
->Direct
.GainLF
;
358 for(i
= 0;i
< NumSends
;i
++)
360 WetGain
[i
] = clampf(SourceVolume
, MinVolume
, MaxVolume
);
361 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
362 WetGainHF
[i
] = ALSource
->Send
[i
].GainHF
;
363 WetGainLF
[i
] = ALSource
->Send
[i
].GainLF
;
374 if(!(Device
->Flags
&DEVICE_WIDE_STEREO
))
376 /* HACK: Place the stereo channels at +/-90 degrees when using non-
377 * HRTF stereo output. This helps reduce the "monoization" caused
378 * by them panning towards the center. */
379 if(Device
->FmtChans
== DevFmtStereo
&& !Device
->Hrtf
)
380 chans
= StereoWideMap
;
386 chans
= StereoWideMap
;
387 hwidth
= DEG2RAD(60.0f
);
418 if(DirectChannels
!= AL_FALSE
)
420 for(c
= 0;c
< num_channels
;c
++)
422 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[c
].Target
;
423 for(j
= 0;j
< MaxChannels
;j
++)
427 for(c
= 0;c
< num_channels
;c
++)
429 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[c
].Target
;
430 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
432 enum Channel chan
= Device
->Speaker2Chan
[i
];
433 if(chan
== chans
[c
].channel
)
435 Target
[chan
] = DryGain
;
441 if(!src
->Direct
.Moving
)
443 for(i
= 0;i
< num_channels
;i
++)
445 ALfloat
*restrict Current
= src
->Direct
.Mix
.Gains
[i
].Current
;
446 ALfloat
*restrict Step
= src
->Direct
.Mix
.Gains
[i
].Step
;
447 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[i
].Target
;
448 for(j
= 0;j
< MaxChannels
;j
++)
450 Current
[j
] = Target
[j
];
454 src
->Direct
.Counter
= 0;
455 src
->Direct
.Moving
= AL_TRUE
;
459 for(i
= 0;i
< num_channels
;i
++)
461 ALfloat
*restrict Current
= src
->Direct
.Mix
.Gains
[i
].Current
;
462 ALfloat
*restrict Step
= src
->Direct
.Mix
.Gains
[i
].Step
;
463 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[i
].Target
;
464 for(j
= 0;j
< MaxChannels
;j
++)
466 ALfloat cur
= maxf(Current
[j
], FLT_EPSILON
);
467 ALfloat trg
= maxf(Target
[j
], FLT_EPSILON
);
468 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
469 Step
[j
] = powf(trg
/cur
, 1.0f
/64.0f
);
475 src
->Direct
.Counter
= 64;
478 src
->IsHrtf
= AL_FALSE
;
479 src
->Dry
.Mix
= SelectDirectMixer();
481 else if(Device
->Hrtf
)
483 for(c
= 0;c
< num_channels
;c
++)
485 if(chans
[c
].channel
== LFE
)
488 src
->Direct
.Mix
.Hrtf
.Params
[c
].Delay
[0] = 0;
489 src
->Direct
.Mix
.Hrtf
.Params
[c
].Delay
[1] = 0;
490 for(i
= 0;i
< HRIR_LENGTH
;i
++)
492 src
->Direct
.Mix
.Hrtf
.Params
[c
].Coeffs
[i
][0] = 0.0f
;
493 src
->Direct
.Mix
.Hrtf
.Params
[c
].Coeffs
[i
][1] = 0.0f
;
498 /* Get the static HRIR coefficients and delays for this
500 GetLerpedHrtfCoeffs(Device
->Hrtf
,
501 0.0f
, chans
[c
].angle
, DryGain
,
502 src
->Direct
.Mix
.Hrtf
.Params
[c
].Coeffs
,
503 src
->Direct
.Mix
.Hrtf
.Params
[c
].Delay
);
506 src
->Direct
.Counter
= 0;
507 src
->Direct
.Moving
= AL_TRUE
;
508 src
->Direct
.Mix
.Hrtf
.IrSize
= GetHrtfIrSize(Device
->Hrtf
);
510 src
->IsHrtf
= AL_TRUE
;
511 src
->Dry
.HrtfMix
= SelectHrtfMixer();
515 for(i
= 0;i
< num_channels
;i
++)
517 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[i
].Target
;
518 for(j
= 0;j
< MaxChannels
;j
++)
522 DryGain
*= lerp(1.0f
, 1.0f
/sqrtf((float)Device
->NumChan
), hwidth
/F_PI
);
523 for(c
= 0;c
< num_channels
;c
++)
525 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[c
].Target
;
526 /* Special-case LFE */
527 if(chans
[c
].channel
== LFE
)
529 Target
[chans
[c
].channel
] = DryGain
;
532 ComputeAngleGains(Device
, chans
[c
].angle
, hwidth
, DryGain
, Target
);
535 if(!src
->Direct
.Moving
)
537 for(i
= 0;i
< num_channels
;i
++)
539 ALfloat
*restrict Current
= src
->Direct
.Mix
.Gains
[i
].Current
;
540 ALfloat
*restrict Step
= src
->Direct
.Mix
.Gains
[i
].Step
;
541 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[i
].Target
;
542 for(j
= 0;j
< MaxChannels
;j
++)
544 Current
[j
] = Target
[j
];
548 src
->Direct
.Counter
= 0;
549 src
->Direct
.Moving
= AL_TRUE
;
553 for(i
= 0;i
< num_channels
;i
++)
555 ALfloat
*restrict Current
= src
->Direct
.Mix
.Gains
[i
].Current
;
556 ALfloat
*restrict Step
= src
->Direct
.Mix
.Gains
[i
].Step
;
557 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[i
].Target
;
558 for(j
= 0;j
< MaxChannels
;j
++)
560 ALfloat trg
= maxf(Target
[j
], FLT_EPSILON
);
561 ALfloat cur
= maxf(Current
[j
], FLT_EPSILON
);
562 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
563 Step
[j
] = powf(trg
/cur
, 1.0f
/64.0f
);
569 src
->Direct
.Counter
= 64;
572 src
->IsHrtf
= AL_FALSE
;
573 src
->Dry
.Mix
= SelectDirectMixer();
575 for(i
= 0;i
< NumSends
;i
++)
577 src
->Send
[i
].Gain
.Target
= WetGain
[i
];
578 if(!src
->Send
[i
].Moving
)
580 src
->Send
[i
].Gain
.Current
= src
->Send
[i
].Gain
.Target
;
581 src
->Send
[i
].Gain
.Step
= 1.0f
;
582 src
->Send
[i
].Counter
= 0;
583 src
->Send
[i
].Moving
= AL_TRUE
;
587 ALfloat cur
= maxf(src
->Send
[i
].Gain
.Current
, FLT_EPSILON
);
588 ALfloat trg
= maxf(src
->Send
[i
].Gain
.Target
, FLT_EPSILON
);
589 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
590 src
->Send
[i
].Gain
.Step
= powf(trg
/cur
, 1.0f
/64.0f
);
592 src
->Send
[i
].Gain
.Step
= 1.0f
;
593 src
->Send
[i
].Gain
.Current
= cur
;
594 src
->Send
[i
].Counter
= 64;
597 src
->WetMix
= SelectSendMixer();
600 ALfloat gainhf
= maxf(0.01f
, DryGainHF
);
601 ALfloat gainlf
= maxf(0.01f
, DryGainLF
);
602 ALfloat hfscale
= ALSource
->Direct
.HFReference
/ Frequency
;
603 ALfloat lfscale
= ALSource
->Direct
.LFReference
/ Frequency
;
604 for(c
= 0;c
< num_channels
;c
++)
606 src
->Direct
.Filters
[c
].ActiveType
= AF_None
;
607 if(gainhf
!= 1.0f
) src
->Direct
.Filters
[c
].ActiveType
|= AF_LowPass
;
608 if(gainlf
!= 1.0f
) src
->Direct
.Filters
[c
].ActiveType
|= AF_HighPass
;
609 ALfilterState_setParams(
610 &src
->Direct
.Filters
[c
].LowPass
, ALfilterType_HighShelf
, gainhf
,
613 ALfilterState_setParams(
614 &src
->Direct
.Filters
[c
].HighPass
, ALfilterType_LowShelf
, gainlf
,
619 for(i
= 0;i
< NumSends
;i
++)
621 ALfloat gainhf
= maxf(0.01f
, WetGainHF
[i
]);
622 ALfloat gainlf
= maxf(0.01f
, WetGainLF
[i
]);
623 ALfloat hfscale
= ALSource
->Send
[i
].HFReference
/ Frequency
;
624 ALfloat lfscale
= ALSource
->Send
[i
].LFReference
/ Frequency
;
625 for(c
= 0;c
< num_channels
;c
++)
627 src
->Send
[i
].Filters
[c
].ActiveType
= AF_None
;
628 if(gainhf
!= 1.0f
) src
->Send
[i
].Filters
[c
].ActiveType
|= AF_LowPass
;
629 if(gainlf
!= 1.0f
) src
->Send
[i
].Filters
[c
].ActiveType
|= AF_HighPass
;
630 ALfilterState_setParams(
631 &src
->Send
[i
].Filters
[c
].LowPass
, ALfilterType_HighShelf
, gainhf
,
634 ALfilterState_setParams(
635 &src
->Send
[i
].Filters
[c
].HighPass
, ALfilterType_LowShelf
, gainlf
,
642 ALvoid
CalcSourceParams(ALactivesource
*src
, const ALCcontext
*ALContext
)
644 ALCdevice
*Device
= ALContext
->Device
;
645 const ALsource
*ALSource
= src
->Source
;
646 ALfloat Velocity
[3],Direction
[3],Position
[3],SourceToListener
[3];
647 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,ClampedDist
;
648 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
;
649 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
650 ALfloat DopplerFactor
, SpeedOfSound
;
651 ALfloat AirAbsorptionFactor
;
652 ALfloat RoomAirAbsorption
[MAX_SENDS
];
653 ALbufferlistitem
*BufferListItem
;
655 ALfloat RoomAttenuation
[MAX_SENDS
];
656 ALfloat MetersPerUnit
;
657 ALfloat RoomRolloffBase
;
658 ALfloat RoomRolloff
[MAX_SENDS
];
659 ALfloat DecayDistance
[MAX_SENDS
];
663 ALboolean DryGainHFAuto
;
664 ALfloat WetGain
[MAX_SENDS
];
665 ALfloat WetGainHF
[MAX_SENDS
];
666 ALfloat WetGainLF
[MAX_SENDS
];
667 ALboolean WetGainAuto
;
668 ALboolean WetGainHFAuto
;
669 enum Resampler Resampler
;
677 for(i
= 0;i
< MAX_SENDS
;i
++)
683 /* Get context/device properties */
684 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
685 SpeedOfSound
= ALContext
->SpeedOfSound
* ALContext
->DopplerVelocity
;
686 NumSends
= Device
->NumAuxSends
;
687 Frequency
= Device
->Frequency
;
689 /* Get listener properties */
690 ListenerGain
= ALContext
->Listener
->Gain
;
691 MetersPerUnit
= ALContext
->Listener
->MetersPerUnit
;
693 /* Get source properties */
694 SourceVolume
= ALSource
->Gain
;
695 MinVolume
= ALSource
->MinGain
;
696 MaxVolume
= ALSource
->MaxGain
;
697 Pitch
= ALSource
->Pitch
;
698 Resampler
= ALSource
->Resampler
;
699 Position
[0] = ALSource
->Position
[0];
700 Position
[1] = ALSource
->Position
[1];
701 Position
[2] = ALSource
->Position
[2];
702 Direction
[0] = ALSource
->Orientation
[0];
703 Direction
[1] = ALSource
->Orientation
[1];
704 Direction
[2] = ALSource
->Orientation
[2];
705 Velocity
[0] = ALSource
->Velocity
[0];
706 Velocity
[1] = ALSource
->Velocity
[1];
707 Velocity
[2] = ALSource
->Velocity
[2];
708 MinDist
= ALSource
->RefDistance
;
709 MaxDist
= ALSource
->MaxDistance
;
710 Rolloff
= ALSource
->RollOffFactor
;
711 InnerAngle
= ALSource
->InnerAngle
;
712 OuterAngle
= ALSource
->OuterAngle
;
713 AirAbsorptionFactor
= ALSource
->AirAbsorptionFactor
;
714 DryGainHFAuto
= ALSource
->DryGainHFAuto
;
715 WetGainAuto
= ALSource
->WetGainAuto
;
716 WetGainHFAuto
= ALSource
->WetGainHFAuto
;
717 RoomRolloffBase
= ALSource
->RoomRolloffFactor
;
719 src
->Direct
.OutBuffer
= Device
->DryBuffer
;
720 for(i
= 0;i
< NumSends
;i
++)
722 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
725 Slot
= Device
->DefaultSlot
;
726 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
729 RoomRolloff
[i
] = 0.0f
;
730 DecayDistance
[i
] = 0.0f
;
731 RoomAirAbsorption
[i
] = 1.0f
;
733 else if(Slot
->AuxSendAuto
)
735 RoomRolloff
[i
] = RoomRolloffBase
;
736 if(IsReverbEffect(Slot
->EffectType
))
738 RoomRolloff
[i
] += Slot
->EffectProps
.Reverb
.RoomRolloffFactor
;
739 DecayDistance
[i
] = Slot
->EffectProps
.Reverb
.DecayTime
*
740 SPEEDOFSOUNDMETRESPERSEC
;
741 RoomAirAbsorption
[i
] = Slot
->EffectProps
.Reverb
.AirAbsorptionGainHF
;
745 DecayDistance
[i
] = 0.0f
;
746 RoomAirAbsorption
[i
] = 1.0f
;
751 /* If the slot's auxiliary send auto is off, the data sent to the
752 * effect slot is the same as the dry path, sans filter effects */
753 RoomRolloff
[i
] = Rolloff
;
754 DecayDistance
[i
] = 0.0f
;
755 RoomAirAbsorption
[i
] = AIRABSORBGAINHF
;
758 if(!Slot
|| Slot
->EffectType
== AL_EFFECT_NULL
)
759 src
->Send
[i
].OutBuffer
= NULL
;
761 src
->Send
[i
].OutBuffer
= Slot
->WetBuffer
;
764 /* Transform source to listener space (convert to head relative) */
765 if(ALSource
->HeadRelative
== AL_FALSE
)
767 ALfloat (*restrict Matrix
)[4] = ALContext
->Listener
->Params
.Matrix
;
768 /* Transform source vectors */
769 aluMatrixVector(Position
, 1.0f
, Matrix
);
770 aluMatrixVector(Direction
, 0.0f
, Matrix
);
771 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
775 const ALfloat
*ListenerVel
= ALContext
->Listener
->Params
.Velocity
;
776 /* Offset the source velocity to be relative of the listener velocity */
777 Velocity
[0] += ListenerVel
[0];
778 Velocity
[1] += ListenerVel
[1];
779 Velocity
[2] += ListenerVel
[2];
782 SourceToListener
[0] = -Position
[0];
783 SourceToListener
[1] = -Position
[1];
784 SourceToListener
[2] = -Position
[2];
785 aluNormalize(SourceToListener
);
786 aluNormalize(Direction
);
788 /* Calculate distance attenuation */
789 Distance
= sqrtf(aluDotproduct(Position
, Position
));
790 ClampedDist
= Distance
;
793 for(i
= 0;i
< NumSends
;i
++)
794 RoomAttenuation
[i
] = 1.0f
;
795 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
796 ALContext
->DistanceModel
)
798 case InverseDistanceClamped
:
799 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
800 if(MaxDist
< MinDist
)
803 case InverseDistance
:
806 if((MinDist
+ (Rolloff
* (ClampedDist
- MinDist
))) > 0.0f
)
807 Attenuation
= MinDist
/ (MinDist
+ (Rolloff
* (ClampedDist
- MinDist
)));
808 for(i
= 0;i
< NumSends
;i
++)
810 if((MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
))) > 0.0f
)
811 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (ClampedDist
- MinDist
)));
816 case LinearDistanceClamped
:
817 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
818 if(MaxDist
< MinDist
)
822 if(MaxDist
!= MinDist
)
824 Attenuation
= 1.0f
- (Rolloff
*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
825 Attenuation
= maxf(Attenuation
, 0.0f
);
826 for(i
= 0;i
< NumSends
;i
++)
828 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(ClampedDist
-MinDist
)/(MaxDist
- MinDist
));
829 RoomAttenuation
[i
] = maxf(RoomAttenuation
[i
], 0.0f
);
834 case ExponentDistanceClamped
:
835 ClampedDist
= clampf(ClampedDist
, MinDist
, MaxDist
);
836 if(MaxDist
< MinDist
)
839 case ExponentDistance
:
840 if(ClampedDist
> 0.0f
&& MinDist
> 0.0f
)
842 Attenuation
= powf(ClampedDist
/MinDist
, -Rolloff
);
843 for(i
= 0;i
< NumSends
;i
++)
844 RoomAttenuation
[i
] = powf(ClampedDist
/MinDist
, -RoomRolloff
[i
]);
848 case DisableDistance
:
849 ClampedDist
= MinDist
;
853 /* Source Gain + Attenuation */
854 DryGain
= SourceVolume
* Attenuation
;
855 for(i
= 0;i
< NumSends
;i
++)
856 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
858 /* Distance-based air absorption */
859 if(AirAbsorptionFactor
> 0.0f
&& ClampedDist
> MinDist
)
861 ALfloat meters
= maxf(ClampedDist
-MinDist
, 0.0f
) * MetersPerUnit
;
862 DryGainHF
*= powf(AIRABSORBGAINHF
, AirAbsorptionFactor
*meters
);
863 for(i
= 0;i
< NumSends
;i
++)
864 WetGainHF
[i
] *= powf(RoomAirAbsorption
[i
], AirAbsorptionFactor
*meters
);
869 ALfloat ApparentDist
= 1.0f
/maxf(Attenuation
, 0.00001f
) - 1.0f
;
871 /* Apply a decay-time transformation to the wet path, based on the
872 * attenuation of the dry path.
874 * Using the apparent distance, based on the distance attenuation, the
875 * initial decay of the reverb effect is calculated and applied to the
878 for(i
= 0;i
< NumSends
;i
++)
880 if(DecayDistance
[i
] > 0.0f
)
881 WetGain
[i
] *= powf(0.001f
/*-60dB*/, ApparentDist
/DecayDistance
[i
]);
885 /* Calculate directional soundcones */
886 Angle
= RAD2DEG(acosf(aluDotproduct(Direction
,SourceToListener
)) * ConeScale
) * 2.0f
;
887 if(Angle
> InnerAngle
&& Angle
<= OuterAngle
)
889 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
890 ConeVolume
= lerp(1.0f
, ALSource
->OuterGain
, scale
);
891 ConeHF
= lerp(1.0f
, ALSource
->OuterGainHF
, scale
);
893 else if(Angle
> OuterAngle
)
895 ConeVolume
= ALSource
->OuterGain
;
896 ConeHF
= ALSource
->OuterGainHF
;
904 DryGain
*= ConeVolume
;
907 for(i
= 0;i
< NumSends
;i
++)
908 WetGain
[i
] *= ConeVolume
;
914 for(i
= 0;i
< NumSends
;i
++)
915 WetGainHF
[i
] *= ConeHF
;
918 /* Clamp to Min/Max Gain */
919 DryGain
= clampf(DryGain
, MinVolume
, MaxVolume
);
920 for(i
= 0;i
< NumSends
;i
++)
921 WetGain
[i
] = clampf(WetGain
[i
], MinVolume
, MaxVolume
);
923 /* Apply gain and frequency filters */
924 DryGain
*= ALSource
->Direct
.Gain
* ListenerGain
;
925 DryGainHF
*= ALSource
->Direct
.GainHF
;
926 DryGainLF
*= ALSource
->Direct
.GainLF
;
927 for(i
= 0;i
< NumSends
;i
++)
929 WetGain
[i
] *= ALSource
->Send
[i
].Gain
* ListenerGain
;
930 WetGainHF
[i
] *= ALSource
->Send
[i
].GainHF
;
931 WetGainLF
[i
] *= ALSource
->Send
[i
].GainLF
;
934 /* Calculate velocity-based doppler effect */
935 if(DopplerFactor
> 0.0f
)
937 const ALfloat
*ListenerVel
= ALContext
->Listener
->Params
.Velocity
;
940 if(SpeedOfSound
< 1.0f
)
942 DopplerFactor
*= 1.0f
/SpeedOfSound
;
946 VSS
= aluDotproduct(Velocity
, SourceToListener
) * DopplerFactor
;
947 VLS
= aluDotproduct(ListenerVel
, SourceToListener
) * DopplerFactor
;
949 Pitch
*= clampf(SpeedOfSound
-VLS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
) /
950 clampf(SpeedOfSound
-VSS
, 1.0f
, SpeedOfSound
*2.0f
- 1.0f
);
953 BufferListItem
= ALSource
->queue
;
954 while(BufferListItem
!= NULL
)
957 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
959 /* Calculate fixed-point stepping value, based on the pitch, buffer
960 * frequency, and output frequency. */
961 Pitch
= Pitch
* ALBuffer
->Frequency
/ Frequency
;
962 if(Pitch
> (ALfloat
)MAX_PITCH
)
963 src
->Step
= MAX_PITCH
<<FRACTIONBITS
;
966 src
->Step
= fastf2i(Pitch
*FRACTIONONE
);
970 src
->Resample
= SelectResampler(Resampler
, src
->Step
);
974 BufferListItem
= BufferListItem
->next
;
979 /* Use a binaural HRTF algorithm for stereo headphone playback */
980 ALfloat delta
, ev
= 0.0f
, az
= 0.0f
;
982 if(Distance
> FLT_EPSILON
)
984 ALfloat invlen
= 1.0f
/Distance
;
985 Position
[0] *= invlen
;
986 Position
[1] *= invlen
;
987 Position
[2] *= invlen
;
989 /* Calculate elevation and azimuth only when the source is not at
990 * the listener. This prevents +0 and -0 Z from producing
991 * inconsistent panning. Also, clamp Y in case FP precision errors
992 * cause it to land outside of -1..+1. */
993 ev
= asinf(clampf(Position
[1], -1.0f
, 1.0f
));
994 az
= atan2f(Position
[0], -Position
[2]*ZScale
);
997 /* Check to see if the HRIR is already moving. */
998 if(src
->Direct
.Moving
)
1000 /* Calculate the normalized HRTF transition factor (delta). */
1001 delta
= CalcHrtfDelta(src
->Direct
.Mix
.Hrtf
.Gain
, DryGain
,
1002 src
->Direct
.Mix
.Hrtf
.Dir
, Position
);
1003 /* If the delta is large enough, get the moving HRIR target
1004 * coefficients, target delays, steppping values, and counter. */
1007 ALuint counter
= GetMovingHrtfCoeffs(Device
->Hrtf
,
1008 ev
, az
, DryGain
, delta
,
1009 src
->Direct
.Counter
,
1010 src
->Direct
.Mix
.Hrtf
.Params
[0].Coeffs
,
1011 src
->Direct
.Mix
.Hrtf
.Params
[0].Delay
,
1012 src
->Direct
.Mix
.Hrtf
.Params
[0].CoeffStep
,
1013 src
->Direct
.Mix
.Hrtf
.Params
[0].DelayStep
);
1014 src
->Direct
.Counter
= counter
;
1015 src
->Direct
.Mix
.Hrtf
.Gain
= DryGain
;
1016 src
->Direct
.Mix
.Hrtf
.Dir
[0] = Position
[0];
1017 src
->Direct
.Mix
.Hrtf
.Dir
[1] = Position
[1];
1018 src
->Direct
.Mix
.Hrtf
.Dir
[2] = Position
[2];
1023 /* Get the initial (static) HRIR coefficients and delays. */
1024 GetLerpedHrtfCoeffs(Device
->Hrtf
, ev
, az
, DryGain
,
1025 src
->Direct
.Mix
.Hrtf
.Params
[0].Coeffs
,
1026 src
->Direct
.Mix
.Hrtf
.Params
[0].Delay
);
1027 src
->Direct
.Counter
= 0;
1028 src
->Direct
.Moving
= AL_TRUE
;
1029 src
->Direct
.Mix
.Hrtf
.Gain
= DryGain
;
1030 src
->Direct
.Mix
.Hrtf
.Dir
[0] = Position
[0];
1031 src
->Direct
.Mix
.Hrtf
.Dir
[1] = Position
[1];
1032 src
->Direct
.Mix
.Hrtf
.Dir
[2] = Position
[2];
1034 src
->Direct
.Mix
.Hrtf
.IrSize
= GetHrtfIrSize(Device
->Hrtf
);
1036 src
->IsHrtf
= AL_TRUE
;
1037 src
->Dry
.HrtfMix
= SelectHrtfMixer();
1041 ALfloat
*restrict Target
= src
->Direct
.Mix
.Gains
[0].Target
;
1042 ALfloat DirGain
= 0.0f
;
1043 ALfloat AmbientGain
;
1045 for(j
= 0;j
< MaxChannels
;j
++)
1048 /* Normalize the length, and compute panned gains. */
1049 if(Distance
> FLT_EPSILON
)
1051 ALfloat invlen
= 1.0f
/Distance
;
1052 Position
[0] *= invlen
;
1053 Position
[1] *= invlen
;
1054 Position
[2] *= invlen
;
1056 DirGain
= sqrtf(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
1057 ComputeAngleGains(Device
, atan2f(Position
[0], -Position
[2]*ZScale
), 0.0f
,
1058 DryGain
*DirGain
, Target
);
1061 /* Adjustment for vertical offsets. Not the greatest, but simple
1063 AmbientGain
= DryGain
* sqrtf(1.0f
/Device
->NumChan
) * (1.0f
-DirGain
);
1064 for(i
= 0;i
< (ALint
)Device
->NumChan
;i
++)
1066 enum Channel chan
= Device
->Speaker2Chan
[i
];
1067 Target
[chan
] = maxf(Target
[chan
], AmbientGain
);
1070 if(!src
->Direct
.Moving
)
1072 ALfloat
*restrict Current
= src
->Direct
.Mix
.Gains
[0].Current
;
1073 ALfloat
*restrict Step
= src
->Direct
.Mix
.Gains
[0].Step
;
1074 for(j
= 0;j
< MaxChannels
;j
++)
1076 Current
[j
] = Target
[j
];
1079 src
->Direct
.Counter
= 0;
1080 src
->Direct
.Moving
= AL_TRUE
;
1084 ALfloat
*restrict Current
= src
->Direct
.Mix
.Gains
[0].Current
;
1085 ALfloat
*restrict Step
= src
->Direct
.Mix
.Gains
[0].Step
;
1086 for(j
= 0;j
< MaxChannels
;j
++)
1088 ALfloat cur
= maxf(Current
[j
], FLT_EPSILON
);
1089 ALfloat trg
= maxf(Target
[j
], FLT_EPSILON
);
1090 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
1091 Step
[j
] = powf(trg
/cur
, 1.0f
/64.0f
);
1096 src
->Direct
.Counter
= 64;
1099 src
->IsHrtf
= AL_FALSE
;
1100 src
->Dry
.Mix
= SelectDirectMixer();
1102 for(i
= 0;i
< NumSends
;i
++)
1104 src
->Send
[i
].Gain
.Target
= WetGain
[i
];
1105 if(!src
->Send
[i
].Moving
)
1107 src
->Send
[i
].Gain
.Current
= src
->Send
[i
].Gain
.Target
;
1108 src
->Send
[i
].Gain
.Step
= 1.0f
;
1109 src
->Send
[i
].Counter
= 0;
1110 src
->Send
[i
].Moving
= AL_TRUE
;
1114 ALfloat cur
= maxf(src
->Send
[i
].Gain
.Current
, FLT_EPSILON
);
1115 ALfloat trg
= maxf(src
->Send
[i
].Gain
.Target
, FLT_EPSILON
);
1116 if(fabs(trg
- cur
) >= GAIN_SILENCE_THRESHOLD
)
1117 src
->Send
[i
].Gain
.Step
= powf(trg
/cur
, 1.0f
/64.0f
);
1119 src
->Send
[i
].Gain
.Step
= 1.0f
;
1120 src
->Send
[i
].Gain
.Current
= cur
;
1121 src
->Send
[i
].Counter
= 64;
1124 src
->WetMix
= SelectSendMixer();
1127 ALfloat gainhf
= maxf(0.01f
, DryGainHF
);
1128 ALfloat gainlf
= maxf(0.01f
, DryGainLF
);
1129 ALfloat hfscale
= ALSource
->Direct
.HFReference
/ Frequency
;
1130 ALfloat lfscale
= ALSource
->Direct
.LFReference
/ Frequency
;
1131 src
->Direct
.Filters
[0].ActiveType
= AF_None
;
1132 if(gainhf
!= 1.0f
) src
->Direct
.Filters
[0].ActiveType
|= AF_LowPass
;
1133 if(gainlf
!= 1.0f
) src
->Direct
.Filters
[0].ActiveType
|= AF_HighPass
;
1134 ALfilterState_setParams(
1135 &src
->Direct
.Filters
[0].LowPass
, ALfilterType_HighShelf
, gainhf
,
1138 ALfilterState_setParams(
1139 &src
->Direct
.Filters
[0].HighPass
, ALfilterType_LowShelf
, gainlf
,
1143 for(i
= 0;i
< NumSends
;i
++)
1145 ALfloat gainhf
= maxf(0.01f
, WetGainHF
[i
]);
1146 ALfloat gainlf
= maxf(0.01f
, WetGainLF
[i
]);
1147 ALfloat hfscale
= ALSource
->Send
[i
].HFReference
/ Frequency
;
1148 ALfloat lfscale
= ALSource
->Send
[i
].LFReference
/ Frequency
;
1149 src
->Send
[i
].Filters
[0].ActiveType
= AF_None
;
1150 if(gainhf
!= 1.0f
) src
->Send
[i
].Filters
[0].ActiveType
|= AF_LowPass
;
1151 if(gainlf
!= 1.0f
) src
->Send
[i
].Filters
[0].ActiveType
|= AF_HighPass
;
1152 ALfilterState_setParams(
1153 &src
->Send
[i
].Filters
[0].LowPass
, ALfilterType_HighShelf
, gainhf
,
1156 ALfilterState_setParams(
1157 &src
->Send
[i
].Filters
[0].HighPass
, ALfilterType_LowShelf
, gainlf
,
1164 static inline ALint
aluF2I25(ALfloat val
)
1166 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1167 if(fabsf(val
) > 1.0f
)
1168 val
= (ALfloat
)((0.0f
< val
) - (val
< 0.0f
));
1169 /* Convert to a signed integer, between -16777215 and +16777215. */
1170 return fastf2i(val
*16777215.0f
);
1173 static inline ALfloat
aluF2F(ALfloat val
)
1175 static inline ALint
aluF2I(ALfloat val
)
1176 { return aluF2I25(val
)<<7; }
1177 static inline ALuint
aluF2UI(ALfloat val
)
1178 { return aluF2I(val
)+2147483648u; }
1179 static inline ALshort
aluF2S(ALfloat val
)
1180 { return aluF2I25(val
)>>9; }
1181 static inline ALushort
aluF2US(ALfloat val
)
1182 { return aluF2S(val
)+32768; }
1183 static inline ALbyte
aluF2B(ALfloat val
)
1184 { return aluF2I25(val
)>>17; }
1185 static inline ALubyte
aluF2UB(ALfloat val
)
1186 { return aluF2B(val
)+128; }
1188 #define DECL_TEMPLATE(T, func) \
1189 static void Write_##T(ALCdevice *device, ALvoid **buffer, ALuint SamplesToDo) \
1191 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
1192 const ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
1193 const ALuint *offsets = device->ChannelOffsets; \
1196 for(j = 0;j < MaxChannels;j++) \
1200 if(offsets[j] == INVALID_OFFSET) \
1203 out = (T*)(*buffer) + offsets[j]; \
1204 for(i = 0;i < SamplesToDo;i++) \
1205 out[i*numchans] = func(DryBuffer[j][i]); \
1207 *buffer = (char*)(*buffer) + SamplesToDo*numchans*sizeof(T); \
1210 DECL_TEMPLATE(ALfloat
, aluF2F
)
1211 DECL_TEMPLATE(ALuint
, aluF2UI
)
1212 DECL_TEMPLATE(ALint
, aluF2I
)
1213 DECL_TEMPLATE(ALushort
, aluF2US
)
1214 DECL_TEMPLATE(ALshort
, aluF2S
)
1215 DECL_TEMPLATE(ALubyte
, aluF2UB
)
1216 DECL_TEMPLATE(ALbyte
, aluF2B
)
1218 #undef DECL_TEMPLATE
1221 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1224 ALeffectslot
**slot
, **slot_end
;
1225 ALactivesource
**src
, **src_end
;
1230 SetMixerFPUMode(&oldMode
);
1234 IncrementRef(&device
->MixCount
);
1236 SamplesToDo
= minu(size
, BUFFERSIZE
);
1237 for(c
= 0;c
< MaxChannels
;c
++)
1238 memset(device
->DryBuffer
[c
], 0, SamplesToDo
*sizeof(ALfloat
));
1240 ALCdevice_Lock(device
);
1241 V(device
->Synth
,process
)(SamplesToDo
, device
->DryBuffer
);
1243 ctx
= device
->ContextList
;
1246 ALenum DeferUpdates
= ctx
->DeferUpdates
;
1247 ALenum UpdateSources
= AL_FALSE
;
1250 UpdateSources
= ExchangeInt(&ctx
->UpdateSources
, AL_FALSE
);
1253 CalcListenerParams(ctx
->Listener
);
1255 /* source processing */
1256 src
= ctx
->ActiveSources
;
1257 src_end
= src
+ ctx
->ActiveSourceCount
;
1258 while(src
!= src_end
)
1260 ALsource
*source
= (*src
)->Source
;
1262 if(source
->state
!= AL_PLAYING
&& source
->state
!= AL_PAUSED
)
1264 ALactivesource
*temp
= *(--src_end
);
1267 --(ctx
->ActiveSourceCount
);
1271 if(!DeferUpdates
&& (ExchangeInt(&source
->NeedsUpdate
, AL_FALSE
) ||
1273 (*src
)->Update(*src
, ctx
);
1275 if(source
->state
!= AL_PAUSED
)
1276 MixSource(*src
, device
, SamplesToDo
);
1280 /* effect slot processing */
1281 slot
= VECTOR_ITER_BEGIN(ctx
->ActiveAuxSlots
);
1282 slot_end
= VECTOR_ITER_END(ctx
->ActiveAuxSlots
);
1283 while(slot
!= slot_end
)
1285 if(!DeferUpdates
&& ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1286 V((*slot
)->EffectState
,update
)(device
, *slot
);
1288 V((*slot
)->EffectState
,process
)(SamplesToDo
, (*slot
)->WetBuffer
[0],
1291 for(i
= 0;i
< SamplesToDo
;i
++)
1292 (*slot
)->WetBuffer
[0][i
] = 0.0f
;
1300 slot
= &device
->DefaultSlot
;
1303 if(ExchangeInt(&(*slot
)->NeedsUpdate
, AL_FALSE
))
1304 V((*slot
)->EffectState
,update
)(device
, *slot
);
1306 V((*slot
)->EffectState
,process
)(SamplesToDo
, (*slot
)->WetBuffer
[0],
1309 for(i
= 0;i
< SamplesToDo
;i
++)
1310 (*slot
)->WetBuffer
[0][i
] = 0.0f
;
1313 /* Increment the clock time. Every second's worth of samples is
1314 * converted and added to clock base so that large sample counts don't
1315 * overflow during conversion. This also guarantees an exact, stable
1317 device
->SamplesDone
+= SamplesToDo
;
1318 device
->ClockBase
+= (device
->SamplesDone
/device
->Frequency
) * DEVICE_CLOCK_RES
;
1319 device
->SamplesDone
%= device
->Frequency
;
1320 ALCdevice_Unlock(device
);
1324 /* Apply binaural/crossfeed filter */
1325 for(i
= 0;i
< SamplesToDo
;i
++)
1328 samples
[0] = device
->DryBuffer
[FrontLeft
][i
];
1329 samples
[1] = device
->DryBuffer
[FrontRight
][i
];
1330 bs2b_cross_feed(device
->Bs2b
, samples
);
1331 device
->DryBuffer
[FrontLeft
][i
] = samples
[0];
1332 device
->DryBuffer
[FrontRight
][i
] = samples
[1];
1338 switch(device
->FmtType
)
1341 Write_ALbyte(device
, &buffer
, SamplesToDo
);
1344 Write_ALubyte(device
, &buffer
, SamplesToDo
);
1347 Write_ALshort(device
, &buffer
, SamplesToDo
);
1350 Write_ALushort(device
, &buffer
, SamplesToDo
);
1353 Write_ALint(device
, &buffer
, SamplesToDo
);
1356 Write_ALuint(device
, &buffer
, SamplesToDo
);
1359 Write_ALfloat(device
, &buffer
, SamplesToDo
);
1364 size
-= SamplesToDo
;
1365 IncrementRef(&device
->MixCount
);
1368 RestoreFPUMode(&oldMode
);
1372 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1374 ALCcontext
*Context
;
1376 device
->Connected
= ALC_FALSE
;
1378 Context
= device
->ContextList
;
1381 ALactivesource
**src
, **src_end
;
1383 src
= Context
->ActiveSources
;
1384 src_end
= src
+ Context
->ActiveSourceCount
;
1385 while(src
!= src_end
)
1387 ALsource
*source
= (*src
)->Source
;
1388 if(source
->state
== AL_PLAYING
)
1390 source
->state
= AL_STOPPED
;
1391 source
->current_buffer
= NULL
;
1392 source
->position
= 0;
1393 source
->position_fraction
= 0;
1397 Context
->ActiveSourceCount
= 0;
1399 Context
= Context
->next
;