2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
35 #include "alListener.h"
36 #include "alAuxEffectSlot.h"
40 #if defined(HAVE_STDINT_H)
42 typedef int64_t ALint64
;
43 #elif defined(HAVE___INT64)
44 typedef __int64 ALint64
;
45 #elif (SIZEOF_LONG == 8)
47 #elif (SIZEOF_LONG_LONG == 8)
48 typedef long long ALint64
;
51 #define FRACTIONBITS 14
52 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
53 #define MAX_PITCH 65536
55 /* Minimum ramp length in milliseconds. The value below was chosen to
56 * adequately reduce clicks and pops from harsh gain changes. */
57 #define MIN_RAMP_LENGTH 16
59 ALboolean DuplicateStereo
= AL_FALSE
;
62 static __inline ALfloat
aluF2F(ALfloat Value
)
64 if(Value
< 0.f
) return Value
/32768.f
;
65 if(Value
> 0.f
) return Value
/32767.f
;
69 static __inline ALshort
aluF2S(ALfloat Value
)
79 static __inline ALubyte
aluF2UB(ALfloat Value
)
81 ALshort i
= aluF2S(Value
);
86 static __inline ALvoid
aluCrossproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
, ALfloat
*outVector
)
88 outVector
[0] = inVector1
[1]*inVector2
[2] - inVector1
[2]*inVector2
[1];
89 outVector
[1] = inVector1
[2]*inVector2
[0] - inVector1
[0]*inVector2
[2];
90 outVector
[2] = inVector1
[0]*inVector2
[1] - inVector1
[1]*inVector2
[0];
93 static __inline ALfloat
aluDotproduct(const ALfloat
*inVector1
, const ALfloat
*inVector2
)
95 return inVector1
[0]*inVector2
[0] + inVector1
[1]*inVector2
[1] +
96 inVector1
[2]*inVector2
[2];
99 static __inline ALvoid
aluNormalize(ALfloat
*inVector
)
101 ALfloat length
, inverse_length
;
103 length
= aluSqrt(aluDotproduct(inVector
, inVector
));
106 inverse_length
= 1.0f
/length
;
107 inVector
[0] *= inverse_length
;
108 inVector
[1] *= inverse_length
;
109 inVector
[2] *= inverse_length
;
113 static __inline ALvoid
aluMatrixVector(ALfloat
*vector
,ALfloat matrix
[3][3])
117 result
[0] = vector
[0]*matrix
[0][0] + vector
[1]*matrix
[1][0] + vector
[2]*matrix
[2][0];
118 result
[1] = vector
[0]*matrix
[0][1] + vector
[1]*matrix
[1][1] + vector
[2]*matrix
[2][1];
119 result
[2] = vector
[0]*matrix
[0][2] + vector
[1]*matrix
[1][2] + vector
[2]*matrix
[2][2];
120 memcpy(vector
, result
, sizeof(result
));
123 static ALvoid
SetSpeakerArrangement(const char *name
, ALfloat SpeakerAngle
[OUTPUTCHANNELS
],
124 ALint Speaker2Chan
[OUTPUTCHANNELS
], ALint chans
)
132 confkey
= GetConfigValue(NULL
, name
, "");
137 next
= strchr(confkey
, ',');
142 } while(isspace(*next
));
145 sep
= strchr(confkey
, '=');
146 if(!sep
|| confkey
== sep
)
150 while(isspace(*end
) && end
!= confkey
)
154 if(strncmp(confkey
, "fl", end
-confkey
) == 0)
156 else if(strncmp(confkey
, "fr", end
-confkey
) == 0)
158 else if(strncmp(confkey
, "fc", end
-confkey
) == 0)
160 else if(strncmp(confkey
, "bl", end
-confkey
) == 0)
162 else if(strncmp(confkey
, "br", end
-confkey
) == 0)
164 else if(strncmp(confkey
, "bc", end
-confkey
) == 0)
166 else if(strncmp(confkey
, "sl", end
-confkey
) == 0)
168 else if(strncmp(confkey
, "sr", end
-confkey
) == 0)
172 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name
, confkey
[0], confkey
[1]);
180 for(i
= 0;i
< chans
;i
++)
182 if(Speaker2Chan
[i
] == val
)
184 val
= strtol(sep
, NULL
, 10);
185 if(val
>= -180 && val
<= 180)
186 SpeakerAngle
[i
] = val
* M_PI
/180.0f
;
188 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey
[0], confkey
[1], val
);
194 for(i
= 1;i
< chans
;i
++)
196 if(SpeakerAngle
[i
] <= SpeakerAngle
[i
-1])
198 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i
, chans
,
199 SpeakerAngle
[i
-1] * 180.0f
/M_PI
, SpeakerAngle
[i
] * 180.0f
/M_PI
);
200 SpeakerAngle
[i
] = SpeakerAngle
[i
-1] + 1 * 180.0f
/M_PI
;
205 static __inline ALfloat
aluLUTpos2Angle(ALint pos
)
207 if(pos
< QUADRANT_NUM
)
208 return aluAtan((ALfloat
)pos
/ (ALfloat
)(QUADRANT_NUM
- pos
));
209 if(pos
< 2 * QUADRANT_NUM
)
210 return M_PI_2
+ aluAtan((ALfloat
)(pos
- QUADRANT_NUM
) / (ALfloat
)(2 * QUADRANT_NUM
- pos
));
211 if(pos
< 3 * QUADRANT_NUM
)
212 return aluAtan((ALfloat
)(pos
- 2 * QUADRANT_NUM
) / (ALfloat
)(3 * QUADRANT_NUM
- pos
)) - M_PI
;
213 return aluAtan((ALfloat
)(pos
- 3 * QUADRANT_NUM
) / (ALfloat
)(4 * QUADRANT_NUM
- pos
)) - M_PI_2
;
216 ALvoid
aluInitPanning(ALCcontext
*Context
)
218 ALint pos
, offset
, s
;
219 ALfloat Alpha
, Theta
;
220 ALfloat SpeakerAngle
[OUTPUTCHANNELS
];
221 ALint Speaker2Chan
[OUTPUTCHANNELS
];
223 for(s
= 0;s
< OUTPUTCHANNELS
;s
++)
226 for(s2
= 0;s2
< OUTPUTCHANNELS
;s2
++)
227 Context
->ChannelMatrix
[s
][s2
] = ((s
==s2
) ? 1.0f
: 0.0f
);
230 switch(Context
->Device
->Format
)
232 /* Mono is rendered as stereo, then downmixed during post-process */
233 case AL_FORMAT_MONO8
:
234 case AL_FORMAT_MONO16
:
235 case AL_FORMAT_MONO_FLOAT32
:
236 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
237 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
238 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
239 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
240 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
241 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
242 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
243 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
244 Context
->NumChan
= 2;
245 Speaker2Chan
[0] = FRONT_LEFT
;
246 Speaker2Chan
[1] = FRONT_RIGHT
;
247 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
248 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
251 case AL_FORMAT_STEREO8
:
252 case AL_FORMAT_STEREO16
:
253 case AL_FORMAT_STEREO_FLOAT32
:
254 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
255 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
256 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = 1.0f
;
257 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = 1.0f
;
258 Context
->ChannelMatrix
[BACK_LEFT
][FRONT_LEFT
] = 1.0f
;
259 Context
->ChannelMatrix
[BACK_RIGHT
][FRONT_RIGHT
] = 1.0f
;
260 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
261 Context
->ChannelMatrix
[BACK_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
262 Context
->NumChan
= 2;
263 Speaker2Chan
[0] = FRONT_LEFT
;
264 Speaker2Chan
[1] = FRONT_RIGHT
;
265 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
266 SpeakerAngle
[1] = 90.0f
* M_PI
/180.0f
;
267 SetSpeakerArrangement("layout_STEREO", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
270 case AL_FORMAT_QUAD8
:
271 case AL_FORMAT_QUAD16
:
272 case AL_FORMAT_QUAD32
:
273 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_LEFT
] = aluSqrt(0.5);
274 Context
->ChannelMatrix
[FRONT_CENTER
][FRONT_RIGHT
] = aluSqrt(0.5);
275 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
276 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
277 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
278 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
279 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
280 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
281 Context
->NumChan
= 4;
282 Speaker2Chan
[0] = BACK_LEFT
;
283 Speaker2Chan
[1] = FRONT_LEFT
;
284 Speaker2Chan
[2] = FRONT_RIGHT
;
285 Speaker2Chan
[3] = BACK_RIGHT
;
286 SpeakerAngle
[0] = -135.0f
* M_PI
/180.0f
;
287 SpeakerAngle
[1] = -45.0f
* M_PI
/180.0f
;
288 SpeakerAngle
[2] = 45.0f
* M_PI
/180.0f
;
289 SpeakerAngle
[3] = 135.0f
* M_PI
/180.0f
;
290 SetSpeakerArrangement("layout_QUAD", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
293 case AL_FORMAT_51CHN8
:
294 case AL_FORMAT_51CHN16
:
295 case AL_FORMAT_51CHN32
:
296 Context
->ChannelMatrix
[SIDE_LEFT
][FRONT_LEFT
] = aluSqrt(0.5);
297 Context
->ChannelMatrix
[SIDE_LEFT
][BACK_LEFT
] = aluSqrt(0.5);
298 Context
->ChannelMatrix
[SIDE_RIGHT
][FRONT_RIGHT
] = aluSqrt(0.5);
299 Context
->ChannelMatrix
[SIDE_RIGHT
][BACK_RIGHT
] = aluSqrt(0.5);
300 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
301 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
302 Context
->NumChan
= 5;
303 Speaker2Chan
[0] = BACK_LEFT
;
304 Speaker2Chan
[1] = FRONT_LEFT
;
305 Speaker2Chan
[2] = FRONT_CENTER
;
306 Speaker2Chan
[3] = FRONT_RIGHT
;
307 Speaker2Chan
[4] = BACK_RIGHT
;
308 SpeakerAngle
[0] = -110.0f
* M_PI
/180.0f
;
309 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
310 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
311 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
312 SpeakerAngle
[4] = 110.0f
* M_PI
/180.0f
;
313 SetSpeakerArrangement("layout_51CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
316 case AL_FORMAT_61CHN8
:
317 case AL_FORMAT_61CHN16
:
318 case AL_FORMAT_61CHN32
:
319 Context
->ChannelMatrix
[BACK_LEFT
][BACK_CENTER
] = aluSqrt(0.5);
320 Context
->ChannelMatrix
[BACK_LEFT
][SIDE_LEFT
] = aluSqrt(0.5);
321 Context
->ChannelMatrix
[BACK_RIGHT
][BACK_CENTER
] = aluSqrt(0.5);
322 Context
->ChannelMatrix
[BACK_RIGHT
][SIDE_RIGHT
] = aluSqrt(0.5);
323 Context
->NumChan
= 6;
324 Speaker2Chan
[0] = SIDE_LEFT
;
325 Speaker2Chan
[1] = FRONT_LEFT
;
326 Speaker2Chan
[2] = FRONT_CENTER
;
327 Speaker2Chan
[3] = FRONT_RIGHT
;
328 Speaker2Chan
[4] = SIDE_RIGHT
;
329 Speaker2Chan
[5] = BACK_CENTER
;
330 SpeakerAngle
[0] = -90.0f
* M_PI
/180.0f
;
331 SpeakerAngle
[1] = -30.0f
* M_PI
/180.0f
;
332 SpeakerAngle
[2] = 0.0f
* M_PI
/180.0f
;
333 SpeakerAngle
[3] = 30.0f
* M_PI
/180.0f
;
334 SpeakerAngle
[4] = 90.0f
* M_PI
/180.0f
;
335 SpeakerAngle
[5] = 180.0f
* M_PI
/180.0f
;
336 SetSpeakerArrangement("layout_61CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
339 case AL_FORMAT_71CHN8
:
340 case AL_FORMAT_71CHN16
:
341 case AL_FORMAT_71CHN32
:
342 Context
->ChannelMatrix
[BACK_CENTER
][BACK_LEFT
] = aluSqrt(0.5);
343 Context
->ChannelMatrix
[BACK_CENTER
][BACK_RIGHT
] = aluSqrt(0.5);
344 Context
->NumChan
= 7;
345 Speaker2Chan
[0] = BACK_LEFT
;
346 Speaker2Chan
[1] = SIDE_LEFT
;
347 Speaker2Chan
[2] = FRONT_LEFT
;
348 Speaker2Chan
[3] = FRONT_CENTER
;
349 Speaker2Chan
[4] = FRONT_RIGHT
;
350 Speaker2Chan
[5] = SIDE_RIGHT
;
351 Speaker2Chan
[6] = BACK_RIGHT
;
352 SpeakerAngle
[0] = -150.0f
* M_PI
/180.0f
;
353 SpeakerAngle
[1] = -90.0f
* M_PI
/180.0f
;
354 SpeakerAngle
[2] = -30.0f
* M_PI
/180.0f
;
355 SpeakerAngle
[3] = 0.0f
* M_PI
/180.0f
;
356 SpeakerAngle
[4] = 30.0f
* M_PI
/180.0f
;
357 SpeakerAngle
[5] = 90.0f
* M_PI
/180.0f
;
358 SpeakerAngle
[6] = 150.0f
* M_PI
/180.0f
;
359 SetSpeakerArrangement("layout_71CHN", SpeakerAngle
, Speaker2Chan
, Context
->NumChan
);
366 for(pos
= 0; pos
< LUT_NUM
; pos
++)
369 Theta
= aluLUTpos2Angle(pos
);
371 /* clear all values */
372 offset
= OUTPUTCHANNELS
* pos
;
373 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
374 Context
->PanningLUT
[offset
+s
] = 0.0f
;
376 /* set panning values */
377 for(s
= 0; s
< Context
->NumChan
- 1; s
++)
379 if(Theta
>= SpeakerAngle
[s
] && Theta
< SpeakerAngle
[s
+1])
381 /* source between speaker s and speaker s+1 */
382 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
383 (SpeakerAngle
[s
+1]-SpeakerAngle
[s
]);
384 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
385 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
+1]] = sin(Alpha
);
389 if(s
== Context
->NumChan
- 1)
391 /* source between last and first speaker */
392 if(Theta
< SpeakerAngle
[0])
393 Theta
+= 2.0f
* M_PI
;
394 Alpha
= M_PI_2
* (Theta
-SpeakerAngle
[s
]) /
395 (2.0f
* M_PI
+ SpeakerAngle
[0]-SpeakerAngle
[s
]);
396 Context
->PanningLUT
[offset
+ Speaker2Chan
[s
]] = cos(Alpha
);
397 Context
->PanningLUT
[offset
+ Speaker2Chan
[0]] = sin(Alpha
);
402 static __inline ALint
aluCart2LUTpos(ALfloat re
, ALfloat im
)
405 ALfloat denom
= aluFabs(re
) + aluFabs(im
);
407 pos
= (ALint
)(QUADRANT_NUM
*aluFabs(im
) / denom
+ 0.5);
410 pos
= 2 * QUADRANT_NUM
- pos
;
416 static ALvoid
CalcSourceParams(const ALCcontext
*ALContext
,
417 const ALsource
*ALSource
, ALenum isMono
,
418 ALfloat
*drysend
, ALfloat
*wetsend
,
419 ALfloat
*pitch
, ALfloat
*drygainhf
,
422 ALfloat InnerAngle
,OuterAngle
,Angle
,Distance
,DryMix
;
423 ALfloat Direction
[3],Position
[3],SourceToListener
[3];
424 ALfloat MinVolume
,MaxVolume
,MinDist
,MaxDist
,Rolloff
,OuterGainHF
;
425 ALfloat ConeVolume
,ConeHF
,SourceVolume
,ListenerGain
;
426 ALfloat U
[3],V
[3],N
[3];
427 ALfloat DopplerFactor
, DopplerVelocity
, flSpeedOfSound
;
428 ALfloat Matrix
[3][3];
429 ALfloat flAttenuation
;
430 ALfloat RoomAttenuation
[MAX_SENDS
];
431 ALfloat MetersPerUnit
;
432 ALfloat RoomRolloff
[MAX_SENDS
];
433 ALfloat DryGainHF
= 1.0f
;
434 ALfloat DirGain
, AmbientGain
;
436 const ALfloat
*SpeakerGain
;
440 //Get context properties
441 DopplerFactor
= ALContext
->DopplerFactor
* ALSource
->DopplerFactor
;
442 DopplerVelocity
= ALContext
->DopplerVelocity
;
443 flSpeedOfSound
= ALContext
->flSpeedOfSound
;
444 NumSends
= ALContext
->Device
->NumAuxSends
;
446 //Get listener properties
447 ListenerGain
= ALContext
->Listener
.Gain
;
448 MetersPerUnit
= ALContext
->Listener
.MetersPerUnit
;
450 //Get source properties
451 SourceVolume
= ALSource
->flGain
;
452 memcpy(Position
, ALSource
->vPosition
, sizeof(ALSource
->vPosition
));
453 memcpy(Direction
, ALSource
->vOrientation
, sizeof(ALSource
->vOrientation
));
454 MinVolume
= ALSource
->flMinGain
;
455 MaxVolume
= ALSource
->flMaxGain
;
456 MinDist
= ALSource
->flRefDistance
;
457 MaxDist
= ALSource
->flMaxDistance
;
458 Rolloff
= ALSource
->flRollOffFactor
;
459 InnerAngle
= ALSource
->flInnerAngle
;
460 OuterAngle
= ALSource
->flOuterAngle
;
461 OuterGainHF
= ALSource
->OuterGainHF
;
463 //Only apply 3D calculations for mono buffers
464 if(isMono
== AL_FALSE
)
466 //1. Multi-channel buffers always play "normal"
467 pitch
[0] = ALSource
->flPitch
;
469 DryMix
= SourceVolume
;
470 DryMix
= __min(DryMix
,MaxVolume
);
471 DryMix
= __max(DryMix
,MinVolume
);
473 switch(ALSource
->DirectFilter
.type
)
475 case AL_FILTER_LOWPASS
:
476 DryMix
*= ALSource
->DirectFilter
.Gain
;
477 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
481 drysend
[FRONT_LEFT
] = DryMix
* ListenerGain
;
482 drysend
[FRONT_RIGHT
] = DryMix
* ListenerGain
;
483 drysend
[SIDE_LEFT
] = DryMix
* ListenerGain
;
484 drysend
[SIDE_RIGHT
] = DryMix
* ListenerGain
;
485 drysend
[BACK_LEFT
] = DryMix
* ListenerGain
;
486 drysend
[BACK_RIGHT
] = DryMix
* ListenerGain
;
487 drysend
[FRONT_CENTER
] = DryMix
* ListenerGain
;
488 drysend
[BACK_CENTER
] = DryMix
* ListenerGain
;
489 drysend
[LFE
] = DryMix
* ListenerGain
;
490 *drygainhf
= DryGainHF
;
492 for(i
= 0;i
< MAX_SENDS
;i
++)
501 //1. Translate Listener to origin (convert to head relative)
502 // Note that Direction and SourceToListener are *not* transformed.
503 // SourceToListener is used with the source and listener velocities,
504 // which are untransformed, and Direction is used with SourceToListener
505 // for the sound cone
506 if(ALSource
->bHeadRelative
==AL_FALSE
)
508 // Build transform matrix
509 aluCrossproduct(ALContext
->Listener
.Forward
, ALContext
->Listener
.Up
, U
); // Right-vector
510 aluNormalize(U
); // Normalized Right-vector
511 memcpy(V
, ALContext
->Listener
.Up
, sizeof(V
)); // Up-vector
512 aluNormalize(V
); // Normalized Up-vector
513 memcpy(N
, ALContext
->Listener
.Forward
, sizeof(N
)); // At-vector
514 aluNormalize(N
); // Normalized At-vector
515 Matrix
[0][0] = U
[0]; Matrix
[0][1] = V
[0]; Matrix
[0][2] = -N
[0];
516 Matrix
[1][0] = U
[1]; Matrix
[1][1] = V
[1]; Matrix
[1][2] = -N
[1];
517 Matrix
[2][0] = U
[2]; Matrix
[2][1] = V
[2]; Matrix
[2][2] = -N
[2];
519 // Translate source position into listener space
520 Position
[0] -= ALContext
->Listener
.Position
[0];
521 Position
[1] -= ALContext
->Listener
.Position
[1];
522 Position
[2] -= ALContext
->Listener
.Position
[2];
524 SourceToListener
[0] = -Position
[0];
525 SourceToListener
[1] = -Position
[1];
526 SourceToListener
[2] = -Position
[2];
528 // Transform source position into listener space
529 aluMatrixVector(Position
, Matrix
);
533 SourceToListener
[0] = -Position
[0];
534 SourceToListener
[1] = -Position
[1];
535 SourceToListener
[2] = -Position
[2];
537 aluNormalize(SourceToListener
);
538 aluNormalize(Direction
);
540 //2. Calculate distance attenuation
541 Distance
= aluSqrt(aluDotproduct(Position
, Position
));
543 flAttenuation
= 1.0f
;
544 for(i
= 0;i
< MAX_SENDS
;i
++)
546 RoomAttenuation
[i
] = 1.0f
;
548 RoomRolloff
[i
] = ALSource
->RoomRolloffFactor
;
549 if(ALSource
->Send
[i
].Slot
&&
550 ALSource
->Send
[i
].Slot
->effect
.type
== AL_EFFECT_REVERB
)
551 RoomRolloff
[i
] += ALSource
->Send
[i
].Slot
->effect
.Reverb
.RoomRolloffFactor
;
554 switch(ALSource
->DistanceModel
)
556 case AL_INVERSE_DISTANCE_CLAMPED
:
557 Distance
=__max(Distance
,MinDist
);
558 Distance
=__min(Distance
,MaxDist
);
559 if(MaxDist
< MinDist
)
562 case AL_INVERSE_DISTANCE
:
565 if((MinDist
+ (Rolloff
* (Distance
- MinDist
))) > 0.0f
)
566 flAttenuation
= MinDist
/ (MinDist
+ (Rolloff
* (Distance
- MinDist
)));
567 for(i
= 0;i
< NumSends
;i
++)
569 if((MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
))) > 0.0f
)
570 RoomAttenuation
[i
] = MinDist
/ (MinDist
+ (RoomRolloff
[i
] * (Distance
- MinDist
)));
575 case AL_LINEAR_DISTANCE_CLAMPED
:
576 Distance
=__max(Distance
,MinDist
);
577 Distance
=__min(Distance
,MaxDist
);
578 if(MaxDist
< MinDist
)
581 case AL_LINEAR_DISTANCE
:
582 Distance
=__min(Distance
,MaxDist
);
583 if(MaxDist
!= MinDist
)
585 flAttenuation
= 1.0f
- (Rolloff
*(Distance
-MinDist
)/(MaxDist
- MinDist
));
586 for(i
= 0;i
< NumSends
;i
++)
587 RoomAttenuation
[i
] = 1.0f
- (RoomRolloff
[i
]*(Distance
-MinDist
)/(MaxDist
- MinDist
));
591 case AL_EXPONENT_DISTANCE_CLAMPED
:
592 Distance
=__max(Distance
,MinDist
);
593 Distance
=__min(Distance
,MaxDist
);
594 if(MaxDist
< MinDist
)
597 case AL_EXPONENT_DISTANCE
:
598 if(Distance
> 0.0f
&& MinDist
> 0.0f
)
600 flAttenuation
= (ALfloat
)pow(Distance
/MinDist
, -Rolloff
);
601 for(i
= 0;i
< NumSends
;i
++)
602 RoomAttenuation
[i
] = (ALfloat
)pow(Distance
/MinDist
, -RoomRolloff
[i
]);
610 // Source Gain + Attenuation and clamp to Min/Max Gain
611 DryMix
= SourceVolume
* flAttenuation
;
612 DryMix
= __min(DryMix
,MaxVolume
);
613 DryMix
= __max(DryMix
,MinVolume
);
615 for(i
= 0;i
< NumSends
;i
++)
617 ALfloat WetMix
= SourceVolume
* RoomAttenuation
[i
];
618 WetMix
= __min(WetMix
,MaxVolume
);
619 wetsend
[i
] = __max(WetMix
,MinVolume
);
623 // Distance-based air absorption
624 if(ALSource
->AirAbsorptionFactor
> 0.0f
&& ALSource
->DistanceModel
!= AL_NONE
)
626 ALfloat dist
= Distance
-MinDist
;
629 if(dist
< 0.0f
) dist
= 0.0f
;
630 // Absorption calculation is done in dB
631 absorb
= (ALSource
->AirAbsorptionFactor
*AIRABSORBGAINDBHF
) *
632 (dist
*MetersPerUnit
);
633 // Convert dB to linear gain before applying
634 absorb
= pow(10.0, absorb
/20.0);
636 for(i
= 0;i
< MAX_SENDS
;i
++)
637 wetgainhf
[i
] *= absorb
;
640 //3. Apply directional soundcones
641 Angle
= aluAcos(aluDotproduct(Direction
,SourceToListener
)) * 180.0f
/M_PI
;
642 if(Angle
>= InnerAngle
&& Angle
<= OuterAngle
)
644 ALfloat scale
= (Angle
-InnerAngle
) / (OuterAngle
-InnerAngle
);
645 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
)*scale
);
646 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
)*scale
);
647 DryMix
*= ConeVolume
;
648 if(ALSource
->DryGainHFAuto
)
651 else if(Angle
> OuterAngle
)
653 ConeVolume
= (1.0f
+(ALSource
->flOuterGain
-1.0f
));
654 ConeHF
= (1.0f
+(OuterGainHF
-1.0f
));
655 DryMix
*= ConeVolume
;
656 if(ALSource
->DryGainHFAuto
)
665 //4. Calculate Velocity
666 if(DopplerFactor
!= 0.0f
)
668 ALfloat flVSS
, flVLS
= 0.0f
;
669 ALfloat flMaxVelocity
= (DopplerVelocity
* flSpeedOfSound
) /
672 flVSS
= aluDotproduct(ALSource
->vVelocity
, SourceToListener
);
673 if(flVSS
>= flMaxVelocity
)
674 flVSS
= (flMaxVelocity
- 1.0f
);
675 else if(flVSS
<= -flMaxVelocity
)
676 flVSS
= -flMaxVelocity
+ 1.0f
;
678 if(ALSource
->bHeadRelative
== AL_FALSE
)
680 flVLS
= aluDotproduct(ALContext
->Listener
.Velocity
, SourceToListener
);
681 if(flVLS
>= flMaxVelocity
)
682 flVLS
= (flMaxVelocity
- 1.0f
);
683 else if(flVLS
<= -flMaxVelocity
)
684 flVLS
= -flMaxVelocity
+ 1.0f
;
687 pitch
[0] = ALSource
->flPitch
*
688 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVLS
)) /
689 ((flSpeedOfSound
* DopplerVelocity
) - (DopplerFactor
* flVSS
));
692 pitch
[0] = ALSource
->flPitch
;
694 for(i
= 0;i
< NumSends
;i
++)
696 if(ALSource
->Send
[i
].Slot
&&
697 ALSource
->Send
[i
].Slot
->effect
.type
!= AL_EFFECT_NULL
)
699 if(ALSource
->WetGainAuto
)
700 wetsend
[i
] *= ConeVolume
;
701 if(ALSource
->WetGainHFAuto
)
702 wetgainhf
[i
] *= ConeHF
;
704 if(ALSource
->Send
[i
].Slot
->AuxSendAuto
)
706 // Apply minimal attenuation in place of missing
707 // statistical reverb model.
708 wetsend
[i
] *= pow(DryMix
, 1.0f
/ 2.0f
);
712 // If the slot's auxiliary send auto is off, the data sent to
713 // the effect slot is the same as the dry path, sans filter
716 wetgainhf
[i
] = DryGainHF
;
719 switch(ALSource
->Send
[i
].WetFilter
.type
)
721 case AL_FILTER_LOWPASS
:
722 wetsend
[i
] *= ALSource
->Send
[i
].WetFilter
.Gain
;
723 wetgainhf
[i
] *= ALSource
->Send
[i
].WetFilter
.GainHF
;
726 wetsend
[i
] *= ListenerGain
;
734 for(i
= NumSends
;i
< MAX_SENDS
;i
++)
740 //5. Apply filter gains and filters
741 switch(ALSource
->DirectFilter
.type
)
743 case AL_FILTER_LOWPASS
:
744 DryMix
*= ALSource
->DirectFilter
.Gain
;
745 DryGainHF
*= ALSource
->DirectFilter
.GainHF
;
748 DryMix
*= ListenerGain
;
750 // Use energy-preserving panning algorithm for multi-speaker playback
751 length
= aluSqrt(Position
[0]*Position
[0] + Position
[1]*Position
[1] +
752 Position
[2]*Position
[2]);
753 length
= __max(length
, MinDist
);
756 ALfloat invlen
= 1.0f
/length
;
757 Position
[0] *= invlen
;
758 Position
[1] *= invlen
;
759 Position
[2] *= invlen
;
762 pos
= aluCart2LUTpos(-Position
[2], Position
[0]);
763 SpeakerGain
= &ALContext
->PanningLUT
[OUTPUTCHANNELS
* pos
];
765 DirGain
= aluSqrt(Position
[0]*Position
[0] + Position
[2]*Position
[2]);
766 // elevation adjustment for directional gain. this sucks, but
767 // has low complexity
768 AmbientGain
= 1.0/aluSqrt(ALContext
->NumChan
) * (1.0-DirGain
);
769 for(s
= 0; s
< OUTPUTCHANNELS
; s
++)
771 ALfloat gain
= SpeakerGain
[s
]*DirGain
+ AmbientGain
;
772 drysend
[s
] = DryMix
* gain
;
774 *drygainhf
= DryGainHF
;
777 static __inline ALshort
lerp(ALshort val1
, ALshort val2
, ALint frac
)
779 return val1
+ (((val2
-val1
)*frac
)>>FRACTIONBITS
);
782 static void MixSomeSources(ALCcontext
*ALContext
, float (*DryBuffer
)[OUTPUTCHANNELS
], ALuint SamplesToDo
)
784 static float DummyBuffer
[BUFFERSIZE
];
785 ALfloat
*WetBuffer
[MAX_SENDS
];
786 ALfloat (*Matrix
)[OUTPUTCHANNELS
] = ALContext
->ChannelMatrix
;
787 ALfloat DrySend
[OUTPUTCHANNELS
] = { 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
};
788 ALfloat dryGainStep
[OUTPUTCHANNELS
];
789 ALfloat wetGainStep
[MAX_SENDS
];
794 ALbufferlistitem
*BufferListItem
;
795 ALint64 DataSize64
,DataPos64
;
796 FILTER
*DryFilter
, *WetFilter
[MAX_SENDS
];
797 ALfloat WetSend
[MAX_SENDS
];
798 ALfloat DryGainHF
= 0.0f
;
799 ALfloat WetGainHF
[MAX_SENDS
];
805 if(!(ALSource
=ALContext
->Source
))
808 frequency
= ALContext
->Device
->Frequency
;
810 rampLength
= frequency
* MIN_RAMP_LENGTH
/ 1000;
811 rampLength
= max(rampLength
, SamplesToDo
);
815 State
= ALSource
->state
;
816 while(State
== AL_PLAYING
&& j
< SamplesToDo
)
819 ALuint DataPosInt
= 0;
820 ALuint DataPosFrac
= 0;
823 ALuint Channels
, Frequency
;
827 /* Get buffer info */
828 if(!(Buffer
= ALSource
->ulBufferID
))
830 ALBuffer
= (ALbuffer
*)ALTHUNK_LOOKUPENTRY(Buffer
);
832 Data
= ALBuffer
->data
;
833 Channels
= aluChannelsFromFormat(ALBuffer
->format
);
834 DataSize
= ALBuffer
->size
;
835 DataSize
/= Channels
* aluBytesFromFormat(ALBuffer
->format
);
836 Frequency
= ALBuffer
->frequency
;
838 DataPosInt
= ALSource
->position
;
839 DataPosFrac
= ALSource
->position_fraction
;
841 if(DataPosInt
>= DataSize
)
844 /* Get source info */
845 DryFilter
= &ALSource
->iirFilter
;
846 for(i
= 0;i
< MAX_SENDS
;i
++)
848 WetFilter
[i
] = &ALSource
->Send
[i
].iirFilter
;
849 WetBuffer
[i
] = (ALSource
->Send
[i
].Slot
?
850 ALSource
->Send
[i
].Slot
->WetBuffer
:
854 CalcSourceParams(ALContext
, ALSource
, (Channels
==1)?AL_TRUE
:AL_FALSE
,
855 DrySend
, WetSend
, &Pitch
, &DryGainHF
, WetGainHF
);
856 Pitch
= (Pitch
*Frequency
) / frequency
;
862 /* Update filter coefficients. Calculations based on the I3DL2
864 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ frequency
);
865 /* We use four chained one-pole filters, so we need to take the
866 * fourth root of the squared gain, which is the same as the square
867 * root of the base gain. */
868 /* Be careful with gains < 0.0001, as that causes the coefficient
869 * head towards 1, which will flatten the signal */
870 g
= aluSqrt(__max(DryGainHF
, 0.0001f
));
872 if(g
< 0.9999f
) /* 1-epsilon */
873 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
875 DryFilter
->coeff
= a
;
877 for(i
= 0;i
< MAX_SENDS
;i
++)
879 /* The wet path uses two chained one-pole filters, so take the
880 * base gain (square root of the squared gain) */
881 g
= __max(WetGainHF
[i
], 0.01f
);
883 if(g
< 0.9999f
) /* 1-epsilon */
884 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
886 WetFilter
[i
]->coeff
= a
;
893 /* Multi-channel sources use two chained one-pole filters */
894 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ frequency
);
895 g
= __max(DryGainHF
, 0.01f
);
897 if(g
< 0.9999f
) /* 1-epsilon */
898 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
900 DryFilter
->coeff
= a
;
901 for(i
= 0;i
< MAX_SENDS
;i
++)
902 WetFilter
[i
]->coeff
= 0.0f
;
904 if(DuplicateStereo
&& Channels
== 2)
906 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 1.0f
;
907 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 1.0f
;
908 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 1.0f
;
909 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 1.0f
;
911 else if(DuplicateStereo
)
913 Matrix
[FRONT_LEFT
][SIDE_LEFT
] = 0.0f
;
914 Matrix
[FRONT_RIGHT
][SIDE_RIGHT
] = 0.0f
;
915 Matrix
[FRONT_LEFT
][BACK_LEFT
] = 0.0f
;
916 Matrix
[FRONT_RIGHT
][BACK_RIGHT
] = 0.0f
;
920 /* Compute the gain steps for each output channel */
921 if(ALSource
->FirstStart
)
923 ALSource
->FirstStart
= AL_FALSE
;
924 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
925 dryGainStep
[i
] = 0.0f
;
926 for(i
= 0;i
< MAX_SENDS
;i
++)
927 wetGainStep
[i
] = 0.0f
;
931 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
933 dryGainStep
[i
] = (DrySend
[i
]-ALSource
->DryGains
[i
]) / rampLength
;
934 DrySend
[i
] = ALSource
->DryGains
[i
];
936 for(i
= 0;i
< MAX_SENDS
;i
++)
938 wetGainStep
[i
] = (WetSend
[i
]-ALSource
->WetGains
[i
]) / rampLength
;
939 WetSend
[i
] = ALSource
->WetGains
[i
];
943 /* Compute 18.14 fixed point step */
944 if(Pitch
> (float)MAX_PITCH
)
945 Pitch
= (float)MAX_PITCH
;
946 increment
= (ALint
)(Pitch
*(ALfloat
)(1L<<FRACTIONBITS
));
948 increment
= (1<<FRACTIONBITS
);
950 /* Figure out how many samples we can mix. */
951 DataSize64
= DataSize
;
952 DataSize64
<<= FRACTIONBITS
;
953 DataPos64
= DataPosInt
;
954 DataPos64
<<= FRACTIONBITS
;
955 DataPos64
+= DataPosFrac
;
956 BufferSize
= (ALuint
)((DataSize64
-DataPos64
+(increment
-1)) / increment
);
958 BufferListItem
= ALSource
->queue
;
959 for(i
= 0;i
< ALSource
->BuffersPlayed
&& BufferListItem
;i
++)
960 BufferListItem
= BufferListItem
->next
;
964 ALuint ulExtraSamples
;
966 if(BufferListItem
->next
)
968 NextBuf
= (ALbuffer
*)ALTHUNK_LOOKUPENTRY(BufferListItem
->next
->buffer
);
969 if(NextBuf
&& NextBuf
->data
)
971 ulExtraSamples
= min(NextBuf
->size
, (ALint
)(ALBuffer
->padding
*Channels
*2));
972 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
975 else if(ALSource
->bLooping
)
977 NextBuf
= (ALbuffer
*)ALTHUNK_LOOKUPENTRY(ALSource
->queue
->buffer
);
978 if(NextBuf
&& NextBuf
->data
)
980 ulExtraSamples
= min(NextBuf
->size
, (ALint
)(ALBuffer
->padding
*Channels
*2));
981 memcpy(&Data
[DataSize
*Channels
], NextBuf
->data
, ulExtraSamples
);
985 memset(&Data
[DataSize
*Channels
], 0, (ALBuffer
->padding
*Channels
*2));
987 BufferSize
= min(BufferSize
, (SamplesToDo
-j
));
989 /* Actual sample mixing loop */
991 Data
+= DataPosInt
*Channels
;
993 if(Channels
== 1) /* Mono */
999 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1000 DrySend
[i
] += dryGainStep
[i
];
1001 for(i
= 0;i
< MAX_SENDS
;i
++)
1002 WetSend
[i
] += wetGainStep
[i
];
1004 /* First order interpolator */
1005 value
= lerp(Data
[k
], Data
[k
+1], DataPosFrac
);
1007 /* Direct path final mix buffer and panning */
1008 outsamp
= lpFilter4P(DryFilter
, 0, value
);
1009 DryBuffer
[j
][FRONT_LEFT
] += outsamp
*DrySend
[FRONT_LEFT
];
1010 DryBuffer
[j
][FRONT_RIGHT
] += outsamp
*DrySend
[FRONT_RIGHT
];
1011 DryBuffer
[j
][SIDE_LEFT
] += outsamp
*DrySend
[SIDE_LEFT
];
1012 DryBuffer
[j
][SIDE_RIGHT
] += outsamp
*DrySend
[SIDE_RIGHT
];
1013 DryBuffer
[j
][BACK_LEFT
] += outsamp
*DrySend
[BACK_LEFT
];
1014 DryBuffer
[j
][BACK_RIGHT
] += outsamp
*DrySend
[BACK_RIGHT
];
1015 DryBuffer
[j
][FRONT_CENTER
] += outsamp
*DrySend
[FRONT_CENTER
];
1016 DryBuffer
[j
][BACK_CENTER
] += outsamp
*DrySend
[BACK_CENTER
];
1018 /* Room path final mix buffer and panning */
1019 for(i
= 0;i
< MAX_SENDS
;i
++)
1021 outsamp
= lpFilter2P(WetFilter
[i
], 0, value
);
1022 WetBuffer
[i
][j
] += outsamp
*WetSend
[i
];
1025 DataPosFrac
+= increment
;
1026 k
+= DataPosFrac
>>FRACTIONBITS
;
1027 DataPosFrac
&= FRACTIONMASK
;
1031 else if(Channels
== 2) /* Stereo */
1033 const int chans
[] = {
1034 FRONT_LEFT
, FRONT_RIGHT
1037 #define DO_MIX() do { \
1038 for(i = 0;i < MAX_SENDS;i++) \
1039 WetSend[i] += wetGainStep[i]*BufferSize; \
1040 while(BufferSize--) \
1042 for(i = 0;i < OUTPUTCHANNELS;i++) \
1043 DrySend[i] += dryGainStep[i]; \
1045 for(i = 0;i < Channels;i++) \
1047 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1048 value = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1049 for(out = 0;out < OUTPUTCHANNELS;out++) \
1050 DryBuffer[j][out] += value*Matrix[chans[i]][out]; \
1053 DataPosFrac += increment; \
1054 k += DataPosFrac>>FRACTIONBITS; \
1055 DataPosFrac &= FRACTIONMASK; \
1062 else if(Channels
== 4) /* Quad */
1064 const int chans
[] = {
1065 FRONT_LEFT
, FRONT_RIGHT
,
1066 BACK_LEFT
, BACK_RIGHT
1071 else if(Channels
== 6) /* 5.1 */
1073 const int chans
[] = {
1074 FRONT_LEFT
, FRONT_RIGHT
,
1076 BACK_LEFT
, BACK_RIGHT
1081 else if(Channels
== 7) /* 6.1 */
1083 const int chans
[] = {
1084 FRONT_LEFT
, FRONT_RIGHT
,
1087 SIDE_LEFT
, SIDE_RIGHT
1092 else if(Channels
== 8) /* 7.1 */
1094 const int chans
[] = {
1095 FRONT_LEFT
, FRONT_RIGHT
,
1097 BACK_LEFT
, BACK_RIGHT
,
1098 SIDE_LEFT
, SIDE_RIGHT
1106 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1107 DrySend
[i
] += dryGainStep
[i
]*BufferSize
;
1108 for(i
= 0;i
< MAX_SENDS
;i
++)
1109 WetSend
[i
] += wetGainStep
[i
]*BufferSize
;
1112 DataPosFrac
+= increment
;
1113 k
+= DataPosFrac
>>FRACTIONBITS
;
1114 DataPosFrac
&= FRACTIONMASK
;
1120 /* Update source info */
1121 ALSource
->position
= DataPosInt
;
1122 ALSource
->position_fraction
= DataPosFrac
;
1123 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
1124 ALSource
->DryGains
[i
] = DrySend
[i
];
1125 for(i
= 0;i
< MAX_SENDS
;i
++)
1126 ALSource
->WetGains
[i
] = WetSend
[i
];
1129 /* Handle looping sources */
1130 if(!Buffer
|| DataPosInt
>= DataSize
)
1135 Looping
= ALSource
->bLooping
;
1136 if(ALSource
->BuffersPlayed
< (ALSource
->BuffersInQueue
-1))
1138 BufferListItem
= ALSource
->queue
;
1139 for(i
= 0;i
<= ALSource
->BuffersPlayed
&& BufferListItem
;i
++)
1142 BufferListItem
->bufferstate
= PROCESSED
;
1143 BufferListItem
= BufferListItem
->next
;
1146 ALSource
->ulBufferID
= BufferListItem
->buffer
;
1147 ALSource
->position
= DataPosInt
-DataSize
;
1148 ALSource
->position_fraction
= DataPosFrac
;
1149 ALSource
->BuffersPlayed
++;
1156 ALSource
->state
= AL_STOPPED
;
1157 ALSource
->inuse
= AL_FALSE
;
1158 ALSource
->BuffersPlayed
= ALSource
->BuffersInQueue
;
1159 BufferListItem
= ALSource
->queue
;
1160 while(BufferListItem
!= NULL
)
1162 BufferListItem
->bufferstate
= PROCESSED
;
1163 BufferListItem
= BufferListItem
->next
;
1165 ALSource
->position
= 0;
1166 ALSource
->position_fraction
= 0;
1170 /* alSourceRewind */
1172 ALSource
->state
= AL_PLAYING
;
1173 ALSource
->inuse
= AL_TRUE
;
1174 ALSource
->play
= AL_TRUE
;
1175 ALSource
->BuffersPlayed
= 0;
1176 BufferListItem
= ALSource
->queue
;
1177 while(BufferListItem
!= NULL
)
1179 BufferListItem
->bufferstate
= PENDING
;
1180 BufferListItem
= BufferListItem
->next
;
1182 ALSource
->ulBufferID
= ALSource
->queue
->buffer
;
1184 if(ALSource
->BuffersInQueue
== 1)
1185 ALSource
->position
= DataPosInt
%DataSize
;
1187 ALSource
->position
= DataPosInt
-DataSize
;
1188 ALSource
->position_fraction
= DataPosFrac
;
1194 /* Get source state */
1195 State
= ALSource
->state
;
1198 if((ALSource
=ALSource
->next
) != NULL
)
1199 goto another_source
;
1202 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
)
1204 float (*DryBuffer
)[OUTPUTCHANNELS
];
1206 ALeffectslot
*ALEffectSlot
;
1207 ALCcontext
*ALContext
;
1211 SuspendContext(NULL
);
1213 #if defined(HAVE_FESETROUND)
1214 fpuState
= fegetround();
1215 fesetround(FE_TOWARDZERO
);
1216 #elif defined(HAVE__CONTROLFP)
1217 fpuState
= _controlfp(0, 0);
1218 _controlfp(_RC_CHOP
, _MCW_RC
);
1223 DryBuffer
= device
->DryBuffer
;
1226 /* Setup variables */
1227 SamplesToDo
= min(size
, BUFFERSIZE
);
1229 /* Clear mixing buffer */
1230 memset(DryBuffer
, 0, SamplesToDo
*OUTPUTCHANNELS
*sizeof(ALfloat
));
1232 for(c
= 0;c
< device
->NumContexts
;c
++)
1234 ALContext
= device
->Contexts
[c
];
1235 SuspendContext(ALContext
);
1237 MixSomeSources(ALContext
, DryBuffer
, SamplesToDo
);
1239 /* effect slot processing */
1240 ALEffectSlot
= ALContext
->AuxiliaryEffectSlot
;
1243 if(ALEffectSlot
->EffectState
)
1244 ALEffect_Process(ALEffectSlot
->EffectState
, ALEffectSlot
, SamplesToDo
, ALEffectSlot
->WetBuffer
, DryBuffer
);
1246 for(i
= 0;i
< SamplesToDo
;i
++)
1247 ALEffectSlot
->WetBuffer
[i
] = 0.0f
;
1248 ALEffectSlot
= ALEffectSlot
->next
;
1250 ProcessContext(ALContext
);
1253 //Post processing loop
1254 switch(device
->Format
)
1256 #define CHECK_WRITE_FORMAT(bits, type, func, isWin) \
1257 case AL_FORMAT_MONO##bits: \
1258 for(i = 0;i < SamplesToDo;i++) \
1260 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT] + \
1261 DryBuffer[i][FRONT_RIGHT]); \
1262 buffer = ((type*)buffer) + 1; \
1265 case AL_FORMAT_STEREO##bits: \
1268 for(i = 0;i < SamplesToDo;i++) \
1271 samples[0] = DryBuffer[i][FRONT_LEFT]; \
1272 samples[1] = DryBuffer[i][FRONT_RIGHT]; \
1273 bs2b_cross_feed(device->Bs2b, samples); \
1274 ((type*)buffer)[0] = (func)(samples[0]); \
1275 ((type*)buffer)[1] = (func)(samples[1]); \
1276 buffer = ((type*)buffer) + 2; \
1281 for(i = 0;i < SamplesToDo;i++) \
1283 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1284 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1285 buffer = ((type*)buffer) + 2; \
1289 case AL_FORMAT_QUAD##bits: \
1290 for(i = 0;i < SamplesToDo;i++) \
1292 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1293 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1294 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1295 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1296 buffer = ((type*)buffer) + 4; \
1299 case AL_FORMAT_51CHN##bits: \
1300 for(i = 0;i < SamplesToDo;i++) \
1302 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1303 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1305 /* Of course, Windows can't use the same ordering... */ \
1306 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1307 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1308 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1309 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1311 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1312 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1313 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1314 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1316 buffer = ((type*)buffer) + 6; \
1319 case AL_FORMAT_61CHN##bits: \
1320 for(i = 0;i < SamplesToDo;i++) \
1322 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1323 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1324 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1325 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1326 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_CENTER]); \
1327 ((type*)buffer)[5] = (func)(DryBuffer[i][SIDE_LEFT]); \
1328 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1329 buffer = ((type*)buffer) + 7; \
1332 case AL_FORMAT_71CHN##bits: \
1333 for(i = 0;i < SamplesToDo;i++) \
1335 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1336 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1338 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1339 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1340 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1341 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1343 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1344 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1345 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1346 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1348 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_LEFT]); \
1349 ((type*)buffer)[7] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1350 buffer = ((type*)buffer) + 8; \
1354 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1355 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1357 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 1)
1358 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 1)
1359 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 1)
1361 CHECK_WRITE_FORMAT(8, ALubyte
, aluF2UB
, 0)
1362 CHECK_WRITE_FORMAT(16, ALshort
, aluF2S
, 0)
1363 CHECK_WRITE_FORMAT(32, ALfloat
, aluF2F
, 0)
1365 #undef AL_FORMAT_STEREO32
1366 #undef AL_FORMAT_MONO32
1367 #undef CHECK_WRITE_FORMAT
1373 size
-= SamplesToDo
;
1376 #if defined(HAVE_FESETROUND)
1377 fesetround(fpuState
);
1378 #elif defined(HAVE__CONTROLFP)
1379 _controlfp(fpuState
, 0xfffff);
1382 ProcessContext(NULL
);
1385 ALvoid
aluHandleDisconnect(ALCdevice
*device
)
1389 for(i
= 0;i
< device
->NumContexts
;i
++)
1393 SuspendContext(device
->Contexts
[i
]);
1395 source
= device
->Contexts
[i
]->Source
;
1398 if(source
->state
== AL_PLAYING
)
1400 ALbufferlistitem
*BufferListItem
;
1402 source
->state
= AL_STOPPED
;
1403 source
->inuse
= AL_FALSE
;
1404 source
->BuffersPlayed
= source
->BuffersInQueue
;
1405 BufferListItem
= source
->queue
;
1406 while(BufferListItem
!= NULL
)
1408 BufferListItem
->bufferstate
= PROCESSED
;
1409 BufferListItem
= BufferListItem
->next
;
1411 source
->position
= 0;
1412 source
->position_fraction
= 0;
1414 source
= source
->next
;
1416 ProcessContext(device
->Contexts
[i
]);
1419 device
->Connected
= ALC_FALSE
;