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 #if defined(HAVE_STDINT_H)
42 typedef int64_t ALint64
;
43 #elif defined(HAVE___INT64)
44 typedef __int64 ALint64
;
45 #elif (SIZEOF_LONG == 8)
47 #elif (SIZEOF_LONG_LONG == 8)
48 typedef long long ALint64
;
51 #define FRACTIONBITS 14
52 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
53 #define MAX_PITCH 65536
55 /* Minimum ramp length in milliseconds. The value below was chosen to
56 * adequately reduce clicks and pops from harsh gain changes. */
57 #define MIN_RAMP_LENGTH 16
59 ALboolean DuplicateStereo
= AL_FALSE
;
62 static __inline ALfloat
aluF2F(ALfloat Value
)
64 if(Value
< 0.f
) return Value
/32768.f
;
65 if(Value
> 0.f
) return Value
/32767.f
;
69 static __inline ALshort
aluF2S(ALfloat Value
)
79 static __inline ALubyte
aluF2UB(ALfloat Value
)
81 ALshort i
= aluF2S(Value
);
86 static __inline ALvoid
aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
88 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
89 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
90 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
93 static __inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
95 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
96 inVector1
[2]*inVector2
[2];
99 static __inline ALvoid
aluNormalize(ALfloat
*inVector
)
101 ALfloat length
, inverse_length
;
103 length
= aluSqrt(aluDotproduct(inVector
, inVector
));
106 inverse_length
= 1.0f
/length
;
107 inVector
[0] *= inverse_length
;
108 inVector
[1] *= inverse_length
;
109 inVector
[2] *= inverse_length
;
113 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
116 vector
[0], vector
[1], vector
[2], w
119 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
120 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
121 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
124 static ALvoid
SetSpeakerArrangement(const char *name
, ALfloat SpeakerAngle
[OUTPUTCHANNELS
],
125 ALint Speaker2Chan
[OUTPUTCHANNELS
], ALint chans
)
133 confkey
= GetConfigValue(NULL
, name
, "");
138 next
= strchr(confkey
, ',');
143 } while(isspace(*next
));
146 sep
= strchr(confkey
, '=');
147 if(!sep
|| confkey
== sep
)
151 while(isspace(*end
) && end
!= confkey
)
155 if(strncmp(confkey
, "fl", end
-confkey
) == 0)
157 else if(strncmp(confkey
, "fr", end
-confkey
) == 0)
159 else if(strncmp(confkey
, "fc", end
-confkey
) == 0)
161 else if(strncmp(confkey
, "bl", end
-confkey
) == 0)
163 else if(strncmp(confkey
, "br", end
-confkey
) == 0)
165 else if(strncmp(confkey
, "bc", end
-confkey
) == 0)
167 else if(strncmp(confkey
, "sl", end
-confkey
) == 0)
169 else if(strncmp(confkey
, "sr", end
-confkey
) == 0)
173 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name
, confkey
[0], confkey
[1]);
181 for(i
= 0;i
< chans
;i
++)
183 if(Speaker2Chan
[i
] == val
)
185 val
= strtol(sep
, NULL
, 10);
186 if(val
>= -180 && val
<= 180)
187 SpeakerAngle
[i
] = val
* M_PI
/180.0f
;
189 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey
[0], confkey
[1], val
);
195 for(i
= 1;i
< chans
;i
++)
197 if(SpeakerAngle
[i
] <= SpeakerAngle
[i
-1])
199 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i
, chans
,
200 SpeakerAngle
[i
-1] * 180.0f
/M_PI
, SpeakerAngle
[i
] * 180.0f
/M_PI
);
201 SpeakerAngle
[i
] = SpeakerAngle
[i
-1] + 1 * 180.0f
/M_PI
;
206 static __inline ALfloat
aluLUTpos2Angle(ALint pos
)
208 if(pos
< QUADRANT_NUM
)
209 return aluAtan((ALfloat
)pos
/ (ALfloat
)(QUADRANT_NUM
- pos
));
210 if(pos
< 2 * QUADRANT_NUM
)
211 return M_PI_2
+ aluAtan((ALfloat
)(pos
- QUADRANT_NUM
) / (ALfloat
)(2 * QUADRANT_NUM
- pos
));
212 if(pos
< 3 * QUADRANT_NUM
)
213 return aluAtan((ALfloat
)(pos
- 2 * QUADRANT_NUM
) / (ALfloat
)(3 * QUADRANT_NUM
- pos
)) - M_PI
;
214 return aluAtan((ALfloat
)(pos
- 3 * QUADRANT_NUM
) / (ALfloat
)(4 * QUADRANT_NUM
- pos
)) - M_PI_2
;
217 ALvoid
aluInitPanning(ALCcontext
*Context
)
219 ALint pos
, offset
, s
;
220 ALfloat Alpha
, Theta
;
221 ALfloat SpeakerAngle
[OUTPUTCHANNELS
];
222 ALint Speaker2Chan
[OUTPUTCHANNELS
];
224 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
227 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
228 Context
->ChannelMatrix
[s
][s2
] = ((s
==s2
) ? 1.0f
: 0.0f
);
231 switch(Context
->Device
->Format
)
233 /* Mono is rendered as stereo, then downmixed during post-process */
234 case AL_FORMAT_MONO8
:
235 case AL_FORMAT_MONO16
:
236 case AL_FORMAT_MONO_FLOAT32
:
237 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
238 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
239 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
240 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
241 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
242 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
243 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
244 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
245 Context
->NumChan
= 2;
246 Speaker2Chan
[0] = FRONT_LEFT
;
247 Speaker2Chan
[1] = FRONT_RIGHT
;
248 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
249 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
252 case AL_FORMAT_STEREO8
:
253 case AL_FORMAT_STEREO16
:
254 case AL_FORMAT_STEREO_FLOAT32
:
255 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
256 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
257 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
258 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
259 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
260 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
261 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
262 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
263 Context
->NumChan
= 2;
264 Speaker2Chan
[0] = FRONT_LEFT
;
265 Speaker2Chan
[1] = FRONT_RIGHT
;
266 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
267 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
268 SetSpeakerArrangement("layout_STEREO", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
271 case AL_FORMAT_QUAD8
:
272 case AL_FORMAT_QUAD16
:
273 case AL_FORMAT_QUAD32
:
274 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
275 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
276 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
277 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
278 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
279 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
280 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
281 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
282 Context
->NumChan
= 4;
283 Speaker2Chan
[0] = BACK_LEFT
;
284 Speaker2Chan
[1] = FRONT_LEFT
;
285 Speaker2Chan
[2] = FRONT_RIGHT
;
286 Speaker2Chan
[3] = BACK_RIGHT
;
287 SpeakerAngle
[0] = -135.0f
* M_PI
/180.0f
;
288 SpeakerAngle
[1] = -45.0f
* M_PI
/180.0f
;
289 SpeakerAngle
[2] = 45.0f
* M_PI
/180.0f
;
290 SpeakerAngle
[3] = 135.0f
* M_PI
/180.0f
;
291 SetSpeakerArrangement("layout_QUAD", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
294 case AL_FORMAT_51CHN8
:
295 case AL_FORMAT_51CHN16
:
296 case AL_FORMAT_51CHN32
:
297 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
298 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
299 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
300 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
301 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
302 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
303 Context
->NumChan
= 5;
304 Speaker2Chan
[0] = BACK_LEFT
;
305 Speaker2Chan
[1] = FRONT_LEFT
;
306 Speaker2Chan
[2] = FRONT_CENTER
;
307 Speaker2Chan
[3] = FRONT_RIGHT
;
308 Speaker2Chan
[4] = BACK_RIGHT
;
309 SpeakerAngle
[0] = -110.0f
* M_PI
/180.0f
;
310 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
311 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
312 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
313 SpeakerAngle
[4] = 110.0f
* M_PI
/180.0f
;
314 SetSpeakerArrangement("layout_51CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
317 case AL_FORMAT_61CHN8
:
318 case AL_FORMAT_61CHN16
:
319 case AL_FORMAT_61CHN32
:
320 Context
->ChannelMatrix
[BACK_LEFT
][BACK_CENTER
] = aluSqrt(0.5);
321 Context
->ChannelMatrix
[BACK_LEFT
][SIDE_LEFT
] = aluSqrt(0.5);
322 Context
->ChannelMatrix
[BACK_RIGHT
][BACK_CENTER
] = aluSqrt(0.5);
323 Context
->ChannelMatrix
[BACK_RIGHT
][SIDE_RIGHT
] = aluSqrt(0.5);
324 Context
->NumChan
= 6;
325 Speaker2Chan
[0] = SIDE_LEFT
;
326 Speaker2Chan
[1] = FRONT_LEFT
;
327 Speaker2Chan
[2] = FRONT_CENTER
;
328 Speaker2Chan
[3] = FRONT_RIGHT
;
329 Speaker2Chan
[4] = SIDE_RIGHT
;
330 Speaker2Chan
[5] = BACK_CENTER
;
331 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
332 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
333 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
334 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
335 SpeakerAngle
[4] = 90.0f
* M_PI
/180.0f
;
336 SpeakerAngle
[5] = 180.0f
* M_PI
/180.0f
;
337 SetSpeakerArrangement("layout_61CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
340 case AL_FORMAT_71CHN8
:
341 case AL_FORMAT_71CHN16
:
342 case AL_FORMAT_71CHN32
:
343 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
344 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
345 Context
->NumChan
= 7;
346 Speaker2Chan
[0] = BACK_LEFT
;
347 Speaker2Chan
[1] = SIDE_LEFT
;
348 Speaker2Chan
[2] = FRONT_LEFT
;
349 Speaker2Chan
[3] = FRONT_CENTER
;
350 Speaker2Chan
[4] = FRONT_RIGHT
;
351 Speaker2Chan
[5] = SIDE_RIGHT
;
352 Speaker2Chan
[6] = BACK_RIGHT
;
353 SpeakerAngle
[0] = -150.0f
* M_PI
/180.0f
;
354 SpeakerAngle
[1] = -90.0f
* M_PI
/180.0f
;
355 SpeakerAngle
[2] = -30.0f
* M_PI
/180.0f
;
356 SpeakerAngle
[3] = 0.0f
* M_PI
/180.0f
;
357 SpeakerAngle
[4] = 30.0f
* M_PI
/180.0f
;
358 SpeakerAngle
[5] = 90.0f
* M_PI
/180.0f
;
359 SpeakerAngle
[6] = 150.0f
* M_PI
/180.0f
;
360 SetSpeakerArrangement("layout_71CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
367 for(pos
= 0; pos
< LUT_NUM
; pos
++)
370 Theta
= aluLUTpos2Angle(pos
);
372 /* clear all values */
373 offset
= OUTPUTCHANNELS
* pos
;
374 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
375 Context
->PanningLUT
[offset
+s
] = 0.0f
;
377 /* set panning values */
378 for(s
= 0; s
< Context
->NumChan
- 1; s
++)
380 if(Theta
>= SpeakerAngle
[s
] && Theta
< SpeakerAngle
[s
+1])
382 /* source between speaker s and speaker s+1 */
383 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
384 (SpeakerAngle
[s
+1]-SpeakerAngle
[s
]);
385 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
386 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
+1]] = sin(Alpha
);
390 if(s
== Context
->NumChan
- 1)
392 /* source between last and first speaker */
393 if(Theta
< SpeakerAngle
[0])
394 Theta
+= 2.0f
* M_PI
;
395 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
396 (2.0f
* M_PI
+ SpeakerAngle
[0]-SpeakerAngle
[s
]);
397 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
398 Context
->PanningLUT
[offset
+ Speaker2Chan
[0]] = sin(Alpha
);
403 static ALvoid
CalcSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
,
406 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,DryMix
;
407 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
408 ALfloat Velocity
[3],ListenerVel
[3];
409 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
,OuterGainHF
;
410 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
411 ALfloat DopplerFactor
, DopplerVelocity
, flSpeedOfSound
;
412 ALfloat Matrix
[4][4];
413 ALfloat flAttenuation
;
414 ALfloat RoomAttenuation
[MAX_SENDS
];
415 ALfloat MetersPerUnit
;
416 ALfloat RoomRolloff
[MAX_SENDS
];
417 ALfloat DryGainHF
= 1.0f
;
418 ALfloat WetGain
[MAX_SENDS
];
419 ALfloat WetGainHF
[MAX_SENDS
];
420 ALfloat DirGain
, AmbientGain
;
422 const ALfloat
*SpeakerGain
;
428 //Get context properties
429 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
430 DopplerVelocity
= ALContext
->DopplerVelocity
;
431 flSpeedOfSound
= ALContext
->flSpeedOfSound
;
432 NumSends
= ALContext
->Device
->NumAuxSends
;
433 Frequency
= ALContext
->Device
->Frequency
;
435 //Get listener properties
436 ListenerGain
= ALContext
->Listener
.Gain
;
437 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
438 memcpy(ListenerVel
, ALContext
->Listener
.Velocity
, sizeof(ALContext
->Listener
.Velocity
));
440 //Get source properties
441 SourceVolume
= ALSource
->flGain
;
442 memcpy(Position
, ALSource
->vPosition
, sizeof(ALSource
->vPosition
));
443 memcpy(Direction
, ALSource
->vOrientation
, sizeof(ALSource
->vOrientation
));
444 memcpy(Velocity
, ALSource
->vVelocity
, sizeof(ALSource
->vVelocity
));
445 MinVolume
= ALSource
->flMinGain
;
446 MaxVolume
= ALSource
->flMaxGain
;
447 MinDist
= ALSource
->flRefDistance
;
448 MaxDist
= ALSource
->flMaxDistance
;
449 Rolloff
= ALSource
->flRollOffFactor
;
450 InnerAngle
= ALSource
->flInnerAngle
;
451 OuterAngle
= ALSource
->flOuterAngle
;
452 OuterGainHF
= ALSource
->OuterGainHF
;
454 //Only apply 3D calculations for mono buffers
455 if(isMono
== AL_FALSE
)
457 //1. Multi-channel buffers always play "normal"
458 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
460 DryMix
= SourceVolume
;
461 DryMix
= __min(DryMix
,MaxVolume
);
462 DryMix
= __max(DryMix
,MinVolume
);
464 switch(ALSource
->DirectFilter
.type
)
466 case AL_FILTER_LOWPASS
:
467 DryMix
*= ALSource
->DirectFilter
.Gain
;
468 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
472 ALSource
->Params
.DryGains
[FRONT_LEFT
] = DryMix
* ListenerGain
;
473 ALSource
->Params
.DryGains
[FRONT_RIGHT
] = DryMix
* ListenerGain
;
474 ALSource
->Params
.DryGains
[SIDE_LEFT
] = DryMix
* ListenerGain
;
475 ALSource
->Params
.DryGains
[SIDE_RIGHT
] = DryMix
* ListenerGain
;
476 ALSource
->Params
.DryGains
[BACK_LEFT
] = DryMix
* ListenerGain
;
477 ALSource
->Params
.DryGains
[BACK_RIGHT
] = DryMix
* ListenerGain
;
478 ALSource
->Params
.DryGains
[FRONT_CENTER
] = DryMix
* ListenerGain
;
479 ALSource
->Params
.DryGains
[BACK_CENTER
] = DryMix
* ListenerGain
;
480 ALSource
->Params
.DryGains
[LFE
] = DryMix
* ListenerGain
;
481 for(i
= 0;i
< MAX_SENDS
;i
++)
482 ALSource
->Params
.WetGains
[i
] = 0.0f
;
484 /* Update filter coefficients. Calculations based on the I3DL2
486 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
487 /* We use two chained one-pole filters, so we need to take the
488 * square root of the squared gain, which is the same as the base
490 g
= __max(DryGainHF
, 0.01f
);
492 /* Be careful with gains < 0.0001, as that causes the coefficient
493 * head towards 1, which will flatten the signal */
494 if(g
< 0.9999f
) /* 1-epsilon */
495 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
497 ALSource
->Params
.iirFilter
.coeff
= a
;
498 for(i
= 0;i
< MAX_SENDS
;i
++)
499 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= 0.0f
;
504 //1. Translate Listener to origin (convert to head relative)
505 if(ALSource
->bHeadRelative
==AL_FALSE
)
507 ALfloat U
[3],V
[3],N
[3],P
[3];
509 // Build transform matrix
510 memcpy(N
, ALContext
->Listener
.Forward
, sizeof(N
)); // At-vector
511 aluNormalize(N
); // Normalized At-vector
512 memcpy(V
, ALContext
->Listener
.Up
, sizeof(V
)); // Up-vector
513 aluNormalize(V
); // Normalized Up-vector
514 aluCrossproduct(N
, V
, U
); // Right-vector
515 aluNormalize(U
); // Normalized Right-vector
516 P
[0] = -(ALContext
->Listener
.Position
[0]*U
[0] + // Translation
517 ALContext
->Listener
.Position
[1]*U
[1] +
518 ALContext
->Listener
.Position
[2]*U
[2]);
519 P
[1] = -(ALContext
->Listener
.Position
[0]*V
[0] +
520 ALContext
->Listener
.Position
[1]*V
[1] +
521 ALContext
->Listener
.Position
[2]*V
[2]);
522 P
[2] = -(ALContext
->Listener
.Position
[0]*-N
[0] +
523 ALContext
->Listener
.Position
[1]*-N
[1] +
524 ALContext
->Listener
.Position
[2]*-N
[2]);
525 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0]; Matrix
[0][3] = 0.0f
;
526 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1]; Matrix
[1][3] = 0.0f
;
527 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2]; Matrix
[2][3] = 0.0f
;
528 Matrix
[3][0] = P
[0]; Matrix
[3][1] = P
[1]; Matrix
[3][2] = P
[2]; Matrix
[3][3] = 1.0f
;
530 // Transform source position and direction into listener space
531 aluMatrixVector(Position
, 1.0f
, Matrix
);
532 aluMatrixVector(Direction
, 0.0f
, Matrix
);
533 // Transform source and listener velocity into listener space
534 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
535 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
538 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
540 SourceToListener
[0] = -Position
[0];
541 SourceToListener
[1] = -Position
[1];
542 SourceToListener
[2] = -Position
[2];
543 aluNormalize(SourceToListener
);
544 aluNormalize(Direction
);
546 //2. Calculate distance attenuation
547 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
549 flAttenuation
= 1.0f
;
550 for(i
= 0;i
< MAX_SENDS
;i
++)
552 RoomAttenuation
[i
] = 1.0f
;
554 RoomRolloff
[i
] = ALSource
->RoomRolloffFactor
;
555 if(ALSource
->Send
[i
].Slot
&&
556 (ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
557 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
))
558 RoomRolloff
[i
] += ALSource
->Send
[i
].Slot
->effect
.Reverb
.RoomRolloffFactor
;
561 switch(ALSource
->DistanceModel
)
563 case AL_INVERSE_DISTANCE_CLAMPED
:
564 Distance
=__max(Distance
,MinDist
);
565 Distance
=__min(Distance
,MaxDist
);
566 if(MaxDist
< MinDist
)
569 case AL_INVERSE_DISTANCE
:
572 if((MinDist
+ (Rolloff
* (Distance
- MinDist
))) > 0.0f
)
573 flAttenuation
= MinDist
/ (MinDist
+ (Rolloff
* (Distance
- MinDist
)));
574 for(i
= 0;i
< NumSends
;i
++)
576 if((MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
))) > 0.0f
)
577 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
)));
582 case AL_LINEAR_DISTANCE_CLAMPED
:
583 Distance
=__max(Distance
,MinDist
);
584 Distance
=__min(Distance
,MaxDist
);
585 if(MaxDist
< MinDist
)
588 case AL_LINEAR_DISTANCE
:
589 Distance
=__min(Distance
,MaxDist
);
590 if(MaxDist
!= MinDist
)
592 flAttenuation
= 1.0f
- (Rolloff
*(Distance
-MinDist
)/(MaxDist
- MinDist
));
593 for(i
= 0;i
< NumSends
;i
++)
594 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(Distance
-MinDist
)/(MaxDist
- MinDist
));
598 case AL_EXPONENT_DISTANCE_CLAMPED
:
599 Distance
=__max(Distance
,MinDist
);
600 Distance
=__min(Distance
,MaxDist
);
601 if(MaxDist
< MinDist
)
604 case AL_EXPONENT_DISTANCE
:
605 if(Distance
> 0.0f
&& MinDist
> 0.0f
)
607 flAttenuation
= (ALfloat
)pow(Distance
/MinDist
, -Rolloff
);
608 for(i
= 0;i
< NumSends
;i
++)
609 RoomAttenuation
[i
] = (ALfloat
)pow(Distance
/MinDist
, -RoomRolloff
[i
]);
617 // Source Gain + Attenuation and clamp to Min/Max Gain
618 DryMix
= SourceVolume
* flAttenuation
;
619 DryMix
= __min(DryMix
,MaxVolume
);
620 DryMix
= __max(DryMix
,MinVolume
);
622 for(i
= 0;i
< NumSends
;i
++)
624 ALfloat WetMix
= SourceVolume
* RoomAttenuation
[i
];
625 WetMix
= __min(WetMix
,MaxVolume
);
626 WetGain
[i
] = __max(WetMix
,MinVolume
);
630 // Distance-based air absorption
631 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& flAttenuation
< 1.0f
)
633 ALfloat absorb
= 0.0f
;
635 // Absorption calculation is done in dB
636 if(flAttenuation
> 0.0f
)
638 absorb
= (MinDist
/flAttenuation
- MinDist
)*MetersPerUnit
*
639 (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
);
640 // Convert dB to linear gain before applying
641 absorb
= pow(10.0, absorb
/20.0);
644 for(i
= 0;i
< MAX_SENDS
;i
++)
645 WetGainHF
[i
] *= absorb
;
648 //3. Apply directional soundcones
649 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * 180.0f
/M_PI
;
650 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
652 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
653 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
)*scale
);
654 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
)*scale
);
655 DryMix
*= ConeVolume
;
656 if(ALSource
->DryGainHFAuto
)
659 else if(Angle
> OuterAngle
)
661 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
));
662 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
));
663 DryMix
*= ConeVolume
;
664 if(ALSource
->DryGainHFAuto
)
673 //4. Calculate Velocity
674 if(DopplerFactor
!= 0.0f
)
676 ALfloat flVSS
, flVLS
;
677 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
680 flVSS
= aluDotproduct(Velocity
, SourceToListener
);
681 if(flVSS
>= flMaxVelocity
)
682 flVSS
= (flMaxVelocity
- 1.0f
);
683 else if(flVSS
<= -flMaxVelocity
)
684 flVSS
= -flMaxVelocity
+ 1.0f
;
686 flVLS
= aluDotproduct(ListenerVel
, SourceToListener
);
687 if(flVLS
>= flMaxVelocity
)
688 flVLS
= (flMaxVelocity
- 1.0f
);
689 else if(flVLS
<= -flMaxVelocity
)
690 flVLS
= -flMaxVelocity
+ 1.0f
;
692 ALSource
->Params
.Pitch
= ALSource
->flPitch
*
693 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
694 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
697 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
699 for(i
= 0;i
< NumSends
;i
++)
701 if(ALSource
->Send
[i
].Slot
&&
702 ALSource
->Send
[i
].Slot
->effect
.type
!= AL_EFFECT_NULL
)
704 if(ALSource
->Send
[i
].Slot
->AuxSendAuto
)
706 if(ALSource
->WetGainAuto
)
707 WetGain
[i
] *= ConeVolume
;
708 if(ALSource
->WetGainHFAuto
)
709 WetGainHF
[i
] *= ConeHF
;
711 if(ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
712 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
)
714 /* Apply a decay-time transformation to the wet path,
715 * based on the attenuation of the dry path. This should
716 * better approximate the statistical attenuation model
717 * for the reverb effect.
719 * This simple equation converts the distance attenuation
720 * into the time it would take to reach -60 dB. From
721 * there it establishes an origin (0.333s; the decay time
722 * that will produce equal attenuation) and applies the
723 * current decay time. Finally, it converts the result
724 * back to an attenuation for the reverb path.
726 WetGain
[i
] *= pow(10.0f
, log10(flAttenuation
) * 0.333f
/
727 ALSource
->Send
[i
].Slot
->effect
.Reverb
.DecayTime
);
732 // If the slot's auxiliary send auto is off, the data sent to
733 // the effect slot is the same as the dry path, sans filter
736 WetGainHF
[i
] = DryGainHF
;
739 switch(ALSource
->Send
[i
].WetFilter
.type
)
741 case AL_FILTER_LOWPASS
:
742 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
743 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
746 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
750 ALSource
->Params
.WetGains
[i
] = 0.0f
;
754 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
756 ALSource
->Params
.WetGains
[i
] = 0.0f
;
760 //5. Apply filter gains and filters
761 switch(ALSource
->DirectFilter
.type
)
763 case AL_FILTER_LOWPASS
:
764 DryMix
*= ALSource
->DirectFilter
.Gain
;
765 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
768 DryMix
*= ListenerGain
;
770 // Use energy-preserving panning algorithm for multi-speaker playback
771 length
= aluSqrt(Position
[0]*Position
[0] + Position
[1]*Position
[1] +
772 Position
[2]*Position
[2]);
773 length
= __max(length
, MinDist
);
776 ALfloat invlen
= 1.0f
/length
;
777 Position
[0] *= invlen
;
778 Position
[1] *= invlen
;
779 Position
[2] *= invlen
;
782 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
783 SpeakerGain
= &ALContext
->PanningLUT
[OUTPUTCHANNELS
* pos
];
785 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
786 // elevation adjustment for directional gain. this sucks, but
787 // has low complexity
788 AmbientGain
= 1.0/aluSqrt(ALContext
->NumChan
) * (1.0-DirGain
);
789 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
791 ALfloat gain
= SpeakerGain
[s
]*DirGain
+ AmbientGain
;
792 ALSource
->Params
.DryGains
[s
] = DryMix
* gain
;
795 /* Update filter coefficients. */
796 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
797 /* Spatialized sources use four chained one-pole filters, so we need to
798 * take the fourth root of the squared gain, which is the same as the
799 * square root of the base gain. */
800 g
= aluSqrt(__max(DryGainHF
, 0.0001f
));
802 if(g
< 0.9999f
) /* 1-epsilon */
803 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
805 ALSource
->Params
.iirFilter
.coeff
= a
;
807 for(i
= 0;i
< NumSends
;i
++)
809 /* The wet path uses two chained one-pole filters, so take the
810 * base gain (square root of the squared gain) */
811 g
= __max(WetGainHF
[i
], 0.01f
);
813 if(g
< 0.9999f
) /* 1-epsilon */
814 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
816 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
820 static __inline ALshort
lerp(ALshort val1
, ALshort val2
, ALint frac
)
822 return val1
+ (((val2
-val1
)*frac
)>>FRACTIONBITS
);
825 static void MixSomeSources(ALCcontext
*ALContext
, float (*DryBuffer
)[OUTPUTCHANNELS
], ALuint SamplesToDo
)
827 static float DummyBuffer
[BUFFERSIZE
];
828 ALfloat
*WetBuffer
[MAX_SENDS
];
829 ALfloat (*Matrix
)[OUTPUTCHANNELS
] = ALContext
->ChannelMatrix
;
830 ALfloat DrySend
[OUTPUTCHANNELS
];
831 ALfloat dryGainStep
[OUTPUTCHANNELS
];
832 ALfloat wetGainStep
[MAX_SENDS
];
836 ALbufferlistitem
*BufferListItem
;
837 ALint64 DataSize64
,DataPos64
;
838 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
839 ALfloat WetSend
[MAX_SENDS
];
843 ALuint DataPosInt
, DataPosFrac
;
844 ALuint Channels
, Bytes
;
846 ALuint BuffersPlayed
;
850 if(!(ALSource
=ALContext
->Source
))
853 DeviceFreq
= ALContext
->Device
->Frequency
;
855 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
856 rampLength
= max(rampLength
, SamplesToDo
);
859 State
= ALSource
->state
;
860 if(State
!= AL_PLAYING
)
862 if((ALSource
=ALSource
->next
) != NULL
)
868 /* Find buffer format */
872 BufferListItem
= ALSource
->queue
;
873 while(BufferListItem
!= NULL
)
876 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
878 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
879 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
880 Frequency
= ALBuffer
->frequency
;
883 BufferListItem
= BufferListItem
->next
;
886 /* Get source info */
887 BuffersPlayed
= ALSource
->BuffersPlayed
;
888 DataPosInt
= ALSource
->position
;
889 DataPosFrac
= ALSource
->position_fraction
;
891 CalcSourceParams(ALContext
, ALSource
, (Channels
==1)?AL_TRUE
:AL_FALSE
);
893 /* Compute 18.14 fixed point step */
894 Pitch
= (ALSource
->Params
.Pitch
*Frequency
) / DeviceFreq
;
895 if(Pitch
> (float)MAX_PITCH
) Pitch
= (float)MAX_PITCH
;
896 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
897 if(increment
<= 0) increment
= (1<<FRACTIONBITS
);
899 /* Compute the gain steps for each output channel */
900 if(ALSource
->FirstStart
)
902 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
903 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
904 for(i
= 0;i
< MAX_SENDS
;i
++)
905 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
909 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
910 DrySend
[i
] = ALSource
->DryGains
[i
];
911 for(i
= 0;i
< MAX_SENDS
;i
++)
912 WetSend
[i
] = ALSource
->WetGains
[i
];
915 DryFilter
= &ALSource
->Params
.iirFilter
;
916 for(i
= 0;i
< MAX_SENDS
;i
++)
918 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
919 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
920 ALSource
->Send
[i
].Slot
->WetBuffer
:
924 if(DuplicateStereo
&& Channels
== 2)
926 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
927 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
928 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
929 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
931 else if(DuplicateStereo
)
933 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
934 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
935 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
936 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
939 /* Get current buffer queue item */
940 BufferListItem
= ALSource
->queue
;
941 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
942 BufferListItem
= BufferListItem
->next
;
944 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
951 /* Get buffer info */
952 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
954 Data
= ALBuffer
->data
;
955 DataSize
= ALBuffer
->size
;
956 DataSize
/= Channels
* Bytes
;
958 if(DataPosInt
>= DataSize
)
961 if(BufferListItem
->next
)
963 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
964 if(NextBuf
&& NextBuf
->data
)
966 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
967 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
968 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
971 else if(ALSource
->bLooping
)
973 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
974 if(NextBuf
&& NextBuf
->data
)
976 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
977 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
978 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
982 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
984 /* Compute the gain steps for each output channel */
985 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
986 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-
987 DrySend
[i
]) / rampLength
;
988 for(i
= 0;i
< MAX_SENDS
;i
++)
989 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-
990 WetSend
[i
]) / rampLength
;
992 /* Figure out how many samples we can mix. */
993 DataSize64
= DataSize
;
994 DataSize64
<<= FRACTIONBITS
;
995 DataPos64
= DataPosInt
;
996 DataPos64
<<= FRACTIONBITS
;
997 DataPos64
+= DataPosFrac
;
998 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
1000 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
1002 /* Actual sample mixing loop */
1004 Data
+= DataPosInt
*Channels
;
1006 if(Channels
== 1) /* Mono */
1012 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1013 DrySend
[i
] += dryGainStep
[i
];
1014 for(i
= 0;i
< MAX_SENDS
;i
++)
1015 WetSend
[i
] += wetGainStep
[i
];
1017 /* First order interpolator */
1018 value
= lerp(Data
[k
], Data
[k
+1], DataPosFrac
);
1020 /* Direct path final mix buffer and panning */
1021 outsamp
= lpFilter4P(DryFilter
, 0, value
);
1022 DryBuffer
[j
][FRONT_LEFT
] += outsamp
*DrySend
[FRONT_LEFT
];
1023 DryBuffer
[j
][FRONT_RIGHT
] += outsamp
*DrySend
[FRONT_RIGHT
];
1024 DryBuffer
[j
][SIDE_LEFT
] += outsamp
*DrySend
[SIDE_LEFT
];
1025 DryBuffer
[j
][SIDE_RIGHT
] += outsamp
*DrySend
[SIDE_RIGHT
];
1026 DryBuffer
[j
][BACK_LEFT
] += outsamp
*DrySend
[BACK_LEFT
];
1027 DryBuffer
[j
][BACK_RIGHT
] += outsamp
*DrySend
[BACK_RIGHT
];
1028 DryBuffer
[j
][FRONT_CENTER
] += outsamp
*DrySend
[FRONT_CENTER
];
1029 DryBuffer
[j
][BACK_CENTER
] += outsamp
*DrySend
[BACK_CENTER
];
1031 /* Room path final mix buffer and panning */
1032 for(i
= 0;i
< MAX_SENDS
;i
++)
1034 outsamp
= lpFilter2P(WetFilter
[i
], 0, value
);
1035 WetBuffer
[i
][j
] += outsamp
*WetSend
[i
];
1038 DataPosFrac
+= increment
;
1039 k
+= DataPosFrac
>>FRACTIONBITS
;
1040 DataPosFrac
&= FRACTIONMASK
;
1044 else if(Channels
== 2) /* Stereo */
1046 const int chans
[] = {
1047 FRONT_LEFT
, FRONT_RIGHT
1050 #define DO_MIX() do { \
1051 for(i = 0;i < MAX_SENDS;i++) \
1052 WetSend[i] += wetGainStep[i]*BufferSize; \
1053 while(BufferSize--) \
1055 for(i = 0;i < OUTPUTCHANNELS;i++) \
1056 DrySend[i] += dryGainStep[i]; \
1058 for(i = 0;i < Channels;i++) \
1060 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1061 value = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1062 for(out = 0;out < OUTPUTCHANNELS;out++) \
1063 DryBuffer[j][out] += value*Matrix[chans[i]][out]; \
1066 DataPosFrac += increment; \
1067 k += DataPosFrac>>FRACTIONBITS; \
1068 DataPosFrac &= FRACTIONMASK; \
1075 else if(Channels
== 4) /* Quad */
1077 const int chans
[] = {
1078 FRONT_LEFT
, FRONT_RIGHT
,
1079 BACK_LEFT
, BACK_RIGHT
1084 else if(Channels
== 6) /* 5.1 */
1086 const int chans
[] = {
1087 FRONT_LEFT
, FRONT_RIGHT
,
1089 BACK_LEFT
, BACK_RIGHT
1094 else if(Channels
== 7) /* 6.1 */
1096 const int chans
[] = {
1097 FRONT_LEFT
, FRONT_RIGHT
,
1100 SIDE_LEFT
, SIDE_RIGHT
1105 else if(Channels
== 8) /* 7.1 */
1107 const int chans
[] = {
1108 FRONT_LEFT
, FRONT_RIGHT
,
1110 BACK_LEFT
, BACK_RIGHT
,
1111 SIDE_LEFT
, SIDE_RIGHT
1119 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1120 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1121 for(i
= 0;i
< MAX_SENDS
;i
++)
1122 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1125 DataPosFrac
+= increment
;
1126 k
+= DataPosFrac
>>FRACTIONBITS
;
1127 DataPosFrac
&= FRACTIONMASK
;
1134 /* Handle looping sources */
1135 if(DataPosInt
>= DataSize
)
1137 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1139 BufferListItem
= BufferListItem
->next
;
1141 DataPosInt
-= DataSize
;
1145 if(!ALSource
->bLooping
)
1148 BufferListItem
= ALSource
->queue
;
1149 BuffersPlayed
= ALSource
->BuffersInQueue
;
1155 BufferListItem
= ALSource
->queue
;
1157 if(ALSource
->BuffersInQueue
== 1)
1158 DataPosInt
%= DataSize
;
1160 DataPosInt
-= DataSize
;
1166 /* Update source info */
1167 ALSource
->state
= State
;
1168 ALSource
->BuffersPlayed
= BuffersPlayed
;
1169 ALSource
->position
= DataPosInt
;
1170 ALSource
->position_fraction
= DataPosFrac
;
1171 ALSource
->Buffer
= BufferListItem
->buffer
;
1173 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1174 ALSource
->DryGains
[i
] = DrySend
[i
];
1175 for(i
= 0;i
< MAX_SENDS
;i
++)
1176 ALSource
->WetGains
[i
] = WetSend
[i
];
1178 ALSource
->FirstStart
= AL_FALSE
;
1180 if((ALSource
=ALSource
->next
) != NULL
)
1181 goto another_source
;
1184 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1186 float (*DryBuffer
)[OUTPUTCHANNELS
];
1188 ALeffectslot
*ALEffectSlot
;
1189 ALCcontext
*ALContext
;
1193 SuspendContext(NULL
);
1195 #if defined(HAVE_FESETROUND)
1196 fpuState
= fegetround();
1197 fesetround(FE_TOWARDZERO
);
1198 #elif defined(HAVE__CONTROLFP)
1199 fpuState
= _controlfp(0, 0);
1200 _controlfp(_RC_CHOP
, _MCW_RC
);
1205 DryBuffer
= device
->DryBuffer
;
1208 /* Setup variables */
1209 SamplesToDo
= min(size
, BUFFERSIZE
);
1211 /* Clear mixing buffer */
1212 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1214 for(c
= 0;c
< device
->NumContexts
;c
++)
1216 ALContext
= device
->Contexts
[c
];
1217 SuspendContext(ALContext
);
1219 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1221 /* effect slot processing */
1222 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1225 if(ALEffectSlot
->EffectState
)
1226 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1228 for(i
= 0;i
< SamplesToDo
;i
++)
1229 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1230 ALEffectSlot
= ALEffectSlot
->next
;
1232 ProcessContext(ALContext
);
1235 //Post processing loop
1236 switch(device
->Format
)
1238 #define CHECK_WRITE_FORMAT(bits, type, func, isWin) \
1239 case AL_FORMAT_MONO##bits: \
1240 for(i = 0;i < SamplesToDo;i++) \
1242 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT] + \
1243 DryBuffer[i][FRONT_RIGHT]); \
1244 buffer = ((type*)buffer) + 1; \
1247 case AL_FORMAT_STEREO##bits: \
1250 for(i = 0;i < SamplesToDo;i++) \
1253 samples[0] = DryBuffer[i][FRONT_LEFT]; \
1254 samples[1] = DryBuffer[i][FRONT_RIGHT]; \
1255 bs2b_cross_feed(device->Bs2b, samples); \
1256 ((type*)buffer)[0] = (func)(samples[0]); \
1257 ((type*)buffer)[1] = (func)(samples[1]); \
1258 buffer = ((type*)buffer) + 2; \
1263 for(i = 0;i < SamplesToDo;i++) \
1265 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1266 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1267 buffer = ((type*)buffer) + 2; \
1271 case AL_FORMAT_QUAD##bits: \
1272 for(i = 0;i < SamplesToDo;i++) \
1274 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1275 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1276 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1277 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1278 buffer = ((type*)buffer) + 4; \
1281 case AL_FORMAT_51CHN##bits: \
1282 for(i = 0;i < SamplesToDo;i++) \
1284 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1285 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1287 /* Of course, Windows can't use the same ordering... */ \
1288 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1289 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1290 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1291 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1293 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1294 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1295 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1296 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1298 buffer = ((type*)buffer) + 6; \
1301 case AL_FORMAT_61CHN##bits: \
1302 for(i = 0;i < SamplesToDo;i++) \
1304 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1305 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1306 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1307 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1308 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_CENTER]); \
1309 ((type*)buffer)[5] = (func)(DryBuffer[i][SIDE_LEFT]); \
1310 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1311 buffer = ((type*)buffer) + 7; \
1314 case AL_FORMAT_71CHN##bits: \
1315 for(i = 0;i < SamplesToDo;i++) \
1317 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1318 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1320 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1321 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1322 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1323 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1325 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1326 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1327 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1328 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1330 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_LEFT]); \
1331 ((type*)buffer)[7] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1332 buffer = ((type*)buffer) + 8; \
1336 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1337 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1339 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 1)
1340 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 1)
1341 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 1)
1343 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 0)
1344 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 0)
1345 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 0)
1347 #undef AL_FORMAT_STEREO32
1348 #undef AL_FORMAT_MONO32
1349 #undef CHECK_WRITE_FORMAT
1355 size
-= SamplesToDo
;
1358 #if defined(HAVE_FESETROUND)
1359 fesetround(fpuState
);
1360 #elif defined(HAVE__CONTROLFP)
1361 _controlfp(fpuState
, 0xfffff);
1364 ProcessContext(NULL
);
1367 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1371 SuspendContext(NULL
);
1372 for(i
= 0;i
< device
->NumContexts
;i
++)
1376 SuspendContext(device
->Contexts
[i
]);
1378 source
= device
->Contexts
[i
]->Source
;
1381 if(source
->state
== AL_PLAYING
)
1383 source
->state
= AL_STOPPED
;
1384 source
->BuffersPlayed
= source
->BuffersInQueue
;
1385 source
->position
= 0;
1386 source
->position_fraction
= 0;
1388 source
= source
->next
;
1390 ProcessContext(device
->Contexts
[i
]);
1393 device
->Connected
= ALC_FALSE
;
1394 ProcessContext(NULL
);