2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
35 #include "alListener.h"
36 #include "alAuxEffectSlot.h"
40 #define FRACTIONBITS 14
41 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
42 #define MAX_PITCH 65536
44 /* Minimum ramp length in milliseconds. The value below was chosen to
45 * adequately reduce clicks and pops from harsh gain changes. */
46 #define MIN_RAMP_LENGTH 16
49 static __inline ALfloat
aluF2F(ALfloat Value
)
54 static __inline ALshort
aluF2S(ALfloat Value
)
60 i
= (ALint
)(Value
*32768.0f
);
65 i
= (ALint
)(Value
*32767.0f
);
71 static __inline ALubyte
aluF2UB(ALfloat Value
)
73 ALshort i
= aluF2S(Value
);
78 static __inline ALvoid
aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
80 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
81 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
82 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
85 static __inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
87 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
88 inVector1
[2]*inVector2
[2];
91 static __inline ALvoid
aluNormalize(ALfloat
*inVector
)
93 ALfloat length
, inverse_length
;
95 length
= aluSqrt(aluDotproduct(inVector
, inVector
));
98 inverse_length
= 1.0f
/length
;
99 inVector
[0] *= inverse_length
;
100 inVector
[1] *= inverse_length
;
101 inVector
[2] *= inverse_length
;
105 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
108 vector
[0], vector
[1], vector
[2], w
111 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
112 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
113 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
116 static ALvoid
SetSpeakerArrangement(const char *name
, ALfloat SpeakerAngle
[OUTPUTCHANNELS
],
117 Channel Speaker2Chan
[OUTPUTCHANNELS
], ALint chans
)
119 char layout_str
[256];
120 char *confkey
, *next
;
125 strncpy(layout_str
, GetConfigValue(NULL
, name
, ""), sizeof(layout_str
));
131 next
= confkey
= layout_str
;
135 next
= strchr(confkey
, ',');
141 } while(isspace(*next
) || *next
== ',');
144 sep
= strchr(confkey
, '=');
145 if(!sep
|| confkey
== sep
)
149 while(isspace(*end
) && end
!= confkey
)
153 if(strcmp(confkey
, "fl") == 0 || strcmp(confkey
, "front-left") == 0)
155 else if(strcmp(confkey
, "fr") == 0 || strcmp(confkey
, "front-right") == 0)
157 else if(strcmp(confkey
, "fc") == 0 || strcmp(confkey
, "front-center") == 0)
159 else if(strcmp(confkey
, "bl") == 0 || strcmp(confkey
, "back-left") == 0)
161 else if(strcmp(confkey
, "br") == 0 || strcmp(confkey
, "back-right") == 0)
163 else if(strcmp(confkey
, "bc") == 0 || strcmp(confkey
, "back-center") == 0)
165 else if(strcmp(confkey
, "sl") == 0 || strcmp(confkey
, "side-left") == 0)
167 else if(strcmp(confkey
, "sr") == 0 || strcmp(confkey
, "side-right") == 0)
171 AL_PRINT("Unknown speaker for %s: \"%s\"\n", name
, confkey
);
179 for(i
= 0;i
< chans
;i
++)
181 if(Speaker2Chan
[i
] == val
)
183 long angle
= strtol(sep
, NULL
, 10);
184 if(angle
>= -180 && angle
<= 180)
185 SpeakerAngle
[i
] = angle
* M_PI
/180.0f
;
187 AL_PRINT("Invalid angle for speaker \"%s\": %ld\n", confkey
, angle
);
193 for(i
= 0;i
< chans
;i
++)
198 for(i2
= i
+1;i2
< chans
;i2
++)
200 if(SpeakerAngle
[i2
] < SpeakerAngle
[min
])
209 tmpf
= SpeakerAngle
[i
];
210 SpeakerAngle
[i
] = SpeakerAngle
[min
];
211 SpeakerAngle
[min
] = tmpf
;
213 tmpc
= Speaker2Chan
[i
];
214 Speaker2Chan
[i
] = Speaker2Chan
[min
];
215 Speaker2Chan
[min
] = tmpc
;
220 static __inline ALfloat
aluLUTpos2Angle(ALint pos
)
222 if(pos
< QUADRANT_NUM
)
223 return aluAtan((ALfloat
)pos
/ (ALfloat
)(QUADRANT_NUM
- pos
));
224 if(pos
< 2 * QUADRANT_NUM
)
225 return M_PI_2
+ aluAtan((ALfloat
)(pos
- QUADRANT_NUM
) / (ALfloat
)(2 * QUADRANT_NUM
- pos
));
226 if(pos
< 3 * QUADRANT_NUM
)
227 return aluAtan((ALfloat
)(pos
- 2 * QUADRANT_NUM
) / (ALfloat
)(3 * QUADRANT_NUM
- pos
)) - M_PI
;
228 return aluAtan((ALfloat
)(pos
- 3 * QUADRANT_NUM
) / (ALfloat
)(4 * QUADRANT_NUM
- pos
)) - M_PI_2
;
231 ALvoid
aluInitPanning(ALCdevice
*Device
)
233 ALfloat SpeakerAngle
[OUTPUTCHANNELS
];
234 Channel
*Speaker2Chan
;
235 ALfloat Alpha
, Theta
;
239 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
241 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
242 Device
->ChannelMatrix
[s
][s2
] = ((s
==s2
) ? 1.0f
: 0.0f
);
245 Speaker2Chan
= Device
->Speaker2Chan
;
246 switch(Device
->Format
)
248 case AL_FORMAT_MONO8
:
249 case AL_FORMAT_MONO16
:
250 case AL_FORMAT_MONO_FLOAT32
:
251 Device
->DuplicateStereo
= AL_FALSE
;
252 Device
->ChannelMatrix
[FRONT_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
253 Device
->ChannelMatrix
[FRONT_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
254 Device
->ChannelMatrix
[SIDE_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
255 Device
->ChannelMatrix
[SIDE_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
256 Device
->ChannelMatrix
[BACK_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
257 Device
->ChannelMatrix
[BACK_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
258 Device
->ChannelMatrix
[BACK_CENTER
][FRONT_CENTER
] = 1.0f
;
260 Speaker2Chan
[0] = FRONT_CENTER
;
261 SpeakerAngle
[0] = 0.0f
* M_PI
/180.0f
;
264 case AL_FORMAT_STEREO8
:
265 case AL_FORMAT_STEREO16
:
266 case AL_FORMAT_STEREO_FLOAT32
:
267 Device
->DuplicateStereo
= AL_FALSE
;
268 Device
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
269 Device
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
270 Device
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
271 Device
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
272 Device
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
273 Device
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
274 Device
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
275 Device
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
277 Speaker2Chan
[0] = FRONT_LEFT
;
278 Speaker2Chan
[1] = FRONT_RIGHT
;
279 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
280 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
281 SetSpeakerArrangement("layout", SpeakerAngle
, Speaker2Chan
, Device
->NumChan
);
284 case AL_FORMAT_QUAD8
:
285 case AL_FORMAT_QUAD16
:
286 case AL_FORMAT_QUAD32
:
287 Device
->DuplicateStereo
= GetConfigValueBool(NULL
, "stereodup", 0);
288 Device
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
289 Device
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
290 Device
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
291 Device
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
292 Device
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
293 Device
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
294 Device
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
295 Device
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
297 Speaker2Chan
[0] = BACK_LEFT
;
298 Speaker2Chan
[1] = FRONT_LEFT
;
299 Speaker2Chan
[2] = FRONT_RIGHT
;
300 Speaker2Chan
[3] = BACK_RIGHT
;
301 SpeakerAngle
[0] = -135.0f
* M_PI
/180.0f
;
302 SpeakerAngle
[1] = -45.0f
* M_PI
/180.0f
;
303 SpeakerAngle
[2] = 45.0f
* M_PI
/180.0f
;
304 SpeakerAngle
[3] = 135.0f
* M_PI
/180.0f
;
305 SetSpeakerArrangement("layout", SpeakerAngle
, Speaker2Chan
, Device
->NumChan
);
308 case AL_FORMAT_51CHN8
:
309 case AL_FORMAT_51CHN16
:
310 case AL_FORMAT_51CHN32
:
311 Device
->DuplicateStereo
= GetConfigValueBool(NULL
, "stereodup", 0);
312 Device
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
313 Device
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
314 Device
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
315 Device
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
316 Device
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
317 Device
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
319 Speaker2Chan
[0] = BACK_LEFT
;
320 Speaker2Chan
[1] = FRONT_LEFT
;
321 Speaker2Chan
[2] = FRONT_CENTER
;
322 Speaker2Chan
[3] = FRONT_RIGHT
;
323 Speaker2Chan
[4] = BACK_RIGHT
;
324 SpeakerAngle
[0] = -110.0f
* M_PI
/180.0f
;
325 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
326 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
327 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
328 SpeakerAngle
[4] = 110.0f
* M_PI
/180.0f
;
329 SetSpeakerArrangement("layout", SpeakerAngle
, Speaker2Chan
, Device
->NumChan
);
332 case AL_FORMAT_61CHN8
:
333 case AL_FORMAT_61CHN16
:
334 case AL_FORMAT_61CHN32
:
335 Device
->DuplicateStereo
= GetConfigValueBool(NULL
, "stereodup", 0);
336 Device
->ChannelMatrix
[BACK_LEFT
][BACK_CENTER
] = aluSqrt(0.5);
337 Device
->ChannelMatrix
[BACK_LEFT
][SIDE_LEFT
] = aluSqrt(0.5);
338 Device
->ChannelMatrix
[BACK_RIGHT
][BACK_CENTER
] = aluSqrt(0.5);
339 Device
->ChannelMatrix
[BACK_RIGHT
][SIDE_RIGHT
] = aluSqrt(0.5);
341 Speaker2Chan
[0] = SIDE_LEFT
;
342 Speaker2Chan
[1] = FRONT_LEFT
;
343 Speaker2Chan
[2] = FRONT_CENTER
;
344 Speaker2Chan
[3] = FRONT_RIGHT
;
345 Speaker2Chan
[4] = SIDE_RIGHT
;
346 Speaker2Chan
[5] = BACK_CENTER
;
347 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
348 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
349 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
350 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
351 SpeakerAngle
[4] = 90.0f
* M_PI
/180.0f
;
352 SpeakerAngle
[5] = 180.0f
* M_PI
/180.0f
;
353 SetSpeakerArrangement("layout", SpeakerAngle
, Speaker2Chan
, Device
->NumChan
);
356 case AL_FORMAT_71CHN8
:
357 case AL_FORMAT_71CHN16
:
358 case AL_FORMAT_71CHN32
:
359 Device
->DuplicateStereo
= GetConfigValueBool(NULL
, "stereodup", 0);
360 Device
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
361 Device
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
363 Speaker2Chan
[0] = BACK_LEFT
;
364 Speaker2Chan
[1] = SIDE_LEFT
;
365 Speaker2Chan
[2] = FRONT_LEFT
;
366 Speaker2Chan
[3] = FRONT_CENTER
;
367 Speaker2Chan
[4] = FRONT_RIGHT
;
368 Speaker2Chan
[5] = SIDE_RIGHT
;
369 Speaker2Chan
[6] = BACK_RIGHT
;
370 SpeakerAngle
[0] = -150.0f
* M_PI
/180.0f
;
371 SpeakerAngle
[1] = -90.0f
* M_PI
/180.0f
;
372 SpeakerAngle
[2] = -30.0f
* M_PI
/180.0f
;
373 SpeakerAngle
[3] = 0.0f
* M_PI
/180.0f
;
374 SpeakerAngle
[4] = 30.0f
* M_PI
/180.0f
;
375 SpeakerAngle
[5] = 90.0f
* M_PI
/180.0f
;
376 SpeakerAngle
[6] = 150.0f
* M_PI
/180.0f
;
377 SetSpeakerArrangement("layout", SpeakerAngle
, Speaker2Chan
, Device
->NumChan
);
384 if(GetConfigValueBool(NULL
, "scalemix", 0))
386 ALfloat maxout
= 1.0f
;
387 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
390 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
391 out
+= Device
->ChannelMatrix
[s2
][s
];
392 maxout
= __max(maxout
, out
);
395 maxout
= 1.0f
/maxout
;
396 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
398 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
399 Device
->ChannelMatrix
[s2
][s
] *= maxout
;
403 for(pos
= 0; pos
< LUT_NUM
; pos
++)
405 /* clear all values */
406 offset
= OUTPUTCHANNELS
* pos
;
407 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
408 Device
->PanningLUT
[offset
+s
] = 0.0f
;
410 if(Device
->NumChan
== 1)
412 Device
->PanningLUT
[offset
+ Speaker2Chan
[0]] = 1.0f
;
417 Theta
= aluLUTpos2Angle(pos
);
419 /* set panning values */
420 for(s
= 0; s
< Device
->NumChan
- 1; s
++)
422 if(Theta
>= SpeakerAngle
[s
] && Theta
< SpeakerAngle
[s
+1])
424 /* source between speaker s and speaker s+1 */
425 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
426 (SpeakerAngle
[s
+1]-SpeakerAngle
[s
]);
427 Device
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
428 Device
->PanningLUT
[offset
+ Speaker2Chan
[s
+1]] = sin(Alpha
);
432 if(s
== Device
->NumChan
- 1)
434 /* source between last and first speaker */
435 if(Theta
< SpeakerAngle
[0])
436 Theta
+= 2.0f
* M_PI
;
437 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
438 (2.0f
* M_PI
+ SpeakerAngle
[0]-SpeakerAngle
[s
]);
439 Device
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
440 Device
->PanningLUT
[offset
+ Speaker2Chan
[0]] = sin(Alpha
);
445 static ALvoid
CalcNonAttnSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
)
447 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
448 ALfloat DryGain
, DryGainHF
;
449 ALfloat WetGain
[MAX_SENDS
];
450 ALfloat WetGainHF
[MAX_SENDS
];
451 ALint NumSends
, Frequency
;
455 //Get context properties
456 NumSends
= ALContext
->Device
->NumAuxSends
;
457 Frequency
= ALContext
->Device
->Frequency
;
459 //Get listener properties
460 ListenerGain
= ALContext
->Listener
.Gain
;
462 //Get source properties
463 SourceVolume
= ALSource
->flGain
;
464 MinVolume
= ALSource
->flMinGain
;
465 MaxVolume
= ALSource
->flMaxGain
;
467 //1. Multi-channel buffers always play "normal"
468 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
470 DryGain
= SourceVolume
;
471 DryGain
= __min(DryGain
,MaxVolume
);
472 DryGain
= __max(DryGain
,MinVolume
);
475 switch(ALSource
->DirectFilter
.type
)
477 case AL_FILTER_LOWPASS
:
478 DryGain
*= ALSource
->DirectFilter
.Gain
;
479 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
483 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
484 ALSource
->Params
.DryGains
[i
] = DryGain
* ListenerGain
;
486 for(i
= 0;i
< NumSends
;i
++)
488 WetGain
[i
] = SourceVolume
;
489 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
490 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
493 switch(ALSource
->Send
[i
].WetFilter
.type
)
495 case AL_FILTER_LOWPASS
:
496 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
497 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
501 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
503 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
505 ALSource
->Params
.WetGains
[i
] = 0.0f
;
509 /* Update filter coefficients. Calculations based on the I3DL2
511 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
513 /* We use two chained one-pole filters, so we need to take the
514 * square root of the squared gain, which is the same as the base
516 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
518 for(i
= 0;i
< NumSends
;i
++)
520 /* We use a one-pole filter, so we need to take the squared gain */
521 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
522 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
526 static ALvoid
CalcSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
)
528 const ALCdevice
*Device
= ALContext
->Device
;
529 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,DryMix
,OrigDist
;
530 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
531 ALfloat Velocity
[3],ListenerVel
[3];
532 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
,OuterGainHF
;
533 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
534 ALfloat DopplerFactor
, DopplerVelocity
, flSpeedOfSound
;
535 ALfloat Matrix
[4][4];
536 ALfloat flAttenuation
, effectiveDist
;
537 ALfloat RoomAttenuation
[MAX_SENDS
];
538 ALfloat MetersPerUnit
;
539 ALfloat RoomRolloff
[MAX_SENDS
];
540 ALfloat DryGainHF
= 1.0f
;
541 ALfloat WetGain
[MAX_SENDS
];
542 ALfloat WetGainHF
[MAX_SENDS
];
543 ALfloat DirGain
, AmbientGain
;
544 const ALfloat
*SpeakerGain
;
551 for(i
= 0;i
< MAX_SENDS
;i
++)
554 //Get context properties
555 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
556 DopplerVelocity
= ALContext
->DopplerVelocity
;
557 flSpeedOfSound
= ALContext
->flSpeedOfSound
;
558 NumSends
= Device
->NumAuxSends
;
559 Frequency
= Device
->Frequency
;
561 //Get listener properties
562 ListenerGain
= ALContext
->Listener
.Gain
;
563 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
564 memcpy(ListenerVel
, ALContext
->Listener
.Velocity
, sizeof(ALContext
->Listener
.Velocity
));
566 //Get source properties
567 SourceVolume
= ALSource
->flGain
;
568 memcpy(Position
, ALSource
->vPosition
, sizeof(ALSource
->vPosition
));
569 memcpy(Direction
, ALSource
->vOrientation
, sizeof(ALSource
->vOrientation
));
570 memcpy(Velocity
, ALSource
->vVelocity
, sizeof(ALSource
->vVelocity
));
571 MinVolume
= ALSource
->flMinGain
;
572 MaxVolume
= ALSource
->flMaxGain
;
573 MinDist
= ALSource
->flRefDistance
;
574 MaxDist
= ALSource
->flMaxDistance
;
575 Rolloff
= ALSource
->flRollOffFactor
;
576 InnerAngle
= ALSource
->flInnerAngle
;
577 OuterAngle
= ALSource
->flOuterAngle
;
578 OuterGainHF
= ALSource
->OuterGainHF
;
580 //1. Translate Listener to origin (convert to head relative)
581 if(ALSource
->bHeadRelative
==AL_FALSE
)
583 ALfloat U
[3],V
[3],N
[3];
585 // Build transform matrix
586 memcpy(N
, ALContext
->Listener
.Forward
, sizeof(N
)); // At-vector
587 aluNormalize(N
); // Normalized At-vector
588 memcpy(V
, ALContext
->Listener
.Up
, sizeof(V
)); // Up-vector
589 aluNormalize(V
); // Normalized Up-vector
590 aluCrossproduct(N
, V
, U
); // Right-vector
591 aluNormalize(U
); // Normalized Right-vector
592 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0]; Matrix
[0][3] = 0.0f
;
593 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1]; Matrix
[1][3] = 0.0f
;
594 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2]; Matrix
[2][3] = 0.0f
;
595 Matrix
[3][0] = 0.0f
; Matrix
[3][1] = 0.0f
; Matrix
[3][2] = 0.0f
; Matrix
[3][3] = 1.0f
;
597 // Translate position
598 Position
[0] -= ALContext
->Listener
.Position
[0];
599 Position
[1] -= ALContext
->Listener
.Position
[1];
600 Position
[2] -= ALContext
->Listener
.Position
[2];
602 // Transform source position and direction into listener space
603 aluMatrixVector(Position
, 1.0f
, Matrix
);
604 aluMatrixVector(Direction
, 0.0f
, Matrix
);
605 // Transform source and listener velocity into listener space
606 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
607 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
610 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
612 SourceToListener
[0] = -Position
[0];
613 SourceToListener
[1] = -Position
[1];
614 SourceToListener
[2] = -Position
[2];
615 aluNormalize(SourceToListener
);
616 aluNormalize(Direction
);
618 //2. Calculate distance attenuation
619 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
622 flAttenuation
= 1.0f
;
623 for(i
= 0;i
< NumSends
;i
++)
625 RoomAttenuation
[i
] = 1.0f
;
627 RoomRolloff
[i
] = ALSource
->RoomRolloffFactor
;
628 if(ALSource
->Send
[i
].Slot
&&
629 (ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
630 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
))
631 RoomRolloff
[i
] += ALSource
->Send
[i
].Slot
->effect
.Reverb
.RoomRolloffFactor
;
634 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
635 ALContext
->DistanceModel
)
637 case AL_INVERSE_DISTANCE_CLAMPED
:
638 Distance
=__max(Distance
,MinDist
);
639 Distance
=__min(Distance
,MaxDist
);
640 if(MaxDist
< MinDist
)
643 case AL_INVERSE_DISTANCE
:
646 if((MinDist
+ (Rolloff
* (Distance
- MinDist
))) > 0.0f
)
647 flAttenuation
= MinDist
/ (MinDist
+ (Rolloff
* (Distance
- MinDist
)));
648 for(i
= 0;i
< NumSends
;i
++)
650 if((MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
))) > 0.0f
)
651 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
)));
656 case AL_LINEAR_DISTANCE_CLAMPED
:
657 Distance
=__max(Distance
,MinDist
);
658 Distance
=__min(Distance
,MaxDist
);
659 if(MaxDist
< MinDist
)
662 case AL_LINEAR_DISTANCE
:
663 Distance
=__min(Distance
,MaxDist
);
664 if(MaxDist
!= MinDist
)
666 flAttenuation
= 1.0f
- (Rolloff
*(Distance
-MinDist
)/(MaxDist
- MinDist
));
667 for(i
= 0;i
< NumSends
;i
++)
668 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(Distance
-MinDist
)/(MaxDist
- MinDist
));
672 case AL_EXPONENT_DISTANCE_CLAMPED
:
673 Distance
=__max(Distance
,MinDist
);
674 Distance
=__min(Distance
,MaxDist
);
675 if(MaxDist
< MinDist
)
678 case AL_EXPONENT_DISTANCE
:
679 if(Distance
> 0.0f
&& MinDist
> 0.0f
)
681 flAttenuation
= aluPow(Distance
/MinDist
, -Rolloff
);
682 for(i
= 0;i
< NumSends
;i
++)
683 RoomAttenuation
[i
] = aluPow(Distance
/MinDist
, -RoomRolloff
[i
]);
691 // Source Gain + Attenuation
692 DryMix
= SourceVolume
* flAttenuation
;
693 for(i
= 0;i
< NumSends
;i
++)
694 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
696 effectiveDist
= 0.0f
;
698 effectiveDist
= (MinDist
/flAttenuation
- MinDist
)*MetersPerUnit
;
700 // Distance-based air absorption
701 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& effectiveDist
> 0.0f
)
705 // Absorption calculation is done in dB
706 absorb
= (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
) *
708 // Convert dB to linear gain before applying
709 absorb
= aluPow(10.0f
, absorb
/20.0f
);
714 //3. Apply directional soundcones
715 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * 180.0f
/M_PI
;
716 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
718 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
719 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
)*scale
);
720 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
)*scale
);
722 else if(Angle
> OuterAngle
)
724 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
));
725 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
));
733 // Apply some high-frequency attenuation for sources behind the listener
734 // NOTE: This should be aluDotproduct({0,0,-1}, ListenerToSource), however
735 // that is equivalent to aluDotproduct({0,0,1}, SourceToListener), which is
736 // the same as SourceToListener[2]
737 Angle
= aluAcos(SourceToListener
[2]) * 180.0f
/M_PI
;
738 // Sources within the minimum distance attenuate less
739 if(OrigDist
< MinDist
)
740 Angle
*= OrigDist
/MinDist
;
743 ALfloat scale
= (Angle
-90.0f
) / (180.1f
-90.0f
); // .1 to account for fp errors
744 ConeHF
*= 1.0f
- (Device
->HeadDampen
*scale
);
747 DryMix
*= ConeVolume
;
748 if(ALSource
->DryGainHFAuto
)
751 // Clamp to Min/Max Gain
752 DryMix
= __min(DryMix
,MaxVolume
);
753 DryMix
= __max(DryMix
,MinVolume
);
755 for(i
= 0;i
< NumSends
;i
++)
757 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
759 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
761 ALSource
->Params
.WetGains
[i
] = 0.0f
;
766 if(Slot
->AuxSendAuto
)
768 if(ALSource
->WetGainAuto
)
769 WetGain
[i
] *= ConeVolume
;
770 if(ALSource
->WetGainHFAuto
)
771 WetGainHF
[i
] *= ConeHF
;
773 // Clamp to Min/Max Gain
774 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
775 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
777 if(Slot
->effect
.type
== AL_EFFECT_REVERB
||
778 Slot
->effect
.type
== AL_EFFECT_EAXREVERB
)
780 /* Apply a decay-time transformation to the wet path, based on
781 * the attenuation of the dry path.
783 * Using the approximate (effective) source to listener
784 * distance, the initial decay of the reverb effect is
785 * calculated and applied to the wet path.
787 WetGain
[i
] *= aluPow(10.0f
, effectiveDist
/
788 (SPEEDOFSOUNDMETRESPERSEC
*
789 Slot
->effect
.Reverb
.DecayTime
) *
792 WetGainHF
[i
] *= aluPow(10.0f
,
793 log10(Slot
->effect
.Reverb
.AirAbsorptionGainHF
) *
794 ALSource
->AirAbsorptionFactor
* effectiveDist
);
799 /* If the slot's auxiliary send auto is off, the data sent to the
800 * effect slot is the same as the dry path, sans filter effects */
802 WetGainHF
[i
] = DryGainHF
;
805 switch(ALSource
->Send
[i
].WetFilter
.type
)
807 case AL_FILTER_LOWPASS
:
808 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
809 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
812 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
814 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
816 ALSource
->Params
.WetGains
[i
] = 0.0f
;
820 // Apply filter gains and filters
821 switch(ALSource
->DirectFilter
.type
)
823 case AL_FILTER_LOWPASS
:
824 DryMix
*= ALSource
->DirectFilter
.Gain
;
825 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
828 DryMix
*= ListenerGain
;
830 // Calculate Velocity
831 if(DopplerFactor
!= 0.0f
)
833 ALfloat flVSS
, flVLS
;
834 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
837 flVSS
= aluDotproduct(Velocity
, SourceToListener
);
838 if(flVSS
>= flMaxVelocity
)
839 flVSS
= (flMaxVelocity
- 1.0f
);
840 else if(flVSS
<= -flMaxVelocity
)
841 flVSS
= -flMaxVelocity
+ 1.0f
;
843 flVLS
= aluDotproduct(ListenerVel
, SourceToListener
);
844 if(flVLS
>= flMaxVelocity
)
845 flVLS
= (flMaxVelocity
- 1.0f
);
846 else if(flVLS
<= -flMaxVelocity
)
847 flVLS
= -flMaxVelocity
+ 1.0f
;
849 ALSource
->Params
.Pitch
= ALSource
->flPitch
*
850 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
851 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
854 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
856 // Use energy-preserving panning algorithm for multi-speaker playback
857 length
= __max(OrigDist
, MinDist
);
860 ALfloat invlen
= 1.0f
/length
;
861 Position
[0] *= invlen
;
862 Position
[1] *= invlen
;
863 Position
[2] *= invlen
;
866 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
867 SpeakerGain
= &Device
->PanningLUT
[OUTPUTCHANNELS
* pos
];
869 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
870 // elevation adjustment for directional gain. this sucks, but
871 // has low complexity
872 AmbientGain
= 1.0/aluSqrt(Device
->NumChan
) * (1.0-DirGain
);
873 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
874 ALSource
->Params
.DryGains
[s
] = 0.0f
;
875 for(s
= 0;s
< (ALsizei
)Device
->NumChan
;s
++)
877 Channel chan
= Device
->Speaker2Chan
[s
];
878 ALfloat gain
= SpeakerGain
[chan
]*DirGain
+ AmbientGain
;
879 ALSource
->Params
.DryGains
[chan
] = DryMix
* gain
;
882 /* Update filter coefficients. */
883 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
885 /* Spatialized sources use four chained one-pole filters, so we need to
886 * take the fourth root of the squared gain, which is the same as the
887 * square root of the base gain. */
888 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(aluSqrt(DryGainHF
), cw
);
890 for(i
= 0;i
< NumSends
;i
++)
892 /* The wet path uses two chained one-pole filters, so take the
893 * base gain (square root of the squared gain) */
894 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= lpCoeffCalc(WetGainHF
[i
], cw
);
898 static __inline ALfloat
point(ALfloat val1
, ALfloat val2
, ALint frac
)
904 static __inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALint frac
)
906 return val1
+ ((val2
-val1
)*(frac
* (1.0f
/(1<<FRACTIONBITS
))));
908 static __inline ALfloat
cos_lerp(ALfloat val1
, ALfloat val2
, ALint frac
)
910 ALfloat mult
= (1.0f
-cos(frac
* (1.0f
/(1<<FRACTIONBITS
)) * M_PI
)) * 0.5f
;
911 return val1
+ ((val2
-val1
)*mult
);
914 static void MixSomeSources(ALCcontext
*ALContext
, float (*DryBuffer
)[OUTPUTCHANNELS
], ALuint SamplesToDo
)
916 static float DummyBuffer
[BUFFERSIZE
];
917 ALfloat
*WetBuffer
[MAX_SENDS
];
918 ALfloat DrySend
[OUTPUTCHANNELS
];
919 ALfloat dryGainStep
[OUTPUTCHANNELS
];
920 ALfloat wetGainStep
[MAX_SENDS
];
923 ALfloat value
, outsamp
;
924 ALbufferlistitem
*BufferListItem
;
925 ALint64 DataSize64
,DataPos64
;
926 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
927 ALfloat WetSend
[MAX_SENDS
];
929 ALboolean DuplicateStereo
;
932 ALuint DataPosInt
, DataPosFrac
;
933 ALuint Channels
, Bytes
;
935 ALuint LoopStart
, LoopEnd
;
936 resampler_t Resampler
;
937 ALuint BuffersPlayed
;
943 if(ALContext
->SourceMap
.size
<= 0)
946 DuplicateStereo
= ALContext
->Device
->DuplicateStereo
;
947 DeviceFreq
= ALContext
->Device
->Frequency
;
949 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
950 rampLength
= max(rampLength
, SamplesToDo
);
952 pos
= ALContext
->SourceMap
.size
;
957 ALSource
= ALContext
->SourceMap
.array
[pos
].value
;
958 } while(ALSource
->state
!= AL_PLAYING
);
961 /* Find buffer format */
965 BufferListItem
= ALSource
->queue
;
966 while(BufferListItem
!= NULL
)
969 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
971 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
972 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
973 Frequency
= ALBuffer
->frequency
;
976 BufferListItem
= BufferListItem
->next
;
979 if(ALSource
->NeedsUpdate
)
981 //Only apply 3D calculations for mono buffers
983 CalcSourceParams(ALContext
, ALSource
);
985 CalcNonAttnSourceParams(ALContext
, ALSource
);
986 ALSource
->NeedsUpdate
= AL_FALSE
;
989 /* Get source info */
990 Resampler
= ALSource
->Resampler
;
991 State
= ALSource
->state
;
992 BuffersPlayed
= ALSource
->BuffersPlayed
;
993 DataPosInt
= ALSource
->position
;
994 DataPosFrac
= ALSource
->position_fraction
;
995 Looping
= ALSource
->bLooping
;
996 LoopStart
= ALSource
->LoopStart
;
997 LoopEnd
= ALSource
->LoopEnd
;
999 /* Compute 18.14 fixed point step */
1000 Pitch
= (ALSource
->Params
.Pitch
*Frequency
) / DeviceFreq
;
1001 if(Pitch
> (float)MAX_PITCH
) Pitch
= (float)MAX_PITCH
;
1002 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
1003 if(increment
<= 0) increment
= (1<<FRACTIONBITS
);
1005 if(ALSource
->FirstStart
)
1007 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1008 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
1009 for(i
= 0;i
< MAX_SENDS
;i
++)
1010 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
1014 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1015 DrySend
[i
] = ALSource
->DryGains
[i
];
1016 for(i
= 0;i
< MAX_SENDS
;i
++)
1017 WetSend
[i
] = ALSource
->WetGains
[i
];
1020 DryFilter
= &ALSource
->Params
.iirFilter
;
1021 for(i
= 0;i
< MAX_SENDS
;i
++)
1023 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
1024 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
1025 ALSource
->Send
[i
].Slot
->WetBuffer
:
1029 /* Get current buffer queue item */
1030 BufferListItem
= ALSource
->queue
;
1031 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
1032 BufferListItem
= BufferListItem
->next
;
1034 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
1036 ALuint DataSize
= 0;
1041 /* Get buffer info */
1042 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
1044 Data
= ALBuffer
->data
;
1045 DataSize
= ALBuffer
->size
;
1046 DataSize
/= Channels
* Bytes
;
1048 if(DataPosInt
>= DataSize
)
1051 if(Looping
&& ALSource
->lSourceType
== AL_STATIC
)
1053 /* If current offset is beyond the loop range, do not loop */
1054 if(DataPosInt
>= LoopEnd
)
1057 if(!Looping
|| ALSource
->lSourceType
!= AL_STATIC
)
1059 /* Non-looping and non-static sources ignore loop points */
1064 if(BufferListItem
->next
)
1066 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
1067 if(NextBuf
&& NextBuf
->size
)
1069 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1070 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1071 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1076 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
1077 if(NextBuf
&& NextBuf
->size
)
1079 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1080 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1081 memcpy(&Data
[DataSize
*Channels
], &NextBuf
->data
[LoopStart
*Channels
], ulExtraSamples
);
1085 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
1087 /* Compute the gain steps for each output channel */
1088 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1089 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-DrySend
[i
]) /
1091 for(i
= 0;i
< MAX_SENDS
;i
++)
1092 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-WetSend
[i
]) /
1095 /* Figure out how many samples we can mix. */
1096 DataSize64
= LoopEnd
;
1097 DataSize64
<<= FRACTIONBITS
;
1098 DataPos64
= DataPosInt
;
1099 DataPos64
<<= FRACTIONBITS
;
1100 DataPos64
+= DataPosFrac
;
1101 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
1103 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
1105 /* Actual sample mixing loop */
1107 Data
+= DataPosInt
*Channels
;
1109 if(Channels
== 1) /* Mono */
1111 #define DO_MIX(resampler) do { \
1112 while(BufferSize--) \
1114 for(i = 0;i < OUTPUTCHANNELS;i++) \
1115 DrySend[i] += dryGainStep[i]; \
1116 for(i = 0;i < MAX_SENDS;i++) \
1117 WetSend[i] += wetGainStep[i]; \
1119 /* First order interpolator */ \
1120 value = (resampler)(Data[k], Data[k+1], DataPosFrac); \
1122 /* Direct path final mix buffer and panning */ \
1123 outsamp = lpFilter4P(DryFilter, 0, value); \
1124 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT]; \
1125 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT]; \
1126 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT]; \
1127 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT]; \
1128 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT]; \
1129 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT]; \
1130 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER]; \
1131 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER]; \
1133 /* Room path final mix buffer and panning */ \
1134 for(i = 0;i < MAX_SENDS;i++) \
1136 outsamp = lpFilter2P(WetFilter[i], 0, value); \
1137 WetBuffer[i][j] += outsamp*WetSend[i]; \
1140 DataPosFrac += increment; \
1141 k += DataPosFrac>>FRACTIONBITS; \
1142 DataPosFrac &= FRACTIONMASK; \
1149 case POINT_RESAMPLER
:
1150 DO_MIX(point
); break;
1151 case LINEAR_RESAMPLER
:
1152 DO_MIX(lerp
); break;
1153 case COSINE_RESAMPLER
:
1154 DO_MIX(cos_lerp
); break;
1161 else if(Channels
== 2 && DuplicateStereo
) /* Stereo */
1163 const int chans
[] = {
1164 FRONT_LEFT
, FRONT_RIGHT
1166 const int chans2
[] = {
1167 BACK_LEFT
, SIDE_LEFT
, BACK_RIGHT
, SIDE_RIGHT
1169 const ALfloat scaler
= 1.0f
/Channels
;
1170 const ALfloat dupscaler
= aluSqrt(1.0f
/3.0f
);
1172 #define DO_MIX(resampler) do { \
1173 while(BufferSize--) \
1175 for(i = 0;i < OUTPUTCHANNELS;i++) \
1176 DrySend[i] += dryGainStep[i]; \
1177 for(i = 0;i < MAX_SENDS;i++) \
1178 WetSend[i] += wetGainStep[i]; \
1180 for(i = 0;i < Channels;i++) \
1182 value = (resampler)(Data[k*Channels + i],Data[(k+1)*Channels + i],\
1184 outsamp = lpFilter2P(DryFilter, chans[i]*2, value) * dupscaler; \
1185 DryBuffer[j][chans[i]] += outsamp*DrySend[chans[i]]; \
1186 DryBuffer[j][chans2[i*2+0]] += outsamp*DrySend[chans2[i*2+0]]; \
1187 DryBuffer[j][chans2[i*2+1]] += outsamp*DrySend[chans2[i*2+1]]; \
1188 for(out = 0;out < MAX_SENDS;out++) \
1190 outsamp = lpFilter1P(WetFilter[out], chans[i], value); \
1191 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1195 DataPosFrac += increment; \
1196 k += DataPosFrac>>FRACTIONBITS; \
1197 DataPosFrac &= FRACTIONMASK; \
1204 case POINT_RESAMPLER
:
1205 DO_MIX(point
); break;
1206 case LINEAR_RESAMPLER
:
1207 DO_MIX(lerp
); break;
1208 case COSINE_RESAMPLER
:
1209 DO_MIX(cos_lerp
); break;
1216 else if(Channels
== 2) /* Stereo */
1218 const int chans
[] = {
1219 FRONT_LEFT
, FRONT_RIGHT
1221 const ALfloat scaler
= 1.0f
/Channels
;
1223 #define DO_MIX(resampler) do { \
1224 while(BufferSize--) \
1226 for(i = 0;i < OUTPUTCHANNELS;i++) \
1227 DrySend[i] += dryGainStep[i]; \
1228 for(i = 0;i < MAX_SENDS;i++) \
1229 WetSend[i] += wetGainStep[i]; \
1231 for(i = 0;i < Channels;i++) \
1233 value = (resampler)(Data[k*Channels + i],Data[(k+1)*Channels + i],\
1235 outsamp = lpFilter2P(DryFilter, chans[i]*2, value); \
1236 DryBuffer[j][chans[i]] += outsamp*DrySend[chans[i]]; \
1237 for(out = 0;out < MAX_SENDS;out++) \
1239 outsamp = lpFilter1P(WetFilter[out], chans[i], value); \
1240 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1244 DataPosFrac += increment; \
1245 k += DataPosFrac>>FRACTIONBITS; \
1246 DataPosFrac &= FRACTIONMASK; \
1253 case POINT_RESAMPLER
:
1254 DO_MIX(point
); break;
1255 case LINEAR_RESAMPLER
:
1256 DO_MIX(lerp
); break;
1257 case COSINE_RESAMPLER
:
1258 DO_MIX(cos_lerp
); break;
1264 else if(Channels
== 4) /* Quad */
1266 const int chans
[] = {
1267 FRONT_LEFT
, FRONT_RIGHT
,
1268 BACK_LEFT
, BACK_RIGHT
1270 const ALfloat scaler
= 1.0f
/Channels
;
1274 case POINT_RESAMPLER
:
1275 DO_MIX(point
); break;
1276 case LINEAR_RESAMPLER
:
1277 DO_MIX(lerp
); break;
1278 case COSINE_RESAMPLER
:
1279 DO_MIX(cos_lerp
); break;
1285 else if(Channels
== 6) /* 5.1 */
1287 const int chans
[] = {
1288 FRONT_LEFT
, FRONT_RIGHT
,
1290 BACK_LEFT
, BACK_RIGHT
1292 const ALfloat scaler
= 1.0f
/Channels
;
1296 case POINT_RESAMPLER
:
1297 DO_MIX(point
); break;
1298 case LINEAR_RESAMPLER
:
1299 DO_MIX(lerp
); break;
1300 case COSINE_RESAMPLER
:
1301 DO_MIX(cos_lerp
); break;
1307 else if(Channels
== 7) /* 6.1 */
1309 const int chans
[] = {
1310 FRONT_LEFT
, FRONT_RIGHT
,
1313 SIDE_LEFT
, SIDE_RIGHT
1315 const ALfloat scaler
= 1.0f
/Channels
;
1319 case POINT_RESAMPLER
:
1320 DO_MIX(point
); break;
1321 case LINEAR_RESAMPLER
:
1322 DO_MIX(lerp
); break;
1323 case COSINE_RESAMPLER
:
1324 DO_MIX(cos_lerp
); break;
1330 else if(Channels
== 8) /* 7.1 */
1332 const int chans
[] = {
1333 FRONT_LEFT
, FRONT_RIGHT
,
1335 BACK_LEFT
, BACK_RIGHT
,
1336 SIDE_LEFT
, SIDE_RIGHT
1338 const ALfloat scaler
= 1.0f
/Channels
;
1342 case POINT_RESAMPLER
:
1343 DO_MIX(point
); break;
1344 case LINEAR_RESAMPLER
:
1345 DO_MIX(lerp
); break;
1346 case COSINE_RESAMPLER
:
1347 DO_MIX(cos_lerp
); break;
1356 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1357 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1358 for(i
= 0;i
< MAX_SENDS
;i
++)
1359 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1362 DataPosFrac
+= increment
;
1363 k
+= DataPosFrac
>>FRACTIONBITS
;
1364 DataPosFrac
&= FRACTIONMASK
;
1371 /* Handle looping sources */
1372 if(DataPosInt
>= LoopEnd
)
1374 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1376 BufferListItem
= BufferListItem
->next
;
1378 DataPosInt
-= DataSize
;
1382 BufferListItem
= ALSource
->queue
;
1384 if(ALSource
->lSourceType
== AL_STATIC
)
1385 DataPosInt
= ((DataPosInt
-LoopStart
)%(LoopEnd
-LoopStart
)) + LoopStart
;
1387 DataPosInt
-= DataSize
;
1392 BufferListItem
= ALSource
->queue
;
1393 BuffersPlayed
= ALSource
->BuffersInQueue
;
1400 /* Update source info */
1401 ALSource
->state
= State
;
1402 ALSource
->BuffersPlayed
= BuffersPlayed
;
1403 ALSource
->position
= DataPosInt
;
1404 ALSource
->position_fraction
= DataPosFrac
;
1405 ALSource
->Buffer
= BufferListItem
->buffer
;
1407 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1408 ALSource
->DryGains
[i
] = DrySend
[i
];
1409 for(i
= 0;i
< MAX_SENDS
;i
++)
1410 ALSource
->WetGains
[i
] = WetSend
[i
];
1412 ALSource
->FirstStart
= AL_FALSE
;
1417 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1419 float (*DryBuffer
)[OUTPUTCHANNELS
];
1420 ALfloat (*Matrix
)[OUTPUTCHANNELS
];
1421 const ALuint
*ChanMap
;
1423 ALeffectslot
*ALEffectSlot
;
1424 ALCcontext
*ALContext
;
1430 #if defined(HAVE_FESETROUND)
1431 fpuState
= fegetround();
1432 fesetround(FE_TOWARDZERO
);
1433 #elif defined(HAVE__CONTROLFP)
1434 fpuState
= _controlfp(0, 0);
1435 _controlfp(_RC_CHOP
, _MCW_RC
);
1440 DryBuffer
= device
->DryBuffer
;
1443 /* Setup variables */
1444 SamplesToDo
= min(size
, BUFFERSIZE
);
1446 /* Clear mixing buffer */
1447 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1449 SuspendContext(NULL
);
1450 for(c
= 0;c
< device
->NumContexts
;c
++)
1452 ALContext
= device
->Contexts
[c
];
1453 SuspendContext(ALContext
);
1455 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1457 /* effect slot processing */
1458 for(e
= 0;e
< ALContext
->EffectSlotMap
.size
;e
++)
1460 ALEffectSlot
= ALContext
->EffectSlotMap
.array
[e
].value
;
1461 if(ALEffectSlot
->EffectState
)
1462 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1464 for(i
= 0;i
< SamplesToDo
;i
++)
1465 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1467 ProcessContext(ALContext
);
1469 ProcessContext(NULL
);
1471 //Post processing loop
1472 ChanMap
= device
->DevChannels
;
1473 Matrix
= device
->ChannelMatrix
;
1474 switch(device
->Format
)
1476 #define CHECK_WRITE_FORMAT(bits, type, func) \
1477 case AL_FORMAT_MONO##bits: \
1478 for(i = 0;i < SamplesToDo;i++) \
1481 for(c = 0;c < OUTPUTCHANNELS;c++) \
1482 samp += DryBuffer[i][c] * Matrix[c][FRONT_CENTER]; \
1483 ((type*)buffer)[ChanMap[FRONT_CENTER]] = (func)(samp); \
1484 buffer = ((type*)buffer) + 1; \
1487 case AL_FORMAT_STEREO##bits: \
1490 for(i = 0;i < SamplesToDo;i++) \
1492 float samples[2] = { 0.0f, 0.0f }; \
1493 for(c = 0;c < OUTPUTCHANNELS;c++) \
1495 samples[0] += DryBuffer[i][c]*Matrix[c][FRONT_LEFT]; \
1496 samples[1] += DryBuffer[i][c]*Matrix[c][FRONT_RIGHT]; \
1498 bs2b_cross_feed(device->Bs2b, samples); \
1499 ((type*)buffer)[ChanMap[FRONT_LEFT]] = (func)(samples[0]);\
1500 ((type*)buffer)[ChanMap[FRONT_RIGHT]]= (func)(samples[1]);\
1501 buffer = ((type*)buffer) + 2; \
1506 for(i = 0;i < SamplesToDo;i++) \
1508 static const Channel chans[] = { \
1509 FRONT_LEFT, FRONT_RIGHT \
1511 for(j = 0;j < 2;j++) \
1514 for(c = 0;c < OUTPUTCHANNELS;c++) \
1515 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1516 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1518 buffer = ((type*)buffer) + 2; \
1522 case AL_FORMAT_QUAD##bits: \
1523 for(i = 0;i < SamplesToDo;i++) \
1525 static const Channel chans[] = { \
1526 FRONT_LEFT, FRONT_RIGHT, \
1527 BACK_LEFT, BACK_RIGHT, \
1529 for(j = 0;j < 4;j++) \
1532 for(c = 0;c < OUTPUTCHANNELS;c++) \
1533 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1534 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1536 buffer = ((type*)buffer) + 4; \
1539 case AL_FORMAT_51CHN##bits: \
1540 for(i = 0;i < SamplesToDo;i++) \
1542 static const Channel chans[] = { \
1543 FRONT_LEFT, FRONT_RIGHT, \
1544 FRONT_CENTER, LFE, \
1545 BACK_LEFT, BACK_RIGHT, \
1547 for(j = 0;j < 6;j++) \
1550 for(c = 0;c < OUTPUTCHANNELS;c++) \
1551 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1552 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1554 buffer = ((type*)buffer) + 6; \
1557 case AL_FORMAT_61CHN##bits: \
1558 for(i = 0;i < SamplesToDo;i++) \
1560 static const Channel chans[] = { \
1561 FRONT_LEFT, FRONT_RIGHT, \
1562 FRONT_CENTER, LFE, BACK_CENTER, \
1563 SIDE_LEFT, SIDE_RIGHT, \
1565 for(j = 0;j < 7;j++) \
1568 for(c = 0;c < OUTPUTCHANNELS;c++) \
1569 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1570 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1572 buffer = ((type*)buffer) + 7; \
1575 case AL_FORMAT_71CHN##bits: \
1576 for(i = 0;i < SamplesToDo;i++) \
1578 static const Channel chans[] = { \
1579 FRONT_LEFT, FRONT_RIGHT, \
1580 FRONT_CENTER, LFE, \
1581 BACK_LEFT, BACK_RIGHT, \
1582 SIDE_LEFT, SIDE_RIGHT \
1584 for(j = 0;j < 8;j++) \
1587 for(c = 0;c < OUTPUTCHANNELS;c++) \
1588 samp += DryBuffer[i][c] * Matrix[c][chans[j]]; \
1589 ((type*)buffer)[ChanMap[chans[j]]] = (func)(samp); \
1591 buffer = ((type*)buffer) + 8; \
1595 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1596 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1597 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
)
1598 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
)
1599 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
)
1600 #undef AL_FORMAT_STEREO32
1601 #undef AL_FORMAT_MONO32
1602 #undef CHECK_WRITE_FORMAT
1608 size
-= SamplesToDo
;
1611 #if defined(HAVE_FESETROUND)
1612 fesetround(fpuState
);
1613 #elif defined(HAVE__CONTROLFP)
1614 _controlfp(fpuState
, 0xfffff);
1618 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1622 SuspendContext(NULL
);
1623 for(i
= 0;i
< device
->NumContexts
;i
++)
1625 ALCcontext
*Context
= device
->Contexts
[i
];
1629 SuspendContext(Context
);
1631 for(pos
= 0;pos
< Context
->SourceMap
.size
;pos
++)
1633 source
= Context
->SourceMap
.array
[pos
].value
;
1634 if(source
->state
== AL_PLAYING
)
1636 source
->state
= AL_STOPPED
;
1637 source
->BuffersPlayed
= source
->BuffersInQueue
;
1638 source
->position
= 0;
1639 source
->position_fraction
= 0;
1642 ProcessContext(Context
);
1645 device
->Connected
= ALC_FALSE
;
1646 ProcessContext(NULL
);