Add initial auxiliary send paths
[openal-soft.git] / Alc / ALu.c
blob061f87a13eb1dfee1df7686ed86ba314fb841d39
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"
30 #if defined(HAVE_STDINT_H)
31 #include <stdint.h>
32 typedef int64_t ALint64;
33 #elif defined(HAVE___INT64)
34 typedef __int64 ALint64;
35 #elif (SIZEOF_LONG == 8)
36 typedef long ALint64;
37 #elif (SIZEOF_LONG_LONG == 8)
38 typedef long long ALint64;
39 #endif
41 #ifdef HAVE_SQRTF
42 #define aluSqrt(x) ((ALfloat)sqrtf((float)(x)))
43 #else
44 #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
45 #endif
47 // fixes for mingw32.
48 #if defined(max) && !defined(__max)
49 #define __max max
50 #endif
51 #if defined(min) && !defined(__min)
52 #define __min min
53 #endif
55 __inline ALuint aluBytesFromFormat(ALenum format)
57 switch(format)
59 case AL_FORMAT_MONO8:
60 case AL_FORMAT_STEREO8:
61 case AL_FORMAT_QUAD8:
62 return 1;
64 case AL_FORMAT_MONO16:
65 case AL_FORMAT_STEREO16:
66 case AL_FORMAT_QUAD16:
67 return 2;
69 default:
70 return 0;
74 __inline ALuint aluChannelsFromFormat(ALenum format)
76 switch(format)
78 case AL_FORMAT_MONO8:
79 case AL_FORMAT_MONO16:
80 return 1;
82 case AL_FORMAT_STEREO8:
83 case AL_FORMAT_STEREO16:
84 return 2;
86 case AL_FORMAT_QUAD8:
87 case AL_FORMAT_QUAD16:
88 return 4;
90 default:
91 return 0;
95 static __inline ALint aluF2L(ALfloat Value)
97 if(sizeof(ALint) == 4 && sizeof(double) == 8)
99 double temp;
100 temp = Value + (((65536.0*65536.0*16.0)+(65536.0*65536.0*8.0))*65536.0);
101 return *((ALint*)&temp);
103 return (ALint)Value;
106 static __inline ALshort aluF2S(ALfloat Value)
108 ALint i;
110 i = aluF2L(Value);
111 i = __min( 32767, i);
112 i = __max(-32768, i);
113 return ((ALshort)i);
116 static __inline ALvoid aluCrossproduct(ALfloat *inVector1,ALfloat *inVector2,ALfloat *outVector)
118 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
119 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
120 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
123 static __inline ALfloat aluDotproduct(ALfloat *inVector1,ALfloat *inVector2)
125 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
126 inVector1[2]*inVector2[2];
129 static __inline ALvoid aluNormalize(ALfloat *inVector)
131 ALfloat length, inverse_length;
133 length = (ALfloat)aluSqrt(aluDotproduct(inVector, inVector));
134 if(length != 0)
136 inverse_length = 1.0f/length;
137 inVector[0] *= inverse_length;
138 inVector[1] *= inverse_length;
139 inVector[2] *= inverse_length;
143 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
145 ALfloat result[3];
147 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
148 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
149 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
150 memcpy(vector, result, sizeof(result));
153 static __inline ALfloat aluComputeDrySample(ALsource *source, ALfloat DryGainHF, ALfloat sample)
155 if(DryGainHF < 1.0f)
157 sample *= DryGainHF;
158 sample += source->LastDrySample * (1.0f - DryGainHF);
161 source->LastDrySample = sample;
162 return sample;
165 static __inline ALfloat aluComputeWetSample(ALsource *source, ALfloat WetGainHF, ALfloat sample)
167 if(WetGainHF < 1.0f)
169 sample *= WetGainHF;
170 sample += source->LastWetSample * (1.0f - WetGainHF);
173 source->LastWetSample = sample;
174 return sample;
177 static ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
178 ALenum isMono, ALenum OutputFormat,
179 ALfloat *drysend, ALfloat *wetsend,
180 ALfloat *pitch, ALfloat *drygainhf,
181 ALfloat *wetgainhf)
183 ALfloat ListenerOrientation[6],ListenerPosition[3],ListenerVelocity[3];
184 ALfloat InnerAngle,OuterAngle,OuterGain,Angle,Distance,DryMix,WetMix;
185 ALfloat Direction[3],Position[3],Velocity[3],SourceToListener[3];
186 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
187 ALfloat Pitch,ConeVolume,SourceVolume,PanningFB,PanningLR,ListenerGain;
188 ALfloat U[3],V[3],N[3];
189 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
190 ALfloat flVSS, flVLS;
191 ALint DistanceModel;
192 ALfloat Matrix[3][3];
193 ALint HeadRelative;
194 ALfloat flAttenuation;
195 ALfloat MetersPerUnit;
196 ALfloat DryGainHF = 1.0f;
197 ALfloat WetGainHF = 1.0f;
199 //Get context properties
200 DopplerFactor = ALContext->DopplerFactor;
201 DistanceModel = ALContext->DistanceModel;
202 DopplerVelocity = ALContext->DopplerVelocity;
203 flSpeedOfSound = ALContext->flSpeedOfSound;
205 //Get listener properties
206 ListenerGain = ALContext->Listener.Gain;
207 MetersPerUnit = ALContext->Listener.MetersPerUnit;
208 memcpy(ListenerPosition, ALContext->Listener.Position, sizeof(ALContext->Listener.Position));
209 memcpy(ListenerVelocity, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
210 memcpy(&ListenerOrientation[0], ALContext->Listener.Forward, sizeof(ALContext->Listener.Forward));
211 memcpy(&ListenerOrientation[3], ALContext->Listener.Up, sizeof(ALContext->Listener.Up));
213 //Get source properties
214 Pitch = ALSource->flPitch;
215 SourceVolume = ALSource->flGain;
216 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
217 memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
218 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
219 MinVolume = ALSource->flMinGain;
220 MaxVolume = ALSource->flMaxGain;
221 MinDist = ALSource->flRefDistance;
222 MaxDist = ALSource->flMaxDistance;
223 Rolloff = ALSource->flRollOffFactor;
224 OuterGain = ALSource->flOuterGain;
225 InnerAngle = ALSource->flInnerAngle;
226 OuterAngle = ALSource->flOuterAngle;
227 HeadRelative = ALSource->bHeadRelative;
228 OuterGainHF = (ALSource->DryGainHFAuto ? ALSource->OuterGainHF : 1.0f);
230 //Set working variables
231 DryMix = (ALfloat)(1.0f);
232 WetMix = (ALfloat)(0.0f);
234 //Only apply 3D calculations for mono buffers
235 if(isMono != AL_FALSE)
237 //1. Translate Listener to origin (convert to head relative)
238 if(HeadRelative==AL_FALSE)
240 Position[0] -= ListenerPosition[0];
241 Position[1] -= ListenerPosition[1];
242 Position[2] -= ListenerPosition[2];
245 //2. Calculate distance attenuation
246 Distance = aluSqrt(aluDotproduct(Position, Position));
248 flAttenuation = 1.0f;
249 switch (DistanceModel)
251 case AL_INVERSE_DISTANCE_CLAMPED:
252 Distance=__max(Distance,MinDist);
253 Distance=__min(Distance,MaxDist);
254 if (MaxDist < MinDist)
255 break;
256 //fall-through
257 case AL_INVERSE_DISTANCE:
258 if (MinDist > 0.0f)
260 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
261 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
263 break;
265 case AL_LINEAR_DISTANCE_CLAMPED:
266 Distance=__max(Distance,MinDist);
267 Distance=__min(Distance,MaxDist);
268 if (MaxDist < MinDist)
269 break;
270 //fall-through
271 case AL_LINEAR_DISTANCE:
272 Distance=__min(Distance,MaxDist);
273 if (MaxDist != MinDist)
274 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
275 break;
277 case AL_EXPONENT_DISTANCE_CLAMPED:
278 Distance=__max(Distance,MinDist);
279 Distance=__min(Distance,MaxDist);
280 if (MaxDist < MinDist)
281 break;
282 //fall-through
283 case AL_EXPONENT_DISTANCE:
284 if ((Distance > 0.0f) && (MinDist > 0.0f))
285 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
286 break;
288 case AL_NONE:
289 default:
290 flAttenuation = 1.0f;
291 break;
294 // Source Gain + Attenuation
295 DryMix = SourceVolume * flAttenuation;
297 // Clamp to Min/Max Gain
298 DryMix = __min(DryMix,MaxVolume);
299 DryMix = __max(DryMix,MinVolume);
300 WetMix = __min(WetMix,MaxVolume);
301 WetMix = __max(WetMix,MinVolume);
302 //3. Apply directional soundcones
303 SourceToListener[0] = -Position[0];
304 SourceToListener[1] = -Position[1];
305 SourceToListener[2] = -Position[2];
306 aluNormalize(Direction);
307 aluNormalize(SourceToListener);
308 Angle = (ALfloat)(180.0*acos(aluDotproduct(Direction,SourceToListener))/3.141592654f);
309 if(Angle >= InnerAngle && Angle <= OuterAngle)
311 ALfloat scale = (Angle-InnerAngle) / (OuterAngle-InnerAngle);
312 ConeVolume = (1.0f+(OuterGain-1.0f)*scale);
313 DryGainHF *= (1.0f+(OuterGainHF-1.0f)*scale);
315 else if(Angle > OuterAngle)
317 ConeVolume = (1.0f+(OuterGain-1.0f));
318 DryGainHF *= (1.0f+(OuterGainHF-1.0f));
320 else
321 ConeVolume = 1.0f;
323 //4. Calculate Velocity
324 if(DopplerFactor != 0.0f)
326 flVLS = aluDotproduct(ListenerVelocity, SourceToListener);
327 flVSS = aluDotproduct(Velocity, SourceToListener);
329 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
331 if (flVSS >= flMaxVelocity)
332 flVSS = (flMaxVelocity - 1.0f);
333 else if (flVSS <= -flMaxVelocity)
334 flVSS = -flMaxVelocity + 1.0f;
336 if (flVLS >= flMaxVelocity)
337 flVLS = (flMaxVelocity - 1.0f);
338 else if (flVLS <= -flMaxVelocity)
339 flVLS = -flMaxVelocity + 1.0f;
341 pitch[0] = Pitch * ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
342 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
344 else
345 pitch[0] = Pitch;
347 //5. Align coordinate system axes
348 aluCrossproduct(&ListenerOrientation[0], &ListenerOrientation[3], U); // Right-vector
349 aluNormalize(U); // Normalized Right-vector
350 memcpy(V, &ListenerOrientation[3], sizeof(V)); // Up-vector
351 aluNormalize(V); // Normalized Up-vector
352 memcpy(N, &ListenerOrientation[0], sizeof(N)); // At-vector
353 aluNormalize(N); // Normalized At-vector
354 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
355 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
356 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
357 aluMatrixVector(Position, Matrix);
359 //6. Convert normalized position into left/right front/back pannings
360 if(Distance != 0.0f)
362 aluNormalize(Position);
363 PanningLR = 0.5f + 0.5f*Position[0];
364 PanningFB = 0.5f + 0.5f*Position[2];
366 else
368 PanningLR = 0.5f;
369 PanningFB = 0.5f;
372 //7. Apply filter gains and filters
373 switch(ALSource->DirectFilter.filter)
375 case AL_FILTER_LOWPASS:
376 DryMix *= ALSource->DirectFilter.Gain;
377 DryGainHF *= ALSource->DirectFilter.GainHF;
378 break;
381 switch(ALSource->Send[0].WetFilter.filter)
383 case AL_FILTER_LOWPASS:
384 WetMix *= ALSource->Send[0].WetFilter.Gain;
385 WetGainHF *= ALSource->Send[0].WetFilter.GainHF;
386 break;
389 if(ALSource->AirAbsorptionFactor > 0.0f)
390 DryGainHF *= pow(ALSource->AirAbsorptionFactor * AIRABSORBGAINHF,
391 Distance * MetersPerUnit);
393 *drygainhf = DryGainHF;
394 *wetgainhf = WetGainHF;
396 //8. Convert pannings into channel volumes
397 switch(OutputFormat)
399 case AL_FORMAT_MONO8:
400 case AL_FORMAT_MONO16:
401 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
402 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
403 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
404 wetsend[1] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
405 break;
406 case AL_FORMAT_STEREO8:
407 case AL_FORMAT_STEREO16:
408 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f-PanningLR); //L Direct
409 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt( PanningLR); //R Direct
410 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f-PanningLR); //L Room
411 wetsend[1] = ListenerGain * WetMix * aluSqrt( PanningLR); //R Room
412 break;
413 case AL_FORMAT_QUAD8:
414 case AL_FORMAT_QUAD16:
415 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Direct
416 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Direct
417 drysend[2] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Direct
418 drysend[3] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Direct
419 wetsend[0] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Room
420 wetsend[1] = ListenerGain * WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Room
421 wetsend[2] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Room
422 wetsend[3] = ListenerGain * WetMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Room
423 break;
424 default:
425 break;
428 else
430 //1. Multi-channel buffers always play "normal"
431 drysend[0] = SourceVolume * 1.0f * ListenerGain;
432 drysend[1] = SourceVolume * 1.0f * ListenerGain;
433 drysend[2] = SourceVolume * 1.0f * ListenerGain;
434 drysend[3] = SourceVolume * 1.0f * ListenerGain;
435 wetsend[0] = SourceVolume * 0.0f * ListenerGain;
436 wetsend[1] = SourceVolume * 0.0f * ListenerGain;
437 wetsend[2] = SourceVolume * 0.0f * ListenerGain;
438 wetsend[3] = SourceVolume * 0.0f * ListenerGain;
440 pitch[0] = Pitch;
442 *drygainhf = DryGainHF;
443 *wetgainhf = WetGainHF;
447 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
449 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
450 static float WetBuffer[BUFFERSIZE][OUTPUTCHANNELS];
451 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f };
452 ALfloat WetSend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f };
453 ALfloat DryGainHF = 0.0f;
454 ALfloat WetGainHF = 0.0f;
455 ALuint BlockAlign,BufferSize;
456 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
457 ALuint Channels,Bits,Frequency,ulExtraSamples;
458 ALfloat Pitch;
459 ALint Looping,increment,State;
460 ALuint Buffer,fraction;
461 ALuint SamplesToDo;
462 ALsource *ALSource;
463 ALbuffer *ALBuffer;
464 ALfloat value;
465 ALshort *Data;
466 ALuint i,j,k;
467 ALbufferlistitem *BufferListItem;
468 ALuint loop;
469 ALint64 DataSize64,DataPos64;
471 SuspendContext(ALContext);
473 if(buffer)
475 //Figure output format variables
476 BlockAlign = aluChannelsFromFormat(format);
477 BlockAlign *= aluBytesFromFormat(format);
479 size /= BlockAlign;
480 while(size > 0)
482 //Setup variables
483 ALSource = (ALContext ? ALContext->Source : NULL);
484 SamplesToDo = min(size, BUFFERSIZE);
486 //Clear mixing buffer
487 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
488 memset(WetBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
490 //Actual mixing loop
491 while(ALSource)
493 j = 0;
494 State = ALSource->state;
495 while(State == AL_PLAYING && j < SamplesToDo)
497 DataSize = 0;
498 DataPosInt = 0;
499 DataPosFrac = 0;
501 //Get buffer info
502 if((Buffer = ALSource->ulBufferID))
504 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
506 Data = ALBuffer->data;
507 Bits = aluBytesFromFormat(ALBuffer->format) * 8;
508 Channels = aluChannelsFromFormat(ALBuffer->format);
509 DataSize = ALBuffer->size;
510 Frequency = ALBuffer->frequency;
512 CalcSourceParams(ALContext, ALSource,
513 (Channels==1) ? AL_TRUE : AL_FALSE,
514 format, DrySend, WetSend, &Pitch,
515 &DryGainHF, &WetGainHF);
518 Pitch = (Pitch*Frequency) / ALContext->Frequency;
519 DataSize = DataSize / (Bits*Channels/8);
521 //Get source info
522 DataPosInt = ALSource->position;
523 DataPosFrac = ALSource->position_fraction;
525 //Compute 18.14 fixed point step
526 increment = aluF2L(Pitch*(1L<<FRACTIONBITS));
527 if(increment > (MAX_PITCH<<FRACTIONBITS))
528 increment = (MAX_PITCH<<FRACTIONBITS);
530 //Figure out how many samples we can mix.
531 //Pitch must be <= 4 (the number below !)
532 DataSize64 = DataSize+MAX_PITCH;
533 DataSize64 <<= FRACTIONBITS;
534 DataPos64 = DataPosInt;
535 DataPos64 <<= FRACTIONBITS;
536 DataPos64 += DataPosFrac;
537 BufferSize = (ALuint)((DataSize64-DataPos64) / increment);
538 BufferListItem = ALSource->queue;
539 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
541 if(BufferListItem)
542 BufferListItem = BufferListItem->next;
544 if (BufferListItem)
546 if (BufferListItem->next)
548 if(BufferListItem->next->buffer &&
549 ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data)
551 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->size, (ALint)(16*Channels));
552 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data, ulExtraSamples);
555 else if (ALSource->bLooping)
557 if (ALSource->queue->buffer)
559 if(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data)
561 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->size, (ALint)(16*Channels));
562 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data, ulExtraSamples);
567 BufferSize = min(BufferSize, (SamplesToDo-j));
569 //Actual sample mixing loop
570 Data += DataPosInt*Channels;
571 while(BufferSize--)
573 k = DataPosFrac>>FRACTIONBITS;
574 fraction = DataPosFrac&FRACTIONMASK;
575 if(Channels==1)
577 //First order interpolator
578 ALfloat sample = (ALfloat)((ALshort)(((Data[k]*((1L<<FRACTIONBITS)-fraction))+(Data[k+1]*(fraction)))>>FRACTIONBITS));
580 //Direct path final mix buffer and panning
581 value = aluComputeDrySample(ALSource, DryGainHF, sample);
582 DryBuffer[j][0] += value*DrySend[0];
583 DryBuffer[j][1] += value*DrySend[1];
584 DryBuffer[j][2] += value*DrySend[2];
585 DryBuffer[j][3] += value*DrySend[3];
587 if(ALSource->Send[0].Slot.effectslot)
589 //Room path final mix buffer and panning
590 value = aluComputeWetSample(ALSource, WetGainHF, sample);
591 WetBuffer[j][0] += value*WetSend[0];
592 WetBuffer[j][1] += value*WetSend[1];
593 WetBuffer[j][2] += value*WetSend[2];
594 WetBuffer[j][3] += value*WetSend[3];
597 else
599 //First order interpolator (left)
600 value = (ALfloat)((ALshort)(((Data[k*2 ]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+2]*(fraction)))>>FRACTIONBITS));
601 //Direct path final mix buffer and panning (left)
602 DryBuffer[j][0] += value*DrySend[0];
603 //Room path final mix buffer and panning (left)
604 WetBuffer[j][0] += value*WetSend[0];
605 //First order interpolator (right)
606 value = (ALfloat)((ALshort)(((Data[k*2+1]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+3]*(fraction)))>>FRACTIONBITS));
607 //Direct path final mix buffer and panning (right)
608 DryBuffer[j][1] += value*DrySend[1];
609 //Room path final mix buffer and panning (right)
610 WetBuffer[j][1] += value*WetSend[1];
612 DataPosFrac += increment;
613 j++;
615 DataPosInt += (DataPosFrac>>FRACTIONBITS);
616 DataPosFrac = (DataPosFrac&FRACTIONMASK);
618 //Update source info
619 ALSource->position = DataPosInt;
620 ALSource->position_fraction = DataPosFrac;
623 //Handle looping sources
624 if(!Buffer || DataPosInt >= DataSize)
626 //queueing
627 if(ALSource->queue)
629 Looping = ALSource->bLooping;
630 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
632 BufferListItem = ALSource->queue;
633 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
635 if(BufferListItem)
637 if(!Looping)
638 BufferListItem->bufferstate = PROCESSED;
639 BufferListItem = BufferListItem->next;
642 if(!Looping)
643 ALSource->BuffersProcessed++;
644 if(BufferListItem)
645 ALSource->ulBufferID = BufferListItem->buffer;
646 ALSource->position = DataPosInt-DataSize;
647 ALSource->position_fraction = DataPosFrac;
648 ALSource->BuffersPlayed++;
650 else
652 if(!Looping)
654 /* alSourceStop */
655 ALSource->state = AL_STOPPED;
656 ALSource->inuse = AL_FALSE;
657 ALSource->BuffersPlayed = ALSource->BuffersProcessed = ALSource->BuffersInQueue;
658 BufferListItem = ALSource->queue;
659 while(BufferListItem != NULL)
661 BufferListItem->bufferstate = PROCESSED;
662 BufferListItem = BufferListItem->next;
665 else
667 /* alSourceRewind */
668 /* alSourcePlay */
669 ALSource->state = AL_PLAYING;
670 ALSource->inuse = AL_TRUE;
671 ALSource->play = AL_TRUE;
672 ALSource->BuffersPlayed = 0;
673 ALSource->BufferPosition = 0;
674 ALSource->lBytesPlayed = 0;
675 ALSource->BuffersProcessed = 0;
676 BufferListItem = ALSource->queue;
677 while(BufferListItem != NULL)
679 BufferListItem->bufferstate = PENDING;
680 BufferListItem = BufferListItem->next;
682 ALSource->ulBufferID = ALSource->queue->buffer;
684 ALSource->position = DataPosInt-DataSize;
685 ALSource->position_fraction = DataPosFrac;
691 //Get source state
692 State = ALSource->state;
695 ALSource = ALSource->next;
698 //Post processing loop
699 switch(format)
701 case AL_FORMAT_MONO8:
702 for(i = 0;i < SamplesToDo;i++)
704 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1])>>8)+128);
705 buffer = ((ALubyte*)buffer) + 1;
707 break;
708 case AL_FORMAT_STEREO8:
709 for(i = 0;i < SamplesToDo*2;i++)
711 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1])>>8)+128);
712 buffer = ((ALubyte*)buffer) + 1;
714 break;
715 case AL_FORMAT_QUAD8:
716 for(i = 0;i < SamplesToDo*4;i++)
718 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3])>>8)+128);
719 buffer = ((ALubyte*)buffer) + 1;
721 break;
722 case AL_FORMAT_MONO16:
723 for(i = 0;i < SamplesToDo;i++)
725 *((ALshort*)buffer) = aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1]);
726 buffer = ((ALshort*)buffer) + 1;
728 break;
729 case AL_FORMAT_STEREO16:
730 default:
731 for(i = 0;i < SamplesToDo*2;i++)
733 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1]);
734 buffer = ((ALshort*)buffer) + 1;
736 break;
737 case AL_FORMAT_QUAD16:
738 for(i = 0;i < SamplesToDo*4;i++)
740 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3]);
741 buffer = ((ALshort*)buffer) + 1;
743 break;
746 size -= SamplesToDo;
750 ProcessContext(ALContext);