Apply slot gain on slot output, not input
[openal-soft.git] / Alc / ALu.c
bloba7845dc3c3b51eac3a8ee8472b9f4c4a67d7bb19
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 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <assert.h>
29 #include "alMain.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
32 #include "alSource.h"
33 #include "alBuffer.h"
34 #include "alThunk.h"
35 #include "alListener.h"
36 #include "alAuxEffectSlot.h"
37 #include "alu.h"
38 #include "bs2b.h"
40 #if defined(HAVE_STDINT_H)
41 #include <stdint.h>
42 typedef int64_t ALint64;
43 #elif defined(HAVE___INT64)
44 typedef __int64 ALint64;
45 #elif (SIZEOF_LONG == 8)
46 typedef long ALint64;
47 #elif (SIZEOF_LONG_LONG == 8)
48 typedef long long ALint64;
49 #endif
51 #define FRACTIONBITS 14
52 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
53 #define MAX_PITCH 65536
55 /* Minimum ramp length in milliseconds. The value below was chosen to
56 * adequately reduce clicks and pops from harsh gain changes. */
57 #define MIN_RAMP_LENGTH 16
59 ALboolean DuplicateStereo = AL_FALSE;
61 /* NOTE: The AL_FORMAT_REAR* enums aren't handled here be cause they're
62 * converted to AL_FORMAT_QUAD* when loaded */
63 __inline ALuint aluBytesFromFormat(ALenum format)
65 switch(format)
67 case AL_FORMAT_MONO8:
68 case AL_FORMAT_STEREO8:
69 case AL_FORMAT_QUAD8_LOKI:
70 case AL_FORMAT_QUAD8:
71 case AL_FORMAT_51CHN8:
72 case AL_FORMAT_61CHN8:
73 case AL_FORMAT_71CHN8:
74 return 1;
76 case AL_FORMAT_MONO16:
77 case AL_FORMAT_STEREO16:
78 case AL_FORMAT_QUAD16_LOKI:
79 case AL_FORMAT_QUAD16:
80 case AL_FORMAT_51CHN16:
81 case AL_FORMAT_61CHN16:
82 case AL_FORMAT_71CHN16:
83 return 2;
85 case AL_FORMAT_MONO_FLOAT32:
86 case AL_FORMAT_STEREO_FLOAT32:
87 case AL_FORMAT_QUAD32:
88 case AL_FORMAT_51CHN32:
89 case AL_FORMAT_61CHN32:
90 case AL_FORMAT_71CHN32:
91 return 4;
93 default:
94 return 0;
98 __inline ALuint aluChannelsFromFormat(ALenum format)
100 switch(format)
102 case AL_FORMAT_MONO8:
103 case AL_FORMAT_MONO16:
104 case AL_FORMAT_MONO_FLOAT32:
105 return 1;
107 case AL_FORMAT_STEREO8:
108 case AL_FORMAT_STEREO16:
109 case AL_FORMAT_STEREO_FLOAT32:
110 return 2;
112 case AL_FORMAT_QUAD8_LOKI:
113 case AL_FORMAT_QUAD16_LOKI:
114 case AL_FORMAT_QUAD8:
115 case AL_FORMAT_QUAD16:
116 case AL_FORMAT_QUAD32:
117 return 4;
119 case AL_FORMAT_51CHN8:
120 case AL_FORMAT_51CHN16:
121 case AL_FORMAT_51CHN32:
122 return 6;
124 case AL_FORMAT_61CHN8:
125 case AL_FORMAT_61CHN16:
126 case AL_FORMAT_61CHN32:
127 return 7;
129 case AL_FORMAT_71CHN8:
130 case AL_FORMAT_71CHN16:
131 case AL_FORMAT_71CHN32:
132 return 8;
134 default:
135 return 0;
140 static __inline ALshort aluF2S(ALfloat Value)
142 ALint i;
144 i = (ALint)Value;
145 i = __min( 32767, i);
146 i = __max(-32768, i);
147 return ((ALshort)i);
150 static __inline ALvoid aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
152 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
153 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
154 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
157 static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
159 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
160 inVector1[2]*inVector2[2];
163 static __inline ALvoid aluNormalize(ALfloat *inVector)
165 ALfloat length, inverse_length;
167 length = aluSqrt(aluDotproduct(inVector, inVector));
168 if(length != 0.0f)
170 inverse_length = 1.0f/length;
171 inVector[0] *= inverse_length;
172 inVector[1] *= inverse_length;
173 inVector[2] *= inverse_length;
177 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
179 ALfloat result[3];
181 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
182 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
183 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
184 memcpy(vector, result, sizeof(result));
187 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
188 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
190 const char *confkey;
191 const char *next;
192 const char *sep;
193 const char *end;
194 int i, val;
196 confkey = GetConfigValue(NULL, name, "");
197 next = confkey;
198 while(next && *next)
200 confkey = next;
201 next = strchr(confkey, ',');
202 if(next)
204 do {
205 next++;
206 } while(isspace(*next));
209 sep = strchr(confkey, '=');
210 if(!sep || confkey == sep)
211 continue;
213 end = sep - 1;
214 while(isspace(*end) && end != confkey)
215 end--;
217 if(strncmp(confkey, "fl", end-confkey) == 0)
218 val = FRONT_LEFT;
219 else if(strncmp(confkey, "fr", end-confkey) == 0)
220 val = FRONT_RIGHT;
221 else if(strncmp(confkey, "fc", end-confkey) == 0)
222 val = FRONT_CENTER;
223 else if(strncmp(confkey, "bl", end-confkey) == 0)
224 val = BACK_LEFT;
225 else if(strncmp(confkey, "br", end-confkey) == 0)
226 val = BACK_RIGHT;
227 else if(strncmp(confkey, "bc", end-confkey) == 0)
228 val = BACK_CENTER;
229 else if(strncmp(confkey, "sl", end-confkey) == 0)
230 val = SIDE_LEFT;
231 else if(strncmp(confkey, "sr", end-confkey) == 0)
232 val = SIDE_RIGHT;
233 else
235 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
236 continue;
239 sep++;
240 while(isspace(*sep))
241 sep++;
243 for(i = 0;i < chans;i++)
245 if(Speaker2Chan[i] == val)
247 val = strtol(sep, NULL, 10);
248 if(val >= -180 && val <= 180)
249 SpeakerAngle[i] = val * M_PI/180.0f;
250 else
251 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
252 break;
257 for(i = 1;i < chans;i++)
259 if(SpeakerAngle[i] <= SpeakerAngle[i-1])
261 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i, chans,
262 SpeakerAngle[i-1] * 180.0f/M_PI, SpeakerAngle[i] * 180.0f/M_PI);
263 SpeakerAngle[i] = SpeakerAngle[i-1] + 1 * 180.0f/M_PI;
268 static __inline ALfloat aluLUTpos2Angle(ALint pos)
270 if(pos < QUADRANT_NUM)
271 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
272 if(pos < 2 * QUADRANT_NUM)
273 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
274 if(pos < 3 * QUADRANT_NUM)
275 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
276 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
279 ALvoid aluInitPanning(ALCcontext *Context)
281 ALint pos, offset, s;
282 ALfloat Alpha, Theta;
283 ALfloat SpeakerAngle[OUTPUTCHANNELS];
284 ALint Speaker2Chan[OUTPUTCHANNELS];
286 for(s = 0;s < OUTPUTCHANNELS;s++)
288 int s2;
289 for(s2 = 0;s2 < OUTPUTCHANNELS;s2++)
290 Context->ChannelMatrix[s][s2] = ((s==s2) ? 1.0f : 0.0f);
293 switch(Context->Device->Format)
295 /* Mono is rendered as stereo, then downmixed during post-process */
296 case AL_FORMAT_MONO8:
297 case AL_FORMAT_MONO16:
298 case AL_FORMAT_MONO_FLOAT32:
299 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
300 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
301 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
302 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
303 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
304 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
305 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
306 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
307 Context->NumChan = 2;
308 Speaker2Chan[0] = FRONT_LEFT;
309 Speaker2Chan[1] = FRONT_RIGHT;
310 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
311 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
312 break;
314 case AL_FORMAT_STEREO8:
315 case AL_FORMAT_STEREO16:
316 case AL_FORMAT_STEREO_FLOAT32:
317 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
318 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
319 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
320 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
321 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
322 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
323 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
324 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
325 Context->NumChan = 2;
326 Speaker2Chan[0] = FRONT_LEFT;
327 Speaker2Chan[1] = FRONT_RIGHT;
328 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
329 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
330 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
331 break;
333 case AL_FORMAT_QUAD8:
334 case AL_FORMAT_QUAD16:
335 case AL_FORMAT_QUAD32:
336 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
337 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
338 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
339 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
340 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
341 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
342 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
343 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
344 Context->NumChan = 4;
345 Speaker2Chan[0] = BACK_LEFT;
346 Speaker2Chan[1] = FRONT_LEFT;
347 Speaker2Chan[2] = FRONT_RIGHT;
348 Speaker2Chan[3] = BACK_RIGHT;
349 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
350 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
351 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
352 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
353 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
354 break;
356 case AL_FORMAT_51CHN8:
357 case AL_FORMAT_51CHN16:
358 case AL_FORMAT_51CHN32:
359 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
360 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
361 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
362 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
363 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
364 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
365 Context->NumChan = 5;
366 Speaker2Chan[0] = BACK_LEFT;
367 Speaker2Chan[1] = FRONT_LEFT;
368 Speaker2Chan[2] = FRONT_CENTER;
369 Speaker2Chan[3] = FRONT_RIGHT;
370 Speaker2Chan[4] = BACK_RIGHT;
371 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
372 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
373 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
374 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
375 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
376 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
377 break;
379 case AL_FORMAT_61CHN8:
380 case AL_FORMAT_61CHN16:
381 case AL_FORMAT_61CHN32:
382 Context->ChannelMatrix[BACK_LEFT][BACK_CENTER] = aluSqrt(0.5);
383 Context->ChannelMatrix[BACK_LEFT][SIDE_LEFT] = aluSqrt(0.5);
384 Context->ChannelMatrix[BACK_RIGHT][BACK_CENTER] = aluSqrt(0.5);
385 Context->ChannelMatrix[BACK_RIGHT][SIDE_RIGHT] = aluSqrt(0.5);
386 Context->NumChan = 6;
387 Speaker2Chan[0] = SIDE_LEFT;
388 Speaker2Chan[1] = FRONT_LEFT;
389 Speaker2Chan[2] = FRONT_CENTER;
390 Speaker2Chan[3] = FRONT_RIGHT;
391 Speaker2Chan[4] = SIDE_RIGHT;
392 Speaker2Chan[5] = BACK_CENTER;
393 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
394 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
395 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
396 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
397 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
398 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
399 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
400 break;
402 case AL_FORMAT_71CHN8:
403 case AL_FORMAT_71CHN16:
404 case AL_FORMAT_71CHN32:
405 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
406 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
407 Context->NumChan = 7;
408 Speaker2Chan[0] = BACK_LEFT;
409 Speaker2Chan[1] = SIDE_LEFT;
410 Speaker2Chan[2] = FRONT_LEFT;
411 Speaker2Chan[3] = FRONT_CENTER;
412 Speaker2Chan[4] = FRONT_RIGHT;
413 Speaker2Chan[5] = SIDE_RIGHT;
414 Speaker2Chan[6] = BACK_RIGHT;
415 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
416 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
417 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
418 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
419 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
420 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
421 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
422 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
423 break;
425 default:
426 assert(0);
429 for(pos = 0; pos < LUT_NUM; pos++)
431 /* source angle */
432 Theta = aluLUTpos2Angle(pos);
434 /* clear all values */
435 offset = OUTPUTCHANNELS * pos;
436 for(s = 0; s < OUTPUTCHANNELS; s++)
437 Context->PanningLUT[offset+s] = 0.0f;
439 /* set panning values */
440 for(s = 0; s < Context->NumChan - 1; s++)
442 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
444 /* source between speaker s and speaker s+1 */
445 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
446 (SpeakerAngle[s+1]-SpeakerAngle[s]);
447 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
448 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
449 break;
452 if(s == Context->NumChan - 1)
454 /* source between last and first speaker */
455 if(Theta < SpeakerAngle[0])
456 Theta += 2.0f * M_PI;
457 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
458 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
459 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
460 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
465 static __inline ALint aluCart2LUTpos(ALfloat re, ALfloat im)
467 ALint pos = 0;
468 ALfloat denom = aluFabs(re) + aluFabs(im);
469 if(denom > 0.0f)
470 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
472 if(re < 0.0)
473 pos = 2 * QUADRANT_NUM - pos;
474 if(im < 0.0)
475 pos = LUT_NUM - pos;
476 return pos%LUT_NUM;
479 static ALvoid CalcSourceParams(const ALCcontext *ALContext,
480 const ALsource *ALSource, ALenum isMono,
481 ALfloat *drysend, ALfloat *wetsend,
482 ALfloat *pitch, ALfloat *drygainhf,
483 ALfloat *wetgainhf)
485 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix;
486 ALfloat Direction[3],Position[3],SourceToListener[3];
487 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
488 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
489 ALfloat U[3],V[3],N[3];
490 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
491 ALfloat Matrix[3][3];
492 ALfloat flAttenuation;
493 ALfloat RoomAttenuation[MAX_SENDS];
494 ALfloat MetersPerUnit;
495 ALfloat RoomRolloff[MAX_SENDS];
496 ALfloat DryGainHF = 1.0f;
497 ALfloat DirGain, AmbientGain;
498 ALfloat length;
499 const ALfloat *SpeakerGain;
500 ALint NumSends;
501 ALint pos, s, i;
503 //Get context properties
504 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
505 DopplerVelocity = ALContext->DopplerVelocity;
506 flSpeedOfSound = ALContext->flSpeedOfSound;
507 NumSends = ALContext->NumSends;
509 //Get listener properties
510 ListenerGain = ALContext->Listener.Gain;
511 MetersPerUnit = ALContext->Listener.MetersPerUnit;
513 //Get source properties
514 SourceVolume = ALSource->flGain;
515 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
516 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
517 MinVolume = ALSource->flMinGain;
518 MaxVolume = ALSource->flMaxGain;
519 MinDist = ALSource->flRefDistance;
520 MaxDist = ALSource->flMaxDistance;
521 Rolloff = ALSource->flRollOffFactor;
522 InnerAngle = ALSource->flInnerAngle;
523 OuterAngle = ALSource->flOuterAngle;
524 OuterGainHF = ALSource->OuterGainHF;
526 //Only apply 3D calculations for mono buffers
527 if(isMono != AL_FALSE)
529 //1. Translate Listener to origin (convert to head relative)
530 // Note that Direction and SourceToListener are *not* transformed.
531 // SourceToListener is used with the source and listener velocities,
532 // which are untransformed, and Direction is used with SourceToListener
533 // for the sound cone
534 if(ALSource->bHeadRelative==AL_FALSE)
536 // Build transform matrix
537 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
538 aluNormalize(U); // Normalized Right-vector
539 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
540 aluNormalize(V); // Normalized Up-vector
541 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
542 aluNormalize(N); // Normalized At-vector
543 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
544 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
545 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
547 // Translate source position into listener space
548 Position[0] -= ALContext->Listener.Position[0];
549 Position[1] -= ALContext->Listener.Position[1];
550 Position[2] -= ALContext->Listener.Position[2];
552 SourceToListener[0] = -Position[0];
553 SourceToListener[1] = -Position[1];
554 SourceToListener[2] = -Position[2];
556 // Transform source position into listener space
557 aluMatrixVector(Position, Matrix);
559 else
561 SourceToListener[0] = -Position[0];
562 SourceToListener[1] = -Position[1];
563 SourceToListener[2] = -Position[2];
565 aluNormalize(SourceToListener);
566 aluNormalize(Direction);
568 //2. Calculate distance attenuation
569 Distance = aluSqrt(aluDotproduct(Position, Position));
571 flAttenuation = 1.0f;
572 for(i = 0;i < MAX_SENDS;i++)
574 RoomAttenuation[i] = 1.0f;
576 RoomRolloff[i] = ALSource->RoomRolloffFactor;
577 if(ALSource->Send[i].Slot &&
578 ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB)
579 RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
582 switch (ALSource->DistanceModel)
584 case AL_INVERSE_DISTANCE_CLAMPED:
585 Distance=__max(Distance,MinDist);
586 Distance=__min(Distance,MaxDist);
587 if (MaxDist < MinDist)
588 break;
589 //fall-through
590 case AL_INVERSE_DISTANCE:
591 if (MinDist > 0.0f)
593 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
594 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
595 for(i = 0;i < NumSends;i++)
597 if ((MinDist + (RoomRolloff[i] * (Distance - MinDist))) > 0.0f)
598 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (Distance - MinDist)));
601 break;
603 case AL_LINEAR_DISTANCE_CLAMPED:
604 Distance=__max(Distance,MinDist);
605 Distance=__min(Distance,MaxDist);
606 if (MaxDist < MinDist)
607 break;
608 //fall-through
609 case AL_LINEAR_DISTANCE:
610 Distance=__min(Distance,MaxDist);
611 if (MaxDist != MinDist)
613 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
614 for(i = 0;i < NumSends;i++)
615 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(Distance-MinDist)/(MaxDist - MinDist));
617 break;
619 case AL_EXPONENT_DISTANCE_CLAMPED:
620 Distance=__max(Distance,MinDist);
621 Distance=__min(Distance,MaxDist);
622 if (MaxDist < MinDist)
623 break;
624 //fall-through
625 case AL_EXPONENT_DISTANCE:
626 if ((Distance > 0.0f) && (MinDist > 0.0f))
628 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
629 for(i = 0;i < NumSends;i++)
630 RoomAttenuation[i] = (ALfloat)pow(Distance/MinDist, -RoomRolloff[i]);
632 break;
634 case AL_NONE:
635 break;
638 // Source Gain + Attenuation and clamp to Min/Max Gain
639 DryMix = SourceVolume * flAttenuation;
640 DryMix = __min(DryMix,MaxVolume);
641 DryMix = __max(DryMix,MinVolume);
643 for(i = 0;i < NumSends;i++)
645 ALfloat WetMix = SourceVolume * RoomAttenuation[i];
646 WetMix = __min(WetMix,MaxVolume);
647 wetsend[i] = __max(WetMix,MinVolume);
648 wetgainhf[i] = 1.0f;
651 // Distance-based air absorption
652 if(ALSource->AirAbsorptionFactor > 0.0f && ALSource->DistanceModel != AL_NONE)
654 ALfloat dist = Distance-MinDist;
655 ALfloat absorb;
657 if(dist < 0.0f) dist = 0.0f;
658 // Absorption calculation is done in dB
659 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
660 (dist*MetersPerUnit);
661 // Convert dB to linear gain before applying
662 absorb = pow(10.0, absorb/20.0);
663 DryGainHF *= absorb;
664 for(i = 0;i < MAX_SENDS;i++)
665 wetgainhf[i] *= absorb;
668 //3. Apply directional soundcones
669 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
670 if(Angle >= InnerAngle && Angle <= OuterAngle)
672 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
673 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
674 ConeHF = (1.0f+(OuterGainHF-1.0f)*scale);
675 DryMix *= ConeVolume;
676 if(ALSource->DryGainHFAuto)
677 DryGainHF *= ConeHF;
679 else if(Angle > OuterAngle)
681 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
682 ConeHF = (1.0f+(OuterGainHF-1.0f));
683 DryMix *= ConeVolume;
684 if(ALSource->DryGainHFAuto)
685 DryGainHF *= ConeHF;
687 else
689 ConeVolume = 1.0f;
690 ConeHF = 1.0f;
693 //4. Calculate Velocity
694 if(DopplerFactor != 0.0f)
696 ALfloat flVSS, flVLS = 0.0f;
698 if(ALSource->bHeadRelative==AL_FALSE)
699 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
700 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
702 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
704 if (flVSS >= flMaxVelocity)
705 flVSS = (flMaxVelocity - 1.0f);
706 else if (flVSS <= -flMaxVelocity)
707 flVSS = -flMaxVelocity + 1.0f;
709 if (flVLS >= flMaxVelocity)
710 flVLS = (flMaxVelocity - 1.0f);
711 else if (flVLS <= -flMaxVelocity)
712 flVLS = -flMaxVelocity + 1.0f;
714 pitch[0] = ALSource->flPitch *
715 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
716 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
718 else
719 pitch[0] = ALSource->flPitch;
721 for(i = 0;i < NumSends;i++)
723 if(ALSource->Send[i].Slot &&
724 ALSource->Send[i].Slot->effect.type != AL_EFFECT_NULL)
726 if(ALSource->WetGainAuto)
727 wetsend[i] *= ConeVolume;
728 if(ALSource->WetGainHFAuto)
729 wetgainhf[i] *= ConeHF;
731 if(ALSource->Send[i].Slot->AuxSendAuto)
733 // Apply minimal attenuation in place of missing
734 // statistical reverb model.
735 wetsend[i] *= pow(DryMix, 1.0f / 2.0f);
737 else
739 // If the slot's auxilliary send auto is off, the data sent to the
740 // effect slot is the same as the dry path, sans filter effects
741 wetsend[i] = DryMix;
742 wetgainhf[i] = DryGainHF;
745 switch(ALSource->Send[i].WetFilter.type)
747 case AL_FILTER_LOWPASS:
748 wetsend[i] *= ALSource->Send[i].WetFilter.Gain;
749 wetgainhf[i] *= ALSource->Send[i].WetFilter.GainHF;
750 break;
752 wetsend[i] *= ListenerGain;
754 else
756 wetsend[i] = 0.0f;
757 wetgainhf[i] = 1.0f;
760 for(i = NumSends;i < MAX_SENDS;i++)
762 wetsend[i] = 0.0f;
763 wetgainhf[i] = 1.0f;
766 //5. Apply filter gains and filters
767 switch(ALSource->DirectFilter.type)
769 case AL_FILTER_LOWPASS:
770 DryMix *= ALSource->DirectFilter.Gain;
771 DryGainHF *= ALSource->DirectFilter.GainHF;
772 break;
774 DryMix *= ListenerGain;
776 // Use energy-preserving panning algorithm for multi-speaker playback
777 length = aluSqrt(Position[0]*Position[0] + Position[1]*Position[1] +
778 Position[2]*Position[2]);
779 length = __max(length, MinDist);
780 if(length > 0.0f)
782 ALfloat invlen = 1.0f/length;
783 Position[0] *= invlen;
784 Position[1] *= invlen;
785 Position[2] *= invlen;
788 pos = aluCart2LUTpos(-Position[2], Position[0]);
789 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
791 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
792 // elevation adjustment for directional gain. this sucks, but
793 // has low complexity
794 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
795 for(s = 0; s < OUTPUTCHANNELS; s++)
797 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
798 drysend[s] = DryMix * gain;
800 *drygainhf = DryGainHF;
802 else
804 //1. Multi-channel buffers always play "normal"
805 pitch[0] = ALSource->flPitch;
807 DryMix = SourceVolume;
808 DryMix = __min(DryMix,MaxVolume);
809 DryMix = __max(DryMix,MinVolume);
811 switch(ALSource->DirectFilter.type)
813 case AL_FILTER_LOWPASS:
814 DryMix *= ALSource->DirectFilter.Gain;
815 DryGainHF *= ALSource->DirectFilter.GainHF;
816 break;
819 drysend[FRONT_LEFT] = DryMix * ListenerGain;
820 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
821 drysend[SIDE_LEFT] = DryMix * ListenerGain;
822 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
823 drysend[BACK_LEFT] = DryMix * ListenerGain;
824 drysend[BACK_RIGHT] = DryMix * ListenerGain;
825 drysend[FRONT_CENTER] = DryMix * ListenerGain;
826 drysend[BACK_CENTER] = DryMix * ListenerGain;
827 drysend[LFE] = DryMix * ListenerGain;
828 *drygainhf = DryGainHF;
830 for(i = 0;i < MAX_SENDS;i++)
832 wetsend[i] = 0.0f;
833 wetgainhf[i] = 1.0f;
838 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
840 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
843 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
845 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
846 static float DummyBuffer[BUFFERSIZE];
847 ALfloat *WetBuffer[MAX_SENDS];
848 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
849 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
850 ALfloat WetSend[MAX_SENDS];
851 ALfloat DryGainHF = 0.0f;
852 ALfloat WetGainHF[MAX_SENDS];
853 ALuint rampLength;
854 ALfloat dryGainStep[OUTPUTCHANNELS];
855 ALfloat wetGainStep[MAX_SENDS];
856 ALuint BlockAlign,BufferSize;
857 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
858 ALuint Channels,Frequency,ulExtraSamples;
859 ALfloat Pitch;
860 ALint Looping,State;
861 ALint increment;
862 ALuint Buffer;
863 ALuint SamplesToDo;
864 ALsource *ALSource;
865 ALbuffer *ALBuffer;
866 ALeffectslot *ALEffectSlot;
867 ALfloat values[OUTPUTCHANNELS];
868 ALfloat value;
869 ALshort *Data;
870 ALuint i,j,k,out;
871 ALfloat cw, a, g;
872 ALbufferlistitem *BufferListItem;
873 ALuint loop;
874 ALint64 DataSize64,DataPos64;
875 FILTER *DryFilter, *WetFilter[MAX_SENDS];
876 int fpuState;
878 SuspendContext(ALContext);
880 #if defined(HAVE_FESETROUND)
881 fpuState = fegetround();
882 fesetround(FE_TOWARDZERO);
883 #elif defined(HAVE__CONTROLFP)
884 fpuState = _controlfp(0, 0);
885 _controlfp(_RC_CHOP, _MCW_RC);
886 #else
887 (void)fpuState;
888 #endif
890 //Figure output format variables
891 BlockAlign = aluChannelsFromFormat(format);
892 BlockAlign *= aluBytesFromFormat(format);
894 size /= BlockAlign;
895 while(size > 0)
897 //Setup variables
898 SamplesToDo = min(size, BUFFERSIZE);
899 if(ALContext)
901 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
902 ALSource = ALContext->Source;
903 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
905 else
907 ALEffectSlot = NULL;
908 ALSource = NULL;
909 rampLength = 0;
911 rampLength = max(rampLength, SamplesToDo);
913 //Clear mixing buffer
914 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
916 //Actual mixing loop
917 while(ALSource)
919 j = 0;
920 State = ALSource->state;
922 while(State == AL_PLAYING && j < SamplesToDo)
924 DataSize = 0;
925 DataPosInt = 0;
926 DataPosFrac = 0;
928 //Get buffer info
929 if((Buffer = ALSource->ulBufferID))
931 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
933 Data = ALBuffer->data;
934 Channels = aluChannelsFromFormat(ALBuffer->format);
935 DataSize = ALBuffer->size;
936 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
937 Frequency = ALBuffer->frequency;
938 DataPosInt = ALSource->position;
939 DataPosFrac = ALSource->position_fraction;
941 if(DataPosInt >= DataSize)
942 goto skipmix;
944 //Get source info
945 DryFilter = &ALSource->iirFilter;
946 for(i = 0;i < MAX_SENDS;i++)
948 WetFilter[i] = &ALSource->Send[i].iirFilter;
949 WetBuffer[i] = (ALSource->Send[i].Slot ?
950 ALSource->Send[i].Slot->WetBuffer :
951 DummyBuffer);
954 CalcSourceParams(ALContext, ALSource,
955 (Channels==1) ? AL_TRUE : AL_FALSE,
956 DrySend, WetSend, &Pitch,
957 &DryGainHF, WetGainHF);
958 Pitch = (Pitch*Frequency) / ALContext->Frequency;
960 if(Channels == 1)
962 // Update filter coefficients. Calculations based on
963 // the I3DL2 spec.
964 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
965 // We use four chained one-pole filters, so we need to
966 // take the fourth root of the squared gain, which is
967 // the same as the square root of the base gain.
968 // Be careful with gains < 0.0001, as that causes the
969 // coefficient to head towards 1, which will flatten
970 // the signal
971 g = aluSqrt(__max(DryGainHF, 0.0001f));
972 a = 0.0f;
973 if(g < 0.9999f) // 1-epsilon
974 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
975 DryFilter->coeff = a;
977 for(i = 0;i < MAX_SENDS;i++)
979 // The wet path uses two chained one-pole filters,
980 // so take the base gain (square root of the
981 // squared gain)
982 g = __max(WetGainHF[i], 0.01f);
983 a = 0.0f;
984 if(g < 0.9999f) // 1-epsilon
985 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
986 WetFilter[i]->coeff = a;
989 else
991 // Multi-channel sources use two chained one-pole
992 // filters
993 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
994 g = __max(DryGainHF, 0.01f);
995 a = 0.0f;
996 if(g < 0.9999f) // 1-epsilon
997 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
998 DryFilter->coeff = a;
999 for(i = 0;i < MAX_SENDS;i++)
1000 WetFilter[i]->coeff = 0.0f;
1002 if(DuplicateStereo && Channels == 2)
1004 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
1005 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
1006 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
1007 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
1009 else if(DuplicateStereo)
1011 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
1012 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
1013 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
1014 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
1018 //Compute the gain steps for each output channel
1019 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
1021 for(i = 0;i < OUTPUTCHANNELS;i++)
1022 dryGainStep[i] = 0.0f;
1023 for(i = 0;i < MAX_SENDS;i++)
1024 wetGainStep[i] = 0.0f;
1026 else
1028 for(i = 0;i < OUTPUTCHANNELS;i++)
1030 dryGainStep[i] = (DrySend[i]-ALSource->DryGains[i]) / rampLength;
1031 DrySend[i] = ALSource->DryGains[i];
1033 for(i = 0;i < MAX_SENDS;i++)
1035 wetGainStep[i] = (WetSend[i]-ALSource->WetGains[i]) / rampLength;
1036 WetSend[i] = ALSource->WetGains[i];
1039 ALSource->FirstStart = AL_FALSE;
1041 //Compute 18.14 fixed point step
1042 if(Pitch > (float)MAX_PITCH)
1043 Pitch = (float)MAX_PITCH;
1044 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
1045 if(increment <= 0)
1046 increment = (1<<FRACTIONBITS);
1048 //Figure out how many samples we can mix.
1049 DataSize64 = DataSize;
1050 DataSize64 <<= FRACTIONBITS;
1051 DataPos64 = DataPosInt;
1052 DataPos64 <<= FRACTIONBITS;
1053 DataPos64 += DataPosFrac;
1054 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1056 BufferListItem = ALSource->queue;
1057 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
1059 if(BufferListItem)
1060 BufferListItem = BufferListItem->next;
1062 if (BufferListItem)
1064 if (BufferListItem->next)
1066 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1067 if(NextBuf && NextBuf->data)
1069 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1070 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1073 else if (ALSource->bLooping)
1075 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
1076 if (NextBuf && NextBuf->data)
1078 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1079 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1082 else
1083 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1085 BufferSize = min(BufferSize, (SamplesToDo-j));
1087 //Actual sample mixing loop
1088 k = 0;
1089 Data += DataPosInt*Channels;
1091 if(Channels == 1) /* Mono */
1093 ALfloat outsamp;
1095 while(BufferSize--)
1097 for(i = 0;i < OUTPUTCHANNELS;i++)
1098 DrySend[i] += dryGainStep[i];
1099 for(i = 0;i < MAX_SENDS;i++)
1100 WetSend[i] += wetGainStep[i];
1102 //First order interpolator
1103 value = lerp(Data[k], Data[k+1], DataPosFrac);
1105 //Direct path final mix buffer and panning
1106 outsamp = lpFilter4P(DryFilter, 0, value);
1107 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1108 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1109 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1110 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1111 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1112 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1113 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1114 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1116 //Room path final mix buffer and panning
1117 for(i = 0;i < MAX_SENDS;i++)
1119 outsamp = lpFilter2P(WetFilter[i], 0, value);
1120 WetBuffer[i][j] += outsamp*WetSend[i];
1123 DataPosFrac += increment;
1124 k += DataPosFrac>>FRACTIONBITS;
1125 DataPosFrac &= FRACTIONMASK;
1126 j++;
1129 else if(Channels == 2) /* Stereo */
1131 const int chans[] = {
1132 FRONT_LEFT, FRONT_RIGHT
1135 #define DO_MIX() do { \
1136 for(i = 0;i < MAX_SENDS;i++) \
1137 WetSend[i] += wetGainStep[i]*BufferSize; \
1138 while(BufferSize--) \
1140 for(i = 0;i < OUTPUTCHANNELS;i++) \
1141 DrySend[i] += dryGainStep[i]; \
1143 for(i = 0;i < Channels;i++) \
1145 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1146 values[i] = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1148 for(out = 0;out < OUTPUTCHANNELS;out++) \
1150 ALfloat sum = 0.0f; \
1151 for(i = 0;i < Channels;i++) \
1152 sum += values[i]*Matrix[chans[i]][out]; \
1153 DryBuffer[j][out] += sum; \
1156 DataPosFrac += increment; \
1157 k += DataPosFrac>>FRACTIONBITS; \
1158 DataPosFrac &= FRACTIONMASK; \
1159 j++; \
1161 } while(0)
1163 DO_MIX();
1165 else if(Channels == 4) /* Quad */
1167 const int chans[] = {
1168 FRONT_LEFT, FRONT_RIGHT,
1169 BACK_LEFT, BACK_RIGHT
1172 DO_MIX();
1174 else if(Channels == 6) /* 5.1 */
1176 const int chans[] = {
1177 FRONT_LEFT, FRONT_RIGHT,
1178 FRONT_CENTER, LFE,
1179 BACK_LEFT, BACK_RIGHT
1182 DO_MIX();
1184 else if(Channels == 7) /* 6.1 */
1186 const int chans[] = {
1187 FRONT_LEFT, FRONT_RIGHT,
1188 FRONT_CENTER, LFE,
1189 BACK_CENTER,
1190 SIDE_LEFT, SIDE_RIGHT
1193 DO_MIX();
1195 else if(Channels == 8) /* 7.1 */
1197 const int chans[] = {
1198 FRONT_LEFT, FRONT_RIGHT,
1199 FRONT_CENTER, LFE,
1200 BACK_LEFT, BACK_RIGHT,
1201 SIDE_LEFT, SIDE_RIGHT
1204 DO_MIX();
1205 #undef DO_MIX
1207 else /* Unknown? */
1209 for(i = 0;i < OUTPUTCHANNELS;i++)
1210 DrySend[i] += dryGainStep[i]*BufferSize;
1211 for(i = 0;i < MAX_SENDS;i++)
1212 WetSend[i] += wetGainStep[i]*BufferSize;
1213 while(BufferSize--)
1215 DataPosFrac += increment;
1216 k += DataPosFrac>>FRACTIONBITS;
1217 DataPosFrac &= FRACTIONMASK;
1218 j++;
1221 DataPosInt += k;
1223 //Update source info
1224 ALSource->position = DataPosInt;
1225 ALSource->position_fraction = DataPosFrac;
1226 for(i = 0;i < OUTPUTCHANNELS;i++)
1227 ALSource->DryGains[i] = DrySend[i];
1228 for(i = 0;i < MAX_SENDS;i++)
1229 ALSource->WetGains[i] = WetSend[i];
1231 skipmix: ;
1234 //Handle looping sources
1235 if(!Buffer || DataPosInt >= DataSize)
1237 //queueing
1238 if(ALSource->queue)
1240 Looping = ALSource->bLooping;
1241 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1243 BufferListItem = ALSource->queue;
1244 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1246 if(BufferListItem)
1248 if(!Looping)
1249 BufferListItem->bufferstate = PROCESSED;
1250 BufferListItem = BufferListItem->next;
1253 if(BufferListItem)
1254 ALSource->ulBufferID = BufferListItem->buffer;
1255 ALSource->position = DataPosInt-DataSize;
1256 ALSource->position_fraction = DataPosFrac;
1257 ALSource->BuffersPlayed++;
1259 else
1261 if(!Looping)
1263 /* alSourceStop */
1264 ALSource->state = AL_STOPPED;
1265 ALSource->inuse = AL_FALSE;
1266 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1267 BufferListItem = ALSource->queue;
1268 while(BufferListItem != NULL)
1270 BufferListItem->bufferstate = PROCESSED;
1271 BufferListItem = BufferListItem->next;
1273 ALSource->position = DataSize;
1274 ALSource->position_fraction = 0;
1276 else
1278 /* alSourceRewind */
1279 /* alSourcePlay */
1280 ALSource->state = AL_PLAYING;
1281 ALSource->inuse = AL_TRUE;
1282 ALSource->play = AL_TRUE;
1283 ALSource->BuffersPlayed = 0;
1284 BufferListItem = ALSource->queue;
1285 while(BufferListItem != NULL)
1287 BufferListItem->bufferstate = PENDING;
1288 BufferListItem = BufferListItem->next;
1290 ALSource->ulBufferID = ALSource->queue->buffer;
1292 if(ALSource->BuffersInQueue == 1)
1293 ALSource->position = DataPosInt%DataSize;
1294 else
1295 ALSource->position = DataPosInt-DataSize;
1296 ALSource->position_fraction = DataPosFrac;
1302 //Get source state
1303 State = ALSource->state;
1306 ALSource = ALSource->next;
1309 // effect slot processing
1310 while(ALEffectSlot)
1312 if(ALEffectSlot->EffectState)
1313 ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1315 for(i = 0;i < SamplesToDo;i++)
1316 ALEffectSlot->WetBuffer[i] = 0.0f;
1317 ALEffectSlot = ALEffectSlot->next;
1320 //Post processing loop
1321 switch(format)
1323 case AL_FORMAT_MONO8:
1324 for(i = 0;i < SamplesToDo;i++)
1326 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1327 buffer = ((ALubyte*)buffer) + 1;
1329 break;
1330 case AL_FORMAT_STEREO8:
1331 if(ALContext && ALContext->bs2b)
1333 for(i = 0;i < SamplesToDo;i++)
1335 float samples[2];
1336 samples[0] = DryBuffer[i][FRONT_LEFT];
1337 samples[1] = DryBuffer[i][FRONT_RIGHT];
1338 bs2b_cross_feed(ALContext->bs2b, samples);
1339 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1340 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1341 buffer = ((ALubyte*)buffer) + 2;
1344 else
1346 for(i = 0;i < SamplesToDo;i++)
1348 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1349 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1350 buffer = ((ALubyte*)buffer) + 2;
1353 break;
1354 case AL_FORMAT_QUAD8:
1355 for(i = 0;i < SamplesToDo;i++)
1357 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1358 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1359 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1360 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1361 buffer = ((ALubyte*)buffer) + 4;
1363 break;
1364 case AL_FORMAT_51CHN8:
1365 for(i = 0;i < SamplesToDo;i++)
1367 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1368 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1369 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1370 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1371 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1372 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1373 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1374 #else
1375 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1376 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1377 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1378 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1379 #endif
1380 buffer = ((ALubyte*)buffer) + 6;
1382 break;
1383 case AL_FORMAT_61CHN8:
1384 for(i = 0;i < SamplesToDo;i++)
1386 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1387 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1388 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1389 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1390 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_CENTER])>>8)+128);
1391 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1392 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1393 buffer = ((ALubyte*)buffer) + 7;
1395 break;
1396 case AL_FORMAT_71CHN8:
1397 for(i = 0;i < SamplesToDo;i++)
1399 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1400 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1401 #ifdef _WIN32
1402 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1403 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1404 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1405 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1406 #else
1407 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1408 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1409 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1410 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1411 #endif
1412 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1413 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1414 buffer = ((ALubyte*)buffer) + 8;
1416 break;
1418 case AL_FORMAT_MONO16:
1419 for(i = 0;i < SamplesToDo;i++)
1421 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1422 buffer = ((ALshort*)buffer) + 1;
1424 break;
1425 case AL_FORMAT_STEREO16:
1426 if(ALContext && ALContext->bs2b)
1428 for(i = 0;i < SamplesToDo;i++)
1430 float samples[2];
1431 samples[0] = DryBuffer[i][FRONT_LEFT];
1432 samples[1] = DryBuffer[i][FRONT_RIGHT];
1433 bs2b_cross_feed(ALContext->bs2b, samples);
1434 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1435 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1436 buffer = ((ALshort*)buffer) + 2;
1439 else
1441 for(i = 0;i < SamplesToDo;i++)
1443 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1444 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1445 buffer = ((ALshort*)buffer) + 2;
1448 break;
1449 case AL_FORMAT_QUAD16:
1450 for(i = 0;i < SamplesToDo;i++)
1452 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1453 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1454 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1455 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1456 buffer = ((ALshort*)buffer) + 4;
1458 break;
1459 case AL_FORMAT_51CHN16:
1460 for(i = 0;i < SamplesToDo;i++)
1462 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1463 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1464 #ifdef _WIN32
1465 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1466 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1467 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1468 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1469 #else
1470 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1471 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1472 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1473 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1474 #endif
1475 buffer = ((ALshort*)buffer) + 6;
1477 break;
1478 case AL_FORMAT_61CHN16:
1479 for(i = 0;i < SamplesToDo;i++)
1481 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1482 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1483 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1484 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1485 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_CENTER]);
1486 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1487 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1488 buffer = ((ALshort*)buffer) + 7;
1490 break;
1491 case AL_FORMAT_71CHN16:
1492 for(i = 0;i < SamplesToDo;i++)
1494 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1495 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1496 #ifdef _WIN32
1497 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1498 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1499 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1500 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1501 #else
1502 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1503 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1504 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1505 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1506 #endif
1507 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1508 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1509 buffer = ((ALshort*)buffer) + 8;
1511 break;
1513 default:
1514 break;
1517 size -= SamplesToDo;
1520 #if defined(HAVE_FESETROUND)
1521 fesetround(fpuState);
1522 #elif defined(HAVE__CONTROLFP)
1523 _controlfp(fpuState, 0xfffff);
1524 #endif
1526 ProcessContext(ALContext);