Avoid a loop when updating the source position variables
[openal-soft.git] / Alc / ALu.c
blobe661af1d28183e9167434df87946396c80f086d3
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"
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!");
47 struct ChanMap {
48 enum Channel channel;
49 ALfloat angle;
52 /* Cone scalar */
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;
89 switch(Resampler)
91 case PointResampler:
92 return Resample_point32_C;
93 case LinearResampler:
94 return Resample_lerp32_C;
95 case CubicResampler:
96 return Resample_cubic32_C;
97 case ResamplerMax:
98 /* Shouldn't happen */
99 break;
102 return Resample_point32_C;
106 static HrtfMixerFunc SelectHrtfMixer(void)
108 #ifdef HAVE_SSE
109 if((CPUCapFlags&CPU_CAP_SSE))
110 return MixDirect_Hrtf_SSE;
111 #endif
112 #ifdef HAVE_NEON
113 if((CPUCapFlags&CPU_CAP_NEON))
114 return MixDirect_Hrtf_Neon;
115 #endif
117 return MixDirect_Hrtf_C;
120 static DryMixerFunc SelectDirectMixer(void)
122 #ifdef HAVE_SSE
123 if((CPUCapFlags&CPU_CAP_SSE))
124 return MixDirect_SSE;
125 #endif
126 #ifdef HAVE_NEON
127 if((CPUCapFlags&CPU_CAP_NEON))
128 return MixDirect_Neon;
129 #endif
131 return MixDirect_C;
134 static WetMixerFunc SelectSendMixer(void)
136 #ifdef HAVE_SSE
137 if((CPUCapFlags&CPU_CAP_SSE))
138 return MixSend_SSE;
139 #endif
140 #ifdef HAVE_NEON
141 if((CPUCapFlags&CPU_CAP_NEON))
142 return MixSend_Neon;
143 #endif
145 return MixSend_C;
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);
165 if(lengthsqr > 0.0f)
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])
176 ALfloat temp[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];
190 /* AT then UP */
191 N[0] = Listener->Forward[0];
192 N[1] = Listener->Forward[1];
193 N[2] = Listener->Forward[2];
194 aluNormalize(N);
195 V[0] = Listener->Up[0];
196 V[1] = Listener->Up[1];
197 V[2] = Listener->Up[2];
198 aluNormalize(V);
199 /* Build and normalize right-vector */
200 aluCrossproduct(N, V, U);
201 aluNormalize(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) },
259 { LFE, 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) },
267 { LFE, 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) },
276 { LFE, 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;
298 ALfloat Pitch;
299 ALint i, j, c;
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;
320 if(!Slot && i == 0)
321 Slot = Device->DefaultSlot;
322 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
323 src->Send[i].OutBuffer = NULL;
324 else
325 src->Send[i].OutBuffer = Slot->WetBuffer;
328 /* Calculate the stepping value */
329 Channels = FmtMono;
330 BufferListItem = ALSource->queue;
331 while(BufferListItem != NULL)
333 ALbuffer *ALBuffer;
334 if((ALBuffer=BufferListItem->buffer) != NULL)
336 Pitch = Pitch * ALBuffer->Frequency / Frequency;
337 if(Pitch > (ALfloat)MAX_PITCH)
338 src->Step = MAX_PITCH<<FRACTIONBITS;
339 else
341 src->Step = fastf2i(Pitch*FRACTIONONE);
342 if(src->Step == 0)
343 src->Step = 1;
345 src->Resample = SelectResampler(Resampler, src->Step);
347 Channels = ALBuffer->FmtChannels;
348 break;
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;
366 switch(Channels)
368 case FmtMono:
369 chans = MonoMap;
370 num_channels = 1;
371 break;
373 case FmtStereo:
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;
381 else
382 chans = StereoMap;
384 else
386 chans = StereoWideMap;
387 hwidth = DEG2RAD(60.0f);
389 num_channels = 2;
390 break;
392 case FmtRear:
393 chans = RearMap;
394 num_channels = 2;
395 break;
397 case FmtQuad:
398 chans = QuadMap;
399 num_channels = 4;
400 break;
402 case FmtX51:
403 chans = X51Map;
404 num_channels = 6;
405 break;
407 case FmtX61:
408 chans = X61Map;
409 num_channels = 7;
410 break;
412 case FmtX71:
413 chans = X71Map;
414 num_channels = 8;
415 break;
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++)
424 Target[j] = 0.0f;
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;
436 break;
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];
451 Step[j] = 1.0f;
454 src->Direct.Counter = 0;
455 src->Direct.Moving = AL_TRUE;
457 else
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);
470 else
471 Step[j] = 1.0f;
472 Current[j] = cur;
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)
487 /* Skip 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;
496 else
498 /* Get the static HRIR coefficients and delays for this
499 * channel. */
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();
513 else
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++)
519 Target[j] = 0.0f;
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;
530 continue;
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];
545 Step[j] = 1.0f;
548 src->Direct.Counter = 0;
549 src->Direct.Moving = AL_TRUE;
551 else
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);
564 else
565 Step[j] = 1.0f;
566 Current[j] = cur;
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;
585 else
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);
591 else
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,
611 hfscale, 0.0f
613 ALfilterState_setParams(
614 &src->Direct.Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
615 lfscale, 0.0f
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,
632 hfscale, 0.0f
634 ALfilterState_setParams(
635 &src->Send[i].Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
636 lfscale, 0.0f
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;
654 ALfloat Attenuation;
655 ALfloat RoomAttenuation[MAX_SENDS];
656 ALfloat MetersPerUnit;
657 ALfloat RoomRolloffBase;
658 ALfloat RoomRolloff[MAX_SENDS];
659 ALfloat DecayDistance[MAX_SENDS];
660 ALfloat DryGain;
661 ALfloat DryGainHF;
662 ALfloat DryGainLF;
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;
670 ALfloat Pitch;
671 ALuint Frequency;
672 ALint NumSends;
673 ALint i, j;
675 DryGainHF = 1.0f;
676 DryGainLF = 1.0f;
677 for(i = 0;i < MAX_SENDS;i++)
679 WetGainHF[i] = 1.0f;
680 WetGainLF[i] = 1.0f;
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;
724 if(!Slot && i == 0)
725 Slot = Device->DefaultSlot;
726 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
728 Slot = 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;
743 else
745 DecayDistance[i] = 0.0f;
746 RoomAirAbsorption[i] = 1.0f;
749 else
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;
760 else
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);
773 else
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;
792 Attenuation = 1.0f;
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)
801 break;
802 /*fall-through*/
803 case InverseDistance:
804 if(MinDist > 0.0f)
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)));
814 break;
816 case LinearDistanceClamped:
817 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
818 if(MaxDist < MinDist)
819 break;
820 /*fall-through*/
821 case LinearDistance:
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);
832 break;
834 case ExponentDistanceClamped:
835 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
836 if(MaxDist < MinDist)
837 break;
838 /*fall-through*/
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]);
846 break;
848 case DisableDistance:
849 ClampedDist = MinDist;
850 break;
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);
867 if(WetGainAuto)
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
876 * wet path.
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;
898 else
900 ConeVolume = 1.0f;
901 ConeHF = 1.0f;
904 DryGain *= ConeVolume;
905 if(WetGainAuto)
907 for(i = 0;i < NumSends;i++)
908 WetGain[i] *= ConeVolume;
910 if(DryGainHFAuto)
911 DryGainHF *= ConeHF;
912 if(WetGainHFAuto)
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;
938 ALfloat VSS, VLS;
940 if(SpeedOfSound < 1.0f)
942 DopplerFactor *= 1.0f/SpeedOfSound;
943 SpeedOfSound = 1.0f;
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)
956 ALbuffer *ALBuffer;
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;
964 else
966 src->Step = fastf2i(Pitch*FRACTIONONE);
967 if(src->Step == 0)
968 src->Step = 1;
970 src->Resample = SelectResampler(Resampler, src->Step);
972 break;
974 BufferListItem = BufferListItem->next;
977 if(Device->Hrtf)
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. */
1005 if(delta > 0.001f)
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];
1021 else
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();
1039 else
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++)
1046 Target[j] = 0.0f;
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
1062 * enough. */
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];
1077 Step[j] = 1.0f;
1079 src->Direct.Counter = 0;
1080 src->Direct.Moving = AL_TRUE;
1082 else
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);
1092 else
1093 Step[j] = 1.0f;
1094 Current[j] = cur;
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;
1112 else
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);
1118 else
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,
1136 hfscale, 0.0f
1138 ALfilterState_setParams(
1139 &src->Direct.Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1140 lfscale, 0.0f
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,
1154 hfscale, 0.0f
1156 ALfilterState_setParams(
1157 &src->Send[i].Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1158 lfscale, 0.0f
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)
1174 { return 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; \
1194 ALuint i, j; \
1196 for(j = 0;j < MaxChannels;j++) \
1198 T *restrict out; \
1200 if(offsets[j] == INVALID_OFFSET) \
1201 continue; \
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)
1223 ALuint SamplesToDo;
1224 ALeffectslot **slot, **slot_end;
1225 ALactivesource **src, **src_end;
1226 ALCcontext *ctx;
1227 FPUCtl oldMode;
1228 ALuint i, c;
1230 SetMixerFPUMode(&oldMode);
1232 while(size > 0)
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;
1244 while(ctx)
1246 ALenum DeferUpdates = ctx->DeferUpdates;
1247 ALenum UpdateSources = AL_FALSE;
1249 if(!DeferUpdates)
1250 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
1252 if(UpdateSources)
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);
1265 *src_end = *src;
1266 *src = temp;
1267 --(ctx->ActiveSourceCount);
1268 continue;
1271 if(!DeferUpdates && (ExchangeInt(&source->NeedsUpdate, AL_FALSE) ||
1272 UpdateSources))
1273 (*src)->Update(*src, ctx);
1275 if(source->state != AL_PAUSED)
1276 MixSource(*src, device, SamplesToDo);
1277 src++;
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],
1289 device->DryBuffer);
1291 for(i = 0;i < SamplesToDo;i++)
1292 (*slot)->WetBuffer[0][i] = 0.0f;
1294 slot++;
1297 ctx = ctx->next;
1300 slot = &device->DefaultSlot;
1301 if(*slot != NULL)
1303 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1304 V((*slot)->EffectState,update)(device, *slot);
1306 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1307 device->DryBuffer);
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
1316 * conversion. */
1317 device->SamplesDone += SamplesToDo;
1318 device->ClockBase += (device->SamplesDone/device->Frequency) * DEVICE_CLOCK_RES;
1319 device->SamplesDone %= device->Frequency;
1320 ALCdevice_Unlock(device);
1322 if(device->Bs2b)
1324 /* Apply binaural/crossfeed filter */
1325 for(i = 0;i < SamplesToDo;i++)
1327 float samples[2];
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];
1336 if(buffer)
1338 switch(device->FmtType)
1340 case DevFmtByte:
1341 Write_ALbyte(device, &buffer, SamplesToDo);
1342 break;
1343 case DevFmtUByte:
1344 Write_ALubyte(device, &buffer, SamplesToDo);
1345 break;
1346 case DevFmtShort:
1347 Write_ALshort(device, &buffer, SamplesToDo);
1348 break;
1349 case DevFmtUShort:
1350 Write_ALushort(device, &buffer, SamplesToDo);
1351 break;
1352 case DevFmtInt:
1353 Write_ALint(device, &buffer, SamplesToDo);
1354 break;
1355 case DevFmtUInt:
1356 Write_ALuint(device, &buffer, SamplesToDo);
1357 break;
1358 case DevFmtFloat:
1359 Write_ALfloat(device, &buffer, SamplesToDo);
1360 break;
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;
1379 while(Context)
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;
1395 src++;
1397 Context->ActiveSourceCount = 0;
1399 Context = Context->next;