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 * 180.0f
/M_PI
;
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
= (ALfloat
)pow(Distance
/MinDist
, -Rolloff
);
648 for(i
= 0;i
< NumSends
;i
++)
649 RoomAttenuation
[i
] = (ALfloat
)pow(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
= pow(10.0, absorb
/20.0);
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 if(Slot
->AuxSendAuto
)
729 if(ALSource
->WetGainAuto
)
730 WetGain
[i
] *= ConeVolume
;
731 if(ALSource
->WetGainHFAuto
)
732 WetGainHF
[i
] *= ConeHF
;
734 // Clamp to Min/Max Gain
735 WetGain
[i
] = __min(WetGain
[i
],MaxVolume
);
736 WetGain
[i
] = __max(WetGain
[i
],MinVolume
);
738 if(Slot
->effect
.type
== AL_EFFECT_REVERB
||
739 Slot
->effect
.type
== AL_EFFECT_EAXREVERB
)
741 /* Apply a decay-time transformation to the wet path,
742 * based on the attenuation of the dry path.
744 * Using the approximate (effective) source to listener
745 * distance, the initial decay of the reverb effect is
746 * calculated and applied to the wet path.
748 WetGain
[i
] *= pow(10.0, effectiveDist
/
749 (SPEEDOFSOUNDMETRESPERSEC
*
750 Slot
->effect
.Reverb
.DecayTime
) *
753 WetGainHF
[i
] *= pow(10.0,
754 log10(Slot
->effect
.Reverb
.AirAbsorptionGainHF
) *
755 ALSource
->AirAbsorptionFactor
* effectiveDist
);
760 // If the slot's auxiliary send auto is off, the data sent to
761 // the effect slot is the same as the dry path, sans filter
764 WetGainHF
[i
] = DryGainHF
;
767 switch(ALSource
->Send
[i
].WetFilter
.type
)
769 case AL_FILTER_LOWPASS
:
770 WetGain
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
771 WetGainHF
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
774 ALSource
->Params
.WetGains
[i
] = WetGain
[i
] * ListenerGain
;
778 ALSource
->Params
.WetGains
[i
] = 0.0f
;
782 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
784 ALSource
->Params
.WetGains
[i
] = 0.0f
;
788 // Apply filter gains and filters
789 switch(ALSource
->DirectFilter
.type
)
791 case AL_FILTER_LOWPASS
:
792 DryMix
*= ALSource
->DirectFilter
.Gain
;
793 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
796 DryMix
*= ListenerGain
;
798 // Calculate Velocity
799 if(DopplerFactor
!= 0.0f
)
801 ALfloat flVSS
, flVLS
;
802 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
805 flVSS
= aluDotproduct(Velocity
, SourceToListener
);
806 if(flVSS
>= flMaxVelocity
)
807 flVSS
= (flMaxVelocity
- 1.0f
);
808 else if(flVSS
<= -flMaxVelocity
)
809 flVSS
= -flMaxVelocity
+ 1.0f
;
811 flVLS
= aluDotproduct(ListenerVel
, SourceToListener
);
812 if(flVLS
>= flMaxVelocity
)
813 flVLS
= (flMaxVelocity
- 1.0f
);
814 else if(flVLS
<= -flMaxVelocity
)
815 flVLS
= -flMaxVelocity
+ 1.0f
;
817 ALSource
->Params
.Pitch
= ALSource
->flPitch
*
818 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
819 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
822 ALSource
->Params
.Pitch
= ALSource
->flPitch
;
824 // Use energy-preserving panning algorithm for multi-speaker playback
825 length
= __max(OrigDist
, MinDist
);
828 ALfloat invlen
= 1.0f
/length
;
829 Position
[0] *= invlen
;
830 Position
[1] *= invlen
;
831 Position
[2] *= invlen
;
834 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
835 SpeakerGain
= &ALContext
->PanningLUT
[OUTPUTCHANNELS
* pos
];
837 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
838 // elevation adjustment for directional gain. this sucks, but
839 // has low complexity
840 AmbientGain
= 1.0/aluSqrt(ALContext
->NumChan
) * (1.0-DirGain
);
841 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
843 ALfloat gain
= SpeakerGain
[s
]*DirGain
+ AmbientGain
;
844 ALSource
->Params
.DryGains
[s
] = DryMix
* gain
;
847 /* Update filter coefficients. */
848 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ Frequency
);
850 /* Spatialized sources use four chained one-pole filters, so we need to
851 * take the fourth root of the squared gain, which is the same as the
852 * square root of the base gain. */
853 ALSource
->Params
.iirFilter
.coeff
= lpCoeffCalc(aluSqrt(DryGainHF
), cw
);
855 for(i
= 0;i
< NumSends
;i
++)
857 /* The wet path uses two chained one-pole filters, so take the
858 * base gain (square root of the squared gain) */
859 ALSource
->Params
.Send
[i
].iirFilter
.coeff
= lpCoeffCalc(WetGainHF
[i
], cw
);
863 static __inline ALfloat
point(ALfloat val1
, ALfloat val2
, ALint frac
)
869 static __inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALint frac
)
871 return val1
+ ((val2
-val1
)*(frac
* (1.0f
/(1<<FRACTIONBITS
))));
873 static __inline ALfloat
cos_lerp(ALfloat val1
, ALfloat val2
, ALint frac
)
875 ALfloat mult
= (1.0f
-cos(frac
* (1.0f
/(1<<FRACTIONBITS
)) * M_PI
)) * 0.5f
;
876 return val1
+ ((val2
-val1
)*mult
);
879 static void MixSomeSources(ALCcontext
*ALContext
, float (*DryBuffer
)[OUTPUTCHANNELS
], ALuint SamplesToDo
)
881 static float DummyBuffer
[BUFFERSIZE
];
882 ALfloat
*WetBuffer
[MAX_SENDS
];
883 ALfloat (*Matrix
)[OUTPUTCHANNELS
] = ALContext
->ChannelMatrix
;
884 ALfloat DrySend
[OUTPUTCHANNELS
];
885 ALfloat dryGainStep
[OUTPUTCHANNELS
];
886 ALfloat wetGainStep
[MAX_SENDS
];
889 ALfloat value
, outsamp
;
890 ALbufferlistitem
*BufferListItem
;
891 ALint64 DataSize64
,DataPos64
;
892 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
893 ALfloat WetSend
[MAX_SENDS
];
897 ALuint DataPosInt
, DataPosFrac
;
898 ALuint Channels
, Bytes
;
900 resampler_t Resampler
;
901 ALuint BuffersPlayed
;
905 if(!(ALSource
=ALContext
->Source
))
908 DeviceFreq
= ALContext
->Device
->Frequency
;
910 rampLength
= DeviceFreq
* MIN_RAMP_LENGTH
/ 1000;
911 rampLength
= max(rampLength
, SamplesToDo
);
914 if(ALSource
->state
!= AL_PLAYING
)
916 if((ALSource
=ALSource
->next
) != NULL
)
922 /* Find buffer format */
926 BufferListItem
= ALSource
->queue
;
927 while(BufferListItem
!= NULL
)
930 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
932 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
933 Bytes
= aluBytesFromFormat(ALBuffer
->format
);
934 Frequency
= ALBuffer
->frequency
;
937 BufferListItem
= BufferListItem
->next
;
940 if(ALSource
->NeedsUpdate
)
942 //Only apply 3D calculations for mono buffers
944 CalcSourceParams(ALContext
, ALSource
);
946 CalcNonAttnSourceParams(ALContext
, ALSource
);
947 ALSource
->NeedsUpdate
= AL_FALSE
;
950 /* Get source info */
951 Resampler
= ALSource
->Resampler
;
952 State
= ALSource
->state
;
953 BuffersPlayed
= ALSource
->BuffersPlayed
;
954 DataPosInt
= ALSource
->position
;
955 DataPosFrac
= ALSource
->position_fraction
;
957 /* Compute 18.14 fixed point step */
958 Pitch
= (ALSource
->Params
.Pitch
*Frequency
) / DeviceFreq
;
959 if(Pitch
> (float)MAX_PITCH
) Pitch
= (float)MAX_PITCH
;
960 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
961 if(increment
<= 0) increment
= (1<<FRACTIONBITS
);
963 /* Compute the gain steps for each output channel */
964 if(ALSource
->FirstStart
)
966 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
967 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
968 for(i
= 0;i
< MAX_SENDS
;i
++)
969 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
973 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
974 DrySend
[i
] = ALSource
->DryGains
[i
];
975 for(i
= 0;i
< MAX_SENDS
;i
++)
976 WetSend
[i
] = ALSource
->WetGains
[i
];
979 DryFilter
= &ALSource
->Params
.iirFilter
;
980 for(i
= 0;i
< MAX_SENDS
;i
++)
982 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
983 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
984 ALSource
->Send
[i
].Slot
->WetBuffer
:
988 if(DuplicateStereo
&& Channels
== 2)
990 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
991 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
992 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
993 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
995 else if(DuplicateStereo
)
997 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
998 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
999 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
1000 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
1003 /* Get current buffer queue item */
1004 BufferListItem
= ALSource
->queue
;
1005 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
1006 BufferListItem
= BufferListItem
->next
;
1008 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
1010 ALuint DataSize
= 0;
1015 /* Get buffer info */
1016 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
1018 Data
= ALBuffer
->data
;
1019 DataSize
= ALBuffer
->size
;
1020 DataSize
/= Channels
* Bytes
;
1022 if(DataPosInt
>= DataSize
)
1025 if(BufferListItem
->next
)
1027 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
1028 if(NextBuf
&& NextBuf
->size
)
1030 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1031 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1032 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1035 else if(ALSource
->bLooping
)
1037 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
1038 if(NextBuf
&& NextBuf
->size
)
1040 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1041 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1042 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1046 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
1048 /* Compute the gain steps for each output channel */
1049 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1050 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-DrySend
[i
]) /
1052 for(i
= 0;i
< MAX_SENDS
;i
++)
1053 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-WetSend
[i
]) /
1056 /* Figure out how many samples we can mix. */
1057 DataSize64
= DataSize
;
1058 DataSize64
<<= FRACTIONBITS
;
1059 DataPos64
= DataPosInt
;
1060 DataPos64
<<= FRACTIONBITS
;
1061 DataPos64
+= DataPosFrac
;
1062 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
1064 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
1066 /* Actual sample mixing loop */
1068 Data
+= DataPosInt
*Channels
;
1070 if(Channels
== 1) /* Mono */
1072 #define DO_MIX(resampler) do { \
1073 while(BufferSize--) \
1075 for(i = 0;i < OUTPUTCHANNELS;i++) \
1076 DrySend[i] += dryGainStep[i]; \
1077 for(i = 0;i < MAX_SENDS;i++) \
1078 WetSend[i] += wetGainStep[i]; \
1080 /* First order interpolator */ \
1081 value = (resampler)(Data[k], Data[k+1], DataPosFrac); \
1083 /* Direct path final mix buffer and panning */ \
1084 outsamp = lpFilter4P(DryFilter, 0, value); \
1085 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT]; \
1086 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT]; \
1087 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT]; \
1088 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT]; \
1089 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT]; \
1090 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT]; \
1091 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER]; \
1092 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER]; \
1094 /* Room path final mix buffer and panning */ \
1095 for(i = 0;i < MAX_SENDS;i++) \
1097 outsamp = lpFilter2P(WetFilter[i], 0, value); \
1098 WetBuffer[i][j] += outsamp*WetSend[i]; \
1101 DataPosFrac += increment; \
1102 k += DataPosFrac>>FRACTIONBITS; \
1103 DataPosFrac &= FRACTIONMASK; \
1110 case POINT_RESAMPLER
:
1111 DO_MIX(point
); break;
1112 case LINEAR_RESAMPLER
:
1113 DO_MIX(lerp
); break;
1114 case COSINE_RESAMPLER
:
1115 DO_MIX(cos_lerp
); break;
1122 else if(Channels
== 2) /* Stereo */
1124 const int chans
[] = {
1125 FRONT_LEFT
, FRONT_RIGHT
1127 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1129 #define DO_MIX(resampler) do { \
1130 while(BufferSize--) \
1132 for(i = 0;i < OUTPUTCHANNELS;i++) \
1133 DrySend[i] += dryGainStep[i]; \
1134 for(i = 0;i < MAX_SENDS;i++) \
1135 WetSend[i] += wetGainStep[i]; \
1137 for(i = 0;i < Channels;i++) \
1139 value = (resampler)(Data[k*Channels + i], Data[(k+1)*Channels + i], \
1141 outsamp = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1142 for(out = 0;out < OUTPUTCHANNELS;out++) \
1143 DryBuffer[j][out] += outsamp*Matrix[chans[i]][out]; \
1144 for(out = 0;out < MAX_SENDS;out++) \
1146 outsamp = lpFilter1P(WetFilter[out], chans[i], value); \
1147 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1151 DataPosFrac += increment; \
1152 k += DataPosFrac>>FRACTIONBITS; \
1153 DataPosFrac &= FRACTIONMASK; \
1160 case POINT_RESAMPLER
:
1161 DO_MIX(point
); break;
1162 case LINEAR_RESAMPLER
:
1163 DO_MIX(lerp
); break;
1164 case COSINE_RESAMPLER
:
1165 DO_MIX(cos_lerp
); break;
1171 else if(Channels
== 4) /* Quad */
1173 const int chans
[] = {
1174 FRONT_LEFT
, FRONT_RIGHT
,
1175 BACK_LEFT
, BACK_RIGHT
1177 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1181 case POINT_RESAMPLER
:
1182 DO_MIX(point
); break;
1183 case LINEAR_RESAMPLER
:
1184 DO_MIX(lerp
); break;
1185 case COSINE_RESAMPLER
:
1186 DO_MIX(cos_lerp
); break;
1192 else if(Channels
== 6) /* 5.1 */
1194 const int chans
[] = {
1195 FRONT_LEFT
, FRONT_RIGHT
,
1197 BACK_LEFT
, BACK_RIGHT
1199 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1203 case POINT_RESAMPLER
:
1204 DO_MIX(point
); break;
1205 case LINEAR_RESAMPLER
:
1206 DO_MIX(lerp
); break;
1207 case COSINE_RESAMPLER
:
1208 DO_MIX(cos_lerp
); break;
1214 else if(Channels
== 7) /* 6.1 */
1216 const int chans
[] = {
1217 FRONT_LEFT
, FRONT_RIGHT
,
1220 SIDE_LEFT
, SIDE_RIGHT
1222 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1226 case POINT_RESAMPLER
:
1227 DO_MIX(point
); break;
1228 case LINEAR_RESAMPLER
:
1229 DO_MIX(lerp
); break;
1230 case COSINE_RESAMPLER
:
1231 DO_MIX(cos_lerp
); break;
1237 else if(Channels
== 8) /* 7.1 */
1239 const int chans
[] = {
1240 FRONT_LEFT
, FRONT_RIGHT
,
1242 BACK_LEFT
, BACK_RIGHT
,
1243 SIDE_LEFT
, SIDE_RIGHT
1245 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1249 case POINT_RESAMPLER
:
1250 DO_MIX(point
); break;
1251 case LINEAR_RESAMPLER
:
1252 DO_MIX(lerp
); break;
1253 case COSINE_RESAMPLER
:
1254 DO_MIX(cos_lerp
); break;
1263 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1264 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1265 for(i
= 0;i
< MAX_SENDS
;i
++)
1266 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1269 DataPosFrac
+= increment
;
1270 k
+= DataPosFrac
>>FRACTIONBITS
;
1271 DataPosFrac
&= FRACTIONMASK
;
1278 /* Handle looping sources */
1279 if(DataPosInt
>= DataSize
)
1281 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1283 BufferListItem
= BufferListItem
->next
;
1285 DataPosInt
-= DataSize
;
1287 else if(ALSource
->bLooping
)
1289 BufferListItem
= ALSource
->queue
;
1291 if(ALSource
->BuffersInQueue
== 1)
1292 DataPosInt
%= DataSize
;
1294 DataPosInt
-= DataSize
;
1299 BufferListItem
= ALSource
->queue
;
1300 BuffersPlayed
= ALSource
->BuffersInQueue
;
1307 /* Update source info */
1308 ALSource
->state
= State
;
1309 ALSource
->BuffersPlayed
= BuffersPlayed
;
1310 ALSource
->position
= DataPosInt
;
1311 ALSource
->position_fraction
= DataPosFrac
;
1312 ALSource
->Buffer
= BufferListItem
->buffer
;
1314 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1315 ALSource
->DryGains
[i
] = DrySend
[i
];
1316 for(i
= 0;i
< MAX_SENDS
;i
++)
1317 ALSource
->WetGains
[i
] = WetSend
[i
];
1319 ALSource
->FirstStart
= AL_FALSE
;
1321 if((ALSource
=ALSource
->next
) != NULL
)
1322 goto another_source
;
1325 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1327 float (*DryBuffer
)[OUTPUTCHANNELS
];
1328 const Channel
*ChanMap
;
1330 ALeffectslot
*ALEffectSlot
;
1331 ALCcontext
*ALContext
;
1335 #if defined(HAVE_FESETROUND)
1336 fpuState
= fegetround();
1337 fesetround(FE_TOWARDZERO
);
1338 #elif defined(HAVE__CONTROLFP)
1339 fpuState
= _controlfp(0, 0);
1340 _controlfp(_RC_CHOP
, _MCW_RC
);
1345 DryBuffer
= device
->DryBuffer
;
1348 /* Setup variables */
1349 SamplesToDo
= min(size
, BUFFERSIZE
);
1351 /* Clear mixing buffer */
1352 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1354 SuspendContext(NULL
);
1355 for(c
= 0;c
< device
->NumContexts
;c
++)
1357 ALContext
= device
->Contexts
[c
];
1358 SuspendContext(ALContext
);
1360 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1362 /* effect slot processing */
1363 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1366 if(ALEffectSlot
->EffectState
)
1367 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1369 for(i
= 0;i
< SamplesToDo
;i
++)
1370 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1371 ALEffectSlot
= ALEffectSlot
->next
;
1373 ProcessContext(ALContext
);
1375 ProcessContext(NULL
);
1377 //Post processing loop
1378 ChanMap
= device
->DevChannels
;
1379 switch(device
->Format
)
1381 #define CHECK_WRITE_FORMAT(bits, type, func) \
1382 case AL_FORMAT_MONO##bits: \
1383 for(i = 0;i < SamplesToDo;i++) \
1385 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1386 buffer = ((type*)buffer) + 1; \
1389 case AL_FORMAT_STEREO##bits: \
1392 for(i = 0;i < SamplesToDo;i++) \
1395 samples[0] = DryBuffer[i][ChanMap[0]]; \
1396 samples[1] = DryBuffer[i][ChanMap[1]]; \
1397 bs2b_cross_feed(device->Bs2b, samples); \
1398 ((type*)buffer)[0] = (func)(samples[0]); \
1399 ((type*)buffer)[1] = (func)(samples[1]); \
1400 buffer = ((type*)buffer) + 2; \
1405 for(i = 0;i < SamplesToDo;i++) \
1407 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1408 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1409 buffer = ((type*)buffer) + 2; \
1413 case AL_FORMAT_QUAD##bits: \
1414 for(i = 0;i < SamplesToDo;i++) \
1416 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1417 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1418 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1419 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1420 buffer = ((type*)buffer) + 4; \
1423 case AL_FORMAT_51CHN##bits: \
1424 for(i = 0;i < SamplesToDo;i++) \
1426 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1427 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1428 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1429 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1430 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1431 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1432 buffer = ((type*)buffer) + 6; \
1435 case AL_FORMAT_61CHN##bits: \
1436 for(i = 0;i < SamplesToDo;i++) \
1438 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1439 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1440 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1441 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1442 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1443 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1444 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1445 buffer = ((type*)buffer) + 7; \
1448 case AL_FORMAT_71CHN##bits: \
1449 for(i = 0;i < SamplesToDo;i++) \
1451 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1452 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1453 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1454 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1455 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1456 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1457 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1458 ((type*)buffer)[7] = (func)(DryBuffer[i][ChanMap[7]]); \
1459 buffer = ((type*)buffer) + 8; \
1463 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1464 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1465 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
)
1466 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
)
1467 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
)
1468 #undef AL_FORMAT_STEREO32
1469 #undef AL_FORMAT_MONO32
1470 #undef CHECK_WRITE_FORMAT
1476 size
-= SamplesToDo
;
1479 #if defined(HAVE_FESETROUND)
1480 fesetround(fpuState
);
1481 #elif defined(HAVE__CONTROLFP)
1482 _controlfp(fpuState
, 0xfffff);
1486 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1490 SuspendContext(NULL
);
1491 for(i
= 0;i
< device
->NumContexts
;i
++)
1495 SuspendContext(device
->Contexts
[i
]);
1497 source
= device
->Contexts
[i
]->Source
;
1500 if(source
->state
== AL_PLAYING
)
1502 source
->state
= AL_STOPPED
;
1503 source
->BuffersPlayed
= source
->BuffersInQueue
;
1504 source
->position
= 0;
1505 source
->position_fraction
= 0;
1507 source
= source
->next
;
1509 ProcessContext(device
->Contexts
[i
]);
1512 device
->Connected
= ALC_FALSE
;
1513 ProcessContext(NULL
);