Move the WetBuffer into the effect slot object
[openal-soft.git] / Alc / ALu.c
blob5f98d2f936b299cd4725efa820bc33fbd1ca6a30
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>
29 #include <assert.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
34 #include "alSource.h"
35 #include "alBuffer.h"
36 #include "alThunk.h"
37 #include "alListener.h"
38 #include "alAuxEffectSlot.h"
39 #include "alu.h"
40 #include "bs2b.h"
41 #include "alReverb.h"
43 #if defined (HAVE_FLOAT_H)
44 #include <float.h>
45 #endif
47 #ifndef M_PI
48 #define M_PI 3.14159265358979323846 /* pi */
49 #define M_PI_2 1.57079632679489661923 /* pi/2 */
50 #endif
52 #if defined(HAVE_STDINT_H)
53 #include <stdint.h>
54 typedef int64_t ALint64;
55 #elif defined(HAVE___INT64)
56 typedef __int64 ALint64;
57 #elif (SIZEOF_LONG == 8)
58 typedef long ALint64;
59 #elif (SIZEOF_LONG_LONG == 8)
60 typedef long long ALint64;
61 #endif
63 #ifdef HAVE_SQRTF
64 #define aluSqrt(x) ((ALfloat)sqrtf((float)(x)))
65 #else
66 #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
67 #endif
69 #ifdef HAVE_ACOSF
70 #define aluAcos(x) ((ALfloat)acosf((float)(x)))
71 #else
72 #define aluAcos(x) ((ALfloat)acos((double)(x)))
73 #endif
75 #ifdef HAVE_ATANF
76 #define aluAtan(x) ((ALfloat)atanf((float)(x)))
77 #else
78 #define aluAtan(x) ((ALfloat)atan((double)(x)))
79 #endif
81 #ifdef HAVE_FABSF
82 #define aluFabs(x) ((ALfloat)fabsf((float)(x)))
83 #else
84 #define aluFabs(x) ((ALfloat)fabs((double)(x)))
85 #endif
87 // fixes for mingw32.
88 #if defined(max) && !defined(__max)
89 #define __max max
90 #endif
91 #if defined(min) && !defined(__min)
92 #define __min min
93 #endif
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(const ALfloat *inVector1, const 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(const ALfloat *inVector1, const 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(const ALCcontext *ALContext,
557 const ALsource *ALSource, ALenum isMono,
558 ALfloat *drysend, ALfloat *wetsend,
559 ALfloat *pitch, ALfloat *drygainhf,
560 ALfloat *wetgainhf)
562 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix,WetMix=0.0f;
563 ALfloat Direction[3],Position[3],SourceToListener[3];
564 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
565 ALfloat ConeVolume,SourceVolume,ListenerGain;
566 ALfloat U[3],V[3],N[3];
567 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
568 ALfloat Matrix[3][3];
569 ALfloat flAttenuation;
570 ALfloat RoomAttenuation;
571 ALfloat MetersPerUnit;
572 ALfloat RoomRolloff;
573 ALfloat DryGainHF = 1.0f;
574 ALfloat WetGainHF = 1.0f;
575 ALfloat DirGain, AmbientGain;
576 const ALfloat *SpeakerGain;
577 ALint pos, s;
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 (dist*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 *drygainhf = DryGainHF;
853 *wetgainhf = WetGainHF;
855 else
857 //1. Multi-channel buffers always play "normal"
858 pitch[0] = ALSource->flPitch;
860 DryMix = SourceVolume;
861 DryMix = __min(DryMix,MaxVolume);
862 DryMix = __max(DryMix,MinVolume);
864 switch(ALSource->DirectFilter.type)
866 case AL_FILTER_LOWPASS:
867 DryMix *= ALSource->DirectFilter.Gain;
868 DryGainHF *= ALSource->DirectFilter.GainHF;
869 break;
872 drysend[FRONT_LEFT] = DryMix * ListenerGain;
873 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
874 drysend[SIDE_LEFT] = DryMix * ListenerGain;
875 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
876 drysend[BACK_LEFT] = DryMix * ListenerGain;
877 drysend[BACK_RIGHT] = DryMix * ListenerGain;
878 drysend[FRONT_CENTER] = DryMix * ListenerGain;
879 drysend[BACK_CENTER] = DryMix * ListenerGain;
880 drysend[LFE] = DryMix * ListenerGain;
881 *wetsend = 0.0f;
883 *drygainhf = DryGainHF;
884 *wetgainhf = WetGainHF;
888 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
890 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
893 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
895 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
896 static float DummyBuffer[BUFFERSIZE];
897 ALfloat *WetBuffer = NULL;
898 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
899 ALfloat newDrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
900 ALfloat newWetSend = 0.0f;
901 ALfloat DryGainHF = 0.0f;
902 ALfloat WetGainHF = 0.0f;
903 ALfloat *DrySend;
904 ALfloat *WetSend;
905 ALuint rampLength;
906 ALfloat dryGainStep[OUTPUTCHANNELS];
907 ALfloat wetGainStep;
908 ALuint BlockAlign,BufferSize;
909 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
910 ALuint Channels,Frequency,ulExtraSamples;
911 ALfloat Pitch;
912 ALint Looping,State;
913 ALint increment;
914 ALuint Buffer;
915 ALuint SamplesToDo;
916 ALsource *ALSource;
917 ALbuffer *ALBuffer;
918 ALeffectslot *ALEffectSlot;
919 ALfloat values[OUTPUTCHANNELS];
920 ALfloat value;
921 ALshort *Data;
922 ALuint i,j,k,out;
923 ALfloat cw, a, g;
924 ALbufferlistitem *BufferListItem;
925 ALuint loop;
926 ALint64 DataSize64,DataPos64;
927 FILTER *DryFilter, *WetFilter;
928 int fpuState;
930 SuspendContext(ALContext);
932 #if defined(HAVE_FESETROUND)
933 fpuState = fegetround();
934 fesetround(FE_TOWARDZERO);
935 #elif defined(HAVE__CONTROLFP)
936 fpuState = _controlfp(0, 0);
937 _controlfp(_RC_CHOP, _MCW_RC);
938 #else
939 (void)fpuState;
940 #endif
942 //Figure output format variables
943 BlockAlign = aluChannelsFromFormat(format);
944 BlockAlign *= aluBytesFromFormat(format);
946 size /= BlockAlign;
947 while(size > 0)
949 //Setup variables
950 SamplesToDo = min(size, BUFFERSIZE);
951 if(ALContext)
953 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
954 ALSource = ALContext->Source;
955 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
957 else
959 ALEffectSlot = NULL;
960 ALSource = NULL;
961 rampLength = 0;
963 rampLength = max(rampLength, SamplesToDo);
965 //Clear mixing buffer
966 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
968 //Actual mixing loop
969 while(ALSource)
971 j = 0;
972 State = ALSource->state;
974 while(State == AL_PLAYING && j < SamplesToDo)
976 DataSize = 0;
977 DataPosInt = 0;
978 DataPosFrac = 0;
980 //Get buffer info
981 if((Buffer = ALSource->ulBufferID))
983 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
985 Data = ALBuffer->data;
986 Channels = aluChannelsFromFormat(ALBuffer->format);
987 DataSize = ALBuffer->size;
988 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
989 Frequency = ALBuffer->frequency;
990 DataPosInt = ALSource->position;
991 DataPosFrac = ALSource->position_fraction;
993 if(DataPosInt >= DataSize)
994 goto skipmix;
996 //Get source info
997 DryFilter = &ALSource->iirFilter;
998 WetFilter = &ALSource->Send[0].iirFilter;
999 WetBuffer = (ALSource->Send[0].Slot ?
1000 ALSource->Send[0].Slot->WetBuffer :
1001 DummyBuffer);
1002 DrySend = ALSource->DryGains;
1003 WetSend = &ALSource->WetGain;
1005 CalcSourceParams(ALContext, ALSource,
1006 (Channels==1) ? AL_TRUE : AL_FALSE,
1007 newDrySend, &newWetSend, &Pitch,
1008 &DryGainHF, &WetGainHF);
1009 Pitch = (Pitch*Frequency) / ALContext->Frequency;
1011 if(Channels == 1)
1013 // Update filter coefficients. Calculations based on
1014 // the I3DL2 spec.
1015 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
1016 // We use four chained one-pole filters, so we need to
1017 // take the fourth root of the squared gain, which is
1018 // the same as the square root of the base gain.
1019 // Be careful with gains < 0.0001, as that causes the
1020 // coefficient to head towards 1, which will flatten
1021 // the signal
1022 g = aluSqrt(__max(DryGainHF, 0.0001f));
1023 a = 0.0f;
1024 if(g < 0.9999f) // 1-epsilon
1025 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
1026 DryFilter->coeff = a;
1028 g = aluSqrt(__max(WetGainHF, 0.0001f));
1029 a = 0.0f;
1030 if(g < 0.9999f) // 1-epsilon
1031 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
1032 WetFilter->coeff = a;
1034 else
1036 // Multi-channel sources use two chained one-pole
1037 // filters, so take the base gain (square root of the
1038 // squared gain)
1039 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
1040 g = __max(DryGainHF, 0.01f);
1041 a = 0.0f;
1042 if(g < 0.9999f) // 1-epsilon
1043 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
1044 DryFilter->coeff = a;
1045 WetFilter->coeff = 0.0f;
1047 if(DuplicateStereo && Channels == 2)
1049 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
1050 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
1051 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
1052 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
1054 else if(DuplicateStereo)
1056 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
1057 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
1058 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
1059 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
1063 //Compute the gain steps for each output channel
1064 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
1066 for(i = 0;i < OUTPUTCHANNELS;i++)
1068 DrySend[i] = newDrySend[i];
1069 dryGainStep[i] = 0;
1071 *WetSend = newWetSend;
1072 wetGainStep = 0;
1074 else
1076 for(i = 0;i < OUTPUTCHANNELS;i++)
1077 dryGainStep[i] = (newDrySend[i]-DrySend[i]) / rampLength;
1078 wetGainStep = (newWetSend-(*WetSend)) / rampLength;
1080 ALSource->FirstStart = AL_FALSE;
1082 //Compute 18.14 fixed point step
1083 if(Pitch > (float)MAX_PITCH)
1084 Pitch = (float)MAX_PITCH;
1085 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
1086 if(increment <= 0)
1087 increment = (1<<FRACTIONBITS);
1089 //Figure out how many samples we can mix.
1090 DataSize64 = DataSize;
1091 DataSize64 <<= FRACTIONBITS;
1092 DataPos64 = DataPosInt;
1093 DataPos64 <<= FRACTIONBITS;
1094 DataPos64 += DataPosFrac;
1095 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1097 BufferListItem = ALSource->queue;
1098 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
1100 if(BufferListItem)
1101 BufferListItem = BufferListItem->next;
1103 if (BufferListItem)
1105 if (BufferListItem->next)
1107 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1108 if(NextBuf && NextBuf->data)
1110 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1111 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1114 else if (ALSource->bLooping)
1116 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
1117 if (NextBuf && NextBuf->data)
1119 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1120 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1123 else
1124 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1126 BufferSize = min(BufferSize, (SamplesToDo-j));
1128 //Actual sample mixing loop
1129 k = 0;
1130 Data += DataPosInt*Channels;
1132 if(Channels == 1) /* Mono */
1134 ALfloat outsamp;
1136 while(BufferSize--)
1138 for(i = 0;i < OUTPUTCHANNELS;i++)
1139 DrySend[i] += dryGainStep[i];
1140 *WetSend += wetGainStep;
1142 //First order interpolator
1143 value = lerp(Data[k], Data[k+1], DataPosFrac);
1145 //Direct path final mix buffer and panning
1146 outsamp = lpFilter(DryFilter, value);
1147 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1148 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1149 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1150 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1151 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1152 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1153 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1154 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1156 //Room path final mix buffer and panning
1157 outsamp = lpFilter(WetFilter, value);
1158 WetBuffer[j] += outsamp*(*WetSend);
1160 DataPosFrac += increment;
1161 k += DataPosFrac>>FRACTIONBITS;
1162 DataPosFrac &= FRACTIONMASK;
1163 j++;
1166 else if(Channels == 2) /* Stereo */
1168 const int chans[] = {
1169 FRONT_LEFT, FRONT_RIGHT
1172 #define DO_MIX() do { \
1173 *WetSend += wetGainStep*BufferSize; \
1174 while(BufferSize--) \
1176 for(i = 0;i < OUTPUTCHANNELS;i++) \
1177 DrySend[i] += dryGainStep[i]; \
1179 for(i = 0;i < Channels;i++) \
1181 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1182 values[i] = lpFilterMC(DryFilter, chans[i], value)*DrySend[chans[i]]; \
1184 for(out = 0;out < OUTPUTCHANNELS;out++) \
1186 ALfloat sum = 0.0f; \
1187 for(i = 0;i < Channels;i++) \
1188 sum += values[i]*Matrix[chans[i]][out]; \
1189 DryBuffer[j][out] += sum; \
1192 DataPosFrac += increment; \
1193 k += DataPosFrac>>FRACTIONBITS; \
1194 DataPosFrac &= FRACTIONMASK; \
1195 j++; \
1197 } while(0)
1199 DO_MIX();
1201 else if(Channels == 4) /* Quad */
1203 const int chans[] = {
1204 FRONT_LEFT, FRONT_RIGHT,
1205 BACK_LEFT, BACK_RIGHT
1208 DO_MIX();
1210 else if(Channels == 6) /* 5.1 */
1212 const int chans[] = {
1213 FRONT_LEFT, FRONT_RIGHT,
1214 FRONT_CENTER, LFE,
1215 BACK_LEFT, BACK_RIGHT
1218 DO_MIX();
1220 else if(Channels == 7) /* 6.1 */
1222 const int chans[] = {
1223 FRONT_LEFT, FRONT_RIGHT,
1224 FRONT_CENTER, LFE,
1225 BACK_CENTER,
1226 SIDE_LEFT, SIDE_RIGHT
1229 DO_MIX();
1231 else if(Channels == 8) /* 7.1 */
1233 const int chans[] = {
1234 FRONT_LEFT, FRONT_RIGHT,
1235 FRONT_CENTER, LFE,
1236 BACK_LEFT, BACK_RIGHT,
1237 SIDE_LEFT, SIDE_RIGHT
1240 DO_MIX();
1241 #undef DO_MIX
1243 else /* Unknown? */
1245 *WetSend += wetGainStep*BufferSize;
1246 for(i = 0;i < OUTPUTCHANNELS;i++)
1247 DrySend[i] += dryGainStep[i]*BufferSize;
1248 while(BufferSize--)
1250 DataPosFrac += increment;
1251 k += DataPosFrac>>FRACTIONBITS;
1252 DataPosFrac &= FRACTIONMASK;
1253 j++;
1256 DataPosInt += k;
1258 //Update source info
1259 ALSource->position = DataPosInt;
1260 ALSource->position_fraction = DataPosFrac;
1262 skipmix: ;
1265 //Handle looping sources
1266 if(!Buffer || DataPosInt >= DataSize)
1268 //queueing
1269 if(ALSource->queue)
1271 Looping = ALSource->bLooping;
1272 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1274 BufferListItem = ALSource->queue;
1275 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1277 if(BufferListItem)
1279 if(!Looping)
1280 BufferListItem->bufferstate = PROCESSED;
1281 BufferListItem = BufferListItem->next;
1284 if(BufferListItem)
1285 ALSource->ulBufferID = BufferListItem->buffer;
1286 ALSource->position = DataPosInt-DataSize;
1287 ALSource->position_fraction = DataPosFrac;
1288 ALSource->BuffersPlayed++;
1290 else
1292 if(!Looping)
1294 /* alSourceStop */
1295 ALSource->state = AL_STOPPED;
1296 ALSource->inuse = AL_FALSE;
1297 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1298 BufferListItem = ALSource->queue;
1299 while(BufferListItem != NULL)
1301 BufferListItem->bufferstate = PROCESSED;
1302 BufferListItem = BufferListItem->next;
1304 ALSource->position = DataSize;
1305 ALSource->position_fraction = 0;
1307 else
1309 /* alSourceRewind */
1310 /* alSourcePlay */
1311 ALSource->state = AL_PLAYING;
1312 ALSource->inuse = AL_TRUE;
1313 ALSource->play = AL_TRUE;
1314 ALSource->BuffersPlayed = 0;
1315 BufferListItem = ALSource->queue;
1316 while(BufferListItem != NULL)
1318 BufferListItem->bufferstate = PENDING;
1319 BufferListItem = BufferListItem->next;
1321 ALSource->ulBufferID = ALSource->queue->buffer;
1323 if(ALSource->BuffersInQueue == 1)
1324 ALSource->position = DataPosInt%DataSize;
1325 else
1326 ALSource->position = DataPosInt-DataSize;
1327 ALSource->position_fraction = DataPosFrac;
1333 //Get source state
1334 State = ALSource->state;
1337 ALSource = ALSource->next;
1340 // effect slot processing
1341 while(ALEffectSlot)
1343 if(ALEffectSlot->effect.type == AL_EFFECT_REVERB)
1344 VerbProcess(ALEffectSlot->ReverbState, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1346 for(i = 0;i < SamplesToDo;i++)
1347 ALEffectSlot->WetBuffer[i] = 0.0f;
1348 ALEffectSlot = ALEffectSlot->next;
1351 //Post processing loop
1352 switch(format)
1354 case AL_FORMAT_MONO8:
1355 for(i = 0;i < SamplesToDo;i++)
1357 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1358 buffer = ((ALubyte*)buffer) + 1;
1360 break;
1361 case AL_FORMAT_STEREO8:
1362 if(ALContext && ALContext->bs2b)
1364 for(i = 0;i < SamplesToDo;i++)
1366 float samples[2];
1367 samples[0] = DryBuffer[i][FRONT_LEFT];
1368 samples[1] = DryBuffer[i][FRONT_RIGHT];
1369 bs2b_cross_feed(ALContext->bs2b, samples);
1370 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1371 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1372 buffer = ((ALubyte*)buffer) + 2;
1375 else
1377 for(i = 0;i < SamplesToDo;i++)
1379 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1380 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1381 buffer = ((ALubyte*)buffer) + 2;
1384 break;
1385 case AL_FORMAT_QUAD8:
1386 for(i = 0;i < SamplesToDo;i++)
1388 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1389 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1390 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1391 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1392 buffer = ((ALubyte*)buffer) + 4;
1394 break;
1395 case AL_FORMAT_51CHN8:
1396 for(i = 0;i < SamplesToDo;i++)
1398 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1399 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1400 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1401 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1402 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1403 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1404 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1405 #else
1406 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1407 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1408 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1409 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1410 #endif
1411 buffer = ((ALubyte*)buffer) + 6;
1413 break;
1414 case AL_FORMAT_61CHN8:
1415 for(i = 0;i < SamplesToDo;i++)
1417 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1418 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
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_CENTER])>>8)+128);
1422 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1423 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1424 buffer = ((ALubyte*)buffer) + 7;
1426 break;
1427 case AL_FORMAT_71CHN8:
1428 for(i = 0;i < SamplesToDo;i++)
1430 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1431 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1432 #ifdef _WIN32
1433 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1434 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1435 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1436 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1437 #else
1438 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1439 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1440 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1441 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1442 #endif
1443 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1444 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1445 buffer = ((ALubyte*)buffer) + 8;
1447 break;
1449 case AL_FORMAT_MONO16:
1450 for(i = 0;i < SamplesToDo;i++)
1452 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1453 buffer = ((ALshort*)buffer) + 1;
1455 break;
1456 case AL_FORMAT_STEREO16:
1457 if(ALContext && ALContext->bs2b)
1459 for(i = 0;i < SamplesToDo;i++)
1461 float samples[2];
1462 samples[0] = DryBuffer[i][FRONT_LEFT];
1463 samples[1] = DryBuffer[i][FRONT_RIGHT];
1464 bs2b_cross_feed(ALContext->bs2b, samples);
1465 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1466 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1467 buffer = ((ALshort*)buffer) + 2;
1470 else
1472 for(i = 0;i < SamplesToDo;i++)
1474 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1475 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1476 buffer = ((ALshort*)buffer) + 2;
1479 break;
1480 case AL_FORMAT_QUAD16:
1481 for(i = 0;i < SamplesToDo;i++)
1483 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1484 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1485 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1486 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1487 buffer = ((ALshort*)buffer) + 4;
1489 break;
1490 case AL_FORMAT_51CHN16:
1491 for(i = 0;i < SamplesToDo;i++)
1493 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1494 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1495 #ifdef _WIN32
1496 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1497 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1498 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1499 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1500 #else
1501 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1502 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1503 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1504 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1505 #endif
1506 buffer = ((ALshort*)buffer) + 6;
1508 break;
1509 case AL_FORMAT_61CHN16:
1510 for(i = 0;i < SamplesToDo;i++)
1512 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1513 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
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_CENTER]);
1517 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1518 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1519 buffer = ((ALshort*)buffer) + 7;
1521 break;
1522 case AL_FORMAT_71CHN16:
1523 for(i = 0;i < SamplesToDo;i++)
1525 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1526 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1527 #ifdef _WIN32
1528 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1529 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1530 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1531 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1532 #else
1533 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1534 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1535 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1536 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1537 #endif
1538 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1539 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1540 buffer = ((ALshort*)buffer) + 8;
1542 break;
1544 default:
1545 break;
1548 size -= SamplesToDo;
1551 #if defined(HAVE_FESETROUND)
1552 fesetround(fpuState);
1553 #elif defined(HAVE__CONTROLFP)
1554 _controlfp(fpuState, 0xfffff);
1555 #endif
1557 ProcessContext(ALContext);