Constify some parameters
[openal-soft/openal-hmr.git] / Alc / ALu.c
blobeee4fa9aa7ba76172118170b5e8e13ef86528c9f
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(const ALfloat *inVector1, const 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(const ALfloat *inVector1, const 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(const ALCcontext *ALContext,
558 const ALsource *ALSource, ALenum isMono,
559 ALfloat *drysend, ALfloat *wetsend,
560 ALfloat *pitch, ALfloat *drygainhf,
561 ALfloat *wetgainhf)
563 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix,WetMix=0.0f;
564 ALfloat Direction[3],Position[3],SourceToListener[3];
565 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
566 ALfloat ConeVolume,SourceVolume,ListenerGain;
567 ALfloat U[3],V[3],N[3];
568 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
569 ALfloat Matrix[3][3];
570 ALfloat flAttenuation;
571 ALfloat RoomAttenuation;
572 ALfloat MetersPerUnit;
573 ALfloat RoomRolloff;
574 ALfloat DryGainHF = 1.0f;
575 ALfloat WetGainHF = 1.0f;
576 ALfloat DirGain, AmbientGain;
577 const ALfloat *SpeakerGain;
578 ALint pos, s;
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 *drygainhf = DryGainHF;
854 *wetgainhf = WetGainHF;
856 else
858 //1. Multi-channel buffers always play "normal"
859 pitch[0] = ALSource->flPitch;
861 DryMix = SourceVolume;
862 DryMix = __min(DryMix,MaxVolume);
863 DryMix = __max(DryMix,MinVolume);
865 switch(ALSource->DirectFilter.type)
867 case AL_FILTER_LOWPASS:
868 DryMix *= ALSource->DirectFilter.Gain;
869 DryGainHF *= ALSource->DirectFilter.GainHF;
870 break;
873 drysend[FRONT_LEFT] = DryMix * ListenerGain;
874 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
875 drysend[SIDE_LEFT] = DryMix * ListenerGain;
876 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
877 drysend[BACK_LEFT] = DryMix * ListenerGain;
878 drysend[BACK_RIGHT] = DryMix * ListenerGain;
879 drysend[FRONT_CENTER] = DryMix * ListenerGain;
880 drysend[BACK_CENTER] = DryMix * ListenerGain;
881 drysend[LFE] = DryMix * ListenerGain;
882 *wetsend = 0.0f;
884 *drygainhf = DryGainHF;
885 *wetgainhf = WetGainHF;
889 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
891 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
894 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
896 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
897 static float WetBuffer[BUFFERSIZE];
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(WetBuffer, 0, SamplesToDo*sizeof(ALfloat));
967 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
969 //Actual mixing loop
970 while(ALSource)
972 j = 0;
973 State = ALSource->state;
975 while(State == AL_PLAYING && j < SamplesToDo)
977 DataSize = 0;
978 DataPosInt = 0;
979 DataPosFrac = 0;
981 //Get buffer info
982 if((Buffer = ALSource->ulBufferID))
984 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
986 Data = ALBuffer->data;
987 Channels = aluChannelsFromFormat(ALBuffer->format);
988 DataSize = ALBuffer->size;
989 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
990 Frequency = ALBuffer->frequency;
991 DataPosInt = ALSource->position;
992 DataPosFrac = ALSource->position_fraction;
994 if(DataPosInt >= DataSize)
995 goto skipmix;
997 //Get source info
998 DryFilter = &ALSource->iirFilter;
999 WetFilter = &ALSource->Send[0].iirFilter;
1000 DrySend = ALSource->DryGains;
1001 WetSend = &ALSource->WetGain;
1003 CalcSourceParams(ALContext, ALSource,
1004 (Channels==1) ? AL_TRUE : AL_FALSE,
1005 newDrySend, &newWetSend, &Pitch,
1006 &DryGainHF, &WetGainHF);
1007 Pitch = (Pitch*Frequency) / ALContext->Frequency;
1009 if(Channels == 1)
1011 // Update filter coefficients. Calculations based on
1012 // the I3DL2 spec.
1013 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
1014 // We use four chained one-pole filters, so we need to
1015 // take the fourth root of the squared gain, which is
1016 // the same as the square root of the base gain.
1017 // Be careful with gains < 0.0001, as that causes the
1018 // coefficient to head towards 1, which will flatten
1019 // the signal
1020 g = aluSqrt(__max(DryGainHF, 0.0001f));
1021 a = 0.0f;
1022 if(g < 0.9999f) // 1-epsilon
1023 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
1024 DryFilter->coeff = a;
1026 g = aluSqrt(__max(WetGainHF, 0.0001f));
1027 a = 0.0f;
1028 if(g < 0.9999f) // 1-epsilon
1029 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
1030 WetFilter->coeff = a;
1032 else
1034 // Multi-channel sources use two chained one-pole
1035 // filters, so take the base gain (square root of the
1036 // squared gain)
1037 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
1038 g = __max(DryGainHF, 0.01f);
1039 a = 0.0f;
1040 if(g < 0.9999f) // 1-epsilon
1041 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
1042 DryFilter->coeff = a;
1043 WetFilter->coeff = 0.0f;
1045 if(DuplicateStereo && Channels == 2)
1047 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
1048 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
1049 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
1050 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
1052 else if(DuplicateStereo)
1054 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
1055 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
1056 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
1057 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
1061 //Compute the gain steps for each output channel
1062 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
1064 for(i = 0;i < OUTPUTCHANNELS;i++)
1066 DrySend[i] = newDrySend[i];
1067 dryGainStep[i] = 0;
1069 *WetSend = newWetSend;
1070 wetGainStep = 0;
1072 else
1074 for(i = 0;i < OUTPUTCHANNELS;i++)
1075 dryGainStep[i] = (newDrySend[i]-DrySend[i]) / rampLength;
1076 wetGainStep = (newWetSend-(*WetSend)) / rampLength;
1078 ALSource->FirstStart = AL_FALSE;
1080 //Compute 18.14 fixed point step
1081 if(Pitch > (float)MAX_PITCH)
1082 Pitch = (float)MAX_PITCH;
1083 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
1084 if(increment <= 0)
1085 increment = (1<<FRACTIONBITS);
1087 //Figure out how many samples we can mix.
1088 DataSize64 = DataSize;
1089 DataSize64 <<= FRACTIONBITS;
1090 DataPos64 = DataPosInt;
1091 DataPos64 <<= FRACTIONBITS;
1092 DataPos64 += DataPosFrac;
1093 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1095 BufferListItem = ALSource->queue;
1096 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
1098 if(BufferListItem)
1099 BufferListItem = BufferListItem->next;
1101 if (BufferListItem)
1103 if (BufferListItem->next)
1105 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1106 if(NextBuf && NextBuf->data)
1108 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1109 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1112 else if (ALSource->bLooping)
1114 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
1115 if (NextBuf && NextBuf->data)
1117 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1118 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1121 else
1122 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1124 BufferSize = min(BufferSize, (SamplesToDo-j));
1126 //Actual sample mixing loop
1127 k = 0;
1128 Data += DataPosInt*Channels;
1130 if(Channels == 1) /* Mono */
1132 ALfloat outsamp;
1134 while(BufferSize--)
1136 for(i = 0;i < OUTPUTCHANNELS;i++)
1137 DrySend[i] += dryGainStep[i];
1138 *WetSend += wetGainStep;
1140 //First order interpolator
1141 value = lerp(Data[k], Data[k+1], DataPosFrac);
1143 //Direct path final mix buffer and panning
1144 outsamp = lpFilter(DryFilter, value);
1145 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1146 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1147 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1148 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1149 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1150 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1151 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1152 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1154 //Room path final mix buffer and panning
1155 outsamp = lpFilter(WetFilter, value);
1156 WetBuffer[j] += outsamp*(*WetSend);
1158 DataPosFrac += increment;
1159 k += DataPosFrac>>FRACTIONBITS;
1160 DataPosFrac &= FRACTIONMASK;
1161 j++;
1164 else if(Channels == 2) /* Stereo */
1166 const int chans[] = {
1167 FRONT_LEFT, FRONT_RIGHT
1170 #define DO_MIX() do { \
1171 *WetSend += wetGainStep*BufferSize; \
1172 while(BufferSize--) \
1174 for(i = 0;i < OUTPUTCHANNELS;i++) \
1175 DrySend[i] += dryGainStep[i]; \
1177 for(i = 0;i < Channels;i++) \
1179 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1180 values[i] = lpFilterMC(DryFilter, chans[i], value)*DrySend[chans[i]]; \
1182 for(out = 0;out < OUTPUTCHANNELS;out++) \
1184 ALfloat sum = 0.0f; \
1185 for(i = 0;i < Channels;i++) \
1186 sum += values[i]*Matrix[chans[i]][out]; \
1187 DryBuffer[j][out] += sum; \
1190 DataPosFrac += increment; \
1191 k += DataPosFrac>>FRACTIONBITS; \
1192 DataPosFrac &= FRACTIONMASK; \
1193 j++; \
1195 } while(0)
1197 DO_MIX();
1199 else if(Channels == 4) /* Quad */
1201 const int chans[] = {
1202 FRONT_LEFT, FRONT_RIGHT,
1203 BACK_LEFT, BACK_RIGHT
1206 DO_MIX();
1208 else if(Channels == 6) /* 5.1 */
1210 const int chans[] = {
1211 FRONT_LEFT, FRONT_RIGHT,
1212 FRONT_CENTER, LFE,
1213 BACK_LEFT, BACK_RIGHT
1216 DO_MIX();
1218 else if(Channels == 7) /* 6.1 */
1220 const int chans[] = {
1221 FRONT_LEFT, FRONT_RIGHT,
1222 FRONT_CENTER, LFE,
1223 BACK_CENTER,
1224 SIDE_LEFT, SIDE_RIGHT
1227 DO_MIX();
1229 else if(Channels == 8) /* 7.1 */
1231 const int chans[] = {
1232 FRONT_LEFT, FRONT_RIGHT,
1233 FRONT_CENTER, LFE,
1234 BACK_LEFT, BACK_RIGHT,
1235 SIDE_LEFT, SIDE_RIGHT
1238 DO_MIX();
1239 #undef DO_MIX
1241 else /* Unknown? */
1243 *WetSend += wetGainStep*BufferSize;
1244 for(i = 0;i < OUTPUTCHANNELS;i++)
1245 DrySend[i] += dryGainStep[i]*BufferSize;
1246 while(BufferSize--)
1248 DataPosFrac += increment;
1249 k += DataPosFrac>>FRACTIONBITS;
1250 DataPosFrac &= FRACTIONMASK;
1251 j++;
1254 DataPosInt += k;
1256 //Update source info
1257 ALSource->position = DataPosInt;
1258 ALSource->position_fraction = DataPosFrac;
1260 skipmix: ;
1263 //Handle looping sources
1264 if(!Buffer || DataPosInt >= DataSize)
1266 //queueing
1267 if(ALSource->queue)
1269 Looping = ALSource->bLooping;
1270 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1272 BufferListItem = ALSource->queue;
1273 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1275 if(BufferListItem)
1277 if(!Looping)
1278 BufferListItem->bufferstate = PROCESSED;
1279 BufferListItem = BufferListItem->next;
1282 if(BufferListItem)
1283 ALSource->ulBufferID = BufferListItem->buffer;
1284 ALSource->position = DataPosInt-DataSize;
1285 ALSource->position_fraction = DataPosFrac;
1286 ALSource->BuffersPlayed++;
1288 else
1290 if(!Looping)
1292 /* alSourceStop */
1293 ALSource->state = AL_STOPPED;
1294 ALSource->inuse = AL_FALSE;
1295 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1296 BufferListItem = ALSource->queue;
1297 while(BufferListItem != NULL)
1299 BufferListItem->bufferstate = PROCESSED;
1300 BufferListItem = BufferListItem->next;
1302 ALSource->position = DataSize;
1303 ALSource->position_fraction = 0;
1305 else
1307 /* alSourceRewind */
1308 /* alSourcePlay */
1309 ALSource->state = AL_PLAYING;
1310 ALSource->inuse = AL_TRUE;
1311 ALSource->play = AL_TRUE;
1312 ALSource->BuffersPlayed = 0;
1313 BufferListItem = ALSource->queue;
1314 while(BufferListItem != NULL)
1316 BufferListItem->bufferstate = PENDING;
1317 BufferListItem = BufferListItem->next;
1319 ALSource->ulBufferID = ALSource->queue->buffer;
1321 if(ALSource->BuffersInQueue == 1)
1322 ALSource->position = DataPosInt%DataSize;
1323 else
1324 ALSource->position = DataPosInt-DataSize;
1325 ALSource->position_fraction = DataPosFrac;
1331 //Get source state
1332 State = ALSource->state;
1335 ALSource = ALSource->next;
1338 // effect slot processing
1339 while(ALEffectSlot)
1341 if(ALEffectSlot->effect.type == AL_EFFECT_REVERB)
1342 VerbProcess(ALEffectSlot->ReverbState, SamplesToDo, WetBuffer, DryBuffer);
1344 ALEffectSlot = ALEffectSlot->next;
1347 //Post processing loop
1348 switch(format)
1350 case AL_FORMAT_MONO8:
1351 for(i = 0;i < SamplesToDo;i++)
1353 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1354 buffer = ((ALubyte*)buffer) + 1;
1356 break;
1357 case AL_FORMAT_STEREO8:
1358 if(ALContext && ALContext->bs2b)
1360 for(i = 0;i < SamplesToDo;i++)
1362 float samples[2];
1363 samples[0] = DryBuffer[i][FRONT_LEFT];
1364 samples[1] = DryBuffer[i][FRONT_RIGHT];
1365 bs2b_cross_feed(ALContext->bs2b, samples);
1366 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1367 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1368 buffer = ((ALubyte*)buffer) + 2;
1371 else
1373 for(i = 0;i < SamplesToDo;i++)
1375 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1376 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1377 buffer = ((ALubyte*)buffer) + 2;
1380 break;
1381 case AL_FORMAT_QUAD8:
1382 for(i = 0;i < SamplesToDo;i++)
1384 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1385 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1386 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1387 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1388 buffer = ((ALubyte*)buffer) + 4;
1390 break;
1391 case AL_FORMAT_51CHN8:
1392 for(i = 0;i < SamplesToDo;i++)
1394 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1395 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1396 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1397 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1398 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1399 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1400 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1401 #else
1402 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1403 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1404 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1405 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1406 #endif
1407 buffer = ((ALubyte*)buffer) + 6;
1409 break;
1410 case AL_FORMAT_61CHN8:
1411 for(i = 0;i < SamplesToDo;i++)
1413 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1414 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1415 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1416 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1417 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_CENTER])>>8)+128);
1418 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1419 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1420 buffer = ((ALubyte*)buffer) + 7;
1422 break;
1423 case AL_FORMAT_71CHN8:
1424 for(i = 0;i < SamplesToDo;i++)
1426 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1427 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1428 #ifdef _WIN32
1429 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1430 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1431 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1432 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1433 #else
1434 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1435 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1436 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1437 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1438 #endif
1439 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1440 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1441 buffer = ((ALubyte*)buffer) + 8;
1443 break;
1445 case AL_FORMAT_MONO16:
1446 for(i = 0;i < SamplesToDo;i++)
1448 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1449 buffer = ((ALshort*)buffer) + 1;
1451 break;
1452 case AL_FORMAT_STEREO16:
1453 if(ALContext && ALContext->bs2b)
1455 for(i = 0;i < SamplesToDo;i++)
1457 float samples[2];
1458 samples[0] = DryBuffer[i][FRONT_LEFT];
1459 samples[1] = DryBuffer[i][FRONT_RIGHT];
1460 bs2b_cross_feed(ALContext->bs2b, samples);
1461 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1462 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1463 buffer = ((ALshort*)buffer) + 2;
1466 else
1468 for(i = 0;i < SamplesToDo;i++)
1470 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1471 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1472 buffer = ((ALshort*)buffer) + 2;
1475 break;
1476 case AL_FORMAT_QUAD16:
1477 for(i = 0;i < SamplesToDo;i++)
1479 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1480 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1481 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1482 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1483 buffer = ((ALshort*)buffer) + 4;
1485 break;
1486 case AL_FORMAT_51CHN16:
1487 for(i = 0;i < SamplesToDo;i++)
1489 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1490 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1491 #ifdef _WIN32
1492 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1493 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1494 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1495 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1496 #else
1497 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1498 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1499 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1500 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1501 #endif
1502 buffer = ((ALshort*)buffer) + 6;
1504 break;
1505 case AL_FORMAT_61CHN16:
1506 for(i = 0;i < SamplesToDo;i++)
1508 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1509 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1510 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1511 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1512 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_CENTER]);
1513 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1514 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1515 buffer = ((ALshort*)buffer) + 7;
1517 break;
1518 case AL_FORMAT_71CHN16:
1519 for(i = 0;i < SamplesToDo;i++)
1521 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1522 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1523 #ifdef _WIN32
1524 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1525 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1526 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1527 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1528 #else
1529 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1530 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1531 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1532 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1533 #endif
1534 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1535 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1536 buffer = ((ALshort*)buffer) + 8;
1538 break;
1540 default:
1541 break;
1544 size -= SamplesToDo;
1547 #if defined(HAVE_FESETROUND)
1548 fesetround(fpuState);
1549 #elif defined(HAVE__CONTROLFP)
1550 _controlfp(fpuState, 0xfffff);
1551 #endif
1553 ProcessContext(ALContext);