Implement basic lowpass filter path
[openal-soft.git] / Alc / ALu.c
blob63de05056448b5ecafca2639fc51ef314a0c26a1
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;
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, WetGainHF;
198 //Get context properties
199 DopplerFactor = ALContext->DopplerFactor;
200 DistanceModel = ALContext->DistanceModel;
201 DopplerVelocity = ALContext->DopplerVelocity;
202 flSpeedOfSound = ALContext->flSpeedOfSound;
204 //Get listener properties
205 ListenerGain = ALContext->Listener.Gain;
206 MetersPerUnit = ALContext->Listener.MetersPerUnit;
207 memcpy(ListenerPosition, ALContext->Listener.Position, sizeof(ALContext->Listener.Position));
208 memcpy(ListenerVelocity, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
209 memcpy(&ListenerOrientation[0], ALContext->Listener.Forward, sizeof(ALContext->Listener.Forward));
210 memcpy(&ListenerOrientation[3], ALContext->Listener.Up, sizeof(ALContext->Listener.Up));
212 //Get source properties
213 Pitch = ALSource->flPitch;
214 SourceVolume = ALSource->flGain;
215 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
216 memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
217 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
218 MinVolume = ALSource->flMinGain;
219 MaxVolume = ALSource->flMaxGain;
220 MinDist = ALSource->flRefDistance;
221 MaxDist = ALSource->flMaxDistance;
222 Rolloff = ALSource->flRollOffFactor;
223 OuterGain = ALSource->flOuterGain;
224 InnerAngle = ALSource->flInnerAngle;
225 OuterAngle = ALSource->flOuterAngle;
226 HeadRelative = ALSource->bHeadRelative;
228 //Set working variables
229 DryMix = (ALfloat)(1.0f);
230 WetMix = (ALfloat)(0.0f);
232 //Only apply 3D calculations for mono buffers
233 if(isMono != AL_FALSE)
235 //1. Translate Listener to origin (convert to head relative)
236 if(HeadRelative==AL_FALSE)
238 Position[0] -= ListenerPosition[0];
239 Position[1] -= ListenerPosition[1];
240 Position[2] -= ListenerPosition[2];
243 //2. Calculate distance attenuation
244 Distance = aluSqrt(aluDotproduct(Position, Position));
246 flAttenuation = 1.0f;
247 switch (DistanceModel)
249 case AL_INVERSE_DISTANCE_CLAMPED:
250 Distance=__max(Distance,MinDist);
251 Distance=__min(Distance,MaxDist);
252 if (MaxDist < MinDist)
253 break;
254 //fall-through
255 case AL_INVERSE_DISTANCE:
256 if (MinDist > 0.0f)
258 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
259 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
261 break;
263 case AL_LINEAR_DISTANCE_CLAMPED:
264 Distance=__max(Distance,MinDist);
265 Distance=__min(Distance,MaxDist);
266 if (MaxDist < MinDist)
267 break;
268 //fall-through
269 case AL_LINEAR_DISTANCE:
270 Distance=__min(Distance,MaxDist);
271 if (MaxDist != MinDist)
272 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
273 break;
275 case AL_EXPONENT_DISTANCE_CLAMPED:
276 Distance=__max(Distance,MinDist);
277 Distance=__min(Distance,MaxDist);
278 if (MaxDist < MinDist)
279 break;
280 //fall-through
281 case AL_EXPONENT_DISTANCE:
282 if ((Distance > 0.0f) && (MinDist > 0.0f))
283 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
284 break;
286 case AL_NONE:
287 default:
288 flAttenuation = 1.0f;
289 break;
292 // Source Gain + Attenuation
293 DryMix = SourceVolume * flAttenuation;
295 // Clamp to Min/Max Gain
296 DryMix = __min(DryMix,MaxVolume);
297 DryMix = __max(DryMix,MinVolume);
298 WetMix = __min(WetMix,MaxVolume);
299 WetMix = __max(WetMix,MinVolume);
300 //3. Apply directional soundcones
301 DryGainHF = 1.0f;
302 WetGainHF = 1.0f;
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)
310 ConeVolume = (1.0f+(OuterGain-1.0f)*(Angle-InnerAngle)/(OuterAngle-InnerAngle));
311 else if(Angle > OuterAngle)
312 ConeVolume = (1.0f+(OuterGain-1.0f) );
313 else
314 ConeVolume = 1.0f;
316 //4. Calculate Velocity
317 if(DopplerFactor != 0.0f)
319 flVLS = aluDotproduct(ListenerVelocity, SourceToListener);
320 flVSS = aluDotproduct(Velocity, SourceToListener);
322 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
324 if (flVSS >= flMaxVelocity)
325 flVSS = (flMaxVelocity - 1.0f);
326 else if (flVSS <= -flMaxVelocity)
327 flVSS = -flMaxVelocity + 1.0f;
329 if (flVLS >= flMaxVelocity)
330 flVLS = (flMaxVelocity - 1.0f);
331 else if (flVLS <= -flMaxVelocity)
332 flVLS = -flMaxVelocity + 1.0f;
334 pitch[0] = Pitch * ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
335 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
337 else
338 pitch[0] = Pitch;
340 //5. Align coordinate system axes
341 aluCrossproduct(&ListenerOrientation[0], &ListenerOrientation[3], U); // Right-vector
342 aluNormalize(U); // Normalized Right-vector
343 memcpy(V, &ListenerOrientation[3], sizeof(V)); // Up-vector
344 aluNormalize(V); // Normalized Up-vector
345 memcpy(N, &ListenerOrientation[0], sizeof(N)); // At-vector
346 aluNormalize(N); // Normalized At-vector
347 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
348 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
349 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
350 aluMatrixVector(Position, Matrix);
352 //6. Convert normalized position into left/right front/back pannings
353 if(Distance != 0.0f)
355 aluNormalize(Position);
356 PanningLR = 0.5f + 0.5f*Position[0];
357 PanningFB = 0.5f + 0.5f*Position[2];
359 else
361 PanningLR = 0.5f;
362 PanningFB = 0.5f;
365 *drygainhf = DryGainHF;
366 *wetgainhf = WetGainHF;
368 //7. Convert pannings into channel volumes
369 switch(OutputFormat)
371 case AL_FORMAT_MONO8:
372 case AL_FORMAT_MONO16:
373 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
374 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
375 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
376 wetsend[1] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
377 break;
378 case AL_FORMAT_STEREO8:
379 case AL_FORMAT_STEREO16:
380 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f-PanningLR); //L Direct
381 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt( PanningLR); //R Direct
382 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f-PanningLR); //L Room
383 wetsend[1] = ListenerGain * WetMix * aluSqrt( PanningLR); //R Room
384 break;
385 case AL_FORMAT_QUAD8:
386 case AL_FORMAT_QUAD16:
387 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Direct
388 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Direct
389 drysend[2] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Direct
390 drysend[3] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Direct
391 wetsend[0] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Room
392 wetsend[1] = ListenerGain * WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Room
393 wetsend[2] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Room
394 wetsend[3] = ListenerGain * WetMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Room
395 break;
396 default:
397 break;
400 else
402 //1. Multi-channel buffers always play "normal"
403 drysend[0] = SourceVolume * 1.0f * ListenerGain;
404 drysend[1] = SourceVolume * 1.0f * ListenerGain;
405 drysend[2] = SourceVolume * 1.0f * ListenerGain;
406 drysend[3] = SourceVolume * 1.0f * ListenerGain;
407 wetsend[0] = SourceVolume * 0.0f * ListenerGain;
408 wetsend[1] = SourceVolume * 0.0f * ListenerGain;
409 wetsend[2] = SourceVolume * 0.0f * ListenerGain;
410 wetsend[3] = SourceVolume * 0.0f * ListenerGain;
412 pitch[0] = Pitch;
414 *drygainhf = 1.0;
415 *wetgainhf = 1.0;
419 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
421 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
422 static float WetBuffer[BUFFERSIZE][OUTPUTCHANNELS];
423 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f };
424 ALfloat WetSend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f };
425 ALfloat DryGainHF = 0.0f;
426 ALfloat WetGainHF = 0.0f;
427 ALuint BlockAlign,BufferSize;
428 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
429 ALuint Channels,Bits,Frequency,ulExtraSamples;
430 ALfloat Pitch;
431 ALint Looping,increment,State;
432 ALuint Buffer,fraction;
433 ALuint SamplesToDo;
434 ALsource *ALSource;
435 ALbuffer *ALBuffer;
436 ALfloat value;
437 ALshort *Data;
438 ALuint i,j,k;
439 ALbufferlistitem *BufferListItem;
440 ALuint loop;
441 ALint64 DataSize64,DataPos64;
443 SuspendContext(ALContext);
445 if(buffer)
447 //Figure output format variables
448 BlockAlign = aluChannelsFromFormat(format);
449 BlockAlign *= aluBytesFromFormat(format);
451 size /= BlockAlign;
452 while(size > 0)
454 //Setup variables
455 ALSource = (ALContext ? ALContext->Source : NULL);
456 SamplesToDo = min(size, BUFFERSIZE);
458 //Clear mixing buffer
459 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
460 memset(WetBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
462 //Actual mixing loop
463 while(ALSource)
465 j = 0;
466 State = ALSource->state;
467 while(State == AL_PLAYING && j < SamplesToDo)
469 DataSize = 0;
470 DataPosInt = 0;
471 DataPosFrac = 0;
473 //Get buffer info
474 if((Buffer = ALSource->ulBufferID))
476 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
478 Data = ALBuffer->data;
479 Bits = aluBytesFromFormat(ALBuffer->format) * 8;
480 Channels = aluChannelsFromFormat(ALBuffer->format);
481 DataSize = ALBuffer->size;
482 Frequency = ALBuffer->frequency;
484 CalcSourceParams(ALContext, ALSource,
485 (Channels==1) ? AL_TRUE : AL_FALSE,
486 format, DrySend, WetSend, &Pitch,
487 &DryGainHF, &WetGainHF);
490 Pitch = (Pitch*Frequency) / ALContext->Frequency;
491 DataSize = DataSize / (Bits*Channels/8);
493 //Get source info
494 DataPosInt = ALSource->position;
495 DataPosFrac = ALSource->position_fraction;
497 //Compute 18.14 fixed point step
498 increment = aluF2L(Pitch*(1L<<FRACTIONBITS));
499 if(increment > (MAX_PITCH<<FRACTIONBITS))
500 increment = (MAX_PITCH<<FRACTIONBITS);
502 //Figure out how many samples we can mix.
503 //Pitch must be <= 4 (the number below !)
504 DataSize64 = DataSize+MAX_PITCH;
505 DataSize64 <<= FRACTIONBITS;
506 DataPos64 = DataPosInt;
507 DataPos64 <<= FRACTIONBITS;
508 DataPos64 += DataPosFrac;
509 BufferSize = (ALuint)((DataSize64-DataPos64) / increment);
510 BufferListItem = ALSource->queue;
511 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
513 if(BufferListItem)
514 BufferListItem = BufferListItem->next;
516 if (BufferListItem)
518 if (BufferListItem->next)
520 if(BufferListItem->next->buffer &&
521 ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data)
523 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->size, (ALint)(16*Channels));
524 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data, ulExtraSamples);
527 else if (ALSource->bLooping)
529 if (ALSource->queue->buffer)
531 if(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data)
533 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->size, (ALint)(16*Channels));
534 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data, ulExtraSamples);
539 BufferSize = min(BufferSize, (SamplesToDo-j));
541 //Actual sample mixing loop
542 Data += DataPosInt*Channels;
543 while(BufferSize--)
545 k = DataPosFrac>>FRACTIONBITS;
546 fraction = DataPosFrac&FRACTIONMASK;
547 if(Channels==1)
549 //First order interpolator
550 ALfloat sample = (ALfloat)((ALshort)(((Data[k]*((1L<<FRACTIONBITS)-fraction))+(Data[k+1]*(fraction)))>>FRACTIONBITS));
552 //Direct path final mix buffer and panning
553 value = aluComputeDrySample(ALSource, DryGainHF, sample);
554 DryBuffer[j][0] += value*DrySend[0];
555 DryBuffer[j][1] += value*DrySend[1];
556 DryBuffer[j][2] += value*DrySend[2];
557 DryBuffer[j][3] += value*DrySend[3];
559 //Room path final mix buffer and panning
560 value = aluComputeWetSample(ALSource, WetGainHF, sample);
561 WetBuffer[j][0] += value*WetSend[0];
562 WetBuffer[j][1] += value*WetSend[1];
563 WetBuffer[j][2] += value*WetSend[2];
564 WetBuffer[j][3] += value*WetSend[3];
566 else
568 //First order interpolator (left)
569 value = (ALfloat)((ALshort)(((Data[k*2 ]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+2]*(fraction)))>>FRACTIONBITS));
570 //Direct path final mix buffer and panning (left)
571 DryBuffer[j][0] += value*DrySend[0];
572 //Room path final mix buffer and panning (left)
573 WetBuffer[j][0] += value*WetSend[0];
574 //First order interpolator (right)
575 value = (ALfloat)((ALshort)(((Data[k*2+1]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+3]*(fraction)))>>FRACTIONBITS));
576 //Direct path final mix buffer and panning (right)
577 DryBuffer[j][1] += value*DrySend[1];
578 //Room path final mix buffer and panning (right)
579 WetBuffer[j][1] += value*WetSend[1];
581 DataPosFrac += increment;
582 j++;
584 DataPosInt += (DataPosFrac>>FRACTIONBITS);
585 DataPosFrac = (DataPosFrac&FRACTIONMASK);
587 //Update source info
588 ALSource->position = DataPosInt;
589 ALSource->position_fraction = DataPosFrac;
592 //Handle looping sources
593 if(!Buffer || DataPosInt >= DataSize)
595 //queueing
596 if(ALSource->queue)
598 Looping = ALSource->bLooping;
599 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
601 BufferListItem = ALSource->queue;
602 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
604 if(BufferListItem)
606 if(!Looping)
607 BufferListItem->bufferstate = PROCESSED;
608 BufferListItem = BufferListItem->next;
611 if(!Looping)
612 ALSource->BuffersProcessed++;
613 if(BufferListItem)
614 ALSource->ulBufferID = BufferListItem->buffer;
615 ALSource->position = DataPosInt-DataSize;
616 ALSource->position_fraction = DataPosFrac;
617 ALSource->BuffersPlayed++;
619 else
621 if(!Looping)
623 /* alSourceStop */
624 ALSource->state = AL_STOPPED;
625 ALSource->inuse = AL_FALSE;
626 ALSource->BuffersPlayed = ALSource->BuffersProcessed = ALSource->BuffersInQueue;
627 BufferListItem = ALSource->queue;
628 while(BufferListItem != NULL)
630 BufferListItem->bufferstate = PROCESSED;
631 BufferListItem = BufferListItem->next;
634 else
636 /* alSourceRewind */
637 /* alSourcePlay */
638 ALSource->state = AL_PLAYING;
639 ALSource->inuse = AL_TRUE;
640 ALSource->play = AL_TRUE;
641 ALSource->BuffersPlayed = 0;
642 ALSource->BufferPosition = 0;
643 ALSource->lBytesPlayed = 0;
644 ALSource->BuffersProcessed = 0;
645 BufferListItem = ALSource->queue;
646 while(BufferListItem != NULL)
648 BufferListItem->bufferstate = PENDING;
649 BufferListItem = BufferListItem->next;
651 ALSource->ulBufferID = ALSource->queue->buffer;
653 ALSource->position = DataPosInt-DataSize;
654 ALSource->position_fraction = DataPosFrac;
660 //Get source state
661 State = ALSource->state;
664 ALSource = ALSource->next;
667 //Post processing loop
668 switch(format)
670 case AL_FORMAT_MONO8:
671 for(i = 0;i < SamplesToDo;i++)
673 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1])>>8)+128);
674 buffer = ((ALubyte*)buffer) + 1;
676 break;
677 case AL_FORMAT_STEREO8:
678 for(i = 0;i < SamplesToDo*2;i++)
680 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1])>>8)+128);
681 buffer = ((ALubyte*)buffer) + 1;
683 break;
684 case AL_FORMAT_QUAD8:
685 for(i = 0;i < SamplesToDo*4;i++)
687 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3])>>8)+128);
688 buffer = ((ALubyte*)buffer) + 1;
690 break;
691 case AL_FORMAT_MONO16:
692 for(i = 0;i < SamplesToDo;i++)
694 *((ALshort*)buffer) = aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1]);
695 buffer = ((ALshort*)buffer) + 1;
697 break;
698 case AL_FORMAT_STEREO16:
699 default:
700 for(i = 0;i < SamplesToDo*2;i++)
702 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1]);
703 buffer = ((ALshort*)buffer) + 1;
705 break;
706 case AL_FORMAT_QUAD16:
707 for(i = 0;i < SamplesToDo*4;i++)
709 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3]);
710 buffer = ((ALshort*)buffer) + 1;
712 break;
715 size -= SamplesToDo;
719 ProcessContext(ALContext);