More casts
[openal-soft.git] / Alc / ALu.c
blobcee00dd0f8e01468208d0e0678cc663406dbe057
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 ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
154 ALenum isMono, ALenum OutputFormat,
155 ALfloat *drysend, ALfloat *wetsend,
156 ALfloat *pitch)
158 ALfloat ListenerOrientation[6],ListenerPosition[3],ListenerVelocity[3];
159 ALfloat InnerAngle,OuterAngle,OuterGain,Angle,Distance,DryMix,WetMix;
160 ALfloat Direction[3],Position[3],Velocity[3],SourceToListener[3];
161 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
162 ALfloat Pitch,ConeVolume,SourceVolume,PanningFB,PanningLR,ListenerGain;
163 ALfloat U[3],V[3],N[3];
164 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
165 ALfloat flVSS, flVLS;
166 ALint DistanceModel;
167 ALfloat Matrix[3][3];
168 ALint HeadRelative;
169 ALfloat flAttenuation;
171 //Get context properties
172 DopplerFactor = ALContext->DopplerFactor;
173 DistanceModel = ALContext->DistanceModel;
174 DopplerVelocity = ALContext->DopplerVelocity;
175 flSpeedOfSound = ALContext->flSpeedOfSound;
177 //Get listener properties
178 ListenerGain = ALContext->Listener.Gain;
179 memcpy(ListenerPosition, ALContext->Listener.Position, sizeof(ALContext->Listener.Position));
180 memcpy(ListenerVelocity, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
181 memcpy(&ListenerOrientation[0], ALContext->Listener.Forward, sizeof(ALContext->Listener.Forward));
182 memcpy(&ListenerOrientation[3], ALContext->Listener.Up, sizeof(ALContext->Listener.Up));
184 //Get source properties
185 Pitch = ALSource->flPitch;
186 SourceVolume = ALSource->flGain;
187 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
188 memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
189 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
190 MinVolume = ALSource->flMinGain;
191 MaxVolume = ALSource->flMaxGain;
192 MinDist = ALSource->flRefDistance;
193 MaxDist = ALSource->flMaxDistance;
194 Rolloff = ALSource->flRollOffFactor;
195 OuterGain = ALSource->flOuterGain;
196 InnerAngle = ALSource->flInnerAngle;
197 OuterAngle = ALSource->flOuterAngle;
198 HeadRelative = ALSource->bHeadRelative;
200 //Set working variables
201 DryMix = (ALfloat)(1.0f);
202 WetMix = (ALfloat)(0.0f);
204 //Only apply 3D calculations for mono buffers
205 if(isMono != AL_FALSE)
207 //1. Translate Listener to origin (convert to head relative)
208 if(HeadRelative==AL_FALSE)
210 Position[0] -= ListenerPosition[0];
211 Position[1] -= ListenerPosition[1];
212 Position[2] -= ListenerPosition[2];
215 //2. Calculate distance attenuation
216 Distance = aluSqrt(aluDotproduct(Position, Position));
218 flAttenuation = 1.0f;
219 switch (DistanceModel)
221 case AL_INVERSE_DISTANCE_CLAMPED:
222 Distance=__max(Distance,MinDist);
223 Distance=__min(Distance,MaxDist);
224 if (MaxDist < MinDist)
225 break;
226 //fall-through
227 case AL_INVERSE_DISTANCE:
228 if (MinDist > 0.0f)
230 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
231 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
233 break;
235 case AL_LINEAR_DISTANCE_CLAMPED:
236 Distance=__max(Distance,MinDist);
237 Distance=__min(Distance,MaxDist);
238 if (MaxDist < MinDist)
239 break;
240 //fall-through
241 case AL_LINEAR_DISTANCE:
242 Distance=__min(Distance,MaxDist);
243 if (MaxDist != MinDist)
244 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
245 break;
247 case AL_EXPONENT_DISTANCE_CLAMPED:
248 Distance=__max(Distance,MinDist);
249 Distance=__min(Distance,MaxDist);
250 if (MaxDist < MinDist)
251 break;
252 //fall-through
253 case AL_EXPONENT_DISTANCE:
254 if ((Distance > 0.0f) && (MinDist > 0.0f))
255 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
256 break;
258 case AL_NONE:
259 default:
260 flAttenuation = 1.0f;
261 break;
264 // Source Gain + Attenuation
265 DryMix = SourceVolume * flAttenuation;
267 // Clamp to Min/Max Gain
268 DryMix = __min(DryMix,MaxVolume);
269 DryMix = __max(DryMix,MinVolume);
270 WetMix = __min(WetMix,MaxVolume);
271 WetMix = __max(WetMix,MinVolume);
272 //3. Apply directional soundcones
273 SourceToListener[0] = -Position[0];
274 SourceToListener[1] = -Position[1];
275 SourceToListener[2] = -Position[2];
276 aluNormalize(Direction);
277 aluNormalize(SourceToListener);
278 Angle = (ALfloat)(180.0*acos(aluDotproduct(Direction,SourceToListener))/3.141592654f);
279 if(Angle >= InnerAngle && Angle <= OuterAngle)
280 ConeVolume = (1.0f+(OuterGain-1.0f)*(Angle-InnerAngle)/(OuterAngle-InnerAngle));
281 else if(Angle > OuterAngle)
282 ConeVolume = (1.0f+(OuterGain-1.0f) );
283 else
284 ConeVolume = 1.0f;
286 //4. Calculate Velocity
287 if(DopplerFactor != 0.0f)
289 flVLS = aluDotproduct(ListenerVelocity, SourceToListener);
290 flVSS = aluDotproduct(Velocity, SourceToListener);
292 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
294 if (flVSS >= flMaxVelocity)
295 flVSS = (flMaxVelocity - 1.0f);
296 else if (flVSS <= -flMaxVelocity)
297 flVSS = -flMaxVelocity + 1.0f;
299 if (flVLS >= flMaxVelocity)
300 flVLS = (flMaxVelocity - 1.0f);
301 else if (flVLS <= -flMaxVelocity)
302 flVLS = -flMaxVelocity + 1.0f;
304 pitch[0] = Pitch * ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
305 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
307 else
308 pitch[0] = Pitch;
310 //5. Align coordinate system axes
311 aluCrossproduct(&ListenerOrientation[0], &ListenerOrientation[3], U); // Right-vector
312 aluNormalize(U); // Normalized Right-vector
313 memcpy(V, &ListenerOrientation[3], sizeof(V)); // Up-vector
314 aluNormalize(V); // Normalized Up-vector
315 memcpy(N, &ListenerOrientation[0], sizeof(N)); // At-vector
316 aluNormalize(N); // Normalized At-vector
317 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
318 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
319 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
320 aluMatrixVector(Position, Matrix);
322 //6. Convert normalized position into left/right front/back pannings
323 if(Distance != 0.0f)
325 aluNormalize(Position);
326 PanningLR = 0.5f + 0.5f*Position[0];
327 PanningFB = 0.5f + 0.5f*Position[2];
329 else
331 PanningLR = 0.5f;
332 PanningFB = 0.5f;
335 //7. Convert pannings into channel volumes
336 switch(OutputFormat)
338 case AL_FORMAT_MONO8:
339 case AL_FORMAT_MONO16:
340 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
341 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
342 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
343 wetsend[1] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
344 break;
345 case AL_FORMAT_STEREO8:
346 case AL_FORMAT_STEREO16:
347 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f-PanningLR); //L Direct
348 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt( PanningLR); //R Direct
349 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f-PanningLR); //L Room
350 wetsend[1] = ListenerGain * WetMix * aluSqrt( PanningLR); //R Room
351 break;
352 case AL_FORMAT_QUAD8:
353 case AL_FORMAT_QUAD16:
354 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Direct
355 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Direct
356 drysend[2] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Direct
357 drysend[3] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Direct
358 wetsend[0] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Room
359 wetsend[1] = ListenerGain * WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Room
360 wetsend[2] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Room
361 wetsend[3] = ListenerGain * WetMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Room
362 break;
363 default:
364 break;
367 else
369 //1. Multi-channel buffers always play "normal"
370 drysend[0] = SourceVolume * 1.0f * ListenerGain;
371 drysend[1] = SourceVolume * 1.0f * ListenerGain;
372 drysend[2] = SourceVolume * 1.0f * ListenerGain;
373 drysend[3] = SourceVolume * 1.0f * ListenerGain;
374 wetsend[0] = SourceVolume * 0.0f * ListenerGain;
375 wetsend[1] = SourceVolume * 0.0f * ListenerGain;
376 wetsend[2] = SourceVolume * 0.0f * ListenerGain;
377 wetsend[3] = SourceVolume * 0.0f * ListenerGain;
379 pitch[0] = Pitch;
383 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
385 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
386 static float WetBuffer[BUFFERSIZE][OUTPUTCHANNELS];
387 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f };
388 ALfloat WetSend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f };
389 ALuint BlockAlign,BufferSize;
390 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
391 ALuint Channels,Bits,Frequency,ulExtraSamples;
392 ALfloat Pitch;
393 ALint Looping,increment,State;
394 ALuint Buffer,fraction;
395 ALuint SamplesToDo;
396 ALsource *ALSource;
397 ALbuffer *ALBuffer;
398 ALfloat value;
399 ALshort *Data;
400 ALuint i,j,k;
401 ALbufferlistitem *BufferListItem;
402 ALuint loop;
403 ALint64 DataSize64,DataPos64;
405 SuspendContext(ALContext);
407 if(buffer)
409 //Figure output format variables
410 BlockAlign = aluChannelsFromFormat(format) *
411 aluBytesFromFormat(format);
413 size /= BlockAlign;
414 while(size > 0)
416 //Setup variables
417 ALSource = (ALContext ? ALContext->Source : NULL);
418 SamplesToDo = min(size, BUFFERSIZE);
420 //Clear mixing buffer
421 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
422 memset(WetBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
424 //Actual mixing loop
425 while(ALSource)
427 j = 0;
428 State = ALSource->state;
429 while(State == AL_PLAYING && j < SamplesToDo)
431 DataSize = 0;
432 DataPosInt = 0;
433 DataPosFrac = 0;
435 //Get buffer info
436 if((Buffer = ALSource->ulBufferID))
438 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
440 Data = ALBuffer->data;
441 Bits = aluBytesFromFormat(ALBuffer->format) * 8;
442 Channels = aluChannelsFromFormat(ALBuffer->format);
443 DataSize = ALBuffer->size;
444 Frequency = ALBuffer->frequency;
446 CalcSourceParams(ALContext, ALSource,
447 (Channels==1) ? AL_TRUE : AL_FALSE,
448 format, DrySend, WetSend, &Pitch);
451 Pitch = (Pitch*Frequency) / ALContext->Frequency;
452 DataSize = DataSize / (Bits*Channels/8);
454 //Get source info
455 DataPosInt = ALSource->position;
456 DataPosFrac = ALSource->position_fraction;
458 //Compute 18.14 fixed point step
459 increment = aluF2L(Pitch*(1L<<FRACTIONBITS));
460 if(increment > (MAX_PITCH<<FRACTIONBITS))
461 increment = (MAX_PITCH<<FRACTIONBITS);
463 //Figure out how many samples we can mix.
464 //Pitch must be <= 4 (the number below !)
465 DataSize64 = DataSize+MAX_PITCH;
466 DataSize64 <<= FRACTIONBITS;
467 DataPos64 = DataPosInt;
468 DataPos64 <<= FRACTIONBITS;
469 DataPos64 += DataPosFrac;
470 BufferSize = (ALuint)((DataSize64-DataPos64) / increment);
471 BufferListItem = ALSource->queue;
472 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
474 if(BufferListItem)
475 BufferListItem = BufferListItem->next;
477 if (BufferListItem)
479 if (BufferListItem->next)
481 if(BufferListItem->next->buffer &&
482 ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data)
484 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->size, (ALint)(16*Channels));
485 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data, ulExtraSamples);
488 else if (ALSource->bLooping)
490 if (ALSource->queue->buffer)
492 if(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data)
494 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->size, (ALint)(16*Channels));
495 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data, ulExtraSamples);
500 BufferSize = min(BufferSize, (SamplesToDo-j));
502 //Actual sample mixing loop
503 Data += DataPosInt*Channels;
504 while(BufferSize--)
506 k = DataPosFrac>>FRACTIONBITS;
507 fraction = DataPosFrac&FRACTIONMASK;
508 if(Channels==1)
510 //First order interpolator
511 value = (ALfloat)((ALshort)(((Data[k]*((1L<<FRACTIONBITS)-fraction))+(Data[k+1]*(fraction)))>>FRACTIONBITS));
512 //Direct path final mix buffer and panning
513 DryBuffer[j][0] += value*DrySend[0];
514 DryBuffer[j][1] += value*DrySend[1];
515 DryBuffer[j][2] += value*DrySend[2];
516 DryBuffer[j][3] += value*DrySend[3];
517 //Room path final mix buffer and panning
518 WetBuffer[j][0] += value*WetSend[0];
519 WetBuffer[j][1] += value*WetSend[1];
520 WetBuffer[j][2] += value*WetSend[2];
521 WetBuffer[j][3] += value*WetSend[3];
523 else
525 //First order interpolator (left)
526 value = (ALfloat)((ALshort)(((Data[k*2 ]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+2]*(fraction)))>>FRACTIONBITS));
527 //Direct path final mix buffer and panning (left)
528 DryBuffer[j][0] += value*DrySend[0];
529 //Room path final mix buffer and panning (left)
530 WetBuffer[j][0] += value*WetSend[0];
531 //First order interpolator (right)
532 value = (ALfloat)((ALshort)(((Data[k*2+1]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+3]*(fraction)))>>FRACTIONBITS));
533 //Direct path final mix buffer and panning (right)
534 DryBuffer[j][1] += value*DrySend[1];
535 //Room path final mix buffer and panning (right)
536 WetBuffer[j][1] += value*WetSend[1];
538 DataPosFrac += increment;
539 j++;
541 DataPosInt += (DataPosFrac>>FRACTIONBITS);
542 DataPosFrac = (DataPosFrac&FRACTIONMASK);
544 //Update source info
545 ALSource->position = DataPosInt;
546 ALSource->position_fraction = DataPosFrac;
549 //Handle looping sources
550 if(!Buffer || DataPosInt >= DataSize)
552 //queueing
553 if(ALSource->queue)
555 Looping = ALSource->bLooping;
556 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
558 BufferListItem = ALSource->queue;
559 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
561 if(BufferListItem)
563 if(!Looping)
564 BufferListItem->bufferstate = PROCESSED;
565 BufferListItem = BufferListItem->next;
568 if(!Looping)
569 ALSource->BuffersProcessed++;
570 if(BufferListItem)
571 ALSource->ulBufferID = BufferListItem->buffer;
572 ALSource->position = DataPosInt-DataSize;
573 ALSource->position_fraction = DataPosFrac;
574 ALSource->BuffersPlayed++;
576 else
578 if(!Looping)
580 /* alSourceStop */
581 ALSource->state = AL_STOPPED;
582 ALSource->inuse = AL_FALSE;
583 ALSource->BuffersPlayed = ALSource->BuffersProcessed = ALSource->BuffersInQueue;
584 BufferListItem = ALSource->queue;
585 while(BufferListItem != NULL)
587 BufferListItem->bufferstate = PROCESSED;
588 BufferListItem = BufferListItem->next;
591 else
593 /* alSourceRewind */
594 /* alSourcePlay */
595 ALSource->state = AL_PLAYING;
596 ALSource->inuse = AL_TRUE;
597 ALSource->play = AL_TRUE;
598 ALSource->BuffersPlayed = 0;
599 ALSource->BufferPosition = 0;
600 ALSource->lBytesPlayed = 0;
601 ALSource->BuffersProcessed = 0;
602 BufferListItem = ALSource->queue;
603 while(BufferListItem != NULL)
605 BufferListItem->bufferstate = PENDING;
606 BufferListItem = BufferListItem->next;
608 ALSource->ulBufferID = ALSource->queue->buffer;
610 ALSource->position = DataPosInt-DataSize;
611 ALSource->position_fraction = DataPosFrac;
617 //Get source state
618 State = ALSource->state;
621 ALSource = ALSource->next;
624 //Post processing loop
625 switch(format)
627 case AL_FORMAT_MONO8:
628 for(i = 0;i < SamplesToDo;i++)
630 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1])>>8)+128);
631 buffer = ((ALubyte*)buffer) + 1;
633 break;
634 case AL_FORMAT_STEREO8:
635 for(i = 0;i < SamplesToDo*2;i++)
637 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1])>>8)+128);
638 buffer = ((ALubyte*)buffer) + 1;
640 break;
641 case AL_FORMAT_QUAD8:
642 for(i = 0;i < SamplesToDo*4;i++)
644 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3])>>8)+128);
645 buffer = ((ALubyte*)buffer) + 1;
647 break;
648 case AL_FORMAT_MONO16:
649 for(i = 0;i < SamplesToDo;i++)
651 *((ALshort*)buffer) = aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1]);
652 buffer = ((ALshort*)buffer) + 1;
654 break;
655 case AL_FORMAT_STEREO16:
656 default:
657 for(i = 0;i < SamplesToDo*2;i++)
659 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1]);
660 buffer = ((ALshort*)buffer) + 1;
662 break;
663 case AL_FORMAT_QUAD16:
664 for(i = 0;i < SamplesToDo*4;i++)
666 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3]);
667 buffer = ((ALshort*)buffer) + 1;
669 break;
672 size -= SamplesToDo;
676 ProcessContext(ALContext);