Use acosf when available
[openal-soft.git] / Alc / ALu.c
blob22f3ad120c9aeed153c3b947be74233615ff61a2
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
23 #include "config.h"
25 #include <math.h>
26 #include "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #include "alSource.h"
30 #include "alBuffer.h"
31 #include "alThunk.h"
32 #include "alListener.h"
33 #include "bs2b.h"
35 #if defined(HAVE_STDINT_H)
36 #include <stdint.h>
37 typedef int64_t ALint64;
38 #elif defined(HAVE___INT64)
39 typedef __int64 ALint64;
40 #elif (SIZEOF_LONG == 8)
41 typedef long ALint64;
42 #elif (SIZEOF_LONG_LONG == 8)
43 typedef long long ALint64;
44 #endif
46 #ifdef HAVE_SQRTF
47 #define aluSqrt(x) ((ALfloat)sqrtf((float)(x)))
48 #else
49 #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
50 #endif
52 #ifdef HAVE_ACOSF
53 #define aluAcos(x) ((ALfloat)acosf((float)(x)))
54 #else
55 #define aluAcos(x) ((ALfloat)acos((double)(x)))
56 #endif
58 // fixes for mingw32.
59 #if defined(max) && !defined(__max)
60 #define __max max
61 #endif
62 #if defined(min) && !defined(__min)
63 #define __min min
64 #endif
66 #define BUFFERSIZE 48000
67 #define FRACTIONBITS 14
68 #define FRACTIONMASK ((1L<<FRACTIONBITS)-1)
69 #define MAX_PITCH 4
71 enum {
72 FRONT_LEFT = 0,
73 FRONT_RIGHT,
74 SIDE_LEFT,
75 SIDE_RIGHT,
76 BACK_LEFT,
77 BACK_RIGHT,
78 CENTER,
79 LFE,
81 OUTPUTCHANNELS
84 /* NOTE: The AL_FORMAT_REAR* enums aren't handled here be cause they're
85 * converted to AL_FORMAT_QUAD* when loaded */
86 __inline ALuint aluBytesFromFormat(ALenum format)
88 switch(format)
90 case AL_FORMAT_MONO8:
91 case AL_FORMAT_STEREO8:
92 case AL_FORMAT_QUAD8_LOKI:
93 case AL_FORMAT_QUAD8:
94 case AL_FORMAT_51CHN8:
95 case AL_FORMAT_61CHN8:
96 case AL_FORMAT_71CHN8:
97 return 1;
99 case AL_FORMAT_MONO16:
100 case AL_FORMAT_STEREO16:
101 case AL_FORMAT_QUAD16_LOKI:
102 case AL_FORMAT_QUAD16:
103 case AL_FORMAT_51CHN16:
104 case AL_FORMAT_61CHN16:
105 case AL_FORMAT_71CHN16:
106 return 2;
108 case AL_FORMAT_MONO_FLOAT32:
109 case AL_FORMAT_STEREO_FLOAT32:
110 case AL_FORMAT_QUAD32:
111 case AL_FORMAT_51CHN32:
112 case AL_FORMAT_61CHN32:
113 case AL_FORMAT_71CHN32:
114 return 4;
116 default:
117 return 0;
121 __inline ALuint aluChannelsFromFormat(ALenum format)
123 switch(format)
125 case AL_FORMAT_MONO8:
126 case AL_FORMAT_MONO16:
127 case AL_FORMAT_MONO_FLOAT32:
128 return 1;
130 case AL_FORMAT_STEREO8:
131 case AL_FORMAT_STEREO16:
132 case AL_FORMAT_STEREO_FLOAT32:
133 return 2;
135 case AL_FORMAT_QUAD8_LOKI:
136 case AL_FORMAT_QUAD16_LOKI:
137 case AL_FORMAT_QUAD8:
138 case AL_FORMAT_QUAD16:
139 case AL_FORMAT_QUAD32:
140 return 4;
142 case AL_FORMAT_51CHN8:
143 case AL_FORMAT_51CHN16:
144 case AL_FORMAT_51CHN32:
145 return 6;
147 case AL_FORMAT_61CHN8:
148 case AL_FORMAT_61CHN16:
149 case AL_FORMAT_61CHN32:
150 return 7;
152 case AL_FORMAT_71CHN8:
153 case AL_FORMAT_71CHN16:
154 case AL_FORMAT_71CHN32:
155 return 8;
157 default:
158 return 0;
162 static __inline ALint aluF2L(ALfloat Value)
164 #if 0
165 if(sizeof(ALint) == 4 && sizeof(double) == 8)
167 double temp;
168 temp = Value + (((65536.0*65536.0*16.0)+(65536.0*65536.0*8.0))*65536.0);
169 return *((ALint*)&temp);
171 #endif
172 return (ALint)Value;
175 static __inline ALshort aluF2S(ALfloat Value)
177 ALint i;
179 i = aluF2L(Value);
180 i = __min( 32767, i);
181 i = __max(-32768, i);
182 return ((ALshort)i);
185 static __inline ALvoid aluCrossproduct(ALfloat *inVector1,ALfloat *inVector2,ALfloat *outVector)
187 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
188 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
189 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
192 static __inline ALfloat aluDotproduct(ALfloat *inVector1,ALfloat *inVector2)
194 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
195 inVector1[2]*inVector2[2];
198 static __inline ALvoid aluNormalize(ALfloat *inVector)
200 ALfloat length, inverse_length;
202 length = (ALfloat)aluSqrt(aluDotproduct(inVector, inVector));
203 if(length != 0)
205 inverse_length = 1.0f/length;
206 inVector[0] *= inverse_length;
207 inVector[1] *= inverse_length;
208 inVector[2] *= inverse_length;
212 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
214 ALfloat result[3];
216 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
217 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
218 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
219 memcpy(vector, result, sizeof(result));
222 static __inline ALfloat aluComputeDrySample(ALsource *source, ALfloat DryGainHF, ALfloat sample)
224 if(DryGainHF < 1.0f)
226 if(DryGainHF > 0.0f)
228 sample *= DryGainHF;
229 sample += source->LastDrySample * (1.0f-DryGainHF);
231 else
232 sample = 0.0f;
235 source->LastDrySample = sample;
236 return sample;
239 static __inline ALfloat aluComputeWetSample(ALsource *source, ALfloat WetGainHF, ALfloat sample)
241 if(WetGainHF < 1.0f)
243 if(WetGainHF > 0.0f)
245 sample *= WetGainHF;
246 sample += source->LastWetSample * (1.0f-WetGainHF);
248 else
249 sample = 0.0f;
252 source->LastWetSample = sample;
253 return sample;
256 static ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
257 ALenum isMono, ALenum OutputFormat,
258 ALfloat *drysend, ALfloat *wetsend,
259 ALfloat *pitch, ALfloat *drygainhf,
260 ALfloat *wetgainhf)
262 ALfloat ListenerOrientation[6],ListenerPosition[3],ListenerVelocity[3];
263 ALfloat InnerAngle,OuterAngle,OuterGain,Angle,Distance,DryMix,WetMix;
264 ALfloat Direction[3],Position[3],Velocity[3],SourceToListener[3];
265 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
266 ALfloat Pitch,ConeVolume,SourceVolume,PanningFB,PanningLR,ListenerGain;
267 ALfloat U[3],V[3],N[3];
268 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
269 ALfloat flVSS, flVLS;
270 ALint DistanceModel;
271 ALfloat Matrix[3][3];
272 ALint HeadRelative;
273 ALfloat flAttenuation;
274 ALfloat RoomAttenuation;
275 ALfloat MetersPerUnit;
276 ALfloat RoomRolloff;
277 ALfloat DryGainHF = 1.0f;
278 ALfloat WetGainHF = 1.0f;
280 //Get context properties
281 DopplerFactor = ALContext->DopplerFactor;
282 DistanceModel = ALContext->DistanceModel;
283 DopplerVelocity = ALContext->DopplerVelocity;
284 flSpeedOfSound = ALContext->flSpeedOfSound;
286 //Get listener properties
287 ListenerGain = ALContext->Listener.Gain;
288 MetersPerUnit = ALContext->Listener.MetersPerUnit;
289 memcpy(ListenerPosition, ALContext->Listener.Position, sizeof(ALContext->Listener.Position));
290 memcpy(ListenerVelocity, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
291 memcpy(&ListenerOrientation[0], ALContext->Listener.Forward, sizeof(ALContext->Listener.Forward));
292 memcpy(&ListenerOrientation[3], ALContext->Listener.Up, sizeof(ALContext->Listener.Up));
294 //Get source properties
295 Pitch = ALSource->flPitch;
296 SourceVolume = ALSource->flGain;
297 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
298 memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
299 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
300 MinVolume = ALSource->flMinGain;
301 MaxVolume = ALSource->flMaxGain;
302 MinDist = ALSource->flRefDistance;
303 MaxDist = ALSource->flMaxDistance;
304 Rolloff = ALSource->flRollOffFactor;
305 OuterGain = ALSource->flOuterGain;
306 InnerAngle = ALSource->flInnerAngle;
307 OuterAngle = ALSource->flOuterAngle;
308 HeadRelative = ALSource->bHeadRelative;
309 OuterGainHF = ALSource->OuterGainHF;
310 RoomRolloff = ALSource->RoomRolloffFactor;
312 //Only apply 3D calculations for mono buffers
313 if(isMono != AL_FALSE)
315 //1. Translate Listener to origin (convert to head relative)
316 if(HeadRelative==AL_FALSE)
318 Position[0] -= ListenerPosition[0];
319 Position[1] -= ListenerPosition[1];
320 Position[2] -= ListenerPosition[2];
323 //2. Calculate distance attenuation
324 Distance = aluSqrt(aluDotproduct(Position, Position));
326 flAttenuation = 1.0f;
327 RoomAttenuation = 1.0f;
328 switch (DistanceModel)
330 case AL_INVERSE_DISTANCE_CLAMPED:
331 Distance=__max(Distance,MinDist);
332 Distance=__min(Distance,MaxDist);
333 if (MaxDist < MinDist)
334 break;
335 //fall-through
336 case AL_INVERSE_DISTANCE:
337 if (MinDist > 0.0f)
339 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
340 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
341 if ((MinDist + (RoomRolloff * (Distance - MinDist))) > 0.0f)
342 RoomAttenuation = MinDist / (MinDist + (RoomRolloff * (Distance - MinDist)));
344 break;
346 case AL_LINEAR_DISTANCE_CLAMPED:
347 Distance=__max(Distance,MinDist);
348 Distance=__min(Distance,MaxDist);
349 if (MaxDist < MinDist)
350 break;
351 //fall-through
352 case AL_LINEAR_DISTANCE:
353 Distance=__min(Distance,MaxDist);
354 if (MaxDist != MinDist)
356 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
357 RoomAttenuation = 1.0f - (RoomRolloff*(Distance-MinDist)/(MaxDist - MinDist));
359 break;
361 case AL_EXPONENT_DISTANCE_CLAMPED:
362 Distance=__max(Distance,MinDist);
363 Distance=__min(Distance,MaxDist);
364 if (MaxDist < MinDist)
365 break;
366 //fall-through
367 case AL_EXPONENT_DISTANCE:
368 if ((Distance > 0.0f) && (MinDist > 0.0f))
370 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
371 RoomAttenuation = (ALfloat)pow(Distance/MinDist, -RoomRolloff);
373 break;
375 case AL_NONE:
376 default:
377 flAttenuation = 1.0f;
378 RoomAttenuation = 1.0f;
379 break;
382 // Source Gain + Attenuation
383 DryMix = SourceVolume * flAttenuation;
384 WetMix = SourceVolume * ((ALSource->WetGainAuto &&
385 ALSource->Send[0].Slot.AuxSendAuto) ?
386 RoomAttenuation : 1.0f);
388 // Clamp to Min/Max Gain
389 DryMix = __min(DryMix,MaxVolume);
390 DryMix = __max(DryMix,MinVolume);
391 WetMix = __min(WetMix,MaxVolume);
392 WetMix = __max(WetMix,MinVolume);
393 //3. Apply directional soundcones
394 SourceToListener[0] = -Position[0];
395 SourceToListener[1] = -Position[1];
396 SourceToListener[2] = -Position[2];
397 aluNormalize(Direction);
398 aluNormalize(SourceToListener);
399 Angle = aluAcos(aluDotproduct(Direction,SourceToListener)) * 180.0f /
400 3.141592654f;
401 if(Angle >= InnerAngle && Angle <= OuterAngle)
403 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
404 ConeVolume = (1.0f+(OuterGain-1.0f)*scale);
405 if(ALSource->WetGainAuto)
406 WetMix *= ConeVolume;
407 if(ALSource->DryGainHFAuto)
408 DryGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
409 if(ALSource->WetGainHFAuto)
410 WetGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
412 else if(Angle > OuterAngle)
414 ConeVolume = (1.0f+(OuterGain-1.0f));
415 if(ALSource->WetGainAuto)
416 WetMix *= ConeVolume;
417 if(ALSource->DryGainHFAuto)
418 DryGainHF *= (1.0f+(OuterGainHF-1.0f));
419 if(ALSource->WetGainHFAuto)
420 WetGainHF *= (1.0f+(OuterGainHF-1.0f));
422 else
423 ConeVolume = 1.0f;
425 //4. Calculate Velocity
426 if(DopplerFactor != 0.0f)
428 flVLS = aluDotproduct(ListenerVelocity, SourceToListener);
429 flVSS = aluDotproduct(Velocity, SourceToListener);
431 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
433 if (flVSS >= flMaxVelocity)
434 flVSS = (flMaxVelocity - 1.0f);
435 else if (flVSS <= -flMaxVelocity)
436 flVSS = -flMaxVelocity + 1.0f;
438 if (flVLS >= flMaxVelocity)
439 flVLS = (flMaxVelocity - 1.0f);
440 else if (flVLS <= -flMaxVelocity)
441 flVLS = -flMaxVelocity + 1.0f;
443 pitch[0] = Pitch * ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
444 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
446 else
447 pitch[0] = Pitch;
449 //5. Align coordinate system axes
450 aluCrossproduct(&ListenerOrientation[0], &ListenerOrientation[3], U); // Right-vector
451 aluNormalize(U); // Normalized Right-vector
452 memcpy(V, &ListenerOrientation[3], sizeof(V)); // Up-vector
453 aluNormalize(V); // Normalized Up-vector
454 memcpy(N, &ListenerOrientation[0], sizeof(N)); // At-vector
455 aluNormalize(N); // Normalized At-vector
456 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
457 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
458 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
459 aluMatrixVector(Position, Matrix);
461 //6. Apply filter gains and filters
462 switch(ALSource->DirectFilter.filter)
464 case AL_FILTER_LOWPASS:
465 DryMix *= ALSource->DirectFilter.Gain;
466 DryGainHF *= ALSource->DirectFilter.GainHF;
467 break;
470 switch(ALSource->Send[0].WetFilter.filter)
472 case AL_FILTER_LOWPASS:
473 WetMix *= ALSource->Send[0].WetFilter.Gain;
474 WetGainHF *= ALSource->Send[0].WetFilter.GainHF;
475 break;
478 if(ALSource->AirAbsorptionFactor > 0.0f)
479 DryGainHF *= pow(ALSource->AirAbsorptionFactor * AIRABSORBGAINHF,
480 Distance * MetersPerUnit);
482 WetMix *= ALSource->Send[0].Slot.Gain;
484 //7. Convert normalized position into pannings, then into channel volumes
485 aluNormalize(Position);
486 switch(aluChannelsFromFormat(OutputFormat))
488 case 1:
489 drysend[FRONT_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
490 drysend[FRONT_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
491 if(ALSource->Send[0].Slot.effectslot)
493 wetsend[FRONT_LEFT] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
494 wetsend[FRONT_RIGHT] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
496 else
498 wetsend[FRONT_LEFT] = 0.0f;
499 wetsend[FRONT_RIGHT] = 0.0f;
500 WetGainHF = 1.0f;
502 break;
503 case 2:
504 PanningLR = 0.5f + 0.5f*Position[0];
505 drysend[FRONT_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f-PanningLR); //L Direct
506 drysend[FRONT_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt( PanningLR); //R Direct
507 if(ALSource->Send[0].Slot.effectslot)
509 wetsend[FRONT_LEFT] = ListenerGain * WetMix * aluSqrt(1.0f-PanningLR); //L Room
510 wetsend[FRONT_RIGHT] = ListenerGain * WetMix * aluSqrt( PanningLR); //R Room
512 else
514 wetsend[FRONT_LEFT] = 0.0f;
515 wetsend[FRONT_RIGHT] = 0.0f;
516 WetGainHF = 1.0f;
518 break;
519 case 4:
520 /* TODO: Add center/lfe channel in spatial calculations? */
521 case 6:
522 // Apply a scalar so each individual speaker has more weight
523 PanningLR = 0.5f + (0.5f*Position[0]*1.41421356f);
524 PanningLR = __min(1.0f, PanningLR);
525 PanningLR = __max(0.0f, PanningLR);
526 PanningFB = 0.5f + (0.5f*Position[2]*1.41421356f);
527 PanningFB = __min(1.0f, PanningFB);
528 PanningFB = __max(0.0f, PanningFB);
529 drysend[FRONT_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
530 drysend[FRONT_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
531 drysend[BACK_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
532 drysend[BACK_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*( PanningFB));
533 if(ALSource->Send[0].Slot.effectslot)
535 wetsend[FRONT_LEFT] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
536 wetsend[FRONT_RIGHT] = ListenerGain * WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
537 wetsend[BACK_LEFT] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
538 wetsend[BACK_RIGHT] = ListenerGain * WetMix * aluSqrt(( PanningLR)*( PanningFB));
540 else
542 wetsend[FRONT_LEFT] = 0.0f;
543 wetsend[FRONT_RIGHT] = 0.0f;
544 wetsend[BACK_LEFT] = 0.0f;
545 wetsend[BACK_RIGHT] = 0.0f;
546 WetGainHF = 1.0f;
548 break;
549 case 7:
550 case 8:
551 PanningFB = 1.0f - fabs(Position[2]*1.15470054f);
552 PanningFB = __min(1.0f, PanningFB);
553 PanningFB = __max(0.0f, PanningFB);
554 PanningLR = 0.5f + (0.5*Position[0]*((1.0f-PanningFB)*2.0f));
555 PanningLR = __min(1.0f, PanningLR);
556 PanningLR = __max(0.0f, PanningLR);
557 if(Position[2] > 0.0f)
559 drysend[BACK_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
560 drysend[BACK_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
561 drysend[SIDE_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
562 drysend[SIDE_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*( PanningFB));
563 drysend[FRONT_LEFT] = 0.0f;
564 drysend[FRONT_RIGHT] = 0.0f;
565 if(ALSource->Send[0].Slot.effectslot)
567 wetsend[BACK_LEFT] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
568 wetsend[BACK_RIGHT] = ListenerGain * WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
569 wetsend[SIDE_LEFT] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
570 wetsend[SIDE_RIGHT] = ListenerGain * WetMix * aluSqrt(( PanningLR)*( PanningFB));
571 wetsend[FRONT_LEFT] = 0.0f;
572 wetsend[FRONT_RIGHT] = 0.0f;
574 else
576 wetsend[FRONT_LEFT] = 0.0f;
577 wetsend[FRONT_RIGHT] = 0.0f;
578 wetsend[SIDE_LEFT] = 0.0f;
579 wetsend[SIDE_RIGHT] = 0.0f;
580 wetsend[BACK_LEFT] = 0.0f;
581 wetsend[BACK_RIGHT] = 0.0f;
582 WetGainHF = 1.0f;
585 else
587 drysend[FRONT_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
588 drysend[FRONT_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
589 drysend[SIDE_LEFT] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
590 drysend[SIDE_RIGHT] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*( PanningFB));
591 drysend[BACK_LEFT] = 0.0f;
592 drysend[BACK_RIGHT] = 0.0f;
593 if(ALSource->Send[0].Slot.effectslot)
595 wetsend[FRONT_LEFT] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB));
596 wetsend[FRONT_RIGHT] = ListenerGain * WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB));
597 wetsend[SIDE_LEFT] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB));
598 wetsend[SIDE_RIGHT] = ListenerGain * WetMix * aluSqrt(( PanningLR)*( PanningFB));
599 wetsend[BACK_LEFT] = 0.0f;
600 wetsend[BACK_RIGHT] = 0.0f;
602 else
604 wetsend[FRONT_LEFT] = 0.0f;
605 wetsend[FRONT_RIGHT] = 0.0f;
606 wetsend[SIDE_LEFT] = 0.0f;
607 wetsend[SIDE_RIGHT] = 0.0f;
608 wetsend[BACK_LEFT] = 0.0f;
609 wetsend[BACK_RIGHT] = 0.0f;
610 WetGainHF = 1.0f;
613 default:
614 break;
617 *drygainhf = DryGainHF;
618 *wetgainhf = WetGainHF;
620 else
622 *drygainhf = DryGainHF;
623 *wetgainhf = WetGainHF;
625 //1. Multi-channel buffers always play "normal"
626 drysend[FRONT_LEFT] = SourceVolume * 1.0f * ListenerGain;
627 drysend[FRONT_RIGHT] = SourceVolume * 1.0f * ListenerGain;
628 drysend[SIDE_LEFT] = SourceVolume * 1.0f * ListenerGain;
629 drysend[SIDE_RIGHT] = SourceVolume * 1.0f * ListenerGain;
630 drysend[BACK_LEFT] = SourceVolume * 1.0f * ListenerGain;
631 drysend[BACK_RIGHT] = SourceVolume * 1.0f * ListenerGain;
632 drysend[CENTER] = SourceVolume * 1.0f * ListenerGain;
633 drysend[LFE] = SourceVolume * 1.0f * ListenerGain;
634 if(ALSource->Send[0].Slot.effectslot)
636 wetsend[FRONT_LEFT] = SourceVolume * 0.0f * ListenerGain;
637 wetsend[FRONT_RIGHT] = SourceVolume * 0.0f * ListenerGain;
638 wetsend[SIDE_LEFT] = SourceVolume * 0.0f * ListenerGain;
639 wetsend[SIDE_RIGHT] = SourceVolume * 0.0f * ListenerGain;
640 wetsend[BACK_LEFT] = SourceVolume * 0.0f * ListenerGain;
641 wetsend[BACK_RIGHT] = SourceVolume * 0.0f * ListenerGain;
642 wetsend[CENTER] = SourceVolume * 0.0f * ListenerGain;
643 wetsend[LFE] = SourceVolume * 0.0f * ListenerGain;
645 else
647 wetsend[FRONT_LEFT] = 0.0f;
648 wetsend[FRONT_RIGHT] = 0.0f;
649 wetsend[SIDE_LEFT] = 0.0f;
650 wetsend[SIDE_RIGHT] = 0.0f;
651 wetsend[BACK_LEFT] = 0.0f;
652 wetsend[BACK_RIGHT] = 0.0f;
653 wetsend[CENTER] = 0.0f;
654 wetsend[LFE] = 0.0f;
655 *wetgainhf = 1.0f;
658 pitch[0] = Pitch;
662 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
664 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
665 static float WetBuffer[BUFFERSIZE][OUTPUTCHANNELS];
666 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
667 ALfloat WetSend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
668 ALfloat DryGainHF = 0.0f;
669 ALfloat WetGainHF = 0.0f;
670 ALuint BlockAlign,BufferSize;
671 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
672 ALuint Channels,Bits,Frequency,ulExtraSamples;
673 ALfloat Pitch;
674 ALint Looping,increment,State;
675 ALuint Buffer,fraction;
676 ALuint SamplesToDo;
677 ALsource *ALSource;
678 ALbuffer *ALBuffer;
679 ALfloat value;
680 ALshort *Data;
681 ALuint i,j,k;
682 ALbufferlistitem *BufferListItem;
683 ALuint loop;
684 ALint64 DataSize64,DataPos64;
686 SuspendContext(ALContext);
688 if(buffer)
690 //Figure output format variables
691 BlockAlign = aluChannelsFromFormat(format);
692 BlockAlign *= aluBytesFromFormat(format);
694 size /= BlockAlign;
695 while(size > 0)
697 //Setup variables
698 ALSource = (ALContext ? ALContext->Source : NULL);
699 SamplesToDo = min(size, BUFFERSIZE);
701 //Clear mixing buffer
702 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
703 memset(WetBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
705 //Actual mixing loop
706 while(ALSource)
708 j = 0;
709 State = ALSource->state;
710 while(State == AL_PLAYING && j < SamplesToDo)
712 DataSize = 0;
713 DataPosInt = 0;
714 DataPosFrac = 0;
716 //Get buffer info
717 if((Buffer = ALSource->ulBufferID))
719 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
721 Data = ALBuffer->data;
722 Bits = aluBytesFromFormat(ALBuffer->format) * 8;
723 Channels = aluChannelsFromFormat(ALBuffer->format);
724 DataSize = ALBuffer->size;
725 Frequency = ALBuffer->frequency;
727 CalcSourceParams(ALContext, ALSource,
728 (Channels==1) ? AL_TRUE : AL_FALSE,
729 format, DrySend, WetSend, &Pitch,
730 &DryGainHF, &WetGainHF);
733 Pitch = (Pitch*Frequency) / ALContext->Frequency;
734 DataSize = DataSize / (Bits*Channels/8);
736 //Get source info
737 DataPosInt = ALSource->position;
738 DataPosFrac = ALSource->position_fraction;
740 //Compute 18.14 fixed point step
741 increment = aluF2L(Pitch*(1L<<FRACTIONBITS));
742 if(increment > (MAX_PITCH<<FRACTIONBITS))
743 increment = (MAX_PITCH<<FRACTIONBITS);
745 //Figure out how many samples we can mix.
746 //Pitch must be <= 4 (the number below !)
747 DataSize64 = DataSize+MAX_PITCH;
748 DataSize64 <<= FRACTIONBITS;
749 DataPos64 = DataPosInt;
750 DataPos64 <<= FRACTIONBITS;
751 DataPos64 += DataPosFrac;
752 BufferSize = (ALuint)((DataSize64-DataPos64) / increment);
753 BufferListItem = ALSource->queue;
754 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
756 if(BufferListItem)
757 BufferListItem = BufferListItem->next;
759 if (BufferListItem)
761 if (BufferListItem->next)
763 if(BufferListItem->next->buffer &&
764 ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data)
766 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->size, (ALint)(16*Channels));
767 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data, ulExtraSamples);
770 else if (ALSource->bLooping)
772 if (ALSource->queue->buffer)
774 if(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data)
776 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->size, (ALint)(16*Channels));
777 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data, ulExtraSamples);
782 BufferSize = min(BufferSize, (SamplesToDo-j));
784 //Actual sample mixing loop
785 Data += DataPosInt*Channels;
786 while(BufferSize--)
788 k = DataPosFrac>>FRACTIONBITS;
789 fraction = DataPosFrac&FRACTIONMASK;
790 if(Channels==1)
792 //First order interpolator
793 ALfloat sample = (ALfloat)((ALshort)(((Data[k]*((1L<<FRACTIONBITS)-fraction))+(Data[k+1]*(fraction)))>>FRACTIONBITS));
795 //Direct path final mix buffer and panning
796 value = aluComputeDrySample(ALSource, DryGainHF, sample);
797 DryBuffer[j][FRONT_LEFT] += value*DrySend[FRONT_LEFT];
798 DryBuffer[j][FRONT_RIGHT] += value*DrySend[FRONT_RIGHT];
799 DryBuffer[j][SIDE_LEFT] += value*DrySend[SIDE_LEFT];
800 DryBuffer[j][SIDE_RIGHT] += value*DrySend[SIDE_RIGHT];
801 DryBuffer[j][BACK_LEFT] += value*DrySend[BACK_LEFT];
802 DryBuffer[j][BACK_RIGHT] += value*DrySend[BACK_RIGHT];
803 //Room path final mix buffer and panning
804 value = aluComputeWetSample(ALSource, WetGainHF, sample);
805 WetBuffer[j][FRONT_LEFT] += value*WetSend[FRONT_LEFT];
806 WetBuffer[j][FRONT_RIGHT] += value*WetSend[FRONT_RIGHT];
807 WetBuffer[j][SIDE_LEFT] += value*WetSend[SIDE_LEFT];
808 WetBuffer[j][SIDE_RIGHT] += value*WetSend[SIDE_RIGHT];
809 WetBuffer[j][BACK_LEFT] += value*WetSend[BACK_LEFT];
810 WetBuffer[j][BACK_RIGHT] += value*WetSend[BACK_RIGHT];
812 else
814 //First order interpolator (front left)
815 value = (ALfloat)((ALshort)(((Data[k*Channels ]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels ]*(fraction)))>>FRACTIONBITS));
816 DryBuffer[j][FRONT_LEFT] += value*DrySend[FRONT_LEFT];
817 WetBuffer[j][FRONT_LEFT] += value*WetSend[FRONT_LEFT];
818 //First order interpolator (front right)
819 value = (ALfloat)((ALshort)(((Data[k*Channels+1]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+1]*(fraction)))>>FRACTIONBITS));
820 DryBuffer[j][FRONT_RIGHT] += value*DrySend[FRONT_RIGHT];
821 WetBuffer[j][FRONT_RIGHT] += value*WetSend[FRONT_RIGHT];
822 if(Channels >= 4)
824 int i = 2;
825 if(Channels >= 6)
827 if(Channels != 7)
829 //First order interpolator (center)
830 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
831 DryBuffer[j][CENTER] += value*DrySend[CENTER];
832 WetBuffer[j][CENTER] += value*WetSend[CENTER];
833 i++;
835 //First order interpolator (lfe)
836 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
837 DryBuffer[j][LFE] += value*DrySend[LFE];
838 WetBuffer[j][LFE] += value*WetSend[LFE];
839 i++;
841 //First order interpolator (back left)
842 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
843 DryBuffer[j][BACK_LEFT] += value*DrySend[BACK_LEFT];
844 WetBuffer[j][BACK_LEFT] += value*WetSend[BACK_LEFT];
845 i++;
846 //First order interpolator (back right)
847 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
848 DryBuffer[j][BACK_RIGHT] += value*DrySend[BACK_RIGHT];
849 WetBuffer[j][BACK_RIGHT] += value*WetSend[BACK_RIGHT];
850 i++;
851 if(Channels >= 7)
853 //First order interpolator (side left)
854 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
855 DryBuffer[j][SIDE_LEFT] += value*DrySend[SIDE_LEFT];
856 WetBuffer[j][SIDE_LEFT] += value*WetSend[SIDE_LEFT];
857 i++;
858 //First order interpolator (side right)
859 value = (ALfloat)((ALshort)(((Data[k*Channels+i]*((1L<<FRACTIONBITS)-fraction))+(Data[(k+1)*Channels+i]*(fraction)))>>FRACTIONBITS));
860 DryBuffer[j][SIDE_RIGHT] += value*DrySend[SIDE_RIGHT];
861 WetBuffer[j][SIDE_RIGHT] += value*WetSend[SIDE_RIGHT];
862 i++;
866 DataPosFrac += increment;
867 j++;
869 DataPosInt += (DataPosFrac>>FRACTIONBITS);
870 DataPosFrac = (DataPosFrac&FRACTIONMASK);
872 //Update source info
873 ALSource->position = DataPosInt;
874 ALSource->position_fraction = DataPosFrac;
877 //Handle looping sources
878 if(!Buffer || DataPosInt >= DataSize)
880 //queueing
881 if(ALSource->queue)
883 Looping = ALSource->bLooping;
884 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
886 BufferListItem = ALSource->queue;
887 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
889 if(BufferListItem)
891 if(!Looping)
892 BufferListItem->bufferstate = PROCESSED;
893 BufferListItem = BufferListItem->next;
896 if(!Looping)
897 ALSource->BuffersProcessed++;
898 if(BufferListItem)
899 ALSource->ulBufferID = BufferListItem->buffer;
900 ALSource->position = DataPosInt-DataSize;
901 ALSource->position_fraction = DataPosFrac;
902 ALSource->BuffersPlayed++;
904 else
906 if(!Looping)
908 /* alSourceStop */
909 ALSource->state = AL_STOPPED;
910 ALSource->inuse = AL_FALSE;
911 ALSource->BuffersPlayed = ALSource->BuffersProcessed = ALSource->BuffersInQueue;
912 BufferListItem = ALSource->queue;
913 while(BufferListItem != NULL)
915 BufferListItem->bufferstate = PROCESSED;
916 BufferListItem = BufferListItem->next;
919 else
921 /* alSourceRewind */
922 /* alSourcePlay */
923 ALSource->state = AL_PLAYING;
924 ALSource->inuse = AL_TRUE;
925 ALSource->play = AL_TRUE;
926 ALSource->BuffersPlayed = 0;
927 ALSource->BufferPosition = 0;
928 ALSource->lBytesPlayed = 0;
929 ALSource->BuffersProcessed = 0;
930 BufferListItem = ALSource->queue;
931 while(BufferListItem != NULL)
933 BufferListItem->bufferstate = PENDING;
934 BufferListItem = BufferListItem->next;
936 ALSource->ulBufferID = ALSource->queue->buffer;
938 ALSource->position = DataPosInt-DataSize;
939 ALSource->position_fraction = DataPosFrac;
945 //Get source state
946 State = ALSource->state;
949 ALSource = ALSource->next;
952 //Post processing loop
953 switch(format)
955 case AL_FORMAT_MONO8:
956 for(i = 0;i < SamplesToDo;i++)
958 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]+
959 WetBuffer[i][FRONT_LEFT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
960 buffer = ((ALubyte*)buffer) + 1;
962 break;
963 case AL_FORMAT_STEREO8:
964 if(ALContext->bs2b)
966 for(i = 0;i < SamplesToDo;i++)
968 float samples[2];
969 samples[0] = DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT];
970 samples[1] = DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT];
971 bs2b_cross_feed(ALContext->bs2b, samples);
972 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(samples[0])>>8)+128);
973 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(samples[1])>>8)+128);
974 buffer = ((ALubyte*)buffer) + 2;
977 else
979 for(i = 0;i < SamplesToDo;i++)
981 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
982 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
983 buffer = ((ALubyte*)buffer) + 2;
986 break;
987 case AL_FORMAT_QUAD8:
988 for(i = 0;i < SamplesToDo;i++)
990 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
991 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
992 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
993 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
994 buffer = ((ALubyte*)buffer) + 4;
996 break;
997 case AL_FORMAT_51CHN8:
998 for(i = 0;i < SamplesToDo;i++)
1000 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
1001 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
1002 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1003 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1004 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER])>>8)+128);
1005 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1006 buffer = ((ALubyte*)buffer) + 6;
1008 break;
1009 case AL_FORMAT_61CHN8:
1010 for(i = 0;i < SamplesToDo;i++)
1012 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
1013 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
1014 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT])>>8)+128);
1015 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT])>>8)+128);
1016 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1017 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1018 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1019 buffer = ((ALubyte*)buffer) + 7;
1021 break;
1022 case AL_FORMAT_71CHN8:
1023 for(i = 0;i < SamplesToDo;i++)
1025 ((ALubyte*)buffer)[0] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT])>>8)+128);
1026 ((ALubyte*)buffer)[1] = (ALubyte)((aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT])>>8)+128);
1027 ((ALubyte*)buffer)[2] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT])>>8)+128);
1028 ((ALubyte*)buffer)[3] = (ALubyte)((aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT])>>8)+128);
1029 ((ALubyte*)buffer)[4] = (ALubyte)((aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT])>>8)+128);
1030 ((ALubyte*)buffer)[5] = (ALubyte)((aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT])>>8)+128);
1031 ((ALubyte*)buffer)[6] = (ALubyte)((aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER])>>8)+128);
1032 ((ALubyte*)buffer)[7] = (ALubyte)((aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE])>>8)+128);
1033 buffer = ((ALubyte*)buffer) + 8;
1035 break;
1037 case AL_FORMAT_MONO16:
1038 for(i = 0;i < SamplesToDo;i++)
1040 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT]+DryBuffer[i][FRONT_RIGHT]+
1041 WetBuffer[i][FRONT_LEFT]+WetBuffer[i][FRONT_RIGHT]);
1042 buffer = ((ALshort*)buffer) + 1;
1044 break;
1045 case AL_FORMAT_STEREO16:
1046 if(ALContext->bs2b)
1048 for(i = 0;i < SamplesToDo;i++)
1050 float samples[2];
1051 samples[0] = DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT];
1052 samples[1] = DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT];
1053 bs2b_cross_feed(ALContext->bs2b, samples);
1054 ((ALshort*)buffer)[0] = aluF2S(samples[0]);
1055 ((ALshort*)buffer)[1] = aluF2S(samples[1]);
1056 buffer = ((ALshort*)buffer) + 2;
1059 else
1061 for(i = 0;i < SamplesToDo;i++)
1063 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1064 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1065 buffer = ((ALshort*)buffer) + 2;
1068 break;
1069 case AL_FORMAT_QUAD16:
1070 for(i = 0;i < SamplesToDo;i++)
1072 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1073 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1074 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1075 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1076 buffer = ((ALshort*)buffer) + 4;
1078 break;
1079 case AL_FORMAT_51CHN16:
1080 for(i = 0;i < SamplesToDo;i++)
1082 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1083 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1084 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1085 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1086 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER]);
1087 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1088 buffer = ((ALshort*)buffer) + 6;
1090 break;
1091 case AL_FORMAT_61CHN16:
1092 for(i = 0;i < SamplesToDo;i++)
1094 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1095 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1096 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT]);
1097 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT]);
1098 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1099 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1100 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1101 buffer = ((ALshort*)buffer) + 7;
1103 break;
1104 case AL_FORMAT_71CHN16:
1105 for(i = 0;i < SamplesToDo;i++)
1107 ((ALshort*)buffer)[0] = aluF2S(DryBuffer[i][FRONT_LEFT] +WetBuffer[i][FRONT_LEFT]);
1108 ((ALshort*)buffer)[1] = aluF2S(DryBuffer[i][FRONT_RIGHT]+WetBuffer[i][FRONT_RIGHT]);
1109 ((ALshort*)buffer)[2] = aluF2S(DryBuffer[i][SIDE_LEFT] +WetBuffer[i][SIDE_LEFT]);
1110 ((ALshort*)buffer)[3] = aluF2S(DryBuffer[i][SIDE_RIGHT] +WetBuffer[i][SIDE_RIGHT]);
1111 ((ALshort*)buffer)[4] = aluF2S(DryBuffer[i][BACK_LEFT] +WetBuffer[i][BACK_LEFT]);
1112 ((ALshort*)buffer)[5] = aluF2S(DryBuffer[i][BACK_RIGHT] +WetBuffer[i][BACK_RIGHT]);
1113 ((ALshort*)buffer)[6] = aluF2S(DryBuffer[i][CENTER] +WetBuffer[i][CENTER]);
1114 ((ALshort*)buffer)[7] = aluF2S(DryBuffer[i][LFE] +WetBuffer[i][LFE]);
1115 buffer = ((ALshort*)buffer) + 8;
1117 break;
1119 default:
1120 break;
1123 size -= SamplesToDo;
1127 ProcessContext(ALContext);