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
)
67 static __inline ALshort
aluF2S(ALfloat Value
)
73 i
= (ALint
)(Value
*32768.0f
);
78 i
= (ALint
)(Value
*32767.0f
);
84 static __inline ALubyte
aluF2UB(ALfloat Value
)
86 ALshort i
= aluF2S(Value
);
91 static __inline ALvoid
aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
93 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
94 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
95 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
98 static __inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
100 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
101 inVector1
[2]*inVector2
[2];
104 static __inline ALvoid
aluNormalize(ALfloat
*inVector
)
106 ALfloat length
, inverse_length
;
108 length
= aluSqrt(aluDotproduct(inVector
, inVector
));
111 inverse_length
= 1.0f
/length
;
112 inVector
[0] *= inverse_length
;
113 inVector
[1] *= inverse_length
;
114 inVector
[2] *= inverse_length
;
118 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
121 vector
[0], vector
[1], vector
[2], w
124 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
125 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
126 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
129 static ALvoid
SetSpeakerArrangement(const char *name
, ALfloat SpeakerAngle
[OUTPUTCHANNELS
],
130 ALint Speaker2Chan
[OUTPUTCHANNELS
], ALint chans
)
138 confkey
= GetConfigValue(NULL
, name
, "");
143 next
= strchr(confkey
, ',');
148 } while(isspace(*next
));
151 sep
= strchr(confkey
, '=');
152 if(!sep
|| confkey
== sep
)
156 while(isspace(*end
) && end
!= confkey
)
160 if(strncmp(confkey
, "fl", end
-confkey
) == 0)
162 else if(strncmp(confkey
, "fr", end
-confkey
) == 0)
164 else if(strncmp(confkey
, "fc", end
-confkey
) == 0)
166 else if(strncmp(confkey
, "bl", end
-confkey
) == 0)
168 else if(strncmp(confkey
, "br", end
-confkey
) == 0)
170 else if(strncmp(confkey
, "bc", end
-confkey
) == 0)
172 else if(strncmp(confkey
, "sl", end
-confkey
) == 0)
174 else if(strncmp(confkey
, "sr", end
-confkey
) == 0)
178 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name
, confkey
[0], confkey
[1]);
186 for(i
= 0;i
< chans
;i
++)
188 if(Speaker2Chan
[i
] == val
)
190 val
= strtol(sep
, NULL
, 10);
191 if(val
>= -180 && val
<= 180)
192 SpeakerAngle
[i
] = val
* M_PI
/180.0f
;
194 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey
[0], confkey
[1], val
);
200 for(i
= 1;i
< chans
;i
++)
202 if(SpeakerAngle
[i
] <= SpeakerAngle
[i
-1])
204 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i
, chans
,
205 SpeakerAngle
[i
-1] * 180.0f
/M_PI
, SpeakerAngle
[i
] * 180.0f
/M_PI
);
206 SpeakerAngle
[i
] = SpeakerAngle
[i
-1] + 1 * 180.0f
/M_PI
;
211 static __inline ALfloat
aluLUTpos2Angle(ALint pos
)
213 if(pos
< QUADRANT_NUM
)
214 return aluAtan((ALfloat
)pos
/ (ALfloat
)(QUADRANT_NUM
- pos
));
215 if(pos
< 2 * QUADRANT_NUM
)
216 return M_PI_2
+ aluAtan((ALfloat
)(pos
- QUADRANT_NUM
) / (ALfloat
)(2 * QUADRANT_NUM
- pos
));
217 if(pos
< 3 * QUADRANT_NUM
)
218 return aluAtan((ALfloat
)(pos
- 2 * QUADRANT_NUM
) / (ALfloat
)(3 * QUADRANT_NUM
- pos
)) - M_PI
;
219 return aluAtan((ALfloat
)(pos
- 3 * QUADRANT_NUM
) / (ALfloat
)(4 * QUADRANT_NUM
- pos
)) - M_PI_2
;
222 ALvoid
aluInitPanning(ALCcontext
*Context
)
224 ALint pos
, offset
, s
;
225 ALfloat Alpha
, Theta
;
226 ALfloat SpeakerAngle
[OUTPUTCHANNELS
];
227 ALint Speaker2Chan
[OUTPUTCHANNELS
];
229 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
232 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
233 Context
->ChannelMatrix
[s
][s2
] = ((s
==s2
) ? 1.0f
: 0.0f
);
236 switch(Context
->Device
->Format
)
238 case AL_FORMAT_MONO8
:
239 case AL_FORMAT_MONO16
:
240 case AL_FORMAT_MONO_FLOAT32
:
241 Context
->ChannelMatrix
[FRONT_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
242 Context
->ChannelMatrix
[FRONT_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
243 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
244 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
245 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
246 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
247 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_CENTER
] = 1.0f
;
248 Context
->NumChan
= 1;
249 Speaker2Chan
[0] = FRONT_CENTER
;
250 SpeakerAngle
[0] = 0.0f
* M_PI
/180.0f
;
253 case AL_FORMAT_STEREO8
:
254 case AL_FORMAT_STEREO16
:
255 case AL_FORMAT_STEREO_FLOAT32
:
256 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
257 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
258 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
259 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
260 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
261 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
262 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
263 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
264 Context
->NumChan
= 2;
265 Speaker2Chan
[0] = FRONT_LEFT
;
266 Speaker2Chan
[1] = FRONT_RIGHT
;
267 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
268 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
269 SetSpeakerArrangement("layout_STEREO", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
272 case AL_FORMAT_QUAD8
:
273 case AL_FORMAT_QUAD16
:
274 case AL_FORMAT_QUAD32
:
275 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
276 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
277 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
278 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
279 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
280 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
281 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
282 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
283 Context
->NumChan
= 4;
284 Speaker2Chan
[0] = BACK_LEFT
;
285 Speaker2Chan
[1] = FRONT_LEFT
;
286 Speaker2Chan
[2] = FRONT_RIGHT
;
287 Speaker2Chan
[3] = BACK_RIGHT
;
288 SpeakerAngle
[0] = -135.0f
* M_PI
/180.0f
;
289 SpeakerAngle
[1] = -45.0f
* M_PI
/180.0f
;
290 SpeakerAngle
[2] = 45.0f
* M_PI
/180.0f
;
291 SpeakerAngle
[3] = 135.0f
* M_PI
/180.0f
;
292 SetSpeakerArrangement("layout_QUAD", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
295 case AL_FORMAT_51CHN8
:
296 case AL_FORMAT_51CHN16
:
297 case AL_FORMAT_51CHN32
:
298 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
299 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
300 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
301 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
302 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
303 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
304 Context
->NumChan
= 5;
305 Speaker2Chan
[0] = BACK_LEFT
;
306 Speaker2Chan
[1] = FRONT_LEFT
;
307 Speaker2Chan
[2] = FRONT_CENTER
;
308 Speaker2Chan
[3] = FRONT_RIGHT
;
309 Speaker2Chan
[4] = BACK_RIGHT
;
310 SpeakerAngle
[0] = -110.0f
* M_PI
/180.0f
;
311 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
312 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
313 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
314 SpeakerAngle
[4] = 110.0f
* M_PI
/180.0f
;
315 SetSpeakerArrangement("layout_51CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
318 case AL_FORMAT_61CHN8
:
319 case AL_FORMAT_61CHN16
:
320 case AL_FORMAT_61CHN32
:
321 Context
->ChannelMatrix
[BACK_LEFT
][BACK_CENTER
] = aluSqrt(0.5);
322 Context
->ChannelMatrix
[BACK_LEFT
][SIDE_LEFT
] = aluSqrt(0.5);
323 Context
->ChannelMatrix
[BACK_RIGHT
][BACK_CENTER
] = aluSqrt(0.5);
324 Context
->ChannelMatrix
[BACK_RIGHT
][SIDE_RIGHT
] = aluSqrt(0.5);
325 Context
->NumChan
= 6;
326 Speaker2Chan
[0] = SIDE_LEFT
;
327 Speaker2Chan
[1] = FRONT_LEFT
;
328 Speaker2Chan
[2] = FRONT_CENTER
;
329 Speaker2Chan
[3] = FRONT_RIGHT
;
330 Speaker2Chan
[4] = SIDE_RIGHT
;
331 Speaker2Chan
[5] = BACK_CENTER
;
332 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
333 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
334 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
335 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
336 SpeakerAngle
[4] = 90.0f
* M_PI
/180.0f
;
337 SpeakerAngle
[5] = 180.0f
* M_PI
/180.0f
;
338 SetSpeakerArrangement("layout_61CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
341 case AL_FORMAT_71CHN8
:
342 case AL_FORMAT_71CHN16
:
343 case AL_FORMAT_71CHN32
:
344 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
345 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
346 Context
->NumChan
= 7;
347 Speaker2Chan
[0] = BACK_LEFT
;
348 Speaker2Chan
[1] = SIDE_LEFT
;
349 Speaker2Chan
[2] = FRONT_LEFT
;
350 Speaker2Chan
[3] = FRONT_CENTER
;
351 Speaker2Chan
[4] = FRONT_RIGHT
;
352 Speaker2Chan
[5] = SIDE_RIGHT
;
353 Speaker2Chan
[6] = BACK_RIGHT
;
354 SpeakerAngle
[0] = -150.0f
* M_PI
/180.0f
;
355 SpeakerAngle
[1] = -90.0f
* M_PI
/180.0f
;
356 SpeakerAngle
[2] = -30.0f
* M_PI
/180.0f
;
357 SpeakerAngle
[3] = 0.0f
* M_PI
/180.0f
;
358 SpeakerAngle
[4] = 30.0f
* M_PI
/180.0f
;
359 SpeakerAngle
[5] = 90.0f
* M_PI
/180.0f
;
360 SpeakerAngle
[6] = 150.0f
* M_PI
/180.0f
;
361 SetSpeakerArrangement("layout_71CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
368 for(pos
= 0; pos
< LUT_NUM
; pos
++)
370 /* clear all values */
371 offset
= OUTPUTCHANNELS
* pos
;
372 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
373 Context
->PanningLUT
[offset
+s
] = 0.0f
;
375 if(Context
->NumChan
== 1)
377 Context
->PanningLUT
[offset
+ Speaker2Chan
[0]] = 1.0f
;
382 Theta
= aluLUTpos2Angle(pos
);
384 /* set panning values */
385 for(s
= 0; s
< Context
->NumChan
- 1; s
++)
387 if(Theta
>= SpeakerAngle
[s
] && Theta
< SpeakerAngle
[s
+1])
389 /* source between speaker s and speaker s+1 */
390 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
391 (SpeakerAngle
[s
+1]-SpeakerAngle
[s
]);
392 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
393 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
+1]] = sin(Alpha
);
397 if(s
== Context
->NumChan
- 1)
399 /* source between last and first speaker */
400 if(Theta
< SpeakerAngle
[0])
401 Theta
+= 2.0f
* M_PI
;
402 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
403 (2.0f
* M_PI
+ SpeakerAngle
[0]-SpeakerAngle
[s
]);
404 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
405 Context
->PanningLUT
[offset
+ Speaker2Chan
[0]] = sin(Alpha
);
410 static ALvoid
CalcNonAttnSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
)
412 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
413 ALfloat DryGain
, DryGainHF
;
414 ALfloat WetGain
[MAX_SENDS
];
415 ALfloat WetGainHF
[MAX_SENDS
];
416 ALint NumSends
, Frequency
;
420 //Get context properties
421 NumSends
= ALContext
->Device
->NumAuxSends
;
422 Frequency
= ALContext
->Device
->Frequency
;
424 //Get listener properties
425 ListenerGain
= ALContext
->Listener
.Gain
;
427 //Get source properties
428 SourceVolume
= ALSource
->flGain
;
429 MinVolume
= ALSource
->flMinGain
;
430 MaxVolume
= ALSource
->flMaxGain
;
432 //1. Multi-channel buffers always play "normal"
433 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
435 DryGain
= SourceVolume
;
436 DryGain
= __min(DryGain
,MaxVolume
);
437 DryGain
= __max(DryGain
,MinVolume
);
440 switch(ALSource
->DirectFilter
.type
)
442 case AL_FILTER_LOWPASS
:
443 DryGain
*= ALSource
->DirectFilter
.Gain
;
444 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
448 ALSource
->Params
.DryGains
[FRONT_LEFT
] = DryGain
* ListenerGain
;
449 ALSource
->Params
.DryGains
[FRONT_RIGHT
] = DryGain
* ListenerGain
;
450 ALSource
->Params
.DryGains
[SIDE_LEFT
] = DryGain
* ListenerGain
;
451 ALSource
->Params
.DryGains
[SIDE_RIGHT
] = DryGain
* ListenerGain
;
452 ALSource
->Params
.DryGains
[BACK_LEFT
] = DryGain
* ListenerGain
;
453 ALSource
->Params
.DryGains
[BACK_RIGHT
] = DryGain
* ListenerGain
;
454 ALSource
->Params
.DryGains
[FRONT_CENTER
] = DryGain
* ListenerGain
;
455 ALSource
->Params
.DryGains
[BACK_CENTER
] = DryGain
* ListenerGain
;
456 ALSource
->Params
.DryGains
[LFE
] = DryGain
* ListenerGain
;
458 for(i
= 0;i
< NumSends
;i
++)
460 WetGain
[i
] = SourceVolume
;
461 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
462 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
465 switch(ALSource
->Send
[i
].WetFilter
.type
)
467 case AL_FILTER_LOWPASS
:
468 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
469 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
473 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
475 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
477 ALSource
->Params
.WetGains
[i
] = 0.0f
;
481 /* Update filter coefficients. Calculations based on the I3DL2
483 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
485 /* We use two chained one-pole filters, so we need to take the
486 * square root of the squared gain, which is the same as the base
488 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
490 for(i
= 0;i
< NumSends
;i
++)
492 /* We use a one-pole filter, so we need to take the squared gain */
493 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
494 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
498 static ALvoid
CalcSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
)
500 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,DryMix
,OrigDist
;
501 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
502 ALfloat Velocity
[3],ListenerVel
[3];
503 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
,OuterGainHF
;
504 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
505 ALfloat DopplerFactor
, DopplerVelocity
, flSpeedOfSound
;
506 ALfloat Matrix
[4][4];
507 ALfloat flAttenuation
, effectiveDist
;
508 ALfloat RoomAttenuation
[MAX_SENDS
];
509 ALfloat MetersPerUnit
;
510 ALfloat RoomRolloff
[MAX_SENDS
];
511 ALfloat DryGainHF
= 1.0f
;
512 ALfloat WetGain
[MAX_SENDS
];
513 ALfloat WetGainHF
[MAX_SENDS
];
514 ALfloat DirGain
, AmbientGain
;
516 const ALfloat
*SpeakerGain
;
522 for(i
= 0;i
< MAX_SENDS
;i
++)
525 //Get context properties
526 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
527 DopplerVelocity
= ALContext
->DopplerVelocity
;
528 flSpeedOfSound
= ALContext
->flSpeedOfSound
;
529 NumSends
= ALContext
->Device
->NumAuxSends
;
530 Frequency
= ALContext
->Device
->Frequency
;
532 //Get listener properties
533 ListenerGain
= ALContext
->Listener
.Gain
;
534 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
535 memcpy(ListenerVel
, ALContext
->Listener
.Velocity
, sizeof(ALContext
->Listener
.Velocity
));
537 //Get source properties
538 SourceVolume
= ALSource
->flGain
;
539 memcpy(Position
, ALSource
->vPosition
, sizeof(ALSource
->vPosition
));
540 memcpy(Direction
, ALSource
->vOrientation
, sizeof(ALSource
->vOrientation
));
541 memcpy(Velocity
, ALSource
->vVelocity
, sizeof(ALSource
->vVelocity
));
542 MinVolume
= ALSource
->flMinGain
;
543 MaxVolume
= ALSource
->flMaxGain
;
544 MinDist
= ALSource
->flRefDistance
;
545 MaxDist
= ALSource
->flMaxDistance
;
546 Rolloff
= ALSource
->flRollOffFactor
;
547 InnerAngle
= ALSource
->flInnerAngle
;
548 OuterAngle
= ALSource
->flOuterAngle
;
549 OuterGainHF
= ALSource
->OuterGainHF
;
551 //1. Translate Listener to origin (convert to head relative)
552 if(ALSource
->bHeadRelative
==AL_FALSE
)
554 ALfloat U
[3],V
[3],N
[3],P
[3];
556 // Build transform matrix
557 memcpy(N
, ALContext
->Listener
.Forward
, sizeof(N
)); // At-vector
558 aluNormalize(N
); // Normalized At-vector
559 memcpy(V
, ALContext
->Listener
.Up
, sizeof(V
)); // Up-vector
560 aluNormalize(V
); // Normalized Up-vector
561 aluCrossproduct(N
, V
, U
); // Right-vector
562 aluNormalize(U
); // Normalized Right-vector
563 P
[0] = -(ALContext
->Listener
.Position
[0]*U
[0] + // Translation
564 ALContext
->Listener
.Position
[1]*U
[1] +
565 ALContext
->Listener
.Position
[2]*U
[2]);
566 P
[1] = -(ALContext
->Listener
.Position
[0]*V
[0] +
567 ALContext
->Listener
.Position
[1]*V
[1] +
568 ALContext
->Listener
.Position
[2]*V
[2]);
569 P
[2] = -(ALContext
->Listener
.Position
[0]*-N
[0] +
570 ALContext
->Listener
.Position
[1]*-N
[1] +
571 ALContext
->Listener
.Position
[2]*-N
[2]);
572 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0]; Matrix
[0][3] = 0.0f
;
573 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1]; Matrix
[1][3] = 0.0f
;
574 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2]; Matrix
[2][3] = 0.0f
;
575 Matrix
[3][0] = P
[0]; Matrix
[3][1] = P
[1]; Matrix
[3][2] = P
[2]; Matrix
[3][3] = 1.0f
;
577 // Transform source position and direction into listener space
578 aluMatrixVector(Position
, 1.0f
, Matrix
);
579 aluMatrixVector(Direction
, 0.0f
, Matrix
);
580 // Transform source and listener velocity into listener space
581 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
582 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
585 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
587 SourceToListener
[0] = -Position
[0];
588 SourceToListener
[1] = -Position
[1];
589 SourceToListener
[2] = -Position
[2];
590 aluNormalize(SourceToListener
);
591 aluNormalize(Direction
);
593 //2. Calculate distance attenuation
594 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
597 flAttenuation
= 1.0f
;
598 for(i
= 0;i
< NumSends
;i
++)
600 RoomAttenuation
[i
] = 1.0f
;
602 RoomRolloff
[i
] = ALSource
->RoomRolloffFactor
;
603 if(ALSource
->Send
[i
].Slot
&&
604 (ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
605 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
))
606 RoomRolloff
[i
] += ALSource
->Send
[i
].Slot
->effect
.Reverb
.RoomRolloffFactor
;
609 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
610 ALContext
->DistanceModel
)
612 case AL_INVERSE_DISTANCE_CLAMPED
:
613 Distance
=__max(Distance
,MinDist
);
614 Distance
=__min(Distance
,MaxDist
);
615 if(MaxDist
< MinDist
)
618 case AL_INVERSE_DISTANCE
:
621 if((MinDist
+ (Rolloff
* (Distance
- MinDist
))) > 0.0f
)
622 flAttenuation
= MinDist
/ (MinDist
+ (Rolloff
* (Distance
- MinDist
)));
623 for(i
= 0;i
< NumSends
;i
++)
625 if((MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
))) > 0.0f
)
626 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
)));
631 case AL_LINEAR_DISTANCE_CLAMPED
:
632 Distance
=__max(Distance
,MinDist
);
633 Distance
=__min(Distance
,MaxDist
);
634 if(MaxDist
< MinDist
)
637 case AL_LINEAR_DISTANCE
:
638 Distance
=__min(Distance
,MaxDist
);
639 if(MaxDist
!= MinDist
)
641 flAttenuation
= 1.0f
- (Rolloff
*(Distance
-MinDist
)/(MaxDist
- MinDist
));
642 for(i
= 0;i
< NumSends
;i
++)
643 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(Distance
-MinDist
)/(MaxDist
- MinDist
));
647 case AL_EXPONENT_DISTANCE_CLAMPED
:
648 Distance
=__max(Distance
,MinDist
);
649 Distance
=__min(Distance
,MaxDist
);
650 if(MaxDist
< MinDist
)
653 case AL_EXPONENT_DISTANCE
:
654 if(Distance
> 0.0f
&& MinDist
> 0.0f
)
656 flAttenuation
= (ALfloat
)pow(Distance
/MinDist
, -Rolloff
);
657 for(i
= 0;i
< NumSends
;i
++)
658 RoomAttenuation
[i
] = (ALfloat
)pow(Distance
/MinDist
, -RoomRolloff
[i
]);
666 // Source Gain + Attenuation
667 DryMix
= SourceVolume
* flAttenuation
;
668 for(i
= 0;i
< NumSends
;i
++)
669 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
671 effectiveDist
= 0.0f
;
673 effectiveDist
= (MinDist
/flAttenuation
- MinDist
)*MetersPerUnit
;
675 // Distance-based air absorption
676 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& effectiveDist
> 0.0f
)
680 // Absorption calculation is done in dB
681 absorb
= (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
) *
683 // Convert dB to linear gain before applying
684 absorb
= pow(10.0, absorb
/20.0);
689 //3. Apply directional soundcones
690 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * 180.0f
/M_PI
;
691 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
693 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
694 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
)*scale
);
695 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
)*scale
);
697 else if(Angle
> OuterAngle
)
699 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
));
700 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
));
708 // Apply some high-frequency attenuation for sources behind the listener
709 // NOTE: This should be aluDotproduct({0,0,-1}, ListenerToSource), however
710 // that is equivalent to aluDotproduct({0,0,1}, SourceToListener), which is
711 // the same as SourceToListener[2]
712 Angle
= aluAcos(SourceToListener
[2]) * 180.0f
/M_PI
;
713 // Sources within the minimum distance attenuate less
714 if(OrigDist
< MinDist
)
715 Angle
*= OrigDist
/MinDist
;
718 ALfloat scale
= (Angle
-90.0f
) / (180.1f
-90.0f
); // .1 to account for fp errors
719 ConeHF
*= 1.0f
- (ALContext
->Device
->HeadDampen
*scale
);
722 DryMix
*= ConeVolume
;
723 if(ALSource
->DryGainHFAuto
)
726 // Clamp to Min/Max Gain
727 DryMix
= __min(DryMix
,MaxVolume
);
728 DryMix
= __max(DryMix
,MinVolume
);
730 for(i
= 0;i
< NumSends
;i
++)
732 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
734 if(Slot
&& Slot
->effect
.type
!= AL_EFFECT_NULL
)
736 if(Slot
->AuxSendAuto
)
738 if(ALSource
->WetGainAuto
)
739 WetGain
[i
] *= ConeVolume
;
740 if(ALSource
->WetGainHFAuto
)
741 WetGainHF
[i
] *= ConeHF
;
743 // Clamp to Min/Max Gain
744 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
745 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
747 if(Slot
->effect
.type
== AL_EFFECT_REVERB
||
748 Slot
->effect
.type
== AL_EFFECT_EAXREVERB
)
750 /* Apply a decay-time transformation to the wet path,
751 * based on the attenuation of the dry path.
753 * Using the approximate (effective) source to listener
754 * distance, the initial decay of the reverb effect is
755 * calculated and applied to the wet path.
757 WetGain
[i
] *= pow(10.0, effectiveDist
/
758 (SPEEDOFSOUNDMETRESPERSEC
*
759 Slot
->effect
.Reverb
.DecayTime
) *
762 WetGainHF
[i
] *= pow(10.0,
763 log10(Slot
->effect
.Reverb
.AirAbsorptionGainHF
) *
764 ALSource
->AirAbsorptionFactor
* effectiveDist
);
769 // If the slot's auxiliary send auto is off, the data sent to
770 // the effect slot is the same as the dry path, sans filter
773 WetGainHF
[i
] = DryGainHF
;
776 switch(ALSource
->Send
[i
].WetFilter
.type
)
778 case AL_FILTER_LOWPASS
:
779 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
780 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
783 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
787 ALSource
->Params
.WetGains
[i
] = 0.0f
;
791 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
793 ALSource
->Params
.WetGains
[i
] = 0.0f
;
797 // Apply filter gains and filters
798 switch(ALSource
->DirectFilter
.type
)
800 case AL_FILTER_LOWPASS
:
801 DryMix
*= ALSource
->DirectFilter
.Gain
;
802 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
805 DryMix
*= ListenerGain
;
807 // Calculate Velocity
808 if(DopplerFactor
!= 0.0f
)
810 ALfloat flVSS
, flVLS
;
811 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
814 flVSS
= aluDotproduct(Velocity
, SourceToListener
);
815 if(flVSS
>= flMaxVelocity
)
816 flVSS
= (flMaxVelocity
- 1.0f
);
817 else if(flVSS
<= -flMaxVelocity
)
818 flVSS
= -flMaxVelocity
+ 1.0f
;
820 flVLS
= aluDotproduct(ListenerVel
, SourceToListener
);
821 if(flVLS
>= flMaxVelocity
)
822 flVLS
= (flMaxVelocity
- 1.0f
);
823 else if(flVLS
<= -flMaxVelocity
)
824 flVLS
= -flMaxVelocity
+ 1.0f
;
826 ALSource
->Params
.Pitch
= ALSource
->flPitch
*
827 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
828 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
831 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
833 // Use energy-preserving panning algorithm for multi-speaker playback
834 length
= __max(OrigDist
, MinDist
);
837 ALfloat invlen
= 1.0f
/length
;
838 Position
[0] *= invlen
;
839 Position
[1] *= invlen
;
840 Position
[2] *= invlen
;
843 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
844 SpeakerGain
= &ALContext
->PanningLUT
[OUTPUTCHANNELS
* pos
];
846 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
847 // elevation adjustment for directional gain. this sucks, but
848 // has low complexity
849 AmbientGain
= 1.0/aluSqrt(ALContext
->NumChan
) * (1.0-DirGain
);
850 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
852 ALfloat gain
= SpeakerGain
[s
]*DirGain
+ AmbientGain
;
853 ALSource
->Params
.DryGains
[s
] = DryMix
* gain
;
856 /* Update filter coefficients. */
857 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
859 /* Spatialized sources use four chained one-pole filters, so we need to
860 * take the fourth root of the squared gain, which is the same as the
861 * square root of the base gain. */
862 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(aluSqrt(DryGainHF
), cw
);
864 for(i
= 0;i
< NumSends
;i
++)
866 /* The wet path uses two chained one-pole filters, so take the
867 * base gain (square root of the squared gain) */
868 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= lpCoeffCalc(WetGainHF
[i
], cw
);
872 static __inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALint frac
)
874 return val1
+ ((val2
-val1
)*(frac
* (1.0f
/(1<<FRACTIONBITS
))));
877 static void MixSomeSources(ALCcontext
*ALContext
, float (*DryBuffer
)[OUTPUTCHANNELS
], ALuint SamplesToDo
)
879 static float DummyBuffer
[BUFFERSIZE
];
880 ALfloat
*WetBuffer
[MAX_SENDS
];
881 ALfloat (*Matrix
)[OUTPUTCHANNELS
] = ALContext
->ChannelMatrix
;
882 ALfloat DrySend
[OUTPUTCHANNELS
];
883 ALfloat dryGainStep
[OUTPUTCHANNELS
];
884 ALfloat wetGainStep
[MAX_SENDS
];
887 ALfloat value
, outsamp
;
888 ALbufferlistitem
*BufferListItem
;
889 ALint64 DataSize64
,DataPos64
;
890 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
891 ALfloat WetSend
[MAX_SENDS
];
895 ALuint DataPosInt
, DataPosFrac
;
896 ALuint Channels
, Bytes
;
898 ALuint BuffersPlayed
;
902 if(!(ALSource
=ALContext
->Source
))
905 DeviceFreq
= ALContext
->Device
->Frequency
;
907 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
908 rampLength
= max(rampLength
, SamplesToDo
);
911 if(ALSource
->state
!= AL_PLAYING
)
913 if((ALSource
=ALSource
->next
) != NULL
)
919 /* Find buffer format */
923 BufferListItem
= ALSource
->queue
;
924 while(BufferListItem
!= NULL
)
927 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
929 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
930 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
931 Frequency
= ALBuffer
->frequency
;
934 BufferListItem
= BufferListItem
->next
;
937 if(ALSource
->NeedsUpdate
)
939 //Only apply 3D calculations for mono buffers
941 CalcSourceParams(ALContext
, ALSource
);
943 CalcNonAttnSourceParams(ALContext
, ALSource
);
944 ALSource
->NeedsUpdate
= AL_FALSE
;
947 /* Get source info */
948 State
= ALSource
->state
;
949 BuffersPlayed
= ALSource
->BuffersPlayed
;
950 DataPosInt
= ALSource
->position
;
951 DataPosFrac
= ALSource
->position_fraction
;
953 /* Compute 18.14 fixed point step */
954 Pitch
= (ALSource
->Params
.Pitch
*Frequency
) / DeviceFreq
;
955 if(Pitch
> (float)MAX_PITCH
) Pitch
= (float)MAX_PITCH
;
956 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
957 if(increment
<= 0) increment
= (1<<FRACTIONBITS
);
959 /* Compute the gain steps for each output channel */
960 if(ALSource
->FirstStart
)
962 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
963 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
964 for(i
= 0;i
< MAX_SENDS
;i
++)
965 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
969 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
970 DrySend
[i
] = ALSource
->DryGains
[i
];
971 for(i
= 0;i
< MAX_SENDS
;i
++)
972 WetSend
[i
] = ALSource
->WetGains
[i
];
975 DryFilter
= &ALSource
->Params
.iirFilter
;
976 for(i
= 0;i
< MAX_SENDS
;i
++)
978 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
979 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
980 ALSource
->Send
[i
].Slot
->WetBuffer
:
984 if(DuplicateStereo
&& Channels
== 2)
986 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
987 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
988 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
989 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
991 else if(DuplicateStereo
)
993 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
994 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
995 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
996 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
999 /* Get current buffer queue item */
1000 BufferListItem
= ALSource
->queue
;
1001 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
1002 BufferListItem
= BufferListItem
->next
;
1004 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
1006 ALuint DataSize
= 0;
1011 /* Get buffer info */
1012 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
1014 Data
= ALBuffer
->data
;
1015 DataSize
= ALBuffer
->size
;
1016 DataSize
/= Channels
* Bytes
;
1018 if(DataPosInt
>= DataSize
)
1021 if(BufferListItem
->next
)
1023 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
1024 if(NextBuf
&& NextBuf
->data
)
1026 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1027 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1028 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1031 else if(ALSource
->bLooping
)
1033 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
1034 if(NextBuf
&& NextBuf
->data
)
1036 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1037 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1038 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1042 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
1044 /* Compute the gain steps for each output channel */
1045 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1046 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-DrySend
[i
]) /
1048 for(i
= 0;i
< MAX_SENDS
;i
++)
1049 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-WetSend
[i
]) /
1052 /* Figure out how many samples we can mix. */
1053 DataSize64
= DataSize
;
1054 DataSize64
<<= FRACTIONBITS
;
1055 DataPos64
= DataPosInt
;
1056 DataPos64
<<= FRACTIONBITS
;
1057 DataPos64
+= DataPosFrac
;
1058 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
1060 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
1062 /* Actual sample mixing loop */
1064 Data
+= DataPosInt
*Channels
;
1066 if(Channels
== 1) /* Mono */
1070 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1071 DrySend
[i
] += dryGainStep
[i
];
1072 for(i
= 0;i
< MAX_SENDS
;i
++)
1073 WetSend
[i
] += wetGainStep
[i
];
1075 /* First order interpolator */
1076 value
= lerp(Data
[k
], Data
[k
+1], DataPosFrac
);
1078 /* Direct path final mix buffer and panning */
1079 outsamp
= lpFilter4P(DryFilter
, 0, value
);
1080 DryBuffer
[j
][FRONT_LEFT
] += outsamp
*DrySend
[FRONT_LEFT
];
1081 DryBuffer
[j
][FRONT_RIGHT
] += outsamp
*DrySend
[FRONT_RIGHT
];
1082 DryBuffer
[j
][SIDE_LEFT
] += outsamp
*DrySend
[SIDE_LEFT
];
1083 DryBuffer
[j
][SIDE_RIGHT
] += outsamp
*DrySend
[SIDE_RIGHT
];
1084 DryBuffer
[j
][BACK_LEFT
] += outsamp
*DrySend
[BACK_LEFT
];
1085 DryBuffer
[j
][BACK_RIGHT
] += outsamp
*DrySend
[BACK_RIGHT
];
1086 DryBuffer
[j
][FRONT_CENTER
] += outsamp
*DrySend
[FRONT_CENTER
];
1087 DryBuffer
[j
][BACK_CENTER
] += outsamp
*DrySend
[BACK_CENTER
];
1089 /* Room path final mix buffer and panning */
1090 for(i
= 0;i
< MAX_SENDS
;i
++)
1092 outsamp
= lpFilter2P(WetFilter
[i
], 0, value
);
1093 WetBuffer
[i
][j
] += outsamp
*WetSend
[i
];
1096 DataPosFrac
+= increment
;
1097 k
+= DataPosFrac
>>FRACTIONBITS
;
1098 DataPosFrac
&= FRACTIONMASK
;
1102 else if(Channels
== 2) /* Stereo */
1104 const int chans
[] = {
1105 FRONT_LEFT
, FRONT_RIGHT
1107 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1109 #define DO_MIX() do { \
1110 while(BufferSize--) \
1112 for(i = 0;i < OUTPUTCHANNELS;i++) \
1113 DrySend[i] += dryGainStep[i]; \
1114 for(i = 0;i < MAX_SENDS;i++) \
1115 WetSend[i] += wetGainStep[i]; \
1117 for(i = 0;i < Channels;i++) \
1119 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1120 outsamp = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1121 for(out = 0;out < OUTPUTCHANNELS;out++) \
1122 DryBuffer[j][out] += outsamp*Matrix[chans[i]][out]; \
1123 for(out = 0;out < MAX_SENDS;out++) \
1125 outsamp = lpFilter1P(WetFilter[out], chans[i], value); \
1126 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1130 DataPosFrac += increment; \
1131 k += DataPosFrac>>FRACTIONBITS; \
1132 DataPosFrac &= FRACTIONMASK; \
1139 else if(Channels
== 4) /* Quad */
1141 const int chans
[] = {
1142 FRONT_LEFT
, FRONT_RIGHT
,
1143 BACK_LEFT
, BACK_RIGHT
1145 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1149 else if(Channels
== 6) /* 5.1 */
1151 const int chans
[] = {
1152 FRONT_LEFT
, FRONT_RIGHT
,
1154 BACK_LEFT
, BACK_RIGHT
1156 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1160 else if(Channels
== 7) /* 6.1 */
1162 const int chans
[] = {
1163 FRONT_LEFT
, FRONT_RIGHT
,
1166 SIDE_LEFT
, SIDE_RIGHT
1168 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1172 else if(Channels
== 8) /* 7.1 */
1174 const int chans
[] = {
1175 FRONT_LEFT
, FRONT_RIGHT
,
1177 BACK_LEFT
, BACK_RIGHT
,
1178 SIDE_LEFT
, SIDE_RIGHT
1180 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1187 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1188 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1189 for(i
= 0;i
< MAX_SENDS
;i
++)
1190 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1193 DataPosFrac
+= increment
;
1194 k
+= DataPosFrac
>>FRACTIONBITS
;
1195 DataPosFrac
&= FRACTIONMASK
;
1202 /* Handle looping sources */
1203 if(DataPosInt
>= DataSize
)
1205 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1207 BufferListItem
= BufferListItem
->next
;
1209 DataPosInt
-= DataSize
;
1211 else if(ALSource
->bLooping
)
1213 BufferListItem
= ALSource
->queue
;
1215 if(ALSource
->BuffersInQueue
== 1)
1216 DataPosInt
%= DataSize
;
1218 DataPosInt
-= DataSize
;
1223 BufferListItem
= ALSource
->queue
;
1224 BuffersPlayed
= ALSource
->BuffersInQueue
;
1231 /* Update source info */
1232 ALSource
->state
= State
;
1233 ALSource
->BuffersPlayed
= BuffersPlayed
;
1234 ALSource
->position
= DataPosInt
;
1235 ALSource
->position_fraction
= DataPosFrac
;
1236 ALSource
->Buffer
= BufferListItem
->buffer
;
1238 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1239 ALSource
->DryGains
[i
] = DrySend
[i
];
1240 for(i
= 0;i
< MAX_SENDS
;i
++)
1241 ALSource
->WetGains
[i
] = WetSend
[i
];
1243 ALSource
->FirstStart
= AL_FALSE
;
1245 if((ALSource
=ALSource
->next
) != NULL
)
1246 goto another_source
;
1249 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1251 float (*DryBuffer
)[OUTPUTCHANNELS
];
1252 const Channel
*ChanMap
;
1254 ALeffectslot
*ALEffectSlot
;
1255 ALCcontext
*ALContext
;
1259 SuspendContext(NULL
);
1261 #if defined(HAVE_FESETROUND)
1262 fpuState
= fegetround();
1263 fesetround(FE_TOWARDZERO
);
1264 #elif defined(HAVE__CONTROLFP)
1265 fpuState
= _controlfp(0, 0);
1266 _controlfp(_RC_CHOP
, _MCW_RC
);
1271 DryBuffer
= device
->DryBuffer
;
1274 /* Setup variables */
1275 SamplesToDo
= min(size
, BUFFERSIZE
);
1277 /* Clear mixing buffer */
1278 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1280 for(c
= 0;c
< device
->NumContexts
;c
++)
1282 ALContext
= device
->Contexts
[c
];
1283 SuspendContext(ALContext
);
1285 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1287 /* effect slot processing */
1288 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1291 if(ALEffectSlot
->EffectState
)
1292 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1294 for(i
= 0;i
< SamplesToDo
;i
++)
1295 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1296 ALEffectSlot
= ALEffectSlot
->next
;
1298 ProcessContext(ALContext
);
1301 //Post processing loop
1302 ChanMap
= device
->DevChannels
;
1303 switch(device
->Format
)
1305 #define CHECK_WRITE_FORMAT(bits, type, func) \
1306 case AL_FORMAT_MONO##bits: \
1307 for(i = 0;i < SamplesToDo;i++) \
1309 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1310 buffer = ((type*)buffer) + 1; \
1313 case AL_FORMAT_STEREO##bits: \
1316 for(i = 0;i < SamplesToDo;i++) \
1319 samples[0] = DryBuffer[i][ChanMap[0]]; \
1320 samples[1] = DryBuffer[i][ChanMap[1]]; \
1321 bs2b_cross_feed(device->Bs2b, samples); \
1322 ((type*)buffer)[0] = (func)(samples[0]); \
1323 ((type*)buffer)[1] = (func)(samples[1]); \
1324 buffer = ((type*)buffer) + 2; \
1329 for(i = 0;i < SamplesToDo;i++) \
1331 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1332 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1333 buffer = ((type*)buffer) + 2; \
1337 case AL_FORMAT_QUAD##bits: \
1338 for(i = 0;i < SamplesToDo;i++) \
1340 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1341 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1342 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1343 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1344 buffer = ((type*)buffer) + 4; \
1347 case AL_FORMAT_51CHN##bits: \
1348 for(i = 0;i < SamplesToDo;i++) \
1350 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1351 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1352 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1353 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1354 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1355 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1356 buffer = ((type*)buffer) + 6; \
1359 case AL_FORMAT_61CHN##bits: \
1360 for(i = 0;i < SamplesToDo;i++) \
1362 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1363 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1364 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1365 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1366 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1367 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1368 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1369 buffer = ((type*)buffer) + 7; \
1372 case AL_FORMAT_71CHN##bits: \
1373 for(i = 0;i < SamplesToDo;i++) \
1375 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1376 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1377 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1378 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1379 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1380 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1381 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1382 ((type*)buffer)[7] = (func)(DryBuffer[i][ChanMap[7]]); \
1383 buffer = ((type*)buffer) + 8; \
1387 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1388 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1389 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
)
1390 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
)
1391 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
)
1392 #undef AL_FORMAT_STEREO32
1393 #undef AL_FORMAT_MONO32
1394 #undef CHECK_WRITE_FORMAT
1400 size
-= SamplesToDo
;
1403 #if defined(HAVE_FESETROUND)
1404 fesetround(fpuState
);
1405 #elif defined(HAVE__CONTROLFP)
1406 _controlfp(fpuState
, 0xfffff);
1409 ProcessContext(NULL
);
1412 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1416 SuspendContext(NULL
);
1417 for(i
= 0;i
< device
->NumContexts
;i
++)
1421 SuspendContext(device
->Contexts
[i
]);
1423 source
= device
->Contexts
[i
]->Source
;
1426 if(source
->state
== AL_PLAYING
)
1428 source
->state
= AL_STOPPED
;
1429 source
->BuffersPlayed
= source
->BuffersInQueue
;
1430 source
->position
= 0;
1431 source
->position_fraction
= 0;
1433 source
= source
->next
;
1435 ProcessContext(device
->Contexts
[i
]);
1438 device
->Connected
= ALC_FALSE
;
1439 ProcessContext(NULL
);