Fix incorrect comment
[openal-soft.git] / Alc / ALu.c
blobd3fdf9996ca468f326c03141dd3f9f85f2983564
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 for(s = 0;s < OUTPUTCHANNELS;s++)
365 int s2;
366 for(s2 = 0;s2 < OUTPUTCHANNELS;s2++)
367 Context->ChannelMatrix[s][s2] = ((s==s2) ? 1.0f : 0.0f);
370 switch(Context->Device->Format)
372 /* Mono is rendered as stereo, then downmixed during post-process */
373 case AL_FORMAT_MONO8:
374 case AL_FORMAT_MONO16:
375 case AL_FORMAT_MONO_FLOAT32:
376 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
377 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
378 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
379 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
380 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
381 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
382 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
383 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
384 Context->NumChan = 2;
385 Speaker2Chan[0] = FRONT_LEFT;
386 Speaker2Chan[1] = FRONT_RIGHT;
387 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
388 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
389 break;
391 case AL_FORMAT_STEREO8:
392 case AL_FORMAT_STEREO16:
393 case AL_FORMAT_STEREO_FLOAT32:
394 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
395 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
396 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
397 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
398 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
399 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
400 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
401 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
402 Context->NumChan = 2;
403 Speaker2Chan[0] = FRONT_LEFT;
404 Speaker2Chan[1] = FRONT_RIGHT;
405 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
406 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
407 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
408 break;
410 case AL_FORMAT_QUAD8:
411 case AL_FORMAT_QUAD16:
412 case AL_FORMAT_QUAD32:
413 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
414 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
415 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
416 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
417 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
418 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
419 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
420 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
421 Context->NumChan = 4;
422 Speaker2Chan[0] = BACK_LEFT;
423 Speaker2Chan[1] = FRONT_LEFT;
424 Speaker2Chan[2] = FRONT_RIGHT;
425 Speaker2Chan[3] = BACK_RIGHT;
426 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
427 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
428 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
429 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
430 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
431 break;
433 case AL_FORMAT_51CHN8:
434 case AL_FORMAT_51CHN16:
435 case AL_FORMAT_51CHN32:
436 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
437 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
438 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
439 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
440 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
441 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
442 Context->NumChan = 5;
443 Speaker2Chan[0] = BACK_LEFT;
444 Speaker2Chan[1] = FRONT_LEFT;
445 Speaker2Chan[2] = FRONT_CENTER;
446 Speaker2Chan[3] = FRONT_RIGHT;
447 Speaker2Chan[4] = BACK_RIGHT;
448 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
449 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
450 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
451 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
452 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
453 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
454 break;
456 case AL_FORMAT_61CHN8:
457 case AL_FORMAT_61CHN16:
458 case AL_FORMAT_61CHN32:
459 Context->ChannelMatrix[BACK_LEFT][BACK_CENTER] = aluSqrt(0.5);
460 Context->ChannelMatrix[BACK_LEFT][SIDE_LEFT] = aluSqrt(0.5);
461 Context->ChannelMatrix[BACK_RIGHT][BACK_CENTER] = aluSqrt(0.5);
462 Context->ChannelMatrix[BACK_RIGHT][SIDE_RIGHT] = aluSqrt(0.5);
463 Context->NumChan = 6;
464 Speaker2Chan[0] = SIDE_LEFT;
465 Speaker2Chan[1] = FRONT_LEFT;
466 Speaker2Chan[2] = FRONT_CENTER;
467 Speaker2Chan[3] = FRONT_RIGHT;
468 Speaker2Chan[4] = SIDE_RIGHT;
469 Speaker2Chan[5] = BACK_CENTER;
470 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
471 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
472 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
473 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
474 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
475 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
476 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
477 break;
479 case AL_FORMAT_71CHN8:
480 case AL_FORMAT_71CHN16:
481 case AL_FORMAT_71CHN32:
482 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
483 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
484 Context->NumChan = 7;
485 Speaker2Chan[0] = BACK_LEFT;
486 Speaker2Chan[1] = SIDE_LEFT;
487 Speaker2Chan[2] = FRONT_LEFT;
488 Speaker2Chan[3] = FRONT_CENTER;
489 Speaker2Chan[4] = FRONT_RIGHT;
490 Speaker2Chan[5] = SIDE_RIGHT;
491 Speaker2Chan[6] = BACK_RIGHT;
492 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
493 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
494 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
495 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
496 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
497 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
498 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
499 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
500 break;
502 default:
503 assert(0);
506 for(pos = 0; pos < LUT_NUM; pos++)
508 /* source angle */
509 Theta = aluLUTpos2Angle(pos);
511 /* clear all values */
512 offset = OUTPUTCHANNELS * pos;
513 for(s = 0; s < OUTPUTCHANNELS; s++)
514 Context->PanningLUT[offset+s] = 0.0f;
516 /* set panning values */
517 for(s = 0; s < Context->NumChan - 1; s++)
519 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
521 /* source between speaker s and speaker s+1 */
522 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
523 (SpeakerAngle[s+1]-SpeakerAngle[s]);
524 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
525 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
526 break;
529 if(s == Context->NumChan - 1)
531 /* source between last and first speaker */
532 if(Theta < SpeakerAngle[0])
533 Theta += 2.0f * M_PI;
534 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
535 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
536 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
537 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
542 static __inline ALint aluCart2LUTpos(ALfloat re, ALfloat im)
544 ALint pos = 0;
545 ALfloat denom = aluFabs(re) + aluFabs(im);
546 if(denom > 0.0f)
547 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
549 if(re < 0.0)
550 pos = 2 * QUADRANT_NUM - pos;
551 if(im < 0.0)
552 pos = LUT_NUM - pos;
553 return pos%LUT_NUM;
556 static ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
557 ALenum isMono, ALfloat *drysend,
558 ALfloat *wetsend, ALfloat *pitch,
559 ALfloat *drygainhf, ALfloat *wetgainhf)
561 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix,WetMix=0.0f;
562 ALfloat Direction[3],Position[3],SourceToListener[3];
563 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
564 ALfloat ConeVolume,SourceVolume,ListenerGain;
565 ALfloat U[3],V[3],N[3];
566 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
567 ALfloat Matrix[3][3];
568 ALfloat flAttenuation;
569 ALfloat RoomAttenuation;
570 ALfloat MetersPerUnit;
571 ALfloat RoomRolloff;
572 ALfloat DryGainHF = 1.0f;
573 ALfloat WetGainHF = 1.0f;
574 ALfloat DirGain, AmbientGain;
575 const ALfloat *SpeakerGain;
576 ALint pos, s;
577 ALfloat cw, a, g;
579 //Get context properties
580 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
581 DopplerVelocity = ALContext->DopplerVelocity;
582 flSpeedOfSound = ALContext->flSpeedOfSound;
584 //Get listener properties
585 ListenerGain = ALContext->Listener.Gain;
586 MetersPerUnit = ALContext->Listener.MetersPerUnit;
588 //Get source properties
589 SourceVolume = ALSource->flGain;
590 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
591 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
592 MinVolume = ALSource->flMinGain;
593 MaxVolume = ALSource->flMaxGain;
594 MinDist = ALSource->flRefDistance;
595 MaxDist = ALSource->flMaxDistance;
596 Rolloff = ALSource->flRollOffFactor;
597 InnerAngle = ALSource->flInnerAngle;
598 OuterAngle = ALSource->flOuterAngle;
599 OuterGainHF = ALSource->OuterGainHF;
600 RoomRolloff = ALSource->RoomRolloffFactor;
602 //Only apply 3D calculations for mono buffers
603 if(isMono != AL_FALSE)
605 //1. Translate Listener to origin (convert to head relative)
606 // Note that Direction and SourceToListener are *not* transformed.
607 // SourceToListener is used with the source and listener velocities,
608 // which are untransformed, and Direction is used with SourceToListener
609 // for the sound cone
610 if(ALSource->bHeadRelative==AL_FALSE)
612 // Build transform matrix
613 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
614 aluNormalize(U); // Normalized Right-vector
615 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
616 aluNormalize(V); // Normalized Up-vector
617 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
618 aluNormalize(N); // Normalized At-vector
619 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
620 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
621 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
623 // Translate source position into listener space
624 Position[0] -= ALContext->Listener.Position[0];
625 Position[1] -= ALContext->Listener.Position[1];
626 Position[2] -= ALContext->Listener.Position[2];
628 SourceToListener[0] = -Position[0];
629 SourceToListener[1] = -Position[1];
630 SourceToListener[2] = -Position[2];
632 // Transform source position into listener space
633 aluMatrixVector(Position, Matrix);
635 else
637 SourceToListener[0] = -Position[0];
638 SourceToListener[1] = -Position[1];
639 SourceToListener[2] = -Position[2];
641 aluNormalize(SourceToListener);
642 aluNormalize(Direction);
644 //2. Calculate distance attenuation
645 Distance = aluSqrt(aluDotproduct(Position, Position));
647 if(ALSource->Send[0].Slot)
649 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
650 RoomRolloff += ALSource->Send[0].Slot->effect.Reverb.RoomRolloffFactor;
653 flAttenuation = 1.0f;
654 RoomAttenuation = 1.0f;
655 switch (ALSource->DistanceModel)
657 case AL_INVERSE_DISTANCE_CLAMPED:
658 Distance=__max(Distance,MinDist);
659 Distance=__min(Distance,MaxDist);
660 if (MaxDist < MinDist)
661 break;
662 //fall-through
663 case AL_INVERSE_DISTANCE:
664 if (MinDist > 0.0f)
666 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
667 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
668 if ((MinDist + (RoomRolloff * (Distance - MinDist))) > 0.0f)
669 RoomAttenuation = MinDist / (MinDist + (RoomRolloff * (Distance - MinDist)));
671 break;
673 case AL_LINEAR_DISTANCE_CLAMPED:
674 Distance=__max(Distance,MinDist);
675 Distance=__min(Distance,MaxDist);
676 if (MaxDist < MinDist)
677 break;
678 //fall-through
679 case AL_LINEAR_DISTANCE:
680 Distance=__min(Distance,MaxDist);
681 if (MaxDist != MinDist)
683 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
684 RoomAttenuation = 1.0f - (RoomRolloff*(Distance-MinDist)/(MaxDist - MinDist));
686 break;
688 case AL_EXPONENT_DISTANCE_CLAMPED:
689 Distance=__max(Distance,MinDist);
690 Distance=__min(Distance,MaxDist);
691 if (MaxDist < MinDist)
692 break;
693 //fall-through
694 case AL_EXPONENT_DISTANCE:
695 if ((Distance > 0.0f) && (MinDist > 0.0f))
697 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
698 RoomAttenuation = (ALfloat)pow(Distance/MinDist, -RoomRolloff);
700 break;
702 case AL_NONE:
703 flAttenuation = 1.0f;
704 RoomAttenuation = 1.0f;
705 break;
708 // Distance-based air absorption
709 if(ALSource->AirAbsorptionFactor > 0.0f && ALContext->DistanceModel != AL_NONE)
711 ALfloat dist = Distance-MinDist;
712 ALfloat absorb;
714 if(dist < 0.0f) dist = 0.0f;
715 // Absorption calculation is done in dB
716 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
717 (Distance*MetersPerUnit);
718 // Convert dB to linear gain before applying
719 absorb = pow(10.0, absorb/20.0);
720 DryGainHF *= absorb;
721 WetGainHF *= absorb;
724 // Source Gain + Attenuation and clamp to Min/Max Gain
725 DryMix = SourceVolume * flAttenuation;
726 DryMix = __min(DryMix,MaxVolume);
727 DryMix = __max(DryMix,MinVolume);
729 WetMix = SourceVolume * RoomAttenuation;
730 WetMix = __min(WetMix,MaxVolume);
731 WetMix = __max(WetMix,MinVolume);
733 //3. Apply directional soundcones
734 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
735 if(Angle >= InnerAngle && Angle <= OuterAngle)
737 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
738 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
739 DryMix *= ConeVolume;
740 if(ALSource->WetGainAuto)
741 WetMix *= ConeVolume;
742 if(ALSource->DryGainHFAuto)
743 DryGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
744 if(ALSource->WetGainHFAuto)
745 WetGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
747 else if(Angle > OuterAngle)
749 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
750 DryMix *= ConeVolume;
751 if(ALSource->WetGainAuto)
752 WetMix *= ConeVolume;
753 if(ALSource->DryGainHFAuto)
754 DryGainHF *= (1.0f+(OuterGainHF-1.0f));
755 if(ALSource->WetGainHFAuto)
756 WetGainHF *= (1.0f+(OuterGainHF-1.0f));
759 //4. Calculate Velocity
760 if(DopplerFactor != 0.0f)
762 ALfloat flVSS, flVLS = 0.0f;
764 if(ALSource->bHeadRelative==AL_FALSE)
765 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
766 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
768 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
770 if (flVSS >= flMaxVelocity)
771 flVSS = (flMaxVelocity - 1.0f);
772 else if (flVSS <= -flMaxVelocity)
773 flVSS = -flMaxVelocity + 1.0f;
775 if (flVLS >= flMaxVelocity)
776 flVLS = (flMaxVelocity - 1.0f);
777 else if (flVLS <= -flMaxVelocity)
778 flVLS = -flMaxVelocity + 1.0f;
780 pitch[0] = ALSource->flPitch *
781 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
782 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
784 else
785 pitch[0] = ALSource->flPitch;
787 if(ALSource->Send[0].Slot &&
788 ALSource->Send[0].Slot->effect.type != AL_EFFECT_NULL)
790 if(ALSource->Send[0].Slot->AuxSendAuto)
792 // Apply minimal attenuation in place of missing statistical
793 // reverb model.
794 WetMix *= pow(DryMix, 1.0f / 2.0f);
796 else
798 // If the slot's auxilliary send auto is off, the data sent to the
799 // effect slot is the same as the dry path, sans filter effects
800 WetMix = DryMix;
801 WetGainHF = DryGainHF;
804 // Note that this is really applied by the effect slot. However,
805 // it's easier (more optimal) to handle it here.
806 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
807 WetGainHF *= ALSource->Send[0].Slot->effect.Reverb.GainHF;
809 else
811 WetMix = 0.0f;
812 WetGainHF = 1.0f;
815 //5. Apply filter gains and filters
816 switch(ALSource->DirectFilter.type)
818 case AL_FILTER_LOWPASS:
819 DryMix *= ALSource->DirectFilter.Gain;
820 DryGainHF *= ALSource->DirectFilter.GainHF;
821 break;
824 switch(ALSource->Send[0].WetFilter.type)
826 case AL_FILTER_LOWPASS:
827 WetMix *= ALSource->Send[0].WetFilter.Gain;
828 WetGainHF *= ALSource->Send[0].WetFilter.GainHF;
829 break;
832 DryMix *= ListenerGain;
833 WetMix *= ListenerGain;
835 // Use energy-preserving panning algorithm for multi-speaker playback
836 aluNormalize(Position);
838 pos = aluCart2LUTpos(-Position[2], Position[0]);
839 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
841 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
842 // elevation adjustment for directional gain. this sucks, but
843 // has low complexity
844 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
845 for(s = 0; s < OUTPUTCHANNELS; s++)
847 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
848 drysend[s] = DryMix * gain;
850 *wetsend = WetMix;
852 // Update filter coefficients. Calculations based on the I3DL2 spec.
853 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
854 // We use four chained one-pole filters, so we need to take the fourth
855 // root of the squared gain, which is the same as the square root of
856 // the base gain.
857 // Be careful with gains < 0.0001, as that causes the coefficient to
858 // head towards 1, which will flatten the signal
859 g = aluSqrt(__max(DryGainHF, 0.0001f));
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;
865 g = aluSqrt(__max(WetGainHF, 0.0001f));
866 a = 0.0f;
867 if(g < 0.9999f) // 1-epsilon
868 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
869 ALSource->Send[0].iirFilter.coeff = a;
871 *drygainhf = DryGainHF;
872 *wetgainhf = WetGainHF;
874 else
876 //1. Multi-channel buffers always play "normal"
877 pitch[0] = ALSource->flPitch;
879 DryMix = SourceVolume;
881 switch(ALSource->DirectFilter.type)
883 case AL_FILTER_LOWPASS:
884 DryMix *= ALSource->DirectFilter.Gain;
885 DryGainHF *= ALSource->DirectFilter.GainHF;
886 break;
889 drysend[FRONT_LEFT] = DryMix * ListenerGain;
890 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
891 drysend[SIDE_LEFT] = DryMix * ListenerGain;
892 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
893 drysend[BACK_LEFT] = DryMix * ListenerGain;
894 drysend[BACK_RIGHT] = DryMix * ListenerGain;
895 drysend[FRONT_CENTER] = DryMix * ListenerGain;
896 drysend[BACK_CENTER] = DryMix * ListenerGain;
897 drysend[LFE] = DryMix * ListenerGain;
898 *wetsend = 0.0f;
900 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
901 g = __max(DryGainHF, 0.01f);
902 a = 0.0f;
903 if(g < 0.9999f) // 1-epsilon
904 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
905 ALSource->iirFilter.coeff = a;
906 ALSource->Send[0].iirFilter.coeff = 0.0f;
908 *drygainhf = DryGainHF;
909 *wetgainhf = WetGainHF;
913 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
915 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
918 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
920 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
921 static float WetBuffer[BUFFERSIZE];
922 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
923 ALfloat newDrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
924 ALfloat newWetSend = 0.0f;
925 ALfloat DryGainHF = 0.0f;
926 ALfloat WetGainHF = 0.0f;
927 ALfloat *DrySend;
928 ALfloat *WetSend;
929 ALuint rampLength;
930 ALfloat dryGainStep[OUTPUTCHANNELS];
931 ALfloat wetGainStep;
932 ALuint BlockAlign,BufferSize;
933 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
934 ALuint Channels,Frequency,ulExtraSamples;
935 ALfloat Pitch;
936 ALint Looping,State;
937 ALint increment;
938 ALuint Buffer;
939 ALuint SamplesToDo;
940 ALsource *ALSource;
941 ALbuffer *ALBuffer;
942 ALeffectslot *ALEffectSlot;
943 ALfloat values[OUTPUTCHANNELS];
944 ALfloat value;
945 ALshort *Data;
946 ALuint i,j,k,out;
947 ALbufferlistitem *BufferListItem;
948 ALuint loop;
949 ALint64 DataSize64,DataPos64;
950 FILTER *DryFilter, *WetFilter;
951 int fpuState;
953 SuspendContext(ALContext);
955 #if defined(HAVE_FESETROUND)
956 fpuState = fegetround();
957 fesetround(FE_TOWARDZERO);
958 #elif defined(HAVE__CONTROLFP)
959 fpuState = _controlfp(0, 0);
960 _controlfp(_RC_CHOP, _MCW_RC);
961 #else
962 (void)fpuState;
963 #endif
965 //Figure output format variables
966 BlockAlign = aluChannelsFromFormat(format);
967 BlockAlign *= aluBytesFromFormat(format);
969 size /= BlockAlign;
970 while(size > 0)
972 //Setup variables
973 SamplesToDo = min(size, BUFFERSIZE);
974 if(ALContext)
976 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
977 ALSource = ALContext->Source;
978 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
980 else
982 ALEffectSlot = NULL;
983 ALSource = NULL;
984 rampLength = 0;
986 rampLength = max(rampLength, SamplesToDo);
988 //Clear mixing buffer
989 memset(WetBuffer, 0, SamplesToDo*sizeof(ALfloat));
990 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
992 //Actual mixing loop
993 while(ALSource)
995 j = 0;
996 State = ALSource->state;
998 while(State == AL_PLAYING && j < SamplesToDo)
1000 DataSize = 0;
1001 DataPosInt = 0;
1002 DataPosFrac = 0;
1004 //Get buffer info
1005 if((Buffer = ALSource->ulBufferID))
1007 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
1009 Data = ALBuffer->data;
1010 Channels = aluChannelsFromFormat(ALBuffer->format);
1011 DataSize = ALBuffer->size;
1012 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
1013 Frequency = ALBuffer->frequency;
1014 DataPosInt = ALSource->position;
1015 DataPosFrac = ALSource->position_fraction;
1017 if(DataPosInt >= DataSize)
1018 goto skipmix;
1020 CalcSourceParams(ALContext, ALSource,
1021 (Channels==1) ? AL_TRUE : AL_FALSE,
1022 newDrySend, &newWetSend, &Pitch,
1023 &DryGainHF, &WetGainHF);
1025 Pitch = (Pitch*Frequency) / ALContext->Frequency;
1027 if(DuplicateStereo && Channels > 1)
1029 if(Channels == 2)
1031 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
1032 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
1033 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
1034 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
1036 else
1038 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
1039 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
1040 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
1041 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
1045 //Get source info
1046 DryFilter = &ALSource->iirFilter;
1047 WetFilter = &ALSource->Send[0].iirFilter;
1048 DrySend = ALSource->DryGains;
1049 WetSend = &ALSource->WetGain;
1051 //Compute the gain steps for each output channel
1052 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
1054 for(i = 0;i < OUTPUTCHANNELS;i++)
1056 DrySend[i] = newDrySend[i];
1057 dryGainStep[i] = 0;
1059 *WetSend = newWetSend;
1060 wetGainStep = 0;
1062 else
1064 for(i = 0;i < OUTPUTCHANNELS;i++)
1065 dryGainStep[i] = (newDrySend[i]-DrySend[i]) / rampLength;
1066 wetGainStep = (newWetSend-(*WetSend)) / rampLength;
1068 ALSource->FirstStart = AL_FALSE;
1070 //Compute 18.14 fixed point step
1071 if(Pitch > (float)MAX_PITCH)
1072 Pitch = (float)MAX_PITCH;
1073 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
1074 if(increment <= 0)
1075 increment = (1<<FRACTIONBITS);
1077 //Figure out how many samples we can mix.
1078 DataSize64 = DataSize;
1079 DataSize64 <<= FRACTIONBITS;
1080 DataPos64 = DataPosInt;
1081 DataPos64 <<= FRACTIONBITS;
1082 DataPos64 += DataPosFrac;
1083 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1085 BufferListItem = ALSource->queue;
1086 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
1088 if(BufferListItem)
1089 BufferListItem = BufferListItem->next;
1091 if (BufferListItem)
1093 if (BufferListItem->next)
1095 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1096 if(NextBuf && NextBuf->data)
1098 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1099 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1102 else if (ALSource->bLooping)
1104 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
1105 if (NextBuf && NextBuf->data)
1107 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1108 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1111 else
1112 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1114 BufferSize = min(BufferSize, (SamplesToDo-j));
1116 //Actual sample mixing loop
1117 k = 0;
1118 Data += DataPosInt*Channels;
1120 if(Channels == 1) /* Mono */
1122 ALfloat outsamp;
1124 while(BufferSize--)
1126 for(i = 0;i < OUTPUTCHANNELS;i++)
1127 DrySend[i] += dryGainStep[i];
1128 *WetSend += wetGainStep;
1130 //First order interpolator
1131 value = lerp(Data[k], Data[k+1], DataPosFrac);
1133 //Direct path final mix buffer and panning
1134 outsamp = lpFilter(DryFilter, value);
1135 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1136 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1137 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1138 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1139 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1140 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1141 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1142 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1144 //Room path final mix buffer and panning
1145 outsamp = lpFilter(WetFilter, value);
1146 WetBuffer[j] += outsamp*(*WetSend);
1148 DataPosFrac += increment;
1149 k += DataPosFrac>>FRACTIONBITS;
1150 DataPosFrac &= FRACTIONMASK;
1151 j++;
1154 else if(Channels == 2) /* Stereo */
1156 const int chans[] = {
1157 FRONT_LEFT, FRONT_RIGHT
1160 #define DO_MIX() do { \
1161 *WetSend += wetGainStep*BufferSize; \
1162 while(BufferSize--) \
1164 for(i = 0;i < OUTPUTCHANNELS;i++) \
1165 DrySend[i] += dryGainStep[i]; \
1167 for(i = 0;i < Channels;i++) \
1169 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1170 values[i] = lpFilterMC(DryFilter, chans[i], value)*DrySend[chans[i]]; \
1172 for(out = 0;out < OUTPUTCHANNELS;out++) \
1174 ALfloat sum = 0.0f; \
1175 for(i = 0;i < Channels;i++) \
1176 sum += values[i]*Matrix[chans[i]][out]; \
1177 DryBuffer[j][out] += sum; \
1180 DataPosFrac += increment; \
1181 k += DataPosFrac>>FRACTIONBITS; \
1182 DataPosFrac &= FRACTIONMASK; \
1183 j++; \
1185 } while(0)
1187 DO_MIX();
1189 else if(Channels == 4) /* Quad */
1191 const int chans[] = {
1192 FRONT_LEFT, FRONT_RIGHT,
1193 BACK_LEFT, BACK_RIGHT
1196 DO_MIX();
1198 else if(Channels == 6) /* 5.1 */
1200 const int chans[] = {
1201 FRONT_LEFT, FRONT_RIGHT,
1202 FRONT_CENTER, LFE,
1203 BACK_LEFT, BACK_RIGHT
1206 DO_MIX();
1208 else if(Channels == 7) /* 6.1 */
1210 const int chans[] = {
1211 FRONT_LEFT, FRONT_RIGHT,
1212 FRONT_CENTER, LFE,
1213 BACK_CENTER,
1214 SIDE_LEFT, SIDE_RIGHT
1217 DO_MIX();
1219 else if(Channels == 8) /* 7.1 */
1221 const int chans[] = {
1222 FRONT_LEFT, FRONT_RIGHT,
1223 FRONT_CENTER, LFE,
1224 BACK_LEFT, BACK_RIGHT,
1225 SIDE_LEFT, SIDE_RIGHT
1228 DO_MIX();
1229 #undef DO_MIX
1231 else /* Unknown? */
1233 *WetSend += wetGainStep*BufferSize;
1234 for(i = 0;i < OUTPUTCHANNELS;i++)
1235 DrySend[i] += dryGainStep[i]*BufferSize;
1236 while(BufferSize--)
1238 DataPosFrac += increment;
1239 k += DataPosFrac>>FRACTIONBITS;
1240 DataPosFrac &= FRACTIONMASK;
1241 j++;
1244 DataPosInt += k;
1246 //Update source info
1247 ALSource->position = DataPosInt;
1248 ALSource->position_fraction = DataPosFrac;
1250 skipmix: ;
1253 //Handle looping sources
1254 if(!Buffer || DataPosInt >= DataSize)
1256 //queueing
1257 if(ALSource->queue)
1259 Looping = ALSource->bLooping;
1260 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1262 BufferListItem = ALSource->queue;
1263 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1265 if(BufferListItem)
1267 if(!Looping)
1268 BufferListItem->bufferstate = PROCESSED;
1269 BufferListItem = BufferListItem->next;
1272 if(BufferListItem)
1273 ALSource->ulBufferID = BufferListItem->buffer;
1274 ALSource->position = DataPosInt-DataSize;
1275 ALSource->position_fraction = DataPosFrac;
1276 ALSource->BuffersPlayed++;
1278 else
1280 if(!Looping)
1282 /* alSourceStop */
1283 ALSource->state = AL_STOPPED;
1284 ALSource->inuse = AL_FALSE;
1285 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1286 BufferListItem = ALSource->queue;
1287 while(BufferListItem != NULL)
1289 BufferListItem->bufferstate = PROCESSED;
1290 BufferListItem = BufferListItem->next;
1292 ALSource->position = DataSize;
1293 ALSource->position_fraction = 0;
1295 else
1297 /* alSourceRewind */
1298 /* alSourcePlay */
1299 ALSource->state = AL_PLAYING;
1300 ALSource->inuse = AL_TRUE;
1301 ALSource->play = AL_TRUE;
1302 ALSource->BuffersPlayed = 0;
1303 BufferListItem = ALSource->queue;
1304 while(BufferListItem != NULL)
1306 BufferListItem->bufferstate = PENDING;
1307 BufferListItem = BufferListItem->next;
1309 ALSource->ulBufferID = ALSource->queue->buffer;
1311 if(ALSource->BuffersInQueue == 1)
1312 ALSource->position = DataPosInt%DataSize;
1313 else
1314 ALSource->position = DataPosInt-DataSize;
1315 ALSource->position_fraction = DataPosFrac;
1321 //Get source state
1322 State = ALSource->state;
1325 ALSource = ALSource->next;
1328 // effect slot processing
1329 while(ALEffectSlot)
1331 if(ALEffectSlot->effect.type == AL_EFFECT_REVERB)
1332 VerbProcess(ALEffectSlot->ReverbState, SamplesToDo, WetBuffer, DryBuffer);
1334 ALEffectSlot = ALEffectSlot->next;
1337 //Post processing loop
1338 switch(format)
1340 case AL_FORMAT_MONO8:
1341 for(i = 0;i < SamplesToDo;i++)
1343 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1344 buffer = ((ALubyte*)buffer) + 1;
1346 break;
1347 case AL_FORMAT_STEREO8:
1348 if(ALContext && ALContext->bs2b)
1350 for(i = 0;i < SamplesToDo;i++)
1352 float samples[2];
1353 samples[0] = DryBuffer[i][FRONT_LEFT];
1354 samples[1] = DryBuffer[i][FRONT_RIGHT];
1355 bs2b_cross_feed(ALContext->bs2b, samples);
1356 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1357 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1358 buffer = ((ALubyte*)buffer) + 2;
1361 else
1363 for(i = 0;i < SamplesToDo;i++)
1365 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1366 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1367 buffer = ((ALubyte*)buffer) + 2;
1370 break;
1371 case AL_FORMAT_QUAD8:
1372 for(i = 0;i < SamplesToDo;i++)
1374 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1375 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1376 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1377 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1378 buffer = ((ALubyte*)buffer) + 4;
1380 break;
1381 case AL_FORMAT_51CHN8:
1382 for(i = 0;i < SamplesToDo;i++)
1384 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1385 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1386 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1387 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1388 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1389 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1390 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1391 #else
1392 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1393 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1394 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1395 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1396 #endif
1397 buffer = ((ALubyte*)buffer) + 6;
1399 break;
1400 case AL_FORMAT_61CHN8:
1401 for(i = 0;i < SamplesToDo;i++)
1403 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1404 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1405 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1406 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1407 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_CENTER])>>8)+128);
1408 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1409 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1410 buffer = ((ALubyte*)buffer) + 7;
1412 break;
1413 case AL_FORMAT_71CHN8:
1414 for(i = 0;i < SamplesToDo;i++)
1416 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1417 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1418 #ifdef _WIN32
1419 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1420 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1421 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1422 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1423 #else
1424 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1425 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1426 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1427 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1428 #endif
1429 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1430 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1431 buffer = ((ALubyte*)buffer) + 8;
1433 break;
1435 case AL_FORMAT_MONO16:
1436 for(i = 0;i < SamplesToDo;i++)
1438 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1439 buffer = ((ALshort*)buffer) + 1;
1441 break;
1442 case AL_FORMAT_STEREO16:
1443 if(ALContext && ALContext->bs2b)
1445 for(i = 0;i < SamplesToDo;i++)
1447 float samples[2];
1448 samples[0] = DryBuffer[i][FRONT_LEFT];
1449 samples[1] = DryBuffer[i][FRONT_RIGHT];
1450 bs2b_cross_feed(ALContext->bs2b, samples);
1451 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1452 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1453 buffer = ((ALshort*)buffer) + 2;
1456 else
1458 for(i = 0;i < SamplesToDo;i++)
1460 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1461 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1462 buffer = ((ALshort*)buffer) + 2;
1465 break;
1466 case AL_FORMAT_QUAD16:
1467 for(i = 0;i < SamplesToDo;i++)
1469 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1470 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1471 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1472 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1473 buffer = ((ALshort*)buffer) + 4;
1475 break;
1476 case AL_FORMAT_51CHN16:
1477 for(i = 0;i < SamplesToDo;i++)
1479 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1480 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1481 #ifdef _WIN32
1482 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1483 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1484 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1485 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1486 #else
1487 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1488 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1489 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1490 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1491 #endif
1492 buffer = ((ALshort*)buffer) + 6;
1494 break;
1495 case AL_FORMAT_61CHN16:
1496 for(i = 0;i < SamplesToDo;i++)
1498 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1499 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1500 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1501 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1502 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_CENTER]);
1503 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1504 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1505 buffer = ((ALshort*)buffer) + 7;
1507 break;
1508 case AL_FORMAT_71CHN16:
1509 for(i = 0;i < SamplesToDo;i++)
1511 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1512 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1513 #ifdef _WIN32
1514 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1515 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1516 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1517 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1518 #else
1519 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1520 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1521 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1522 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1523 #endif
1524 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1525 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1526 buffer = ((ALshort*)buffer) + 8;
1528 break;
1530 default:
1531 break;
1534 size -= SamplesToDo;
1537 #if defined(HAVE_FESETROUND)
1538 fesetround(fpuState);
1539 #elif defined(HAVE__CONTROLFP)
1540 _controlfp(fpuState, 0xfffff);
1541 #endif
1543 ProcessContext(ALContext);