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 /* Compute the gain steps for each output channel */
962 if(ALSource
->FirstStart
)
964 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
965 DrySend
[i
] = ALSource
->Params
.DryGains
[i
];
966 for(i
= 0;i
< MAX_SENDS
;i
++)
967 WetSend
[i
] = ALSource
->Params
.WetGains
[i
];
971 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
972 DrySend
[i
] = ALSource
->DryGains
[i
];
973 for(i
= 0;i
< MAX_SENDS
;i
++)
974 WetSend
[i
] = ALSource
->WetGains
[i
];
977 DryFilter
= &ALSource
->Params
.iirFilter
;
978 for(i
= 0;i
< MAX_SENDS
;i
++)
980 WetFilter
[i
] = &ALSource
->Params
.Send
[i
].iirFilter
;
981 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
982 ALSource
->Send
[i
].Slot
->WetBuffer
:
986 if(DuplicateStereo
&& Channels
== 2)
988 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
989 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
990 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
991 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
993 else if(DuplicateStereo
)
995 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
996 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
997 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
998 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
1001 /* Get current buffer queue item */
1002 BufferListItem
= ALSource
->queue
;
1003 for(i
= 0;i
< BuffersPlayed
&& BufferListItem
;i
++)
1004 BufferListItem
= BufferListItem
->next
;
1006 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
1008 ALuint DataSize
= 0;
1013 /* Get buffer info */
1014 if((ALBuffer
=BufferListItem
->buffer
) != NULL
)
1016 Data
= ALBuffer
->data
;
1017 DataSize
= ALBuffer
->size
;
1018 DataSize
/= Channels
* Bytes
;
1020 if(DataPosInt
>= DataSize
)
1023 if(BufferListItem
->next
)
1025 ALbuffer
*NextBuf
= BufferListItem
->next
->buffer
;
1026 if(NextBuf
&& NextBuf
->size
)
1028 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1029 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1030 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1033 else if(ALSource
->bLooping
)
1035 ALbuffer
*NextBuf
= ALSource
->queue
->buffer
;
1036 if(NextBuf
&& NextBuf
->size
)
1038 ALint ulExtraSamples
= BUFFER_PADDING
*Channels
*Bytes
;
1039 ulExtraSamples
= min(NextBuf
->size
, ulExtraSamples
);
1040 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
1044 memset(&Data
[DataSize
*Channels
], 0, (BUFFER_PADDING
*Channels
*Bytes
));
1046 /* Compute the gain steps for each output channel */
1047 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1048 dryGainStep
[i
] = (ALSource
->Params
.DryGains
[i
]-DrySend
[i
]) /
1050 for(i
= 0;i
< MAX_SENDS
;i
++)
1051 wetGainStep
[i
] = (ALSource
->Params
.WetGains
[i
]-WetSend
[i
]) /
1054 /* Figure out how many samples we can mix. */
1055 DataSize64
= DataSize
;
1056 DataSize64
<<= FRACTIONBITS
;
1057 DataPos64
= DataPosInt
;
1058 DataPos64
<<= FRACTIONBITS
;
1059 DataPos64
+= DataPosFrac
;
1060 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
1062 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
1064 /* Actual sample mixing loop */
1066 Data
+= DataPosInt
*Channels
;
1068 if(Channels
== 1) /* Mono */
1070 #define DO_MIX(resampler) do { \
1071 while(BufferSize--) \
1073 for(i = 0;i < OUTPUTCHANNELS;i++) \
1074 DrySend[i] += dryGainStep[i]; \
1075 for(i = 0;i < MAX_SENDS;i++) \
1076 WetSend[i] += wetGainStep[i]; \
1078 /* First order interpolator */ \
1079 value = (resampler)(Data[k], Data[k+1], DataPosFrac); \
1081 /* Direct path final mix buffer and panning */ \
1082 outsamp = lpFilter4P(DryFilter, 0, value); \
1083 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT]; \
1084 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT]; \
1085 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT]; \
1086 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT]; \
1087 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT]; \
1088 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT]; \
1089 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER]; \
1090 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER]; \
1092 /* Room path final mix buffer and panning */ \
1093 for(i = 0;i < MAX_SENDS;i++) \
1095 outsamp = lpFilter2P(WetFilter[i], 0, value); \
1096 WetBuffer[i][j] += outsamp*WetSend[i]; \
1099 DataPosFrac += increment; \
1100 k += DataPosFrac>>FRACTIONBITS; \
1101 DataPosFrac &= FRACTIONMASK; \
1108 case POINT_RESAMPLER
:
1109 DO_MIX(point
); break;
1110 case LINEAR_RESAMPLER
:
1111 DO_MIX(lerp
); break;
1112 case COSINE_RESAMPLER
:
1113 DO_MIX(cos_lerp
); break;
1120 else if(Channels
== 2) /* Stereo */
1122 const int chans
[] = {
1123 FRONT_LEFT
, FRONT_RIGHT
1125 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1127 #define DO_MIX(resampler) do { \
1128 while(BufferSize--) \
1130 for(i = 0;i < OUTPUTCHANNELS;i++) \
1131 DrySend[i] += dryGainStep[i]; \
1132 for(i = 0;i < MAX_SENDS;i++) \
1133 WetSend[i] += wetGainStep[i]; \
1135 for(i = 0;i < Channels;i++) \
1137 value = (resampler)(Data[k*Channels + i], Data[(k+1)*Channels + i], \
1139 outsamp = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1140 for(out = 0;out < OUTPUTCHANNELS;out++) \
1141 DryBuffer[j][out] += outsamp*Matrix[chans[i]][out]; \
1142 for(out = 0;out < MAX_SENDS;out++) \
1144 outsamp = lpFilter1P(WetFilter[out], chans[i], value); \
1145 WetBuffer[out][j] += outsamp*WetSend[out]*scaler; \
1149 DataPosFrac += increment; \
1150 k += DataPosFrac>>FRACTIONBITS; \
1151 DataPosFrac &= FRACTIONMASK; \
1158 case POINT_RESAMPLER
:
1159 DO_MIX(point
); break;
1160 case LINEAR_RESAMPLER
:
1161 DO_MIX(lerp
); break;
1162 case COSINE_RESAMPLER
:
1163 DO_MIX(cos_lerp
); break;
1169 else if(Channels
== 4) /* Quad */
1171 const int chans
[] = {
1172 FRONT_LEFT
, FRONT_RIGHT
,
1173 BACK_LEFT
, BACK_RIGHT
1175 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1179 case POINT_RESAMPLER
:
1180 DO_MIX(point
); break;
1181 case LINEAR_RESAMPLER
:
1182 DO_MIX(lerp
); break;
1183 case COSINE_RESAMPLER
:
1184 DO_MIX(cos_lerp
); break;
1190 else if(Channels
== 6) /* 5.1 */
1192 const int chans
[] = {
1193 FRONT_LEFT
, FRONT_RIGHT
,
1195 BACK_LEFT
, BACK_RIGHT
1197 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1201 case POINT_RESAMPLER
:
1202 DO_MIX(point
); break;
1203 case LINEAR_RESAMPLER
:
1204 DO_MIX(lerp
); break;
1205 case COSINE_RESAMPLER
:
1206 DO_MIX(cos_lerp
); break;
1212 else if(Channels
== 7) /* 6.1 */
1214 const int chans
[] = {
1215 FRONT_LEFT
, FRONT_RIGHT
,
1218 SIDE_LEFT
, SIDE_RIGHT
1220 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1224 case POINT_RESAMPLER
:
1225 DO_MIX(point
); break;
1226 case LINEAR_RESAMPLER
:
1227 DO_MIX(lerp
); break;
1228 case COSINE_RESAMPLER
:
1229 DO_MIX(cos_lerp
); break;
1235 else if(Channels
== 8) /* 7.1 */
1237 const int chans
[] = {
1238 FRONT_LEFT
, FRONT_RIGHT
,
1240 BACK_LEFT
, BACK_RIGHT
,
1241 SIDE_LEFT
, SIDE_RIGHT
1243 const ALfloat scaler
= aluSqrt(1.0f
/Channels
);
1247 case POINT_RESAMPLER
:
1248 DO_MIX(point
); break;
1249 case LINEAR_RESAMPLER
:
1250 DO_MIX(lerp
); break;
1251 case COSINE_RESAMPLER
:
1252 DO_MIX(cos_lerp
); break;
1261 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1262 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1263 for(i
= 0;i
< MAX_SENDS
;i
++)
1264 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1267 DataPosFrac
+= increment
;
1268 k
+= DataPosFrac
>>FRACTIONBITS
;
1269 DataPosFrac
&= FRACTIONMASK
;
1276 /* Handle looping sources */
1277 if(DataPosInt
>= DataSize
)
1279 if(BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1281 BufferListItem
= BufferListItem
->next
;
1283 DataPosInt
-= DataSize
;
1285 else if(ALSource
->bLooping
)
1287 BufferListItem
= ALSource
->queue
;
1289 if(ALSource
->BuffersInQueue
== 1)
1290 DataPosInt
%= DataSize
;
1292 DataPosInt
-= DataSize
;
1297 BufferListItem
= ALSource
->queue
;
1298 BuffersPlayed
= ALSource
->BuffersInQueue
;
1305 /* Update source info */
1306 ALSource
->state
= State
;
1307 ALSource
->BuffersPlayed
= BuffersPlayed
;
1308 ALSource
->position
= DataPosInt
;
1309 ALSource
->position_fraction
= DataPosFrac
;
1310 ALSource
->Buffer
= BufferListItem
->buffer
;
1312 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1313 ALSource
->DryGains
[i
] = DrySend
[i
];
1314 for(i
= 0;i
< MAX_SENDS
;i
++)
1315 ALSource
->WetGains
[i
] = WetSend
[i
];
1317 ALSource
->FirstStart
= AL_FALSE
;
1319 if((ALSource
=ALSource
->next
) != NULL
)
1320 goto another_source
;
1323 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1325 float (*DryBuffer
)[OUTPUTCHANNELS
];
1326 const Channel
*ChanMap
;
1328 ALeffectslot
*ALEffectSlot
;
1329 ALCcontext
*ALContext
;
1333 #if defined(HAVE_FESETROUND)
1334 fpuState
= fegetround();
1335 fesetround(FE_TOWARDZERO
);
1336 #elif defined(HAVE__CONTROLFP)
1337 fpuState
= _controlfp(0, 0);
1338 _controlfp(_RC_CHOP
, _MCW_RC
);
1343 DryBuffer
= device
->DryBuffer
;
1346 /* Setup variables */
1347 SamplesToDo
= min(size
, BUFFERSIZE
);
1349 /* Clear mixing buffer */
1350 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1352 SuspendContext(NULL
);
1353 for(c
= 0;c
< device
->NumContexts
;c
++)
1355 ALContext
= device
->Contexts
[c
];
1356 SuspendContext(ALContext
);
1358 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1360 /* effect slot processing */
1361 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1364 if(ALEffectSlot
->EffectState
)
1365 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1367 for(i
= 0;i
< SamplesToDo
;i
++)
1368 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1369 ALEffectSlot
= ALEffectSlot
->next
;
1371 ProcessContext(ALContext
);
1373 ProcessContext(NULL
);
1375 //Post processing loop
1376 ChanMap
= device
->DevChannels
;
1377 switch(device
->Format
)
1379 #define CHECK_WRITE_FORMAT(bits, type, func) \
1380 case AL_FORMAT_MONO##bits: \
1381 for(i = 0;i < SamplesToDo;i++) \
1383 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1384 buffer = ((type*)buffer) + 1; \
1387 case AL_FORMAT_STEREO##bits: \
1390 for(i = 0;i < SamplesToDo;i++) \
1393 samples[0] = DryBuffer[i][ChanMap[0]]; \
1394 samples[1] = DryBuffer[i][ChanMap[1]]; \
1395 bs2b_cross_feed(device->Bs2b, samples); \
1396 ((type*)buffer)[0] = (func)(samples[0]); \
1397 ((type*)buffer)[1] = (func)(samples[1]); \
1398 buffer = ((type*)buffer) + 2; \
1403 for(i = 0;i < SamplesToDo;i++) \
1405 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1406 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1407 buffer = ((type*)buffer) + 2; \
1411 case AL_FORMAT_QUAD##bits: \
1412 for(i = 0;i < SamplesToDo;i++) \
1414 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1415 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1416 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1417 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1418 buffer = ((type*)buffer) + 4; \
1421 case AL_FORMAT_51CHN##bits: \
1422 for(i = 0;i < SamplesToDo;i++) \
1424 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1425 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1426 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1427 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1428 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1429 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1430 buffer = ((type*)buffer) + 6; \
1433 case AL_FORMAT_61CHN##bits: \
1434 for(i = 0;i < SamplesToDo;i++) \
1436 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1437 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1438 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1439 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1440 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1441 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1442 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1443 buffer = ((type*)buffer) + 7; \
1446 case AL_FORMAT_71CHN##bits: \
1447 for(i = 0;i < SamplesToDo;i++) \
1449 ((type*)buffer)[0] = (func)(DryBuffer[i][ChanMap[0]]); \
1450 ((type*)buffer)[1] = (func)(DryBuffer[i][ChanMap[1]]); \
1451 ((type*)buffer)[2] = (func)(DryBuffer[i][ChanMap[2]]); \
1452 ((type*)buffer)[3] = (func)(DryBuffer[i][ChanMap[3]]); \
1453 ((type*)buffer)[4] = (func)(DryBuffer[i][ChanMap[4]]); \
1454 ((type*)buffer)[5] = (func)(DryBuffer[i][ChanMap[5]]); \
1455 ((type*)buffer)[6] = (func)(DryBuffer[i][ChanMap[6]]); \
1456 ((type*)buffer)[7] = (func)(DryBuffer[i][ChanMap[7]]); \
1457 buffer = ((type*)buffer) + 8; \
1461 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1462 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1463 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
)
1464 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
)
1465 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
)
1466 #undef AL_FORMAT_STEREO32
1467 #undef AL_FORMAT_MONO32
1468 #undef CHECK_WRITE_FORMAT
1474 size
-= SamplesToDo
;
1477 #if defined(HAVE_FESETROUND)
1478 fesetround(fpuState
);
1479 #elif defined(HAVE__CONTROLFP)
1480 _controlfp(fpuState
, 0xfffff);
1484 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1488 SuspendContext(NULL
);
1489 for(i
= 0;i
< device
->NumContexts
;i
++)
1493 SuspendContext(device
->Contexts
[i
]);
1495 source
= device
->Contexts
[i
]->Source
;
1498 if(source
->state
== AL_PLAYING
)
1500 source
->state
= AL_STOPPED
;
1501 source
->BuffersPlayed
= source
->BuffersInQueue
;
1502 source
->position
= 0;
1503 source
->position_fraction
= 0;
1505 source
= source
->next
;
1507 ProcessContext(device
->Contexts
[i
]);
1510 device
->Connected
= ALC_FALSE
;
1511 ProcessContext(NULL
);