Use a struct to store the source's direct gain/gainhf properties
[openal-soft.git] / Alc / ALu.c
blob71e09e052b395d3c45dd5127a0002984cd5e8bc8
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 DryMixerFunc 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;
285 ALfloat WetGain[MAX_SENDS];
286 ALfloat WetGainHF[MAX_SENDS];
287 ALint NumSends, Frequency;
288 const struct ChanMap *chans = NULL;
289 enum Resampler Resampler;
290 ALint num_channels = 0;
291 ALboolean DirectChannels;
292 ALfloat hwidth = 0.0f;
293 ALfloat Pitch;
294 ALint i, j, c;
296 /* Get device properties */
297 NumSends = Device->NumAuxSends;
298 Frequency = Device->Frequency;
300 /* Get listener properties */
301 ListenerGain = ALContext->Listener->Gain;
303 /* Get source properties */
304 SourceVolume = ALSource->Gain;
305 MinVolume = ALSource->MinGain;
306 MaxVolume = ALSource->MaxGain;
307 Pitch = ALSource->Pitch;
308 Resampler = ALSource->Resampler;
309 DirectChannels = ALSource->DirectChannels;
311 src->Direct.OutBuffer = Device->DryBuffer;
312 for(i = 0;i < NumSends;i++)
314 ALeffectslot *Slot = ALSource->Send[i].Slot;
315 if(!Slot && i == 0)
316 Slot = Device->DefaultSlot;
317 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
318 src->Send[i].OutBuffer = NULL;
319 else
320 src->Send[i].OutBuffer = Slot->WetBuffer;
323 /* Calculate the stepping value */
324 Channels = FmtMono;
325 BufferListItem = ALSource->queue;
326 while(BufferListItem != NULL)
328 ALbuffer *ALBuffer;
329 if((ALBuffer=BufferListItem->buffer) != NULL)
331 Pitch = Pitch * ALBuffer->Frequency / Frequency;
332 if(Pitch > 10.0f)
333 src->Step = 10<<FRACTIONBITS;
334 else
336 src->Step = fastf2i(Pitch*FRACTIONONE);
337 if(src->Step == 0)
338 src->Step = 1;
340 src->Resample = SelectResampler(Resampler, src->Step);
342 Channels = ALBuffer->FmtChannels;
343 break;
345 BufferListItem = BufferListItem->next;
348 /* Calculate gains */
349 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
350 DryGain *= ALSource->Direct.Gain * ListenerGain;
351 DryGainHF = ALSource->Direct.GainHF;
352 for(i = 0;i < NumSends;i++)
354 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
355 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
356 WetGainHF[i] = ALSource->Send[i].GainHF;
359 switch(Channels)
361 case FmtMono:
362 chans = MonoMap;
363 num_channels = 1;
364 break;
366 case FmtStereo:
367 if(!(Device->Flags&DEVICE_WIDE_STEREO))
369 /* HACK: Place the stereo channels at +/-90 degrees when using non-
370 * HRTF stereo output. This helps reduce the "monoization" caused
371 * by them panning towards the center. */
372 if(Device->FmtChans == DevFmtStereo && !Device->Hrtf)
373 chans = StereoWideMap;
374 else
375 chans = StereoMap;
377 else
379 chans = StereoWideMap;
380 hwidth = DEG2RAD(60.0f);
382 num_channels = 2;
383 break;
385 case FmtRear:
386 chans = RearMap;
387 num_channels = 2;
388 break;
390 case FmtQuad:
391 chans = QuadMap;
392 num_channels = 4;
393 break;
395 case FmtX51:
396 chans = X51Map;
397 num_channels = 6;
398 break;
400 case FmtX61:
401 chans = X61Map;
402 num_channels = 7;
403 break;
405 case FmtX71:
406 chans = X71Map;
407 num_channels = 8;
408 break;
411 if(DirectChannels != AL_FALSE)
413 ALfloat (*Matrix)[MaxChannels] = src->Direct.Mix.Gains.Target;
414 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
416 for(c = 0;c < MaxChannels;c++)
417 Matrix[i][c] = 0.0f;
419 for(c = 0;c < num_channels;c++)
421 for(i = 0;i < (ALint)Device->NumChan;i++)
423 enum Channel chan = Device->Speaker2Chan[i];
424 if(chan == chans[c].channel)
426 Matrix[c][chan] = DryGain;
427 break;
432 if(src->Direct.Moving)
434 ALfloat (*restrict Current)[MaxChannels] = src->Direct.Mix.Gains.Current;
435 ALfloat (*restrict Step)[MaxChannels] = src->Direct.Mix.Gains.Step;
436 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
438 for(j = 0;j < MaxChannels;j++)
440 ALfloat cur = maxf(Current[i][j], FLT_EPSILON);
441 ALfloat trg = maxf(Matrix[i][j], FLT_EPSILON);
442 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
443 Step[i][j] = powf(trg/cur, 1.0f/64.0f);
444 else
445 Step[i][j] = 1.0f;
446 Current[i][j] = cur;
449 src->Direct.Counter = 64;
451 else
453 ALfloat (*restrict Current)[MaxChannels] = src->Direct.Mix.Gains.Current;
454 ALfloat (*restrict Step)[MaxChannels] = src->Direct.Mix.Gains.Step;
455 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
457 for(j = 0;j < MaxChannels;j++)
459 Current[i][j] = Matrix[i][j];
460 Step[i][j] = 1.0f;
463 src->Direct.Counter = 0;
464 src->Direct.Moving = AL_TRUE;
467 src->DryMix = SelectDirectMixer();
469 else if(Device->Hrtf)
471 for(c = 0;c < num_channels;c++)
473 if(chans[c].channel == LFE)
475 /* Skip LFE */
476 src->Direct.Mix.Hrtf.Params[c].Delay[0] = 0;
477 src->Direct.Mix.Hrtf.Params[c].Delay[1] = 0;
478 for(i = 0;i < HRIR_LENGTH;i++)
480 src->Direct.Mix.Hrtf.Params[c].Coeffs[i][0] = 0.0f;
481 src->Direct.Mix.Hrtf.Params[c].Coeffs[i][1] = 0.0f;
484 else
486 /* Get the static HRIR coefficients and delays for this
487 * channel. */
488 GetLerpedHrtfCoeffs(Device->Hrtf,
489 0.0f, chans[c].angle, DryGain,
490 src->Direct.Mix.Hrtf.Params[c].Coeffs,
491 src->Direct.Mix.Hrtf.Params[c].Delay);
494 src->Direct.Counter = 0;
495 src->Direct.Moving = AL_TRUE;
496 src->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
498 src->DryMix = SelectHrtfMixer();
500 else
502 ALfloat (*Matrix)[MaxChannels] = src->Direct.Mix.Gains.Target;
503 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
505 for(c = 0;c < MaxChannels;c++)
506 Matrix[i][c] = 0.0f;
509 DryGain *= lerp(1.0f, 1.0f/sqrtf((float)Device->NumChan), hwidth/F_PI);
510 for(c = 0;c < num_channels;c++)
512 /* Special-case LFE */
513 if(chans[c].channel == LFE)
515 Matrix[c][chans[c].channel] = DryGain;
516 continue;
518 ComputeAngleGains(Device, chans[c].angle, hwidth, DryGain,
519 Matrix[c]);
522 if(src->Direct.Moving)
524 ALfloat (*restrict Current)[MaxChannels] = src->Direct.Mix.Gains.Current;
525 ALfloat (*restrict Step)[MaxChannels] = src->Direct.Mix.Gains.Step;
526 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
528 for(j = 0;j < MaxChannels;j++)
530 ALfloat trg = maxf(Matrix[i][j], FLT_EPSILON);
531 ALfloat cur = maxf(Current[i][j], FLT_EPSILON);
532 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
533 Step[i][j] = powf(trg/cur, 1.0f/64.0f);
534 else
535 Step[i][j] = 1.0f;
536 Current[i][j] = cur;
539 src->Direct.Counter = 64;
541 else
543 ALfloat (*restrict Current)[MaxChannels] = src->Direct.Mix.Gains.Current;
544 ALfloat (*restrict Step)[MaxChannels] = src->Direct.Mix.Gains.Step;
545 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
547 for(j = 0;j < MaxChannels;j++)
549 Current[i][j] = Matrix[i][j];
550 Step[i][j] = 1.0f;
553 src->Direct.Counter = 0;
554 src->Direct.Moving = AL_TRUE;
557 src->DryMix = SelectDirectMixer();
559 for(i = 0;i < NumSends;i++)
561 if(src->Send[i].Moving)
563 ALfloat cur = maxf(src->Send[i].Gain.Current, FLT_EPSILON);
564 ALfloat trg = maxf(src->Send[i].Gain.Target, FLT_EPSILON);
565 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
566 src->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
567 else
568 src->Send[i].Gain.Step = 1.0f;
569 src->Send[i].Gain.Current = cur;
570 src->Send[i].Counter = 64;
572 else
574 src->Send[i].Gain.Current = WetGain[i];
575 src->Send[i].Gain.Target = WetGain[i];
576 src->Send[i].Gain.Step = 1.0f;
577 src->Send[i].Counter = 0;
578 src->Send[i].Moving = AL_TRUE;
581 src->WetMix = SelectSendMixer();
584 ALfloat gain = maxf(0.01f, DryGainHF);
585 for(c = 0;c < num_channels;c++)
586 ALfilterState_setParams(&src->Direct.LpFilter[c],
587 ALfilterType_HighShelf, gain,
588 (ALfloat)LOWPASSFREQREF/Frequency, 0.0f);
590 for(i = 0;i < NumSends;i++)
592 ALfloat gain = maxf(0.01f, WetGainHF[i]);
593 for(c = 0;c < num_channels;c++)
594 ALfilterState_setParams(&src->Send[i].LpFilter[c],
595 ALfilterType_HighShelf, gain,
596 (ALfloat)LOWPASSFREQREF/Frequency, 0.0f);
600 ALvoid CalcSourceParams(ALactivesource *src, const ALCcontext *ALContext)
602 ALCdevice *Device = ALContext->Device;
603 const ALsource *ALSource = src->Source;
604 ALfloat Velocity[3],Direction[3],Position[3],SourceToListener[3];
605 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
606 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
607 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
608 ALfloat DopplerFactor, SpeedOfSound;
609 ALfloat AirAbsorptionFactor;
610 ALfloat RoomAirAbsorption[MAX_SENDS];
611 ALbufferlistitem *BufferListItem;
612 ALfloat Attenuation;
613 ALfloat RoomAttenuation[MAX_SENDS];
614 ALfloat MetersPerUnit;
615 ALfloat RoomRolloffBase;
616 ALfloat RoomRolloff[MAX_SENDS];
617 ALfloat DecayDistance[MAX_SENDS];
618 ALfloat DryGain;
619 ALfloat DryGainHF;
620 ALboolean DryGainHFAuto;
621 ALfloat WetGain[MAX_SENDS];
622 ALfloat WetGainHF[MAX_SENDS];
623 ALboolean WetGainAuto;
624 ALboolean WetGainHFAuto;
625 enum Resampler Resampler;
626 ALfloat Pitch;
627 ALuint Frequency;
628 ALint NumSends;
629 ALint i, j;
631 DryGainHF = 1.0f;
632 for(i = 0;i < MAX_SENDS;i++)
633 WetGainHF[i] = 1.0f;
635 /* Get context/device properties */
636 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
637 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
638 NumSends = Device->NumAuxSends;
639 Frequency = Device->Frequency;
641 /* Get listener properties */
642 ListenerGain = ALContext->Listener->Gain;
643 MetersPerUnit = ALContext->Listener->MetersPerUnit;
645 /* Get source properties */
646 SourceVolume = ALSource->Gain;
647 MinVolume = ALSource->MinGain;
648 MaxVolume = ALSource->MaxGain;
649 Pitch = ALSource->Pitch;
650 Resampler = ALSource->Resampler;
651 Position[0] = ALSource->Position[0];
652 Position[1] = ALSource->Position[1];
653 Position[2] = ALSource->Position[2];
654 Direction[0] = ALSource->Orientation[0];
655 Direction[1] = ALSource->Orientation[1];
656 Direction[2] = ALSource->Orientation[2];
657 Velocity[0] = ALSource->Velocity[0];
658 Velocity[1] = ALSource->Velocity[1];
659 Velocity[2] = ALSource->Velocity[2];
660 MinDist = ALSource->RefDistance;
661 MaxDist = ALSource->MaxDistance;
662 Rolloff = ALSource->RollOffFactor;
663 InnerAngle = ALSource->InnerAngle;
664 OuterAngle = ALSource->OuterAngle;
665 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
666 DryGainHFAuto = ALSource->DryGainHFAuto;
667 WetGainAuto = ALSource->WetGainAuto;
668 WetGainHFAuto = ALSource->WetGainHFAuto;
669 RoomRolloffBase = ALSource->RoomRolloffFactor;
671 src->Direct.OutBuffer = Device->DryBuffer;
672 for(i = 0;i < NumSends;i++)
674 ALeffectslot *Slot = ALSource->Send[i].Slot;
676 if(!Slot && i == 0)
677 Slot = Device->DefaultSlot;
678 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
680 Slot = NULL;
681 RoomRolloff[i] = 0.0f;
682 DecayDistance[i] = 0.0f;
683 RoomAirAbsorption[i] = 1.0f;
685 else if(Slot->AuxSendAuto)
687 RoomRolloff[i] = RoomRolloffBase;
688 if(IsReverbEffect(Slot->EffectType))
690 RoomRolloff[i] += Slot->EffectProps.Reverb.RoomRolloffFactor;
691 DecayDistance[i] = Slot->EffectProps.Reverb.DecayTime *
692 SPEEDOFSOUNDMETRESPERSEC;
693 RoomAirAbsorption[i] = Slot->EffectProps.Reverb.AirAbsorptionGainHF;
695 else
697 DecayDistance[i] = 0.0f;
698 RoomAirAbsorption[i] = 1.0f;
701 else
703 /* If the slot's auxiliary send auto is off, the data sent to the
704 * effect slot is the same as the dry path, sans filter effects */
705 RoomRolloff[i] = Rolloff;
706 DecayDistance[i] = 0.0f;
707 RoomAirAbsorption[i] = AIRABSORBGAINHF;
710 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
711 src->Send[i].OutBuffer = NULL;
712 else
713 src->Send[i].OutBuffer = Slot->WetBuffer;
716 /* Transform source to listener space (convert to head relative) */
717 if(ALSource->HeadRelative == AL_FALSE)
719 ALfloat (*restrict Matrix)[4] = ALContext->Listener->Params.Matrix;
720 /* Transform source vectors */
721 aluMatrixVector(Position, 1.0f, Matrix);
722 aluMatrixVector(Direction, 0.0f, Matrix);
723 aluMatrixVector(Velocity, 0.0f, Matrix);
725 else
727 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
728 /* Offset the source velocity to be relative of the listener velocity */
729 Velocity[0] += ListenerVel[0];
730 Velocity[1] += ListenerVel[1];
731 Velocity[2] += ListenerVel[2];
734 SourceToListener[0] = -Position[0];
735 SourceToListener[1] = -Position[1];
736 SourceToListener[2] = -Position[2];
737 aluNormalize(SourceToListener);
738 aluNormalize(Direction);
740 /* Calculate distance attenuation */
741 Distance = sqrtf(aluDotproduct(Position, Position));
742 ClampedDist = Distance;
744 Attenuation = 1.0f;
745 for(i = 0;i < NumSends;i++)
746 RoomAttenuation[i] = 1.0f;
747 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
748 ALContext->DistanceModel)
750 case InverseDistanceClamped:
751 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
752 if(MaxDist < MinDist)
753 break;
754 /*fall-through*/
755 case InverseDistance:
756 if(MinDist > 0.0f)
758 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
759 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
760 for(i = 0;i < NumSends;i++)
762 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
763 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
766 break;
768 case LinearDistanceClamped:
769 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
770 if(MaxDist < MinDist)
771 break;
772 /*fall-through*/
773 case LinearDistance:
774 if(MaxDist != MinDist)
776 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
777 Attenuation = maxf(Attenuation, 0.0f);
778 for(i = 0;i < NumSends;i++)
780 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
781 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
784 break;
786 case ExponentDistanceClamped:
787 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
788 if(MaxDist < MinDist)
789 break;
790 /*fall-through*/
791 case ExponentDistance:
792 if(ClampedDist > 0.0f && MinDist > 0.0f)
794 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
795 for(i = 0;i < NumSends;i++)
796 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
798 break;
800 case DisableDistance:
801 ClampedDist = MinDist;
802 break;
805 /* Source Gain + Attenuation */
806 DryGain = SourceVolume * Attenuation;
807 for(i = 0;i < NumSends;i++)
808 WetGain[i] = SourceVolume * RoomAttenuation[i];
810 /* Distance-based air absorption */
811 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
813 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
814 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
815 for(i = 0;i < NumSends;i++)
816 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
819 if(WetGainAuto)
821 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
823 /* Apply a decay-time transformation to the wet path, based on the
824 * attenuation of the dry path.
826 * Using the apparent distance, based on the distance attenuation, the
827 * initial decay of the reverb effect is calculated and applied to the
828 * wet path.
830 for(i = 0;i < NumSends;i++)
832 if(DecayDistance[i] > 0.0f)
833 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
837 /* Calculate directional soundcones */
838 Angle = RAD2DEG(acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale) * 2.0f;
839 if(Angle > InnerAngle && Angle <= OuterAngle)
841 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
842 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
843 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
845 else if(Angle > OuterAngle)
847 ConeVolume = ALSource->OuterGain;
848 ConeHF = ALSource->OuterGainHF;
850 else
852 ConeVolume = 1.0f;
853 ConeHF = 1.0f;
856 DryGain *= ConeVolume;
857 if(WetGainAuto)
859 for(i = 0;i < NumSends;i++)
860 WetGain[i] *= ConeVolume;
862 if(DryGainHFAuto)
863 DryGainHF *= ConeHF;
864 if(WetGainHFAuto)
866 for(i = 0;i < NumSends;i++)
867 WetGainHF[i] *= ConeHF;
870 /* Clamp to Min/Max Gain */
871 DryGain = clampf(DryGain, MinVolume, MaxVolume);
872 for(i = 0;i < NumSends;i++)
873 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
875 /* Apply gain and frequency filters */
876 DryGain *= ALSource->Direct.Gain * ListenerGain;
877 DryGainHF *= ALSource->Direct.GainHF;
878 for(i = 0;i < NumSends;i++)
880 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
881 WetGainHF[i] *= ALSource->Send[i].GainHF;
884 /* Calculate velocity-based doppler effect */
885 if(DopplerFactor > 0.0f)
887 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
888 ALfloat VSS, VLS;
890 if(SpeedOfSound < 1.0f)
892 DopplerFactor *= 1.0f/SpeedOfSound;
893 SpeedOfSound = 1.0f;
896 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
897 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
899 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
900 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
903 BufferListItem = ALSource->queue;
904 while(BufferListItem != NULL)
906 ALbuffer *ALBuffer;
907 if((ALBuffer=BufferListItem->buffer) != NULL)
909 /* Calculate fixed-point stepping value, based on the pitch, buffer
910 * frequency, and output frequency. */
911 Pitch = Pitch * ALBuffer->Frequency / Frequency;
912 if(Pitch > 10.0f)
913 src->Step = 10<<FRACTIONBITS;
914 else
916 src->Step = fastf2i(Pitch*FRACTIONONE);
917 if(src->Step == 0)
918 src->Step = 1;
920 src->Resample = SelectResampler(Resampler, src->Step);
922 break;
924 BufferListItem = BufferListItem->next;
927 if(Device->Hrtf)
929 /* Use a binaural HRTF algorithm for stereo headphone playback */
930 ALfloat delta, ev = 0.0f, az = 0.0f;
932 if(Distance > FLT_EPSILON)
934 ALfloat invlen = 1.0f/Distance;
935 Position[0] *= invlen;
936 Position[1] *= invlen;
937 Position[2] *= invlen;
939 /* Calculate elevation and azimuth only when the source is not at
940 * the listener. This prevents +0 and -0 Z from producing
941 * inconsistent panning. Also, clamp Y in case FP precision errors
942 * cause it to land outside of -1..+1. */
943 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
944 az = atan2f(Position[0], -Position[2]*ZScale);
947 /* Check to see if the HRIR is already moving. */
948 if(src->Direct.Moving)
950 /* Calculate the normalized HRTF transition factor (delta). */
951 delta = CalcHrtfDelta(src->Direct.Mix.Hrtf.Gain, DryGain,
952 src->Direct.Mix.Hrtf.Dir, Position);
953 /* If the delta is large enough, get the moving HRIR target
954 * coefficients, target delays, steppping values, and counter. */
955 if(delta > 0.001f)
957 ALuint counter = GetMovingHrtfCoeffs(Device->Hrtf,
958 ev, az, DryGain, delta,
959 src->Direct.Counter,
960 src->Direct.Mix.Hrtf.Params[0].Coeffs,
961 src->Direct.Mix.Hrtf.Params[0].Delay,
962 src->Direct.Mix.Hrtf.Params[0].CoeffStep,
963 src->Direct.Mix.Hrtf.Params[0].DelayStep);
964 src->Direct.Counter = counter;
965 src->Direct.Mix.Hrtf.Gain = DryGain;
966 src->Direct.Mix.Hrtf.Dir[0] = Position[0];
967 src->Direct.Mix.Hrtf.Dir[1] = Position[1];
968 src->Direct.Mix.Hrtf.Dir[2] = Position[2];
971 else
973 /* Get the initial (static) HRIR coefficients and delays. */
974 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, DryGain,
975 src->Direct.Mix.Hrtf.Params[0].Coeffs,
976 src->Direct.Mix.Hrtf.Params[0].Delay);
977 src->Direct.Counter = 0;
978 src->Direct.Moving = AL_TRUE;
979 src->Direct.Mix.Hrtf.Gain = DryGain;
980 src->Direct.Mix.Hrtf.Dir[0] = Position[0];
981 src->Direct.Mix.Hrtf.Dir[1] = Position[1];
982 src->Direct.Mix.Hrtf.Dir[2] = Position[2];
984 src->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
986 src->DryMix = SelectHrtfMixer();
988 else
990 ALfloat (*Matrix)[MaxChannels] = src->Direct.Mix.Gains.Target;
991 ALfloat DirGain = 0.0f;
992 ALfloat AmbientGain;
994 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
996 for(j = 0;j < MaxChannels;j++)
997 Matrix[i][j] = 0.0f;
1000 /* Normalize the length, and compute panned gains. */
1001 if(Distance > FLT_EPSILON)
1003 ALfloat invlen = 1.0f/Distance;
1004 Position[0] *= invlen;
1005 Position[1] *= invlen;
1006 Position[2] *= invlen;
1008 DirGain = sqrtf(Position[0]*Position[0] + Position[2]*Position[2]);
1009 ComputeAngleGains(Device, atan2f(Position[0], -Position[2]*ZScale), 0.0f,
1010 DryGain*DirGain, Matrix[0]);
1013 /* Adjustment for vertical offsets. Not the greatest, but simple
1014 * enough. */
1015 AmbientGain = DryGain * sqrtf(1.0f/Device->NumChan) * (1.0f-DirGain);
1016 for(i = 0;i < (ALint)Device->NumChan;i++)
1018 enum Channel chan = Device->Speaker2Chan[i];
1019 Matrix[0][chan] = maxf(Matrix[0][chan], AmbientGain);
1022 if(src->Direct.Moving)
1024 ALfloat (*restrict Current)[MaxChannels] = src->Direct.Mix.Gains.Current;
1025 ALfloat (*restrict Step)[MaxChannels] = src->Direct.Mix.Gains.Step;
1026 for(j = 0;j < MaxChannels;j++)
1028 ALfloat cur = maxf(Current[0][j], FLT_EPSILON);
1029 ALfloat trg = maxf(Matrix[0][j], FLT_EPSILON);
1030 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1031 Step[0][j] = powf(trg/cur, 1.0f/64.0f);
1032 else
1033 Step[0][j] = 1.0f;
1034 Current[0][j] = cur;
1036 src->Direct.Counter = 64;
1038 else
1040 ALfloat (*restrict Current)[MaxChannels] = src->Direct.Mix.Gains.Current;
1041 ALfloat (*restrict Step)[MaxChannels] = src->Direct.Mix.Gains.Step;
1042 for(i = 0;i < MAX_INPUT_CHANNELS;i++)
1044 for(j = 0;j < MaxChannels;j++)
1046 Current[i][j] = Matrix[i][j];
1047 Step[i][j] = 1.0f;
1050 src->Direct.Counter = 0;
1051 src->Direct.Moving = AL_TRUE;
1054 src->DryMix = SelectDirectMixer();
1056 for(i = 0;i < NumSends;i++)
1058 if(src->Send[i].Moving)
1060 ALfloat cur = maxf(src->Send[i].Gain.Current, FLT_EPSILON);
1061 ALfloat trg = maxf(src->Send[i].Gain.Target, FLT_EPSILON);
1062 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1063 src->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
1064 else
1065 src->Send[i].Gain.Step = 1.0f;
1066 src->Send[i].Gain.Current = cur;
1067 src->Send[i].Counter = 64;
1069 else
1071 src->Send[i].Gain.Current = WetGain[i];
1072 src->Send[i].Gain.Target = WetGain[i];
1073 src->Send[i].Gain.Step = 1.0f;
1074 src->Send[i].Counter = 0;
1075 src->Send[i].Moving = AL_TRUE;
1078 src->WetMix = SelectSendMixer();
1081 ALfloat gain = maxf(0.01f, DryGainHF);
1082 ALfilterState_setParams(&src->Direct.LpFilter[0],
1083 ALfilterType_HighShelf, gain,
1084 (ALfloat)LOWPASSFREQREF/Frequency, 0.0f);
1086 for(i = 0;i < NumSends;i++)
1088 ALfloat gain = maxf(0.01f, WetGainHF[i]);
1089 ALfilterState_setParams(&src->Send[i].LpFilter[0],
1090 ALfilterType_HighShelf, gain,
1091 (ALfloat)LOWPASSFREQREF/Frequency, 0.0f);
1096 static inline ALint aluF2I25(ALfloat val)
1098 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1099 if(fabsf(val) > 1.0f)
1100 val = (ALfloat)((0.0f < val) - (val < 0.0f));
1101 /* Convert to a signed integer, between -16777215 and +16777215. */
1102 return fastf2i(val*16777215.0f);
1105 static inline ALfloat aluF2F(ALfloat val)
1106 { return val; }
1107 static inline ALint aluF2I(ALfloat val)
1108 { return aluF2I25(val)<<7; }
1109 static inline ALuint aluF2UI(ALfloat val)
1110 { return aluF2I(val)+2147483648u; }
1111 static inline ALshort aluF2S(ALfloat val)
1112 { return aluF2I25(val)>>9; }
1113 static inline ALushort aluF2US(ALfloat val)
1114 { return aluF2S(val)+32768; }
1115 static inline ALbyte aluF2B(ALfloat val)
1116 { return aluF2I25(val)>>17; }
1117 static inline ALubyte aluF2UB(ALfloat val)
1118 { return aluF2B(val)+128; }
1120 #define DECL_TEMPLATE(T, func) \
1121 static void Write_##T(ALCdevice *device, ALvoid **buffer, ALuint SamplesToDo) \
1123 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
1124 const ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
1125 const ALuint *offsets = device->ChannelOffsets; \
1126 ALuint i, j; \
1128 for(j = 0;j < MaxChannels;j++) \
1130 T *restrict out; \
1132 if(offsets[j] == INVALID_OFFSET) \
1133 continue; \
1135 out = (T*)(*buffer) + offsets[j]; \
1136 for(i = 0;i < SamplesToDo;i++) \
1137 out[i*numchans] = func(DryBuffer[j][i]); \
1139 *buffer = (char*)(*buffer) + SamplesToDo*numchans*sizeof(T); \
1142 DECL_TEMPLATE(ALfloat, aluF2F)
1143 DECL_TEMPLATE(ALuint, aluF2UI)
1144 DECL_TEMPLATE(ALint, aluF2I)
1145 DECL_TEMPLATE(ALushort, aluF2US)
1146 DECL_TEMPLATE(ALshort, aluF2S)
1147 DECL_TEMPLATE(ALubyte, aluF2UB)
1148 DECL_TEMPLATE(ALbyte, aluF2B)
1150 #undef DECL_TEMPLATE
1153 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1155 ALuint SamplesToDo;
1156 ALeffectslot **slot, **slot_end;
1157 ALactivesource **src, **src_end;
1158 ALCcontext *ctx;
1159 FPUCtl oldMode;
1160 ALuint i, c;
1162 SetMixerFPUMode(&oldMode);
1164 while(size > 0)
1166 IncrementRef(&device->MixCount);
1168 SamplesToDo = minu(size, BUFFERSIZE);
1169 for(c = 0;c < MaxChannels;c++)
1170 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
1172 ALCdevice_Lock(device);
1173 V(device->Synth,process)(SamplesToDo, device->DryBuffer);
1175 ctx = device->ContextList;
1176 while(ctx)
1178 ALenum DeferUpdates = ctx->DeferUpdates;
1179 ALenum UpdateSources = AL_FALSE;
1181 if(!DeferUpdates)
1182 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
1184 if(UpdateSources)
1185 CalcListenerParams(ctx->Listener);
1187 /* source processing */
1188 src = ctx->ActiveSources;
1189 src_end = src + ctx->ActiveSourceCount;
1190 while(src != src_end)
1192 ALsource *source = (*src)->Source;
1194 if(source->state != AL_PLAYING && source->state != AL_PAUSED)
1196 ALactivesource *temp = *(--src_end);
1197 *src_end = *src;
1198 *src = temp;
1199 --(ctx->ActiveSourceCount);
1200 continue;
1203 if(!DeferUpdates && (ExchangeInt(&source->NeedsUpdate, AL_FALSE) ||
1204 UpdateSources))
1205 (*src)->Update(*src, ctx);
1207 if(source->state != AL_PAUSED)
1208 MixSource(*src, device, SamplesToDo);
1209 src++;
1212 /* effect slot processing */
1213 slot = VECTOR_ITER_BEGIN(ctx->ActiveAuxSlots);
1214 slot_end = VECTOR_ITER_END(ctx->ActiveAuxSlots);
1215 while(slot != slot_end)
1217 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1218 V((*slot)->EffectState,update)(device, *slot);
1220 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1221 device->DryBuffer);
1223 for(i = 0;i < SamplesToDo;i++)
1224 (*slot)->WetBuffer[0][i] = 0.0f;
1226 slot++;
1229 ctx = ctx->next;
1232 slot = &device->DefaultSlot;
1233 if(*slot != NULL)
1235 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1236 V((*slot)->EffectState,update)(device, *slot);
1238 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1239 device->DryBuffer);
1241 for(i = 0;i < SamplesToDo;i++)
1242 (*slot)->WetBuffer[0][i] = 0.0f;
1245 /* Increment the clock time. Every second's worth of samples is
1246 * converted and added to clock base so that large sample counts don't
1247 * overflow during conversion. This also guarantees an exact, stable
1248 * conversion. */
1249 device->SamplesDone += SamplesToDo;
1250 device->ClockBase += (device->SamplesDone/device->Frequency) * DEVICE_CLOCK_RES;
1251 device->SamplesDone %= device->Frequency;
1252 ALCdevice_Unlock(device);
1254 if(device->Bs2b)
1256 /* Apply binaural/crossfeed filter */
1257 for(i = 0;i < SamplesToDo;i++)
1259 float samples[2];
1260 samples[0] = device->DryBuffer[FrontLeft][i];
1261 samples[1] = device->DryBuffer[FrontRight][i];
1262 bs2b_cross_feed(device->Bs2b, samples);
1263 device->DryBuffer[FrontLeft][i] = samples[0];
1264 device->DryBuffer[FrontRight][i] = samples[1];
1268 if(buffer)
1270 switch(device->FmtType)
1272 case DevFmtByte:
1273 Write_ALbyte(device, &buffer, SamplesToDo);
1274 break;
1275 case DevFmtUByte:
1276 Write_ALubyte(device, &buffer, SamplesToDo);
1277 break;
1278 case DevFmtShort:
1279 Write_ALshort(device, &buffer, SamplesToDo);
1280 break;
1281 case DevFmtUShort:
1282 Write_ALushort(device, &buffer, SamplesToDo);
1283 break;
1284 case DevFmtInt:
1285 Write_ALint(device, &buffer, SamplesToDo);
1286 break;
1287 case DevFmtUInt:
1288 Write_ALuint(device, &buffer, SamplesToDo);
1289 break;
1290 case DevFmtFloat:
1291 Write_ALfloat(device, &buffer, SamplesToDo);
1292 break;
1296 size -= SamplesToDo;
1297 IncrementRef(&device->MixCount);
1300 RestoreFPUMode(&oldMode);
1304 ALvoid aluHandleDisconnect(ALCdevice *device)
1306 ALCcontext *Context;
1308 device->Connected = ALC_FALSE;
1310 Context = device->ContextList;
1311 while(Context)
1313 ALactivesource **src, **src_end;
1315 src = Context->ActiveSources;
1316 src_end = src + Context->ActiveSourceCount;
1317 while(src != src_end)
1319 ALsource *source = (*src)->Source;
1320 if(source->state == AL_PLAYING)
1322 source->state = AL_STOPPED;
1323 source->current_buffer = NULL;
1324 source->position = 0;
1325 source->position_fraction = 0;
1327 src++;
1329 Context->ActiveSourceCount = 0;
1331 Context = Context->next;