Duplicate stereo onto the side channels as well as the back
[openal-soft.git] / Alc / ALu.c
blob70570666ff3366ccb67f7b9f7432fd1233eb71b7
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 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
23 #include "config.h"
25 #include <math.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "alMain.h"
31 #include "AL/al.h"
32 #include "AL/alc.h"
33 #include "alSource.h"
34 #include "alBuffer.h"
35 #include "alThunk.h"
36 #include "alListener.h"
37 #include "alAuxEffectSlot.h"
38 #include "alu.h"
39 #include "bs2b.h"
40 #include "alReverb.h"
42 #if defined (HAVE_FLOAT_H)
43 #include <float.h>
44 #endif
46 #ifndef M_PI
47 #define M_PI 3.14159265358979323846 /* pi */
48 #define M_PI_2 1.57079632679489661923 /* pi/2 */
49 #endif
51 #if defined(HAVE_STDINT_H)
52 #include <stdint.h>
53 typedef int64_t ALint64;
54 #elif defined(HAVE___INT64)
55 typedef __int64 ALint64;
56 #elif (SIZEOF_LONG == 8)
57 typedef long ALint64;
58 #elif (SIZEOF_LONG_LONG == 8)
59 typedef long long ALint64;
60 #endif
62 #ifdef HAVE_SQRTF
63 #define aluSqrt(x) ((ALfloat)sqrtf((float)(x)))
64 #else
65 #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
66 #endif
68 #ifdef HAVE_ACOSF
69 #define aluAcos(x) ((ALfloat)acosf((float)(x)))
70 #else
71 #define aluAcos(x) ((ALfloat)acos((double)(x)))
72 #endif
74 #ifdef HAVE_ATANF
75 #define aluAtan(x) ((ALfloat)atanf((float)(x)))
76 #else
77 #define aluAtan(x) ((ALfloat)atan((double)(x)))
78 #endif
80 #ifdef HAVE_FABSF
81 #define aluFabs(x) ((ALfloat)fabsf((float)(x)))
82 #else
83 #define aluFabs(x) ((ALfloat)fabs((double)(x)))
84 #endif
86 // fixes for mingw32.
87 #if defined(max) && !defined(__max)
88 #define __max max
89 #endif
90 #if defined(min) && !defined(__min)
91 #define __min min
92 #endif
94 #define BUFFERSIZE 24000
95 #define FRACTIONBITS 14
96 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
97 #define MAX_PITCH 65536
99 /* Minimum ramp length in milliseconds. The value below was chosen to
100 * adequately reduce clicks and pops from harsh gain changes. */
101 #define MIN_RAMP_LENGTH 16
103 ALboolean DuplicateStereo = AL_FALSE;
105 /* NOTE: The AL_FORMAT_REAR* enums aren't handled here be cause they're
106 * converted to AL_FORMAT_QUAD* when loaded */
107 __inline ALuint aluBytesFromFormat(ALenum format)
109 switch(format)
111 case AL_FORMAT_MONO8:
112 case AL_FORMAT_STEREO8:
113 case AL_FORMAT_QUAD8_LOKI:
114 case AL_FORMAT_QUAD8:
115 case AL_FORMAT_51CHN8:
116 case AL_FORMAT_61CHN8:
117 case AL_FORMAT_71CHN8:
118 return 1;
120 case AL_FORMAT_MONO16:
121 case AL_FORMAT_STEREO16:
122 case AL_FORMAT_QUAD16_LOKI:
123 case AL_FORMAT_QUAD16:
124 case AL_FORMAT_51CHN16:
125 case AL_FORMAT_61CHN16:
126 case AL_FORMAT_71CHN16:
127 return 2;
129 case AL_FORMAT_MONO_FLOAT32:
130 case AL_FORMAT_STEREO_FLOAT32:
131 case AL_FORMAT_QUAD32:
132 case AL_FORMAT_51CHN32:
133 case AL_FORMAT_61CHN32:
134 case AL_FORMAT_71CHN32:
135 return 4;
137 default:
138 return 0;
142 __inline ALuint aluChannelsFromFormat(ALenum format)
144 switch(format)
146 case AL_FORMAT_MONO8:
147 case AL_FORMAT_MONO16:
148 case AL_FORMAT_MONO_FLOAT32:
149 return 1;
151 case AL_FORMAT_STEREO8:
152 case AL_FORMAT_STEREO16:
153 case AL_FORMAT_STEREO_FLOAT32:
154 return 2;
156 case AL_FORMAT_QUAD8_LOKI:
157 case AL_FORMAT_QUAD16_LOKI:
158 case AL_FORMAT_QUAD8:
159 case AL_FORMAT_QUAD16:
160 case AL_FORMAT_QUAD32:
161 return 4;
163 case AL_FORMAT_51CHN8:
164 case AL_FORMAT_51CHN16:
165 case AL_FORMAT_51CHN32:
166 return 6;
168 case AL_FORMAT_61CHN8:
169 case AL_FORMAT_61CHN16:
170 case AL_FORMAT_61CHN32:
171 return 7;
173 case AL_FORMAT_71CHN8:
174 case AL_FORMAT_71CHN16:
175 case AL_FORMAT_71CHN32:
176 return 8;
178 default:
179 return 0;
184 static __inline ALfloat lpFilter(FILTER *iir, ALfloat input)
186 ALfloat *history = iir->history;
187 ALfloat a = iir->coeff;
188 ALfloat output = input;
190 output = output + (history[0]-output)*a;
191 history[0] = output;
192 output = output + (history[1]-output)*a;
193 history[1] = output;
194 output = output + (history[2]-output)*a;
195 history[2] = output;
196 output = output + (history[3]-output)*a;
197 history[3] = output;
199 return output;
202 static __inline ALfloat lpFilterMC(FILTER *iir, ALuint chan, ALfloat input)
204 ALfloat *history = &iir->history[chan*2];
205 ALfloat a = iir->coeff;
206 ALfloat output = input;
208 output = output + (history[0]-output)*a;
209 history[0] = output;
210 output = output + (history[1]-output)*a;
211 history[1] = output;
213 return output;
217 static __inline ALshort aluF2S(ALfloat Value)
219 ALint i;
221 i = (ALint)Value;
222 i = __min( 32767, i);
223 i = __max(-32768, i);
224 return ((ALshort)i);
227 static __inline ALvoid aluCrossproduct(ALfloat *inVector1,ALfloat *inVector2,ALfloat *outVector)
229 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
230 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
231 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
234 static __inline ALfloat aluDotproduct(ALfloat *inVector1,ALfloat *inVector2)
236 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
237 inVector1[2]*inVector2[2];
240 static __inline ALvoid aluNormalize(ALfloat *inVector)
242 ALfloat length, inverse_length;
244 length = aluSqrt(aluDotproduct(inVector, inVector));
245 if(length != 0.0f)
247 inverse_length = 1.0f/length;
248 inVector[0] *= inverse_length;
249 inVector[1] *= inverse_length;
250 inVector[2] *= inverse_length;
254 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
256 ALfloat result[3];
258 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
259 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
260 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
261 memcpy(vector, result, sizeof(result));
264 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
265 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
267 const char *confkey;
268 const char *next;
269 const char *sep;
270 const char *end;
271 int i, val;
273 confkey = GetConfigValue(NULL, name, "");
274 next = confkey;
275 while(next && *next)
277 confkey = next;
278 next = strchr(confkey, ',');
279 if(next)
281 do {
282 next++;
283 } while(isspace(*next));
286 sep = strchr(confkey, '=');
287 if(!sep || confkey == sep)
288 continue;
290 end = sep - 1;
291 while(isspace(*end) && end != confkey)
292 end--;
294 if(strncmp(confkey, "fl", end-confkey) == 0)
295 val = FRONT_LEFT;
296 else if(strncmp(confkey, "fr", end-confkey) == 0)
297 val = FRONT_RIGHT;
298 else if(strncmp(confkey, "fc", end-confkey) == 0)
299 val = FRONT_CENTER;
300 else if(strncmp(confkey, "bl", end-confkey) == 0)
301 val = BACK_LEFT;
302 else if(strncmp(confkey, "br", end-confkey) == 0)
303 val = BACK_RIGHT;
304 else if(strncmp(confkey, "bc", end-confkey) == 0)
305 val = BACK_CENTER;
306 else if(strncmp(confkey, "sl", end-confkey) == 0)
307 val = SIDE_LEFT;
308 else if(strncmp(confkey, "sr", end-confkey) == 0)
309 val = SIDE_RIGHT;
310 else
312 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
313 continue;
316 sep++;
317 while(isspace(*sep))
318 sep++;
320 for(i = 0;i < chans;i++)
322 if(Speaker2Chan[i] == val)
324 val = strtol(sep, NULL, 10);
325 if(val >= -180 && val <= 180)
326 SpeakerAngle[i] = val * M_PI/180.0f;
327 else
328 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
329 break;
334 for(i = 1;i < chans;i++)
336 if(SpeakerAngle[i] <= SpeakerAngle[i-1])
338 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i, chans,
339 SpeakerAngle[i-1] * 180.0f/M_PI, SpeakerAngle[i] * 180.0f/M_PI);
340 SpeakerAngle[i] = SpeakerAngle[i-1] + 1 * 180.0f/M_PI;
345 static __inline ALfloat aluLUTpos2Angle(ALint pos)
347 if(pos < QUADRANT_NUM)
348 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
349 if(pos < 2 * QUADRANT_NUM)
350 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
351 if(pos < 3 * QUADRANT_NUM)
352 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
353 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
356 ALvoid aluInitPanning(ALCcontext *Context)
358 ALint pos, offset, s;
359 ALfloat Alpha, Theta;
360 ALfloat SpeakerAngle[OUTPUTCHANNELS];
361 ALint Speaker2Chan[OUTPUTCHANNELS];
363 switch(Context->Device->Format)
365 /* Mono is rendered as stereo, then downmixed during post-process */
366 case AL_FORMAT_MONO8:
367 case AL_FORMAT_MONO16:
368 case AL_FORMAT_MONO_FLOAT32:
369 Context->NumChan = 2;
370 Speaker2Chan[0] = FRONT_LEFT;
371 Speaker2Chan[1] = FRONT_RIGHT;
372 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
373 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
374 break;
376 case AL_FORMAT_STEREO8:
377 case AL_FORMAT_STEREO16:
378 case AL_FORMAT_STEREO_FLOAT32:
379 Context->NumChan = 2;
380 Speaker2Chan[0] = FRONT_LEFT;
381 Speaker2Chan[1] = FRONT_RIGHT;
382 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
383 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
384 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
385 break;
387 case AL_FORMAT_QUAD8:
388 case AL_FORMAT_QUAD16:
389 case AL_FORMAT_QUAD32:
390 Context->NumChan = 4;
391 Speaker2Chan[0] = BACK_LEFT;
392 Speaker2Chan[1] = FRONT_LEFT;
393 Speaker2Chan[2] = FRONT_RIGHT;
394 Speaker2Chan[3] = BACK_RIGHT;
395 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
396 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
397 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
398 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
399 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
400 break;
402 case AL_FORMAT_51CHN8:
403 case AL_FORMAT_51CHN16:
404 case AL_FORMAT_51CHN32:
405 Context->NumChan = 5;
406 Speaker2Chan[0] = BACK_LEFT;
407 Speaker2Chan[1] = FRONT_LEFT;
408 Speaker2Chan[2] = FRONT_CENTER;
409 Speaker2Chan[3] = FRONT_RIGHT;
410 Speaker2Chan[4] = BACK_RIGHT;
411 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
412 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
413 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
414 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
415 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
416 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
417 break;
419 case AL_FORMAT_61CHN8:
420 case AL_FORMAT_61CHN16:
421 case AL_FORMAT_61CHN32:
422 Context->NumChan = 6;
423 Speaker2Chan[0] = SIDE_LEFT;
424 Speaker2Chan[1] = FRONT_LEFT;
425 Speaker2Chan[2] = FRONT_CENTER;
426 Speaker2Chan[3] = FRONT_RIGHT;
427 Speaker2Chan[4] = SIDE_RIGHT;
428 Speaker2Chan[5] = BACK_CENTER;
429 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
430 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
431 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
432 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
433 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
434 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
435 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
436 break;
438 case AL_FORMAT_71CHN8:
439 case AL_FORMAT_71CHN16:
440 case AL_FORMAT_71CHN32:
441 Context->NumChan = 7;
442 Speaker2Chan[0] = BACK_LEFT;
443 Speaker2Chan[1] = SIDE_LEFT;
444 Speaker2Chan[2] = FRONT_LEFT;
445 Speaker2Chan[3] = FRONT_CENTER;
446 Speaker2Chan[4] = FRONT_RIGHT;
447 Speaker2Chan[5] = SIDE_RIGHT;
448 Speaker2Chan[6] = BACK_RIGHT;
449 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
450 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
451 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
452 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
453 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
454 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
455 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
456 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
457 break;
459 default:
460 assert(0);
463 for(pos = 0; pos < LUT_NUM; pos++)
465 /* source angle */
466 Theta = aluLUTpos2Angle(pos);
468 /* clear all values */
469 offset = OUTPUTCHANNELS * pos;
470 for(s = 0; s < OUTPUTCHANNELS; s++)
471 Context->PanningLUT[offset+s] = 0.0f;
473 /* set panning values */
474 for(s = 0; s < Context->NumChan - 1; s++)
476 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
478 /* source between speaker s and speaker s+1 */
479 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
480 (SpeakerAngle[s+1]-SpeakerAngle[s]);
481 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
482 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
483 break;
486 if(s == Context->NumChan - 1)
488 /* source between last and first speaker */
489 if(Theta < SpeakerAngle[0])
490 Theta += 2.0f * M_PI;
491 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
492 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
493 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
494 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
499 static __inline ALint aluCart2LUTpos(ALfloat re, ALfloat im)
501 ALint pos = 0;
502 ALfloat denom = aluFabs(re) + aluFabs(im);
503 if(denom > 0.0f)
504 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
506 if(re < 0.0)
507 pos = 2 * QUADRANT_NUM - pos;
508 if(im < 0.0)
509 pos = LUT_NUM - pos;
510 return pos%LUT_NUM;
513 static ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
514 ALenum isMono, ALfloat *drysend,
515 ALfloat *wetsend, ALfloat *pitch,
516 ALfloat *drygainhf, ALfloat *wetgainhf)
518 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix,WetMix=0.0f;
519 ALfloat Direction[3],Position[3],SourceToListener[3];
520 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
521 ALfloat ConeVolume,SourceVolume,ListenerGain;
522 ALfloat U[3],V[3],N[3];
523 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
524 ALfloat Matrix[3][3];
525 ALfloat flAttenuation;
526 ALfloat RoomAttenuation;
527 ALfloat MetersPerUnit;
528 ALfloat RoomRolloff;
529 ALfloat DryGainHF = 1.0f;
530 ALfloat WetGainHF = 1.0f;
531 ALfloat DirGain, AmbientGain;
532 const ALfloat *SpeakerGain;
533 ALint pos, s;
534 ALfloat cw, a, g;
536 //Get context properties
537 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
538 DopplerVelocity = ALContext->DopplerVelocity;
539 flSpeedOfSound = ALContext->flSpeedOfSound;
541 //Get listener properties
542 ListenerGain = ALContext->Listener.Gain;
543 MetersPerUnit = ALContext->Listener.MetersPerUnit;
545 //Get source properties
546 SourceVolume = ALSource->flGain;
547 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
548 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
549 MinVolume = ALSource->flMinGain;
550 MaxVolume = ALSource->flMaxGain;
551 MinDist = ALSource->flRefDistance;
552 MaxDist = ALSource->flMaxDistance;
553 Rolloff = ALSource->flRollOffFactor;
554 InnerAngle = ALSource->flInnerAngle;
555 OuterAngle = ALSource->flOuterAngle;
556 OuterGainHF = ALSource->OuterGainHF;
557 RoomRolloff = ALSource->RoomRolloffFactor;
559 //Only apply 3D calculations for mono buffers
560 if(isMono != AL_FALSE)
562 //1. Translate Listener to origin (convert to head relative)
563 // Note that Direction and SourceToListener are *not* transformed.
564 // SourceToListener is used with the source and listener velocities,
565 // which are untransformed, and Direction is used with SourceToListener
566 // for the sound cone
567 if(ALSource->bHeadRelative==AL_FALSE)
569 // Build transform matrix
570 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
571 aluNormalize(U); // Normalized Right-vector
572 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
573 aluNormalize(V); // Normalized Up-vector
574 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
575 aluNormalize(N); // Normalized At-vector
576 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
577 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
578 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
580 // Translate source position into listener space
581 Position[0] -= ALContext->Listener.Position[0];
582 Position[1] -= ALContext->Listener.Position[1];
583 Position[2] -= ALContext->Listener.Position[2];
585 SourceToListener[0] = -Position[0];
586 SourceToListener[1] = -Position[1];
587 SourceToListener[2] = -Position[2];
589 // Transform source position and direction into listener space
590 aluMatrixVector(Position, Matrix);
592 else
594 SourceToListener[0] = -Position[0];
595 SourceToListener[1] = -Position[1];
596 SourceToListener[2] = -Position[2];
598 aluNormalize(SourceToListener);
599 aluNormalize(Direction);
601 //2. Calculate distance attenuation
602 Distance = aluSqrt(aluDotproduct(Position, Position));
604 if(ALSource->Send[0].Slot)
606 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
607 RoomRolloff += ALSource->Send[0].Slot->effect.Reverb.RoomRolloffFactor;
610 flAttenuation = 1.0f;
611 RoomAttenuation = 1.0f;
612 switch (ALSource->DistanceModel)
614 case AL_INVERSE_DISTANCE_CLAMPED:
615 Distance=__max(Distance,MinDist);
616 Distance=__min(Distance,MaxDist);
617 if (MaxDist < MinDist)
618 break;
619 //fall-through
620 case AL_INVERSE_DISTANCE:
621 if (MinDist > 0.0f)
623 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
624 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
625 if ((MinDist + (RoomRolloff * (Distance - MinDist))) > 0.0f)
626 RoomAttenuation = MinDist / (MinDist + (RoomRolloff * (Distance - MinDist)));
628 break;
630 case AL_LINEAR_DISTANCE_CLAMPED:
631 Distance=__max(Distance,MinDist);
632 Distance=__min(Distance,MaxDist);
633 if (MaxDist < MinDist)
634 break;
635 //fall-through
636 case AL_LINEAR_DISTANCE:
637 Distance=__min(Distance,MaxDist);
638 if (MaxDist != MinDist)
640 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
641 RoomAttenuation = 1.0f - (RoomRolloff*(Distance-MinDist)/(MaxDist - MinDist));
643 break;
645 case AL_EXPONENT_DISTANCE_CLAMPED:
646 Distance=__max(Distance,MinDist);
647 Distance=__min(Distance,MaxDist);
648 if (MaxDist < MinDist)
649 break;
650 //fall-through
651 case AL_EXPONENT_DISTANCE:
652 if ((Distance > 0.0f) && (MinDist > 0.0f))
654 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
655 RoomAttenuation = (ALfloat)pow(Distance/MinDist, -RoomRolloff);
657 break;
659 case AL_NONE:
660 flAttenuation = 1.0f;
661 RoomAttenuation = 1.0f;
662 break;
665 // Distance-based air absorption
666 if(ALSource->AirAbsorptionFactor > 0.0f && ALContext->DistanceModel != AL_NONE)
668 ALfloat dist = Distance-MinDist;
669 ALfloat absorb;
671 if(dist < 0.0f) dist = 0.0f;
672 // Absorption calculation is done in dB
673 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
674 (Distance*MetersPerUnit);
675 // Convert dB to linear gain before applying
676 absorb = pow(10.0, absorb/20.0);
677 DryGainHF *= absorb;
678 WetGainHF *= absorb;
681 // Source Gain + Attenuation and clamp to Min/Max Gain
682 DryMix = SourceVolume * flAttenuation;
683 DryMix = __min(DryMix,MaxVolume);
684 DryMix = __max(DryMix,MinVolume);
686 WetMix = SourceVolume * RoomAttenuation;
687 WetMix = __min(WetMix,MaxVolume);
688 WetMix = __max(WetMix,MinVolume);
690 //3. Apply directional soundcones
691 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f /
692 3.141592654f;
693 if(Angle >= InnerAngle && Angle <= OuterAngle)
695 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
696 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
697 DryMix *= ConeVolume;
698 if(ALSource->WetGainAuto)
699 WetMix *= ConeVolume;
700 if(ALSource->DryGainHFAuto)
701 DryGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
702 if(ALSource->WetGainHFAuto)
703 WetGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
705 else if(Angle > OuterAngle)
707 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
708 DryMix *= ConeVolume;
709 if(ALSource->WetGainAuto)
710 WetMix *= ConeVolume;
711 if(ALSource->DryGainHFAuto)
712 DryGainHF *= (1.0f+(OuterGainHF-1.0f));
713 if(ALSource->WetGainHFAuto)
714 WetGainHF *= (1.0f+(OuterGainHF-1.0f));
717 //4. Calculate Velocity
718 if(DopplerFactor != 0.0f)
720 ALfloat flVSS, flVLS = 0.0f;
722 if(ALSource->bHeadRelative==AL_FALSE)
723 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
724 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
726 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
728 if (flVSS >= flMaxVelocity)
729 flVSS = (flMaxVelocity - 1.0f);
730 else if (flVSS <= -flMaxVelocity)
731 flVSS = -flMaxVelocity + 1.0f;
733 if (flVLS >= flMaxVelocity)
734 flVLS = (flMaxVelocity - 1.0f);
735 else if (flVLS <= -flMaxVelocity)
736 flVLS = -flMaxVelocity + 1.0f;
738 pitch[0] = ALSource->flPitch *
739 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
740 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
742 else
743 pitch[0] = ALSource->flPitch;
745 if(ALSource->Send[0].Slot &&
746 ALSource->Send[0].Slot->effect.type != AL_EFFECT_NULL)
748 if(ALSource->Send[0].Slot->AuxSendAuto)
750 // Apply minimal attenuation in place of missing statistical
751 // reverb model.
752 WetMix *= pow(DryMix, 1.0f / 2.0f);
754 else
756 // If the slot's auxilliary send auto is off, the data sent to the
757 // effect slot is the same as the dry path, sans filter effects
758 WetMix = DryMix;
759 WetGainHF = DryGainHF;
762 // Note that this is really applied by the effect slot. However,
763 // it's easier (more optimal) to handle it here.
764 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
765 WetGainHF *= ALSource->Send[0].Slot->effect.Reverb.GainHF;
767 else
769 WetMix = 0.0f;
770 WetGainHF = 1.0f;
773 //5. Apply filter gains and filters
774 switch(ALSource->DirectFilter.type)
776 case AL_FILTER_LOWPASS:
777 DryMix *= ALSource->DirectFilter.Gain;
778 DryGainHF *= ALSource->DirectFilter.GainHF;
779 break;
782 switch(ALSource->Send[0].WetFilter.type)
784 case AL_FILTER_LOWPASS:
785 WetMix *= ALSource->Send[0].WetFilter.Gain;
786 WetGainHF *= ALSource->Send[0].WetFilter.GainHF;
787 break;
790 DryMix *= ListenerGain;
791 WetMix *= ListenerGain;
793 // Use energy-preserving panning algorithm for multi-speaker playback
794 aluNormalize(Position);
796 pos = aluCart2LUTpos(-Position[2], Position[0]);
797 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
799 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
800 // elevation adjustment for directional gain. this sucks, but
801 // has low complexity
802 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
803 for(s = 0; s < OUTPUTCHANNELS; s++)
805 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
806 drysend[s] = DryMix * gain;
808 *wetsend = WetMix;
810 // Update filter coefficients. Calculations based on the I3DL2 spec.
811 cw = cos(2.0f*3.141592654f * LOWPASSFREQCUTOFF / ALContext->Frequency);
812 // We use four chained one-pole filters, so we need to take the fourth
813 // root of the squared gain, which is the same as the square root of
814 // the base gain.
815 // Be careful with gains < 0.0001, as that causes the coefficient to
816 // head towards 1, which will flatten the signal
817 g = aluSqrt(__max(DryGainHF, 0.0001f));
818 a = 0.0f;
819 if(g < 0.9999f) // 1-epsilon
820 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
821 ALSource->iirFilter.coeff = a;
823 g = aluSqrt(__max(WetGainHF, 0.0001f));
824 a = 0.0f;
825 if(g < 0.9999f) // 1-epsilon
826 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
827 ALSource->Send[0].iirFilter.coeff = a;
829 *drygainhf = DryGainHF;
830 *wetgainhf = WetGainHF;
832 else
834 //1. Multi-channel buffers always play "normal"
835 pitch[0] = ALSource->flPitch;
837 DryMix = SourceVolume;
839 switch(ALSource->DirectFilter.type)
841 case AL_FILTER_LOWPASS:
842 DryMix *= ALSource->DirectFilter.Gain;
843 DryGainHF *= ALSource->DirectFilter.GainHF;
844 break;
847 drysend[FRONT_LEFT] = DryMix * ListenerGain;
848 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
849 drysend[SIDE_LEFT] = DryMix * ListenerGain;
850 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
851 drysend[BACK_LEFT] = DryMix * ListenerGain;
852 drysend[BACK_RIGHT] = DryMix * ListenerGain;
853 drysend[FRONT_CENTER] = DryMix * ListenerGain;
854 drysend[BACK_CENTER] = DryMix * ListenerGain;
855 drysend[LFE] = DryMix * ListenerGain;
856 *wetsend = 0.0f;
858 cw = cos(2.0f*3.141592654f * LOWPASSFREQCUTOFF / ALContext->Frequency);
859 g = __max(DryGainHF, 0.01f);
860 a = 0.0f;
861 if(g < 0.9999f) // 1-epsilon
862 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
863 ALSource->iirFilter.coeff = a;
864 ALSource->Send[0].iirFilter.coeff = 0.0f;
866 *drygainhf = DryGainHF;
867 *wetgainhf = WetGainHF;
871 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
873 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
876 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
878 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
879 static float WetBuffer[BUFFERSIZE];
880 ALfloat newDrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
881 ALfloat newWetSend = 0.0f;
882 ALfloat DryGainHF = 0.0f;
883 ALfloat WetGainHF = 0.0f;
884 ALfloat *DrySend;
885 ALfloat *WetSend;
886 ALuint rampLength;
887 ALfloat dryGainStep[OUTPUTCHANNELS];
888 ALfloat wetGainStep;
889 ALuint BlockAlign,BufferSize;
890 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
891 ALuint Channels,Frequency,ulExtraSamples;
892 ALfloat Pitch;
893 ALint Looping,State;
894 ALint increment;
895 ALuint Buffer;
896 ALuint SamplesToDo;
897 ALsource *ALSource;
898 ALbuffer *ALBuffer;
899 ALeffectslot *ALEffectSlot;
900 ALfloat value;
901 ALshort *Data;
902 ALuint i,j,k;
903 ALbufferlistitem *BufferListItem;
904 ALuint loop;
905 ALint64 DataSize64,DataPos64;
906 FILTER *DryFilter, *WetFilter;
907 int fpuState;
909 SuspendContext(ALContext);
911 #if defined(HAVE_FESETROUND)
912 fpuState = fegetround();
913 fesetround(FE_TOWARDZERO);
914 #elif defined(HAVE__CONTROLFP)
915 fpuState = _controlfp(0, 0);
916 _controlfp(_RC_CHOP, _MCW_RC);
917 #else
918 (void)fpuState;
919 #endif
921 //Figure output format variables
922 BlockAlign = aluChannelsFromFormat(format);
923 BlockAlign *= aluBytesFromFormat(format);
925 size /= BlockAlign;
926 while(size > 0)
928 //Setup variables
929 SamplesToDo = min(size, BUFFERSIZE);
930 if(ALContext)
932 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
933 ALSource = ALContext->Source;
934 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
936 else
938 ALEffectSlot = NULL;
939 ALSource = NULL;
940 rampLength = 0;
942 rampLength = max(rampLength, SamplesToDo);
944 //Clear mixing buffer
945 memset(WetBuffer, 0, SamplesToDo*sizeof(ALfloat));
946 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
948 //Actual mixing loop
949 while(ALSource)
951 j = 0;
952 State = ALSource->state;
954 while(State == AL_PLAYING && j < SamplesToDo)
956 DataSize = 0;
957 DataPosInt = 0;
958 DataPosFrac = 0;
960 //Get buffer info
961 if((Buffer = ALSource->ulBufferID))
963 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
965 Data = ALBuffer->data;
966 Channels = aluChannelsFromFormat(ALBuffer->format);
967 DataSize = ALBuffer->size;
968 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
969 Frequency = ALBuffer->frequency;
970 DataPosInt = ALSource->position;
971 DataPosFrac = ALSource->position_fraction;
973 if(DataPosInt >= DataSize)
974 goto skipmix;
976 CalcSourceParams(ALContext, ALSource,
977 (Channels==1) ? AL_TRUE : AL_FALSE,
978 newDrySend, &newWetSend, &Pitch,
979 &DryGainHF, &WetGainHF);
981 Pitch = (Pitch*Frequency) / ALContext->Frequency;
983 //Get source info
984 DryFilter = &ALSource->iirFilter;
985 WetFilter = &ALSource->Send[0].iirFilter;
986 DrySend = ALSource->DryGains;
987 WetSend = &ALSource->WetGain;
989 //Compute the gain steps for each output channel
990 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
992 for(i = 0;i < OUTPUTCHANNELS;i++)
994 DrySend[i] = newDrySend[i];
995 dryGainStep[i] = 0;
997 *WetSend = newWetSend;
998 wetGainStep = 0;
1000 else
1002 for(i = 0;i < OUTPUTCHANNELS;i++)
1003 dryGainStep[i] = (newDrySend[i]-DrySend[i]) / rampLength;
1004 wetGainStep = (newWetSend-(*WetSend)) / rampLength;
1006 ALSource->FirstStart = AL_FALSE;
1008 //Compute 18.14 fixed point step
1009 if(Pitch > (float)MAX_PITCH)
1010 Pitch = (float)MAX_PITCH;
1011 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
1012 if(increment <= 0)
1013 increment = (1<<FRACTIONBITS);
1015 //Figure out how many samples we can mix.
1016 DataSize64 = DataSize;
1017 DataSize64 <<= FRACTIONBITS;
1018 DataPos64 = DataPosInt;
1019 DataPos64 <<= FRACTIONBITS;
1020 DataPos64 += DataPosFrac;
1021 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1023 BufferListItem = ALSource->queue;
1024 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
1026 if(BufferListItem)
1027 BufferListItem = BufferListItem->next;
1029 if (BufferListItem)
1031 if (BufferListItem->next)
1033 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1034 if(NextBuf && NextBuf->data)
1036 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1037 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1040 else if (ALSource->bLooping)
1042 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
1043 if (NextBuf && NextBuf->data)
1045 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1046 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1049 else
1050 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1052 BufferSize = min(BufferSize, (SamplesToDo-j));
1054 //Actual sample mixing loop
1055 k = 0;
1056 Data += DataPosInt*Channels;
1058 if(Channels == 1) /* Mono */
1060 ALfloat outsamp;
1062 while(BufferSize--)
1064 for(i = 0;i < OUTPUTCHANNELS;i++)
1065 DrySend[i] += dryGainStep[i];
1066 *WetSend += wetGainStep;
1068 //First order interpolator
1069 value = lerp(Data[k], Data[k+1], DataPosFrac);
1071 //Direct path final mix buffer and panning
1072 outsamp = lpFilter(DryFilter, value);
1073 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1074 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1075 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1076 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1077 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1078 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1079 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1080 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1082 //Room path final mix buffer and panning
1083 outsamp = lpFilter(WetFilter, value);
1084 WetBuffer[j] += outsamp*(*WetSend);
1086 DataPosFrac += increment;
1087 k += DataPosFrac>>FRACTIONBITS;
1088 DataPosFrac &= FRACTIONMASK;
1089 j++;
1092 else if(Channels == 2) /* Stereo */
1094 ALfloat samp1, samp2;
1096 *WetSend += wetGainStep*BufferSize;
1097 while(BufferSize--)
1099 for(i = 0;i < OUTPUTCHANNELS;i++)
1100 DrySend[i] += dryGainStep[i];
1102 samp1 = lerp(Data[k*Channels], Data[(k+1)*Channels], DataPosFrac);
1103 samp1 = lpFilterMC(DryFilter, FRONT_LEFT, samp1);
1105 samp2 = lerp(Data[k*Channels+1], Data[(k+1)*Channels+1], DataPosFrac);
1106 samp2 = lpFilterMC(DryFilter, FRONT_RIGHT, samp2);
1108 DryBuffer[j][FRONT_LEFT] += samp1*DrySend[FRONT_LEFT];
1109 DryBuffer[j][FRONT_RIGHT] += samp2*DrySend[FRONT_RIGHT];
1110 if(DuplicateStereo)
1112 //Duplicate stereo channels on the side and
1113 //back speakers
1114 DryBuffer[j][SIDE_LEFT] += samp1*DrySend[SIDE_LEFT];
1115 DryBuffer[j][SIDE_RIGHT] += samp2*DrySend[SIDE_RIGHT];
1116 DryBuffer[j][BACK_LEFT] += samp1*DrySend[BACK_LEFT];
1117 DryBuffer[j][BACK_RIGHT] += samp2*DrySend[BACK_RIGHT];
1120 DataPosFrac += increment;
1121 k += DataPosFrac>>FRACTIONBITS;
1122 DataPosFrac &= FRACTIONMASK;
1123 j++;
1126 else if(Channels == 4) /* Quad */
1128 const int chans[] = {
1129 FRONT_LEFT, FRONT_RIGHT,
1130 BACK_LEFT, BACK_RIGHT
1133 #define DO_MIX() do { \
1134 *WetSend += wetGainStep*BufferSize; \
1135 while(BufferSize--) \
1137 for(i = 0;i < OUTPUTCHANNELS;i++) \
1138 DrySend[i] += dryGainStep[i]; \
1140 for(i = 0;i < Channels;i++) \
1142 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1143 DryBuffer[j][chans[i]] += lpFilterMC(DryFilter, chans[i], value)*DrySend[chans[i]]; \
1146 DataPosFrac += increment; \
1147 k += DataPosFrac>>FRACTIONBITS; \
1148 DataPosFrac &= FRACTIONMASK; \
1149 j++; \
1151 } while(0)
1153 DO_MIX();
1155 else if(Channels == 6) /* 5.1 */
1157 const int chans[] = {
1158 FRONT_LEFT, FRONT_RIGHT,
1159 FRONT_CENTER, LFE,
1160 BACK_LEFT, BACK_RIGHT
1163 DO_MIX();
1165 else if(Channels == 7) /* 6.1 */
1167 const int chans[] = {
1168 FRONT_LEFT, FRONT_RIGHT,
1169 FRONT_CENTER, LFE,
1170 BACK_CENTER,
1171 SIDE_LEFT, SIDE_RIGHT
1174 DO_MIX();
1176 else if(Channels == 8) /* 7.1 */
1178 const int chans[] = {
1179 FRONT_LEFT, FRONT_RIGHT,
1180 FRONT_CENTER, LFE,
1181 BACK_LEFT, BACK_RIGHT,
1182 SIDE_LEFT, SIDE_RIGHT
1185 DO_MIX();
1186 #undef DO_MIX
1188 else /* Unknown? */
1190 *WetSend += wetGainStep*BufferSize;
1191 for(i = 0;i < OUTPUTCHANNELS;i++)
1192 DrySend[i] += dryGainStep[i]*BufferSize;
1193 while(BufferSize--)
1195 DataPosFrac += increment;
1196 k += DataPosFrac>>FRACTIONBITS;
1197 DataPosFrac &= FRACTIONMASK;
1198 j++;
1201 DataPosInt += k;
1203 //Update source info
1204 ALSource->position = DataPosInt;
1205 ALSource->position_fraction = DataPosFrac;
1207 skipmix: ;
1210 //Handle looping sources
1211 if(!Buffer || DataPosInt >= DataSize)
1213 //queueing
1214 if(ALSource->queue)
1216 Looping = ALSource->bLooping;
1217 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1219 BufferListItem = ALSource->queue;
1220 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1222 if(BufferListItem)
1224 if(!Looping)
1225 BufferListItem->bufferstate = PROCESSED;
1226 BufferListItem = BufferListItem->next;
1229 if(BufferListItem)
1230 ALSource->ulBufferID = BufferListItem->buffer;
1231 ALSource->position = DataPosInt-DataSize;
1232 ALSource->position_fraction = DataPosFrac;
1233 ALSource->BuffersPlayed++;
1235 else
1237 if(!Looping)
1239 /* alSourceStop */
1240 ALSource->state = AL_STOPPED;
1241 ALSource->inuse = AL_FALSE;
1242 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1243 BufferListItem = ALSource->queue;
1244 while(BufferListItem != NULL)
1246 BufferListItem->bufferstate = PROCESSED;
1247 BufferListItem = BufferListItem->next;
1249 ALSource->position = DataSize;
1250 ALSource->position_fraction = 0;
1252 else
1254 /* alSourceRewind */
1255 /* alSourcePlay */
1256 ALSource->state = AL_PLAYING;
1257 ALSource->inuse = AL_TRUE;
1258 ALSource->play = AL_TRUE;
1259 ALSource->BuffersPlayed = 0;
1260 BufferListItem = ALSource->queue;
1261 while(BufferListItem != NULL)
1263 BufferListItem->bufferstate = PENDING;
1264 BufferListItem = BufferListItem->next;
1266 ALSource->ulBufferID = ALSource->queue->buffer;
1268 if(ALSource->BuffersInQueue == 1)
1269 ALSource->position = DataPosInt%DataSize;
1270 else
1271 ALSource->position = DataPosInt-DataSize;
1272 ALSource->position_fraction = DataPosFrac;
1278 //Get source state
1279 State = ALSource->state;
1282 ALSource = ALSource->next;
1285 // effect slot processing
1286 while(ALEffectSlot)
1288 if(ALEffectSlot->effect.type == AL_EFFECT_REVERB)
1289 VerbProcess(ALEffectSlot->ReverbState, SamplesToDo, WetBuffer, DryBuffer);
1291 ALEffectSlot = ALEffectSlot->next;
1294 //Post processing loop
1295 switch(format)
1297 case AL_FORMAT_MONO8:
1298 for(i = 0;i < SamplesToDo;i++)
1300 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1301 buffer = ((ALubyte*)buffer) + 1;
1303 break;
1304 case AL_FORMAT_STEREO8:
1305 if(ALContext && ALContext->bs2b)
1307 for(i = 0;i < SamplesToDo;i++)
1309 float samples[2];
1310 samples[0] = DryBuffer[i][FRONT_LEFT];
1311 samples[1] = DryBuffer[i][FRONT_RIGHT];
1312 bs2b_cross_feed(ALContext->bs2b, samples);
1313 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1314 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1315 buffer = ((ALubyte*)buffer) + 2;
1318 else
1320 for(i = 0;i < SamplesToDo;i++)
1322 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1323 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1324 buffer = ((ALubyte*)buffer) + 2;
1327 break;
1328 case AL_FORMAT_QUAD8:
1329 for(i = 0;i < SamplesToDo;i++)
1331 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1332 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1333 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1334 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1335 buffer = ((ALubyte*)buffer) + 4;
1337 break;
1338 case AL_FORMAT_51CHN8:
1339 for(i = 0;i < SamplesToDo;i++)
1341 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1342 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1343 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1344 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1345 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1346 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1347 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1348 #else
1349 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1350 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1351 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1352 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1353 #endif
1354 buffer = ((ALubyte*)buffer) + 6;
1356 break;
1357 case AL_FORMAT_61CHN8:
1358 for(i = 0;i < SamplesToDo;i++)
1360 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1361 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1362 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1363 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1364 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_CENTER])>>8)+128);
1365 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1366 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1367 buffer = ((ALubyte*)buffer) + 7;
1369 break;
1370 case AL_FORMAT_71CHN8:
1371 for(i = 0;i < SamplesToDo;i++)
1373 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1374 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1375 #ifdef _WIN32
1376 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1377 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1378 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1379 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1380 #else
1381 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1382 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1383 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1384 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1385 #endif
1386 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1387 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1388 buffer = ((ALubyte*)buffer) + 8;
1390 break;
1392 case AL_FORMAT_MONO16:
1393 for(i = 0;i < SamplesToDo;i++)
1395 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1396 buffer = ((ALshort*)buffer) + 1;
1398 break;
1399 case AL_FORMAT_STEREO16:
1400 if(ALContext && ALContext->bs2b)
1402 for(i = 0;i < SamplesToDo;i++)
1404 float samples[2];
1405 samples[0] = DryBuffer[i][FRONT_LEFT];
1406 samples[1] = DryBuffer[i][FRONT_RIGHT];
1407 bs2b_cross_feed(ALContext->bs2b, samples);
1408 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1409 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1410 buffer = ((ALshort*)buffer) + 2;
1413 else
1415 for(i = 0;i < SamplesToDo;i++)
1417 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1418 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1419 buffer = ((ALshort*)buffer) + 2;
1422 break;
1423 case AL_FORMAT_QUAD16:
1424 for(i = 0;i < SamplesToDo;i++)
1426 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1427 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1428 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1429 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1430 buffer = ((ALshort*)buffer) + 4;
1432 break;
1433 case AL_FORMAT_51CHN16:
1434 for(i = 0;i < SamplesToDo;i++)
1436 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1437 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1438 #ifdef _WIN32
1439 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1440 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1441 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1442 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1443 #else
1444 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1445 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1446 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1447 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1448 #endif
1449 buffer = ((ALshort*)buffer) + 6;
1451 break;
1452 case AL_FORMAT_61CHN16:
1453 for(i = 0;i < SamplesToDo;i++)
1455 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1456 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1457 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1458 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1459 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_CENTER]);
1460 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1461 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1462 buffer = ((ALshort*)buffer) + 7;
1464 break;
1465 case AL_FORMAT_71CHN16:
1466 for(i = 0;i < SamplesToDo;i++)
1468 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1469 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1470 #ifdef _WIN32
1471 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1472 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1473 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1474 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1475 #else
1476 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1477 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1478 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1479 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1480 #endif
1481 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1482 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1483 buffer = ((ALshort*)buffer) + 8;
1485 break;
1487 default:
1488 break;
1491 size -= SamplesToDo;
1494 #if defined(HAVE_FESETROUND)
1495 fesetround(fpuState);
1496 #elif defined(HAVE__CONTROLFP)
1497 _controlfp(fpuState, 0xfffff);
1498 #endif
1500 ProcessContext(ALContext);