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
48 ALboolean DuplicateStereo
= AL_FALSE
;
51 static __inline ALfloat
aluF2F(ALfloat Value
)
56 static __inline ALshort
aluF2S(ALfloat Value
)
62 i
= (ALint
)(Value
*32768.0f
);
67 i
= (ALint
)(Value
*32767.0f
);
73 static __inline ALubyte
aluF2UB(ALfloat Value
)
75 ALshort i
= aluF2S(Value
);
80 static __inline ALvoid
aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
82 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
83 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
84 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
87 static __inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
89 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
90 inVector1
[2]*inVector2
[2];
93 static __inline ALvoid
aluNormalize(ALfloat
*inVector
)
95 ALfloat length
, inverse_length
;
97 length
= aluSqrt(aluDotproduct(inVector
, inVector
));
100 inverse_length
= 1.0f
/length
;
101 inVector
[0] *= inverse_length
;
102 inVector
[1] *= inverse_length
;
103 inVector
[2] *= inverse_length
;
107 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat w
,ALfloat matrix
[4][4])
110 vector
[0], vector
[1], vector
[2], w
113 vector
[0] = temp
[0]*matrix
[0][0] + temp
[1]*matrix
[1][0] + temp
[2]*matrix
[2][0] + temp
[3]*matrix
[3][0];
114 vector
[1] = temp
[0]*matrix
[0][1] + temp
[1]*matrix
[1][1] + temp
[2]*matrix
[2][1] + temp
[3]*matrix
[3][1];
115 vector
[2] = temp
[0]*matrix
[0][2] + temp
[1]*matrix
[1][2] + temp
[2]*matrix
[2][2] + temp
[3]*matrix
[3][2];
118 static ALvoid
SetSpeakerArrangement(const char *name
, ALfloat SpeakerAngle
[OUTPUTCHANNELS
],
119 ALint Speaker2Chan
[OUTPUTCHANNELS
], ALint chans
)
121 char layout_str
[256];
122 char *confkey
, *next
;
126 strncpy(layout_str
, GetConfigValue(NULL
, name
, ""), sizeof(layout_str
));
129 next
= confkey
= layout_str
;
133 next
= strchr(confkey
, ',');
139 } while(isspace(*next
) || *next
== ',');
142 sep
= strchr(confkey
, '=');
143 if(!sep
|| confkey
== sep
)
147 while(isspace(*end
) && end
!= confkey
)
151 if(strcmp(confkey
, "fl") == 0 || strcmp(confkey
, "front-left") == 0)
153 else if(strcmp(confkey
, "fr") == 0 || strcmp(confkey
, "front-right") == 0)
155 else if(strcmp(confkey
, "fc") == 0 || strcmp(confkey
, "front-center") == 0)
157 else if(strcmp(confkey
, "bl") == 0 || strcmp(confkey
, "back-left") == 0)
159 else if(strcmp(confkey
, "br") == 0 || strcmp(confkey
, "back-right") == 0)
161 else if(strcmp(confkey
, "bc") == 0 || strcmp(confkey
, "back-center") == 0)
163 else if(strcmp(confkey
, "sl") == 0 || strcmp(confkey
, "side-left") == 0)
165 else if(strcmp(confkey
, "sr") == 0 || strcmp(confkey
, "side-right") == 0)
169 AL_PRINT("Unknown speaker for %s: \"%s\"\n", name
, confkey
);
177 for(i
= 0;i
< chans
;i
++)
179 if(Speaker2Chan
[i
] == val
)
181 val
= strtol(sep
, NULL
, 10);
182 if(val
>= -180 && val
<= 180)
183 SpeakerAngle
[i
] = val
* M_PI
/180.0f
;
185 AL_PRINT("Invalid angle for speaker \"%s\": %d\n", confkey
, val
);
191 for(i
= 1;i
< chans
;i
++)
193 if(SpeakerAngle
[i
] <= SpeakerAngle
[i
-1])
195 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i
, chans
,
196 SpeakerAngle
[i
-1] * 180.0f
/M_PI
, SpeakerAngle
[i
] * 180.0f
/M_PI
);
197 SpeakerAngle
[i
] = SpeakerAngle
[i
-1] + 1 * M_PI
/180.0f
;
202 static __inline ALfloat
aluLUTpos2Angle(ALint pos
)
204 if(pos
< QUADRANT_NUM
)
205 return aluAtan((ALfloat
)pos
/ (ALfloat
)(QUADRANT_NUM
- pos
));
206 if(pos
< 2 * QUADRANT_NUM
)
207 return M_PI_2
+ aluAtan((ALfloat
)(pos
- QUADRANT_NUM
) / (ALfloat
)(2 * QUADRANT_NUM
- pos
));
208 if(pos
< 3 * QUADRANT_NUM
)
209 return aluAtan((ALfloat
)(pos
- 2 * QUADRANT_NUM
) / (ALfloat
)(3 * QUADRANT_NUM
- pos
)) - M_PI
;
210 return aluAtan((ALfloat
)(pos
- 3 * QUADRANT_NUM
) / (ALfloat
)(4 * QUADRANT_NUM
- pos
)) - M_PI_2
;
213 ALvoid
aluInitPanning(ALCcontext
*Context
)
215 ALint pos
, offset
, s
;
216 ALfloat Alpha
, Theta
;
217 ALfloat SpeakerAngle
[OUTPUTCHANNELS
];
218 ALint Speaker2Chan
[OUTPUTCHANNELS
];
220 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
223 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
224 Context
->ChannelMatrix
[s
][s2
] = ((s
==s2
) ? 1.0f
: 0.0f
);
227 switch(Context
->Device
->Format
)
229 case AL_FORMAT_MONO8
:
230 case AL_FORMAT_MONO16
:
231 case AL_FORMAT_MONO_FLOAT32
:
232 Context
->ChannelMatrix
[FRONT_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
233 Context
->ChannelMatrix
[FRONT_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
234 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
235 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
236 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_CENTER
] = aluSqrt(0.5);
237 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_CENTER
] = aluSqrt(0.5);
238 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_CENTER
] = 1.0f
;
239 Context
->NumChan
= 1;
240 Speaker2Chan
[0] = FRONT_CENTER
;
241 SpeakerAngle
[0] = 0.0f
* M_PI
/180.0f
;
244 case AL_FORMAT_STEREO8
:
245 case AL_FORMAT_STEREO16
:
246 case AL_FORMAT_STEREO_FLOAT32
:
247 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
248 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
249 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
250 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
251 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
252 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
253 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
254 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
255 Context
->NumChan
= 2;
256 Speaker2Chan
[0] = FRONT_LEFT
;
257 Speaker2Chan
[1] = FRONT_RIGHT
;
258 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
259 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
260 SetSpeakerArrangement("layout_STEREO", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
263 case AL_FORMAT_QUAD8
:
264 case AL_FORMAT_QUAD16
:
265 case AL_FORMAT_QUAD32
:
266 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
267 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
268 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
269 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
270 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
271 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
272 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
273 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
274 Context
->NumChan
= 4;
275 Speaker2Chan
[0] = BACK_LEFT
;
276 Speaker2Chan
[1] = FRONT_LEFT
;
277 Speaker2Chan
[2] = FRONT_RIGHT
;
278 Speaker2Chan
[3] = BACK_RIGHT
;
279 SpeakerAngle
[0] = -135.0f
* M_PI
/180.0f
;
280 SpeakerAngle
[1] = -45.0f
* M_PI
/180.0f
;
281 SpeakerAngle
[2] = 45.0f
* M_PI
/180.0f
;
282 SpeakerAngle
[3] = 135.0f
* M_PI
/180.0f
;
283 SetSpeakerArrangement("layout_QUAD", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
286 case AL_FORMAT_51CHN8
:
287 case AL_FORMAT_51CHN16
:
288 case AL_FORMAT_51CHN32
:
289 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
290 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
291 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
292 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
293 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
294 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
295 Context
->NumChan
= 5;
296 Speaker2Chan
[0] = BACK_LEFT
;
297 Speaker2Chan
[1] = FRONT_LEFT
;
298 Speaker2Chan
[2] = FRONT_CENTER
;
299 Speaker2Chan
[3] = FRONT_RIGHT
;
300 Speaker2Chan
[4] = BACK_RIGHT
;
301 SpeakerAngle
[0] = -110.0f
* M_PI
/180.0f
;
302 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
303 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
304 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
305 SpeakerAngle
[4] = 110.0f
* M_PI
/180.0f
;
306 SetSpeakerArrangement("layout_51CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
309 case AL_FORMAT_61CHN8
:
310 case AL_FORMAT_61CHN16
:
311 case AL_FORMAT_61CHN32
:
312 Context
->ChannelMatrix
[BACK_LEFT
][BACK_CENTER
] = aluSqrt(0.5);
313 Context
->ChannelMatrix
[BACK_LEFT
][SIDE_LEFT
] = aluSqrt(0.5);
314 Context
->ChannelMatrix
[BACK_RIGHT
][BACK_CENTER
] = aluSqrt(0.5);
315 Context
->ChannelMatrix
[BACK_RIGHT
][SIDE_RIGHT
] = aluSqrt(0.5);
316 Context
->NumChan
= 6;
317 Speaker2Chan
[0] = SIDE_LEFT
;
318 Speaker2Chan
[1] = FRONT_LEFT
;
319 Speaker2Chan
[2] = FRONT_CENTER
;
320 Speaker2Chan
[3] = FRONT_RIGHT
;
321 Speaker2Chan
[4] = SIDE_RIGHT
;
322 Speaker2Chan
[5] = BACK_CENTER
;
323 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
324 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
325 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
326 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
327 SpeakerAngle
[4] = 90.0f
* M_PI
/180.0f
;
328 SpeakerAngle
[5] = 180.0f
* M_PI
/180.0f
;
329 SetSpeakerArrangement("layout_61CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
332 case AL_FORMAT_71CHN8
:
333 case AL_FORMAT_71CHN16
:
334 case AL_FORMAT_71CHN32
:
335 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
336 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
337 Context
->NumChan
= 7;
338 Speaker2Chan
[0] = BACK_LEFT
;
339 Speaker2Chan
[1] = SIDE_LEFT
;
340 Speaker2Chan
[2] = FRONT_LEFT
;
341 Speaker2Chan
[3] = FRONT_CENTER
;
342 Speaker2Chan
[4] = FRONT_RIGHT
;
343 Speaker2Chan
[5] = SIDE_RIGHT
;
344 Speaker2Chan
[6] = BACK_RIGHT
;
345 SpeakerAngle
[0] = -150.0f
* M_PI
/180.0f
;
346 SpeakerAngle
[1] = -90.0f
* M_PI
/180.0f
;
347 SpeakerAngle
[2] = -30.0f
* M_PI
/180.0f
;
348 SpeakerAngle
[3] = 0.0f
* M_PI
/180.0f
;
349 SpeakerAngle
[4] = 30.0f
* M_PI
/180.0f
;
350 SpeakerAngle
[5] = 90.0f
* M_PI
/180.0f
;
351 SpeakerAngle
[6] = 150.0f
* M_PI
/180.0f
;
352 SetSpeakerArrangement("layout_71CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
359 for(pos
= 0; pos
< LUT_NUM
; pos
++)
361 /* clear all values */
362 offset
= OUTPUTCHANNELS
* pos
;
363 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
364 Context
->PanningLUT
[offset
+s
] = 0.0f
;
366 if(Context
->NumChan
== 1)
368 Context
->PanningLUT
[offset
+ Speaker2Chan
[0]] = 1.0f
;
373 Theta
= aluLUTpos2Angle(pos
);
375 /* set panning values */
376 for(s
= 0; s
< Context
->NumChan
- 1; s
++)
378 if(Theta
>= SpeakerAngle
[s
] && Theta
< SpeakerAngle
[s
+1])
380 /* source between speaker s and speaker s+1 */
381 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
382 (SpeakerAngle
[s
+1]-SpeakerAngle
[s
]);
383 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
384 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
+1]] = sin(Alpha
);
388 if(s
== Context
->NumChan
- 1)
390 /* source between last and first speaker */
391 if(Theta
< SpeakerAngle
[0])
392 Theta
+= 2.0f
* M_PI
;
393 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
394 (2.0f
* M_PI
+ SpeakerAngle
[0]-SpeakerAngle
[s
]);
395 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
396 Context
->PanningLUT
[offset
+ Speaker2Chan
[0]] = sin(Alpha
);
401 static ALvoid
CalcNonAttnSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
)
403 ALfloat SourceVolume
,ListenerGain
,MinVolume
,MaxVolume
;
404 ALfloat DryGain
, DryGainHF
;
405 ALfloat WetGain
[MAX_SENDS
];
406 ALfloat WetGainHF
[MAX_SENDS
];
407 ALint NumSends
, Frequency
;
411 //Get context properties
412 NumSends
= ALContext
->Device
->NumAuxSends
;
413 Frequency
= ALContext
->Device
->Frequency
;
415 //Get listener properties
416 ListenerGain
= ALContext
->Listener
.Gain
;
418 //Get source properties
419 SourceVolume
= ALSource
->flGain
;
420 MinVolume
= ALSource
->flMinGain
;
421 MaxVolume
= ALSource
->flMaxGain
;
423 //1. Multi-channel buffers always play "normal"
424 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
426 DryGain
= SourceVolume
;
427 DryGain
= __min(DryGain
,MaxVolume
);
428 DryGain
= __max(DryGain
,MinVolume
);
431 switch(ALSource
->DirectFilter
.type
)
433 case AL_FILTER_LOWPASS
:
434 DryGain
*= ALSource
->DirectFilter
.Gain
;
435 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
439 ALSource
->Params
.DryGains
[FRONT_LEFT
] = DryGain
* ListenerGain
;
440 ALSource
->Params
.DryGains
[FRONT_RIGHT
] = DryGain
* ListenerGain
;
441 ALSource
->Params
.DryGains
[SIDE_LEFT
] = DryGain
* ListenerGain
;
442 ALSource
->Params
.DryGains
[SIDE_RIGHT
] = DryGain
* ListenerGain
;
443 ALSource
->Params
.DryGains
[BACK_LEFT
] = DryGain
* ListenerGain
;
444 ALSource
->Params
.DryGains
[BACK_RIGHT
] = DryGain
* ListenerGain
;
445 ALSource
->Params
.DryGains
[FRONT_CENTER
] = DryGain
* ListenerGain
;
446 ALSource
->Params
.DryGains
[BACK_CENTER
] = DryGain
* ListenerGain
;
447 ALSource
->Params
.DryGains
[LFE
] = DryGain
* ListenerGain
;
449 for(i
= 0;i
< NumSends
;i
++)
451 WetGain
[i
] = SourceVolume
;
452 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
453 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
456 switch(ALSource
->Send
[i
].WetFilter
.type
)
458 case AL_FILTER_LOWPASS
:
459 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
460 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
464 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
466 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
468 ALSource
->Params
.WetGains
[i
] = 0.0f
;
472 /* Update filter coefficients. Calculations based on the I3DL2
474 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
476 /* We use two chained one-pole filters, so we need to take the
477 * square root of the squared gain, which is the same as the base
479 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(DryGainHF
, cw
);
481 for(i
= 0;i
< NumSends
;i
++)
483 /* We use a one-pole filter, so we need to take the squared gain */
484 ALfloat a
= lpCoeffCalc(WetGainHF
[i
]*WetGainHF
[i
], cw
);
485 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= a
;
489 static ALvoid
CalcSourceParams(const ALCcontext
*ALContext
, ALsource
*ALSource
)
491 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,DryMix
,OrigDist
;
492 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
493 ALfloat Velocity
[3],ListenerVel
[3];
494 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
,OuterGainHF
;
495 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
496 ALfloat DopplerFactor
, DopplerVelocity
, flSpeedOfSound
;
497 ALfloat Matrix
[4][4];
498 ALfloat flAttenuation
, effectiveDist
;
499 ALfloat RoomAttenuation
[MAX_SENDS
];
500 ALfloat MetersPerUnit
;
501 ALfloat RoomRolloff
[MAX_SENDS
];
502 ALfloat DryGainHF
= 1.0f
;
503 ALfloat WetGain
[MAX_SENDS
];
504 ALfloat WetGainHF
[MAX_SENDS
];
505 ALfloat DirGain
, AmbientGain
;
507 const ALfloat
*SpeakerGain
;
513 for(i
= 0;i
< MAX_SENDS
;i
++)
516 //Get context properties
517 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
518 DopplerVelocity
= ALContext
->DopplerVelocity
;
519 flSpeedOfSound
= ALContext
->flSpeedOfSound
;
520 NumSends
= ALContext
->Device
->NumAuxSends
;
521 Frequency
= ALContext
->Device
->Frequency
;
523 //Get listener properties
524 ListenerGain
= ALContext
->Listener
.Gain
;
525 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
526 memcpy(ListenerVel
, ALContext
->Listener
.Velocity
, sizeof(ALContext
->Listener
.Velocity
));
528 //Get source properties
529 SourceVolume
= ALSource
->flGain
;
530 memcpy(Position
, ALSource
->vPosition
, sizeof(ALSource
->vPosition
));
531 memcpy(Direction
, ALSource
->vOrientation
, sizeof(ALSource
->vOrientation
));
532 memcpy(Velocity
, ALSource
->vVelocity
, sizeof(ALSource
->vVelocity
));
533 MinVolume
= ALSource
->flMinGain
;
534 MaxVolume
= ALSource
->flMaxGain
;
535 MinDist
= ALSource
->flRefDistance
;
536 MaxDist
= ALSource
->flMaxDistance
;
537 Rolloff
= ALSource
->flRollOffFactor
;
538 InnerAngle
= ALSource
->flInnerAngle
;
539 OuterAngle
= ALSource
->flOuterAngle
;
540 OuterGainHF
= ALSource
->OuterGainHF
;
542 //1. Translate Listener to origin (convert to head relative)
543 if(ALSource
->bHeadRelative
==AL_FALSE
)
545 ALfloat U
[3],V
[3],N
[3],P
[3];
547 // Build transform matrix
548 memcpy(N
, ALContext
->Listener
.Forward
, sizeof(N
)); // At-vector
549 aluNormalize(N
); // Normalized At-vector
550 memcpy(V
, ALContext
->Listener
.Up
, sizeof(V
)); // Up-vector
551 aluNormalize(V
); // Normalized Up-vector
552 aluCrossproduct(N
, V
, U
); // Right-vector
553 aluNormalize(U
); // Normalized Right-vector
554 P
[0] = -(ALContext
->Listener
.Position
[0]*U
[0] + // Translation
555 ALContext
->Listener
.Position
[1]*U
[1] +
556 ALContext
->Listener
.Position
[2]*U
[2]);
557 P
[1] = -(ALContext
->Listener
.Position
[0]*V
[0] +
558 ALContext
->Listener
.Position
[1]*V
[1] +
559 ALContext
->Listener
.Position
[2]*V
[2]);
560 P
[2] = -(ALContext
->Listener
.Position
[0]*-N
[0] +
561 ALContext
->Listener
.Position
[1]*-N
[1] +
562 ALContext
->Listener
.Position
[2]*-N
[2]);
563 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0]; Matrix
[0][3] = 0.0f
;
564 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1]; Matrix
[1][3] = 0.0f
;
565 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2]; Matrix
[2][3] = 0.0f
;
566 Matrix
[3][0] = P
[0]; Matrix
[3][1] = P
[1]; Matrix
[3][2] = P
[2]; Matrix
[3][3] = 1.0f
;
568 // Transform source position and direction into listener space
569 aluMatrixVector(Position
, 1.0f
, Matrix
);
570 aluMatrixVector(Direction
, 0.0f
, Matrix
);
571 // Transform source and listener velocity into listener space
572 aluMatrixVector(Velocity
, 0.0f
, Matrix
);
573 aluMatrixVector(ListenerVel
, 0.0f
, Matrix
);
576 ListenerVel
[0] = ListenerVel
[1] = ListenerVel
[2] = 0.0f
;
578 SourceToListener
[0] = -Position
[0];
579 SourceToListener
[1] = -Position
[1];
580 SourceToListener
[2] = -Position
[2];
581 aluNormalize(SourceToListener
);
582 aluNormalize(Direction
);
584 //2. Calculate distance attenuation
585 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
588 flAttenuation
= 1.0f
;
589 for(i
= 0;i
< NumSends
;i
++)
591 RoomAttenuation
[i
] = 1.0f
;
593 RoomRolloff
[i
] = ALSource
->RoomRolloffFactor
;
594 if(ALSource
->Send
[i
].Slot
&&
595 (ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
||
596 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_EAXREVERB
))
597 RoomRolloff
[i
] += ALSource
->Send
[i
].Slot
->effect
.Reverb
.RoomRolloffFactor
;
600 switch(ALContext
->SourceDistanceModel
? ALSource
->DistanceModel
:
601 ALContext
->DistanceModel
)
603 case AL_INVERSE_DISTANCE_CLAMPED
:
604 Distance
=__max(Distance
,MinDist
);
605 Distance
=__min(Distance
,MaxDist
);
606 if(MaxDist
< MinDist
)
609 case AL_INVERSE_DISTANCE
:
612 if((MinDist
+ (Rolloff
* (Distance
- MinDist
))) > 0.0f
)
613 flAttenuation
= MinDist
/ (MinDist
+ (Rolloff
* (Distance
- MinDist
)));
614 for(i
= 0;i
< NumSends
;i
++)
616 if((MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
))) > 0.0f
)
617 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
)));
622 case AL_LINEAR_DISTANCE_CLAMPED
:
623 Distance
=__max(Distance
,MinDist
);
624 Distance
=__min(Distance
,MaxDist
);
625 if(MaxDist
< MinDist
)
628 case AL_LINEAR_DISTANCE
:
629 Distance
=__min(Distance
,MaxDist
);
630 if(MaxDist
!= MinDist
)
632 flAttenuation
= 1.0f
- (Rolloff
*(Distance
-MinDist
)/(MaxDist
- MinDist
));
633 for(i
= 0;i
< NumSends
;i
++)
634 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(Distance
-MinDist
)/(MaxDist
- MinDist
));
638 case AL_EXPONENT_DISTANCE_CLAMPED
:
639 Distance
=__max(Distance
,MinDist
);
640 Distance
=__min(Distance
,MaxDist
);
641 if(MaxDist
< MinDist
)
644 case AL_EXPONENT_DISTANCE
:
645 if(Distance
> 0.0f
&& MinDist
> 0.0f
)
647 flAttenuation
= aluPow(Distance
/MinDist
, -Rolloff
);
648 for(i
= 0;i
< NumSends
;i
++)
649 RoomAttenuation
[i
] = aluPow(Distance
/MinDist
, -RoomRolloff
[i
]);
657 // Source Gain + Attenuation
658 DryMix
= SourceVolume
* flAttenuation
;
659 for(i
= 0;i
< NumSends
;i
++)
660 WetGain
[i
] = SourceVolume
* RoomAttenuation
[i
];
662 effectiveDist
= 0.0f
;
664 effectiveDist
= (MinDist
/flAttenuation
- MinDist
)*MetersPerUnit
;
666 // Distance-based air absorption
667 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& effectiveDist
> 0.0f
)
671 // Absorption calculation is done in dB
672 absorb
= (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
) *
674 // Convert dB to linear gain before applying
675 absorb
= aluPow(10.0f
, absorb
/20.0f
);
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
);
688 else if(Angle
> OuterAngle
)
690 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
));
691 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
));
699 // Apply some high-frequency attenuation for sources behind the listener
700 // NOTE: This should be aluDotproduct({0,0,-1}, ListenerToSource), however
701 // that is equivalent to aluDotproduct({0,0,1}, SourceToListener), which is
702 // the same as SourceToListener[2]
703 Angle
= aluAcos(SourceToListener
[2]) * 180.0f
/M_PI
;
704 // Sources within the minimum distance attenuate less
705 if(OrigDist
< MinDist
)
706 Angle
*= OrigDist
/MinDist
;
709 ALfloat scale
= (Angle
-90.0f
) / (180.1f
-90.0f
); // .1 to account for fp errors
710 ConeHF
*= 1.0f
- (ALContext
->Device
->HeadDampen
*scale
);
713 DryMix
*= ConeVolume
;
714 if(ALSource
->DryGainHFAuto
)
717 // Clamp to Min/Max Gain
718 DryMix
= __min(DryMix
,MaxVolume
);
719 DryMix
= __max(DryMix
,MinVolume
);
721 for(i
= 0;i
< NumSends
;i
++)
723 ALeffectslot
*Slot
= ALSource
->Send
[i
].Slot
;
725 if(!Slot
|| Slot
->effect
.type
== AL_EFFECT_NULL
)
727 ALSource
->Params
.WetGains
[i
] = 0.0f
;
732 if(Slot
->AuxSendAuto
)
734 if(ALSource
->WetGainAuto
)
735 WetGain
[i
] *= ConeVolume
;
736 if(ALSource
->WetGainHFAuto
)
737 WetGainHF
[i
] *= ConeHF
;
739 // Clamp to Min/Max Gain
740 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
741 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
743 if(Slot
->effect
.type
== AL_EFFECT_REVERB
||
744 Slot
->effect
.type
== AL_EFFECT_EAXREVERB
)
746 /* Apply a decay-time transformation to the wet path, based on
747 * the attenuation of the dry path.
749 * Using the approximate (effective) source to listener
750 * distance, the initial decay of the reverb effect is
751 * calculated and applied to the wet path.
753 WetGain
[i
] *= aluPow(10.0f
, effectiveDist
/
754 (SPEEDOFSOUNDMETRESPERSEC
*
755 Slot
->effect
.Reverb
.DecayTime
) *
758 WetGainHF
[i
] *= aluPow(10.0f
,
759 log10(Slot
->effect
.Reverb
.AirAbsorptionGainHF
) *
760 ALSource
->AirAbsorptionFactor
* effectiveDist
);
765 /* If the slot's auxiliary send auto is off, the data sent to the
766 * effect slot is the same as the dry path, sans filter effects */
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
;
780 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
782 ALSource
->Params
.WetGains
[i
] = 0.0f
;
786 // Apply filter gains and filters
787 switch(ALSource
->DirectFilter
.type
)
789 case AL_FILTER_LOWPASS
:
790 DryMix
*= ALSource
->DirectFilter
.Gain
;
791 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
794 DryMix
*= ListenerGain
;
796 // Calculate Velocity
797 if(DopplerFactor
!= 0.0f
)
799 ALfloat flVSS
, flVLS
;
800 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
803 flVSS
= aluDotproduct(Velocity
, SourceToListener
);
804 if(flVSS
>= flMaxVelocity
)
805 flVSS
= (flMaxVelocity
- 1.0f
);
806 else if(flVSS
<= -flMaxVelocity
)
807 flVSS
= -flMaxVelocity
+ 1.0f
;
809 flVLS
= aluDotproduct(ListenerVel
, SourceToListener
);
810 if(flVLS
>= flMaxVelocity
)
811 flVLS
= (flMaxVelocity
- 1.0f
);
812 else if(flVLS
<= -flMaxVelocity
)
813 flVLS
= -flMaxVelocity
+ 1.0f
;
815 ALSource
->Params
.Pitch
= ALSource
->flPitch
*
816 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
817 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
820 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
822 // Use energy-preserving panning algorithm for multi-speaker playback
823 length
= __max(OrigDist
, MinDist
);
826 ALfloat invlen
= 1.0f
/length
;
827 Position
[0] *= invlen
;
828 Position
[1] *= invlen
;
829 Position
[2] *= invlen
;
832 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
833 SpeakerGain
= &ALContext
->PanningLUT
[OUTPUTCHANNELS
* pos
];
835 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
836 // elevation adjustment for directional gain. this sucks, but
837 // has low complexity
838 AmbientGain
= 1.0/aluSqrt(ALContext
->NumChan
) * (1.0-DirGain
);
839 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
841 ALfloat gain
= SpeakerGain
[s
]*DirGain
+ AmbientGain
;
842 ALSource
->Params
.DryGains
[s
] = DryMix
* gain
;
845 /* Update filter coefficients. */
846 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
848 /* Spatialized sources use four chained one-pole filters, so we need to
849 * take the fourth root of the squared gain, which is the same as the
850 * square root of the base gain. */
851 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(aluSqrt(DryGainHF
), cw
);
853 for(i
= 0;i
< NumSends
;i
++)
855 /* The wet path uses two chained one-pole filters, so take the
856 * base gain (square root of the squared gain) */
857 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= lpCoeffCalc(WetGainHF
[i
], cw
);
861 static __inline ALfloat
point(ALfloat val1
, ALfloat val2
, ALint frac
)
867 static __inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALint frac
)
869 return val1
+ ((val2
-val1
)*(frac
* (1.0f
/(1<<FRACTIONBITS
))));
871 static __inline ALfloat
cos_lerp(ALfloat val1
, ALfloat val2
, ALint frac
)
873 ALfloat mult
= (1.0f
-cos(frac
* (1.0f
/(1<<FRACTIONBITS
)) * M_PI
)) * 0.5f
;
874 return val1
+ ((val2
-val1
)*mult
);
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 resampler_t Resampler
;
899 ALuint BuffersPlayed
;
903 if(!(ALSource
=ALContext
->Source
))
906 DeviceFreq
= ALContext
->Device
->Frequency
;
908 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
909 rampLength
= max(rampLength
, SamplesToDo
);
912 if(ALSource
->state
!= AL_PLAYING
)
914 if((ALSource
=ALSource
->next
) != NULL
)
920 /* Find buffer format */
924 BufferListItem
= ALSource
->queue
;
925 while(BufferListItem
!= NULL
)
928 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
930 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
931 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
932 Frequency
= ALBuffer
->frequency
;
935 BufferListItem
= BufferListItem
->next
;
938 if(ALSource
->NeedsUpdate
)
940 //Only apply 3D calculations for mono buffers
942 CalcSourceParams(ALContext
, ALSource
);
944 CalcNonAttnSourceParams(ALContext
, ALSource
);
945 ALSource
->NeedsUpdate
= AL_FALSE
;
948 /* Get source info */
949 Resampler
= ALSource
->Resampler
;
950 State
= ALSource
->state
;
951 BuffersPlayed
= ALSource
->BuffersPlayed
;
952 DataPosInt
= ALSource
->position
;
953 DataPosFrac
= ALSource
->position_fraction
;
955 /* Compute 18.14 fixed point step */
956 Pitch
= (ALSource
->Params
.Pitch
*Frequency
) / DeviceFreq
;
957 if(Pitch
> (float)MAX_PITCH
) Pitch
= (float)MAX_PITCH
;
958 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
959 if(increment
<= 0) increment
= (1<<FRACTIONBITS
);
961 if(ALSource
->FirstStart
)
963 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
964 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
965 for(i
= 0;i
< MAX_SENDS
;i
++)
966 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
970 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
971 DrySend
[i
] = ALSource
->DryGains
[i
];
972 for(i
= 0;i
< MAX_SENDS
;i
++)
973 WetSend
[i
] = ALSource
->WetGains
[i
];
976 DryFilter
= &ALSource
->Params
.iirFilter
;
977 for(i
= 0;i
< MAX_SENDS
;i
++)
979 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
980 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
981 ALSource
->Send
[i
].Slot
->WetBuffer
:
985 if(DuplicateStereo
&& Channels
== 2)
987 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
988 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
989 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
990 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
992 else if(DuplicateStereo
)
994 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
995 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
996 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
997 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
1000 /* Get current buffer queue item */
1001 BufferListItem
= ALSource
->queue
;
1002 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
1003 BufferListItem
= BufferListItem
->next
;
1005 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
1007 ALuint DataSize
= 0;
1012 /* Get buffer info */
1013 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
1015 Data
= ALBuffer
->data
;
1016 DataSize
= ALBuffer
->size
;
1017 DataSize
/= Channels
* Bytes
;
1019 if(DataPosInt
>= DataSize
)
1022 if(BufferListItem
->next
)
1024 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
1025 if(NextBuf
&& NextBuf
->size
)
1027 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1028 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1029 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1032 else if(ALSource
->bLooping
)
1034 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
1035 if(NextBuf
&& NextBuf
->size
)
1037 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1038 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1039 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1043 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
1045 /* Compute the gain steps for each output channel */
1046 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1047 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-DrySend
[i
]) /
1049 for(i
= 0;i
< MAX_SENDS
;i
++)
1050 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-WetSend
[i
]) /
1053 /* Figure out how many samples we can mix. */
1054 DataSize64
= DataSize
;
1055 DataSize64
<<= FRACTIONBITS
;
1056 DataPos64
= DataPosInt
;
1057 DataPos64
<<= FRACTIONBITS
;
1058 DataPos64
+= DataPosFrac
;
1059 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
1061 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
1063 /* Actual sample mixing loop */
1065 Data
+= DataPosInt
*Channels
;
1067 if(Channels
== 1) /* Mono */
1069 #define DO_MIX(resampler) do { \
1070 while(BufferSize--) \
1072 for(i = 0;i < OUTPUTCHANNELS;i++) \
1073 DrySend[i] += dryGainStep[i]; \
1074 for(i = 0;i < MAX_SENDS;i++) \
1075 WetSend[i] += wetGainStep[i]; \
1077 /* First order interpolator */ \
1078 value = (resampler)(Data[k], Data[k+1], DataPosFrac); \
1080 /* Direct path final mix buffer and panning */ \
1081 outsamp = lpFilter4P(DryFilter, 0, value); \
1082 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT]; \
1083 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT]; \
1084 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT]; \
1085 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT]; \
1086 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT]; \
1087 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT]; \
1088 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER]; \
1089 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER]; \
1091 /* Room path final mix buffer and panning */ \
1092 for(i = 0;i < MAX_SENDS;i++) \
1094 outsamp = lpFilter2P(WetFilter[i], 0, value); \
1095 WetBuffer[i][j] += outsamp*WetSend[i]; \
1098 DataPosFrac += increment; \
1099 k += DataPosFrac>>FRACTIONBITS; \
1100 DataPosFrac &= FRACTIONMASK; \
1107 case POINT_RESAMPLER
:
1108 DO_MIX(point
); break;
1109 case LINEAR_RESAMPLER
:
1110 DO_MIX(lerp
); break;
1111 case COSINE_RESAMPLER
:
1112 DO_MIX(cos_lerp
); break;
1119 else if(Channels
== 2) /* Stereo */
1121 const int chans
[] = {
1122 FRONT_LEFT
, FRONT_RIGHT
1124 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1126 #define DO_MIX(resampler) do { \
1127 while(BufferSize--) \
1129 for(i = 0;i < OUTPUTCHANNELS;i++) \
1130 DrySend[i] += dryGainStep[i]; \
1131 for(i = 0;i < MAX_SENDS;i++) \
1132 WetSend[i] += wetGainStep[i]; \
1134 for(i = 0;i < Channels;i++) \
1136 value = (resampler)(Data[k*Channels + i], Data[(k+1)*Channels + i], \
1138 outsamp = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1139 for(out = 0;out < OUTPUTCHANNELS;out++) \
1140 DryBuffer[j][out] += outsamp*Matrix[chans[i]][out]; \
1141 for(out = 0;out < MAX_SENDS;out++) \
1143 outsamp = lpFilter1P(WetFilter[out], chans[i], value); \
1144 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1148 DataPosFrac += increment; \
1149 k += DataPosFrac>>FRACTIONBITS; \
1150 DataPosFrac &= FRACTIONMASK; \
1157 case POINT_RESAMPLER
:
1158 DO_MIX(point
); break;
1159 case LINEAR_RESAMPLER
:
1160 DO_MIX(lerp
); break;
1161 case COSINE_RESAMPLER
:
1162 DO_MIX(cos_lerp
); break;
1168 else if(Channels
== 4) /* Quad */
1170 const int chans
[] = {
1171 FRONT_LEFT
, FRONT_RIGHT
,
1172 BACK_LEFT
, BACK_RIGHT
1174 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1178 case POINT_RESAMPLER
:
1179 DO_MIX(point
); break;
1180 case LINEAR_RESAMPLER
:
1181 DO_MIX(lerp
); break;
1182 case COSINE_RESAMPLER
:
1183 DO_MIX(cos_lerp
); break;
1189 else if(Channels
== 6) /* 5.1 */
1191 const int chans
[] = {
1192 FRONT_LEFT
, FRONT_RIGHT
,
1194 BACK_LEFT
, BACK_RIGHT
1196 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1200 case POINT_RESAMPLER
:
1201 DO_MIX(point
); break;
1202 case LINEAR_RESAMPLER
:
1203 DO_MIX(lerp
); break;
1204 case COSINE_RESAMPLER
:
1205 DO_MIX(cos_lerp
); break;
1211 else if(Channels
== 7) /* 6.1 */
1213 const int chans
[] = {
1214 FRONT_LEFT
, FRONT_RIGHT
,
1217 SIDE_LEFT
, SIDE_RIGHT
1219 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1223 case POINT_RESAMPLER
:
1224 DO_MIX(point
); break;
1225 case LINEAR_RESAMPLER
:
1226 DO_MIX(lerp
); break;
1227 case COSINE_RESAMPLER
:
1228 DO_MIX(cos_lerp
); break;
1234 else if(Channels
== 8) /* 7.1 */
1236 const int chans
[] = {
1237 FRONT_LEFT
, FRONT_RIGHT
,
1239 BACK_LEFT
, BACK_RIGHT
,
1240 SIDE_LEFT
, SIDE_RIGHT
1242 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1246 case POINT_RESAMPLER
:
1247 DO_MIX(point
); break;
1248 case LINEAR_RESAMPLER
:
1249 DO_MIX(lerp
); break;
1250 case COSINE_RESAMPLER
:
1251 DO_MIX(cos_lerp
); break;
1260 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1261 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1262 for(i
= 0;i
< MAX_SENDS
;i
++)
1263 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1266 DataPosFrac
+= increment
;
1267 k
+= DataPosFrac
>>FRACTIONBITS
;
1268 DataPosFrac
&= FRACTIONMASK
;
1275 /* Handle looping sources */
1276 if(DataPosInt
>= DataSize
)
1278 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1280 BufferListItem
= BufferListItem
->next
;
1282 DataPosInt
-= DataSize
;
1284 else if(ALSource
->bLooping
)
1286 BufferListItem
= ALSource
->queue
;
1288 if(ALSource
->BuffersInQueue
== 1)
1289 DataPosInt
%= DataSize
;
1291 DataPosInt
-= DataSize
;
1296 BufferListItem
= ALSource
->queue
;
1297 BuffersPlayed
= ALSource
->BuffersInQueue
;
1304 /* Update source info */
1305 ALSource
->state
= State
;
1306 ALSource
->BuffersPlayed
= BuffersPlayed
;
1307 ALSource
->position
= DataPosInt
;
1308 ALSource
->position_fraction
= DataPosFrac
;
1309 ALSource
->Buffer
= BufferListItem
->buffer
;
1311 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1312 ALSource
->DryGains
[i
] = DrySend
[i
];
1313 for(i
= 0;i
< MAX_SENDS
;i
++)
1314 ALSource
->WetGains
[i
] = WetSend
[i
];
1316 ALSource
->FirstStart
= AL_FALSE
;
1318 if((ALSource
=ALSource
->next
) != NULL
)
1319 goto another_source
;
1322 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1324 float (*DryBuffer
)[OUTPUTCHANNELS
];
1325 const Channel
*ChanMap
;
1327 ALeffectslot
*ALEffectSlot
;
1328 ALCcontext
*ALContext
;
1332 #if defined(HAVE_FESETROUND)
1333 fpuState
= fegetround();
1334 fesetround(FE_TOWARDZERO
);
1335 #elif defined(HAVE__CONTROLFP)
1336 fpuState
= _controlfp(0, 0);
1337 _controlfp(_RC_CHOP
, _MCW_RC
);
1342 DryBuffer
= device
->DryBuffer
;
1345 /* Setup variables */
1346 SamplesToDo
= min(size
, BUFFERSIZE
);
1348 /* Clear mixing buffer */
1349 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1351 SuspendContext(NULL
);
1352 for(c
= 0;c
< device
->NumContexts
;c
++)
1354 ALContext
= device
->Contexts
[c
];
1355 SuspendContext(ALContext
);
1357 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1359 /* effect slot processing */
1360 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1363 if(ALEffectSlot
->EffectState
)
1364 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1366 for(i
= 0;i
< SamplesToDo
;i
++)
1367 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1368 ALEffectSlot
= ALEffectSlot
->next
;
1370 ProcessContext(ALContext
);
1372 ProcessContext(NULL
);
1374 //Post processing loop
1375 ChanMap
= device
->DevChannels
;
1376 switch(device
->Format
)
1378 #define CHECK_WRITE_FORMAT(bits, type, func) \
1379 case AL_FORMAT_MONO##bits: \
1380 for(i = 0;i < SamplesToDo;i++) \
1382 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1383 buffer = ((type*)buffer) + 1; \
1386 case AL_FORMAT_STEREO##bits: \
1389 for(i = 0;i < SamplesToDo;i++) \
1392 samples[0] = DryBuffer[i][ChanMap[0]]; \
1393 samples[1] = DryBuffer[i][ChanMap[1]]; \
1394 bs2b_cross_feed(device->Bs2b, samples); \
1395 ((type*)buffer)[0] = (func)(samples[0]); \
1396 ((type*)buffer)[1] = (func)(samples[1]); \
1397 buffer = ((type*)buffer) + 2; \
1402 for(i = 0;i < SamplesToDo;i++) \
1404 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1405 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1406 buffer = ((type*)buffer) + 2; \
1410 case AL_FORMAT_QUAD##bits: \
1411 for(i = 0;i < SamplesToDo;i++) \
1413 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1414 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1415 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1416 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1417 buffer = ((type*)buffer) + 4; \
1420 case AL_FORMAT_51CHN##bits: \
1421 for(i = 0;i < SamplesToDo;i++) \
1423 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1424 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1425 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1426 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1427 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1428 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1429 buffer = ((type*)buffer) + 6; \
1432 case AL_FORMAT_61CHN##bits: \
1433 for(i = 0;i < SamplesToDo;i++) \
1435 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1436 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1437 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1438 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1439 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1440 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1441 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1442 buffer = ((type*)buffer) + 7; \
1445 case AL_FORMAT_71CHN##bits: \
1446 for(i = 0;i < SamplesToDo;i++) \
1448 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1449 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1450 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1451 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1452 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1453 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1454 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1455 ((type*)buffer)[7] = (func)(DryBuffer[i][ChanMap[7]]); \
1456 buffer = ((type*)buffer) + 8; \
1460 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1461 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1462 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
)
1463 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
)
1464 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
)
1465 #undef AL_FORMAT_STEREO32
1466 #undef AL_FORMAT_MONO32
1467 #undef CHECK_WRITE_FORMAT
1473 size
-= SamplesToDo
;
1476 #if defined(HAVE_FESETROUND)
1477 fesetround(fpuState
);
1478 #elif defined(HAVE__CONTROLFP)
1479 _controlfp(fpuState
, 0xfffff);
1483 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1487 SuspendContext(NULL
);
1488 for(i
= 0;i
< device
->NumContexts
;i
++)
1492 SuspendContext(device
->Contexts
[i
]);
1494 source
= device
->Contexts
[i
]->Source
;
1497 if(source
->state
== AL_PLAYING
)
1499 source
->state
= AL_STOPPED
;
1500 source
->BuffersPlayed
= source
->BuffersInQueue
;
1501 source
->position
= 0;
1502 source
->position_fraction
= 0;
1504 source
= source
->next
;
1506 ProcessContext(device
->Contexts
[i
]);
1509 device
->Connected
= ALC_FALSE
;
1510 ProcessContext(NULL
);