Set the device channel matrix when the device is reset
[openal-soft/android/lowlatency.git] / Alc / ALu.c
blob22008fc37eaf6a297b1d04a29ad309c1000e6ae9
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 "AL/al.h"
31 #include "AL/alc.h"
32 #include "alSource.h"
33 #include "alBuffer.h"
34 #include "alThunk.h"
35 #include "alListener.h"
36 #include "alAuxEffectSlot.h"
37 #include "alu.h"
38 #include "bs2b.h"
40 #define FRACTIONBITS 14
41 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
42 #define MAX_PITCH 65536
44 /* Minimum ramp length in milliseconds. The value below was chosen to
45 * adequately reduce clicks and pops from harsh gain changes. */
46 #define MIN_RAMP_LENGTH 16
48 ALboolean DuplicateStereo = AL_FALSE;
51 static __inline ALfloat aluF2F(ALfloat Value)
53 return Value;
56 static __inline ALshort aluF2S(ALfloat Value)
58 ALint i;
60 if(Value < 0.0f)
62 i = (ALint)(Value*32768.0f);
63 i = max(-32768, i);
65 else
67 i = (ALint)(Value*32767.0f);
68 i = min( 32767, i);
70 return ((ALshort)i);
73 static __inline ALubyte aluF2UB(ALfloat Value)
75 ALshort i = aluF2S(Value);
76 return (i>>8)+128;
80 static __inline ALvoid aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
82 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
83 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
84 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
87 static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
89 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
90 inVector1[2]*inVector2[2];
93 static __inline ALvoid aluNormalize(ALfloat *inVector)
95 ALfloat length, inverse_length;
97 length = aluSqrt(aluDotproduct(inVector, inVector));
98 if(length != 0.0f)
100 inverse_length = 1.0f/length;
101 inVector[0] *= inverse_length;
102 inVector[1] *= inverse_length;
103 inVector[2] *= inverse_length;
107 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4])
109 ALfloat temp[4] = {
110 vector[0], vector[1], vector[2], w
113 vector[0] = temp[0]*matrix[0][0] + temp[1]*matrix[1][0] + temp[2]*matrix[2][0] + temp[3]*matrix[3][0];
114 vector[1] = temp[0]*matrix[0][1] + temp[1]*matrix[1][1] + temp[2]*matrix[2][1] + temp[3]*matrix[3][1];
115 vector[2] = temp[0]*matrix[0][2] + temp[1]*matrix[1][2] + temp[2]*matrix[2][2] + temp[3]*matrix[3][2];
118 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
119 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
121 char layout_str[256];
122 char *confkey, *next;
123 char *sep, *end;
124 int i, val;
126 strncpy(layout_str, GetConfigValue(NULL, name, ""), sizeof(layout_str));
127 layout_str[255] = 0;
129 next = confkey = layout_str;
130 while(next && *next)
132 confkey = next;
133 next = strchr(confkey, ',');
134 if(next)
136 *next = 0;
137 do {
138 next++;
139 } while(isspace(*next) || *next == ',');
142 sep = strchr(confkey, '=');
143 if(!sep || confkey == sep)
144 continue;
146 end = sep - 1;
147 while(isspace(*end) && end != confkey)
148 end--;
149 *(++end) = 0;
151 if(strcmp(confkey, "fl") == 0 || strcmp(confkey, "front-left") == 0)
152 val = FRONT_LEFT;
153 else if(strcmp(confkey, "fr") == 0 || strcmp(confkey, "front-right") == 0)
154 val = FRONT_RIGHT;
155 else if(strcmp(confkey, "fc") == 0 || strcmp(confkey, "front-center") == 0)
156 val = FRONT_CENTER;
157 else if(strcmp(confkey, "bl") == 0 || strcmp(confkey, "back-left") == 0)
158 val = BACK_LEFT;
159 else if(strcmp(confkey, "br") == 0 || strcmp(confkey, "back-right") == 0)
160 val = BACK_RIGHT;
161 else if(strcmp(confkey, "bc") == 0 || strcmp(confkey, "back-center") == 0)
162 val = BACK_CENTER;
163 else if(strcmp(confkey, "sl") == 0 || strcmp(confkey, "side-left") == 0)
164 val = SIDE_LEFT;
165 else if(strcmp(confkey, "sr") == 0 || strcmp(confkey, "side-right") == 0)
166 val = SIDE_RIGHT;
167 else
169 AL_PRINT("Unknown speaker for %s: \"%s\"\n", name, confkey);
170 continue;
173 *(sep++) = 0;
174 while(isspace(*sep))
175 sep++;
177 for(i = 0;i < chans;i++)
179 if(Speaker2Chan[i] == val)
181 val = strtol(sep, NULL, 10);
182 if(val >= -180 && val <= 180)
183 SpeakerAngle[i] = val * M_PI/180.0f;
184 else
185 AL_PRINT("Invalid angle for speaker \"%s\": %d\n", confkey, val);
186 break;
191 for(i = 0;i < chans;i++)
193 int min = i;
194 int i2;
196 for(i2 = i+1;i2 < chans;i2++)
198 if(SpeakerAngle[i2] < SpeakerAngle[min])
199 min = i2;
202 if(min != i)
204 ALfloat tmpf;
205 ALint tmpi;
207 tmpf = SpeakerAngle[i];
208 SpeakerAngle[i] = SpeakerAngle[min];
209 SpeakerAngle[min] = tmpf;
211 tmpi = Speaker2Chan[i];
212 Speaker2Chan[i] = Speaker2Chan[min];
213 Speaker2Chan[min] = tmpi;
218 static __inline ALfloat aluLUTpos2Angle(ALint pos)
220 if(pos < QUADRANT_NUM)
221 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
222 if(pos < 2 * QUADRANT_NUM)
223 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
224 if(pos < 3 * QUADRANT_NUM)
225 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
226 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
229 ALvoid aluInitPanning(ALCcontext *Context)
231 ALint pos, offset, s;
232 ALfloat Alpha, Theta;
233 ALfloat SpeakerAngle[OUTPUTCHANNELS];
234 ALint Speaker2Chan[OUTPUTCHANNELS];
236 Context->NumChan = 8;
237 Speaker2Chan[0] = BACK_LEFT;
238 Speaker2Chan[1] = SIDE_LEFT;
239 Speaker2Chan[2] = FRONT_LEFT;
240 Speaker2Chan[3] = FRONT_CENTER;
241 Speaker2Chan[4] = FRONT_RIGHT;
242 Speaker2Chan[5] = SIDE_RIGHT;
243 Speaker2Chan[6] = BACK_RIGHT;
244 Speaker2Chan[7] = BACK_CENTER;
245 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
246 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
247 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
248 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
249 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
250 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
251 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
252 SpeakerAngle[7] = 180.0f * M_PI/180.0f;
253 SetSpeakerArrangement("layout_81CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
255 for(pos = 0; pos < LUT_NUM; pos++)
257 /* clear all values */
258 offset = OUTPUTCHANNELS * pos;
259 for(s = 0; s < OUTPUTCHANNELS; s++)
260 Context->PanningLUT[offset+s] = 0.0f;
262 /* source angle */
263 Theta = aluLUTpos2Angle(pos);
265 /* set panning values */
266 for(s = 0; s < Context->NumChan - 1; s++)
268 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
270 /* source between speaker s and speaker s+1 */
271 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
272 (SpeakerAngle[s+1]-SpeakerAngle[s]);
273 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
274 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
275 break;
278 if(s == Context->NumChan - 1)
280 /* source between last and first speaker */
281 if(Theta < SpeakerAngle[0])
282 Theta += 2.0f * M_PI;
283 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
284 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
285 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
286 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
291 static ALvoid CalcNonAttnSourceParams(const ALCcontext *ALContext, ALsource *ALSource)
293 ALfloat SourceVolume,ListenerGain,MinVolume,MaxVolume;
294 ALfloat DryGain, DryGainHF;
295 ALfloat WetGain[MAX_SENDS];
296 ALfloat WetGainHF[MAX_SENDS];
297 ALint NumSends, Frequency;
298 ALfloat cw;
299 ALint i;
301 //Get context properties
302 NumSends = ALContext->Device->NumAuxSends;
303 Frequency = ALContext->Device->Frequency;
305 //Get listener properties
306 ListenerGain = ALContext->Listener.Gain;
308 //Get source properties
309 SourceVolume = ALSource->flGain;
310 MinVolume = ALSource->flMinGain;
311 MaxVolume = ALSource->flMaxGain;
313 //1. Multi-channel buffers always play "normal"
314 ALSource->Params.Pitch = ALSource->flPitch;
316 DryGain = SourceVolume;
317 DryGain = __min(DryGain,MaxVolume);
318 DryGain = __max(DryGain,MinVolume);
319 DryGainHF = 1.0f;
321 switch(ALSource->DirectFilter.type)
323 case AL_FILTER_LOWPASS:
324 DryGain *= ALSource->DirectFilter.Gain;
325 DryGainHF *= ALSource->DirectFilter.GainHF;
326 break;
329 ALSource->Params.DryGains[FRONT_LEFT] = DryGain * ListenerGain;
330 ALSource->Params.DryGains[FRONT_RIGHT] = DryGain * ListenerGain;
331 ALSource->Params.DryGains[SIDE_LEFT] = DryGain * ListenerGain;
332 ALSource->Params.DryGains[SIDE_RIGHT] = DryGain * ListenerGain;
333 ALSource->Params.DryGains[BACK_LEFT] = DryGain * ListenerGain;
334 ALSource->Params.DryGains[BACK_RIGHT] = DryGain * ListenerGain;
335 ALSource->Params.DryGains[FRONT_CENTER] = DryGain * ListenerGain;
336 ALSource->Params.DryGains[BACK_CENTER] = DryGain * ListenerGain;
337 ALSource->Params.DryGains[LFE] = DryGain * ListenerGain;
339 for(i = 0;i < NumSends;i++)
341 WetGain[i] = SourceVolume;
342 WetGain[i] = __min(WetGain[i],MaxVolume);
343 WetGain[i] = __max(WetGain[i],MinVolume);
344 WetGainHF[i] = 1.0f;
346 switch(ALSource->Send[i].WetFilter.type)
348 case AL_FILTER_LOWPASS:
349 WetGain[i] *= ALSource->Send[i].WetFilter.Gain;
350 WetGainHF[i] *= ALSource->Send[i].WetFilter.GainHF;
351 break;
354 ALSource->Params.WetGains[i] = WetGain[i] * ListenerGain;
356 for(i = NumSends;i < MAX_SENDS;i++)
358 ALSource->Params.WetGains[i] = 0.0f;
359 WetGainHF[i] = 1.0f;
362 /* Update filter coefficients. Calculations based on the I3DL2
363 * spec. */
364 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Frequency);
366 /* We use two chained one-pole filters, so we need to take the
367 * square root of the squared gain, which is the same as the base
368 * gain. */
369 ALSource->Params.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
371 for(i = 0;i < NumSends;i++)
373 /* We use a one-pole filter, so we need to take the squared gain */
374 ALfloat a = lpCoeffCalc(WetGainHF[i]*WetGainHF[i], cw);
375 ALSource->Params.Send[i].iirFilter.coeff = a;
379 static ALvoid CalcSourceParams(const ALCcontext *ALContext, ALsource *ALSource)
381 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix,OrigDist;
382 ALfloat Direction[3],Position[3],SourceToListener[3];
383 ALfloat Velocity[3],ListenerVel[3];
384 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
385 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
386 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound;
387 ALfloat Matrix[4][4];
388 ALfloat flAttenuation, effectiveDist;
389 ALfloat RoomAttenuation[MAX_SENDS];
390 ALfloat MetersPerUnit;
391 ALfloat RoomRolloff[MAX_SENDS];
392 ALfloat DryGainHF = 1.0f;
393 ALfloat WetGain[MAX_SENDS];
394 ALfloat WetGainHF[MAX_SENDS];
395 ALfloat DirGain, AmbientGain;
396 ALfloat length;
397 const ALfloat *SpeakerGain;
398 ALuint Frequency;
399 ALint NumSends;
400 ALint pos, s, i;
401 ALfloat cw;
403 for(i = 0;i < MAX_SENDS;i++)
404 WetGainHF[i] = 1.0f;
406 //Get context properties
407 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
408 DopplerVelocity = ALContext->DopplerVelocity;
409 flSpeedOfSound = ALContext->flSpeedOfSound;
410 NumSends = ALContext->Device->NumAuxSends;
411 Frequency = ALContext->Device->Frequency;
413 //Get listener properties
414 ListenerGain = ALContext->Listener.Gain;
415 MetersPerUnit = ALContext->Listener.MetersPerUnit;
416 memcpy(ListenerVel, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
418 //Get source properties
419 SourceVolume = ALSource->flGain;
420 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
421 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
422 memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
423 MinVolume = ALSource->flMinGain;
424 MaxVolume = ALSource->flMaxGain;
425 MinDist = ALSource->flRefDistance;
426 MaxDist = ALSource->flMaxDistance;
427 Rolloff = ALSource->flRollOffFactor;
428 InnerAngle = ALSource->flInnerAngle;
429 OuterAngle = ALSource->flOuterAngle;
430 OuterGainHF = ALSource->OuterGainHF;
432 //1. Translate Listener to origin (convert to head relative)
433 if(ALSource->bHeadRelative==AL_FALSE)
435 ALfloat U[3],V[3],N[3],P[3];
437 // Build transform matrix
438 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
439 aluNormalize(N); // Normalized At-vector
440 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
441 aluNormalize(V); // Normalized Up-vector
442 aluCrossproduct(N, V, U); // Right-vector
443 aluNormalize(U); // Normalized Right-vector
444 P[0] = -(ALContext->Listener.Position[0]*U[0] + // Translation
445 ALContext->Listener.Position[1]*U[1] +
446 ALContext->Listener.Position[2]*U[2]);
447 P[1] = -(ALContext->Listener.Position[0]*V[0] +
448 ALContext->Listener.Position[1]*V[1] +
449 ALContext->Listener.Position[2]*V[2]);
450 P[2] = -(ALContext->Listener.Position[0]*-N[0] +
451 ALContext->Listener.Position[1]*-N[1] +
452 ALContext->Listener.Position[2]*-N[2]);
453 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0]; Matrix[0][3] = 0.0f;
454 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1]; Matrix[1][3] = 0.0f;
455 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2]; Matrix[2][3] = 0.0f;
456 Matrix[3][0] = P[0]; Matrix[3][1] = P[1]; Matrix[3][2] = P[2]; Matrix[3][3] = 1.0f;
458 // Transform source position and direction into listener space
459 aluMatrixVector(Position, 1.0f, Matrix);
460 aluMatrixVector(Direction, 0.0f, Matrix);
461 // Transform source and listener velocity into listener space
462 aluMatrixVector(Velocity, 0.0f, Matrix);
463 aluMatrixVector(ListenerVel, 0.0f, Matrix);
465 else
466 ListenerVel[0] = ListenerVel[1] = ListenerVel[2] = 0.0f;
468 SourceToListener[0] = -Position[0];
469 SourceToListener[1] = -Position[1];
470 SourceToListener[2] = -Position[2];
471 aluNormalize(SourceToListener);
472 aluNormalize(Direction);
474 //2. Calculate distance attenuation
475 Distance = aluSqrt(aluDotproduct(Position, Position));
476 OrigDist = Distance;
478 flAttenuation = 1.0f;
479 for(i = 0;i < NumSends;i++)
481 RoomAttenuation[i] = 1.0f;
483 RoomRolloff[i] = ALSource->RoomRolloffFactor;
484 if(ALSource->Send[i].Slot &&
485 (ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB ||
486 ALSource->Send[i].Slot->effect.type == AL_EFFECT_EAXREVERB))
487 RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
490 switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
491 ALContext->DistanceModel)
493 case AL_INVERSE_DISTANCE_CLAMPED:
494 Distance=__max(Distance,MinDist);
495 Distance=__min(Distance,MaxDist);
496 if(MaxDist < MinDist)
497 break;
498 //fall-through
499 case AL_INVERSE_DISTANCE:
500 if(MinDist > 0.0f)
502 if((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
503 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
504 for(i = 0;i < NumSends;i++)
506 if((MinDist + (RoomRolloff[i] * (Distance - MinDist))) > 0.0f)
507 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (Distance - MinDist)));
510 break;
512 case AL_LINEAR_DISTANCE_CLAMPED:
513 Distance=__max(Distance,MinDist);
514 Distance=__min(Distance,MaxDist);
515 if(MaxDist < MinDist)
516 break;
517 //fall-through
518 case AL_LINEAR_DISTANCE:
519 Distance=__min(Distance,MaxDist);
520 if(MaxDist != MinDist)
522 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
523 for(i = 0;i < NumSends;i++)
524 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(Distance-MinDist)/(MaxDist - MinDist));
526 break;
528 case AL_EXPONENT_DISTANCE_CLAMPED:
529 Distance=__max(Distance,MinDist);
530 Distance=__min(Distance,MaxDist);
531 if(MaxDist < MinDist)
532 break;
533 //fall-through
534 case AL_EXPONENT_DISTANCE:
535 if(Distance > 0.0f && MinDist > 0.0f)
537 flAttenuation = aluPow(Distance/MinDist, -Rolloff);
538 for(i = 0;i < NumSends;i++)
539 RoomAttenuation[i] = aluPow(Distance/MinDist, -RoomRolloff[i]);
541 break;
543 case AL_NONE:
544 break;
547 // Source Gain + Attenuation
548 DryMix = SourceVolume * flAttenuation;
549 for(i = 0;i < NumSends;i++)
550 WetGain[i] = SourceVolume * RoomAttenuation[i];
552 effectiveDist = 0.0f;
553 if(MinDist > 0.0f)
554 effectiveDist = (MinDist/flAttenuation - MinDist)*MetersPerUnit;
556 // Distance-based air absorption
557 if(ALSource->AirAbsorptionFactor > 0.0f && effectiveDist > 0.0f)
559 ALfloat absorb;
561 // Absorption calculation is done in dB
562 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
563 effectiveDist;
564 // Convert dB to linear gain before applying
565 absorb = aluPow(10.0f, absorb/20.0f);
567 DryGainHF *= absorb;
570 //3. Apply directional soundcones
571 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
572 if(Angle >= InnerAngle && Angle <= OuterAngle)
574 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
575 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
576 ConeHF = (1.0f+(OuterGainHF-1.0f)*scale);
578 else if(Angle > OuterAngle)
580 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
581 ConeHF = (1.0f+(OuterGainHF-1.0f));
583 else
585 ConeVolume = 1.0f;
586 ConeHF = 1.0f;
589 // Apply some high-frequency attenuation for sources behind the listener
590 // NOTE: This should be aluDotproduct({0,0,-1}, ListenerToSource), however
591 // that is equivalent to aluDotproduct({0,0,1}, SourceToListener), which is
592 // the same as SourceToListener[2]
593 Angle = aluAcos(SourceToListener[2]) * 180.0f/M_PI;
594 // Sources within the minimum distance attenuate less
595 if(OrigDist < MinDist)
596 Angle *= OrigDist/MinDist;
597 if(Angle > 90.0f)
599 ALfloat scale = (Angle-90.0f) / (180.1f-90.0f); // .1 to account for fp errors
600 ConeHF *= 1.0f - (ALContext->Device->HeadDampen*scale);
603 DryMix *= ConeVolume;
604 if(ALSource->DryGainHFAuto)
605 DryGainHF *= ConeHF;
607 // Clamp to Min/Max Gain
608 DryMix = __min(DryMix,MaxVolume);
609 DryMix = __max(DryMix,MinVolume);
611 for(i = 0;i < NumSends;i++)
613 ALeffectslot *Slot = ALSource->Send[i].Slot;
615 if(!Slot || Slot->effect.type == AL_EFFECT_NULL)
617 ALSource->Params.WetGains[i] = 0.0f;
618 WetGainHF[i] = 1.0f;
619 continue;
622 if(Slot->AuxSendAuto)
624 if(ALSource->WetGainAuto)
625 WetGain[i] *= ConeVolume;
626 if(ALSource->WetGainHFAuto)
627 WetGainHF[i] *= ConeHF;
629 // Clamp to Min/Max Gain
630 WetGain[i] = __min(WetGain[i],MaxVolume);
631 WetGain[i] = __max(WetGain[i],MinVolume);
633 if(Slot->effect.type == AL_EFFECT_REVERB ||
634 Slot->effect.type == AL_EFFECT_EAXREVERB)
636 /* Apply a decay-time transformation to the wet path, based on
637 * the attenuation of the dry path.
639 * Using the approximate (effective) source to listener
640 * distance, the initial decay of the reverb effect is
641 * calculated and applied to the wet path.
643 WetGain[i] *= aluPow(10.0f, effectiveDist /
644 (SPEEDOFSOUNDMETRESPERSEC *
645 Slot->effect.Reverb.DecayTime) *
646 -60.0 / 20.0);
648 WetGainHF[i] *= aluPow(10.0f,
649 log10(Slot->effect.Reverb.AirAbsorptionGainHF) *
650 ALSource->AirAbsorptionFactor * effectiveDist);
653 else
655 /* If the slot's auxiliary send auto is off, the data sent to the
656 * effect slot is the same as the dry path, sans filter effects */
657 WetGain[i] = DryMix;
658 WetGainHF[i] = DryGainHF;
661 switch(ALSource->Send[i].WetFilter.type)
663 case AL_FILTER_LOWPASS:
664 WetGain[i] *= ALSource->Send[i].WetFilter.Gain;
665 WetGainHF[i] *= ALSource->Send[i].WetFilter.GainHF;
666 break;
668 ALSource->Params.WetGains[i] = WetGain[i] * ListenerGain;
670 for(i = NumSends;i < MAX_SENDS;i++)
672 ALSource->Params.WetGains[i] = 0.0f;
673 WetGainHF[i] = 1.0f;
676 // Apply filter gains and filters
677 switch(ALSource->DirectFilter.type)
679 case AL_FILTER_LOWPASS:
680 DryMix *= ALSource->DirectFilter.Gain;
681 DryGainHF *= ALSource->DirectFilter.GainHF;
682 break;
684 DryMix *= ListenerGain;
686 // Calculate Velocity
687 if(DopplerFactor != 0.0f)
689 ALfloat flVSS, flVLS;
690 ALfloat flMaxVelocity = (DopplerVelocity * flSpeedOfSound) /
691 DopplerFactor;
693 flVSS = aluDotproduct(Velocity, SourceToListener);
694 if(flVSS >= flMaxVelocity)
695 flVSS = (flMaxVelocity - 1.0f);
696 else if(flVSS <= -flMaxVelocity)
697 flVSS = -flMaxVelocity + 1.0f;
699 flVLS = aluDotproduct(ListenerVel, SourceToListener);
700 if(flVLS >= flMaxVelocity)
701 flVLS = (flMaxVelocity - 1.0f);
702 else if(flVLS <= -flMaxVelocity)
703 flVLS = -flMaxVelocity + 1.0f;
705 ALSource->Params.Pitch = ALSource->flPitch *
706 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
707 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
709 else
710 ALSource->Params.Pitch = ALSource->flPitch;
712 // Use energy-preserving panning algorithm for multi-speaker playback
713 length = __max(OrigDist, MinDist);
714 if(length > 0.0f)
716 ALfloat invlen = 1.0f/length;
717 Position[0] *= invlen;
718 Position[1] *= invlen;
719 Position[2] *= invlen;
722 pos = aluCart2LUTpos(-Position[2], Position[0]);
723 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
725 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
726 // elevation adjustment for directional gain. this sucks, but
727 // has low complexity
728 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
729 for(s = 0; s < OUTPUTCHANNELS; s++)
731 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
732 ALSource->Params.DryGains[s] = DryMix * gain;
735 /* Update filter coefficients. */
736 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Frequency);
738 /* Spatialized sources use four chained one-pole filters, so we need to
739 * take the fourth root of the squared gain, which is the same as the
740 * square root of the base gain. */
741 ALSource->Params.iirFilter.coeff = lpCoeffCalc(aluSqrt(DryGainHF), cw);
743 for(i = 0;i < NumSends;i++)
745 /* The wet path uses two chained one-pole filters, so take the
746 * base gain (square root of the squared gain) */
747 ALSource->Params.Send[i].iirFilter.coeff = lpCoeffCalc(WetGainHF[i], cw);
751 static __inline ALfloat point(ALfloat val1, ALfloat val2, ALint frac)
753 return val1;
754 (void)val2;
755 (void)frac;
757 static __inline ALfloat lerp(ALfloat val1, ALfloat val2, ALint frac)
759 return val1 + ((val2-val1)*(frac * (1.0f/(1<<FRACTIONBITS))));
761 static __inline ALfloat cos_lerp(ALfloat val1, ALfloat val2, ALint frac)
763 ALfloat mult = (1.0f-cos(frac * (1.0f/(1<<FRACTIONBITS)) * M_PI)) * 0.5f;
764 return val1 + ((val2-val1)*mult);
767 static void MixSomeSources(ALCcontext *ALContext, float (*DryBuffer)[OUTPUTCHANNELS], ALuint SamplesToDo)
769 static float DummyBuffer[BUFFERSIZE];
770 ALfloat *WetBuffer[MAX_SENDS];
771 ALfloat DrySend[OUTPUTCHANNELS];
772 ALfloat dryGainStep[OUTPUTCHANNELS];
773 ALfloat wetGainStep[MAX_SENDS];
774 ALuint i, j, k, out;
775 ALsource *ALSource;
776 ALfloat value, outsamp;
777 ALbufferlistitem *BufferListItem;
778 ALint64 DataSize64,DataPos64;
779 FILTER *DryFilter, *WetFilter[MAX_SENDS];
780 ALfloat WetSend[MAX_SENDS];
781 ALuint rampLength;
782 ALuint DeviceFreq;
783 ALint increment;
784 ALuint DataPosInt, DataPosFrac;
785 ALuint Channels, Bytes;
786 ALuint Frequency;
787 resampler_t Resampler;
788 ALuint BuffersPlayed;
789 ALfloat Pitch;
790 ALenum State;
792 if(!(ALSource=ALContext->SourceList))
793 return;
795 DeviceFreq = ALContext->Device->Frequency;
797 rampLength = DeviceFreq * MIN_RAMP_LENGTH / 1000;
798 rampLength = max(rampLength, SamplesToDo);
800 another_source:
801 if(ALSource->state != AL_PLAYING)
803 if((ALSource=ALSource->next) != NULL)
804 goto another_source;
805 return;
807 j = 0;
809 /* Find buffer format */
810 Frequency = 0;
811 Channels = 0;
812 Bytes = 0;
813 BufferListItem = ALSource->queue;
814 while(BufferListItem != NULL)
816 ALbuffer *ALBuffer;
817 if((ALBuffer=BufferListItem->buffer) != NULL)
819 Channels = aluChannelsFromFormat(ALBuffer->format);
820 Bytes = aluBytesFromFormat(ALBuffer->format);
821 Frequency = ALBuffer->frequency;
822 break;
824 BufferListItem = BufferListItem->next;
827 if(ALSource->NeedsUpdate)
829 //Only apply 3D calculations for mono buffers
830 if(Channels == 1)
831 CalcSourceParams(ALContext, ALSource);
832 else
833 CalcNonAttnSourceParams(ALContext, ALSource);
834 ALSource->NeedsUpdate = AL_FALSE;
837 /* Get source info */
838 Resampler = ALSource->Resampler;
839 State = ALSource->state;
840 BuffersPlayed = ALSource->BuffersPlayed;
841 DataPosInt = ALSource->position;
842 DataPosFrac = ALSource->position_fraction;
844 /* Compute 18.14 fixed point step */
845 Pitch = (ALSource->Params.Pitch*Frequency) / DeviceFreq;
846 if(Pitch > (float)MAX_PITCH) Pitch = (float)MAX_PITCH;
847 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
848 if(increment <= 0) increment = (1<<FRACTIONBITS);
850 if(ALSource->FirstStart)
852 for(i = 0;i < OUTPUTCHANNELS;i++)
853 DrySend[i] = ALSource->Params.DryGains[i];
854 for(i = 0;i < MAX_SENDS;i++)
855 WetSend[i] = ALSource->Params.WetGains[i];
857 else
859 for(i = 0;i < OUTPUTCHANNELS;i++)
860 DrySend[i] = ALSource->DryGains[i];
861 for(i = 0;i < MAX_SENDS;i++)
862 WetSend[i] = ALSource->WetGains[i];
865 DryFilter = &ALSource->Params.iirFilter;
866 for(i = 0;i < MAX_SENDS;i++)
868 WetFilter[i] = &ALSource->Params.Send[i].iirFilter;
869 WetBuffer[i] = (ALSource->Send[i].Slot ?
870 ALSource->Send[i].Slot->WetBuffer :
871 DummyBuffer);
874 /* Get current buffer queue item */
875 BufferListItem = ALSource->queue;
876 for(i = 0;i < BuffersPlayed && BufferListItem;i++)
877 BufferListItem = BufferListItem->next;
879 while(State == AL_PLAYING && j < SamplesToDo)
881 ALuint DataSize = 0;
882 ALbuffer *ALBuffer;
883 ALfloat *Data;
884 ALuint BufferSize;
886 /* Get buffer info */
887 if((ALBuffer=BufferListItem->buffer) != NULL)
889 Data = ALBuffer->data;
890 DataSize = ALBuffer->size;
891 DataSize /= Channels * Bytes;
893 if(DataPosInt >= DataSize)
894 goto skipmix;
896 if(BufferListItem->next)
898 ALbuffer *NextBuf = BufferListItem->next->buffer;
899 if(NextBuf && NextBuf->size)
901 ALint ulExtraSamples = BUFFER_PADDING*Channels*Bytes;
902 ulExtraSamples = min(NextBuf->size, ulExtraSamples);
903 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
906 else if(ALSource->bLooping)
908 ALbuffer *NextBuf = ALSource->queue->buffer;
909 if(NextBuf && NextBuf->size)
911 ALint ulExtraSamples = BUFFER_PADDING*Channels*Bytes;
912 ulExtraSamples = min(NextBuf->size, ulExtraSamples);
913 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
916 else
917 memset(&Data[DataSize*Channels], 0, (BUFFER_PADDING*Channels*Bytes));
919 /* Compute the gain steps for each output channel */
920 for(i = 0;i < OUTPUTCHANNELS;i++)
921 dryGainStep[i] = (ALSource->Params.DryGains[i]-DrySend[i]) /
922 rampLength;
923 for(i = 0;i < MAX_SENDS;i++)
924 wetGainStep[i] = (ALSource->Params.WetGains[i]-WetSend[i]) /
925 rampLength;
927 /* Figure out how many samples we can mix. */
928 DataSize64 = DataSize;
929 DataSize64 <<= FRACTIONBITS;
930 DataPos64 = DataPosInt;
931 DataPos64 <<= FRACTIONBITS;
932 DataPos64 += DataPosFrac;
933 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
935 BufferSize = min(BufferSize, (SamplesToDo-j));
937 /* Actual sample mixing loop */
938 k = 0;
939 Data += DataPosInt*Channels;
941 if(Channels == 1) /* Mono */
943 #define DO_MIX(resampler) do { \
944 while(BufferSize--) \
946 for(i = 0;i < OUTPUTCHANNELS;i++) \
947 DrySend[i] += dryGainStep[i]; \
948 for(i = 0;i < MAX_SENDS;i++) \
949 WetSend[i] += wetGainStep[i]; \
951 /* First order interpolator */ \
952 value = (resampler)(Data[k], Data[k+1], DataPosFrac); \
954 /* Direct path final mix buffer and panning */ \
955 outsamp = lpFilter4P(DryFilter, 0, value); \
956 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT]; \
957 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT]; \
958 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT]; \
959 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT]; \
960 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT]; \
961 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT]; \
962 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER]; \
963 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER]; \
965 /* Room path final mix buffer and panning */ \
966 for(i = 0;i < MAX_SENDS;i++) \
968 outsamp = lpFilter2P(WetFilter[i], 0, value); \
969 WetBuffer[i][j] += outsamp*WetSend[i]; \
972 DataPosFrac += increment; \
973 k += DataPosFrac>>FRACTIONBITS; \
974 DataPosFrac &= FRACTIONMASK; \
975 j++; \
977 } while(0)
979 switch(Resampler)
981 case POINT_RESAMPLER:
982 DO_MIX(point); break;
983 case LINEAR_RESAMPLER:
984 DO_MIX(lerp); break;
985 case COSINE_RESAMPLER:
986 DO_MIX(cos_lerp); break;
987 case RESAMPLER_MIN:
988 case RESAMPLER_MAX:
989 break;
991 #undef DO_MIX
993 else if(Channels == 2) /* Stereo */
995 const int chans[] = {
996 FRONT_LEFT, FRONT_RIGHT
998 const ALfloat scaler = aluSqrt(1.0f/Channels);
1000 #define DO_MIX(resampler) do { \
1001 while(BufferSize--) \
1003 for(i = 0;i < OUTPUTCHANNELS;i++) \
1004 DrySend[i] += dryGainStep[i]; \
1005 for(i = 0;i < MAX_SENDS;i++) \
1006 WetSend[i] += wetGainStep[i]; \
1008 for(i = 0;i < Channels;i++) \
1010 value = (resampler)(Data[k*Channels + i],Data[(k+1)*Channels + i],\
1011 DataPosFrac); \
1012 outsamp = lpFilter2P(DryFilter, chans[i]*2, value); \
1013 DryBuffer[j][chans[i]] += outsamp*DrySend[chans[i]]; \
1014 for(out = 0;out < MAX_SENDS;out++) \
1016 outsamp = lpFilter1P(WetFilter[out], chans[i], value); \
1017 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1021 DataPosFrac += increment; \
1022 k += DataPosFrac>>FRACTIONBITS; \
1023 DataPosFrac &= FRACTIONMASK; \
1024 j++; \
1026 } while(0)
1028 switch(Resampler)
1030 case POINT_RESAMPLER:
1031 DO_MIX(point); break;
1032 case LINEAR_RESAMPLER:
1033 DO_MIX(lerp); break;
1034 case COSINE_RESAMPLER:
1035 DO_MIX(cos_lerp); break;
1036 case RESAMPLER_MIN:
1037 case RESAMPLER_MAX:
1038 break;
1041 else if(Channels == 4) /* Quad */
1043 const int chans[] = {
1044 FRONT_LEFT, FRONT_RIGHT,
1045 BACK_LEFT, BACK_RIGHT
1047 const ALfloat scaler = aluSqrt(1.0f/Channels);
1049 switch(Resampler)
1051 case POINT_RESAMPLER:
1052 DO_MIX(point); break;
1053 case LINEAR_RESAMPLER:
1054 DO_MIX(lerp); break;
1055 case COSINE_RESAMPLER:
1056 DO_MIX(cos_lerp); break;
1057 case RESAMPLER_MIN:
1058 case RESAMPLER_MAX:
1059 break;
1062 else if(Channels == 6) /* 5.1 */
1064 const int chans[] = {
1065 FRONT_LEFT, FRONT_RIGHT,
1066 FRONT_CENTER, LFE,
1067 BACK_LEFT, BACK_RIGHT
1069 const ALfloat scaler = aluSqrt(1.0f/Channels);
1071 switch(Resampler)
1073 case POINT_RESAMPLER:
1074 DO_MIX(point); break;
1075 case LINEAR_RESAMPLER:
1076 DO_MIX(lerp); break;
1077 case COSINE_RESAMPLER:
1078 DO_MIX(cos_lerp); break;
1079 case RESAMPLER_MIN:
1080 case RESAMPLER_MAX:
1081 break;
1084 else if(Channels == 7) /* 6.1 */
1086 const int chans[] = {
1087 FRONT_LEFT, FRONT_RIGHT,
1088 FRONT_CENTER, LFE,
1089 BACK_CENTER,
1090 SIDE_LEFT, SIDE_RIGHT
1092 const ALfloat scaler = aluSqrt(1.0f/Channels);
1094 switch(Resampler)
1096 case POINT_RESAMPLER:
1097 DO_MIX(point); break;
1098 case LINEAR_RESAMPLER:
1099 DO_MIX(lerp); break;
1100 case COSINE_RESAMPLER:
1101 DO_MIX(cos_lerp); break;
1102 case RESAMPLER_MIN:
1103 case RESAMPLER_MAX:
1104 break;
1107 else if(Channels == 8) /* 7.1 */
1109 const int chans[] = {
1110 FRONT_LEFT, FRONT_RIGHT,
1111 FRONT_CENTER, LFE,
1112 BACK_LEFT, BACK_RIGHT,
1113 SIDE_LEFT, SIDE_RIGHT
1115 const ALfloat scaler = aluSqrt(1.0f/Channels);
1117 switch(Resampler)
1119 case POINT_RESAMPLER:
1120 DO_MIX(point); break;
1121 case LINEAR_RESAMPLER:
1122 DO_MIX(lerp); break;
1123 case COSINE_RESAMPLER:
1124 DO_MIX(cos_lerp); break;
1125 case RESAMPLER_MIN:
1126 case RESAMPLER_MAX:
1127 break;
1129 #undef DO_MIX
1131 else /* Unknown? */
1133 for(i = 0;i < OUTPUTCHANNELS;i++)
1134 DrySend[i] += dryGainStep[i]*BufferSize;
1135 for(i = 0;i < MAX_SENDS;i++)
1136 WetSend[i] += wetGainStep[i]*BufferSize;
1137 while(BufferSize--)
1139 DataPosFrac += increment;
1140 k += DataPosFrac>>FRACTIONBITS;
1141 DataPosFrac &= FRACTIONMASK;
1142 j++;
1145 DataPosInt += k;
1147 skipmix:
1148 /* Handle looping sources */
1149 if(DataPosInt >= DataSize)
1151 if(BuffersPlayed < (ALSource->BuffersInQueue-1))
1153 BufferListItem = BufferListItem->next;
1154 BuffersPlayed++;
1155 DataPosInt -= DataSize;
1157 else if(ALSource->bLooping)
1159 BufferListItem = ALSource->queue;
1160 BuffersPlayed = 0;
1161 if(ALSource->BuffersInQueue == 1)
1162 DataPosInt %= DataSize;
1163 else
1164 DataPosInt -= DataSize;
1166 else
1168 State = AL_STOPPED;
1169 BufferListItem = ALSource->queue;
1170 BuffersPlayed = ALSource->BuffersInQueue;
1171 DataPosInt = 0;
1172 DataPosFrac = 0;
1177 /* Update source info */
1178 ALSource->state = State;
1179 ALSource->BuffersPlayed = BuffersPlayed;
1180 ALSource->position = DataPosInt;
1181 ALSource->position_fraction = DataPosFrac;
1182 ALSource->Buffer = BufferListItem->buffer;
1184 for(i = 0;i < OUTPUTCHANNELS;i++)
1185 ALSource->DryGains[i] = DrySend[i];
1186 for(i = 0;i < MAX_SENDS;i++)
1187 ALSource->WetGains[i] = WetSend[i];
1189 ALSource->FirstStart = AL_FALSE;
1191 if((ALSource=ALSource->next) != NULL)
1192 goto another_source;
1195 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
1197 float (*DryBuffer)[OUTPUTCHANNELS];
1198 ALfloat (*Matrix)[OUTPUTCHANNELS];
1199 const ALuint *ChanMap;
1200 ALuint SamplesToDo;
1201 ALeffectslot *ALEffectSlot;
1202 ALCcontext *ALContext;
1203 ALfloat samp;
1204 int fpuState;
1205 ALuint i, j, c;
1207 #if defined(HAVE_FESETROUND)
1208 fpuState = fegetround();
1209 fesetround(FE_TOWARDZERO);
1210 #elif defined(HAVE__CONTROLFP)
1211 fpuState = _controlfp(0, 0);
1212 _controlfp(_RC_CHOP, _MCW_RC);
1213 #else
1214 (void)fpuState;
1215 #endif
1217 DryBuffer = device->DryBuffer;
1218 while(size > 0)
1220 /* Setup variables */
1221 SamplesToDo = min(size, BUFFERSIZE);
1223 /* Clear mixing buffer */
1224 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
1226 SuspendContext(NULL);
1227 for(c = 0;c < device->NumContexts;c++)
1229 ALContext = device->Contexts[c];
1230 SuspendContext(ALContext);
1232 MixSomeSources(ALContext, DryBuffer, SamplesToDo);
1234 /* effect slot processing */
1235 ALEffectSlot = ALContext->EffectSlotList;
1236 while(ALEffectSlot)
1238 if(ALEffectSlot->EffectState)
1239 ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1241 for(i = 0;i < SamplesToDo;i++)
1242 ALEffectSlot->WetBuffer[i] = 0.0f;
1243 ALEffectSlot = ALEffectSlot->next;
1245 ProcessContext(ALContext);
1247 ProcessContext(NULL);
1249 //Post processing loop
1250 ChanMap = device->DevChannels;
1251 Matrix = device->ChannelMatrix;
1252 switch(device->Format)
1254 #define CHECK_WRITE_FORMAT(bits, type, func) \
1255 case AL_FORMAT_MONO##bits: \
1256 for(i = 0;i < SamplesToDo;i++) \
1258 samp = 0.0f; \
1259 for(c = 0;c < OUTPUTCHANNELS;c++) \
1260 samp += DryBuffer[i][c] * Matrix[c][FRONT_CENTER]; \
1261 ((type*)buffer)[ChanMap[FRONT_CENTER]] = (func)(samp); \
1262 buffer = ((type*)buffer) + 1; \
1264 break; \
1265 case AL_FORMAT_STEREO##bits: \
1266 if(device->Bs2b) \
1268 for(i = 0;i < SamplesToDo;i++) \
1270 float samples[2] = { 0.0f, 0.0f }; \
1271 for(c = 0;c < OUTPUTCHANNELS;c++) \
1273 samples[0] += DryBuffer[i][c]*Matrix[c][FRONT_LEFT]; \
1274 samples[1] += DryBuffer[i][c]*Matrix[c][FRONT_RIGHT]; \
1276 bs2b_cross_feed(device->Bs2b, samples); \
1277 ((type*)buffer)[ChanMap[FRONT_LEFT]] = (func)(samples[0]);\
1278 ((type*)buffer)[ChanMap[FRONT_RIGHT]]= (func)(samples[1]);\
1279 buffer = ((type*)buffer) + 2; \
1282 else \
1284 for(i = 0;i < SamplesToDo;i++) \
1286 static const Channel chans[] = { \
1287 FRONT_LEFT, FRONT_RIGHT \
1288 }; \
1289 for(j = 0;j < 2;j++) \
1291 samp = 0.0f; \
1292 for(c = 0;c < OUTPUTCHANNELS;c++) \
1293 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1294 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1296 buffer = ((type*)buffer) + 2; \
1299 break; \
1300 case AL_FORMAT_QUAD##bits: \
1301 for(i = 0;i < SamplesToDo;i++) \
1303 static const Channel chans[] = { \
1304 FRONT_LEFT, FRONT_RIGHT, \
1305 BACK_LEFT, BACK_RIGHT, \
1306 }; \
1307 for(j = 0;j < 4;j++) \
1309 samp = 0.0f; \
1310 for(c = 0;c < OUTPUTCHANNELS;c++) \
1311 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1312 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1314 buffer = ((type*)buffer) + 4; \
1316 break; \
1317 case AL_FORMAT_51CHN##bits: \
1318 for(i = 0;i < SamplesToDo;i++) \
1320 static const Channel chans[] = { \
1321 FRONT_LEFT, FRONT_RIGHT, \
1322 FRONT_CENTER, LFE, \
1323 BACK_LEFT, BACK_RIGHT, \
1324 }; \
1325 for(j = 0;j < 6;j++) \
1327 samp = 0.0f; \
1328 for(c = 0;c < OUTPUTCHANNELS;c++) \
1329 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1330 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1332 buffer = ((type*)buffer) + 6; \
1334 break; \
1335 case AL_FORMAT_61CHN##bits: \
1336 for(i = 0;i < SamplesToDo;i++) \
1338 static const Channel chans[] = { \
1339 FRONT_LEFT, FRONT_RIGHT, \
1340 FRONT_CENTER, LFE, BACK_CENTER, \
1341 SIDE_LEFT, SIDE_RIGHT, \
1342 }; \
1343 for(j = 0;j < 7;j++) \
1345 samp = 0.0f; \
1346 for(c = 0;c < OUTPUTCHANNELS;c++) \
1347 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1348 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1350 buffer = ((type*)buffer) + 7; \
1352 break; \
1353 case AL_FORMAT_71CHN##bits: \
1354 for(i = 0;i < SamplesToDo;i++) \
1356 static const Channel chans[] = { \
1357 FRONT_LEFT, FRONT_RIGHT, \
1358 FRONT_CENTER, LFE, \
1359 BACK_LEFT, BACK_RIGHT, \
1360 SIDE_LEFT, SIDE_RIGHT \
1361 }; \
1362 for(j = 0;j < 8;j++) \
1364 samp = 0.0f; \
1365 for(c = 0;c < OUTPUTCHANNELS;c++) \
1366 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1367 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1369 buffer = ((type*)buffer) + 8; \
1371 break;
1373 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1374 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1375 CHECK_WRITE_FORMAT(8, ALubyte, aluF2UB)
1376 CHECK_WRITE_FORMAT(16, ALshort, aluF2S)
1377 CHECK_WRITE_FORMAT(32, ALfloat, aluF2F)
1378 #undef AL_FORMAT_STEREO32
1379 #undef AL_FORMAT_MONO32
1380 #undef CHECK_WRITE_FORMAT
1382 default:
1383 break;
1386 size -= SamplesToDo;
1389 #if defined(HAVE_FESETROUND)
1390 fesetround(fpuState);
1391 #elif defined(HAVE__CONTROLFP)
1392 _controlfp(fpuState, 0xfffff);
1393 #endif
1396 ALvoid aluHandleDisconnect(ALCdevice *device)
1398 ALuint i;
1400 SuspendContext(NULL);
1401 for(i = 0;i < device->NumContexts;i++)
1403 ALsource *source;
1405 SuspendContext(device->Contexts[i]);
1407 source = device->Contexts[i]->SourceList;
1408 while(source)
1410 if(source->state == AL_PLAYING)
1412 source->state = AL_STOPPED;
1413 source->BuffersPlayed = source->BuffersInQueue;
1414 source->position = 0;
1415 source->position_fraction = 0;
1417 source = source->next;
1419 ProcessContext(device->Contexts[i]);
1422 device->Connected = ALC_FALSE;
1423 ProcessContext(NULL);