Get the mixer and resampler functions when needed
[openal-soft.git] / Alc / ALu.c
blobf28e6e6d8f71ffad8b4492fd96d488cf66b7d793
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 "midi/base.h"
42 static_assert((INT_MAX>>FRACTIONBITS)/MAX_PITCH > BUFFERSIZE,
43 "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
45 struct ChanMap {
46 enum Channel channel;
47 ALfloat angle;
50 /* Cone scalar */
51 ALfloat ConeScale = 1.0f;
53 /* Localized Z scalar for mono sources */
54 ALfloat ZScale = 1.0f;
56 extern inline ALfloat minf(ALfloat a, ALfloat b);
57 extern inline ALfloat maxf(ALfloat a, ALfloat b);
58 extern inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max);
60 extern inline ALdouble mind(ALdouble a, ALdouble b);
61 extern inline ALdouble maxd(ALdouble a, ALdouble b);
62 extern inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max);
64 extern inline ALuint minu(ALuint a, ALuint b);
65 extern inline ALuint maxu(ALuint a, ALuint b);
66 extern inline ALuint clampu(ALuint val, ALuint min, ALuint max);
68 extern inline ALint mini(ALint a, ALint b);
69 extern inline ALint maxi(ALint a, ALint b);
70 extern inline ALint clampi(ALint val, ALint min, ALint max);
72 extern inline ALint64 mini64(ALint64 a, ALint64 b);
73 extern inline ALint64 maxi64(ALint64 a, ALint64 b);
74 extern inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max);
76 extern inline ALuint64 minu64(ALuint64 a, ALuint64 b);
77 extern inline ALuint64 maxu64(ALuint64 a, ALuint64 b);
78 extern inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max);
80 extern inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu);
81 extern inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu);
84 static inline void aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
86 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
87 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
88 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
91 static inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
93 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
94 inVector1[2]*inVector2[2];
97 static inline void aluNormalize(ALfloat *inVector)
99 ALfloat lengthsqr = aluDotproduct(inVector, inVector);
100 if(lengthsqr > 0.0f)
102 ALfloat inv_length = 1.0f/sqrtf(lengthsqr);
103 inVector[0] *= inv_length;
104 inVector[1] *= inv_length;
105 inVector[2] *= inv_length;
109 static inline ALvoid aluMatrixVector(ALfloat *vector, ALfloat w, ALfloat (*restrict matrix)[4])
111 ALfloat temp[4] = {
112 vector[0], vector[1], vector[2], w
115 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
116 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
117 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
121 static ALvoid CalcListenerParams(ALlistener *Listener)
123 ALfloat N[3], V[3], U[3], P[3];
125 /* AT then UP */
126 N[0] = Listener->Forward[0];
127 N[1] = Listener->Forward[1];
128 N[2] = Listener->Forward[2];
129 aluNormalize(N);
130 V[0] = Listener->Up[0];
131 V[1] = Listener->Up[1];
132 V[2] = Listener->Up[2];
133 aluNormalize(V);
134 /* Build and normalize right-vector */
135 aluCrossproduct(N, V, U);
136 aluNormalize(U);
138 Listener->Params.Matrix[0][0] = U[0];
139 Listener->Params.Matrix[0][1] = V[0];
140 Listener->Params.Matrix[0][2] = -N[0];
141 Listener->Params.Matrix[0][3] = 0.0f;
142 Listener->Params.Matrix[1][0] = U[1];
143 Listener->Params.Matrix[1][1] = V[1];
144 Listener->Params.Matrix[1][2] = -N[1];
145 Listener->Params.Matrix[1][3] = 0.0f;
146 Listener->Params.Matrix[2][0] = U[2];
147 Listener->Params.Matrix[2][1] = V[2];
148 Listener->Params.Matrix[2][2] = -N[2];
149 Listener->Params.Matrix[2][3] = 0.0f;
150 Listener->Params.Matrix[3][0] = 0.0f;
151 Listener->Params.Matrix[3][1] = 0.0f;
152 Listener->Params.Matrix[3][2] = 0.0f;
153 Listener->Params.Matrix[3][3] = 1.0f;
155 P[0] = Listener->Position[0];
156 P[1] = Listener->Position[1];
157 P[2] = Listener->Position[2];
158 aluMatrixVector(P, 1.0f, Listener->Params.Matrix);
159 Listener->Params.Matrix[3][0] = -P[0];
160 Listener->Params.Matrix[3][1] = -P[1];
161 Listener->Params.Matrix[3][2] = -P[2];
163 Listener->Params.Velocity[0] = Listener->Velocity[0];
164 Listener->Params.Velocity[1] = Listener->Velocity[1];
165 Listener->Params.Velocity[2] = Listener->Velocity[2];
166 aluMatrixVector(Listener->Params.Velocity, 0.0f, Listener->Params.Matrix);
169 ALvoid CalcNonAttnSourceParams(ALactivesource *src, const ALCcontext *ALContext)
171 static const struct ChanMap MonoMap[1] = { { FrontCenter, 0.0f } };
172 static const struct ChanMap StereoMap[2] = {
173 { FrontLeft, DEG2RAD(-30.0f) },
174 { FrontRight, DEG2RAD( 30.0f) }
176 static const struct ChanMap StereoWideMap[2] = {
177 { FrontLeft, DEG2RAD(-90.0f) },
178 { FrontRight, DEG2RAD( 90.0f) }
180 static const struct ChanMap RearMap[2] = {
181 { BackLeft, DEG2RAD(-150.0f) },
182 { BackRight, DEG2RAD( 150.0f) }
184 static const struct ChanMap QuadMap[4] = {
185 { FrontLeft, DEG2RAD( -45.0f) },
186 { FrontRight, DEG2RAD( 45.0f) },
187 { BackLeft, DEG2RAD(-135.0f) },
188 { BackRight, DEG2RAD( 135.0f) }
190 static const struct ChanMap X51Map[6] = {
191 { FrontLeft, DEG2RAD( -30.0f) },
192 { FrontRight, DEG2RAD( 30.0f) },
193 { FrontCenter, DEG2RAD( 0.0f) },
194 { LFE, 0.0f },
195 { BackLeft, DEG2RAD(-110.0f) },
196 { BackRight, DEG2RAD( 110.0f) }
198 static const struct ChanMap X61Map[7] = {
199 { FrontLeft, DEG2RAD(-30.0f) },
200 { FrontRight, DEG2RAD( 30.0f) },
201 { FrontCenter, DEG2RAD( 0.0f) },
202 { LFE, 0.0f },
203 { BackCenter, DEG2RAD(180.0f) },
204 { SideLeft, DEG2RAD(-90.0f) },
205 { SideRight, DEG2RAD( 90.0f) }
207 static const struct ChanMap X71Map[8] = {
208 { FrontLeft, DEG2RAD( -30.0f) },
209 { FrontRight, DEG2RAD( 30.0f) },
210 { FrontCenter, DEG2RAD( 0.0f) },
211 { LFE, 0.0f },
212 { BackLeft, DEG2RAD(-150.0f) },
213 { BackRight, DEG2RAD( 150.0f) },
214 { SideLeft, DEG2RAD( -90.0f) },
215 { SideRight, DEG2RAD( 90.0f) }
218 ALCdevice *Device = ALContext->Device;
219 const ALsource *ALSource = src->Source;
220 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
221 ALbufferlistitem *BufferListItem;
222 enum FmtChannels Channels;
223 ALfloat DryGain, DryGainHF, DryGainLF;
224 ALfloat WetGain[MAX_SENDS];
225 ALfloat WetGainHF[MAX_SENDS];
226 ALfloat WetGainLF[MAX_SENDS];
227 ALint NumSends, Frequency;
228 const struct ChanMap *chans = NULL;
229 enum Resampler Resampler;
230 ALint num_channels = 0;
231 ALboolean DirectChannels;
232 ALfloat hwidth = 0.0f;
233 ALfloat Pitch;
234 ALint i, j, c;
236 /* Get device properties */
237 NumSends = Device->NumAuxSends;
238 Frequency = Device->Frequency;
240 /* Get listener properties */
241 ListenerGain = ALContext->Listener->Gain;
243 /* Get source properties */
244 SourceVolume = ALSource->Gain;
245 MinVolume = ALSource->MinGain;
246 MaxVolume = ALSource->MaxGain;
247 Pitch = ALSource->Pitch;
248 Resampler = ALSource->Resampler;
249 DirectChannels = ALSource->DirectChannels;
251 src->Direct.OutBuffer = Device->DryBuffer;
252 for(i = 0;i < NumSends;i++)
254 ALeffectslot *Slot = ALSource->Send[i].Slot;
255 if(!Slot && i == 0)
256 Slot = Device->DefaultSlot;
257 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
258 src->Send[i].OutBuffer = NULL;
259 else
260 src->Send[i].OutBuffer = Slot->WetBuffer;
263 /* Calculate the stepping value */
264 Channels = FmtMono;
265 BufferListItem = ALSource->queue;
266 while(BufferListItem != NULL)
268 ALbuffer *ALBuffer;
269 if((ALBuffer=BufferListItem->buffer) != NULL)
271 Pitch = Pitch * ALBuffer->Frequency / Frequency;
272 if(Pitch > (ALfloat)MAX_PITCH)
273 src->Step = MAX_PITCH<<FRACTIONBITS;
274 else
276 src->Step = fastf2i(Pitch*FRACTIONONE);
277 if(src->Step == 0)
278 src->Step = 1;
281 Channels = ALBuffer->FmtChannels;
282 break;
284 BufferListItem = BufferListItem->next;
287 /* Calculate gains */
288 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
289 DryGain *= ALSource->Direct.Gain * ListenerGain;
290 DryGainHF = ALSource->Direct.GainHF;
291 DryGainLF = ALSource->Direct.GainLF;
292 for(i = 0;i < NumSends;i++)
294 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
295 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
296 WetGainHF[i] = ALSource->Send[i].GainHF;
297 WetGainLF[i] = ALSource->Send[i].GainLF;
300 switch(Channels)
302 case FmtMono:
303 chans = MonoMap;
304 num_channels = 1;
305 break;
307 case FmtStereo:
308 if(!(Device->Flags&DEVICE_WIDE_STEREO))
310 /* HACK: Place the stereo channels at +/-90 degrees when using non-
311 * HRTF stereo output. This helps reduce the "monoization" caused
312 * by them panning towards the center. */
313 if(Device->FmtChans == DevFmtStereo && !Device->Hrtf)
314 chans = StereoWideMap;
315 else
316 chans = StereoMap;
318 else
320 chans = StereoWideMap;
321 hwidth = DEG2RAD(60.0f);
323 num_channels = 2;
324 break;
326 case FmtRear:
327 chans = RearMap;
328 num_channels = 2;
329 break;
331 case FmtQuad:
332 chans = QuadMap;
333 num_channels = 4;
334 break;
336 case FmtX51:
337 chans = X51Map;
338 num_channels = 6;
339 break;
341 case FmtX61:
342 chans = X61Map;
343 num_channels = 7;
344 break;
346 case FmtX71:
347 chans = X71Map;
348 num_channels = 8;
349 break;
352 if(DirectChannels != AL_FALSE)
354 for(c = 0;c < num_channels;c++)
356 MixGains *gains = src->Direct.Mix.Gains[c];
357 for(j = 0;j < MaxChannels;j++)
358 gains[j].Target = 0.0f;
361 for(c = 0;c < num_channels;c++)
363 MixGains *gains = src->Direct.Mix.Gains[c];
364 for(i = 0;i < (ALint)Device->NumChan;i++)
366 enum Channel chan = Device->Speaker2Chan[i];
367 if(chan == chans[c].channel)
369 gains[chan].Target = DryGain;
370 break;
375 if(!src->Direct.Moving)
377 for(i = 0;i < num_channels;i++)
379 MixGains *gains = src->Direct.Mix.Gains[i];
380 for(j = 0;j < MaxChannels;j++)
382 gains[j].Current = gains[j].Target;
383 gains[j].Step = 1.0f;
386 src->Direct.Counter = 0;
387 src->Direct.Moving = AL_TRUE;
389 else
391 for(i = 0;i < num_channels;i++)
393 MixGains *gains = src->Direct.Mix.Gains[i];
394 for(j = 0;j < MaxChannels;j++)
396 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
397 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
398 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
399 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
400 else
401 gains[j].Step = 1.0f;
402 gains[j].Current = cur;
405 src->Direct.Counter = 64;
408 src->IsHrtf = AL_FALSE;
410 else if(Device->Hrtf)
412 for(c = 0;c < num_channels;c++)
414 if(chans[c].channel == LFE)
416 /* Skip LFE */
417 src->Direct.Mix.Hrtf.Params[c].Delay[0] = 0;
418 src->Direct.Mix.Hrtf.Params[c].Delay[1] = 0;
419 for(i = 0;i < HRIR_LENGTH;i++)
421 src->Direct.Mix.Hrtf.Params[c].Coeffs[i][0] = 0.0f;
422 src->Direct.Mix.Hrtf.Params[c].Coeffs[i][1] = 0.0f;
425 else
427 /* Get the static HRIR coefficients and delays for this
428 * channel. */
429 GetLerpedHrtfCoeffs(Device->Hrtf,
430 0.0f, chans[c].angle, DryGain,
431 src->Direct.Mix.Hrtf.Params[c].Coeffs,
432 src->Direct.Mix.Hrtf.Params[c].Delay);
435 src->Direct.Counter = 0;
436 src->Direct.Moving = AL_TRUE;
437 src->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
439 src->IsHrtf = AL_TRUE;
441 else
443 for(i = 0;i < num_channels;i++)
445 MixGains *gains = src->Direct.Mix.Gains[i];
446 for(j = 0;j < MaxChannels;j++)
447 gains[j].Target = 0.0f;
450 DryGain *= lerp(1.0f, 1.0f/sqrtf((float)Device->NumChan), hwidth/F_PI);
451 for(c = 0;c < num_channels;c++)
453 MixGains *gains = src->Direct.Mix.Gains[c];
454 ALfloat Target[MaxChannels];
456 /* Special-case LFE */
457 if(chans[c].channel == LFE)
459 gains[chans[c].channel].Target = DryGain;
460 continue;
462 ComputeAngleGains(Device, chans[c].angle, hwidth, DryGain, Target);
463 for(i = 0;i < MaxChannels;i++)
464 gains[i].Target = Target[i];
467 if(!src->Direct.Moving)
469 for(i = 0;i < num_channels;i++)
471 MixGains *gains = src->Direct.Mix.Gains[i];
472 for(j = 0;j < MaxChannels;j++)
474 gains[j].Current = gains[j].Target;
475 gains[j].Step = 1.0f;
478 src->Direct.Counter = 0;
479 src->Direct.Moving = AL_TRUE;
481 else
483 for(i = 0;i < num_channels;i++)
485 MixGains *gains = src->Direct.Mix.Gains[i];
486 for(j = 0;j < MaxChannels;j++)
488 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
489 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
490 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
491 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
492 else
493 gains[j].Step = 1.0f;
494 gains[j].Current = cur;
497 src->Direct.Counter = 64;
500 src->IsHrtf = AL_FALSE;
502 for(i = 0;i < NumSends;i++)
504 src->Send[i].Gain.Target = WetGain[i];
505 if(!src->Send[i].Moving)
507 src->Send[i].Gain.Current = src->Send[i].Gain.Target;
508 src->Send[i].Gain.Step = 1.0f;
509 src->Send[i].Counter = 0;
510 src->Send[i].Moving = AL_TRUE;
512 else
514 ALfloat cur = maxf(src->Send[i].Gain.Current, FLT_EPSILON);
515 ALfloat trg = maxf(src->Send[i].Gain.Target, FLT_EPSILON);
516 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
517 src->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
518 else
519 src->Send[i].Gain.Step = 1.0f;
520 src->Send[i].Gain.Current = cur;
521 src->Send[i].Counter = 64;
526 ALfloat gainhf = maxf(0.01f, DryGainHF);
527 ALfloat gainlf = maxf(0.01f, DryGainLF);
528 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
529 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
530 for(c = 0;c < num_channels;c++)
532 src->Direct.Filters[c].ActiveType = AF_None;
533 if(gainhf != 1.0f) src->Direct.Filters[c].ActiveType |= AF_LowPass;
534 if(gainlf != 1.0f) src->Direct.Filters[c].ActiveType |= AF_HighPass;
535 ALfilterState_setParams(
536 &src->Direct.Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
537 hfscale, 0.0f
539 ALfilterState_setParams(
540 &src->Direct.Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
541 lfscale, 0.0f
545 for(i = 0;i < NumSends;i++)
547 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
548 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
549 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
550 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
551 for(c = 0;c < num_channels;c++)
553 src->Send[i].Filters[c].ActiveType = AF_None;
554 if(gainhf != 1.0f) src->Send[i].Filters[c].ActiveType |= AF_LowPass;
555 if(gainlf != 1.0f) src->Send[i].Filters[c].ActiveType |= AF_HighPass;
556 ALfilterState_setParams(
557 &src->Send[i].Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
558 hfscale, 0.0f
560 ALfilterState_setParams(
561 &src->Send[i].Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
562 lfscale, 0.0f
568 ALvoid CalcSourceParams(ALactivesource *src, const ALCcontext *ALContext)
570 ALCdevice *Device = ALContext->Device;
571 const ALsource *ALSource = src->Source;
572 ALfloat Velocity[3],Direction[3],Position[3],SourceToListener[3];
573 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
574 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
575 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
576 ALfloat DopplerFactor, SpeedOfSound;
577 ALfloat AirAbsorptionFactor;
578 ALfloat RoomAirAbsorption[MAX_SENDS];
579 ALbufferlistitem *BufferListItem;
580 ALfloat Attenuation;
581 ALfloat RoomAttenuation[MAX_SENDS];
582 ALfloat MetersPerUnit;
583 ALfloat RoomRolloffBase;
584 ALfloat RoomRolloff[MAX_SENDS];
585 ALfloat DecayDistance[MAX_SENDS];
586 ALfloat DryGain;
587 ALfloat DryGainHF;
588 ALfloat DryGainLF;
589 ALboolean DryGainHFAuto;
590 ALfloat WetGain[MAX_SENDS];
591 ALfloat WetGainHF[MAX_SENDS];
592 ALfloat WetGainLF[MAX_SENDS];
593 ALboolean WetGainAuto;
594 ALboolean WetGainHFAuto;
595 enum Resampler Resampler;
596 ALfloat Pitch;
597 ALuint Frequency;
598 ALint NumSends;
599 ALint i, j;
601 DryGainHF = 1.0f;
602 DryGainLF = 1.0f;
603 for(i = 0;i < MAX_SENDS;i++)
605 WetGainHF[i] = 1.0f;
606 WetGainLF[i] = 1.0f;
609 /* Get context/device properties */
610 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
611 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
612 NumSends = Device->NumAuxSends;
613 Frequency = Device->Frequency;
615 /* Get listener properties */
616 ListenerGain = ALContext->Listener->Gain;
617 MetersPerUnit = ALContext->Listener->MetersPerUnit;
619 /* Get source properties */
620 SourceVolume = ALSource->Gain;
621 MinVolume = ALSource->MinGain;
622 MaxVolume = ALSource->MaxGain;
623 Pitch = ALSource->Pitch;
624 Resampler = ALSource->Resampler;
625 Position[0] = ALSource->Position[0];
626 Position[1] = ALSource->Position[1];
627 Position[2] = ALSource->Position[2];
628 Direction[0] = ALSource->Orientation[0];
629 Direction[1] = ALSource->Orientation[1];
630 Direction[2] = ALSource->Orientation[2];
631 Velocity[0] = ALSource->Velocity[0];
632 Velocity[1] = ALSource->Velocity[1];
633 Velocity[2] = ALSource->Velocity[2];
634 MinDist = ALSource->RefDistance;
635 MaxDist = ALSource->MaxDistance;
636 Rolloff = ALSource->RollOffFactor;
637 InnerAngle = ALSource->InnerAngle;
638 OuterAngle = ALSource->OuterAngle;
639 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
640 DryGainHFAuto = ALSource->DryGainHFAuto;
641 WetGainAuto = ALSource->WetGainAuto;
642 WetGainHFAuto = ALSource->WetGainHFAuto;
643 RoomRolloffBase = ALSource->RoomRolloffFactor;
645 src->Direct.OutBuffer = Device->DryBuffer;
646 for(i = 0;i < NumSends;i++)
648 ALeffectslot *Slot = ALSource->Send[i].Slot;
650 if(!Slot && i == 0)
651 Slot = Device->DefaultSlot;
652 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
654 Slot = NULL;
655 RoomRolloff[i] = 0.0f;
656 DecayDistance[i] = 0.0f;
657 RoomAirAbsorption[i] = 1.0f;
659 else if(Slot->AuxSendAuto)
661 RoomRolloff[i] = RoomRolloffBase;
662 if(IsReverbEffect(Slot->EffectType))
664 RoomRolloff[i] += Slot->EffectProps.Reverb.RoomRolloffFactor;
665 DecayDistance[i] = Slot->EffectProps.Reverb.DecayTime *
666 SPEEDOFSOUNDMETRESPERSEC;
667 RoomAirAbsorption[i] = Slot->EffectProps.Reverb.AirAbsorptionGainHF;
669 else
671 DecayDistance[i] = 0.0f;
672 RoomAirAbsorption[i] = 1.0f;
675 else
677 /* If the slot's auxiliary send auto is off, the data sent to the
678 * effect slot is the same as the dry path, sans filter effects */
679 RoomRolloff[i] = Rolloff;
680 DecayDistance[i] = 0.0f;
681 RoomAirAbsorption[i] = AIRABSORBGAINHF;
684 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
685 src->Send[i].OutBuffer = NULL;
686 else
687 src->Send[i].OutBuffer = Slot->WetBuffer;
690 /* Transform source to listener space (convert to head relative) */
691 if(ALSource->HeadRelative == AL_FALSE)
693 ALfloat (*restrict Matrix)[4] = ALContext->Listener->Params.Matrix;
694 /* Transform source vectors */
695 aluMatrixVector(Position, 1.0f, Matrix);
696 aluMatrixVector(Direction, 0.0f, Matrix);
697 aluMatrixVector(Velocity, 0.0f, Matrix);
699 else
701 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
702 /* Offset the source velocity to be relative of the listener velocity */
703 Velocity[0] += ListenerVel[0];
704 Velocity[1] += ListenerVel[1];
705 Velocity[2] += ListenerVel[2];
708 SourceToListener[0] = -Position[0];
709 SourceToListener[1] = -Position[1];
710 SourceToListener[2] = -Position[2];
711 aluNormalize(SourceToListener);
712 aluNormalize(Direction);
714 /* Calculate distance attenuation */
715 Distance = sqrtf(aluDotproduct(Position, Position));
716 ClampedDist = Distance;
718 Attenuation = 1.0f;
719 for(i = 0;i < NumSends;i++)
720 RoomAttenuation[i] = 1.0f;
721 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
722 ALContext->DistanceModel)
724 case InverseDistanceClamped:
725 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
726 if(MaxDist < MinDist)
727 break;
728 /*fall-through*/
729 case InverseDistance:
730 if(MinDist > 0.0f)
732 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
733 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
734 for(i = 0;i < NumSends;i++)
736 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
737 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
740 break;
742 case LinearDistanceClamped:
743 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
744 if(MaxDist < MinDist)
745 break;
746 /*fall-through*/
747 case LinearDistance:
748 if(MaxDist != MinDist)
750 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
751 Attenuation = maxf(Attenuation, 0.0f);
752 for(i = 0;i < NumSends;i++)
754 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
755 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
758 break;
760 case ExponentDistanceClamped:
761 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
762 if(MaxDist < MinDist)
763 break;
764 /*fall-through*/
765 case ExponentDistance:
766 if(ClampedDist > 0.0f && MinDist > 0.0f)
768 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
769 for(i = 0;i < NumSends;i++)
770 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
772 break;
774 case DisableDistance:
775 ClampedDist = MinDist;
776 break;
779 /* Source Gain + Attenuation */
780 DryGain = SourceVolume * Attenuation;
781 for(i = 0;i < NumSends;i++)
782 WetGain[i] = SourceVolume * RoomAttenuation[i];
784 /* Distance-based air absorption */
785 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
787 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
788 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
789 for(i = 0;i < NumSends;i++)
790 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
793 if(WetGainAuto)
795 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
797 /* Apply a decay-time transformation to the wet path, based on the
798 * attenuation of the dry path.
800 * Using the apparent distance, based on the distance attenuation, the
801 * initial decay of the reverb effect is calculated and applied to the
802 * wet path.
804 for(i = 0;i < NumSends;i++)
806 if(DecayDistance[i] > 0.0f)
807 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
811 /* Calculate directional soundcones */
812 Angle = RAD2DEG(acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale) * 2.0f;
813 if(Angle > InnerAngle && Angle <= OuterAngle)
815 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
816 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
817 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
819 else if(Angle > OuterAngle)
821 ConeVolume = ALSource->OuterGain;
822 ConeHF = ALSource->OuterGainHF;
824 else
826 ConeVolume = 1.0f;
827 ConeHF = 1.0f;
830 DryGain *= ConeVolume;
831 if(WetGainAuto)
833 for(i = 0;i < NumSends;i++)
834 WetGain[i] *= ConeVolume;
836 if(DryGainHFAuto)
837 DryGainHF *= ConeHF;
838 if(WetGainHFAuto)
840 for(i = 0;i < NumSends;i++)
841 WetGainHF[i] *= ConeHF;
844 /* Clamp to Min/Max Gain */
845 DryGain = clampf(DryGain, MinVolume, MaxVolume);
846 for(i = 0;i < NumSends;i++)
847 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
849 /* Apply gain and frequency filters */
850 DryGain *= ALSource->Direct.Gain * ListenerGain;
851 DryGainHF *= ALSource->Direct.GainHF;
852 DryGainLF *= ALSource->Direct.GainLF;
853 for(i = 0;i < NumSends;i++)
855 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
856 WetGainHF[i] *= ALSource->Send[i].GainHF;
857 WetGainLF[i] *= ALSource->Send[i].GainLF;
860 /* Calculate velocity-based doppler effect */
861 if(DopplerFactor > 0.0f)
863 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
864 ALfloat VSS, VLS;
866 if(SpeedOfSound < 1.0f)
868 DopplerFactor *= 1.0f/SpeedOfSound;
869 SpeedOfSound = 1.0f;
872 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
873 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
875 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
876 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
879 BufferListItem = ALSource->queue;
880 while(BufferListItem != NULL)
882 ALbuffer *ALBuffer;
883 if((ALBuffer=BufferListItem->buffer) != NULL)
885 /* Calculate fixed-point stepping value, based on the pitch, buffer
886 * frequency, and output frequency. */
887 Pitch = Pitch * ALBuffer->Frequency / Frequency;
888 if(Pitch > (ALfloat)MAX_PITCH)
889 src->Step = MAX_PITCH<<FRACTIONBITS;
890 else
892 src->Step = fastf2i(Pitch*FRACTIONONE);
893 if(src->Step == 0)
894 src->Step = 1;
897 break;
899 BufferListItem = BufferListItem->next;
902 if(Device->Hrtf)
904 /* Use a binaural HRTF algorithm for stereo headphone playback */
905 ALfloat delta, ev = 0.0f, az = 0.0f;
907 if(Distance > FLT_EPSILON)
909 ALfloat invlen = 1.0f/Distance;
910 Position[0] *= invlen;
911 Position[1] *= invlen;
912 Position[2] *= invlen;
914 /* Calculate elevation and azimuth only when the source is not at
915 * the listener. This prevents +0 and -0 Z from producing
916 * inconsistent panning. Also, clamp Y in case FP precision errors
917 * cause it to land outside of -1..+1. */
918 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
919 az = atan2f(Position[0], -Position[2]*ZScale);
922 /* Check to see if the HRIR is already moving. */
923 if(src->Direct.Moving)
925 /* Calculate the normalized HRTF transition factor (delta). */
926 delta = CalcHrtfDelta(src->Direct.Mix.Hrtf.Gain, DryGain,
927 src->Direct.Mix.Hrtf.Dir, Position);
928 /* If the delta is large enough, get the moving HRIR target
929 * coefficients, target delays, steppping values, and counter. */
930 if(delta > 0.001f)
932 ALuint counter = GetMovingHrtfCoeffs(Device->Hrtf,
933 ev, az, DryGain, delta,
934 src->Direct.Counter,
935 src->Direct.Mix.Hrtf.Params[0].Coeffs,
936 src->Direct.Mix.Hrtf.Params[0].Delay,
937 src->Direct.Mix.Hrtf.Params[0].CoeffStep,
938 src->Direct.Mix.Hrtf.Params[0].DelayStep);
939 src->Direct.Counter = counter;
940 src->Direct.Mix.Hrtf.Gain = DryGain;
941 src->Direct.Mix.Hrtf.Dir[0] = Position[0];
942 src->Direct.Mix.Hrtf.Dir[1] = Position[1];
943 src->Direct.Mix.Hrtf.Dir[2] = Position[2];
946 else
948 /* Get the initial (static) HRIR coefficients and delays. */
949 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, DryGain,
950 src->Direct.Mix.Hrtf.Params[0].Coeffs,
951 src->Direct.Mix.Hrtf.Params[0].Delay);
952 src->Direct.Counter = 0;
953 src->Direct.Moving = AL_TRUE;
954 src->Direct.Mix.Hrtf.Gain = DryGain;
955 src->Direct.Mix.Hrtf.Dir[0] = Position[0];
956 src->Direct.Mix.Hrtf.Dir[1] = Position[1];
957 src->Direct.Mix.Hrtf.Dir[2] = Position[2];
959 src->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
961 src->IsHrtf = AL_TRUE;
963 else
965 MixGains *gains = src->Direct.Mix.Gains[0];
966 ALfloat DirGain = 0.0f;
967 ALfloat AmbientGain;
969 for(j = 0;j < MaxChannels;j++)
970 gains[j].Target = 0.0f;
972 /* Normalize the length, and compute panned gains. */
973 if(Distance > FLT_EPSILON)
975 ALfloat Target[MaxChannels];
976 ALfloat invlen = 1.0f/Distance;
977 Position[0] *= invlen;
978 Position[1] *= invlen;
979 Position[2] *= invlen;
981 DirGain = sqrtf(Position[0]*Position[0] + Position[2]*Position[2]);
982 ComputeAngleGains(Device, atan2f(Position[0], -Position[2]*ZScale), 0.0f,
983 DryGain*DirGain, Target);
984 for(j = 0;j < MaxChannels;j++)
985 gains[j].Target = Target[j];
988 /* Adjustment for vertical offsets. Not the greatest, but simple
989 * enough. */
990 AmbientGain = DryGain * sqrtf(1.0f/Device->NumChan) * (1.0f-DirGain);
991 for(i = 0;i < (ALint)Device->NumChan;i++)
993 enum Channel chan = Device->Speaker2Chan[i];
994 gains[chan].Target = maxf(gains[chan].Target, AmbientGain);
997 if(!src->Direct.Moving)
999 for(j = 0;j < MaxChannels;j++)
1001 gains[j].Current = gains[j].Target;
1002 gains[j].Step = 1.0f;
1004 src->Direct.Counter = 0;
1005 src->Direct.Moving = AL_TRUE;
1007 else
1009 for(j = 0;j < MaxChannels;j++)
1011 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
1012 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
1013 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1014 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
1015 else
1016 gains[j].Step = 1.0f;
1017 gains[j].Current = cur;
1019 src->Direct.Counter = 64;
1022 src->IsHrtf = AL_FALSE;
1024 for(i = 0;i < NumSends;i++)
1026 src->Send[i].Gain.Target = WetGain[i];
1027 if(!src->Send[i].Moving)
1029 src->Send[i].Gain.Current = src->Send[i].Gain.Target;
1030 src->Send[i].Gain.Step = 1.0f;
1031 src->Send[i].Counter = 0;
1032 src->Send[i].Moving = AL_TRUE;
1034 else
1036 ALfloat cur = maxf(src->Send[i].Gain.Current, FLT_EPSILON);
1037 ALfloat trg = maxf(src->Send[i].Gain.Target, FLT_EPSILON);
1038 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1039 src->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
1040 else
1041 src->Send[i].Gain.Step = 1.0f;
1042 src->Send[i].Gain.Current = cur;
1043 src->Send[i].Counter = 64;
1048 ALfloat gainhf = maxf(0.01f, DryGainHF);
1049 ALfloat gainlf = maxf(0.01f, DryGainLF);
1050 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
1051 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
1052 src->Direct.Filters[0].ActiveType = AF_None;
1053 if(gainhf != 1.0f) src->Direct.Filters[0].ActiveType |= AF_LowPass;
1054 if(gainlf != 1.0f) src->Direct.Filters[0].ActiveType |= AF_HighPass;
1055 ALfilterState_setParams(
1056 &src->Direct.Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
1057 hfscale, 0.0f
1059 ALfilterState_setParams(
1060 &src->Direct.Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1061 lfscale, 0.0f
1064 for(i = 0;i < NumSends;i++)
1066 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
1067 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
1068 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
1069 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
1070 src->Send[i].Filters[0].ActiveType = AF_None;
1071 if(gainhf != 1.0f) src->Send[i].Filters[0].ActiveType |= AF_LowPass;
1072 if(gainlf != 1.0f) src->Send[i].Filters[0].ActiveType |= AF_HighPass;
1073 ALfilterState_setParams(
1074 &src->Send[i].Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
1075 hfscale, 0.0f
1077 ALfilterState_setParams(
1078 &src->Send[i].Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1079 lfscale, 0.0f
1085 static inline ALint aluF2I25(ALfloat val)
1087 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1088 if(fabsf(val) > 1.0f)
1089 val = (ALfloat)((0.0f < val) - (val < 0.0f));
1090 /* Convert to a signed integer, between -16777215 and +16777215. */
1091 return fastf2i(val*16777215.0f);
1094 static inline ALfloat aluF2F(ALfloat val)
1095 { return val; }
1096 static inline ALint aluF2I(ALfloat val)
1097 { return aluF2I25(val)<<7; }
1098 static inline ALuint aluF2UI(ALfloat val)
1099 { return aluF2I(val)+2147483648u; }
1100 static inline ALshort aluF2S(ALfloat val)
1101 { return aluF2I25(val)>>9; }
1102 static inline ALushort aluF2US(ALfloat val)
1103 { return aluF2S(val)+32768; }
1104 static inline ALbyte aluF2B(ALfloat val)
1105 { return aluF2I25(val)>>17; }
1106 static inline ALubyte aluF2UB(ALfloat val)
1107 { return aluF2B(val)+128; }
1109 #define DECL_TEMPLATE(T, func) \
1110 static void Write_##T(ALCdevice *device, ALvoid **buffer, ALuint SamplesToDo) \
1112 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
1113 const ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
1114 const ALuint *offsets = device->ChannelOffsets; \
1115 ALuint i, j; \
1117 for(j = 0;j < MaxChannels;j++) \
1119 T *restrict out; \
1121 if(offsets[j] == INVALID_OFFSET) \
1122 continue; \
1124 out = (T*)(*buffer) + offsets[j]; \
1125 for(i = 0;i < SamplesToDo;i++) \
1126 out[i*numchans] = func(DryBuffer[j][i]); \
1128 *buffer = (char*)(*buffer) + SamplesToDo*numchans*sizeof(T); \
1131 DECL_TEMPLATE(ALfloat, aluF2F)
1132 DECL_TEMPLATE(ALuint, aluF2UI)
1133 DECL_TEMPLATE(ALint, aluF2I)
1134 DECL_TEMPLATE(ALushort, aluF2US)
1135 DECL_TEMPLATE(ALshort, aluF2S)
1136 DECL_TEMPLATE(ALubyte, aluF2UB)
1137 DECL_TEMPLATE(ALbyte, aluF2B)
1139 #undef DECL_TEMPLATE
1142 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1144 ALuint SamplesToDo;
1145 ALeffectslot **slot, **slot_end;
1146 ALactivesource **src, **src_end;
1147 ALCcontext *ctx;
1148 FPUCtl oldMode;
1149 ALuint i, c;
1151 SetMixerFPUMode(&oldMode);
1153 while(size > 0)
1155 IncrementRef(&device->MixCount);
1157 SamplesToDo = minu(size, BUFFERSIZE);
1158 for(c = 0;c < MaxChannels;c++)
1159 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
1161 ALCdevice_Lock(device);
1162 V(device->Synth,process)(SamplesToDo, device->DryBuffer);
1164 ctx = device->ContextList;
1165 while(ctx)
1167 ALenum DeferUpdates = ctx->DeferUpdates;
1168 ALenum UpdateSources = AL_FALSE;
1170 if(!DeferUpdates)
1171 UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
1173 if(UpdateSources)
1174 CalcListenerParams(ctx->Listener);
1176 /* source processing */
1177 src = ctx->ActiveSources;
1178 src_end = src + ctx->ActiveSourceCount;
1179 while(src != src_end)
1181 ALsource *source = (*src)->Source;
1183 if(source->state != AL_PLAYING && source->state != AL_PAUSED)
1185 ALactivesource *temp = *(--src_end);
1186 *src_end = *src;
1187 *src = temp;
1188 --(ctx->ActiveSourceCount);
1189 continue;
1192 if(!DeferUpdates && (ExchangeInt(&source->NeedsUpdate, AL_FALSE) ||
1193 UpdateSources))
1194 (*src)->Update(*src, ctx);
1196 if(source->state != AL_PAUSED)
1197 MixSource(*src, device, SamplesToDo);
1198 src++;
1201 /* effect slot processing */
1202 slot = VECTOR_ITER_BEGIN(ctx->ActiveAuxSlots);
1203 slot_end = VECTOR_ITER_END(ctx->ActiveAuxSlots);
1204 while(slot != slot_end)
1206 if(!DeferUpdates && ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1207 V((*slot)->EffectState,update)(device, *slot);
1209 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1210 device->DryBuffer);
1212 for(i = 0;i < SamplesToDo;i++)
1213 (*slot)->WetBuffer[0][i] = 0.0f;
1215 slot++;
1218 ctx = ctx->next;
1221 slot = &device->DefaultSlot;
1222 if(*slot != NULL)
1224 if(ExchangeInt(&(*slot)->NeedsUpdate, AL_FALSE))
1225 V((*slot)->EffectState,update)(device, *slot);
1227 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1228 device->DryBuffer);
1230 for(i = 0;i < SamplesToDo;i++)
1231 (*slot)->WetBuffer[0][i] = 0.0f;
1234 /* Increment the clock time. Every second's worth of samples is
1235 * converted and added to clock base so that large sample counts don't
1236 * overflow during conversion. This also guarantees an exact, stable
1237 * conversion. */
1238 device->SamplesDone += SamplesToDo;
1239 device->ClockBase += (device->SamplesDone/device->Frequency) * DEVICE_CLOCK_RES;
1240 device->SamplesDone %= device->Frequency;
1241 ALCdevice_Unlock(device);
1243 if(device->Bs2b)
1245 /* Apply binaural/crossfeed filter */
1246 for(i = 0;i < SamplesToDo;i++)
1248 float samples[2];
1249 samples[0] = device->DryBuffer[FrontLeft][i];
1250 samples[1] = device->DryBuffer[FrontRight][i];
1251 bs2b_cross_feed(device->Bs2b, samples);
1252 device->DryBuffer[FrontLeft][i] = samples[0];
1253 device->DryBuffer[FrontRight][i] = samples[1];
1257 if(buffer)
1259 switch(device->FmtType)
1261 case DevFmtByte:
1262 Write_ALbyte(device, &buffer, SamplesToDo);
1263 break;
1264 case DevFmtUByte:
1265 Write_ALubyte(device, &buffer, SamplesToDo);
1266 break;
1267 case DevFmtShort:
1268 Write_ALshort(device, &buffer, SamplesToDo);
1269 break;
1270 case DevFmtUShort:
1271 Write_ALushort(device, &buffer, SamplesToDo);
1272 break;
1273 case DevFmtInt:
1274 Write_ALint(device, &buffer, SamplesToDo);
1275 break;
1276 case DevFmtUInt:
1277 Write_ALuint(device, &buffer, SamplesToDo);
1278 break;
1279 case DevFmtFloat:
1280 Write_ALfloat(device, &buffer, SamplesToDo);
1281 break;
1285 size -= SamplesToDo;
1286 IncrementRef(&device->MixCount);
1289 RestoreFPUMode(&oldMode);
1293 ALvoid aluHandleDisconnect(ALCdevice *device)
1295 ALCcontext *Context;
1297 device->Connected = ALC_FALSE;
1299 Context = device->ContextList;
1300 while(Context)
1302 ALactivesource **src, **src_end;
1304 src = Context->ActiveSources;
1305 src_end = src + Context->ActiveSourceCount;
1306 while(src != src_end)
1308 ALsource *source = (*src)->Source;
1309 if(source->state == AL_PLAYING)
1311 source->state = AL_STOPPED;
1312 source->current_buffer = NULL;
1313 source->position = 0;
1314 source->position_fraction = 0;
1316 src++;
1318 Context->ActiveSourceCount = 0;
1320 Context = Context->next;