Don't update the source's gains in the sample mixing loop
[openal-soft.git] / Alc / ALu.c
blobf00fdafaafaff8634286274f12a55e89f11e3911
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"
39 #include "alReverb.h"
41 #if defined(HAVE_STDINT_H)
42 #include <stdint.h>
43 typedef int64_t ALint64;
44 #elif defined(HAVE___INT64)
45 typedef __int64 ALint64;
46 #elif (SIZEOF_LONG == 8)
47 typedef long ALint64;
48 #elif (SIZEOF_LONG_LONG == 8)
49 typedef long long ALint64;
50 #endif
52 #define FRACTIONBITS 14
53 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
54 #define MAX_PITCH 65536
56 /* Minimum ramp length in milliseconds. The value below was chosen to
57 * adequately reduce clicks and pops from harsh gain changes. */
58 #define MIN_RAMP_LENGTH 16
60 ALboolean DuplicateStereo = AL_FALSE;
62 /* NOTE: The AL_FORMAT_REAR* enums aren't handled here be cause they're
63 * converted to AL_FORMAT_QUAD* when loaded */
64 __inline ALuint aluBytesFromFormat(ALenum format)
66 switch(format)
68 case AL_FORMAT_MONO8:
69 case AL_FORMAT_STEREO8:
70 case AL_FORMAT_QUAD8_LOKI:
71 case AL_FORMAT_QUAD8:
72 case AL_FORMAT_51CHN8:
73 case AL_FORMAT_61CHN8:
74 case AL_FORMAT_71CHN8:
75 return 1;
77 case AL_FORMAT_MONO16:
78 case AL_FORMAT_STEREO16:
79 case AL_FORMAT_QUAD16_LOKI:
80 case AL_FORMAT_QUAD16:
81 case AL_FORMAT_51CHN16:
82 case AL_FORMAT_61CHN16:
83 case AL_FORMAT_71CHN16:
84 return 2;
86 case AL_FORMAT_MONO_FLOAT32:
87 case AL_FORMAT_STEREO_FLOAT32:
88 case AL_FORMAT_QUAD32:
89 case AL_FORMAT_51CHN32:
90 case AL_FORMAT_61CHN32:
91 case AL_FORMAT_71CHN32:
92 return 4;
94 default:
95 return 0;
99 __inline ALuint aluChannelsFromFormat(ALenum format)
101 switch(format)
103 case AL_FORMAT_MONO8:
104 case AL_FORMAT_MONO16:
105 case AL_FORMAT_MONO_FLOAT32:
106 return 1;
108 case AL_FORMAT_STEREO8:
109 case AL_FORMAT_STEREO16:
110 case AL_FORMAT_STEREO_FLOAT32:
111 return 2;
113 case AL_FORMAT_QUAD8_LOKI:
114 case AL_FORMAT_QUAD16_LOKI:
115 case AL_FORMAT_QUAD8:
116 case AL_FORMAT_QUAD16:
117 case AL_FORMAT_QUAD32:
118 return 4;
120 case AL_FORMAT_51CHN8:
121 case AL_FORMAT_51CHN16:
122 case AL_FORMAT_51CHN32:
123 return 6;
125 case AL_FORMAT_61CHN8:
126 case AL_FORMAT_61CHN16:
127 case AL_FORMAT_61CHN32:
128 return 7;
130 case AL_FORMAT_71CHN8:
131 case AL_FORMAT_71CHN16:
132 case AL_FORMAT_71CHN32:
133 return 8;
135 default:
136 return 0;
141 static __inline ALshort aluF2S(ALfloat Value)
143 ALint i;
145 i = (ALint)Value;
146 i = __min( 32767, i);
147 i = __max(-32768, i);
148 return ((ALshort)i);
151 static __inline ALvoid aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
153 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
154 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
155 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
158 static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
160 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
161 inVector1[2]*inVector2[2];
164 static __inline ALvoid aluNormalize(ALfloat *inVector)
166 ALfloat length, inverse_length;
168 length = aluSqrt(aluDotproduct(inVector, inVector));
169 if(length != 0.0f)
171 inverse_length = 1.0f/length;
172 inVector[0] *= inverse_length;
173 inVector[1] *= inverse_length;
174 inVector[2] *= inverse_length;
178 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
180 ALfloat result[3];
182 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
183 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
184 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
185 memcpy(vector, result, sizeof(result));
188 static ALvoid SetSpeakerArrangement(const char *name, ALfloat SpeakerAngle[OUTPUTCHANNELS],
189 ALint Speaker2Chan[OUTPUTCHANNELS], ALint chans)
191 const char *confkey;
192 const char *next;
193 const char *sep;
194 const char *end;
195 int i, val;
197 confkey = GetConfigValue(NULL, name, "");
198 next = confkey;
199 while(next && *next)
201 confkey = next;
202 next = strchr(confkey, ',');
203 if(next)
205 do {
206 next++;
207 } while(isspace(*next));
210 sep = strchr(confkey, '=');
211 if(!sep || confkey == sep)
212 continue;
214 end = sep - 1;
215 while(isspace(*end) && end != confkey)
216 end--;
218 if(strncmp(confkey, "fl", end-confkey) == 0)
219 val = FRONT_LEFT;
220 else if(strncmp(confkey, "fr", end-confkey) == 0)
221 val = FRONT_RIGHT;
222 else if(strncmp(confkey, "fc", end-confkey) == 0)
223 val = FRONT_CENTER;
224 else if(strncmp(confkey, "bl", end-confkey) == 0)
225 val = BACK_LEFT;
226 else if(strncmp(confkey, "br", end-confkey) == 0)
227 val = BACK_RIGHT;
228 else if(strncmp(confkey, "bc", end-confkey) == 0)
229 val = BACK_CENTER;
230 else if(strncmp(confkey, "sl", end-confkey) == 0)
231 val = SIDE_LEFT;
232 else if(strncmp(confkey, "sr", end-confkey) == 0)
233 val = SIDE_RIGHT;
234 else
236 AL_PRINT("Unknown speaker for %s: \"%c%c\"\n", name, confkey[0], confkey[1]);
237 continue;
240 sep++;
241 while(isspace(*sep))
242 sep++;
244 for(i = 0;i < chans;i++)
246 if(Speaker2Chan[i] == val)
248 val = strtol(sep, NULL, 10);
249 if(val >= -180 && val <= 180)
250 SpeakerAngle[i] = val * M_PI/180.0f;
251 else
252 AL_PRINT("Invalid angle for speaker \"%c%c\": %d\n", confkey[0], confkey[1], val);
253 break;
258 for(i = 1;i < chans;i++)
260 if(SpeakerAngle[i] <= SpeakerAngle[i-1])
262 AL_PRINT("Speaker %d of %d does not follow previous: %f > %f\n", i, chans,
263 SpeakerAngle[i-1] * 180.0f/M_PI, SpeakerAngle[i] * 180.0f/M_PI);
264 SpeakerAngle[i] = SpeakerAngle[i-1] + 1 * 180.0f/M_PI;
269 static __inline ALfloat aluLUTpos2Angle(ALint pos)
271 if(pos < QUADRANT_NUM)
272 return aluAtan((ALfloat)pos / (ALfloat)(QUADRANT_NUM - pos));
273 if(pos < 2 * QUADRANT_NUM)
274 return M_PI_2 + aluAtan((ALfloat)(pos - QUADRANT_NUM) / (ALfloat)(2 * QUADRANT_NUM - pos));
275 if(pos < 3 * QUADRANT_NUM)
276 return aluAtan((ALfloat)(pos - 2 * QUADRANT_NUM) / (ALfloat)(3 * QUADRANT_NUM - pos)) - M_PI;
277 return aluAtan((ALfloat)(pos - 3 * QUADRANT_NUM) / (ALfloat)(4 * QUADRANT_NUM - pos)) - M_PI_2;
280 ALvoid aluInitPanning(ALCcontext *Context)
282 ALint pos, offset, s;
283 ALfloat Alpha, Theta;
284 ALfloat SpeakerAngle[OUTPUTCHANNELS];
285 ALint Speaker2Chan[OUTPUTCHANNELS];
287 for(s = 0;s < OUTPUTCHANNELS;s++)
289 int s2;
290 for(s2 = 0;s2 < OUTPUTCHANNELS;s2++)
291 Context->ChannelMatrix[s][s2] = ((s==s2) ? 1.0f : 0.0f);
294 switch(Context->Device->Format)
296 /* Mono is rendered as stereo, then downmixed during post-process */
297 case AL_FORMAT_MONO8:
298 case AL_FORMAT_MONO16:
299 case AL_FORMAT_MONO_FLOAT32:
300 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
301 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
302 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
303 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
304 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
305 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
306 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
307 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
308 Context->NumChan = 2;
309 Speaker2Chan[0] = FRONT_LEFT;
310 Speaker2Chan[1] = FRONT_RIGHT;
311 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
312 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
313 break;
315 case AL_FORMAT_STEREO8:
316 case AL_FORMAT_STEREO16:
317 case AL_FORMAT_STEREO_FLOAT32:
318 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
319 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
320 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = 1.0f;
321 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = 1.0f;
322 Context->ChannelMatrix[BACK_LEFT][FRONT_LEFT] = 1.0f;
323 Context->ChannelMatrix[BACK_RIGHT][FRONT_RIGHT] = 1.0f;
324 Context->ChannelMatrix[BACK_CENTER][FRONT_LEFT] = aluSqrt(0.5);
325 Context->ChannelMatrix[BACK_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
326 Context->NumChan = 2;
327 Speaker2Chan[0] = FRONT_LEFT;
328 Speaker2Chan[1] = FRONT_RIGHT;
329 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
330 SpeakerAngle[1] = 90.0f * M_PI/180.0f;
331 SetSpeakerArrangement("layout_STEREO", SpeakerAngle, Speaker2Chan, Context->NumChan);
332 break;
334 case AL_FORMAT_QUAD8:
335 case AL_FORMAT_QUAD16:
336 case AL_FORMAT_QUAD32:
337 Context->ChannelMatrix[FRONT_CENTER][FRONT_LEFT] = aluSqrt(0.5);
338 Context->ChannelMatrix[FRONT_CENTER][FRONT_RIGHT] = aluSqrt(0.5);
339 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
340 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
341 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
342 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
343 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
344 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
345 Context->NumChan = 4;
346 Speaker2Chan[0] = BACK_LEFT;
347 Speaker2Chan[1] = FRONT_LEFT;
348 Speaker2Chan[2] = FRONT_RIGHT;
349 Speaker2Chan[3] = BACK_RIGHT;
350 SpeakerAngle[0] = -135.0f * M_PI/180.0f;
351 SpeakerAngle[1] = -45.0f * M_PI/180.0f;
352 SpeakerAngle[2] = 45.0f * M_PI/180.0f;
353 SpeakerAngle[3] = 135.0f * M_PI/180.0f;
354 SetSpeakerArrangement("layout_QUAD", SpeakerAngle, Speaker2Chan, Context->NumChan);
355 break;
357 case AL_FORMAT_51CHN8:
358 case AL_FORMAT_51CHN16:
359 case AL_FORMAT_51CHN32:
360 Context->ChannelMatrix[SIDE_LEFT][FRONT_LEFT] = aluSqrt(0.5);
361 Context->ChannelMatrix[SIDE_LEFT][BACK_LEFT] = aluSqrt(0.5);
362 Context->ChannelMatrix[SIDE_RIGHT][FRONT_RIGHT] = aluSqrt(0.5);
363 Context->ChannelMatrix[SIDE_RIGHT][BACK_RIGHT] = aluSqrt(0.5);
364 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
365 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
366 Context->NumChan = 5;
367 Speaker2Chan[0] = BACK_LEFT;
368 Speaker2Chan[1] = FRONT_LEFT;
369 Speaker2Chan[2] = FRONT_CENTER;
370 Speaker2Chan[3] = FRONT_RIGHT;
371 Speaker2Chan[4] = BACK_RIGHT;
372 SpeakerAngle[0] = -110.0f * M_PI/180.0f;
373 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
374 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
375 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
376 SpeakerAngle[4] = 110.0f * M_PI/180.0f;
377 SetSpeakerArrangement("layout_51CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
378 break;
380 case AL_FORMAT_61CHN8:
381 case AL_FORMAT_61CHN16:
382 case AL_FORMAT_61CHN32:
383 Context->ChannelMatrix[BACK_LEFT][BACK_CENTER] = aluSqrt(0.5);
384 Context->ChannelMatrix[BACK_LEFT][SIDE_LEFT] = aluSqrt(0.5);
385 Context->ChannelMatrix[BACK_RIGHT][BACK_CENTER] = aluSqrt(0.5);
386 Context->ChannelMatrix[BACK_RIGHT][SIDE_RIGHT] = aluSqrt(0.5);
387 Context->NumChan = 6;
388 Speaker2Chan[0] = SIDE_LEFT;
389 Speaker2Chan[1] = FRONT_LEFT;
390 Speaker2Chan[2] = FRONT_CENTER;
391 Speaker2Chan[3] = FRONT_RIGHT;
392 Speaker2Chan[4] = SIDE_RIGHT;
393 Speaker2Chan[5] = BACK_CENTER;
394 SpeakerAngle[0] = -90.0f * M_PI/180.0f;
395 SpeakerAngle[1] = -30.0f * M_PI/180.0f;
396 SpeakerAngle[2] = 0.0f * M_PI/180.0f;
397 SpeakerAngle[3] = 30.0f * M_PI/180.0f;
398 SpeakerAngle[4] = 90.0f * M_PI/180.0f;
399 SpeakerAngle[5] = 180.0f * M_PI/180.0f;
400 SetSpeakerArrangement("layout_61CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
401 break;
403 case AL_FORMAT_71CHN8:
404 case AL_FORMAT_71CHN16:
405 case AL_FORMAT_71CHN32:
406 Context->ChannelMatrix[BACK_CENTER][BACK_LEFT] = aluSqrt(0.5);
407 Context->ChannelMatrix[BACK_CENTER][BACK_RIGHT] = aluSqrt(0.5);
408 Context->NumChan = 7;
409 Speaker2Chan[0] = BACK_LEFT;
410 Speaker2Chan[1] = SIDE_LEFT;
411 Speaker2Chan[2] = FRONT_LEFT;
412 Speaker2Chan[3] = FRONT_CENTER;
413 Speaker2Chan[4] = FRONT_RIGHT;
414 Speaker2Chan[5] = SIDE_RIGHT;
415 Speaker2Chan[6] = BACK_RIGHT;
416 SpeakerAngle[0] = -150.0f * M_PI/180.0f;
417 SpeakerAngle[1] = -90.0f * M_PI/180.0f;
418 SpeakerAngle[2] = -30.0f * M_PI/180.0f;
419 SpeakerAngle[3] = 0.0f * M_PI/180.0f;
420 SpeakerAngle[4] = 30.0f * M_PI/180.0f;
421 SpeakerAngle[5] = 90.0f * M_PI/180.0f;
422 SpeakerAngle[6] = 150.0f * M_PI/180.0f;
423 SetSpeakerArrangement("layout_71CHN", SpeakerAngle, Speaker2Chan, Context->NumChan);
424 break;
426 default:
427 assert(0);
430 for(pos = 0; pos < LUT_NUM; pos++)
432 /* source angle */
433 Theta = aluLUTpos2Angle(pos);
435 /* clear all values */
436 offset = OUTPUTCHANNELS * pos;
437 for(s = 0; s < OUTPUTCHANNELS; s++)
438 Context->PanningLUT[offset+s] = 0.0f;
440 /* set panning values */
441 for(s = 0; s < Context->NumChan - 1; s++)
443 if(Theta >= SpeakerAngle[s] && Theta < SpeakerAngle[s+1])
445 /* source between speaker s and speaker s+1 */
446 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
447 (SpeakerAngle[s+1]-SpeakerAngle[s]);
448 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
449 Context->PanningLUT[offset + Speaker2Chan[s+1]] = sin(Alpha);
450 break;
453 if(s == Context->NumChan - 1)
455 /* source between last and first speaker */
456 if(Theta < SpeakerAngle[0])
457 Theta += 2.0f * M_PI;
458 Alpha = M_PI_2 * (Theta-SpeakerAngle[s]) /
459 (2.0f * M_PI + SpeakerAngle[0]-SpeakerAngle[s]);
460 Context->PanningLUT[offset + Speaker2Chan[s]] = cos(Alpha);
461 Context->PanningLUT[offset + Speaker2Chan[0]] = sin(Alpha);
466 static __inline ALint aluCart2LUTpos(ALfloat re, ALfloat im)
468 ALint pos = 0;
469 ALfloat denom = aluFabs(re) + aluFabs(im);
470 if(denom > 0.0f)
471 pos = (ALint)(QUADRANT_NUM*aluFabs(im) / denom + 0.5);
473 if(re < 0.0)
474 pos = 2 * QUADRANT_NUM - pos;
475 if(im < 0.0)
476 pos = LUT_NUM - pos;
477 return pos%LUT_NUM;
480 static ALvoid CalcSourceParams(const ALCcontext *ALContext,
481 const ALsource *ALSource, ALenum isMono,
482 ALfloat *drysend, ALfloat *wetsend,
483 ALfloat *pitch, ALfloat *drygainhf,
484 ALfloat *wetgainhf)
486 ALfloat InnerAngle,OuterAngle,Angle,Distance,DryMix;
487 ALfloat Direction[3],Position[3],SourceToListener[3];
488 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
489 ALfloat ConeVolume,ConeHF,SourceVolume,ListenerGain;
490 ALfloat U[3],V[3],N[3];
491 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
492 ALfloat Matrix[3][3];
493 ALfloat flAttenuation;
494 ALfloat RoomAttenuation[MAX_SENDS];
495 ALfloat MetersPerUnit;
496 ALfloat RoomRolloff[MAX_SENDS];
497 ALfloat DryGainHF = 1.0f;
498 ALfloat DirGain, AmbientGain;
499 ALfloat length;
500 const ALfloat *SpeakerGain;
501 ALint NumSends;
502 ALint pos, s, i;
504 //Get context properties
505 DopplerFactor = ALContext->DopplerFactor * ALSource->DopplerFactor;
506 DopplerVelocity = ALContext->DopplerVelocity;
507 flSpeedOfSound = ALContext->flSpeedOfSound;
508 NumSends = ALContext->NumSends;
510 //Get listener properties
511 ListenerGain = ALContext->Listener.Gain;
512 MetersPerUnit = ALContext->Listener.MetersPerUnit;
514 //Get source properties
515 SourceVolume = ALSource->flGain;
516 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
517 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
518 MinVolume = ALSource->flMinGain;
519 MaxVolume = ALSource->flMaxGain;
520 MinDist = ALSource->flRefDistance;
521 MaxDist = ALSource->flMaxDistance;
522 Rolloff = ALSource->flRollOffFactor;
523 InnerAngle = ALSource->flInnerAngle;
524 OuterAngle = ALSource->flOuterAngle;
525 OuterGainHF = ALSource->OuterGainHF;
527 //Only apply 3D calculations for mono buffers
528 if(isMono != AL_FALSE)
530 //1. Translate Listener to origin (convert to head relative)
531 // Note that Direction and SourceToListener are *not* transformed.
532 // SourceToListener is used with the source and listener velocities,
533 // which are untransformed, and Direction is used with SourceToListener
534 // for the sound cone
535 if(ALSource->bHeadRelative==AL_FALSE)
537 // Build transform matrix
538 aluCrossproduct(ALContext->Listener.Forward, ALContext->Listener.Up, U); // Right-vector
539 aluNormalize(U); // Normalized Right-vector
540 memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
541 aluNormalize(V); // Normalized Up-vector
542 memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
543 aluNormalize(N); // Normalized At-vector
544 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
545 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
546 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
548 // Translate source position into listener space
549 Position[0] -= ALContext->Listener.Position[0];
550 Position[1] -= ALContext->Listener.Position[1];
551 Position[2] -= ALContext->Listener.Position[2];
553 SourceToListener[0] = -Position[0];
554 SourceToListener[1] = -Position[1];
555 SourceToListener[2] = -Position[2];
557 // Transform source position into listener space
558 aluMatrixVector(Position, Matrix);
560 else
562 SourceToListener[0] = -Position[0];
563 SourceToListener[1] = -Position[1];
564 SourceToListener[2] = -Position[2];
566 aluNormalize(SourceToListener);
567 aluNormalize(Direction);
569 //2. Calculate distance attenuation
570 Distance = aluSqrt(aluDotproduct(Position, Position));
572 flAttenuation = 1.0f;
573 for(i = 0;i < MAX_SENDS;i++)
575 RoomAttenuation[i] = 1.0f;
577 RoomRolloff[i] = ALSource->RoomRolloffFactor;
578 if(ALSource->Send[i].Slot &&
579 ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB)
580 RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
583 switch (ALSource->DistanceModel)
585 case AL_INVERSE_DISTANCE_CLAMPED:
586 Distance=__max(Distance,MinDist);
587 Distance=__min(Distance,MaxDist);
588 if (MaxDist < MinDist)
589 break;
590 //fall-through
591 case AL_INVERSE_DISTANCE:
592 if (MinDist > 0.0f)
594 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
595 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
596 for(i = 0;i < NumSends;i++)
598 if ((MinDist + (RoomRolloff[i] * (Distance - MinDist))) > 0.0f)
599 RoomAttenuation[i] = MinDist / (MinDist + (RoomRolloff[i] * (Distance - MinDist)));
602 break;
604 case AL_LINEAR_DISTANCE_CLAMPED:
605 Distance=__max(Distance,MinDist);
606 Distance=__min(Distance,MaxDist);
607 if (MaxDist < MinDist)
608 break;
609 //fall-through
610 case AL_LINEAR_DISTANCE:
611 Distance=__min(Distance,MaxDist);
612 if (MaxDist != MinDist)
614 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
615 for(i = 0;i < NumSends;i++)
616 RoomAttenuation[i] = 1.0f - (RoomRolloff[i]*(Distance-MinDist)/(MaxDist - MinDist));
618 break;
620 case AL_EXPONENT_DISTANCE_CLAMPED:
621 Distance=__max(Distance,MinDist);
622 Distance=__min(Distance,MaxDist);
623 if (MaxDist < MinDist)
624 break;
625 //fall-through
626 case AL_EXPONENT_DISTANCE:
627 if ((Distance > 0.0f) && (MinDist > 0.0f))
629 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
630 for(i = 0;i < NumSends;i++)
631 RoomAttenuation[i] = (ALfloat)pow(Distance/MinDist, -RoomRolloff[i]);
633 break;
635 case AL_NONE:
636 break;
639 // Source Gain + Attenuation and clamp to Min/Max Gain
640 DryMix = SourceVolume * flAttenuation;
641 DryMix = __min(DryMix,MaxVolume);
642 DryMix = __max(DryMix,MinVolume);
644 for(i = 0;i < NumSends;i++)
646 ALfloat WetMix = SourceVolume * RoomAttenuation[i];
647 WetMix = __min(WetMix,MaxVolume);
648 wetsend[i] = __max(WetMix,MinVolume);
649 wetgainhf[i] = 1.0f;
652 // Distance-based air absorption
653 if(ALSource->AirAbsorptionFactor > 0.0f && ALSource->DistanceModel != AL_NONE)
655 ALfloat dist = Distance-MinDist;
656 ALfloat absorb;
658 if(dist < 0.0f) dist = 0.0f;
659 // Absorption calculation is done in dB
660 absorb = (ALSource->AirAbsorptionFactor*AIRABSORBGAINDBHF) *
661 (dist*MetersPerUnit);
662 // Convert dB to linear gain before applying
663 absorb = pow(10.0, absorb/20.0);
664 DryGainHF *= absorb;
665 for(i = 0;i < MAX_SENDS;i++)
666 wetgainhf[i] *= absorb;
669 //3. Apply directional soundcones
670 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f/M_PI;
671 if(Angle >= InnerAngle && Angle <= OuterAngle)
673 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
674 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f)*scale);
675 ConeHF = (1.0f+(OuterGainHF-1.0f)*scale);
676 DryMix *= ConeVolume;
677 if(ALSource->DryGainHFAuto)
678 DryGainHF *= ConeHF;
680 else if(Angle > OuterAngle)
682 ConeVolume = (1.0f+(ALSource->flOuterGain-1.0f));
683 ConeHF = (1.0f+(OuterGainHF-1.0f));
684 DryMix *= ConeVolume;
685 if(ALSource->DryGainHFAuto)
686 DryGainHF *= ConeHF;
688 else
690 ConeVolume = 1.0f;
691 ConeHF = 1.0f;
694 //4. Calculate Velocity
695 if(DopplerFactor != 0.0f)
697 ALfloat flVSS, flVLS = 0.0f;
699 if(ALSource->bHeadRelative==AL_FALSE)
700 flVLS = aluDotproduct(ALContext->Listener.Velocity, SourceToListener);
701 flVSS = aluDotproduct(ALSource->vVelocity, SourceToListener);
703 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
705 if (flVSS >= flMaxVelocity)
706 flVSS = (flMaxVelocity - 1.0f);
707 else if (flVSS <= -flMaxVelocity)
708 flVSS = -flMaxVelocity + 1.0f;
710 if (flVLS >= flMaxVelocity)
711 flVLS = (flMaxVelocity - 1.0f);
712 else if (flVLS <= -flMaxVelocity)
713 flVLS = -flMaxVelocity + 1.0f;
715 pitch[0] = ALSource->flPitch *
716 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
717 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
719 else
720 pitch[0] = ALSource->flPitch;
722 for(i = 0;i < NumSends;i++)
724 if(ALSource->Send[i].Slot &&
725 ALSource->Send[i].Slot->effect.type != AL_EFFECT_NULL)
727 if(ALSource->WetGainAuto)
728 wetsend[i] *= ConeVolume;
729 if(ALSource->WetGainHFAuto)
730 wetgainhf[i] *= ConeHF;
732 if(ALSource->Send[i].Slot->AuxSendAuto)
734 // Apply minimal attenuation in place of missing
735 // statistical reverb model.
736 wetsend[i] *= pow(DryMix, 1.0f / 2.0f);
738 else
740 // If the slot's auxilliary send auto is off, the data sent to the
741 // effect slot is the same as the dry path, sans filter effects
742 wetsend[i] = DryMix;
743 wetgainhf[i] = DryGainHF;
746 // Note that this is really applied by the effect slot. However,
747 // it's easier (more optimal) to handle it here.
748 if(ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB)
749 wetgainhf[i] *= ALSource->Send[0].Slot->effect.Reverb.GainHF;
751 switch(ALSource->Send[i].WetFilter.type)
753 case AL_FILTER_LOWPASS:
754 wetsend[i] *= ALSource->Send[i].WetFilter.Gain;
755 wetgainhf[i] *= ALSource->Send[i].WetFilter.GainHF;
756 break;
758 wetsend[i] *= ListenerGain;
760 else
762 wetsend[i] = 0.0f;
763 wetgainhf[i] = 1.0f;
766 for(i = NumSends;i < MAX_SENDS;i++)
768 wetsend[i] = 0.0f;
769 wetgainhf[i] = 1.0f;
772 //5. Apply filter gains and filters
773 switch(ALSource->DirectFilter.type)
775 case AL_FILTER_LOWPASS:
776 DryMix *= ALSource->DirectFilter.Gain;
777 DryGainHF *= ALSource->DirectFilter.GainHF;
778 break;
780 DryMix *= ListenerGain;
782 // Use energy-preserving panning algorithm for multi-speaker playback
783 length = aluSqrt(Position[0]*Position[0] + Position[1]*Position[1] +
784 Position[2]*Position[2]);
785 length = __max(length, MinDist);
786 if(length > 0.0f)
788 ALfloat invlen = 1.0f/length;
789 Position[0] *= invlen;
790 Position[1] *= invlen;
791 Position[2] *= invlen;
794 pos = aluCart2LUTpos(-Position[2], Position[0]);
795 SpeakerGain = &ALContext->PanningLUT[OUTPUTCHANNELS * pos];
797 DirGain = aluSqrt(Position[0]*Position[0] + Position[2]*Position[2]);
798 // elevation adjustment for directional gain. this sucks, but
799 // has low complexity
800 AmbientGain = 1.0/aluSqrt(ALContext->NumChan) * (1.0-DirGain);
801 for(s = 0; s < OUTPUTCHANNELS; s++)
803 ALfloat gain = SpeakerGain[s]*DirGain + AmbientGain;
804 drysend[s] = DryMix * gain;
806 *drygainhf = DryGainHF;
808 else
810 //1. Multi-channel buffers always play "normal"
811 pitch[0] = ALSource->flPitch;
813 DryMix = SourceVolume;
814 DryMix = __min(DryMix,MaxVolume);
815 DryMix = __max(DryMix,MinVolume);
817 switch(ALSource->DirectFilter.type)
819 case AL_FILTER_LOWPASS:
820 DryMix *= ALSource->DirectFilter.Gain;
821 DryGainHF *= ALSource->DirectFilter.GainHF;
822 break;
825 drysend[FRONT_LEFT] = DryMix * ListenerGain;
826 drysend[FRONT_RIGHT] = DryMix * ListenerGain;
827 drysend[SIDE_LEFT] = DryMix * ListenerGain;
828 drysend[SIDE_RIGHT] = DryMix * ListenerGain;
829 drysend[BACK_LEFT] = DryMix * ListenerGain;
830 drysend[BACK_RIGHT] = DryMix * ListenerGain;
831 drysend[FRONT_CENTER] = DryMix * ListenerGain;
832 drysend[BACK_CENTER] = DryMix * ListenerGain;
833 drysend[LFE] = DryMix * ListenerGain;
834 *drygainhf = DryGainHF;
836 for(i = 0;i < MAX_SENDS;i++)
838 wetsend[i] = 0.0f;
839 wetgainhf[i] = 1.0f;
844 static __inline ALshort lerp(ALshort val1, ALshort val2, ALint frac)
846 return val1 + (((val2-val1)*frac)>>FRACTIONBITS);
849 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
851 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
852 static float DummyBuffer[BUFFERSIZE];
853 ALfloat *WetBuffer[MAX_SENDS];
854 ALfloat (*Matrix)[OUTPUTCHANNELS] = ALContext->ChannelMatrix;
855 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
856 ALfloat WetSend[MAX_SENDS];
857 ALfloat DryGainHF = 0.0f;
858 ALfloat WetGainHF[MAX_SENDS];
859 ALuint rampLength;
860 ALfloat dryGainStep[OUTPUTCHANNELS];
861 ALfloat wetGainStep[MAX_SENDS];
862 ALuint BlockAlign,BufferSize;
863 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
864 ALuint Channels,Frequency,ulExtraSamples;
865 ALfloat Pitch;
866 ALint Looping,State;
867 ALint increment;
868 ALuint Buffer;
869 ALuint SamplesToDo;
870 ALsource *ALSource;
871 ALbuffer *ALBuffer;
872 ALeffectslot *ALEffectSlot;
873 ALfloat values[OUTPUTCHANNELS];
874 ALfloat value;
875 ALshort *Data;
876 ALuint i,j,k,out;
877 ALfloat cw, a, g;
878 ALbufferlistitem *BufferListItem;
879 ALuint loop;
880 ALint64 DataSize64,DataPos64;
881 FILTER *DryFilter, *WetFilter[MAX_SENDS];
882 int fpuState;
884 SuspendContext(ALContext);
886 #if defined(HAVE_FESETROUND)
887 fpuState = fegetround();
888 fesetround(FE_TOWARDZERO);
889 #elif defined(HAVE__CONTROLFP)
890 fpuState = _controlfp(0, 0);
891 _controlfp(_RC_CHOP, _MCW_RC);
892 #else
893 (void)fpuState;
894 #endif
896 //Figure output format variables
897 BlockAlign = aluChannelsFromFormat(format);
898 BlockAlign *= aluBytesFromFormat(format);
900 size /= BlockAlign;
901 while(size > 0)
903 //Setup variables
904 SamplesToDo = min(size, BUFFERSIZE);
905 if(ALContext)
907 ALEffectSlot = ALContext->AuxiliaryEffectSlot;
908 ALSource = ALContext->Source;
909 rampLength = ALContext->Frequency * MIN_RAMP_LENGTH / 1000;
911 else
913 ALEffectSlot = NULL;
914 ALSource = NULL;
915 rampLength = 0;
917 rampLength = max(rampLength, SamplesToDo);
919 //Clear mixing buffer
920 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
922 //Actual mixing loop
923 while(ALSource)
925 j = 0;
926 State = ALSource->state;
928 while(State == AL_PLAYING && j < SamplesToDo)
930 DataSize = 0;
931 DataPosInt = 0;
932 DataPosFrac = 0;
934 //Get buffer info
935 if((Buffer = ALSource->ulBufferID))
937 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
939 Data = ALBuffer->data;
940 Channels = aluChannelsFromFormat(ALBuffer->format);
941 DataSize = ALBuffer->size;
942 DataSize /= Channels * aluBytesFromFormat(ALBuffer->format);
943 Frequency = ALBuffer->frequency;
944 DataPosInt = ALSource->position;
945 DataPosFrac = ALSource->position_fraction;
947 if(DataPosInt >= DataSize)
948 goto skipmix;
950 //Get source info
951 DryFilter = &ALSource->iirFilter;
952 for(i = 0;i < MAX_SENDS;i++)
954 WetFilter[i] = &ALSource->Send[i].iirFilter;
955 WetBuffer[i] = (ALSource->Send[i].Slot ?
956 ALSource->Send[i].Slot->WetBuffer :
957 DummyBuffer);
960 CalcSourceParams(ALContext, ALSource,
961 (Channels==1) ? AL_TRUE : AL_FALSE,
962 DrySend, WetSend, &Pitch,
963 &DryGainHF, WetGainHF);
964 Pitch = (Pitch*Frequency) / ALContext->Frequency;
966 if(Channels == 1)
968 // Update filter coefficients. Calculations based on
969 // the I3DL2 spec.
970 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
971 // We use four chained one-pole filters, so we need to
972 // take the fourth root of the squared gain, which is
973 // the same as the square root of the base gain.
974 // Be careful with gains < 0.0001, as that causes the
975 // coefficient to head towards 1, which will flatten
976 // the signal
977 g = aluSqrt(__max(DryGainHF, 0.0001f));
978 a = 0.0f;
979 if(g < 0.9999f) // 1-epsilon
980 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
981 DryFilter->coeff = a;
983 for(i = 0;i < MAX_SENDS;i++)
985 // The wet path uses two chained one-pole filters,
986 // so take the base gain (square root of the
987 // squared gain)
988 g = __max(WetGainHF[i], 0.01f);
989 a = 0.0f;
990 if(g < 0.9999f) // 1-epsilon
991 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
992 WetFilter[i]->coeff = a;
995 else
997 // Multi-channel sources use two chained one-pole
998 // filters
999 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / ALContext->Frequency);
1000 g = __max(DryGainHF, 0.01f);
1001 a = 0.0f;
1002 if(g < 0.9999f) // 1-epsilon
1003 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
1004 DryFilter->coeff = a;
1005 for(i = 0;i < MAX_SENDS;i++)
1006 WetFilter[i]->coeff = 0.0f;
1008 if(DuplicateStereo && Channels == 2)
1010 Matrix[FRONT_LEFT][SIDE_LEFT] = 1.0f;
1011 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 1.0f;
1012 Matrix[FRONT_LEFT][BACK_LEFT] = 1.0f;
1013 Matrix[FRONT_RIGHT][BACK_RIGHT] = 1.0f;
1015 else if(DuplicateStereo)
1017 Matrix[FRONT_LEFT][SIDE_LEFT] = 0.0f;
1018 Matrix[FRONT_RIGHT][SIDE_RIGHT] = 0.0f;
1019 Matrix[FRONT_LEFT][BACK_LEFT] = 0.0f;
1020 Matrix[FRONT_RIGHT][BACK_RIGHT] = 0.0f;
1024 //Compute the gain steps for each output channel
1025 if(ALSource->FirstStart && DataPosInt == 0 && DataPosFrac == 0)
1027 for(i = 0;i < OUTPUTCHANNELS;i++)
1028 dryGainStep[i] = 0;
1029 for(i = 0;i < MAX_SENDS;i++)
1030 wetGainStep[i] = 0;
1032 else
1034 for(i = 0;i < OUTPUTCHANNELS;i++)
1036 dryGainStep[i] = (DrySend[i]-ALSource->DryGains[i]) / rampLength;
1037 DrySend[i] = ALSource->DryGains[i];
1039 for(i = 0;i < MAX_SENDS;i++)
1041 wetGainStep[i] = (WetSend[i]-ALSource->WetGains[i]) / rampLength;
1042 WetSend[i] = ALSource->WetGains[i];
1045 ALSource->FirstStart = AL_FALSE;
1047 //Compute 18.14 fixed point step
1048 if(Pitch > (float)MAX_PITCH)
1049 Pitch = (float)MAX_PITCH;
1050 increment = (ALint)(Pitch*(ALfloat)(1L<<FRACTIONBITS));
1051 if(increment <= 0)
1052 increment = (1<<FRACTIONBITS);
1054 //Figure out how many samples we can mix.
1055 DataSize64 = DataSize;
1056 DataSize64 <<= FRACTIONBITS;
1057 DataPos64 = DataPosInt;
1058 DataPos64 <<= FRACTIONBITS;
1059 DataPos64 += DataPosFrac;
1060 BufferSize = (ALuint)((DataSize64-DataPos64+(increment-1)) / increment);
1062 BufferListItem = ALSource->queue;
1063 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
1065 if(BufferListItem)
1066 BufferListItem = BufferListItem->next;
1068 if (BufferListItem)
1070 if (BufferListItem->next)
1072 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer);
1073 if(NextBuf && NextBuf->data)
1075 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1076 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1079 else if (ALSource->bLooping)
1081 ALbuffer *NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer);
1082 if (NextBuf && NextBuf->data)
1084 ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*2));
1085 memcpy(&Data[DataSize*Channels], NextBuf->data, ulExtraSamples);
1088 else
1089 memset(&Data[DataSize*Channels], 0, (ALBuffer->padding*Channels*2));
1091 BufferSize = min(BufferSize, (SamplesToDo-j));
1093 //Actual sample mixing loop
1094 k = 0;
1095 Data += DataPosInt*Channels;
1097 if(Channels == 1) /* Mono */
1099 ALfloat outsamp;
1101 while(BufferSize--)
1103 for(i = 0;i < OUTPUTCHANNELS;i++)
1104 DrySend[i] += dryGainStep[i];
1105 for(i = 0;i < MAX_SENDS;i++)
1106 WetSend[i] += wetGainStep[i];
1108 //First order interpolator
1109 value = lerp(Data[k], Data[k+1], DataPosFrac);
1111 //Direct path final mix buffer and panning
1112 outsamp = lpFilter4P(DryFilter, 0, value);
1113 DryBuffer[j][FRONT_LEFT] += outsamp*DrySend[FRONT_LEFT];
1114 DryBuffer[j][FRONT_RIGHT] += outsamp*DrySend[FRONT_RIGHT];
1115 DryBuffer[j][SIDE_LEFT] += outsamp*DrySend[SIDE_LEFT];
1116 DryBuffer[j][SIDE_RIGHT] += outsamp*DrySend[SIDE_RIGHT];
1117 DryBuffer[j][BACK_LEFT] += outsamp*DrySend[BACK_LEFT];
1118 DryBuffer[j][BACK_RIGHT] += outsamp*DrySend[BACK_RIGHT];
1119 DryBuffer[j][FRONT_CENTER] += outsamp*DrySend[FRONT_CENTER];
1120 DryBuffer[j][BACK_CENTER] += outsamp*DrySend[BACK_CENTER];
1122 //Room path final mix buffer and panning
1123 for(i = 0;i < MAX_SENDS;i++)
1125 outsamp = lpFilter2P(WetFilter[i], 0, value);
1126 WetBuffer[i][j] += outsamp*WetSend[i];
1129 DataPosFrac += increment;
1130 k += DataPosFrac>>FRACTIONBITS;
1131 DataPosFrac &= FRACTIONMASK;
1132 j++;
1135 else if(Channels == 2) /* Stereo */
1137 const int chans[] = {
1138 FRONT_LEFT, FRONT_RIGHT
1141 #define DO_MIX() do { \
1142 for(i = 0;i < MAX_SENDS;i++) \
1143 WetSend[i] += wetGainStep[i]*BufferSize; \
1144 while(BufferSize--) \
1146 for(i = 0;i < OUTPUTCHANNELS;i++) \
1147 DrySend[i] += dryGainStep[i]; \
1149 for(i = 0;i < Channels;i++) \
1151 value = lerp(Data[k*Channels + i], Data[(k+1)*Channels + i], DataPosFrac); \
1152 values[i] = lpFilter2P(DryFilter, chans[i]*2, value)*DrySend[chans[i]]; \
1154 for(out = 0;out < OUTPUTCHANNELS;out++) \
1156 ALfloat sum = 0.0f; \
1157 for(i = 0;i < Channels;i++) \
1158 sum += values[i]*Matrix[chans[i]][out]; \
1159 DryBuffer[j][out] += sum; \
1162 DataPosFrac += increment; \
1163 k += DataPosFrac>>FRACTIONBITS; \
1164 DataPosFrac &= FRACTIONMASK; \
1165 j++; \
1167 } while(0)
1169 DO_MIX();
1171 else if(Channels == 4) /* Quad */
1173 const int chans[] = {
1174 FRONT_LEFT, FRONT_RIGHT,
1175 BACK_LEFT, BACK_RIGHT
1178 DO_MIX();
1180 else if(Channels == 6) /* 5.1 */
1182 const int chans[] = {
1183 FRONT_LEFT, FRONT_RIGHT,
1184 FRONT_CENTER, LFE,
1185 BACK_LEFT, BACK_RIGHT
1188 DO_MIX();
1190 else if(Channels == 7) /* 6.1 */
1192 const int chans[] = {
1193 FRONT_LEFT, FRONT_RIGHT,
1194 FRONT_CENTER, LFE,
1195 BACK_CENTER,
1196 SIDE_LEFT, SIDE_RIGHT
1199 DO_MIX();
1201 else if(Channels == 8) /* 7.1 */
1203 const int chans[] = {
1204 FRONT_LEFT, FRONT_RIGHT,
1205 FRONT_CENTER, LFE,
1206 BACK_LEFT, BACK_RIGHT,
1207 SIDE_LEFT, SIDE_RIGHT
1210 DO_MIX();
1211 #undef DO_MIX
1213 else /* Unknown? */
1215 for(i = 0;i < OUTPUTCHANNELS;i++)
1216 DrySend[i] += dryGainStep[i]*BufferSize;
1217 for(i = 0;i < MAX_SENDS;i++)
1218 WetSend[i] += wetGainStep[i]*BufferSize;
1219 while(BufferSize--)
1221 DataPosFrac += increment;
1222 k += DataPosFrac>>FRACTIONBITS;
1223 DataPosFrac &= FRACTIONMASK;
1224 j++;
1227 DataPosInt += k;
1229 //Update source info
1230 ALSource->position = DataPosInt;
1231 ALSource->position_fraction = DataPosFrac;
1232 for(i = 0;i < OUTPUTCHANNELS;i++)
1233 ALSource->DryGains[i] = DrySend[i];
1234 for(i = 0;i < MAX_SENDS;i++)
1235 ALSource->WetGains[i] = WetSend[i];
1237 skipmix: ;
1240 //Handle looping sources
1241 if(!Buffer || DataPosInt >= DataSize)
1243 //queueing
1244 if(ALSource->queue)
1246 Looping = ALSource->bLooping;
1247 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
1249 BufferListItem = ALSource->queue;
1250 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
1252 if(BufferListItem)
1254 if(!Looping)
1255 BufferListItem->bufferstate = PROCESSED;
1256 BufferListItem = BufferListItem->next;
1259 if(BufferListItem)
1260 ALSource->ulBufferID = BufferListItem->buffer;
1261 ALSource->position = DataPosInt-DataSize;
1262 ALSource->position_fraction = DataPosFrac;
1263 ALSource->BuffersPlayed++;
1265 else
1267 if(!Looping)
1269 /* alSourceStop */
1270 ALSource->state = AL_STOPPED;
1271 ALSource->inuse = AL_FALSE;
1272 ALSource->BuffersPlayed = ALSource->BuffersInQueue;
1273 BufferListItem = ALSource->queue;
1274 while(BufferListItem != NULL)
1276 BufferListItem->bufferstate = PROCESSED;
1277 BufferListItem = BufferListItem->next;
1279 ALSource->position = DataSize;
1280 ALSource->position_fraction = 0;
1282 else
1284 /* alSourceRewind */
1285 /* alSourcePlay */
1286 ALSource->state = AL_PLAYING;
1287 ALSource->inuse = AL_TRUE;
1288 ALSource->play = AL_TRUE;
1289 ALSource->BuffersPlayed = 0;
1290 BufferListItem = ALSource->queue;
1291 while(BufferListItem != NULL)
1293 BufferListItem->bufferstate = PENDING;
1294 BufferListItem = BufferListItem->next;
1296 ALSource->ulBufferID = ALSource->queue->buffer;
1298 if(ALSource->BuffersInQueue == 1)
1299 ALSource->position = DataPosInt%DataSize;
1300 else
1301 ALSource->position = DataPosInt-DataSize;
1302 ALSource->position_fraction = DataPosFrac;
1308 //Get source state
1309 State = ALSource->state;
1312 ALSource = ALSource->next;
1315 // effect slot processing
1316 while(ALEffectSlot)
1318 if(ALEffectSlot->effect.type == AL_EFFECT_REVERB)
1319 VerbProcess(ALEffectSlot->ReverbState, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1320 else if(ALEffectSlot->effect.type == AL_EFFECT_ECHO)
1321 EchoProcess(ALEffectSlot->EchoState, SamplesToDo, ALEffectSlot->WetBuffer, DryBuffer);
1323 for(i = 0;i < SamplesToDo;i++)
1324 ALEffectSlot->WetBuffer[i] = 0.0f;
1325 ALEffectSlot = ALEffectSlot->next;
1328 //Post processing loop
1329 switch(format)
1331 case AL_FORMAT_MONO8:
1332 for(i = 0;i < SamplesToDo;i++)
1334 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT])>>8)+128);
1335 buffer = ((ALubyte*)buffer) + 1;
1337 break;
1338 case AL_FORMAT_STEREO8:
1339 if(ALContext && ALContext->bs2b)
1341 for(i = 0;i < SamplesToDo;i++)
1343 float samples[2];
1344 samples[0] = DryBuffer[i][FRONT_LEFT];
1345 samples[1] = DryBuffer[i][FRONT_RIGHT];
1346 bs2b_cross_feed(ALContext->bs2b, samples);
1347 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
1348 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
1349 buffer = ((ALubyte*)buffer) + 2;
1352 else
1354 for(i = 0;i < SamplesToDo;i++)
1356 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1357 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1358 buffer = ((ALubyte*)buffer) + 2;
1361 break;
1362 case AL_FORMAT_QUAD8:
1363 for(i = 0;i < SamplesToDo;i++)
1365 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1366 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1367 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1368 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1369 buffer = ((ALubyte*)buffer) + 4;
1371 break;
1372 case AL_FORMAT_51CHN8:
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 #ifdef _WIN32 /* Of course, Windows can't use the same ordering... */
1378 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1379 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1380 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1381 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1382 #else
1383 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1384 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1385 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1386 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1387 #endif
1388 buffer = ((ALubyte*)buffer) + 6;
1390 break;
1391 case AL_FORMAT_61CHN8:
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 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1397 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1398 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_CENTER])>>8)+128);
1399 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1400 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1401 buffer = ((ALubyte*)buffer) + 7;
1403 break;
1404 case AL_FORMAT_71CHN8:
1405 for(i = 0;i < SamplesToDo;i++)
1407 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT])>>8)+128);
1408 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT])>>8)+128);
1409 #ifdef _WIN32
1410 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1411 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1412 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1413 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1414 #else
1415 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT])>>8)+128);
1416 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT])>>8)+128);
1417 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_CENTER])>>8)+128);
1418 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE])>>8)+128);
1419 #endif
1420 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT])>>8)+128);
1421 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT])>>8)+128);
1422 buffer = ((ALubyte*)buffer) + 8;
1424 break;
1426 case AL_FORMAT_MONO16:
1427 for(i = 0;i < SamplesToDo;i++)
1429 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]);
1430 buffer = ((ALshort*)buffer) + 1;
1432 break;
1433 case AL_FORMAT_STEREO16:
1434 if(ALContext && ALContext->bs2b)
1436 for(i = 0;i < SamplesToDo;i++)
1438 float samples[2];
1439 samples[0] = DryBuffer[i][FRONT_LEFT];
1440 samples[1] = DryBuffer[i][FRONT_RIGHT];
1441 bs2b_cross_feed(ALContext->bs2b, samples);
1442 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1443 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1444 buffer = ((ALshort*)buffer) + 2;
1447 else
1449 for(i = 0;i < SamplesToDo;i++)
1451 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1452 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1453 buffer = ((ALshort*)buffer) + 2;
1456 break;
1457 case AL_FORMAT_QUAD16:
1458 for(i = 0;i < SamplesToDo;i++)
1460 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1461 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1462 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1463 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1464 buffer = ((ALshort*)buffer) + 4;
1466 break;
1467 case AL_FORMAT_51CHN16:
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 #ifdef _WIN32
1473 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1474 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1475 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1476 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1477 #else
1478 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1479 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1480 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1481 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1482 #endif
1483 buffer = ((ALshort*)buffer) + 6;
1485 break;
1486 case AL_FORMAT_61CHN16:
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 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1492 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1493 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_CENTER]);
1494 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1495 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1496 buffer = ((ALshort*)buffer) + 7;
1498 break;
1499 case AL_FORMAT_71CHN16:
1500 for(i = 0;i < SamplesToDo;i++)
1502 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]);
1503 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]);
1504 #ifdef _WIN32
1505 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1506 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][LFE]);
1507 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT]);
1508 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1509 #else
1510 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT]);
1511 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT]);
1512 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][FRONT_CENTER]);
1513 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE]);
1514 #endif
1515 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][SIDE_LEFT]);
1516 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][SIDE_RIGHT]);
1517 buffer = ((ALshort*)buffer) + 8;
1519 break;
1521 default:
1522 break;
1525 size -= SamplesToDo;
1528 #if defined(HAVE_FESETROUND)
1529 fesetround(fpuState);
1530 #elif defined(HAVE__CONTROLFP)
1531 _controlfp(fpuState, 0xfffff);
1532 #endif
1534 ProcessContext(ALContext);