Rework HRTF decision logic
[openal-soft.git] / Alc / ALu.c
blobf7408da079a3497eaf0c46f48d8e207670a266fb
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 "mixer_defs.h"
41 #include "backends/base.h"
42 #include "midi/base.h"
45 static_assert((INT_MAX>>FRACTIONBITS)/MAX_PITCH > BUFFERSIZE,
46 "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
48 struct ChanMap {
49 enum Channel channel;
50 ALfloat angle;
51 ALfloat elevation;
54 /* Cone scalar */
55 ALfloat ConeScale = 1.0f;
57 /* Localized Z scalar for mono sources */
58 ALfloat ZScale = 1.0f;
60 extern inline ALfloat minf(ALfloat a, ALfloat b);
61 extern inline ALfloat maxf(ALfloat a, ALfloat b);
62 extern inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max);
64 extern inline ALdouble mind(ALdouble a, ALdouble b);
65 extern inline ALdouble maxd(ALdouble a, ALdouble b);
66 extern inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max);
68 extern inline ALuint minu(ALuint a, ALuint b);
69 extern inline ALuint maxu(ALuint a, ALuint b);
70 extern inline ALuint clampu(ALuint val, ALuint min, ALuint max);
72 extern inline ALint mini(ALint a, ALint b);
73 extern inline ALint maxi(ALint a, ALint b);
74 extern inline ALint clampi(ALint val, ALint min, ALint max);
76 extern inline ALint64 mini64(ALint64 a, ALint64 b);
77 extern inline ALint64 maxi64(ALint64 a, ALint64 b);
78 extern inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max);
80 extern inline ALuint64 minu64(ALuint64 a, ALuint64 b);
81 extern inline ALuint64 maxu64(ALuint64 a, ALuint64 b);
82 extern inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max);
84 extern inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu);
85 extern inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu);
88 static inline HrtfMixerFunc SelectHrtfMixer(void)
90 #ifdef HAVE_SSE
91 if((CPUCapFlags&CPU_CAP_SSE))
92 return MixHrtf_SSE;
93 #endif
94 #ifdef HAVE_NEON
95 if((CPUCapFlags&CPU_CAP_NEON))
96 return MixHrtf_Neon;
97 #endif
99 return MixHrtf_C;
103 static inline void aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
105 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
106 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
107 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
110 static inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
112 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
113 inVector1[2]*inVector2[2];
116 static inline void aluNormalize(ALfloat *inVector)
118 ALfloat lengthsqr = aluDotproduct(inVector, inVector);
119 if(lengthsqr > 0.0f)
121 ALfloat inv_length = 1.0f/sqrtf(lengthsqr);
122 inVector[0] *= inv_length;
123 inVector[1] *= inv_length;
124 inVector[2] *= inv_length;
128 static inline ALvoid aluMatrixVector(ALfloat *vector, ALfloat w, ALfloat (*restrict matrix)[4])
130 ALfloat temp[4] = {
131 vector[0], vector[1], vector[2], w
134 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
135 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
136 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
140 static void UpdateDryStepping(DirectParams *params, ALuint num_chans)
142 ALuint i, j;
144 if(!params->Moving)
146 for(i = 0;i < num_chans;i++)
148 MixGains *gains = params->Gains[i];
149 for(j = 0;j < params->OutChannels;j++)
151 gains[j].Current = gains[j].Target;
152 gains[j].Step = 1.0f;
155 params->Moving = AL_TRUE;
156 params->Counter = 0;
157 return;
160 for(i = 0;i < num_chans;i++)
162 MixGains *gains = params->Gains[i];
163 for(j = 0;j < params->OutChannels;j++)
165 ALfloat cur = maxf(gains[j].Current, FLT_EPSILON);
166 ALfloat trg = maxf(gains[j].Target, FLT_EPSILON);
167 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
168 gains[j].Step = powf(trg/cur, 1.0f/64.0f);
169 else
170 gains[j].Step = 1.0f;
171 gains[j].Current = cur;
174 params->Counter = 64;
177 static void UpdateWetStepping(SendParams *params)
179 ALfloat cur, trg;
181 if(!params->Moving)
183 params->Gain.Current = params->Gain.Target;
184 params->Gain.Step = 1.0f;
186 params->Moving = AL_TRUE;
187 params->Counter = 0;
188 return;
191 cur = maxf(params->Gain.Current, FLT_EPSILON);
192 trg = maxf(params->Gain.Target, FLT_EPSILON);
193 if(fabs(trg - cur) >= GAIN_SILENCE_THRESHOLD)
194 params->Gain.Step = powf(trg/cur, 1.0f/64.0f);
195 else
196 params->Gain.Step = 1.0f;
197 params->Gain.Current = cur;
199 params->Counter = 64;
203 static ALvoid CalcListenerParams(ALlistener *Listener)
205 ALfloat N[3], V[3], U[3], P[3];
207 /* AT then UP */
208 N[0] = Listener->Forward[0];
209 N[1] = Listener->Forward[1];
210 N[2] = Listener->Forward[2];
211 aluNormalize(N);
212 V[0] = Listener->Up[0];
213 V[1] = Listener->Up[1];
214 V[2] = Listener->Up[2];
215 aluNormalize(V);
216 /* Build and normalize right-vector */
217 aluCrossproduct(N, V, U);
218 aluNormalize(U);
220 Listener->Params.Matrix[0][0] = U[0];
221 Listener->Params.Matrix[0][1] = V[0];
222 Listener->Params.Matrix[0][2] = -N[0];
223 Listener->Params.Matrix[0][3] = 0.0f;
224 Listener->Params.Matrix[1][0] = U[1];
225 Listener->Params.Matrix[1][1] = V[1];
226 Listener->Params.Matrix[1][2] = -N[1];
227 Listener->Params.Matrix[1][3] = 0.0f;
228 Listener->Params.Matrix[2][0] = U[2];
229 Listener->Params.Matrix[2][1] = V[2];
230 Listener->Params.Matrix[2][2] = -N[2];
231 Listener->Params.Matrix[2][3] = 0.0f;
232 Listener->Params.Matrix[3][0] = 0.0f;
233 Listener->Params.Matrix[3][1] = 0.0f;
234 Listener->Params.Matrix[3][2] = 0.0f;
235 Listener->Params.Matrix[3][3] = 1.0f;
237 P[0] = Listener->Position[0];
238 P[1] = Listener->Position[1];
239 P[2] = Listener->Position[2];
240 aluMatrixVector(P, 1.0f, Listener->Params.Matrix);
241 Listener->Params.Matrix[3][0] = -P[0];
242 Listener->Params.Matrix[3][1] = -P[1];
243 Listener->Params.Matrix[3][2] = -P[2];
245 Listener->Params.Velocity[0] = Listener->Velocity[0];
246 Listener->Params.Velocity[1] = Listener->Velocity[1];
247 Listener->Params.Velocity[2] = Listener->Velocity[2];
248 aluMatrixVector(Listener->Params.Velocity, 0.0f, Listener->Params.Matrix);
251 ALvoid CalcNonAttnSourceParams(ALvoice *voice, const ALsource *ALSource, const ALCcontext *ALContext)
253 static const struct ChanMap MonoMap[1] = { { FrontCenter, 0.0f, 0.0f } };
254 static const struct ChanMap StereoMap[2] = {
255 { FrontLeft, DEG2RAD(-30.0f), DEG2RAD(0.0f) },
256 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) }
258 static const struct ChanMap StereoWideMap[2] = {
259 { FrontLeft, DEG2RAD(-90.0f), DEG2RAD(0.0f) },
260 { FrontRight, DEG2RAD( 90.0f), DEG2RAD(0.0f) }
262 static const struct ChanMap RearMap[2] = {
263 { BackLeft, DEG2RAD(-150.0f), DEG2RAD(0.0f) },
264 { BackRight, DEG2RAD( 150.0f), DEG2RAD(0.0f) }
266 static const struct ChanMap QuadMap[4] = {
267 { FrontLeft, DEG2RAD( -45.0f), DEG2RAD(0.0f) },
268 { FrontRight, DEG2RAD( 45.0f), DEG2RAD(0.0f) },
269 { BackLeft, DEG2RAD(-135.0f), DEG2RAD(0.0f) },
270 { BackRight, DEG2RAD( 135.0f), DEG2RAD(0.0f) }
272 static const struct ChanMap X51Map[6] = {
273 { FrontLeft, DEG2RAD( -30.0f), DEG2RAD(0.0f) },
274 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) },
275 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f) },
276 { LFE, 0.0f, 0.0f },
277 { SideLeft, DEG2RAD(-110.0f), DEG2RAD(0.0f) },
278 { SideRight, DEG2RAD( 110.0f), DEG2RAD(0.0f) }
280 static const struct ChanMap X61Map[7] = {
281 { FrontLeft, DEG2RAD(-30.0f), DEG2RAD(0.0f) },
282 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) },
283 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f) },
284 { LFE, 0.0f, 0.0f },
285 { BackCenter, DEG2RAD(180.0f), DEG2RAD(0.0f) },
286 { SideLeft, DEG2RAD(-90.0f), DEG2RAD(0.0f) },
287 { SideRight, DEG2RAD( 90.0f), DEG2RAD(0.0f) }
289 static const struct ChanMap X71Map[8] = {
290 { FrontLeft, DEG2RAD( -30.0f), DEG2RAD(0.0f) },
291 { FrontRight, DEG2RAD( 30.0f), DEG2RAD(0.0f) },
292 { FrontCenter, DEG2RAD( 0.0f), DEG2RAD(0.0f) },
293 { LFE, 0.0f, 0.0f },
294 { BackLeft, DEG2RAD(-150.0f), DEG2RAD(0.0f) },
295 { BackRight, DEG2RAD( 150.0f), DEG2RAD(0.0f) },
296 { SideLeft, DEG2RAD( -90.0f), DEG2RAD(0.0f) },
297 { SideRight, DEG2RAD( 90.0f), DEG2RAD(0.0f) }
300 ALCdevice *Device = ALContext->Device;
301 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
302 ALbufferlistitem *BufferListItem;
303 enum FmtChannels Channels;
304 ALfloat DryGain, DryGainHF, DryGainLF;
305 ALfloat WetGain[MAX_SENDS];
306 ALfloat WetGainHF[MAX_SENDS];
307 ALfloat WetGainLF[MAX_SENDS];
308 ALuint NumSends, Frequency;
309 ALboolean Relative;
310 const struct ChanMap *chans = NULL;
311 ALuint num_channels = 0;
312 ALboolean DirectChannels;
313 ALboolean isbformat = AL_FALSE;
314 ALfloat Pitch;
315 ALuint i, j, c;
317 /* Get device properties */
318 NumSends = Device->NumAuxSends;
319 Frequency = Device->Frequency;
321 /* Get listener properties */
322 ListenerGain = ALContext->Listener->Gain;
324 /* Get source properties */
325 SourceVolume = ALSource->Gain;
326 MinVolume = ALSource->MinGain;
327 MaxVolume = ALSource->MaxGain;
328 Pitch = ALSource->Pitch;
329 Relative = ALSource->HeadRelative;
330 DirectChannels = ALSource->DirectChannels;
332 voice->Direct.OutBuffer = Device->DryBuffer;
333 voice->Direct.OutChannels = Device->NumChannels;
334 for(i = 0;i < NumSends;i++)
336 ALeffectslot *Slot = ALSource->Send[i].Slot;
337 if(!Slot && i == 0)
338 Slot = Device->DefaultSlot;
339 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
340 voice->Send[i].OutBuffer = NULL;
341 else
342 voice->Send[i].OutBuffer = Slot->WetBuffer;
345 /* Calculate the stepping value */
346 Channels = FmtMono;
347 BufferListItem = ATOMIC_LOAD(&ALSource->queue);
348 while(BufferListItem != NULL)
350 ALbuffer *ALBuffer;
351 if((ALBuffer=BufferListItem->buffer) != NULL)
353 Pitch = Pitch * ALBuffer->Frequency / Frequency;
354 if(Pitch > (ALfloat)MAX_PITCH)
355 voice->Step = MAX_PITCH<<FRACTIONBITS;
356 else
358 voice->Step = fastf2i(Pitch*FRACTIONONE);
359 if(voice->Step == 0)
360 voice->Step = 1;
363 Channels = ALBuffer->FmtChannels;
364 break;
366 BufferListItem = BufferListItem->next;
369 /* Calculate gains */
370 DryGain = clampf(SourceVolume, MinVolume, MaxVolume);
371 DryGain *= ALSource->Direct.Gain * ListenerGain;
372 DryGainHF = ALSource->Direct.GainHF;
373 DryGainLF = ALSource->Direct.GainLF;
374 for(i = 0;i < NumSends;i++)
376 WetGain[i] = clampf(SourceVolume, MinVolume, MaxVolume);
377 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
378 WetGainHF[i] = ALSource->Send[i].GainHF;
379 WetGainLF[i] = ALSource->Send[i].GainLF;
382 switch(Channels)
384 case FmtMono:
385 chans = MonoMap;
386 num_channels = 1;
387 break;
389 case FmtStereo:
390 /* HACK: Place the stereo channels at +/-90 degrees when using non-
391 * HRTF stereo output. This helps reduce the "monoization" caused
392 * by them panning towards the center. */
393 if(Device->FmtChans == DevFmtStereo && !Device->Hrtf)
394 chans = StereoWideMap;
395 else
396 chans = StereoMap;
397 num_channels = 2;
398 break;
400 case FmtRear:
401 chans = RearMap;
402 num_channels = 2;
403 break;
405 case FmtQuad:
406 chans = QuadMap;
407 num_channels = 4;
408 break;
410 case FmtX51:
411 chans = X51Map;
412 num_channels = 6;
413 break;
415 case FmtX61:
416 chans = X61Map;
417 num_channels = 7;
418 break;
420 case FmtX71:
421 chans = X71Map;
422 num_channels = 8;
423 break;
425 case FmtBFormat2D:
426 num_channels = 3;
427 isbformat = AL_TRUE;
428 DirectChannels = AL_FALSE;
429 break;
431 case FmtBFormat3D:
432 num_channels = 4;
433 isbformat = AL_TRUE;
434 DirectChannels = AL_FALSE;
435 break;
438 if(isbformat)
440 ALfloat N[3], V[3], U[3];
441 ALfloat matrix[4][4];
443 /* AT then UP */
444 N[0] = ALSource->Orientation[0][0];
445 N[1] = ALSource->Orientation[0][1];
446 N[2] = ALSource->Orientation[0][2];
447 aluNormalize(N);
448 V[0] = ALSource->Orientation[1][0];
449 V[1] = ALSource->Orientation[1][1];
450 V[2] = ALSource->Orientation[1][2];
451 aluNormalize(V);
452 if(!Relative)
454 ALfloat (*restrict lmatrix)[4] = ALContext->Listener->Params.Matrix;
455 aluMatrixVector(N, 0.0f, lmatrix);
456 aluMatrixVector(V, 0.0f, lmatrix);
458 /* Build and normalize right-vector */
459 aluCrossproduct(N, V, U);
460 aluNormalize(U);
462 matrix[0][0] = 1.0f;
463 matrix[0][1] = 0.0f;
464 matrix[0][2] = 0.0f;
465 matrix[0][3] = 0.0f;
466 matrix[1][0] = 0.0f;
467 matrix[1][1] = -N[2];
468 matrix[1][2] = -N[0];
469 matrix[1][3] = N[1];
470 matrix[2][0] = 0.0f;
471 matrix[2][1] = U[2];
472 matrix[2][2] = U[0];
473 matrix[2][3] = -U[1];
474 matrix[3][0] = 0.0f;
475 matrix[3][1] = -V[2];
476 matrix[3][2] = -V[0];
477 matrix[3][3] = V[1];
479 for(c = 0;c < num_channels;c++)
481 MixGains *gains = voice->Direct.Gains[c];
482 ALfloat Target[MAX_OUTPUT_CHANNELS];
484 ComputeBFormatGains(Device, matrix[c], DryGain, Target);
485 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
486 gains[i].Target = Target[i];
488 /* B-Format cannot handle logarithmic gain stepping, since the gain can
489 * switch between positive and negative values. */
490 voice->Direct.Moving = AL_FALSE;
491 UpdateDryStepping(&voice->Direct, num_channels);
493 voice->IsHrtf = AL_FALSE;
494 for(i = 0;i < NumSends;i++)
495 WetGain[i] *= 1.4142f;
497 else if(DirectChannels != AL_FALSE)
499 if(Device->Hrtf)
501 voice->Direct.OutBuffer = &voice->Direct.OutBuffer[voice->Direct.OutChannels];
502 voice->Direct.OutChannels = 2;
503 for(c = 0;c < num_channels;c++)
505 MixGains *gains = voice->Direct.Gains[c];
507 for(j = 0;j < MAX_OUTPUT_CHANNELS;j++)
508 gains[j].Target = 0.0f;
510 if(chans[c].channel == FrontLeft)
511 gains[0].Target = DryGain;
512 else if(chans[c].channel == FrontRight)
513 gains[1].Target = DryGain;
516 else for(c = 0;c < num_channels;c++)
518 MixGains *gains = voice->Direct.Gains[c];
519 int idx;
521 for(j = 0;j < MAX_OUTPUT_CHANNELS;j++)
522 gains[j].Target = 0.0f;
523 if((idx=GetChannelIdxByName(Device, chans[c].channel)) != -1)
524 gains[idx].Target = DryGain;
526 UpdateDryStepping(&voice->Direct, num_channels);
528 voice->IsHrtf = AL_FALSE;
530 else
532 for(c = 0;c < num_channels;c++)
534 MixGains *gains = voice->Direct.Gains[c];
535 ALfloat Target[MAX_OUTPUT_CHANNELS];
537 /* Special-case LFE */
538 if(chans[c].channel == LFE)
540 int idx;
541 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
542 gains[i].Target = 0.0f;
543 if((idx=GetChannelIdxByName(Device, chans[c].channel)) != -1)
544 gains[idx].Target = DryGain;
545 continue;
548 ComputeAngleGains(Device, chans[c].angle, chans[c].elevation, DryGain, Target);
549 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
550 gains[i].Target = Target[i];
552 UpdateDryStepping(&voice->Direct, num_channels);
554 voice->IsHrtf = AL_FALSE;
556 for(i = 0;i < NumSends;i++)
558 voice->Send[i].Gain.Target = WetGain[i];
559 UpdateWetStepping(&voice->Send[i]);
563 ALfloat gainhf = maxf(0.01f, DryGainHF);
564 ALfloat gainlf = maxf(0.01f, DryGainLF);
565 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
566 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
567 for(c = 0;c < num_channels;c++)
569 voice->Direct.Filters[c].ActiveType = AF_None;
570 if(gainhf != 1.0f) voice->Direct.Filters[c].ActiveType |= AF_LowPass;
571 if(gainlf != 1.0f) voice->Direct.Filters[c].ActiveType |= AF_HighPass;
572 ALfilterState_setParams(
573 &voice->Direct.Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
574 hfscale, 0.0f
576 ALfilterState_setParams(
577 &voice->Direct.Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
578 lfscale, 0.0f
582 for(i = 0;i < NumSends;i++)
584 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
585 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
586 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
587 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
588 for(c = 0;c < num_channels;c++)
590 voice->Send[i].Filters[c].ActiveType = AF_None;
591 if(gainhf != 1.0f) voice->Send[i].Filters[c].ActiveType |= AF_LowPass;
592 if(gainlf != 1.0f) voice->Send[i].Filters[c].ActiveType |= AF_HighPass;
593 ALfilterState_setParams(
594 &voice->Send[i].Filters[c].LowPass, ALfilterType_HighShelf, gainhf,
595 hfscale, 0.0f
597 ALfilterState_setParams(
598 &voice->Send[i].Filters[c].HighPass, ALfilterType_LowShelf, gainlf,
599 lfscale, 0.0f
605 ALvoid CalcSourceParams(ALvoice *voice, const ALsource *ALSource, const ALCcontext *ALContext)
607 ALCdevice *Device = ALContext->Device;
608 ALfloat Velocity[3],Direction[3],Position[3],SourceToListener[3];
609 ALfloat InnerAngle,OuterAngle,Angle,Distance,ClampedDist;
610 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
611 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
612 ALfloat DopplerFactor, SpeedOfSound;
613 ALfloat AirAbsorptionFactor;
614 ALfloat RoomAirAbsorption[MAX_SENDS];
615 ALbufferlistitem *BufferListItem;
616 ALfloat Attenuation;
617 ALfloat RoomAttenuation[MAX_SENDS];
618 ALfloat MetersPerUnit;
619 ALfloat RoomRolloffBase;
620 ALfloat RoomRolloff[MAX_SENDS];
621 ALfloat DecayDistance[MAX_SENDS];
622 ALfloat DryGain;
623 ALfloat DryGainHF;
624 ALfloat DryGainLF;
625 ALboolean DryGainHFAuto;
626 ALfloat WetGain[MAX_SENDS];
627 ALfloat WetGainHF[MAX_SENDS];
628 ALfloat WetGainLF[MAX_SENDS];
629 ALboolean WetGainAuto;
630 ALboolean WetGainHFAuto;
631 ALfloat Pitch;
632 ALuint Frequency;
633 ALint NumSends;
634 ALint i, j;
636 DryGainHF = 1.0f;
637 DryGainLF = 1.0f;
638 for(i = 0;i < MAX_SENDS;i++)
640 WetGainHF[i] = 1.0f;
641 WetGainLF[i] = 1.0f;
644 /* Get context/device properties */
645 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
646 SpeedOfSound = ALContext->SpeedOfSound * ALContext->DopplerVelocity;
647 NumSends = Device->NumAuxSends;
648 Frequency = Device->Frequency;
650 /* Get listener properties */
651 ListenerGain = ALContext->Listener->Gain;
652 MetersPerUnit = ALContext->Listener->MetersPerUnit;
654 /* Get source properties */
655 SourceVolume = ALSource->Gain;
656 MinVolume = ALSource->MinGain;
657 MaxVolume = ALSource->MaxGain;
658 Pitch = ALSource->Pitch;
659 Position[0] = ALSource->Position[0];
660 Position[1] = ALSource->Position[1];
661 Position[2] = ALSource->Position[2];
662 Direction[0] = ALSource->Direction[0];
663 Direction[1] = ALSource->Direction[1];
664 Direction[2] = ALSource->Direction[2];
665 Velocity[0] = ALSource->Velocity[0];
666 Velocity[1] = ALSource->Velocity[1];
667 Velocity[2] = ALSource->Velocity[2];
668 MinDist = ALSource->RefDistance;
669 MaxDist = ALSource->MaxDistance;
670 Rolloff = ALSource->RollOffFactor;
671 InnerAngle = ALSource->InnerAngle;
672 OuterAngle = ALSource->OuterAngle;
673 AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
674 DryGainHFAuto = ALSource->DryGainHFAuto;
675 WetGainAuto = ALSource->WetGainAuto;
676 WetGainHFAuto = ALSource->WetGainHFAuto;
677 RoomRolloffBase = ALSource->RoomRolloffFactor;
679 voice->Direct.OutBuffer = Device->DryBuffer;
680 voice->Direct.OutChannels = Device->NumChannels;
681 for(i = 0;i < NumSends;i++)
683 ALeffectslot *Slot = ALSource->Send[i].Slot;
685 if(!Slot && i == 0)
686 Slot = Device->DefaultSlot;
687 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
689 Slot = NULL;
690 RoomRolloff[i] = 0.0f;
691 DecayDistance[i] = 0.0f;
692 RoomAirAbsorption[i] = 1.0f;
694 else if(Slot->AuxSendAuto)
696 RoomRolloff[i] = RoomRolloffBase;
697 if(IsReverbEffect(Slot->EffectType))
699 RoomRolloff[i] += Slot->EffectProps.Reverb.RoomRolloffFactor;
700 DecayDistance[i] = Slot->EffectProps.Reverb.DecayTime *
701 SPEEDOFSOUNDMETRESPERSEC;
702 RoomAirAbsorption[i] = Slot->EffectProps.Reverb.AirAbsorptionGainHF;
704 else
706 DecayDistance[i] = 0.0f;
707 RoomAirAbsorption[i] = 1.0f;
710 else
712 /* If the slot's auxiliary send auto is off, the data sent to the
713 * effect slot is the same as the dry path, sans filter effects */
714 RoomRolloff[i] = Rolloff;
715 DecayDistance[i] = 0.0f;
716 RoomAirAbsorption[i] = AIRABSORBGAINHF;
719 if(!Slot || Slot->EffectType == AL_EFFECT_NULL)
720 voice->Send[i].OutBuffer = NULL;
721 else
722 voice->Send[i].OutBuffer = Slot->WetBuffer;
725 /* Transform source to listener space (convert to head relative) */
726 if(ALSource->HeadRelative == AL_FALSE)
728 ALfloat (*restrict Matrix)[4] = ALContext->Listener->Params.Matrix;
729 /* Transform source vectors */
730 aluMatrixVector(Position, 1.0f, Matrix);
731 aluMatrixVector(Direction, 0.0f, Matrix);
732 aluMatrixVector(Velocity, 0.0f, Matrix);
734 else
736 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
737 /* Offset the source velocity to be relative of the listener velocity */
738 Velocity[0] += ListenerVel[0];
739 Velocity[1] += ListenerVel[1];
740 Velocity[2] += ListenerVel[2];
743 SourceToListener[0] = -Position[0];
744 SourceToListener[1] = -Position[1];
745 SourceToListener[2] = -Position[2];
746 aluNormalize(SourceToListener);
747 aluNormalize(Direction);
749 /* Calculate distance attenuation */
750 Distance = sqrtf(aluDotproduct(Position, Position));
751 ClampedDist = Distance;
753 Attenuation = 1.0f;
754 for(i = 0;i < NumSends;i++)
755 RoomAttenuation[i] = 1.0f;
756 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
757 ALContext->DistanceModel)
759 case InverseDistanceClamped:
760 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
761 if(MaxDist < MinDist)
762 break;
763 /*fall-through*/
764 case InverseDistance:
765 if(MinDist > 0.0f)
767 if((MinDist + (Rolloff * (ClampedDist - MinDist))) > 0.0f)
768 Attenuation = MinDist / (MinDist + (Rolloff * (ClampedDist - MinDist)));
769 for(i = 0;i < NumSends;i++)
771 if((MinDist + (RoomRolloff[i] * (ClampedDist - MinDist))) > 0.0f)
772 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (ClampedDist - MinDist)));
775 break;
777 case LinearDistanceClamped:
778 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
779 if(MaxDist < MinDist)
780 break;
781 /*fall-through*/
782 case LinearDistance:
783 if(MaxDist != MinDist)
785 Attenuation = 1.0f - (Rolloff*(ClampedDist-MinDist)/(MaxDist - MinDist));
786 Attenuation = maxf(Attenuation, 0.0f);
787 for(i = 0;i < NumSends;i++)
789 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(ClampedDist-MinDist)/(MaxDist - MinDist));
790 RoomAttenuation[i] = maxf(RoomAttenuation[i], 0.0f);
793 break;
795 case ExponentDistanceClamped:
796 ClampedDist = clampf(ClampedDist, MinDist, MaxDist);
797 if(MaxDist < MinDist)
798 break;
799 /*fall-through*/
800 case ExponentDistance:
801 if(ClampedDist > 0.0f && MinDist > 0.0f)
803 Attenuation = powf(ClampedDist/MinDist, -Rolloff);
804 for(i = 0;i < NumSends;i++)
805 RoomAttenuation[i] = powf(ClampedDist/MinDist, -RoomRolloff[i]);
807 break;
809 case DisableDistance:
810 ClampedDist = MinDist;
811 break;
814 /* Source Gain + Attenuation */
815 DryGain = SourceVolume * Attenuation;
816 for(i = 0;i < NumSends;i++)
817 WetGain[i] = SourceVolume * RoomAttenuation[i];
819 /* Distance-based air absorption */
820 if(AirAbsorptionFactor > 0.0f && ClampedDist > MinDist)
822 ALfloat meters = maxf(ClampedDist-MinDist, 0.0f) * MetersPerUnit;
823 DryGainHF *= powf(AIRABSORBGAINHF, AirAbsorptionFactor*meters);
824 for(i = 0;i < NumSends;i++)
825 WetGainHF[i] *= powf(RoomAirAbsorption[i], AirAbsorptionFactor*meters);
828 if(WetGainAuto)
830 ALfloat ApparentDist = 1.0f/maxf(Attenuation, 0.00001f) - 1.0f;
832 /* Apply a decay-time transformation to the wet path, based on the
833 * attenuation of the dry path.
835 * Using the apparent distance, based on the distance attenuation, the
836 * initial decay of the reverb effect is calculated and applied to the
837 * wet path.
839 for(i = 0;i < NumSends;i++)
841 if(DecayDistance[i] > 0.0f)
842 WetGain[i] *= powf(0.001f/*-60dB*/, ApparentDist/DecayDistance[i]);
846 /* Calculate directional soundcones */
847 Angle = RAD2DEG(acosf(aluDotproduct(Direction,SourceToListener)) * ConeScale) * 2.0f;
848 if(Angle > InnerAngle && Angle <= OuterAngle)
850 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
851 ConeVolume = lerp(1.0f, ALSource->OuterGain, scale);
852 ConeHF = lerp(1.0f, ALSource->OuterGainHF, scale);
854 else if(Angle > OuterAngle)
856 ConeVolume = ALSource->OuterGain;
857 ConeHF = ALSource->OuterGainHF;
859 else
861 ConeVolume = 1.0f;
862 ConeHF = 1.0f;
865 DryGain *= ConeVolume;
866 if(WetGainAuto)
868 for(i = 0;i < NumSends;i++)
869 WetGain[i] *= ConeVolume;
871 if(DryGainHFAuto)
872 DryGainHF *= ConeHF;
873 if(WetGainHFAuto)
875 for(i = 0;i < NumSends;i++)
876 WetGainHF[i] *= ConeHF;
879 /* Clamp to Min/Max Gain */
880 DryGain = clampf(DryGain, MinVolume, MaxVolume);
881 for(i = 0;i < NumSends;i++)
882 WetGain[i] = clampf(WetGain[i], MinVolume, MaxVolume);
884 /* Apply gain and frequency filters */
885 DryGain *= ALSource->Direct.Gain * ListenerGain;
886 DryGainHF *= ALSource->Direct.GainHF;
887 DryGainLF *= ALSource->Direct.GainLF;
888 for(i = 0;i < NumSends;i++)
890 WetGain[i] *= ALSource->Send[i].Gain * ListenerGain;
891 WetGainHF[i] *= ALSource->Send[i].GainHF;
892 WetGainLF[i] *= ALSource->Send[i].GainLF;
895 /* Calculate velocity-based doppler effect */
896 if(DopplerFactor > 0.0f)
898 const ALfloat *ListenerVel = ALContext->Listener->Params.Velocity;
899 ALfloat VSS, VLS;
901 if(SpeedOfSound < 1.0f)
903 DopplerFactor *= 1.0f/SpeedOfSound;
904 SpeedOfSound = 1.0f;
907 VSS = aluDotproduct(Velocity, SourceToListener) * DopplerFactor;
908 VLS = aluDotproduct(ListenerVel, SourceToListener) * DopplerFactor;
910 Pitch *= clampf(SpeedOfSound-VLS, 1.0f, SpeedOfSound*2.0f - 1.0f) /
911 clampf(SpeedOfSound-VSS, 1.0f, SpeedOfSound*2.0f - 1.0f);
914 BufferListItem = ATOMIC_LOAD(&ALSource->queue);
915 while(BufferListItem != NULL)
917 ALbuffer *ALBuffer;
918 if((ALBuffer=BufferListItem->buffer) != NULL)
920 /* Calculate fixed-point stepping value, based on the pitch, buffer
921 * frequency, and output frequency. */
922 Pitch = Pitch * ALBuffer->Frequency / Frequency;
923 if(Pitch > (ALfloat)MAX_PITCH)
924 voice->Step = MAX_PITCH<<FRACTIONBITS;
925 else
927 voice->Step = fastf2i(Pitch*FRACTIONONE);
928 if(voice->Step == 0)
929 voice->Step = 1;
932 break;
934 BufferListItem = BufferListItem->next;
938 MixGains *gains = voice->Direct.Gains[0];
939 ALfloat radius = ALSource->Radius;
940 ALfloat Target[MAX_OUTPUT_CHANNELS];
942 /* Normalize the length, and compute panned gains. */
943 if(!(Distance > FLT_EPSILON) && !(radius > FLT_EPSILON))
945 const ALfloat front[3] = { 0.0f, 0.0f, -1.0f };
946 ComputeDirectionalGains(Device, front, DryGain, Target);
948 else
950 ALfloat invlen = 1.0f/maxf(Distance, radius);
951 Position[0] *= invlen;
952 Position[1] *= invlen;
953 Position[2] *= invlen;
954 ComputeDirectionalGains(Device, Position, DryGain, Target);
957 for(j = 0;j < MAX_OUTPUT_CHANNELS;j++)
958 gains[j].Target = Target[j];
959 UpdateDryStepping(&voice->Direct, 1);
961 voice->IsHrtf = AL_FALSE;
963 for(i = 0;i < NumSends;i++)
965 voice->Send[i].Gain.Target = WetGain[i];
966 UpdateWetStepping(&voice->Send[i]);
970 ALfloat gainhf = maxf(0.01f, DryGainHF);
971 ALfloat gainlf = maxf(0.01f, DryGainLF);
972 ALfloat hfscale = ALSource->Direct.HFReference / Frequency;
973 ALfloat lfscale = ALSource->Direct.LFReference / Frequency;
974 voice->Direct.Filters[0].ActiveType = AF_None;
975 if(gainhf != 1.0f) voice->Direct.Filters[0].ActiveType |= AF_LowPass;
976 if(gainlf != 1.0f) voice->Direct.Filters[0].ActiveType |= AF_HighPass;
977 ALfilterState_setParams(
978 &voice->Direct.Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
979 hfscale, 0.0f
981 ALfilterState_setParams(
982 &voice->Direct.Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
983 lfscale, 0.0f
986 for(i = 0;i < NumSends;i++)
988 ALfloat gainhf = maxf(0.01f, WetGainHF[i]);
989 ALfloat gainlf = maxf(0.01f, WetGainLF[i]);
990 ALfloat hfscale = ALSource->Send[i].HFReference / Frequency;
991 ALfloat lfscale = ALSource->Send[i].LFReference / Frequency;
992 voice->Send[i].Filters[0].ActiveType = AF_None;
993 if(gainhf != 1.0f) voice->Send[i].Filters[0].ActiveType |= AF_LowPass;
994 if(gainlf != 1.0f) voice->Send[i].Filters[0].ActiveType |= AF_HighPass;
995 ALfilterState_setParams(
996 &voice->Send[i].Filters[0].LowPass, ALfilterType_HighShelf, gainhf,
997 hfscale, 0.0f
999 ALfilterState_setParams(
1000 &voice->Send[i].Filters[0].HighPass, ALfilterType_LowShelf, gainlf,
1001 lfscale, 0.0f
1007 static inline ALint aluF2I25(ALfloat val)
1009 /* Clamp the value between -1 and +1. This handles that with only a single branch. */
1010 if(fabsf(val) > 1.0f)
1011 val = (ALfloat)((0.0f < val) - (val < 0.0f));
1012 /* Convert to a signed integer, between -16777215 and +16777215. */
1013 return fastf2i(val*16777215.0f);
1016 static inline ALfloat aluF2F(ALfloat val)
1017 { return val; }
1018 static inline ALint aluF2I(ALfloat val)
1019 { return aluF2I25(val)<<7; }
1020 static inline ALuint aluF2UI(ALfloat val)
1021 { return aluF2I(val)+2147483648u; }
1022 static inline ALshort aluF2S(ALfloat val)
1023 { return aluF2I25(val)>>9; }
1024 static inline ALushort aluF2US(ALfloat val)
1025 { return aluF2S(val)+32768; }
1026 static inline ALbyte aluF2B(ALfloat val)
1027 { return aluF2I25(val)>>17; }
1028 static inline ALubyte aluF2UB(ALfloat val)
1029 { return aluF2B(val)+128; }
1031 #define DECL_TEMPLATE(T, func) \
1032 static void Write_##T(const ALfloatBUFFERSIZE *InBuffer, ALvoid *OutBuffer, \
1033 ALuint SamplesToDo, ALuint numchans) \
1035 ALuint i, j; \
1036 for(j = 0;j < numchans;j++) \
1038 const ALfloat *in = InBuffer[j]; \
1039 T *restrict out = (T*)OutBuffer + j; \
1040 for(i = 0;i < SamplesToDo;i++) \
1041 out[i*numchans] = func(in[i]); \
1045 DECL_TEMPLATE(ALfloat, aluF2F)
1046 DECL_TEMPLATE(ALuint, aluF2UI)
1047 DECL_TEMPLATE(ALint, aluF2I)
1048 DECL_TEMPLATE(ALushort, aluF2US)
1049 DECL_TEMPLATE(ALshort, aluF2S)
1050 DECL_TEMPLATE(ALubyte, aluF2UB)
1051 DECL_TEMPLATE(ALbyte, aluF2B)
1053 #undef DECL_TEMPLATE
1056 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1058 ALuint SamplesToDo;
1059 ALeffectslot **slot, **slot_end;
1060 ALvoice *voice, *voice_end;
1061 ALCcontext *ctx;
1062 FPUCtl oldMode;
1063 ALuint i, c;
1065 SetMixerFPUMode(&oldMode);
1067 while(size > 0)
1069 ALuint outchanoffset = 0;
1070 ALuint outchancount = device->NumChannels;
1072 IncrementRef(&device->MixCount);
1074 SamplesToDo = minu(size, BUFFERSIZE);
1075 for(c = 0;c < device->NumChannels;c++)
1076 memset(device->DryBuffer[c], 0, SamplesToDo*sizeof(ALfloat));
1077 if(device->Hrtf)
1079 outchanoffset = device->NumChannels;
1080 outchancount = 2;
1081 for(c = 0;c < outchancount;c++)
1082 memset(device->DryBuffer[outchanoffset+c], 0, SamplesToDo*sizeof(ALfloat));
1085 V0(device->Backend,lock)();
1086 V(device->Synth,process)(SamplesToDo, &device->DryBuffer[outchanoffset]);
1088 ctx = ATOMIC_LOAD(&device->ContextList);
1089 while(ctx)
1091 ALenum DeferUpdates = ctx->DeferUpdates;
1092 ALenum UpdateSources = AL_FALSE;
1094 if(!DeferUpdates)
1095 UpdateSources = ATOMIC_EXCHANGE(ALenum, &ctx->UpdateSources, AL_FALSE);
1097 if(UpdateSources)
1098 CalcListenerParams(ctx->Listener);
1100 /* source processing */
1101 voice = ctx->Voices;
1102 voice_end = voice + ctx->VoiceCount;
1103 while(voice != voice_end)
1105 ALsource *source = voice->Source;
1106 if(!source) goto next;
1108 if(source->state != AL_PLAYING && source->state != AL_PAUSED)
1110 voice->Source = NULL;
1111 goto next;
1114 if(!DeferUpdates && (ATOMIC_EXCHANGE(ALenum, &source->NeedsUpdate, AL_FALSE) ||
1115 UpdateSources))
1116 voice->Update(voice, source, ctx);
1118 if(source->state != AL_PAUSED)
1119 MixSource(voice, source, device, SamplesToDo);
1120 next:
1121 voice++;
1124 /* effect slot processing */
1125 slot = VECTOR_ITER_BEGIN(ctx->ActiveAuxSlots);
1126 slot_end = VECTOR_ITER_END(ctx->ActiveAuxSlots);
1127 while(slot != slot_end)
1129 if(!DeferUpdates && ATOMIC_EXCHANGE(ALenum, &(*slot)->NeedsUpdate, AL_FALSE))
1130 V((*slot)->EffectState,update)(device, *slot);
1132 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1133 device->DryBuffer, device->NumChannels);
1135 for(i = 0;i < SamplesToDo;i++)
1136 (*slot)->WetBuffer[0][i] = 0.0f;
1138 slot++;
1141 ctx = ctx->next;
1144 slot = &device->DefaultSlot;
1145 if(*slot != NULL)
1147 if(ATOMIC_EXCHANGE(ALenum, &(*slot)->NeedsUpdate, AL_FALSE))
1148 V((*slot)->EffectState,update)(device, *slot);
1150 V((*slot)->EffectState,process)(SamplesToDo, (*slot)->WetBuffer[0],
1151 device->DryBuffer, device->NumChannels);
1153 for(i = 0;i < SamplesToDo;i++)
1154 (*slot)->WetBuffer[0][i] = 0.0f;
1157 /* Increment the clock time. Every second's worth of samples is
1158 * converted and added to clock base so that large sample counts don't
1159 * overflow during conversion. This also guarantees an exact, stable
1160 * conversion. */
1161 device->SamplesDone += SamplesToDo;
1162 device->ClockBase += (device->SamplesDone/device->Frequency) * DEVICE_CLOCK_RES;
1163 device->SamplesDone %= device->Frequency;
1164 V0(device->Backend,unlock)();
1166 if(device->Hrtf)
1168 HrtfMixerFunc HrtfMix = SelectHrtfMixer();
1169 ALuint irsize = GetHrtfIrSize(device->Hrtf);
1170 for(c = 0;c < device->NumChannels;c++)
1171 HrtfMix(&device->DryBuffer[outchanoffset], device->DryBuffer[c], device->Hrtf_Offset, irsize,
1172 &device->Hrtf_Params[c], &device->Hrtf_State[c], SamplesToDo);
1173 device->Hrtf_Offset += SamplesToDo;
1175 else if(device->Bs2b)
1177 /* Apply binaural/crossfeed filter */
1178 for(i = 0;i < SamplesToDo;i++)
1180 float samples[2];
1181 samples[0] = device->DryBuffer[0][i];
1182 samples[1] = device->DryBuffer[1][i];
1183 bs2b_cross_feed(device->Bs2b, samples);
1184 device->DryBuffer[0][i] = samples[0];
1185 device->DryBuffer[1][i] = samples[1];
1189 if(buffer)
1191 switch(device->FmtType)
1193 case DevFmtByte:
1194 Write_ALbyte(&device->DryBuffer[outchanoffset], buffer, SamplesToDo, outchancount);
1195 buffer = (char*)buffer + SamplesToDo*outchancount*sizeof(ALbyte);
1196 break;
1197 case DevFmtUByte:
1198 Write_ALubyte(&device->DryBuffer[outchanoffset], buffer, SamplesToDo, outchancount);
1199 buffer = (char*)buffer + SamplesToDo*outchancount*sizeof(ALubyte);
1200 break;
1201 case DevFmtShort:
1202 Write_ALshort(&device->DryBuffer[outchanoffset], buffer, SamplesToDo, outchancount);
1203 buffer = (char*)buffer + SamplesToDo*outchancount*sizeof(ALshort);
1204 break;
1205 case DevFmtUShort:
1206 Write_ALushort(&device->DryBuffer[outchanoffset], buffer, SamplesToDo, outchancount);
1207 buffer = (char*)buffer + SamplesToDo*outchancount*sizeof(ALushort);
1208 break;
1209 case DevFmtInt:
1210 Write_ALint(&device->DryBuffer[outchanoffset], buffer, SamplesToDo, outchancount);
1211 buffer = (char*)buffer + SamplesToDo*outchancount*sizeof(ALint);
1212 break;
1213 case DevFmtUInt:
1214 Write_ALuint(&device->DryBuffer[outchanoffset], buffer, SamplesToDo, outchancount);
1215 buffer = (char*)buffer + SamplesToDo*outchancount*sizeof(ALuint);
1216 break;
1217 case DevFmtFloat:
1218 Write_ALfloat(&device->DryBuffer[outchanoffset], buffer, SamplesToDo, outchancount);
1219 buffer = (char*)buffer + SamplesToDo*outchancount*sizeof(ALfloat);
1220 break;
1224 size -= SamplesToDo;
1225 IncrementRef(&device->MixCount);
1228 RestoreFPUMode(&oldMode);
1232 ALvoid aluHandleDisconnect(ALCdevice *device)
1234 ALCcontext *Context;
1236 device->Connected = ALC_FALSE;
1238 Context = ATOMIC_LOAD(&device->ContextList);
1239 while(Context)
1241 ALvoice *voice, *voice_end;
1243 voice = Context->Voices;
1244 voice_end = voice + Context->VoiceCount;
1245 while(voice != voice_end)
1247 ALsource *source = voice->Source;
1248 voice->Source = NULL;
1250 if(source && source->state == AL_PLAYING)
1252 source->state = AL_STOPPED;
1253 ATOMIC_STORE(&source->current_buffer, NULL);
1254 source->position = 0;
1255 source->position_fraction = 0;
1258 voice++;
1260 Context->VoiceCount = 0;
1262 Context = Context->next;