Add base support for ALC_EXT_disconnect
[openal-soft.git] / Alc / ALu.c
blobcafc31748fd8348780a2e28898451de45919b56e
1 /**
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
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <assert.h>
29 #include "alMain.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
32 #include "alSource.h"
33 #include "alBuffer.h"
34 #include "alThunk.h"
35 #include "alListener.h"
36 #include "alAuxEffectSlot.h"
37 #include "alu.h"
38 #include "bs2b.h"
40 #if defined(HAVE_STDINT_H)
41 #include <stdint.h>
42 typedef int64_t ALint64;
43 #elif defined(HAVE___INT64)
44 typedef __int64 ALint64;
45 #elif (SIZEOF_LONG == 8)
46 typedef long ALint64;
47 #elif (SIZEOF_LONG_LONG == 8)
48 typedef long long ALint64;
49 #endif
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) Value /= 32768.f;
65 else Value /= 32767.f;
66 return Value;
69 static __inline ALshort aluF2S(ALfloat Value)
71 ALint i;
73 i = (ALint)Value;
74 i = __min( 32767, i);
75 i = __max(-32768, i);
76 return ((ALshort)i);
79 static __inline ALubyte aluF2UB(ALfloat Value)
81 ALshort i = aluF2S(Value);
82 return (i>>8)+128;
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));
104 if(length != 0.0f)
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])
115 ALfloat result[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)
126 const char *confkey;
127 const char *next;
128 const char *sep;
129 const char *end;
130 int i, val;
132 confkey = GetConfigValue(NULL, name, "");
133 next = confkey;
134 while(next && *next)
136 confkey = next;
137 next = strchr(confkey, ',');
138 if(next)
140 do {
141 next++;
142 } while(isspace(*next));
145 sep = strchr(confkey, '=');
146 if(!sep || confkey == sep)
147 continue;
149 end = sep - 1;
150 while(isspace(*end) && end != confkey)
151 end--;
152 end++;
154 if(strncmp(confkey, "fl", end-confkey) == 0)
155 val = FRONT_LEFT;
156 else if(strncmp(confkey, "fr", end-confkey) == 0)
157 val = FRONT_RIGHT;
158 else if(strncmp(confkey, "fc", end-confkey) == 0)
159 val = FRONT_CENTER;
160 else if(strncmp(confkey, "bl", end-confkey) == 0)
161 val = BACK_LEFT;
162 else if(strncmp(confkey, "br", end-confkey) == 0)
163 val = BACK_RIGHT;
164 else if(strncmp(confkey, "bc", end-confkey) == 0)
165 val = BACK_CENTER;
166 else if(strncmp(confkey, "sl", end-confkey) == 0)
167 val = SIDE_LEFT;
168 else if(strncmp(confkey, "sr", end-confkey) == 0)
169 val = SIDE_RIGHT;
170 else
172 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
173 continue;
176 sep++;
177 while(isspace(*sep))
178 sep++;
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;
187 else
188 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
189 break;
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++)
225 int s2;
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;
249 break;
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);
268 break;
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);
291 break;
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);
314 break;
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);
337 break;
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);
360 break;
362 default:
363 assert(0);
366 for(pos = 0; pos < LUT_NUM; pos++)
368 /* source angle */
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);
386 break;
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)
404 ALint pos = 0;
405 ALfloat denom = aluFabs(re) + aluFabs(im);
406 if(denom > 0.0f)
407 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
409 if(re < 0.0)
410 pos = 2 * QUADRANT_NUM - pos;
411 if(im < 0.0)
412 pos = LUT_NUM - pos;
413 return pos%LUT_NUM;
416 static ALvoid CalcSourceParams(const ALCcontext *ALContext,
417 const ALsource *ALSource, ALenum isMono,
418 ALfloat *drysend, ALfloat *wetsend,
419 ALfloat *pitch, ALfloat *drygainhf,
420 ALfloat *wetgainhf)
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, flMaxVelocity;
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;
435 ALfloat length;
436 const ALfloat *SpeakerGain;
437 ALint NumSends;
438 ALint pos, s, i;
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. Translate Listener to origin (convert to head relative)
467 // Note that Direction and SourceToListener are *not* transformed.
468 // SourceToListener is used with the source and listener velocities,
469 // which are untransformed, and Direction is used with SourceToListener
470 // for the sound cone
471 if(ALSource->bHeadRelative==AL_FALSE)
473 // Build transform matrix
474 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
475 aluNormalize(U); // Normalized Right-vector
476 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
477 aluNormalize(V); // Normalized Up-vector
478 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
479 aluNormalize(N); // Normalized At-vector
480 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
481 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
482 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
484 // Translate source position into listener space
485 Position[0] -= ALContext->Listener.Position[0];
486 Position[1] -= ALContext->Listener.Position[1];
487 Position[2] -= ALContext->Listener.Position[2];
489 SourceToListener[0] = -Position[0];
490 SourceToListener[1] = -Position[1];
491 SourceToListener[2] = -Position[2];
493 // Transform source position into listener space
494 aluMatrixVector(Position, Matrix);
496 else
498 SourceToListener[0] = -Position[0];
499 SourceToListener[1] = -Position[1];
500 SourceToListener[2] = -Position[2];
502 aluNormalize(SourceToListener);
503 aluNormalize(Direction);
505 //2. Calculate distance attenuation
506 Distance = aluSqrt(aluDotproduct(Position, Position));
508 flAttenuation = 1.0f;
509 for(i = 0;i < MAX_SENDS;i++)
511 RoomAttenuation[i] = 1.0f;
513 RoomRolloff[i] = ALSource->RoomRolloffFactor;
514 if(ALSource->Send[i].Slot &&
515 ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB)
516 RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
519 switch (ALSource->DistanceModel)
521 case AL_INVERSE_DISTANCE_CLAMPED:
522 Distance=__max(Distance,MinDist);
523 Distance=__min(Distance,MaxDist);
524 if (MaxDist < MinDist)
525 break;
526 //fall-through
527 case AL_INVERSE_DISTANCE:
528 if (MinDist > 0.0f)
530 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
531 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
532 for(i = 0;i < NumSends;i++)
534 if ((MinDist + (RoomRolloff[i] * (Distance - MinDist))) > 0.0f)
535 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (Distance - MinDist)));
538 break;
540 case AL_LINEAR_DISTANCE_CLAMPED:
541 Distance=__max(Distance,MinDist);
542 Distance=__min(Distance,MaxDist);
543 if (MaxDist < MinDist)
544 break;
545 //fall-through
546 case AL_LINEAR_DISTANCE:
547 Distance=__min(Distance,MaxDist);
548 if (MaxDist != MinDist)
550 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
551 for(i = 0;i < NumSends;i++)
552 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(Distance-MinDist)/(MaxDist - MinDist));
554 break;
556 case AL_EXPONENT_DISTANCE_CLAMPED:
557 Distance=__max(Distance,MinDist);
558 Distance=__min(Distance,MaxDist);
559 if (MaxDist < MinDist)
560 break;
561 //fall-through
562 case AL_EXPONENT_DISTANCE:
563 if ((Distance > 0.0f) && (MinDist > 0.0f))
565 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
566 for(i = 0;i < NumSends;i++)
567 RoomAttenuation[i] = (ALfloat)pow(Distance/MinDist, -RoomRolloff[i]);
569 break;
571 case AL_NONE:
572 break;
575 // Source Gain + Attenuation and clamp to Min/Max Gain
576 DryMix = SourceVolume * flAttenuation;
577 DryMix = __min(DryMix,MaxVolume);
578 DryMix = __max(DryMix,MinVolume);
580 for(i = 0;i < NumSends;i++)
582 ALfloat WetMix = SourceVolume * RoomAttenuation[i];
583 WetMix = __min(WetMix,MaxVolume);
584 wetsend[i] = __max(WetMix,MinVolume);
585 wetgainhf[i] = 1.0f;
588 // Distance-based air absorption
589 if(ALSource->AirAbsorptionFactor > 0.0f && ALSource->DistanceModel != AL_NONE)
591 ALfloat dist = Distance-MinDist;
592 ALfloat absorb;
594 if(dist < 0.0f) dist = 0.0f;
595 // Absorption calculation is done in dB
596 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
597 (dist*MetersPerUnit);
598 // Convert dB to linear gain before applying
599 absorb = pow(10.0, absorb/20.0);
600 DryGainHF *= absorb;
601 for(i = 0;i < MAX_SENDS;i++)
602 wetgainhf[i] *= absorb;
605 //3. Apply directional soundcones
606 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
607 if(Angle >= InnerAngle && Angle <= OuterAngle)
609 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
610 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
611 ConeHF = (1.0f+(OuterGainHF-1.0f)*scale);
612 DryMix *= ConeVolume;
613 if(ALSource->DryGainHFAuto)
614 DryGainHF *= ConeHF;
616 else if(Angle > OuterAngle)
618 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
619 ConeHF = (1.0f+(OuterGainHF-1.0f));
620 DryMix *= ConeVolume;
621 if(ALSource->DryGainHFAuto)
622 DryGainHF *= ConeHF;
624 else
626 ConeVolume = 1.0f;
627 ConeHF = 1.0f;
630 //4. Calculate Velocity
631 if(DopplerFactor != 0.0f)
633 ALfloat flVSS, flVLS = 0.0f;
635 if(ALSource->bHeadRelative==AL_FALSE)
636 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
637 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
639 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
641 if (flVSS >= flMaxVelocity)
642 flVSS = (flMaxVelocity - 1.0f);
643 else if (flVSS <= -flMaxVelocity)
644 flVSS = -flMaxVelocity + 1.0f;
646 if (flVLS >= flMaxVelocity)
647 flVLS = (flMaxVelocity - 1.0f);
648 else if (flVLS <= -flMaxVelocity)
649 flVLS = -flMaxVelocity + 1.0f;
651 pitch[0] = ALSource->flPitch *
652 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
653 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
655 else
656 pitch[0] = ALSource->flPitch;
658 for(i = 0;i < NumSends;i++)
660 if(ALSource->Send[i].Slot &&
661 ALSource->Send[i].Slot->effect.type != AL_EFFECT_NULL)
663 if(ALSource->WetGainAuto)
664 wetsend[i] *= ConeVolume;
665 if(ALSource->WetGainHFAuto)
666 wetgainhf[i] *= ConeHF;
668 if(ALSource->Send[i].Slot->AuxSendAuto)
670 // Apply minimal attenuation in place of missing
671 // statistical reverb model.
672 wetsend[i] *= pow(DryMix, 1.0f / 2.0f);
674 else
676 // If the slot's auxilliary send auto is off, the data sent to the
677 // effect slot is the same as the dry path, sans filter effects
678 wetsend[i] = DryMix;
679 wetgainhf[i] = DryGainHF;
682 switch(ALSource->Send[i].WetFilter.type)
684 case AL_FILTER_LOWPASS:
685 wetsend[i] *= ALSource->Send[i].WetFilter.Gain;
686 wetgainhf[i] *= ALSource->Send[i].WetFilter.GainHF;
687 break;
689 wetsend[i] *= ListenerGain;
691 else
693 wetsend[i] = 0.0f;
694 wetgainhf[i] = 1.0f;
697 for(i = NumSends;i < MAX_SENDS;i++)
699 wetsend[i] = 0.0f;
700 wetgainhf[i] = 1.0f;
703 //5. Apply filter gains and filters
704 switch(ALSource->DirectFilter.type)
706 case AL_FILTER_LOWPASS:
707 DryMix *= ALSource->DirectFilter.Gain;
708 DryGainHF *= ALSource->DirectFilter.GainHF;
709 break;
711 DryMix *= ListenerGain;
713 // Use energy-preserving panning algorithm for multi-speaker playback
714 length = aluSqrt(Position[0]*Position[0] + Position[1]*Position[1] +
715 Position[2]*Position[2]);
716 length = __max(length, MinDist);
717 if(length > 0.0f)
719 ALfloat invlen = 1.0f/length;
720 Position[0] *= invlen;
721 Position[1] *= invlen;
722 Position[2] *= invlen;
725 pos = aluCart2LUTpos(-Position[2], Position[0]);
726 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
728 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
729 // elevation adjustment for directional gain. this sucks, but
730 // has low complexity
731 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
732 for(s = 0; s < OUTPUTCHANNELS; s++)
734 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
735 drysend[s] = DryMix * gain;
737 *drygainhf = DryGainHF;
739 else
741 //1. Multi-channel buffers always play "normal"
742 pitch[0] = ALSource->flPitch;
744 DryMix = SourceVolume;
745 DryMix = __min(DryMix,MaxVolume);
746 DryMix = __max(DryMix,MinVolume);
748 switch(ALSource->DirectFilter.type)
750 case AL_FILTER_LOWPASS:
751 DryMix *= ALSource->DirectFilter.Gain;
752 DryGainHF *= ALSource->DirectFilter.GainHF;
753 break;
756 drysend[FRONT_LEFT] = DryMix * ListenerGain;
757 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
758 drysend[SIDE_LEFT] = DryMix * ListenerGain;
759 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
760 drysend[BACK_LEFT] = DryMix * ListenerGain;
761 drysend[BACK_RIGHT] = DryMix * ListenerGain;
762 drysend[FRONT_CENTER] = DryMix * ListenerGain;
763 drysend[BACK_CENTER] = DryMix * ListenerGain;
764 drysend[LFE] = DryMix * ListenerGain;
765 *drygainhf = DryGainHF;
767 for(i = 0;i < MAX_SENDS;i++)
769 wetsend[i] = 0.0f;
770 wetgainhf[i] = 1.0f;
775 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
777 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
780 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
782 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
783 static float DummyBuffer[BUFFERSIZE];
784 ALfloat *WetBuffer[MAX_SENDS];
785 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
786 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
787 ALfloat WetSend[MAX_SENDS];
788 ALfloat DryGainHF = 0.0f;
789 ALfloat WetGainHF[MAX_SENDS];
790 ALuint rampLength;
791 ALfloat dryGainStep[OUTPUTCHANNELS];
792 ALfloat wetGainStep[MAX_SENDS];
793 ALuint BlockAlign,BufferSize;
794 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
795 ALuint Channels,Frequency,ulExtraSamples;
796 ALfloat Pitch;
797 ALint Looping,State;
798 ALint increment;
799 ALuint Buffer;
800 ALuint SamplesToDo;
801 ALsource *ALSource;
802 ALbuffer *ALBuffer;
803 ALeffectslot *ALEffectSlot;
804 ALfloat values[OUTPUTCHANNELS];
805 ALfloat value;
806 ALshort *Data;
807 ALuint i,j,k,out;
808 ALfloat cw, a, g;
809 ALbufferlistitem *BufferListItem;
810 ALuint loop;
811 ALint64 DataSize64,DataPos64;
812 FILTER *DryFilter, *WetFilter[MAX_SENDS];
813 int fpuState;
815 SuspendContext(ALContext);
817 #if defined(HAVE_FESETROUND)
818 fpuState = fegetround();
819 fesetround(FE_TOWARDZERO);
820 #elif defined(HAVE__CONTROLFP)
821 fpuState = _controlfp(0, 0);
822 _controlfp(_RC_CHOP, _MCW_RC);
823 #else
824 (void)fpuState;
825 #endif
827 //Figure output format variables
828 BlockAlign = aluChannelsFromFormat(format);
829 BlockAlign *= aluBytesFromFormat(format);
831 size /= BlockAlign;
832 while(size > 0)
834 //Setup variables
835 SamplesToDo = min(size, BUFFERSIZE);
836 if(ALContext)
838 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
839 ALSource = ALContext->Source;
840 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
842 else
844 ALEffectSlot = NULL;
845 ALSource = NULL;
846 rampLength = 0;
848 rampLength = max(rampLength, SamplesToDo);
850 //Clear mixing buffer
851 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
853 //Actual mixing loop
854 while(ALSource)
856 j = 0;
857 State = ALSource->state;
859 while(State == AL_PLAYING && j < SamplesToDo)
861 DataSize = 0;
862 DataPosInt = 0;
863 DataPosFrac = 0;
865 //Get buffer info
866 if((Buffer = ALSource->ulBufferID))
868 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
870 Data = ALBuffer->data;
871 Channels = aluChannelsFromFormat(ALBuffer->format);
872 DataSize = ALBuffer->size;
873 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
874 Frequency = ALBuffer->frequency;
875 DataPosInt = ALSource->position;
876 DataPosFrac = ALSource->position_fraction;
878 if(DataPosInt >= DataSize)
879 goto skipmix;
881 //Get source info
882 DryFilter = &ALSource->iirFilter;
883 for(i = 0;i < MAX_SENDS;i++)
885 WetFilter[i] = &ALSource->Send[i].iirFilter;
886 WetBuffer[i] = (ALSource->Send[i].Slot ?
887 ALSource->Send[i].Slot->WetBuffer :
888 DummyBuffer);
891 CalcSourceParams(ALContext, ALSource,
892 (Channels==1) ? AL_TRUE : AL_FALSE,
893 DrySend, WetSend, &Pitch,
894 &DryGainHF, WetGainHF);
895 Pitch = (Pitch*Frequency) / ALContext->Frequency;
897 if(Channels == 1)
899 // Update filter coefficients. Calculations based on
900 // the I3DL2 spec.
901 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
902 // We use four chained one-pole filters, so we need to
903 // take the fourth root of the squared gain, which is
904 // the same as the square root of the base gain.
905 // Be careful with gains < 0.0001, as that causes the
906 // coefficient to head towards 1, which will flatten
907 // the signal
908 g = aluSqrt(__max(DryGainHF, 0.0001f));
909 a = 0.0f;
910 if(g < 0.9999f) // 1-epsilon
911 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
912 DryFilter->coeff = a;
914 for(i = 0;i < MAX_SENDS;i++)
916 // The wet path uses two chained one-pole filters,
917 // so take the base gain (square root of the
918 // squared gain)
919 g = __max(WetGainHF[i], 0.01f);
920 a = 0.0f;
921 if(g < 0.9999f) // 1-epsilon
922 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
923 WetFilter[i]->coeff = a;
926 else
928 // Multi-channel sources use two chained one-pole
929 // filters
930 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
931 g = __max(DryGainHF, 0.01f);
932 a = 0.0f;
933 if(g < 0.9999f) // 1-epsilon
934 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
935 DryFilter->coeff = a;
936 for(i = 0;i < MAX_SENDS;i++)
937 WetFilter[i]->coeff = 0.0f;
939 if(DuplicateStereo && Channels == 2)
941 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
942 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
943 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
944 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
946 else if(DuplicateStereo)
948 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
949 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
950 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
951 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
955 //Compute the gain steps for each output channel
956 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
958 for(i = 0;i < OUTPUTCHANNELS;i++)
959 dryGainStep[i] = 0.0f;
960 for(i = 0;i < MAX_SENDS;i++)
961 wetGainStep[i] = 0.0f;
963 else
965 for(i = 0;i < OUTPUTCHANNELS;i++)
967 dryGainStep[i] = (DrySend[i]-ALSource->DryGains[i]) / rampLength;
968 DrySend[i] = ALSource->DryGains[i];
970 for(i = 0;i < MAX_SENDS;i++)
972 wetGainStep[i] = (WetSend[i]-ALSource->WetGains[i]) / rampLength;
973 WetSend[i] = ALSource->WetGains[i];
976 ALSource->FirstStart = AL_FALSE;
978 //Compute 18.14 fixed point step
979 if(Pitch > (float)MAX_PITCH)
980 Pitch = (float)MAX_PITCH;
981 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
982 if(increment <= 0)
983 increment = (1<<FRACTIONBITS);
985 //Figure out how many samples we can mix.
986 DataSize64 = DataSize;
987 DataSize64 <<= FRACTIONBITS;
988 DataPos64 = DataPosInt;
989 DataPos64 <<= FRACTIONBITS;
990 DataPos64 += DataPosFrac;
991 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
993 BufferListItem = ALSource->queue;
994 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
996 if(BufferListItem)
997 BufferListItem = BufferListItem->next;
999 if (BufferListItem)
1001 if (BufferListItem->next)
1003 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1004 if(NextBuf && NextBuf->data)
1006 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1007 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1010 else if (ALSource->bLooping)
1012 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
1013 if (NextBuf && NextBuf->data)
1015 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1016 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1019 else
1020 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1022 BufferSize = min(BufferSize, (SamplesToDo-j));
1024 //Actual sample mixing loop
1025 k = 0;
1026 Data += DataPosInt*Channels;
1028 if(Channels == 1) /* Mono */
1030 ALfloat outsamp;
1032 while(BufferSize--)
1034 for(i = 0;i < OUTPUTCHANNELS;i++)
1035 DrySend[i] += dryGainStep[i];
1036 for(i = 0;i < MAX_SENDS;i++)
1037 WetSend[i] += wetGainStep[i];
1039 //First order interpolator
1040 value = lerp(Data[k], Data[k+1], DataPosFrac);
1042 //Direct path final mix buffer and panning
1043 outsamp = lpFilter4P(DryFilter, 0, value);
1044 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1045 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1046 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1047 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1048 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1049 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1050 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1051 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1053 //Room path final mix buffer and panning
1054 for(i = 0;i < MAX_SENDS;i++)
1056 outsamp = lpFilter2P(WetFilter[i], 0, value);
1057 WetBuffer[i][j] += outsamp*WetSend[i];
1060 DataPosFrac += increment;
1061 k += DataPosFrac>>FRACTIONBITS;
1062 DataPosFrac &= FRACTIONMASK;
1063 j++;
1066 else if(Channels == 2) /* Stereo */
1068 const int chans[] = {
1069 FRONT_LEFT, FRONT_RIGHT
1072 #define DO_MIX() do { \
1073 for(i = 0;i < MAX_SENDS;i++) \
1074 WetSend[i] += wetGainStep[i]*BufferSize; \
1075 while(BufferSize--) \
1077 for(i = 0;i < OUTPUTCHANNELS;i++) \
1078 DrySend[i] += dryGainStep[i]; \
1080 for(i = 0;i < Channels;i++) \
1082 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1083 values[i] = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1085 for(out = 0;out < OUTPUTCHANNELS;out++) \
1087 ALfloat sum = 0.0f; \
1088 for(i = 0;i < Channels;i++) \
1089 sum += values[i]*Matrix[chans[i]][out]; \
1090 DryBuffer[j][out] += sum; \
1093 DataPosFrac += increment; \
1094 k += DataPosFrac>>FRACTIONBITS; \
1095 DataPosFrac &= FRACTIONMASK; \
1096 j++; \
1098 } while(0)
1100 DO_MIX();
1102 else if(Channels == 4) /* Quad */
1104 const int chans[] = {
1105 FRONT_LEFT, FRONT_RIGHT,
1106 BACK_LEFT, BACK_RIGHT
1109 DO_MIX();
1111 else if(Channels == 6) /* 5.1 */
1113 const int chans[] = {
1114 FRONT_LEFT, FRONT_RIGHT,
1115 FRONT_CENTER, LFE,
1116 BACK_LEFT, BACK_RIGHT
1119 DO_MIX();
1121 else if(Channels == 7) /* 6.1 */
1123 const int chans[] = {
1124 FRONT_LEFT, FRONT_RIGHT,
1125 FRONT_CENTER, LFE,
1126 BACK_CENTER,
1127 SIDE_LEFT, SIDE_RIGHT
1130 DO_MIX();
1132 else if(Channels == 8) /* 7.1 */
1134 const int chans[] = {
1135 FRONT_LEFT, FRONT_RIGHT,
1136 FRONT_CENTER, LFE,
1137 BACK_LEFT, BACK_RIGHT,
1138 SIDE_LEFT, SIDE_RIGHT
1141 DO_MIX();
1142 #undef DO_MIX
1144 else /* Unknown? */
1146 for(i = 0;i < OUTPUTCHANNELS;i++)
1147 DrySend[i] += dryGainStep[i]*BufferSize;
1148 for(i = 0;i < MAX_SENDS;i++)
1149 WetSend[i] += wetGainStep[i]*BufferSize;
1150 while(BufferSize--)
1152 DataPosFrac += increment;
1153 k += DataPosFrac>>FRACTIONBITS;
1154 DataPosFrac &= FRACTIONMASK;
1155 j++;
1158 DataPosInt += k;
1160 //Update source info
1161 ALSource->position = DataPosInt;
1162 ALSource->position_fraction = DataPosFrac;
1163 for(i = 0;i < OUTPUTCHANNELS;i++)
1164 ALSource->DryGains[i] = DrySend[i];
1165 for(i = 0;i < MAX_SENDS;i++)
1166 ALSource->WetGains[i] = WetSend[i];
1168 skipmix: ;
1171 //Handle looping sources
1172 if(!Buffer || DataPosInt >= DataSize)
1174 //queueing
1175 if(ALSource->queue)
1177 Looping = ALSource->bLooping;
1178 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1180 BufferListItem = ALSource->queue;
1181 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1183 if(BufferListItem)
1185 if(!Looping)
1186 BufferListItem->bufferstate = PROCESSED;
1187 BufferListItem = BufferListItem->next;
1190 if(BufferListItem)
1191 ALSource->ulBufferID = BufferListItem->buffer;
1192 ALSource->position = DataPosInt-DataSize;
1193 ALSource->position_fraction = DataPosFrac;
1194 ALSource->BuffersPlayed++;
1196 else
1198 if(!Looping)
1200 /* alSourceStop */
1201 ALSource->state = AL_STOPPED;
1202 ALSource->inuse = AL_FALSE;
1203 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1204 BufferListItem = ALSource->queue;
1205 while(BufferListItem != NULL)
1207 BufferListItem->bufferstate = PROCESSED;
1208 BufferListItem = BufferListItem->next;
1210 ALSource->position = DataSize;
1211 ALSource->position_fraction = 0;
1213 else
1215 /* alSourceRewind */
1216 /* alSourcePlay */
1217 ALSource->state = AL_PLAYING;
1218 ALSource->inuse = AL_TRUE;
1219 ALSource->play = AL_TRUE;
1220 ALSource->BuffersPlayed = 0;
1221 BufferListItem = ALSource->queue;
1222 while(BufferListItem != NULL)
1224 BufferListItem->bufferstate = PENDING;
1225 BufferListItem = BufferListItem->next;
1227 ALSource->ulBufferID = ALSource->queue->buffer;
1229 if(ALSource->BuffersInQueue == 1)
1230 ALSource->position = DataPosInt%DataSize;
1231 else
1232 ALSource->position = DataPosInt-DataSize;
1233 ALSource->position_fraction = DataPosFrac;
1239 //Get source state
1240 State = ALSource->state;
1243 ALSource = ALSource->next;
1246 // effect slot processing
1247 while(ALEffectSlot)
1249 if(ALEffectSlot->EffectState)
1250 ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1252 for(i = 0;i < SamplesToDo;i++)
1253 ALEffectSlot->WetBuffer[i] = 0.0f;
1254 ALEffectSlot = ALEffectSlot->next;
1257 //Post processing loop
1258 switch(format)
1260 #define CHECK_WRITE_FORMAT(bits, type, func, isWin) \
1261 case AL_FORMAT_MONO##bits: \
1262 for(i = 0;i < SamplesToDo;i++) \
1264 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT] + \
1265 DryBuffer[i][FRONT_RIGHT]); \
1266 buffer = ((type*)buffer) + 1; \
1268 break; \
1269 case AL_FORMAT_STEREO##bits: \
1270 if(ALContext && ALContext->bs2b) \
1272 for(i = 0;i < SamplesToDo;i++) \
1274 float samples[2]; \
1275 samples[0] = DryBuffer[i][FRONT_LEFT]; \
1276 samples[1] = DryBuffer[i][FRONT_RIGHT]; \
1277 bs2b_cross_feed(ALContext->bs2b, samples); \
1278 ((type*)buffer)[0] = (func)(samples[0]); \
1279 ((type*)buffer)[1] = (func)(samples[1]); \
1280 buffer = ((type*)buffer) + 2; \
1283 else \
1285 for(i = 0;i < SamplesToDo;i++) \
1287 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1288 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1289 buffer = ((type*)buffer) + 2; \
1292 break; \
1293 case AL_FORMAT_QUAD##bits: \
1294 for(i = 0;i < SamplesToDo;i++) \
1296 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1297 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1298 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1299 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1300 buffer = ((type*)buffer) + 4; \
1302 break; \
1303 case AL_FORMAT_51CHN##bits: \
1304 for(i = 0;i < SamplesToDo;i++) \
1306 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1307 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1308 if(isWin) { \
1309 /* Of course, Windows can't use the same ordering... */ \
1310 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1311 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1312 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1313 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1314 } else { \
1315 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1316 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1317 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1318 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1320 buffer = ((type*)buffer) + 6; \
1322 break; \
1323 case AL_FORMAT_61CHN##bits: \
1324 for(i = 0;i < SamplesToDo;i++) \
1326 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1327 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1328 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1329 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1330 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_CENTER]); \
1331 ((type*)buffer)[5] = (func)(DryBuffer[i][SIDE_LEFT]); \
1332 ((type*)buffer)[6] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1333 buffer = ((type*)buffer) + 7; \
1335 break; \
1336 case AL_FORMAT_71CHN##bits: \
1337 for(i = 0;i < SamplesToDo;i++) \
1339 ((type*)buffer)[0] = (func)(DryBuffer[i][FRONT_LEFT]); \
1340 ((type*)buffer)[1] = (func)(DryBuffer[i][FRONT_RIGHT]); \
1341 if(isWin) { \
1342 ((type*)buffer)[2] = (func)(DryBuffer[i][FRONT_CENTER]); \
1343 ((type*)buffer)[3] = (func)(DryBuffer[i][LFE]); \
1344 ((type*)buffer)[4] = (func)(DryBuffer[i][BACK_LEFT]); \
1345 ((type*)buffer)[5] = (func)(DryBuffer[i][BACK_RIGHT]); \
1346 } else { \
1347 ((type*)buffer)[2] = (func)(DryBuffer[i][BACK_LEFT]); \
1348 ((type*)buffer)[3] = (func)(DryBuffer[i][BACK_RIGHT]); \
1349 ((type*)buffer)[4] = (func)(DryBuffer[i][FRONT_CENTER]); \
1350 ((type*)buffer)[5] = (func)(DryBuffer[i][LFE]); \
1352 ((ALubyte*)buffer)[6] = (func)(DryBuffer[i][SIDE_LEFT]); \
1353 ((ALubyte*)buffer)[7] = (func)(DryBuffer[i][SIDE_RIGHT]); \
1354 buffer = ((type*)buffer) + 8; \
1356 break;
1358 #define AL_FORMAT_MONO32 AL_FORMAT_MONO_FLOAT32
1359 #define AL_FORMAT_STEREO32 AL_FORMAT_STEREO_FLOAT32
1360 #ifdef _WIN32
1361 CHECK_WRITE_FORMAT(8, ALubyte, aluF2UB, 1)
1362 CHECK_WRITE_FORMAT(16, ALshort, aluF2S, 1)
1363 CHECK_WRITE_FORMAT(32, ALfloat, aluF2F, 1)
1364 #else
1365 CHECK_WRITE_FORMAT(8, ALubyte, aluF2UB, 0)
1366 CHECK_WRITE_FORMAT(16, ALshort, aluF2S, 0)
1367 CHECK_WRITE_FORMAT(32, ALfloat, aluF2F, 0)
1368 #endif
1369 #undef AL_FORMAT_STEREO32
1370 #undef AL_FORMAT_MONO32
1371 #undef CHECK_WRITE_FORMAT
1373 default:
1374 break;
1377 size -= SamplesToDo;
1380 #if defined(HAVE_FESETROUND)
1381 fesetround(fpuState);
1382 #elif defined(HAVE__CONTROLFP)
1383 _controlfp(fpuState, 0xfffff);
1384 #endif
1386 ProcessContext(ALContext);
1389 ALvoid aluHandleDisconnect(ALCdevice *device)
1391 if(!device->IsCaptureDevice)
1393 ALsource *source = NULL;
1395 SuspendContext(device->Context);
1396 if(device->Context)
1397 source = device->Context->Source;
1399 while(source)
1401 if(source->state == AL_PLAYING)
1403 ALbufferlistitem *BufferListItem;
1405 source->state = AL_STOPPED;
1406 source->inuse = AL_FALSE;
1407 source->BuffersPlayed = source->BuffersInQueue;
1408 BufferListItem = source->queue;
1409 while(BufferListItem != NULL)
1411 BufferListItem->bufferstate = PROCESSED;
1412 BufferListItem = BufferListItem->next;
1414 source->position = 0;
1415 source->position_fraction = 0;
1417 source = source->next;
1419 ProcessContext(device->Context);
1422 device->Connected = ALC_FALSE;