Include strings.h if it exists for strncasecmp
[openal-soft.git] / Alc / ALu.c
blob82932cf38ccadcbab550a50056d99bcd59eb0d64
1 /**
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
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <assert.h>
29 #include "alMain.h"
30 #include "alSource.h"
31 #include "alBuffer.h"
32 #include "alListener.h"
33 #include "alAuxEffectSlot.h"
34 #include "alu.h"
35 #include "bs2b.h"
36 #include "hrtf.h"
38 #include "mixer_defs.h"
40 #include "midi/base.h"
43 struct ChanMap {
44 enum Channel channel;
45 ALfloat angle;
48 /* Cone scalar */
49 ALfloat ConeScale = 1.0f;
51 /* Localized Z scalar for mono sources */
52 ALfloat ZScale = 1.0f;
54 extern inline ALfloat minf(ALfloat a, ALfloat b);
55 extern inline ALfloat maxf(ALfloat a, ALfloat b);
56 extern inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max);
58 extern inline ALdouble mind(ALdouble a, ALdouble b);
59 extern inline ALdouble maxd(ALdouble a, ALdouble b);
60 extern inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max);
62 extern inline ALuint minu(ALuint a, ALuint b);
63 extern inline ALuint maxu(ALuint a, ALuint b);
64 extern inline ALuint clampu(ALuint val, ALuint min, ALuint max);
66 extern inline ALint mini(ALint a, ALint b);
67 extern inline ALint maxi(ALint a, ALint b);
68 extern inline ALint clampi(ALint val, ALint min, ALint max);
70 extern inline ALint64 mini64(ALint64 a, ALint64 b);
71 extern inline ALint64 maxi64(ALint64 a, ALint64 b);
72 extern inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max);
74 extern inline ALuint64 minu64(ALuint64 a, ALuint64 b);
75 extern inline ALuint64 maxu64(ALuint64 a, ALuint64 b);
76 extern inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max);
78 extern inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu);
79 extern inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu);
81 static ResamplerFunc SelectResampler(enum Resampler Resampler, ALuint increment)
83 if(increment == FRACTIONONE)
84 return Resample_copy32_C;
85 switch(Resampler)
87 case PointResampler:
88 return Resample_point32_C;
89 case LinearResampler:
90 return Resample_lerp32_C;
91 case CubicResampler:
92 return Resample_cubic32_C;
93 case ResamplerMax:
94 /* Shouldn't happen */
95 break;
98 return Resample_point32_C;
102 static HrtfMixerFunc SelectHrtfMixer(void)
104 #ifdef HAVE_SSE
105 if((CPUCapFlags&CPU_CAP_SSE))
106 return MixDirect_Hrtf_SSE;
107 #endif
108 #ifdef HAVE_NEON
109 if((CPUCapFlags&CPU_CAP_NEON))
110 return MixDirect_Hrtf_Neon;
111 #endif
113 return MixDirect_Hrtf_C;
116 static DryMixerFunc SelectDirectMixer(void)
118 #ifdef HAVE_SSE
119 if((CPUCapFlags&CPU_CAP_SSE))
120 return MixDirect_SSE;
121 #endif
122 #ifdef HAVE_NEON
123 if((CPUCapFlags&CPU_CAP_NEON))
124 return MixDirect_Neon;
125 #endif
127 return MixDirect_C;
130 static WetMixerFunc SelectSendMixer(void)
132 #ifdef HAVE_SSE
133 if((CPUCapFlags&CPU_CAP_SSE))
134 return MixSend_SSE;
135 #endif
136 #ifdef HAVE_NEON
137 if((CPUCapFlags&CPU_CAP_NEON))
138 return MixSend_Neon;
139 #endif
141 return MixSend_C;
145 static inline void aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
147 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
148 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
149 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
152 static inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
154 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
155 inVector1[2]*inVector2[2];
158 static inline void aluNormalize(ALfloat *inVector)
160 ALfloat lengthsqr = aluDotproduct(inVector, inVector);
161 if(lengthsqr > 0.0f)
163 ALfloat inv_length = 1.0f/sqrtf(lengthsqr);
164 inVector[0] *= inv_length;
165 inVector[1] *= inv_length;
166 inVector[2] *= inv_length;
170 static inline ALvoid aluMatrixVector(ALfloat *vector, ALfloat w, ALfloat (*restrict matrix)[4])
172 ALfloat temp[4] = {
173 vector[0], vector[1], vector[2], w
176 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
177 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
178 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
182 static ALvoid CalcListenerParams(ALlistener *Listener)
184 ALfloat N[3], V[3], U[3], P[3];
186 /* AT then UP */
187 N[0] = Listener->Forward[0];
188 N[1] = Listener->Forward[1];
189 N[2] = Listener->Forward[2];
190 aluNormalize(N);
191 V[0] = Listener->Up[0];
192 V[1] = Listener->Up[1];
193 V[2] = Listener->Up[2];
194 aluNormalize(V);
195 /* Build and normalize right-vector */
196 aluCrossproduct(N, V, U);
197 aluNormalize(U);
199 Listener->Params.Matrix[0][0] = U[0];
200 Listener->Params.Matrix[0][1] = V[0];
201 Listener->Params.Matrix[0][2] = -N[0];
202 Listener->Params.Matrix[0][3] = 0.0f;
203 Listener->Params.Matrix[1][0] = U[1];
204 Listener->Params.Matrix[1][1] = V[1];
205 Listener->Params.Matrix[1][2] = -N[1];
206 Listener->Params.Matrix[1][3] = 0.0f;
207 Listener->Params.Matrix[2][0] = U[2];
208 Listener->Params.Matrix[2][1] = V[2];
209 Listener->Params.Matrix[2][2] = -N[2];
210 Listener->Params.Matrix[2][3] = 0.0f;
211 Listener->Params.Matrix[3][0] = 0.0f;
212 Listener->Params.Matrix[3][1] = 0.0f;
213 Listener->Params.Matrix[3][2] = 0.0f;
214 Listener->Params.Matrix[3][3] = 1.0f;
216 P[0] = Listener->Position[0];
217 P[1] = Listener->Position[1];
218 P[2] = Listener->Position[2];
219 aluMatrixVector(P, 1.0f, Listener->Params.Matrix);
220 Listener->Params.Matrix[3][0] = -P[0];
221 Listener->Params.Matrix[3][1] = -P[1];
222 Listener->Params.Matrix[3][2] = -P[2];
224 Listener->Params.Velocity[0] = Listener->Velocity[0];
225 Listener->Params.Velocity[1] = Listener->Velocity[1];
226 Listener->Params.Velocity[2] = Listener->Velocity[2];
227 aluMatrixVector(Listener->Params.Velocity, 0.0f, Listener->Params.Matrix);
230 ALvoid CalcNonAttnSourceParams(ALactivesource *src, const ALCcontext *ALContext)
232 static const struct ChanMap MonoMap[1] = { { FrontCenter, 0.0f } };
233 static const struct ChanMap StereoMap[2] = {
234 { FrontLeft, DEG2RAD(-30.0f) },
235 { FrontRight, DEG2RAD( 30.0f) }
237 static const struct ChanMap StereoWideMap[2] = {
238 { FrontLeft, DEG2RAD(-90.0f) },
239 { FrontRight, DEG2RAD( 90.0f) }
241 static const struct ChanMap RearMap[2] = {
242 { BackLeft, DEG2RAD(-150.0f) },
243 { BackRight, DEG2RAD( 150.0f) }
245 static const struct ChanMap QuadMap[4] = {
246 { FrontLeft, DEG2RAD( -45.0f) },
247 { FrontRight, DEG2RAD( 45.0f) },
248 { BackLeft, DEG2RAD(-135.0f) },
249 { BackRight, DEG2RAD( 135.0f) }
251 static const struct ChanMap X51Map[6] = {
252 { FrontLeft, DEG2RAD( -30.0f) },
253 { FrontRight, DEG2RAD( 30.0f) },
254 { FrontCenter, DEG2RAD( 0.0f) },
255 { LFE, 0.0f },
256 { BackLeft, DEG2RAD(-110.0f) },
257 { BackRight, DEG2RAD( 110.0f) }
259 static const struct ChanMap X61Map[7] = {
260 { FrontLeft, DEG2RAD(-30.0f) },
261 { FrontRight, DEG2RAD( 30.0f) },
262 { FrontCenter, DEG2RAD( 0.0f) },
263 { LFE, 0.0f },
264 { BackCenter, DEG2RAD(180.0f) },
265 { SideLeft, DEG2RAD(-90.0f) },
266 { SideRight, DEG2RAD( 90.0f) }
268 static const struct ChanMap X71Map[8] = {
269 { FrontLeft, DEG2RAD( -30.0f) },
270 { FrontRight, DEG2RAD( 30.0f) },
271 { FrontCenter, DEG2RAD( 0.0f) },
272 { LFE, 0.0f },
273 { BackLeft, DEG2RAD(-150.0f) },
274 { BackRight, DEG2RAD( 150.0f) },
275 { SideLeft, DEG2RAD( -90.0f) },
276 { SideRight, DEG2RAD( 90.0f) }
279 ALCdevice *Device = ALContext->Device;
280 const ALsource *ALSource = src->Source;
281 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
282 ALbufferlistitem *BufferListItem;
283 enum FmtChannels Channels;
284 ALfloat DryGain, DryGainHF, DryGainLF;
285 ALfloat WetGain[MAX_SENDS];
286 ALfloat WetGainHF[MAX_SENDS];
287 ALfloat WetGainLF[MAX_SENDS];
288 ALint NumSends, Frequency;
289 const struct ChanMap *chans = NULL;
290 enum Resampler Resampler;
291 ALint num_channels = 0;
292 ALboolean DirectChannels;
293 ALfloat hwidth = 0.0f;
294 ALfloat Pitch;
295 ALint i, j, c;
297 /* Get device properties */
298 NumSends = Device->NumAuxSends;
299 Frequency = Device->Frequency;
301 /* Get listener properties */
302 ListenerGain = ALContext->Listener->Gain;
304 /* Get source properties */
305 SourceVolume = ALSource->Gain;
306 MinVolume = ALSource->MinGain;
307 MaxVolume = ALSource->MaxGain;
308 Pitch = ALSource->Pitch;
309 Resampler = ALSource->Resampler;
310 DirectChannels = ALSource->DirectChannels;
312 src->Direct.OutBuffer = Device->DryBuffer;
313 for(i = 0;i < NumSends;i++)
315 ALeffectslot *Slot = ALSource->Send[i].Slot;
316 if(!Slot && i == 0)
317 Slot = Device->DefaultSlot;
318 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
319 src->Send[i].OutBuffer = NULL;
320 else
321 src->Send[i].OutBuffer = Slot->WetBuffer;
324 /* Calculate the stepping value */
325 Channels = FmtMono;
326 BufferListItem = ALSource->queue;
327 while(BufferListItem != NULL)
329 ALbuffer *ALBuffer;
330 if((ALBuffer=BufferListItem->buffer) != NULL)
332 Pitch = Pitch * ALBuffer->Frequency / Frequency;
333 if(Pitch > 10.0f)
334 src->Step = 10<<FRACTIONBITS;
335 else
337 src->Step = fastf2i(Pitch*FRACTIONONE);
338 if(src->Step == 0)
339 src->Step = 1;
341 src->Resample = SelectResampler(Resampler, src->Step);
343 Channels = ALBuffer->FmtChannels;
344 break;
346 BufferListItem = BufferListItem->next;
349 /* Calculate gains */
350 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
351 DryGain *= ALSource->Direct.Gain * ListenerGain;
352 DryGainHF = ALSource->Direct.GainHF;
353 DryGainLF = ALSource->Direct.GainLF;
354 for(i = 0;i < NumSends;i++)
356 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
357 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
358 WetGainHF[i] = ALSource->Send[i].GainHF;
359 WetGainLF[i] = ALSource->Send[i].GainLF;
362 switch(Channels)
364 case FmtMono:
365 chans = MonoMap;
366 num_channels = 1;
367 break;
369 case FmtStereo:
370 if(!(Device->Flags&DEVICE_WIDE_STEREO))
372 /* HACK: Place the stereo channels at +/-90 degrees when using non-
373 * HRTF stereo output. This helps reduce the "monoization" caused
374 * by them panning towards the center. */
375 if(Device->FmtChans == DevFmtStereo && !Device->Hrtf)
376 chans = StereoWideMap;
377 else
378 chans = StereoMap;
380 else
382 chans = StereoWideMap;
383 hwidth = DEG2RAD(60.0f);
385 num_channels = 2;
386 break;
388 case FmtRear:
389 chans = RearMap;
390 num_channels = 2;
391 break;
393 case FmtQuad:
394 chans = QuadMap;
395 num_channels = 4;
396 break;
398 case FmtX51:
399 chans = X51Map;
400 num_channels = 6;
401 break;
403 case FmtX61:
404 chans = X61Map;
405 num_channels = 7;
406 break;
408 case FmtX71:
409 chans = X71Map;
410 num_channels = 8;
411 break;
414 if(DirectChannels != AL_FALSE)
416 for(c = 0;c < num_channels;c++)
418 ALfloat *restrict Target = src->Direct.Mix.Gains[c].Target;
419 for(j = 0;j < MaxChannels;j++)
420 Target[j] = 0.0f;
423 for(c = 0;c < num_channels;c++)
425 ALfloat *restrict Target = src->Direct.Mix.Gains[c].Target;
426 for(i = 0;i < (ALint)Device->NumChan;i++)
428 enum Channel chan = Device->Speaker2Chan[i];
429 if(chan == chans[c].channel)
431 Target[chan] = DryGain;
432 break;
437 if(!src->Direct.Moving)
439 for(i = 0;i < num_channels;i++)
441 ALfloat *restrict Current = src->Direct.Mix.Gains[i].Current;
442 ALfloat *restrict Step = src->Direct.Mix.Gains[i].Step;
443 ALfloat *restrict Target = src->Direct.Mix.Gains[i].Target;
444 for(j = 0;j < MaxChannels;j++)
446 Current[j] = Target[j];
447 Step[j] = 1.0f;
450 src->Direct.Counter = 0;
451 src->Direct.Moving = AL_TRUE;
453 else
455 for(i = 0;i < num_channels;i++)
457 ALfloat *restrict Current = src->Direct.Mix.Gains[i].Current;
458 ALfloat *restrict Step = src->Direct.Mix.Gains[i].Step;
459 ALfloat *restrict Target = src->Direct.Mix.Gains[i].Target;
460 for(j = 0;j < MaxChannels;j++)
462 ALfloat cur = maxf(Current[j], FLT_EPSILON);
463 ALfloat trg = maxf(Target[j], FLT_EPSILON);
464 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
465 Step[j] = powf(trg/cur, 1.0f/64.0f);
466 else
467 Step[j] = 1.0f;
468 Current[j] = cur;
471 src->Direct.Counter = 64;
474 src->IsHrtf = AL_FALSE;
475 src->Dry.Mix = SelectDirectMixer();
477 else if(Device->Hrtf)
479 for(c = 0;c < num_channels;c++)
481 if(chans[c].channel == LFE)
483 /* Skip LFE */
484 src->Direct.Mix.Hrtf.Params[c].Delay[0] = 0;
485 src->Direct.Mix.Hrtf.Params[c].Delay[1] = 0;
486 for(i = 0;i < HRIR_LENGTH;i++)
488 src->Direct.Mix.Hrtf.Params[c].Coeffs[i][0] = 0.0f;
489 src->Direct.Mix.Hrtf.Params[c].Coeffs[i][1] = 0.0f;
492 else
494 /* Get the static HRIR coefficients and delays for this
495 * channel. */
496 GetLerpedHrtfCoeffs(Device->Hrtf,
497 0.0f, chans[c].angle, DryGain,
498 src->Direct.Mix.Hrtf.Params[c].Coeffs,
499 src->Direct.Mix.Hrtf.Params[c].Delay);
502 src->Direct.Counter = 0;
503 src->Direct.Moving = AL_TRUE;
504 src->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
506 src->IsHrtf = AL_TRUE;
507 src->Dry.HrtfMix = SelectHrtfMixer();
509 else
511 for(i = 0;i < num_channels;i++)
513 ALfloat *restrict Target = src->Direct.Mix.Gains[i].Target;
514 for(j = 0;j < MaxChannels;j++)
515 Target[j] = 0.0f;
518 DryGain *= lerp(1.0f, 1.0f/sqrtf((float)Device->NumChan), hwidth/F_PI);
519 for(c = 0;c < num_channels;c++)
521 ALfloat *restrict Target = src->Direct.Mix.Gains[c].Target;
522 /* Special-case LFE */
523 if(chans[c].channel == LFE)
525 Target[chans[c].channel] = DryGain;
526 continue;
528 ComputeAngleGains(Device, chans[c].angle, hwidth, DryGain, Target);
531 if(!src->Direct.Moving)
533 for(i = 0;i < num_channels;i++)
535 ALfloat *restrict Current = src->Direct.Mix.Gains[i].Current;
536 ALfloat *restrict Step = src->Direct.Mix.Gains[i].Step;
537 ALfloat *restrict Target = src->Direct.Mix.Gains[i].Target;
538 for(j = 0;j < MaxChannels;j++)
540 Current[j] = Target[j];
541 Step[j] = 1.0f;
544 src->Direct.Counter = 0;
545 src->Direct.Moving = AL_TRUE;
547 else
549 for(i = 0;i < num_channels;i++)
551 ALfloat *restrict Current = src->Direct.Mix.Gains[i].Current;
552 ALfloat *restrict Step = src->Direct.Mix.Gains[i].Step;
553 ALfloat *restrict Target = src->Direct.Mix.Gains[i].Target;
554 for(j = 0;j < MaxChannels;j++)
556 ALfloat trg = maxf(Target[j], FLT_EPSILON);
557 ALfloat cur = maxf(Current[j], FLT_EPSILON);
558 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
559 Step[j] = powf(trg/cur, 1.0f/64.0f);
560 else
561 Step[j] = 1.0f;
562 Current[j] = cur;
565 src->Direct.Counter = 64;
568 src->IsHrtf = AL_FALSE;
569 src->Dry.Mix = SelectDirectMixer();
571 for(i = 0;i < NumSends;i++)
573 src->Send[i].Gain.Target = WetGain[i];
574 if(!src->Send[i].Moving)
576 src->Send[i].Gain.Current = src->Send[i].Gain.Target;
577 src->Send[i].Gain.Step = 1.0f;
578 src->Send[i].Counter = 0;
579 src->Send[i].Moving = AL_TRUE;
581 else
583 ALfloat cur = maxf(src->Send[i].Gain.Current, FLT_EPSILON);
584 ALfloat trg = maxf(src->Send[i].Gain.Target, FLT_EPSILON);
585 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
586 src->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
587 else
588 src->Send[i].Gain.Step = 1.0f;
589 src->Send[i].Gain.Current = cur;
590 src->Send[i].Counter = 64;
593 src->WetMix = SelectSendMixer();
596 ALfloat gainhf = maxf(0.01f, DryGainHF);
597 ALfloat gainlf = maxf(0.01f, DryGainLF);
598 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
599 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
600 for(c = 0;c < num_channels;c++)
602 src->Direct.Filters[c].ActiveType = AF_None;
603 if(gainhf != 1.0f) src->Direct.Filters[c].ActiveType |= AF_LowPass;
604 if(gainlf != 1.0f) src->Direct.Filters[c].ActiveType |= AF_HighPass;
605 ALfilterState_setParams(
606 &src->Direct.Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
607 hfscale, 0.0f
609 ALfilterState_setParams(
610 &src->Direct.Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
611 lfscale, 0.0f
615 for(i = 0;i < NumSends;i++)
617 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
618 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
619 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
620 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
621 for(c = 0;c < num_channels;c++)
623 src->Send[i].Filters[c].ActiveType = AF_None;
624 if(gainhf != 1.0f) src->Send[i].Filters[c].ActiveType |= AF_LowPass;
625 if(gainlf != 1.0f) src->Send[i].Filters[c].ActiveType |= AF_HighPass;
626 ALfilterState_setParams(
627 &src->Send[i].Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
628 hfscale, 0.0f
630 ALfilterState_setParams(
631 &src->Send[i].Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
632 lfscale, 0.0f
638 ALvoid CalcSourceParams(ALactivesource *src, const ALCcontext *ALContext)
640 ALCdevice *Device = ALContext->Device;
641 const ALsource *ALSource = src->Source;
642 ALfloat Velocity[3],Direction[3],Position[3],SourceToListener[3];
643 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
644 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
645 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
646 ALfloat DopplerFactor, SpeedOfSound;
647 ALfloat AirAbsorptionFactor;
648 ALfloat RoomAirAbsorption[MAX_SENDS];
649 ALbufferlistitem *BufferListItem;
650 ALfloat Attenuation;
651 ALfloat RoomAttenuation[MAX_SENDS];
652 ALfloat MetersPerUnit;
653 ALfloat RoomRolloffBase;
654 ALfloat RoomRolloff[MAX_SENDS];
655 ALfloat DecayDistance[MAX_SENDS];
656 ALfloat DryGain;
657 ALfloat DryGainHF;
658 ALfloat DryGainLF;
659 ALboolean DryGainHFAuto;
660 ALfloat WetGain[MAX_SENDS];
661 ALfloat WetGainHF[MAX_SENDS];
662 ALfloat WetGainLF[MAX_SENDS];
663 ALboolean WetGainAuto;
664 ALboolean WetGainHFAuto;
665 enum Resampler Resampler;
666 ALfloat Pitch;
667 ALuint Frequency;
668 ALint NumSends;
669 ALint i, j;
671 DryGainHF = 1.0f;
672 DryGainLF = 1.0f;
673 for(i = 0;i < MAX_SENDS;i++)
675 WetGainHF[i] = 1.0f;
676 WetGainLF[i] = 1.0f;
679 /* Get context/device properties */
680 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
681 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
682 NumSends = Device->NumAuxSends;
683 Frequency = Device->Frequency;
685 /* Get listener properties */
686 ListenerGain = ALContext->Listener->Gain;
687 MetersPerUnit = ALContext->Listener->MetersPerUnit;
689 /* Get source properties */
690 SourceVolume = ALSource->Gain;
691 MinVolume = ALSource->MinGain;
692 MaxVolume = ALSource->MaxGain;
693 Pitch = ALSource->Pitch;
694 Resampler = ALSource->Resampler;
695 Position[0] = ALSource->Position[0];
696 Position[1] = ALSource->Position[1];
697 Position[2] = ALSource->Position[2];
698 Direction[0] = ALSource->Orientation[0];
699 Direction[1] = ALSource->Orientation[1];
700 Direction[2] = ALSource->Orientation[2];
701 Velocity[0] = ALSource->Velocity[0];
702 Velocity[1] = ALSource->Velocity[1];
703 Velocity[2] = ALSource->Velocity[2];
704 MinDist = ALSource->RefDistance;
705 MaxDist = ALSource->MaxDistance;
706 Rolloff = ALSource->RollOffFactor;
707 InnerAngle = ALSource->InnerAngle;
708 OuterAngle = ALSource->OuterAngle;
709 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
710 DryGainHFAuto = ALSource->DryGainHFAuto;
711 WetGainAuto = ALSource->WetGainAuto;
712 WetGainHFAuto = ALSource->WetGainHFAuto;
713 RoomRolloffBase = ALSource->RoomRolloffFactor;
715 src->Direct.OutBuffer = Device->DryBuffer;
716 for(i = 0;i < NumSends;i++)
718 ALeffectslot *Slot = ALSource->Send[i].Slot;
720 if(!Slot && i == 0)
721 Slot = Device->DefaultSlot;
722 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
724 Slot = NULL;
725 RoomRolloff[i] = 0.0f;
726 DecayDistance[i] = 0.0f;
727 RoomAirAbsorption[i] = 1.0f;
729 else if(Slot->AuxSendAuto)
731 RoomRolloff[i] = RoomRolloffBase;
732 if(IsReverbEffect(Slot->EffectType))
734 RoomRolloff[i] += Slot->EffectProps.Reverb.RoomRolloffFactor;
735 DecayDistance[i] = Slot->EffectProps.Reverb.DecayTime *
736 SPEEDOFSOUNDMETRESPERSEC;
737 RoomAirAbsorption[i] = Slot->EffectProps.Reverb.AirAbsorptionGainHF;
739 else
741 DecayDistance[i] = 0.0f;
742 RoomAirAbsorption[i] = 1.0f;
745 else
747 /* If the slot's auxiliary send auto is off, the data sent to the
748 * effect slot is the same as the dry path, sans filter effects */
749 RoomRolloff[i] = Rolloff;
750 DecayDistance[i] = 0.0f;
751 RoomAirAbsorption[i] = AIRABSORBGAINHF;
754 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
755 src->Send[i].OutBuffer = NULL;
756 else
757 src->Send[i].OutBuffer = Slot->WetBuffer;
760 /* Transform source to listener space (convert to head relative) */
761 if(ALSource->HeadRelative == AL_FALSE)
763 ALfloat (*restrict Matrix)[4] = ALContext->Listener->Params.Matrix;
764 /* Transform source vectors */
765 aluMatrixVector(Position, 1.0f, Matrix);
766 aluMatrixVector(Direction, 0.0f, Matrix);
767 aluMatrixVector(Velocity, 0.0f, Matrix);
769 else
771 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
772 /* Offset the source velocity to be relative of the listener velocity */
773 Velocity[0] += ListenerVel[0];
774 Velocity[1] += ListenerVel[1];
775 Velocity[2] += ListenerVel[2];
778 SourceToListener[0] = -Position[0];
779 SourceToListener[1] = -Position[1];
780 SourceToListener[2] = -Position[2];
781 aluNormalize(SourceToListener);
782 aluNormalize(Direction);
784 /* Calculate distance attenuation */
785 Distance = sqrtf(aluDotproduct(Position, Position));
786 ClampedDist = Distance;
788 Attenuation = 1.0f;
789 for(i = 0;i < NumSends;i++)
790 RoomAttenuation[i] = 1.0f;
791 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
792 ALContext->DistanceModel)
794 case InverseDistanceClamped:
795 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
796 if(MaxDist < MinDist)
797 break;
798 /*fall-through*/
799 case InverseDistance:
800 if(MinDist > 0.0f)
802 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
803 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
804 for(i = 0;i < NumSends;i++)
806 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
807 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
810 break;
812 case LinearDistanceClamped:
813 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
814 if(MaxDist < MinDist)
815 break;
816 /*fall-through*/
817 case LinearDistance:
818 if(MaxDist != MinDist)
820 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
821 Attenuation = maxf(Attenuation, 0.0f);
822 for(i = 0;i < NumSends;i++)
824 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
825 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
828 break;
830 case ExponentDistanceClamped:
831 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
832 if(MaxDist < MinDist)
833 break;
834 /*fall-through*/
835 case ExponentDistance:
836 if(ClampedDist > 0.0f && MinDist > 0.0f)
838 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
839 for(i = 0;i < NumSends;i++)
840 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
842 break;
844 case DisableDistance:
845 ClampedDist = MinDist;
846 break;
849 /* Source Gain + Attenuation */
850 DryGain = SourceVolume * Attenuation;
851 for(i = 0;i < NumSends;i++)
852 WetGain[i] = SourceVolume * RoomAttenuation[i];
854 /* Distance-based air absorption */
855 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
857 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
858 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
859 for(i = 0;i < NumSends;i++)
860 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
863 if(WetGainAuto)
865 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
867 /* Apply a decay-time transformation to the wet path, based on the
868 * attenuation of the dry path.
870 * Using the apparent distance, based on the distance attenuation, the
871 * initial decay of the reverb effect is calculated and applied to the
872 * wet path.
874 for(i = 0;i < NumSends;i++)
876 if(DecayDistance[i] > 0.0f)
877 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
881 /* Calculate directional soundcones */
882 Angle = RAD2DEG(acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale) * 2.0f;
883 if(Angle > InnerAngle && Angle <= OuterAngle)
885 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
886 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
887 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
889 else if(Angle > OuterAngle)
891 ConeVolume = ALSource->OuterGain;
892 ConeHF = ALSource->OuterGainHF;
894 else
896 ConeVolume = 1.0f;
897 ConeHF = 1.0f;
900 DryGain *= ConeVolume;
901 if(WetGainAuto)
903 for(i = 0;i < NumSends;i++)
904 WetGain[i] *= ConeVolume;
906 if(DryGainHFAuto)
907 DryGainHF *= ConeHF;
908 if(WetGainHFAuto)
910 for(i = 0;i < NumSends;i++)
911 WetGainHF[i] *= ConeHF;
914 /* Clamp to Min/Max Gain */
915 DryGain = clampf(DryGain, MinVolume, MaxVolume);
916 for(i = 0;i < NumSends;i++)
917 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
919 /* Apply gain and frequency filters */
920 DryGain *= ALSource->Direct.Gain * ListenerGain;
921 DryGainHF *= ALSource->Direct.GainHF;
922 DryGainLF *= ALSource->Direct.GainLF;
923 for(i = 0;i < NumSends;i++)
925 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
926 WetGainHF[i] *= ALSource->Send[i].GainHF;
927 WetGainLF[i] *= ALSource->Send[i].GainLF;
930 /* Calculate velocity-based doppler effect */
931 if(DopplerFactor > 0.0f)
933 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
934 ALfloat VSS, VLS;
936 if(SpeedOfSound < 1.0f)
938 DopplerFactor *= 1.0f/SpeedOfSound;
939 SpeedOfSound = 1.0f;
942 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
943 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
945 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
946 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
949 BufferListItem = ALSource->queue;
950 while(BufferListItem != NULL)
952 ALbuffer *ALBuffer;
953 if((ALBuffer=BufferListItem->buffer) != NULL)
955 /* Calculate fixed-point stepping value, based on the pitch, buffer
956 * frequency, and output frequency. */
957 Pitch = Pitch * ALBuffer->Frequency / Frequency;
958 if(Pitch > 10.0f)
959 src->Step = 10<<FRACTIONBITS;
960 else
962 src->Step = fastf2i(Pitch*FRACTIONONE);
963 if(src->Step == 0)
964 src->Step = 1;
966 src->Resample = SelectResampler(Resampler, src->Step);
968 break;
970 BufferListItem = BufferListItem->next;
973 if(Device->Hrtf)
975 /* Use a binaural HRTF algorithm for stereo headphone playback */
976 ALfloat delta, ev = 0.0f, az = 0.0f;
978 if(Distance > FLT_EPSILON)
980 ALfloat invlen = 1.0f/Distance;
981 Position[0] *= invlen;
982 Position[1] *= invlen;
983 Position[2] *= invlen;
985 /* Calculate elevation and azimuth only when the source is not at
986 * the listener. This prevents +0 and -0 Z from producing
987 * inconsistent panning. Also, clamp Y in case FP precision errors
988 * cause it to land outside of -1..+1. */
989 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
990 az = atan2f(Position[0], -Position[2]*ZScale);
993 /* Check to see if the HRIR is already moving. */
994 if(src->Direct.Moving)
996 /* Calculate the normalized HRTF transition factor (delta). */
997 delta = CalcHrtfDelta(src->Direct.Mix.Hrtf.Gain, DryGain,
998 src->Direct.Mix.Hrtf.Dir, Position);
999 /* If the delta is large enough, get the moving HRIR target
1000 * coefficients, target delays, steppping values, and counter. */
1001 if(delta > 0.001f)
1003 ALuint counter = GetMovingHrtfCoeffs(Device->Hrtf,
1004 ev, az, DryGain, delta,
1005 src->Direct.Counter,
1006 src->Direct.Mix.Hrtf.Params[0].Coeffs,
1007 src->Direct.Mix.Hrtf.Params[0].Delay,
1008 src->Direct.Mix.Hrtf.Params[0].CoeffStep,
1009 src->Direct.Mix.Hrtf.Params[0].DelayStep);
1010 src->Direct.Counter = counter;
1011 src->Direct.Mix.Hrtf.Gain = DryGain;
1012 src->Direct.Mix.Hrtf.Dir[0] = Position[0];
1013 src->Direct.Mix.Hrtf.Dir[1] = Position[1];
1014 src->Direct.Mix.Hrtf.Dir[2] = Position[2];
1017 else
1019 /* Get the initial (static) HRIR coefficients and delays. */
1020 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, DryGain,
1021 src->Direct.Mix.Hrtf.Params[0].Coeffs,
1022 src->Direct.Mix.Hrtf.Params[0].Delay);
1023 src->Direct.Counter = 0;
1024 src->Direct.Moving = AL_TRUE;
1025 src->Direct.Mix.Hrtf.Gain = DryGain;
1026 src->Direct.Mix.Hrtf.Dir[0] = Position[0];
1027 src->Direct.Mix.Hrtf.Dir[1] = Position[1];
1028 src->Direct.Mix.Hrtf.Dir[2] = Position[2];
1030 src->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
1032 src->IsHrtf = AL_TRUE;
1033 src->Dry.HrtfMix = SelectHrtfMixer();
1035 else
1037 ALfloat *restrict Target = src->Direct.Mix.Gains[0].Target;
1038 ALfloat DirGain = 0.0f;
1039 ALfloat AmbientGain;
1041 for(j = 0;j < MaxChannels;j++)
1042 Target[j] = 0.0f;
1044 /* Normalize the length, and compute panned gains. */
1045 if(Distance > FLT_EPSILON)
1047 ALfloat invlen = 1.0f/Distance;
1048 Position[0] *= invlen;
1049 Position[1] *= invlen;
1050 Position[2] *= invlen;
1052 DirGain = sqrtf(Position[0]*Position[0] + Position[2]*Position[2]);
1053 ComputeAngleGains(Device, atan2f(Position[0], -Position[2]*ZScale), 0.0f,
1054 DryGain*DirGain, Target);
1057 /* Adjustment for vertical offsets. Not the greatest, but simple
1058 * enough. */
1059 AmbientGain = DryGain * sqrtf(1.0f/Device->NumChan) * (1.0f-DirGain);
1060 for(i = 0;i < (ALint)Device->NumChan;i++)
1062 enum Channel chan = Device->Speaker2Chan[i];
1063 Target[chan] = maxf(Target[chan], AmbientGain);
1066 if(!src->Direct.Moving)
1068 ALfloat *restrict Current = src->Direct.Mix.Gains[0].Current;
1069 ALfloat *restrict Step = src->Direct.Mix.Gains[0].Step;
1070 for(j = 0;j < MaxChannels;j++)
1072 Current[j] = Target[j];
1073 Step[j] = 1.0f;
1075 src->Direct.Counter = 0;
1076 src->Direct.Moving = AL_TRUE;
1078 else
1080 ALfloat *restrict Current = src->Direct.Mix.Gains[0].Current;
1081 ALfloat *restrict Step = src->Direct.Mix.Gains[0].Step;
1082 for(j = 0;j < MaxChannels;j++)
1084 ALfloat cur = maxf(Current[j], FLT_EPSILON);
1085 ALfloat trg = maxf(Target[j], FLT_EPSILON);
1086 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1087 Step[j] = powf(trg/cur, 1.0f/64.0f);
1088 else
1089 Step[j] = 1.0f;
1090 Current[j] = cur;
1092 src->Direct.Counter = 64;
1095 src->IsHrtf = AL_FALSE;
1096 src->Dry.Mix = SelectDirectMixer();
1098 for(i = 0;i < NumSends;i++)
1100 src->Send[i].Gain.Target = WetGain[i];
1101 if(!src->Send[i].Moving)
1103 src->Send[i].Gain.Current = src->Send[i].Gain.Target;
1104 src->Send[i].Gain.Step = 1.0f;
1105 src->Send[i].Counter = 0;
1106 src->Send[i].Moving = AL_TRUE;
1108 else
1110 ALfloat cur = maxf(src->Send[i].Gain.Current, FLT_EPSILON);
1111 ALfloat trg = maxf(src->Send[i].Gain.Target, FLT_EPSILON);
1112 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1113 src->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
1114 else
1115 src->Send[i].Gain.Step = 1.0f;
1116 src->Send[i].Gain.Current = cur;
1117 src->Send[i].Counter = 64;
1120 src->WetMix = SelectSendMixer();
1123 ALfloat gainhf = maxf(0.01f, DryGainHF);
1124 ALfloat gainlf = maxf(0.01f, DryGainLF);
1125 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
1126 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
1127 src->Direct.Filters[0].ActiveType = AF_None;
1128 if(gainhf != 1.0f) src->Direct.Filters[0].ActiveType |= AF_LowPass;
1129 if(gainlf != 1.0f) src->Direct.Filters[0].ActiveType |= AF_HighPass;
1130 ALfilterState_setParams(
1131 &src->Direct.Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
1132 hfscale, 0.0f
1134 ALfilterState_setParams(
1135 &src->Direct.Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1136 lfscale, 0.0f
1139 for(i = 0;i < NumSends;i++)
1141 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
1142 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
1143 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
1144 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
1145 src->Send[i].Filters[0].ActiveType = AF_None;
1146 if(gainhf != 1.0f) src->Send[i].Filters[0].ActiveType |= AF_LowPass;
1147 if(gainlf != 1.0f) src->Send[i].Filters[0].ActiveType |= AF_HighPass;
1148 ALfilterState_setParams(
1149 &src->Send[i].Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
1150 hfscale, 0.0f
1152 ALfilterState_setParams(
1153 &src->Send[i].Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1154 lfscale, 0.0f
1160 static inline ALint aluF2I25(ALfloat val)
1162 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1163 if(fabsf(val) > 1.0f)
1164 val = (ALfloat)((0.0f < val) - (val < 0.0f));
1165 /* Convert to a signed integer, between -16777215 and +16777215. */
1166 return fastf2i(val*16777215.0f);
1169 static inline ALfloat aluF2F(ALfloat val)
1170 { return val; }
1171 static inline ALint aluF2I(ALfloat val)
1172 { return aluF2I25(val)<<7; }
1173 static inline ALuint aluF2UI(ALfloat val)
1174 { return aluF2I(val)+2147483648u; }
1175 static inline ALshort aluF2S(ALfloat val)
1176 { return aluF2I25(val)>>9; }
1177 static inline ALushort aluF2US(ALfloat val)
1178 { return aluF2S(val)+32768; }
1179 static inline ALbyte aluF2B(ALfloat val)
1180 { return aluF2I25(val)>>17; }
1181 static inline ALubyte aluF2UB(ALfloat val)
1182 { return aluF2B(val)+128; }
1184 #define DECL_TEMPLATE(T, func) \
1185 static void Write_##T(ALCdevice *device, ALvoid **buffer, ALuint SamplesToDo) \
1187 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
1188 const ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
1189 const ALuint *offsets = device->ChannelOffsets; \
1190 ALuint i, j; \
1192 for(j = 0;j < MaxChannels;j++) \
1194 T *restrict out; \
1196 if(offsets[j] == INVALID_OFFSET) \
1197 continue; \
1199 out = (T*)(*buffer) + offsets[j]; \
1200 for(i = 0;i < SamplesToDo;i++) \
1201 out[i*numchans] = func(DryBuffer[j][i]); \
1203 *buffer = (char*)(*buffer) + SamplesToDo*numchans*sizeof(T); \
1206 DECL_TEMPLATE(ALfloat, aluF2F)
1207 DECL_TEMPLATE(ALuint, aluF2UI)
1208 DECL_TEMPLATE(ALint, aluF2I)
1209 DECL_TEMPLATE(ALushort, aluF2US)
1210 DECL_TEMPLATE(ALshort, aluF2S)
1211 DECL_TEMPLATE(ALubyte, aluF2UB)
1212 DECL_TEMPLATE(ALbyte, aluF2B)
1214 #undef DECL_TEMPLATE
1217 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1219 ALuint SamplesToDo;
1220 ALeffectslot **slot, **slot_end;
1221 ALactivesource **src, **src_end;
1222 ALCcontext *ctx;
1223 FPUCtl oldMode;
1224 ALuint i, c;
1226 SetMixerFPUMode(&oldMode);
1228 while(size > 0)
1230 IncrementRef(&device->MixCount);
1232 SamplesToDo = minu(size, BUFFERSIZE);
1233 for(c = 0;c < MaxChannels;c++)
1234 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
1236 ALCdevice_Lock(device);
1237 V(device->Synth,process)(SamplesToDo, device->DryBuffer);
1239 ctx = device->ContextList;
1240 while(ctx)
1242 ALenum DeferUpdates = ctx->DeferUpdates;
1243 ALenum UpdateSources = AL_FALSE;
1245 if(!DeferUpdates)
1246 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
1248 if(UpdateSources)
1249 CalcListenerParams(ctx->Listener);
1251 /* source processing */
1252 src = ctx->ActiveSources;
1253 src_end = src + ctx->ActiveSourceCount;
1254 while(src != src_end)
1256 ALsource *source = (*src)->Source;
1258 if(source->state != AL_PLAYING && source->state != AL_PAUSED)
1260 ALactivesource *temp = *(--src_end);
1261 *src_end = *src;
1262 *src = temp;
1263 --(ctx->ActiveSourceCount);
1264 continue;
1267 if(!DeferUpdates && (ExchangeInt(&source->NeedsUpdate, AL_FALSE) ||
1268 UpdateSources))
1269 (*src)->Update(*src, ctx);
1271 if(source->state != AL_PAUSED)
1272 MixSource(*src, device, SamplesToDo);
1273 src++;
1276 /* effect slot processing */
1277 slot = VECTOR_ITER_BEGIN(ctx->ActiveAuxSlots);
1278 slot_end = VECTOR_ITER_END(ctx->ActiveAuxSlots);
1279 while(slot != slot_end)
1281 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1282 V((*slot)->EffectState,update)(device, *slot);
1284 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1285 device->DryBuffer);
1287 for(i = 0;i < SamplesToDo;i++)
1288 (*slot)->WetBuffer[0][i] = 0.0f;
1290 slot++;
1293 ctx = ctx->next;
1296 slot = &device->DefaultSlot;
1297 if(*slot != NULL)
1299 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1300 V((*slot)->EffectState,update)(device, *slot);
1302 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1303 device->DryBuffer);
1305 for(i = 0;i < SamplesToDo;i++)
1306 (*slot)->WetBuffer[0][i] = 0.0f;
1309 /* Increment the clock time. Every second's worth of samples is
1310 * converted and added to clock base so that large sample counts don't
1311 * overflow during conversion. This also guarantees an exact, stable
1312 * conversion. */
1313 device->SamplesDone += SamplesToDo;
1314 device->ClockBase += (device->SamplesDone/device->Frequency) * DEVICE_CLOCK_RES;
1315 device->SamplesDone %= device->Frequency;
1316 ALCdevice_Unlock(device);
1318 if(device->Bs2b)
1320 /* Apply binaural/crossfeed filter */
1321 for(i = 0;i < SamplesToDo;i++)
1323 float samples[2];
1324 samples[0] = device->DryBuffer[FrontLeft][i];
1325 samples[1] = device->DryBuffer[FrontRight][i];
1326 bs2b_cross_feed(device->Bs2b, samples);
1327 device->DryBuffer[FrontLeft][i] = samples[0];
1328 device->DryBuffer[FrontRight][i] = samples[1];
1332 if(buffer)
1334 switch(device->FmtType)
1336 case DevFmtByte:
1337 Write_ALbyte(device, &buffer, SamplesToDo);
1338 break;
1339 case DevFmtUByte:
1340 Write_ALubyte(device, &buffer, SamplesToDo);
1341 break;
1342 case DevFmtShort:
1343 Write_ALshort(device, &buffer, SamplesToDo);
1344 break;
1345 case DevFmtUShort:
1346 Write_ALushort(device, &buffer, SamplesToDo);
1347 break;
1348 case DevFmtInt:
1349 Write_ALint(device, &buffer, SamplesToDo);
1350 break;
1351 case DevFmtUInt:
1352 Write_ALuint(device, &buffer, SamplesToDo);
1353 break;
1354 case DevFmtFloat:
1355 Write_ALfloat(device, &buffer, SamplesToDo);
1356 break;
1360 size -= SamplesToDo;
1361 IncrementRef(&device->MixCount);
1364 RestoreFPUMode(&oldMode);
1368 ALvoid aluHandleDisconnect(ALCdevice *device)
1370 ALCcontext *Context;
1372 device->Connected = ALC_FALSE;
1374 Context = device->ContextList;
1375 while(Context)
1377 ALactivesource **src, **src_end;
1379 src = Context->ActiveSources;
1380 src_end = src + Context->ActiveSourceCount;
1381 while(src != src_end)
1383 ALsource *source = (*src)->Source;
1384 if(source->state == AL_PLAYING)
1386 source->state = AL_STOPPED;
1387 source->current_buffer = NULL;
1388 source->position = 0;
1389 source->position_fraction = 0;
1391 src++;
1393 Context->ActiveSourceCount = 0;
1395 Context = Context->next;