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
;
482 for(i
= 0;i
< NumSends
;i
++)
484 WetGain
[i
] = SourceVolume
;
485 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
486 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
489 switch(ALSource
->Send
[i
].WetFilter
.type
)
491 case AL_FILTER_LOWPASS
:
492 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
493 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
497 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
499 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
501 ALSource
->Params
.WetGains
[i
] = 0.0f
;
505 /* Update filter coefficients. Calculations based on the I3DL2
507 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
508 /* We use two chained one-pole filters, so we need to take the
509 * square root of the squared gain, which is the same as the base
511 g
= __max(DryGainHF
, 0.01f
);
513 /* Be careful with gains < 0.0001, as that causes the coefficient
514 * head towards 1, which will flatten the signal */
515 if(g
< 0.9999f
) /* 1-epsilon */
516 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
518 ALSource
->Params
.iirFilter
.coeff
= a
;
520 for(i
= 0;i
< NumSends
;i
++)
522 /* We use a one-pole filter, so we need to take the squared gain */
523 g
= __max(WetGainHF
[i
], 0.1f
);
526 if(g
< 0.9999f
) /* 1-epsilon */
527 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
529 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
535 //1. Translate Listener to origin (convert to head relative)
536 if(ALSource
->bHeadRelative
==AL_FALSE
)
538 ALfloat U
[3],V
[3],N
[3],P
[3];
540 // Build transform matrix
541 memcpy(N
, ALContext
->Listener
.Forward
, sizeof(N
)); // At-vector
542 aluNormalize(N
); // Normalized At-vector
543 memcpy(V
, ALContext
->Listener
.Up
, sizeof(V
)); // Up-vector
544 aluNormalize(V
); // Normalized Up-vector
545 aluCrossproduct(N
, V
, U
); // Right-vector
546 aluNormalize(U
); // Normalized Right-vector
547 P
[0] = -(ALContext
->Listener
.Position
[0]*U
[0] + // Translation
548 ALContext
->Listener
.Position
[1]*U
[1] +
549 ALContext
->Listener
.Position
[2]*U
[2]);
550 P
[1] = -(ALContext
->Listener
.Position
[0]*V
[0] +
551 ALContext
->Listener
.Position
[1]*V
[1] +
552 ALContext
->Listener
.Position
[2]*V
[2]);
553 P
[2] = -(ALContext
->Listener
.Position
[0]*-N
[0] +
554 ALContext
->Listener
.Position
[1]*-N
[1] +
555 ALContext
->Listener
.Position
[2]*-N
[2]);
556 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0]; Matrix
[0][3] = 0.0f
;
557 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1]; Matrix
[1][3] = 0.0f
;
558 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2]; Matrix
[2][3] = 0.0f
;
559 Matrix
[3][0] = P
[0]; Matrix
[3][1] = P
[1]; Matrix
[3][2] = P
[2]; Matrix
[3][3] = 1.0f
;
561 // Transform source position and direction into listener space
562 aluMatrixVector(Position
, 1.0f
, Matrix
);
563 aluMatrixVector(Direction
, 0.0f
, Matrix
);
564 // Transform source and listener velocity into listener space
565 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
566 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
569 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
571 SourceToListener
[0] = -Position
[0];
572 SourceToListener
[1] = -Position
[1];
573 SourceToListener
[2] = -Position
[2];
574 aluNormalize(SourceToListener
);
575 aluNormalize(Direction
);
577 //2. Calculate distance attenuation
578 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
580 flAttenuation
= 1.0f
;
581 for(i
= 0;i
< MAX_SENDS
;i
++)
583 RoomAttenuation
[i
] = 1.0f
;
585 RoomRolloff
[i
] = ALSource
->RoomRolloffFactor
;
586 if(ALSource
->Send
[i
].Slot
&&
587 (ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
588 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
))
589 RoomRolloff
[i
] += ALSource
->Send
[i
].Slot
->effect
.Reverb
.RoomRolloffFactor
;
592 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
593 ALContext
->DistanceModel
)
595 case AL_INVERSE_DISTANCE_CLAMPED
:
596 Distance
=__max(Distance
,MinDist
);
597 Distance
=__min(Distance
,MaxDist
);
598 if(MaxDist
< MinDist
)
601 case AL_INVERSE_DISTANCE
:
604 if((MinDist
+ (Rolloff
* (Distance
- MinDist
))) > 0.0f
)
605 flAttenuation
= MinDist
/ (MinDist
+ (Rolloff
* (Distance
- MinDist
)));
606 for(i
= 0;i
< NumSends
;i
++)
608 if((MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
))) > 0.0f
)
609 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
)));
614 case AL_LINEAR_DISTANCE_CLAMPED
:
615 Distance
=__max(Distance
,MinDist
);
616 Distance
=__min(Distance
,MaxDist
);
617 if(MaxDist
< MinDist
)
620 case AL_LINEAR_DISTANCE
:
621 Distance
=__min(Distance
,MaxDist
);
622 if(MaxDist
!= MinDist
)
624 flAttenuation
= 1.0f
- (Rolloff
*(Distance
-MinDist
)/(MaxDist
- MinDist
));
625 for(i
= 0;i
< NumSends
;i
++)
626 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(Distance
-MinDist
)/(MaxDist
- MinDist
));
630 case AL_EXPONENT_DISTANCE_CLAMPED
:
631 Distance
=__max(Distance
,MinDist
);
632 Distance
=__min(Distance
,MaxDist
);
633 if(MaxDist
< MinDist
)
636 case AL_EXPONENT_DISTANCE
:
637 if(Distance
> 0.0f
&& MinDist
> 0.0f
)
639 flAttenuation
= (ALfloat
)pow(Distance
/MinDist
, -Rolloff
);
640 for(i
= 0;i
< NumSends
;i
++)
641 RoomAttenuation
[i
] = (ALfloat
)pow(Distance
/MinDist
, -RoomRolloff
[i
]);
649 // Source Gain + Attenuation and clamp to Min/Max Gain
650 DryMix
= SourceVolume
* flAttenuation
;
651 DryMix
= __min(DryMix
,MaxVolume
);
652 DryMix
= __max(DryMix
,MinVolume
);
654 for(i
= 0;i
< NumSends
;i
++)
656 ALfloat WetMix
= SourceVolume
* RoomAttenuation
[i
];
657 WetMix
= __min(WetMix
,MaxVolume
);
658 WetGain
[i
] = __max(WetMix
,MinVolume
);
662 // Distance-based air absorption
663 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& flAttenuation
< 1.0f
)
665 ALfloat absorb
= 0.0f
;
667 // Absorption calculation is done in dB
668 if(flAttenuation
> 0.0f
)
670 absorb
= (MinDist
/flAttenuation
- MinDist
)*MetersPerUnit
*
671 (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
);
672 // Convert dB to linear gain before applying
673 absorb
= pow(10.0, absorb
/20.0);
676 for(i
= 0;i
< MAX_SENDS
;i
++)
677 WetGainHF
[i
] *= absorb
;
680 //3. Apply directional soundcones
681 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * 180.0f
/M_PI
;
682 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
684 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
685 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
)*scale
);
686 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
)*scale
);
687 DryMix
*= ConeVolume
;
688 if(ALSource
->DryGainHFAuto
)
691 else if(Angle
> OuterAngle
)
693 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
));
694 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
));
695 DryMix
*= ConeVolume
;
696 if(ALSource
->DryGainHFAuto
)
705 //4. Calculate Velocity
706 if(DopplerFactor
!= 0.0f
)
708 ALfloat flVSS
, flVLS
;
709 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
712 flVSS
= aluDotproduct(Velocity
, SourceToListener
);
713 if(flVSS
>= flMaxVelocity
)
714 flVSS
= (flMaxVelocity
- 1.0f
);
715 else if(flVSS
<= -flMaxVelocity
)
716 flVSS
= -flMaxVelocity
+ 1.0f
;
718 flVLS
= aluDotproduct(ListenerVel
, SourceToListener
);
719 if(flVLS
>= flMaxVelocity
)
720 flVLS
= (flMaxVelocity
- 1.0f
);
721 else if(flVLS
<= -flMaxVelocity
)
722 flVLS
= -flMaxVelocity
+ 1.0f
;
724 ALSource
->Params
.Pitch
= ALSource
->flPitch
*
725 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
726 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
729 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
731 for(i
= 0;i
< NumSends
;i
++)
733 if(ALSource
->Send
[i
].Slot
&&
734 ALSource
->Send
[i
].Slot
->effect
.type
!= AL_EFFECT_NULL
)
736 if(ALSource
->Send
[i
].Slot
->AuxSendAuto
)
738 if(ALSource
->WetGainAuto
)
739 WetGain
[i
] *= ConeVolume
;
740 if(ALSource
->WetGainHFAuto
)
741 WetGainHF
[i
] *= ConeHF
;
743 if(ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
744 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
)
746 /* Apply a decay-time transformation to the wet path,
747 * based on the attenuation of the dry path. This should
748 * better approximate the statistical attenuation model
749 * for the reverb effect.
751 * This simple equation converts the distance attenuation
752 * into the time it would take to reach -60 dB. From
753 * there it establishes an origin (0.333s; the decay time
754 * that will produce equal attenuation) and applies the
755 * current decay time. Finally, it converts the result
756 * back to an attenuation for the reverb path.
758 WetGain
[i
] *= pow(10.0f
, log10(flAttenuation
) * 0.333f
/
759 ALSource
->Send
[i
].Slot
->effect
.Reverb
.DecayTime
);
764 // If the slot's auxiliary send auto is off, the data sent to
765 // the effect slot is the same as the dry path, sans filter
768 WetGainHF
[i
] = DryGainHF
;
771 switch(ALSource
->Send
[i
].WetFilter
.type
)
773 case AL_FILTER_LOWPASS
:
774 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
775 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
778 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
782 ALSource
->Params
.WetGains
[i
] = 0.0f
;
786 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
788 ALSource
->Params
.WetGains
[i
] = 0.0f
;
792 //5. Apply filter gains and filters
793 switch(ALSource
->DirectFilter
.type
)
795 case AL_FILTER_LOWPASS
:
796 DryMix
*= ALSource
->DirectFilter
.Gain
;
797 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
800 DryMix
*= ListenerGain
;
802 // Use energy-preserving panning algorithm for multi-speaker playback
803 length
= aluSqrt(Position
[0]*Position
[0] + Position
[1]*Position
[1] +
804 Position
[2]*Position
[2]);
805 length
= __max(length
, MinDist
);
808 ALfloat invlen
= 1.0f
/length
;
809 Position
[0] *= invlen
;
810 Position
[1] *= invlen
;
811 Position
[2] *= invlen
;
814 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
815 SpeakerGain
= &ALContext
->PanningLUT
[OUTPUTCHANNELS
* pos
];
817 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
818 // elevation adjustment for directional gain. this sucks, but
819 // has low complexity
820 AmbientGain
= 1.0/aluSqrt(ALContext
->NumChan
) * (1.0-DirGain
);
821 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
823 ALfloat gain
= SpeakerGain
[s
]*DirGain
+ AmbientGain
;
824 ALSource
->Params
.DryGains
[s
] = DryMix
* gain
;
827 /* Update filter coefficients. */
828 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
829 /* Spatialized sources use four chained one-pole filters, so we need to
830 * take the fourth root of the squared gain, which is the same as the
831 * square root of the base gain. */
832 g
= aluSqrt(__max(DryGainHF
, 0.0001f
));
834 if(g
< 0.9999f
) /* 1-epsilon */
835 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
837 ALSource
->Params
.iirFilter
.coeff
= a
;
839 for(i
= 0;i
< NumSends
;i
++)
841 /* The wet path uses two chained one-pole filters, so take the
842 * base gain (square root of the squared gain) */
843 g
= __max(WetGainHF
[i
], 0.01f
);
845 if(g
< 0.9999f
) /* 1-epsilon */
846 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
848 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
852 static __inline ALshort
lerp(ALshort val1
, ALshort val2
, ALint frac
)
854 return val1
+ (((val2
-val1
)*frac
)>>FRACTIONBITS
);
857 static void MixSomeSources(ALCcontext
*ALContext
, float (*DryBuffer
)[OUTPUTCHANNELS
], ALuint SamplesToDo
)
859 static float DummyBuffer
[BUFFERSIZE
];
860 ALfloat
*WetBuffer
[MAX_SENDS
];
861 ALfloat (*Matrix
)[OUTPUTCHANNELS
] = ALContext
->ChannelMatrix
;
862 ALfloat DrySend
[OUTPUTCHANNELS
];
863 ALfloat dryGainStep
[OUTPUTCHANNELS
];
864 ALfloat wetGainStep
[MAX_SENDS
];
867 ALfloat value
, outsamp
;
868 ALbufferlistitem
*BufferListItem
;
869 ALint64 DataSize64
,DataPos64
;
870 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
871 ALfloat WetSend
[MAX_SENDS
];
875 ALuint DataPosInt
, DataPosFrac
;
876 ALuint Channels
, Bytes
;
878 ALuint BuffersPlayed
;
882 if(!(ALSource
=ALContext
->Source
))
885 DeviceFreq
= ALContext
->Device
->Frequency
;
887 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
888 rampLength
= max(rampLength
, SamplesToDo
);
891 State
= ALSource
->state
;
892 if(State
!= AL_PLAYING
)
894 if((ALSource
=ALSource
->next
) != NULL
)
900 /* Find buffer format */
904 BufferListItem
= ALSource
->queue
;
905 while(BufferListItem
!= NULL
)
908 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
910 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
911 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
912 Frequency
= ALBuffer
->frequency
;
915 BufferListItem
= BufferListItem
->next
;
918 /* Get source info */
919 BuffersPlayed
= ALSource
->BuffersPlayed
;
920 DataPosInt
= ALSource
->position
;
921 DataPosFrac
= ALSource
->position_fraction
;
923 if(ALSource
->NeedsUpdate
)
925 CalcSourceParams(ALContext
, ALSource
, (Channels
==1)?AL_TRUE
:AL_FALSE
);
926 ALSource
->NeedsUpdate
= AL_FALSE
;
929 /* Compute 18.14 fixed point step */
930 Pitch
= (ALSource
->Params
.Pitch
*Frequency
) / DeviceFreq
;
931 if(Pitch
> (float)MAX_PITCH
) Pitch
= (float)MAX_PITCH
;
932 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
933 if(increment
<= 0) increment
= (1<<FRACTIONBITS
);
935 /* Compute the gain steps for each output channel */
936 if(ALSource
->FirstStart
)
938 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
939 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
940 for(i
= 0;i
< MAX_SENDS
;i
++)
941 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
945 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
946 DrySend
[i
] = ALSource
->DryGains
[i
];
947 for(i
= 0;i
< MAX_SENDS
;i
++)
948 WetSend
[i
] = ALSource
->WetGains
[i
];
951 DryFilter
= &ALSource
->Params
.iirFilter
;
952 for(i
= 0;i
< MAX_SENDS
;i
++)
954 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
955 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
956 ALSource
->Send
[i
].Slot
->WetBuffer
:
960 if(DuplicateStereo
&& Channels
== 2)
962 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
963 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
964 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
965 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
967 else if(DuplicateStereo
)
969 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
970 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
971 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
972 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
975 /* Get current buffer queue item */
976 BufferListItem
= ALSource
->queue
;
977 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
978 BufferListItem
= BufferListItem
->next
;
980 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
987 /* Get buffer info */
988 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
990 Data
= ALBuffer
->data
;
991 DataSize
= ALBuffer
->size
;
992 DataSize
/= Channels
* Bytes
;
994 if(DataPosInt
>= DataSize
)
997 if(BufferListItem
->next
)
999 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
1000 if(NextBuf
&& NextBuf
->data
)
1002 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1003 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1004 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1007 else if(ALSource
->bLooping
)
1009 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
1010 if(NextBuf
&& NextBuf
->data
)
1012 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1013 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1014 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1018 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
1020 /* Compute the gain steps for each output channel */
1021 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1022 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-
1023 DrySend
[i
]) / rampLength
;
1024 for(i
= 0;i
< MAX_SENDS
;i
++)
1025 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-
1026 WetSend
[i
]) / rampLength
;
1028 /* Figure out how many samples we can mix. */
1029 DataSize64
= DataSize
;
1030 DataSize64
<<= FRACTIONBITS
;
1031 DataPos64
= DataPosInt
;
1032 DataPos64
<<= FRACTIONBITS
;
1033 DataPos64
+= DataPosFrac
;
1034 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
1036 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
1038 /* Actual sample mixing loop */
1040 Data
+= DataPosInt
*Channels
;
1042 if(Channels
== 1) /* Mono */
1046 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1047 DrySend
[i
] += dryGainStep
[i
];
1048 for(i
= 0;i
< MAX_SENDS
;i
++)
1049 WetSend
[i
] += wetGainStep
[i
];
1051 /* First order interpolator */
1052 value
= lerp(Data
[k
], Data
[k
+1], DataPosFrac
);
1054 /* Direct path final mix buffer and panning */
1055 outsamp
= lpFilter4P(DryFilter
, 0, value
);
1056 DryBuffer
[j
][FRONT_LEFT
] += outsamp
*DrySend
[FRONT_LEFT
];
1057 DryBuffer
[j
][FRONT_RIGHT
] += outsamp
*DrySend
[FRONT_RIGHT
];
1058 DryBuffer
[j
][SIDE_LEFT
] += outsamp
*DrySend
[SIDE_LEFT
];
1059 DryBuffer
[j
][SIDE_RIGHT
] += outsamp
*DrySend
[SIDE_RIGHT
];
1060 DryBuffer
[j
][BACK_LEFT
] += outsamp
*DrySend
[BACK_LEFT
];
1061 DryBuffer
[j
][BACK_RIGHT
] += outsamp
*DrySend
[BACK_RIGHT
];
1062 DryBuffer
[j
][FRONT_CENTER
] += outsamp
*DrySend
[FRONT_CENTER
];
1063 DryBuffer
[j
][BACK_CENTER
] += outsamp
*DrySend
[BACK_CENTER
];
1065 /* Room path final mix buffer and panning */
1066 for(i
= 0;i
< MAX_SENDS
;i
++)
1068 outsamp
= lpFilter2P(WetFilter
[i
], 0, value
);
1069 WetBuffer
[i
][j
] += outsamp
*WetSend
[i
];
1072 DataPosFrac
+= increment
;
1073 k
+= DataPosFrac
>>FRACTIONBITS
;
1074 DataPosFrac
&= FRACTIONMASK
;
1078 else if(Channels
== 2) /* Stereo */
1080 const int chans
[] = {
1081 FRONT_LEFT
, FRONT_RIGHT
1083 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1085 #define DO_MIX() do { \
1086 while(BufferSize--) \
1088 for(i = 0;i < OUTPUTCHANNELS;i++) \
1089 DrySend[i] += dryGainStep[i]; \
1090 for(i = 0;i < MAX_SENDS;i++) \
1091 WetSend[i] += wetGainStep[i]; \
1093 for(i = 0;i < Channels;i++) \
1095 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1096 outsamp = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1097 for(out = 0;out < OUTPUTCHANNELS;out++) \
1098 DryBuffer[j][out] += outsamp*Matrix[chans[i]][out]; \
1099 for(out = 0;out < MAX_SENDS;out++) \
1101 outsamp = lpFilter1P(WetFilter[out], chans[out], value); \
1102 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1106 DataPosFrac += increment; \
1107 k += DataPosFrac>>FRACTIONBITS; \
1108 DataPosFrac &= FRACTIONMASK; \
1115 else if(Channels
== 4) /* Quad */
1117 const int chans
[] = {
1118 FRONT_LEFT
, FRONT_RIGHT
,
1119 BACK_LEFT
, BACK_RIGHT
1121 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1125 else if(Channels
== 6) /* 5.1 */
1127 const int chans
[] = {
1128 FRONT_LEFT
, FRONT_RIGHT
,
1130 BACK_LEFT
, BACK_RIGHT
1132 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1136 else if(Channels
== 7) /* 6.1 */
1138 const int chans
[] = {
1139 FRONT_LEFT
, FRONT_RIGHT
,
1142 SIDE_LEFT
, SIDE_RIGHT
1144 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1148 else if(Channels
== 8) /* 7.1 */
1150 const int chans
[] = {
1151 FRONT_LEFT
, FRONT_RIGHT
,
1153 BACK_LEFT
, BACK_RIGHT
,
1154 SIDE_LEFT
, SIDE_RIGHT
1156 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1163 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1164 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1165 for(i
= 0;i
< MAX_SENDS
;i
++)
1166 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1169 DataPosFrac
+= increment
;
1170 k
+= DataPosFrac
>>FRACTIONBITS
;
1171 DataPosFrac
&= FRACTIONMASK
;
1178 /* Handle looping sources */
1179 if(DataPosInt
>= DataSize
)
1181 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1183 BufferListItem
= BufferListItem
->next
;
1185 DataPosInt
-= DataSize
;
1189 if(!ALSource
->bLooping
)
1192 BufferListItem
= ALSource
->queue
;
1193 BuffersPlayed
= ALSource
->BuffersInQueue
;
1199 BufferListItem
= ALSource
->queue
;
1201 if(ALSource
->BuffersInQueue
== 1)
1202 DataPosInt
%= DataSize
;
1204 DataPosInt
-= DataSize
;
1210 /* Update source info */
1211 ALSource
->state
= State
;
1212 ALSource
->BuffersPlayed
= BuffersPlayed
;
1213 ALSource
->position
= DataPosInt
;
1214 ALSource
->position_fraction
= DataPosFrac
;
1215 ALSource
->Buffer
= BufferListItem
->buffer
;
1217 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1218 ALSource
->DryGains
[i
] = DrySend
[i
];
1219 for(i
= 0;i
< MAX_SENDS
;i
++)
1220 ALSource
->WetGains
[i
] = WetSend
[i
];
1222 ALSource
->FirstStart
= AL_FALSE
;
1224 if((ALSource
=ALSource
->next
) != NULL
)
1225 goto another_source
;
1228 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1230 float (*DryBuffer
)[OUTPUTCHANNELS
];
1232 ALeffectslot
*ALEffectSlot
;
1233 ALCcontext
*ALContext
;
1237 SuspendContext(NULL
);
1239 #if defined(HAVE_FESETROUND)
1240 fpuState
= fegetround();
1241 fesetround(FE_TOWARDZERO
);
1242 #elif defined(HAVE__CONTROLFP)
1243 fpuState
= _controlfp(0, 0);
1244 _controlfp(_RC_CHOP
, _MCW_RC
);
1249 DryBuffer
= device
->DryBuffer
;
1252 /* Setup variables */
1253 SamplesToDo
= min(size
, BUFFERSIZE
);
1255 /* Clear mixing buffer */
1256 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1258 for(c
= 0;c
< device
->NumContexts
;c
++)
1260 ALContext
= device
->Contexts
[c
];
1261 SuspendContext(ALContext
);
1263 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1265 /* effect slot processing */
1266 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1269 if(ALEffectSlot
->EffectState
)
1270 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1272 for(i
= 0;i
< SamplesToDo
;i
++)
1273 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1274 ALEffectSlot
= ALEffectSlot
->next
;
1276 ProcessContext(ALContext
);
1279 //Post processing loop
1280 switch(device
->Format
)
1282 #define CHECK_WRITE_FORMAT(bits, type, func, isWin) \
1283 case AL_FORMAT_MONO##bits: \
1284 for(i = 0;i < SamplesToDo;i++) \
1286 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT] + \
1287 DryBuffer[i][FRONT_RIGHT]); \
1288 buffer = ((type*)buffer) + 1; \
1291 case AL_FORMAT_STEREO##bits: \
1294 for(i = 0;i < SamplesToDo;i++) \
1297 samples[0] = DryBuffer[i][FRONT_LEFT]; \
1298 samples[1] = DryBuffer[i][FRONT_RIGHT]; \
1299 bs2b_cross_feed(device->Bs2b, samples); \
1300 ((type*)buffer)[0] = (func)(samples[0]); \
1301 ((type*)buffer)[1] = (func)(samples[1]); \
1302 buffer = ((type*)buffer) + 2; \
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]); \
1311 buffer = ((type*)buffer) + 2; \
1315 case AL_FORMAT_QUAD##bits: \
1316 for(i = 0;i < SamplesToDo;i++) \
1318 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1319 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1320 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1321 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1322 buffer = ((type*)buffer) + 4; \
1325 case AL_FORMAT_51CHN##bits: \
1326 for(i = 0;i < SamplesToDo;i++) \
1328 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1329 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1331 /* Of course, Windows can't use the same ordering... */ \
1332 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1333 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1334 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1335 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1337 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1338 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1339 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1340 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1342 buffer = ((type*)buffer) + 6; \
1345 case AL_FORMAT_61CHN##bits: \
1346 for(i = 0;i < SamplesToDo;i++) \
1348 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1349 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1350 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1351 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1352 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_CENTER]); \
1353 ((type*)buffer)[5] = (func)(DryBuffer[i][SIDE_LEFT]); \
1354 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1355 buffer = ((type*)buffer) + 7; \
1358 case AL_FORMAT_71CHN##bits: \
1359 for(i = 0;i < SamplesToDo;i++) \
1361 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1362 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1364 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1365 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1366 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1367 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1369 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1370 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1371 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1372 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1374 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_LEFT]); \
1375 ((type*)buffer)[7] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1376 buffer = ((type*)buffer) + 8; \
1380 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1381 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1383 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 1)
1384 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 1)
1385 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 1)
1387 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 0)
1388 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 0)
1389 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 0)
1391 #undef AL_FORMAT_STEREO32
1392 #undef AL_FORMAT_MONO32
1393 #undef CHECK_WRITE_FORMAT
1399 size
-= SamplesToDo
;
1402 #if defined(HAVE_FESETROUND)
1403 fesetround(fpuState
);
1404 #elif defined(HAVE__CONTROLFP)
1405 _controlfp(fpuState
, 0xfffff);
1408 ProcessContext(NULL
);
1411 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1415 SuspendContext(NULL
);
1416 for(i
= 0;i
< device
->NumContexts
;i
++)
1420 SuspendContext(device
->Contexts
[i
]);
1422 source
= device
->Contexts
[i
]->Source
;
1425 if(source
->state
== AL_PLAYING
)
1427 source
->state
= AL_STOPPED
;
1428 source
->BuffersPlayed
= source
->BuffersInQueue
;
1429 source
->position
= 0;
1430 source
->position_fraction
= 0;
1432 source
= source
->next
;
1434 ProcessContext(device
->Contexts
[i
]);
1437 device
->Connected
= ALC_FALSE
;
1438 ProcessContext(NULL
);