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
35 #include "alListener.h"
36 #include "alAuxEffectSlot.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
)
56 static __inline ALshort
aluF2S(ALfloat Value
)
62 i
= (ALint
)(Value
*32768.0f
);
67 i
= (ALint
)(Value
*32767.0f
);
73 static __inline ALubyte
aluF2UB(ALfloat Value
)
75 ALshort i
= aluF2S(Value
);
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
));
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])
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
;
126 strncpy(layout_str
, GetConfigValue(NULL
, name
, ""), sizeof(layout_str
));
129 next
= confkey
= layout_str
;
133 next
= strchr(confkey
, ',');
139 } while(isspace(*next
) || *next
== ',');
142 sep
= strchr(confkey
, '=');
143 if(!sep
|| confkey
== sep
)
147 while(isspace(*end
) && end
!= confkey
)
151 if(strcmp(confkey
, "fl") == 0 || strcmp(confkey
, "front-left") == 0)
153 else if(strcmp(confkey
, "fr") == 0 || strcmp(confkey
, "front-right") == 0)
155 else if(strcmp(confkey
, "fc") == 0 || strcmp(confkey
, "front-center") == 0)
157 else if(strcmp(confkey
, "bl") == 0 || strcmp(confkey
, "back-left") == 0)
159 else if(strcmp(confkey
, "br") == 0 || strcmp(confkey
, "back-right") == 0)
161 else if(strcmp(confkey
, "bc") == 0 || strcmp(confkey
, "back-center") == 0)
163 else if(strcmp(confkey
, "sl") == 0 || strcmp(confkey
, "side-left") == 0)
165 else if(strcmp(confkey
, "sr") == 0 || strcmp(confkey
, "side-right") == 0)
169 AL_PRINT("Unknown speaker for %s: \"%s\"\n", name
, confkey
);
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
;
185 AL_PRINT("Invalid angle for speaker \"%s\": %d\n", confkey
, val
);
191 for(i
= 0;i
< chans
;i
++)
196 for(i2
= i
+1;i2
< chans
;i2
++)
198 if(SpeakerAngle
[i2
] < SpeakerAngle
[min
])
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
;
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
);
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
;
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
);
321 switch(ALSource
->DirectFilter
.type
)
323 case AL_FILTER_LOWPASS
:
324 DryGain
*= ALSource
->DirectFilter
.Gain
;
325 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
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
);
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
;
354 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
356 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
358 ALSource
->Params
.WetGains
[i
] = 0.0f
;
362 /* Update filter coefficients. Calculations based on the I3DL2
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
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
;
397 const ALfloat
*SpeakerGain
;
403 for(i
= 0;i
< MAX_SENDS
;i
++)
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
);
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
));
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
)
499 case AL_INVERSE_DISTANCE
:
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
)));
512 case AL_LINEAR_DISTANCE_CLAMPED
:
513 Distance
=__max(Distance
,MinDist
);
514 Distance
=__min(Distance
,MaxDist
);
515 if(MaxDist
< MinDist
)
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
));
528 case AL_EXPONENT_DISTANCE_CLAMPED
:
529 Distance
=__max(Distance
,MinDist
);
530 Distance
=__min(Distance
,MaxDist
);
531 if(MaxDist
< MinDist
)
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
]);
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
;
554 effectiveDist
= (MinDist
/flAttenuation
- MinDist
)*MetersPerUnit
;
556 // Distance-based air absorption
557 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& effectiveDist
> 0.0f
)
561 // Absorption calculation is done in dB
562 absorb
= (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
) *
564 // Convert dB to linear gain before applying
565 absorb
= aluPow(10.0f
, absorb
/20.0f
);
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
));
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
;
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
)
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
;
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
) *
648 WetGainHF
[i
] *= aluPow(10.0f
,
649 log10(Slot
->effect
.Reverb
.AirAbsorptionGainHF
) *
650 ALSource
->AirAbsorptionFactor
* effectiveDist
);
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 */
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
;
668 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
670 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
672 ALSource
->Params
.WetGains
[i
] = 0.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
;
684 DryMix
*= ListenerGain
;
686 // Calculate Velocity
687 if(DopplerFactor
!= 0.0f
)
689 ALfloat flVSS
, flVLS
;
690 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
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
));
710 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
712 // Use energy-preserving panning algorithm for multi-speaker playback
713 length
= __max(OrigDist
, MinDist
);
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
)
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
];
776 ALfloat value
, outsamp
;
777 ALbufferlistitem
*BufferListItem
;
778 ALint64 DataSize64
,DataPos64
;
779 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
780 ALfloat WetSend
[MAX_SENDS
];
784 ALuint DataPosInt
, DataPosFrac
;
785 ALuint Channels
, Bytes
;
787 resampler_t Resampler
;
788 ALuint BuffersPlayed
;
792 if(!(ALSource
=ALContext
->SourceList
))
795 DeviceFreq
= ALContext
->Device
->Frequency
;
797 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
798 rampLength
= max(rampLength
, SamplesToDo
);
801 if(ALSource
->state
!= AL_PLAYING
)
803 if((ALSource
=ALSource
->next
) != NULL
)
809 /* Find buffer format */
813 BufferListItem
= ALSource
->queue
;
814 while(BufferListItem
!= NULL
)
817 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
819 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
820 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
821 Frequency
= ALBuffer
->frequency
;
824 BufferListItem
= BufferListItem
->next
;
827 if(ALSource
->NeedsUpdate
)
829 //Only apply 3D calculations for mono buffers
831 CalcSourceParams(ALContext
, ALSource
);
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
];
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
:
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
)
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
)
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
);
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
]) /
923 for(i
= 0;i
< MAX_SENDS
;i
++)
924 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-WetSend
[i
]) /
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 */
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; \
981 case POINT_RESAMPLER
:
982 DO_MIX(point
); break;
983 case LINEAR_RESAMPLER
:
985 case COSINE_RESAMPLER
:
986 DO_MIX(cos_lerp
); break;
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],\
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; \
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;
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
);
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;
1062 else if(Channels
== 6) /* 5.1 */
1064 const int chans
[] = {
1065 FRONT_LEFT
, FRONT_RIGHT
,
1067 BACK_LEFT
, BACK_RIGHT
1069 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
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;
1084 else if(Channels
== 7) /* 6.1 */
1086 const int chans
[] = {
1087 FRONT_LEFT
, FRONT_RIGHT
,
1090 SIDE_LEFT
, SIDE_RIGHT
1092 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
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;
1107 else if(Channels
== 8) /* 7.1 */
1109 const int chans
[] = {
1110 FRONT_LEFT
, FRONT_RIGHT
,
1112 BACK_LEFT
, BACK_RIGHT
,
1113 SIDE_LEFT
, SIDE_RIGHT
1115 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
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;
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
;
1139 DataPosFrac
+= increment
;
1140 k
+= DataPosFrac
>>FRACTIONBITS
;
1141 DataPosFrac
&= FRACTIONMASK
;
1148 /* Handle looping sources */
1149 if(DataPosInt
>= DataSize
)
1151 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1153 BufferListItem
= BufferListItem
->next
;
1155 DataPosInt
-= DataSize
;
1157 else if(ALSource
->bLooping
)
1159 BufferListItem
= ALSource
->queue
;
1161 if(ALSource
->BuffersInQueue
== 1)
1162 DataPosInt
%= DataSize
;
1164 DataPosInt
-= DataSize
;
1169 BufferListItem
= ALSource
->queue
;
1170 BuffersPlayed
= ALSource
->BuffersInQueue
;
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
;
1201 ALeffectslot
*ALEffectSlot
;
1202 ALCcontext
*ALContext
;
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
);
1217 DryBuffer
= device
->DryBuffer
;
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
;
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++) \
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; \
1265 case AL_FORMAT_STEREO##bits: \
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; \
1284 for(i = 0;i < SamplesToDo;i++) \
1286 static const Channel chans[] = { \
1287 FRONT_LEFT, FRONT_RIGHT \
1289 for(j = 0;j < 2;j++) \
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; \
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, \
1307 for(j = 0;j < 4;j++) \
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; \
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, \
1325 for(j = 0;j < 6;j++) \
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; \
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, \
1343 for(j = 0;j < 7;j++) \
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; \
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 \
1362 for(j = 0;j < 8;j++) \
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; \
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
1386 size
-= SamplesToDo
;
1389 #if defined(HAVE_FESETROUND)
1390 fesetround(fpuState
);
1391 #elif defined(HAVE__CONTROLFP)
1392 _controlfp(fpuState
, 0xfffff);
1396 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1400 SuspendContext(NULL
);
1401 for(i
= 0;i
< device
->NumContexts
;i
++)
1405 SuspendContext(device
->Contexts
[i
]);
1407 source
= device
->Contexts
[i
]->SourceList
;
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
);