Use an ambisonics-based panning method
[openal-soft.git] / Alc / ALu.c
blobeffa06b45fbdb8251a4254e93b64d7437a8d43ae
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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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(ALvoice *voice, const ALsource *ALSource, 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 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
220 ALbufferlistitem *BufferListItem;
221 enum FmtChannels Channels;
222 ALfloat DryGain, DryGainHF, DryGainLF;
223 ALfloat WetGain[MAX_SENDS];
224 ALfloat WetGainHF[MAX_SENDS];
225 ALfloat WetGainLF[MAX_SENDS];
226 ALint NumSends, Frequency;
227 const struct ChanMap *chans = NULL;
228 ALint num_channels = 0;
229 ALboolean DirectChannels;
230 ALfloat hwidth = 0.0f;
231 ALfloat Pitch;
232 ALint i, j, c;
234 /* Get device properties */
235 NumSends = Device->NumAuxSends;
236 Frequency = Device->Frequency;
238 /* Get listener properties */
239 ListenerGain = ALContext->Listener->Gain;
241 /* Get source properties */
242 SourceVolume = ALSource->Gain;
243 MinVolume = ALSource->MinGain;
244 MaxVolume = ALSource->MaxGain;
245 Pitch = ALSource->Pitch;
246 DirectChannels = ALSource->DirectChannels;
248 voice->Direct.OutBuffer = Device->DryBuffer;
249 for(i = 0;i < NumSends;i++)
251 ALeffectslot *Slot = ALSource->Send[i].Slot;
252 if(!Slot && i == 0)
253 Slot = Device->DefaultSlot;
254 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
255 voice->Send[i].OutBuffer = NULL;
256 else
257 voice->Send[i].OutBuffer = Slot->WetBuffer;
260 /* Calculate the stepping value */
261 Channels = FmtMono;
262 BufferListItem = ATOMIC_LOAD(&ALSource->queue);
263 while(BufferListItem != NULL)
265 ALbuffer *ALBuffer;
266 if((ALBuffer=BufferListItem->buffer) != NULL)
268 Pitch = Pitch * ALBuffer->Frequency / Frequency;
269 if(Pitch > (ALfloat)MAX_PITCH)
270 voice->Step = MAX_PITCH<<FRACTIONBITS;
271 else
273 voice->Step = fastf2i(Pitch*FRACTIONONE);
274 if(voice->Step == 0)
275 voice->Step = 1;
278 Channels = ALBuffer->FmtChannels;
279 break;
281 BufferListItem = BufferListItem->next;
284 /* Calculate gains */
285 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
286 DryGain *= ALSource->Direct.Gain * ListenerGain;
287 DryGainHF = ALSource->Direct.GainHF;
288 DryGainLF = ALSource->Direct.GainLF;
289 for(i = 0;i < NumSends;i++)
291 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
292 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
293 WetGainHF[i] = ALSource->Send[i].GainHF;
294 WetGainLF[i] = ALSource->Send[i].GainLF;
297 switch(Channels)
299 case FmtMono:
300 chans = MonoMap;
301 num_channels = 1;
302 break;
304 case FmtStereo:
305 if(!(Device->Flags&DEVICE_WIDE_STEREO))
307 /* HACK: Place the stereo channels at +/-90 degrees when using non-
308 * HRTF stereo output. This helps reduce the "monoization" caused
309 * by them panning towards the center. */
310 if(Device->FmtChans == DevFmtStereo && !Device->Hrtf)
311 chans = StereoWideMap;
312 else
313 chans = StereoMap;
315 else
317 chans = StereoWideMap;
318 hwidth = DEG2RAD(60.0f);
320 num_channels = 2;
321 break;
323 case FmtRear:
324 chans = RearMap;
325 num_channels = 2;
326 break;
328 case FmtQuad:
329 chans = QuadMap;
330 num_channels = 4;
331 break;
333 case FmtX51:
334 chans = X51Map;
335 num_channels = 6;
336 break;
338 case FmtX61:
339 chans = X61Map;
340 num_channels = 7;
341 break;
343 case FmtX71:
344 chans = X71Map;
345 num_channels = 8;
346 break;
349 if(DirectChannels != AL_FALSE)
351 for(c = 0;c < num_channels;c++)
353 MixGains *gains = voice->Direct.Mix.Gains[c];
354 for(j = 0;j < MaxChannels;j++)
355 gains[j].Target = 0.0f;
358 for(c = 0;c < num_channels;c++)
360 MixGains *gains = voice->Direct.Mix.Gains[c];
361 for(i = 0;i < (ALint)Device->NumSpeakers;i++)
363 enum Channel chan = Device->Speaker[i].ChanName;
364 if(chan == chans[c].channel)
366 gains[chan].Target = DryGain;
367 break;
372 if(!voice->Direct.Moving)
374 for(i = 0;i < num_channels;i++)
376 MixGains *gains = voice->Direct.Mix.Gains[i];
377 for(j = 0;j < MaxChannels;j++)
379 gains[j].Current = gains[j].Target;
380 gains[j].Step = 1.0f;
383 voice->Direct.Counter = 0;
384 voice->Direct.Moving = AL_TRUE;
386 else
388 for(i = 0;i < num_channels;i++)
390 MixGains *gains = voice->Direct.Mix.Gains[i];
391 for(j = 0;j < MaxChannels;j++)
393 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
394 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
395 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
396 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
397 else
398 gains[j].Step = 1.0f;
399 gains[j].Current = cur;
402 voice->Direct.Counter = 64;
405 voice->IsHrtf = AL_FALSE;
407 else if(Device->Hrtf)
409 for(c = 0;c < num_channels;c++)
411 if(chans[c].channel == LFE)
413 /* Skip LFE */
414 voice->Direct.Mix.Hrtf.Params[c].Delay[0] = 0;
415 voice->Direct.Mix.Hrtf.Params[c].Delay[1] = 0;
416 for(i = 0;i < HRIR_LENGTH;i++)
418 voice->Direct.Mix.Hrtf.Params[c].Coeffs[i][0] = 0.0f;
419 voice->Direct.Mix.Hrtf.Params[c].Coeffs[i][1] = 0.0f;
422 else
424 /* Get the static HRIR coefficients and delays for this
425 * channel. */
426 GetLerpedHrtfCoeffs(Device->Hrtf,
427 0.0f, chans[c].angle, 1.0f, DryGain,
428 voice->Direct.Mix.Hrtf.Params[c].Coeffs,
429 voice->Direct.Mix.Hrtf.Params[c].Delay);
432 voice->Direct.Counter = 0;
433 voice->Direct.Moving = AL_TRUE;
434 voice->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
436 voice->IsHrtf = AL_TRUE;
438 else
440 for(i = 0;i < num_channels;i++)
442 MixGains *gains = voice->Direct.Mix.Gains[i];
443 for(j = 0;j < MaxChannels;j++)
444 gains[j].Target = 0.0f;
447 DryGain *= lerp(1.0f, 1.0f/sqrtf((float)Device->NumSpeakers), hwidth/F_PI);
448 for(c = 0;c < num_channels;c++)
450 MixGains *gains = voice->Direct.Mix.Gains[c];
451 ALfloat Target[MaxChannels];
453 /* Special-case LFE */
454 if(chans[c].channel == LFE)
456 gains[chans[c].channel].Target = DryGain;
457 continue;
459 ComputeAngleGains(Device, chans[c].angle, hwidth, DryGain, Target);
460 for(i = 0;i < MaxChannels;i++)
461 gains[i].Target = Target[i];
464 if(!voice->Direct.Moving)
466 for(i = 0;i < num_channels;i++)
468 MixGains *gains = voice->Direct.Mix.Gains[i];
469 for(j = 0;j < MaxChannels;j++)
471 gains[j].Current = gains[j].Target;
472 gains[j].Step = 1.0f;
475 voice->Direct.Counter = 0;
476 voice->Direct.Moving = AL_TRUE;
478 else
480 for(i = 0;i < num_channels;i++)
482 MixGains *gains = voice->Direct.Mix.Gains[i];
483 for(j = 0;j < MaxChannels;j++)
485 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
486 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
487 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
488 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
489 else
490 gains[j].Step = 1.0f;
491 gains[j].Current = cur;
494 voice->Direct.Counter = 64;
497 voice->IsHrtf = AL_FALSE;
499 for(i = 0;i < NumSends;i++)
501 voice->Send[i].Gain.Target = WetGain[i];
502 if(!voice->Send[i].Moving)
504 voice->Send[i].Gain.Current = voice->Send[i].Gain.Target;
505 voice->Send[i].Gain.Step = 1.0f;
506 voice->Send[i].Counter = 0;
507 voice->Send[i].Moving = AL_TRUE;
509 else
511 ALfloat cur = maxf(voice->Send[i].Gain.Current, FLT_EPSILON);
512 ALfloat trg = maxf(voice->Send[i].Gain.Target, FLT_EPSILON);
513 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
514 voice->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
515 else
516 voice->Send[i].Gain.Step = 1.0f;
517 voice->Send[i].Gain.Current = cur;
518 voice->Send[i].Counter = 64;
523 ALfloat gainhf = maxf(0.01f, DryGainHF);
524 ALfloat gainlf = maxf(0.01f, DryGainLF);
525 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
526 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
527 for(c = 0;c < num_channels;c++)
529 voice->Direct.Filters[c].ActiveType = AF_None;
530 if(gainhf != 1.0f) voice->Direct.Filters[c].ActiveType |= AF_LowPass;
531 if(gainlf != 1.0f) voice->Direct.Filters[c].ActiveType |= AF_HighPass;
532 ALfilterState_setParams(
533 &voice->Direct.Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
534 hfscale, 0.0f
536 ALfilterState_setParams(
537 &voice->Direct.Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
538 lfscale, 0.0f
542 for(i = 0;i < NumSends;i++)
544 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
545 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
546 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
547 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
548 for(c = 0;c < num_channels;c++)
550 voice->Send[i].Filters[c].ActiveType = AF_None;
551 if(gainhf != 1.0f) voice->Send[i].Filters[c].ActiveType |= AF_LowPass;
552 if(gainlf != 1.0f) voice->Send[i].Filters[c].ActiveType |= AF_HighPass;
553 ALfilterState_setParams(
554 &voice->Send[i].Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
555 hfscale, 0.0f
557 ALfilterState_setParams(
558 &voice->Send[i].Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
559 lfscale, 0.0f
565 ALvoid CalcSourceParams(ALvoice *voice, const ALsource *ALSource, const ALCcontext *ALContext)
567 ALCdevice *Device = ALContext->Device;
568 ALfloat Velocity[3],Direction[3],Position[3],SourceToListener[3];
569 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
570 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
571 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
572 ALfloat DopplerFactor, SpeedOfSound;
573 ALfloat AirAbsorptionFactor;
574 ALfloat RoomAirAbsorption[MAX_SENDS];
575 ALbufferlistitem *BufferListItem;
576 ALfloat Attenuation;
577 ALfloat RoomAttenuation[MAX_SENDS];
578 ALfloat MetersPerUnit;
579 ALfloat RoomRolloffBase;
580 ALfloat RoomRolloff[MAX_SENDS];
581 ALfloat DecayDistance[MAX_SENDS];
582 ALfloat DryGain;
583 ALfloat DryGainHF;
584 ALfloat DryGainLF;
585 ALboolean DryGainHFAuto;
586 ALfloat WetGain[MAX_SENDS];
587 ALfloat WetGainHF[MAX_SENDS];
588 ALfloat WetGainLF[MAX_SENDS];
589 ALboolean WetGainAuto;
590 ALboolean WetGainHFAuto;
591 ALfloat Pitch;
592 ALuint Frequency;
593 ALint NumSends;
594 ALint i, j;
596 DryGainHF = 1.0f;
597 DryGainLF = 1.0f;
598 for(i = 0;i < MAX_SENDS;i++)
600 WetGainHF[i] = 1.0f;
601 WetGainLF[i] = 1.0f;
604 /* Get context/device properties */
605 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
606 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
607 NumSends = Device->NumAuxSends;
608 Frequency = Device->Frequency;
610 /* Get listener properties */
611 ListenerGain = ALContext->Listener->Gain;
612 MetersPerUnit = ALContext->Listener->MetersPerUnit;
614 /* Get source properties */
615 SourceVolume = ALSource->Gain;
616 MinVolume = ALSource->MinGain;
617 MaxVolume = ALSource->MaxGain;
618 Pitch = ALSource->Pitch;
619 Position[0] = ALSource->Position[0];
620 Position[1] = ALSource->Position[1];
621 Position[2] = ALSource->Position[2];
622 Direction[0] = ALSource->Orientation[0];
623 Direction[1] = ALSource->Orientation[1];
624 Direction[2] = ALSource->Orientation[2];
625 Velocity[0] = ALSource->Velocity[0];
626 Velocity[1] = ALSource->Velocity[1];
627 Velocity[2] = ALSource->Velocity[2];
628 MinDist = ALSource->RefDistance;
629 MaxDist = ALSource->MaxDistance;
630 Rolloff = ALSource->RollOffFactor;
631 InnerAngle = ALSource->InnerAngle;
632 OuterAngle = ALSource->OuterAngle;
633 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
634 DryGainHFAuto = ALSource->DryGainHFAuto;
635 WetGainAuto = ALSource->WetGainAuto;
636 WetGainHFAuto = ALSource->WetGainHFAuto;
637 RoomRolloffBase = ALSource->RoomRolloffFactor;
639 voice->Direct.OutBuffer = Device->DryBuffer;
640 for(i = 0;i < NumSends;i++)
642 ALeffectslot *Slot = ALSource->Send[i].Slot;
644 if(!Slot && i == 0)
645 Slot = Device->DefaultSlot;
646 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
648 Slot = NULL;
649 RoomRolloff[i] = 0.0f;
650 DecayDistance[i] = 0.0f;
651 RoomAirAbsorption[i] = 1.0f;
653 else if(Slot->AuxSendAuto)
655 RoomRolloff[i] = RoomRolloffBase;
656 if(IsReverbEffect(Slot->EffectType))
658 RoomRolloff[i] += Slot->EffectProps.Reverb.RoomRolloffFactor;
659 DecayDistance[i] = Slot->EffectProps.Reverb.DecayTime *
660 SPEEDOFSOUNDMETRESPERSEC;
661 RoomAirAbsorption[i] = Slot->EffectProps.Reverb.AirAbsorptionGainHF;
663 else
665 DecayDistance[i] = 0.0f;
666 RoomAirAbsorption[i] = 1.0f;
669 else
671 /* If the slot's auxiliary send auto is off, the data sent to the
672 * effect slot is the same as the dry path, sans filter effects */
673 RoomRolloff[i] = Rolloff;
674 DecayDistance[i] = 0.0f;
675 RoomAirAbsorption[i] = AIRABSORBGAINHF;
678 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
679 voice->Send[i].OutBuffer = NULL;
680 else
681 voice->Send[i].OutBuffer = Slot->WetBuffer;
684 /* Transform source to listener space (convert to head relative) */
685 if(ALSource->HeadRelative == AL_FALSE)
687 ALfloat (*restrict Matrix)[4] = ALContext->Listener->Params.Matrix;
688 /* Transform source vectors */
689 aluMatrixVector(Position, 1.0f, Matrix);
690 aluMatrixVector(Direction, 0.0f, Matrix);
691 aluMatrixVector(Velocity, 0.0f, Matrix);
693 else
695 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
696 /* Offset the source velocity to be relative of the listener velocity */
697 Velocity[0] += ListenerVel[0];
698 Velocity[1] += ListenerVel[1];
699 Velocity[2] += ListenerVel[2];
702 SourceToListener[0] = -Position[0];
703 SourceToListener[1] = -Position[1];
704 SourceToListener[2] = -Position[2];
705 aluNormalize(SourceToListener);
706 aluNormalize(Direction);
708 /* Calculate distance attenuation */
709 Distance = sqrtf(aluDotproduct(Position, Position));
710 ClampedDist = Distance;
712 Attenuation = 1.0f;
713 for(i = 0;i < NumSends;i++)
714 RoomAttenuation[i] = 1.0f;
715 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
716 ALContext->DistanceModel)
718 case InverseDistanceClamped:
719 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
720 if(MaxDist < MinDist)
721 break;
722 /*fall-through*/
723 case InverseDistance:
724 if(MinDist > 0.0f)
726 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
727 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
728 for(i = 0;i < NumSends;i++)
730 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
731 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
734 break;
736 case LinearDistanceClamped:
737 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
738 if(MaxDist < MinDist)
739 break;
740 /*fall-through*/
741 case LinearDistance:
742 if(MaxDist != MinDist)
744 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
745 Attenuation = maxf(Attenuation, 0.0f);
746 for(i = 0;i < NumSends;i++)
748 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
749 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
752 break;
754 case ExponentDistanceClamped:
755 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
756 if(MaxDist < MinDist)
757 break;
758 /*fall-through*/
759 case ExponentDistance:
760 if(ClampedDist > 0.0f && MinDist > 0.0f)
762 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
763 for(i = 0;i < NumSends;i++)
764 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
766 break;
768 case DisableDistance:
769 ClampedDist = MinDist;
770 break;
773 /* Source Gain + Attenuation */
774 DryGain = SourceVolume * Attenuation;
775 for(i = 0;i < NumSends;i++)
776 WetGain[i] = SourceVolume * RoomAttenuation[i];
778 /* Distance-based air absorption */
779 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
781 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
782 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
783 for(i = 0;i < NumSends;i++)
784 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
787 if(WetGainAuto)
789 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
791 /* Apply a decay-time transformation to the wet path, based on the
792 * attenuation of the dry path.
794 * Using the apparent distance, based on the distance attenuation, the
795 * initial decay of the reverb effect is calculated and applied to the
796 * wet path.
798 for(i = 0;i < NumSends;i++)
800 if(DecayDistance[i] > 0.0f)
801 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
805 /* Calculate directional soundcones */
806 Angle = RAD2DEG(acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale) * 2.0f;
807 if(Angle > InnerAngle && Angle <= OuterAngle)
809 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
810 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
811 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
813 else if(Angle > OuterAngle)
815 ConeVolume = ALSource->OuterGain;
816 ConeHF = ALSource->OuterGainHF;
818 else
820 ConeVolume = 1.0f;
821 ConeHF = 1.0f;
824 DryGain *= ConeVolume;
825 if(WetGainAuto)
827 for(i = 0;i < NumSends;i++)
828 WetGain[i] *= ConeVolume;
830 if(DryGainHFAuto)
831 DryGainHF *= ConeHF;
832 if(WetGainHFAuto)
834 for(i = 0;i < NumSends;i++)
835 WetGainHF[i] *= ConeHF;
838 /* Clamp to Min/Max Gain */
839 DryGain = clampf(DryGain, MinVolume, MaxVolume);
840 for(i = 0;i < NumSends;i++)
841 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
843 /* Apply gain and frequency filters */
844 DryGain *= ALSource->Direct.Gain * ListenerGain;
845 DryGainHF *= ALSource->Direct.GainHF;
846 DryGainLF *= ALSource->Direct.GainLF;
847 for(i = 0;i < NumSends;i++)
849 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
850 WetGainHF[i] *= ALSource->Send[i].GainHF;
851 WetGainLF[i] *= ALSource->Send[i].GainLF;
854 /* Calculate velocity-based doppler effect */
855 if(DopplerFactor > 0.0f)
857 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
858 ALfloat VSS, VLS;
860 if(SpeedOfSound < 1.0f)
862 DopplerFactor *= 1.0f/SpeedOfSound;
863 SpeedOfSound = 1.0f;
866 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
867 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
869 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
870 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
873 BufferListItem = ATOMIC_LOAD(&ALSource->queue);
874 while(BufferListItem != NULL)
876 ALbuffer *ALBuffer;
877 if((ALBuffer=BufferListItem->buffer) != NULL)
879 /* Calculate fixed-point stepping value, based on the pitch, buffer
880 * frequency, and output frequency. */
881 Pitch = Pitch * ALBuffer->Frequency / Frequency;
882 if(Pitch > (ALfloat)MAX_PITCH)
883 voice->Step = MAX_PITCH<<FRACTIONBITS;
884 else
886 voice->Step = fastf2i(Pitch*FRACTIONONE);
887 if(voice->Step == 0)
888 voice->Step = 1;
891 break;
893 BufferListItem = BufferListItem->next;
896 if(Device->Hrtf)
898 /* Use a binaural HRTF algorithm for stereo headphone playback */
899 ALfloat delta, ev = 0.0f, az = 0.0f;
900 ALfloat radius = ALSource->Radius;
901 ALfloat dirfact = 1.0f;
903 if(Distance > FLT_EPSILON)
905 ALfloat invlen = 1.0f/Distance;
906 Position[0] *= invlen;
907 Position[1] *= invlen;
908 Position[2] *= invlen;
910 /* Calculate elevation and azimuth only when the source is not at
911 * the listener. This prevents +0 and -0 Z from producing
912 * inconsistent panning. Also, clamp Y in case FP precision errors
913 * cause it to land outside of -1..+1. */
914 ev = asinf(clampf(Position[1], -1.0f, 1.0f));
915 az = atan2f(Position[0], -Position[2]*ZScale);
917 if(radius > Distance)
918 dirfact *= Distance / radius;
920 /* Check to see if the HRIR is already moving. */
921 if(voice->Direct.Moving)
923 /* Calculate the normalized HRTF transition factor (delta). */
924 delta = CalcHrtfDelta(voice->Direct.Mix.Hrtf.Gain, DryGain,
925 voice->Direct.Mix.Hrtf.Dir, Position);
926 /* If the delta is large enough, get the moving HRIR target
927 * coefficients, target delays, steppping values, and counter. */
928 if(delta > 0.001f)
930 ALuint counter = GetMovingHrtfCoeffs(Device->Hrtf,
931 ev, az, dirfact, DryGain, delta, voice->Direct.Counter,
932 voice->Direct.Mix.Hrtf.Params[0].Coeffs, voice->Direct.Mix.Hrtf.Params[0].Delay,
933 voice->Direct.Mix.Hrtf.Params[0].CoeffStep, voice->Direct.Mix.Hrtf.Params[0].DelayStep
935 voice->Direct.Counter = counter;
936 voice->Direct.Mix.Hrtf.Gain = DryGain;
937 voice->Direct.Mix.Hrtf.Dir[0] = Position[0];
938 voice->Direct.Mix.Hrtf.Dir[1] = Position[1];
939 voice->Direct.Mix.Hrtf.Dir[2] = Position[2];
942 else
944 /* Get the initial (static) HRIR coefficients and delays. */
945 GetLerpedHrtfCoeffs(Device->Hrtf, ev, az, dirfact, DryGain,
946 voice->Direct.Mix.Hrtf.Params[0].Coeffs,
947 voice->Direct.Mix.Hrtf.Params[0].Delay);
948 voice->Direct.Counter = 0;
949 voice->Direct.Moving = AL_TRUE;
950 voice->Direct.Mix.Hrtf.Gain = DryGain;
951 voice->Direct.Mix.Hrtf.Dir[0] = Position[0];
952 voice->Direct.Mix.Hrtf.Dir[1] = Position[1];
953 voice->Direct.Mix.Hrtf.Dir[2] = Position[2];
955 voice->Direct.Mix.Hrtf.IrSize = GetHrtfIrSize(Device->Hrtf);
957 voice->IsHrtf = AL_TRUE;
959 else
961 MixGains *gains = voice->Direct.Mix.Gains[0];
963 for(j = 0;j < MaxChannels;j++)
964 gains[j].Target = 0.0f;
966 /* Normalize the length, and compute panned gains. */
967 if(!(Distance > FLT_EPSILON))
969 ALfloat gain = 1.0f / sqrtf(Device->NumSpeakers);
970 for(i = 0;i < (ALint)Device->NumSpeakers;i++)
972 enum Channel chan = Device->Speaker[i].ChanName;
973 gains[chan].Target = gain;
976 else
978 ALfloat radius = ALSource->Radius;
979 ALfloat Target[MaxChannels];
980 ALfloat invlen = 1.0f/maxf(Distance, radius);
981 Position[0] *= invlen;
982 Position[1] *= invlen;
983 Position[2] *= invlen;
985 ComputeDirectionalGains(Device, Position, DryGain, Target);
986 for(j = 0;j < MaxChannels;j++)
987 gains[j].Target = Target[j];
990 if(!voice->Direct.Moving)
992 for(j = 0;j < MaxChannels;j++)
994 gains[j].Current = gains[j].Target;
995 gains[j].Step = 1.0f;
997 voice->Direct.Counter = 0;
998 voice->Direct.Moving = AL_TRUE;
1000 else
1002 for(j = 0;j < MaxChannels;j++)
1004 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
1005 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
1006 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1007 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
1008 else
1009 gains[j].Step = 1.0f;
1010 gains[j].Current = cur;
1012 voice->Direct.Counter = 64;
1015 voice->IsHrtf = AL_FALSE;
1017 for(i = 0;i < NumSends;i++)
1019 voice->Send[i].Gain.Target = WetGain[i];
1020 if(!voice->Send[i].Moving)
1022 voice->Send[i].Gain.Current = voice->Send[i].Gain.Target;
1023 voice->Send[i].Gain.Step = 1.0f;
1024 voice->Send[i].Counter = 0;
1025 voice->Send[i].Moving = AL_TRUE;
1027 else
1029 ALfloat cur = maxf(voice->Send[i].Gain.Current, FLT_EPSILON);
1030 ALfloat trg = maxf(voice->Send[i].Gain.Target, FLT_EPSILON);
1031 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
1032 voice->Send[i].Gain.Step = powf(trg/cur, 1.0f/64.0f);
1033 else
1034 voice->Send[i].Gain.Step = 1.0f;
1035 voice->Send[i].Gain.Current = cur;
1036 voice->Send[i].Counter = 64;
1041 ALfloat gainhf = maxf(0.01f, DryGainHF);
1042 ALfloat gainlf = maxf(0.01f, DryGainLF);
1043 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
1044 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
1045 voice->Direct.Filters[0].ActiveType = AF_None;
1046 if(gainhf != 1.0f) voice->Direct.Filters[0].ActiveType |= AF_LowPass;
1047 if(gainlf != 1.0f) voice->Direct.Filters[0].ActiveType |= AF_HighPass;
1048 ALfilterState_setParams(
1049 &voice->Direct.Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
1050 hfscale, 0.0f
1052 ALfilterState_setParams(
1053 &voice->Direct.Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1054 lfscale, 0.0f
1057 for(i = 0;i < NumSends;i++)
1059 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
1060 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
1061 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
1062 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
1063 voice->Send[i].Filters[0].ActiveType = AF_None;
1064 if(gainhf != 1.0f) voice->Send[i].Filters[0].ActiveType |= AF_LowPass;
1065 if(gainlf != 1.0f) voice->Send[i].Filters[0].ActiveType |= AF_HighPass;
1066 ALfilterState_setParams(
1067 &voice->Send[i].Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
1068 hfscale, 0.0f
1070 ALfilterState_setParams(
1071 &voice->Send[i].Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1072 lfscale, 0.0f
1078 static inline ALint aluF2I25(ALfloat val)
1080 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1081 if(fabsf(val) > 1.0f)
1082 val = (ALfloat)((0.0f < val) - (val < 0.0f));
1083 /* Convert to a signed integer, between -16777215 and +16777215. */
1084 return fastf2i(val*16777215.0f);
1087 static inline ALfloat aluF2F(ALfloat val)
1088 { return val; }
1089 static inline ALint aluF2I(ALfloat val)
1090 { return aluF2I25(val)<<7; }
1091 static inline ALuint aluF2UI(ALfloat val)
1092 { return aluF2I(val)+2147483648u; }
1093 static inline ALshort aluF2S(ALfloat val)
1094 { return aluF2I25(val)>>9; }
1095 static inline ALushort aluF2US(ALfloat val)
1096 { return aluF2S(val)+32768; }
1097 static inline ALbyte aluF2B(ALfloat val)
1098 { return aluF2I25(val)>>17; }
1099 static inline ALubyte aluF2UB(ALfloat val)
1100 { return aluF2B(val)+128; }
1102 #define DECL_TEMPLATE(T, func) \
1103 static void Write_##T(ALCdevice *device, ALvoid **buffer, ALuint SamplesToDo) \
1105 ALfloat (*restrict DryBuffer)[BUFFERSIZE] = device->DryBuffer; \
1106 const ALuint numchans = ChannelsFromDevFmt(device->FmtChans); \
1107 const enum Channel *chans = device->ChannelName; \
1108 ALuint i, j; \
1110 for(j = 0;j < MaxChannels;j++) \
1112 const enum Channel c = chans[j]; \
1113 const ALfloat *in; \
1114 T *restrict out; \
1116 if(c == InvalidChannel) \
1117 continue; \
1119 in = DryBuffer[c]; \
1120 out = (T*)(*buffer) + j; \
1121 for(i = 0;i < SamplesToDo;i++) \
1122 out[i*numchans] = func(in[i]); \
1124 *buffer = (char*)(*buffer) + SamplesToDo*numchans*sizeof(T); \
1127 DECL_TEMPLATE(ALfloat, aluF2F)
1128 DECL_TEMPLATE(ALuint, aluF2UI)
1129 DECL_TEMPLATE(ALint, aluF2I)
1130 DECL_TEMPLATE(ALushort, aluF2US)
1131 DECL_TEMPLATE(ALshort, aluF2S)
1132 DECL_TEMPLATE(ALubyte, aluF2UB)
1133 DECL_TEMPLATE(ALbyte, aluF2B)
1135 #undef DECL_TEMPLATE
1138 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1140 ALuint SamplesToDo;
1141 ALeffectslot **slot, **slot_end;
1142 ALvoice *voice, *voice_end;
1143 ALCcontext *ctx;
1144 FPUCtl oldMode;
1145 ALuint i, c;
1147 SetMixerFPUMode(&oldMode);
1149 while(size > 0)
1151 IncrementRef(&device->MixCount);
1153 SamplesToDo = minu(size, BUFFERSIZE);
1154 for(c = 0;c < MaxChannels;c++)
1155 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
1157 ALCdevice_Lock(device);
1158 V(device->Synth,process)(SamplesToDo, device->DryBuffer);
1160 ctx = ATOMIC_LOAD(&device->ContextList);
1161 while(ctx)
1163 ALenum DeferUpdates = ctx->DeferUpdates;
1164 ALenum UpdateSources = AL_FALSE;
1166 if(!DeferUpdates)
1167 UpdateSources = ATOMIC_EXCHANGE(ALenum, &ctx->UpdateSources, AL_FALSE);
1169 if(UpdateSources)
1170 CalcListenerParams(ctx->Listener);
1172 /* source processing */
1173 voice = ctx->Voices;
1174 voice_end = voice + ctx->VoiceCount;
1175 while(voice != voice_end)
1177 ALsource *source = voice->Source;
1178 if(!source) goto next;
1180 if(source->state != AL_PLAYING && source->state != AL_PAUSED)
1182 voice->Source = NULL;
1183 goto next;
1186 if(!DeferUpdates && (ATOMIC_EXCHANGE(ALenum, &source->NeedsUpdate, AL_FALSE) ||
1187 UpdateSources))
1188 voice->Update(voice, source, ctx);
1190 if(source->state != AL_PAUSED)
1191 MixSource(voice, source, device, SamplesToDo);
1192 next:
1193 voice++;
1196 /* effect slot processing */
1197 slot = VECTOR_ITER_BEGIN(ctx->ActiveAuxSlots);
1198 slot_end = VECTOR_ITER_END(ctx->ActiveAuxSlots);
1199 while(slot != slot_end)
1201 if(!DeferUpdates && ATOMIC_EXCHANGE(ALenum, &(*slot)->NeedsUpdate, AL_FALSE))
1202 V((*slot)->EffectState,update)(device, *slot);
1204 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1205 device->DryBuffer);
1207 for(i = 0;i < SamplesToDo;i++)
1208 (*slot)->WetBuffer[0][i] = 0.0f;
1210 slot++;
1213 ctx = ctx->next;
1216 slot = &device->DefaultSlot;
1217 if(*slot != NULL)
1219 if(ATOMIC_EXCHANGE(ALenum, &(*slot)->NeedsUpdate, AL_FALSE))
1220 V((*slot)->EffectState,update)(device, *slot);
1222 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1223 device->DryBuffer);
1225 for(i = 0;i < SamplesToDo;i++)
1226 (*slot)->WetBuffer[0][i] = 0.0f;
1229 /* Increment the clock time. Every second's worth of samples is
1230 * converted and added to clock base so that large sample counts don't
1231 * overflow during conversion. This also guarantees an exact, stable
1232 * conversion. */
1233 device->SamplesDone += SamplesToDo;
1234 device->ClockBase += (device->SamplesDone/device->Frequency) * DEVICE_CLOCK_RES;
1235 device->SamplesDone %= device->Frequency;
1236 ALCdevice_Unlock(device);
1238 if(device->Bs2b)
1240 /* Apply binaural/crossfeed filter */
1241 for(i = 0;i < SamplesToDo;i++)
1243 float samples[2];
1244 samples[0] = device->DryBuffer[FrontLeft][i];
1245 samples[1] = device->DryBuffer[FrontRight][i];
1246 bs2b_cross_feed(device->Bs2b, samples);
1247 device->DryBuffer[FrontLeft][i] = samples[0];
1248 device->DryBuffer[FrontRight][i] = samples[1];
1252 if(buffer)
1254 switch(device->FmtType)
1256 case DevFmtByte:
1257 Write_ALbyte(device, &buffer, SamplesToDo);
1258 break;
1259 case DevFmtUByte:
1260 Write_ALubyte(device, &buffer, SamplesToDo);
1261 break;
1262 case DevFmtShort:
1263 Write_ALshort(device, &buffer, SamplesToDo);
1264 break;
1265 case DevFmtUShort:
1266 Write_ALushort(device, &buffer, SamplesToDo);
1267 break;
1268 case DevFmtInt:
1269 Write_ALint(device, &buffer, SamplesToDo);
1270 break;
1271 case DevFmtUInt:
1272 Write_ALuint(device, &buffer, SamplesToDo);
1273 break;
1274 case DevFmtFloat:
1275 Write_ALfloat(device, &buffer, SamplesToDo);
1276 break;
1280 size -= SamplesToDo;
1281 IncrementRef(&device->MixCount);
1284 RestoreFPUMode(&oldMode);
1288 ALvoid aluHandleDisconnect(ALCdevice *device)
1290 ALCcontext *Context;
1292 device->Connected = ALC_FALSE;
1294 Context = ATOMIC_LOAD(&device->ContextList);
1295 while(Context)
1297 ALvoice *voice, *voice_end;
1299 voice = Context->Voices;
1300 voice_end = voice + Context->VoiceCount;
1301 while(voice != voice_end)
1303 ALsource *source = voice->Source;
1304 voice->Source = NULL;
1306 if(source && source->state == AL_PLAYING)
1308 source->state = AL_STOPPED;
1309 ATOMIC_STORE(&source->current_buffer, NULL);
1310 source->position = 0;
1311 source->position_fraction = 0;
1314 voice++;
1316 Context->VoiceCount = 0;
1318 Context = Context->next;