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 matrix
[3][3])
117 result
[0] = vector
[0]*matrix
[0][0] + vector
[1]*matrix
[1][0] + vector
[2]*matrix
[2][0];
118 result
[1] = vector
[0]*matrix
[0][1] + vector
[1]*matrix
[1][1] + vector
[2]*matrix
[2][1];
119 result
[2] = vector
[0]*matrix
[0][2] + vector
[1]*matrix
[1][2] + vector
[2]*matrix
[2][2];
120 memcpy(vector
, result
, sizeof(result
));
123 static ALvoid
SetSpeakerArrangement(const char *name
, ALfloat SpeakerAngle
[OUTPUTCHANNELS
],
124 ALint Speaker2Chan
[OUTPUTCHANNELS
], ALint chans
)
132 confkey
= GetConfigValue(NULL
, name
, "");
137 next
= strchr(confkey
, ',');
142 } while(isspace(*next
));
145 sep
= strchr(confkey
, '=');
146 if(!sep
|| confkey
== sep
)
150 while(isspace(*end
) && end
!= confkey
)
154 if(strncmp(confkey
, "fl", end
-confkey
) == 0)
156 else if(strncmp(confkey
, "fr", end
-confkey
) == 0)
158 else if(strncmp(confkey
, "fc", end
-confkey
) == 0)
160 else if(strncmp(confkey
, "bl", end
-confkey
) == 0)
162 else if(strncmp(confkey
, "br", end
-confkey
) == 0)
164 else if(strncmp(confkey
, "bc", end
-confkey
) == 0)
166 else if(strncmp(confkey
, "sl", end
-confkey
) == 0)
168 else if(strncmp(confkey
, "sr", end
-confkey
) == 0)
172 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name
, confkey
[0], confkey
[1]);
180 for(i
= 0;i
< chans
;i
++)
182 if(Speaker2Chan
[i
] == val
)
184 val
= strtol(sep
, NULL
, 10);
185 if(val
>= -180 && val
<= 180)
186 SpeakerAngle
[i
] = val
* M_PI
/180.0f
;
188 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey
[0], confkey
[1], val
);
194 for(i
= 1;i
< chans
;i
++)
196 if(SpeakerAngle
[i
] <= SpeakerAngle
[i
-1])
198 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i
, chans
,
199 SpeakerAngle
[i
-1] * 180.0f
/M_PI
, SpeakerAngle
[i
] * 180.0f
/M_PI
);
200 SpeakerAngle
[i
] = SpeakerAngle
[i
-1] + 1 * 180.0f
/M_PI
;
205 static __inline ALfloat
aluLUTpos2Angle(ALint pos
)
207 if(pos
< QUADRANT_NUM
)
208 return aluAtan((ALfloat
)pos
/ (ALfloat
)(QUADRANT_NUM
- pos
));
209 if(pos
< 2 * QUADRANT_NUM
)
210 return M_PI_2
+ aluAtan((ALfloat
)(pos
- QUADRANT_NUM
) / (ALfloat
)(2 * QUADRANT_NUM
- pos
));
211 if(pos
< 3 * QUADRANT_NUM
)
212 return aluAtan((ALfloat
)(pos
- 2 * QUADRANT_NUM
) / (ALfloat
)(3 * QUADRANT_NUM
- pos
)) - M_PI
;
213 return aluAtan((ALfloat
)(pos
- 3 * QUADRANT_NUM
) / (ALfloat
)(4 * QUADRANT_NUM
- pos
)) - M_PI_2
;
216 ALvoid
aluInitPanning(ALCcontext
*Context
)
218 ALint pos
, offset
, s
;
219 ALfloat Alpha
, Theta
;
220 ALfloat SpeakerAngle
[OUTPUTCHANNELS
];
221 ALint Speaker2Chan
[OUTPUTCHANNELS
];
223 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
226 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
227 Context
->ChannelMatrix
[s
][s2
] = ((s
==s2
) ? 1.0f
: 0.0f
);
230 switch(Context
->Device
->Format
)
232 /* Mono is rendered as stereo, then downmixed during post-process */
233 case AL_FORMAT_MONO8
:
234 case AL_FORMAT_MONO16
:
235 case AL_FORMAT_MONO_FLOAT32
:
236 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
237 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
238 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
239 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
240 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
241 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
242 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
243 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
244 Context
->NumChan
= 2;
245 Speaker2Chan
[0] = FRONT_LEFT
;
246 Speaker2Chan
[1] = FRONT_RIGHT
;
247 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
248 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
251 case AL_FORMAT_STEREO8
:
252 case AL_FORMAT_STEREO16
:
253 case AL_FORMAT_STEREO_FLOAT32
:
254 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
255 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
256 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
257 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
258 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
259 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
260 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
261 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
262 Context
->NumChan
= 2;
263 Speaker2Chan
[0] = FRONT_LEFT
;
264 Speaker2Chan
[1] = FRONT_RIGHT
;
265 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
266 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
267 SetSpeakerArrangement("layout_STEREO", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
270 case AL_FORMAT_QUAD8
:
271 case AL_FORMAT_QUAD16
:
272 case AL_FORMAT_QUAD32
:
273 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
274 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
275 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
276 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
277 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
278 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
279 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
280 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
281 Context
->NumChan
= 4;
282 Speaker2Chan
[0] = BACK_LEFT
;
283 Speaker2Chan
[1] = FRONT_LEFT
;
284 Speaker2Chan
[2] = FRONT_RIGHT
;
285 Speaker2Chan
[3] = BACK_RIGHT
;
286 SpeakerAngle
[0] = -135.0f
* M_PI
/180.0f
;
287 SpeakerAngle
[1] = -45.0f
* M_PI
/180.0f
;
288 SpeakerAngle
[2] = 45.0f
* M_PI
/180.0f
;
289 SpeakerAngle
[3] = 135.0f
* M_PI
/180.0f
;
290 SetSpeakerArrangement("layout_QUAD", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
293 case AL_FORMAT_51CHN8
:
294 case AL_FORMAT_51CHN16
:
295 case AL_FORMAT_51CHN32
:
296 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
297 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
298 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
299 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
300 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
301 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
302 Context
->NumChan
= 5;
303 Speaker2Chan
[0] = BACK_LEFT
;
304 Speaker2Chan
[1] = FRONT_LEFT
;
305 Speaker2Chan
[2] = FRONT_CENTER
;
306 Speaker2Chan
[3] = FRONT_RIGHT
;
307 Speaker2Chan
[4] = BACK_RIGHT
;
308 SpeakerAngle
[0] = -110.0f
* M_PI
/180.0f
;
309 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
310 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
311 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
312 SpeakerAngle
[4] = 110.0f
* M_PI
/180.0f
;
313 SetSpeakerArrangement("layout_51CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
316 case AL_FORMAT_61CHN8
:
317 case AL_FORMAT_61CHN16
:
318 case AL_FORMAT_61CHN32
:
319 Context
->ChannelMatrix
[BACK_LEFT
][BACK_CENTER
] = aluSqrt(0.5);
320 Context
->ChannelMatrix
[BACK_LEFT
][SIDE_LEFT
] = aluSqrt(0.5);
321 Context
->ChannelMatrix
[BACK_RIGHT
][BACK_CENTER
] = aluSqrt(0.5);
322 Context
->ChannelMatrix
[BACK_RIGHT
][SIDE_RIGHT
] = aluSqrt(0.5);
323 Context
->NumChan
= 6;
324 Speaker2Chan
[0] = SIDE_LEFT
;
325 Speaker2Chan
[1] = FRONT_LEFT
;
326 Speaker2Chan
[2] = FRONT_CENTER
;
327 Speaker2Chan
[3] = FRONT_RIGHT
;
328 Speaker2Chan
[4] = SIDE_RIGHT
;
329 Speaker2Chan
[5] = BACK_CENTER
;
330 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
331 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
332 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
333 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
334 SpeakerAngle
[4] = 90.0f
* M_PI
/180.0f
;
335 SpeakerAngle
[5] = 180.0f
* M_PI
/180.0f
;
336 SetSpeakerArrangement("layout_61CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
339 case AL_FORMAT_71CHN8
:
340 case AL_FORMAT_71CHN16
:
341 case AL_FORMAT_71CHN32
:
342 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
343 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
344 Context
->NumChan
= 7;
345 Speaker2Chan
[0] = BACK_LEFT
;
346 Speaker2Chan
[1] = SIDE_LEFT
;
347 Speaker2Chan
[2] = FRONT_LEFT
;
348 Speaker2Chan
[3] = FRONT_CENTER
;
349 Speaker2Chan
[4] = FRONT_RIGHT
;
350 Speaker2Chan
[5] = SIDE_RIGHT
;
351 Speaker2Chan
[6] = BACK_RIGHT
;
352 SpeakerAngle
[0] = -150.0f
* M_PI
/180.0f
;
353 SpeakerAngle
[1] = -90.0f
* M_PI
/180.0f
;
354 SpeakerAngle
[2] = -30.0f
* M_PI
/180.0f
;
355 SpeakerAngle
[3] = 0.0f
* M_PI
/180.0f
;
356 SpeakerAngle
[4] = 30.0f
* M_PI
/180.0f
;
357 SpeakerAngle
[5] = 90.0f
* M_PI
/180.0f
;
358 SpeakerAngle
[6] = 150.0f
* M_PI
/180.0f
;
359 SetSpeakerArrangement("layout_71CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
366 for(pos
= 0; pos
< LUT_NUM
; pos
++)
369 Theta
= aluLUTpos2Angle(pos
);
371 /* clear all values */
372 offset
= OUTPUTCHANNELS
* pos
;
373 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
374 Context
->PanningLUT
[offset
+s
] = 0.0f
;
376 /* set panning values */
377 for(s
= 0; s
< Context
->NumChan
- 1; s
++)
379 if(Theta
>= SpeakerAngle
[s
] && Theta
< SpeakerAngle
[s
+1])
381 /* source between speaker s and speaker s+1 */
382 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
383 (SpeakerAngle
[s
+1]-SpeakerAngle
[s
]);
384 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
385 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
+1]] = sin(Alpha
);
389 if(s
== Context
->NumChan
- 1)
391 /* source between last and first speaker */
392 if(Theta
< SpeakerAngle
[0])
393 Theta
+= 2.0f
* M_PI
;
394 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
395 (2.0f
* M_PI
+ SpeakerAngle
[0]-SpeakerAngle
[s
]);
396 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
397 Context
->PanningLUT
[offset
+ Speaker2Chan
[0]] = sin(Alpha
);
402 static ALvoid
CalcSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
,
405 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,DryMix
;
406 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
407 ALfloat Velocity
[3],ListenerVel
[3];
408 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
,OuterGainHF
;
409 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
410 ALfloat DopplerFactor
, DopplerVelocity
, flSpeedOfSound
;
411 ALfloat Matrix
[3][3];
412 ALfloat flAttenuation
;
413 ALfloat RoomAttenuation
[MAX_SENDS
];
414 ALfloat MetersPerUnit
;
415 ALfloat RoomRolloff
[MAX_SENDS
];
416 ALfloat DryGainHF
= 1.0f
;
417 ALfloat WetGain
[MAX_SENDS
];
418 ALfloat WetGainHF
[MAX_SENDS
];
419 ALfloat DirGain
, AmbientGain
;
421 const ALfloat
*SpeakerGain
;
427 //Get context properties
428 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
429 DopplerVelocity
= ALContext
->DopplerVelocity
;
430 flSpeedOfSound
= ALContext
->flSpeedOfSound
;
431 NumSends
= ALContext
->Device
->NumAuxSends
;
432 Frequency
= ALContext
->Device
->Frequency
;
434 //Get listener properties
435 ListenerGain
= ALContext
->Listener
.Gain
;
436 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
437 memcpy(ListenerVel
, ALContext
->Listener
.Velocity
, sizeof(ALContext
->Listener
.Velocity
));
439 //Get source properties
440 SourceVolume
= ALSource
->flGain
;
441 memcpy(Position
, ALSource
->vPosition
, sizeof(ALSource
->vPosition
));
442 memcpy(Direction
, ALSource
->vOrientation
, sizeof(ALSource
->vOrientation
));
443 memcpy(Velocity
, ALSource
->vVelocity
, sizeof(ALSource
->vVelocity
));
444 MinVolume
= ALSource
->flMinGain
;
445 MaxVolume
= ALSource
->flMaxGain
;
446 MinDist
= ALSource
->flRefDistance
;
447 MaxDist
= ALSource
->flMaxDistance
;
448 Rolloff
= ALSource
->flRollOffFactor
;
449 InnerAngle
= ALSource
->flInnerAngle
;
450 OuterAngle
= ALSource
->flOuterAngle
;
451 OuterGainHF
= ALSource
->OuterGainHF
;
453 //Only apply 3D calculations for mono buffers
454 if(isMono
== AL_FALSE
)
456 //1. Multi-channel buffers always play "normal"
457 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
459 DryMix
= SourceVolume
;
460 DryMix
= __min(DryMix
,MaxVolume
);
461 DryMix
= __max(DryMix
,MinVolume
);
463 switch(ALSource
->DirectFilter
.type
)
465 case AL_FILTER_LOWPASS
:
466 DryMix
*= ALSource
->DirectFilter
.Gain
;
467 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
471 ALSource
->Params
.DryGains
[FRONT_LEFT
] = DryMix
* ListenerGain
;
472 ALSource
->Params
.DryGains
[FRONT_RIGHT
] = DryMix
* ListenerGain
;
473 ALSource
->Params
.DryGains
[SIDE_LEFT
] = DryMix
* ListenerGain
;
474 ALSource
->Params
.DryGains
[SIDE_RIGHT
] = DryMix
* ListenerGain
;
475 ALSource
->Params
.DryGains
[BACK_LEFT
] = DryMix
* ListenerGain
;
476 ALSource
->Params
.DryGains
[BACK_RIGHT
] = DryMix
* ListenerGain
;
477 ALSource
->Params
.DryGains
[FRONT_CENTER
] = DryMix
* ListenerGain
;
478 ALSource
->Params
.DryGains
[BACK_CENTER
] = DryMix
* ListenerGain
;
479 ALSource
->Params
.DryGains
[LFE
] = DryMix
* ListenerGain
;
480 for(i
= 0;i
< MAX_SENDS
;i
++)
481 ALSource
->Params
.WetGains
[i
] = 0.0f
;
483 /* Update filter coefficients. Calculations based on the I3DL2
485 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
486 /* We use two chained one-pole filters, so we need to take the
487 * square root of the squared gain, which is the same as the base
489 g
= __max(DryGainHF
, 0.01f
);
491 /* Be careful with gains < 0.0001, as that causes the coefficient
492 * head towards 1, which will flatten the signal */
493 if(g
< 0.9999f
) /* 1-epsilon */
494 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
496 ALSource
->Params
.iirFilter
.coeff
= a
;
497 for(i
= 0;i
< MAX_SENDS
;i
++)
498 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= 0.0f
;
503 //1. Translate Listener to origin (convert to head relative)
504 if(ALSource
->bHeadRelative
==AL_FALSE
)
506 ALfloat U
[3],V
[3],N
[3];
508 // Build transform matrix
509 aluCrossproduct(ALContext
->Listener
.Forward
, ALContext
->Listener
.Up
, U
); // Right-vector
510 aluNormalize(U
); // Normalized Right-vector
511 memcpy(V
, ALContext
->Listener
.Up
, sizeof(V
)); // Up-vector
512 aluNormalize(V
); // Normalized Up-vector
513 memcpy(N
, ALContext
->Listener
.Forward
, sizeof(N
)); // At-vector
514 aluNormalize(N
); // Normalized At-vector
515 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0];
516 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1];
517 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2];
519 // Translate source position into listener space
520 Position
[0] -= ALContext
->Listener
.Position
[0];
521 Position
[1] -= ALContext
->Listener
.Position
[1];
522 Position
[2] -= ALContext
->Listener
.Position
[2];
523 // Transform source position and direction into listener space
524 aluMatrixVector(Position
, Matrix
);
525 aluMatrixVector(Direction
, Matrix
);
526 // Transform source and listener velocity into listener space
527 aluMatrixVector(Velocity
, Matrix
);
528 aluMatrixVector(ListenerVel
, Matrix
);
531 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
533 SourceToListener
[0] = -Position
[0];
534 SourceToListener
[1] = -Position
[1];
535 SourceToListener
[2] = -Position
[2];
536 aluNormalize(SourceToListener
);
537 aluNormalize(Direction
);
539 //2. Calculate distance attenuation
540 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
542 flAttenuation
= 1.0f
;
543 for(i
= 0;i
< MAX_SENDS
;i
++)
545 RoomAttenuation
[i
] = 1.0f
;
547 RoomRolloff
[i
] = ALSource
->RoomRolloffFactor
;
548 if(ALSource
->Send
[i
].Slot
&&
549 (ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
550 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
))
551 RoomRolloff
[i
] += ALSource
->Send
[i
].Slot
->effect
.Reverb
.RoomRolloffFactor
;
554 switch(ALSource
->DistanceModel
)
556 case AL_INVERSE_DISTANCE_CLAMPED
:
557 Distance
=__max(Distance
,MinDist
);
558 Distance
=__min(Distance
,MaxDist
);
559 if(MaxDist
< MinDist
)
562 case AL_INVERSE_DISTANCE
:
565 if((MinDist
+ (Rolloff
* (Distance
- MinDist
))) > 0.0f
)
566 flAttenuation
= MinDist
/ (MinDist
+ (Rolloff
* (Distance
- MinDist
)));
567 for(i
= 0;i
< NumSends
;i
++)
569 if((MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
))) > 0.0f
)
570 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
)));
575 case AL_LINEAR_DISTANCE_CLAMPED
:
576 Distance
=__max(Distance
,MinDist
);
577 Distance
=__min(Distance
,MaxDist
);
578 if(MaxDist
< MinDist
)
581 case AL_LINEAR_DISTANCE
:
582 Distance
=__min(Distance
,MaxDist
);
583 if(MaxDist
!= MinDist
)
585 flAttenuation
= 1.0f
- (Rolloff
*(Distance
-MinDist
)/(MaxDist
- MinDist
));
586 for(i
= 0;i
< NumSends
;i
++)
587 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(Distance
-MinDist
)/(MaxDist
- MinDist
));
591 case AL_EXPONENT_DISTANCE_CLAMPED
:
592 Distance
=__max(Distance
,MinDist
);
593 Distance
=__min(Distance
,MaxDist
);
594 if(MaxDist
< MinDist
)
597 case AL_EXPONENT_DISTANCE
:
598 if(Distance
> 0.0f
&& MinDist
> 0.0f
)
600 flAttenuation
= (ALfloat
)pow(Distance
/MinDist
, -Rolloff
);
601 for(i
= 0;i
< NumSends
;i
++)
602 RoomAttenuation
[i
] = (ALfloat
)pow(Distance
/MinDist
, -RoomRolloff
[i
]);
610 // Source Gain + Attenuation and clamp to Min/Max Gain
611 DryMix
= SourceVolume
* flAttenuation
;
612 DryMix
= __min(DryMix
,MaxVolume
);
613 DryMix
= __max(DryMix
,MinVolume
);
615 for(i
= 0;i
< NumSends
;i
++)
617 ALfloat WetMix
= SourceVolume
* RoomAttenuation
[i
];
618 WetMix
= __min(WetMix
,MaxVolume
);
619 WetGain
[i
] = __max(WetMix
,MinVolume
);
623 // Distance-based air absorption
624 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& ALSource
->DistanceModel
!= AL_NONE
)
626 ALfloat dist
= Distance
-MinDist
;
629 if(dist
< 0.0f
) dist
= 0.0f
;
630 // Absorption calculation is done in dB
631 absorb
= (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
) *
632 (dist
*MetersPerUnit
);
633 // Convert dB to linear gain before applying
634 absorb
= pow(10.0, absorb
/20.0);
636 for(i
= 0;i
< MAX_SENDS
;i
++)
637 WetGainHF
[i
] *= absorb
;
640 //3. Apply directional soundcones
641 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * 180.0f
/M_PI
;
642 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
644 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
645 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
)*scale
);
646 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
)*scale
);
647 DryMix
*= ConeVolume
;
648 if(ALSource
->DryGainHFAuto
)
651 else if(Angle
> OuterAngle
)
653 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
));
654 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
));
655 DryMix
*= ConeVolume
;
656 if(ALSource
->DryGainHFAuto
)
665 //4. Calculate Velocity
666 if(DopplerFactor
!= 0.0f
)
668 ALfloat flVSS
, flVLS
;
669 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
672 flVSS
= aluDotproduct(Velocity
, SourceToListener
);
673 if(flVSS
>= flMaxVelocity
)
674 flVSS
= (flMaxVelocity
- 1.0f
);
675 else if(flVSS
<= -flMaxVelocity
)
676 flVSS
= -flMaxVelocity
+ 1.0f
;
678 flVLS
= aluDotproduct(ListenerVel
, SourceToListener
);
679 if(flVLS
>= flMaxVelocity
)
680 flVLS
= (flMaxVelocity
- 1.0f
);
681 else if(flVLS
<= -flMaxVelocity
)
682 flVLS
= -flMaxVelocity
+ 1.0f
;
684 ALSource
->Params
.Pitch
= ALSource
->flPitch
*
685 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
686 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
689 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
691 for(i
= 0;i
< NumSends
;i
++)
693 if(ALSource
->Send
[i
].Slot
&&
694 ALSource
->Send
[i
].Slot
->effect
.type
!= AL_EFFECT_NULL
)
696 if(ALSource
->Send
[i
].Slot
->AuxSendAuto
)
698 if(ALSource
->WetGainAuto
)
699 WetGain
[i
] *= ConeVolume
;
700 if(ALSource
->WetGainHFAuto
)
701 WetGainHF
[i
] *= ConeHF
;
703 if(ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
704 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
)
706 /* Apply a decay-time transformation to the wet path,
707 * based on the attenuation of the dry path. This should
708 * better approximate the statistical attenuation model
709 * for the reverb effect.
711 * This simple equation converts the distance attenuation
712 * into the time it would take to reach -60 dB. From
713 * there it establishes an origin (0.333s; the decay time
714 * that will produce equal attenuation) and applies the
715 * current decay time. Finally, it converts the result
716 * back to an attenuation for the reverb path.
718 WetGain
[i
] *= pow(10.0f
, log10(flAttenuation
) * 0.333f
/
719 ALSource
->Send
[i
].Slot
->effect
.Reverb
.DecayTime
);
724 // If the slot's auxiliary send auto is off, the data sent to
725 // the effect slot is the same as the dry path, sans filter
728 WetGainHF
[i
] = DryGainHF
;
731 switch(ALSource
->Send
[i
].WetFilter
.type
)
733 case AL_FILTER_LOWPASS
:
734 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
735 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
738 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
742 ALSource
->Params
.WetGains
[i
] = 0.0f
;
746 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
748 ALSource
->Params
.WetGains
[i
] = 0.0f
;
752 //5. Apply filter gains and filters
753 switch(ALSource
->DirectFilter
.type
)
755 case AL_FILTER_LOWPASS
:
756 DryMix
*= ALSource
->DirectFilter
.Gain
;
757 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
760 DryMix
*= ListenerGain
;
762 // Use energy-preserving panning algorithm for multi-speaker playback
763 length
= aluSqrt(Position
[0]*Position
[0] + Position
[1]*Position
[1] +
764 Position
[2]*Position
[2]);
765 length
= __max(length
, MinDist
);
768 ALfloat invlen
= 1.0f
/length
;
769 Position
[0] *= invlen
;
770 Position
[1] *= invlen
;
771 Position
[2] *= invlen
;
774 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
775 SpeakerGain
= &ALContext
->PanningLUT
[OUTPUTCHANNELS
* pos
];
777 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
778 // elevation adjustment for directional gain. this sucks, but
779 // has low complexity
780 AmbientGain
= 1.0/aluSqrt(ALContext
->NumChan
) * (1.0-DirGain
);
781 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
783 ALfloat gain
= SpeakerGain
[s
]*DirGain
+ AmbientGain
;
784 ALSource
->Params
.DryGains
[s
] = DryMix
* gain
;
787 /* Update filter coefficients. */
788 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
789 /* Spatialized sources use four chained one-pole filters, so we need to
790 * take the fourth root of the squared gain, which is the same as the
791 * square root of the base gain. */
792 g
= aluSqrt(__max(DryGainHF
, 0.0001f
));
794 if(g
< 0.9999f
) /* 1-epsilon */
795 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
797 ALSource
->Params
.iirFilter
.coeff
= a
;
799 for(i
= 0;i
< NumSends
;i
++)
801 /* The wet path uses two chained one-pole filters, so take the
802 * base gain (square root of the squared gain) */
803 g
= __max(WetGainHF
[i
], 0.01f
);
805 if(g
< 0.9999f
) /* 1-epsilon */
806 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
808 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
812 static __inline ALshort
lerp(ALshort val1
, ALshort val2
, ALint frac
)
814 return val1
+ (((val2
-val1
)*frac
)>>FRACTIONBITS
);
817 static void MixSomeSources(ALCcontext
*ALContext
, float (*DryBuffer
)[OUTPUTCHANNELS
], ALuint SamplesToDo
)
819 static float DummyBuffer
[BUFFERSIZE
];
820 ALfloat
*WetBuffer
[MAX_SENDS
];
821 ALfloat (*Matrix
)[OUTPUTCHANNELS
] = ALContext
->ChannelMatrix
;
822 ALfloat DrySend
[OUTPUTCHANNELS
];
823 ALfloat dryGainStep
[OUTPUTCHANNELS
];
824 ALfloat wetGainStep
[MAX_SENDS
];
828 ALbufferlistitem
*BufferListItem
;
829 ALint64 DataSize64
,DataPos64
;
830 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
831 ALfloat WetSend
[MAX_SENDS
];
835 ALuint DataPosInt
, DataPosFrac
;
836 ALuint Channels
, Bytes
;
838 ALuint BuffersPlayed
;
842 if(!(ALSource
=ALContext
->Source
))
845 DeviceFreq
= ALContext
->Device
->Frequency
;
847 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
848 rampLength
= max(rampLength
, SamplesToDo
);
851 State
= ALSource
->state
;
852 if(State
!= AL_PLAYING
)
854 if((ALSource
=ALSource
->next
) != NULL
)
860 /* Find buffer format */
864 BufferListItem
= ALSource
->queue
;
865 while(BufferListItem
!= NULL
)
868 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
870 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
871 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
872 Frequency
= ALBuffer
->frequency
;
875 BufferListItem
= BufferListItem
->next
;
878 /* Get source info */
879 BuffersPlayed
= ALSource
->BuffersPlayed
;
880 DataPosInt
= ALSource
->position
;
881 DataPosFrac
= ALSource
->position_fraction
;
883 CalcSourceParams(ALContext
, ALSource
, (Channels
==1)?AL_TRUE
:AL_FALSE
);
885 /* Compute 18.14 fixed point step */
886 Pitch
= (ALSource
->Params
.Pitch
*Frequency
) / DeviceFreq
;
887 if(Pitch
> (float)MAX_PITCH
) Pitch
= (float)MAX_PITCH
;
888 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
889 if(increment
<= 0) increment
= (1<<FRACTIONBITS
);
891 /* Compute the gain steps for each output channel */
892 if(ALSource
->FirstStart
)
894 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
895 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
896 for(i
= 0;i
< MAX_SENDS
;i
++)
897 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
901 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
902 DrySend
[i
] = ALSource
->DryGains
[i
];
903 for(i
= 0;i
< MAX_SENDS
;i
++)
904 WetSend
[i
] = ALSource
->WetGains
[i
];
907 DryFilter
= &ALSource
->Params
.iirFilter
;
908 for(i
= 0;i
< MAX_SENDS
;i
++)
910 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
911 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
912 ALSource
->Send
[i
].Slot
->WetBuffer
:
916 if(DuplicateStereo
&& Channels
== 2)
918 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
919 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
920 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
921 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
923 else if(DuplicateStereo
)
925 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
926 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
927 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
928 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
931 /* Get current buffer queue item */
932 BufferListItem
= ALSource
->queue
;
933 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
934 BufferListItem
= BufferListItem
->next
;
936 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
943 /* Get buffer info */
944 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
946 Data
= ALBuffer
->data
;
947 DataSize
= ALBuffer
->size
;
948 DataSize
/= Channels
* Bytes
;
950 if(DataPosInt
>= DataSize
)
953 if(BufferListItem
->next
)
955 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
956 if(NextBuf
&& NextBuf
->data
)
958 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
959 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
960 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
963 else if(ALSource
->bLooping
)
965 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
966 if(NextBuf
&& NextBuf
->data
)
968 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
969 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
970 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
974 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
976 /* Compute the gain steps for each output channel */
977 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
978 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-
979 DrySend
[i
]) / rampLength
;
980 for(i
= 0;i
< MAX_SENDS
;i
++)
981 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-
982 WetSend
[i
]) / rampLength
;
984 /* Figure out how many samples we can mix. */
985 DataSize64
= DataSize
;
986 DataSize64
<<= FRACTIONBITS
;
987 DataPos64
= DataPosInt
;
988 DataPos64
<<= FRACTIONBITS
;
989 DataPos64
+= DataPosFrac
;
990 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
992 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
994 /* Actual sample mixing loop */
996 Data
+= DataPosInt
*Channels
;
998 if(Channels
== 1) /* Mono */
1004 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1005 DrySend
[i
] += dryGainStep
[i
];
1006 for(i
= 0;i
< MAX_SENDS
;i
++)
1007 WetSend
[i
] += wetGainStep
[i
];
1009 /* First order interpolator */
1010 value
= lerp(Data
[k
], Data
[k
+1], DataPosFrac
);
1012 /* Direct path final mix buffer and panning */
1013 outsamp
= lpFilter4P(DryFilter
, 0, value
);
1014 DryBuffer
[j
][FRONT_LEFT
] += outsamp
*DrySend
[FRONT_LEFT
];
1015 DryBuffer
[j
][FRONT_RIGHT
] += outsamp
*DrySend
[FRONT_RIGHT
];
1016 DryBuffer
[j
][SIDE_LEFT
] += outsamp
*DrySend
[SIDE_LEFT
];
1017 DryBuffer
[j
][SIDE_RIGHT
] += outsamp
*DrySend
[SIDE_RIGHT
];
1018 DryBuffer
[j
][BACK_LEFT
] += outsamp
*DrySend
[BACK_LEFT
];
1019 DryBuffer
[j
][BACK_RIGHT
] += outsamp
*DrySend
[BACK_RIGHT
];
1020 DryBuffer
[j
][FRONT_CENTER
] += outsamp
*DrySend
[FRONT_CENTER
];
1021 DryBuffer
[j
][BACK_CENTER
] += outsamp
*DrySend
[BACK_CENTER
];
1023 /* Room path final mix buffer and panning */
1024 for(i
= 0;i
< MAX_SENDS
;i
++)
1026 outsamp
= lpFilter2P(WetFilter
[i
], 0, value
);
1027 WetBuffer
[i
][j
] += outsamp
*WetSend
[i
];
1030 DataPosFrac
+= increment
;
1031 k
+= DataPosFrac
>>FRACTIONBITS
;
1032 DataPosFrac
&= FRACTIONMASK
;
1036 else if(Channels
== 2) /* Stereo */
1038 const int chans
[] = {
1039 FRONT_LEFT
, FRONT_RIGHT
1042 #define DO_MIX() do { \
1043 for(i = 0;i < MAX_SENDS;i++) \
1044 WetSend[i] += wetGainStep[i]*BufferSize; \
1045 while(BufferSize--) \
1047 for(i = 0;i < OUTPUTCHANNELS;i++) \
1048 DrySend[i] += dryGainStep[i]; \
1050 for(i = 0;i < Channels;i++) \
1052 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1053 value = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1054 for(out = 0;out < OUTPUTCHANNELS;out++) \
1055 DryBuffer[j][out] += value*Matrix[chans[i]][out]; \
1058 DataPosFrac += increment; \
1059 k += DataPosFrac>>FRACTIONBITS; \
1060 DataPosFrac &= FRACTIONMASK; \
1067 else if(Channels
== 4) /* Quad */
1069 const int chans
[] = {
1070 FRONT_LEFT
, FRONT_RIGHT
,
1071 BACK_LEFT
, BACK_RIGHT
1076 else if(Channels
== 6) /* 5.1 */
1078 const int chans
[] = {
1079 FRONT_LEFT
, FRONT_RIGHT
,
1081 BACK_LEFT
, BACK_RIGHT
1086 else if(Channels
== 7) /* 6.1 */
1088 const int chans
[] = {
1089 FRONT_LEFT
, FRONT_RIGHT
,
1092 SIDE_LEFT
, SIDE_RIGHT
1097 else if(Channels
== 8) /* 7.1 */
1099 const int chans
[] = {
1100 FRONT_LEFT
, FRONT_RIGHT
,
1102 BACK_LEFT
, BACK_RIGHT
,
1103 SIDE_LEFT
, SIDE_RIGHT
1111 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1112 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1113 for(i
= 0;i
< MAX_SENDS
;i
++)
1114 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1117 DataPosFrac
+= increment
;
1118 k
+= DataPosFrac
>>FRACTIONBITS
;
1119 DataPosFrac
&= FRACTIONMASK
;
1126 /* Handle looping sources */
1127 if(DataPosInt
>= DataSize
)
1129 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1131 BufferListItem
= BufferListItem
->next
;
1133 DataPosInt
-= DataSize
;
1137 if(!ALSource
->bLooping
)
1140 BufferListItem
= ALSource
->queue
;
1141 BuffersPlayed
= ALSource
->BuffersInQueue
;
1147 BufferListItem
= ALSource
->queue
;
1149 if(ALSource
->BuffersInQueue
== 1)
1150 DataPosInt
%= DataSize
;
1152 DataPosInt
-= DataSize
;
1158 /* Update source info */
1159 ALSource
->state
= State
;
1160 ALSource
->BuffersPlayed
= BuffersPlayed
;
1161 ALSource
->position
= DataPosInt
;
1162 ALSource
->position_fraction
= DataPosFrac
;
1163 ALSource
->Buffer
= BufferListItem
->buffer
;
1165 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1166 ALSource
->DryGains
[i
] = DrySend
[i
];
1167 for(i
= 0;i
< MAX_SENDS
;i
++)
1168 ALSource
->WetGains
[i
] = WetSend
[i
];
1170 ALSource
->FirstStart
= AL_FALSE
;
1172 if((ALSource
=ALSource
->next
) != NULL
)
1173 goto another_source
;
1176 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1178 float (*DryBuffer
)[OUTPUTCHANNELS
];
1180 ALeffectslot
*ALEffectSlot
;
1181 ALCcontext
*ALContext
;
1185 SuspendContext(NULL
);
1187 #if defined(HAVE_FESETROUND)
1188 fpuState
= fegetround();
1189 fesetround(FE_TOWARDZERO
);
1190 #elif defined(HAVE__CONTROLFP)
1191 fpuState
= _controlfp(0, 0);
1192 _controlfp(_RC_CHOP
, _MCW_RC
);
1197 DryBuffer
= device
->DryBuffer
;
1200 /* Setup variables */
1201 SamplesToDo
= min(size
, BUFFERSIZE
);
1203 /* Clear mixing buffer */
1204 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1206 for(c
= 0;c
< device
->NumContexts
;c
++)
1208 ALContext
= device
->Contexts
[c
];
1209 SuspendContext(ALContext
);
1211 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1213 /* effect slot processing */
1214 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1217 if(ALEffectSlot
->EffectState
)
1218 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1220 for(i
= 0;i
< SamplesToDo
;i
++)
1221 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1222 ALEffectSlot
= ALEffectSlot
->next
;
1224 ProcessContext(ALContext
);
1227 //Post processing loop
1228 switch(device
->Format
)
1230 #define CHECK_WRITE_FORMAT(bits, type, func, isWin) \
1231 case AL_FORMAT_MONO##bits: \
1232 for(i = 0;i < SamplesToDo;i++) \
1234 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT] + \
1235 DryBuffer[i][FRONT_RIGHT]); \
1236 buffer = ((type*)buffer) + 1; \
1239 case AL_FORMAT_STEREO##bits: \
1242 for(i = 0;i < SamplesToDo;i++) \
1245 samples[0] = DryBuffer[i][FRONT_LEFT]; \
1246 samples[1] = DryBuffer[i][FRONT_RIGHT]; \
1247 bs2b_cross_feed(device->Bs2b, samples); \
1248 ((type*)buffer)[0] = (func)(samples[0]); \
1249 ((type*)buffer)[1] = (func)(samples[1]); \
1250 buffer = ((type*)buffer) + 2; \
1255 for(i = 0;i < SamplesToDo;i++) \
1257 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1258 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1259 buffer = ((type*)buffer) + 2; \
1263 case AL_FORMAT_QUAD##bits: \
1264 for(i = 0;i < SamplesToDo;i++) \
1266 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1267 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1268 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1269 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1270 buffer = ((type*)buffer) + 4; \
1273 case AL_FORMAT_51CHN##bits: \
1274 for(i = 0;i < SamplesToDo;i++) \
1276 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1277 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1279 /* Of course, Windows can't use the same ordering... */ \
1280 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1281 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1282 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1283 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1285 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1286 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1287 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1288 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1290 buffer = ((type*)buffer) + 6; \
1293 case AL_FORMAT_61CHN##bits: \
1294 for(i = 0;i < SamplesToDo;i++) \
1296 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1297 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1298 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1299 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1300 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_CENTER]); \
1301 ((type*)buffer)[5] = (func)(DryBuffer[i][SIDE_LEFT]); \
1302 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1303 buffer = ((type*)buffer) + 7; \
1306 case AL_FORMAT_71CHN##bits: \
1307 for(i = 0;i < SamplesToDo;i++) \
1309 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1310 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1312 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1313 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1314 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1315 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1317 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1318 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1319 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1320 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1322 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_LEFT]); \
1323 ((type*)buffer)[7] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1324 buffer = ((type*)buffer) + 8; \
1328 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1329 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1331 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 1)
1332 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 1)
1333 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 1)
1335 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 0)
1336 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 0)
1337 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 0)
1339 #undef AL_FORMAT_STEREO32
1340 #undef AL_FORMAT_MONO32
1341 #undef CHECK_WRITE_FORMAT
1347 size
-= SamplesToDo
;
1350 #if defined(HAVE_FESETROUND)
1351 fesetround(fpuState
);
1352 #elif defined(HAVE__CONTROLFP)
1353 _controlfp(fpuState
, 0xfffff);
1356 ProcessContext(NULL
);
1359 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1363 SuspendContext(NULL
);
1364 for(i
= 0;i
< device
->NumContexts
;i
++)
1368 SuspendContext(device
->Contexts
[i
]);
1370 source
= device
->Contexts
[i
]->Source
;
1373 if(source
->state
== AL_PLAYING
)
1375 source
->state
= AL_STOPPED
;
1376 source
->BuffersPlayed
= source
->BuffersInQueue
;
1377 source
->position
= 0;
1378 source
->position_fraction
= 0;
1380 source
= source
->next
;
1382 ProcessContext(device
->Contexts
[i
]);
1385 device
->Connected
= ALC_FALSE
;
1386 ProcessContext(NULL
);