Don't check explicitly against formats, but rather their byte/channel count
[openal-soft.git] / Alc / ALu.c
blobb5af500bf19334bf1d9bde8928176e06291a7411
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 case AL_FORMAT_51CHN8:
63 return 1;
65 case AL_FORMAT_MONO16:
66 case AL_FORMAT_STEREO16:
67 case AL_FORMAT_QUAD16:
68 case AL_FORMAT_51CHN16:
69 return 2;
71 default:
72 return 0;
76 __inline ALuint aluChannelsFromFormat(ALenum format)
78 switch(format)
80 case AL_FORMAT_MONO8:
81 case AL_FORMAT_MONO16:
82 return 1;
84 case AL_FORMAT_STEREO8:
85 case AL_FORMAT_STEREO16:
86 return 2;
88 case AL_FORMAT_QUAD8:
89 case AL_FORMAT_QUAD16:
90 return 4;
92 case AL_FORMAT_51CHN8:
93 case AL_FORMAT_51CHN16:
94 return 6;
96 default:
97 return 0;
101 static __inline ALint aluF2L(ALfloat Value)
103 if(sizeof(ALint) == 4 && sizeof(double) == 8)
105 double temp;
106 temp = Value + (((65536.0*65536.0*16.0)+(65536.0*65536.0*8.0))*65536.0);
107 return *((ALint*)&temp);
109 return (ALint)Value;
112 static __inline ALshort aluF2S(ALfloat Value)
114 ALint i;
116 i = aluF2L(Value);
117 i = __min( 32767, i);
118 i = __max(-32768, i);
119 return ((ALshort)i);
122 static __inline ALvoid aluCrossproduct(ALfloat *inVector1,ALfloat *inVector2,ALfloat *outVector)
124 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
125 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
126 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
129 static __inline ALfloat aluDotproduct(ALfloat *inVector1,ALfloat *inVector2)
131 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
132 inVector1[2]*inVector2[2];
135 static __inline ALvoid aluNormalize(ALfloat *inVector)
137 ALfloat length, inverse_length;
139 length = (ALfloat)aluSqrt(aluDotproduct(inVector, inVector));
140 if(length != 0)
142 inverse_length = 1.0f/length;
143 inVector[0] *= inverse_length;
144 inVector[1] *= inverse_length;
145 inVector[2] *= inverse_length;
149 static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat matrix[3][3])
151 ALfloat result[3];
153 result[0] = vector[0]*matrix[0][0] + vector[1]*matrix[1][0] + vector[2]*matrix[2][0];
154 result[1] = vector[0]*matrix[0][1] + vector[1]*matrix[1][1] + vector[2]*matrix[2][1];
155 result[2] = vector[0]*matrix[0][2] + vector[1]*matrix[1][2] + vector[2]*matrix[2][2];
156 memcpy(vector, result, sizeof(result));
159 static ALvoid CalcSourceParams(ALCcontext *ALContext, ALsource *ALSource,
160 ALenum isMono, ALenum OutputFormat,
161 ALfloat *drysend, ALfloat *wetsend,
162 ALfloat *pitch)
164 ALfloat ListenerOrientation[6],ListenerPosition[3],ListenerVelocity[3];
165 ALfloat InnerAngle,OuterAngle,OuterGain,Angle,Distance,DryMix,WetMix;
166 ALfloat Direction[3],Position[3],Velocity[3],SourceToListener[3];
167 ALfloat MinVolume,MaxVolume,MinDist,MaxDist,Rolloff;
168 ALfloat Pitch,ConeVolume,SourceVolume,PanningFB,PanningLR,ListenerGain;
169 ALfloat U[3],V[3],N[3];
170 ALfloat DopplerFactor, DopplerVelocity, flSpeedOfSound, flMaxVelocity;
171 ALfloat flVSS, flVLS;
172 ALint DistanceModel;
173 ALfloat Matrix[3][3];
174 ALint HeadRelative;
175 ALfloat flAttenuation;
177 //Get context properties
178 DopplerFactor = ALContext->DopplerFactor;
179 DistanceModel = ALContext->DistanceModel;
180 DopplerVelocity = ALContext->DopplerVelocity;
181 flSpeedOfSound = ALContext->flSpeedOfSound;
183 //Get listener properties
184 ListenerGain = ALContext->Listener.Gain;
185 memcpy(ListenerPosition, ALContext->Listener.Position, sizeof(ALContext->Listener.Position));
186 memcpy(ListenerVelocity, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
187 memcpy(&ListenerOrientation[0], ALContext->Listener.Forward, sizeof(ALContext->Listener.Forward));
188 memcpy(&ListenerOrientation[3], ALContext->Listener.Up, sizeof(ALContext->Listener.Up));
190 //Get source properties
191 Pitch = ALSource->flPitch;
192 SourceVolume = ALSource->flGain;
193 memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
194 memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
195 memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
196 MinVolume = ALSource->flMinGain;
197 MaxVolume = ALSource->flMaxGain;
198 MinDist = ALSource->flRefDistance;
199 MaxDist = ALSource->flMaxDistance;
200 Rolloff = ALSource->flRollOffFactor;
201 OuterGain = ALSource->flOuterGain;
202 InnerAngle = ALSource->flInnerAngle;
203 OuterAngle = ALSource->flOuterAngle;
204 HeadRelative = ALSource->bHeadRelative;
206 //Set working variables
207 DryMix = (ALfloat)(1.0f);
208 WetMix = (ALfloat)(0.0f);
210 //Only apply 3D calculations for mono buffers
211 if(isMono != AL_FALSE)
213 //1. Translate Listener to origin (convert to head relative)
214 if(HeadRelative==AL_FALSE)
216 Position[0] -= ListenerPosition[0];
217 Position[1] -= ListenerPosition[1];
218 Position[2] -= ListenerPosition[2];
221 //2. Calculate distance attenuation
222 Distance = aluSqrt(aluDotproduct(Position, Position));
224 flAttenuation = 1.0f;
225 switch (DistanceModel)
227 case AL_INVERSE_DISTANCE_CLAMPED:
228 Distance=__max(Distance,MinDist);
229 Distance=__min(Distance,MaxDist);
230 if (MaxDist < MinDist)
231 break;
232 //fall-through
233 case AL_INVERSE_DISTANCE:
234 if (MinDist > 0.0f)
236 if ((MinDist + (Rolloff * (Distance - MinDist))) > 0.0f)
237 flAttenuation = MinDist / (MinDist + (Rolloff * (Distance - MinDist)));
239 break;
241 case AL_LINEAR_DISTANCE_CLAMPED:
242 Distance=__max(Distance,MinDist);
243 Distance=__min(Distance,MaxDist);
244 if (MaxDist < MinDist)
245 break;
246 //fall-through
247 case AL_LINEAR_DISTANCE:
248 Distance=__min(Distance,MaxDist);
249 if (MaxDist != MinDist)
250 flAttenuation = 1.0f - (Rolloff*(Distance-MinDist)/(MaxDist - MinDist));
251 break;
253 case AL_EXPONENT_DISTANCE_CLAMPED:
254 Distance=__max(Distance,MinDist);
255 Distance=__min(Distance,MaxDist);
256 if (MaxDist < MinDist)
257 break;
258 //fall-through
259 case AL_EXPONENT_DISTANCE:
260 if ((Distance > 0.0f) && (MinDist > 0.0f))
261 flAttenuation = (ALfloat)pow(Distance/MinDist, -Rolloff);
262 break;
264 case AL_NONE:
265 default:
266 flAttenuation = 1.0f;
267 break;
270 // Source Gain + Attenuation
271 DryMix = SourceVolume * flAttenuation;
273 // Clamp to Min/Max Gain
274 DryMix = __min(DryMix,MaxVolume);
275 DryMix = __max(DryMix,MinVolume);
276 WetMix = __min(WetMix,MaxVolume);
277 WetMix = __max(WetMix,MinVolume);
278 //3. Apply directional soundcones
279 SourceToListener[0] = -Position[0];
280 SourceToListener[1] = -Position[1];
281 SourceToListener[2] = -Position[2];
282 aluNormalize(Direction);
283 aluNormalize(SourceToListener);
284 Angle = (ALfloat)(180.0*acos(aluDotproduct(Direction,SourceToListener))/3.141592654f);
285 if(Angle >= InnerAngle && Angle <= OuterAngle)
286 ConeVolume = (1.0f+(OuterGain-1.0f)*(Angle-InnerAngle)/(OuterAngle-InnerAngle));
287 else if(Angle > OuterAngle)
288 ConeVolume = (1.0f+(OuterGain-1.0f) );
289 else
290 ConeVolume = 1.0f;
292 //4. Calculate Velocity
293 if(DopplerFactor != 0.0f)
295 flVLS = aluDotproduct(ListenerVelocity, SourceToListener);
296 flVSS = aluDotproduct(Velocity, SourceToListener);
298 flMaxVelocity = (DopplerVelocity * flSpeedOfSound) / DopplerFactor;
300 if (flVSS >= flMaxVelocity)
301 flVSS = (flMaxVelocity - 1.0f);
302 else if (flVSS <= -flMaxVelocity)
303 flVSS = -flMaxVelocity + 1.0f;
305 if (flVLS >= flMaxVelocity)
306 flVLS = (flMaxVelocity - 1.0f);
307 else if (flVLS <= -flMaxVelocity)
308 flVLS = -flMaxVelocity + 1.0f;
310 pitch[0] = Pitch * ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVLS)) /
311 ((flSpeedOfSound * DopplerVelocity) - (DopplerFactor * flVSS));
313 else
314 pitch[0] = Pitch;
316 //5. Align coordinate system axes
317 aluCrossproduct(&ListenerOrientation[0], &ListenerOrientation[3], U); // Right-vector
318 aluNormalize(U); // Normalized Right-vector
319 memcpy(V, &ListenerOrientation[3], sizeof(V)); // Up-vector
320 aluNormalize(V); // Normalized Up-vector
321 memcpy(N, &ListenerOrientation[0], sizeof(N)); // At-vector
322 aluNormalize(N); // Normalized At-vector
323 Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0];
324 Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1];
325 Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2];
326 aluMatrixVector(Position, Matrix);
328 //6. Convert normalized position into pannings, then into channel volumes
329 aluNormalize(Position);
330 switch(aluChannelsFromFormat(OutputFormat))
332 case 1:
333 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
334 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f); //Direct
335 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
336 wetsend[1] = ListenerGain * WetMix * aluSqrt(1.0f); //Room
337 break;
338 case 2:
339 PanningLR = 0.5f + 0.5f*Position[0];
340 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt(1.0f-PanningLR); //L Direct
341 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt( PanningLR); //R Direct
342 wetsend[0] = ListenerGain * WetMix * aluSqrt(1.0f-PanningLR); //L Room
343 wetsend[1] = ListenerGain * WetMix * aluSqrt( PanningLR); //R Room
344 break;
345 case 4:
346 /* TODO: Add center/lfe channel in spatial calculations? */
347 case 6:
348 // Apply a scalar so each individual speaker has more weight
349 PanningLR = 0.5f + (0.5f*Position[0]*1.41421356f);
350 PanningLR = __min(1.0f, PanningLR);
351 PanningLR = __max(0.0f, PanningLR);
352 PanningFB = 0.5f + (0.5f*Position[2]*1.41421356f);
353 PanningFB = __min(1.0f, PanningFB);
354 PanningFB = __max(0.0f, PanningFB);
355 drysend[0] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Direct
356 drysend[1] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Direct
357 drysend[2] = ConeVolume * ListenerGain * DryMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Direct
358 drysend[3] = ConeVolume * ListenerGain * DryMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Direct
359 wetsend[0] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*(1.0f-PanningFB)); //FL Room
360 wetsend[1] = ListenerGain * WetMix * aluSqrt(( PanningLR)*(1.0f-PanningFB)); //FR Room
361 wetsend[2] = ListenerGain * WetMix * aluSqrt((1.0f-PanningLR)*( PanningFB)); //BL Room
362 wetsend[3] = ListenerGain * WetMix * aluSqrt(( PanningLR)*( PanningFB)); //BR Room
363 break;
364 default:
365 break;
368 else
370 //1. Multi-channel buffers always play "normal"
371 drysend[0] = SourceVolume * 1.0f * ListenerGain;
372 drysend[1] = SourceVolume * 1.0f * ListenerGain;
373 drysend[2] = SourceVolume * 1.0f * ListenerGain;
374 drysend[3] = SourceVolume * 1.0f * ListenerGain;
375 drysend[4] = SourceVolume * 1.0f * ListenerGain;
376 drysend[5] = SourceVolume * 1.0f * ListenerGain;
377 wetsend[0] = SourceVolume * 0.0f * ListenerGain;
378 wetsend[1] = SourceVolume * 0.0f * ListenerGain;
379 wetsend[2] = SourceVolume * 0.0f * ListenerGain;
380 wetsend[3] = SourceVolume * 0.0f * ListenerGain;
381 wetsend[4] = SourceVolume * 0.0f * ListenerGain;
382 wetsend[5] = SourceVolume * 0.0f * ListenerGain;
384 pitch[0] = Pitch;
388 ALvoid aluMixData(ALCcontext *ALContext,ALvoid *buffer,ALsizei size,ALenum format)
390 static float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
391 static float WetBuffer[BUFFERSIZE][OUTPUTCHANNELS];
392 ALfloat DrySend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
393 ALfloat WetSend[OUTPUTCHANNELS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
394 ALuint BlockAlign,BufferSize;
395 ALuint DataSize=0,DataPosInt=0,DataPosFrac=0;
396 ALuint Channels,Bits,Frequency,ulExtraSamples;
397 ALfloat Pitch;
398 ALint Looping,increment,State;
399 ALuint Buffer,fraction;
400 ALuint SamplesToDo;
401 ALsource *ALSource;
402 ALbuffer *ALBuffer;
403 ALfloat value;
404 ALshort *Data;
405 ALuint i,j,k;
406 ALbufferlistitem *BufferListItem;
407 ALuint loop;
408 ALint64 DataSize64,DataPos64;
410 SuspendContext(ALContext);
412 if(buffer)
414 //Figure output format variables
415 BlockAlign = aluChannelsFromFormat(format);
416 BlockAlign *= aluBytesFromFormat(format);
418 size /= BlockAlign;
419 while(size > 0)
421 //Setup variables
422 ALSource = (ALContext ? ALContext->Source : NULL);
423 SamplesToDo = min(size, BUFFERSIZE);
425 //Clear mixing buffer
426 memset(DryBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
427 memset(WetBuffer, 0, SamplesToDo*OUTPUTCHANNELS*sizeof(ALfloat));
429 //Actual mixing loop
430 while(ALSource)
432 j = 0;
433 State = ALSource->state;
434 while(State == AL_PLAYING && j < SamplesToDo)
436 DataSize = 0;
437 DataPosInt = 0;
438 DataPosFrac = 0;
440 //Get buffer info
441 if((Buffer = ALSource->ulBufferID))
443 ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer);
445 Data = ALBuffer->data;
446 Bits = aluBytesFromFormat(ALBuffer->format) * 8;
447 Channels = aluChannelsFromFormat(ALBuffer->format);
448 DataSize = ALBuffer->size;
449 Frequency = ALBuffer->frequency;
451 CalcSourceParams(ALContext, ALSource,
452 (Channels==1) ? AL_TRUE : AL_FALSE,
453 format, DrySend, WetSend, &Pitch);
456 Pitch = (Pitch*Frequency) / ALContext->Frequency;
457 DataSize = DataSize / (Bits*Channels/8);
459 //Get source info
460 DataPosInt = ALSource->position;
461 DataPosFrac = ALSource->position_fraction;
463 //Compute 18.14 fixed point step
464 increment = aluF2L(Pitch*(1L<<FRACTIONBITS));
465 if(increment > (MAX_PITCH<<FRACTIONBITS))
466 increment = (MAX_PITCH<<FRACTIONBITS);
468 //Figure out how many samples we can mix.
469 //Pitch must be <= 4 (the number below !)
470 DataSize64 = DataSize+MAX_PITCH;
471 DataSize64 <<= FRACTIONBITS;
472 DataPos64 = DataPosInt;
473 DataPos64 <<= FRACTIONBITS;
474 DataPos64 += DataPosFrac;
475 BufferSize = (ALuint)((DataSize64-DataPos64) / increment);
476 BufferListItem = ALSource->queue;
477 for(loop = 0; loop < ALSource->BuffersPlayed; loop++)
479 if(BufferListItem)
480 BufferListItem = BufferListItem->next;
482 if (BufferListItem)
484 if (BufferListItem->next)
486 if(BufferListItem->next->buffer &&
487 ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data)
489 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->size, (ALint)(16*Channels));
490 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer))->data, ulExtraSamples);
493 else if (ALSource->bLooping)
495 if (ALSource->queue->buffer)
497 if(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data)
499 ulExtraSamples = min(((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->size, (ALint)(16*Channels));
500 memcpy(&Data[DataSize*Channels], ((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer))->data, ulExtraSamples);
505 BufferSize = min(BufferSize, (SamplesToDo-j));
507 //Actual sample mixing loop
508 Data += DataPosInt*Channels;
509 while(BufferSize--)
511 k = DataPosFrac>>FRACTIONBITS;
512 fraction = DataPosFrac&FRACTIONMASK;
513 if(Channels==1)
515 //First order interpolator
516 value = (ALfloat)((ALshort)(((Data[k]*((1L<<FRACTIONBITS)-fraction))+(Data[k+1]*(fraction)))>>FRACTIONBITS));
517 //Direct path final mix buffer and panning
518 DryBuffer[j][0] += value*DrySend[0];
519 DryBuffer[j][1] += value*DrySend[1];
520 DryBuffer[j][2] += value*DrySend[2];
521 DryBuffer[j][3] += value*DrySend[3];
522 //Room path final mix buffer and panning
523 WetBuffer[j][0] += value*WetSend[0];
524 WetBuffer[j][1] += value*WetSend[1];
525 WetBuffer[j][2] += value*WetSend[2];
526 WetBuffer[j][3] += value*WetSend[3];
528 else
530 //First order interpolator (left)
531 value = (ALfloat)((ALshort)(((Data[k*2 ]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+2]*(fraction)))>>FRACTIONBITS));
532 //Direct path final mix buffer and panning (left)
533 DryBuffer[j][0] += value*DrySend[0];
534 //Room path final mix buffer and panning (left)
535 WetBuffer[j][0] += value*WetSend[0];
536 //First order interpolator (right)
537 value = (ALfloat)((ALshort)(((Data[k*2+1]*((1L<<FRACTIONBITS)-fraction))+(Data[k*2+3]*(fraction)))>>FRACTIONBITS));
538 //Direct path final mix buffer and panning (right)
539 DryBuffer[j][1] += value*DrySend[1];
540 //Room path final mix buffer and panning (right)
541 WetBuffer[j][1] += value*WetSend[1];
543 DataPosFrac += increment;
544 j++;
546 DataPosInt += (DataPosFrac>>FRACTIONBITS);
547 DataPosFrac = (DataPosFrac&FRACTIONMASK);
549 //Update source info
550 ALSource->position = DataPosInt;
551 ALSource->position_fraction = DataPosFrac;
554 //Handle looping sources
555 if(!Buffer || DataPosInt >= DataSize)
557 //queueing
558 if(ALSource->queue)
560 Looping = ALSource->bLooping;
561 if(ALSource->BuffersPlayed < (ALSource->BuffersInQueue-1))
563 BufferListItem = ALSource->queue;
564 for(loop = 0; loop <= ALSource->BuffersPlayed; loop++)
566 if(BufferListItem)
568 if(!Looping)
569 BufferListItem->bufferstate = PROCESSED;
570 BufferListItem = BufferListItem->next;
573 if(!Looping)
574 ALSource->BuffersProcessed++;
575 if(BufferListItem)
576 ALSource->ulBufferID = BufferListItem->buffer;
577 ALSource->position = DataPosInt-DataSize;
578 ALSource->position_fraction = DataPosFrac;
579 ALSource->BuffersPlayed++;
581 else
583 if(!Looping)
585 /* alSourceStop */
586 ALSource->state = AL_STOPPED;
587 ALSource->inuse = AL_FALSE;
588 ALSource->BuffersPlayed = ALSource->BuffersProcessed = ALSource->BuffersInQueue;
589 BufferListItem = ALSource->queue;
590 while(BufferListItem != NULL)
592 BufferListItem->bufferstate = PROCESSED;
593 BufferListItem = BufferListItem->next;
596 else
598 /* alSourceRewind */
599 /* alSourcePlay */
600 ALSource->state = AL_PLAYING;
601 ALSource->inuse = AL_TRUE;
602 ALSource->play = AL_TRUE;
603 ALSource->BuffersPlayed = 0;
604 ALSource->BufferPosition = 0;
605 ALSource->lBytesPlayed = 0;
606 ALSource->BuffersProcessed = 0;
607 BufferListItem = ALSource->queue;
608 while(BufferListItem != NULL)
610 BufferListItem->bufferstate = PENDING;
611 BufferListItem = BufferListItem->next;
613 ALSource->ulBufferID = ALSource->queue->buffer;
615 ALSource->position = DataPosInt-DataSize;
616 ALSource->position_fraction = DataPosFrac;
622 //Get source state
623 State = ALSource->state;
626 ALSource = ALSource->next;
629 //Post processing loop
630 switch(format)
632 case AL_FORMAT_MONO8:
633 for(i = 0;i < SamplesToDo;i++)
635 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1])>>8)+128);
636 buffer = ((ALubyte*)buffer) + 1;
638 break;
639 case AL_FORMAT_STEREO8:
640 for(i = 0;i < SamplesToDo*2;i++)
642 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1])>>8)+128);
643 buffer = ((ALubyte*)buffer) + 1;
645 break;
646 case AL_FORMAT_QUAD8:
647 for(i = 0;i < SamplesToDo*4;i++)
649 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3])>>8)+128);
650 buffer = ((ALubyte*)buffer) + 1;
652 break;
653 case AL_FORMAT_51CHN8:
654 for(i = 0;i < SamplesToDo*6;i++)
656 *((ALubyte*)buffer) = (ALubyte)((aluF2S(DryBuffer[i/6][i%6]+WetBuffer[i/6][i%6])>>8)+128);
657 buffer = ((ALubyte*)buffer) + 1;
659 break;
660 case AL_FORMAT_MONO16:
661 for(i = 0;i < SamplesToDo;i++)
663 *((ALshort*)buffer) = aluF2S(DryBuffer[i][0]+DryBuffer[i][1]+WetBuffer[i][0]+WetBuffer[i][1]);
664 buffer = ((ALshort*)buffer) + 1;
666 break;
667 case AL_FORMAT_STEREO16:
668 default:
669 for(i = 0;i < SamplesToDo*2;i++)
671 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>1][i&1]+WetBuffer[i>>1][i&1]);
672 buffer = ((ALshort*)buffer) + 1;
674 break;
675 case AL_FORMAT_QUAD16:
676 for(i = 0;i < SamplesToDo*4;i++)
678 *((ALshort*)buffer) = aluF2S(DryBuffer[i>>2][i&3]+WetBuffer[i>>2][i&3]);
679 buffer = ((ALshort*)buffer) + 1;
681 break;
682 case AL_FORMAT_51CHN16:
683 for(i = 0;i < SamplesToDo*6;i++)
685 *((ALshort*)buffer) = aluF2S(DryBuffer[i/6][i%6]+WetBuffer[i/6][i%6]);
686 buffer = ((ALshort*)buffer) + 1;
688 break;
691 size -= SamplesToDo;
695 ProcessContext(ALContext);