Fix alsoftrc configuration sample comments
[openal-soft.git] / Alc / ALu.c
blob741b5394701bbd530e770037e64c7782139f1cb2
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 BUFFERSIZE 24000
96 #define FRACTIONBITS 14
97 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
98 #define MAX_PITCH 65536
100 /* Minimum ramp length in milliseconds. The value below was chosen to
101 * adequately reduce clicks and pops from harsh gain changes. */
102 #define MIN_RAMP_LENGTH 16
104 ALboolean DuplicateStereo = AL_FALSE;
106 /* NOTE: The AL_FORMAT_REAR* enums aren't handled here be cause they're
107 * converted to AL_FORMAT_QUAD* when loaded */
108 __inline ALuint aluBytesFromFormat(ALenum format)
110 switch(format)
112 case AL_FORMAT_MONO8:
113 case AL_FORMAT_STEREO8:
114 case AL_FORMAT_QUAD8_LOKI:
115 case AL_FORMAT_QUAD8:
116 case AL_FORMAT_51CHN8:
117 case AL_FORMAT_61CHN8:
118 case AL_FORMAT_71CHN8:
119 return 1;
121 case AL_FORMAT_MONO16:
122 case AL_FORMAT_STEREO16:
123 case AL_FORMAT_QUAD16_LOKI:
124 case AL_FORMAT_QUAD16:
125 case AL_FORMAT_51CHN16:
126 case AL_FORMAT_61CHN16:
127 case AL_FORMAT_71CHN16:
128 return 2;
130 case AL_FORMAT_MONO_FLOAT32:
131 case AL_FORMAT_STEREO_FLOAT32:
132 case AL_FORMAT_QUAD32:
133 case AL_FORMAT_51CHN32:
134 case AL_FORMAT_61CHN32:
135 case AL_FORMAT_71CHN32:
136 return 4;
138 default:
139 return 0;
143 __inline ALuint aluChannelsFromFormat(ALenum format)
145 switch(format)
147 case AL_FORMAT_MONO8:
148 case AL_FORMAT_MONO16:
149 case AL_FORMAT_MONO_FLOAT32:
150 return 1;
152 case AL_FORMAT_STEREO8:
153 case AL_FORMAT_STEREO16:
154 case AL_FORMAT_STEREO_FLOAT32:
155 return 2;
157 case AL_FORMAT_QUAD8_LOKI:
158 case AL_FORMAT_QUAD16_LOKI:
159 case AL_FORMAT_QUAD8:
160 case AL_FORMAT_QUAD16:
161 case AL_FORMAT_QUAD32:
162 return 4;
164 case AL_FORMAT_51CHN8:
165 case AL_FORMAT_51CHN16:
166 case AL_FORMAT_51CHN32:
167 return 6;
169 case AL_FORMAT_61CHN8:
170 case AL_FORMAT_61CHN16:
171 case AL_FORMAT_61CHN32:
172 return 7;
174 case AL_FORMAT_71CHN8:
175 case AL_FORMAT_71CHN16:
176 case AL_FORMAT_71CHN32:
177 return 8;
179 default:
180 return 0;
185 static __inline ALfloat lpFilter(FILTER *iir, ALfloat input)
187 ALfloat *history = iir->history;
188 ALfloat a = iir->coeff;
189 ALfloat output = input;
191 output = output + (history[0]-output)*a;
192 history[0] = output;
193 output = output + (history[1]-output)*a;
194 history[1] = output;
195 output = output + (history[2]-output)*a;
196 history[2] = output;
197 output = output + (history[3]-output)*a;
198 history[3] = output;
200 return output;
203 static __inline ALfloat lpFilterMC(FILTER *iir, ALuint chan, ALfloat input)
205 ALfloat *history = &iir->history[chan*2];
206 ALfloat a = iir->coeff;
207 ALfloat output = input;
209 output = output + (history[0]-output)*a;
210 history[0] = output;
211 output = output + (history[1]-output)*a;
212 history[1] = output;
214 return output;
218 static __inline ALshort aluF2S(ALfloat Value)
220 ALint i;
222 i = (ALint)Value;
223 i = __min( 32767, i);
224 i = __max(-32768, i);
225 return ((ALshort)i);
228 static __inline ALvoid aluCrossproduct(ALfloat *inVector1,ALfloat *inVector2,ALfloat *outVector)
230 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
231 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
232 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
235 static __inline ALfloat aluDotproduct(ALfloat *inVector1,ALfloat *inVector2)
237 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
238 inVector1[2]*inVector2[2];
241 static __inline ALvoid aluNormalize(ALfloat *inVector)
243 ALfloat length, inverse_length;
245 length = aluSqrt(aluDotproduct(inVector, inVector));
246 if(length != 0.0f)
248 inverse_length = 1.0f/length;
249 inVector[0] *= inverse_length;
250 inVector[1] *= inverse_length;
251 inVector[2] *= inverse_length;
255 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
257 ALfloat result[3];
259 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
260 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
261 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
262 memcpy(vector, result, sizeof(result));
265 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
266 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
268 const char *confkey;
269 const char *next;
270 const char *sep;
271 const char *end;
272 int i, val;
274 confkey = GetConfigValue(NULL, name, "");
275 next = confkey;
276 while(next && *next)
278 confkey = next;
279 next = strchr(confkey, ',');
280 if(next)
282 do {
283 next++;
284 } while(isspace(*next));
287 sep = strchr(confkey, '=');
288 if(!sep || confkey == sep)
289 continue;
291 end = sep - 1;
292 while(isspace(*end) && end != confkey)
293 end--;
295 if(strncmp(confkey, "fl", end-confkey) == 0)
296 val = FRONT_LEFT;
297 else if(strncmp(confkey, "fr", end-confkey) == 0)
298 val = FRONT_RIGHT;
299 else if(strncmp(confkey, "fc", end-confkey) == 0)
300 val = FRONT_CENTER;
301 else if(strncmp(confkey, "bl", end-confkey) == 0)
302 val = BACK_LEFT;
303 else if(strncmp(confkey, "br", end-confkey) == 0)
304 val = BACK_RIGHT;
305 else if(strncmp(confkey, "bc", end-confkey) == 0)
306 val = BACK_CENTER;
307 else if(strncmp(confkey, "sl", end-confkey) == 0)
308 val = SIDE_LEFT;
309 else if(strncmp(confkey, "sr", end-confkey) == 0)
310 val = SIDE_RIGHT;
311 else
313 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
314 continue;
317 sep++;
318 while(isspace(*sep))
319 sep++;
321 for(i = 0;i < chans;i++)
323 if(Speaker2Chan[i] == val)
325 val = strtol(sep, NULL, 10);
326 if(val >= -180 && val <= 180)
327 SpeakerAngle[i] = val * M_PI/180.0f;
328 else
329 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
330 break;
335 for(i = 1;i < chans;i++)
337 if(SpeakerAngle[i] <= SpeakerAngle[i-1])
339 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i, chans,
340 SpeakerAngle[i-1] * 180.0f/M_PI, SpeakerAngle[i] * 180.0f/M_PI);
341 SpeakerAngle[i] = SpeakerAngle[i-1] + 1 * 180.0f/M_PI;
346 static __inline ALfloat aluLUTpos2Angle(ALint pos)
348 if(pos < QUADRANT_NUM)
349 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
350 if(pos < 2 * QUADRANT_NUM)
351 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
352 if(pos < 3 * QUADRANT_NUM)
353 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
354 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
357 ALvoid aluInitPanning(ALCcontext *Context)
359 ALint pos, offset, s;
360 ALfloat Alpha, Theta;
361 ALfloat SpeakerAngle[OUTPUTCHANNELS];
362 ALint Speaker2Chan[OUTPUTCHANNELS];
364 for(s = 0;s < OUTPUTCHANNELS;s++)
366 int s2;
367 for(s2 = 0;s2 < OUTPUTCHANNELS;s2++)
368 Context->ChannelMatrix[s][s2] = ((s==s2) ? 1.0f : 0.0f);
371 switch(Context->Device->Format)
373 /* Mono is rendered as stereo, then downmixed during post-process */
374 case AL_FORMAT_MONO8:
375 case AL_FORMAT_MONO16:
376 case AL_FORMAT_MONO_FLOAT32:
377 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
378 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
379 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
380 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
381 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
382 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
383 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
384 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
385 Context->NumChan = 2;
386 Speaker2Chan[0] = FRONT_LEFT;
387 Speaker2Chan[1] = FRONT_RIGHT;
388 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
389 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
390 break;
392 case AL_FORMAT_STEREO8:
393 case AL_FORMAT_STEREO16:
394 case AL_FORMAT_STEREO_FLOAT32:
395 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
396 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
397 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
398 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
399 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
400 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
401 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
402 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
403 Context->NumChan = 2;
404 Speaker2Chan[0] = FRONT_LEFT;
405 Speaker2Chan[1] = FRONT_RIGHT;
406 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
407 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
408 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
409 break;
411 case AL_FORMAT_QUAD8:
412 case AL_FORMAT_QUAD16:
413 case AL_FORMAT_QUAD32:
414 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
415 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
416 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
417 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
418 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
419 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
420 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
421 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
422 Context->NumChan = 4;
423 Speaker2Chan[0] = BACK_LEFT;
424 Speaker2Chan[1] = FRONT_LEFT;
425 Speaker2Chan[2] = FRONT_RIGHT;
426 Speaker2Chan[3] = BACK_RIGHT;
427 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
428 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
429 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
430 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
431 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
432 break;
434 case AL_FORMAT_51CHN8:
435 case AL_FORMAT_51CHN16:
436 case AL_FORMAT_51CHN32:
437 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
438 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
439 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
440 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
441 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
442 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
443 Context->NumChan = 5;
444 Speaker2Chan[0] = BACK_LEFT;
445 Speaker2Chan[1] = FRONT_LEFT;
446 Speaker2Chan[2] = FRONT_CENTER;
447 Speaker2Chan[3] = FRONT_RIGHT;
448 Speaker2Chan[4] = BACK_RIGHT;
449 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
450 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
451 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
452 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
453 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
454 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
455 break;
457 case AL_FORMAT_61CHN8:
458 case AL_FORMAT_61CHN16:
459 case AL_FORMAT_61CHN32:
460 Context->ChannelMatrix[BACK_LEFT][BACK_CENTER] = aluSqrt(0.5);
461 Context->ChannelMatrix[BACK_LEFT][SIDE_LEFT] = aluSqrt(0.5);
462 Context->ChannelMatrix[BACK_RIGHT][BACK_CENTER] = aluSqrt(0.5);
463 Context->ChannelMatrix[BACK_RIGHT][SIDE_RIGHT] = aluSqrt(0.5);
464 Context->NumChan = 6;
465 Speaker2Chan[0] = SIDE_LEFT;
466 Speaker2Chan[1] = FRONT_LEFT;
467 Speaker2Chan[2] = FRONT_CENTER;
468 Speaker2Chan[3] = FRONT_RIGHT;
469 Speaker2Chan[4] = SIDE_RIGHT;
470 Speaker2Chan[5] = BACK_CENTER;
471 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
472 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
473 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
474 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
475 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
476 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
477 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
478 break;
480 case AL_FORMAT_71CHN8:
481 case AL_FORMAT_71CHN16:
482 case AL_FORMAT_71CHN32:
483 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
484 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
485 Context->NumChan = 7;
486 Speaker2Chan[0] = BACK_LEFT;
487 Speaker2Chan[1] = SIDE_LEFT;
488 Speaker2Chan[2] = FRONT_LEFT;
489 Speaker2Chan[3] = FRONT_CENTER;
490 Speaker2Chan[4] = FRONT_RIGHT;
491 Speaker2Chan[5] = SIDE_RIGHT;
492 Speaker2Chan[6] = BACK_RIGHT;
493 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
494 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
495 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
496 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
497 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
498 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
499 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
500 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
501 break;
503 default:
504 assert(0);
507 for(pos = 0; pos < LUT_NUM; pos++)
509 /* source angle */
510 Theta = aluLUTpos2Angle(pos);
512 /* clear all values */
513 offset = OUTPUTCHANNELS * pos;
514 for(s = 0; s < OUTPUTCHANNELS; s++)
515 Context->PanningLUT[offset+s] = 0.0f;
517 /* set panning values */
518 for(s = 0; s < Context->NumChan - 1; s++)
520 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
522 /* source between speaker s and speaker s+1 */
523 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
524 (SpeakerAngle[s+1]-SpeakerAngle[s]);
525 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
526 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
527 break;
530 if(s == Context->NumChan - 1)
532 /* source between last and first speaker */
533 if(Theta < SpeakerAngle[0])
534 Theta += 2.0f * M_PI;
535 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
536 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
537 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
538 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
543 static __inline ALint aluCart2LUTpos(ALfloat re, ALfloat im)
545 ALint pos = 0;
546 ALfloat denom = aluFabs(re) + aluFabs(im);
547 if(denom > 0.0f)
548 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
550 if(re < 0.0)
551 pos = 2 * QUADRANT_NUM - pos;
552 if(im < 0.0)
553 pos = LUT_NUM - pos;
554 return pos%LUT_NUM;
557 static ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
558 ALenum isMono, ALfloat *drysend,
559 ALfloat *wetsend, ALfloat *pitch,
560 ALfloat *drygainhf, 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;
578 ALfloat cw, a, g;
580 //Get context properties
581 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
582 DopplerVelocity = ALContext->DopplerVelocity;
583 flSpeedOfSound = ALContext->flSpeedOfSound;
585 //Get listener properties
586 ListenerGain = ALContext->Listener.Gain;
587 MetersPerUnit = ALContext->Listener.MetersPerUnit;
589 //Get source properties
590 SourceVolume = ALSource->flGain;
591 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
592 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
593 MinVolume = ALSource->flMinGain;
594 MaxVolume = ALSource->flMaxGain;
595 MinDist = ALSource->flRefDistance;
596 MaxDist = ALSource->flMaxDistance;
597 Rolloff = ALSource->flRollOffFactor;
598 InnerAngle = ALSource->flInnerAngle;
599 OuterAngle = ALSource->flOuterAngle;
600 OuterGainHF = ALSource->OuterGainHF;
601 RoomRolloff = ALSource->RoomRolloffFactor;
603 //Only apply 3D calculations for mono buffers
604 if(isMono != AL_FALSE)
606 //1. Translate Listener to origin (convert to head relative)
607 // Note that Direction and SourceToListener are *not* transformed.
608 // SourceToListener is used with the source and listener velocities,
609 // which are untransformed, and Direction is used with SourceToListener
610 // for the sound cone
611 if(ALSource->bHeadRelative==AL_FALSE)
613 // Build transform matrix
614 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
615 aluNormalize(U); // Normalized Right-vector
616 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
617 aluNormalize(V); // Normalized Up-vector
618 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
619 aluNormalize(N); // Normalized At-vector
620 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
621 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
622 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
624 // Translate source position into listener space
625 Position[0] -= ALContext->Listener.Position[0];
626 Position[1] -= ALContext->Listener.Position[1];
627 Position[2] -= ALContext->Listener.Position[2];
629 SourceToListener[0] = -Position[0];
630 SourceToListener[1] = -Position[1];
631 SourceToListener[2] = -Position[2];
633 // Transform source position into listener space
634 aluMatrixVector(Position, Matrix);
636 else
638 SourceToListener[0] = -Position[0];
639 SourceToListener[1] = -Position[1];
640 SourceToListener[2] = -Position[2];
642 aluNormalize(SourceToListener);
643 aluNormalize(Direction);
645 //2. Calculate distance attenuation
646 Distance = aluSqrt(aluDotproduct(Position, Position));
648 if(ALSource->Send[0].Slot)
650 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
651 RoomRolloff += ALSource->Send[0].Slot->effect.Reverb.RoomRolloffFactor;
654 flAttenuation = 1.0f;
655 RoomAttenuation = 1.0f;
656 switch (ALSource->DistanceModel)
658 case AL_INVERSE_DISTANCE_CLAMPED:
659 Distance=__max(Distance,MinDist);
660 Distance=__min(Distance,MaxDist);
661 if (MaxDist < MinDist)
662 break;
663 //fall-through
664 case AL_INVERSE_DISTANCE:
665 if (MinDist > 0.0f)
667 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
668 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
669 if ((MinDist + (RoomRolloff * (Distance - MinDist))) > 0.0f)
670 RoomAttenuation = MinDist / (MinDist + (RoomRolloff * (Distance - MinDist)));
672 break;
674 case AL_LINEAR_DISTANCE_CLAMPED:
675 Distance=__max(Distance,MinDist);
676 Distance=__min(Distance,MaxDist);
677 if (MaxDist < MinDist)
678 break;
679 //fall-through
680 case AL_LINEAR_DISTANCE:
681 Distance=__min(Distance,MaxDist);
682 if (MaxDist != MinDist)
684 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
685 RoomAttenuation = 1.0f - (RoomRolloff*(Distance-MinDist)/(MaxDist - MinDist));
687 break;
689 case AL_EXPONENT_DISTANCE_CLAMPED:
690 Distance=__max(Distance,MinDist);
691 Distance=__min(Distance,MaxDist);
692 if (MaxDist < MinDist)
693 break;
694 //fall-through
695 case AL_EXPONENT_DISTANCE:
696 if ((Distance > 0.0f) && (MinDist > 0.0f))
698 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
699 RoomAttenuation = (ALfloat)pow(Distance/MinDist, -RoomRolloff);
701 break;
703 case AL_NONE:
704 flAttenuation = 1.0f;
705 RoomAttenuation = 1.0f;
706 break;
709 // Distance-based air absorption
710 if(ALSource->AirAbsorptionFactor > 0.0f && ALContext->DistanceModel != AL_NONE)
712 ALfloat dist = Distance-MinDist;
713 ALfloat absorb;
715 if(dist < 0.0f) dist = 0.0f;
716 // Absorption calculation is done in dB
717 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
718 (dist*MetersPerUnit);
719 // Convert dB to linear gain before applying
720 absorb = pow(10.0, absorb/20.0);
721 DryGainHF *= absorb;
722 WetGainHF *= absorb;
725 // Source Gain + Attenuation and clamp to Min/Max Gain
726 DryMix = SourceVolume * flAttenuation;
727 DryMix = __min(DryMix,MaxVolume);
728 DryMix = __max(DryMix,MinVolume);
730 WetMix = SourceVolume * RoomAttenuation;
731 WetMix = __min(WetMix,MaxVolume);
732 WetMix = __max(WetMix,MinVolume);
734 //3. Apply directional soundcones
735 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
736 if(Angle >= InnerAngle && Angle <= OuterAngle)
738 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
739 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
740 DryMix *= ConeVolume;
741 if(ALSource->WetGainAuto)
742 WetMix *= ConeVolume;
743 if(ALSource->DryGainHFAuto)
744 DryGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
745 if(ALSource->WetGainHFAuto)
746 WetGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
748 else if(Angle > OuterAngle)
750 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
751 DryMix *= ConeVolume;
752 if(ALSource->WetGainAuto)
753 WetMix *= ConeVolume;
754 if(ALSource->DryGainHFAuto)
755 DryGainHF *= (1.0f+(OuterGainHF-1.0f));
756 if(ALSource->WetGainHFAuto)
757 WetGainHF *= (1.0f+(OuterGainHF-1.0f));
760 //4. Calculate Velocity
761 if(DopplerFactor != 0.0f)
763 ALfloat flVSS, flVLS = 0.0f;
765 if(ALSource->bHeadRelative==AL_FALSE)
766 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
767 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
769 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
771 if (flVSS >= flMaxVelocity)
772 flVSS = (flMaxVelocity - 1.0f);
773 else if (flVSS <= -flMaxVelocity)
774 flVSS = -flMaxVelocity + 1.0f;
776 if (flVLS >= flMaxVelocity)
777 flVLS = (flMaxVelocity - 1.0f);
778 else if (flVLS <= -flMaxVelocity)
779 flVLS = -flMaxVelocity + 1.0f;
781 pitch[0] = ALSource->flPitch *
782 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
783 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
785 else
786 pitch[0] = ALSource->flPitch;
788 if(ALSource->Send[0].Slot &&
789 ALSource->Send[0].Slot->effect.type != AL_EFFECT_NULL)
791 if(ALSource->Send[0].Slot->AuxSendAuto)
793 // Apply minimal attenuation in place of missing statistical
794 // reverb model.
795 WetMix *= pow(DryMix, 1.0f / 2.0f);
797 else
799 // If the slot's auxilliary send auto is off, the data sent to the
800 // effect slot is the same as the dry path, sans filter effects
801 WetMix = DryMix;
802 WetGainHF = DryGainHF;
805 // Note that this is really applied by the effect slot. However,
806 // it's easier (more optimal) to handle it here.
807 if(ALSource->Send[0].Slot->effect.type == AL_EFFECT_REVERB)
808 WetGainHF *= ALSource->Send[0].Slot->effect.Reverb.GainHF;
810 else
812 WetMix = 0.0f;
813 WetGainHF = 1.0f;
816 //5. Apply filter gains and filters
817 switch(ALSource->DirectFilter.type)
819 case AL_FILTER_LOWPASS:
820 DryMix *= ALSource->DirectFilter.Gain;
821 DryGainHF *= ALSource->DirectFilter.GainHF;
822 break;
825 switch(ALSource->Send[0].WetFilter.type)
827 case AL_FILTER_LOWPASS:
828 WetMix *= ALSource->Send[0].WetFilter.Gain;
829 WetGainHF *= ALSource->Send[0].WetFilter.GainHF;
830 break;
833 DryMix *= ListenerGain;
834 WetMix *= ListenerGain;
836 // Use energy-preserving panning algorithm for multi-speaker playback
837 aluNormalize(Position);
839 pos = aluCart2LUTpos(-Position[2], Position[0]);
840 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
842 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
843 // elevation adjustment for directional gain. this sucks, but
844 // has low complexity
845 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
846 for(s = 0; s < OUTPUTCHANNELS; s++)
848 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
849 drysend[s] = DryMix * gain;
851 *wetsend = WetMix;
853 // Update filter coefficients. Calculations based on the I3DL2 spec.
854 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
855 // We use four chained one-pole filters, so we need to take the fourth
856 // root of the squared gain, which is the same as the square root of
857 // the base gain.
858 // Be careful with gains < 0.0001, as that causes the coefficient to
859 // head towards 1, which will flatten the signal
860 g = aluSqrt(__max(DryGainHF, 0.0001f));
861 a = 0.0f;
862 if(g < 0.9999f) // 1-epsilon
863 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
864 ALSource->iirFilter.coeff = a;
866 g = aluSqrt(__max(WetGainHF, 0.0001f));
867 a = 0.0f;
868 if(g < 0.9999f) // 1-epsilon
869 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
870 ALSource->Send[0].iirFilter.coeff = a;
872 *drygainhf = DryGainHF;
873 *wetgainhf = WetGainHF;
875 else
877 //1. Multi-channel buffers always play "normal"
878 pitch[0] = ALSource->flPitch;
880 DryMix = SourceVolume;
881 DryMix = __min(DryMix,MaxVolume);
882 DryMix = __max(DryMix,MinVolume);
884 switch(ALSource->DirectFilter.type)
886 case AL_FILTER_LOWPASS:
887 DryMix *= ALSource->DirectFilter.Gain;
888 DryGainHF *= ALSource->DirectFilter.GainHF;
889 break;
892 drysend[FRONT_LEFT] = DryMix * ListenerGain;
893 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
894 drysend[SIDE_LEFT] = DryMix * ListenerGain;
895 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
896 drysend[BACK_LEFT] = DryMix * ListenerGain;
897 drysend[BACK_RIGHT] = DryMix * ListenerGain;
898 drysend[FRONT_CENTER] = DryMix * ListenerGain;
899 drysend[BACK_CENTER] = DryMix * ListenerGain;
900 drysend[LFE] = DryMix * ListenerGain;
901 *wetsend = 0.0f;
903 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
904 g = __max(DryGainHF, 0.01f);
905 a = 0.0f;
906 if(g < 0.9999f) // 1-epsilon
907 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
908 ALSource->iirFilter.coeff = a;
909 ALSource->Send[0].iirFilter.coeff = 0.0f;
911 *drygainhf = DryGainHF;
912 *wetgainhf = WetGainHF;
916 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
918 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
921 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
923 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
924 static float WetBuffer[BUFFERSIZE];
925 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
926 ALfloat newDrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
927 ALfloat newWetSend = 0.0f;
928 ALfloat DryGainHF = 0.0f;
929 ALfloat WetGainHF = 0.0f;
930 ALfloat *DrySend;
931 ALfloat *WetSend;
932 ALuint rampLength;
933 ALfloat dryGainStep[OUTPUTCHANNELS];
934 ALfloat wetGainStep;
935 ALuint BlockAlign,BufferSize;
936 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
937 ALuint Channels,Frequency,ulExtraSamples;
938 ALfloat Pitch;
939 ALint Looping,State;
940 ALint increment;
941 ALuint Buffer;
942 ALuint SamplesToDo;
943 ALsource *ALSource;
944 ALbuffer *ALBuffer;
945 ALeffectslot *ALEffectSlot;
946 ALfloat values[OUTPUTCHANNELS];
947 ALfloat value;
948 ALshort *Data;
949 ALuint i,j,k,out;
950 ALbufferlistitem *BufferListItem;
951 ALuint loop;
952 ALint64 DataSize64,DataPos64;
953 FILTER *DryFilter, *WetFilter;
954 int fpuState;
956 SuspendContext(ALContext);
958 #if defined(HAVE_FESETROUND)
959 fpuState = fegetround();
960 fesetround(FE_TOWARDZERO);
961 #elif defined(HAVE__CONTROLFP)
962 fpuState = _controlfp(0, 0);
963 _controlfp(_RC_CHOP, _MCW_RC);
964 #else
965 (void)fpuState;
966 #endif
968 //Figure output format variables
969 BlockAlign = aluChannelsFromFormat(format);
970 BlockAlign *= aluBytesFromFormat(format);
972 size /= BlockAlign;
973 while(size > 0)
975 //Setup variables
976 SamplesToDo = min(size, BUFFERSIZE);
977 if(ALContext)
979 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
980 ALSource = ALContext->Source;
981 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
983 else
985 ALEffectSlot = NULL;
986 ALSource = NULL;
987 rampLength = 0;
989 rampLength = max(rampLength, SamplesToDo);
991 //Clear mixing buffer
992 memset(WetBuffer, 0, SamplesToDo*sizeof(ALfloat));
993 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
995 //Actual mixing loop
996 while(ALSource)
998 j = 0;
999 State = ALSource->state;
1001 while(State == AL_PLAYING && j < SamplesToDo)
1003 DataSize = 0;
1004 DataPosInt = 0;
1005 DataPosFrac = 0;
1007 //Get buffer info
1008 if((Buffer = ALSource->ulBufferID))
1010 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
1012 Data = ALBuffer->data;
1013 Channels = aluChannelsFromFormat(ALBuffer->format);
1014 DataSize = ALBuffer->size;
1015 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
1016 Frequency = ALBuffer->frequency;
1017 DataPosInt = ALSource->position;
1018 DataPosFrac = ALSource->position_fraction;
1020 if(DataPosInt >= DataSize)
1021 goto skipmix;
1023 CalcSourceParams(ALContext, ALSource,
1024 (Channels==1) ? AL_TRUE : AL_FALSE,
1025 newDrySend, &newWetSend, &Pitch,
1026 &DryGainHF, &WetGainHF);
1028 Pitch = (Pitch*Frequency) / ALContext->Frequency;
1030 if(DuplicateStereo && Channels > 1)
1032 if(Channels == 2)
1034 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
1035 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
1036 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
1037 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
1039 else
1041 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
1042 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
1043 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
1044 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
1048 //Get source info
1049 DryFilter = &ALSource->iirFilter;
1050 WetFilter = &ALSource->Send[0].iirFilter;
1051 DrySend = ALSource->DryGains;
1052 WetSend = &ALSource->WetGain;
1054 //Compute the gain steps for each output channel
1055 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
1057 for(i = 0;i < OUTPUTCHANNELS;i++)
1059 DrySend[i] = newDrySend[i];
1060 dryGainStep[i] = 0;
1062 *WetSend = newWetSend;
1063 wetGainStep = 0;
1065 else
1067 for(i = 0;i < OUTPUTCHANNELS;i++)
1068 dryGainStep[i] = (newDrySend[i]-DrySend[i]) / rampLength;
1069 wetGainStep = (newWetSend-(*WetSend)) / rampLength;
1071 ALSource->FirstStart = AL_FALSE;
1073 //Compute 18.14 fixed point step
1074 if(Pitch > (float)MAX_PITCH)
1075 Pitch = (float)MAX_PITCH;
1076 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
1077 if(increment <= 0)
1078 increment = (1<<FRACTIONBITS);
1080 //Figure out how many samples we can mix.
1081 DataSize64 = DataSize;
1082 DataSize64 <<= FRACTIONBITS;
1083 DataPos64 = DataPosInt;
1084 DataPos64 <<= FRACTIONBITS;
1085 DataPos64 += DataPosFrac;
1086 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1088 BufferListItem = ALSource->queue;
1089 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
1091 if(BufferListItem)
1092 BufferListItem = BufferListItem->next;
1094 if (BufferListItem)
1096 if (BufferListItem->next)
1098 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1099 if(NextBuf && NextBuf->data)
1101 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1102 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1105 else if (ALSource->bLooping)
1107 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->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
1115 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1117 BufferSize = min(BufferSize, (SamplesToDo-j));
1119 //Actual sample mixing loop
1120 k = 0;
1121 Data += DataPosInt*Channels;
1123 if(Channels == 1) /* Mono */
1125 ALfloat outsamp;
1127 while(BufferSize--)
1129 for(i = 0;i < OUTPUTCHANNELS;i++)
1130 DrySend[i] += dryGainStep[i];
1131 *WetSend += wetGainStep;
1133 //First order interpolator
1134 value = lerp(Data[k], Data[k+1], DataPosFrac);
1136 //Direct path final mix buffer and panning
1137 outsamp = lpFilter(DryFilter, value);
1138 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1139 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1140 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1141 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1142 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1143 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1144 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1145 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1147 //Room path final mix buffer and panning
1148 outsamp = lpFilter(WetFilter, value);
1149 WetBuffer[j] += outsamp*(*WetSend);
1151 DataPosFrac += increment;
1152 k += DataPosFrac>>FRACTIONBITS;
1153 DataPosFrac &= FRACTIONMASK;
1154 j++;
1157 else if(Channels == 2) /* Stereo */
1159 const int chans[] = {
1160 FRONT_LEFT, FRONT_RIGHT
1163 #define DO_MIX() do { \
1164 *WetSend += wetGainStep*BufferSize; \
1165 while(BufferSize--) \
1167 for(i = 0;i < OUTPUTCHANNELS;i++) \
1168 DrySend[i] += dryGainStep[i]; \
1170 for(i = 0;i < Channels;i++) \
1172 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1173 values[i] = lpFilterMC(DryFilter, chans[i], value)*DrySend[chans[i]]; \
1175 for(out = 0;out < OUTPUTCHANNELS;out++) \
1177 ALfloat sum = 0.0f; \
1178 for(i = 0;i < Channels;i++) \
1179 sum += values[i]*Matrix[chans[i]][out]; \
1180 DryBuffer[j][out] += sum; \
1183 DataPosFrac += increment; \
1184 k += DataPosFrac>>FRACTIONBITS; \
1185 DataPosFrac &= FRACTIONMASK; \
1186 j++; \
1188 } while(0)
1190 DO_MIX();
1192 else if(Channels == 4) /* Quad */
1194 const int chans[] = {
1195 FRONT_LEFT, FRONT_RIGHT,
1196 BACK_LEFT, BACK_RIGHT
1199 DO_MIX();
1201 else if(Channels == 6) /* 5.1 */
1203 const int chans[] = {
1204 FRONT_LEFT, FRONT_RIGHT,
1205 FRONT_CENTER, LFE,
1206 BACK_LEFT, BACK_RIGHT
1209 DO_MIX();
1211 else if(Channels == 7) /* 6.1 */
1213 const int chans[] = {
1214 FRONT_LEFT, FRONT_RIGHT,
1215 FRONT_CENTER, LFE,
1216 BACK_CENTER,
1217 SIDE_LEFT, SIDE_RIGHT
1220 DO_MIX();
1222 else if(Channels == 8) /* 7.1 */
1224 const int chans[] = {
1225 FRONT_LEFT, FRONT_RIGHT,
1226 FRONT_CENTER, LFE,
1227 BACK_LEFT, BACK_RIGHT,
1228 SIDE_LEFT, SIDE_RIGHT
1231 DO_MIX();
1232 #undef DO_MIX
1234 else /* Unknown? */
1236 *WetSend += wetGainStep*BufferSize;
1237 for(i = 0;i < OUTPUTCHANNELS;i++)
1238 DrySend[i] += dryGainStep[i]*BufferSize;
1239 while(BufferSize--)
1241 DataPosFrac += increment;
1242 k += DataPosFrac>>FRACTIONBITS;
1243 DataPosFrac &= FRACTIONMASK;
1244 j++;
1247 DataPosInt += k;
1249 //Update source info
1250 ALSource->position = DataPosInt;
1251 ALSource->position_fraction = DataPosFrac;
1253 skipmix: ;
1256 //Handle looping sources
1257 if(!Buffer || DataPosInt >= DataSize)
1259 //queueing
1260 if(ALSource->queue)
1262 Looping = ALSource->bLooping;
1263 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1265 BufferListItem = ALSource->queue;
1266 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1268 if(BufferListItem)
1270 if(!Looping)
1271 BufferListItem->bufferstate = PROCESSED;
1272 BufferListItem = BufferListItem->next;
1275 if(BufferListItem)
1276 ALSource->ulBufferID = BufferListItem->buffer;
1277 ALSource->position = DataPosInt-DataSize;
1278 ALSource->position_fraction = DataPosFrac;
1279 ALSource->BuffersPlayed++;
1281 else
1283 if(!Looping)
1285 /* alSourceStop */
1286 ALSource->state = AL_STOPPED;
1287 ALSource->inuse = AL_FALSE;
1288 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1289 BufferListItem = ALSource->queue;
1290 while(BufferListItem != NULL)
1292 BufferListItem->bufferstate = PROCESSED;
1293 BufferListItem = BufferListItem->next;
1295 ALSource->position = DataSize;
1296 ALSource->position_fraction = 0;
1298 else
1300 /* alSourceRewind */
1301 /* alSourcePlay */
1302 ALSource->state = AL_PLAYING;
1303 ALSource->inuse = AL_TRUE;
1304 ALSource->play = AL_TRUE;
1305 ALSource->BuffersPlayed = 0;
1306 BufferListItem = ALSource->queue;
1307 while(BufferListItem != NULL)
1309 BufferListItem->bufferstate = PENDING;
1310 BufferListItem = BufferListItem->next;
1312 ALSource->ulBufferID = ALSource->queue->buffer;
1314 if(ALSource->BuffersInQueue == 1)
1315 ALSource->position = DataPosInt%DataSize;
1316 else
1317 ALSource->position = DataPosInt-DataSize;
1318 ALSource->position_fraction = DataPosFrac;
1324 //Get source state
1325 State = ALSource->state;
1328 ALSource = ALSource->next;
1331 // effect slot processing
1332 while(ALEffectSlot)
1334 if(ALEffectSlot->effect.type == AL_EFFECT_REVERB)
1335 VerbProcess(ALEffectSlot->ReverbState, SamplesToDo, WetBuffer, DryBuffer);
1337 ALEffectSlot = ALEffectSlot->next;
1340 //Post processing loop
1341 switch(format)
1343 case AL_FORMAT_MONO8:
1344 for(i = 0;i < SamplesToDo;i++)
1346 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1347 buffer = ((ALubyte*)buffer) + 1;
1349 break;
1350 case AL_FORMAT_STEREO8:
1351 if(ALContext && ALContext->bs2b)
1353 for(i = 0;i < SamplesToDo;i++)
1355 float samples[2];
1356 samples[0] = DryBuffer[i][FRONT_LEFT];
1357 samples[1] = DryBuffer[i][FRONT_RIGHT];
1358 bs2b_cross_feed(ALContext->bs2b, samples);
1359 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1360 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1361 buffer = ((ALubyte*)buffer) + 2;
1364 else
1366 for(i = 0;i < SamplesToDo;i++)
1368 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1369 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1370 buffer = ((ALubyte*)buffer) + 2;
1373 break;
1374 case AL_FORMAT_QUAD8:
1375 for(i = 0;i < SamplesToDo;i++)
1377 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1378 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1379 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1380 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1381 buffer = ((ALubyte*)buffer) + 4;
1383 break;
1384 case AL_FORMAT_51CHN8:
1385 for(i = 0;i < SamplesToDo;i++)
1387 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1388 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1389 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1390 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1391 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1392 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1393 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1394 #else
1395 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1396 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1397 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1398 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1399 #endif
1400 buffer = ((ALubyte*)buffer) + 6;
1402 break;
1403 case AL_FORMAT_61CHN8:
1404 for(i = 0;i < SamplesToDo;i++)
1406 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1407 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1408 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1409 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1410 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_CENTER])>>8)+128);
1411 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1412 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1413 buffer = ((ALubyte*)buffer) + 7;
1415 break;
1416 case AL_FORMAT_71CHN8:
1417 for(i = 0;i < SamplesToDo;i++)
1419 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1420 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1421 #ifdef _WIN32
1422 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1423 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1424 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1425 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1426 #else
1427 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1428 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1429 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1430 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1431 #endif
1432 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1433 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1434 buffer = ((ALubyte*)buffer) + 8;
1436 break;
1438 case AL_FORMAT_MONO16:
1439 for(i = 0;i < SamplesToDo;i++)
1441 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1442 buffer = ((ALshort*)buffer) + 1;
1444 break;
1445 case AL_FORMAT_STEREO16:
1446 if(ALContext && ALContext->bs2b)
1448 for(i = 0;i < SamplesToDo;i++)
1450 float samples[2];
1451 samples[0] = DryBuffer[i][FRONT_LEFT];
1452 samples[1] = DryBuffer[i][FRONT_RIGHT];
1453 bs2b_cross_feed(ALContext->bs2b, samples);
1454 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1455 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1456 buffer = ((ALshort*)buffer) + 2;
1459 else
1461 for(i = 0;i < SamplesToDo;i++)
1463 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1464 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1465 buffer = ((ALshort*)buffer) + 2;
1468 break;
1469 case AL_FORMAT_QUAD16:
1470 for(i = 0;i < SamplesToDo;i++)
1472 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1473 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1474 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1475 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1476 buffer = ((ALshort*)buffer) + 4;
1478 break;
1479 case AL_FORMAT_51CHN16:
1480 for(i = 0;i < SamplesToDo;i++)
1482 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1483 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1484 #ifdef _WIN32
1485 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1486 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1487 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1488 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1489 #else
1490 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1491 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1492 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1493 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1494 #endif
1495 buffer = ((ALshort*)buffer) + 6;
1497 break;
1498 case AL_FORMAT_61CHN16:
1499 for(i = 0;i < SamplesToDo;i++)
1501 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1502 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1503 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1504 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1505 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_CENTER]);
1506 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1507 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1508 buffer = ((ALshort*)buffer) + 7;
1510 break;
1511 case AL_FORMAT_71CHN16:
1512 for(i = 0;i < SamplesToDo;i++)
1514 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1515 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1516 #ifdef _WIN32
1517 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1518 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1519 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1520 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1521 #else
1522 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1523 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1524 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1525 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1526 #endif
1527 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1528 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1529 buffer = ((ALshort*)buffer) + 8;
1531 break;
1533 default:
1534 break;
1537 size -= SamplesToDo;
1540 #if defined(HAVE_FESETROUND)
1541 fesetround(fpuState);
1542 #elif defined(HAVE__CONTROLFP)
1543 _controlfp(fpuState, 0xfffff);
1544 #endif
1546 ProcessContext(ALContext);